@@ -211,46 +211,56 @@ def test_unregister_hook(self):
211211
212212
213213def test_magics (tmp_path ):
214- context = zmq .Context ()
215- socket = context .socket (zmq .PUB )
216- shell = InteractiveShell ()
217- shell .user_ns ["hi" ] = 1
218- magics = KernelMagics (shell )
219-
220- tmp_file = tmp_path / "test.txt"
221- tmp_file .write_text ("hi" , "utf8" )
222- magics .edit (str (tmp_file ))
223- payload = shell .payload_manager .read_payload ()[0 ]
224- assert payload ["filename" ] == str (tmp_file )
225-
226- magics .clear ([])
227- magics .less (str (tmp_file ))
228- if os .name == "posix" :
229- magics .man ("ls" )
230- magics .autosave ("10" )
231-
232- socket .close ()
233- context .destroy ()
214+ try :
215+ context = zmq .Context ()
216+ socket = context .socket (zmq .PUB )
217+ shell = InteractiveShell ()
218+ shell .user_ns ["hi" ] = 1
219+ magics = KernelMagics (shell )
220+
221+ tmp_file = tmp_path / "test.txt"
222+ tmp_file .write_text ("hi" , "utf8" )
223+ magics .edit (str (tmp_file ))
224+ payload = shell .payload_manager .read_payload ()[0 ]
225+ assert payload ["filename" ] == str (tmp_file )
226+
227+ magics .clear ([])
228+ magics .less (str (tmp_file ))
229+ if os .name == "posix" :
230+ magics .man ("ls" )
231+ magics .autosave ("10" )
232+
233+ socket .close ()
234+ context .destroy ()
235+ finally :
236+ shell .history_manager = None
237+ shell .configurables = []
238+ InteractiveShell .clear_instance ()
234239
235240
236241def test_zmq_interactive_shell (kernel ):
237- shell = ZMQInteractiveShell ()
238-
239- with pytest .raises (RuntimeError ):
240- shell .enable_gui ("tk" )
241-
242- with warnings .catch_warnings ():
243- warnings .simplefilter ("ignore" , DeprecationWarning )
244- shell .data_pub_class = MagicMock () # type:ignore
245- shell .data_pub
246- shell .kernel = kernel
247- shell .set_next_input ("hi" )
248- assert shell .get_parent () is None
249- if os .name == "posix" :
250- shell .system_piped ("ls" )
251- else :
252- shell .system_piped ("dir" )
253- shell .ask_exit ()
242+ try :
243+ shell = ZMQInteractiveShell ()
244+
245+ with pytest .raises (RuntimeError ):
246+ shell .enable_gui ("tk" )
247+
248+ with warnings .catch_warnings ():
249+ warnings .simplefilter ("ignore" , DeprecationWarning )
250+ shell .data_pub_class = MagicMock () # type:ignore
251+ shell .data_pub
252+ shell .kernel = kernel
253+ shell .set_next_input ("hi" )
254+ assert shell .get_parent () is None
255+ if os .name == "posix" :
256+ shell .system_piped ("ls" )
257+ else :
258+ shell .system_piped ("dir" )
259+ shell .ask_exit ()
260+ finally :
261+ shell .history_manager = None
262+ shell .configurables = []
263+ ZMQInteractiveShell .clear_instance ()
254264
255265
256266if __name__ == "__main__" :
0 commit comments