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_load_file():
srcdir = get_srcdir()
load_py = osp.realpath(osp.join(srcdir, "..", "xdis", "load.py"))
co_file = load_file(load_py)
obj_path = check_object_path(load_py)
(version, timestamp, magic_int, co_module, pypy,
source_size, sip_hash) = load_module(obj_path)
if 3.3 <= version <= 3.7:
statinfo = os.stat(load_py)
assert statinfo.st_size == source_size
assert sip_hash is None
elif version < 3.3:
assert source_size is None, source_size
assert sip_hash is None
for field in CodeTypeUnionFields:
if hasattr(co_file, field):
if field == "co_code" and (pypy or IS_PYPY):
continue
load_file_field = getattr(co_file, field)
load_module_field = getattr(co_module, field)
assert load_module_field == load_file_field, (
"field %s\nmodule:\n\t%s\nfile:\n\t%s" % (field, load_module_field, load_file_field)
# >> 25 POP_BLOCK
# >> 26 LOAD_CONST 0 (None)
# 29 RETURN_VALUE
offset_map = opcode_27.get_jump_target_maps(code, opcode_27)
expected = { 3: [0], 6: [3], 9: [6], 12: [9], 15: [12],
16: [15, 22], 19: [16], 22: [19], 25: [16],
26: [0, 25], 29: [26]}
assert expected == offset_map
offsets = opcode_27.get_jump_targets(code, opcode_27)
assert offsets == [26, 25, 16]
test_pyc = my_dir +'/../test/bytecode_2.7/01_dead_code.pyc'
(version, timestamp, magic_int, co, pypy,
source_size, _) = load_module(test_pyc)
code = co.co_consts[0]
offsets = opcode_27.get_jump_targets(code, opcode_27)
assert [10] == offsets
# from xdis import disassemble_file
# print('\n')
# disassemble_file(test_pyc)
# 2: 0 LOAD_FAST 0 (a)
# 3 POP_JUMP_IF_FALSE 10 (to 10)
#
# 3: 6 LOAD_CONST 1 (5)
# 9 RETURN_VALUE
#
# 5: >> 10 LOAD_CONST 2 (6)
# 13 RETURN_VALUE
codestring = f.read()
f.close()
codeobject1 = compile(codestring, source_filename, "exec")
(
version,
timestamp,
magic_int,
codeobject2,
is_pypy,
source_size,
_,
) = load_module(real_bytecode_filename)
# A hack for PyPy 3.2
if magic_int == 3180 + 7:
magic_int = 48
assert MAGIC == int2magic(magic_int), "magic_int %d vs %d in %s/%s" % (
magic_int,
PYTHON_MAGIC_INT,
os.getcwd(),
real_bytecode_filename,
)
bytecode_filename1 = os.path.join(tempdir, "testing1.pyc")
dump_compile(codeobject1, bytecode_filename1, timestamp, MAGIC)
(version, timestamp, magic_int, codeobject3, is_pypy, source_size, _) = load_module(
real_bytecode_filename, fast_load=not is_pypy
)
def disassemble_file(filename, outstream=sys.stdout, asm_format=False):
"""
disassemble Python byte-code file (.pyc)
If given a Python source file (".py") file, we'll
try to find the corresponding compiled object.
"""
filename = check_object_path(filename)
version, timestamp, magic_int, co, is_pypy, source_size = load_module(filename)
disco(version, co, timestamp, outstream, is_pypy, magic_int, source_size,
asm_format=asm_format)
# print co.co_filename
return filename, co, version, timestamp, magic_int
def line_number_mapping(pyc_filename, src_filename):
(version, timestamp, magic_int, code1, is_pypy,
source_size) = load_module(pyc_filename)
try:
code2 = load_file(src_filename)
except SyntaxError as e:
return str(e)
queue = deque([code1, code2])
mappings = []
opc = get_opcode(version, is_pypy)
number_loop(queue, mappings, opc)
return sorted(mappings, key=lambda x: x[1])
def lineoffsets_in_file(filename, toplevel_only=False):
obj_path = check_object_path(filename)
version, timestamp, magic_int, code, pypy, source_size, sip_hash = load_module(
obj_path
)
if pypy:
variant = "pypy"
else:
variant = None
opc = get_opcode_module(version, variant)
return LineOffsetInfo(opc, code, not toplevel_only)
pass
def disassemble_file(filename, outstream=None):
"""
disassemble Python byte-code file (.pyc)
If given a Python source file (".py") file, we'll
try to find the corresponding compiled object.
"""
filename = check_object_path(filename)
(version, timestamp, magic_int, co, is_pypy, source_size) = load_module(filename)
if type(co) == list:
for con in co:
disco(version, con, outstream)
else:
disco(version, co, outstream, is_pypy=is_pypy)
co = None
bytecode_file
and (not bytecode_file.endswith(".pyo") or bytecode_file.endswith("pyc"))
):
# bytecode_file may be a source file. Try to tun it into a bytecode file for diassembly.
bytecode_file = cache_from_source(bytecode_file)
if bytecode_file and Mfile.readable(bytecode_file):
self.msg("Reading %s ..." % bytecode_file)
(
version,
timestamp,
magic_int,
obj,
is_pypy,
source_size,
sip_hash,
) = load_module(bytecode_file)
elif not curframe:
self.errmsg("No frame selected.")
return
else:
try:
obj = self.proc.eval(args[1])
opts["start_line"] = -1
except:
self.errmsg(
("Object '%s' is not something we can" + " disassemble.")
% args[1]
)
return
# We now have all information. Do the listing.
(obj, proc.list_offset) = dis(
def disassemble_file(
filename, outstream=sys.stdout, asm_format="classic"
):
"""
disassemble Python byte-code file (.pyc)
If given a Python source file (".py") file, we'll
try to find the corresponding compiled object.
If that fails we'll compile internally for the Python version currently running
"""
pyc_filename = None
try:
# FIXME: add whether we want PyPy
pyc_filename = check_object_path(filename)
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: