uncompyle6.semantics package

Submodules

uncompyle6.semantics.aligner module

class uncompyle6.semantics.aligner.AligningWalker(version, scanner, out, showast=False, debug_parser={'rules': False, 'errorstack': None, 'transition': False, 'reduce': False, 'context': True}, compile_mode='exec', is_pypy=False)

Bases: uncompyle6.semantics.pysource.SourceWalker, object

default(node)
println(*data)
write(*data)
uncompyle6.semantics.aligner.align_deparse_code(version, co, out=<open file '<stderr>', mode 'w'>, showasm=False, showast=False, showgrammar=False, code_objects={}, compile_mode='exec', is_pypy=False)

ingests and deparses a given code block ‘co’

uncompyle6.semantics.fragments module

Creates Python source code from an uncompyle6 abstract syntax tree, and indexes fragments which can be accessed by instruction offset address.

See the comments in pysource for information on the abstract sytax tree and how semantic actions are written.

We add some format specifiers here not used in pysource

1. %x

%x takes an argument (src, (dest...)) and copies all of the range attributes

from src to dest.

For example in:
‘importstmt’: ( ‘%|import %c%x

‘, 2, (2,(0,1)), ),

node 2 range information, it in %c, is copied to nodes 0 and 1.

2. %r

%r associates recursively location information for the string that follows
For example in:
‘break_stmt’: ( ‘%|%rbreak

‘, ),

The node will be associated with the text break, excluding the trailing newline.

Note we associate the accumulated text with the node normally, but we just don’t do it recursively which is where offsets are probably located.

2. %b

%b associates the text from the previous start node up to what we have now
For example in:
‘importmultiple’: ( ‘%|import%b %c%c

‘, 0, 2, 3 ),

The node position 0 will be associated with “import”.

class uncompyle6.semantics.fragments.ExtractInfo(lineNo, lineStartOffset, markerLine, selectedLine, selectedText)

Bases: tuple

lineNo

Alias for field number 0

lineStartOffset

Alias for field number 1

markerLine

Alias for field number 2

selectedLine

Alias for field number 3

selectedText

Alias for field number 4

class uncompyle6.semantics.fragments.FragmentsWalker(version, scanner, showast=False, debug_parser={'rules': False, 'errorstack': None, 'transition': False, 'reduce': False, 'context': True}, compile_mode='exec', is_pypy=False)

Bases: uncompyle6.semantics.pysource.SourceWalker, object

MAP_DIRECT_FRAGMENT = ()
build_ast(tokens, customize, isLambda=False, noneInNames=False)
comprehension_walk(node, iter_index, code_index=-5)
comprehension_walk3(node, iter_index, code_index=-5)

List comprehensions the way they are done in Python3. They’re more other comprehensions, e.g. set comprehensions See if we can combine code.

engine(entry, startnode)

The format template interpetation engine. See the comment at the beginning of this module for the how we interpret format specifications such as %c, %C, and so on.

extract_line_info(name, offset)
extract_node_info(nodeInfo)
extract_parent_info(node)
f
fixup_offsets(new_start, node)

Adjust all offsets under node

fixup_parents(node, parent)

Make sure each node has a parent

gen_source(ast, name, customize, isLambda=False, returnNone=False)

convert AST to Python source code

indent
isLambda
listcomprehension_walk2(node)

List comprehensions the way they are done in Python3. They’re more other comprehensions, e.g. set comprehensions See if we can combine code.

make_function(node, isLambda, nested=1, code=None)

Dump function defintion, doc string, and function body.

n_LOAD_CONST(node)
n__ifstmts_jump_exit(node)
n_binary_expr(node)
n_build_list(node)

prettyprint a list or tuple

n_buildslice2(node)
n_buildslice3(node)
n_classdef(node)
n_classdefdeco2(node)
n_elifelsestmtr(node)
n_exec_stmt(node)

exec_stmt ::= expr exprlist DUP_TOP EXEC_STMT exec_stmt ::= expr exprlist EXEC_STMT

n_expr(node)
n_genexpr(node)
n_ifelsestmtr(node)
n_import_as(node)
n_list_compr(node)

List comprehensions the way they are done in Python 2.

n_listcomp(node)
n_mapexpr(node)

prettyprint a mapexpr ‘mapexpr’ is something like k = {‘a’: 1, ‘b’: 42 }”

n_mkfunc(node)
n_ret_expr(node)
n_return_if_stmt(node)
n_return_stmt(node)
n_setcomp(node)
n_setcomp_func(node)
n_yield(node)
n_yield_from(node)
node_append(before_str, node_text, node)
preorder(node=None)
print_super_classes(node)
print_super_classes3(node)
set_pos_info(node, start, finish, name=None)
set_pos_info_recurse(node, start, finish, parent=None)

Set positions under node

setcomprehension_walk3(node, collection_index)

List comprehensions the way they are done in Python3. They’re more other comprehensions, e.g. set comprehensions See if we can combine code.

stacked_params = ('f', 'indent', 'isLambda', '_globals')
traverse(node, indent=None, isLambda=False)

Buulds up fragment which can be used inside a larger block of code

class uncompyle6.semantics.fragments.NodeInfo(node, start, finish)

Bases: tuple

finish

Alias for field number 2

node

Alias for field number 0

start

Alias for field number 1

uncompyle6.semantics.fragments.deparse_code(version, co, out=<StringIO.StringIO instance>, showasm=False, showast=False, showgrammar=False, is_pypy=False)

Convert the code object co into a python source fragment.

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 ingestd code is written to sys.stdout or not. (It is also to pass a file like object, into which the asm will be written).
  • showast – Flag which determines whether the constructed 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).
  • showgrammar – Flag which determines whether the grammar is written to sys.stdout or not. (It is also to pass a file like object, into which the grammer will be written).
Returns:

The deparsed source fragment.

uncompyle6.semantics.pysource module

Creates Python source code from an uncompyle6 abstract syntax tree.

The terminal symbols are CPython bytecode instructions. (See the python documentation under module “dis” for a list of instructions and what they mean).

Upper levels of the grammar is a more-or-less conventional grammar for Python.

Semantic action rules for nonterminal symbols can be specified here by creating a method prefaced with “n_” for that nonterminal. For example, “n_exec_stmt” handles the semantic actions for the “exec_smnt” nonterminal symbol. Similarly if a method with the name of the nonterminal is suffixed with “_exit” it will be called after all of its children are called.

Another other way to specify a semantic rule for a nonterminal is via rule given in one of the tables MAP_R0, MAP_R, or MAP_DIRECT.

These uses a printf-like syntax to direct substitution from attributes of the nonterminal and its children..

The rest of the below describes how table-driven semantic actions work and gives a list of the format specifiers. The default() and engine() methods implement most of the below.

Step 1 determines a table (T) and a path to a table key (K) from the node type (N) (other nodes are shown as O):

N N N&K
/ | ... / | ... / | ... O O O O O K O O O

K

MAP_R0 (TABLE_R0) MAP_R (TABLE_R) MAP_DIRECT (TABLE_DIRECT)

The default is a direct mapping. The key K is then extracted from the subtree and used to find a table entry T[K], if any. The result is a format string and arguments (a la printf()) for the formatting engine. Escapes in the format string are:

%c evaluate children N[A] recursively* %C evaluate children N[A[0]]..N[A[1]-1] recursively, separate by A[2]* %P same as %C but sets operator precedence %D same as %C but is for left-recursive lists like kwargs which

goes to epsilon at the beginning. Using %C an extra separator with an epsilon appears at the beginning
%, print ‘,’ if last %C only printed one item. This is mostly for tuples
on the LHS of an assignment statement since BUILD_TUPLE_n pretty-prints other tuples.

%| tab to current indentation level %+ increase current indentation level %- decrease current indentation level %{...} evaluate ... in context of N %% literal ‘%’ %p evaluate N setting precedence

  • indicates an argument (A) required.

The ‘%’ may optionally be followed by a number (C) in square brackets, which makes the engine walk down to N[C] before evaluating the escape code.

uncompyle6.semantics.pysource.ASSIGN_DOC_STRING(doc_string)
uncompyle6.semantics.pysource.ASSIGN_TUPLE_PARAM(param_name)
exception uncompyle6.semantics.pysource.ParserError(error, tokens)

Bases: uncompyle6.parser.ParserError

class uncompyle6.semantics.pysource.SourceWalker(version, out, scanner, showast=False, debug_parser={'rules': False, 'errorstack': None, 'transition': False, 'reduce': False, 'context': True}, compile_mode='exec', is_pypy=False, linestarts={})

Bases: spark_parser.ast.GenericASTTraversal, object

FSTRING_CONVERSION_MAP = {1: '!s', 2: '!r', 3: '!a'}
build_ast(tokens, customize, isLambda=False, noneInNames=False, isTopLevel=False)
build_class(code)

Dump class definition, doc string and class body.

comprehension_walk(node, iter_index, code_index=-5)
comprehension_walk3(node, iter_index, code_index=-5)

List comprehensions the way they are done in Python3. They’re more other comprehensions, e.g. set comprehensions See if we can combine code.

customize(customize)

Special handling for opcodes, such as those that take a variable number of arguments – we add a new entry for each in TABLE_R.

static customize_for_version(is_pypy, version)
default(node)
engine(entry, startnode)

The format template interpetation engine. See the comment at the beginning of this module for the how we interpret format specifications such as %c, %C, and so on.

f
gen_source(ast, name, customize, isLambda=False, returnNone=False)

convert AST to Python source code

get_tuple_parameter(ast, name)

If the name of the formal parameter starts with dot, it’s a tuple parameter, like this: # def MyFunc(xx, (a,b,c), yy): # print a, b*2, c*42 In byte-code, the whole tuple is assigned to parameter ‘.1’ and then the tuple gets unpacked to ‘a’, ‘b’ and ‘c’.

Since identifiers starting with a dot are illegal in Python, we can search for the byte-code equivalent to ‘(a,b,c) = .1’

indent
indentLess(indent=' ')
indentMore(indent=' ')
isLambda
is_return_none(node)
listcomprehension_walk2(node)

List comprehensions the way they are done in Python 2. They’re more other comprehensions, e.g. set comprehensions See if we can combine code.

make_function(node, isLambda, nested=1, codeNode=None)

Dump function defintion, doc string, and function body.

n_LOAD_CONST(node)
n_assign(node)
n_assign2(node)
n_assign3(node)
n_binary_expr(node)
n_binary_subscr(node)
n_build_list(node)

prettyprint a list or tuple

n_buildslice2(node)
n_buildslice3(node)
n_classdef(node)
n_classdefdeco2(node)
n_delete_subscr(node)
n_dictcomp(node)
n_elifelsestmtr(node)
n_except_cond2(node)
n_exec_stmt(node)

exec_stmt ::= expr exprlist DUP_TOP EXEC_STMT exec_stmt ::= expr exprlist EXEC_STMT

n_expr(node)
n_fstring_expr(node)
n_genexpr(node)
n_ifelsestmt(node, preprocess=False)
n_ifelsestmtc(node, preprocess=False)
n_ifelsestmtl(node, preprocess=False)
n_ifelsestmtr(node)
n_import_as(node)
n_import_as_cont(node)
n_importfrom(node)
n_importstar(node)
n_list_compr(node)

List comprehensions the way they are done in Python 2.

n_list_compr_pypy27(node)

List comprehensions the way they are done in PYPY Python 2.7.

n_listcomp(node)
n_mapexpr(node)

prettyprint a mapexpr ‘mapexpr’ is something like k = {‘a’: 1, ‘b’: 42}” We will source-code use line breaks to guide us when to break.

n_mkfunc(node)
n_mklambda(node)
n_ret_expr(node)
n_ret_expr_or_cond(node)
n_return_if_stmt(node)
n_return_stmt(node)
n_setcomp(node)
n_store_subscr(node)
n_str(node)
n_tryfinallystmt(node)
n_unpack(node)
n_unpack_w_parens(node)
n_yield(node)
n_yield_from(node)
pp_tuple(tup)

Pretty print a tuple

preorder(node=None)
print_docstring(indent, docstring)
print_super_classes(node)
print_super_classes3(node)
println(*data)
set_pos_info(node)
setcomprehension_walk3(node, collection_index)

List comprehensions the way they are done in Python3. They’re more other comprehensions, e.g. set comprehensions See if we can combine code.

stacked_params = ('f', 'indent', 'isLambda', '_globals')
traverse(node, indent=None, isLambda=False)
write(*data)
exception uncompyle6.semantics.pysource.SourceWalkerError(errmsg)

Bases: exceptions.Exception

uncompyle6.semantics.pysource.deparse_code(version, co, out=<open file '<stdout>', mode 'w'>, showasm=False, showast=False, showgrammar=False, code_objects={}, compile_mode='exec', is_pypy=False)

ingests and deparses a given code block ‘co’

uncompyle6.semantics.pysource.find_all_globals(node, globs)

Find globals in this statement.

uncompyle6.semantics.pysource.find_globals(node, globs)

Find globals in this statement.

uncompyle6.semantics.pysource.find_none(node)
uncompyle6.semantics.pysource.is_docstring(node)

Module contents