Author: Ronan Lamy <[email protected]>
Branch: 
Changeset: r86608:de272690e3c2
Date: 2016-08-27 18:01 +0100
http://bitbucket.org/pypy/pypy/changeset/de272690e3c2/

Log:    Extract class_attr as a separate strategy

diff --git a/pypy/objspace/std/test/test_random_attr.py 
b/pypy/objspace/std/test/test_random_attr.py
--- a/pypy/objspace/std/test/test_random_attr.py
+++ b/pypy/objspace/std/test/test_random_attr.py
@@ -24,26 +24,25 @@
 attrnames = strategies.sampled_from(["a", "b", "c"])
 
 @strategies.composite
+def class_attr(draw):
+    what = draw(strategies.sampled_from(["value", "method", "property"]))
+    if what == "value":
+        val = draw(strategies.integers())
+        return val, str(val)
+    if what == "method":
+        val = draw(strategies.integers())
+        return (lambda self, val=val: val,
+                "lambda self: %d" % val)
+    if what == "property":
+        val = draw(strategies.integers())
+        return (property(lambda self, val=val: val,
+                            lambda self, val: None,
+                            lambda self: None),
+                "property(lambda self: %d, lambda self, val: None, lambda 
self: None)" % val)
+
[email protected]
 def make_code(draw):
-    # now here we can do this kind of thing:
     baseclass, initargs, hasdict = draw(base_initargs)
-    # and with arbitrary strategies
-
-    def class_attr():
-        what = draw(strategies.sampled_from(["value", "method", "property"]))
-        if what == "value":
-            val = draw(strategies.integers())
-            return val, str(val)
-        if what == "method":
-            val = draw(strategies.integers())
-            return (lambda self, val=val: val,
-                    "lambda self: %d" % val)
-        if what == "property":
-            val = draw(strategies.integers())
-            return (property(lambda self, val=val: val,
-                             lambda self, val: None,
-                             lambda self: None),
-                    "property(lambda self: %d, lambda self, val: None, lambda 
self: None)" % val)
 
     code = ["import sys", "class OldBase:pass", "class NewBase(object):pass", 
"class A(%s):" % baseclass]
     dct = {}
@@ -56,7 +55,7 @@
     for name in ["a", "b", "c"]:
         if not draw(strategies.booleans()):
             continue
-        dct[name], codeval = class_attr()
+        dct[name], codeval = draw(class_attr())
         code.append("    %s = %s" % (name, codeval))
     class OldBase: pass
     class NewBase(object): pass
@@ -100,11 +99,11 @@
             else:
                 code.append("a.%s = lambda : %s" % (attr, val))
         elif op == "writeclass":
-            val, codeval = class_attr()
+            val, codeval = draw(class_attr())
             setattr(cls, attr, val)
             code.append("A.%s = %s" % (attr, codeval))
         elif op == "writebase":
-            val, codeval = class_attr()
+            val, codeval = draw(class_attr())
             setattr(OldBase, attr, val)
             setattr(NewBase, attr, val)
             code.append("OldBase.%s = NewBase.%s = %s" % (attr, attr , 
codeval))
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to