]> granicus.if.org Git - python/commitdiff
dynamics(): add tests for dynamic *instances* (which are currently
authorGuido van Rossum <guido@python.org>
Sun, 12 Aug 2001 03:38:18 +0000 (03:38 +0000)
committerGuido van Rossum <guido@python.org>
Sun, 12 Aug 2001 03:38:18 +0000 (03:38 +0000)
broken).  Also fix an invalid reference to C (should be S).

Lib/test/test_descr.py

index 737fce9b719773034fd7835969f673c247f0fe3a..2e934b7dc279878a0e0807537fc956f21d1bb30e 100644 (file)
@@ -550,7 +550,7 @@ def dynamics():
     verify(S1.__dynamic__ == 0)
     class S(object):
         pass
-    verify(C.__dynamic__ == 0)
+    verify(S.__dynamic__ == 0)
     class D(object):
         __dynamic__ = 1
     verify(D.__dynamic__ == 1)
@@ -581,6 +581,25 @@ def dynamics():
         pass
     else:
         verify(0, "assignment to SS.foo should be illegal")
+    # Test dynamic instances
+    class C(object):
+        __dynamic__ = 1
+        foobar = 1
+        def __repr__(self):
+            return "<C object>"
+    a = C()
+    verify(not hasattr(a, "spam"))
+    verify(a.foobar == 1)
+    C.foobar = 2
+    verify(a.foobar == 2)
+    C.method = lambda self: 42
+    verify(a.method() == 42)
+    verify(repr(a) == "<C object>")
+    C.__repr__ = lambda self: "C()"
+    verify(repr(a) == "C()")
+    # The following test should succeed, but doesn't yet
+##    C.__int__ = lambda self: 100
+##    verify(int(a) == 100)
 
 def errors():
     if verbose: print "Testing errors..."