Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_get_jump_targets():
my_dir = osp.dirname(osp.abspath(__file__))
# Python 2.7 code
code = bug708901.__code__
# FIXME: Consider loading from a file like below to remove
# dependence on running interpreter
if PYTHON_VERSION == 2.7:
# 19: 0 SETUP_LOOP 23 (to 26)
# 3 LOAD_GLOBAL 0 (range)
# 6 LOAD_CONST 1 (1)
#
# 20: 9 LOAD_CONST 2 (10)
# 12 CALL_FUNCTION 2 (2 positional, 0 keyword pair)
# 15 GET_ITER
# >> 16 FOR_ITER 6 (to 25)
# 19 STORE_FAST 0 (res)
#
# 21: 22 JUMP_ABSOLUTE 16 (to 16)
# >> 25 POP_BLOCK
# >> 26 LOAD_CONST 0 (None)
# 29 RETURN_VALUE
offset_map = opcode_27.get_jump_target_maps(code, opcode_27)
files.extend(
[os.path.normpath(os.path.join(root, n))
for n in basenames
for pat in patterns
if fnmatch(n, pat)])
files = []
# Change directories so use relative rather than
# absolute paths. This speeds up things, and allows
# main() to write to a relative-path destination.
cwd = os.getcwd()
os.chdir(src_dir)
if opts['do_compile']:
compiled_version = opts['compiled_version']
if compiled_version and PYTHON_VERSION != compiled_version:
sys.stderr.write("Not compiling: desired Python version is %s "
"but we are running %s\n" %
(compiled_version, PYTHON_VERSION))
else:
for root, dirs, basenames in os.walk(src_dir):
file_matches(files, root, basenames, PY)
for sfile in files:
py_compile.compile(sfile)
pass
pass
files = []
pass
pass
for root, dirs, basenames in os.walk('.'):
# Turn root into a relative path
# FIXME we could call super and ten have just the diffs below
for field in "co_consts co_names co_varnames co_freevars co_cellvars".split():
val = getattr(self, field)
if isinstance(val, list):
setattr(self, field, tuple(val))
if isinstance(self.co_lnotab, dict):
d = self.co_lnotab
self.co_lnotab = sorted(zip(d.keys(), d.values()), key=lambda tup: tup[0])
if isinstance(self.co_lnotab, list):
# We assume we have a list of tuples:
# (offset, linenumber) which we convert
# into the encoded format
self.encode_lineno_tab()
if PYTHON_VERSION >= 3.8:
args = (
self.co_argcount,
self.co_posonlyargcount,
self.co_kwonlyargcount,
self.co_nlocals,
self.co_stacksize,
self.co_flags,
self.co_code,
self.co_consts,
self.co_names,
self.co_varnames,
self.co_filename,
self.co_name,
self.co_firstlineno,
self.co_lnotab,
self.co_freevars,
def main():
Usage_short = """usage: %s FILE...
Type -h for for full help.""" % program
if not (2.5 <= PYTHON_VERSION <= 3.6):
sys.stderr(print("This works on Python version 2.5..3.6; have %s" % PYTHON_VERSION))
if len(sys.argv) == 1:
sys.stderr(print("No file(s) given"))
print(Usage_short, file=sys.stderr)
sys.exit(1)
try:
opts, files = getopt.getopt(sys.argv[1:], 'hVU',
['help', 'version'])
except getopt.GetoptError as e:
print('%s: %s' % (os.path.basename(sys.argv[0]), e), file=sys.stderr)
sys.exit(-1)
for opt, val in opts:
if opt in ('-h', '--help'):
from xdis import load_module, PYTHON_VERSION, IS_PYPY
(
python_version,
timestamp,
magic_int,
co,
is_pypy,
source_size,
sip_hash,
) = load_module(mainpyfile, code_objects=None, fast_load=False)
if is_pypy != IS_PYPY:
print("Bytecode is pypy %s, but we are %s." % (is_pypy, IS_PYPY))
print("For a cross-version debugger, use trepan-xpy with x-python.")
sys.exit(2)
if python_version != PYTHON_VERSION:
print(
"Bytecode is for version %s but we are version %s."
% (python_version, PYTHON_VERSION)
)
print("For a cross-version debugger, use trepan-xpy with x-python.")
sys.exit(2)
py_file = co.co_filename
if osp.isabs(py_file):
try_file = py_file
else:
mainpydir = osp.dirname(mainpyfile)
tag = sys.implementation.cache_tag
dirnames = (
[osp.join(mainpydir, tag), mainpydir]
+ os.environ["PATH"].split(osp.pathsep)
if fs_size and fs_size != my_size:
return False, (
"frame file size, %d bytes, and local file size, %d bytes, on file %s mismatch" %
(fs_size, my_size, path)
)
return True, None
def is_exec_stmt(frame):
"""Return True if we are looking at an exec statement"""
return hasattr(frame, "f_back") and get_call_function_name(frame) == "exec"
import dis
opc = get_opcode(PYTHON_VERSION, IS_PYPY)
def get_call_function_name(frame):
"""If f_back is looking at a call function, return
the name for it. Otherwise return None"""
f_back = frame.f_back
if not f_back:
return None
if "CALL_FUNCTION" != Mbytecode.op_at_frame(f_back):
return None
co = f_back.f_code
code = co.co_code
# labels = dis.findlabels(code)
linestarts = dict(dis.findlinestarts(co))
offset = f_back.f_lasti
version, timestamp, magic_int, co, is_pypy, source_size, sip_hash = load_module(pyc_filename)
except:
# Hack alert: we're using pyc_filename set as a proxy for whether the filename exists.
# check_object_path() will succeed if the file exists.
if pyc_filename is None:
raise
stat = os.stat(filename)
source = open(filename, "r").read()
co = compile(source, filename, "exec")
is_pypy = IS_PYPY
magic_int = PYTHON_MAGIC_INT
sip_hash = 0
source_size = stat.st_size
timestamp = stat.st_mtime
version = PYTHON_VERSION
else:
filename = pyc_filename
if asm_format == "header":
show_module_header(
version,
co,
timestamp,
outstream,
is_pypy,
magic_int,
source_size,
sip_hash,
show_filename=True,
)
else:
if IS_PYPY:
variant = "pypy"
else:
variant = ""
opc = get_opcode_module(None, variant)
for opname, opcode, in opc.opmap.items():
if opname in ("EXTENDED_ARG", "NOP"):
continue
xdis_args = [opcode, opc]
dis_args = [opcode]
if op_has_argument(opcode, opc):
xdis_args.append(0)
dis_args.append(0)
if (
PYTHON_VERSION > 3.7
and opcode in opc.CONDITION_OPS
and opname
not in (
"JUMP_IF_FALSE_OR_POP",
"JUMP_IF_TRUE_OR_POP",
"POP_JUMP_IF_FALSE",
"POP_JUMP_IF_TRUE",
"SETUP_FINALLY",
)
):
xdis_args.append(0)
dis_args.append(0)
effect = xstack_effect(*xdis_args)
check_effect = dis.stack_effect(*dis_args)
if effect == -100:
version, timestamp, magic_int, co, is_pypy, source_size, sip_hash = load_module(pyc_filename)
except:
# Hack alert: we're using pyc_filename set as a proxy for whether the filename exists.
# check_object_path() will succeed if the file exists.
if pyc_filename is None:
raise
stat = os.stat(filename)
source = open(filename, "r").read()
co = compile(source, filename, "exec")
is_pypy = IS_PYPY
magic_int = PYTHON_MAGIC_INT
sip_hash = 0
source_size = stat.st_size
timestamp = stat.st_mtime
version = PYTHON_VERSION
else:
filename = pyc_filename
if header:
show_module_header(
version,
co,
timestamp,
outstream,
is_pypy,
magic_int,
source_size,
sip_hash,
show_filename=True,
)