MCPcopy
hub / github.com/rocky/python-uncompyle6 / transform

Method transform

uncompyle6/semantics/transform.py:467–507  ·  view source on GitHub ↗
(self, parse_tree: GenericASTTraversal, code)

Source from the content-addressed store, hash-verified

465 return node
466
467 def transform(self, parse_tree: GenericASTTraversal, code) -> GenericASTTraversal:
468 self.maybe_show_tree(parse_tree)
469 self.ast = copy(parse_tree)
470 del parse_tree
471 self.ast = self.traverse(self.ast, is_lambda=False)
472 n = len(self.ast)
473
474 try:
475 # Disambiguate a string (expression) which appears as a "call_stmt" at
476 # the beginning of a function versus a docstring. Seems pretty academic,
477 # but this is Python.
478 call_stmt = self.ast[0][0]
479 if is_not_docstring(call_stmt):
480 call_stmt.kind = "string_at_beginning"
481 call_stmt.transformed_by = "transform"
482 pass
483 except Exception:
484 pass
485
486 try:
487 for i in range(n):
488 sstmt = self.ast[i]
489 if len(sstmt) == 1 and sstmt == "sstmt":
490 self.ast[i] = self.ast[i][0]
491
492 if is_docstring(self.ast[i], self.version, code.co_consts):
493 load_const = copy(self.ast[i].first_child())
494 store_name = copy(self.ast[i].last_child())
495 docstring_ast = SyntaxTree("docstring", [load_const, store_name])
496 docstring_ast.transformed_by = "transform"
497 del self.ast[i]
498 self.ast.insert(0, docstring_ast)
499 break
500
501 if self.ast[-1] == RETURN_NONE:
502 self.ast.pop() # remove last node
503 # todo: if empty, add 'pass'
504 except Exception:
505 pass
506
507 return self.ast
508
509 # Write template_engine
510 # def template_engine

Callers 2

build_astMethod · 0.80
build_astMethod · 0.80

Calls 7

maybe_show_treeMethod · 0.95
traverseMethod · 0.95
SyntaxTreeClass · 0.90
is_not_docstringFunction · 0.85
is_docstringFunction · 0.85
first_childMethod · 0.80
last_childMethod · 0.80

Tested by

no test coverage detected