Skip to content

Commit e169d97

Browse files
committed
Update test_cmd_line_script from Cpython v3.11.2
1 parent 07bad48 commit e169d97

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

Lib/test/test_cmd_line_script.py

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,9 @@ def _check_output(self, script_name, exit_code, data,
116116
self.assertIn(printed_file.encode('utf-8'), data)
117117
self.assertIn(printed_package.encode('utf-8'), data)
118118
self.assertIn(printed_argv0.encode('utf-8'), data)
119-
self.assertIn(printed_path0.encode('utf-8'), data)
119+
# PYTHONSAFEPATH=1 changes the default sys.path[0]
120+
if not sys.flags.safe_path:
121+
self.assertIn(printed_path0.encode('utf-8'), data)
120122
self.assertIn(printed_cwd.encode('utf-8'), data)
121123

122124
def _check_script(self, script_exec_args, expected_file,
@@ -492,7 +494,6 @@ def test_dash_m_errors(self):
492494
br'ModuleNotFoundError'),
493495
('builtins.x.y', br'Error while finding module specification.*'
494496
br'ModuleNotFoundError.*No module named.*not a package'),
495-
('os.path', br'loader.*cannot handle'),
496497
('importlib', br'No module named.*'
497498
br'is a package and cannot be directly executed'),
498499
('importlib.nonexistent', br'No module named'),
@@ -577,8 +578,9 @@ def test_non_ascii(self):
577578
# Mac OS X denies the creation of a file with an invalid UTF-8 name.
578579
# Windows allows creating a name with an arbitrary bytes name, but
579580
# Python cannot a undecodable bytes argument to a subprocess.
581+
# WASI does not permit invalid UTF-8 names.
580582
if (os_helper.TESTFN_UNDECODABLE
581-
and sys.platform not in ('win32', 'darwin')):
583+
and sys.platform not in ('win32', 'darwin', 'emscripten', 'wasi')):
582584
name = os.fsdecode(os_helper.TESTFN_UNDECODABLE)
583585
elif os_helper.TESTFN_NONASCII:
584586
name = os_helper.TESTFN_NONASCII
@@ -770,14 +772,27 @@ def test_nonexisting_script(self):
770772
self.assertIn(": can't open file ", err)
771773
self.assertNotEqual(proc.returncode, 0)
772774

773-
# TODO: RUSTPYTHON
774-
# def tearDownModule():
775-
def test_main():
776-
support.run_unittest(CmdLineTest)
775+
@unittest.skipUnless(os.path.exists('/dev/fd/0'), 'requires /dev/fd platform')
776+
@unittest.skipIf(sys.platform.startswith("freebsd") and
777+
os.stat("/dev").st_dev == os.stat("/dev/fd").st_dev,
778+
"Requires fdescfs mounted on /dev/fd on FreeBSD")
779+
def test_script_as_dev_fd(self):
780+
# GH-87235: On macOS passing a non-trivial script to /dev/fd/N can cause
781+
# problems because all open /dev/fd/N file descriptors share the same
782+
# offset.
783+
script = 'print("12345678912345678912345")'
784+
with os_helper.temp_dir() as work_dir:
785+
script_name = _make_test_script(work_dir, 'script.py', script)
786+
with open(script_name, "r") as fp:
787+
p = spawn_python(f"/dev/fd/{fp.fileno()}", close_fds=False, pass_fds=(0,1,2,fp.fileno()))
788+
out, err = p.communicate()
789+
self.assertEqual(out, b"12345678912345678912345\n")
790+
791+
792+
793+
def tearDownModule():
777794
support.reap_children()
778795

779796

780797
if __name__ == '__main__':
781-
# TODO: RUSTPYTHON
782-
# unittest.main()
783-
test_main()
798+
unittest.main()

0 commit comments

Comments
 (0)