Squashed 'kiauh/core/submodules/simple_config_parser/' changes from 3a006f1..2a0f266

2a0f266 fix: catch TypeError and AttributeError in _get_conv()

git-subtree-dir: kiauh/core/submodules/simple_config_parser
git-subtree-split: 2a0f266926a5d03f8451063fc91221410fc4f7ac
This commit is contained in:
dw-0
2024-09-26 20:45:12 +02:00
parent 681e5d8d05
commit d4af7b0b11
3 changed files with 30 additions and 11 deletions

View File

@@ -311,7 +311,7 @@ class SimpleConfigParser:
"""Return the value of the given option in the given section as a converted value""" """Return the value of the given option in the given section as a converted value"""
try: try:
return conv(self.getval(section, option, fallback)) return conv(self.getval(section, option, fallback))
except ValueError as e: except (ValueError, TypeError, AttributeError) as e:
if fallback is not _UNSET: if fallback is not _UNSET:
return fallback return fallback
raise ValueError( raise ValueError(

View File

@@ -57,6 +57,7 @@ def test_getval(parser):
def test_getval_fallback(parser): def test_getval_fallback(parser):
assert parser.getval("section_1", "option_128", "fallback") == "fallback" assert parser.getval("section_1", "option_128", "fallback") == "fallback"
assert parser.getval("section_1", "option_128", None) is None
def test_getval_exceptions(parser): def test_getval_exceptions(parser):
@@ -89,6 +90,7 @@ def test_getint_from_boolean(parser):
def test_getint_fallback(parser): def test_getint_fallback(parser):
assert parser.getint("section_1", "option_128", 128) == 128 assert parser.getint("section_1", "option_128", 128) == 128
assert parser.getint("section_1", "option_128", None) is None
def test_getboolean(parser): def test_getboolean(parser):
@@ -115,6 +117,7 @@ def test_getboolean_from_float(parser):
def test_getboolean_fallback(parser): def test_getboolean_fallback(parser):
assert parser.getboolean("section_1", "option_128", True) is True assert parser.getboolean("section_1", "option_128", True) is True
assert parser.getboolean("section_1", "option_128", False) is False assert parser.getboolean("section_1", "option_128", False) is False
assert parser.getboolean("section_1", "option_128", None) is None
def test_getfloat(parser): def test_getfloat(parser):
@@ -139,6 +142,7 @@ def test_getfloat_from_boolean(parser):
def test_getfloat_fallback(parser): def test_getfloat_fallback(parser):
assert parser.getfloat("section_1", "option_128", 1.234) == 1.234 assert parser.getfloat("section_1", "option_128", 1.234) == 1.234
assert parser.getfloat("section_1", "option_128", None) is None
def test_set_existing_option(parser): def test_set_existing_option(parser):

View File

@@ -25,50 +25,65 @@ def parser():
return parser return parser
def test_get_conv(parser): def test_get_int_conv(parser):
# Test conversion to int
should_be_int = parser._get_conv("section_1", "option_1_2", int) should_be_int = parser._get_conv("section_1", "option_1_2", int)
assert isinstance(should_be_int, int) assert isinstance(should_be_int, int)
# Test conversion to float
def test_get_float_conv(parser):
should_be_float = parser._get_conv("section_1", "option_1_3", float) should_be_float = parser._get_conv("section_1", "option_1_3", float)
assert isinstance(should_be_float, float) assert isinstance(should_be_float, float)
# Test conversion to boolean
def test_get_bool_conv(parser):
should_be_bool = parser._get_conv( should_be_bool = parser._get_conv(
"section_1", "option_1_1", parser._convert_to_boolean "section_1", "option_1_1", parser._convert_to_boolean
) )
assert isinstance(should_be_bool, bool) assert isinstance(should_be_bool, bool)
# Test fallback for int
def test_get_int_conv_fallback(parser):
should_be_fallback_int = parser._get_conv( should_be_fallback_int = parser._get_conv(
"section_1", "option_128", int, fallback=128 "section_1", "option_128", int, fallback=128
) )
assert isinstance(should_be_fallback_int, int) assert isinstance(should_be_fallback_int, int)
assert should_be_fallback_int == 128 assert should_be_fallback_int == 128
assert parser._get_conv("section_1", "option_128", int, None) is None
# Test fallback for float
def test_get_float_conv_fallback(parser):
should_be_fallback_float = parser._get_conv( should_be_fallback_float = parser._get_conv(
"section_1", "option_128", float, fallback=1.234 "section_1", "option_128", float, fallback=1.234
) )
assert isinstance(should_be_fallback_float, float) assert isinstance(should_be_fallback_float, float)
assert should_be_fallback_float == 1.234 assert should_be_fallback_float == 1.234
# Test fallback for boolean assert parser._get_conv("section_1", "option_128", float, None) is None
def test_get_bool_conv_fallback(parser):
should_be_fallback_bool = parser._get_conv( should_be_fallback_bool = parser._get_conv(
"section_1", "option_128", parser._convert_to_boolean, fallback=True "section_1", "option_128", parser._convert_to_boolean, fallback=True
) )
assert isinstance(should_be_fallback_bool, bool) assert isinstance(should_be_fallback_bool, bool)
assert should_be_fallback_bool is True assert should_be_fallback_bool is True
# Test ValueError exception for invalid int conversion assert (
parser._get_conv("section_1", "option_128", parser._convert_to_boolean, None)
is None
)
def test_get_int_conv_exception(parser):
with pytest.raises(ValueError): with pytest.raises(ValueError):
parser._get_conv("section_1", "option_1", int) parser._get_conv("section_1", "option_1", int)
# Test ValueError exception for invalid float conversion
def test_get_float_conv_exception(parser):
with pytest.raises(ValueError): with pytest.raises(ValueError):
parser._get_conv("section_1", "option_1", float) parser._get_conv("section_1", "option_1", float)
# Test ValueError exception for invalid boolean conversion
def test_get_bool_conv_exception(parser):
with pytest.raises(ValueError): with pytest.raises(ValueError):
parser._get_conv("section_1", "option_1", parser._convert_to_boolean) parser._get_conv("section_1", "option_1", parser._convert_to_boolean)