mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-06 20:16:53 +00:00
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
This commit is contained in:
parent
72ad6dc953
commit
f4cd1ba0d6
813 changed files with 66015 additions and 58839 deletions
|
|
@ -1,8 +1,6 @@
|
|||
# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
|
||||
# For details: https://github.com/nedbat/coveragepy/blob/master/NOTICE.txt
|
||||
|
||||
"""Execute files of Python code."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import importlib.machinery
|
||||
|
|
@ -12,14 +10,18 @@ import marshal
|
|||
import os
|
||||
import struct
|
||||
import sys
|
||||
|
||||
from importlib.machinery import ModuleSpec
|
||||
from types import CodeType, ModuleType
|
||||
from types import CodeType
|
||||
from types import ModuleType
|
||||
from typing import Any
|
||||
|
||||
from coverage import env
|
||||
from coverage.exceptions import CoverageException, _ExceptionDuringRun, NoCode, NoSource
|
||||
from coverage.files import canonical_filename, python_reported_file
|
||||
from coverage.exceptions import _ExceptionDuringRun
|
||||
from coverage.exceptions import CoverageException
|
||||
from coverage.exceptions import NoCode
|
||||
from coverage.exceptions import NoSource
|
||||
from coverage.files import canonical_filename
|
||||
from coverage.files import python_reported_file
|
||||
from coverage.misc import isolate_module
|
||||
from coverage.python import get_python_source
|
||||
|
||||
|
|
@ -28,11 +30,13 @@ os = isolate_module(os)
|
|||
|
||||
PYC_MAGIC_NUMBER = importlib.util.MAGIC_NUMBER
|
||||
|
||||
|
||||
class DummyLoader:
|
||||
"""A shim for the pep302 __loader__, emulating pkgutil.ImpLoader.
|
||||
|
||||
Currently only implements the .fullname attribute
|
||||
"""
|
||||
|
||||
def __init__(self, fullname: str, *_args: Any) -> None:
|
||||
self.fullname = fullname
|
||||
|
||||
|
|
@ -50,20 +54,20 @@ def find_module(
|
|||
except ImportError as err:
|
||||
raise NoSource(str(err)) from err
|
||||
if not spec:
|
||||
raise NoSource(f"No module named {modulename!r}")
|
||||
raise NoSource(f'No module named {modulename!r}')
|
||||
pathname = spec.origin
|
||||
packagename = spec.name
|
||||
if spec.submodule_search_locations:
|
||||
mod_main = modulename + ".__main__"
|
||||
mod_main = modulename + '.__main__'
|
||||
spec = importlib.util.find_spec(mod_main)
|
||||
if not spec:
|
||||
raise NoSource(
|
||||
f"No module named {mod_main}; " +
|
||||
f"{modulename!r} is a package and cannot be directly executed",
|
||||
f'No module named {mod_main}; ' +
|
||||
f'{modulename!r} is a package and cannot be directly executed',
|
||||
)
|
||||
pathname = spec.origin
|
||||
packagename = spec.name
|
||||
packagename = packagename.rpartition(".")[0]
|
||||
packagename = packagename.rpartition('.')[0]
|
||||
return pathname, packagename, spec
|
||||
|
||||
|
||||
|
|
@ -73,6 +77,7 @@ class PyRunner:
|
|||
This is meant to emulate real Python execution as closely as possible.
|
||||
|
||||
"""
|
||||
|
||||
def __init__(self, args: list[str], as_module: bool = False) -> None:
|
||||
self.args = args
|
||||
self.as_module = as_module
|
||||
|
|
@ -142,8 +147,8 @@ class PyRunner:
|
|||
elif os.path.isdir(self.arg0):
|
||||
# Running a directory means running the __main__.py file in that
|
||||
# directory.
|
||||
for ext in [".py", ".pyc", ".pyo"]:
|
||||
try_filename = os.path.join(self.arg0, "__main__" + ext)
|
||||
for ext in ['.py', '.pyc', '.pyo']:
|
||||
try_filename = os.path.join(self.arg0, '__main__' + ext)
|
||||
# 3.8.10 changed how files are reported when running a
|
||||
# directory. But I'm not sure how far this change is going to
|
||||
# spread, so I'll just hard-code it here for now.
|
||||
|
|
@ -157,12 +162,12 @@ class PyRunner:
|
|||
|
||||
# Make a spec. I don't know if this is the right way to do it.
|
||||
try_filename = python_reported_file(try_filename)
|
||||
self.spec = importlib.machinery.ModuleSpec("__main__", None, origin=try_filename)
|
||||
self.spec = importlib.machinery.ModuleSpec('__main__', None, origin=try_filename)
|
||||
self.spec.has_location = True
|
||||
self.package = ""
|
||||
self.loader = DummyLoader("__main__")
|
||||
self.package = ''
|
||||
self.loader = DummyLoader('__main__')
|
||||
else:
|
||||
self.loader = DummyLoader("__main__")
|
||||
self.loader = DummyLoader('__main__')
|
||||
|
||||
self.arg0 = python_reported_file(self.arg0)
|
||||
|
||||
|
|
@ -172,9 +177,9 @@ class PyRunner:
|
|||
self._prepare2()
|
||||
|
||||
# Create a module to serve as __main__
|
||||
main_mod = ModuleType("__main__")
|
||||
main_mod = ModuleType('__main__')
|
||||
|
||||
from_pyc = self.arg0.endswith((".pyc", ".pyo"))
|
||||
from_pyc = self.arg0.endswith(('.pyc', '.pyo'))
|
||||
main_mod.__file__ = self.arg0
|
||||
if from_pyc:
|
||||
main_mod.__file__ = main_mod.__file__[:-1]
|
||||
|
|
@ -184,9 +189,9 @@ class PyRunner:
|
|||
if self.spec is not None:
|
||||
main_mod.__spec__ = self.spec
|
||||
|
||||
main_mod.__builtins__ = sys.modules["builtins"] # type: ignore[attr-defined]
|
||||
main_mod.__builtins__ = sys.modules['builtins'] # type: ignore[attr-defined]
|
||||
|
||||
sys.modules["__main__"] = main_mod
|
||||
sys.modules['__main__'] = main_mod
|
||||
|
||||
# Set sys.argv properly.
|
||||
sys.argv = self.args
|
||||
|
|
@ -228,7 +233,7 @@ class PyRunner:
|
|||
# is non-None when the exception is reported at the upper layer,
|
||||
# and a nested exception is shown to the user. This getattr fixes
|
||||
# it somehow? https://bitbucket.org/pypy/pypy/issue/1903
|
||||
getattr(err, "__context__", None)
|
||||
getattr(err, '__context__', None)
|
||||
|
||||
# Call the excepthook.
|
||||
try:
|
||||
|
|
@ -240,7 +245,7 @@ class PyRunner:
|
|||
except Exception as exc:
|
||||
# Getting the output right in the case of excepthook
|
||||
# shenanigans is kind of involved.
|
||||
sys.stderr.write("Error in sys.excepthook:\n")
|
||||
sys.stderr.write('Error in sys.excepthook:\n')
|
||||
typ2, err2, tb2 = sys.exc_info()
|
||||
assert typ2 is not None
|
||||
assert err2 is not None
|
||||
|
|
@ -249,7 +254,7 @@ class PyRunner:
|
|||
assert err2.__traceback__ is not None
|
||||
err2.__traceback__ = err2.__traceback__.tb_next
|
||||
sys.__excepthook__(typ2, err2, tb2.tb_next)
|
||||
sys.stderr.write("\nOriginal exception was:\n")
|
||||
sys.stderr.write('\nOriginal exception was:\n')
|
||||
raise _ExceptionDuringRun(typ, err, tb.tb_next) from exc
|
||||
else:
|
||||
sys.exit(1)
|
||||
|
|
@ -294,13 +299,13 @@ def make_code_from_py(filename: str) -> CodeType:
|
|||
except (OSError, NoSource) as exc:
|
||||
raise NoSource(f"No file to run: '{filename}'") from exc
|
||||
|
||||
return compile(source, filename, "exec", dont_inherit=True)
|
||||
return compile(source, filename, 'exec', dont_inherit=True)
|
||||
|
||||
|
||||
def make_code_from_pyc(filename: str) -> CodeType:
|
||||
"""Get a code object from a .pyc file."""
|
||||
try:
|
||||
fpyc = open(filename, "rb")
|
||||
fpyc = open(filename, 'rb')
|
||||
except OSError as exc:
|
||||
raise NoCode(f"No file to run: '{filename}'") from exc
|
||||
|
||||
|
|
@ -309,9 +314,9 @@ def make_code_from_pyc(filename: str) -> CodeType:
|
|||
# match or we won't run the file.
|
||||
magic = fpyc.read(4)
|
||||
if magic != PYC_MAGIC_NUMBER:
|
||||
raise NoCode(f"Bad magic number in .pyc file: {magic!r} != {PYC_MAGIC_NUMBER!r}")
|
||||
raise NoCode(f'Bad magic number in .pyc file: {magic!r} != {PYC_MAGIC_NUMBER!r}')
|
||||
|
||||
flags = struct.unpack("<L", fpyc.read(4))[0]
|
||||
flags = struct.unpack('<L', fpyc.read(4))[0]
|
||||
hash_based = flags & 0x01
|
||||
if hash_based:
|
||||
fpyc.read(8) # Skip the hash.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue