From 8934579cda9e9d0464c615fe67a08e50270689ce Mon Sep 17 00:00:00 2001 From: Mike Gray Date: Sat, 29 Oct 2022 21:23:07 -0500 Subject: [PATCH 1/3] chore: convert try/except to explicit checks Rather than causing an exception and handling it, this change explicitly checks the type of certain arguments and only converts strings to integers if needed. Both this change and the previous implementation don't handle cases where the string passed is not numeric. However, I'm just getting familiar with the codebase, so this is possibly handled elsewhere. --- evennia/utils/funcparser.py | 33 ++++++++++++++------------------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/evennia/utils/funcparser.py b/evennia/utils/funcparser.py index 911b6c39d1..da64397cb8 100644 --- a/evennia/utils/funcparser.py +++ b/evennia/utils/funcparser.py @@ -657,10 +657,9 @@ def funcparser_callable_eval(*args, **kwargs): def funcparser_callable_toint(*args, **kwargs): """Usage: $toint(43.0) -> 43""" inp = funcparser_callable_eval(*args, **kwargs) - try: - return int(inp) - except TypeError: - return inp + if isinstance(inp, str) and inp.isnumeric(): + inp = int(inp) + return inp def funcparser_callable_int2str(*args, **kwargs): @@ -674,10 +673,9 @@ def funcparser_callable_int2str(*args, **kwargs): """ if not args: return "" - try: + number = args[0] + if isinstance(args[0], str) and args[0].isnumeric(): number = int(args[0]) - except ValueError: - return args[0] return int2str(number) @@ -900,10 +898,9 @@ def funcparser_callable_pad(*args, **kwargs): text, *rest = args nrest = len(rest) - try: - width = int(kwargs.get("width", rest[0] if nrest > 0 else _CLIENT_DEFAULT_WIDTH)) - except TypeError: - width = _CLIENT_DEFAULT_WIDTH + width = kwargs.get("width", rest[0] if nrest > 0 else _CLIENT_DEFAULT_WIDTH) + if isinstance(width, str) and width.isnumeric(): + width = int(width) align = kwargs.get("align", rest[1] if nrest > 1 else "c") fillchar = kwargs.get("fillchar", rest[2] if nrest > 2 else " ") @@ -932,10 +929,9 @@ def funcparser_callable_crop(*args, **kwargs): return "" text, *rest = args nrest = len(rest) - try: - width = int(kwargs.get("width", rest[0] if nrest > 0 else _CLIENT_DEFAULT_WIDTH)) - except TypeError: - width = _CLIENT_DEFAULT_WIDTH + width = kwargs.get("width", rest[0] if nrest > 0 else _CLIENT_DEFAULT_WIDTH) + if isinstance(width, str) and width.isnumeric(): + width = int(width) suffix = kwargs.get("suffix", rest[1] if nrest > 1 else "[...]") return crop(str(text), width=width, suffix=str(suffix)) @@ -949,10 +945,9 @@ def funcparser_callable_space(*args, **kwarg): """ if not args: return "" - try: - width = int(args[0]) - except TypeError: - width = 1 + width = args[0] + if isinstance(width, str) and width.isnumeric(): + width = int(width) return " " * width From bafa9f05bb69408229b8c6f1b5da3d918105815b Mon Sep 17 00:00:00 2001 From: Mike Gray Date: Mon, 31 Oct 2022 20:35:22 -0500 Subject: [PATCH 2/3] chore: revert to try/except and replace TypeError --- evennia/utils/funcparser.py | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/evennia/utils/funcparser.py b/evennia/utils/funcparser.py index da64397cb8..4019bb9cec 100644 --- a/evennia/utils/funcparser.py +++ b/evennia/utils/funcparser.py @@ -657,9 +657,10 @@ def funcparser_callable_eval(*args, **kwargs): def funcparser_callable_toint(*args, **kwargs): """Usage: $toint(43.0) -> 43""" inp = funcparser_callable_eval(*args, **kwargs) - if isinstance(inp, str) and inp.isnumeric(): - inp = int(inp) - return inp + try: + return int(inp) + except ValueError: + return inp def funcparser_callable_int2str(*args, **kwargs): @@ -673,9 +674,10 @@ def funcparser_callable_int2str(*args, **kwargs): """ if not args: return "" - number = args[0] - if isinstance(args[0], str) and args[0].isnumeric(): + try: number = int(args[0]) + except ValueError: + return args[0] return int2str(number) @@ -898,9 +900,10 @@ def funcparser_callable_pad(*args, **kwargs): text, *rest = args nrest = len(rest) - width = kwargs.get("width", rest[0] if nrest > 0 else _CLIENT_DEFAULT_WIDTH) - if isinstance(width, str) and width.isnumeric(): - width = int(width) + try: + width = int(kwargs.get("width", rest[0] if nrest > 0 else _CLIENT_DEFAULT_WIDTH)) + except ValueError: + width = _CLIENT_DEFAULT_WIDTH align = kwargs.get("align", rest[1] if nrest > 1 else "c") fillchar = kwargs.get("fillchar", rest[2] if nrest > 2 else " ") @@ -929,9 +932,10 @@ def funcparser_callable_crop(*args, **kwargs): return "" text, *rest = args nrest = len(rest) - width = kwargs.get("width", rest[0] if nrest > 0 else _CLIENT_DEFAULT_WIDTH) - if isinstance(width, str) and width.isnumeric(): - width = int(width) + try: + width = int(kwargs.get("width", rest[0] if nrest > 0 else _CLIENT_DEFAULT_WIDTH)) + except ValueError: + width = _CLIENT_DEFAULT_WIDTH suffix = kwargs.get("suffix", rest[1] if nrest > 1 else "[...]") return crop(str(text), width=width, suffix=str(suffix)) @@ -945,9 +949,10 @@ def funcparser_callable_space(*args, **kwarg): """ if not args: return "" - width = args[0] - if isinstance(width, str) and width.isnumeric(): - width = int(width) + try: + width = int(args[0]) + except ValueError: + width = 1 return " " * width @@ -975,12 +980,12 @@ def funcparser_callable_justify(*args, **kwargs): lrest = len(rest) try: width = int(kwargs.get("width", rest[0] if lrest > 0 else _CLIENT_DEFAULT_WIDTH)) - except TypeError: + except ValueError: width = _CLIENT_DEFAULT_WIDTH align = str(kwargs.get("align", rest[1] if lrest > 1 else "f")) try: indent = int(kwargs.get("indent", rest[2] if lrest > 2 else 0)) - except TypeError: + except ValueError: indent = 0 return justify(str(text), width=width, align=align, indent=indent) From 98d9e5d198ae817a06fa561fa59a0b9c86e7695c Mon Sep 17 00:00:00 2001 From: Mike Gray Date: Mon, 31 Oct 2022 20:55:37 -0500 Subject: [PATCH 3/3] fix: allow for corner case --- evennia/utils/funcparser.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/evennia/utils/funcparser.py b/evennia/utils/funcparser.py index 4019bb9cec..1265a561eb 100644 --- a/evennia/utils/funcparser.py +++ b/evennia/utils/funcparser.py @@ -659,6 +659,8 @@ def funcparser_callable_toint(*args, **kwargs): inp = funcparser_callable_eval(*args, **kwargs) try: return int(inp) + except TypeError: + return inp except ValueError: return inp