我们从Python开源项目中,提取了以下37个代码示例,用于说明如何使用_ast.PyCF_ONLY_AST()。
def check(self, code, filename, ignore=None): """Check the code with pyflakes to find errors """ class FakeLoc: lineno = 0 try: fname = '' if filename is not None: fname = filename.encode('utf8') or '' code = code.encode('utf8') + b'\n' tree = compile(code, fname, 'exec', _ast.PyCF_ONLY_AST) except (SyntaxError, IndentationError): return self._handle_syntactic_error(code, filename) except ValueError as error: return [PyFlakesError(filename, FakeLoc(), 'E', error.args[0]), []] else: # the file is syntactically valid, check it now w = pyflakes.Checker(tree, filename, ignore) return w.messages
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
def parse(source, filename='<string>'): # NOTE: the raw string should be given to `compile` function if isinstance(source, str): source = fscommands.unicode_to_file_data(source) source = source.decode() if '\r' in source: source = source.replace('\r\n', '\n').replace('\r', '\n') if not source.endswith('\n'): source += '\n' try: return compile(source.encode(), 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
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 '\r' in source: source = source.replace('\r\n', '\n').replace('\r', '\n') if not source.endswith('\n'): source += '\n' try: return compile(source, filename, 'exec', _ast.PyCF_ONLY_AST) except (TypeError, ValueError), e: error = SyntaxError() error.lineno = 1 error.filename = filename error.msg = str(e) raise error
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} for m in sorted(w.messages, key=lambda m: m.lineno) ] # pylama:ignore=E501,C0301
def __new__(cls, name, bases, dct): slots = dct.get('__slots__', []) orig_slots = [] for base in bases: if hasattr(base, "__slots__"): orig_slots += base.__slots__ if '__init__' in dct: init = dct['__init__'] initproc = type.__new__(cls, name, bases, dct) initproc_source = inspect.getsource(initproc) ast = compile(initproc_source, "dont_care", 'exec', _ast.PyCF_ONLY_AST) classdef = ast.body[0] stmts = classdef.body for declaration in stmts: if isinstance(declaration, _ast.FunctionDef): name1 = declaration.name if name1 == '__init__': # delete this line if you do not initialize all instance variables in __init__ initbody = declaration.body for statement in initbody: if isinstance(statement, _ast.Assign): for target in statement.targets: name1 = target.attr if name1 not in orig_slots: slots.append(name1) if slots: dct['__slots__'] = slots return type.__new__(cls, name, bases, dct)
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')
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')
def parse(source, mode): return compile(source, '', mode, _ast.PyCF_ONLY_AST)
def parse(expr, filename='<unknown>', mode='eval'): """parse(source[, filename], mode]] -> code object Parse an expression into an AST node. Equivalent to compile(expr, filename, mode, PyCF_ONLY_AST). """ return compile(expr, filename, mode, ast.PyCF_ONLY_AST)
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 # 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
def parse(string): return compile(string, "<string>", 'exec', PyCF_ONLY_AST)
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)
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()
def __new__(cls, name: str, bases: list, dictionary: dict) -> object: slots, original_slots = dictionary.get('__slots__', []), [] for base in bases: if hasattr(base, "__slots__"): original_slots += base.__slots__ if '__init__' in dictionary: init_src = getsource(type.__new__(cls, name, bases, dictionary)) ast = compile(init_src, "", 'exec', _ast.PyCF_ONLY_AST, optimize=2) for declaration in tuple(ast.body[0].body): if isinstance(declaration, _ast.FunctionDef): nombre = declaration.name if nombre == '__init__': initbody = declaration.body for statement in initbody: if isinstance(statement, _ast.Assign): for target in statement.targets: nombrecito = target.attr if nombrecito not in original_slots: slots.append(nombrecito) dictionary['__slots__'] = namedtuple("AutoSlots", slots)(*slots) dictionary['__doc__'] = dictionary.get( '__doc__', f"""{name} class from '{dictionary['__module__']}'. \nInstantiated using Angler AutoSlots MetaClass. Mutable/writable/readable attributes: {dictionary['__slots__']} \nThis does not make the {name} class Inmmutable. \nAdding new attributes via assignments/setattr/etc is Blocked. \nUses less memory, speeds up attribute accesses and avoid bugs \nTry to add new attributes to this {name} class will raise: AttributeError: {name} object has no attribute\n\n""" + __doc__ ) return type.__new__(cls, name, bases, dictionary)
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] if sys.version_info[0] >= 3: # XXX py3's inspect.getsourcefile() checks for a module # and a pep302 __loader__ ... we don't have a module # at code compile-time so we need to fake it here m = ModuleType("_pycodecompile_pseudo_module") py.std.inspect.modulesbyfile[filename] = None py.std.sys.modules[None] = m m.__loader__ = 1 py.std.linecache.cache[filename] = (1, None, lines, filename) return co # # public API shortcut functions #
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 #
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
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)
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 #
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 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)