Skip to content

Commit

Permalink
Support the new BINARY_OP opcode in python 3.11
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 547243343
  • Loading branch information
martindemello authored and rchen152 committed Jul 11, 2023
1 parent deb8e73 commit e570a9f
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
9 changes: 9 additions & 0 deletions pytype/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1240,3 +1240,12 @@ py_test(
DEPS
.test_base
)

py_test(
NAME
test_py_311
SRCS
test_py_311.py
DEPS
.test_base
)
30 changes: 30 additions & 0 deletions pytype/tests/test_py_311.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""Tests for 3.11 support.
These tests are separated into their own file because the main test suite
doesn't pass under python 3.11 yet; this lets us tests individual features as we
get them working.
"""

from pytype.tests import test_base
from pytype.tests import test_utils


@test_utils.skipBeforePy((3, 11), "Tests specifically for 3.11 support")
class TestPy311(test_base.BaseTest):
"""Tests for python 3.11 support."""

def test_binop(self):
self.Check("""
def f(x: int | str):
pass
def g(x: int, y: int) -> int:
return x & y
def h(x: int, y: int):
x ^= y
""")


if __name__ == "__main__":
test_base.main()
36 changes: 34 additions & 2 deletions pytype/vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -3211,8 +3211,40 @@ def byte_COPY(self, state, op):
return state

def byte_BINARY_OP(self, state, op):
del op
return state
"""Implementation of BINARY_OP opcode."""
# Python 3.11 unified a lot of BINARY_* and INPLACE_* opcodes into a single
# BINARY_OP. The underlying operations remain unchanged, so we can just
# dispatch to them.
binops = [
self.byte_BINARY_ADD,
self.byte_BINARY_AND,
self.byte_BINARY_FLOOR_DIVIDE,
self.byte_BINARY_LSHIFT,
self.byte_BINARY_MATRIX_MULTIPLY,
self.byte_BINARY_MULTIPLY,
self.byte_BINARY_MODULO, # NB_REMAINDER in 3.11
self.byte_BINARY_OR,
self.byte_BINARY_POWER,
self.byte_BINARY_RSHIFT,
self.byte_BINARY_SUBTRACT,
self.byte_BINARY_TRUE_DIVIDE,
self.byte_BINARY_XOR,
self.byte_INPLACE_ADD,
self.byte_INPLACE_AND,
self.byte_INPLACE_FLOOR_DIVIDE,
self.byte_INPLACE_LSHIFT,
self.byte_INPLACE_MATRIX_MULTIPLY,
self.byte_INPLACE_MULTIPLY,
self.byte_INPLACE_MODULO, # NB_INPLACE_REMAINDER in 3.11
self.byte_INPLACE_OR,
self.byte_INPLACE_POWER,
self.byte_INPLACE_RSHIFT,
self.byte_INPLACE_SUBTRACT,
self.byte_INPLACE_TRUE_DIVIDE,
self.byte_INPLACE_XOR,
]
binop = binops[op.arg]
return binop(state, op)

def byte_SEND(self, state, op):
del op
Expand Down

0 comments on commit e570a9f

Please sign in to comment.