gh-105214: Use named constants for MAKE_FUNCTION oparg (#105215)

This commit is contained in:
Jelle Zijlstra 2023-06-02 07:10:45 -07:00 committed by GitHub
parent 41de54378d
commit 44bb03f856
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 294 additions and 289 deletions

View File

@ -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
}

View File

@ -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
@ -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 *)
@ -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;
}

View File

@ -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

View File

@ -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);
@ -2025,7 +2025,7 @@ 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,
@ -2033,7 +2033,7 @@ compiler_default_arguments(struct compiler *c, location loc,
args->kw_defaults);
RETURN_IF_ERROR(res);
if (res > 0) {
funcflags |= 0x02;
funcflags |= MAKE_FUNCTION_KWDEFAULTS;
}
}
return funcflags;
@ -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) {
@ -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));
}
}
@ -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) {

File diff suppressed because it is too large Load Diff

View File

@ -362,7 +362,7 @@ _PyOpcode_num_popped(int opcode, int oparg, bool jump) {
case CALL_FUNCTION_EX:
return ((oparg & 1) ? 1 : 0) + 3;
case MAKE_FUNCTION:
return ((oparg & 0x01) ? 1 : 0) + ((oparg & 0x02) ? 1 : 0) + ((oparg & 0x04) ? 1 : 0) + ((oparg & 0x08) ? 1 : 0) + 1;
return ((oparg & MAKE_FUNCTION_DEFAULTS) ? 1 : 0) + ((oparg & MAKE_FUNCTION_KWDEFAULTS) ? 1 : 0) + ((oparg & MAKE_FUNCTION_ANNOTATIONS) ? 1 : 0) + ((oparg & MAKE_FUNCTION_CLOSURE) ? 1 : 0) + 1;
case RETURN_GENERATOR:
return 0;
case BUILD_SLICE: