[Python-checkins] bpo-34783: Add test_cmd_line_script.test_nonexisting_script() (GH-9535)

Miss Islington (bot) webhook-mailer at python.org
Mon Sep 24 11:28:30 EDT 2018


https://github.com/python/cpython/commit/7a26222d7caa507fc46119ba9246f163502eb1fd
commit: 7a26222d7caa507fc46119ba9246f163502eb1fd
branch: 3.7
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2018-09-24T08:28:23-07:00
summary:

bpo-34783: Add test_cmd_line_script.test_nonexisting_script() (GH-9535)


Make sure that "./python script.py" does not crash if the script
file doesn't exist.
(cherry picked from commit a46467ff198c42c8f34768c7be4b4562f6f44736)

Co-authored-by: Victor Stinner <vstinner at redhat.com>

files:
M Lib/test/test_cmd_line_script.py

diff --git a/Lib/test/test_cmd_line_script.py b/Lib/test/test_cmd_line_script.py
index a9c1309ad6d3..2595ca98c7ad 100644
--- a/Lib/test/test_cmd_line_script.py
+++ b/Lib/test/test_cmd_line_script.py
@@ -630,6 +630,25 @@ def test_consistent_sys_path_for_module_execution(self):
             traceback_lines = stderr.decode().splitlines()
             self.assertIn("No module named script_pkg", traceback_lines[-1])
 
+    def test_nonexisting_script(self):
+        # bpo-34783: "./python script.py" must not crash
+        # if the script file doesn't exist.
+        script = 'nonexistingscript.py'
+        self.assertFalse(os.path.exists(script))
+        # Only test the base name, since the error message can use
+        # a relative path, whereas sys.executable can be an asolution path.
+        program = os.path.basename(sys.executable)
+
+        proc = spawn_python(script, text=True,
+                            stdout=subprocess.PIPE,
+                            stderr=subprocess.PIPE)
+        out, err = proc.communicate()
+        # "./python" must be in the error message:
+        # "./python: can't open file (...)"
+        self.assertIn(program, err)
+        self.assertNotEqual(proc.returncode, 0)
+
+
 def test_main():
     support.run_unittest(CmdLineTest)
     support.reap_children()



More information about the Python-checkins mailing list