You've already forked python-uncompyle6
mirror of
https://github.com/rocky/python-uncompyle6.git
synced 2025-08-04 09:22:40 +08:00
Add deparse_code_with_fragments_and_map and simplify
This commit is contained in:
@@ -1,10 +1,9 @@
|
||||
# Copyright (c) 2018 by Rocky Bernstein
|
||||
from uncompyle6.semantics.pysource import SourceWalker, deparse_code
|
||||
import uncompyle6.semantics.fragments as fragments
|
||||
|
||||
# FIXME: does this handle nested code, and lambda properly
|
||||
class LineMapWalker(SourceWalker):
|
||||
def __init__(self, *args, **kwargs):
|
||||
if 'first_line' not in kwargs:
|
||||
first_line = 0
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(LineMapWalker, self).__init__(*args, **kwargs)
|
||||
self.source_linemap = {}
|
||||
@@ -28,6 +27,12 @@ class LineMapWalker(SourceWalker):
|
||||
self.source_linemap[self.current_line_number] = node.linestart
|
||||
return super(LineMapWalker, self).default(node)
|
||||
|
||||
class LineMapFragmentWalker(fragments.FragmentsWalker, LineMapWalker):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(LineMapFragmentWalker, self).__init__(*args, **kwargs)
|
||||
self.source_linemap = {}
|
||||
self.current_line_number = 0
|
||||
|
||||
def deparse_code_with_map(*args, **kwargs):
|
||||
"""
|
||||
Like deparse_code but saves line number correspondences.
|
||||
@@ -35,13 +40,21 @@ def deparse_code_with_map(*args, **kwargs):
|
||||
kwargs['walker'] = LineMapWalker
|
||||
return deparse_code(*args, **kwargs)
|
||||
|
||||
def deparse_code_with_fragments_and_map(*args, **kwargs):
|
||||
"""
|
||||
Like deparse_code_with_map but saves fragments.
|
||||
"""
|
||||
kwargs['walker'] = LineMapFragmentWalker
|
||||
return fragments.deparse_code(*args, **kwargs)
|
||||
|
||||
if __name__ == '__main__':
|
||||
def deparse_test(co):
|
||||
"This is a docstring"
|
||||
import sys
|
||||
sys_version = float(sys.version[0:3])
|
||||
# deparsed = deparse_code(sys_version, co, showasm=True, showast=True)
|
||||
deparsed = deparse_code_with_map(sys_version, co, showasm=False, showast=False,
|
||||
deparsed = deparse_code_with_map(sys_version, co, showasm=False,
|
||||
showast=False,
|
||||
showgrammar=False)
|
||||
a = 1; b = 2
|
||||
print("\n")
|
||||
@@ -49,5 +62,16 @@ if __name__ == '__main__':
|
||||
for line_no in
|
||||
sorted(deparsed.source_linemap.keys())]
|
||||
print(linemap)
|
||||
deparsed = deparse_code_with_fragments_and_map(sys_version,
|
||||
co, showasm=False,
|
||||
showast=False,
|
||||
showgrammar=False)
|
||||
a = 1; b = 2
|
||||
print("\n")
|
||||
linemap2 = [(line_no, deparsed.source_linemap[line_no])
|
||||
for line_no in
|
||||
sorted(deparsed.source_linemap.keys())]
|
||||
print(linemap2)
|
||||
assert linemap == linemap2
|
||||
return
|
||||
deparse_test(deparse_test.__code__)
|
||||
|
Reference in New Issue
Block a user