[Python-checkins] (no subject)

Łukasz Langa webhook-mailer at python.org
Mon Jul 1 07:29:19 EDT 2019




To: python-checkins at python.org
Subject: bpo-37221: Add PyCode_NewWithPosOnlyArgs to be used internally and
 set PyCode_New as a compatibility wrapper (GH-13959) (#14505)
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: quoted-printable
MIME-Version: 1.0

https://github.com/python/cpython/commit/cb083f7cdf604c1d9d264f387f9e8846bc95=
3eb3
commit: cb083f7cdf604c1d9d264f387f9e8846bc953eb3
branch: 3.8
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.co=
m>
committer: =C5=81ukasz Langa <lukasz at langa.pl>
date: 2019-07-01T13:29:14+02:00
summary:

bpo-37221: Add PyCode_NewWithPosOnlyArgs to be used internally and set PyCode=
_New as a compatibility wrapper (GH-13959) (#14505)

Add PyCode_NewEx to be used internally and set PyCode_New as a compatibility =
wrapper
(cherry picked from commit 4a2edc34a405150d0b23ecfdcb401e7cf59f4650)

Co-authored-by: Pablo Galindo <Pablogsal at gmail.com>

files:
A Misc/NEWS.d/next/C API/2019-06-11-02-50-38.bpo-37221.4tClQT.rst
M Doc/c-api/code.rst
M Doc/data/refcounts.dat
M Doc/whatsnew/3.8.rst
M Include/code.h
M Objects/codeobject.c
M Python/compile.c
M Python/marshal.c

diff --git a/Doc/c-api/code.rst b/Doc/c-api/code.rst
index 7353df56e7d3..3c4f66923da5 100644
--- a/Doc/c-api/code.rst
+++ b/Doc/c-api/code.rst
@@ -33,20 +33,21 @@ bound into a function.
=20
    Return the number of free variables in *co*.
=20
-.. c:function:: PyCodeObject* PyCode_New(int argcount, int posonlyargcount, =
int kwonlyargcount, int nlocals, int stacksize, int flags, PyObject *code, Py=
Object *consts, PyObject *names, PyObject *varnames, PyObject *freevars, PyOb=
ject *cellvars, PyObject *filename, PyObject *name, int firstlineno, PyObject=
 *lnotab)
+.. c:function:: PyCodeObject* PyCode_New(int argcount, int kwonlyargcount, i=
nt nlocals, int stacksize, int flags, PyObject *code, PyObject *consts, PyObj=
ect *names, PyObject *varnames, PyObject *freevars, PyObject *cellvars, PyObj=
ect *filename, PyObject *name, int firstlineno, PyObject *lnotab)
=20
-   Return a new code object.  If you need a dummy code object to
-   create a frame, use :c:func:`PyCode_NewEmpty` instead.  Calling
-   :c:func:`PyCode_New` directly can bind you to a precise Python
-   version since the definition of the bytecode changes often.
-
-   .. versionchanged:: 3.8
-      An extra parameter is required (*posonlyargcount*) to support :PEP:`57=
0`.
-      The first parameter (*argcount*) now represents the total number of po=
sitional arguments,
-      including positional-only.
+   Return a new code object.  If you need a dummy code object to create a fr=
ame,
+   use :c:func:`PyCode_NewEmpty` instead.  Calling :c:func:`PyCode_New` dire=
ctly
+   can bind you to a precise Python version since the definition of the byte=
code
+   changes often.
=20
    .. audit-event:: code.__new__ code,filename,name,argcount,posonlyargcount=
,kwonlyargcount,nlocals,stacksize,flags c.PyCode_New
=20
+.. c:function:: PyCodeObject* PyCode_NewWithPosOnlyArgs(int argcount, int po=
sonlyargcount, int kwonlyargcount, int nlocals, int stacksize, int flags, PyO=
bject *code, PyObject *consts, PyObject *names, PyObject *varnames, PyObject =
*freevars, PyObject *cellvars, PyObject *filename, PyObject *name, int firstl=
ineno, 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 ch=
ar *funcname, int firstlineno)
=20
    Return a new empty code object with the specified filename,
diff --git a/Doc/data/refcounts.dat b/Doc/data/refcounts.dat
index aca57a1dae9d..fda347eab102 100644
--- a/Doc/data/refcounts.dat
+++ b/Doc/data/refcounts.dat
@@ -234,9 +234,26 @@ PyCode_Check:PyObject*:co:0:
 PyCode_GetNumFree:int:::
 PyCode_GetNumFree:PyCodeObject*:co:0:
=20
+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:int:argcount::
-PyCode_New:int:posonlyargcount::
 PyCode_New:int:kwonlyargcount::
 PyCode_New:int:nlocals::
 PyCode_New:int:stacksize::
diff --git a/Doc/whatsnew/3.8.rst b/Doc/whatsnew/3.8.rst
index 8c2f80ce24e0..f423765c8917 100644
--- a/Doc/whatsnew/3.8.rst
+++ b/Doc/whatsnew/3.8.rst
@@ -1042,6 +1042,11 @@ Build and C API Changes
   allocation or deallocation may need to be adjusted.
   (Contributed by Eddie Elizondo in :issue:`35810`.)
=20
+* 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`.)
+
=20
 Deprecated
 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
diff --git a/Include/code.h b/Include/code.h
index b79d977394e0..3afddd20c80d 100644
--- a/Include/code.h
+++ b/Include/code.h
@@ -120,6 +120,11 @@ PyAPI_DATA(PyTypeObject) PyCode_Type;
=20
 /* Public interface */
 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 *,
         PyObject *, PyObject *, PyObject *, PyObject *,
         PyObject *, PyObject *, int, PyObject *);
diff --git a/Misc/NEWS.d/next/C API/2019-06-11-02-50-38.bpo-37221.4tClQT.rst =
b/Misc/NEWS.d/next/C API/2019-06-11-02-50-38.bpo-37221.4tClQT.rst
new file mode 100644
index 000000000000..0ea8b9b86788
--- /dev/null
+++ b/Misc/NEWS.d/next/C API/2019-06-11-02-50-38.bpo-37221.4tClQT.rst=09
@@ -0,0 +1,3 @@
+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.
diff --git a/Objects/codeobject.c b/Objects/codeobject.c
index 1333cc833e1e..39bf6fc6f228 100644
--- a/Objects/codeobject.c
+++ b/Objects/codeobject.c
@@ -102,14 +102,13 @@ intern_string_constants(PyObject *tuple)
     return modified;
 }
=20
-
 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)
+PyCode_NewWithPosOnlyArgs(int argcount, int posonlyargcount, int kwonlyargco=
unt,
+                          int nlocals, int stacksize, int flags,
+                          PyObject *code, PyObject *consts, PyObject *names,
+                          PyObject *varnames, PyObject *freevars, PyObject *=
cellvars,
+                          PyObject *filename, PyObject *name, int firstlinen=
o,
+                          PyObject *lnotab)
 {
     PyCodeObject *co;
     Py_ssize_t *cell2arg =3D NULL;
@@ -243,6 +242,20 @@ PyCode_New(int argcount, int posonlyargcount, int kwonly=
argcount,
     return co;
 }
=20
+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
 _PyCode_InitOpcache(PyCodeObject *co)
 {
@@ -311,7 +324,8 @@ PyCode_NewEmpty(const char *filename, const char *funcnam=
e, int firstlineno)
     if (filename_ob =3D=3D NULL)
         goto failed;
=20
-    result =3D PyCode_New(0,                      /* argcount */
+    result =3D PyCode_NewWithPosOnlyArgs(
+                0,                    /* argcount */
                 0,                              /* posonlyargcount */
                 0,                              /* kwonlyargcount */
                 0,                              /* nlocals */
@@ -492,12 +506,14 @@ code_new(PyTypeObject *type, PyObject *args, PyObject *=
kw)
     if (ourcellvars =3D=3D NULL)
         goto cleanup;
=20
-    co =3D (PyObject *)PyCode_New(argcount, posonlyargcount, kwonlyargcount,
-                                nlocals, stacksize, flags,
-                                code, consts, ournames, ourvarnames,
-                                ourfreevars, ourcellvars, filename,
-                                name, firstlineno, lnotab);
-  cleanup:
+    co =3D (PyObject *)PyCode_NewWithPosOnlyArgs(argcount, posonlyargcount,
+                                               kwonlyargcount,
+                                               nlocals, stacksize, flags,
+                                               code, consts, ournames,
+                                               ourvarnames, ourfreevars,
+                                               ourcellvars, filename,
+                                               name, firstlineno, lnotab);
+  cleanup:=20
     Py_XDECREF(ournames);
     Py_XDECREF(ourvarnames);
     Py_XDECREF(ourfreevars);
@@ -625,7 +641,7 @@ code_replace_impl(PyCodeObject *self, int co_argcount,
=20
 #undef CHECK_INT_ARG
=20
-    return (PyObject *)PyCode_New(
+    return (PyObject *)PyCode_NewWithPosOnlyArgs(
         co_argcount, co_posonlyargcount, co_kwonlyargcount, co_nlocals,
         co_stacksize, co_flags, (PyObject*)co_code, co_consts, co_names,
         co_varnames, co_freevars, co_cellvars, co_filename, co_name,
diff --git a/Python/compile.c b/Python/compile.c
index 7bdf406079d3..9cce8aeb4e1f 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -5813,13 +5813,11 @@ makecode(struct compiler *c, struct assembler *a)
     if (maxdepth < 0) {
         goto error;
     }
-    co =3D PyCode_New(posonlyargcount+posorkeywordargcount, posonlyargcount,
-                    kwonlyargcount, nlocals_int, maxdepth, flags,
-                    bytecode, consts, names, varnames,
-                    freevars, cellvars,
-                    c->c_filename, c->u->u_name,
-                    c->u->u_firstlineno,
-                    a->a_lnotab);
+    co =3D PyCode_NewWithPosOnlyArgs(posonlyargcount+posorkeywordargcount,
+                                   posonlyargcount, kwonlyargcount, nlocals_=
int,=20
+                                   maxdepth, flags, bytecode, consts, names,
+                                   varnames, freevars, cellvars, c->c_filena=
me,
+                                   c->u->u_name, c->u->u_firstlineno, a->a_l=
notab);
  error:
     Py_XDECREF(consts);
     Py_XDECREF(names);
diff --git a/Python/marshal.c b/Python/marshal.c
index caaddfe9e44e..b2daff2c8a3b 100644
--- a/Python/marshal.c
+++ b/Python/marshal.c
@@ -1396,7 +1396,7 @@ r_object(RFILE *p)
             if (lnotab =3D=3D NULL)
                 goto code_error;
=20
-            v =3D (PyObject *) PyCode_New(
+            v =3D (PyObject *) PyCode_NewWithPosOnlyArgs(
                             argcount, posonlyargcount, kwonlyargcount,
                             nlocals, stacksize, flags,
                             code, consts, names, varnames,



More information about the Python-checkins mailing list