Kaydet (Commit) c95f51a9 authored tarafından Serhiy Storchaka's avatar Serhiy Storchaka

Issue #22193: Added private function _PySys_GetSizeOf() needed to implement

some __sizeof__() methods.
üst ce0f744a
...@@ -23,6 +23,10 @@ PyAPI_FUNC(void) PySys_ResetWarnOptions(void); ...@@ -23,6 +23,10 @@ PyAPI_FUNC(void) PySys_ResetWarnOptions(void);
PyAPI_FUNC(void) PySys_AddWarnOption(char *); PyAPI_FUNC(void) PySys_AddWarnOption(char *);
PyAPI_FUNC(int) PySys_HasWarnOptions(void); PyAPI_FUNC(int) PySys_HasWarnOptions(void);
#ifndef Py_LIMITED_API
PyAPI_DATA(size_t) _PySys_GetSizeOf(PyObject *);
#endif
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
......
...@@ -684,32 +684,20 @@ sys_mdebug(PyObject *self, PyObject *args) ...@@ -684,32 +684,20 @@ sys_mdebug(PyObject *self, PyObject *args)
} }
#endif /* USE_MALLOPT */ #endif /* USE_MALLOPT */
static PyObject * size_t
sys_getsizeof(PyObject *self, PyObject *args, PyObject *kwds) _PySys_GetSizeOf(PyObject *o)
{ {
static PyObject *str__sizeof__ = NULL;
PyObject *res = NULL; PyObject *res = NULL;
static PyObject *str__sizeof__ = NULL, *gc_head_size = NULL; size_t size;
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;
}
/* Make sure the type is initialized. float gets initialized late */ /* Make sure the type is initialized. float gets initialized late */
if (PyType_Ready(Py_TYPE(o)) < 0) if (PyType_Ready(Py_TYPE(o)) < 0)
return NULL; return (size_t)-1;
/* Instance of old-style class */ /* Instance of old-style class */
if (PyInstance_Check(o)) if (PyInstance_Check(o))
res = PyInt_FromSsize_t(PyInstance_Type.tp_basicsize); size = PyInstance_Type.tp_basicsize;
/* all other objects */ /* all other objects */
else { else {
PyObject *method = _PyObject_LookupSpecial(o, "__sizeof__", PyObject *method = _PyObject_LookupSpecial(o, "__sizeof__",
...@@ -724,26 +712,47 @@ sys_getsizeof(PyObject *self, PyObject *args, PyObject *kwds) ...@@ -724,26 +712,47 @@ sys_getsizeof(PyObject *self, PyObject *args, PyObject *kwds)
res = PyObject_CallFunctionObjArgs(method, NULL); res = PyObject_CallFunctionObjArgs(method, NULL);
Py_DECREF(method); Py_DECREF(method);
} }
}
/* Has a default value been given? */ if (res == NULL)
if ((res == NULL) && (dflt != NULL) && return (size_t)-1;
PyErr_ExceptionMatches(PyExc_TypeError))
{ size = (size_t)PyInt_AsSsize_t(res);
PyErr_Clear(); Py_DECREF(res);
Py_INCREF(dflt); if (size == (size_t)-1 && PyErr_Occurred())
return dflt; return (size_t)-1;
} }
else if (res == NULL)
return res;
/* add gc_head size */ /* add gc_head size */
if (PyObject_IS_GC(o)) { if (PyObject_IS_GC(o))
PyObject *tmp = res; size += sizeof(PyGC_Head);
res = PyNumber_Add(tmp, gc_head_size); return size;
Py_DECREF(tmp); }
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;
} }
return res;
return PyInt_FromSize_t(size);
} }
PyDoc_STRVAR(getsizeof_doc, PyDoc_STRVAR(getsizeof_doc,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment