Protect stdout from unexpected progress and console-title

Closes #3023
This commit is contained in:
pukkandan 2022-03-11 19:28:21 +05:30
parent da1d734fbe
commit cf4f42cb97
No known key found for this signature in database
GPG Key ID: 7EEE9E1E817D0A39
2 changed files with 43 additions and 36 deletions

View File

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

View File

@ -159,7 +159,7 @@ class FileDownloader(object):
return int(round(number * multiplier)) return int(round(number * multiplier))
def to_screen(self, *args, **kargs): 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): def to_stderr(self, message):
self.ydl.to_stderr(message) self.ydl.to_stderr(message)
@ -277,9 +277,9 @@ class FileDownloader(object):
elif self.ydl.params.get('logger'): elif self.ydl.params.get('logger'):
self._multiline = MultilineLogger(self.ydl.params['logger'], lines) self._multiline = MultilineLogger(self.ydl.params['logger'], lines)
elif self.params.get('progress_with_newline'): 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: 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') self._multiline.allow_colors = self._multiline._HAVE_FULLCAP and not self.params.get('no_color')
def _finish_multiline_status(self): def _finish_multiline_status(self):