Skip to content

Commit af5392f

Browse files
bpo-30746: Port more tests for os.spawnvpe() and os.execve() from 2.7. (#2394)
1 parent 7770394 commit af5392f

1 file changed

Lines changed: 36 additions & 9 deletions

File tree

Lib/test/test_os.py

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1571,6 +1571,27 @@ def test_internal_execvpe_str(self):
15711571
if os.name != "nt":
15721572
self._test_internal_execvpe(bytes)
15731573

1574+
def test_execve_invalid_env(self):
1575+
args = [sys.executable, '-c', 'pass']
1576+
1577+
# null character in the enviroment variable name
1578+
newenv = os.environ.copy()
1579+
newenv["FRUIT\0VEGETABLE"] = "cabbage"
1580+
with self.assertRaises(ValueError):
1581+
os.execve(args[0], args, newenv)
1582+
1583+
# null character in the enviroment variable value
1584+
newenv = os.environ.copy()
1585+
newenv["FRUIT"] = "orange\0VEGETABLE=cabbage"
1586+
with self.assertRaises(ValueError):
1587+
os.execve(args[0], args, newenv)
1588+
1589+
# equal character in the enviroment variable name
1590+
newenv = os.environ.copy()
1591+
newenv["FRUIT=ORANGE"] = "lemon"
1592+
with self.assertRaises(ValueError):
1593+
os.execve(args[0], args, newenv)
1594+
15741595

15751596
@unittest.skipUnless(sys.platform == "win32", "Win32 specific tests")
15761597
class Win32ErrorTests(unittest.TestCase):
@@ -2382,36 +2403,34 @@ def test_spawnve_noargs(self):
23822403
self.assertRaises(ValueError, os.spawnve, os.P_NOWAIT, args[0], ('',), {})
23832404
self.assertRaises(ValueError, os.spawnve, os.P_NOWAIT, args[0], [''], {})
23842405

2385-
@requires_os_func('spawnve')
2386-
def test_spawnve_invalid_env(self):
2387-
# null character in the enviroment variable name
2406+
def _test_invalid_env(self, spawn):
23882407
args = [sys.executable, '-c', 'pass']
2408+
2409+
# null character in the enviroment variable name
23892410
newenv = os.environ.copy()
23902411
newenv["FRUIT\0VEGETABLE"] = "cabbage"
23912412
try:
2392-
exitcode = os.spawnve(os.P_WAIT, args[0], args, newenv)
2413+
exitcode = spawn(os.P_WAIT, args[0], args, newenv)
23932414
except ValueError:
23942415
pass
23952416
else:
23962417
self.assertEqual(exitcode, 127)
23972418

23982419
# null character in the enviroment variable value
2399-
args = [sys.executable, '-c', 'pass']
24002420
newenv = os.environ.copy()
24012421
newenv["FRUIT"] = "orange\0VEGETABLE=cabbage"
24022422
try:
2403-
exitcode = os.spawnve(os.P_WAIT, args[0], args, newenv)
2423+
exitcode = spawn(os.P_WAIT, args[0], args, newenv)
24042424
except ValueError:
24052425
pass
24062426
else:
24072427
self.assertEqual(exitcode, 127)
24082428

24092429
# equal character in the enviroment variable name
2410-
args = [sys.executable, '-c', 'pass']
24112430
newenv = os.environ.copy()
24122431
newenv["FRUIT=ORANGE"] = "lemon"
24132432
try:
2414-
exitcode = os.spawnve(os.P_WAIT, args[0], args, newenv)
2433+
exitcode = spawn(os.P_WAIT, args[0], args, newenv)
24152434
except ValueError:
24162435
pass
24172436
else:
@@ -2427,9 +2446,17 @@ def test_spawnve_invalid_env(self):
24272446
args = [sys.executable, filename]
24282447
newenv = os.environ.copy()
24292448
newenv["FRUIT"] = "orange=lemon"
2430-
exitcode = os.spawnve(os.P_WAIT, args[0], args, newenv)
2449+
exitcode = spawn(os.P_WAIT, args[0], args, newenv)
24312450
self.assertEqual(exitcode, 0)
24322451

2452+
@requires_os_func('spawnve')
2453+
def test_spawnve_invalid_env(self):
2454+
self._test_invalid_env(os.spawnve)
2455+
2456+
@requires_os_func('spawnvpe')
2457+
def test_spawnvpe_invalid_env(self):
2458+
self._test_invalid_env(os.spawnvpe)
2459+
24332460

24342461
# The introduction of this TestCase caused at least two different errors on
24352462
# *nix buildbots. Temporarily skip this to let the buildbots move along.

0 commit comments

Comments
 (0)