Skip to content

Commit 7ad0c37

Browse files
committed
Correct a couple of bugs...
We weren't distinguising relative imports from absolute imports. Fixes #444 Picking out docstring was broken too.
1 parent b6aa587 commit 7ad0c37

File tree

4 files changed

+22
-24
lines changed

4 files changed

+22
-24
lines changed

uncompyle6/scanners/scanner2.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2015-2023 by Rocky Bernstein
1+
# Copyright (c) 2015-2022 by Rocky Bernstein
22
# Copyright (c) 2005 by Dan Pascu <[email protected]>
33
# Copyright (c) 2000-2002 by hartmut Goebel <[email protected]>
44
#
@@ -38,7 +38,7 @@
3838
from copy import copy
3939

4040
from xdis import code2num, iscode, op_has_argument, instruction_size
41-
from xdis.bytecode import _get_const_info, _get_name_info
41+
from xdis.bytecode import _get_const_info
4242
from uncompyle6.scanner import Scanner, Token
4343

4444
from sys import intern
@@ -320,7 +320,9 @@ def ingest(self, co, classname=None, code_objects={}, show_asm=None):
320320
"BUILD_SET",
321321
):
322322
t = Token(
323-
op_name, oparg, pattr, offset, self.linestarts.get(offset, None), op, has_arg, self.opc
323+
op_name, oparg, pattr, offset,
324+
self.linestarts.get(offset, None),
325+
op, has_arg, self.opc
324326
)
325327
collection_type = op_name.split("_")[1]
326328
next_tokens = self.bound_collection_from_tokens(
@@ -360,7 +362,6 @@ def ingest(self, co, classname=None, code_objects={}, show_asm=None):
360362
pattr = const
361363
pass
362364
elif op in self.opc.NAME_OPS:
363-
_, pattr = _get_name_info(oparg, names)
364365
pattr = names[oparg]
365366
elif op in self.opc.JREL_OPS:
366367
# use instead: hasattr(self, 'patch_continue'): ?

uncompyle6/scanners/tok.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import sys
2020

2121
intern = sys.intern
22+
from typing import Union
2223

2324

2425
def off2int(offset, prefer_last=True):
@@ -60,7 +61,7 @@ def __init__(
6061
opname,
6162
attr=None,
6263
pattr=None,
63-
offset=-1,
64+
offset:Union[int, str]=-1,
6465
linestart=None,
6566
op=None,
6667
has_arg=None,

uncompyle6/semantics/n_actions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,7 @@ def n_ifelsestmtr(self, node):
783783
def n_import_from(self, node):
784784
relative_path_index = 0
785785
if self.version >= (2, 5):
786-
if node[relative_path_index].attr > 0:
786+
if node[relative_path_index].pattr > 0:
787787
node[2].pattr = ("." * node[relative_path_index].attr) + node[2].pattr
788788
if self.version > (2, 7):
789789
if isinstance(node[1].pattr, tuple):

uncompyle6/semantics/pysource.py

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2015-2022 by Rocky Bernstein
1+
# Copyright (c) 2015-2023 by Rocky Bernstein
22
# Copyright (c) 2005 by Dan Pascu <[email protected]>
33
# Copyright (c) 2000-2002 by hartmut Goebel <[email protected]>
44
# Copyright (c) 1999 John Aycock
@@ -131,8 +131,6 @@
131131

132132
import sys
133133

134-
IS_PYPY = "__pypy__" in sys.builtin_module_names
135-
136134
from spark_parser import GenericASTTraversal
137135
from xdis import COMPILER_FLAG_BIT, iscode
138136
from xdis.version_info import PYTHON_VERSION_TRIPLE
@@ -143,7 +141,7 @@
143141
from uncompyle6.scanner import Code, get_scanner
144142
from uncompyle6.scanners.tok import Token
145143
from uncompyle6.semantics.check_ast import checker
146-
from uncompyle6.semantics.consts import (ASSIGN_DOC_STRING, ASSIGN_TUPLE_PARAM,
144+
from uncompyle6.semantics.consts import (ASSIGN_TUPLE_PARAM,
147145
INDENT_PER_LEVEL, LINE_LENGTH, MAP,
148146
MAP_DIRECT, NAME_MODULE, NONE, PASS,
149147
PRECEDENCE, RETURN_LOCALS,
@@ -178,6 +176,8 @@ def unicode(x): return x
178176
"dups": False,
179177
}
180178

179+
IS_PYPY = "__pypy__" in sys.builtin_module_names
180+
181181
TREE_DEFAULT_DEBUG = {"before": False, "after": False}
182182

183183
DEFAULT_DEBUG_OPTS = {
@@ -978,7 +978,6 @@ def get_tuple_parameter(self, ast, name):
978978
return result
979979
# return self.traverse(node[1])
980980
return f"({name}"
981-
raise Exception("Can't find tuple parameter " + name)
982981

983982
def build_class(self, code):
984983
"""Dump class definition, doc string and class body."""
@@ -1193,10 +1192,11 @@ def build_ast(
11931192
del ast # Save memory
11941193
return transform_tree
11951194

1196-
# The bytecode for the end of the main routine has a
1197-
# "return None". However, you can't issue a "return" statement in
1198-
# main. So as the old cigarette slogan goes: I'd rather switch (the token stream)
1199-
# than fight (with the grammar to not emit "return None").
1195+
# The bytecode for the end of the main routine has a "return
1196+
# None". However, you can't issue a "return" statement in
1197+
# main. So as the old cigarette slogan goes: I'd rather switch
1198+
# (the token stream) than fight (with the grammar to not emit
1199+
# "return None").
12001200
if self.hide_internal:
12011201
if len(tokens) >= 2 and not noneInNames:
12021202
if tokens[-1].kind in ("RETURN_VALUE", "RETURN_VALUE_LAMBDA"):
@@ -1257,6 +1257,7 @@ def code_deparse(
12571257

12581258
assert iscode(co)
12591259

1260+
12601261
if version is None:
12611262
version = PYTHON_VERSION_TRIPLE
12621263

@@ -1325,30 +1326,25 @@ def code_deparse(
13251326

13261327
assert not nonlocals
13271328

1328-
if version >= (3, 0):
1329-
load_op = "LOAD_STR"
1330-
else:
1331-
load_op = "LOAD_CONST"
1332-
13331329
# convert leading '__doc__ = "..." into doc string
13341330
try:
13351331
stmts = deparsed.ast
1336-
first_stmt = stmts[0][0]
1337-
if version >= 3.6:
1332+
first_stmt = stmts[0]
1333+
if version >= (3, 6):
13381334
if first_stmt[0] == "SETUP_ANNOTATIONS":
13391335
del stmts[0]
13401336
assert stmts[0] == "sstmt"
13411337
# Nuke sstmt
13421338
first_stmt = stmts[0][0]
13431339
pass
13441340
pass
1345-
if first_stmt == ASSIGN_DOC_STRING(co.co_consts[0], load_op):
1341+
if first_stmt == "docstring":
13461342
print_docstring(deparsed, "", co.co_consts[0])
13471343
del stmts[0]
13481344
if stmts[-1] == RETURN_NONE:
13491345
stmts.pop() # remove last node
13501346
# todo: if empty, add 'pass'
1351-
except:
1347+
except Exception:
13521348
pass
13531349

13541350
deparsed.FUTURE_UNICODE_LITERALS = (

0 commit comments

Comments
 (0)