diff options
author | Victor Stinner <victor.stinner@haypocalc.com> | 2010-04-25 22:39:07 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@haypocalc.com> | 2010-04-25 22:39:07 (GMT) |
commit | a27dcb76ee05397108271547b340c977d52ecabb (patch) | |
tree | 02a6ca89b7ec422ba38442dda8b465f7e99ff53e /Lib | |
parent | e3123915297da3e87f6d62c0e0788a6b5421a5cf (diff) | |
download | cpython-a27dcb76ee05397108271547b340c977d52ecabb.zip cpython-a27dcb76ee05397108271547b340c977d52ecabb.tar.gz cpython-a27dcb76ee05397108271547b340c977d52ecabb.tar.bz2 |
Merged revisions 80421,80424 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k
........
r80421 | victor.stinner | 2010-04-23 23:41:56 +0200 (ven., 23 avril 2010) | 3 lines
Issue #8391: os.execvpe() and os.getenv() supports unicode with surrogates and
bytes strings for environment keys and values
........
r80424 | victor.stinner | 2010-04-24 00:55:39 +0200 (sam., 24 avril 2010) | 13 lines
Fix test_undecodable_env of test_subproces for non-ASCII directory
This test was introduced by r80421 (issue #8391).
The fix: copy the environment variables instead of starting Python in an empty
environement. In an empty environment, the locale is C and Python uses ASCII
for the default file system encoding. The non-ASCII directory will be encoded
using surrogates, but Python3 is unable to load a module or package with a
filename using surrogates.
See issue #8242 for more information about running Python3 with a non-ascii
directory in an empty environement.
........
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/os.py | 2 | ||||
-rw-r--r-- | Lib/test/test_subprocess.py | 26 |
2 files changed, 28 insertions, 0 deletions
@@ -443,6 +443,8 @@ environ = _Environ(environ, _keymap, _putenv, _unsetenv) def getenv(key, default=None): """Get an environment variable, return None if it doesn't exist. The optional second argument can specify an alternate default.""" + if isinstance(key, bytes): + key = key.decode(sys.getfilesystemencoding(), "surrogateescape") return environ.get(key, default) __all__.append("getenv") diff --git a/Lib/test/test_subprocess.py b/Lib/test/test_subprocess.py index 9ab06dc..7711293 100644 --- a/Lib/test/test_subprocess.py +++ b/Lib/test/test_subprocess.py @@ -539,6 +539,32 @@ class ProcessTestCase(unittest.TestCase): if err.errno != 2: # ignore "no such file" raise + def test_undecodable_env(self): + for key, value in (('test', 'abc\uDCFF'), ('test\uDCFF', '42')): + value_repr = repr(value).encode("ascii") + + # test str with surrogates + script = "import os; print(repr(os.getenv(%s)))" % repr(key) + env = os.environ.copy() + env[key] = value + stdout = subprocess.check_output( + [sys.executable, "-c", script], + env=env) + stdout = stdout.rstrip(b'\n\r') + self.assertEquals(stdout, value_repr) + + # test bytes + key = key.encode("ascii", "surrogateescape") + value = value.encode("ascii", "surrogateescape") + script = "import os; print(repr(os.getenv(%s)))" % repr(key) + env = os.environ.copy() + env[key] = value + stdout = subprocess.check_output( + [sys.executable, "-c", script], + env=env) + stdout = stdout.rstrip(b'\n\r') + self.assertEquals(stdout, value_repr) + # # POSIX tests # |