def __init__(self, code=None, text=None, filename=None): if code: self.code = code self.text = text else: if not text: assert filename, "If no code or text, need a filename" with open_python_source(filename) as sourcef: text = sourcef.read() self.text = text try: # Python 2.3 and 2.4 don't like partial last lines, so be sure # the text ends nicely for them. self.code = compile(text + '\n', filename, "exec") except SyntaxError as synerr: raise NotPython( "Couldn't parse '%s' as Python source: '%s' at line %d" % (filename, synerr.msg, synerr.lineno) ) # Alternative Python implementations don't always provide all the # attributes on code objects that we need to do the analysis. for attr in ['co_lnotab', 'co_firstlineno', 'co_consts', 'co_code']: if not hasattr(self.code, attr): raise CoverageException( "This implementation of Python doesn't support code " "analysis.\n" "Run coverage.py under CPython for this command." )
def source(self): """Return the source code, as a string.""" if os.path.exists(self.filename): # A regular text file: open it. with open_python_source(self.filename) as f: return f.read() # Maybe it's in a zip file? source = self.file_locator.get_zip_data(self.filename) if source is not None: return source # Couldn't find source. raise CoverageException("No source for code '%s'." % self.filename)
def source(self): """Return the source code, as a string.""" if os.path.exists(self.filename): # A regular text file: open it. return open_python_source(self.filename).read() # Maybe it's in a zip file? source = self.file_locator.get_zip_data(self.filename) if source is not None: return source # Couldn't find source. raise CoverageException( "No source for code '%s'." % self.filename )
def __init__(self, text=None, filename=None, exclude=None): """ Source can be provided as `text`, the text itself, or `filename`, from which the text will be read. Excluded lines are those that match `exclude`, a regex. """ assert text or filename, "PythonParser needs either text or filename" self.filename = filename or "<code>" self.text = text if not self.text: try: with open_python_source(self.filename) as sourcef: self.text = sourcef.read() except IOError as err: raise NoSource( "No source for code: '%s': %s" % (self.filename, err) ) # Scrap the BOM if it exists. if self.text and ord(self.text[0]) == 0xfeff: self.text = self.text[1:] self.exclude = exclude self.show_tokens = False # The text lines of the parsed code. self.lines = self.text.split('\n') # The line numbers of excluded lines of code. self.excluded = set() # The line numbers of docstring lines. self.docstrings = set() # The line numbers of class definitions. self.classdefs = set() # A dict mapping line numbers to (lo,hi) for multi-line statements. self.multiline = {} # The line numbers that start statements. self.statement_starts = set() # Lazily-created ByteParser self._byte_parser = None
def make_code_from_py(filename): """Get source from `filename` and make a code object of it.""" # Open the source file. try: source_file = open_python_source(filename) except IOError: raise NoSource("No file to run: %r" % filename) with source_file: source = source_file.read() # We have the source. `compile` still needs the last line to be clean, # so make sure it is, then compile a code object from it. if not source or source[-1] != '\n': source += '\n' code = compile(source, filename, "exec") return code