comparison Python/sysmodule.c @ 47466:58f4b60773fe

Merged revisions 63856-63857,63859-63860 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r63856 | robert.schuppenies | 2008-06-01 18:16:17 +0200 (So, 01 Jun 2008) | 2 lines Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes. ........ r63859 | georg.brandl | 2008-06-01 18:42:16 +0200 (So, 01 Jun 2008) | 2 lines Some style nits. Also clarify in the docstrings what __sizeof__ does. ........ r63860 | georg.brandl | 2008-06-01 19:05:56 +0200 (So, 01 Jun 2008) | 2 lines Fix test_descrtut. ........
author Martin v. Löwis <martin@v.loewis.de>
date Wed, 04 Jun 2008 14:18:43 +0000
parents a65619ad59f2
children 7a1d4e8233f8
comparison
equal deleted inserted replaced
47465:8fcc9ec18824 47466:58f4b60773fe
606 mallopt(M_DEBUG, flag); 606 mallopt(M_DEBUG, flag);
607 Py_INCREF(Py_None); 607 Py_INCREF(Py_None);
608 return Py_None; 608 return Py_None;
609 } 609 }
610 #endif /* USE_MALLOPT */ 610 #endif /* USE_MALLOPT */
611
612 static PyObject *
613 sys_getsizeof(PyObject *self, PyObject *args)
614 {
615 static PyObject * str__sizeof__ = NULL;
616
617 /* Initialize static variable needed by _PyType_Lookup */
618 if (str__sizeof__ == NULL) {
619 str__sizeof__ = PyUnicode_InternFromString("__sizeof__");
620 if (str__sizeof__ == NULL)
621 return NULL;
622 }
623
624 /* Type objects */
625 if (PyType_Check(args)){
626 PyObject *method = _PyType_Lookup(Py_TYPE(args),
627 str__sizeof__);
628 if (method == NULL) {
629 PyErr_Format(PyExc_TypeError,
630 "Type %.100s doesn't define __sizeof__",
631 Py_TYPE(args)->tp_name);
632 return NULL;
633 }
634 return PyObject_CallFunctionObjArgs(method, args, NULL);
635 }
636 else
637 return PyObject_CallMethod(args, "__sizeof__", NULL);
638 }
639
640 PyDoc_STRVAR(getsizeof_doc,
641 "getsizeof(object) -> int\n\
642 \n\
643 Return the size of object in bytes.");
611 644
612 static PyObject * 645 static PyObject *
613 sys_getrefcount(PyObject *self, PyObject *arg) 646 sys_getrefcount(PyObject *self, PyObject *arg)
614 { 647 {
615 return PyLong_FromSsize_t(arg->ob_refcnt); 648 return PyLong_FromSsize_t(arg->ob_refcnt);
810 {"gettotalrefcount", (PyCFunction)sys_gettotalrefcount, METH_NOARGS}, 843 {"gettotalrefcount", (PyCFunction)sys_gettotalrefcount, METH_NOARGS},
811 #endif 844 #endif
812 {"getrefcount", (PyCFunction)sys_getrefcount, METH_O, getrefcount_doc}, 845 {"getrefcount", (PyCFunction)sys_getrefcount, METH_O, getrefcount_doc},
813 {"getrecursionlimit", (PyCFunction)sys_getrecursionlimit, METH_NOARGS, 846 {"getrecursionlimit", (PyCFunction)sys_getrecursionlimit, METH_NOARGS,
814 getrecursionlimit_doc}, 847 getrecursionlimit_doc},
848 {"getsizeof", sys_getsizeof, METH_O, getsizeof_doc},
815 {"_getframe", sys_getframe, METH_VARARGS, getframe_doc}, 849 {"_getframe", sys_getframe, METH_VARARGS, getframe_doc},
816 #ifdef MS_WINDOWS 850 #ifdef MS_WINDOWS
817 {"getwindowsversion", (PyCFunction)sys_getwindowsversion, METH_NOARGS, 851 {"getwindowsversion", (PyCFunction)sys_getwindowsversion, METH_NOARGS,
818 getwindowsversion_doc}, 852 getwindowsversion_doc},
819 #endif /* MS_WINDOWS */ 853 #endif /* MS_WINDOWS */
981 exit() -- exit the interpreter by raising SystemExit\n\ 1015 exit() -- exit the interpreter by raising SystemExit\n\
982 getdlopenflags() -- returns flags to be used for dlopen() calls\n\ 1016 getdlopenflags() -- returns flags to be used for dlopen() calls\n\
983 getprofile() -- get the global profiling function\n\ 1017 getprofile() -- get the global profiling function\n\
984 getrefcount() -- return the reference count for an object (plus one :-)\n\ 1018 getrefcount() -- return the reference count for an object (plus one :-)\n\
985 getrecursionlimit() -- return the max recursion depth for the interpreter\n\ 1019 getrecursionlimit() -- return the max recursion depth for the interpreter\n\
1020 getsizeof() -- return the size of an object in bytes\n\
986 gettrace() -- get the global debug tracing function\n\ 1021 gettrace() -- get the global debug tracing function\n\
987 setcheckinterval() -- control how often the interpreter checks for events\n\ 1022 setcheckinterval() -- control how often the interpreter checks for events\n\
988 setdlopenflags() -- set the flags to be used for dlopen() calls\n\ 1023 setdlopenflags() -- set the flags to be used for dlopen() calls\n\
989 setprofile() -- set the global profiling function\n\ 1024 setprofile() -- set the global profiling function\n\
990 setrecursionlimit() -- set the max recursion depth for the interpreter\n\ 1025 setrecursionlimit() -- set the max recursion depth for the interpreter\n\