Skip to content

Commit

Permalink
Replace usages of py.log by custom implementation (#822)
Browse files Browse the repository at this point in the history
Co-authored-by: Bruno Oliveira <nicoddemus@gmail.com>
  • Loading branch information
asottile and nicoddemus committed Oct 22, 2022
1 parent 80130d5 commit 1471164
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 17 deletions.
1 change: 1 addition & 0 deletions changelog/822.trivial.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Replace internal usage of ``py.log`` with a custom solution (but with the same interface).
6 changes: 2 additions & 4 deletions src/xdist/dsession.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import py
import pytest

from xdist.remote import Producer
from xdist.workermanage import NodeManager
from xdist.scheduler import (
EachScheduling,
Expand Down Expand Up @@ -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
Expand Down
29 changes: 25 additions & 4 deletions src/xdist/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -24,6 +24,29 @@ def setproctitle(title):
pass


class Producer:
"""
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
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)
Expand All @@ -37,9 +60,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)

Expand Down
3 changes: 1 addition & 2 deletions src/xdist/scheduler/each.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/xdist/scheduler/load.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/xdist/scheduler/loadfile.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .loadscope import LoadScopeScheduling
from py.log import Producer
from xdist.remote import Producer


class LoadFileScheduling(LoadScopeScheduling):
Expand Down
2 changes: 1 addition & 1 deletion src/xdist/scheduler/loadgroup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .loadscope import LoadScopeScheduling
from py.log import Producer
from xdist.remote import Producer


class LoadGroupScheduling(LoadScopeScheduling):
Expand Down
2 changes: 1 addition & 1 deletion src/xdist/scheduler/loadscope.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
5 changes: 2 additions & 3 deletions src/xdist/workermanage.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import execnet

import xdist.remote
from xdist.remote import Producer
from xdist.plugin import _sys_path


Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 1471164

Please sign in to comment.