[utils] Simplify int_or_none(), based on yt-dlp 9e907ebddf9c9c3084fb1b24156c9e3786ab428e

Also swallow inf, nan
This commit is contained in:
dirkf 2022-01-27 01:09:00 +00:00
parent 7a438da3b1
commit 5988a39483

View File

@ -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):