Skip to content

Commit 059c1bc

Browse files
committed
remote,workermanage: use enums for markers
Enum has a unique type, unlike `object()`, enabling better typing.
1 parent ee0b09c commit 059c1bc

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

src/xdist/remote.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"""
88

99
import contextlib
10+
import enum
1011
import os
1112
import sys
1213
import time
@@ -57,10 +58,12 @@ def worker_title(title):
5758
pass
5859

5960

60-
class WorkerInteractor:
61-
SHUTDOWN_MARK = object()
62-
QUEUE_REPLACED_MARK = object()
61+
class Marker(enum.Enum):
62+
SHUTDOWN = 0
63+
QUEUE_REPLACED = 1
64+
6365

66+
class WorkerInteractor:
6467
def __init__(self, config, channel):
6568
self.config = config
6669
self.workerid = config.workerinput.get("workerid", "?")
@@ -79,7 +82,7 @@ def _get_next_item_index(self):
7982
is replaced concurrently in another thread.
8083
"""
8184
result = self.torun.get()
82-
while result is self.QUEUE_REPLACED_MARK:
85+
while result is Marker.QUEUE_REPLACED:
8386
result = self.torun.get()
8487
return result
8588

@@ -114,8 +117,8 @@ def pytest_collection(self, session):
114117
self.sendevent("collectionstart")
115118

116119
def handle_command(self, command):
117-
if command is self.SHUTDOWN_MARK:
118-
self.torun.put(self.SHUTDOWN_MARK)
120+
if command is Marker.SHUTDOWN:
121+
self.torun.put(Marker.SHUTDOWN)
119122
return
120123

121124
name, kwargs = command
@@ -128,7 +131,7 @@ def handle_command(self, command):
128131
for i in range(len(self.session.items)):
129132
self.torun.put(i)
130133
elif name == "shutdown":
131-
self.torun.put(self.SHUTDOWN_MARK)
134+
self.torun.put(Marker.SHUTDOWN)
132135
elif name == "steal":
133136
self.steal(kwargs["indices"])
134137

@@ -149,14 +152,14 @@ def old_queue_get_nowait_noraise():
149152
self.torun.put(i)
150153

151154
self.sendevent("unscheduled", indices=stolen)
152-
old_queue.put(self.QUEUE_REPLACED_MARK)
155+
old_queue.put(Marker.QUEUE_REPLACED)
153156

154157
@pytest.hookimpl
155158
def pytest_runtestloop(self, session):
156159
self.log("entering main loop")
157-
self.channel.setcallback(self.handle_command, endmarker=self.SHUTDOWN_MARK)
160+
self.channel.setcallback(self.handle_command, endmarker=Marker.SHUTDOWN)
158161
self.nextitem_index = self._get_next_item_index()
159-
while self.nextitem_index is not self.SHUTDOWN_MARK:
162+
while self.nextitem_index is not Marker.SHUTDOWN:
160163
self.run_one_test()
161164
if session.shouldfail or session.shouldstop:
162165
break
@@ -168,7 +171,7 @@ def run_one_test(self):
168171

169172
items = self.session.items
170173
item = items[self.item_index]
171-
if self.nextitem_index is self.SHUTDOWN_MARK:
174+
if self.nextitem_index is Marker.SHUTDOWN:
172175
nextitem = None
173176
else:
174177
nextitem = items[self.nextitem_index]

src/xdist/workermanage.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from __future__ import annotations
2+
3+
import enum
14
import fnmatch
25
import os
36
from pathlib import Path
@@ -230,9 +233,11 @@ def make_reltoroot(roots: Sequence[Path], args: List[str]) -> List[str]:
230233
return result
231234

232235

233-
class WorkerController:
234-
ENDMARK = -1
236+
class Marker(enum.Enum):
237+
END = -1
235238

239+
240+
class WorkerController:
236241
class RemoteHook:
237242
@pytest.hookimpl(trylast=True)
238243
def pytest_xdist_getremotemodule(self):
@@ -283,7 +288,7 @@ def setup(self):
283288
self.channel.send((self.workerinput, args, option_dict, change_sys_path))
284289

285290
if self.putevent:
286-
self.channel.setcallback(self.process_from_remote, endmarker=self.ENDMARK)
291+
self.channel.setcallback(self.process_from_remote, endmarker=Marker.END)
287292

288293
def ensure_teardown(self):
289294
if hasattr(self, "channel"):
@@ -331,7 +336,7 @@ def process_from_remote(self, eventcall):
331336
avoid raising exceptions or doing heavy work.
332337
"""
333338
try:
334-
if eventcall == self.ENDMARK:
339+
if eventcall is Marker.END:
335340
err = self.channel._getremoteerror()
336341
if not self._down:
337342
if not err or isinstance(err, EOFError):

0 commit comments

Comments
 (0)