From aec4c22f595ee6c79bbc1aa0843dd16a4f55c602 Mon Sep 17 00:00:00 2001 From: doe1080 <98906116+doe1080@users.noreply.github.com> Date: Mon, 19 May 2025 00:42:18 +0900 Subject: [PATCH 1/2] [utils] str_to_int: support optional decimal-dot parsing --- test/test_utils.py | 1 + yt_dlp/utils/_utils.py | 14 ++++++++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/test/test_utils.py b/test/test_utils.py index aedb565ec1..fddc9dc586 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -604,6 +604,7 @@ def test_str_to_int(self): self.assertEqual(str_to_int(523), 523) self.assertEqual(str_to_int('noninteger'), None) self.assertEqual(str_to_int([]), None) + self.assertEqual(str_to_int('123.456', dot_decimal=True), 123) def test_url_basename(self): self.assertEqual(url_basename('http://foo.de/'), '') diff --git a/yt_dlp/utils/_utils.py b/yt_dlp/utils/_utils.py index 20aa341ca3..944f0f7146 100644 --- a/yt_dlp/utils/_utils.py +++ b/yt_dlp/utils/_utils.py @@ -2010,13 +2010,19 @@ def str_or_none(v, default=None): return default if v is None else str(v) -def str_to_int(int_str): +def str_to_int(int_str, *, dot_decimal=False): """ A more relaxed version of int_or_none """ if isinstance(int_str, int): return int_str - elif isinstance(int_str, str): - int_str = re.sub(r'[,\.\+]', '', int_str) - return int_or_none(int_str) + if not isinstance(int_str, str): + return None + int_str = re.sub(r'[,+]', '', int_str) + + if dot_decimal: + f = float_or_none(int_str) + return int(f) if f is not None else None + else: + return int_or_none(int_str.replace('.', '')) @partial_application From a2fca0bcf622c76e3f288c865ab88f8a8dc54877 Mon Sep 17 00:00:00 2001 From: doe1080 <98906116+doe1080@users.noreply.github.com> Date: Mon, 19 May 2025 00:59:13 +0900 Subject: [PATCH 2/2] fix --- yt_dlp/utils/_utils.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/yt_dlp/utils/_utils.py b/yt_dlp/utils/_utils.py index 944f0f7146..ebdfa864ce 100644 --- a/yt_dlp/utils/_utils.py +++ b/yt_dlp/utils/_utils.py @@ -2021,8 +2021,7 @@ def str_to_int(int_str, *, dot_decimal=False): if dot_decimal: f = float_or_none(int_str) return int(f) if f is not None else None - else: - return int_or_none(int_str.replace('.', '')) + return int_or_none(int_str.replace('.', '')) @partial_application