Skip to content

Commit 275bd60

Browse files
committed
doc,testing: drop use of py & tmpdir
Use modern alternatives instead.
1 parent aeb39fa commit 275bd60

14 files changed

+263
-214
lines changed

CHANGELOG.rst

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* minimal mypy fixes and python2 support code drop
77
* migrate packaging to hatch
88
* drop deprecated apis of old makegateway names
9+
* Removed ``py`` testing dependency
910

1011

1112

doc/example/conftest.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1+
import pathlib
12
import sys
23

3-
import py
44

5-
# make execnet and example code importable
6-
cand = py.path.local(__file__).dirpath().dirpath().dirpath()
7-
if cand.join("execnet", "__init__.py").check():
5+
# Make execnet and example code importable.
6+
cand = pathlib.Path(__file__).parent.parent.parent
7+
if cand.joinpath("execnet", "__init__.py").exists():
88
if str(cand) not in sys.path:
99
sys.path.insert(0, str(cand))
10-
cand = py.path.local(__file__).dirpath()
10+
cand = pathlib.Path(__file__).parent
1111
if str(cand) not in sys.path:
1212
sys.path.insert(0, str(cand))
1313

doc/example/svn-sync-repo.py

+20-8
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
77
"""
88
import os
9+
import pathlib
10+
import subprocess
911
import sys
1012

1113
import execnet
12-
import py
1314

1415

1516
def usage():
@@ -19,8 +20,8 @@ def usage():
1920

2021
def main(args):
2122
remote = args[0]
22-
localrepo = py.path.local(args[1])
23-
if not localrepo.check(dir=1):
23+
localrepo = pathlib.Path(args[1])
24+
if not localrepo.is_dir():
2425
raise SystemExit(f"localrepo {localrepo} does not exist")
2526
if len(args) == 3:
2627
configfile = args[2]
@@ -39,12 +40,18 @@ def main(args):
3940
# 4. client goes back to step 1
4041
c = gw.remote_exec(
4142
"""
42-
import py
4343
import os
44+
import subprocess
4445
import time
46+
4547
remote_rev, repopath = channel.receive()
46-
while 1:
47-
rev = py.process.cmdexec('svnlook youngest "%s"' % repopath)
48+
while True:
49+
rev = subprocess.run(
50+
["svnlook", "youngest", repopath],
51+
check=True,
52+
capture_output=True,
53+
text=True,
54+
).stdout
4855
rev = int(rev)
4956
if rev > remote_rev:
5057
revrange = (remote_rev+1, rev)
@@ -103,12 +110,17 @@ def svn_load(repo, dumpchannel, maxcount=100):
103110
if count <= 0:
104111
dumpchannel.send(maxcount)
105112
count = maxcount
106-
print >> sys.stdout
113+
print()
107114
f.close()
108115

109116

110117
def get_svn_youngest(repo):
111-
rev = py.process.cmdexec('svnlook youngest "%s"' % repo)
118+
rev = subprocess.run(
119+
["svnlook", "youngest", repo],
120+
check=True,
121+
capture_output=True,
122+
text=True,
123+
).stdout
112124
return int(rev)
113125

114126

doc/example/sysinfo.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
(c) Holger Krekel, MIT license
77
"""
88
import optparse
9+
import pathlib
910
import re
1011
import sys
1112

1213
import execnet
13-
import py
1414

1515

1616
parser = optparse.OptionParser(usage=__doc__)
@@ -34,14 +34,15 @@
3434

3535

3636
def parsehosts(path):
37-
path = py.path.local(path)
37+
path = pathlib.Path(path)
3838
l = []
3939
rex = re.compile(r"Host\s*(\S+)")
40-
for line in path.readlines():
41-
m = rex.match(line)
42-
if m is not None:
43-
(sshname,) = m.groups()
44-
l.append(sshname)
40+
with path.open() as f:
41+
for line in f:
42+
m = rex.match(line)
43+
if m is not None:
44+
(sshname,) = m.groups()
45+
l.append(sshname)
4546
return l
4647

4748

testing/conftest.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
import pathlib
2+
import shutil
13
import subprocess
24
import sys
35

46
import execnet
5-
import py
67
import pytest
78
from execnet.gateway_base import get_execmodel
89
from execnet.gateway_base import WorkerPool
@@ -76,7 +77,7 @@ def getspecssh(config):
7677
xspecs = getgspecs(config)
7778
for spec in xspecs:
7879
if spec.ssh:
79-
if not py.path.local.sysfind("ssh"):
80+
if not shutil.which("ssh"):
8081
pytest.skip("command not found: ssh")
8182
return spec
8283
pytest.skip("need '--gx ssh=...'")
@@ -113,8 +114,9 @@ def getexecutable(name, cache={}):
113114
return cache[name]
114115
except KeyError:
115116
if name == "sys.executable":
116-
return py.path.local(sys.executable)
117-
executable = py.path.local.sysfind(name)
117+
return pathlib.Path(sys.executable)
118+
path = shutil.which(name)
119+
executable = pathlib.Path(path) if path is not None else None
118120
if executable:
119121
if name == "jython":
120122
popen = subprocess.Popen(

testing/test_basics.py

+56-52
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
import os
33
import subprocess
44
import sys
5+
import textwrap
56
from io import BytesIO
67

78
import execnet
8-
import py
99
import pytest
1010
from execnet import gateway
1111
from execnet import gateway_base
@@ -29,20 +29,20 @@ def test_serializer_api(self, val):
2929
val2 = execnet.loads(dumped)
3030
assert val == val2
3131

32-
def test_mmap(self, tmpdir, val):
32+
def test_mmap(self, tmp_path, val):
3333
mmap = pytest.importorskip("mmap").mmap
34-
p = tmpdir.join("data")
34+
p = tmp_path / "data"
3535
with p.open("wb") as f:
3636
f.write(execnet.dumps(val))
37-
f = p.open("r+b")
38-
m = mmap(f.fileno(), 0)
39-
val2 = execnet.load(m)
37+
with p.open("r+b") as f:
38+
m = mmap(f.fileno(), 0)
39+
val2 = execnet.load(m)
4040
assert val == val2
4141

4242
def test_bytesio(self, val):
43-
f = py.io.BytesIO()
43+
f = BytesIO()
4444
execnet.dump(f, val)
45-
read = py.io.BytesIO(f.getvalue())
45+
read = BytesIO(f.getvalue())
4646
val2 = execnet.load(read)
4747
assert val == val2
4848

@@ -81,10 +81,8 @@ def receive():
8181
return popen.stdout.readline()
8282

8383
try:
84-
source = py.code.Source(read_write_loop, "read_write_loop()")
85-
repr_source = repr(str(source)) + "\n"
86-
sendline = repr_source
87-
send(sendline)
84+
source = inspect.getsource(read_write_loop) + "read_write_loop()"
85+
send(repr(source) + "\n")
8886
s = receive()
8987
assert s == "ok\n"
9088
send("hello\n")
@@ -114,11 +112,11 @@ def read_write_loop():
114112
break
115113

116114

117-
def test_io_message(anypython, tmpdir, execmodel):
118-
check = tmpdir.join("check.py")
119-
check.write(
120-
py.code.Source(
121-
gateway_base,
115+
def test_io_message(anypython, tmp_path, execmodel):
116+
check = tmp_path / "check.py"
117+
check.write_text(
118+
inspect.getsource(gateway_base)
119+
+ textwrap.dedent(
122120
"""
123121
from io import BytesIO
124122
import tempfile
@@ -146,24 +144,25 @@ def test_io_message(anypython, tmpdir, execmodel):
146144
),
147145
)
148146
)
149-
# out = py.process.cmdexec("%s %s" %(executable,check))
150-
out = anypython.sysexec(check)
147+
out = subprocess.run(
148+
[str(anypython), str(check)], text=True, capture_output=True, check=True
149+
).stdout
151150
print(out)
152151
assert "all passed" in out
153152

154153

155-
def test_popen_io(anypython, tmpdir, execmodel):
156-
check = tmpdir.join("check.py")
157-
check.write(
158-
py.code.Source(
159-
gateway_base,
154+
def test_popen_io(anypython, tmp_path, execmodel):
155+
check = tmp_path / "check.py"
156+
check.write_text(
157+
inspect.getsource(gateway_base)
158+
+ textwrap.dedent(
160159
f"""
161160
io = init_popen_io(get_execmodel({execmodel.backend!r}))
162161
io.write("hello".encode('ascii'))
163162
s = io.read(1)
164163
assert s == "x".encode('ascii')
165-
""",
166-
)
164+
"""
165+
),
167166
)
168167
from subprocess import Popen, PIPE
169168

@@ -191,32 +190,36 @@ def newread(numbytes):
191190
assert result == b"tes"
192191

193192

194-
def test_rinfo_source(anypython, tmpdir):
195-
check = tmpdir.join("check.py")
196-
check.write(
197-
py.code.Source(
193+
def test_rinfo_source(anypython, tmp_path):
194+
check = tmp_path / "check.py"
195+
check.write_text(
196+
textwrap.dedent(
198197
"""
199198
class Channel:
200199
def send(self, data):
201200
assert eval(repr(data), {}) == data
202201
channel = Channel()
203-
""",
204-
gateway.rinfo_source,
202+
"""
203+
)
204+
+ inspect.getsource(gateway.rinfo_source)
205+
+ textwrap.dedent(
205206
"""
206207
print ('all passed')
207-
""",
208+
"""
208209
)
209210
)
210-
out = anypython.sysexec(check)
211+
out = subprocess.run(
212+
[str(anypython), str(check)], text=True, capture_output=True, check=True
213+
).stdout
211214
print(out)
212215
assert "all passed" in out
213216

214217

215-
def test_geterrortext(anypython, tmpdir):
216-
check = tmpdir.join("check.py")
217-
check.write(
218-
py.code.Source(
219-
gateway_base,
218+
def test_geterrortext(anypython, tmp_path):
219+
check = tmp_path / "check.py"
220+
check.write_text(
221+
inspect.getsource(gateway_base)
222+
+ textwrap.dedent(
220223
"""
221224
class Arg:
222225
pass
@@ -230,21 +233,22 @@ class Arg:
230233
s = geterrortext(excinfo)
231234
assert "17" in s
232235
print ("all passed")
233-
""",
236+
"""
234237
)
235238
)
236-
out = anypython.sysexec(check)
239+
out = subprocess.run(
240+
[str(anypython), str(check)], text=True, capture_output=True, check=True
241+
).stdout
237242
print(out)
238243
assert "all passed" in out
239244

240245

241-
@pytest.mark.skipif("not hasattr(os, 'dup')")
242-
def test_stdouterrin_setnull(execmodel):
243-
cap = py.io.StdCaptureFD()
246+
@pytest.mark.skipif(not hasattr(os, "dup"), reason="no os.dup")
247+
def test_stdouterrin_setnull(execmodel, capfd):
244248
gateway_base.init_popen_io(execmodel)
245249
os.write(1, b"hello")
246250
os.read(0, 1)
247-
out, err = cap.reset()
251+
out, err = capfd.readouterr()
248252
assert not out
249253
assert not err
250254

@@ -267,7 +271,7 @@ def close(self, errortext=None):
267271

268272

269273
def test_exectask(execmodel):
270-
io = py.io.BytesIO()
274+
io = BytesIO()
271275
io.execmodel = execmodel
272276
gw = gateway_base.WorkerGateway(io, id="something")
273277
ch = PseudoChannel()
@@ -278,10 +282,10 @@ def test_exectask(execmodel):
278282
class TestMessage:
279283
def test_wire_protocol(self):
280284
for i, handler in enumerate(Message._types):
281-
one = py.io.BytesIO()
285+
one = BytesIO()
282286
data = b"23"
283287
Message(i, 42, data).to_io(one)
284-
two = py.io.BytesIO(one.getvalue())
288+
two = BytesIO(one.getvalue())
285289
msg = Message.from_io(two)
286290
assert msg.msgcode == i
287291
assert isinstance(msg, Message)
@@ -338,7 +342,7 @@ def prototype(wrong):
338342
def test_function_without_known_source_fails(self):
339343
# this one won't be able to find the source
340344
mess = {}
341-
py.builtin.exec_("def fail(channel): pass", mess, mess)
345+
exec("def fail(channel): pass", mess, mess)
342346
print(inspect.getsourcefile(mess["fail"]))
343347
pytest.raises(ValueError, gateway._source_of_function, mess["fail"])
344348

@@ -361,9 +365,9 @@ def working(channel):
361365

362366
class TestGlobalFinder:
363367
def check(self, func):
364-
src = py.code.Source(func)
365-
code = py.code.Code(func)
366-
return gateway._find_non_builtin_globals(str(src), code.raw)
368+
src = textwrap.dedent(inspect.getsource(func))
369+
code = func.__code__
370+
return gateway._find_non_builtin_globals(src, code)
367371

368372
def test_local(self):
369373
def f(a, b, c):

0 commit comments

Comments
 (0)