def assert_compile_unicode(self, source): """Assert that `source` will compile properly with `compile_unicode`.""" source += u"a = 42\n" # This doesn't raise an exception: code = compile_unicode(source, "<string>", "exec") globs = {} exec(code, globs) self.assertEqual(globs['a'], 42)
def make_code_from_py(filename): """Get source from `filename` and make a code object of it.""" # Open the source file. try: source = get_python_source(filename) except (IOError, NoSource): raise NoSource("No file to run: '%s'" % filename) code = compile_unicode(source, filename, "exec") return code
def __init__(self, text, code=None, filename=None): self.text = text if code: self.code = code else: try: self.code = compile_unicode(text, filename, "exec") except SyntaxError as synerr: raise NotPython( u"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']: if not hasattr(self.code, attr): raise StopEverything( # pragma: only jython "This implementation of Python doesn't support code analysis.\n" "Run coverage.py under another Python for this command.")
def __init__(self, text, code=None, filename=None): self.text = text if code: self.code = code else: try: self.code = compile_unicode(text, 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." )