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)
Esempio n. 2
0
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:
        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:
        disco(
            bytecode_version=version,
            co=co,
            timestamp=timestamp,
            out=outstream,
            is_pypy=is_pypy,
            magic_int=magic_int,
            source_size=source_size,
            sip_hash=sip_hash,
            asm_format=asm_format,
        )
    # print co.co_filename
    return filename, co, version, timestamp, magic_int, is_pypy, source_size, sip_hash
Esempio n. 3
0
def read_pyc(filename):
    filename = check_object_path(filename)
    code_objects = {}
    (version, timestamp, magic_int, co, is_pypy,
     source_size) = load_module(filename, code_objects)
    print(version)
    print(timestamp)
    code_obj(co)
Esempio n. 4
0
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
Esempio n. 5
0
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
Esempio n. 6
0
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
Esempio n. 7
0
    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
Esempio n. 8
0
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
Esempio n. 9
0
def disassemble_file(filename, outstream=None, native=False):
    """
    disassemble Python byte-code file (.pyc)

    If given a Python source file (".py") file, we'll
    try to find the corresponding compiled object.
    """
    if native:
        xdisassemble_file(filename, outstream)
        return

    filename = check_object_path(filename)
    version, timestamp, magic_int, co, is_pypy = 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
Esempio n. 10
0
 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)
Esempio n. 11
0
 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)
Esempio n. 12
0
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
Esempio n. 13
0
def do_tests(
    src_dir,
    patterns,
    target_dir,
    start_with=None,
    do_verify=False,
    max_files=800,
    do_compile=False,
    verbose=False,
):
    def visitor(files, dirname, names):
        files.extend([
            os.path.normpath(os.path.join(dirname, n)) for n in names
            for pat in patterns if fnmatch(n, pat)
        ])

    def file_matches(files, root, basenames, patterns):
        files.extend([
            os.path.normpath(os.path.join(root, n)) for n in basenames
            for pat in patterns if fnmatch(n, pat)
        ])

    files = []
    if do_compile:
        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

    cwd = os.getcwd()
    os.chdir(src_dir)
    if PYTHON3:
        for root, dirname, names in os.walk(os.curdir):
            files.extend([
                os.path.normpath(os.path.join(root, n)) for n in names
                for pat in patterns if fnmatch(n, pat)
            ])
            pass
        pass
    else:
        os.path.walk(os.curdir, visitor, files)
    files.sort()

    if start_with:
        try:
            start_with = files.index(start_with)
            files = files[start_with:]
            print(">>> starting with file", files[0])
        except ValueError:
            pass

    if len(files) > max_files:
        files = [file for file in files if not "site-packages" in file]
        files = [file for file in files if not "test" in file]
        if len(files) > max_files:
            files = files[:max_files]
            pass
    elif len(files) == 0:
        print("No files found\n")
        os.chdir(cwd)
        return

    output = open(os.devnull, "w")
    # output = sys.stdout
    start_time = time.time()
    print(time.ctime())
    for i, bc_file in enumerate(files):
        if verbose:
            print(os.path.join(src_dir, bc_file))
        if sys.version_info >= (3, 4, 0) and bc_file.endswith(".py"):
            check_bc_file = check_object_path(bc_file)
            if not osp.exists(check_bc_file):
                basename = osp.basename(bc_file)[0:-3]
                new_bc_file = tempfile.mkstemp(prefix=basename + "-",
                                               suffix=".pyc",
                                               text=False)[1]
                py_compile.compile(bc_file, cfile=new_bc_file, doraise=True)
                bc_file = new_bc_file

        bc_filename, co, version, ts, magic = main.disassemble_file(
            bc_file, output)
        if do_verify:
            file = co.co_filename
            verify_file(file, bc_filename)
        if i % 100 == 0 and i > 0:
            print("Processed %d files" % (i))
    print("Processed %d files, total" % (i + 1))
    print(time.ctime())
    elapsed_time = time.time() - start_time
    print("%g seconds" % elapsed_time)
    os.chdir(cwd)
Esempio n. 14
0
def do_tests(src_dir, patterns, target_dir, start_with=None,
                 do_verify=False, max_files=800, do_compile=False,
                 verbose=False):

    def visitor(files, dirname, names):
        files.extend(
            [os.path.normpath(os.path.join(dirname, n))
                 for n in names
                    for pat in patterns
                        if fnmatch(n, pat)])

    def file_matches(files, root, basenames, patterns):
        files.extend(
            [os.path.normpath(os.path.join(root, n))
                 for n in basenames
                    for pat in patterns
                        if fnmatch(n, pat)])

    files = []
    if do_compile:
        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

    cwd = os.getcwd()
    os.chdir(src_dir)
    if PYTHON3:
        for root, dirname, names in os.walk(os.curdir):
            files.extend(
                [os.path.normpath(os.path.join(root, n))
                     for n in names
                        for pat in patterns
                            if fnmatch(n, pat)])
            pass
        pass
    else:
        os.path.walk(os.curdir, visitor, files)
    files.sort()

    if start_with:
        try:
            start_with = files.index(start_with)
            files = files[start_with:]
            print('>>> starting with file', files[0])
        except ValueError:
            pass

    if len(files) > max_files:
        files = [file for file in files if not 'site-packages' in file]
        files = [file for file in files if not 'test' in file]
        if len(files) > max_files:
            files = files[:max_files]
            pass
    elif len(files) == 0:
        print("No files found\n")
        os.chdir(cwd)
        return

    output = open(os.devnull,"w")
    # output = sys.stdout
    start_time = time.time()
    print(time.ctime())
    for i, bc_file in enumerate(files):
        if verbose:
            print(os.path.join(src_dir, bc_file))
        if sys.version_info >= (3, 4, 0) and bc_file.endswith('.py'):
            check_bc_file = check_object_path(bc_file)
            if not osp.exists(check_bc_file):
                basename = osp.basename(bc_file)[0:-3]
                new_bc_file = tempfile.mkstemp(prefix=basename + '-',
                                           suffix='.pyc', text=False)[1]
                py_compile.compile(bc_file, cfile=new_bc_file, doraise=True)
                bc_file = new_bc_file

        bc_filename, co, version, ts, magic = main.disassemble_file(bc_file, output)
        if do_verify:
            file = co.co_filename
            verify_file(file, bc_filename)
        if i % 100 == 0 and i > 0:
            print("Processed %d files" % (i))
    print("Processed %d files, total" % (i+1))
    print(time.ctime())
    elapsed_time = time.time() - start_time
    print("%g seconds" % elapsed_time)
    os.chdir(cwd)