Skip to content

Commit d44815c

Browse files
authored
GH-88116: Document that PyCodeNew is dangerous, and make PyCode_NewEmpty less dangerous. (GH-91790)
1 parent 5974827 commit d44815c

File tree

4 files changed

+39
-7
lines changed

4 files changed

+39
-7
lines changed

Doc/c-api/code.rst

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,33 @@ bound into a function.
3333
3434
Return the number of free variables in *co*.
3535
36-
.. 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)
36+
.. 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)
3737
3838
Return a new code object. If you need a dummy code object to create a frame,
3939
use :c:func:`PyCode_NewEmpty` instead. Calling :c:func:`PyCode_New` directly
40-
can bind you to a precise Python version since the definition of the bytecode
41-
changes often.
40+
will bind you to a precise Python version since the definition of the bytecode
41+
changes often. The many arguments of this function are inter-dependent in complex
42+
ways, meaning that subtle changes to values are likely to result in incorrect
43+
execution or VM crashes. Use this function only with extreme care.
4244
43-
.. 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)
45+
.. versionchanged:: 3.11
46+
Added ``exceptiontable`` parameter.
47+
48+
.. 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)
4449
4550
Similar to :c:func:`PyCode_New`, but with an extra "posonlyargcount" for positional-only arguments.
51+
The same caveats that apply to ``PyCode_New`` also apply to this function.
4652
4753
.. versionadded:: 3.8
4854
55+
.. versionchanged:: 3.11
56+
Added ``exceptiontable`` parameter.
57+
4958
.. c:function:: PyCodeObject* PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno)
5059
5160
Return a new empty code object with the specified filename,
52-
function name, and first line number. It is illegal to
53-
:func:`exec` or :func:`eval` the resulting code object.
61+
function name, and first line number. The resulting code
62+
object will raise an ``Exception`` if executed.
5463
5564
.. c:function:: int PyCode_Addr2Line(PyCodeObject *co, int byte_offset)
5665

Doc/whatsnew/3.11.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,6 +1159,12 @@ C API Changes
11591159
as its second parameter, instead of ``PyFrameObject*``.
11601160
See :pep:`523` for more details of how to use this function pointer type.
11611161

1162+
* :c:func:`PyCode_New` and :c:func:`PyCode_NewWithPosOnlyArgs` now take
1163+
an additional ``exception_table`` argument.
1164+
Using these functions should be avoided, if at all possible.
1165+
To get a custom code object: create a code object using the compiler,
1166+
then get a modified version with the ``replace`` method.
1167+
11621168
New Features
11631169
------------
11641170

Lib/test/test_code.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,9 @@ def test_newempty(self):
176176
self.assertEqual(co.co_filename, "filename")
177177
self.assertEqual(co.co_name, "funcname")
178178
self.assertEqual(co.co_firstlineno, 15)
179+
#Empty code object should raise, but not crash the VM
180+
with self.assertRaises(Exception):
181+
exec(co)
179182

180183
@cpython_only
181184
def test_closure_injection(self):

Objects/codeobject.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -626,12 +626,20 @@ PyCode_New(int argcount, int kwonlyargcount,
626626
exceptiontable);
627627
}
628628

629+
static const char assert0[4] = {
630+
LOAD_ASSERTION_ERROR,
631+
0,
632+
RAISE_VARARGS,
633+
1
634+
};
635+
629636
PyCodeObject *
630637
PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno)
631638
{
632639
PyObject *nulltuple = NULL;
633640
PyObject *filename_ob = NULL;
634641
PyObject *funcname_ob = NULL;
642+
PyObject *code_ob = NULL;
635643
PyCodeObject *result = NULL;
636644

637645
nulltuple = PyTuple_New(0);
@@ -646,27 +654,33 @@ PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno)
646654
if (filename_ob == NULL) {
647655
goto failed;
648656
}
657+
code_ob = PyBytes_FromStringAndSize(assert0, 4);
658+
if (code_ob == NULL) {
659+
goto failed;
660+
}
649661

650662
#define emptystring (PyObject *)&_Py_SINGLETON(bytes_empty)
651663
struct _PyCodeConstructor con = {
652664
.filename = filename_ob,
653665
.name = funcname_ob,
654666
.qualname = funcname_ob,
655-
.code = emptystring,
667+
.code = code_ob,
656668
.firstlineno = firstlineno,
657669
.linetable = emptystring,
658670
.consts = nulltuple,
659671
.names = nulltuple,
660672
.localsplusnames = nulltuple,
661673
.localspluskinds = emptystring,
662674
.exceptiontable = emptystring,
675+
.stacksize = 1,
663676
};
664677
result = _PyCode_New(&con);
665678

666679
failed:
667680
Py_XDECREF(nulltuple);
668681
Py_XDECREF(funcname_ob);
669682
Py_XDECREF(filename_ob);
683+
Py_XDECREF(code_ob);
670684
return result;
671685
}
672686

0 commit comments

Comments
 (0)