Bugs in 2.x relative import '.' and 1.x bytecode

This commit is contained in:
rocky
2022-05-14 17:42:30 -04:00
parent 021810bb2c
commit 656a9aa290
4 changed files with 20 additions and 5 deletions

View File

@@ -226,6 +226,8 @@ class Scanner(object):
# Locally we use list for more convenient iteration using indices
linestarts = list(self.opc.findlinestarts(code_obj))
self.linestarts = dict(linestarts)
if not self.linestarts:
return []
# 'List-map' which shows line number of current op and offset of
# first op on following line, given offset of op as index

View File

@@ -521,7 +521,7 @@ class Scanner2(Scanner):
for s in stmt_list:
if code[s] == self.opc.JUMP_ABSOLUTE and s not in pass_stmts:
target = self.get_target(s)
if target > s or self.lines[last_stmt].l_no == self.lines[s].l_no:
if target > s or (self.lines and self.lines[last_stmt].l_no == self.lines[s].l_no):
stmts.remove(s)
continue
j = self.prev[s]
@@ -622,6 +622,7 @@ class Scanner2(Scanner):
parent = self.structs[0]
start = parent["start"]
end = parent["end"]
next_line_byte = end
# Pick inner-most parent for our offset
for struct in self.structs:
@@ -650,7 +651,8 @@ class Scanner2(Scanner):
if setup_target != loop_end_offset:
self.fixed_jumps[offset] = loop_end_offset
(line_no, next_line_byte) = self.lines[offset]
if self.lines:
(line_no, next_line_byte) = self.lines[offset]
# jump_back_offset is the instruction after the SETUP_LOOP
# where we iterate back to.

View File

@@ -113,7 +113,7 @@ class Scanner26(scan.Scanner2):
i = self.next_stmt[last_stmt]
replace = {}
while i < codelen - 1:
if self.lines[last_stmt].next > i:
if self.lines and self.lines[last_stmt].next > i:
# Distinguish "print ..." from "print ...,"
if self.code[last_stmt] == self.opc.PRINT_ITEM:
if self.code[i] == self.opc.PRINT_ITEM:

View File

@@ -30,10 +30,10 @@ def customize_for_version26_27(self, version):
########################################
if version > (2, 6):
TABLE_DIRECT.update({
'except_cond2': ( '%|except %c as %c:\n', 1, 5 ),
"except_cond2": ( "%|except %c as %c:\n", 1, 5 ),
# When a generator is a single parameter of a function,
# it doesn't need the surrounding parenethesis.
'call_generator': ('%c%P', 0, (1, -1, ', ', 100)),
"call_generator": ('%c%P', 0, (1, -1, ', ', 100)),
})
else:
TABLE_DIRECT.update({
@@ -60,3 +60,14 @@ def customize_for_version26_27(self, version):
self.default(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()
self.default(node)
self.n_import_from = n_import_from