Skip to content

Commit 6befd8c

Browse files
committed
Fix AttributeError when using Nuitka-compiled models
When using tortoise-orm with Nuitka-compiled Python code, the _get_comments function fails with an AttributeError because inspect.getsourcefile() returns None for compiled modules, and the code attempts to call .endswith() on None. This error occurs in the following stack trace: - _get_comments() calls inspect.getsource(cls) - inspect.getsource() -> inspect.getsourcelines() -> inspect.findsource() - inspect.findsourcefile() returns None for Nuitka-compiled modules - inspect.findsource() tries to call filename.endswith(), causing AttributeError The fix adds AttributeError to the exception handling in _get_comments(), allowing the function to gracefully return an empty dict when source code cannot be retrieved from compiled modules. Fixes: AttributeError: 'NoneType' object has no attribute 'endswith'
1 parent 1d2400b commit 6befd8c

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

tortoise/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ def _get_comments(cls: type[Model]) -> dict[str, str]:
151151
"""
152152
try:
153153
source = inspect.getsource(cls)
154-
except (TypeError, OSError): # pragma: nocoverage
154+
except (AttributeError, TypeError, OSError): # pragma: nocoverage
155155
return {}
156156
comments = {}
157157

0 commit comments

Comments
 (0)