@@ -717,3 +717,44 @@ def test_shutdown_subprocesses():
717717 child_newpg .terminate ()
718718 except psutil .NoSuchProcess :
719719 pass
720+
721+
722+ def test_parent_header_and_ident ():
723+ # Kernel._parent_ident is private but kept for backward compatibility,
724+ # see https://github.com/jupyterlab/jupyterlab/issues/17785
725+ with kernel () as kc :
726+ # get_parent('shell')
727+ msg_id , _ = execute (
728+ kc = kc ,
729+ code = "k=get_ipython().kernel; p=k.get_parent('shell'); print(p['header']['msg_id'], p['header']['session'])" ,
730+ )
731+ stdout , _ = assemble_output (kc .get_iopub_msg , parent_msg_id = msg_id )
732+ check_msg_id , session = stdout .split ()
733+ assert check_msg_id == msg_id
734+ assert check_msg_id .startswith (msg_id )
735+
736+ # _parent_ident['shell']
737+ msg_id , _ = execute (kc = kc , code = "print(k._parent_ident['shell'])" )
738+ stdout , _ = assemble_output (kc .get_iopub_msg , parent_msg_id = msg_id )
739+ assert stdout == f"[b'{ session } ']\n "
740+
741+ # Send a control message
742+ msg = kc .session .msg ("kernel_info_request" )
743+ kc .control_channel .send (msg )
744+ control_msg_id = msg ["header" ]["msg_id" ]
745+ assemble_output (kc .get_iopub_msg , parent_msg_id = control_msg_id )
746+
747+ # get_parent('control')
748+ msg_id , _ = execute (
749+ kc = kc ,
750+ code = "p=k.get_parent('control'); print(p['header']['msg_id'], p['header']['session'])" ,
751+ )
752+ stdout , _ = assemble_output (kc .get_iopub_msg , parent_msg_id = msg_id )
753+ check_msg_id , session = stdout .split ()
754+ assert check_msg_id == control_msg_id
755+ assert check_msg_id .startswith (control_msg_id )
756+
757+ # _parent_ident['control']
758+ msg_id , _ = execute (kc = kc , code = "print(k._parent_ident['control'])" )
759+ stdout , _ = assemble_output (kc .get_iopub_msg , parent_msg_id = msg_id )
760+ assert stdout == f"[b'{ session } ']\n "
0 commit comments