diff --git a/Include/cpython/code.h b/Include/cpython/code.h index 0cf49f06c87732..6de9e492a20f1a 100644 --- a/Include/cpython/code.h +++ b/Include/cpython/code.h @@ -21,19 +21,27 @@ typedef union { struct { uint8_t opcode; uint8_t oparg; - }; + } _op; } _Py_CODEUNIT; -#define _Py_OPCODE(word) ((word).opcode) -#define _Py_OPARG(word) ((word).oparg) +#define _Py_OPCODE(word) ((word)._op.opcode) +#define _Py_OPARG(word) ((word)._op.oparg) static inline void _py_set_opcode(_Py_CODEUNIT *word, uint8_t opcode) { - word->opcode = opcode; + word->_op.opcode = opcode; } -#define _Py_SET_OPCODE(word, opcode) _py_set_opocde(&(word), opcode) +#define _Py_SET_OPCODE(word, opcode) _py_set_opcode(&(word), opcode) + +static inline void +_py_set_oparg(_Py_CODEUNIT *word, uint8_t oparg) +{ + word->_op.oparg = oparg; +} + +#define _Py_SET_OPARG(word, opcode) _py_set_oparg(&(word), opcode) typedef struct { PyObject *_co_code; diff --git a/Misc/NEWS.d/next/C API/2023-02-14-15-53-01.gh-issue-101907.HgF1N2.rst b/Misc/NEWS.d/next/C API/2023-02-14-15-53-01.gh-issue-101907.HgF1N2.rst new file mode 100644 index 00000000000000..cfc0d72cdbca00 --- /dev/null +++ b/Misc/NEWS.d/next/C API/2023-02-14-15-53-01.gh-issue-101907.HgF1N2.rst @@ -0,0 +1 @@ +Removes use of non-standard C++ extension in public header files. diff --git a/Objects/codeobject.c b/Objects/codeobject.c index ab31b6582cdaae..9270150e4a9ae2 100644 --- a/Objects/codeobject.c +++ b/Objects/codeobject.c @@ -1507,10 +1507,10 @@ deopt_code(_Py_CODEUNIT *instructions, Py_ssize_t len) _Py_CODEUNIT instruction = instructions[i]; int opcode = _PyOpcode_Deopt[_Py_OPCODE(instruction)]; int caches = _PyOpcode_Caches[opcode]; - instructions[i].opcode = opcode; + _Py_SET_OPCODE(instructions[i], opcode); while (caches--) { - instructions[++i].opcode = CACHE; - instructions[i].oparg = 0; + _Py_SET_OPCODE(instructions[++i], CACHE); + _Py_SET_OPARG(instructions[i], 0); } } } @@ -1763,8 +1763,8 @@ code_richcompare(PyObject *self, PyObject *other, int op) for (int i = 0; i < Py_SIZE(co); i++) { _Py_CODEUNIT co_instr = _PyCode_CODE(co)[i]; _Py_CODEUNIT cp_instr = _PyCode_CODE(cp)[i]; - co_instr.opcode = _PyOpcode_Deopt[_Py_OPCODE(co_instr)]; - cp_instr.opcode =_PyOpcode_Deopt[_Py_OPCODE(cp_instr)]; + _Py_SET_OPCODE(co_instr, _PyOpcode_Deopt[_Py_OPCODE(co_instr)]); + _Py_SET_OPCODE(cp_instr, _PyOpcode_Deopt[_Py_OPCODE(cp_instr)]); eq = co_instr.cache == cp_instr.cache; if (!eq) { goto unequal; diff --git a/Objects/genobject.c b/Objects/genobject.c index 35246653c45348..6979fdce84abe2 100644 --- a/Objects/genobject.c +++ b/Objects/genobject.c @@ -371,9 +371,9 @@ gen_close(PyGenObject *gen, PyObject *args) _PyInterpreterFrame *frame = (_PyInterpreterFrame *)gen->gi_iframe; /* It is possible for the previous instruction to not be a * YIELD_VALUE if the debugger has changed the lineno. */ - if (err == 0 && frame->prev_instr->opcode == YIELD_VALUE) { - assert(frame->prev_instr[1].opcode == RESUME); - int exception_handler_depth = frame->prev_instr->oparg; + if (err == 0 && _Py_OPCODE(frame->prev_instr[0]) == YIELD_VALUE) { + assert(_Py_OPCODE(frame->prev_instr[1]) == RESUME); + int exception_handler_depth = _Py_OPCODE(frame->prev_instr[0]); assert(exception_handler_depth > 0); /* We can safely ignore the outermost try block * as it automatically generated to handle diff --git a/Python/compile.c b/Python/compile.c index 0534b536e3d12e..7ad62e254ab934 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -274,31 +274,31 @@ write_instr(_Py_CODEUNIT *codestr, struct instr *instruction, int ilen) int caches = _PyOpcode_Caches[opcode]; switch (ilen - caches) { case 4: - codestr->opcode = EXTENDED_ARG; - codestr->oparg = (oparg >> 24) & 0xFF; + _Py_SET_OPCODE(*codestr, EXTENDED_ARG); + _Py_SET_OPARG(*codestr, (oparg >> 24) & 0xFF); codestr++; /* fall through */ case 3: - codestr->opcode = EXTENDED_ARG; - codestr->oparg = (oparg >> 16) & 0xFF; + _Py_SET_OPCODE(*codestr, EXTENDED_ARG); + _Py_SET_OPARG(*codestr, (oparg >> 16) & 0xFF); codestr++; /* fall through */ case 2: - codestr->opcode = EXTENDED_ARG; - codestr->oparg = (oparg >> 8) & 0xFF; + _Py_SET_OPCODE(*codestr, EXTENDED_ARG); + _Py_SET_OPARG(*codestr, (oparg >> 8) & 0xFF); codestr++; /* fall through */ case 1: - codestr->opcode = opcode; - codestr->oparg = oparg & 0xFF; + _Py_SET_OPCODE(*codestr, opcode); + _Py_SET_OPARG(*codestr, oparg & 0xFF); codestr++; break; default: Py_UNREACHABLE(); } while (caches--) { - codestr->opcode = CACHE; - codestr->oparg = 0; + _Py_SET_OPCODE(*codestr, CACHE); + _Py_SET_OPARG(*codestr, 0); codestr++; } } diff --git a/Python/specialize.c b/Python/specialize.c index 4ede3122d38046..8bbf8fe9e19249 100644 --- a/Python/specialize.c +++ b/Python/specialize.c @@ -291,31 +291,31 @@ _PyCode_Quicken(PyCodeObject *code) } switch (previous_opcode << 8 | opcode) { case LOAD_CONST << 8 | LOAD_FAST: - instructions[i - 1].opcode = LOAD_CONST__LOAD_FAST; + _Py_SET_OPCODE(instructions[i - 1], LOAD_CONST__LOAD_FAST); break; case LOAD_FAST << 8 | LOAD_CONST: - instructions[i - 1].opcode = LOAD_FAST__LOAD_CONST; + _Py_SET_OPCODE(instructions[i - 1], LOAD_FAST__LOAD_CONST); break; case LOAD_FAST << 8 | LOAD_FAST: - instructions[i - 1].opcode = LOAD_FAST__LOAD_FAST; + _Py_SET_OPCODE(instructions[i - 1], LOAD_FAST__LOAD_FAST); break; case STORE_FAST << 8 | LOAD_FAST: - instructions[i - 1].opcode = STORE_FAST__LOAD_FAST; + _Py_SET_OPCODE(instructions[i - 1], STORE_FAST__LOAD_FAST); break; case STORE_FAST << 8 | STORE_FAST: - instructions[i - 1].opcode = STORE_FAST__STORE_FAST; + _Py_SET_OPCODE(instructions[i - 1], STORE_FAST__STORE_FAST); break; case COMPARE_OP << 8 | POP_JUMP_IF_TRUE: case COMPARE_OP << 8 | POP_JUMP_IF_FALSE: { - int oparg = instructions[i - 1 - INLINE_CACHE_ENTRIES_COMPARE_OP].oparg; + int oparg = _Py_OPARG(instructions[i - 1 - INLINE_CACHE_ENTRIES_COMPARE_OP]); assert((oparg >> 4) <= Py_GE); int mask = compare_masks[oparg >> 4]; if (opcode == POP_JUMP_IF_FALSE) { mask = mask ^ 0xf; } - instructions[i - 1 - INLINE_CACHE_ENTRIES_COMPARE_OP].opcode = COMPARE_AND_BRANCH; - instructions[i - 1 - INLINE_CACHE_ENTRIES_COMPARE_OP].oparg = (oparg & 0xf0) | mask; + _Py_SET_OPCODE(instructions[i - 1 - INLINE_CACHE_ENTRIES_COMPARE_OP], COMPARE_AND_BRANCH); + _Py_SET_OPARG(instructions[i - 1 - INLINE_CACHE_ENTRIES_COMPARE_OP], (oparg & 0xf0) | mask); break; } }