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

Delta Between Two Patch Sets: Python/sysmodule.c

Issue 2414: Add gc header size to returned sizeof information SVN Base: http://svn.python.org/view/*checkout*/python/trunk/
Left Patch Set: Created 1 year, 5 months ago
Right 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:
Left: Side by side diff | Download
Right: Side by side diff | Download
LEFTRIGHT
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 PyObject *res = NULL; 645 PyObject *res = NULL;
646 static PyObject *str__sizeof__ = NULL; 646 » static PyObject *str__sizeof__, *gc_head_size = NULL;
647 static PyObject *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;
648 654
649 /* Initialize static variable needed by _PyType_Lookup */ 655 /* Initialize static variable needed by _PyType_Lookup */
650 if (str__sizeof__ == NULL) { 656 if (str__sizeof__ == NULL) {
651 str__sizeof__ = PyString_InternFromString("__sizeof__"); 657 str__sizeof__ = PyString_InternFromString("__sizeof__");
652 if (str__sizeof__ == NULL) 658 if (str__sizeof__ == NULL)
653 return NULL; 659 return NULL;
654 } 660 }
655 661
656 /* Initialize static variable for GC head size */ 662 /* Initialize static variable for GC head size */
657 if (gc_head_size == NULL) { 663 » if (gc_head_size == NULL) {
658 gc_head_size = PyInt_FromSsize_t(sizeof(PyGC_Head)); 664 » » gc_head_size = PyInt_FromSsize_t(sizeof(PyGC_Head));
659 if (gc_head_size == NULL) { 665 » » if (gc_head_size == NULL)
660 Py_DECREF(res); 666 » » » return NULL;
661 return NULL; 667 » }
662 } 668 »
663 } 669 » /* Make sure the type is initialized. float gets initialized late */
664 670 » if (PyType_Ready(Py_TYPE(o)) < 0)
665 » /* Type objects */ 671 » » return NULL;
666 » if (PyType_Check(args)){ 672 »
667 » » PyObject *method = _PyType_Lookup(Py_TYPE(args), 673 » /* non-type objects */
668 » » » » » » str__sizeof__); 674 » else if (!PyType_Check(o))
669 » » if (method == NULL) { 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)
670 PyErr_Format(PyExc_TypeError, 681 PyErr_Format(PyExc_TypeError,
671 "Type %.100s doesn't define __sizeof__", 682 "Type %.100s doesn't define __sizeof__",
672 » » » » Py_TYPE(args)->tp_name); 683 » » » » Py_TYPE(o)->tp_name);
673 » » » return NULL; 684 » » else
674 » » } 685 » » » res = PyObject_CallFunctionObjArgs(method, o, NULL);
675 » » res = PyObject_CallFunctionObjArgs(method, args, NULL); 686 » }
676 » } 687
677 » /* 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
678 » else if (PyInstance_Check(args)) 689 » if ((res == NULL) && (dflt != NULL) &&
679 » » res = PyInt_FromSsize_t(PyInstance_Type.tp_basicsize); 690 » PyErr_ExceptionMatches(PyExc_TypeError))
680 » /* Old-style classes */ 691 » {
681 » else if (PyClass_Check(args)) 692 » » PyErr_Clear();
682 » » res = PyInt_FromSsize_t(PyClass_Type.tp_basicsize); 693 » » Py_INCREF(dflt);
683 » else 694 » » return dflt;
684 » » res = PyObject_CallMethod(args, "__sizeof__", NULL); 695 » }
685 696 » else if (res == NULL)
697 » » return res;
698 »
686 /* add gc_head size if object is tracked by gc */ 699 /* add gc_head size if object is tracked by gc */
687 » if (PyObject_IS_GC(args)) { 700 » if (gc_head && PyObject_IS_GC(o)) {
688 res = PyNumber_Add(res, gc_head_size); 701 » » PyObject *tmp = res;
689 Py_DECREF(gc_head_size); 702 » » res = PyNumber_Add(tmp, gc_head_size);
690 » } 703 » » Py_DECREF(tmp);
691 704 » }
692 return res; 705 return res;
693 } 706 }
694 707
695 PyDoc_STRVAR(getsizeof_doc, 708 PyDoc_STRVAR(getsizeof_doc,
696 "getsizeof(object) -> int\n\ 709 "getsizeof(object, default, gc_head=True) -> int\n\
697 \n\ 710 \n\
698 Return the size of object in bytes."); 711 Return the size of object in bytes.");
699 712
700 static PyObject * 713 static PyObject *
701 sys_getrefcount(PyObject *self, PyObject *arg) 714 sys_getrefcount(PyObject *self, PyObject *arg)
702 { 715 {
703 return PyInt_FromSsize_t(arg->ob_refcnt); 716 return PyInt_FromSsize_t(arg->ob_refcnt);
704 } 717 }
705 718
706 #ifdef Py_REF_DEBUG 719 #ifdef Py_REF_DEBUG
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after
901 #endif 914 #endif
902 #ifdef Py_TRACE_REFS 915 #ifdef Py_TRACE_REFS
903 {"getobjects", _Py_GetObjects, METH_VARARGS}, 916 {"getobjects", _Py_GetObjects, METH_VARARGS},
904 #endif 917 #endif
905 #ifdef Py_REF_DEBUG 918 #ifdef Py_REF_DEBUG
906 {"gettotalrefcount", (PyCFunction)sys_gettotalrefcount, METH_NOARGS}, 919 {"gettotalrefcount", (PyCFunction)sys_gettotalrefcount, METH_NOARGS},
907 #endif 920 #endif
908 {"getrefcount", (PyCFunction)sys_getrefcount, METH_O, getrefcount_doc}, 921 {"getrefcount", (PyCFunction)sys_getrefcount, METH_O, getrefcount_doc},
909 {"getrecursionlimit", (PyCFunction)sys_getrecursionlimit, METH_NOARGS, 922 {"getrecursionlimit", (PyCFunction)sys_getrecursionlimit, METH_NOARGS,
910 getrecursionlimit_doc}, 923 getrecursionlimit_doc},
911 » {"getsizeof",» sys_getsizeof, METH_O, getsizeof_doc}, 924 » {"getsizeof", (PyCFunction)sys_getsizeof,
925 » METH_VARARGS | METH_KEYWORDS, getsizeof_doc},
912 {"_getframe", sys_getframe, METH_VARARGS, getframe_doc}, 926 {"_getframe", sys_getframe, METH_VARARGS, getframe_doc},
913 #ifdef MS_WINDOWS 927 #ifdef MS_WINDOWS
914 {"getwindowsversion", (PyCFunction)sys_getwindowsversion, METH_NOARGS, 928 {"getwindowsversion", (PyCFunction)sys_getwindowsversion, METH_NOARGS,
915 getwindowsversion_doc}, 929 getwindowsversion_doc},
916 #endif /* MS_WINDOWS */ 930 #endif /* MS_WINDOWS */
917 #ifdef USE_MALLOPT 931 #ifdef USE_MALLOPT
918 {"mdebug", sys_mdebug, METH_VARARGS}, 932 {"mdebug", sys_mdebug, METH_VARARGS},
919 #endif 933 #endif
920 #ifdef Py_USING_UNICODE 934 #ifdef Py_USING_UNICODE
921 {"setdefaultencoding", sys_setdefaultencoding, METH_VARARGS, 935 {"setdefaultencoding", sys_setdefaultencoding, METH_VARARGS,
(...skipping 773 matching lines...) Expand 10 before | Expand all | Expand 10 after
1695 1709
1696 void 1710 void
1697 PySys_WriteStderr(const char *format, ...) 1711 PySys_WriteStderr(const char *format, ...)
1698 { 1712 {
1699 va_list va; 1713 va_list va;
1700 1714
1701 va_start(va, format); 1715 va_start(va, format);
1702 mywrite("stderr", stderr, format, va); 1716 mywrite("stderr", stderr, format, va);
1703 va_end(va); 1717 va_end(va);
1704 } 1718 }
LEFTRIGHT

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