summary refs log tree commit diff
diff options
context:
space:
mode:
authorJaime Marquínez Ferrándiz <jaime.marquinez.ferrandiz@gmail.com>2014-11-15 22:00:32 +0100
committerJaime Marquínez Ferrándiz <jaime.marquinez.ferrandiz@gmail.com>2014-11-15 22:00:32 +0100
commitec5f601670dfb6c39d3a4669898284bb2782dd0c (patch)
tree2a46084669c9e5862cdb2a78ef03586019c923d9
parent8caa0c97799dda29fff7d0b0e717e09beee851d3 (diff)
downloadyoutube-dl-ec5f601670dfb6c39d3a4669898284bb2782dd0c.tar.gz
youtube-dl-ec5f601670dfb6c39d3a4669898284bb2782dd0c.tar.xz
youtube-dl-ec5f601670dfb6c39d3a4669898284bb2782dd0c.zip
[utils] Fix "write_json_file" for unicode names in python 2.x (fixes #4125)
-rw-r--r--youtube_dl/utils.py16
1 files changed, 14 insertions, 2 deletions
diff --git a/youtube_dl/utils.py b/youtube_dl/utils.py
index 7460d8701..67be4a9ae 100644
--- a/youtube_dl/utils.py
+++ b/youtube_dl/utils.py
@@ -73,10 +73,22 @@ def preferredencoding():
 def write_json_file(obj, fn):
     """ Encode obj as JSON and write it to fn, atomically """
 
+    if sys.version_info < (3, 0):
+        encoding = get_filesystem_encoding()
+        # os.path.basename returns a bytes object, but NamedTemporaryFile
+        # will fail if the filename contains non ascii characters unless we
+        # use a unicode object
+        path_basename = lambda f: os.path.basename(fn).decode(encoding)
+        # the same for os.path.dirname
+        path_dirname = lambda f: os.path.dirname(fn).decode(encoding)
+    else:
+        path_basename = os.path.basename
+        path_dirname = os.path.dirname
+
     args = {
         'suffix': '.tmp',
-        'prefix': os.path.basename(fn) + '.',
-        'dir': os.path.dirname(fn),
+        'prefix': path_basename(fn) + '.',
+        'dir': path_dirname(fn),
         'delete': False,
     }