@@ -339,12 +339,12 @@ def init_sockets(self):
339339 self .shell_socket = zmq_anyio .Socket (context .socket (zmq .ROUTER ))
340340 self .shell_socket .linger = 1000
341341 self .shell_port = self ._bind_socket (self .shell_socket , self .shell_port )
342- self .log .debug ("shell ROUTER Channel on port: %i" % self .shell_port )
342+ self .log .debug ("shell ROUTER Channel on port: %i" , self .shell_port )
343343
344344 self .stdin_socket = context .socket (zmq .ROUTER )
345345 self .stdin_socket .linger = 1000
346346 self .stdin_port = self ._bind_socket (self .stdin_socket , self .stdin_port )
347- self .log .debug ("stdin ROUTER Channel on port: %i" % self .stdin_port )
347+ self .log .debug ("stdin ROUTER Channel on port: %i" , self .stdin_port )
348348
349349 if hasattr (zmq , "ROUTER_HANDOVER" ):
350350 # set router-handover to workaround zeromq reconnect problems
@@ -360,7 +360,7 @@ def init_control(self, context):
360360 self .control_socket = zmq_anyio .Socket (context .socket (zmq .ROUTER ))
361361 self .control_socket .linger = 1000
362362 self .control_port = self ._bind_socket (self .control_socket , self .control_port )
363- self .log .debug ("control ROUTER Channel on port: %i" % self .control_port )
363+ self .log .debug ("control ROUTER Channel on port: %i" , self .control_port )
364364
365365 self .debugpy_socket = zmq_anyio .Socket (context .socket (zmq .STREAM ))
366366 self .debugpy_socket .linger = 1000
@@ -385,7 +385,7 @@ def init_iopub(self, context):
385385 self .iopub_socket = zmq_anyio .Socket (context .socket (zmq .PUB ))
386386 self .iopub_socket .linger = 1000
387387 self .iopub_port = self ._bind_socket (self .iopub_socket , self .iopub_port )
388- self .log .debug ("iopub PUB Channel on port: %i" % self .iopub_port )
388+ self .log .debug ("iopub PUB Channel on port: %i" , self .iopub_port )
389389 self .configure_tornado_logger ()
390390 self .iopub_thread = IOPubThread (self .iopub_socket , pipe = True )
391391 self .iopub_thread .start ()
@@ -399,7 +399,7 @@ def init_heartbeat(self):
399399 hb_ctx = zmq .Context ()
400400 self .heartbeat = Heartbeat (hb_ctx , (self .transport , self .ip , self .hb_port ))
401401 self .hb_port = self .heartbeat .port
402- self .log .debug ("Heartbeat REP Channel on port: %i" % self .hb_port )
402+ self .log .debug ("Heartbeat REP Channel on port: %i" , self .hb_port )
403403 self .heartbeat .start ()
404404
405405 def close (self ):
@@ -515,7 +515,7 @@ def init_io(self):
515515 isinstance (handler , StreamHandler )
516516 and (buffer := getattr (handler .stream , "buffer" , None ))
517517 and (fileno := getattr (buffer , "fileno" , None ))
518- and fileno () == sys .stderr ._original_stdstream_fd # type:ignore[attr-defined]
518+ and fileno () == sys .stderr ._original_stdstream_fd
519519 ):
520520 self .log .debug ("Seeing logger to stderr, rerouting to raw filedescriptor." )
521521 io_wrapper = TextIOWrapper (
@@ -697,7 +697,7 @@ def init_pdb(self):
697697 pdb .set_trace = debugger .set_trace
698698
699699 @catch_config_error
700- def initialize (self , argv = None ):
700+ def initialize (self , argv = None ) -> None :
701701 """Initialize the application."""
702702 super ().initialize (argv )
703703 if self .subapp is not None :
@@ -735,22 +735,18 @@ def initialize(self, argv=None):
735735 sys .stdout .flush ()
736736 sys .stderr .flush ()
737737
738- def start (self ) -> None :
739- """Start the application."""
738+ async def _start (self , backend : str ) -> None :
739+ """
740+ Async version of start, when the loop is not controlled by IPykernel
741+
742+ For example to be used in test suite with @pytest.mark.trio
743+ """
740744 if self .subapp is not None :
741745 self .subapp .start ()
742746 return
743747 if self .poller is not None :
744748 self .poller .start ()
745- backend = "trio" if self .trio_loop else "asyncio"
746- run (partial (self .main , backend ), backend = backend )
747- return
748-
749- async def _wait_to_enter_eventloop (self ):
750- await self .kernel ._eventloop_set .wait ()
751- await self .kernel .enter_eventloop ()
752749
753- async def main (self , backend : str ):
754750 if backend == "asyncio" and sys .platform == "win32" :
755751 import asyncio
756752
@@ -760,16 +756,27 @@ async def main(self, backend: str):
760756
761757 selector = get_selector ()
762758 selector ._thread .pydev_do_not_trace = True
763- # selector._thread.is_pydev_daemon_thread = True
764759
760+ await self .main ()
761+
762+ def start (self ) -> None :
763+ """Start the application."""
764+ backend = "trio" if self .trio_loop else "asyncio"
765+ run (partial (self ._start , backend ), backend = backend )
766+
767+ async def _wait_to_enter_eventloop (self ) -> None :
768+ await self .kernel ._eventloop_set .wait ()
769+ await self .kernel .enter_eventloop ()
770+
771+ async def main (self ) -> None :
765772 async with create_task_group () as tg :
766773 tg .start_soon (self ._wait_to_enter_eventloop )
767774 tg .start_soon (self .kernel .start )
768775
769776 if self .kernel .eventloop :
770777 self .kernel ._eventloop_set .set ()
771778
772- def stop (self ):
779+ def stop (self ) -> None :
773780 """Stop the kernel, thread-safe."""
774781 self .kernel .stop ()
775782
0 commit comments