From 5988a39483565407b9c952b1d24c8af48d46e822 Mon Sep 17 00:00:00 2001 From: dirkf Date: Thu, 27 Jan 2022 01:09:00 +0000 Subject: [PATCH] [utils] Simplify int_or_none(), based on yt-dlp 9e907ebddf9c9c3084fb1b24156c9e3786ab428e Also swallow inf, nan --- youtube_dl/utils.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/youtube_dl/utils.py b/youtube_dl/utils.py index e7e6bdbcd..5a1c78f4c 100644 --- a/youtube_dl/utils.py +++ b/youtube_dl/utils.py @@ -3900,16 +3900,16 @@ class PUTRequest(compat_urllib_request.Request): def int_or_none(v, scale=1, default=None, get_attr=None, invscale=1, base=None): - if get_attr: - if v is not None: - v = getattr(v, get_attr, None) - if v in (None, ''): - return default - try: - # like int, raise if base is specified and v is not a string - return (int(v) if base is None else int(v, base=base)) * invscale // scale - except (ValueError, TypeError, OverflowError): - return default + if get_attr and v is not None: + v = getattr(v, get_attr, None) + result = default + if v not in (None, ''): + try: + # like int, raise if base is specified and v is not a string + result = (int(v) if base is None else int(v, base=base)) * invscale // scale + except (ValueError, TypeError, OverflowError): + pass + return result def str_or_none(v, default=None):