mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-07 20:26:54 +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,39 +1,33 @@
|
|||
# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
|
||||
# For details: https://github.com/nedbat/coveragepy/blob/master/NOTICE.txt
|
||||
|
||||
"""Callback functions and support for sys.monitoring data collection."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import functools
|
||||
import inspect
|
||||
import os
|
||||
import os.path
|
||||
import sys
|
||||
import threading
|
||||
import traceback
|
||||
|
||||
from dataclasses import dataclass
|
||||
from types import CodeType, FrameType
|
||||
from typing import (
|
||||
Any,
|
||||
Callable,
|
||||
Set,
|
||||
TYPE_CHECKING,
|
||||
cast,
|
||||
)
|
||||
from types import CodeType
|
||||
from types import FrameType
|
||||
from typing import Any
|
||||
from typing import Callable
|
||||
from typing import cast
|
||||
from typing import Set
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from coverage.debug import short_filename, short_stack
|
||||
from coverage.types import (
|
||||
AnyCallable,
|
||||
TArc,
|
||||
TFileDisposition,
|
||||
TLineNo,
|
||||
TTraceData,
|
||||
TTraceFileData,
|
||||
TracerCore,
|
||||
TWarnFn,
|
||||
)
|
||||
from coverage.debug import short_filename
|
||||
from coverage.debug import short_stack
|
||||
from coverage.types import AnyCallable
|
||||
from coverage.types import TArc
|
||||
from coverage.types import TFileDisposition
|
||||
from coverage.types import TLineNo
|
||||
from coverage.types import TracerCore
|
||||
from coverage.types import TTraceData
|
||||
from coverage.types import TTraceFileData
|
||||
from coverage.types import TWarnFn
|
||||
|
||||
# pylint: disable=unused-argument
|
||||
|
||||
|
|
@ -41,7 +35,7 @@ LOG = False
|
|||
|
||||
# This module will be imported in all versions of Python, but only used in 3.12+
|
||||
# It will be type-checked for 3.12, but not for earlier versions.
|
||||
sys_monitoring = getattr(sys, "monitoring", None)
|
||||
sys_monitoring = getattr(sys, 'monitoring', None)
|
||||
|
||||
if TYPE_CHECKING:
|
||||
assert sys_monitoring is not None
|
||||
|
|
@ -61,12 +55,12 @@ if LOG: # pragma: debugging
|
|||
|
||||
def __getattr__(self, name: str) -> Callable[..., Any]:
|
||||
def _wrapped(*args: Any, **kwargs: Any) -> Any:
|
||||
log(f"{self.namespace}.{name}{args}{kwargs}")
|
||||
log(f'{self.namespace}.{name}{args}{kwargs}')
|
||||
return getattr(self.wrapped, name)(*args, **kwargs)
|
||||
|
||||
return _wrapped
|
||||
|
||||
sys_monitoring = LoggingWrapper(sys_monitoring, "sys.monitoring")
|
||||
sys_monitoring = LoggingWrapper(sys_monitoring, 'sys.monitoring')
|
||||
assert sys_monitoring is not None
|
||||
|
||||
short_stack = functools.partial(
|
||||
|
|
@ -80,28 +74,28 @@ if LOG: # pragma: debugging
|
|||
# Make a shorter number more likely to be unique.
|
||||
pid = os.getpid()
|
||||
tid = cast(int, threading.current_thread().ident)
|
||||
tslug = f"{(pid * tid) % 9_999_991:07d}"
|
||||
tslug = f'{(pid * tid) % 9_999_991:07d}'
|
||||
if tid not in seen_threads:
|
||||
seen_threads.add(tid)
|
||||
log(f"New thread {tid} {tslug}:\n{short_stack()}")
|
||||
log(f'New thread {tid} {tslug}:\n{short_stack()}')
|
||||
# log_seq = int(os.getenv("PANSEQ", "0"))
|
||||
# root = f"/tmp/pan.{log_seq:03d}"
|
||||
for filename in [
|
||||
"/tmp/foo.out",
|
||||
'/tmp/foo.out',
|
||||
# f"{root}.out",
|
||||
# f"{root}-{pid}.out",
|
||||
# f"{root}-{pid}-{tslug}.out",
|
||||
]:
|
||||
with open(filename, "a") as f:
|
||||
print(f"{pid}:{tslug}: {msg}", file=f, flush=True)
|
||||
with open(filename, 'a') as f:
|
||||
print(f'{pid}:{tslug}: {msg}', file=f, flush=True)
|
||||
|
||||
def arg_repr(arg: Any) -> str:
|
||||
"""Make a customized repr for logged values."""
|
||||
if isinstance(arg, CodeType):
|
||||
return (
|
||||
f"<code @{id(arg):#x}"
|
||||
+ f" name={arg.co_name},"
|
||||
+ f" file={short_filename(arg.co_filename)!r}#{arg.co_firstlineno}>"
|
||||
f'<code @{id(arg):#x}' +
|
||||
f' name={arg.co_name},' +
|
||||
f' file={short_filename(arg.co_filename)!r}#{arg.co_firstlineno}>'
|
||||
)
|
||||
return repr(arg)
|
||||
|
||||
|
|
@ -117,20 +111,20 @@ if LOG: # pragma: debugging
|
|||
for name, arg in zip(names, args):
|
||||
if name is None:
|
||||
continue
|
||||
args_reprs.append(f"{name}={arg_repr(arg)}")
|
||||
args_reprs.append(f'{name}={arg_repr(arg)}')
|
||||
log(f"{id(self):#x}:{method.__name__}({', '.join(args_reprs)})")
|
||||
ret = method(self, *args)
|
||||
# log(f" end {id(self):#x}:{method.__name__}({', '.join(args_reprs)})")
|
||||
return ret
|
||||
except Exception as exc:
|
||||
log(f"!!{exc.__class__.__name__}: {exc}")
|
||||
log("".join(traceback.format_exception(exc))) # pylint: disable=[no-value-for-parameter]
|
||||
log(f'!!{exc.__class__.__name__}: {exc}')
|
||||
log(''.join(traceback.format_exception(exc))) # pylint: disable=[no-value-for-parameter]
|
||||
try:
|
||||
assert sys_monitoring is not None
|
||||
sys_monitoring.set_events(sys.monitoring.COVERAGE_ID, 0)
|
||||
except ValueError:
|
||||
# We might have already shut off monitoring.
|
||||
log("oops, shutting off events with disabled tool id")
|
||||
log('oops, shutting off events with disabled tool id')
|
||||
raise
|
||||
|
||||
return _wrapped
|
||||
|
|
@ -202,7 +196,7 @@ class SysMonitor(TracerCore):
|
|||
self.sysmon_on = False
|
||||
|
||||
self.stats = {
|
||||
"starts": 0,
|
||||
'starts': 0,
|
||||
}
|
||||
|
||||
self.stopped = False
|
||||
|
|
@ -211,7 +205,7 @@ class SysMonitor(TracerCore):
|
|||
def __repr__(self) -> str:
|
||||
points = sum(len(v) for v in self.data.values())
|
||||
files = len(self.data)
|
||||
return f"<SysMonitor at {id(self):#x}: {points} data points in {files} files>"
|
||||
return f'<SysMonitor at {id(self):#x}: {points} data points in {files} files>'
|
||||
|
||||
@panopticon()
|
||||
def start(self) -> None:
|
||||
|
|
@ -219,7 +213,7 @@ class SysMonitor(TracerCore):
|
|||
self.stopped = False
|
||||
|
||||
assert sys_monitoring is not None
|
||||
sys_monitoring.use_tool_id(self.myid, "coverage.py")
|
||||
sys_monitoring.use_tool_id(self.myid, 'coverage.py')
|
||||
register = functools.partial(sys_monitoring.register_callback, self.myid)
|
||||
events = sys_monitoring.events
|
||||
if self.trace_arcs:
|
||||
|
|
@ -286,12 +280,12 @@ class SysMonitor(TracerCore):
|
|||
"""Get the frame of the Python code we're monitoring."""
|
||||
return inspect.currentframe().f_back.f_back # type: ignore[union-attr,return-value]
|
||||
|
||||
@panopticon("code", "@")
|
||||
@panopticon('code', '@')
|
||||
def sysmon_py_start(self, code: CodeType, instruction_offset: int) -> MonitorReturn:
|
||||
"""Handle sys.monitoring.events.PY_START events."""
|
||||
# Entering a new frame. Decide if we should trace in this file.
|
||||
self._activity = True
|
||||
self.stats["starts"] += 1
|
||||
self.stats['starts'] += 1
|
||||
|
||||
code_info = self.code_infos.get(id(code))
|
||||
tracing_code: bool | None = None
|
||||
|
|
@ -337,11 +331,11 @@ class SysMonitor(TracerCore):
|
|||
sys_monitoring.set_local_events(
|
||||
self.myid,
|
||||
code,
|
||||
events.PY_RETURN
|
||||
events.PY_RETURN |
|
||||
#
|
||||
| events.PY_RESUME
|
||||
events.PY_RESUME |
|
||||
# | events.PY_YIELD
|
||||
| events.LINE,
|
||||
events.LINE,
|
||||
# | events.BRANCH
|
||||
# | events.JUMP
|
||||
)
|
||||
|
|
@ -354,7 +348,7 @@ class SysMonitor(TracerCore):
|
|||
else:
|
||||
return sys.monitoring.DISABLE
|
||||
|
||||
@panopticon("code", "@")
|
||||
@panopticon('code', '@')
|
||||
def sysmon_py_resume_arcs(
|
||||
self, code: CodeType, instruction_offset: int,
|
||||
) -> MonitorReturn:
|
||||
|
|
@ -362,7 +356,7 @@ class SysMonitor(TracerCore):
|
|||
frame = self.callers_frame()
|
||||
self.last_lines[frame] = frame.f_lineno
|
||||
|
||||
@panopticon("code", "@", None)
|
||||
@panopticon('code', '@', None)
|
||||
def sysmon_py_return_arcs(
|
||||
self, code: CodeType, instruction_offset: int, retval: object,
|
||||
) -> MonitorReturn:
|
||||
|
|
@ -379,7 +373,7 @@ class SysMonitor(TracerCore):
|
|||
# Leaving this function, no need for the frame any more.
|
||||
self.last_lines.pop(frame, None)
|
||||
|
||||
@panopticon("code", "@", "exc")
|
||||
@panopticon('code', '@', 'exc')
|
||||
def sysmon_py_unwind_arcs(
|
||||
self, code: CodeType, instruction_offset: int, exception: BaseException,
|
||||
) -> MonitorReturn:
|
||||
|
|
@ -397,8 +391,7 @@ class SysMonitor(TracerCore):
|
|||
# log(f"adding {arc=}")
|
||||
cast(Set[TArc], code_info.file_data).add(arc)
|
||||
|
||||
|
||||
@panopticon("code", "line")
|
||||
@panopticon('code', 'line')
|
||||
def sysmon_line_lines(self, code: CodeType, line_number: int) -> MonitorReturn:
|
||||
"""Handle sys.monitoring.events.LINE events for line coverage."""
|
||||
code_info = self.code_infos[id(code)]
|
||||
|
|
@ -407,7 +400,7 @@ class SysMonitor(TracerCore):
|
|||
# log(f"adding {line_number=}")
|
||||
return sys.monitoring.DISABLE
|
||||
|
||||
@panopticon("code", "line")
|
||||
@panopticon('code', 'line')
|
||||
def sysmon_line_arcs(self, code: CodeType, line_number: int) -> MonitorReturn:
|
||||
"""Handle sys.monitoring.events.LINE events for branch coverage."""
|
||||
code_info = self.code_infos[id(code)]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue