Protect stdout from unexpected progress and console-title

Closes #3023
pull/2852/head
pukkandan 3 years ago
parent da1d734fbe
commit cf4f42cb97
No known key found for this signature in database
GPG Key ID: 7EEE9E1E817D0A39

@ -547,15 +547,20 @@ class YoutubeDL(object):
self._download_retcode = 0
self._num_downloads = 0
self._num_videos = 0
self._screen_file = [sys.stdout, sys.stderr][params.get('logtostderr', False)]
self._err_file = sys.stderr
self.params = params
self.cache = Cache(self)
windows_enable_vt_mode()
self._out_files = {
'error': sys.stderr,
'print': sys.stderr if self.params.get('logtostderr') else sys.stdout,
'console': None if compat_os_name == 'nt' else next(
filter(supports_terminal_sequences, (sys.stderr, sys.stdout)), None)
}
self._out_files['screen'] = sys.stderr if self.params.get('quiet') else self._out_files['print']
self._allow_colors = {
'screen': not self.params.get('no_color') and supports_terminal_sequences(self._screen_file),
'err': not self.params.get('no_color') and supports_terminal_sequences(self._err_file),
type_: not self.params.get('no_color') and supports_terminal_sequences(self._out_files[type_])
for type_ in ('screen', 'error')
}
if sys.version_info < (3, 6):
@ -620,7 +625,7 @@ class YoutubeDL(object):
sp_kwargs = dict(
stdin=subprocess.PIPE,
stdout=slave,
stderr=self._err_file)
stderr=self._out_files['error'])
try:
self._output_process = Popen(['bidiv'] + width_args, **sp_kwargs)
except OSError:
@ -788,14 +793,24 @@ class YoutubeDL(object):
self._printed_messages.add(message)
write_string(message, out=out, encoding=self.params.get('encoding'))
def to_stdout(self, message, skip_eol=False, quiet=False):
def to_stdout(self, message, skip_eol=False, quiet=None):
"""Print message to stdout"""
if quiet is not None:
self.deprecation_warning('"ydl.to_stdout" no longer accepts the argument quiet. Use "ydl.to_screen" instead')
self._write_string(
'%s%s' % (self._bidi_workaround(message), ('' if skip_eol else '\n')),
self._out_files['print'])
def to_screen(self, message, skip_eol=False, quiet=None):
"""Print message to screen if not in quiet mode"""
if self.params.get('logger'):
self.params['logger'].debug(message)
elif not quiet or self.params.get('verbose'):
self._write_string(
'%s%s' % (self._bidi_workaround(message), ('' if skip_eol else '\n')),
self._err_file if quiet else self._screen_file)
return
if (self.params.get('quiet') if quiet is None else quiet) and not self.params.get('verbose'):
return
self._write_string(
'%s%s' % (self._bidi_workaround(message), ('' if skip_eol else '\n')),
self._out_files['screen'])
def to_stderr(self, message, only_once=False):
"""Print message to stderr"""
@ -803,7 +818,12 @@ class YoutubeDL(object):
if self.params.get('logger'):
self.params['logger'].error(message)
else:
self._write_string('%s\n' % self._bidi_workaround(message), self._err_file, only_once=only_once)
self._write_string('%s\n' % self._bidi_workaround(message), self._out_files['error'], only_once=only_once)
def _send_console_code(self, code):
if compat_os_name == 'nt' or not self._out_files['console']:
return
self._write_string(code, self._out_files['console'])
def to_console_title(self, message):
if not self.params.get('consoletitle', False):
@ -814,26 +834,18 @@ class YoutubeDL(object):
# c_wchar_p() might not be necessary if `message` is
# already of type unicode()
ctypes.windll.kernel32.SetConsoleTitleW(ctypes.c_wchar_p(message))
elif 'TERM' in os.environ:
self._write_string('\033]0;%s\007' % message, self._screen_file)
else:
self._send_console_code(f'\033]0;{message}\007')
def save_console_title(self):
if not self.params.get('consoletitle', False):
if not self.params.get('consoletitle') or self.params.get('simulate'):
return
if self.params.get('simulate'):
return
if compat_os_name != 'nt' and 'TERM' in os.environ:
# Save the title on stack
self._write_string('\033[22;0t', self._screen_file)
self._send_console_code('\033[22;0t') # Save the title on stack
def restore_console_title(self):
if not self.params.get('consoletitle', False):
if not self.params.get('consoletitle') or self.params.get('simulate'):
return
if self.params.get('simulate'):
return
if compat_os_name != 'nt' and 'TERM' in os.environ:
# Restore the title from stack
self._write_string('\033[23;0t', self._screen_file)
self._send_console_code('\033[23;0t') # Restore the title from stack
def __enter__(self):
self.save_console_title()
@ -879,11 +891,6 @@ class YoutubeDL(object):
raise DownloadError(message, exc_info)
self._download_retcode = 1
def to_screen(self, message, skip_eol=False):
"""Print message to stdout if not in quiet mode"""
self.to_stdout(
message, skip_eol, quiet=self.params.get('quiet', False))
class Styles(Enum):
HEADERS = 'yellow'
EMPHASIS = 'light blue'
@ -907,11 +914,11 @@ class YoutubeDL(object):
def _format_screen(self, *args, **kwargs):
return self._format_text(
self._screen_file, self._allow_colors['screen'], *args, **kwargs)
self._out_files['screen'], self._allow_colors['screen'], *args, **kwargs)
def _format_err(self, *args, **kwargs):
return self._format_text(
self._err_file, self._allow_colors['err'], *args, **kwargs)
self._out_files['error'], self._allow_colors['error'], *args, **kwargs)
def report_warning(self, message, only_once=False):
'''
@ -3604,7 +3611,7 @@ class YoutubeDL(object):
encoding_str = 'Encodings: locale %s, fs %s, out %s, err %s, pref %s' % (
locale.getpreferredencoding(),
sys.getfilesystemencoding(),
get_encoding(self._screen_file), get_encoding(self._err_file),
get_encoding(self._out_files['screen']), get_encoding(self._out_files['error']),
self.get_encoding())
logger = self.params.get('logger')

@ -159,7 +159,7 @@ class FileDownloader(object):
return int(round(number * multiplier))
def to_screen(self, *args, **kargs):
self.ydl.to_stdout(*args, quiet=self.params.get('quiet'), **kargs)
self.ydl.to_screen(*args, quiet=self.params.get('quiet'), **kargs)
def to_stderr(self, message):
self.ydl.to_stderr(message)
@ -277,9 +277,9 @@ class FileDownloader(object):
elif self.ydl.params.get('logger'):
self._multiline = MultilineLogger(self.ydl.params['logger'], lines)
elif self.params.get('progress_with_newline'):
self._multiline = BreaklineStatusPrinter(self.ydl._screen_file, lines)
self._multiline = BreaklineStatusPrinter(self.ydl._out_files['screen'], lines)
else:
self._multiline = MultilinePrinter(self.ydl._screen_file, lines, not self.params.get('quiet'))
self._multiline = MultilinePrinter(self.ydl._out_files['screen'], lines, not self.params.get('quiet'))
self._multiline.allow_colors = self._multiline._HAVE_FULLCAP and not self.params.get('no_color')
def _finish_multiline_status(self):

Loading…
Cancel
Save