summaryrefslogtreecommitdiffstats
path: root/Lib/os.py
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@haypocalc.com>2010-11-06 12:59:33 (GMT)
committerVictor Stinner <victor.stinner@haypocalc.com>2010-11-06 12:59:33 (GMT)
commit273b7668704c97db1fa36e45eded391a73c5e73f (patch)
treea8eef426bf857741cdb2416c1854864190c8d83e /Lib/os.py
parent6881886389ca422395a4c8156ae8aa6394904ea3 (diff)
downloadcpython-273b7668704c97db1fa36e45eded391a73c5e73f.zip
cpython-273b7668704c97db1fa36e45eded391a73c5e73f.tar.gz
cpython-273b7668704c97db1fa36e45eded391a73c5e73f.tar.bz2
os.get_exec_path() ignores BytesWarning instead of recoding them
Use only one global warning.catch_warnings() context, instead of two local contexts. Improve also the explaination why the function uses a local import.
Diffstat (limited to 'Lib/os.py')
-rw-r--r--Lib/os.py52
1 files changed, 24 insertions, 28 deletions
diff --git a/Lib/os.py b/Lib/os.py
index cdc2a66..5848d74 100644
--- a/Lib/os.py
+++ b/Lib/os.py
@@ -382,41 +382,37 @@ def get_exec_path(env=None):
*env* must be an environment variable dict or None. If *env* is None,
os.environ will be used.
"""
- # Use a local import instead of a global import to avoid bootstrap issue:
- # the os module is used to build Python extensions.
+ # Use a local import instead of a global import to limit the number of
+ # modules loaded at startup: the os module is always loaded at startup by
+ # Python. It may also avoid a bootstrap issue.
import warnings
if env is None:
env = environ
- try:
- # ignore BytesWarning warning
- with warnings.catch_warnings(record=True):
- path_list = env.get('PATH')
- except (TypeError, BytesWarning):
- # A BytesWarning here means that env has a b'PATH' key, but no 'PATH'
- # key. Compare bytes and str raises a BytesWarning exception only if
- # sys.flags.bytes_warning==2, and in this case it is not possible to
- # create a dictionary with both keys.
- path_list = None
-
- if supports_bytes_environ:
+ # {b'PATH': ...}.get('PATH') and {'PATH': ...}.get(b'PATH') emit a
+ # BytesWarning when using python -b or python -bb: ignore the warning
+ with warnings.catch_warnings():
+ warnings.simplefilter("ignore", BytesWarning)
+
try:
- # ignore BytesWarning warning
- with warnings.catch_warnings(record=True):
- path_listb = env[b'PATH']
- except (KeyError, TypeError, BytesWarning):
- # A BytesWarning here means that env has a 'PATH' key, but no
- # b'PATH' key. See the comment above for an explaination.
- pass
- else:
- if path_list is not None:
- raise ValueError(
- "env cannot contain 'PATH' and b'PATH' keys")
- path_list = path_listb
+ path_list = env.get('PATH')
+ except TypeError:
+ path_list = None
- if path_list is not None and isinstance(path_list, bytes):
- path_list = fsdecode(path_list)
+ if supports_bytes_environ:
+ try:
+ path_listb = env[b'PATH']
+ except (KeyError, TypeError):
+ pass
+ else:
+ if path_list is not None:
+ raise ValueError(
+ "env cannot contain 'PATH' and b'PATH' keys")
+ path_list = path_listb
+
+ if path_list is not None and isinstance(path_list, bytes):
+ path_list = fsdecode(path_list)
if path_list is None:
path_list = defpath