22import os
33import subprocess
44import sys
5+ import textwrap
56from io import BytesIO
67
78import execnet
8- import py
99import pytest
1010from execnet import gateway
1111from 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
269273def 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):
278282class 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
362366class 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