From 187986a857993411fd31c4e017ea4fccccf343fa Mon Sep 17 00:00:00 2001 From: pukkandan Date: Fri, 11 Jun 2021 19:13:22 +0530 Subject: [PATCH] Better error handling of syntax errors in `-f` --- test/test_YoutubeDL.py | 5 ++--- yt_dlp/YoutubeDL.py | 34 ++++++++++++++++++---------------- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/test/test_YoutubeDL.py b/test/test_YoutubeDL.py index 0ffcaed91..8d796bcdd 100644 --- a/test/test_YoutubeDL.py +++ b/test/test_YoutubeDL.py @@ -461,14 +461,13 @@ def test_format_selection_issue_10083(self): def test_invalid_format_specs(self): def assert_syntax_error(format_spec): - ydl = YDL({'format': format_spec}) - info_dict = _make_result([{'format_id': 'foo', 'url': TEST_URL}]) - self.assertRaises(SyntaxError, ydl.process_ie_result, info_dict) + self.assertRaises(SyntaxError, YDL, {'format': format_spec}) assert_syntax_error('bestvideo,,best') assert_syntax_error('+bestaudio') assert_syntax_error('bestvideo+') assert_syntax_error('/') + assert_syntax_error('[720width|height|tbr|abr|vbr|asr|filesize|filesize_approx|fps) - \s*(?P%s)(?P\s*\?)?\s* - (?P[0-9.]+(?:[kKmMgGtTpPeEzZyY]i?[Bb]?)?) - $ + (?Pwidth|height|tbr|abr|vbr|asr|filesize|filesize_approx|fps)\s* + (?P%s)(?P\s*\?)?\s* + (?P[0-9.]+(?:[kKmMgGtTpPeEzZyY]i?[Bb]?)?)\s* ''' % '|'.join(map(re.escape, OPERATORS.keys()))) - m = operator_rex.search(filter_spec) + m = operator_rex.fullmatch(filter_spec) if m: try: comparison_value = int(m.group('value')) @@ -1513,13 +1517,12 @@ def _build_format_filter(self, filter_spec): '$=': lambda attr, value: attr.endswith(value), '*=': lambda attr, value: value in attr, } - str_operator_rex = re.compile(r'''(?x) - \s*(?P[a-zA-Z0-9._-]+) - \s*(?P!\s*)?(?P%s)(?P\s*\?)? - \s*(?P[a-zA-Z0-9._-]+) - \s*$ + str_operator_rex = re.compile(r'''(?x)\s* + (?P[a-zA-Z0-9._-]+)\s* + (?P!\s*)?(?P%s)(?P\s*\?)?\s* + (?P[a-zA-Z0-9._-]+)\s* ''' % '|'.join(map(re.escape, STR_OPERATORS.keys()))) - m = str_operator_rex.search(filter_spec) + m = str_operator_rex.fullmatch(filter_spec) if m: comparison_value = m.group('value') str_op = STR_OPERATORS[m.group('op')] @@ -1529,7 +1532,7 @@ def _build_format_filter(self, filter_spec): op = str_op if not m: - raise ValueError('Invalid filter specification %r' % filter_spec) + raise SyntaxError('Invalid filter specification %r' % filter_spec) def _filter(f): actual_value = f.get(m.group('key')) @@ -2118,12 +2121,11 @@ def is_wellformed(f): self.list_formats(info_dict) return - req_format = self.params.get('format') - if req_format is None: + format_selector = self.format_selector + if format_selector is None: req_format = self._default_format_spec(info_dict, download=download) self.write_debug('Default format spec: %s' % req_format) - - format_selector = self.build_format_selector(req_format) + format_selector = self.build_format_selector(req_format) # While in format selection we may need to have an access to the original # format set in order to calculate some metrics or do some processing.