From 6b8a330e85ad6a41f5dadef86e6f453666b7b824 Mon Sep 17 00:00:00 2001 From: Varad Date: Mon, 17 Oct 2022 22:59:13 -0500 Subject: [PATCH 1/2] Fixing format_traceback's change of signature --- azure_functions_worker/logging.py | 11 +++++++++-- azure_functions_worker/utils/common.py | 4 ++-- tests/unittests/test_logging.py | 25 +++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/azure_functions_worker/logging.py b/azure_functions_worker/logging.py index 2f701d4bd..43e8e3de1 100644 --- a/azure_functions_worker/logging.py +++ b/azure_functions_worker/logging.py @@ -23,8 +23,15 @@ def format_exception(exception): msg = str(exception) + "\n" - msg += ''.join(traceback.format_exception( - etype=type(exception), value=exception, tb=exception.__traceback__)) + if sys.version_info.minor < 10: + msg += ''.join(traceback.format_exception( + etype=type(exception), + tb=exception.__traceback__, + value=exception)) + elif sys.version_info.minor == 10: + msg += ''.join(traceback.format_exception(exception)) + else: + return exception return msg diff --git a/azure_functions_worker/utils/common.py b/azure_functions_worker/utils/common.py index 350f858f2..f508a78ff 100644 --- a/azure_functions_worker/utils/common.py +++ b/azure_functions_worker/utils/common.py @@ -11,14 +11,14 @@ def is_true_like(setting: str) -> bool: if setting is None: return False - return setting.lower().strip() in ['1', 'true', 't', 'yes', 'y'] + return setting.lower().strip() in {'1', 'true', 't', 'yes', 'y'} def is_false_like(setting: str) -> bool: if setting is None: return False - return setting.lower().strip() in ['0', 'false', 'f', 'no', 'n'] + return setting.lower().strip() in {'0', 'false', 'f', 'no', 'n'} def is_envvar_true(env_key: str) -> bool: diff --git a/tests/unittests/test_logging.py b/tests/unittests/test_logging.py index 3d3465c63..1a6852331 100644 --- a/tests/unittests/test_logging.py +++ b/tests/unittests/test_logging.py @@ -3,6 +3,7 @@ import unittest from azure_functions_worker import logging as flog +from azure_functions_worker.logging import format_exception class TestLogging(unittest.TestCase): @@ -33,3 +34,27 @@ def test_customer_log_namespace(self): self.assertFalse(flog.is_system_log_category('protobuf')) self.assertFalse(flog.is_system_log_category('root')) self.assertFalse(flog.is_system_log_category('')) + + def test_format_exception(self): + def call0(fn): + call1(fn) + + def call1(fn): + call2(fn) + + def call2(fn): + fn() + + def raising_function(): + raise ValueError("Value error being raised.", ) + + try: + call0(raising_function) + except ValueError as e: + processed_exception = format_exception(e) + self.assertIn("call0", processed_exception) + self.assertIn("call1", processed_exception) + self.assertIn("call2", processed_exception) + self.assertIn("f", processed_exception) + self.assertIn("tests/unittests/test_logging.py", + processed_exception) From 699330a85b2e1b60fc06c14c72076eefaec7fe87 Mon Sep 17 00:00:00 2001 From: Varad Date: Tue, 18 Oct 2022 15:45:32 -0500 Subject: [PATCH 2/2] Making the returns explicit --- azure_functions_worker/logging.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/azure_functions_worker/logging.py b/azure_functions_worker/logging.py index 43e8e3de1..99b3f83b3 100644 --- a/azure_functions_worker/logging.py +++ b/azure_functions_worker/logging.py @@ -21,7 +21,7 @@ error_handler: Optional[logging.Handler] = None -def format_exception(exception): +def format_exception(exception: Exception) -> str: msg = str(exception) + "\n" if sys.version_info.minor < 10: msg += ''.join(traceback.format_exception( @@ -31,7 +31,7 @@ def format_exception(exception): elif sys.version_info.minor == 10: msg += ''.join(traceback.format_exception(exception)) else: - return exception + msg = str(exception) return msg