diff options
author | Mark Shannon <mark@hotpy.org> | 2022-04-21 18:08:36 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-21 18:08:36 (GMT) |
commit | d44815cabc0a8d9932df2fa95cb374eadddb7c17 (patch) | |
tree | 12cd641cbd5b32d20983b5edb7ac650ea85a434a /Doc | |
parent | 5974827c71d884bb3cc58f07a9eaefafe0cbaa6e (diff) | |
download | cpython-d44815cabc0a8d9932df2fa95cb374eadddb7c17.zip cpython-d44815cabc0a8d9932df2fa95cb374eadddb7c17.tar.gz cpython-d44815cabc0a8d9932df2fa95cb374eadddb7c17.tar.bz2 |
GH-88116: Document that PyCodeNew is dangerous, and make PyCode_NewEmpty less dangerous. (GH-91790)
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/c-api/code.rst | 21 | ||||
-rw-r--r-- | Doc/whatsnew/3.11.rst | 6 |
2 files changed, 21 insertions, 6 deletions
diff --git a/Doc/c-api/code.rst b/Doc/c-api/code.rst index 840b842..407d8b4 100644 --- a/Doc/c-api/code.rst +++ b/Doc/c-api/code.rst @@ -33,24 +33,33 @@ bound into a function. Return the number of free variables in *co*. -.. 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) +.. 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 *linetable, PyObject *exceptiontable) 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. + will bind you to a precise Python version since the definition of the bytecode + changes often. The many arguments of this function are inter-dependent in complex + ways, meaning that subtle changes to values are likely to result in incorrect + execution or VM crashes. Use this function only with extreme care. -.. 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) + .. versionchanged:: 3.11 + Added ``exceptiontable`` parameter. + +.. 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 *linetable, PyObject *exceptiontable) Similar to :c:func:`PyCode_New`, but with an extra "posonlyargcount" for positional-only arguments. + The same caveats that apply to ``PyCode_New`` also apply to this function. .. versionadded:: 3.8 + .. versionchanged:: 3.11 + Added ``exceptiontable`` parameter. + .. c:function:: PyCodeObject* PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno) Return a new empty code object with the specified filename, - function name, and first line number. It is illegal to - :func:`exec` or :func:`eval` the resulting code object. + function name, and first line number. The resulting code + object will raise an ``Exception`` if executed. .. c:function:: int PyCode_Addr2Line(PyCodeObject *co, int byte_offset) diff --git a/Doc/whatsnew/3.11.rst b/Doc/whatsnew/3.11.rst index 8d74c9b..c3a8a7e 100644 --- a/Doc/whatsnew/3.11.rst +++ b/Doc/whatsnew/3.11.rst @@ -1159,6 +1159,12 @@ C API Changes as its second parameter, instead of ``PyFrameObject*``. See :pep:`523` for more details of how to use this function pointer type. +* :c:func:`PyCode_New` and :c:func:`PyCode_NewWithPosOnlyArgs` now take + an additional ``exception_table`` argument. + Using these functions should be avoided, if at all possible. + To get a custom code object: create a code object using the compiler, + then get a modified version with the ``replace`` method. + New Features ------------ |