11 import struct |
11 import struct |
12 import sys |
12 import sys |
13 import types |
13 import types |
14 |
14 |
15 from coverage import env |
15 from coverage import env |
16 from coverage.exceptions import CoverageException, ExceptionDuringRun, NoCode, NoSource |
16 from coverage.exceptions import CoverageException, _ExceptionDuringRun, NoCode, NoSource |
17 from coverage.files import canonical_filename, python_reported_file |
17 from coverage.files import canonical_filename, python_reported_file |
18 from coverage.misc import isolate_module |
18 from coverage.misc import isolate_module |
19 from coverage.phystokens import compile_unicode |
19 from coverage.phystokens import compile_unicode |
20 from coverage.python import get_python_source |
20 from coverage.python import get_python_source |
21 |
21 |
142 try_filename = os.path.abspath(try_filename) |
142 try_filename = os.path.abspath(try_filename) |
143 if os.path.exists(try_filename): |
143 if os.path.exists(try_filename): |
144 self.arg0 = try_filename |
144 self.arg0 = try_filename |
145 break |
145 break |
146 else: |
146 else: |
147 raise NoSource("Can't find '__main__' module in '%s'" % self.arg0) |
147 raise NoSource(f"Can't find '__main__' module in '{self.arg0}'") |
148 |
148 |
149 # Make a spec. I don't know if this is the right way to do it. |
149 # Make a spec. I don't know if this is the right way to do it. |
150 try_filename = python_reported_file(try_filename) |
150 try_filename = python_reported_file(try_filename) |
151 self.spec = importlib.machinery.ModuleSpec("__main__", None, origin=try_filename) |
151 self.spec = importlib.machinery.ModuleSpec("__main__", None, origin=try_filename) |
152 self.spec.has_location = True |
152 self.spec.has_location = True |
231 typ2, err2, tb2 = sys.exc_info() |
231 typ2, err2, tb2 = sys.exc_info() |
232 err2.__suppress_context__ = True |
232 err2.__suppress_context__ = True |
233 err2.__traceback__ = err2.__traceback__.tb_next |
233 err2.__traceback__ = err2.__traceback__.tb_next |
234 sys.__excepthook__(typ2, err2, tb2.tb_next) |
234 sys.__excepthook__(typ2, err2, tb2.tb_next) |
235 sys.stderr.write("\nOriginal exception was:\n") |
235 sys.stderr.write("\nOriginal exception was:\n") |
236 raise ExceptionDuringRun(typ, err, tb.tb_next) from exc |
236 raise _ExceptionDuringRun(typ, err, tb.tb_next) from exc |
237 else: |
237 else: |
238 sys.exit(1) |
238 sys.exit(1) |
239 finally: |
239 finally: |
240 os.chdir(cwd) |
240 os.chdir(cwd) |
241 |
241 |
291 with fpyc: |
291 with fpyc: |
292 # First four bytes are a version-specific magic number. It has to |
292 # First four bytes are a version-specific magic number. It has to |
293 # match or we won't run the file. |
293 # match or we won't run the file. |
294 magic = fpyc.read(4) |
294 magic = fpyc.read(4) |
295 if magic != PYC_MAGIC_NUMBER: |
295 if magic != PYC_MAGIC_NUMBER: |
296 raise NoCode(f"Bad magic number in .pyc file: {magic} != {PYC_MAGIC_NUMBER}") |
296 raise NoCode(f"Bad magic number in .pyc file: {magic!r} != {PYC_MAGIC_NUMBER!r}") |
297 |
297 |
298 date_based = True |
298 date_based = True |
299 if env.PYBEHAVIOR.hashed_pyc_pep552: |
299 if env.PYBEHAVIOR.hashed_pyc_pep552: |
300 flags = struct.unpack('<L', fpyc.read(4))[0] |
300 flags = struct.unpack('<L', fpyc.read(4))[0] |
301 hash_based = flags & 0x01 |
301 hash_based = flags & 0x01 |