[Python-checkins] r85902 - in python/branches/py3k/Lib: os.py test/test_os.py

victor.stinner python-checkins at python.org
Fri Oct 29 02:38:58 CEST 2010


Author: victor.stinner
Date: Fri Oct 29 02:38:58 2010
New Revision: 85902

Log:
Issue #10210: os.get_exec_path() ignores BytesWarning warnings


Modified:
   python/branches/py3k/Lib/os.py
   python/branches/py3k/Lib/test/test_os.py

Modified: python/branches/py3k/Lib/os.py
==============================================================================
--- python/branches/py3k/Lib/os.py	(original)
+++ python/branches/py3k/Lib/os.py	Fri Oct 29 02:38:58 2010
@@ -382,18 +382,32 @@
     *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.
+    import warnings
+
     if env is None:
         env = environ
 
     try:
-        path_list = env.get('PATH')
+        # 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:
         try:
-            path_listb = env[b'PATH']
+            # 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:

Modified: python/branches/py3k/Lib/test/test_os.py
==============================================================================
--- python/branches/py3k/Lib/test/test_os.py	(original)
+++ python/branches/py3k/Lib/test/test_os.py	Fri Oct 29 02:38:58 2010
@@ -461,8 +461,11 @@
         if os.supports_bytes_environ:
             # env cannot contain 'PATH' and b'PATH' keys
             try:
-                mixed_env = {'PATH': '1', b'PATH': b'2'}
+                # ignore BytesWarning warning
+                with warnings.catch_warnings(record=True):
+                    mixed_env = {'PATH': '1', b'PATH': b'2'}
             except BytesWarning:
+                # mixed_env cannot be created with python -bb
                 pass
             else:
                 self.assertRaises(ValueError, os.get_exec_path, mixed_env)


More information about the Python-checkins mailing list