##// END OF EJS Templates
Autoclose stdin/err for bg scripts, when not assigned to user_ns...
Matthias Bussonnier -
Show More
@@ -31,14 +31,14 b' def script_args(f):'
31 '--out', type=str,
31 '--out', type=str,
32 help="""The variable in which to store stdout from the script.
32 help="""The variable in which to store stdout from the script.
33 If the script is backgrounded, this will be the stdout *pipe*,
33 If the script is backgrounded, this will be the stdout *pipe*,
34 instead of the stderr text itself.
34 instead of the stderr text itself and will not be auto closed.
35 """
35 """
36 ),
36 ),
37 magic_arguments.argument(
37 magic_arguments.argument(
38 '--err', type=str,
38 '--err', type=str,
39 help="""The variable in which to store stderr from the script.
39 help="""The variable in which to store stderr from the script.
40 If the script is backgrounded, this will be the stderr *pipe*,
40 If the script is backgrounded, this will be the stderr *pipe*,
41 instead of the stderr text itself.
41 instead of the stderr text itself and will not be autoclosed.
42 """
42 """
43 ),
43 ),
44 magic_arguments.argument(
44 magic_arguments.argument(
@@ -187,11 +187,16 b' class ScriptMagics(Magics):'
187 if args.bg:
187 if args.bg:
188 self.bg_processes.append(p)
188 self.bg_processes.append(p)
189 self._gc_bg_processes()
189 self._gc_bg_processes()
190 to_close = []
190 if args.out:
191 if args.out:
191 self.shell.user_ns[args.out] = p.stdout
192 self.shell.user_ns[args.out] = p.stdout
193 else:
194 to_close.append(p.stdout)
192 if args.err:
195 if args.err:
193 self.shell.user_ns[args.err] = p.stderr
196 self.shell.user_ns[args.err] = p.stderr
194 self.job_manager.new(self._run_script, p, cell, daemon=True)
197 else:
198 to_close.append(p.stderr)
199 self.job_manager.new(self._run_script, p, cell, to_close, daemon=True)
195 if args.proc:
200 if args.proc:
196 self.shell.user_ns[args.proc] = p
201 self.shell.user_ns[args.proc] = p
197 return
202 return
@@ -231,10 +236,12 b' class ScriptMagics(Magics):'
231 sys.stderr.write(err)
236 sys.stderr.write(err)
232 sys.stderr.flush()
237 sys.stderr.flush()
233
238
234 def _run_script(self, p, cell):
239 def _run_script(self, p, cell, to_close):
235 """callback for running the script in the background"""
240 """callback for running the script in the background"""
236 p.stdin.write(cell)
241 p.stdin.write(cell)
237 p.stdin.close()
242 p.stdin.close()
243 for s in to_close:
244 s.close()
238 p.wait()
245 p.wait()
239
246
240 @line_magic("killbgscripts")
247 @line_magic("killbgscripts")
@@ -562,17 +562,6 b' def test_timeit_shlex():'
562 _ip.magic('timeit -r1 -n1 f("a " + "b ")')
562 _ip.magic('timeit -r1 -n1 f("a " + "b ")')
563
563
564
564
565 def test_timeit_arguments():
566 "Test valid timeit arguments, should not cause SyntaxError (GH #1269)"
567 if sys.version_info < (3,7):
568 _ip.magic("timeit ('#')")
569 else:
570 # 3.7 optimize no-op statement like above out, and complain there is
571 # nothing in the for loop.
572 _ip.magic("timeit a=('#')")
573
574
575
576 def test_timeit_special_syntax():
565 def test_timeit_special_syntax():
577 "Test %%timeit with IPython special syntax"
566 "Test %%timeit with IPython special syntax"
578 @register_line_magic
567 @register_line_magic
@@ -839,13 +828,16 b' def test_script_out_err():'
839 def test_script_bg_out():
828 def test_script_bg_out():
840 ip = get_ipython()
829 ip = get_ipython()
841 ip.run_cell_magic("script", "--bg --out output sh", "echo 'hi'")
830 ip.run_cell_magic("script", "--bg --out output sh", "echo 'hi'")
831
842 nt.assert_equal(ip.user_ns['output'].read(), b'hi\n')
832 nt.assert_equal(ip.user_ns['output'].read(), b'hi\n')
833 ip.user_ns['output'].close()
843
834
844 @dec.skip_win32
835 @dec.skip_win32
845 def test_script_bg_err():
836 def test_script_bg_err():
846 ip = get_ipython()
837 ip = get_ipython()
847 ip.run_cell_magic("script", "--bg --err error sh", "echo 'hello' >&2")
838 ip.run_cell_magic("script", "--bg --err error sh", "echo 'hello' >&2")
848 nt.assert_equal(ip.user_ns['error'].read(), b'hello\n')
839 nt.assert_equal(ip.user_ns['error'].read(), b'hello\n')
840 ip.user_ns['error'].close()
849
841
850 @dec.skip_win32
842 @dec.skip_win32
851 def test_script_bg_out_err():
843 def test_script_bg_out_err():
@@ -853,6 +845,8 b' def test_script_bg_out_err():'
853 ip.run_cell_magic("script", "--bg --out output --err error sh", "echo 'hi'\necho 'hello' >&2")
845 ip.run_cell_magic("script", "--bg --out output --err error sh", "echo 'hi'\necho 'hello' >&2")
854 nt.assert_equal(ip.user_ns['output'].read(), b'hi\n')
846 nt.assert_equal(ip.user_ns['output'].read(), b'hi\n')
855 nt.assert_equal(ip.user_ns['error'].read(), b'hello\n')
847 nt.assert_equal(ip.user_ns['error'].read(), b'hello\n')
848 ip.user_ns['output'].close()
849 ip.user_ns['error'].close()
856
850
857 def test_script_defaults():
851 def test_script_defaults():
858 ip = get_ipython()
852 ip = get_ipython()
@@ -1072,4 +1066,15 b' def test_logging_magic_not_quiet():'
1072 lm.logstart(os.path.join(td, "not_quiet.log"))
1066 lm.logstart(os.path.join(td, "not_quiet.log"))
1073 finally:
1067 finally:
1074 _ip.logger.logstop()
1068 _ip.logger.logstop()
1075
1069
1070 ##
1071 # this is slow, put at the end for local testing.
1072 ##
1073 def test_timeit_arguments():
1074 "Test valid timeit arguments, should not cause SyntaxError (GH #1269)"
1075 if sys.version_info < (3,7):
1076 _ip.magic("timeit ('#')")
1077 else:
1078 # 3.7 optimize no-op statement like above out, and complain there is
1079 # nothing in the for loop.
1080 _ip.magic("timeit a=('#')")
General Comments 0
You need to be logged in to leave comments. Login now