summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@haypocalc.com>2010-04-23 21:41:56 (GMT)
committerVictor Stinner <victor.stinner@haypocalc.com>2010-04-23 21:41:56 (GMT)
commit13bb71c38f1830f3fabd45b51bb3747aa841b1cb (patch)
treef904efbb7613223a12088bcd4f85b4f00d64c4bb /Lib
parent534db4e19f4d03e4bf85520bd44e0bf90fb38ae3 (diff)
downloadcpython-13bb71c38f1830f3fabd45b51bb3747aa841b1cb.zip
cpython-13bb71c38f1830f3fabd45b51bb3747aa841b1cb.tar.gz
cpython-13bb71c38f1830f3fabd45b51bb3747aa841b1cb.tar.bz2
Issue #8391: os.execvpe() and os.getenv() supports unicode with surrogates and
bytes strings for environment keys and values
Diffstat (limited to 'Lib')
-rw-r--r--Lib/os.py2
-rw-r--r--Lib/subprocess.py5
-rw-r--r--Lib/test/test_subprocess.py24
3 files changed, 29 insertions, 2 deletions
diff --git a/Lib/os.py b/Lib/os.py
index 580b983..7672d6f 100644
--- a/Lib/os.py
+++ b/Lib/os.py
@@ -450,6 +450,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/subprocess.py b/Lib/subprocess.py
index b8bcd5e..ff615d3 100644
--- a/Lib/subprocess.py
+++ b/Lib/subprocess.py
@@ -1090,7 +1090,10 @@ class Popen(object):
fs_encoding = sys.getfilesystemencoding()
def fs_encode(s):
"""Encode s for use in the env, fs or cmdline."""
- return s.encode(fs_encoding, 'surrogateescape')
+ if isinstance(s, bytes):
+ return s
+ else:
+ return s.encode(fs_encoding, 'surrogateescape')
# We must avoid complex work that could involve
# malloc or free in the child process to avoid
diff --git a/Lib/test/test_subprocess.py b/Lib/test/test_subprocess.py
index d47e01c..ce53932 100644
--- a/Lib/test/test_subprocess.py
+++ b/Lib/test/test_subprocess.py
@@ -782,7 +782,7 @@ class POSIXProcessTestCase(BaseTestCase):
self.assertStderrEqual(stderr, b'')
self.assertEqual(p.wait(), -signal.SIGTERM)
- def test_surrogates(self):
+ def test_surrogates_error_message(self):
def prepare():
raise ValueError("surrogate:\uDCff")
@@ -801,6 +801,28 @@ class POSIXProcessTestCase(BaseTestCase):
else:
self.fail("Expected ValueError or RuntimeError")
+ 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)
+ stdout = subprocess.check_output(
+ [sys.executable, "-c", script],
+ env={key: value})
+ 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)
+ stdout = subprocess.check_output(
+ [sys.executable, "-c", script],
+ env={key: value})
+ stdout = stdout.rstrip(b'\n\r')
+ self.assertEquals(stdout, value_repr)
+
@unittest.skipUnless(mswindows, "Windows specific tests")
class Win32ProcessTestCase(BaseTestCase):