Skip to content

Commit 4ab92c8

Browse files
committed
add matrix multiplication operator support to 2to3
1 parent 0134a35 commit 4ab92c8

6 files changed

Lines changed: 19 additions & 9 deletions

File tree

Lib/lib2to3/Grammar.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ small_stmt: (expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt |
5656
expr_stmt: testlist_star_expr (augassign (yield_expr|testlist) |
5757
('=' (yield_expr|testlist_star_expr))*)
5858
testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [',']
59-
augassign: ('+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' |
59+
augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' |
6060
'<<=' | '>>=' | '**=' | '//=')
6161
# For normal assignments, additional restrictions enforced by the interpreter
6262
print_stmt: 'print' ( [ test (',' test)* [','] ] |
@@ -119,7 +119,7 @@ xor_expr: and_expr ('^' and_expr)*
119119
and_expr: shift_expr ('&' shift_expr)*
120120
shift_expr: arith_expr (('<<'|'>>') arith_expr)*
121121
arith_expr: term (('+'|'-') term)*
122-
term: factor (('*'|'/'|'%'|'//') factor)*
122+
term: factor (('*'|'@'|'/'|'%'|'//') factor)*
123123
factor: ('+'|'-'|'~') factor | power
124124
power: atom trailer* ['**' factor]
125125
atom: ('(' [yield_expr|testlist_gexp] ')' |

Lib/lib2to3/pgen2/grammar.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ def report(self):
149149
{ LBRACE
150150
} RBRACE
151151
@ AT
152+
@= ATEQUAL
152153
== EQEQUAL
153154
!= NOTEQUAL
154155
<> NOTEQUAL

Lib/lib2to3/pgen2/token.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,13 @@
5757
DOUBLESLASH = 48
5858
DOUBLESLASHEQUAL = 49
5959
AT = 50
60-
OP = 51
61-
COMMENT = 52
62-
NL = 53
63-
RARROW = 54
64-
ERRORTOKEN = 55
65-
N_TOKENS = 56
60+
ATEQUAL = 51
61+
OP = 52
62+
COMMENT = 53
63+
NL = 54
64+
RARROW = 55
65+
ERRORTOKEN = 56
66+
N_TOKENS = 57
6667
NT_OFFSET = 256
6768
#--end constants--
6869

Lib/lib2to3/pgen2/tokenize.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def maybe(*choices): return group(*choices) + '?'
8484
# recognized as two instances of =).
8585
Operator = group(r"\*\*=?", r">>=?", r"<<=?", r"<>", r"!=",
8686
r"//=?", r"->",
87-
r"[+\-*/%&|^=<>]=?",
87+
r"[+\-*/%&@|^=<>]=?",
8888
r"~")
8989

9090
Bracket = '[][(){}]'

Lib/lib2to3/tests/test_parser.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ def invalid_syntax(self, code):
4848
raise AssertionError("Syntax shouldn't have been valid")
4949

5050

51+
class TestMatrixMultiplication(GrammarTest):
52+
def test_matrix_multiplication_operator(self):
53+
self.validate("a @ b")
54+
self.validate("a @= b")
55+
56+
5157
class TestRaiseChanges(GrammarTest):
5258
def test_2x_style_1(self):
5359
self.validate("raise")

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ Tests
168168
Tools/Demos
169169
-----------
170170

171+
- Add support for the PEP 465 matrix multiplication operator to 2to3.
172+
171173
- Issue #16047: Fix module exception list and __file__ handling in freeze.
172174
Patch by Meador Inge.
173175

0 commit comments

Comments
 (0)