1
1
"""
2
2
base execnet gateway code send to the other side for bootstrapping.
3
3
4
- NOTE: aims to be compatible to Python 2.5-3.X, Jython and IronPython
4
+ NOTE: aims to be compatible to Python 3.8+
5
5
6
6
:copyright: 2004-2015
7
7
:authors:
@@ -277,6 +277,8 @@ class Reply:
277
277
through WorkerPool.spawn()
278
278
"""
279
279
280
+ _exception : BaseException | None = None
281
+
280
282
def __init__ (self , task , threadmodel ):
281
283
self .task = task
282
284
self ._result_ready = threadmodel .Event ()
@@ -289,10 +291,10 @@ def get(self, timeout=None):
289
291
including its traceback.
290
292
"""
291
293
self .waitfinish (timeout )
292
- try :
294
+ if self . _exception is None :
293
295
return self ._result
294
- except AttributeError :
295
- raise self ._exc
296
+ else :
297
+ raise self ._exception . with_traceback ( self . _exception . __traceback__ )
296
298
297
299
def waitfinish (self , timeout = None ):
298
300
if not self ._result_ready .wait (timeout ):
@@ -303,8 +305,9 @@ def run(self):
303
305
try :
304
306
try :
305
307
self ._result = func (* args , ** kwargs )
306
- except BaseException as exc :
307
- self ._exc = exc
308
+ except BaseException as e :
309
+ # sys may be already None when shutting down the interpreter
310
+ self ._exception = e
308
311
finally :
309
312
self ._result_ready .set ()
310
313
self .running = False
@@ -486,7 +489,9 @@ def __init__(self, outfile, infile, execmodel):
486
489
except (AttributeError , OSError ):
487
490
pass
488
491
self ._read = getattr (infile , "buffer" , infile ).read
489
- self ._write = getattr (outfile , "buffer" , outfile ).write
492
+ _outfile = getattr (outfile , "buffer" , outfile )
493
+ self ._write = _outfile .write
494
+ self ._flush = _outfile .flush
490
495
self .execmodel = execmodel
491
496
492
497
def read (self , numbytes ):
@@ -504,7 +509,7 @@ def write(self, data):
504
509
"""write out all data bytes."""
505
510
assert isinstance (data , bytes )
506
511
self ._write (data )
507
- self .outfile . flush ()
512
+ self ._flush ()
508
513
509
514
def close_read (self ):
510
515
self .infile .close ()
0 commit comments