예제 #1
0
def _parse_ast_nodes(text, flags, auto_flags, mode):
    """
    Parse a block of lines into an AST.

    Also annotate ``input_flags``, ``source_flags``, and ``flags`` on the
    resulting ast node.

    :type text:
      ``FileText``
    :type flags:
      ``CompilerFlags``
    :type auto_flags:
      ``bool``
    :param auto_flags:
      Whether to guess different flags if ``text`` can't be parsed with
      ``flags``.
    :param mode:
      Compilation mode: "exec", "single", or "eval".
    :rtype:
      ``ast.Module``
    """
    text = FileText(text)
    flags = CompilerFlags(flags)
    filename = str(text.filename) if text.filename else "<unknown>"
    source = text.joined
    source = dedent(source)
    if PY2 and isinstance(source, unicode):
        source = source.encode('utf-8')
    if not source.endswith("\n"):
        # Ensure that the last line ends with a newline (``ast`` barfs
        # otherwise).
        source += "\n"
    exp = None
    for flags in _flags_to_try(source, flags, auto_flags, mode):
        cflags = ast.PyCF_ONLY_AST | int(flags)
        try:
            result = compile(source,
                             filename,
                             mode,
                             flags=cflags,
                             dont_inherit=1)
        except SyntaxError as e:
            exp = e
            pass
        else:
            # Attach flags to the result.
            result.input_flags = flags
            result.source_flags = CompilerFlags.from_ast(result)
            result.flags = result.input_flags | result.source_flags
            result.text = text
            return result
    raise exp  # SyntaxError
예제 #2
0
def _parse_ast_nodes(text, flags, auto_flags, mode):
    """
    Parse a block of lines into an AST.

    Also annotate C{input_flags}, C{source_flags}, and C{flags} on the
    resulting ast node.

    @type text:
      C{FileText}
    @type flags:
      C{CompilerFlags}
    @type auto_flags:
      C{bool}
    @param auto_flags:
      Whether to guess different flags if C{text} can't be parsed with
      C{flags}.
    @param mode:
      Compilation mode: "exec", "single", or "eval".
    @rtype:
      C{ast.Module}
    """
    text = FileText(text)
    flags = CompilerFlags(flags)
    filename = str(text.filename) if text.filename else "<unknown>"
    source = text.joined
    source = dedent(source)
    if not source.endswith("\n"):
        # Ensure that the last line ends with a newline (C{ast} barfs
        # otherwise).
        source += "\n"
    exp = None
    for flags in _flags_to_try(source, flags, auto_flags, mode):
        cflags = ast.PyCF_ONLY_AST | int(flags)
        try:
            result = compile(source,
                             filename,
                             mode,
                             flags=cflags,
                             dont_inherit=1)
        except SyntaxError as e:
            exp = e
            pass
        else:
            # Attach flags to the result.
            result.input_flags = flags
            result.source_flags = CompilerFlags.from_ast(result)
            result.flags = result.input_flags | result.source_flags
            result.text = text
            return result
    raise exp  # SyntaxError
예제 #3
0
 def __construct_from_annotated_ast(cls, annotated_ast_nodes, text, flags):
     # Constructor for internal use by _split_by_statement() or
     # concatenate().
     ast_node = ast.Module(annotated_ast_nodes)
     ast_node.text = text
     ast_node.flags = flags
     if not hasattr(ast_node, "source_flags"):
         ast_node.source_flags = CompilerFlags.from_ast(annotated_ast_nodes)
     self = object.__new__(cls)
     self._ast_node_or_parse_exception = ast_node
     self.ast_node = ast_node
     self.annotated_ast_node = ast_node
     self.text = text
     self.flags = self._input_flags = flags
     self._auto_flags = False
     return self