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

robert.schuppenies python-checkins at python.org
Thu May 22 23:49:19 CEST 2008


Author: robert.schuppenies
Date: Thu May 22 23:49:19 2008
New Revision: 63539

Log:
implemented magic object method __sizeof__


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 22 23:49:19 2008
@@ -3398,6 +3398,26 @@
         return result;
 }
 
+static PyObject *
+object_sizeof(PyObject *self, PyObject *args)
+{
+	Py_ssize_t res, size;
+ 
+	res = 0;
+	size = self->ob_type->tp_itemsize;
+	if (size > 0) {
+		Py_ssize_t len;
+		len = PyObject_Size(self);
+		if (PyErr_Occurred())
+			return NULL;
+		if (len)
+			res += len * size;
+	}
+	res += self->ob_type->tp_basicsize;
+
+	return PyLong_FromLong(res);	 
+}
+
 static PyMethodDef object_methods[] = {
 	{"__reduce_ex__", object_reduce_ex, METH_VARARGS,
 	 PyDoc_STR("helper for pickle")},
@@ -3407,6 +3427,8 @@
 	 object_subclasshook_doc},
         {"__format__", object_format, METH_VARARGS,
          PyDoc_STR("default object formatter")},
+        {"__sizeof__", object_sizeof, METH_NOARGS,
+         PyDoc_STR("default object formatter")},
 	{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 22 23:49:19 2008
@@ -640,18 +640,19 @@
 #endif /* USE_MALLOPT */
 
 static PyObject *
-sys_sizeof(PyObject *self, PyObject *o)
+sys_sizeof(PyObject *self, PyObject *args)
 {
-	Py_ssize_t res;
-              
-	res = PyObject_Footprint(o);
+	PyObject *res;
+        
+	res = PyObject_CallMethod(args, "__sizeof__", NULL);
 	if (res < 0 && PyErr_Occurred())
 		return NULL;
-	return PyInt_FromSsize_t(res);
+
+	return res;
 }
 
 PyDoc_STRVAR(sizeof_doc,
-"sizeof(object) -> integer\n\
+"sizeof(object) -> long\n\
 \n\
 Return the memory footprint of an object in bytes.");
 


More information about the Python-checkins mailing list