]> granicus.if.org Git - python/commitdiff
Issue #8514: Add os.fsencode() function (Unix only): encode a string to bytes
authorVictor Stinner <victor.stinner@haypocalc.com>
Sat, 8 May 2010 11:10:09 +0000 (11:10 +0000)
committerVictor Stinner <victor.stinner@haypocalc.com>
Sat, 8 May 2010 11:10:09 +0000 (11:10 +0000)
for use in the file system, environment variables or the command line.

Doc/library/os.rst
Lib/os.py
Lib/subprocess.py
Misc/NEWS

index 577c6003b8ebde291bf3b18e13a02cac9ac8293d..1d4b1e164074b230c1c9a8b6d85240f98718d2c6 100644 (file)
@@ -155,6 +155,17 @@ process and user.
    These functions are described in :ref:`os-file-dir`.
 
 
+.. function:: fsencode(value)
+
+   Encode *value* to bytes for use in the file system, environment variables or
+   the command line. Uses :func:`sys.getfilesystemencoding` and
+   ``'surrogateescape'`` error handler for strings and returns bytes unchanged.
+
+   Availability: Unix.
+
+   .. versionadded:: 3.2
+
+
 .. function:: get_exec_path(env=None)
 
    Returns the list of directories that will be searched for a named
index 3e2ee0d6ab65dd488653ecd5f82d82b281d1bd43..13ab18cbcb97ee32b3cc344084c3ad56ef864784 100644 (file)
--- a/Lib/os.py
+++ b/Lib/os.py
@@ -504,6 +504,17 @@ if name not in ('os2', 'nt'):
         return environb.get(key, default)
     __all__.append("getenvb")
 
+if name != 'nt':
+    def fsencode(value):
+        """Encode value for use in the file system, environment variables
+        or the command line."""
+        if isinstance(value, bytes):
+            return value
+        elif isinstance(value, str):
+            return value.encode(sys.getfilesystemencoding(), 'surrogateescape')
+        else:
+            raise TypeError("expect bytes or str, not %s" % type(value).__name__)
+
 def _exists(name):
     return name in globals()
 
index 00d36c35ef82bd6c5b2c1791fcc9fedfef117dc6..695204ab64bc71b598eae086fa8103800ab9a300 100644 (file)
@@ -1079,32 +1079,24 @@ class Popen(object):
                     self._set_cloexec_flag(errpipe_write)
 
                     if _posixsubprocess:
-                        fs_encoding = sys.getfilesystemencoding()
-                        def fs_encode(s):
-                            """Encode s for use in the env, fs or cmdline."""
-                            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
                         # potential deadlocks, thus we do all this here.
                         # and pass it to fork_exec()
 
                         if env:
-                            env_list = [fs_encode(k) + b'=' + fs_encode(v)
+                            env_list = [os.fsencode(k) + b'=' + os.fsencode(v)
                                         for k, v in env.items()]
                         else:
                             env_list = None  # Use execv instead of execve.
                         if os.path.dirname(executable):
-                            executable_list = (fs_encode(executable),)
+                            executable_list = (os.fsencode(executable),)
                         else:
                             # This matches the behavior of os._execvpe().
                             path_list = os.get_exec_path(env)
                             executable_list = (os.path.join(dir, executable)
                                                for dir in path_list)
-                            executable_list = tuple(fs_encode(exe)
+                            executable_list = tuple(os.fsencode(exe)
                                                     for exe in executable_list)
                         self.pid = _posixsubprocess.fork_exec(
                                 args, executable_list,
index 98ec0d27efbeec7c7dd557101bc95865dc7c7a80..77aa054652904e550ffd713809785565ff8a6751 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -351,6 +351,9 @@ C-API
 Library
 -------
 
+- Issue #8514: Add os.fsencode() function (Unix only): encode a string to bytes
+  for use in the file system, environment variables or the command line.
+
 - Issue #8571: Fix an internal error when compressing or decompressing a
   chunk larger than 1GB with the zlib module's compressor and decompressor
   objects.