[compat] Add and use `compat_open()` like Py3 `open()`
authordirkf <fieldhouse@gmx.net>
Wed, 5 Apr 2023 17:39:54 +0000 (18:39 +0100)
committerdirkf <fieldhouse@gmx.net>
Wed, 5 Apr 2023 17:57:37 +0000 (18:57 +0100)
* resolves FIXME: ytdl-org/youtube-dl/commit/dfe5fa4

youtube_dl/compat.py
youtube_dl/options.py

index 39551f8106622bed28101aec4210c9c532ceb13c..fe62caf808a17e44db6f3027d9821739219e77aa 100644 (file)
@@ -3127,6 +3127,16 @@ else:
         return ctypes.WINFUNCTYPE(*args, **kwargs)
 
 
+if sys.version_info < (3, 0):
+    # open(file, mode='r', buffering=- 1, encoding=None, errors=None, newline=None, closefd=True) not: opener=None
+    def compat_open(file_, *args, **kwargs):
+        if len(args) > 6 or 'opener' in kwargs:
+            raise ValueError('open: unsupported argument "opener"')
+        return io.open(file_, *args, **kwargs)
+else:
+    compat_open = open
+
+
 legacy = [
     'compat_HTMLParseError',
     'compat_HTMLParser',
@@ -3185,6 +3195,7 @@ __all__ = [
     'compat_kwargs',
     'compat_map',
     'compat_numeric_types',
+    'compat_open',
     'compat_ord',
     'compat_os_name',
     'compat_os_path_expanduser',
index f6d2b08988961705e693d449ce75917f008f2f07..7b059b51eb51707fbd3f6bed99c5aa98701de4e8 100644 (file)
@@ -11,6 +11,7 @@ from .compat import (
     compat_get_terminal_size,
     compat_getenv,
     compat_kwargs,
+    compat_open as open,
     compat_shlex_split,
 )
 from .utils import (
@@ -41,14 +42,11 @@ def _hide_login_info(opts):
 def parseOpts(overrideArguments=None):
     def _readOptions(filename_bytes, default=[]):
         try:
-            optionf = open(filename_bytes)
+            optionf = open(filename_bytes, encoding=preferredencoding())
         except IOError:
             return default  # silently skip if file is not present
         try:
-            # FIXME: https://github.com/ytdl-org/youtube-dl/commit/dfe5fa49aed02cf36ba9f743b11b0903554b5e56
             contents = optionf.read()
-            if sys.version_info < (3,):
-                contents = contents.decode(preferredencoding())
             res = compat_shlex_split(contents, comments=True)
         finally:
             optionf.close()