@@ -296,8 +296,10 @@ async def process_control_message(self, msg=None):
296
296
except Exception :
297
297
self .log .error ("Exception in control handler:" , exc_info = True ) # noqa: G201
298
298
299
- sys .stdout .flush ()
300
- sys .stderr .flush ()
299
+ if sys .stdout is not None :
300
+ sys .stdout .flush ()
301
+ if sys .stderr is not None :
302
+ sys .stderr .flush ()
301
303
self ._publish_status ("idle" , "control" )
302
304
303
305
async def should_handle (self , stream , msg , idents ):
@@ -439,8 +441,10 @@ async def process_shell_message(self, msg=None):
439
441
except Exception :
440
442
self .log .debug ("Unable to signal in post_handler_hook:" , exc_info = True )
441
443
442
- sys .stdout .flush ()
443
- sys .stderr .flush ()
444
+ if sys .stdout is not None :
445
+ sys .stdout .flush ()
446
+ if sys .stderr is not None :
447
+ sys .stderr .flush ()
444
448
self ._publish_status ("idle" , "shell" )
445
449
446
450
async def control_main (self ):
@@ -665,8 +669,10 @@ async def execute_request(self, socket, ident, parent):
665
669
reply_content = await reply_content
666
670
667
671
# Flush output before sending the reply.
668
- sys .stdout .flush ()
669
- sys .stderr .flush ()
672
+ if sys .stdout is not None :
673
+ sys .stdout .flush ()
674
+ if sys .stderr is not None :
675
+ sys .stderr .flush ()
670
676
# FIXME: on rare occasions, the flush doesn't seem to make it to the
671
677
# clients... This seems to mitigate the problem, but we definitely need
672
678
# to better understand what's going on.
@@ -997,8 +1003,10 @@ async def apply_request(self, socket, ident, parent): # pragma: no cover
997
1003
reply_content , result_buf = self .do_apply (content , bufs , msg_id , md )
998
1004
999
1005
# flush i/o
1000
- sys .stdout .flush ()
1001
- sys .stderr .flush ()
1006
+ if sys .stdout is not None :
1007
+ sys .stdout .flush ()
1008
+ if sys .stderr is not None :
1009
+ sys .stderr .flush ()
1002
1010
1003
1011
md = self .finish_metadata (parent , md , reply_content )
1004
1012
if not self .session :
@@ -1136,8 +1144,10 @@ def raw_input(self, prompt=""):
1136
1144
1137
1145
def _input_request (self , prompt , ident , parent , password = False ):
1138
1146
# Flush output before making the request.
1139
- sys .stderr .flush ()
1140
- sys .stdout .flush ()
1147
+ if sys .stdout is not None :
1148
+ sys .stdout .flush ()
1149
+ if sys .stderr is not None :
1150
+ sys .stderr .flush ()
1141
1151
1142
1152
# flush the stdin socket, to purge stale replies
1143
1153
while True :
0 commit comments