@@ -543,6 +543,16 @@ async def _create_control_lock(self):
543543 # This can be removed when minimum python increases to 3.10
544544 self ._control_lock = asyncio .Lock ()
545545
546+ async def _await_result (self , result , func_name ):
547+ """Await result if awaitable, otherwise warn about non-async do_* method."""
548+ if inspect .isawaitable (result ):
549+ return await result
550+ warnings .warn (
551+ _AWAITABLE_MESSAGE .format (func_name = func_name , target = getattr (self , func_name )),
552+ PendingDeprecationWarning , stacklevel = 2 ,
553+ )
554+ return result
555+
546556 def start (self ):
547557 """register dispatchers for streams"""
548558 self .io_loop = ioloop .IOLoop .current ()
@@ -830,14 +840,7 @@ async def execute_request(self, stream, ident, parent):
830840 # Call do_execute with the appropriate arguments
831841 reply_content = self .do_execute (** do_execute_args )
832842
833- if inspect .isawaitable (reply_content ):
834- reply_content = await reply_content
835- else :
836- warnings .warn (
837- _AWAITABLE_MESSAGE .format (func_name = "do_execute" , target = self .do_execute ),
838- PendingDeprecationWarning ,
839- stacklevel = 1 ,
840- )
843+ reply_content = await self ._await_result (reply_content , "do_execute" )
841844
842845 # Flush output before sending the reply.
843846 if sys .stdout is not None :
@@ -892,14 +895,7 @@ async def complete_request(self, stream, ident, parent):
892895 cursor_pos = content ["cursor_pos" ]
893896
894897 matches = self .do_complete (code , cursor_pos )
895- if inspect .isawaitable (matches ):
896- matches = await matches
897- else :
898- warnings .warn (
899- _AWAITABLE_MESSAGE .format (func_name = "do_complete" , target = self .do_complete ),
900- PendingDeprecationWarning ,
901- stacklevel = 1 ,
902- )
898+ matches = await self ._await_result (matches , "do_complete" )
903899
904900 matches = json_clean (matches )
905901 self .session .send (stream , "complete_reply" , matches , parent , ident )
@@ -926,14 +922,7 @@ async def inspect_request(self, stream, ident, parent):
926922 content .get ("detail_level" , 0 ),
927923 set (content .get ("omit_sections" , [])),
928924 )
929- if inspect .isawaitable (reply_content ):
930- reply_content = await reply_content
931- else :
932- warnings .warn (
933- _AWAITABLE_MESSAGE .format (func_name = "do_inspect" , target = self .do_inspect ),
934- PendingDeprecationWarning ,
935- stacklevel = 1 ,
936- )
925+ reply_content = await self ._await_result (reply_content , "do_inspect" )
937926
938927 # Before we send this object over, we scrub it for JSON usage
939928 reply_content = json_clean (reply_content )
@@ -951,14 +940,7 @@ async def history_request(self, stream, ident, parent):
951940 content = parent ["content" ]
952941
953942 reply_content = self .do_history (** content )
954- if inspect .isawaitable (reply_content ):
955- reply_content = await reply_content
956- else :
957- warnings .warn (
958- _AWAITABLE_MESSAGE .format (func_name = "do_history" , target = self .do_history ),
959- PendingDeprecationWarning ,
960- stacklevel = 1 ,
961- )
943+ reply_content = await self ._await_result (reply_content , "do_history" )
962944
963945 reply_content = json_clean (reply_content )
964946 msg = self .session .send (stream , "history_reply" , reply_content , parent , ident )
@@ -1079,14 +1061,7 @@ async def shutdown_request(self, stream, ident, parent):
10791061 if not self .session :
10801062 return
10811063 content = self .do_shutdown (parent ["content" ]["restart" ])
1082- if inspect .isawaitable (content ):
1083- content = await content
1084- else :
1085- warnings .warn (
1086- _AWAITABLE_MESSAGE .format (func_name = "do_shutdown" , target = self .do_shutdown ),
1087- PendingDeprecationWarning ,
1088- stacklevel = 1 ,
1089- )
1064+ content = await self ._await_result (content , "do_shutdown" )
10901065 self .session .send (stream , "shutdown_reply" , content , parent , ident = ident )
10911066 # same content, but different msg_id for broadcasting on IOPub
10921067 self ._shutdown_message = self .session .msg ("shutdown_reply" , content , parent )
@@ -1118,14 +1093,7 @@ async def is_complete_request(self, stream, ident, parent):
11181093 code = content ["code" ]
11191094
11201095 reply_content = self .do_is_complete (code )
1121- if inspect .isawaitable (reply_content ):
1122- reply_content = await reply_content
1123- else :
1124- warnings .warn (
1125- _AWAITABLE_MESSAGE .format (func_name = "do_is_complete" , target = self .do_is_complete ),
1126- PendingDeprecationWarning ,
1127- stacklevel = 1 ,
1128- )
1096+ reply_content = await self ._await_result (reply_content , "do_is_complete" )
11291097 reply_content = json_clean (reply_content )
11301098 reply_msg = self .session .send (stream , "is_complete_reply" , reply_content , parent , ident )
11311099 self .log .debug ("%s" , reply_msg )
@@ -1140,16 +1108,7 @@ async def debug_request(self, stream, ident, parent):
11401108 return
11411109 content = parent ["content" ]
11421110 reply_content = self .do_debug_request (content )
1143- if inspect .isawaitable (reply_content ):
1144- reply_content = await reply_content
1145- else :
1146- warnings .warn (
1147- _AWAITABLE_MESSAGE .format (
1148- func_name = "do_debug_request" , target = self .do_debug_request
1149- ),
1150- PendingDeprecationWarning ,
1151- stacklevel = 1 ,
1152- )
1111+ reply_content = await self ._await_result (reply_content , "do_debug_request" )
11531112 reply_content = json_clean (reply_content )
11541113 reply_msg = self .session .send (stream , "debug_reply" , reply_content , parent , ident )
11551114 self .log .debug ("%s" , reply_msg )
0 commit comments