Fix 3.7+ import as

This commit is contained in:
rocky
2020-01-01 22:59:07 -05:00
parent 6de57249ed
commit c42e16fafe
8 changed files with 30 additions and 12 deletions

View File

@@ -9,7 +9,7 @@ from sys import path
from os import sep, name
import collections.abc
assert osp.basename("a")
assert osp.basename("a") == "a"
assert path
assert sep
assert name

View File

@@ -0,0 +1,12 @@
# 3.6+ type annotations on variables
from typing import List
# RUNNABLE!
y = 2
x: bool
z: int = 5
x = (z == 5)
assert x
assert y == 2
v: List[int] = [1, 2]
assert v[1] == y

View File

@@ -329,19 +329,18 @@ class Python37Parser(Python37BaseParser):
def p_import37(self, args):
"""
stmt ::= import37
stmt ::= import_as37
import_as37 ::= LOAD_CONST LOAD_CONST importlist37 store POP_TOP
# Where does the POP_TOP really belong?
import37 ::= import POP_TOP
attributes ::= IMPORT_FROM ROT_TWO POP_TOP IMPORT_FROM
attributes ::= attributes ROT_TWO POP_TOP IMPORT_FROM
importlist37 ::= importlist37 ROT_TWO IMPORT_FROM
importlist37 ::= importlist37 ROT_TWO POP_TOP IMPORT_FROM
importlist37 ::= importattr37
importattr37 ::= IMPORT_NAME_ATTR IMPORT_FROM
# The 3.7base scanner adds IMPORT_NAME_ATTR
alias ::= IMPORT_NAME_ATTR IMPORT_FROM store
alias ::= IMPORT_NAME_ATTR attributes store
alias ::= IMPORT_NAME_ATTR store
import_from ::= LOAD_CONST LOAD_CONST IMPORT_NAME_ATTR importlist POP_TOP
import_from ::= LOAD_CONST LOAD_CONST importlist POP_TOP
expr ::= attribute37
attribute37 ::= expr LOAD_METHOD

View File

@@ -1,4 +1,4 @@
# Copyright (c) 2017-2019 by Rocky Bernstein
# Copyright (c) 2017-2020 by Rocky Bernstein
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@@ -230,6 +230,7 @@ TABLE_DIRECT = {
(1, 100), (2, 100) ),
'IMPORT_FROM': ( '%{pattr}', ),
'IMPORT_NAME_ATTR': ( '%{pattr}', ),
'attribute': ( '%c.%[1]{pattr}',
(0, 'expr')),
'LOAD_STR': ( '%{pattr}', ),

View File

@@ -37,6 +37,7 @@ def customize_for_version37(self, version):
TABLE_DIRECT.update(
{
"and_not": ("%c and not %c", (0, "expr"), (2, "expr")),
"ann_assign": (
"%|%[2]{attr}: %c\n", 0,
),
@@ -61,7 +62,6 @@ def customize_for_version37(self, version):
(1, "expr"),
(16, "for_block"),
),
"and_not": ("%c and not %c", (0, "expr"), (2, "expr")),
"async_with_stmt": ("%|async with %c:\n%+%c%-", (0, "expr"), 7),
"async_with_as_stmt": (
"%|async with %c as %c:\n%+%c%-",
@@ -77,6 +77,9 @@ def customize_for_version37(self, version):
(25, "else_suite"),
),
"attribute37": ("%c.%[1]{pattr}", 0),
"attributes37": ("%[0]{pattr} import %c",
(0, "IMPORT_NAME_ATTR"),
(1, "IMPORT_FROM")),
"await_expr": ("await %c", 0),
"await_stmt": ("%|%c\n", 0),
"call_ex": ("%c(%p)", (0, "expr"), (1, 100)),
@@ -127,6 +130,8 @@ def customize_for_version37(self, version):
(5, "expr", 27),
),
"ifstmtl": ("%|if %c:\n%+%c%-", (0, "testexpr"), (1, "_ifstmts_jumpl")),
'import_as37': ( '%|import %c as %c\n', 2, -2),
"importattr37": ("%c", (0, "IMPORT_NAME_ATTR")),
'list_if37': ( " if %p%c", (0, 27), 1 ),
"testfalse_not_or": ("not %c or %c", (0, "expr"), (2, "expr")),
"testfalse_not_and": ("not (%c)", 0),

View File

@@ -1,4 +1,4 @@
# Copyright (c) 2015-2019 by Rocky Bernstein
# Copyright (c) 2015-2020 by Rocky Bernstein
# Copyright (c) 2005 by Dan Pascu <dan@windowmaker.org>
# Copyright (c) 2000-2002 by hartmut Goebel <h.goebel@crazy-compilers.com>
# Copyright (c) 1999 John Aycock
@@ -2071,6 +2071,7 @@ class SourceWalker(GenericASTTraversal, object):
try:
self.write(eval(expr, d, d))
except:
from trepan.api import debug; debug()
raise
m = escape.search(fmt, i)
self.write(fmt[i:])