comparison Python/sysmodule.c @ 47419:88bd07c0a847 legacy-trunk

Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
author Robert Schuppenies <okkotonushi@googlemail.com>
date Sun, 01 Jun 2008 16:16:17 +0000
parents 1062b7dec95b
children a65f095b27f3
comparison
equal deleted inserted replaced
47416:2a63ab3595a1 47419:88bd07c0a847
636 mallopt(M_DEBUG, flag); 636 mallopt(M_DEBUG, flag);
637 Py_INCREF(Py_None); 637 Py_INCREF(Py_None);
638 return Py_None; 638 return Py_None;
639 } 639 }
640 #endif /* USE_MALLOPT */ 640 #endif /* USE_MALLOPT */
641
642 static PyObject *
643 sys_getsizeof(PyObject *self, PyObject *args)
644 {
645 static PyObject * str__sizeof__ = NULL;
646
647 /* Initialize static variable needed by _PyType_Lookup */
648 if (str__sizeof__ == NULL) {
649 str__sizeof__ = PyString_InternFromString("__sizeof__");
650 if (str__sizeof__ == NULL)
651 return NULL;
652 }
653
654 /* Type objects */
655 if (PyType_Check(args)){
656 PyObject *method = _PyType_Lookup(Py_TYPE(args),
657 str__sizeof__);
658 if (method == NULL) {
659 PyErr_Format(PyExc_TypeError,
660 "Type %.100s doesn't define __sizeof__",
661 Py_TYPE(args)->tp_name);
662 return NULL;
663 }
664 return PyObject_CallFunctionObjArgs(method, args, NULL);
665 }
666 /* Instance of old-style classes */
667 else if(PyInstance_Check(args))
668 return PyInt_FromSsize_t(PyInstance_Type.tp_basicsize);
669 /* Old-style class */
670 else if (PyClass_Check(args))
671 return PyInt_FromSsize_t(PyClass_Type.tp_basicsize);
672 else
673 return PyObject_CallMethod(args, "__sizeof__", NULL);
674 }
675
676 PyDoc_STRVAR(getsizeof_doc,
677 "getsizeof(object) -> int\n\
678 \n\
679 Return the size of object in bytes.");
641 680
642 static PyObject * 681 static PyObject *
643 sys_getrefcount(PyObject *self, PyObject *arg) 682 sys_getrefcount(PyObject *self, PyObject *arg)
644 { 683 {
645 return PyInt_FromSsize_t(arg->ob_refcnt); 684 return PyInt_FromSsize_t(arg->ob_refcnt);
848 {"gettotalrefcount", (PyCFunction)sys_gettotalrefcount, METH_NOARGS}, 887 {"gettotalrefcount", (PyCFunction)sys_gettotalrefcount, METH_NOARGS},
849 #endif 888 #endif
850 {"getrefcount", (PyCFunction)sys_getrefcount, METH_O, getrefcount_doc}, 889 {"getrefcount", (PyCFunction)sys_getrefcount, METH_O, getrefcount_doc},
851 {"getrecursionlimit", (PyCFunction)sys_getrecursionlimit, METH_NOARGS, 890 {"getrecursionlimit", (PyCFunction)sys_getrecursionlimit, METH_NOARGS,
852 getrecursionlimit_doc}, 891 getrecursionlimit_doc},
892 {"getsizeof", sys_getsizeof, METH_O, getsizeof_doc},
853 {"_getframe", sys_getframe, METH_VARARGS, getframe_doc}, 893 {"_getframe", sys_getframe, METH_VARARGS, getframe_doc},
854 #ifdef MS_WINDOWS 894 #ifdef MS_WINDOWS
855 {"getwindowsversion", (PyCFunction)sys_getwindowsversion, METH_NOARGS, 895 {"getwindowsversion", (PyCFunction)sys_getwindowsversion, METH_NOARGS,
856 getwindowsversion_doc}, 896 getwindowsversion_doc},
857 #endif /* MS_WINDOWS */ 897 #endif /* MS_WINDOWS */
1029 exit() -- exit the interpreter by raising SystemExit\n\ 1069 exit() -- exit the interpreter by raising SystemExit\n\
1030 getdlopenflags() -- returns flags to be used for dlopen() calls\n\ 1070 getdlopenflags() -- returns flags to be used for dlopen() calls\n\
1031 getprofile() -- get the global profiling function\n\ 1071 getprofile() -- get the global profiling function\n\
1032 getrefcount() -- return the reference count for an object (plus one :-)\n\ 1072 getrefcount() -- return the reference count for an object (plus one :-)\n\
1033 getrecursionlimit() -- return the max recursion depth for the interpreter\n\ 1073 getrecursionlimit() -- return the max recursion depth for the interpreter\n\
1074 getsizeof() -- return the size of an object in bytes\n\
1034 gettrace() -- get the global debug tracing function\n\ 1075 gettrace() -- get the global debug tracing function\n\
1035 setcheckinterval() -- control how often the interpreter checks for events\n\ 1076 setcheckinterval() -- control how often the interpreter checks for events\n\
1036 setdlopenflags() -- set the flags to be used for dlopen() calls\n\ 1077 setdlopenflags() -- set the flags to be used for dlopen() calls\n\
1037 setprofile() -- set the global profiling function\n\ 1078 setprofile() -- set the global profiling function\n\
1038 setrecursionlimit() -- set the max recursion depth for the interpreter\n\ 1079 setrecursionlimit() -- set the max recursion depth for the interpreter\n\