當前位置: 首頁>>代碼示例>>Python>>正文


Python _ast.PyCF_ONLY_AST方法代碼示例

本文整理匯總了Python中_ast.PyCF_ONLY_AST方法的典型用法代碼示例。如果您正苦於以下問題:Python _ast.PyCF_ONLY_AST方法的具體用法?Python _ast.PyCF_ONLY_AST怎麽用?Python _ast.PyCF_ONLY_AST使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在_ast的用法示例。


在下文中一共展示了_ast.PyCF_ONLY_AST方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: run

# 需要導入模塊: import _ast [as 別名]
# 或者: from _ast import PyCF_ONLY_AST [as 別名]
def run(path, code=None, params=None, **meta):
        """Check code with pyflakes.

        :return list: List of errors.
        """
        import _ast

        builtins = params.get("builtins", "")

        if builtins:
            builtins = builtins.split(",")

        tree = compile(code, path, "exec", _ast.PyCF_ONLY_AST)
        w = checker.Checker(tree, path, builtins=builtins)
        w.messages = sorted(w.messages, key=lambda m: m.lineno)
        return [{
            'lnum': m.lineno,
            'text': m.message % m.message_args,
            'type': m.message[0]
        } for m in w.messages]

#  pylama:ignore=E501,C0301 
開發者ID:AtomLinter,項目名稱:linter-pylama,代碼行數:24,代碼來源:pylama_pyflakes.py

示例2: parse

# 需要導入模塊: import _ast [as 別名]
# 或者: from _ast import PyCF_ONLY_AST [as 別名]
def parse(source, filename='<string>'):
    # NOTE: the raw string should be given to `compile` function
    if isinstance(source, unicode):
        source = fscommands.unicode_to_file_data(source)
    if b'\r' in source:
        source = source.replace(b'\r\n', b'\n').replace(b'\r', b'\n')
    if not source.endswith(b'\n'):
        source += b'\n'
    try:
        return compile(source, filename, 'exec', _ast.PyCF_ONLY_AST)
    except (TypeError, ValueError) as e:
        error = SyntaxError()
        error.lineno = 1
        error.filename = filename
        error.msg = str(e)
        raise error 
開發者ID:zrzka,項目名稱:blackmamba,代碼行數:18,代碼來源:ast.py

示例3: parse

# 需要導入模塊: import _ast [as 別名]
# 或者: from _ast import PyCF_ONLY_AST [as 別名]
def parse(expr, filename="<unknown>", mode="exec"):
    """Parse an expression into an AST node."""
    return compile(expr, filename, mode, PyCF_ONLY_AST) 
開發者ID:remg427,項目名稱:misp42splunk,代碼行數:5,代碼來源:_ast_util.py

示例4: _parse

# 需要導入模塊: import _ast [as 別名]
# 或者: from _ast import PyCF_ONLY_AST [as 別名]
def _parse(string):
    return compile(string, "<string>", 'exec', _ast.PyCF_ONLY_AST) 
開發者ID:AtomLinter,項目名稱:linter-pylama,代碼行數:4,代碼來源:builder.py

示例5: getBufferErrors

# 需要導入模塊: import _ast [as 別名]
# 或者: from _ast import PyCF_ONLY_AST [as 別名]
def getBufferErrors(sourceCode):
    """Provides a list of warnings/errors for the given source code"""
    sourceCode += '\n'

    # First, compile into an AST and handle syntax errors.
    try:
        tree = compile(sourceCode, "<string>", "exec", PyCF_ONLY_AST)
    except SyntaxError as value:
        # If there's an encoding problem with the file, the text is None.
        if value.text is None:
            return {}, []
        return {value.lineno: [value.args[0]]}, []
    except (ValueError, TypeError) as value:
        # ValueError may happened in case of invalid \x escape character
        # E.g. http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=674797
        # TypeError may happened in case of null characters in a file
        # E.g. http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=674796
        msg = str(value)
        if msg == "":
            return {-1: ["Could not compile buffer: unknown error"]}, []
        return {-1: ["Could not compile buffer: " + msg]}, []

    # Okay, it's syntactically valid.  Now check it.
    check = Checker(tree, "<string>")
    results = {}
    lines = sourceCode.splitlines()
    for warning in check.messages:
        if isinstance(warning.lineno, int):
            lineno = warning.lineno
        else:
            # By some reasons I see ast NAME node here (pyflakes 0.7.3)
            lineno = warning.lineno.lineno
        if not IGNORE_REGEXP.search(lines[lineno - 1]):
            if lineno in results:
                results[lineno].append(warning.message % warning.message_args)
            else:
                results[lineno] = [warning.message % warning.message_args]

    # Radon: CC complexity as the second value
    return results, sorted_results(cc_visit_ast(tree)) 
開發者ID:SergeySatskiy,項目名稱:codimension,代碼行數:42,代碼來源:ierrors.py

示例6: test_compile_ast

# 需要導入模塊: import _ast [as 別名]
# 或者: from _ast import PyCF_ONLY_AST [as 別名]
def test_compile_ast(self):
        fname = __file__
        if fname.lower().endswith(('pyc', 'pyo')):
            fname = fname[:-1]
        with open(fname, 'r') as f:
            fcontents = f.read()
        sample_code = [
            ['<assign>', 'x = 5'],
            ['<print1>', 'print 1'],
            ['<printv>', 'print v'],
            ['<printTrue>', 'print True'],
            ['<printList>', 'print []'],
            ['<ifblock>', """if True:\n    pass\n"""],
            ['<forblock>', """for n in [1, 2, 3]:\n    print n\n"""],
            ['<deffunc>', """def foo():\n    pass\nfoo()\n"""],
            [fname, fcontents],
        ]

        for fname, code in sample_code:
            co1 = compile(code, '%s1' % fname, 'exec')
            ast = compile(code, '%s2' % fname, 'exec', _ast.PyCF_ONLY_AST)
            self.assertTrue(type(ast) == _ast.Module)
            co2 = compile(ast, '%s3' % fname, 'exec')
            self.assertEqual(co1, co2)
            # the code object's filename comes from the second compilation step
            self.assertEqual(co2.co_filename, '%s3' % fname)

        # raise exception when node type doesn't match with compile mode
        co1 = compile('print 1', '<string>', 'exec', _ast.PyCF_ONLY_AST)
        self.assertRaises(TypeError, compile, co1, '<ast>', 'eval')

        # raise exception when node type is no start node
        self.assertRaises(TypeError, compile, _ast.If(), '<ast>', 'exec')

        # raise exception when node has invalid children
        ast = _ast.Module()
        ast.body = [_ast.BoolOp()]
        self.assertRaises(TypeError, compile, ast, '<ast>', 'exec') 
開發者ID:IronLanguages,項目名稱:ironpython2,代碼行數:40,代碼來源:test_compile.py

示例7: test_compile_ast

# 需要導入模塊: import _ast [as 別名]
# 或者: from _ast import PyCF_ONLY_AST [as 別名]
def test_compile_ast(self):
        fname = __file__
        if fname.lower().endswith('pyc'):
            fname = fname[:-1]
        with open(fname, 'r') as f:
            fcontents = f.read()
        sample_code = [
            ['<assign>', 'x = 5'],
            ['<ifblock>', """if True:\n    pass\n"""],
            ['<forblock>', """for n in [1, 2, 3]:\n    print(n)\n"""],
            ['<deffunc>', """def foo():\n    pass\nfoo()\n"""],
            [fname, fcontents],
        ]

        for fname, code in sample_code:
            co1 = compile(code, '%s1' % fname, 'exec')
            ast = compile(code, '%s2' % fname, 'exec', _ast.PyCF_ONLY_AST)
            self.assertTrue(type(ast) == _ast.Module)
            co2 = compile(ast, '%s3' % fname, 'exec')
            self.assertEqual(co1, co2)
            # the code object's filename comes from the second compilation step
            self.assertEqual(co2.co_filename, '%s3' % fname)

        # raise exception when node type doesn't match with compile mode
        co1 = compile('print(1)', '<string>', 'exec', _ast.PyCF_ONLY_AST)
        self.assertRaises(TypeError, compile, co1, '<ast>', 'eval')

        # raise exception when node type is no start node
        self.assertRaises(TypeError, compile, _ast.If(), '<ast>', 'exec')

        # raise exception when node has invalid children
        ast = _ast.Module()
        ast.body = [_ast.BoolOp()]
        self.assertRaises(TypeError, compile, ast, '<ast>', 'exec') 
開發者ID:Microvellum,項目名稱:Fluid-Designer,代碼行數:36,代碼來源:test_compile.py

示例8: test_compile_ast

# 需要導入模塊: import _ast [as 別名]
# 或者: from _ast import PyCF_ONLY_AST [as 別名]
def test_compile_ast(self):
        fname = __file__
        if fname.lower().endswith(('pyc', 'pyo')):
            fname = fname[:-1]
        with open(fname, 'r') as f:
            fcontents = f.read()
        sample_code = [
            ['<assign>', 'x = 5'],
            ['<ifblock>', """if True:\n    pass\n"""],
            ['<forblock>', """for n in [1, 2, 3]:\n    print(n)\n"""],
            ['<deffunc>', """def foo():\n    pass\nfoo()\n"""],
            [fname, fcontents],
        ]

        for fname, code in sample_code:
            co1 = compile(code, '%s1' % fname, 'exec')
            ast = compile(code, '%s2' % fname, 'exec', _ast.PyCF_ONLY_AST)
            self.assertTrue(type(ast) == _ast.Module)
            co2 = compile(ast, '%s3' % fname, 'exec')
            self.assertEqual(co1, co2)
            # the code object's filename comes from the second compilation step
            self.assertEqual(co2.co_filename, '%s3' % fname)

        # raise exception when node type doesn't match with compile mode
        co1 = compile('print(1)', '<string>', 'exec', _ast.PyCF_ONLY_AST)
        self.assertRaises(TypeError, compile, co1, '<ast>', 'eval')

        # raise exception when node type is no start node
        self.assertRaises(TypeError, compile, _ast.If(), '<ast>', 'exec')

        # raise exception when node has invalid children
        ast = _ast.Module()
        ast.body = [_ast.BoolOp()]
        self.assertRaises(TypeError, compile, ast, '<ast>', 'exec') 
開發者ID:IronLanguages,項目名稱:ironpython3,代碼行數:36,代碼來源:test_compile.py

示例9: test_impossibleContext

# 需要導入模塊: import _ast [as 別名]
# 或者: from _ast import PyCF_ONLY_AST [as 別名]
def test_impossibleContext(self):
        """
        A Name node with an unrecognized context results in a RuntimeError being
        raised.
        """
        tree = compile("x = 10", "<test>", "exec", PyCF_ONLY_AST)
        # Make it into something unrecognizable.
        tree.body[0].targets[0].ctx = object()
        self.assertRaises(RuntimeError, checker.Checker, tree) 
開發者ID:zrzka,項目名稱:blackmamba,代碼行數:11,代碼來源:test_undefined_names.py

示例10: handleDoctests

# 需要導入模塊: import _ast [as 別名]
# 或者: from _ast import PyCF_ONLY_AST [as 別名]
def handleDoctests(self, node):
        try:
            (docstring, node_lineno) = self.getDocstring(node.body[0])
            examples = docstring and self._getDoctestExamples(docstring)
        except (ValueError, IndexError):
            # e.g. line 6 of the docstring for <string> has inconsistent
            # leading whitespace: ...
            return
        if not examples:
            return
        node_offset = self.offset or (0, 0)
        self.pushScope()
        underscore_in_builtins = '_' in self.builtIns
        if not underscore_in_builtins:
            self.builtIns.add('_')
        for example in examples:
            try:
                tree = compile(example.source, "<doctest>", "exec", ast.PyCF_ONLY_AST)
            except SyntaxError:
                e = sys.exc_info()[1]
                position = (node_lineno + example.lineno + e.lineno,
                            example.indent + 4 + (e.offset or 0))
                self.report(messages.DoctestSyntaxError, node, position)
            else:
                self.offset = (node_offset[0] + node_lineno + example.lineno,
                               node_offset[1] + example.indent + 4)
                self.handleChildren(tree)
                self.offset = node_offset
        if not underscore_in_builtins:
            self.builtIns.remove('_')
        self.popScope() 
開發者ID:QQuick,項目名稱:Transcrypt,代碼行數:33,代碼來源:checker.py

示例11: compile

# 需要導入模塊: import _ast [as 別名]
# 或者: from _ast import PyCF_ONLY_AST [as 別名]
def compile(self, filename=None, mode='exec',
                flag=generators.compiler_flag,
                dont_inherit=0, _genframe=None):
        """ return compiled code object. if filename is None
            invent an artificial filename which displays
            the source/line position of the caller frame.
        """
        if not filename or py.path.local(filename).check(file=0):
            if _genframe is None:
                _genframe = sys._getframe(1) # the caller
            fn,lineno = _genframe.f_code.co_filename, _genframe.f_lineno
            base = "<%d-codegen " % self._compilecounter
            self.__class__._compilecounter += 1
            if not filename:
                filename = base + '%s:%d>' % (fn, lineno)
            else:
                filename = base + '%r %s:%d>' % (filename, fn, lineno)
        source = "\n".join(self.lines) + '\n'
        try:
            co = cpy_compile(source, filename, mode, flag)
        except SyntaxError:
            ex = sys.exc_info()[1]
            # re-represent syntax errors from parsing python strings
            msglines = self.lines[:ex.lineno]
            if ex.offset:
                msglines.append(" "*ex.offset + '^')
            msglines.append("(code was compiled probably from here: %s)" % filename)
            newex = SyntaxError('\n'.join(msglines))
            newex.offset = ex.offset
            newex.lineno = ex.lineno
            newex.text = ex.text
            raise newex
        else:
            if flag & _AST_FLAG:
                return co
            lines = [(x + "\n") for x in self.lines]
            import linecache
            linecache.cache[filename] = (1, None, lines, filename)
            return co

#
# public API shortcut functions
# 
開發者ID:pytest-dev,項目名稱:py,代碼行數:45,代碼來源:source.py

示例12: compile

# 需要導入模塊: import _ast [as 別名]
# 或者: from _ast import PyCF_ONLY_AST [as 別名]
def compile(self, filename=None, mode='exec',
                flag=generators.compiler_flag,
                dont_inherit=0, _genframe=None):
        """ return compiled code object. if filename is None
            invent an artificial filename which displays
            the source/line position of the caller frame.
        """
        if not filename or py.path.local(filename).check(file=0):
            if _genframe is None:
                _genframe = sys._getframe(1) # the caller
            fn,lineno = _genframe.f_code.co_filename, _genframe.f_lineno
            base = "<%d-codegen " % self._compilecounter
            self.__class__._compilecounter += 1
            if not filename:
                filename = base + '%s:%d>' % (fn, lineno)
            else:
                filename = base + '%r %s:%d>' % (filename, fn, lineno)
        source = "\n".join(self.lines) + '\n'
        try:
            co = cpy_compile(source, filename, mode, flag)
        except SyntaxError:
            ex = sys.exc_info()[1]
            # re-represent syntax errors from parsing python strings
            msglines = self.lines[:ex.lineno]
            if ex.offset:
                msglines.append(" "*ex.offset + '^')
            msglines.append("(code was compiled probably from here: %s)" % filename)
            newex = SyntaxError('\n'.join(msglines))
            newex.offset = ex.offset
            newex.lineno = ex.lineno
            newex.text = ex.text
            raise newex
        else:
            if flag & _AST_FLAG:
                return co
            lines = [(x + "\n") for x in self.lines]
            py.std.linecache.cache[filename] = (1, None, lines, filename)
            return co

#
# public API shortcut functions
# 
開發者ID:acaceres2176,項目名稱:scylla,代碼行數:44,代碼來源:source.py

示例13: handleDoctests

# 需要導入模塊: import _ast [as 別名]
# 或者: from _ast import PyCF_ONLY_AST [as 別名]
def handleDoctests(self, node):
        try:
            if hasattr(node, 'docstring'):
                docstring = node.docstring

                # This is just a reasonable guess. In Python 3.7, docstrings no
                # longer have line numbers associated with them. This will be
                # incorrect if there are empty lines between the beginning
                # of the function and the docstring.
                node_lineno = node.lineno
                if hasattr(node, 'args'):
                    node_lineno = max([node_lineno] +
                                      [arg.lineno for arg in node.args.args])
            else:
                (docstring, node_lineno) = self.getDocstring(node.body[0])
            examples = docstring and self._getDoctestExamples(docstring)
        except (ValueError, IndexError):
            # e.g. line 6 of the docstring for <string> has inconsistent
            # leading whitespace: ...
            return
        if not examples:
            return

        # Place doctest in module scope
        saved_stack = self.scopeStack
        self.scopeStack = [self.scopeStack[0]]
        node_offset = self.offset or (0, 0)
        self.pushScope(DoctestScope)
        underscore_in_builtins = '_' in self.builtIns
        if not underscore_in_builtins:
            self.builtIns.add('_')
        for example in examples:
            try:
                tree = compile(example.source, "<doctest>", "exec", ast.PyCF_ONLY_AST)
            except SyntaxError:
                e = sys.exc_info()[1]
                if PYPY:
                    e.offset += 1
                position = (node_lineno + example.lineno + e.lineno,
                            example.indent + 4 + (e.offset or 0))
                self.report(messages.DoctestSyntaxError, node, position)
            else:
                self.offset = (node_offset[0] + node_lineno + example.lineno,
                               node_offset[1] + example.indent + 4)
                self.handleChildren(tree)
                self.offset = node_offset
        if not underscore_in_builtins:
            self.builtIns.remove('_')
        self.popScope()
        self.scopeStack = saved_stack 
開發者ID:zrzka,項目名稱:blackmamba,代碼行數:52,代碼來源:checker.py

示例14: check

# 需要導入模塊: import _ast [as 別名]
# 或者: from _ast import PyCF_ONLY_AST [as 別名]
def check(codeString, filename, reporter=None):
    """
    Check the Python source given by C{codeString} for flakes.

    @param codeString: The Python source to check.
    @type codeString: C{str}

    @param filename: The name of the file the source came from, used to report
        errors.
    @type filename: C{str}

    @param reporter: A L{Reporter} instance, where errors and warnings will be
        reported.

    @return: The number of warnings emitted.
    @rtype: C{int}
    """
    if reporter is None:
        reporter = modReporter._makeDefaultReporter()
    # First, compile into an AST and handle syntax errors.
    try:
        tree = compile(codeString, filename, "exec", _ast.PyCF_ONLY_AST)
    except SyntaxError:
        value = sys.exc_info()[1]
        msg = value.args[0]

        (lineno, offset, text) = value.lineno, value.offset, value.text

        if checker.PYPY:
            if text is None:
                lines = codeString.splitlines()
                if len(lines) >= lineno:
                    text = lines[lineno - 1]
                    if sys.version_info >= (3, ) and isinstance(text, bytes):
                        try:
                            text = text.decode('ascii')
                        except UnicodeDecodeError:
                            text = None
            offset -= 1

        # If there's an encoding problem with the file, the text is None.
        if text is None:
            # Avoid using msg, since for the only known case, it contains a
            # bogus message that claims the encoding the file declared was
            # unknown.
            reporter.unexpectedError(filename, 'problem decoding source')
        else:
            reporter.syntaxError(filename, msg, lineno, offset, text)
        return 1
    except Exception:
        reporter.unexpectedError(filename, 'problem decoding source')
        return 1
    # Okay, it's syntactically valid.  Now check it.
    w = checker.Checker(tree, filename)
    w.messages.sort(key=lambda m: m.lineno)
    for warning in w.messages:
        reporter.flake(warning)
    return len(w.messages) 
開發者ID:zrzka,項目名稱:blackmamba,代碼行數:60,代碼來源:api.py

示例15: run_pyflakes

# 需要導入模塊: import _ast [as 別名]
# 或者: from _ast import PyCF_ONLY_AST [as 別名]
def run_pyflakes(request_data):
    """
    Worker that run a frosted (the fork of pyflakes) code analysis on the
    current editor text.
    """
    global prev_results
    from pyflakes import checker
    import _ast
    WARNING = 1
    ERROR = 2
    ret_val = []
    code = request_data['code']
    path = request_data['path']
    encoding = request_data['encoding']
    if not encoding:
        encoding = 'utf-8'
    if not path:
        path = os.path.join(tempfile.gettempdir(), 'temp.py')
    if not code:
        return []
    else:
        # First, compile into an AST and handle syntax errors.
        try:
            tree = compile(code.encode(encoding), path, "exec",
                           _ast.PyCF_ONLY_AST)
        except SyntaxError as value:
            msg = '[pyFlakes] %s' % value.args[0]
            (lineno, offset, text) = value.lineno - 1, value.offset, value.text
            # If there's an encoding problem with the file, the text is None
            if text is None:
                # Avoid using msg, since for the only known case, it
                # contains a bogus message that claims the encoding the
                # file declared was unknown.s
                _logger().warning("[SyntaxError] %s: problem decoding source",
                                  path)
            else:
                ret_val.append((msg, ERROR, lineno))
        else:
            # Okay, it's syntactically valid.  Now check it.
            w = checker.Checker(tree, os.path.split(path)[1])
            w.messages.sort(key=lambda m: m.lineno)
            for message in w.messages:
                msg = "[pyFlakes] %s" % str(message).split(':')[-1].strip()
                line = message.lineno - 1
                status = WARNING \
                    if message.__class__ not in PYFLAKES_ERROR_MESSAGES \
                    else ERROR
                ret_val.append((msg, status, line))
    prev_results = ret_val
    return ret_val 
開發者ID:TuringApp,項目名稱:Turing,代碼行數:52,代碼來源:workers.py


注:本文中的_ast.PyCF_ONLY_AST方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。