def compare_code_with_srcfile(pyc_filename, src_filename, verify): """Compare a .pyc with a source code file. If everything is okay, None is returned. Otherwise a string message describing the mismatch is returned. """ (version, timestamp, magic_int, code_obj1, is_pypy, source_size) = load_module(pyc_filename) if magic_int != PYTHON_MAGIC_INT: msg = ( "Can't compare code - Python is running with magic %s, but code is magic %s " % (PYTHON_MAGIC_INT, magic_int)) return msg try: code_obj2 = load_file(src_filename) except SyntaxError as e: # src_filename can be the first of a group sometimes return str(e).replace(src_filename, pyc_filename) cmp_code_objects(version, is_pypy, code_obj1, code_obj2, verify) if verify == "verify-run": try: retcode = call("%s %s" % (sys.executable, src_filename), shell=True) if retcode != 0: return "Child was terminated by signal %d" % retcode pass except OSError as e: return "Execution failed: %s" % e pass return None
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)) print("ok %s" % field)
def compare_code_with_srcfile(pyc_filename, src_filename): """Compare a .pyc with a source code file.""" version, timestamp, magic_int, code_obj1, is_pypy = load_module(pyc_filename) if magic_int != PYTHON_MAGIC_INT: msg = ("Can't compare code - Python is running with magic %s, but code is magic %s " % (PYTHON_MAGIC_INT, magic_int)) return msg code_obj2 = load_file(src_filename) cmp_code_objects(version, is_pypy, code_obj1, code_obj2) return None
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 compare_code_with_srcfile(pyc_filename, src_filename, weak_verify=False): """Compare a .pyc with a source code file.""" (version, timestamp, magic_int, code_obj1, is_pypy, source_size) = load_module(pyc_filename) if magic_int != PYTHON_MAGIC_INT: msg = ("Can't compare code - Python is running with magic %s, but code is magic %s " % (PYTHON_MAGIC_INT, magic_int)) return msg try: code_obj2 = load_file(src_filename) except SyntaxError as e: # src_filename can be the first of a group sometimes return str(e).replace(src_filename, pyc_filename) cmp_code_objects(version, is_pypy, code_obj1, code_obj2, ignore_code=weak_verify) return None
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) if (3, 3) <= sys.version_info: statinfo = os.stat(__file__) assert statinfo.st_size == source_size else: assert source_size is None if IS_PYPY: assert str(co) == str(co2) else: assert co == co2
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) if (3,3) <= sys.version_info: statinfo = os.stat(__file__) assert statinfo.st_size == source_size else: assert source_size is None if IS_PYPY: assert str(co) == str(co2) else: assert co == co2
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, sip_hash) = load_module(obj_path) self.assertEqual(sys.version[0:3], str(version)) if IS_PYPY: self.assertTrue("Skipped until we get better code comparison on PYPY") else: for attr in """co_names co_flags co_argcount co_varnames""".split(): self.assertEqual(getattr(co, attr), getattr(co2, attr), attr) else: self.assertTrue("Skipped because we can't find %s" % obj_path)
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) self.assertEqual(sys.version[0:3], str(version)) if IS_PYPY: self.assertTrue("Skipped until we get better code comparison on PYPY") else: for attr in """co_filename co_names co_flags co_argcount co_varnames""".split(): self.assertEqual(getattr(co, attr), getattr(co2, attr), attr) else: self.assertTrue("Skipped because we can't find %s" % obj_path)