2
2
import os
3
3
import subprocess
4
4
import sys
5
+ import textwrap
5
6
from io import BytesIO
6
7
7
8
import execnet
8
- import py
9
9
import pytest
10
10
from execnet import gateway
11
11
from execnet import gateway_base
@@ -29,20 +29,20 @@ def test_serializer_api(self, val):
29
29
val2 = execnet .loads (dumped )
30
30
assert val == val2
31
31
32
- def test_mmap (self , tmpdir , val ):
32
+ def test_mmap (self , tmp_path , val ):
33
33
mmap = pytest .importorskip ("mmap" ).mmap
34
- p = tmpdir . join ( "data" )
34
+ p = tmp_path / "data"
35
35
with p .open ("wb" ) as f :
36
36
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 )
40
40
assert val == val2
41
41
42
42
def test_bytesio (self , val ):
43
- f = py . io . BytesIO ()
43
+ f = BytesIO ()
44
44
execnet .dump (f , val )
45
- read = py . io . BytesIO (f .getvalue ())
45
+ read = BytesIO (f .getvalue ())
46
46
val2 = execnet .load (read )
47
47
assert val == val2
48
48
@@ -81,10 +81,8 @@ def receive():
81
81
return popen .stdout .readline ()
82
82
83
83
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 " )
88
86
s = receive ()
89
87
assert s == "ok\n "
90
88
send ("hello\n " )
@@ -114,11 +112,11 @@ def read_write_loop():
114
112
break
115
113
116
114
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 (
122
120
"""
123
121
from io import BytesIO
124
122
import tempfile
@@ -146,24 +144,25 @@ def test_io_message(anypython, tmpdir, execmodel):
146
144
),
147
145
)
148
146
)
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
151
150
print (out )
152
151
assert "all passed" in out
153
152
154
153
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 (
160
159
f"""
161
160
io = init_popen_io(get_execmodel({ execmodel .backend !r} ))
162
161
io.write("hello".encode('ascii'))
163
162
s = io.read(1)
164
163
assert s == "x".encode('ascii')
165
- """ ,
166
- )
164
+ """
165
+ ),
167
166
)
168
167
from subprocess import Popen , PIPE
169
168
@@ -191,32 +190,36 @@ def newread(numbytes):
191
190
assert result == b"tes"
192
191
193
192
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 (
198
197
"""
199
198
class Channel:
200
199
def send(self, data):
201
200
assert eval(repr(data), {}) == data
202
201
channel = Channel()
203
- """ ,
204
- gateway .rinfo_source ,
202
+ """
203
+ )
204
+ + inspect .getsource (gateway .rinfo_source )
205
+ + textwrap .dedent (
205
206
"""
206
207
print ('all passed')
207
- """ ,
208
+ """
208
209
)
209
210
)
210
- out = anypython .sysexec (check )
211
+ out = subprocess .run (
212
+ [str (anypython ), str (check )], text = True , capture_output = True , check = True
213
+ ).stdout
211
214
print (out )
212
215
assert "all passed" in out
213
216
214
217
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 (
220
223
"""
221
224
class Arg:
222
225
pass
@@ -230,21 +233,22 @@ class Arg:
230
233
s = geterrortext(excinfo)
231
234
assert "17" in s
232
235
print ("all passed")
233
- """ ,
236
+ """
234
237
)
235
238
)
236
- out = anypython .sysexec (check )
239
+ out = subprocess .run (
240
+ [str (anypython ), str (check )], text = True , capture_output = True , check = True
241
+ ).stdout
237
242
print (out )
238
243
assert "all passed" in out
239
244
240
245
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 ):
244
248
gateway_base .init_popen_io (execmodel )
245
249
os .write (1 , b"hello" )
246
250
os .read (0 , 1 )
247
- out , err = cap . reset ()
251
+ out , err = capfd . readouterr ()
248
252
assert not out
249
253
assert not err
250
254
@@ -267,7 +271,7 @@ def close(self, errortext=None):
267
271
268
272
269
273
def test_exectask (execmodel ):
270
- io = py . io . BytesIO ()
274
+ io = BytesIO ()
271
275
io .execmodel = execmodel
272
276
gw = gateway_base .WorkerGateway (io , id = "something" )
273
277
ch = PseudoChannel ()
@@ -278,10 +282,10 @@ def test_exectask(execmodel):
278
282
class TestMessage :
279
283
def test_wire_protocol (self ):
280
284
for i , handler in enumerate (Message ._types ):
281
- one = py . io . BytesIO ()
285
+ one = BytesIO ()
282
286
data = b"23"
283
287
Message (i , 42 , data ).to_io (one )
284
- two = py . io . BytesIO (one .getvalue ())
288
+ two = BytesIO (one .getvalue ())
285
289
msg = Message .from_io (two )
286
290
assert msg .msgcode == i
287
291
assert isinstance (msg , Message )
@@ -338,7 +342,7 @@ def prototype(wrong):
338
342
def test_function_without_known_source_fails (self ):
339
343
# this one won't be able to find the source
340
344
mess = {}
341
- py . builtin . exec_ ("def fail(channel): pass" , mess , mess )
345
+ exec ("def fail(channel): pass" , mess , mess )
342
346
print (inspect .getsourcefile (mess ["fail" ]))
343
347
pytest .raises (ValueError , gateway ._source_of_function , mess ["fail" ])
344
348
@@ -361,9 +365,9 @@ def working(channel):
361
365
362
366
class TestGlobalFinder :
363
367
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 )
367
371
368
372
def test_local (self ):
369
373
def f (a , b , c ):
0 commit comments