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_opcode():
opc = get_opcode(PYTHON_VERSION, IS_PYPY)
opmap = dict([(k.replace('+', '_'), v)
for (k, v) in dis.opmap.items()])
print("Extra in dis:", set(opmap.items()) - set(opc.opmap.items()))
print("Extra in xdis:", set(opc.opmap.items()) - set(opmap.items()))
for item in opmap.items():
assert item in opc.opmap.items(), item
fields_str = "hascompare hasconst hasfree hasjabs hasjrel haslocal"
# PyPy 2.7.13 changes opcodes mid-version. It is too complicated
# to figure out where the change actually occurred
# Pypy 3.6.9 may or may not have JUMP_IF_NOT_DEBUG
if not (IS_PYPY and PYTHON_VERSION in (2.7, 3.6)):
assert all(item in opmap.items() for item in opc.opmap.items())
elif (IS_PYPY and PYTHON_VERSION == 3.6):
assert effect != -100, (
"%d (%s) needs adjusting; should be: should have effect %d"
% (opcode, opname, check_effect)
)
if has_arg:
op_val = "with operand %d" % dis_args[1]
else:
op_val = ""
assert check_effect == effect, (
"%d (%s) %s not okay; effect %d vs %d"
% (opcode, opname, op_val, effect, check_effect)
)
print("%d (%s) is good: effect %d" % (opcode, opname, effect))
if xdis.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]
# TODO: if opcode takes an argument, we should vary the arg and try
# values in addition to 0 as done below.
if op_has_argument(opcode, opc):
xdis_args.append(0)
dis_args.append(0)
has_arg = True
def test_complete_identifier(self):
from trepan.processor.command import base_cmd as mBaseCmd
from trepan.processor import complete as mComplete
self.dbgr = Mdebugger.Trepan()
cmdproc = self.dbgr.core.processor
cmdproc.curframe = inspect.currentframe()
cmd = mBaseCmd.DebuggerCommand(cmdproc)
self.assertEqual(mComplete.complete_id_and_builtins(cmd, 'ma'),
['map', 'max'])
self.assertEqual(mComplete.complete_identifier(cmd, 'm'),
['mBaseCmd', 'mComplete'])
return
if not IS_PYPY:
def test_completion(self):
self.dbgr = Mdebugger.Trepan()
for line, expect_completion in [
['set basename ', ['off', 'on']],
['where', ['where ']], # Single alias completion
['sho', ['show']], # Simple single completion
['un', ['unalias', 'undisplay']], # Simple multiple completion
['python ', []], # Don't add anything - no more
['set basename o', ['off', 'on']],
['set basename of', ['off']],
# Multiple completion on two words
['set auto', ['autoeval', 'autolist', 'autopc', 'autopython']],
def test_basic(self):
"""Basic test of magic numbers"""
current = imp.get_magic()
if hasattr(sys, 'version_info'):
version = '.'.join([str(v) for v in sys.version_info[0:3]])
if IS_PYPY:
version += 'pypy'
self.assertTrue(version in magics.magics.keys(),
"version %s is not in magic.magics.keys: %s" %
(version, magics.magics.keys()))
self.assertEqual(current, magics.int2magic(magics.magic2int(current)))
lookup = str(PYTHON_VERSION)
if IS_PYPY:
lookup += 'pypy'
self.assertTrue(lookup in magics.magics.keys(),
"PYTHON VERSION %s is not in magic.magics.keys: %s" %
(lookup, magics.magics.keys()))
if not (3, 5, 2) <= sys.version_info < (3, 6, 0):
self.assertEqual(magics.sysinfo2magic(), current,
"magic from imp.get_magic() for %s "
print("%s: Python script file '%s' does not exist"
% (__title__, mainpyfile,), file=sys.stderr)
sys.exit(1)
elif not is_readable:
print("%s: Can't read Python script file '%s'"
% (__title__, mainpyfile, ), file=sys.stderr)
sys.exit(1)
return
if Mfile.is_compiled_py(mainpyfile):
try:
from xdis import load_module, PYTHON_VERSION, IS_PYPY
(python_version, timestamp, magic_int, co, is_pypy,
source_size) = load_module(mainpyfile, code_objects=None,
fast_load=True)
assert is_pypy == IS_PYPY
assert python_version == PYTHON_VERSION, \
"bytecode is for version %s but we are version %s" % (
python_version, PYTHON_VERSION)
# We should we check version magic_int
py_file = co.co_filename
if osp.isabs(py_file):
try_file = py_file
else:
mainpydir = osp.dirname(mainpyfile)
dirnames = [mainpydir] + os.environ['PATH'].split(os.pathsep) + ['.']
try_file = Mclifns.whence_file(py_file, dirnames)
if osp.isfile(try_file):
mainpyfile = try_file
pass
if Mfile.is_compiled_py(mainpyfile):
try:
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
def deparse_fn(code):
try:
from uncompyle6.semanitcs.linemap import (
deparse_code_with_fragments_and_map as deparse_code,
)
except ImportError:
return None
sys_version = sys.version[:5]
try:
float_version = py_str2float(sys_version)
deparsed = deparse_code(float_version, code, is_pypy=IS_PYPY)
return deparsed
except:
raise
return None
def sysinfo2magic(version_info=sys.version_info):
"""Convert a list sys.versions_info compatible list into a 'canonic'
floating-point number which that can then be used to look up a
magic number. Note that this can raise an exception.
"""
# FIXME: DRY with sysinfo2float()
vers_str = ".".join([str(v) for v in version_info[0:3]])
if version_info[3] != "final":
vers_str += "".join([str(v) for v in version_info[3:]])
if IS_PYPY:
vers_str += "pypy"
else:
try:
import platform
platform = platform.python_implementation()
if platform in ("Jython", "Pyston"):
vers_str += platform
pass
except ImportError:
# Python may be too old, e.g. < 2.6 or implementation may
# just not have platform
pass
return magics[vers_str]
"""
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:
filename = pyc_filename
if header:
show_module_header(
version,
co,
timestamp,
outstream,
is_pypy,
magic_int,
def check_stack_effect():
import dis
from xdis import IS_PYPY
from xdis.op_imports import get_opcode_module
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