From 3f0fbf185822fa8265c688c97b68f03230fc468f Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Fri, 21 Oct 2022 15:33:11 -0700 Subject: [PATCH 1/3] replace usages of py.log --- changelog/822.trivial.rst | 1 + src/xdist/dsession.py | 6 ++---- src/xdist/remote.py | 24 ++++++++++++++++++++---- src/xdist/scheduler/each.py | 3 +-- src/xdist/scheduler/load.py | 2 +- src/xdist/scheduler/loadfile.py | 2 +- src/xdist/scheduler/loadgroup.py | 2 +- src/xdist/scheduler/loadscope.py | 2 +- src/xdist/workermanage.py | 5 ++--- 9 files changed, 30 insertions(+), 17 deletions(-) create mode 100644 changelog/822.trivial.rst diff --git a/changelog/822.trivial.rst b/changelog/822.trivial.rst new file mode 100644 index 00000000..ad7a882b --- /dev/null +++ b/changelog/822.trivial.rst @@ -0,0 +1 @@ +Replace internal usage of ``py.log``. diff --git a/src/xdist/dsession.py b/src/xdist/dsession.py index a950df62..ab8332f7 100644 --- a/src/xdist/dsession.py +++ b/src/xdist/dsession.py @@ -1,6 +1,6 @@ -import py import pytest +from xdist.remote import Producer from xdist.workermanage import NodeManager from xdist.scheduler import ( EachScheduling, @@ -34,9 +34,7 @@ class DSession: def __init__(self, config): self.config = config - self.log = py.log.Producer("dsession") - if not config.option.debug: - py.log.setconsumer(self.log._keywords, None) + self.log = Producer("dsession", enabled=config.option.debug) self.nodemanager = None self.sched = None self.shuttingdown = False diff --git a/src/xdist/remote.py b/src/xdist/remote.py index 160b042a..92063d88 100644 --- a/src/xdist/remote.py +++ b/src/xdist/remote.py @@ -9,8 +9,8 @@ import sys import os import time +from typing import Any -import py import pytest from execnet.gateway_base import dumps, DumpError @@ -24,6 +24,24 @@ def setproctitle(title): pass +class Producer: + """this is defined here because this module can't depend on xdist""" + + def __init__(self, name: str, *, enabled: bool = True): + self.name = name + self.enabled = enabled + + def __repr__(self) -> str: + return f"{type(self).__name__}({self.name!r}, enabled={self.enabled})" + + def __call__(self, *a: Any, **k: Any) -> None: + if self.enabled: + print(f"[{self.name}]", *a, **k, file=sys.stderr) + + def __getattr__(self, name: str) -> "Producer": + return type(self)(name, enabled=self.enabled) + + def worker_title(title): try: setproctitle(title) @@ -37,9 +55,7 @@ def __init__(self, config, channel): self.config = config self.workerid = config.workerinput.get("workerid", "?") self.testrunuid = config.workerinput["testrunuid"] - self.log = py.log.Producer("worker-%s" % self.workerid) - if not config.option.debug: - py.log.setconsumer(self.log._keywords, None) + self.log = Producer(f"worker-{self.workerid}", enabled=config.option.debug) self.channel = channel config.pluginmanager.register(self) diff --git a/src/xdist/scheduler/each.py b/src/xdist/scheduler/each.py index cfe99e7d..45791022 100644 --- a/src/xdist/scheduler/each.py +++ b/src/xdist/scheduler/each.py @@ -1,5 +1,4 @@ -from py.log import Producer - +from xdist.remote import Producer from xdist.workermanage import parse_spec_config from xdist.report import report_collection_diff diff --git a/src/xdist/scheduler/load.py b/src/xdist/scheduler/load.py index f3bef40b..11d5309e 100644 --- a/src/xdist/scheduler/load.py +++ b/src/xdist/scheduler/load.py @@ -1,8 +1,8 @@ from itertools import cycle -from py.log import Producer from _pytest.runner import CollectReport +from xdist.remote import Producer from xdist.workermanage import parse_spec_config from xdist.report import report_collection_diff diff --git a/src/xdist/scheduler/loadfile.py b/src/xdist/scheduler/loadfile.py index 867a94ec..91b59384 100644 --- a/src/xdist/scheduler/loadfile.py +++ b/src/xdist/scheduler/loadfile.py @@ -1,5 +1,5 @@ from .loadscope import LoadScopeScheduling -from py.log import Producer +from xdist.remote import Producer class LoadFileScheduling(LoadScopeScheduling): diff --git a/src/xdist/scheduler/loadgroup.py b/src/xdist/scheduler/loadgroup.py index 072f64ab..ecefa490 100644 --- a/src/xdist/scheduler/loadgroup.py +++ b/src/xdist/scheduler/loadgroup.py @@ -1,5 +1,5 @@ from .loadscope import LoadScopeScheduling -from py.log import Producer +from xdist.remote import Producer class LoadGroupScheduling(LoadScopeScheduling): diff --git a/src/xdist/scheduler/loadscope.py b/src/xdist/scheduler/loadscope.py index 69d9d9a2..fabe1eba 100644 --- a/src/xdist/scheduler/loadscope.py +++ b/src/xdist/scheduler/loadscope.py @@ -1,7 +1,7 @@ from collections import OrderedDict from _pytest.runner import CollectReport -from py.log import Producer +from xdist.remote import Producer from xdist.report import report_collection_diff from xdist.workermanage import parse_spec_config diff --git a/src/xdist/workermanage.py b/src/xdist/workermanage.py index 504a7dec..10f1a481 100644 --- a/src/xdist/workermanage.py +++ b/src/xdist/workermanage.py @@ -11,6 +11,7 @@ import execnet import xdist.remote +from xdist.remote import Producer from xdist.plugin import _sys_path @@ -246,9 +247,7 @@ def __init__(self, nodemanager, gateway, config, putevent): } self._down = False self._shutdown_sent = False - self.log = py.log.Producer("workerctl-%s" % gateway.id) - if not self.config.option.debug: - py.log.setconsumer(self.log._keywords, None) + self.log = Producer(f"workerctl-{gateway.id}", enabled=config.option.debug) def __repr__(self): return "<{} {}>".format(self.__class__.__name__, self.gateway.id) From 7b41e26f3ba4f22745f3103e13401ce7f109daab Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Fri, 21 Oct 2022 21:29:31 -0300 Subject: [PATCH 2/3] Apply suggestions from code review --- changelog/822.trivial.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog/822.trivial.rst b/changelog/822.trivial.rst index ad7a882b..844453ad 100644 --- a/changelog/822.trivial.rst +++ b/changelog/822.trivial.rst @@ -1 +1 @@ -Replace internal usage of ``py.log``. +Replace internal usage of ``py.log`` with a custom solution (but with the same interface). From bb24ab9ca89b421424d25edf0bbf926ef803eaf2 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Sat, 22 Oct 2022 07:13:44 -0300 Subject: [PATCH 3/3] Update src/xdist/remote.py --- src/xdist/remote.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/xdist/remote.py b/src/xdist/remote.py index 92063d88..edf6ae35 100644 --- a/src/xdist/remote.py +++ b/src/xdist/remote.py @@ -25,7 +25,12 @@ def setproctitle(title): class Producer: - """this is defined here because this module can't depend on xdist""" + """ + Simplified implementation of the same interface as py.log, for backward compatibility + since we dropped the dependency on pylib. + Note: this is defined here because this module can't depend on xdist, so we need + to have the other way around. + """ def __init__(self, name: str, *, enabled: bool = True): self.name = name