Index: Doc/library/os.rst =================================================================== --- Doc/library/os.rst (révision 80896) +++ Doc/library/os.rst (copie de travail) @@ -152,6 +152,17 @@ 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. Use :func:`sys.getfilesystemencoding` and + ``'surrogateescape'`` error handler for str, and keep 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: Lib/os.py =================================================================== --- Lib/os.py (révision 80896) +++ Lib/os.py (copie de travail) @@ -504,6 +504,17 @@ 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: Lib/subprocess.py =================================================================== --- Lib/subprocess.py (révision 80896) +++ Lib/subprocess.py (copie de travail) @@ -1079,32 +1079,24 @@ 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,