def ast_tool(args): print("Reconstructing AST %r" % (args.input.name,), file=sys.stderr) mod_ast = deserialize(json.load(args.input)) code = compile(mod_ast, args.input.name, mode='exec', dont_inherit=True) if args.output_type == 'opcode': print_code(code) return elif args.output_type == 'ast': json.dump(serialize(mod_ast), args.output, indent=2) return elif args.output_type == 'python': python_source(mod_ast, file=args.output) return elif args.output_type == 'pyc': if py3 and args.output is sys.stdout: args.output = sys.stdout.buffer try: timestamp = int(os.fstat(args.input.fileno()).st_mtime) except AttributeError: timestamp = int(os.stat(args.input.name).st_mtime) if py3 and args.output is sys.stdout: args.output = sys.stdout.buffer dump_pyc(code, args.output, timestamp=timestamp) else: raise Exception("unknow output type %r" % args.output_type) return
def depyc(args): binary = args.input.read() modtime, code = extract(binary) print("Decompiling module %r compiled on %s" % (args.input.name, modtime,), file=sys.stderr) if args.output_type == 'pyc': if py3 and args.output is sys.stdout: args.output = sys.stdout.buffer args.output.write(binary) return if args.output_type == 'opcode': print_code(code) return mod_ast = make_module(code) if args.output_type == 'ast': json.dump(serialize(mod_ast), args.output, indent=2) return if args.output_type == 'python': python_source(mod_ast, file=args.output) return raise Exception("unknow output type %r" % args.output_type)
def depyc(args): binary = args.input.read() modtime, code = extract(binary) print("Decompiling module %r compiled on %s" % (args.input.name, modtime,), file=sys.stderr) if args.output_type == 'pyc': if py3 and args.output is sys.stdout: args.output = sys.stdout.buffer args.output.write(binary) return if args.output_type == 'opcode': print_code(code) return mod_ast = make_module(code) if args.output_type == 'ast': print_ast(mod_ast, file=args.output) return if args.output_type == 'python': python_source(mod_ast, file=args.output) return raise Exception("unknow output type %r" % args.output_type)
def get_py_ufunc_ast(self, lhs, node): if lhs is not None: lhs.ctx = ast.Load() builder = ufunc_builder.UFuncConverter() tree = builder.visit(node) ufunc_ast = builder.build_ufunc_ast(tree) if print_ufunc: from meta import asttools module = ast.Module(body=[ufunc_ast]) print asttools.python_source(module) # Vectorize Python function if lhs is None: restype = node.type else: restype = lhs.type.dtype argtypes = [op.type.dtype if op.type.is_array else op.type for op in builder.operands] signature = restype(*argtypes) return ufunc_ast, signature, builder
def get_py_ufunc_ast(self, lhs, node): if lhs is not None: lhs.ctx = ast.Load() builder = ufunc_builder.UFuncConverter() tree = builder.visit(node) ufunc_ast = builder.build_ufunc_ast(tree) if print_ufunc: from meta import asttools module = ast.Module(body=[ufunc_ast]) print((asttools.python_source(module))) # Vectorize Python function if lhs is None: restype = node.type else: restype = lhs.type.dtype argtypes = [op.type.dtype if op.type.is_array else op.type for op in builder.operands] signature = restype(*argtypes) return ufunc_ast, signature, builder
def decompile_pyc(bin_pyc, output=sys.stdout): ''' decompile apython pyc or pyo binary file. :param bin_pyc: input file objects :param output: output file objects ''' from meta.asttools import python_source bin = bin_pyc.read() code = marshal.loads(bin[8:]) mod_ast = make_module(code) python_source(mod_ast, file=output)