Exemplo n.º 1
0
def merge_annotations(code, annotations):
    """Merges type comments into their associated opcodes.

  Modifies code in place.

  Args:
    code: An OrderedCode object.
    annotations: A map of lines to annotations.

  Returns:
    The code with annotations added to the relevant opcodes.
  """
    visitor = CollectAnnotationTargetsVisitor()
    code = pyc.visit(code, visitor)

    # Apply type comments to the STORE_* opcodes
    for line, op in visitor.store_ops.items():
        if line in annotations:
            op.annotation = annotations[line]

    # Apply type comments to the MAKE_FUNCTION opcodes
    for start, (end, op) in sorted(visitor.make_function_ops.items(),
                                   reverse=True):
        for i in range(start, end):
            # Take the first comment we find as the function typecomment.
            if i in annotations:
                # Record the line number of the comment for error messages.
                op.annotation = (annotations[i], i)
                break
    return code
Exemplo n.º 2
0
def process_code(code):
  return pyc.visit(code, OrderCodeVisitor())
Exemplo n.º 3
0
def process_code(code, python_version):
    # [binary opcodes] -> [pyc.Opcode]
    ops = pyc.visit(code, DisCodeVisitor())
    # pyc.load_marshal.CodeType -> blocks.OrderedCode
    ordered = pyc.visit(ops, OrderCodeVisitor(python_version))
    return ordered