Index: Python/sysmodule.c =================================================================== --- Python/sysmodule.c (revision 64547) +++ Python/sysmodule.c (working copy) @@ -651,8 +651,15 @@ return NULL; } + /* Make sure the type is initialized. float gets initialized late */ + if (PyType_Ready(Py_TYPE(args)) < 0) + return NULL; + + /* Instance of old-style classes */ + if (PyInstance_Check(args)) + return PyInt_FromSsize_t(PyInstance_Type.tp_basicsize); /* Type objects */ - if (PyType_Check(args)){ + else { PyObject *method = _PyType_Lookup(Py_TYPE(args), str__sizeof__); if (method == NULL) { @@ -662,15 +669,7 @@ return NULL; } return PyObject_CallFunctionObjArgs(method, args, NULL); - } - /* Instance of old-style classes */ - else if (PyInstance_Check(args)) - return PyInt_FromSsize_t(PyInstance_Type.tp_basicsize); - /* Old-style classes */ - else if (PyClass_Check(args)) - return PyInt_FromSsize_t(PyClass_Type.tp_basicsize); - else - return PyObject_CallMethod(args, "__sizeof__", NULL); + } } PyDoc_STRVAR(getsizeof_doc, Index: Lib/test/test_sys.py =================================================================== --- Lib/test/test_sys.py (revision 64547) +++ Lib/test/test_sys.py (working copy) @@ -559,7 +559,21 @@ self.check_sizeof((), size(h)) self.check_sizeof((1,2,3), size(h) + 3*self.P) + def test_moduleobjects(self): + # Some objects from built-in modules + h = self.header + size = self.calcsize + # _sre.PatternObject + import re + x = re.compile("") + self.check_sizeof(x, size(h + 'P PPP PiP PH')) + + # _struct.Struct + x = struct.Struct("") + self.check_sizeof(x, size(h + 'PPPPP')) + + def test_main(): test_classes = (SysModuleTest, SizeofTest)