Mercurial > cpython-lru_cache-weakref
comparison Python/sysmodule.c @ 47925:fd054e73e2f2 legacy-trunk
Added garbage collector overhead and optional default return value to
sys.getsizeof.
| author | Robert Schuppenies <okkotonushi@googlemail.com> |
|---|---|
| date | Thu, 10 Jul 2008 17:13:55 +0000 |
| parents | 68c7964d2534 |
| children | e11c99e1e6e7 |
comparison
equal
deleted
inserted
replaced
| 47924:328f33baffa3 | 47925:fd054e73e2f2 |
|---|---|
| 638 return Py_None; | 638 return Py_None; |
| 639 } | 639 } |
| 640 #endif /* USE_MALLOPT */ | 640 #endif /* USE_MALLOPT */ |
| 641 | 641 |
| 642 static PyObject * | 642 static PyObject * |
| 643 sys_getsizeof(PyObject *self, PyObject *args) | 643 sys_getsizeof(PyObject *self, PyObject *args, PyObject *kwds) |
| 644 { | 644 { |
| 645 static PyObject * str__sizeof__ = NULL; | 645 PyObject *res = NULL; |
| 646 static PyObject *str__sizeof__, *gc_head_size = NULL; | |
| 647 static char *kwlist[] = {"object", "default", 0}; | |
| 648 PyObject *o, *dflt = NULL; | |
| 649 | |
| 650 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:getsizeof", | |
| 651 kwlist, &o, &dflt)) | |
| 652 return NULL; | |
| 646 | 653 |
| 647 /* Initialize static variable needed by _PyType_Lookup */ | 654 /* Initialize static variable needed by _PyType_Lookup */ |
| 648 if (str__sizeof__ == NULL) { | 655 if (str__sizeof__ == NULL) { |
| 649 str__sizeof__ = PyString_InternFromString("__sizeof__"); | 656 str__sizeof__ = PyString_InternFromString("__sizeof__"); |
| 650 if (str__sizeof__ == NULL) | 657 if (str__sizeof__ == NULL) |
| 651 return NULL; | 658 return NULL; |
| 652 } | 659 } |
| 653 | 660 |
| 661 /* Initialize static variable for GC head size */ | |
| 662 if (gc_head_size == NULL) { | |
| 663 gc_head_size = PyInt_FromSsize_t(sizeof(PyGC_Head)); | |
| 664 if (gc_head_size == NULL) | |
| 665 return NULL; | |
| 666 } | |
| 667 | |
| 654 /* Make sure the type is initialized. float gets initialized late */ | 668 /* Make sure the type is initialized. float gets initialized late */ |
| 655 if (PyType_Ready(Py_TYPE(args)) < 0) | 669 if (PyType_Ready(Py_TYPE(o)) < 0) |
| 656 return NULL; | 670 return NULL; |
| 657 | 671 |
| 658 /* Instance of old-style class */ | 672 /* Instance of old-style class */ |
| 659 if (PyInstance_Check(args)) | 673 if (PyInstance_Check(o)) |
| 660 return PyInt_FromSsize_t(PyInstance_Type.tp_basicsize); | 674 res = PyInt_FromSsize_t(PyInstance_Type.tp_basicsize); |
| 661 /* all other objects */ | 675 /* all other objects */ |
| 662 else { | 676 else { |
| 663 PyObject *method = _PyType_Lookup(Py_TYPE(args), | 677 PyObject *method = _PyType_Lookup(Py_TYPE(o), |
| 664 str__sizeof__); | 678 str__sizeof__); |
| 665 if (method == NULL) { | 679 if (method == NULL) |
| 666 PyErr_Format(PyExc_TypeError, | 680 PyErr_Format(PyExc_TypeError, |
| 667 "Type %.100s doesn't define __sizeof__", | 681 "Type %.100s doesn't define __sizeof__", |
| 668 Py_TYPE(args)->tp_name); | 682 Py_TYPE(o)->tp_name); |
| 669 return NULL; | 683 else |
| 670 } | 684 res = PyObject_CallFunctionObjArgs(method, o, NULL); |
| 671 return PyObject_CallFunctionObjArgs(method, args, NULL); | 685 } |
| 672 } | 686 |
| 687 /* Has a default value been given? */ | |
| 688 if ((res == NULL) && (dflt != NULL) && | |
| 689 PyErr_ExceptionMatches(PyExc_TypeError)) | |
| 690 { | |
| 691 PyErr_Clear(); | |
| 692 Py_INCREF(dflt); | |
| 693 return dflt; | |
| 694 } | |
| 695 else if (res == NULL) | |
| 696 return res; | |
| 697 | |
| 698 /* add gc_head size */ | |
| 699 if (PyObject_IS_GC(o)) { | |
| 700 PyObject *tmp = res; | |
| 701 res = PyNumber_Add(tmp, gc_head_size); | |
| 702 Py_DECREF(tmp); | |
| 703 } | |
| 704 return res; | |
| 673 } | 705 } |
| 674 | 706 |
| 675 PyDoc_STRVAR(getsizeof_doc, | 707 PyDoc_STRVAR(getsizeof_doc, |
| 676 "getsizeof(object) -> int\n\ | 708 "getsizeof(object, default) -> int\n\ |
| 677 \n\ | 709 \n\ |
| 678 Return the size of object in bytes."); | 710 Return the size of object in bytes."); |
| 679 | 711 |
| 680 static PyObject * | 712 static PyObject * |
| 681 sys_getrefcount(PyObject *self, PyObject *arg) | 713 sys_getrefcount(PyObject *self, PyObject *arg) |
| 866 {"gettotalrefcount", (PyCFunction)sys_gettotalrefcount, METH_NOARGS}, | 898 {"gettotalrefcount", (PyCFunction)sys_gettotalrefcount, METH_NOARGS}, |
| 867 #endif | 899 #endif |
| 868 {"getrefcount", (PyCFunction)sys_getrefcount, METH_O, getrefcount_doc}, | 900 {"getrefcount", (PyCFunction)sys_getrefcount, METH_O, getrefcount_doc}, |
| 869 {"getrecursionlimit", (PyCFunction)sys_getrecursionlimit, METH_NOARGS, | 901 {"getrecursionlimit", (PyCFunction)sys_getrecursionlimit, METH_NOARGS, |
| 870 getrecursionlimit_doc}, | 902 getrecursionlimit_doc}, |
| 871 {"getsizeof", sys_getsizeof, METH_O, getsizeof_doc}, | 903 {"getsizeof", (PyCFunction)sys_getsizeof, |
| 904 METH_VARARGS | METH_KEYWORDS, getsizeof_doc}, | |
| 872 {"_getframe", sys_getframe, METH_VARARGS, getframe_doc}, | 905 {"_getframe", sys_getframe, METH_VARARGS, getframe_doc}, |
| 873 #ifdef MS_WINDOWS | 906 #ifdef MS_WINDOWS |
| 874 {"getwindowsversion", (PyCFunction)sys_getwindowsversion, METH_NOARGS, | 907 {"getwindowsversion", (PyCFunction)sys_getwindowsversion, METH_NOARGS, |
| 875 getwindowsversion_doc}, | 908 getwindowsversion_doc}, |
| 876 #endif /* MS_WINDOWS */ | 909 #endif /* MS_WINDOWS */ |
