Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 38430e2

Browse files
author
Victor Stinner
committed
Fix os.get_exec_path() (code and tests) for python -bb
Catch BytesWarning exceptions.
1 parent 9802b39 commit 38430e2

2 files changed

Lines changed: 12 additions & 5 deletions

File tree

Lib/os.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,13 +387,13 @@ def get_exec_path(env=None):
387387

388388
try:
389389
path_list = env.get('PATH')
390-
except TypeError:
390+
except (TypeError, BytesWarning):
391391
path_list = None
392392

393393
if supports_bytes_environ:
394394
try:
395395
path_listb = env[b'PATH']
396-
except (KeyError, TypeError):
396+
except (KeyError, TypeError, BytesWarning):
397397
pass
398398
else:
399399
if path_list is not None:

Lib/test/test_os.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -450,8 +450,12 @@ def test_get_exec_path(self):
450450

451451
if os.supports_bytes_environ:
452452
# env cannot contain 'PATH' and b'PATH' keys
453-
self.assertRaises(ValueError,
454-
os.get_exec_path, {'PATH': '1', b'PATH': b'2'})
453+
try:
454+
mixed_env = {'PATH': '1', b'PATH': b'2'}
455+
except BytesWarning:
456+
pass
457+
else:
458+
self.assertRaises(ValueError, os.get_exec_path, mixed_env)
455459

456460
# bytes key and/or value
457461
self.assertSequenceEqual(os.get_exec_path({b'PATH': b'abc'}),
@@ -723,7 +727,10 @@ def _test_internal_execvpe(self, test_type):
723727
# os.get_exec_path() reads the 'PATH' variable
724728
with _execvpe_mockup() as calls:
725729
env_path = env.copy()
726-
env_path['PATH'] = program_path
730+
if test_type is bytes:
731+
env_path[b'PATH'] = program_path
732+
else:
733+
env_path['PATH'] = program_path
727734
self.assertRaises(OSError,
728735
os._execvpe, program, arguments, env=env_path)
729736
self.assertEqual(len(calls), 1)

0 commit comments

Comments
 (0)