comparison Python/sysmodule.c @ 48467:c0c06ffaea83

Merged revisions 64842,64853,64856,64945 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r64842 | robert.schuppenies | 2008-07-10 15:43:26 +0200 (Thu, 10 Jul 2008) | 2 lines Fixed Issue3122 and extended sys.getsizeof tests for built-in types. ........ r64853 | robert.schuppenies | 2008-07-10 17:24:04 +0200 (Thu, 10 Jul 2008) | 3 lines Added additional __sizeof__ implementations and addressed comments made in Issue3122. ........ r64856 | robert.schuppenies | 2008-07-10 19:13:55 +0200 (Thu, 10 Jul 2008) | 3 lines Added garbage collector overhead and optional default return value to sys.getsizeof. ........ r64945 | robert.schuppenies | 2008-07-14 10:42:18 +0200 (Mon, 14 Jul 2008) | 2 lines Fixed test failure on Win64 machines. ........
author Robert Schuppenies <okkotonushi@googlemail.com>
date Mon, 14 Jul 2008 10:13:31 +0000
parents 94f64f11fdd0
children 3d868254ff7e
comparison
equal deleted inserted replaced
48466:afdd64edf189 48467:c0c06ffaea83
608 return Py_None; 608 return Py_None;
609 } 609 }
610 #endif /* USE_MALLOPT */ 610 #endif /* USE_MALLOPT */
611 611
612 static PyObject * 612 static PyObject *
613 sys_getsizeof(PyObject *self, PyObject *args) 613 sys_getsizeof(PyObject *self, PyObject *args, PyObject *kwds)
614 { 614 {
615 static PyObject * str__sizeof__ = NULL; 615 PyObject *res = NULL;
616 static PyObject *str__sizeof__, *gc_head_size = NULL;
617 static char *kwlist[] = {"object", "default", 0};
618 PyObject *o, *dflt = NULL;
619
620 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:getsizeof",
621 kwlist, &o, &dflt))
622 return NULL;
616 623
617 /* Initialize static variable needed by _PyType_Lookup */ 624 /* Initialize static variable needed by _PyType_Lookup */
618 if (str__sizeof__ == NULL) { 625 if (str__sizeof__ == NULL) {
619 str__sizeof__ = PyUnicode_InternFromString("__sizeof__"); 626 str__sizeof__ = PyUnicode_InternFromString("__sizeof__");
620 if (str__sizeof__ == NULL) 627 if (str__sizeof__ == NULL)
621 return NULL; 628 return NULL;
622 } 629 }
623 630
624 /* Type objects */ 631 /* Initialize static variable for GC head size */
625 if (PyType_Check(args)){ 632 if (gc_head_size == NULL) {
626 PyObject *method = _PyType_Lookup(Py_TYPE(args), 633 gc_head_size = PyLong_FromSsize_t(sizeof(PyGC_Head));
627 str__sizeof__); 634 if (gc_head_size == NULL)
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; 635 return NULL;
633 } 636 }
634 return PyObject_CallFunctionObjArgs(method, args, NULL); 637
635 } 638 /* Make sure the type is initialized. float gets initialized late */
639 if (PyType_Ready(Py_TYPE(o)) < 0)
640 return NULL;
641
642 PyObject *method = _PyType_Lookup(Py_TYPE(o), str__sizeof__);
643 if (method == NULL)
644 PyErr_Format(PyExc_TypeError,
645 "Type %.100s doesn't define __sizeof__",
646 Py_TYPE(o)->tp_name);
636 else 647 else
637 return PyObject_CallMethod(args, "__sizeof__", NULL); 648 res = PyObject_CallFunctionObjArgs(method, o, NULL);
649
650 /* Has a default value been given */
651 if ((res == NULL) && (dflt != NULL) &&
652 PyErr_ExceptionMatches(PyExc_TypeError))
653 {
654 PyErr_Clear();
655 Py_INCREF(dflt);
656 return dflt;
657 }
658 else if (res == NULL)
659 return res;
660
661 /* add gc_head size */
662 if (PyObject_IS_GC(o)) {
663 PyObject *tmp = res;
664 res = PyNumber_Add(tmp, gc_head_size);
665 Py_DECREF(tmp);
666 }
667 return res;
638 } 668 }
639 669
640 PyDoc_STRVAR(getsizeof_doc, 670 PyDoc_STRVAR(getsizeof_doc,
641 "getsizeof(object) -> int\n\ 671 "getsizeof(object, default) -> int\n\
642 \n\ 672 \n\
643 Return the size of object in bytes."); 673 Return the size of object in bytes.");
644 674
645 static PyObject * 675 static PyObject *
646 sys_getrefcount(PyObject *self, PyObject *arg) 676 sys_getrefcount(PyObject *self, PyObject *arg)
843 {"gettotalrefcount", (PyCFunction)sys_gettotalrefcount, METH_NOARGS}, 873 {"gettotalrefcount", (PyCFunction)sys_gettotalrefcount, METH_NOARGS},
844 #endif 874 #endif
845 {"getrefcount", (PyCFunction)sys_getrefcount, METH_O, getrefcount_doc}, 875 {"getrefcount", (PyCFunction)sys_getrefcount, METH_O, getrefcount_doc},
846 {"getrecursionlimit", (PyCFunction)sys_getrecursionlimit, METH_NOARGS, 876 {"getrecursionlimit", (PyCFunction)sys_getrecursionlimit, METH_NOARGS,
847 getrecursionlimit_doc}, 877 getrecursionlimit_doc},
848 {"getsizeof", sys_getsizeof, METH_O, getsizeof_doc}, 878 {"getsizeof", (PyCFunction)sys_getsizeof,
879 METH_VARARGS | METH_KEYWORDS, getsizeof_doc},
849 {"_getframe", sys_getframe, METH_VARARGS, getframe_doc}, 880 {"_getframe", sys_getframe, METH_VARARGS, getframe_doc},
850 #ifdef MS_WINDOWS 881 #ifdef MS_WINDOWS
851 {"getwindowsversion", (PyCFunction)sys_getwindowsversion, METH_NOARGS, 882 {"getwindowsversion", (PyCFunction)sys_getwindowsversion, METH_NOARGS,
852 getwindowsversion_doc}, 883 getwindowsversion_doc},
853 #endif /* MS_WINDOWS */ 884 #endif /* MS_WINDOWS */