diff --git a/sentry_sdk/utils.py b/sentry_sdk/utils.py index 7a8917fecc..0fead48377 100644 --- a/sentry_sdk/utils.py +++ b/sentry_sdk/utils.py @@ -1501,7 +1501,7 @@ def qualname_from_function(func): # Python 3: methods, functions, classes if func_qualname is not None: - if hasattr(func, "__module__"): + if hasattr(func, "__module__") and isinstance(func.__module__, str): func_qualname = func.__module__ + "." + func_qualname func_qualname = prefix + func_qualname + suffix diff --git a/tests/test_utils.py b/tests/test_utils.py index 6e01bb4f3a..894638bf4d 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -951,3 +951,23 @@ def test_format_timestamp_naive(): # Ensure that some timestamp is returned, without error. We currently treat these as local time, but this is an # implementation detail which we should not assert here. assert re.fullmatch(timestamp_regex, format_timestamp(datetime_object)) + + +def test_qualname_from_function_inner_function(): + def test_function(): ... + + assert ( + sentry_sdk.utils.qualname_from_function(test_function) + == "tests.test_utils.test_qualname_from_function_inner_function..test_function" + ) + + +def test_qualname_from_function_none_name(): + def test_function(): ... + + test_function.__module__ = None + + assert ( + sentry_sdk.utils.qualname_from_function(test_function) + == "test_qualname_from_function_none_name..test_function" + )