Merge pull request #396 from scopatz/pep465

Implementation of Pep465
This commit is contained in:
Anthony Scopatz 2015-10-04 12:42:38 -04:00
commit 04605cec75
5 changed files with 49 additions and 24 deletions

View file

@ -132,6 +132,10 @@ def test_binop_minus():
def test_binop_times():
yield check_ast, '42 * 65'
@skip_if(VER_MAJOR_MINOR < VER_3_5)
def test_binop_matmult():
yield check_ast, 'x @ y', False
def test_binop_div():
yield check_ast, '42 / 65'
@ -852,6 +856,10 @@ def test_sub_eq():
def test_times_eq():
yield check_stmts, 'x = 42; x *= 2'
@skip_if(VER_MAJOR_MINOR < VER_3_5)
def test_matmult_eq():
yield check_stmts, 'x @= y', False
def test_div_eq():
yield check_stmts, 'x = 42; x /= 2'

View file

@ -1,8 +1,8 @@
"""The xonsh abstract syntax tree node."""
from __future__ import unicode_literals, print_function
from ast import Module, Num, Expr, Str, Bytes, UnaryOp, UAdd, USub, Invert, \
BinOp, Add, Sub, Mult, Div, FloorDiv, Mod, Pow, Compare, Lt, Gt, LtE, \
GtE, Eq, NotEq, In, NotIn, Is, IsNot, Not, BoolOp, Or, And, Subscript, \
BinOp, Add, Sub, Mult, Div, FloorDiv, Mod, Pow, Compare, Lt, Gt, \
LtE, GtE, Eq, NotEq, In, NotIn, Is, IsNot, Not, BoolOp, Or, And, Subscript, \
Load, Slice, List, Tuple, Set, Dict, AST, NameConstant, \
Name, GeneratorExp, Store, comprehension, ListComp, SetComp, DictComp, \
Assign, AugAssign, BitXor, BitAnd, BitOr, LShift, RShift, Assert, Delete, \
@ -13,7 +13,12 @@ from ast import Module, Num, Expr, Str, Bytes, UnaryOp, UAdd, USub, Invert, \
Interactive, Expression, dump
from ast import Ellipsis, Index # pylint:disable=unused-import,redefined-builtin
from xonsh.tools import subproc_toks
from xonsh.tools import subproc_toks, VER_3_5, VER_MAJOR_MINOR
if VER_3_5 <= VER_MAJOR_MINOR:
from ast import MatMult
else:
MatMult = None
STATEMENTS = (FunctionDef, ClassDef, Return, Delete, Assign, AugAssign, For,
While, If, With, Raise, Try, Assert, Import, ImportFrom, Global,

View file

@ -28,14 +28,14 @@ _op_map = {
',': 'COMMA', '.': 'PERIOD', ';': 'SEMI', ':': 'COLON',
'...': 'ELLIPSIS',
# basic operators
'+': 'PLUS', '-': 'MINUS', '*': 'TIMES', '/': 'DIVIDE',
'+': 'PLUS', '-': 'MINUS', '*': 'TIMES', '@': 'AT', '/': 'DIVIDE',
'//': 'DOUBLEDIV', '%': 'MOD', '**': 'POW', '|': 'PIPE',
'~': 'TILDE', '^': 'XOR', '<<': 'LSHIFT', '>>': 'RSHIFT',
'<': 'LT', '<=': 'LE', '>': 'GT', '>=': 'GE', '==': 'EQ',
'!=': 'NE', '->': 'RARROW',
# assignment operators
'=': 'EQUALS', '+=': 'PLUSEQUAL', '-=': 'MINUSEQUAL',
'*=': 'TIMESEQUAL', '/=': 'DIVEQUAL', '%=': 'MODEQUAL',
'*=': 'TIMESEQUAL', '@=': 'ATEQUAL', '/=': 'DIVEQUAL', '%=': 'MODEQUAL',
'**=': 'POWEQUAL', '<<=': 'LSHIFTEQUAL', '>>=': 'RSHIFTEQUAL',
'&=': 'AMPERSANDEQUAL', '^=': 'XOREQUAL', '|=': 'PIPEEQUAL',
'//=': 'DOUBLEDIVEQUAL',

View file

@ -8,21 +8,8 @@ from ply import yacc
from xonsh import ast
from xonsh.lexer import Lexer
VER_3_4 = (3, 4)
VER_3_5 = (3, 5)
VER_MAJOR_MINOR = sys.version_info[:2]
V_MAJOR_MINOR = 'v{0}{1}'.format(*sys.version_info[:2])
def docstring_by_version(**kwargs):
"""Sets a docstring by the python version."""
doc = kwargs.get(V_MAJOR_MINOR, None)
if V_MAJOR_MINOR is None:
raise RuntimeError('unrecognized version ' + V_MAJOR_MINOR)
def dec(f):
f.__doc__ = doc
return f
return dec
from xonsh.tools import VER_3_4, VER_3_5, VER_MAJOR_MINOR, V_MAJOR_MINOR, \
docstring_by_version
class Location(object):
@ -767,6 +754,7 @@ class Parser(object):
'+=': ast.Add,
'-=': ast.Sub,
'*=': ast.Mult,
'@=': ast.MatMult,
'/=': ast.Div,
'%=': ast.Mod,
'//=': ast.FloorDiv,
@ -805,8 +793,11 @@ class Parser(object):
lineno=self.lineno,
col_offset=self.col)
elif lenp == 4:
op = self._augassign_op[p2]()
p0 = ast.AugAssign(target=p1[0], op=op, value=p[3],
op = self._augassign_op[p2]
if op is None:
self._parse_error('operation {0!r} not supported'.format(p2),
self.currloc(lineno=p.lineno, column=p.lexpos))
p0 = ast.AugAssign(target=p1[0], op=op(), value=p[3],
lineno=self.lineno, col_offset=self.col)
elif lenp == 5 or lenp == 6:
if lenp == 5:
@ -874,6 +865,7 @@ class Parser(object):
"""augassign : PLUSEQUAL
| MINUSEQUAL
| TIMESEQUAL
| ATEQUAL
| DIVEQUAL
| MODEQUAL
| AMPERSANDEQUAL
@ -1519,6 +1511,7 @@ class Parser(object):
'+': ast.Add,
'-': ast.Sub,
'*': ast.Mult,
'@': ast.MatMult,
'/': ast.Div,
'%': ast.Mod,
'//': ast.FloorDiv
@ -1555,12 +1548,16 @@ class Parser(object):
def p_op_factor(self, p):
"""op_factor : TIMES factor
| AT factor
| DIVIDE factor
| MOD factor
| DOUBLEDIV factor
"""
op = self._term_binops[p[1]]()
p[0] = [op, p[2]]
op = self._term_binops[p[1]]
if op is None:
self._parse_error('operation {0!r} not supported'.format(p[1]),
self.currloc(lineno=p.lineno, column=p.lexpos))
p[0] = [op(), p[2]]
_factor_ops = {'+': ast.UAdd, '-': ast.USub, '~': ast.Invert}

View file

@ -43,6 +43,21 @@ ON_MAC = (platform.system() == 'Darwin')
ON_LINUX = (platform.system() == 'Linux')
ON_POSIX = (os.name == 'posix')
VER_3_4 = (3, 4)
VER_3_5 = (3, 5)
VER_MAJOR_MINOR = sys.version_info[:2]
V_MAJOR_MINOR = 'v{0}{1}'.format(*sys.version_info[:2])
def docstring_by_version(**kwargs):
"""Sets a docstring by the python version."""
doc = kwargs.get(V_MAJOR_MINOR, None)
if V_MAJOR_MINOR is None:
raise RuntimeError('unrecognized version ' + V_MAJOR_MINOR)
def dec(f):
f.__doc__ = doc
return f
return dec
class XonshError(Exception):
pass