uncompyle6 package

Submodules

uncompyle6.disas module

CPython magic- and version- independent disassembly routines

There are two reasons we can’t use Python’s built-in routines from dis. First, the bytecode we are extracting may be from a different version of Python (different magic number) than the version of Python that is doing the extraction.

Second, we need structured instruction information for the (de)-parsing step. Python 3.4 and up provides this, but we still do want to run on Python 2.7.

uncompyle6.disas.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.

uncompyle6.disas.disco(version, co, out=None, is_pypy=False)

diassembles and deparses a given code block ‘co’

uncompyle6.disas.disco_loop(disasm, queue, real_out)

uncompyle6.main module

uncompyle6.main.main(in_base, out_base, files, codes, outfile=None, showasm=False, showast=False, do_verify=False, showgrammar=False, raise_on_error=False)

in_base base directory for input files out_base base directory for output files (ignored when files list of filenames to be uncompyled (relative to src_base) outfile write output to this filename (overwrites out_base)

For redirecting output to - <filename> outfile=<filename> (out_base is ignored) - files below out_base out_base=... - stdout out_base=None, outfile=None

uncompyle6.main.status_msg(do_verify, tot_files, okay_files, failed_files, verify_failed_files)
uncompyle6.main.uncompyle(bytecode_version, co, out=None, showasm=False, showast=False, timestamp=None, showgrammar=False, code_objects={}, source_size=None, is_pypy=False, magic_int=None)

ingests and deparses a given code block ‘co’

uncompyle6.main.uncompyle_file(filename, outstream=None, showasm=False, showast=False, showgrammar=False)

decompile Python byte-code file (.pyc)

uncompyle6.parser module

Common uncompyle parser routines.

exception uncompyle6.parser.ParserError(token, offset)

Bases: exceptions.Exception

class uncompyle6.parser.PythonParser(AST, start, debug={'rules': False, 'errorstack': None, 'transition': False, 'reduce': False, 'context': True})

Bases: spark_parser.spark.GenericASTBuilder

add_unique_rule(rule, opname, count, customize)

Add rule to grammar, but only if it hasn’t been added previously opname and count are used in the customize() semantic the actions to add the semantic action rule. Often, count is not used.

add_unique_rules(rules, customize)

Add rules to grammar

cleanup()

Remove recursive references to allow garbage collector to collect this object.

error(instructions, index)
nonterminal(nt, args)
p_assign(args)

stmt ::= assign assign ::= expr DUP_TOP designList assign ::= expr designator

stmt ::= assign2 stmt ::= assign3 assign2 ::= expr expr ROT_TWO designator designator assign3 ::= expr expr expr ROT_THREE ROT_TWO designator designator designator

p_augmented_assign(args)

stmt ::= augassign1 stmt ::= augassign2 augassign1 ::= expr expr inplace_op designator augassign1 ::= expr expr inplace_op ROT_THREE STORE_SUBSCR augassign2 ::= expr DUP_TOP LOAD_ATTR expr

inplace_op ROT_TWO STORE_ATTR

inplace_op ::= INPLACE_ADD inplace_op ::= INPLACE_SUBTRACT inplace_op ::= INPLACE_MULTIPLY inplace_op ::= INPLACE_TRUE_DIVIDE inplace_op ::= INPLACE_FLOOR_DIVIDE inplace_op ::= INPLACE_MODULO inplace_op ::= INPLACE_POWER inplace_op ::= INPLACE_LSHIFT inplace_op ::= INPLACE_RSHIFT inplace_op ::= INPLACE_AND inplace_op ::= INPLACE_XOR inplace_op ::= INPLACE_OR

p_call_stmt(args)

# eval-mode compilation. Single-mode interactive compilation # adds another rule. call_stmt ::= expr POP_TOP

p_designator(args)

# Note. The below is right-recursive: designList ::= designator designator designList ::= designator DUP_TOP designList

## Can we replace with left-recursive, and redo with: ## ## designList ::= designLists designator designator ## designLists ::= designLists designator DUP_TOP ## designLists ::= ## Will need to redo semantic actiion

designator ::= STORE_FAST designator ::= STORE_NAME designator ::= STORE_GLOBAL designator ::= STORE_DEREF designator ::= expr STORE_ATTR designator ::= store_subscr store_subscr ::= expr expr STORE_SUBSCR designator ::= unpack designator ::= unpack_list

p_dictcomp(args)

expr ::= dictcomp stmt ::= dictcomp_func dictcomp_func ::= BUILD_MAP_0 LOAD_FAST FOR_ITER designator

comp_iter JUMP_BACK RETURN_VALUE RETURN_LAST
p_expr(args)

expr ::= _mklambda expr ::= LOAD_FAST expr ::= LOAD_NAME expr ::= LOAD_CONST expr ::= LOAD_GLOBAL expr ::= LOAD_DEREF expr ::= load_attr expr ::= binary_expr expr ::= build_list expr ::= cmp expr ::= mapexpr expr ::= and expr ::= and2 expr ::= or expr ::= unary_expr expr ::= call_function expr ::= unary_not expr ::= binary_subscr expr ::= binary_subscr2 expr ::= load_attr expr ::= get_iter expr ::= buildslice2 expr ::= buildslice3 expr ::= yield

# Possibly Python < 2.3 # expr ::= SET_LINENO

binary_expr ::= expr expr binary_op binary_op ::= BINARY_ADD binary_op ::= BINARY_MULTIPLY binary_op ::= BINARY_AND binary_op ::= BINARY_OR binary_op ::= BINARY_XOR binary_op ::= BINARY_SUBTRACT binary_op ::= BINARY_TRUE_DIVIDE binary_op ::= BINARY_FLOOR_DIVIDE binary_op ::= BINARY_MODULO binary_op ::= BINARY_LSHIFT binary_op ::= BINARY_RSHIFT binary_op ::= BINARY_POWER

unary_expr ::= expr unary_op unary_op ::= UNARY_POSITIVE unary_op ::= UNARY_NEGATIVE unary_op ::= UNARY_INVERT

unary_not ::= expr UNARY_NOT

binary_subscr ::= expr expr BINARY_SUBSCR

load_attr ::= expr LOAD_ATTR get_iter ::= expr GET_ITER buildslice3 ::= expr expr expr BUILD_SLICE_3 buildslice2 ::= expr expr BUILD_SLICE_2

yield ::= expr YIELD_VALUE

_mklambda ::= load_closure mklambda _mklambda ::= mklambda

and2 ::= _jump jmp_false COME_FROM expr COME_FROM

expr ::= conditional conditional ::= expr jmp_false expr JUMP_FORWARD expr COME_FROM conditional ::= expr jmp_false expr JUMP_ABSOLUTE expr expr ::= conditionalnot conditionalnot ::= expr jmp_true expr _jump expr COME_FROM

ret_expr ::= expr ret_expr ::= ret_and ret_expr ::= ret_or

ret_expr_or_cond ::= ret_expr ret_expr_or_cond ::= ret_cond ret_expr_or_cond ::= ret_cond_not

stmt ::= return_lambda stmt ::= conditional_lambda

return_lambda ::= ret_expr RETURN_VALUE LAMBDA_MARKER conditional_lambda ::= expr jmp_false return_if_stmt return_stmt LAMBDA_MARKER

cmp ::= cmp_list cmp ::= compare compare ::= expr expr COMPARE_OP cmp_list ::= expr cmp_list1 ROT_TWO POP_TOP

_come_from
cmp_list1 ::= expr DUP_TOP ROT_THREE
COMPARE_OP jmp_false cmp_list1 _come_from
cmp_list1 ::= expr DUP_TOP ROT_THREE
COMPARE_OP jmp_false cmp_list2 _come_from

cmp_list2 ::= expr COMPARE_OP JUMP_FORWARD cmp_list2 ::= expr COMPARE_OP RETURN_VALUE mapexpr ::= BUILD_MAP kvlist

kvlist ::= kvlist kv kvlist ::= kvlist kv2 kvlist ::= kvlist kv3 kvlist ::=

kv ::= DUP_TOP expr ROT_TWO expr STORE_SUBSCR kv2 ::= DUP_TOP expr expr ROT_THREE STORE_SUBSCR kv3 ::= expr expr STORE_MAP

exprlist ::= exprlist expr exprlist ::= expr

# Positional arguments in make_function pos_arg ::= expr

expr32 ::= expr expr expr expr expr expr expr expr expr expr expr expr expr expr expr expr expr expr expr expr expr expr expr expr expr expr expr expr expr expr expr expr expr1024 ::= expr32 expr32 expr32 expr32 expr32 expr32 expr32 expr32 expr32 expr32 expr32 expr32 expr32 expr32 expr32 expr32 expr32 expr32 expr32 expr32 expr32 expr32 expr32 expr32 expr32 expr32 expr32 expr32 expr32 expr32 expr32 expr32

p_forstmt(args)

_for ::= GET_ITER FOR_ITER

for_block ::= l_stmts_opt _come_from JUMP_BACK for_block ::= return_stmts _come_from

forstmt ::= SETUP_LOOP expr _for designator
for_block POP_BLOCK _come_from
forelsestmt ::= SETUP_LOOP expr _for designator
for_block POP_BLOCK else_suite _come_from
forelselaststmt ::= SETUP_LOOP expr _for designator
for_block POP_BLOCK else_suitec _come_from
forelselaststmtl ::= SETUP_LOOP expr _for designator
for_block POP_BLOCK else_suitel _come_from
p_funcdef(args)

stmt ::= funcdef funcdef ::= mkfunc designator stmt ::= funcdefdeco funcdefdeco ::= mkfuncdeco designator mkfuncdeco ::= expr mkfuncdeco CALL_FUNCTION_1 mkfuncdeco ::= expr mkfuncdeco0 CALL_FUNCTION_1 mkfuncdeco0 ::= mkfunc load_closure ::= load_closure LOAD_CLOSURE load_closure ::= LOAD_CLOSURE

p_genexpr(args)

expr ::= genexpr stmt ::= genexpr_func

genexpr_func ::= LOAD_FAST FOR_ITER designator comp_iter JUMP_BACK

p_import20(args)

stmt ::= importstmt stmt ::= importfrom stmt ::= importstar stmt ::= importmultiple

importlist2 ::= importlist2 import_as importlist2 ::= import_as import_as ::= IMPORT_NAME designator import_as ::= IMPORT_NAME load_attrs designator import_as ::= IMPORT_FROM designator

importstmt ::= LOAD_CONST LOAD_CONST import_as importstar ::= LOAD_CONST LOAD_CONST IMPORT_NAME IMPORT_STAR importfrom ::= LOAD_CONST LOAD_CONST IMPORT_NAME importlist2 POP_TOP importmultiple ::= LOAD_CONST LOAD_CONST import_as imports_cont

imports_cont ::= imports_cont import_cont imports_cont ::= import_cont import_cont ::= LOAD_CONST LOAD_CONST import_as_cont import_as_cont ::= IMPORT_FROM designator

load_attrs ::= LOAD_ATTR load_attrs ::= load_attrs LOAD_ATTR

p_jump(args)

_jump ::= JUMP_ABSOLUTE _jump ::= JUMP_FORWARD _jump ::= JUMP_BACK

# Zero or more COME_FROMs # loops can have this _come_from ::= _come_from COME_FROM _come_from ::=

# Zero or one COME_FROM # And/or expressions have this come_from_opt ::= COME_FROM come_from_opt ::=

p_list_comprehension(args)

expr ::= list_compr list_compr ::= BUILD_LIST_0 list_iter

list_iter ::= list_for list_iter ::= list_if list_iter ::= list_if_not list_iter ::= lc_body

list_if ::= expr jmp_false list_iter list_if_not ::= expr jmp_true list_iter

lc_body ::= expr LIST_APPEND

p_setcomp(args)

expr ::= setcomp

setcomp ::= LOAD_SETCOMP MAKE_FUNCTION_0 expr GET_ITER CALL_FUNCTION_1

comp_iter ::= comp_if comp_iter ::= comp_ifnot comp_iter ::= comp_for comp_iter ::= comp_body comp_body ::= gen_comp_body gen_comp_body ::= expr YIELD_VALUE POP_TOP

comp_if ::= expr jmp_false comp_iter comp_ifnot ::= expr jmp_true comp_iter

comp_for ::= expr _for designator comp_iter JUMP_BACK

p_start(args)

# The start or goal symbol stmts ::= stmts sstmt stmts ::= sstmt

p_stmt(args)

passstmt ::=

_stmts ::= _stmts stmt _stmts ::= stmt

# statements with continue c_stmts ::= _stmts c_stmts ::= _stmts lastc_stmt c_stmts ::= lastc_stmt c_stmts ::= continue_stmts

lastc_stmt ::= iflaststmt lastc_stmt ::= whileelselaststmt lastc_stmt ::= forelselaststmt lastc_stmt ::= ifelsestmtr lastc_stmt ::= ifelsestmtc lastc_stmt ::= tryelsestmtc

c_stmts_opt ::= c_stmts c_stmts_opt ::= passstmt

l_stmts ::= _stmts l_stmts ::= return_stmts l_stmts ::= continue_stmts l_stmts ::= _stmts lastl_stmt l_stmts ::= lastl_stmt

lastl_stmt ::= iflaststmtl lastl_stmt ::= ifelsestmtl lastl_stmt ::= forelselaststmtl lastl_stmt ::= tryelsestmtl

l_stmts_opt ::= l_stmts l_stmts_opt ::= passstmt

suite_stmts ::= _stmts suite_stmts ::= return_stmts suite_stmts ::= continue_stmts

suite_stmts_opt ::= suite_stmts suite_stmts_opt ::= passstmt

else_suite ::= suite_stmts else_suitel ::= l_stmts else_suitec ::= c_stmts else_suitec ::= return_stmts

stmt ::= assert stmt ::= assert2

stmt ::= classdef stmt ::= call_stmt

stmt ::= ifstmt stmt ::= ifelsestmt

stmt ::= whilestmt stmt ::= while1stmt stmt ::= whileelsestmt stmt ::= while1elsestmt stmt ::= forstmt stmt ::= forelsestmt stmt ::= trystmt stmt ::= tryelsestmt stmt ::= tryfinallystmt stmt ::= withstmt stmt ::= withasstmt

stmt ::= del_stmt del_stmt ::= DELETE_FAST del_stmt ::= DELETE_NAME del_stmt ::= DELETE_GLOBAL

stmt ::= return_stmt return_stmt ::= ret_expr RETURN_VALUE return_stmts ::= return_stmt return_stmts ::= _stmts return_stmt

p_whilestmt(args)
whilestmt ::= SETUP_LOOP testexpr l_stmts_opt JUMP_BACK
POP_BLOCK _come_from
whilestmt ::= SETUP_LOOP testexpr return_stmts
POP_BLOCK COME_FROM
whileelsestmt ::= SETUP_LOOP testexpr
l_stmts_opt JUMP_BACK POP_BLOCK else_suite COME_FROM
whileelselaststmt ::= SETUP_LOOP testexpr
l_stmts_opt JUMP_BACK POP_BLOCK else_suitec COME_FROM
resolve(list)
typestring(token)
class uncompyle6.parser.PythonParserSingle(AST, start, debug={'rules': False, 'errorstack': None, 'transition': False, 'reduce': False, 'context': True})

Bases: uncompyle6.parser.PythonParser

p_call_stmt_single(args)

# single-mode compilation. Eval-mode interactive compilation # drops the last rule.

call_stmt ::= expr PRINT_EXPR

uncompyle6.parser.get_python_parser(version, debug_parser={}, compile_mode='exec', is_pypy=False)

Returns parser object for Python version 2 or 3, 3.2, 3.5on, etc., depending on the parameters passed. compile_mode is either ‘exec’, ‘eval’, or ‘single’. See https://docs.python.org/3.6/library/functions.html#compile for an explanation of the different modes.

uncompyle6.parser.nop_func(self, args)
uncompyle6.parser.parse(p, tokens, customize)
uncompyle6.parser.python_parser(version, co, out=<open file '<stdout>', mode 'w'>, showasm=False, parser_debug={'rules': False, 'errorstack': None, 'transition': False, 'reduce': False, 'context': True}, is_pypy=False)

Parse a code object to an abstract syntax tree representation.

Parameters:
  • version – The python version this code is from as a float, for example 2.6, 2.7, 3.2, 3.3, 3.4, 3.5 etc.
  • co – The code object to parse.
  • out – File like object to write the output to.
  • showasm – Flag which determines whether the disassembled and ingested code is written to sys.stdout or not.
  • parser_debug – dict containing debug flags for the spark parser.
Returns:

Abstract syntax tree representation of the code object.

uncompyle6.scanner module

scanner/ingestion module. From here we call various version-specific scanners, e.g. for Python 2.7 or 3.4.

class uncompyle6.scanner.Code(co, scanner, classname=None)

Bases: object

Class for representing code-objects.

This is similar to the original code object, but additionally the diassembled code is stored in the attribute ‘_tokens’.

class uncompyle6.scanner.Scanner(version, show_asm=None, is_pypy=False)

Bases: object

all_instr(start, end, instr, target=None, include_beyond_target=False)

Find all <instr> in the block from start to end. <instr> is any python bytecode instruction or a list of opcodes If <instr> is an opcode with a target (like a jump), a target destination can be specified which must match precisely.

Return a list with indexes to them or [] if none found.

first_instr(start, end, instr, target=None, exact=True)

Find the first <instr> in the block from start to end. <instr> is any python bytecode instruction or a list of opcodes If <instr> is an opcode with a target (like a jump), a target destination can be specified which must match precisely if exact is True, or if exact is False, the instruction which has a target closest to <target> will be returned.

Return index to it or None if not found.

get_argument(pos)
get_target(pos, op=None)
is_jump_forward(offset)

Return True if the code at offset is some sort of jump forward. That is, it is ether “JUMP_FORWARD” or an absolute jump that goes forward.

last_instr(start, end, instr, target=None, exact=True)

Find the last <instr> in the block from start to end. <instr> is any python bytecode instruction or a list of opcodes If <instr> is an opcode with a target (like a jump), a target destination can be specified which must match precisely if exact is True, or if exact is False, the instruction which has a target closest to <target> will be returned.

Return index to it or None if not found.

op_hasArgument(op)
op_range(start, end)

Iterate through positions of opcodes, skipping arguments.

print_bytecode()
remove_mid_line_ifs(ifs)

Go through passed offsets, filtering ifs located somewhere mid-line.

resetTokenClass()
restrict_to_parent(target, parent)

Restrict target to parent structure boundaries.

setTokenClass(tokenClass)
uncompyle6.scanner.get_scanner(version, is_pypy=False, show_asm=None)
uncompyle6.scanner.op_has_argument(op, opc)
uncompyle6.scanner.parse_fn_counts(argc)

uncompyle6.show module

uncompyle6.show.maybe_show_asm(showasm, tokens)

Show the asm based on the showasm flag (or file object), writing to the appropriate stream depending on the type of the flag.

Parameters:
  • showasm – Flag which determines whether the ingested code is written to sys.stdout or not. (It is also to pass a file like object, into which the asm will be written).
  • tokens – The asm tokens to show.
uncompyle6.show.maybe_show_ast(showast, ast)

Show the ast based on the showast flag (or file object), writing to the appropriate stream depending on the type of the flag.

Parameters:
  • showasm – Flag which determines whether the abstract syntax tree is written to sys.stdout or not. (It is also to pass a file like object, into which the ast will be written).
  • ast – The ast to show.
uncompyle6.show.maybe_show_ast_param_default(showast, name, default)

Show a function parameter with default for an ast based on the showast flag (or file object), writing to the appropriate stream depending on the type of the flag.

Parameters:
  • showasm – Flag which determines whether the function parameter with default is written to sys.stdout or not. (It is also to pass a file like object, into which the ast will be written).
  • name – The function parameter name.
  • default – The function parameter default.

uncompyle6.verify module

byte-code verification

exception uncompyle6.verify.CmpErrorCode(name, index, token1, token2, tokens1, tokens2)

Bases: uncompyle6.verify.VerifyCmpError

Exception to be raised when code differs.

exception uncompyle6.verify.CmpErrorCodeLen(name, tokens1, tokens2)

Bases: uncompyle6.verify.VerifyCmpError

Exception to be raised when code length differs.

exception uncompyle6.verify.CmpErrorConsts(name, index)

Bases: uncompyle6.verify.VerifyCmpError

Exception to be raised when consts differ.

exception uncompyle6.verify.CmpErrorConstsLen(name, consts1, consts2)

Bases: uncompyle6.verify.VerifyCmpError

Exception to be raised when length of co_consts differs.

exception uncompyle6.verify.CmpErrorConstsType(name, index)

Bases: uncompyle6.verify.VerifyCmpError

Exception to be raised when consts differ.

exception uncompyle6.verify.CmpErrorMember(name, member, data1, data2)

Bases: uncompyle6.verify.VerifyCmpError

Exception to be raised when other members differ.

class uncompyle6.verify.Token(type_, attr=None, pattr=None, offset=-1, linestart=None, op=None, has_arg=None, opc=None)

Bases: uncompyle6.scanners.tok.Token

Token class with changed semantics for ‘cmp()’.

exception uncompyle6.verify.VerifyCmpError

Bases: exceptions.Exception

uncompyle6.verify.cmp_code_objects(version, is_pypy, code_obj1, code_obj2, name='', ignore_code=False)

Compare two code-objects.

This is the main part of this module.

uncompyle6.verify.code_equal(a, b)
uncompyle6.verify.compare_code_with_srcfile(pyc_filename, src_filename, weak_verify=False)

Compare a .pyc with a source code file.

uncompyle6.verify.compare_files(pyc_filename1, pyc_filename2, weak_verify=False)

Compare two .pyc files.

uncompyle6.version module

Module contents

Copyright (c) 1999 John Aycock Copyright (c) 2000 by hartmut Goebel <h.goebel@crazy-compilers.com> Copyright (c) 2015 by Rocky Bernstein

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

NB. This is not a masterpiece of software, but became more like a hack. Probably a complete rewrite would be sensefull. hG/2000-12-27