Skip to content

Commit a89c29f

Browse files
authored
bpo-46841: Add a _Py_SET_OPCODE macro (GH-31780)
1 parent 882d809 commit a89c29f

File tree

3 files changed

+69
-81
lines changed

3 files changed

+69
-81
lines changed

Include/cpython/code.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ typedef uint16_t _Py_CODEUNIT;
2323
# define _Py_MAKECODEUNIT(opcode, oparg) ((opcode)|((oparg)<<8))
2424
#endif
2525

26+
// Use "unsigned char" instead of "uint8_t" here to avoid illegal aliasing:
27+
#define _Py_SET_OPCODE(word, opcode) (((unsigned char *)&(word))[0] = (opcode))
28+
2629

2730
/* Bytecode object */
2831
struct PyCodeObject {

Python/ceval.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5517,7 +5517,7 @@ opname ## _miss: \
55175517
_Py_CODEUNIT *counter = (_Py_CODEUNIT *)next_instr; \
55185518
*counter -= 1; \
55195519
if (*counter == 0) { \
5520-
next_instr[-1] = _Py_MAKECODEUNIT(opname ## _ADAPTIVE, _Py_OPARG(next_instr[-1])); \
5520+
_Py_SET_OPCODE(next_instr[-1], opname ## _ADAPTIVE); \
55215521
STAT_INC(opname, deopt); \
55225522
*counter = ADAPTIVE_CACHE_BACKOFF; \
55235523
} \

0 commit comments

Comments
 (0)