Commit 4a2edc34 authored by Pablo Galindo's avatar Pablo Galindo Committed by Petr Viktorin

bpo-37221: Add PyCode_NewWithPosOnlyArgs to be used internally and set...

bpo-37221: Add PyCode_NewWithPosOnlyArgs to be used internally and set PyCode_New as a compatibility wrapper (GH-13959)

Add PyCode_NewEx to be used internally and set PyCode_New as a compatibility wrapper
parent fc1fbe60
...@@ -33,20 +33,21 @@ bound into a function. ...@@ -33,20 +33,21 @@ bound into a function.
Return the number of free variables in *co*. Return the number of free variables in *co*.
.. c:function:: PyCodeObject* PyCode_New(int argcount, int posonlyargcount, int kwonlyargcount, int nlocals, int stacksize, int flags, PyObject *code, PyObject *consts, PyObject *names, PyObject *varnames, PyObject *freevars, PyObject *cellvars, PyObject *filename, PyObject *name, int firstlineno, PyObject *lnotab) .. c:function:: PyCodeObject* PyCode_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, int firstlineno, PyObject *lnotab)
Return a new code object. If you need a dummy code object to Return a new code object. If you need a dummy code object to create a frame,
create a frame, use :c:func:`PyCode_NewEmpty` instead. Calling use :c:func:`PyCode_NewEmpty` instead. Calling :c:func:`PyCode_New` directly
:c:func:`PyCode_New` directly can bind you to a precise Python can bind you to a precise Python version since the definition of the bytecode
version since the definition of the bytecode changes often. changes often.
.. versionchanged:: 3.8
An extra parameter is required (*posonlyargcount*) to support :PEP:`570`.
The first parameter (*argcount*) now represents the total number of positional arguments,
including positional-only.
.. audit-event:: code.__new__ code,filename,name,argcount,posonlyargcount,kwonlyargcount,nlocals,stacksize,flags c.PyCode_New .. audit-event:: code.__new__ code,filename,name,argcount,posonlyargcount,kwonlyargcount,nlocals,stacksize,flags c.PyCode_New
.. c:function:: PyCodeObject* PyCode_NewWithPosOnlyArgs(int argcount, int posonlyargcount, int kwonlyargcount, int nlocals, int stacksize, int flags, PyObject *code, PyObject *consts, PyObject *names, PyObject *varnames, PyObject *freevars, PyObject *cellvars, PyObject *filename, PyObject *name, int firstlineno, PyObject *lnotab)
Similar to :c:func:`PyCode_New`, but with an extra "posonlyargcount" for positonal-only arguments.
.. versionadded:: 3.8
.. c:function:: PyCodeObject* PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno) .. c:function:: PyCodeObject* PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno)
Return a new empty code object with the specified filename, Return a new empty code object with the specified filename,
......
...@@ -234,9 +234,26 @@ PyCode_Check:PyObject*:co:0: ...@@ -234,9 +234,26 @@ PyCode_Check:PyObject*:co:0:
PyCode_GetNumFree:int::: PyCode_GetNumFree:int:::
PyCode_GetNumFree:PyCodeObject*:co:0: PyCode_GetNumFree:PyCodeObject*:co:0:
PyCode_NewWithPosOnlyArgs:PyCodeObject*::+1:
PyCode_NewWithPosOnlyArgs:int:argcount::
PyCode_NewWithPosOnlyArgs:int:posonlyargcount::
PyCode_NewWithPosOnlyArgs:int:kwonlyargcount::
PyCode_NewWithPosOnlyArgs:int:nlocals::
PyCode_NewWithPosOnlyArgs:int:stacksize::
PyCode_NewWithPosOnlyArgs:int:flags::
PyCode_NewWithPosOnlyArgs:PyObject*:code:0:
PyCode_NewWithPosOnlyArgs:PyObject*:consts:0:
PyCode_NewWithPosOnlyArgs:PyObject*:names:0:
PyCode_NewWithPosOnlyArgs:PyObject*:varnames:0:
PyCode_NewWithPosOnlyArgs:PyObject*:freevars:0:
PyCode_NewWithPosOnlyArgs:PyObject*:cellvars:0:
PyCode_NewWithPosOnlyArgs:PyObject*:filename:0:
PyCode_NewWithPosOnlyArgs:PyObject*:name:0:
PyCode_NewWithPosOnlyArgs:int:firstlineno::
PyCode_NewWithPosOnlyArgs:PyObject*:lnotab:0:
PyCode_New:PyCodeObject*::+1: PyCode_New:PyCodeObject*::+1:
PyCode_New:int:argcount:: PyCode_New:int:argcount::
PyCode_New:int:posonlyargcount::
PyCode_New:int:kwonlyargcount:: PyCode_New:int:kwonlyargcount::
PyCode_New:int:nlocals:: PyCode_New:int:nlocals::
PyCode_New:int:stacksize:: PyCode_New:int:stacksize::
......
...@@ -1045,6 +1045,11 @@ Build and C API Changes ...@@ -1045,6 +1045,11 @@ Build and C API Changes
allocation or deallocation may need to be adjusted. allocation or deallocation may need to be adjusted.
(Contributed by Eddie Elizondo in :issue:`35810`.) (Contributed by Eddie Elizondo in :issue:`35810`.)
* The new function :c:func:`PyCode_NewWithPosOnlyArgs` allows to create
code objects like :c:func:`PyCode_New`, but with an extra *posonlyargcount*
parameter for indicating the number of positional-only arguments.
(Contributed by Pablo Galindo in :issue:`37221`.)
Deprecated Deprecated
========== ==========
......
...@@ -120,6 +120,11 @@ PyAPI_DATA(PyTypeObject) PyCode_Type; ...@@ -120,6 +120,11 @@ PyAPI_DATA(PyTypeObject) PyCode_Type;
/* Public interface */ /* Public interface */
PyAPI_FUNC(PyCodeObject *) PyCode_New( PyAPI_FUNC(PyCodeObject *) PyCode_New(
int, int, int, int, int, PyObject *, PyObject *,
PyObject *, PyObject *, PyObject *, PyObject *,
PyObject *, PyObject *, int, PyObject *);
PyAPI_FUNC(PyCodeObject *) PyCode_NewWithPosOnlyArgs(
int, int, int, int, int, int, PyObject *, PyObject *, int, int, int, int, int, int, PyObject *, PyObject *,
PyObject *, PyObject *, PyObject *, PyObject *, PyObject *, PyObject *, PyObject *, PyObject *,
PyObject *, PyObject *, int, PyObject *); PyObject *, PyObject *, int, PyObject *);
......
The new function :c:func:`PyCode_NewWithPosOnlyArgs` allows to create
code objects like :c:func:`PyCode_New`, but with an extra *posonlyargcount*
parameter for indicating the number of positonal-only arguments.
...@@ -102,14 +102,13 @@ intern_string_constants(PyObject *tuple) ...@@ -102,14 +102,13 @@ intern_string_constants(PyObject *tuple)
return modified; return modified;
} }
PyCodeObject * PyCodeObject *
PyCode_New(int argcount, int posonlyargcount, int kwonlyargcount, PyCode_NewWithPosOnlyArgs(int argcount, int posonlyargcount, int kwonlyargcount,
int nlocals, int stacksize, int flags, int nlocals, int stacksize, int flags,
PyObject *code, PyObject *consts, PyObject *names, PyObject *code, PyObject *consts, PyObject *names,
PyObject *varnames, PyObject *freevars, PyObject *cellvars, PyObject *varnames, PyObject *freevars, PyObject *cellvars,
PyObject *filename, PyObject *name, int firstlineno, PyObject *filename, PyObject *name, int firstlineno,
PyObject *lnotab) PyObject *lnotab)
{ {
PyCodeObject *co; PyCodeObject *co;
Py_ssize_t *cell2arg = NULL; Py_ssize_t *cell2arg = NULL;
...@@ -243,6 +242,20 @@ PyCode_New(int argcount, int posonlyargcount, int kwonlyargcount, ...@@ -243,6 +242,20 @@ PyCode_New(int argcount, int posonlyargcount, int kwonlyargcount,
return co; return co;
} }
PyCodeObject *
PyCode_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, int firstlineno,
PyObject *lnotab)
{
return PyCode_NewWithPosOnlyArgs(argcount, 0, kwonlyargcount, nlocals,
stacksize, flags, code, consts, names,
varnames, freevars, cellvars, filename,
name, firstlineno, lnotab);
}
int int
_PyCode_InitOpcache(PyCodeObject *co) _PyCode_InitOpcache(PyCodeObject *co)
{ {
...@@ -311,7 +324,8 @@ PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno) ...@@ -311,7 +324,8 @@ PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno)
if (filename_ob == NULL) if (filename_ob == NULL)
goto failed; goto failed;
result = PyCode_New(0, /* argcount */ result = PyCode_NewWithPosOnlyArgs(
0, /* argcount */
0, /* posonlyargcount */ 0, /* posonlyargcount */
0, /* kwonlyargcount */ 0, /* kwonlyargcount */
0, /* nlocals */ 0, /* nlocals */
...@@ -492,12 +506,14 @@ code_new(PyTypeObject *type, PyObject *args, PyObject *kw) ...@@ -492,12 +506,14 @@ code_new(PyTypeObject *type, PyObject *args, PyObject *kw)
if (ourcellvars == NULL) if (ourcellvars == NULL)
goto cleanup; goto cleanup;
co = (PyObject *)PyCode_New(argcount, posonlyargcount, kwonlyargcount, co = (PyObject *)PyCode_NewWithPosOnlyArgs(argcount, posonlyargcount,
nlocals, stacksize, flags, kwonlyargcount,
code, consts, ournames, ourvarnames, nlocals, stacksize, flags,
ourfreevars, ourcellvars, filename, code, consts, ournames,
name, firstlineno, lnotab); ourvarnames, ourfreevars,
cleanup: ourcellvars, filename,
name, firstlineno, lnotab);
cleanup:
Py_XDECREF(ournames); Py_XDECREF(ournames);
Py_XDECREF(ourvarnames); Py_XDECREF(ourvarnames);
Py_XDECREF(ourfreevars); Py_XDECREF(ourfreevars);
...@@ -625,7 +641,7 @@ code_replace_impl(PyCodeObject *self, int co_argcount, ...@@ -625,7 +641,7 @@ code_replace_impl(PyCodeObject *self, int co_argcount,
#undef CHECK_INT_ARG #undef CHECK_INT_ARG
return (PyObject *)PyCode_New( return (PyObject *)PyCode_NewWithPosOnlyArgs(
co_argcount, co_posonlyargcount, co_kwonlyargcount, co_nlocals, co_argcount, co_posonlyargcount, co_kwonlyargcount, co_nlocals,
co_stacksize, co_flags, (PyObject*)co_code, co_consts, co_names, co_stacksize, co_flags, (PyObject*)co_code, co_consts, co_names,
co_varnames, co_freevars, co_cellvars, co_filename, co_name, co_varnames, co_freevars, co_cellvars, co_filename, co_name,
......
...@@ -5813,13 +5813,11 @@ makecode(struct compiler *c, struct assembler *a) ...@@ -5813,13 +5813,11 @@ makecode(struct compiler *c, struct assembler *a)
if (maxdepth < 0) { if (maxdepth < 0) {
goto error; goto error;
} }
co = PyCode_New(posonlyargcount+posorkeywordargcount, posonlyargcount, co = PyCode_NewWithPosOnlyArgs(posonlyargcount+posorkeywordargcount,
kwonlyargcount, nlocals_int, maxdepth, flags, posonlyargcount, kwonlyargcount, nlocals_int,
bytecode, consts, names, varnames, maxdepth, flags, bytecode, consts, names,
freevars, cellvars, varnames, freevars, cellvars, c->c_filename,
c->c_filename, c->u->u_name, c->u->u_name, c->u->u_firstlineno, a->a_lnotab);
c->u->u_firstlineno,
a->a_lnotab);
error: error:
Py_XDECREF(consts); Py_XDECREF(consts);
Py_XDECREF(names); Py_XDECREF(names);
......
...@@ -1396,7 +1396,7 @@ r_object(RFILE *p) ...@@ -1396,7 +1396,7 @@ r_object(RFILE *p)
if (lnotab == NULL) if (lnotab == NULL)
goto code_error; goto code_error;
v = (PyObject *) PyCode_New( v = (PyObject *) PyCode_NewWithPosOnlyArgs(
argcount, posonlyargcount, kwonlyargcount, argcount, posonlyargcount, kwonlyargcount,
nlocals, stacksize, flags, nlocals, stacksize, flags,
code, consts, names, varnames, code, consts, names, varnames,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment