Explorar o código

Remove some antipatterns and ensure that we always write the JSON file with UTF-8

Philipp Hagemeister %!s(int64=12) %!d(string=hai) anos
pai
achega
ce4be3a91d
Modificáronse 1 ficheiros con 3 adicións e 5 borrados
  1. 3 5
      youtube_dl/FileDownloader.py

+ 3 - 5
youtube_dl/FileDownloader.py

@@ -4,6 +4,7 @@
 from __future__ import absolute_import
 from __future__ import absolute_import
 
 
 import math
 import math
+import io
 import os
 import os
 import re
 import re
 import socket
 import socket
@@ -453,12 +454,9 @@ class FileDownloader(object):
                 self.trouble(u'ERROR: No JSON encoder found. Update to Python 2.6+, setup a json module, or leave out --write-info-json.')
                 self.trouble(u'ERROR: No JSON encoder found. Update to Python 2.6+, setup a json module, or leave out --write-info-json.')
                 return
                 return
             try:
             try:
-                infof = open(encodeFilename(infofn), 'w')
-                try:
-                    json_info_dict = dict((k, info_dict[k]) for k in info_dict if not k in ['urlhandle'])
+                with io.open(encodeFilename(infofn), 'w', 'utf-8') as infof:
+                    json_info_dict = dict((k, v) for k,v in info_dict.items() if not k in ['urlhandle'])
                     json.dump(json_info_dict, infof)
                     json.dump(json_info_dict, infof)
-                finally:
-                    infof.close()
             except (OSError, IOError):
             except (OSError, IOError):
                 self.trouble(u'ERROR: Cannot write metadata to JSON file ' + infofn)
                 self.trouble(u'ERROR: Cannot write metadata to JSON file ' + infofn)
                 return
                 return