Example #1
0
def post_process_ast(ast, src, name=None):
    """Post-process the parsed AST."""
    ast = definitions.finalize_ast(ast)
    ast = ast.Visit(pep484.ConvertTypingToNative(name))

    if name:
        ast = ast.Replace(name=name)
        ast = ast.Visit(visitors.AddNamePrefix())
    else:
        # If there's no unique name, hash the sourcecode.
        ast = ast.Replace(name=hashlib.md5(src.encode("utf-8")).hexdigest())
    ast = ast.Visit(visitors.StripExternalNamePrefix())

    # Now that we have resolved external names, process any class decorators that
    # do code generation.
    try:
        ast = ast.Visit(decorate.DecorateClassVisitor())
    except TypeError as e:
        # Convert errors into ParseError. Unfortunately we no longer have location
        # information if an error is raised during transformation of a class node.
        raise ParseError.from_exc(e)

    # Typeshed files that explicitly import and refer to "__builtin__" need to
    # have that rewritten to builtins
    ast = ast.Visit(visitors.RenameBuiltinsPrefix())

    return ast
Example #2
0
def post_process_ast(ast, src, name=None):
  """Post-process the parsed AST."""
  ast = definitions.finalize_ast(ast)
  ast = ast.Visit(pep484.ConvertTypingToNative(name))

  if name:
    ast = ast.Replace(name=name)
    ast = ast.Visit(visitors.AddNamePrefix())
  else:
    # If there's no unique name, hash the sourcecode.
    ast = ast.Replace(name=hashlib.md5(src.encode("utf-8")).hexdigest())
  ast = ast.Visit(visitors.StripExternalNamePrefix())

  # Now that we have resolved external names, validate any class decorators that
  # do code generation. (We will generate the class lazily, but we should check
  # for errors at parse time so they can be reported early.)
  try:
    ast = ast.Visit(decorate.ValidateDecoratedClassVisitor())
  except TypeError as e:
    # Convert errors into ParseError. Unfortunately we no longer have location
    # information if an error is raised during transformation of a class node.
    raise ParseError.from_exc(e)

  return ast