11"""
22base execnet gateway code send to the other side for bootstrapping.
33
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+
55
66:copyright: 2004-2015
77:authors:
@@ -277,6 +277,8 @@ class Reply:
277277 through WorkerPool.spawn()
278278 """
279279
280+ _exception : BaseException | None = None
281+
280282 def __init__ (self , task , threadmodel ):
281283 self .task = task
282284 self ._result_ready = threadmodel .Event ()
@@ -289,10 +291,10 @@ def get(self, timeout=None):
289291 including its traceback.
290292 """
291293 self .waitfinish (timeout )
292- try :
294+ if self . _exception is None :
293295 return self ._result
294- except AttributeError :
295- raise self ._exc
296+ else :
297+ raise self ._exception . with_traceback ( self . _exception . __traceback__ )
296298
297299 def waitfinish (self , timeout = None ):
298300 if not self ._result_ready .wait (timeout ):
@@ -303,8 +305,9 @@ def run(self):
303305 try :
304306 try :
305307 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
308311 finally :
309312 self ._result_ready .set ()
310313 self .running = False
@@ -486,7 +489,9 @@ def __init__(self, outfile, infile, execmodel):
486489 except (AttributeError , OSError ):
487490 pass
488491 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
490495 self .execmodel = execmodel
491496
492497 def read (self , numbytes ):
@@ -504,7 +509,7 @@ def write(self, data):
504509 """write out all data bytes."""
505510 assert isinstance (data , bytes )
506511 self ._write (data )
507- self .outfile . flush ()
512+ self ._flush ()
508513
509514 def close_read (self ):
510515 self .infile .close ()
0 commit comments