Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Merge BINARY_*/INPLACE_* into BINARY_OP/INPLACE_OP
  • Loading branch information
brandtbucher committed Oct 27, 2021
commit 4b7125f331c91c3da37fc0586d2a74ca618f4c83
4 changes: 4 additions & 0 deletions Include/abstract.h
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,10 @@ PyAPI_FUNC(PyObject *) PyNumber_InPlaceOr(PyObject *o1, PyObject *o2);
If n is not an int object, it is converted with PyNumber_Index first. */
PyAPI_FUNC(PyObject *) PyNumber_ToBase(PyObject *n, int base);

PyAPI_FUNC(PyObject *) _PyNumber_Op(PyObject *o1, PyObject *o2, int op);

PyAPI_FUNC(PyObject *) _PyNumber_InPlaceOp(PyObject *o1, PyObject *o2, int op);


/* === Sequence protocol ================================================ */

Expand Down
104 changes: 49 additions & 55 deletions Include/opcode.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions Lib/dis.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from opcode import *
from opcode import __all__ as _opcodes_all
from opcode import _nb_ops

__all__ = ["code_info", "dis", "disassemble", "distb", "disco",
"findlinestarts", "findlabels", "show_code",
Expand All @@ -28,6 +29,14 @@

LOAD_CONST = opmap['LOAD_CONST']

BINARY_OP = opmap['BINARY_OP']
BINARY_OPS = {i: name for i, (_, name) in enumerate(_nb_ops)}

INPLACE_OP = opmap['INPLACE_OP']
INPLACE_OPS = {i: f"{name}=" for i, (_, name) in enumerate(_nb_ops)}

del _nb_ops

def _try_compile(source, name):
"""Attempts to compile the given source, first as an expression and
then as a statement if the first approach fails.
Expand Down Expand Up @@ -446,6 +455,10 @@ def _get_instructions_bytes(code, varname_from_oparg=None,
elif op == MAKE_FUNCTION:
argrepr = ', '.join(s for i, s in enumerate(MAKE_FUNCTION_FLAGS)
if arg & (1<<i))
elif op == BINARY_OP:
argrepr = BINARY_OPS[arg]
elif op == INPLACE_OP:
argrepr = INPLACE_OPS[arg]
yield Instruction(opname[op], op,
arg, argval, argrepr,
offset, starts_line, is_jump_target, positions)
Expand Down
3 changes: 2 additions & 1 deletion Lib/importlib/_bootstrap_external.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ def _write_atomic(path, data, mode=0o666):
# Python 3.11a1 3461 (JUMP_ABSOLUTE must jump backwards)
# Python 3.11a2 3462 (bpo-44511: remove COPY_DICT_WITHOUT_KEYS, change
# MATCH_CLASS and MATCH_KEYS, and add COPY)
# Python 3.11a2 3463 (Merge BINARY_*/INPLACE_* into BINARY_OP/INPLACE_OP)

#
# MAGIC must change whenever the bytecode emitted by the compiler may no
Expand All @@ -375,7 +376,7 @@ def _write_atomic(path, data, mode=0o666):
# Whenever MAGIC_NUMBER is changed, the ranges in the magic_values array
# in PC/launcher.c must also be updated.

MAGIC_NUMBER = (3462).to_bytes(2, 'little') + b'\r\n'
MAGIC_NUMBER = (3463).to_bytes(2, 'little') + b'\r\n'
_RAW_MAGIC_NUMBER = int.from_bytes(MAGIC_NUMBER, 'little') # For import.c

_PYCACHE = '__pycache__'
Expand Down
38 changes: 18 additions & 20 deletions Lib/opcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,15 @@ def jabs_op(name, op):
def_op('UNARY_NOT', 12)

def_op('UNARY_INVERT', 15)
def_op('BINARY_MATRIX_MULTIPLY', 16)
def_op('INPLACE_MATRIX_MULTIPLY', 17)

def_op('BINARY_POWER', 19)
def_op('BINARY_MULTIPLY', 20)

def_op('BINARY_MODULO', 22)
def_op('BINARY_ADD', 23)
def_op('BINARY_SUBTRACT', 24)

def_op('BINARY_SUBSCR', 25)
def_op('BINARY_FLOOR_DIVIDE', 26)
def_op('BINARY_TRUE_DIVIDE', 27)
def_op('INPLACE_FLOOR_DIVIDE', 28)
def_op('INPLACE_TRUE_DIVIDE', 29)

def_op('GET_LEN', 30)
def_op('MATCH_MAPPING', 31)
def_op('MATCH_SEQUENCE', 32)
Expand All @@ -95,20 +90,15 @@ def jabs_op(name, op):
def_op('GET_ANEXT', 51)
def_op('BEFORE_ASYNC_WITH', 52)
def_op('BEFORE_WITH', 53)

def_op('END_ASYNC_FOR', 54)
def_op('INPLACE_ADD', 55)
def_op('INPLACE_SUBTRACT', 56)

def_op('INPLACE_MULTIPLY', 57)

def_op('INPLACE_MODULO', 59)
def_op('STORE_SUBSCR', 60)
def_op('DELETE_SUBSCR', 61)
def_op('BINARY_LSHIFT', 62)
def_op('BINARY_RSHIFT', 63)
def_op('BINARY_AND', 64)
def_op('BINARY_XOR', 65)
def_op('BINARY_OR', 66)

def_op('INPLACE_POWER', 67)
def_op('GET_ITER', 68)
def_op('GET_YIELD_FROM_ITER', 69)
Expand All @@ -117,11 +107,6 @@ def jabs_op(name, op):
def_op('YIELD_FROM', 72)
def_op('GET_AWAITABLE', 73)
def_op('LOAD_ASSERTION_ERROR', 74)
def_op('INPLACE_LSHIFT', 75)
def_op('INPLACE_RSHIFT', 76)
def_op('INPLACE_AND', 77)
def_op('INPLACE_XOR', 78)
def_op('INPLACE_OR', 79)

def_op('LIST_TO_TUPLE', 82)
def_op('RETURN_VALUE', 83)
Expand Down Expand Up @@ -167,7 +152,8 @@ def jabs_op(name, op):
def_op('RERAISE', 119)
def_op('COPY', 120)
jabs_op('JUMP_IF_NOT_EXC_MATCH', 121)

def_op('BINARY_OP', 122)
def_op('INPLACE_OP', 123)
def_op('LOAD_FAST', 124) # Local variable number
haslocal.append(124)
def_op('STORE_FAST', 125) # Local variable number
Expand Down Expand Up @@ -219,6 +205,18 @@ def jabs_op(name, op):

del def_op, name_op, jrel_op, jabs_op

_nb_ops = [
("NB_AND", "&"),
("NB_FLOOR_DIVIDE", "//"),
("NB_LSHIFT", "<<"),
("NB_MATRIX_MULTIPLY", "@"),
("NB_OR", "|"),
("NB_RSHIFT", ">>"),
("NB_SUBTRACT", "-"),
("NB_TRUE_DIVIDE", "/"),
("NB_XOR", "^"),
]

_specialized_instructions = [
"BINARY_ADD_ADAPTIVE",
"BINARY_ADD_INT",
Expand Down
24 changes: 13 additions & 11 deletions Lib/test/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -1048,15 +1048,17 @@ def generic_visit(self, node):
return code, ast_tree

def assertOpcodeSourcePositionIs(self, code, opcode,
line, end_line, column, end_column):
line, end_line, column, end_column, occurrence=1):

for instr, position in zip(dis.Bytecode(code), code.co_positions()):
if instr.opname == opcode:
self.assertEqual(position[0], line)
self.assertEqual(position[1], end_line)
self.assertEqual(position[2], column)
self.assertEqual(position[3], end_column)
return
occurrence -= 1
if occurrence == 0:
self.assertEqual(position[0], line)
self.assertEqual(position[1], end_line)
self.assertEqual(position[2], column)
self.assertEqual(position[3], end_column)
return

self.fail(f"Opcode {opcode} not found in code")

Expand All @@ -1077,7 +1079,7 @@ def test_compiles_to_extended_op_arg(self):

compiled_code, _ = self.check_positions_against_ast(snippet)

self.assertOpcodeSourcePositionIs(compiled_code, 'INPLACE_SUBTRACT',
self.assertOpcodeSourcePositionIs(compiled_code, 'INPLACE_OP',
line=10_000 + 2, end_line=10_000 + 2,
column=2, end_column=8)
self.assertOpcodeSourcePositionIs(compiled_code, 'INPLACE_ADD',
Expand Down Expand Up @@ -1114,10 +1116,10 @@ def test_complex_single_line_expression(self):
line=1, end_line=1, column=9, end_column=21)
self.assertOpcodeSourcePositionIs(compiled_code, 'BINARY_ADD',
line=1, end_line=1, column=9, end_column=26)
self.assertOpcodeSourcePositionIs(compiled_code, 'BINARY_MATRIX_MULTIPLY',
line=1, end_line=1, column=4, end_column=27)
self.assertOpcodeSourcePositionIs(compiled_code, 'BINARY_SUBTRACT',
line=1, end_line=1, column=0, end_column=27)
self.assertOpcodeSourcePositionIs(compiled_code, 'BINARY_OP',
line=1, end_line=1, column=4, end_column=27, occurrence=1)
self.assertOpcodeSourcePositionIs(compiled_code, 'BINARY_OP',
line=1, end_line=1, column=0, end_column=27, occurrence=2)


class TestExpressionStackSize(unittest.TestCase):
Expand Down
6 changes: 3 additions & 3 deletions Lib/test/test_dis.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ def bug42562():

%3d 2 LOAD_CONST 1 (1)
4 LOAD_CONST 2 (0)
--> 6 BINARY_TRUE_DIVIDE
--> 6 BINARY_OP 7 (/)
8 POP_TOP

%3d 10 LOAD_FAST 1 (tb)
Expand Down Expand Up @@ -1060,7 +1060,7 @@ def _prepare_test_cases():
Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=62, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=64, starts_line=13, is_jump_target=False, positions=None),
Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=1, argrepr='1', offset=66, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='INPLACE_SUBTRACT', opcode=56, arg=None, argval=None, argrepr='', offset=68, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='INPLACE_OP', opcode=123, arg=6, argval=6, argrepr='-=', offset=68, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='STORE_FAST', opcode=125, arg=0, argval='i', argrepr='i', offset=70, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=72, starts_line=14, is_jump_target=False, positions=None),
Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', offset=74, starts_line=None, is_jump_target=False, positions=None),
Expand All @@ -1081,7 +1081,7 @@ def _prepare_test_cases():
Instruction(opname='NOP', opcode=9, arg=None, argval=None, argrepr='', offset=104, starts_line=20, is_jump_target=True, positions=None),
Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=1, argrepr='1', offset=106, starts_line=21, is_jump_target=False, positions=None),
Instruction(opname='LOAD_CONST', opcode=100, arg=7, argval=0, argrepr='0', offset=108, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='BINARY_TRUE_DIVIDE', opcode=27, arg=None, argval=None, argrepr='', offset=110, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='BINARY_OP', opcode=122, arg=7, argval=7, argrepr='/', offset=110, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=112, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='JUMP_FORWARD', opcode=110, arg=15, argval=146, argrepr='to 146', offset=114, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='PUSH_EXC_INFO', opcode=35, arg=None, argval=None, argrepr='', offset=116, starts_line=None, is_jump_target=False, positions=None),
Expand Down
Loading