Skip to content

Commit

Permalink
misc: Allow BytesConverter to interpret "0" string
Browse files Browse the repository at this point in the history
  • Loading branch information
deajan committed Apr 23, 2024
1 parent 2fe5350 commit 5d3517d
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions ofunctions/misc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
__copyright__ = "Copyright (C) 2014-2024 Orsiris de Jong"
__description__ = "Collection of various functions"
__licence__ = "BSD 3 Clause"
__version__ = "1.7.1"
__build__ = "2024020201"
__version__ = "1.7.2"
__build__ = "2024042301"
__compat__ = "python2.7+"


Expand Down Expand Up @@ -390,13 +390,16 @@ def __new__(cls, value, *args, **kwargs):
converted_value /= 8
break
if not converted_value and converted_value != 0:
needs_raise = False
try:
converted_value = float(value)
# Here we intercept the original ValueError and replace it with ours
except ValueError:
pass
raise ValueError(
'Given string "{}" cannot be converted to bytes'.format(value)
)
needs_raise = True
if needs_raise:
raise ValueError(
'Given string "{}" cannot be converted to bytes'.format(value)
)
value = converted_value
if value < 0:
raise ValueError("Negative bytes should not exist")
Expand All @@ -413,13 +416,15 @@ def _from_units_to_bytes(self, string):
value /= 8
break
if not value:
needs_raise = False
try:
return float(string)
except ValueError:
pass
raise ValueError(
'Given string "{}" cannot be converted to bytes'.format(string)
)
needs_raise = True
if needs_raise:
raise ValueError(
'Given string "{}" cannot be converted to bytes'.format(string)
)
return value

def _from_bytes_to_unit(self, unit):
Expand Down

0 comments on commit 5d3517d

Please sign in to comment.