@@ -2730,6 +2730,22 @@ type_dir(PyObject *self, PyObject *args)
27302730 return result ;
27312731}
27322732
2733+ static PyObject *
2734+ type_sizeof (PyObject * self , PyObject * args_unused )
2735+ {
2736+ Py_ssize_t size ;
2737+ PyTypeObject * type = (PyTypeObject * )self ;
2738+ if (type -> tp_flags & Py_TPFLAGS_HEAPTYPE ) {
2739+ PyHeapTypeObject * et = (PyHeapTypeObject * )type ;
2740+ size = sizeof (PyHeapTypeObject );
2741+ if (et -> ht_cached_keys )
2742+ size += _PyDict_KeysSize (et -> ht_cached_keys );
2743+ }
2744+ else
2745+ size = sizeof (PyTypeObject );
2746+ return PyLong_FromSsize_t (size );
2747+ }
2748+
27332749static PyMethodDef type_methods [] = {
27342750 {"mro" , (PyCFunction )mro_external , METH_NOARGS ,
27352751 PyDoc_STR ("mro() -> list\nreturn a type's method resolution order" )},
@@ -2745,6 +2761,8 @@ static PyMethodDef type_methods[] = {
27452761 PyDoc_STR ("__subclasscheck__() -> bool\ncheck if a class is a subclass" )},
27462762 {"__dir__" , type_dir , METH_NOARGS ,
27472763 PyDoc_STR ("__dir__() -> list\nspecialized __dir__ implementation for types" )},
2764+ {"__sizeof__" , type_sizeof , METH_NOARGS ,
2765+ "__sizeof__() -> int\nreturn memory consumption of the type object" },
27482766 {0 }
27492767};
27502768
0 commit comments