From 0e26bfd28f27603e7039b51cbdf596c58065c6c6 Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Mon, 9 Dec 2024 10:39:41 -0600 Subject: Check for non-negative integers. --- gn_libs/mysqldb.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'gn_libs') diff --git a/gn_libs/mysqldb.py b/gn_libs/mysqldb.py index c738d96..d620b11 100644 --- a/gn_libs/mysqldb.py +++ b/gn_libs/mysqldb.py @@ -24,6 +24,11 @@ def __parse_boolean__(val: str) -> bool: return val.strip().lower() in true_vals +def __non_negative_int__(val: str) -> int: + """Convert a value to a non-negative int.""" + _val = int(val) + assert (val >= 0), f"Expected a non-negative value. Got {_val}" + return _val def __parse_db_opts__(opts: str) -> dict: """Parse database options into their appropriate values. @@ -36,7 +41,7 @@ def __parse_db_opts__(opts: str) -> dict: "local_infile", "autocommit", "binary_prefix") conversion_fns: dict[str, Callable] = { **{opt: str for opt in allowed_opts}, - "connect_timeout": int, + "connect_timeout": __non_negative_int__, "compress": __parse_boolean__, "use_unicode": __parse_boolean__, # "cursorclass": __load_cursor_class__ -- cgit v1.2.3