From 3bbd2fad4d4a282c7a5a3169a4f497b97aeff319 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20v=2E=20L=C3=B6wis?= Date: Thu, 26 Jul 2012 22:23:23 +0200 Subject: Issue #15456: Fix code __sizeof__ after #12399 change. Patch by Serhiy Storchaka. --- Objects/codeobject.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'Objects/codeobject.c') diff --git a/Objects/codeobject.c b/Objects/codeobject.c index 550e28498d..9713f61b24 100644 --- a/Objects/codeobject.c +++ b/Objects/codeobject.c @@ -374,6 +374,17 @@ code_dealloc(PyCodeObject *co) PyObject_DEL(co); } +static PyObject * +code_sizeof(PyCodeObject *co, void *unused) +{ + Py_ssize_t res; + + res = sizeof(PyCodeObject); + if (co->co_cell2arg != NULL && co->co_cellvars != NULL) + res += PyTuple_GET_SIZE(co->co_cellvars) * sizeof(unsigned char); + return PyLong_FromSsize_t(res); +} + static PyObject * code_repr(PyCodeObject *co) { @@ -480,6 +491,11 @@ code_hash(PyCodeObject *co) /* XXX code objects need to participate in GC? */ +static struct PyMethodDef code_methods[] = { + {"__sizeof__", (PyCFunction)code_sizeof, METH_NOARGS}, + {NULL, NULL} /* sentinel */ +}; + PyTypeObject PyCode_Type = { PyVarObject_HEAD_INIT(&PyType_Type, 0) "code", @@ -508,7 +524,7 @@ PyTypeObject PyCode_Type = { offsetof(PyCodeObject, co_weakreflist), /* tp_weaklistoffset */ 0, /* tp_iter */ 0, /* tp_iternext */ - 0, /* tp_methods */ + code_methods, /* tp_methods */ code_memberlist, /* tp_members */ 0, /* tp_getset */ 0, /* tp_base */ -- cgit v1.2.1