[Python-checkins] r63761 - in python/branches/okkoto-sizeof: Lib/test/test_sys.py Objects/typeobject.c Python/sysmodule.c

robert.schuppenies python-checkins at python.org
Wed May 28 17:52:55 CEST 2008


Author: robert.schuppenies
Date: Wed May 28 17:52:55 2008
New Revision: 63761

Log:
going for the less-than-ideal solution: special case handling in 
sys.getsizeof() and type.__sizeof__() as CLASS_METH


Modified:
   python/branches/okkoto-sizeof/Lib/test/test_sys.py
   python/branches/okkoto-sizeof/Objects/typeobject.c
   python/branches/okkoto-sizeof/Python/sysmodule.c

Modified: python/branches/okkoto-sizeof/Lib/test/test_sys.py
==============================================================================
--- python/branches/okkoto-sizeof/Lib/test/test_sys.py	(original)
+++ python/branches/okkoto-sizeof/Lib/test/test_sys.py	Wed May 28 17:52:55 2008
@@ -406,12 +406,9 @@
 
     def check_sizeof(self, o, size, ):
         size += self.headersize
-        if not inspect.isclass(o):
-            result = sys.getsizeof(o)
-        else:
-            result = o.__sizeof__(o)
-        msg = 'wrong size for ' + str(type(o)) + ': '+\
-              str(result) + ' != ' + str(size)
+        result = sys.getsizeof(o)
+        msg = 'wrong size for ' + str(type(o)) + ': got '+\
+              str(result) + ', expected ' + str(size)
         self.assertEqual(result, size, msg)
 
     def align(self, value):
@@ -448,15 +445,15 @@
                 return x
             return inner
         self.check_sizeof(get_cell().func_closure[0], p)
-        # class
-        class clazz():
+        # old-style class
+        class class_oldstyle():
             def method():
                 pass
- #       self.check_sizeof(clazz, 6*p)
+        self.check_sizeof(class_oldstyle, 6*p)
         # instance
-#        self.check_sizeof(clazz(), 3*p)
+        self.check_sizeof(class_oldstyle(), 3*p)
         # method
-        self.check_sizeof(clazz().method, 4*p)
+        self.check_sizeof(class_oldstyle().method, 4*p)
         # code
         self.check_sizeof(get_cell().func_code, self.align(4*i) + 8*p +\
                             self.align(i) + 2*p)
@@ -504,6 +501,15 @@
         l = self.l
         p = self.p
         self.headersize += l
+        # new-style class
+        class class_newstyle(object):
+            def method():
+                pass
+        # type (PyTypeObject + PyNumberMethods +  PyMappingMethods +
+        #       PySequenceMethods +  PyBufferProcs)
+        len_typeobject = p + 2*l + 15*p + l + 4*p + l + 9*p + l + 11*p
+        self.check_sizeof(class_newstyle, \
+                              len_typeobject + 42*p + 10*p + 3*p + 6*p)
         # string
         self.check_sizeof('', l + self.align(i + 1))
         self.check_sizeof('abc', l + self.align(i + 1) + 3)

Modified: python/branches/okkoto-sizeof/Objects/typeobject.c
==============================================================================
--- python/branches/okkoto-sizeof/Objects/typeobject.c	(original)
+++ python/branches/okkoto-sizeof/Objects/typeobject.c	Wed May 28 17:52:55 2008
@@ -2620,7 +2620,7 @@
 	 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,
+	{"__sizeof__", type_sizeof, METH_NOARGS | METH_CLASS,
          PyDoc_STR("default sizeof implementation for type")},
 	{0}
 };

Modified: python/branches/okkoto-sizeof/Python/sysmodule.c
==============================================================================
--- python/branches/okkoto-sizeof/Python/sysmodule.c	(original)
+++ python/branches/okkoto-sizeof/Python/sysmodule.c	Wed May 28 17:52:55 2008
@@ -642,7 +642,18 @@
 static PyObject *
 sys_getsizeof(PyObject *self, PyObject *args)
 {
-	return PyObject_CallMethod(args, "__sizeof__", NULL);
+	/* 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);
+	}
+	else if (args->ob_type == &PyClass_Type) {
+		return PyInt_FromSsize_t(PyClass_Type.tp_basicsize);
+	}
+	else if (args->ob_type == &PyInstance_Type) {
+		return PyInt_FromSsize_t(PyInstance_Type.tp_basicsize);
+	}
+	else 
+		return PyObject_CallMethod(args, "__sizeof__", NULL);
 }
 
 PyDoc_STRVAR(getsizeof_doc,


More information about the Python-checkins mailing list