[Python-checkins] cpython: make extra arguments to object.__init__/__new__ to errors in most cases

benjamin.peterson python-checkins at python.org
Sat Mar 17 06:06:17 CET 2012


http://hg.python.org/cpython/rev/25b71858cb14
changeset:   75773:25b71858cb14
parent:      75770:561fc3b4cc2a
user:        Benjamin Peterson <benjamin at python.org>
date:        Sat Mar 17 00:05:44 2012 -0500
summary:
  make extra arguments to object.__init__/__new__ to errors in most cases (finishes #1683368)

files:
  Lib/test/test_descr.py |  20 +++++++++++++
  Misc/NEWS              |   3 ++
  Objects/typeobject.c   |  45 ++++++-----------------------
  3 files changed, 33 insertions(+), 35 deletions(-)


diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py
--- a/Lib/test/test_descr.py
+++ b/Lib/test/test_descr.py
@@ -4502,6 +4502,26 @@
         for o in gc.get_objects():
             self.assertIsNot(type(o), X)
 
+    def test_object_new_and_init_with_parameters(self):
+        # See issue #1683368
+        class OverrideNeither:
+            pass
+        self.assertRaises(TypeError, OverrideNeither, 1)
+        self.assertRaises(TypeError, OverrideNeither, kw=1)
+        class OverrideNew:
+            def __new__(cls, foo, kw=0, *args, **kwds):
+                return object.__new__(cls, *args, **kwds)
+        class OverrideInit:
+            def __init__(self, foo, kw=0, *args, **kwargs):
+                return object.__init__(self, *args, **kwargs)
+        class OverrideBoth(OverrideNew, OverrideInit):
+            pass
+        for case in OverrideNew, OverrideInit, OverrideBoth:
+            case(1)
+            case(1, kw=2)
+            self.assertRaises(TypeError, case, 1, 2, 3)
+            self.assertRaises(TypeError, case, 1, 2, foo=3)
+
 
 class DictProxyTests(unittest.TestCase):
     def setUp(self):
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@
 Core and Builtins
 -----------------
 
+- Issue #1683368: object.__new__ and object.__init__ raise a TypeError if they
+  are passed arguments and their complementary method is not overridden.
+
 - Give the ast.AST class a __dict__.
 
 - Issue #14334: Prevent in a segfault in type.__getattribute__ when it was not
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -2905,22 +2905,11 @@
 object_init(PyObject *self, PyObject *args, PyObject *kwds)
 {
     int err = 0;
-    if (excess_args(args, kwds)) {
-        PyTypeObject *type = Py_TYPE(self);
-        if (type->tp_init != object_init &&
-            type->tp_new != object_new)
-        {
-            err = PyErr_WarnEx(PyExc_DeprecationWarning,
-                       "object.__init__() takes no parameters",
-                       1);
-        }
-        else if (type->tp_init != object_init ||
-                 type->tp_new == object_new)
-        {
-            PyErr_SetString(PyExc_TypeError,
-                "object.__init__() takes no parameters");
-            err = -1;
-        }
+    PyTypeObject *type = Py_TYPE(self);
+    if (excess_args(args, kwds) &&
+        (type->tp_new == object_new || type->tp_init != object_init)) {
+        PyErr_SetString(PyExc_TypeError, "object.__init__() takes no parameters");
+        err = -1;
     }
     return err;
 }
@@ -2928,26 +2917,12 @@
 static PyObject *
 object_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
 {
-    int err = 0;
-    if (excess_args(args, kwds)) {
-        if (type->tp_new != object_new &&
-            type->tp_init != object_init)
-        {
-            err = PyErr_WarnEx(PyExc_DeprecationWarning,
-                       "object.__new__() takes no parameters",
-                       1);
-        }
-        else if (type->tp_new != object_new ||
-                 type->tp_init == object_init)
-        {
-            PyErr_SetString(PyExc_TypeError,
-                "object.__new__() takes no parameters");
-            err = -1;
-        }
-    }
-    if (err < 0)
+    if (excess_args(args, kwds) &&
+        (type->tp_init == object_init || type->tp_new != object_new)) {
+        PyErr_SetString(PyExc_TypeError, "object.__new__() takes no parameters");
         return NULL;
-
+    }
+ 
     if (type->tp_flags & Py_TPFLAGS_IS_ABSTRACT) {
         PyObject *abstract_methods = NULL;
         PyObject *builtins;

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list