mirror of https://github.com/python/cpython
gh-115756: make PyCode_GetFirstFree an unstable API (GH-115781)
This commit is contained in:
parent
a3cf0fada0
commit
a8e93d3dca
|
@ -34,10 +34,16 @@ bound into a function.
|
||||||
|
|
||||||
Return the number of free variables in a code object.
|
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.
|
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)
|
.. 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,
|
Return a new code object. If you need a dummy code object to create a frame,
|
||||||
|
|
|
@ -1471,6 +1471,10 @@ Changes in the Python API
|
||||||
than directories only. Users may add a trailing slash to match only
|
than directories only. Users may add a trailing slash to match only
|
||||||
directories.
|
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
|
Build Changes
|
||||||
=============
|
=============
|
||||||
|
|
|
@ -226,11 +226,15 @@ static inline Py_ssize_t PyCode_GetNumFree(PyCodeObject *op) {
|
||||||
return op->co_nfreevars;
|
return op->co_nfreevars;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline int PyCode_GetFirstFree(PyCodeObject *op) {
|
static inline int PyUnstable_Code_GetFirstFree(PyCodeObject *op) {
|
||||||
assert(PyCode_Check(op));
|
assert(PyCode_Check(op));
|
||||||
return op->co_nlocalsplus - op->co_nfreevars;
|
return op->co_nlocalsplus - op->co_nfreevars;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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_CODE(CO) _Py_RVALUE((_Py_CODEUNIT *)(CO)->co_code_adaptive)
|
||||||
#define _PyCode_NBYTES(CO) (Py_SIZE(CO) * (Py_ssize_t)sizeof(_Py_CODEUNIT))
|
#define _PyCode_NBYTES(CO) (Py_SIZE(CO) * (Py_ssize_t)sizeof(_Py_CODEUNIT))
|
||||||
|
|
||||||
|
|
|
@ -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`)
|
|
@ -1140,7 +1140,7 @@ frame_init_get_vars(_PyInterpreterFrame *frame)
|
||||||
|
|
||||||
/* Free vars have not been initialized -- Do that */
|
/* Free vars have not been initialized -- Do that */
|
||||||
PyObject *closure = ((PyFunctionObject *)frame->f_funcobj)->func_closure;
|
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) {
|
for (int i = 0; i < co->co_nfreevars; ++i) {
|
||||||
PyObject *o = PyTuple_GET_ITEM(closure, i);
|
PyObject *o = PyTuple_GET_ITEM(closure, i);
|
||||||
frame->localsplus[offset + i] = Py_NewRef(o);
|
frame->localsplus[offset + i] = Py_NewRef(o);
|
||||||
|
|
|
@ -10884,7 +10884,7 @@ super_init_without_args(_PyInterpreterFrame *cframe, PyCodeObject *co,
|
||||||
|
|
||||||
// Look for __class__ in the free vars.
|
// Look for __class__ in the free vars.
|
||||||
PyTypeObject *type = NULL;
|
PyTypeObject *type = NULL;
|
||||||
int i = PyCode_GetFirstFree(co);
|
int i = PyUnstable_Code_GetFirstFree(co);
|
||||||
for (; i < co->co_nlocalsplus; i++) {
|
for (; i < co->co_nlocalsplus; i++) {
|
||||||
assert((_PyLocals_GetKind(co->co_localspluskinds, i) & CO_FAST_FREE) != 0);
|
assert((_PyLocals_GetKind(co->co_localspluskinds, i) & CO_FAST_FREE) != 0);
|
||||||
PyObject *name = PyTuple_GET_ITEM(co->co_localsplusnames, i);
|
PyObject *name = PyTuple_GET_ITEM(co->co_localsplusnames, i);
|
||||||
|
|
|
@ -2903,7 +2903,7 @@ _PyEval_FormatExcUnbound(PyThreadState *tstate, PyCodeObject *co, int oparg)
|
||||||
if (_PyErr_Occurred(tstate))
|
if (_PyErr_Occurred(tstate))
|
||||||
return;
|
return;
|
||||||
name = PyTuple_GET_ITEM(co->co_localsplusnames, oparg);
|
name = PyTuple_GET_ITEM(co->co_localsplusnames, oparg);
|
||||||
if (oparg < PyCode_GetFirstFree(co)) {
|
if (oparg < PyUnstable_Code_GetFirstFree(co)) {
|
||||||
_PyEval_FormatExcCheckArg(tstate, PyExc_UnboundLocalError,
|
_PyEval_FormatExcCheckArg(tstate, PyExc_UnboundLocalError,
|
||||||
UNBOUNDLOCAL_ERROR_MSG, name);
|
UNBOUNDLOCAL_ERROR_MSG, name);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -1830,7 +1830,7 @@ compiler_make_closure(struct compiler *c, location loc,
|
||||||
PyCodeObject *co, Py_ssize_t flags)
|
PyCodeObject *co, Py_ssize_t flags)
|
||||||
{
|
{
|
||||||
if (co->co_nfreevars) {
|
if (co->co_nfreevars) {
|
||||||
int i = PyCode_GetFirstFree(co);
|
int i = PyUnstable_Code_GetFirstFree(co);
|
||||||
for (; i < co->co_nlocalsplus; ++i) {
|
for (; i < co->co_nlocalsplus; ++i) {
|
||||||
/* Bypass com_addop_varname because it will generate
|
/* Bypass com_addop_varname because it will generate
|
||||||
LOAD_DEREF but LOAD_CLOSURE is needed.
|
LOAD_DEREF but LOAD_CLOSURE is needed.
|
||||||
|
|
Loading…
Reference in New Issue