Merge branch 'master' into python-2.4

This commit is contained in:
rocky
2018-03-21 19:57:41 -04:00
41 changed files with 516 additions and 101 deletions

View File

@@ -63,9 +63,10 @@ The node position 0 will be associated with "import".
# FIXME: DRY code with pysource
import re, sys
import re
from xdis.code import iscode
from xdis.magics import sysinfo2float
from uncompyle6.semantics import pysource
from uncompyle6 import parser
from uncompyle6.scanner import Token, Code, get_scanner
@@ -1746,8 +1747,12 @@ def deparse_code(version, co, out=StringIO(), showasm=False, showast=False,
'ast': showast,
'grammar': showgrammar
}
return code_deparse(co, out, version, debug_opts, code_objects, compile_mode,
is_pypy, walker)
return code_deparse(co, out,
version=version,
debug_opts=debug_opts,
code_objects=code_objects,
compile_mode=compile_mode,
is_pypy=is_pypy, walker=walker)
def code_deparse(co, out=StringIO(), version=None, is_pypy=None,
debug_opts=DEFAULT_DEBUG_OPTS,
@@ -1775,7 +1780,7 @@ def code_deparse(co, out=StringIO(), version=None, is_pypy=None,
assert iscode(co)
if version is None:
version = float(sys.version[0:3])
version = sysinfo2float()
if is_pypy is None:
is_pypy = IS_PYPY
@@ -1809,7 +1814,11 @@ def code_deparse(co, out=StringIO(), version=None, is_pypy=None,
# convert leading '__doc__ = "..." into doc string
assert deparsed.ast == 'stmts'
deparsed.mod_globs = pysource.find_globals(deparsed.ast, set())
(deparsed.mod_globs,
nonlocals) = (pysource
.find_globals_and_nonlocals(deparsed.ast,
set(), set(),
co, version))
# Just when you think we've forgotten about what we
# were supposed to to: Generate source from AST!
@@ -1848,15 +1857,22 @@ def find_gt(a, x):
return a[i]
raise ValueError
def deparse_code_around_offset(name, offset, version, co, out=StringIO(),
showasm=False, showast=False,
showgrammar=False, is_pypy=False):
def code_deparse_around_offset(name, offset, co, out=StringIO(),
version=None, is_pypy=None,
debug_opts=DEFAULT_DEBUG_OPTS):
"""
Like deparse_code(), but given a function/module name and
offset, finds the node closest to offset. If offset is not an instruction boundary,
we raise an IndexError.
"""
deparsed = deparse_code(version, co, out, showasm, showast, showgrammar, is_pypy)
assert iscode(co)
if version is None:
version = sysinfo2float()
if is_pypy is None:
is_pypy = IS_PYPY
deparsed = code_deparse(co, out, version, is_pypy, debug_opts)
if (name, offset) in deparsed.offsets.keys():
# This is the easy case
return deparsed
@@ -1869,6 +1885,17 @@ def deparse_code_around_offset(name, offset, version, co, out=StringIO(),
deparsed.offsets[name, offset] = deparsed.offsets[name, found_offset]
return deparsed
# Deprecated. Here still for compatability
def deparse_code_around_offset(name, offset, version, co, out=StringIO(),
showasm=False, showast=False,
showgrammar=False, is_pypy=False):
debug_opts = {
'asm': showasm,
'ast': showast,
'grammar': showgrammar
}
return code_deparse(name, offset, co, out, version, is_pypy,
debug_opts)
def op_at_code_loc(code, loc, opc):
"""Return the instruction name at code[loc] using
@@ -1938,14 +1965,7 @@ def deparsed_find(tup, deparsed, code):
# return
# def deparse_test_around(offset, name, co, is_pypy=IS_PYPY):
# sys_version = sys.version_info[0] + (sys.version_info[1] / 10.0)
# walk = deparse_code_around_offset(name, offset, sys_version, co, showasm=False, showast=False,
# showgrammar=False, is_pypy=IS_PYPY)
# deparsed = deparse_code_around_offset(name, offset, sys_version, co,
# showasm=False,
# showast=False,
# showgrammar=False,
# is_pypy=IS_PYPY)
# deparsed = code_deparse_around_offset(name, offset, co)
# print("deparsed source")
# print(deparsed.text, "\n")
# print('------------------------')
@@ -1972,13 +1992,14 @@ def deparsed_find(tup, deparsed, code):
# return
# def get_code_for_fn(fn):
# if hasattr(fn, 'func_code'):
# return fn.func_code
# return fn.__code__
# def test():
# import os, sys
# def div_test(a, b, c):
# return a / b / c
# def gcd(a, b):
# if a > b:
# (a, b) = (b, a)
@@ -1992,9 +2013,10 @@ def deparsed_find(tup, deparsed, code):
# # check_args(['3', '5'])
# # deparse_test(get_code_for_fn(gcd))
# deparse_test(get_code_for_fn(div_test))
# # deparse_test(get_code_for_fn(test))
# # deparse_test(get_code_for_fn(FragmentsWalker.fixup_offsets))
# # deparse_test(get_code_for_fn(FragmentsWalker.n_list))
# print('=' * 30)
# deparse_test_around(408, 'n_list', get_code_for_fn(FragmentsWalker.n_list))
# # deparse_test_around(408, 'n_list', get_code_for_fn(FragmentsWalker.n_build_list))
# # deparse_test(inspect.currentframe().f_code)