From 5a4466963d763ceaf573f8f9e8fdd2c4d8ed0794 Mon Sep 17 00:00:00 2001 From: Bot <1978924+jbower-fb@users.noreply.github.com> Date: Fri, 8 Nov 2024 09:06:59 -0800 Subject: [PATCH 1/4] Use PyObject_VAR_HEAD for generator objects --- Include/internal/pycore_genobject.h | 2 +- Lib/test/test_sys.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Include/internal/pycore_genobject.h b/Include/internal/pycore_genobject.h index f6d7e6d367177b..359253efc7a028 100644 --- a/Include/internal/pycore_genobject.h +++ b/Include/internal/pycore_genobject.h @@ -13,7 +13,7 @@ extern "C" { /* _PyGenObject_HEAD defines the initial segment of generator and coroutine objects. */ #define _PyGenObject_HEAD(prefix) \ - PyObject_HEAD \ + PyObject_VAR_HEAD \ /* List of weak reference. */ \ PyObject *prefix##_weakreflist; \ /* Name of the generator. */ \ diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py index d839893d2c657e..7328f4eadef37c 100644 --- a/Lib/test/test_sys.py +++ b/Lib/test/test_sys.py @@ -1641,7 +1641,7 @@ def bar(cls): check(bar, size('PP')) # generator def get_gen(): yield 1 - check(get_gen(), size('6P4c' + INTERPRETER_FRAME + 'P')) + check(get_gen(), size('7P4c' + INTERPRETER_FRAME + 'P')) # iterator check(iter('abc'), size('lP')) # callable-iterator From 5241130a2bc29553f13e88dc1dfe3d6646609bf6 Mon Sep 17 00:00:00 2001 From: Bot <1978924+jbower-fb@users.noreply.github.com> Date: Mon, 18 Nov 2024 22:13:27 -0800 Subject: [PATCH 2/4] Remove explicit __sizeof__() definition and add another size check in test_sys --- Lib/test/test_sys.py | 6 ++++-- Objects/genobject.c | 16 ---------------- 2 files changed, 4 insertions(+), 18 deletions(-) diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py index 7328f4eadef37c..71b5e786bcaba5 100644 --- a/Lib/test/test_sys.py +++ b/Lib/test/test_sys.py @@ -1640,8 +1640,10 @@ def bar(cls): # classmethod check(bar, size('PP')) # generator - def get_gen(): yield 1 - check(get_gen(), size('7P4c' + INTERPRETER_FRAME + 'P')) + def get_gen1(): yield + check(get_gen1(), size('7P4c' + INTERPRETER_FRAME + 'P')) + def get_gen2(): _ = yield + check(get_gen2(), size('7P4c' + INTERPRETER_FRAME + 'PP')) # iterator check(iter('abc'), size('lP')) # callable-iterator diff --git a/Objects/genobject.c b/Objects/genobject.c index 19c2c4e3331a89..7282bc6535dbd2 100644 --- a/Objects/genobject.c +++ b/Objects/genobject.c @@ -814,24 +814,10 @@ static PyMemberDef gen_memberlist[] = { {NULL} /* Sentinel */ }; -static PyObject * -gen_sizeof(PyGenObject *gen, PyObject *Py_UNUSED(ignored)) -{ - Py_ssize_t res; - res = offsetof(PyGenObject, gi_iframe) + offsetof(_PyInterpreterFrame, localsplus); - PyCodeObject *code = _PyGen_GetCode(gen); - res += _PyFrame_NumSlotsForCodeObject(code) * sizeof(PyObject *); - return PyLong_FromSsize_t(res); -} - -PyDoc_STRVAR(sizeof__doc__, -"gen.__sizeof__() -> size of gen in memory, in bytes"); - static PyMethodDef gen_methods[] = { {"send", gen_send, METH_O, send_doc}, {"throw", _PyCFunction_CAST(gen_throw), METH_FASTCALL, throw_doc}, {"close", gen_close, METH_NOARGS, close_doc}, - {"__sizeof__", (PyCFunction)gen_sizeof, METH_NOARGS, sizeof__doc__}, {"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")}, {NULL, NULL} /* Sentinel */ }; @@ -1192,7 +1178,6 @@ static PyMethodDef coro_methods[] = { {"send", gen_send, METH_O, coro_send_doc}, {"throw",_PyCFunction_CAST(gen_throw), METH_FASTCALL, coro_throw_doc}, {"close", gen_close, METH_NOARGS, coro_close_doc}, - {"__sizeof__", (PyCFunction)gen_sizeof, METH_NOARGS, sizeof__doc__}, {"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")}, {NULL, NULL} /* Sentinel */ }; @@ -1620,7 +1605,6 @@ static PyMethodDef async_gen_methods[] = { {"asend", (PyCFunction)async_gen_asend, METH_O, async_asend_doc}, {"athrow",(PyCFunction)async_gen_athrow, METH_VARARGS, async_athrow_doc}, {"aclose", (PyCFunction)async_gen_aclose, METH_NOARGS, async_aclose_doc}, - {"__sizeof__", (PyCFunction)gen_sizeof, METH_NOARGS, sizeof__doc__}, {"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")}, {NULL, NULL} /* Sentinel */ From 914d32f1fdf30adc74f4d309af897b46261cc3f7 Mon Sep 17 00:00:00 2001 From: Bot <1978924+jbower-fb@users.noreply.github.com> Date: Mon, 18 Nov 2024 22:30:06 -0800 Subject: [PATCH 3/4] Add NEWS entry. --- .../2024-11-18-22-29-58.gh-issue-126596.66G_sl.rst | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2024-11-18-22-29-58.gh-issue-126596.66G_sl.rst diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2024-11-18-22-29-58.gh-issue-126596.66G_sl.rst b/Misc/NEWS.d/next/Core_and_Builtins/2024-11-18-22-29-58.gh-issue-126596.66G_sl.rst new file mode 100644 index 00000000000000..1fee7fb7e05b6f --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2024-11-18-22-29-58.gh-issue-126596.66G_sl.rst @@ -0,0 +1,10 @@ +Generator objects now have a valid `ob_size` field. + +#.. section: Library #.. section: Documentation #.. section: Tests #.. +section: Build #.. section: Windows #.. section: macOS #.. section: IDLE #.. +section: Tools/Demos #.. section: C API + +# Write your Misc/NEWS.d entry below. It should be a simple ReST paragraph. +# Don't start with "- Issue #: " or "- gh-issue-: " or that sort of +stuff. +########################################################################### From bc714b2b620cb27103dc8db9732e5a029192df1b Mon Sep 17 00:00:00 2001 From: Bot <1978924+jbower-fb@users.noreply.github.com> Date: Mon, 18 Nov 2024 22:33:46 -0800 Subject: [PATCH 4/4] Fix lint on NEWS. --- .../2024-11-18-22-29-58.gh-issue-126596.66G_sl.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2024-11-18-22-29-58.gh-issue-126596.66G_sl.rst b/Misc/NEWS.d/next/Core_and_Builtins/2024-11-18-22-29-58.gh-issue-126596.66G_sl.rst index 1fee7fb7e05b6f..b88ad61e1b0429 100644 --- a/Misc/NEWS.d/next/Core_and_Builtins/2024-11-18-22-29-58.gh-issue-126596.66G_sl.rst +++ b/Misc/NEWS.d/next/Core_and_Builtins/2024-11-18-22-29-58.gh-issue-126596.66G_sl.rst @@ -1,4 +1,4 @@ -Generator objects now have a valid `ob_size` field. +Generator objects now have a valid ``ob_size`` field. #.. section: Library #.. section: Documentation #.. section: Tests #.. section: Build #.. section: Windows #.. section: macOS #.. section: IDLE #..