Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(159)

Side by Side Diff: Python/sysmodule.c

Issue 2414: Add gc header size to returned sizeof information SVN Base: http://svn.python.org/view/*checkout*/python/trunk/
Patch Set: added a default return value and made this and the gc_head inclusion optional Created 1 year, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 1
2 /* System module */ 2 /* System module */
3 3
4 /* 4 /*
5 Various bits of information used by the interpreter are collected in 5 Various bits of information used by the interpreter are collected in
6 module 'sys'. 6 module 'sys'.
7 Function member: 7 Function member:
8 - exit(sts): raise SystemExit 8 - exit(sts): raise SystemExit
9 Data members: 9 Data members:
10 - stdin, stdout, stderr: standard file objects 10 - stdin, stdout, stderr: standard file objects
(...skipping 622 matching lines...) Expand 10 before | Expand all | Expand 10 after
633 int flag; 633 int flag;
634 if (!PyArg_ParseTuple(args, "i:mdebug", &flag)) 634 if (!PyArg_ParseTuple(args, "i:mdebug", &flag))
635 return NULL; 635 return NULL;
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 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", "gc_head", 0};
648 » PyObject *o, *dflt = NULL;
649 » int gc_head;
650
651 » if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|Oi:getsizeof",
652 » » » » » kwlist, &o, &dflt, &gc_head))
653 » » return NULL;
646 654
647 /* Initialize static variable needed by _PyType_Lookup */ 655 /* Initialize static variable needed by _PyType_Lookup */
648 if (str__sizeof__ == NULL) { 656 if (str__sizeof__ == NULL) {
649 str__sizeof__ = PyString_InternFromString("__sizeof__"); 657 str__sizeof__ = PyString_InternFromString("__sizeof__");
650 if (str__sizeof__ == NULL) 658 if (str__sizeof__ == NULL)
651 return NULL; 659 return NULL;
652 } 660 }
653 661
654 » /* Type objects */ 662 /* Initialize static variable for GC head size */
655 » if (PyType_Check(args)){ 663 » if (gc_head_size == NULL) {
656 » » PyObject *method = _PyType_Lookup(Py_TYPE(args), 664 » » gc_head_size = PyInt_FromSsize_t(sizeof(PyGC_Head));
657 » » » » » » str__sizeof__); 665 » » if (gc_head_size == NULL)
658 » » if (method == NULL) { 666 » » » return NULL;
667 » }
668 »
669 » /* Make sure the type is initialized. float gets initialized late */
670 » if (PyType_Ready(Py_TYPE(o)) < 0)
671 » » return NULL;
672 »
673 » /* non-type objects */
674 » else if (!PyType_Check(o))
675 » » res = PyObject_CallMethod(o, "__sizeof__", NULL);
676 » /* type objects and objects which do not have the __sizeof__ attribute * /
677 » if (PyType_Check(o) ||
678 » (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError))) {
679 » » PyObject *method = _PyType_Lookup(Py_TYPE(o), str__sizeof__);
680 » » if (method == NULL)
659 PyErr_Format(PyExc_TypeError, 681 PyErr_Format(PyExc_TypeError,
660 "Type %.100s doesn't define __sizeof__", 682 "Type %.100s doesn't define __sizeof__",
661 » » » » Py_TYPE(args)->tp_name); 683 » » » » Py_TYPE(o)->tp_name);
662 » » » return NULL; 684 » » else
663 » » } 685 » » » res = PyObject_CallFunctionObjArgs(method, o, NULL);
664 » » return PyObject_CallFunctionObjArgs(method, args, NULL); 686 » }
665 » } 687
666 » /* Instance of old-style classes */ 688 » /* failed. is default value is given? */
Martin v. Löwis 2008/07/07 05:24:06 "Has a default value been given" or "Was a default
667 » else if (PyInstance_Check(args)) 689 » if ((res == NULL) && (dflt != NULL) &&
668 » » return PyInt_FromSsize_t(PyInstance_Type.tp_basicsize); 690 » PyErr_ExceptionMatches(PyExc_TypeError))
669 » /* Old-style classes */ 691 » {
670 » else if (PyClass_Check(args)) 692 » » PyErr_Clear();
671 » » return PyInt_FromSsize_t(PyClass_Type.tp_basicsize); 693 » » Py_INCREF(dflt);
672 » else 694 » » return dflt;
673 » » return PyObject_CallMethod(args, "__sizeof__", NULL); 695 » }
696 » else if (res == NULL)
697 » » return res;
698 »
699 » /* add gc_head size if object is tracked by gc */
700 » if (gc_head && PyObject_IS_GC(o)) {
701 » » PyObject *tmp = res;
702 » » res = PyNumber_Add(tmp, gc_head_size);
703 » » Py_DECREF(tmp);
704 » }
705 » return res;
674 } 706 }
675 707
676 PyDoc_STRVAR(getsizeof_doc, 708 PyDoc_STRVAR(getsizeof_doc,
677 "getsizeof(object) -> int\n\ 709 "getsizeof(object, default, gc_head=True) -> int\n\
678 \n\ 710 \n\
679 Return the size of object in bytes."); 711 Return the size of object in bytes.");
680 712
681 static PyObject * 713 static PyObject *
682 sys_getrefcount(PyObject *self, PyObject *arg) 714 sys_getrefcount(PyObject *self, PyObject *arg)
683 { 715 {
684 return PyInt_FromSsize_t(arg->ob_refcnt); 716 return PyInt_FromSsize_t(arg->ob_refcnt);
685 } 717 }
686 718
687 #ifdef Py_REF_DEBUG 719 #ifdef Py_REF_DEBUG
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after
882 #endif 914 #endif
883 #ifdef Py_TRACE_REFS 915 #ifdef Py_TRACE_REFS
884 {"getobjects", _Py_GetObjects, METH_VARARGS}, 916 {"getobjects", _Py_GetObjects, METH_VARARGS},
885 #endif 917 #endif
886 #ifdef Py_REF_DEBUG 918 #ifdef Py_REF_DEBUG
887 {"gettotalrefcount", (PyCFunction)sys_gettotalrefcount, METH_NOARGS}, 919 {"gettotalrefcount", (PyCFunction)sys_gettotalrefcount, METH_NOARGS},
888 #endif 920 #endif
889 {"getrefcount", (PyCFunction)sys_getrefcount, METH_O, getrefcount_doc}, 921 {"getrefcount", (PyCFunction)sys_getrefcount, METH_O, getrefcount_doc},
890 {"getrecursionlimit", (PyCFunction)sys_getrecursionlimit, METH_NOARGS, 922 {"getrecursionlimit", (PyCFunction)sys_getrecursionlimit, METH_NOARGS,
891 getrecursionlimit_doc}, 923 getrecursionlimit_doc},
892 » {"getsizeof",» sys_getsizeof, METH_O, getsizeof_doc}, 924 » {"getsizeof", (PyCFunction)sys_getsizeof,
925 » METH_VARARGS | METH_KEYWORDS, getsizeof_doc},
893 {"_getframe", sys_getframe, METH_VARARGS, getframe_doc}, 926 {"_getframe", sys_getframe, METH_VARARGS, getframe_doc},
894 #ifdef MS_WINDOWS 927 #ifdef MS_WINDOWS
895 {"getwindowsversion", (PyCFunction)sys_getwindowsversion, METH_NOARGS, 928 {"getwindowsversion", (PyCFunction)sys_getwindowsversion, METH_NOARGS,
896 getwindowsversion_doc}, 929 getwindowsversion_doc},
897 #endif /* MS_WINDOWS */ 930 #endif /* MS_WINDOWS */
898 #ifdef USE_MALLOPT 931 #ifdef USE_MALLOPT
899 {"mdebug", sys_mdebug, METH_VARARGS}, 932 {"mdebug", sys_mdebug, METH_VARARGS},
900 #endif 933 #endif
901 #ifdef Py_USING_UNICODE 934 #ifdef Py_USING_UNICODE
902 {"setdefaultencoding", sys_setdefaultencoding, METH_VARARGS, 935 {"setdefaultencoding", sys_setdefaultencoding, METH_VARARGS,
(...skipping 773 matching lines...) Expand 10 before | Expand all | Expand 10 after
1676 1709
1677 void 1710 void
1678 PySys_WriteStderr(const char *format, ...) 1711 PySys_WriteStderr(const char *format, ...)
1679 { 1712 {
1680 va_list va; 1713 va_list va;
1681 1714
1682 va_start(va, format); 1715 va_start(va, format);
1683 mywrite("stderr", stderr, format, va); 1716 mywrite("stderr", stderr, format, va);
1684 va_end(va); 1717 va_end(va);
1685 } 1718 }
OLDNEW

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld r497