Index: Objects/boolobject.c =================================================================== --- Objects/boolobject.c (revision 66040) +++ Objects/boolobject.c (working copy) @@ -54,6 +54,12 @@ return PyBool_FromLong(ok); } +static PyObject * +bool_sizeof(PyObject *b) +{ + return PyLong_FromSsize_t(sizeof(struct _longobject)); +} + /* Arithmetic operations redefined to return bool if both args are bool. */ static PyObject * @@ -89,6 +95,12 @@ The builtins True and False are the only two instances of the class bool.\n\ The class bool is a subclass of the class int, and cannot be subclassed."); +static PyMethodDef bool_methods[] = { + {"__sizeof__", (PyCFunction)bool_sizeof, METH_NOARGS, + "Returns size in memory, in bytes"}, + {NULL, NULL} /* sentinel */ +}; + /* Arithmetic methods -- only so we can override &, |, ^. */ static PyNumberMethods bool_as_number = { @@ -158,7 +170,7 @@ 0, /* tp_weaklistoffset */ 0, /* tp_iter */ 0, /* tp_iternext */ - 0, /* tp_methods */ + bool_methods, /* tp_methods */ 0, /* tp_members */ 0, /* tp_getset */ &PyLong_Type, /* tp_base */ Index: Lib/test/test_sys.py =================================================================== --- Lib/test/test_sys.py (revision 66040) +++ Lib/test/test_sys.py (working copy) @@ -414,7 +414,7 @@ size = self.calcsize gc_header_size = self.gc_headsize # bool objects are not gc tracked - self.assertEqual(sys.getsizeof(True), size(vh) + self.H) + self.assertEqual(sys.getsizeof(True), size(vh + 'H')) # but lists are self.assertEqual(sys.getsizeof([]), size(vh + 'PP') + gc_header_size) @@ -422,8 +422,8 @@ h = self.header vh = self.vheader size = self.calcsize - self.assertEqual(sys.getsizeof(True), size(vh) + self.H) - self.assertEqual(sys.getsizeof(True, -1), size(vh) + self.H) + self.assertEqual(sys.getsizeof(True), size(vh + 'H')) + self.assertEqual(sys.getsizeof(True, -1), size(vh + 'H')) def test_objecttypes(self): # check all types defined in Objects/ @@ -432,7 +432,8 @@ size = self.calcsize check = self.check_sizeof # bool - check(True, size(vh) + self.H) + check(True, size(vh + 'H')) + check(False, size(vh + 'H')) # buffer # XXX # builtin_function_or_method