Issue #8391: os.execvpe() and os.getenv() supports unicode with surrogates and
bytes strings for environment keys and values
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 @@
                         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