Skip to content

Commit 44bb03f

Browse files
gh-105214: Use named constants for MAKE_FUNCTION oparg (#105215)
1 parent 41de543 commit 44bb03f

File tree

6 files changed

+294
-289
lines changed

6 files changed

+294
-289
lines changed

Include/internal/pycore_opcode_utils.h

+6
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ is_bit_set_in_table(const uint32_t *table, int bitindex) {
8585
#undef LOG_BITS_PER_INT
8686
#undef MASK_LOW_LOG_BITS
8787

88+
/* Flags used in the oparg for MAKE_FUNCTION */
89+
#define MAKE_FUNCTION_DEFAULTS 0x01
90+
#define MAKE_FUNCTION_KWDEFAULTS 0x02
91+
#define MAKE_FUNCTION_ANNOTATIONS 0x04
92+
#define MAKE_FUNCTION_CLOSURE 0x08
93+
8894

8995
#ifdef __cplusplus
9096
}

Python/bytecodes.c

+9-8
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "pycore_object.h" // _PyObject_GC_TRACK()
1919
#include "pycore_moduleobject.h" // PyModuleObject
2020
#include "pycore_opcode.h" // EXTRA_CASES
21+
#include "pycore_opcode_utils.h" // MAKE_FUNCTION_*
2122
#include "pycore_pyerrors.h" // _PyErr_GetRaisedException()
2223
#include "pycore_pystate.h" // _PyInterpreterState_GET()
2324
#include "pycore_range.h" // _PyRangeIterObject
@@ -3245,10 +3246,10 @@ dummy_func(
32453246
CHECK_EVAL_BREAKER();
32463247
}
32473248

3248-
inst(MAKE_FUNCTION, (defaults if (oparg & 0x01),
3249-
kwdefaults if (oparg & 0x02),
3250-
annotations if (oparg & 0x04),
3251-
closure if (oparg & 0x08),
3249+
inst(MAKE_FUNCTION, (defaults if (oparg & MAKE_FUNCTION_DEFAULTS),
3250+
kwdefaults if (oparg & MAKE_FUNCTION_KWDEFAULTS),
3251+
annotations if (oparg & MAKE_FUNCTION_ANNOTATIONS),
3252+
closure if (oparg & MAKE_FUNCTION_CLOSURE),
32523253
codeobj -- func)) {
32533254

32543255
PyFunctionObject *func_obj = (PyFunctionObject *)
@@ -3259,19 +3260,19 @@ dummy_func(
32593260
goto error;
32603261
}
32613262

3262-
if (oparg & 0x08) {
3263+
if (oparg & MAKE_FUNCTION_CLOSURE) {
32633264
assert(PyTuple_CheckExact(closure));
32643265
func_obj->func_closure = closure;
32653266
}
3266-
if (oparg & 0x04) {
3267+
if (oparg & MAKE_FUNCTION_ANNOTATIONS) {
32673268
assert(PyTuple_CheckExact(annotations));
32683269
func_obj->func_annotations = annotations;
32693270
}
3270-
if (oparg & 0x02) {
3271+
if (oparg & MAKE_FUNCTION_KWDEFAULTS) {
32713272
assert(PyDict_CheckExact(kwdefaults));
32723273
func_obj->func_kwdefaults = kwdefaults;
32733274
}
3274-
if (oparg & 0x01) {
3275+
if (oparg & MAKE_FUNCTION_DEFAULTS) {
32753276
assert(PyTuple_CheckExact(defaults));
32763277
func_obj->func_defaults = defaults;
32773278
}

Python/ceval.c

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "pycore_object.h" // _PyObject_GC_TRACK()
1515
#include "pycore_moduleobject.h" // PyModuleObject
1616
#include "pycore_opcode.h" // EXTRA_CASES
17+
#include "pycore_opcode_utils.h" // MAKE_FUNCTION_*
1718
#include "pycore_pyerrors.h" // _PyErr_GetRaisedException()
1819
#include "pycore_pystate.h" // _PyInterpreterState_GET()
1920
#include "pycore_range.h" // _PyRangeIterObject

Python/compile.c

+8-11
Original file line numberDiff line numberDiff line change
@@ -1814,7 +1814,7 @@ compiler_make_closure(struct compiler *c, location loc,
18141814
}
18151815
ADDOP_I(c, loc, LOAD_CLOSURE, arg);
18161816
}
1817-
flags |= 0x08;
1817+
flags |= MAKE_FUNCTION_CLOSURE;
18181818
ADDOP_I(c, loc, BUILD_TUPLE, co->co_nfreevars);
18191819
}
18201820
ADDOP_LOAD_CONST(c, loc, (PyObject*)co);
@@ -2025,15 +2025,15 @@ compiler_default_arguments(struct compiler *c, location loc,
20252025
Py_ssize_t funcflags = 0;
20262026
if (args->defaults && asdl_seq_LEN(args->defaults) > 0) {
20272027
RETURN_IF_ERROR(compiler_visit_defaults(c, args, loc));
2028-
funcflags |= 0x01;
2028+
funcflags |= MAKE_FUNCTION_DEFAULTS;
20292029
}
20302030
if (args->kwonlyargs) {
20312031
int res = compiler_visit_kwonlydefaults(c, loc,
20322032
args->kwonlyargs,
20332033
args->kw_defaults);
20342034
RETURN_IF_ERROR(res);
20352035
if (res > 0) {
2036-
funcflags |= 0x02;
2036+
funcflags |= MAKE_FUNCTION_KWDEFAULTS;
20372037
}
20382038
}
20392039
return funcflags;
@@ -2291,10 +2291,10 @@ compiler_function(struct compiler *c, stmt_ty s, int is_async)
22912291
int num_typeparam_args = 0;
22922292

22932293
if (is_generic) {
2294-
if (funcflags & 0x01) {
2294+
if (funcflags & MAKE_FUNCTION_DEFAULTS) {
22952295
num_typeparam_args += 1;
22962296
}
2297-
if (funcflags & 0x02) {
2297+
if (funcflags & MAKE_FUNCTION_KWDEFAULTS) {
22982298
num_typeparam_args += 1;
22992299
}
23002300
if (num_typeparam_args == 2) {
@@ -2311,11 +2311,8 @@ compiler_function(struct compiler *c, stmt_ty s, int is_async)
23112311
}
23122312
Py_DECREF(type_params_name);
23132313
RETURN_IF_ERROR_IN_SCOPE(c, compiler_type_params(c, type_params));
2314-
if ((funcflags & 0x01) || (funcflags & 0x02)) {
2315-
RETURN_IF_ERROR_IN_SCOPE(c, codegen_addop_i(INSTR_SEQUENCE(c), LOAD_FAST, 0, loc));
2316-
}
2317-
if ((funcflags & 0x01) && (funcflags & 0x02)) {
2318-
RETURN_IF_ERROR_IN_SCOPE(c, codegen_addop_i(INSTR_SEQUENCE(c), LOAD_FAST, 1, loc));
2314+
for (int i = 0; i < num_typeparam_args; i++) {
2315+
RETURN_IF_ERROR_IN_SCOPE(c, codegen_addop_i(INSTR_SEQUENCE(c), LOAD_FAST, i, loc));
23192316
}
23202317
}
23212318

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

23332330
if (compiler_function_body(c, s, is_async, funcflags, firstlineno) < 0) {

0 commit comments

Comments
 (0)