Skip to content

Commit

Permalink
Correct 2.5-7 relative import formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
rocky committed May 14, 2022
1 parent 656a9aa commit fa9cc4c
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 9 deletions.
Binary file added test/bytecode_2.5/01_rel_import.pyc
Binary file not shown.
Binary file added test/bytecode_2.6/01_rel_import.pyc
Binary file not shown.
Binary file modified test/bytecode_2.7/01_rel_import.pyc
Binary file not shown.
2 changes: 2 additions & 0 deletions test/simple_source/stmts/01_rel_import.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# Tests relative imports
from . import bogus
from .. import foo
from ..bar import baz
13 changes: 11 additions & 2 deletions uncompyle6/semantics/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,8 +476,17 @@
"kv2": ("%c: %c", 1, 2),
"import": ("%|import %c\n", 2),
"importlist": ("%C", (0, maxint, ", ")),
"import_from": ("%|from %[2]{pattr} import %c\n", (3, "importlist")),
"import_from_star": ("%|from %[2]{pattr} import *\n",),

# Note: the below rule isn't really complete:
# n_import_from() smashes node[2].pattr
"import_from": (
"%|from %[2]{pattr} import %c\n",
(3, "importlist")
),

"import_from_star": (
"%|from %[2]{pattr} import *\n",
),
}


Expand Down
6 changes: 6 additions & 0 deletions uncompyle6/semantics/customize25.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,9 @@ def tryfinallystmt(node):
node[1][0][0][0].kind = 'tf_tryelsestmt'
self.default(node)
self.n_tryfinallystmt = tryfinallystmt

def n_import_from(node):
if node[0].pattr > 0:
node[2].pattr = ("." * node[0].pattr) + node[2].pattr
self.default(node)
self.n_import_from = n_import_from
9 changes: 2 additions & 7 deletions uncompyle6/semantics/customize26_27.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,7 @@ def n_call(node):
self.n_call = n_call

def n_import_from(node):
import_name = node[2]
if import_name == "IMPORT_NAME" and import_name.pattr == "":
fmt = "%|from . import %c\n"
self.template_engine(
(fmt, (3, "importlist")), node
)
self.prune()
if node[0].pattr > 0:
node[2].pattr = ("." * node[0].pattr) + node[2].pattr
self.default(node)
self.n_import_from = n_import_from

0 comments on commit fa9cc4c

Please sign in to comment.