[pypy-svn] pypy default: (alex, armin, mitsuhiko): Provide warnings and exceptions when you try to pass arguments to object.__init__ in various ways.

alex_gaynor commits-noreply at bitbucket.org
Tue Mar 15 23:04:50 CET 2011


Author: Alex Gaynor <alex.gaynor at gmail.com>
Branch: 
Changeset: r42688:25584faa6ace
Date: 2011-03-15 18:04 -0400
http://bitbucket.org/pypy/pypy/changeset/25584faa6ace/

Log:	(alex, armin, mitsuhiko): Provide warnings and exceptions when you
	try to pass arguments to object.__init__ in various ways.

diff --git a/pypy/objspace/std/objecttype.py b/pypy/objspace/std/objecttype.py
--- a/pypy/objspace/std/objecttype.py
+++ b/pypy/objspace/std/objecttype.py
@@ -78,7 +78,20 @@
     return w_obj
 
 def descr__init__(space, w_obj, __args__):
-    pass
+    w_type = space.type(w_obj)
+    w_parent_new, _ = w_type.lookup_where('__new__')
+    w_parent_init, _ = w_type.lookup_where('__init__')
+    try:
+        __args__.fixedunpack(0)
+    except ValueError:
+        if w_parent_new is not space.w_object and w_parent_init is not space.w_object:
+            space.warn("object.__init__() takes no parameters", space.w_DeprecationWarning)
+        elif w_parent_new is space.w_object or w_parent_init is not space.w_object:
+            raise OperationError(space.w_TypeError,
+                space.wrap("object.__init__() takes no parameters")
+            )
+
+
 
 @gateway.unwrap_spec(proto=int)
 def descr__reduce__(space, w_obj, proto=0):

diff --git a/pypy/objspace/std/test/test_obj.py b/pypy/objspace/std/test/test_obj.py
--- a/pypy/objspace/std/test/test_obj.py
+++ b/pypy/objspace/std/test/test_obj.py
@@ -6,10 +6,10 @@
         import sys
         cpython_behavior = (not option.runappdirect
                             or not hasattr(sys, 'pypy_translation_info'))
-                
+
         cls.w_cpython_behavior = cls.space.wrap(cpython_behavior)
         cls.w_cpython_version = cls.space.wrap(tuple(sys.version_info))
-    
+
     def test_hash_builtin(self):
         if not self.cpython_behavior:
             skip("on pypy-c id == hash is not guaranteed")
@@ -21,7 +21,7 @@
 
     def test_hash_method(self):
         o = object()
-        assert hash(o) == o.__hash__() 
+        assert hash(o) == o.__hash__()
 
     def test_hash_list(self):
         l = range(5)
@@ -69,3 +69,26 @@
             pass
         assert x().__subclasshook__(object()) is NotImplemented
         assert x.__subclasshook__(object()) is NotImplemented
+
+    def test_object_init(self):
+        import warnings
+
+        class A(object):
+            pass
+
+        raises(TypeError, A().__init__, 3)
+        raises(TypeError, A().__init__, a=3)
+
+        class B(object):
+            def __new__(cls):
+                return super(B, cls).__new__(cls)
+
+            def __init__(self):
+                super(B, self).__init__(a=3)
+
+        with warnings.catch_warnings(record=True) as log:
+            warnings.simplefilter("always", DeprecationWarning)
+            B()
+        assert len(log) == 1
+        assert log[0].message.args == ("object.__init__() takes no parameters",)
+        assert type(log[0].message) is DeprecationWarning
\ No newline at end of file


More information about the Pypy-commit mailing list