]> jfr.im git - yt-dlp.git/commitdiff
[utils] sanitize_open: Allow any IO stream as stdout
authorpukkandan <redacted>
Sat, 30 Jul 2022 22:01:20 +0000 (03:31 +0530)
committerpukkandan <redacted>
Sat, 30 Jul 2022 22:01:20 +0000 (03:31 +0530)
Fixes: https://github.com/yt-dlp/yt-dlp/issues/3298#issuecomment-1181754989
yt_dlp/utils.py

index fcc25388d8f0449b7adca956fd3d75adea6c371c..bdab9fb4993f78c5229a382c0f47f12f4b82255b 100644 (file)
@@ -598,7 +598,9 @@ def sanitize_open(filename, open_mode):
     if filename == '-':
         if sys.platform == 'win32':
             import msvcrt
-            msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
+            # stdout may be any IO stream. Eg, when using contextlib.redirect_stdout
+            with contextlib.suppress(io.UnsupportedOperation):
+                msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
         return (sys.stdout.buffer if hasattr(sys.stdout, 'buffer') else sys.stdout, filename)
 
     for attempt in range(2):