install.py: use surrogateescape for Python >=3.2
authorZac Medico <zmedico@gentoo.org>
Fri, 21 Jun 2013 23:07:31 +0000 (16:07 -0700)
committerZac Medico <zmedico@gentoo.org>
Fri, 21 Jun 2013 23:07:31 +0000 (16:07 -0700)
We can't trust that the filesystem encoding (locale dependent)
correctly matches the arguments, so use surrogateescape to
pass through the original argv bytes for Python 3.
Since Python <3.2 does not support bytes in Popen args, trust
the locale in that case.

bin/install.py

index 2ca62f2335caaa8ecec88b5fb48d53cf6894384f..f7118a683a6b1820c2398883d84336a8eb1b88dd 100755 (executable)
@@ -223,6 +223,20 @@ def main(args):
        path_installs = Which('install', all=True)
        cmdline = path_installs[0:1]
        cmdline += args
+
+       if sys.hexversion >= 0x3020000:
+               # We can't trust that the filesystem encoding (locale dependent)
+               # correctly matches the arguments, so use surrogateescape to
+               # pass through the original argv bytes for Python 3.
+               # Since Python <3.2 does not support bytes in Popen args, trust
+               # the locale in that case.
+               fs_encoding = sys.getfilesystemencoding()
+               cmdline = [x.encode(fs_encoding, 'surrogateescape') for x in cmdline]
+               files = [x.encode(fs_encoding, 'surrogateescape') for x in files]
+               if opts.target_directory is not None:
+                       opts.target_directory = \
+                               opts.target_directory.encode(fs_encoding, 'surrogateescape')
+
        returncode = subprocess.call(cmdline)
        if returncode == os.EX_OK:
                returncode = copy_xattrs(opts, files)