Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Include/internal/pycore_opcode_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ is_bit_set_in_table(const uint32_t *table, int bitindex) {
#undef LOG_BITS_PER_INT
#undef MASK_LOW_LOG_BITS

/* Flags used in the oparg for MAKE_FUNCTION */
#define MAKE_FUNCTION_DEFAULTS 0x01
#define MAKE_FUNCTION_KWDEFAULTS 0x02
#define MAKE_FUNCTION_ANNOTATIONS 0x04
#define MAKE_FUNCTION_CLOSURE 0x08


#ifdef __cplusplus
}
Expand Down
17 changes: 9 additions & 8 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "pycore_object.h" // _PyObject_GC_TRACK()
#include "pycore_moduleobject.h" // PyModuleObject
#include "pycore_opcode.h" // EXTRA_CASES
#include "pycore_opcode_utils.h" // MAKE_FUNCTION_*
#include "pycore_pyerrors.h" // _PyErr_GetRaisedException()
#include "pycore_pystate.h" // _PyInterpreterState_GET()
#include "pycore_range.h" // _PyRangeIterObject
Expand Down Expand Up @@ -3245,10 +3246,10 @@ dummy_func(
CHECK_EVAL_BREAKER();
}

inst(MAKE_FUNCTION, (defaults if (oparg & 0x01),
kwdefaults if (oparg & 0x02),
annotations if (oparg & 0x04),
closure if (oparg & 0x08),
inst(MAKE_FUNCTION, (defaults if (oparg & MAKE_FUNCTION_DEFAULTS),
kwdefaults if (oparg & MAKE_FUNCTION_KWDEFAULTS),
annotations if (oparg & MAKE_FUNCTION_ANNOTATIONS),
closure if (oparg & MAKE_FUNCTION_CLOSURE),
codeobj -- func)) {

PyFunctionObject *func_obj = (PyFunctionObject *)
Expand All @@ -3259,19 +3260,19 @@ dummy_func(
goto error;
}

if (oparg & 0x08) {
if (oparg & MAKE_FUNCTION_CLOSURE) {
assert(PyTuple_CheckExact(closure));
func_obj->func_closure = closure;
}
if (oparg & 0x04) {
if (oparg & MAKE_FUNCTION_ANNOTATIONS) {
assert(PyTuple_CheckExact(annotations));
func_obj->func_annotations = annotations;
}
if (oparg & 0x02) {
if (oparg & MAKE_FUNCTION_KWDEFAULTS) {
assert(PyDict_CheckExact(kwdefaults));
func_obj->func_kwdefaults = kwdefaults;
}
if (oparg & 0x01) {
if (oparg & MAKE_FUNCTION_DEFAULTS) {
assert(PyTuple_CheckExact(defaults));
func_obj->func_defaults = defaults;
}
Expand Down
1 change: 1 addition & 0 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "pycore_object.h" // _PyObject_GC_TRACK()
#include "pycore_moduleobject.h" // PyModuleObject
#include "pycore_opcode.h" // EXTRA_CASES
#include "pycore_opcode_utils.h" // MAKE_FUNCTION_*
#include "pycore_pyerrors.h" // _PyErr_GetRaisedException()
#include "pycore_pystate.h" // _PyInterpreterState_GET()
#include "pycore_range.h" // _PyRangeIterObject
Expand Down
19 changes: 8 additions & 11 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -1814,7 +1814,7 @@ compiler_make_closure(struct compiler *c, location loc,
}
ADDOP_I(c, loc, LOAD_CLOSURE, arg);
}
flags |= 0x08;
flags |= MAKE_FUNCTION_CLOSURE;
ADDOP_I(c, loc, BUILD_TUPLE, co->co_nfreevars);
}
ADDOP_LOAD_CONST(c, loc, (PyObject*)co);
Expand Down Expand Up @@ -2025,15 +2025,15 @@ compiler_default_arguments(struct compiler *c, location loc,
Py_ssize_t funcflags = 0;
if (args->defaults && asdl_seq_LEN(args->defaults) > 0) {
RETURN_IF_ERROR(compiler_visit_defaults(c, args, loc));
funcflags |= 0x01;
funcflags |= MAKE_FUNCTION_DEFAULTS;
}
if (args->kwonlyargs) {
int res = compiler_visit_kwonlydefaults(c, loc,
args->kwonlyargs,
args->kw_defaults);
RETURN_IF_ERROR(res);
if (res > 0) {
funcflags |= 0x02;
funcflags |= MAKE_FUNCTION_KWDEFAULTS;
}
}
return funcflags;
Expand Down Expand Up @@ -2291,10 +2291,10 @@ compiler_function(struct compiler *c, stmt_ty s, int is_async)
int num_typeparam_args = 0;

if (is_generic) {
if (funcflags & 0x01) {
if (funcflags & MAKE_FUNCTION_DEFAULTS) {
num_typeparam_args += 1;
}
if (funcflags & 0x02) {
if (funcflags & MAKE_FUNCTION_KWDEFAULTS) {
num_typeparam_args += 1;
}
if (num_typeparam_args == 2) {
Expand All @@ -2311,11 +2311,8 @@ compiler_function(struct compiler *c, stmt_ty s, int is_async)
}
Py_DECREF(type_params_name);
RETURN_IF_ERROR_IN_SCOPE(c, compiler_type_params(c, type_params));
if ((funcflags & 0x01) || (funcflags & 0x02)) {
RETURN_IF_ERROR_IN_SCOPE(c, codegen_addop_i(INSTR_SEQUENCE(c), LOAD_FAST, 0, loc));
}
if ((funcflags & 0x01) && (funcflags & 0x02)) {
RETURN_IF_ERROR_IN_SCOPE(c, codegen_addop_i(INSTR_SEQUENCE(c), LOAD_FAST, 1, loc));
for (int i = 0; i < num_typeparam_args; i++) {
RETURN_IF_ERROR_IN_SCOPE(c, codegen_addop_i(INSTR_SEQUENCE(c), LOAD_FAST, i, loc));
}
}

Expand All @@ -2327,7 +2324,7 @@ compiler_function(struct compiler *c, stmt_ty s, int is_async)
return ERROR;
}
if (annotations > 0) {
funcflags |= 0x04;
funcflags |= MAKE_FUNCTION_ANNOTATIONS;
}

if (compiler_function_body(c, s, is_async, funcflags, firstlineno) < 0) {
Expand Down
Loading