Exemplo n.º 1
0
def read_source_file(filename):
    from lib2to3.pgen2.tokenize import cookie_re

    with open_with_encoding_check(filename) as f:
        return ''.join([
            '\n' if i < 2 and cookie_re.match(line) else line
            for i, line in enumerate(f)
        ])
Exemplo n.º 2
0
    def __init__(self, filename, lines):
        """
        Don't call this constructor, see the class docstring.
        """

        self.filename = filename
        text = ''.join(lines)

        if not isinstance(text, text_type):
            encoding = self.detect_encoding(text)
            # noinspection PyUnresolvedReferences
            text = text.decode(encoding)
            lines = [line.decode(encoding) for line in lines]

        self.text = text
        self.lines = [line.rstrip('\r\n') for line in lines]

        if PY3:
            ast_text = text
        else:
            # In python 2 it's a syntax error to parse unicode
            # with an encoding declaration, so we remove it but
            # leave empty lines in its place to keep line numbers the same
            ast_text = ''.join([
                '\n' if i < 2 and encoding_pattern.match(line) else line
                for i, line in enumerate(lines)
            ])

        self._nodes_by_line = defaultdict(list)
        self.tree = None
        self._qualnames = {}

        try:
            self.tree = ast.parse(ast_text, filename=filename)
        except SyntaxError:
            pass
        else:
            for node in ast.walk(self.tree):
                for child in ast.iter_child_nodes(node):
                    child.parent = node
                if hasattr(node, "lineno"):
                    if hasattr(node, "end_lineno") and isinstance(
                            node, ast.expr):
                        linenos = range(node.lineno, node.end_lineno + 1)
                    else:
                        linenos = [node.lineno]
                    for lineno in linenos:
                        self._nodes_by_line[lineno].append(node)

            visitor = QualnameVisitor()
            visitor.visit(self.tree)
            self._qualnames = visitor.qualnames
Exemplo n.º 3
0
    def __init__(self, filename, text):
        """
        Don't call this constructor, see the class docstring.
        """

        self.filename = filename

        if not isinstance(text, text_type):
            text = self.decode_source(text)
        self.text = text

        if PY3:
            ast_text = text
        else:
            # In python 2 it's a syntax error to parse unicode
            # with an encoding declaration, so we remove it but
            # leave empty lines in its place to keep line numbers the same
            ast_text = ''.join([
                '\n' if i < 2 and encoding_pattern.match(line)
                else line
                for i, line in enumerate(text.splitlines(True))
            ])

        self._nodes_by_line = defaultdict(list)
        self.tree = None
        self._qualnames = {}

        if text:
            try:
                self.tree = ast.parse(ast_text, filename=filename)
            except SyntaxError:
                pass
            else:
                for node in ast.walk(self.tree):
                    for child in ast.iter_child_nodes(node):
                        child.parent = node
                    if hasattr(node, 'lineno'):
                        self._nodes_by_line[node.lineno].append(node)

                visitor = QualnameVisitor()
                visitor.visit(self.tree)
                self._qualnames = visitor.qualnames