summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2003-02-13 16:30:16 (GMT)
committerGuido van Rossum <guido@python.org>2003-02-13 16:30:16 (GMT)
commit298e4214538a7196c27ec22b1f01506fdb3c4039 (patch)
tree6d75d061df43965e30b132c378802c5488fd8f64 /Objects
parent0c016a9590b3da47f19420d0616e0c72cae19abf (diff)
downloadcpython-298e4214538a7196c27ec22b1f01506fdb3c4039.zip
cpython-298e4214538a7196c27ec22b1f01506fdb3c4039.tar.gz
cpython-298e4214538a7196c27ec22b1f01506fdb3c4039.tar.bz2
SF patch #685738 by Michael Stone.
This changes the default __new__ to refuse arguments iff tp_init is the default __init__ implementation -- thus making it a TypeError when you try to pass arguments to a constructor if the class doesn't override at least __init__ or __new__.
Diffstat (limited to 'Objects')
-rw-r--r--Objects/typeobject.c20
1 files changed, 19 insertions, 1 deletions
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index 7a8bcd9..eb2e08f 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -2251,6 +2251,24 @@ object_init(PyObject *self, PyObject *args, PyObject *kwds)
return 0;
}
+/* If we don't have a tp_new for a new-style class, new will use this one.
+ Therefore this should take no arguments/keywords. However, this new may
+ also be inherited by objects that define a tp_init but no tp_new. These
+ objects WILL pass argumets to tp_new, because it gets the same args as
+ tp_init. So only allow arguments if we aren't using the default init, in
+ which case we expect init to handle argument parsing. */
+static PyObject *
+object_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
+{
+ if (type->tp_init == object_init && (PyTuple_GET_SIZE(args) ||
+ (kwds && PyDict_Check(kwds) && PyDict_Size(kwds)))) {
+ PyErr_SetString(PyExc_TypeError,
+ "default __new__ takes no parameters");
+ return NULL;
+ }
+ return type->tp_alloc(type, 0);
+}
+
static void
object_dealloc(PyObject *self)
{
@@ -2487,7 +2505,7 @@ PyTypeObject PyBaseObject_Type = {
0, /* tp_dictoffset */
object_init, /* tp_init */
PyType_GenericAlloc, /* tp_alloc */
- PyType_GenericNew, /* tp_new */
+ object_new, /* tp_new */
PyObject_Del, /* tp_free */
};