[pypy-svn] r52102 - in pypy/dist/pypy: interpreter objspace/std objspace/std/test

arigo at codespeak.net arigo at codespeak.net
Mon Mar 3 19:35:12 CET 2008


Author: arigo
Date: Mon Mar  3 19:35:11 2008
New Revision: 52102

Modified:
   pypy/dist/pypy/interpreter/baseobjspace.py
   pypy/dist/pypy/objspace/std/test/test_typeobject.py
   pypy/dist/pypy/objspace/std/typeobject.py
Log:
issue334 in-progress

Give a RuntimeWarning when adding a __del__ method to
a new-style class that didn't have one previously.


Modified: pypy/dist/pypy/interpreter/baseobjspace.py
==============================================================================
--- pypy/dist/pypy/interpreter/baseobjspace.py	(original)
+++ pypy/dist/pypy/interpreter/baseobjspace.py	Mon Mar  3 19:35:11 2008
@@ -916,6 +916,12 @@
                                  self.wrap('cannot convert negative integer '
                                            'to unsigned int'))
 
+    def warn(self, msg, w_warningcls):
+        self.appexec([self.wrap(msg), w_warningcls], """(msg, warningcls):
+            import warnings
+            warnings.warn(msg, warningcls, stacklevel=2)
+        """)
+
 
 class AppExecCache(SpaceCache):
     def build(cache, source):

Modified: pypy/dist/pypy/objspace/std/test/test_typeobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/test/test_typeobject.py	(original)
+++ pypy/dist/pypy/objspace/std/test/test_typeobject.py	Mon Mar  3 19:35:11 2008
@@ -76,6 +76,39 @@
             raises(TypeError, type, 'sub', (stufftype,), {})
         """)
 
+    def test_del_warning(self):
+        warnings = []
+        def my_warn(msg, warningscls):
+            warnings.append(msg)
+            prev_warn(msg, warningscls)
+        space = self.space
+        prev_warn = space.warn
+        try:
+            space.warn = my_warn
+            space.appexec([], """():
+                class X(object):
+                    pass
+                X.__del__ = 5
+                X.__del__ = 6
+                X.__del__ = 7
+                class Y(object):
+                    pass
+                Y.__del__ = 8
+                Y.__del__ = 9
+                Y.__del__ = 0
+                class Z(object):
+                    pass
+                Z._foobar_ = 3
+                Z._foobar_ = 4
+                class U(object):
+                    def __del__(self):
+                        pass
+                U.__del__ = lambda self: 42     # no warning here
+            """)
+        finally:
+            space.warn = prev_warn
+        assert len(warnings) == 2
+
 
 class AppTestTypeObject:
     def test_bases(self):

Modified: pypy/dist/pypy/objspace/std/typeobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/typeobject.py	(original)
+++ pypy/dist/pypy/objspace/std/typeobject.py	Mon Mar  3 19:35:11 2008
@@ -551,6 +551,9 @@
     if not w_type.is_heaptype():
         msg = "can't set attributes on type object '%s'" %(w_type.name,)
         raise OperationError(space.w_TypeError, space.wrap(msg))
+    if name == "__del__" and name not in w_type.dict_w:
+        msg = "a __del__ method added to an existing type will not be called"
+        space.warn(msg, space.w_RuntimeWarning)
     w_type.dict_w[name] = w_value
 
 def delattr__Type_ANY(space, w_type, w_name):


More information about the pypy-svn mailing list