comparison Python/sysmodule.c @ 53947:f90b3eec7c32 legacy-trunk

convert some more special methods to use _PyObject_LookupSpecial
author Benjamin Peterson <benjamin@python.org>
date Sat, 09 May 2009 16:36:39 +0000
parents 9b358eae532a
children 150986ab3db2
comparison
equal deleted inserted replaced
53945:072a8a7ce973 53947:f90b3eec7c32
641 641
642 static PyObject * 642 static PyObject *
643 sys_getsizeof(PyObject *self, PyObject *args, PyObject *kwds) 643 sys_getsizeof(PyObject *self, PyObject *args, PyObject *kwds)
644 { 644 {
645 PyObject *res = NULL; 645 PyObject *res = NULL;
646 static PyObject *str__sizeof__, *gc_head_size = NULL; 646 static PyObject *str__sizeof__ = NULL, *gc_head_size = NULL;
647 static char *kwlist[] = {"object", "default", 0}; 647 static char *kwlist[] = {"object", "default", 0};
648 PyObject *o, *dflt = NULL; 648 PyObject *o, *dflt = NULL;
649 649
650 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:getsizeof", 650 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:getsizeof",
651 kwlist, &o, &dflt)) 651 kwlist, &o, &dflt))
652 return NULL; 652 return NULL;
653
654 /* Initialize static variable needed by _PyType_Lookup */
655 if (str__sizeof__ == NULL) {
656 str__sizeof__ = PyString_InternFromString("__sizeof__");
657 if (str__sizeof__ == NULL)
658 return NULL;
659 }
660 653
661 /* Initialize static variable for GC head size */ 654 /* Initialize static variable for GC head size */
662 if (gc_head_size == NULL) { 655 if (gc_head_size == NULL) {
663 gc_head_size = PyInt_FromSsize_t(sizeof(PyGC_Head)); 656 gc_head_size = PyInt_FromSsize_t(sizeof(PyGC_Head));
664 if (gc_head_size == NULL) 657 if (gc_head_size == NULL)
672 /* Instance of old-style class */ 665 /* Instance of old-style class */
673 if (PyInstance_Check(o)) 666 if (PyInstance_Check(o))
674 res = PyInt_FromSsize_t(PyInstance_Type.tp_basicsize); 667 res = PyInt_FromSsize_t(PyInstance_Type.tp_basicsize);
675 /* all other objects */ 668 /* all other objects */
676 else { 669 else {
677 PyObject *method = _PyType_Lookup(Py_TYPE(o), 670 PyObject *method = _PyObject_LookupSpecial(o, "__sizeof__",
678 str__sizeof__); 671 &str__sizeof__);
679 if (method == NULL) 672 if (method == NULL)
680 PyErr_Format(PyExc_TypeError, 673 PyErr_Format(PyExc_TypeError,
681 "Type %.100s doesn't define __sizeof__", 674 "Type %.100s doesn't define __sizeof__",
682 Py_TYPE(o)->tp_name); 675 Py_TYPE(o)->tp_name);
683 else 676 else {
684 res = PyObject_CallFunctionObjArgs(method, o, NULL); 677 res = PyObject_CallFunctionObjArgs(method, NULL);
678 Py_DECREF(method);
679 }
685 } 680 }
686 681
687 /* Has a default value been given? */ 682 /* Has a default value been given? */
688 if ((res == NULL) && (dflt != NULL) && 683 if ((res == NULL) && (dflt != NULL) &&
689 PyErr_ExceptionMatches(PyExc_TypeError)) 684 PyErr_ExceptionMatches(PyExc_TypeError))