Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-115756: make PyCode_GetFirstFree an unstable API #115781

Merged
merged 7 commits into from Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion Doc/c-api/code.rst
Expand Up @@ -34,10 +34,16 @@ bound into a function.

Return the number of free variables in a code object.

.. c:function:: int PyCode_GetFirstFree(PyCodeObject *co)
.. c:function:: int PyUnstable_Code_GetFirstFree(PyCodeObject *co)

Return the position of the first free variable in a code object.

.. versionchanged:: 3.13

Renamed from ``PyCode_GetFirstFree`` as part of :ref:`unstable-c-api`.
The old name is deprecated, but will remain available until the
signature changes again.

.. c:function:: PyCodeObject* PyUnstable_Code_New(int argcount, int kwonlyargcount, int nlocals, int stacksize, int flags, PyObject *code, PyObject *consts, PyObject *names, PyObject *varnames, PyObject *freevars, PyObject *cellvars, PyObject *filename, PyObject *name, PyObject *qualname, int firstlineno, PyObject *linetable, PyObject *exceptiontable)

Return a new code object. If you need a dummy code object to create a frame,
Expand Down
4 changes: 4 additions & 0 deletions Doc/whatsnew/3.13.rst
Expand Up @@ -1394,6 +1394,10 @@ Changes in the Python API
than directories only. Users may add a trailing slash to match only
directories.

* :c:func:`!PyCode_GetFirstFree` is an ustable API now and has been renamed
to :c:func:`PyUnstable_Code_GetFirstFree`.
(Contributed by Bogdan Romanyuk in :gh:`115781`)


Build Changes
=============
Expand Down
6 changes: 5 additions & 1 deletion Include/cpython/code.h
Expand Up @@ -226,11 +226,15 @@ static inline Py_ssize_t PyCode_GetNumFree(PyCodeObject *op) {
return op->co_nfreevars;
}

static inline int PyCode_GetFirstFree(PyCodeObject *op) {
static inline int PyUnstable_Code_GetFirstFree(PyCodeObject *op) {
assert(PyCode_Check(op));
return op->co_nlocalsplus - op->co_nfreevars;
}

Copy link
Member

@vstinner vstinner Mar 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the deprecation has no detail, for me, it can be removed as soon as in Python 3.15. But the doc is more specific:

"The old name is deprecated, but will remain available until the signature changes again."

Please add a comment to explain it here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the deprecation has no detail, for me, it can be removed in Python 3.15.

What are you basing this on?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PEP 387: Similarly a feature cannot be removed without notice between any two consecutive releases.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry, I didn't disable automerge after your comment. I didn't mean to ignore you :(

But IMO, the “Basic Policy” is just a summary. The “Making Incompatible Changes” section has more detailed instructions.
If you disagree with my reading, let's take it to Discourse; this isn't a good place for it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's just that this function has a specific comment in the doc, IMO it's worth it to repeat it in the header to avoid future eagger removal. But well, now that the change is merged, it's maybe no longer worth it.

Py_DEPRECATED(3.13) static inline int PyCode_GetFirstFree(PyCodeObject *op) {
return PyUnstable_Code_GetFirstFree(op);
}

#define _PyCode_CODE(CO) _Py_RVALUE((_Py_CODEUNIT *)(CO)->co_code_adaptive)
#define _PyCode_NBYTES(CO) (Py_SIZE(CO) * (Py_ssize_t)sizeof(_Py_CODEUNIT))

Expand Down
@@ -0,0 +1,3 @@
:c:func:`!PyCode_GetFirstFree` is an ustable API now and has been renamed to
:c:func:`PyUnstable_Code_GetFirstFree`. (Contributed by Bogdan Romanyuk in
:gh:`115781`)
2 changes: 1 addition & 1 deletion Objects/frameobject.c
Expand Up @@ -1140,7 +1140,7 @@ frame_init_get_vars(_PyInterpreterFrame *frame)

/* Free vars have not been initialized -- Do that */
PyObject *closure = ((PyFunctionObject *)frame->f_funcobj)->func_closure;
int offset = PyCode_GetFirstFree(co);
int offset = PyUnstable_Code_GetFirstFree(co);
for (int i = 0; i < co->co_nfreevars; ++i) {
PyObject *o = PyTuple_GET_ITEM(closure, i);
frame->localsplus[offset + i] = Py_NewRef(o);
Expand Down
2 changes: 1 addition & 1 deletion Objects/typeobject.c
Expand Up @@ -10814,7 +10814,7 @@ super_init_without_args(_PyInterpreterFrame *cframe, PyCodeObject *co,

// Look for __class__ in the free vars.
PyTypeObject *type = NULL;
int i = PyCode_GetFirstFree(co);
int i = PyUnstable_Code_GetFirstFree(co);
for (; i < co->co_nlocalsplus; i++) {
assert((_PyLocals_GetKind(co->co_localspluskinds, i) & CO_FAST_FREE) != 0);
PyObject *name = PyTuple_GET_ITEM(co->co_localsplusnames, i);
Expand Down
2 changes: 1 addition & 1 deletion Python/ceval.c
Expand Up @@ -2898,7 +2898,7 @@ _PyEval_FormatExcUnbound(PyThreadState *tstate, PyCodeObject *co, int oparg)
if (_PyErr_Occurred(tstate))
return;
name = PyTuple_GET_ITEM(co->co_localsplusnames, oparg);
if (oparg < PyCode_GetFirstFree(co)) {
if (oparg < PyUnstable_Code_GetFirstFree(co)) {
_PyEval_FormatExcCheckArg(tstate, PyExc_UnboundLocalError,
UNBOUNDLOCAL_ERROR_MSG, name);
} else {
Expand Down
2 changes: 1 addition & 1 deletion Python/compile.c
Expand Up @@ -1830,7 +1830,7 @@ compiler_make_closure(struct compiler *c, location loc,
PyCodeObject *co, Py_ssize_t flags)
{
if (co->co_nfreevars) {
int i = PyCode_GetFirstFree(co);
int i = PyUnstable_Code_GetFirstFree(co);
for (; i < co->co_nlocalsplus; ++i) {
/* Bypass com_addop_varname because it will generate
LOAD_DEREF but LOAD_CLOSURE is needed.
Expand Down