##// END OF EJS Templates
Merge pull request #11174 from Carreau/close-bg...
Min RK -
r24393:ec3d1a11 merge
parent child Browse files
Show More
@@ -31,14 +31,14 b' def script_args(f):'
31 31 '--out', type=str,
32 32 help="""The variable in which to store stdout from the script.
33 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 37 magic_arguments.argument(
38 38 '--err', type=str,
39 39 help="""The variable in which to store stderr from the script.
40 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 44 magic_arguments.argument(
@@ -187,11 +187,16 b' class ScriptMagics(Magics):'
187 187 if args.bg:
188 188 self.bg_processes.append(p)
189 189 self._gc_bg_processes()
190 to_close = []
190 191 if args.out:
191 192 self.shell.user_ns[args.out] = p.stdout
193 else:
194 to_close.append(p.stdout)
192 195 if args.err:
193 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 200 if args.proc:
196 201 self.shell.user_ns[args.proc] = p
197 202 return
@@ -231,10 +236,12 b' class ScriptMagics(Magics):'
231 236 sys.stderr.write(err)
232 237 sys.stderr.flush()
233 238
234 def _run_script(self, p, cell):
239 def _run_script(self, p, cell, to_close):
235 240 """callback for running the script in the background"""
236 241 p.stdin.write(cell)
237 242 p.stdin.close()
243 for s in to_close:
244 s.close()
238 245 p.wait()
239 246
240 247 @line_magic("killbgscripts")
@@ -562,17 +562,6 b' def test_timeit_shlex():'
562 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 565 def test_timeit_special_syntax():
577 566 "Test %%timeit with IPython special syntax"
578 567 @register_line_magic
@@ -839,13 +828,16 b' def test_script_out_err():'
839 828 def test_script_bg_out():
840 829 ip = get_ipython()
841 830 ip.run_cell_magic("script", "--bg --out output sh", "echo 'hi'")
831
842 832 nt.assert_equal(ip.user_ns['output'].read(), b'hi\n')
833 ip.user_ns['output'].close()
843 834
844 835 @dec.skip_win32
845 836 def test_script_bg_err():
846 837 ip = get_ipython()
847 838 ip.run_cell_magic("script", "--bg --err error sh", "echo 'hello' >&2")
848 839 nt.assert_equal(ip.user_ns['error'].read(), b'hello\n')
840 ip.user_ns['error'].close()
849 841
850 842 @dec.skip_win32
851 843 def test_script_bg_out_err():
@@ -853,6 +845,8 b' def test_script_bg_out_err():'
853 845 ip.run_cell_magic("script", "--bg --out output --err error sh", "echo 'hi'\necho 'hello' >&2")
854 846 nt.assert_equal(ip.user_ns['output'].read(), b'hi\n')
855 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 851 def test_script_defaults():
858 852 ip = get_ipython()
@@ -1072,4 +1066,15 b' def test_logging_magic_not_quiet():'
1072 1066 lm.logstart(os.path.join(td, "not_quiet.log"))
1073 1067 finally:
1074 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