[Python-checkins] python/dist/src/Objects typeobject.c,2.209,2.210

gvanrossum@users.sourceforge.net gvanrossum@users.sourceforge.net
Thu, 13 Feb 2003 08:24:37 -0800


Update of /cvsroot/python/python/dist/src/Objects
In directory sc8-pr-cvs1:/tmp/cvs-serv19269

Modified Files:
	typeobject.c 
Log Message:
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__.


Index: typeobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/typeobject.c,v
retrieving revision 2.209
retrieving revision 2.210
diff -C2 -d -r2.209 -r2.210
*** typeobject.c	12 Feb 2003 03:58:38 -0000	2.209
--- typeobject.c	13 Feb 2003 16:24:34 -0000	2.210
***************
*** 2252,2255 ****
--- 2252,2273 ----
  }
  
+ /* 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)
***************
*** 2488,2492 ****
  	object_init,				/* tp_init */
  	PyType_GenericAlloc,			/* tp_alloc */
! 	PyType_GenericNew,			/* tp_new */
  	PyObject_Del,           		/* tp_free */
  };
--- 2506,2510 ----
  	object_init,				/* tp_init */
  	PyType_GenericAlloc,			/* tp_alloc */
! 	object_new,				/* tp_new */
  	PyObject_Del,           		/* tp_free */
  };