[Python-checkins] cpython (2.7): Issue #22193: Added private function _PySys_GetSizeOf() needed to implement
serhiy.storchaka
python-checkins at python.org
Thu Aug 14 21:34:52 CEST 2014
http://hg.python.org/cpython/rev/b5114747d3ed
changeset: 92093:b5114747d3ed
branch: 2.7
parent: 92084:71cb8f605f77
user: Serhiy Storchaka <storchaka at gmail.com>
date: Thu Aug 14 22:20:23 2014 +0300
summary:
Issue #22193: Added private function _PySys_GetSizeOf() needed to implement
some __sizeof__() methods.
files:
Include/sysmodule.h | 4 +
Python/sysmodule.c | 77 ++++++++++++++++++--------------
2 files changed, 47 insertions(+), 34 deletions(-)
diff --git a/Include/sysmodule.h b/Include/sysmodule.h
--- a/Include/sysmodule.h
+++ b/Include/sysmodule.h
@@ -23,6 +23,10 @@
PyAPI_FUNC(void) PySys_AddWarnOption(char *);
PyAPI_FUNC(int) PySys_HasWarnOptions(void);
+#ifndef Py_LIMITED_API
+PyAPI_DATA(size_t) _PySys_GetSizeOf(PyObject *);
+#endif
+
#ifdef __cplusplus
}
#endif
diff --git a/Python/sysmodule.c b/Python/sysmodule.c
--- a/Python/sysmodule.c
+++ b/Python/sysmodule.c
@@ -684,32 +684,20 @@
}
#endif /* USE_MALLOPT */
-static PyObject *
-sys_getsizeof(PyObject *self, PyObject *args, PyObject *kwds)
+size_t
+_PySys_GetSizeOf(PyObject *o)
{
+ static PyObject *str__sizeof__ = NULL;
PyObject *res = NULL;
- static PyObject *str__sizeof__ = NULL, *gc_head_size = NULL;
- static char *kwlist[] = {"object", "default", 0};
- PyObject *o, *dflt = NULL;
-
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:getsizeof",
- kwlist, &o, &dflt))
- return NULL;
-
- /* Initialize static variable for GC head size */
- if (gc_head_size == NULL) {
- gc_head_size = PyInt_FromSsize_t(sizeof(PyGC_Head));
- if (gc_head_size == NULL)
- return NULL;
- }
+ size_t size;
/* Make sure the type is initialized. float gets initialized late */
if (PyType_Ready(Py_TYPE(o)) < 0)
- return NULL;
+ return (size_t)-1;
/* Instance of old-style class */
if (PyInstance_Check(o))
- res = PyInt_FromSsize_t(PyInstance_Type.tp_basicsize);
+ size = PyInstance_Type.tp_basicsize;
/* all other objects */
else {
PyObject *method = _PyObject_LookupSpecial(o, "__sizeof__",
@@ -724,26 +712,47 @@
res = PyObject_CallFunctionObjArgs(method, NULL);
Py_DECREF(method);
}
+
+ if (res == NULL)
+ return (size_t)-1;
+
+ size = (size_t)PyInt_AsSsize_t(res);
+ Py_DECREF(res);
+ if (size == (size_t)-1 && PyErr_Occurred())
+ return (size_t)-1;
}
- /* Has a default value been given? */
- if ((res == NULL) && (dflt != NULL) &&
- PyErr_ExceptionMatches(PyExc_TypeError))
- {
- PyErr_Clear();
- Py_INCREF(dflt);
- return dflt;
+ /* add gc_head size */
+ if (PyObject_IS_GC(o))
+ size += sizeof(PyGC_Head);
+ return size;
+}
+
+static PyObject *
+sys_getsizeof(PyObject *self, PyObject *args, PyObject *kwds)
+{
+ static char *kwlist[] = {"object", "default", 0};
+ size_t size;
+ PyObject *o, *dflt = NULL;
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:getsizeof",
+ kwlist, &o, &dflt))
+ return NULL;
+
+ size = _PySys_GetSizeOf(o);
+
+ if (size == (size_t)-1 && PyErr_Occurred()) {
+ /* Has a default value been given */
+ if (dflt != NULL && PyErr_ExceptionMatches(PyExc_TypeError)) {
+ PyErr_Clear();
+ Py_INCREF(dflt);
+ return dflt;
+ }
+ else
+ return NULL;
}
- else if (res == NULL)
- return res;
- /* add gc_head size */
- if (PyObject_IS_GC(o)) {
- PyObject *tmp = res;
- res = PyNumber_Add(tmp, gc_head_size);
- Py_DECREF(tmp);
- }
- return res;
+ return PyInt_FromSize_t(size);
}
PyDoc_STRVAR(getsizeof_doc,
--
Repository URL: http://hg.python.org/cpython
More information about the Python-checkins
mailing list