Skip to content

Commit 3bbd2fa

Browse files
committed
Issue #15456: Fix code __sizeof__ after #12399 change.
Patch by Serhiy Storchaka.
1 parent d0118e1 commit 3bbd2fa

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

Lib/test/test_sys.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,12 @@ def inner():
702702
check(get_cell().__closure__[0], size(h + 'P'))
703703
# code
704704
check(get_cell().__code__, size(h + '5i9Pi3P'))
705+
check(get_cell.__code__, size(h + '5i9Pi3P'))
706+
def get_cell2(x):
707+
def inner():
708+
return x
709+
return inner
710+
check(get_cell2.__code__, size(h + '5i9Pi3P') + 1)
705711
# complex
706712
check(complex(0,1), size(h + '2d'))
707713
# method_descriptor (descriptor object)

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ What's New in Python 3.3.0 Beta 2?
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #15456: Fix code __sizeof__ after #12399 change.
14+
Patch by Serhiy Storchaka.
15+
1316
- Issue #15404: Refleak in PyMethodObject repr.
1417

1518
- Issue #15394: An issue in PyModule_Create that caused references to

Objects/codeobject.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,17 @@ code_dealloc(PyCodeObject *co)
374374
PyObject_DEL(co);
375375
}
376376

377+
static PyObject *
378+
code_sizeof(PyCodeObject *co, void *unused)
379+
{
380+
Py_ssize_t res;
381+
382+
res = sizeof(PyCodeObject);
383+
if (co->co_cell2arg != NULL && co->co_cellvars != NULL)
384+
res += PyTuple_GET_SIZE(co->co_cellvars) * sizeof(unsigned char);
385+
return PyLong_FromSsize_t(res);
386+
}
387+
377388
static PyObject *
378389
code_repr(PyCodeObject *co)
379390
{
@@ -480,6 +491,11 @@ code_hash(PyCodeObject *co)
480491

481492
/* XXX code objects need to participate in GC? */
482493

494+
static struct PyMethodDef code_methods[] = {
495+
{"__sizeof__", (PyCFunction)code_sizeof, METH_NOARGS},
496+
{NULL, NULL} /* sentinel */
497+
};
498+
483499
PyTypeObject PyCode_Type = {
484500
PyVarObject_HEAD_INIT(&PyType_Type, 0)
485501
"code",
@@ -508,7 +524,7 @@ PyTypeObject PyCode_Type = {
508524
offsetof(PyCodeObject, co_weakreflist), /* tp_weaklistoffset */
509525
0, /* tp_iter */
510526
0, /* tp_iternext */
511-
0, /* tp_methods */
527+
code_methods, /* tp_methods */
512528
code_memberlist, /* tp_members */
513529
0, /* tp_getset */
514530
0, /* tp_base */

0 commit comments

Comments
 (0)