7
7
from tempfile import mkdtemp
8
8
import time
9
9
10
+ PY3 = sys .version_info [0 ] >= 3
11
+
10
12
def _launch (extra_env ):
11
13
env = os .environ .copy ()
12
14
env .update (extra_env )
13
15
return Popen ([sys .executable , '-c' ,
14
16
'from jupyter_client.kernelapp import main; main()' ],
15
- env = env , stderr = PIPE )
17
+ env = env , stderr = ( PIPE if PY3 else None ) )
16
18
17
19
WAIT_TIME = 10
18
20
POLL_FREQ = 10
19
21
22
+ def hacky_wait (p ):
23
+ """Python 2 subprocess doesn't have timeouts :-("""
24
+ for _ in range (WAIT_TIME * POLL_FREQ ):
25
+ if p .poll () is not None :
26
+ return p .returncode
27
+ time .sleep (1 / POLL_FREQ )
28
+ else :
29
+ raise AssertionError ("Process didn't exit in {} seconds"
30
+ .format (WAIT_TIME ))
31
+
20
32
def test_kernelapp_lifecycle ():
21
33
# Check that 'jupyter kernel' starts and terminates OK.
22
34
runtime_dir = mkdtemp ()
@@ -42,15 +54,13 @@ def test_kernelapp_lifecycle():
42
54
assert cf .startswith ('kernel' )
43
55
assert cf .endswith ('.json' )
44
56
45
- # Read the first three lines from stderr. This will hang if there are
46
- # fewer lines to read; I don't see any way to avoid that without lots
47
- # of extra complexity.
48
- b = b'' .join (p .stderr .readline () for _ in range (2 )).decode ('utf-8' , 'replace' )
49
- assert cf in b
50
-
51
57
# Send SIGTERM to shut down
52
58
p .terminate ()
53
- p .wait (timeout = 10 )
59
+ if PY3 :
60
+ _ , stderr = p .communicate (timeout = WAIT_TIME )
61
+ assert cf in stderr .decode ('utf-8' , 'replace' )
62
+ else :
63
+ hacky_wait (p )
54
64
finally :
55
65
shutil .rmtree (runtime_dir )
56
66
shutil .rmtree (startup_dir )
0 commit comments