Skip to content

Instantly share code, notes, and snippets.

@x746e
Created March 9, 2017 15:56
Show Gist options
  • Save x746e/657573c3d02ae6355dfaf63cce1707d4 to your computer and use it in GitHub Desktop.
Save x746e/657573c3d02ae6355dfaf63cce1707d4 to your computer and use it in GitHub Desktop.
% python setup.py test
running pytest
running egg_info
writing dependency_links to xdis.egg-info/dependency_links.txt
writing xdis.egg-info/PKG-INFO
writing top-level names to xdis.egg-info/top_level.txt
reading manifest file 'xdis.egg-info/SOURCES.txt'
reading manifest template 'MANIFEST.in'
writing manifest file 'xdis.egg-info/SOURCES.txt'
running build_ext
================================================================================================================================== test session starts ==================================================================================================================================
platform linux2 -- Python 2.7.3[pypy-2.2.1-final], pytest-3.0.6, py-1.4.32, pluggy-0.4.0
rootdir: /usr/local/google/home/ksp/src/python-xdis, inifile:
collected 13 items
pytest/test_bytecode.py .
pytest/test_disasm.py ...
pytest/test_load_file.py F
test_unit/test_dis.py ....
test_unit/test_load.py F
test_unit/test_magic.py F
test_unit/test_marsh.py .
test_unit/test_opcode.py .
======================================================================================================================================= FAILURES ========================================================================================================================================
____________________________________________________________________________________________________________________________________ test_load_file _____________________________________________________________________________________________________________________________________
@pytest.mark.skipif(sys.version_info >= (3,5),
reason="Doesn't work on 3.5 and later")
def test_load_file():
co = load_file(__file__)
obj_path = check_object_path(__file__)
(version, timestamp, magic_int, co2, pypy,
> source_size) = load_module(obj_path)
test_load_file.py:10:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
filename = '/tmp/test_load_file-vXw6qS.pyc'
code_objects = {"<code object <lambda>, file 'simple_source/04_pypy_lambda.py', line 4>": <code object <lambda>, file 'simple_source/...0007cee980>, '<xdis.code.Code3 instance at 0x0000000007d5a7c0>': <xdis.code.Code3 instance at 0x0000000007d5a7c0>, ...}, fast_load = False
get_code = True
def load_module(filename, code_objects={}, fast_load=False,
get_code=True):
"""load a module without importing it.
load_module(filename: string): version, magic_int, code_object
filename: name of file containing Python byte-code object
(normally a .pyc)
code_object: code_object from this file
version: Python major/minor value e.g. 2.7. or 3.4
magic_int: more specific than version. The actual byte code version of the
code object
Parsing the code object takes a bit of parsing time, but
sometimes all you want is the module info, time string, code size,
python version, etc. For that, set get_code=False.
"""
timestamp = 0
fp = open(filename, 'rb')
magic = fp.read(4)
magic_int = magics.magic2int(magic)
# For reasons I don't understand PyPy 3.2 stores a magic
# of '0'... The two values below are for Python 2.x and 3.x respectively
if magic[0:1] in ['0', b'0']:
magic = magics.int2magic(3180+7)
try:
version = float(magics.versions[magic][:3])
except KeyError:
if len(magic) >= 2:
fp.close()
raise ImportError("Unknown magic number %s in %s" %
> (ord(magic[0])+256*ord(magic[1]), filename))
E ImportError: Unknown magic number 62217 in /tmp/test_load_file-vXw6qS.pyc
../xdis/load.py:103: ImportError
__________________________________________________________________________________________________________________________________ TestLoad.test_basic __________________________________________________________________________________________________________________________________
self = <test_load.TestLoad testMethod=test_basic>
def test_basic(self):
"""Basic test of load_file, check_object_path and load_module"""
filename = __file__
if filename.endswith('.pyo') or filename.endswith('.pyc'):
filename = filename[:-1]
co = load_file(filename)
obj_path = check_object_path(__file__)
if os.path.exists(obj_path):
(version, timestamp, magic_int, co2, is_pypy,
> source_size) = load_module(obj_path)
../test_unit/test_load.py:17:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
filename = '/tmp/test_load-jkQWxH.pyc'
code_objects = {"<code object <lambda>, file 'simple_source/04_pypy_lambda.py', line 4>": <code object <lambda>, file 'simple_source/...0007cee980>, '<xdis.code.Code3 instance at 0x0000000007d5a7c0>': <xdis.code.Code3 instance at 0x0000000007d5a7c0>, ...}, fast_load = False
get_code = True
def load_module(filename, code_objects={}, fast_load=False,
get_code=True):
"""load a module without importing it.
load_module(filename: string): version, magic_int, code_object
filename: name of file containing Python byte-code object
(normally a .pyc)
code_object: code_object from this file
version: Python major/minor value e.g. 2.7. or 3.4
magic_int: more specific than version. The actual byte code version of the
code object
Parsing the code object takes a bit of parsing time, but
sometimes all you want is the module info, time string, code size,
python version, etc. For that, set get_code=False.
"""
timestamp = 0
fp = open(filename, 'rb')
magic = fp.read(4)
magic_int = magics.magic2int(magic)
# For reasons I don't understand PyPy 3.2 stores a magic
# of '0'... The two values below are for Python 2.x and 3.x respectively
if magic[0:1] in ['0', b'0']:
magic = magics.int2magic(3180+7)
try:
version = float(magics.versions[magic][:3])
except KeyError:
if len(magic) >= 2:
fp.close()
raise ImportError("Unknown magic number %s in %s" %
> (ord(magic[0])+256*ord(magic[1]), filename))
E ImportError: Unknown magic number 62217 in /tmp/test_load-jkQWxH.pyc
../xdis/load.py:103: ImportError
________________________________________________________________________________________________________________________________ TestOpcodes.test_basic _________________________________________________________________________________________________________________________________
self = <test_magic.TestOpcodes testMethod=test_basic>
def test_basic(self):
"""Basic test of magic numbers"""
current = imp.get_magic()
> python_via_magic = magics.by_magic[ current ]
E KeyError: '\t\xf3\r\n'
../test_unit/test_magic.py:10: KeyError
========================================================================================================================== 3 failed, 10 passed in 0.54 seconds ==========================================================================================================================
% python --version
Python 2.7.3 (2.2.1+dfsg-1ubuntu0.3, Sep 30 2015, 15:18:40)
[PyPy 2.2.1 with GCC 4.8.4]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment