* Make internal APIs that take PyFrameConstructor take a PyFunctionObject instead.
* Add reference to function to frame, borrow references to builtins and globals.
* Add COPY_FREE_VARS instruction to allow specialization of calls to inner functions.
``i`` is no longer offset by the length of ``co_varnames``.
+.. opcode:: COPY_FREE_VARS (n)
+
+ Copies the ``n`` free variables from the closure into the frame.
+ Removes the need for special code on the caller's side when calling
+ closures.
+
+ .. versionadded:: 3.11
+
+
.. opcode:: RAISE_VARARGS (argc)
Raises an exception using one of the 3 forms of the ``raise`` statement,
#define PyFunction_GET_ANNOTATIONS(func) \
(((PyFunctionObject *)func) -> func_annotations)
-#define PyFunction_AS_FRAME_CONSTRUCTOR(func) \
- ((PyFrameConstructor *)&((PyFunctionObject *)(func))->func_globals)
-
/* The classmethod and staticmethod types lives here, too */
PyAPI_DATA(PyTypeObject) PyClassMethod_Type;
PyAPI_DATA(PyTypeObject) PyStaticMethod_Type;
extern PyObject *
_PyEval_Vector(PyThreadState *tstate,
- PyFrameConstructor *desc, PyObject *locals,
+ PyFunctionObject *func, PyObject *locals,
PyObject* const* args, size_t argcount,
PyObject *kwnames);
struct _interpreter_frame *_PyEval_GetFrame(void);
-PyObject *_Py_MakeCoro(PyFrameConstructor *, struct _interpreter_frame *);
+PyObject *_Py_MakeCoro(PyFunctionObject *func, struct _interpreter_frame *);
#ifdef __cplusplus
}
typedef signed char PyFrameState;
typedef struct _interpreter_frame {
- PyObject *f_globals;
- PyObject *f_builtins;
- PyObject *f_locals;
- PyCodeObject *f_code;
- PyFrameObject *frame_obj;
- /* Borrowed reference to a generator, or NULL */
- PyObject *generator;
+ PyFunctionObject *f_func; /* Strong reference */
+ PyObject *f_globals; /* Borrowed reference */
+ PyObject *f_builtins; /* Borrowed reference */
+ PyObject *f_locals; /* Strong reference, may be NULL */
+ PyCodeObject *f_code; /* Strong reference */
+ PyFrameObject *frame_obj; /* Strong reference, may be NULL */
+ PyObject *generator; /* Borrowed reference, may be NULL */
struct _interpreter_frame *previous;
int f_lasti; /* Last instruction if called */
int stacktop; /* Offset of TOS from localsplus */
#define FRAME_SPECIALS_SIZE ((sizeof(InterpreterFrame)-1)/sizeof(PyObject *))
InterpreterFrame *
-_PyInterpreterFrame_HeapAlloc(PyFrameConstructor *con, PyObject *locals);
+_PyInterpreterFrame_HeapAlloc(PyFunctionObject *func, PyObject *locals);
static inline void
_PyFrame_InitializeSpecials(
- InterpreterFrame *frame, PyFrameConstructor *con,
+ InterpreterFrame *frame, PyFunctionObject *func,
PyObject *locals, int nlocalsplus)
{
- frame->f_code = (PyCodeObject *)Py_NewRef(con->fc_code);
- frame->f_builtins = Py_NewRef(con->fc_builtins);
- frame->f_globals = Py_NewRef(con->fc_globals);
+ Py_INCREF(func);
+ frame->f_func = func;
+ frame->f_code = (PyCodeObject *)Py_NewRef(func->func_code);
+ frame->f_builtins = func->func_builtins;
+ frame->f_globals = func->func_globals;
frame->f_locals = Py_XNewRef(locals);
frame->stacktop = nlocalsplus;
frame->frame_obj = NULL;
_PyFrame_LocalsToFast(InterpreterFrame *frame, int clear);
InterpreterFrame *_PyThreadState_PushFrame(
- PyThreadState *tstate, PyFrameConstructor *con, PyObject *locals);
+ PyThreadState *tstate, PyFunctionObject *func, PyObject *locals);
extern InterpreterFrame *
_PyThreadState_BumpFramePointerSlow(PyThreadState *tstate, size_t size);
--- /dev/null
+#ifndef Py_INTERNAL_FUNCTION_H
+#define Py_INTERNAL_FUNCTION_H
+
+
+#include "Python.h"
+
+PyFunctionObject *
+_PyFunction_FromConstructor(PyFrameConstructor *constr);
+
+
+#endif /* !Py_INTERNAL_FUNCTION_H */
#define SET_ADD 146
#define MAP_ADD 147
#define LOAD_CLASSDEREF 148
+#define COPY_FREE_VARS 149
#define MATCH_CLASS 152
#define FORMAT_VALUE 155
#define BUILD_CONST_KEY_MAP 156
# active exception)
# Python 3.11a3 3464 (bpo-45636: Merge numeric BINARY_*/INPLACE_* into
# BINARY_OP)
+# Python 3.11a3 3465 (Add COPY_FREE_VARS opcode)
#
# MAGIC must change whenever the bytecode emitted by the compiler may no
# Whenever MAGIC_NUMBER is changed, the ranges in the magic_values array
# in PC/launcher.c must also be updated.
-MAGIC_NUMBER = (3464).to_bytes(2, 'little') + b'\r\n'
+MAGIC_NUMBER = (3465).to_bytes(2, 'little') + b'\r\n'
_RAW_MAGIC_NUMBER = int.from_bytes(MAGIC_NUMBER, 'little') # For import.c
_PYCACHE = '__pycache__'
def_op('MAP_ADD', 147)
def_op('LOAD_CLASSDEREF', 148)
hasfree.append(148)
+def_op('COPY_FREE_VARS', 149)
def_op('MATCH_CLASS', 152)
check_impl_detail, requires_debug_ranges,
gc_collect)
from test.support.script_helper import assert_python_ok
+from opcode import opmap
+COPY_FREE_VARS = opmap['COPY_FREE_VARS']
def consts(t):
def new_code(c):
'''A new code object with a __class__ cell added to freevars'''
- return c.replace(co_freevars=c.co_freevars + ('__class__',))
+ return c.replace(co_freevars=c.co_freevars + ('__class__',), co_code=bytes([COPY_FREE_VARS, 1])+c.co_code)
def add_foreign_method(cls, name, f):
code = new_code(f.__code__)
dis_nested_1 = """%s
Disassembly of <code object foo at 0x..., file "%s", line %d>:
- 0 MAKE_CELL 0 (x)
-
-%3d 2 LOAD_CLOSURE 0 (x)
- 4 BUILD_TUPLE 1
- 6 LOAD_CONST 1 (<code object <listcomp> at 0x..., file "%s", line %d>)
- 8 MAKE_FUNCTION 8 (closure)
- 10 LOAD_DEREF 1 (y)
- 12 GET_ITER
- 14 CALL_FUNCTION 1
- 16 RETURN_VALUE
+ 0 COPY_FREE_VARS 1
+ 2 MAKE_CELL 0 (x)
+
+%3d 4 LOAD_CLOSURE 0 (x)
+ 6 BUILD_TUPLE 1
+ 8 LOAD_CONST 1 (<code object <listcomp> at 0x..., file "%s", line %d>)
+ 10 MAKE_FUNCTION 8 (closure)
+ 12 LOAD_DEREF 1 (y)
+ 14 GET_ITER
+ 16 CALL_FUNCTION 1
+ 18 RETURN_VALUE
""" % (dis_nested_0,
__file__,
_h.__code__.co_firstlineno + 1,
dis_nested_2 = """%s
Disassembly of <code object <listcomp> at 0x..., file "%s", line %d>:
-%3d 0 BUILD_LIST 0
- 2 LOAD_FAST 0 (.0)
- >> 4 FOR_ITER 6 (to 18)
- 6 STORE_FAST 1 (z)
- 8 LOAD_DEREF 2 (x)
- 10 LOAD_FAST 1 (z)
- 12 BINARY_OP 0 (+)
- 14 LIST_APPEND 2
- 16 JUMP_ABSOLUTE 2 (to 4)
- >> 18 RETURN_VALUE
+ 0 COPY_FREE_VARS 1
+
+%3d 2 BUILD_LIST 0
+ 4 LOAD_FAST 0 (.0)
+ >> 6 FOR_ITER 6 (to 20)
+ 8 STORE_FAST 1 (z)
+ 10 LOAD_DEREF 2 (x)
+ 12 LOAD_FAST 1 (z)
+ 14 BINARY_OP 0 (+)
+ 16 LIST_APPEND 2
+ 18 JUMP_ABSOLUTE 3 (to 6)
+ >> 20 RETURN_VALUE
""" % (dis_nested_1,
__file__,
_h.__code__.co_firstlineno + 3,
Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=40, starts_line=None, is_jump_target=False, positions=None),
]
-
expected_opinfo_f = [
- Instruction(opname='MAKE_CELL', opcode=135, arg=0, argval='c', argrepr='c', offset=0, starts_line=None, is_jump_target=False, positions=None),
- Instruction(opname='MAKE_CELL', opcode=135, arg=1, argval='d', argrepr='d', offset=2, starts_line=None, is_jump_target=False, positions=None),
- Instruction(opname='LOAD_CONST', opcode=100, arg=4, argval=(5, 6), argrepr='(5, 6)', offset=4, starts_line=3, is_jump_target=False, positions=None),
- Instruction(opname='LOAD_CLOSURE', opcode=136, arg=3, argval='a', argrepr='a', offset=6, starts_line=None, is_jump_target=False, positions=None),
- Instruction(opname='LOAD_CLOSURE', opcode=136, arg=4, argval='b', argrepr='b', offset=8, starts_line=None, is_jump_target=False, positions=None),
- Instruction(opname='LOAD_CLOSURE', opcode=136, arg=0, argval='c', argrepr='c', offset=10, starts_line=None, is_jump_target=False, positions=None),
- Instruction(opname='LOAD_CLOSURE', opcode=136, arg=1, argval='d', argrepr='d', offset=12, starts_line=None, is_jump_target=False, positions=None),
- Instruction(opname='BUILD_TUPLE', opcode=102, arg=4, argval=4, argrepr='', offset=14, starts_line=None, is_jump_target=False, positions=None),
- Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=code_object_inner, argrepr=repr(code_object_inner), offset=16, starts_line=None, is_jump_target=False, positions=None),
- Instruction(opname='MAKE_FUNCTION', opcode=132, arg=9, argval=9, argrepr='defaults, closure', offset=18, starts_line=None, is_jump_target=False, positions=None),
- Instruction(opname='STORE_FAST', opcode=125, arg=2, argval='inner', argrepr='inner', offset=20, starts_line=None, is_jump_target=False, positions=None),
- Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=22, starts_line=5, is_jump_target=False, positions=None),
- Instruction(opname='LOAD_DEREF', opcode=137, arg=3, argval='a', argrepr='a', offset=24, starts_line=None, is_jump_target=False, positions=None),
- Instruction(opname='LOAD_DEREF', opcode=137, arg=4, argval='b', argrepr='b', offset=26, starts_line=None, is_jump_target=False, positions=None),
- Instruction(opname='LOAD_DEREF', opcode=137, arg=0, argval='c', argrepr='c', offset=28, starts_line=None, is_jump_target=False, positions=None),
- Instruction(opname='LOAD_DEREF', opcode=137, arg=1, argval='d', argrepr='d', offset=30, starts_line=None, is_jump_target=False, positions=None),
- Instruction(opname='CALL_FUNCTION', opcode=131, arg=4, argval=4, argrepr='', offset=32, starts_line=None, is_jump_target=False, positions=None),
- Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=34, starts_line=None, is_jump_target=False, positions=None),
- Instruction(opname='LOAD_FAST', opcode=124, arg=2, argval='inner', argrepr='inner', offset=36, starts_line=6, is_jump_target=False, positions=None),
- Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=38, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='COPY_FREE_VARS', opcode=149, arg=2, argval=2, argrepr='', offset=0, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='MAKE_CELL', opcode=135, arg=0, argval='c', argrepr='c', offset=2, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='MAKE_CELL', opcode=135, arg=1, argval='d', argrepr='d', offset=4, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_CONST', opcode=100, arg=4, argval=(5, 6), argrepr='(5, 6)', offset=6, starts_line=3, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_CLOSURE', opcode=136, arg=3, argval='a', argrepr='a', offset=8, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_CLOSURE', opcode=136, arg=4, argval='b', argrepr='b', offset=10, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_CLOSURE', opcode=136, arg=0, argval='c', argrepr='c', offset=12, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_CLOSURE', opcode=136, arg=1, argval='d', argrepr='d', offset=14, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='BUILD_TUPLE', opcode=102, arg=4, argval=4, argrepr='', offset=16, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=code_object_inner, argrepr=repr(code_object_inner), offset=18, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='MAKE_FUNCTION', opcode=132, arg=9, argval=9, argrepr='defaults, closure', offset=20, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='STORE_FAST', opcode=125, arg=2, argval='inner', argrepr='inner', offset=22, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=24, starts_line=5, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_DEREF', opcode=137, arg=3, argval='a', argrepr='a', offset=26, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_DEREF', opcode=137, arg=4, argval='b', argrepr='b', offset=28, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_DEREF', opcode=137, arg=0, argval='c', argrepr='c', offset=30, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_DEREF', opcode=137, arg=1, argval='d', argrepr='d', offset=32, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='CALL_FUNCTION', opcode=131, arg=4, argval=4, argrepr='', offset=34, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=36, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_FAST', opcode=124, arg=2, argval='inner', argrepr='inner', offset=38, starts_line=6, is_jump_target=False, positions=None),
+ Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=40, starts_line=None, is_jump_target=False, positions=None),
]
expected_opinfo_inner = [
- Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=0, starts_line=4, is_jump_target=False, positions=None),
- Instruction(opname='LOAD_DEREF', opcode=137, arg=2, argval='a', argrepr='a', offset=2, starts_line=None, is_jump_target=False, positions=None),
- Instruction(opname='LOAD_DEREF', opcode=137, arg=3, argval='b', argrepr='b', offset=4, starts_line=None, is_jump_target=False, positions=None),
- Instruction(opname='LOAD_DEREF', opcode=137, arg=4, argval='c', argrepr='c', offset=6, starts_line=None, is_jump_target=False, positions=None),
- Instruction(opname='LOAD_DEREF', opcode=137, arg=5, argval='d', argrepr='d', offset=8, starts_line=None, is_jump_target=False, positions=None),
- Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='e', argrepr='e', offset=10, starts_line=None, is_jump_target=False, positions=None),
- Instruction(opname='LOAD_FAST', opcode=124, arg=1, argval='f', argrepr='f', offset=12, starts_line=None, is_jump_target=False, positions=None),
- Instruction(opname='CALL_FUNCTION', opcode=131, arg=6, argval=6, argrepr='', offset=14, starts_line=None, is_jump_target=False, positions=None),
- Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=16, starts_line=None, is_jump_target=False, positions=None),
- Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=18, starts_line=None, is_jump_target=False, positions=None),
- Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=20, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='COPY_FREE_VARS', opcode=149, arg=4, argval=4, argrepr='', offset=0, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=2, starts_line=4, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_DEREF', opcode=137, arg=2, argval='a', argrepr='a', offset=4, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_DEREF', opcode=137, arg=3, argval='b', argrepr='b', offset=6, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_DEREF', opcode=137, arg=4, argval='c', argrepr='c', offset=8, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_DEREF', opcode=137, arg=5, argval='d', argrepr='d', offset=10, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='e', argrepr='e', offset=12, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_FAST', opcode=124, arg=1, argval='f', argrepr='f', offset=14, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='CALL_FUNCTION', opcode=131, arg=6, argval=6, argrepr='', offset=16, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=18, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=20, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=22, starts_line=None, is_jump_target=False, positions=None),
]
expected_opinfo_jumpy = [
$(srcdir)/Include/internal/pycore_fileutils.h \
$(srcdir)/Include/internal/pycore_floatobject.h \
$(srcdir)/Include/internal/pycore_format.h \
+ $(srcdir)/Include/internal/pycore_function.h \
$(srcdir)/Include/internal/pycore_getopt.h \
$(srcdir)/Include/internal/pycore_gil.h \
$(srcdir)/Include/internal/pycore_hamt.h \
--- /dev/null
+Adds new :opcode:`COPY_FREE_VARS` opcode, to make copying of free variables
+from function to frame explicit. Helps optimization of calls to Python
+function.
size_t nargsf, PyObject *kwnames)
{
assert(PyFunction_Check(func));
- PyFrameConstructor *f = PyFunction_AS_FRAME_CONSTRUCTOR(func);
+ PyFunctionObject *f = (PyFunctionObject *)func;
Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
assert(nargs >= 0);
PyThreadState *tstate = _PyThreadState_GET();
assert(nargs == 0 || stack != NULL);
- if (((PyCodeObject *)f->fc_code)->co_flags & CO_OPTIMIZED) {
+ if (((PyCodeObject *)f->func_code)->co_flags & CO_OPTIMIZED) {
return _PyEval_Vector(tstate, f, NULL, stack, nargs, kwnames);
}
else {
- return _PyEval_Vector(tstate, f, f->fc_globals, stack, nargs, kwnames);
+ return _PyEval_Vector(tstate, f, f->func_globals, stack, nargs, kwnames);
}
}
#include "pycore_moduleobject.h" // _PyModule_GetDict()
#include "pycore_object.h" // _PyObject_GC_UNTRACK()
#include "pycore_code.h" // CO_FAST_LOCAL, etc.
+#include "pycore_function.h" // _PyFunction_FromConstructor()
#include "frameobject.h" // PyFrameObject
#include "pycore_frame.h"
/* Don't clear code object until the end */
co = frame->f_code;
frame->f_code = NULL;
- Py_CLEAR(frame->f_globals);
- Py_CLEAR(frame->f_builtins);
+ Py_CLEAR(frame->f_func);
Py_CLEAR(frame->f_locals);
PyObject **locals = _PyFrame_GetLocalsArray(frame);
for (int i = 0; i < frame->stacktop; i++) {
_Py_IDENTIFIER(__builtins__);
static InterpreterFrame *
-allocate_heap_frame(PyFrameConstructor *con, PyObject *locals)
+allocate_heap_frame(PyFunctionObject *func, PyObject *locals)
{
- PyCodeObject *code = (PyCodeObject *)con->fc_code;
+ PyCodeObject *code = (PyCodeObject *)func->func_code;
int size = code->co_nlocalsplus+code->co_stacksize + FRAME_SPECIALS_SIZE;
InterpreterFrame *frame = (InterpreterFrame *)PyMem_Malloc(sizeof(PyObject *)*size);
if (frame == NULL) {
PyErr_NoMemory();
return NULL;
}
- _PyFrame_InitializeSpecials(frame, con, locals, code->co_nlocalsplus);
+ _PyFrame_InitializeSpecials(frame, func, locals, code->co_nlocalsplus);
for (Py_ssize_t i = 0; i < code->co_nlocalsplus; i++) {
frame->localsplus[i] = NULL;
}
.fc_kwdefaults = NULL,
.fc_closure = NULL
};
- InterpreterFrame *frame = allocate_heap_frame(&desc, locals);
+ PyFunctionObject *func = _PyFunction_FromConstructor(&desc);
+ if (func == NULL) {
+ return NULL;
+ }
+ InterpreterFrame *frame = allocate_heap_frame(func, locals);
+ Py_DECREF(func);
if (frame == NULL) {
return NULL;
}
}
co = frame->f_code;
fast = _PyFrame_GetLocalsArray(frame);
+ if (frame->f_lasti < 0 && _Py_OPCODE(co->co_firstinstr[0]) == COPY_FREE_VARS) {
+ /* Free vars have not been initialized -- Do that */
+ PyCodeObject *co = frame->f_code;
+ PyObject *closure = frame->f_func->func_closure;
+ int offset = co->co_nlocals + co->co_nplaincellvars;
+ for (int i = 0; i < co->co_nfreevars; ++i) {
+ PyObject *o = PyTuple_GET_ITEM(closure, i);
+ Py_INCREF(o);
+ frame->localsplus[offset + i] = o;
+ }
+ frame->f_lasti = 0;
+ }
for (int i = 0; i < co->co_nlocalsplus; i++) {
_PyLocals_Kind kind = _PyLocals_GetKind(co->co_localspluskinds, i);
PyObject *value = fast[i];
if (frame->f_state != FRAME_CLEARED) {
if (kind & CO_FAST_FREE) {
- // The cell was set when the frame was created from
- // the function's closure.
+ // The cell was set by COPY_FREE_VARS.
assert(value != NULL && PyCell_Check(value));
value = PyCell_GET(value);
}
static uint32_t next_func_version = 1;
+PyFunctionObject *
+_PyFunction_FromConstructor(PyFrameConstructor *constr)
+{
+
+ PyFunctionObject *op = PyObject_GC_New(PyFunctionObject, &PyFunction_Type);
+ if (op == NULL) {
+ return NULL;
+ }
+ Py_INCREF(constr->fc_globals);
+ op->func_globals = constr->fc_globals;
+ Py_INCREF(constr->fc_builtins);
+ op->func_builtins = constr->fc_builtins;
+ Py_INCREF(constr->fc_name);
+ op->func_name = constr->fc_name;
+ Py_INCREF(constr->fc_qualname);
+ op->func_qualname = constr->fc_qualname;
+ Py_INCREF(constr->fc_code);
+ op->func_code = constr->fc_code;
+ op->func_defaults = NULL;
+ op->func_kwdefaults = NULL;
+ op->func_closure = NULL;
+ Py_INCREF(Py_None);
+ op->func_doc = Py_None;
+ op->func_dict = NULL;
+ op->func_weakreflist = NULL;
+ op->func_module = NULL;
+ op->func_annotations = NULL;
+ op->vectorcall = _PyFunction_Vectorcall;
+ op->func_version = 0;
+ _PyObject_GC_TRACK(op);
+ return op;
+}
+
PyObject *
PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname)
{
}
assert(_PyFrame_IsRunnable(frame));
- assert(frame->f_lasti >= 0 || ((unsigned char *)PyBytes_AS_STRING(gen->gi_code->co_code))[0] == GEN_START);
/* Push arg onto the frame's value stack */
result = arg ? arg : Py_None;
Py_INCREF(result);
};
static PyObject *
-make_gen(PyTypeObject *type, PyFrameConstructor *con, InterpreterFrame *frame)
+make_gen(PyTypeObject *type, PyFunctionObject *func, InterpreterFrame *frame)
{
PyGenObject *gen = PyObject_GC_New(PyGenObject, type);
if (gen == NULL) {
gen->gi_exc_state.exc_value = NULL;
gen->gi_exc_state.exc_traceback = NULL;
gen->gi_exc_state.previous_item = NULL;
- if (con->fc_name != NULL)
- gen->gi_name = con->fc_name;
+ if (func->func_name != NULL)
+ gen->gi_name = func->func_name;
else
gen->gi_name = gen->gi_code->co_name;
Py_INCREF(gen->gi_name);
- if (con->fc_qualname != NULL)
- gen->gi_qualname = con->fc_qualname;
+ if (func->func_qualname != NULL)
+ gen->gi_qualname = func->func_qualname;
else
gen->gi_qualname = gen->gi_name;
Py_INCREF(gen->gi_qualname);
compute_cr_origin(int origin_depth);
PyObject *
-_Py_MakeCoro(PyFrameConstructor *con, InterpreterFrame *frame)
+_Py_MakeCoro(PyFunctionObject *func, InterpreterFrame *frame)
{
- int coro_flags = ((PyCodeObject *)con->fc_code)->co_flags &
+ int coro_flags = ((PyCodeObject *)func->func_code)->co_flags &
(CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR);
assert(coro_flags);
if (coro_flags == CO_GENERATOR) {
- return make_gen(&PyGen_Type, con, frame);
+ return make_gen(&PyGen_Type, func, frame);
}
if (coro_flags == CO_ASYNC_GENERATOR) {
PyAsyncGenObject *o;
- o = (PyAsyncGenObject *)make_gen(&PyAsyncGen_Type, con, frame);
+ o = (PyAsyncGenObject *)make_gen(&PyAsyncGen_Type, func, frame);
if (o == NULL) {
return NULL;
}
return (PyObject*)o;
}
assert (coro_flags == CO_COROUTINE);
- PyObject *coro = make_gen(&PyCoro_Type, con, frame);
+ PyObject *coro = make_gen(&PyCoro_Type, func, frame);
if (!coro) {
return NULL;
}
// "firstarg" is a cell here unless (very unlikely) super()
// was called from the C-API before the first MAKE_CELL op.
if (f->f_frame->f_lasti >= 0) {
- assert(_Py_OPCODE(*co->co_firstinstr) == MAKE_CELL);
+ assert(_Py_OPCODE(*co->co_firstinstr) == MAKE_CELL || _Py_OPCODE(*co->co_firstinstr) == COPY_FREE_VARS);
assert(PyCell_Check(firstarg));
firstarg = PyCell_GET(firstarg);
}
<ClInclude Include="..\Include\internal\pycore_fileutils.h" />
<ClInclude Include="..\Include\internal\pycore_floatobject.h" />
<ClInclude Include="..\Include\internal\pycore_format.h" />
+ <ClInclude Include="..\Include\internal\pycore_function.h" />
<ClInclude Include="..\Include\internal\pycore_gc.h" />
<ClInclude Include="..\Include\internal\pycore_getopt.h" />
<ClInclude Include="..\Include\internal\pycore_gil.h" />
Py_TYPE(ns)->tp_name);
goto error;
}
- PyFrameConstructor *f = PyFunction_AS_FRAME_CONSTRUCTOR(func);
PyThreadState *tstate = _PyThreadState_GET();
- cell = _PyEval_Vector(tstate, f, ns, NULL, 0, NULL);
+ cell = _PyEval_Vector(tstate, (PyFunctionObject *)func, ns, NULL, 0, NULL);
if (cell != NULL) {
if (bases != orig_bases) {
if (PyMapping_SetItemString(ns, "__orig_bases__", orig_bases) < 0) {
#include "pycore_call.h" // _PyObject_FastCallDictTstate()
#include "pycore_ceval.h" // _PyEval_SignalAsyncExc()
#include "pycore_code.h"
+#include "pycore_function.h"
#include "pycore_initconfig.h" // _PyStatus_OK()
#include "pycore_long.h" // _PyLong_GetZero()
#include "pycore_object.h" // _PyObject_GC_TRACK()
static void format_awaitable_error(PyThreadState *, PyTypeObject *, int, int);
static int get_exception_handler(PyCodeObject *, int, int*, int*, int*);
static InterpreterFrame *
-_PyEvalFramePushAndInit(PyThreadState *tstate, PyFrameConstructor *con,
+_PyEvalFramePushAndInit(PyThreadState *tstate, PyFunctionObject *func,
PyObject *locals, PyObject* const* args,
size_t argcount, PyObject *kwnames);
static int
.fc_kwdefaults = NULL,
.fc_closure = NULL
};
- return _PyEval_Vector(tstate, &desc, locals, NULL, 0, NULL);
+ PyFunctionObject *func = _PyFunction_FromConstructor(&desc);
+ if (func == NULL) {
+ return NULL;
+ }
+ PyObject *res = _PyEval_Vector(tstate, func, locals, NULL, 0, NULL);
+ Py_DECREF(func);
+ return res;
}
}
static PyObject *
-make_coro(PyThreadState *tstate, PyFrameConstructor *con,
+make_coro(PyThreadState *tstate, PyFunctionObject *func,
PyObject *locals,
PyObject* const* args, size_t argcount,
PyObject *kwnames);
if (new_frame == NULL) {
goto error;
}
- _PyFrame_InitializeSpecials(new_frame, PyFunction_AS_FRAME_CONSTRUCTOR(getitem),
+ _PyFrame_InitializeSpecials(new_frame, getitem,
NULL, code->co_nlocalsplus);
STACK_SHRINK(2);
new_frame->localsplus[0] = container;
DISPATCH();
}
+ TARGET(COPY_FREE_VARS) {
+ /* Copy closure variables to free variables */
+ PyCodeObject *co = frame->f_code;
+ PyObject *closure = frame->f_func->func_closure;
+ int offset = co->co_nlocals + co->co_nplaincellvars;
+ assert(oparg == co->co_nfreevars);
+ for (int i = 0; i < oparg; ++i) {
+ PyObject *o = PyTuple_GET_ITEM(closure, i);
+ Py_INCREF(o);
+ frame->localsplus[offset + i] = o;
+ }
+ DISPATCH();
+ }
+
TARGET(BUILD_STRING) {
PyObject *str;
PyObject *empty = PyUnicode_New(0, 0);
PyObject *locals = code_flags & CO_OPTIMIZED ? NULL : PyFunction_GET_GLOBALS(function);
STACK_SHRINK(oparg);
InterpreterFrame *new_frame = _PyEvalFramePushAndInit(
- tstate, PyFunction_AS_FRAME_CONSTRUCTOR(function), locals,
- stack_pointer,
- nargs, kwnames);
+ tstate, (PyFunctionObject *)function, locals,
+ stack_pointer, nargs, kwnames
+ );
STACK_SHRINK(postcall_shrink);
// The frame has stolen all the arguments from the stack,
// so there is no need to clean them up.
if (new_frame == NULL) {
goto error;
}
- _PyFrame_InitializeSpecials(new_frame, PyFunction_AS_FRAME_CONSTRUCTOR(func),
+ _PyFrame_InitializeSpecials(new_frame, func,
NULL, code->co_nlocalsplus);
STACK_SHRINK(argcount);
for (int i = 0; i < argcount; i++) {
}
static int
-initialize_locals(PyThreadState *tstate, PyFrameConstructor *con,
+initialize_locals(PyThreadState *tstate, PyFunctionObject *func,
PyObject **localsplus, PyObject *const *args,
Py_ssize_t argcount, PyObject *kwnames)
{
- PyCodeObject *co = (PyCodeObject*)con->fc_code;
+ PyCodeObject *co = (PyCodeObject*)func->func_code;
const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
/* Create a dictionary for keyword parameters (**kwags) */
if (keyword == NULL || !PyUnicode_Check(keyword)) {
_PyErr_Format(tstate, PyExc_TypeError,
"%U() keywords must be strings",
- con->fc_qualname);
+ func->func_qualname);
goto kw_fail;
}
if (co->co_posonlyargcount
&& positional_only_passed_as_keyword(tstate, co,
kwcount, kwnames,
- con->fc_qualname))
+ func->func_qualname))
{
goto kw_fail;
}
_PyErr_Format(tstate, PyExc_TypeError,
"%U() got an unexpected keyword argument '%S'",
- con->fc_qualname, keyword);
+ func->func_qualname, keyword);
goto kw_fail;
}
if (localsplus[j] != NULL) {
_PyErr_Format(tstate, PyExc_TypeError,
"%U() got multiple values for argument '%S'",
- con->fc_qualname, keyword);
+ func->func_qualname, keyword);
goto kw_fail;
}
localsplus[j] = value;
/* Check the number of positional arguments */
if ((argcount > co->co_argcount) && !(co->co_flags & CO_VARARGS)) {
- too_many_positional(tstate, co, argcount, con->fc_defaults, localsplus,
- con->fc_qualname);
+ too_many_positional(tstate, co, argcount, func->func_defaults, localsplus,
+ func->func_qualname);
goto fail_post_args;
}
/* Add missing positional arguments (copy default values from defs) */
if (argcount < co->co_argcount) {
- Py_ssize_t defcount = con->fc_defaults == NULL ? 0 : PyTuple_GET_SIZE(con->fc_defaults);
+ Py_ssize_t defcount = func->func_defaults == NULL ? 0 : PyTuple_GET_SIZE(func->func_defaults);
Py_ssize_t m = co->co_argcount - defcount;
Py_ssize_t missing = 0;
for (i = argcount; i < m; i++) {
}
if (missing) {
missing_arguments(tstate, co, missing, defcount, localsplus,
- con->fc_qualname);
+ func->func_qualname);
goto fail_post_args;
}
if (n > m)
else
i = 0;
if (defcount) {
- PyObject **defs = &PyTuple_GET_ITEM(con->fc_defaults, 0);
+ PyObject **defs = &PyTuple_GET_ITEM(func->func_defaults, 0);
for (; i < defcount; i++) {
if (localsplus[m+i] == NULL) {
PyObject *def = defs[i];
if (localsplus[i] != NULL)
continue;
PyObject *varname = PyTuple_GET_ITEM(co->co_localsplusnames, i);
- if (con->fc_kwdefaults != NULL) {
- PyObject *def = PyDict_GetItemWithError(con->fc_kwdefaults, varname);
+ if (func->func_kwdefaults != NULL) {
+ PyObject *def = PyDict_GetItemWithError(func->func_kwdefaults, varname);
if (def) {
Py_INCREF(def);
localsplus[i] = def;
}
if (missing) {
missing_arguments(tstate, co, missing, -1, localsplus,
- con->fc_qualname);
+ func->func_qualname);
goto fail_post_args;
}
}
- /* Copy closure variables to free variables */
- for (i = 0; i < co->co_nfreevars; ++i) {
- PyObject *o = PyTuple_GET_ITEM(con->fc_closure, i);
- Py_INCREF(o);
- localsplus[co->co_nlocals + co->co_nplaincellvars + i] = o;
- }
return 0;
fail_pre_positional:
static InterpreterFrame *
make_coro_frame(PyThreadState *tstate,
- PyFrameConstructor *con, PyObject *locals,
+ PyFunctionObject *func, PyObject *locals,
PyObject *const *args, Py_ssize_t argcount,
PyObject *kwnames)
{
assert(is_tstate_valid(tstate));
- assert(con->fc_defaults == NULL || PyTuple_CheckExact(con->fc_defaults));
- PyCodeObject *code = (PyCodeObject *)con->fc_code;
+ assert(func->func_defaults == NULL || PyTuple_CheckExact(func->func_defaults));
+ PyCodeObject *code = (PyCodeObject *)func->func_code;
int size = code->co_nlocalsplus+code->co_stacksize + FRAME_SPECIALS_SIZE;
InterpreterFrame *frame = (InterpreterFrame *)PyMem_Malloc(sizeof(PyObject *)*size);
if (frame == NULL) {
goto fail_no_memory;
}
- _PyFrame_InitializeSpecials(frame, con, locals, code->co_nlocalsplus);
+ _PyFrame_InitializeSpecials(frame, func, locals, code->co_nlocalsplus);
for (int i = 0; i < code->co_nlocalsplus; i++) {
frame->localsplus[i] = NULL;
}
assert(frame->frame_obj == NULL);
- if (initialize_locals(tstate, con, frame->localsplus, args, argcount, kwnames)) {
+ if (initialize_locals(tstate, func, frame->localsplus, args, argcount, kwnames)) {
_PyFrame_Clear(frame, 1);
return NULL;
}
/* Consumes all the references to the args */
static PyObject *
-make_coro(PyThreadState *tstate, PyFrameConstructor *con,
+make_coro(PyThreadState *tstate, PyFunctionObject *func,
PyObject *locals,
PyObject* const* args, size_t argcount,
PyObject *kwnames)
{
- assert (((PyCodeObject *)con->fc_code)->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR));
- InterpreterFrame *frame = make_coro_frame(tstate, con, locals, args, argcount, kwnames);
+ assert (((PyCodeObject *)func->func_code)->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR));
+ InterpreterFrame *frame = make_coro_frame(tstate, func, locals, args, argcount, kwnames);
if (frame == NULL) {
return NULL;
}
- PyObject *gen = _Py_MakeCoro(con, frame);
+ PyObject *gen = _Py_MakeCoro(func, frame);
if (gen == NULL) {
return NULL;
}
/* Consumes all the references to the args */
static InterpreterFrame *
-_PyEvalFramePushAndInit(PyThreadState *tstate, PyFrameConstructor *con,
+_PyEvalFramePushAndInit(PyThreadState *tstate, PyFunctionObject *func,
PyObject *locals, PyObject* const* args,
size_t argcount, PyObject *kwnames)
{
- PyCodeObject * code = (PyCodeObject *)con->fc_code;
+ PyCodeObject * code = (PyCodeObject *)func->func_code;
size_t size = code->co_nlocalsplus + code->co_stacksize + FRAME_SPECIALS_SIZE;
InterpreterFrame *frame = _PyThreadState_BumpFramePointer(tstate, size);
if (frame == NULL) {
goto fail;
}
- _PyFrame_InitializeSpecials(frame, con, locals, code->co_nlocalsplus);
+ _PyFrame_InitializeSpecials(frame, func, locals, code->co_nlocalsplus);
PyObject **localsarray = &frame->localsplus[0];
for (int i = 0; i < code->co_nlocalsplus; i++) {
localsarray[i] = NULL;
}
- if (initialize_locals(tstate, con, localsarray, args, argcount, kwnames)) {
+ if (initialize_locals(tstate, func, localsarray, args, argcount, kwnames)) {
_PyFrame_Clear(frame, 0);
return NULL;
}
}
PyObject *
-_PyEval_Vector(PyThreadState *tstate, PyFrameConstructor *con,
+_PyEval_Vector(PyThreadState *tstate, PyFunctionObject *func,
PyObject *locals,
PyObject* const* args, size_t argcount,
PyObject *kwnames)
{
- PyCodeObject *code = (PyCodeObject *)con->fc_code;
+ PyCodeObject *code = (PyCodeObject *)func->func_code;
/* _PyEvalFramePushAndInit and make_coro consume
* all the references to their arguments
*/
int is_coro = code->co_flags &
(CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR);
if (is_coro) {
- return make_coro(tstate, con, locals, args, argcount, kwnames);
+ return make_coro(tstate, func, locals, args, argcount, kwnames);
}
InterpreterFrame *frame = _PyEvalFramePushAndInit(
- tstate, con, locals, args, argcount, kwnames);
+ tstate, func, locals, args, argcount, kwnames);
if (frame == NULL) {
return NULL;
}
.fc_kwdefaults = kwdefs,
.fc_closure = closure
};
- res = _PyEval_Vector(tstate, &constr, locals,
+ PyFunctionObject *func = _PyFunction_FromConstructor(&constr);
+ if (func == NULL) {
+ return NULL;
+ }
+ res = _PyEval_Vector(tstate, func, locals,
allargs, argcount,
kwnames);
+ Py_DECREF(func);
if (kwcount) {
Py_DECREF(kwnames);
PyMem_Free(newargs);
/* Closures */
case MAKE_CELL:
+ case COPY_FREE_VARS:
return 0;
case LOAD_CLOSURE:
return 1;
static int
insert_prefix_instructions(struct compiler *c, basicblock *entryblock,
- int *fixed)
+ int *fixed, int nfreevars)
{
int flags = compute_code_flags(c);
}
}
+ if (nfreevars) {
+ struct instr copy_frees = {
+ .i_opcode = COPY_FREE_VARS,
+ .i_oparg = nfreevars,
+ .i_lineno = -1,
+ .i_col_offset = -1,
+ .i_end_lineno = -1,
+ .i_end_col_offset = -1,
+ .i_target = NULL,
+ };
+ if (insert_instruction(entryblock, 0, ©_frees) < 0) {
+ return -1;
+ }
+
+ }
+
return 0;
}
}
// This must be called before fix_cell_offsets().
- if (insert_prefix_instructions(c, entryblock, cellfixedoffsets)) {
+ if (insert_prefix_instructions(c, entryblock, cellfixedoffsets, nfreevars)) {
goto error;
}
_PyFrame_Traverse(InterpreterFrame *frame, visitproc visit, void *arg)
{
Py_VISIT(frame->frame_obj);
- Py_VISIT(frame->f_globals);
- Py_VISIT(frame->f_builtins);
Py_VISIT(frame->f_locals);
+ Py_VISIT(frame->f_func);
Py_VISIT(frame->f_code);
/* locals */
PyObject **locals = _PyFrame_GetLocalsArray(frame);
frame->generator = NULL;
Py_XDECREF(frame->frame_obj);
Py_XDECREF(frame->f_locals);
- Py_DECREF(frame->f_globals);
- Py_DECREF(frame->f_builtins);
+ Py_DECREF(frame->f_func);
Py_DECREF(frame->f_code);
}
&&TARGET_SET_ADD,
&&TARGET_MAP_ADD,
&&TARGET_LOAD_CLASSDEREF,
- &&_unknown_opcode,
+ &&TARGET_COPY_FREE_VARS,
&&_unknown_opcode,
&&_unknown_opcode,
&&TARGET_MATCH_CLASS,
InterpreterFrame *
-_PyThreadState_PushFrame(PyThreadState *tstate, PyFrameConstructor *con, PyObject *locals)
+_PyThreadState_PushFrame(PyThreadState *tstate, PyFunctionObject *func, PyObject *locals)
{
- PyCodeObject *code = (PyCodeObject *)con->fc_code;
+ PyCodeObject *code = (PyCodeObject *)func->func_code;
int nlocalsplus = code->co_nlocalsplus;
size_t size = nlocalsplus + code->co_stacksize +
FRAME_SPECIALS_SIZE;
if (frame == NULL) {
return NULL;
}
- _PyFrame_InitializeSpecials(frame, con, locals, nlocalsplus);
+ _PyFrame_InitializeSpecials(frame, func, locals, nlocalsplus);
for (int i=0; i < nlocalsplus; i++) {
frame->localsplus[i] = NULL;
}
#define SPEC_FAIL_WRONG_NUMBER_ARGUMENTS 9
#define SPEC_FAIL_CO_NOT_OPTIMIZED 10
/* SPEC_FAIL_METHOD defined as 11 above */
-#define SPEC_FAIL_FREE_VARS 12
+
#define SPEC_FAIL_PYCFUNCTION 13
#define SPEC_FAIL_PYCFUNCTION_WITH_KEYWORDS 14
#define SPEC_FAIL_PYCFUNCTION_FAST_WITH_KEYWORDS 15
if ((flags & CO_OPTIMIZED) == 0) {
return SPEC_FAIL_CO_NOT_OPTIMIZED;
}
- if (code->co_nfreevars) {
- return SPEC_FAIL_FREE_VARS;
- }
return SIMPLE_FUNCTION;
}