[Python-checkins] r63785 - in python/branches/okkoto-sizeof: Objects/typeobject.c Python/sysmodule.c

robert.schuppenies python-checkins at python.org
Thu May 29 14:52:58 CEST 2008


Author: robert.schuppenies
Date: Thu May 29 14:52:57 2008
New Revision: 63785

Log:
Got rid of type_sizeof() method with C equivalent of type(obj).__sizeof__(obj).


Modified:
   python/branches/okkoto-sizeof/Objects/typeobject.c
   python/branches/okkoto-sizeof/Python/sysmodule.c

Modified: python/branches/okkoto-sizeof/Objects/typeobject.c
==============================================================================
--- python/branches/okkoto-sizeof/Objects/typeobject.c	(original)
+++ python/branches/okkoto-sizeof/Objects/typeobject.c	Thu May 29 14:52:57 2008
@@ -2608,19 +2608,11 @@
 	return list;
 }
 
-static PyObject *
-type_sizeof(PyObject *self, PyObject *args)
-{
-	return PyInt_FromSsize_t(self->ob_type->tp_basicsize);
-}
-
 static PyMethodDef type_methods[] = {
 	{"mro", (PyCFunction)mro_external, METH_NOARGS,
 	 PyDoc_STR("mro() -> list\nreturn a type's method resolution order")},
 	{"__subclasses__", (PyCFunction)type_subclasses, METH_NOARGS,
 	 PyDoc_STR("__subclasses__() -> list of immediate subclasses")},
-	{"__sizeof__", type_sizeof, METH_NOARGS | METH_CLASS,
-         PyDoc_STR("__sizeof__() -> size of type in bytes")},
 	{0}
 };
 

Modified: python/branches/okkoto-sizeof/Python/sysmodule.c
==============================================================================
--- python/branches/okkoto-sizeof/Python/sysmodule.c	(original)
+++ python/branches/okkoto-sizeof/Python/sysmodule.c	Thu May 29 14:52:57 2008
@@ -642,19 +642,35 @@
 static PyObject *
 sys_getsizeof(PyObject *self, PyObject *args)
 {
-	/* work-around to deal with objects which inherit from type, .. */
-	if (args->ob_type == &PyType_Type) {
-		return PyObject_CallMethod((PyObject *)(args->ob_type), "__sizeof__", NULL);
-	}
-	/* .. old-style classes,  */
-	else if (args->ob_type == &PyClass_Type) {
-		return PyInt_FromSsize_t(PyClass_Type.tp_basicsize);
+	static PyObject * str__sizeof__ = NULL;
+
+	/* Initialize static variable needed by _PyType_Lookup */
+	if (str__sizeof__ == NULL) {
+		str__sizeof__ = PyString_InternFromString("__sizeof__");
+		if (str__sizeof__ == NULL)
+			return NULL;
 	}
-	/* .. and instances of old-style classes */
-	else if (args->ob_type == &PyInstance_Type) {
+
+	/* Type objects */
+	if (PyType_Check(args)){
+		PyObject *method = _PyType_Lookup(Py_TYPE(args),
+						  str__sizeof__);
+		if (method == NULL) {
+			PyErr_Format(PyExc_TypeError,
+				     "Type %.100s doesn't define __sizeof__",
+				     Py_TYPE(args)->tp_name);
+			return NULL;
+		}
+		/* And call it, binding it to the value */
+		return PyObject_CallFunctionObjArgs(method, args, NULL);
+	} 
+	/* Instance of old-style classes */
+	else if(PyInstance_Check(args))
 		return PyInt_FromSsize_t(PyInstance_Type.tp_basicsize);
-	}
-	else 
+	/* Old-style class */
+	else if (PyClass_Check(args))
+		return PyInt_FromSsize_t(PyClass_Type.tp_basicsize);
+	else
 		return PyObject_CallMethod(args, "__sizeof__", NULL);
 }
 


More information about the Python-checkins mailing list