Skip to content

Commit 1471164

Browse files
asottilenicoddemus
andauthored
Replace usages of py.log by custom implementation (#822)
Co-authored-by: Bruno Oliveira <[email protected]>
1 parent 80130d5 commit 1471164

File tree

9 files changed

+35
-17
lines changed

9 files changed

+35
-17
lines changed

changelog/822.trivial.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Replace internal usage of ``py.log`` with a custom solution (but with the same interface).

src/xdist/dsession.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import py
21
import pytest
32

3+
from xdist.remote import Producer
44
from xdist.workermanage import NodeManager
55
from xdist.scheduler import (
66
EachScheduling,
@@ -34,9 +34,7 @@ class DSession:
3434

3535
def __init__(self, config):
3636
self.config = config
37-
self.log = py.log.Producer("dsession")
38-
if not config.option.debug:
39-
py.log.setconsumer(self.log._keywords, None)
37+
self.log = Producer("dsession", enabled=config.option.debug)
4038
self.nodemanager = None
4139
self.sched = None
4240
self.shuttingdown = False

src/xdist/remote.py

+25-4
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
import sys
1010
import os
1111
import time
12+
from typing import Any
1213

13-
import py
1414
import pytest
1515
from execnet.gateway_base import dumps, DumpError
1616

@@ -24,6 +24,29 @@ def setproctitle(title):
2424
pass
2525

2626

27+
class Producer:
28+
"""
29+
Simplified implementation of the same interface as py.log, for backward compatibility
30+
since we dropped the dependency on pylib.
31+
Note: this is defined here because this module can't depend on xdist, so we need
32+
to have the other way around.
33+
"""
34+
35+
def __init__(self, name: str, *, enabled: bool = True):
36+
self.name = name
37+
self.enabled = enabled
38+
39+
def __repr__(self) -> str:
40+
return f"{type(self).__name__}({self.name!r}, enabled={self.enabled})"
41+
42+
def __call__(self, *a: Any, **k: Any) -> None:
43+
if self.enabled:
44+
print(f"[{self.name}]", *a, **k, file=sys.stderr)
45+
46+
def __getattr__(self, name: str) -> "Producer":
47+
return type(self)(name, enabled=self.enabled)
48+
49+
2750
def worker_title(title):
2851
try:
2952
setproctitle(title)
@@ -37,9 +60,7 @@ def __init__(self, config, channel):
3760
self.config = config
3861
self.workerid = config.workerinput.get("workerid", "?")
3962
self.testrunuid = config.workerinput["testrunuid"]
40-
self.log = py.log.Producer("worker-%s" % self.workerid)
41-
if not config.option.debug:
42-
py.log.setconsumer(self.log._keywords, None)
63+
self.log = Producer(f"worker-{self.workerid}", enabled=config.option.debug)
4364
self.channel = channel
4465
config.pluginmanager.register(self)
4566

src/xdist/scheduler/each.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
from py.log import Producer
2-
1+
from xdist.remote import Producer
32
from xdist.workermanage import parse_spec_config
43
from xdist.report import report_collection_diff
54

src/xdist/scheduler/load.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
from itertools import cycle
22

3-
from py.log import Producer
43
from _pytest.runner import CollectReport
54

5+
from xdist.remote import Producer
66
from xdist.workermanage import parse_spec_config
77
from xdist.report import report_collection_diff
88

src/xdist/scheduler/loadfile.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from .loadscope import LoadScopeScheduling
2-
from py.log import Producer
2+
from xdist.remote import Producer
33

44

55
class LoadFileScheduling(LoadScopeScheduling):

src/xdist/scheduler/loadgroup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from .loadscope import LoadScopeScheduling
2-
from py.log import Producer
2+
from xdist.remote import Producer
33

44

55
class LoadGroupScheduling(LoadScopeScheduling):

src/xdist/scheduler/loadscope.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from collections import OrderedDict
22

33
from _pytest.runner import CollectReport
4-
from py.log import Producer
4+
from xdist.remote import Producer
55
from xdist.report import report_collection_diff
66
from xdist.workermanage import parse_spec_config
77

src/xdist/workermanage.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import execnet
1212

1313
import xdist.remote
14+
from xdist.remote import Producer
1415
from xdist.plugin import _sys_path
1516

1617

@@ -246,9 +247,7 @@ def __init__(self, nodemanager, gateway, config, putevent):
246247
}
247248
self._down = False
248249
self._shutdown_sent = False
249-
self.log = py.log.Producer("workerctl-%s" % gateway.id)
250-
if not self.config.option.debug:
251-
py.log.setconsumer(self.log._keywords, None)
250+
self.log = Producer(f"workerctl-{gateway.id}", enabled=config.option.debug)
252251

253252
def __repr__(self):
254253
return "<{} {}>".format(self.__class__.__name__, self.gateway.id)

0 commit comments

Comments
 (0)