getattr(obj, name, default) swallows non-AttributeError exceptions and leaves them set
Bug Description
getattr(obj, name, default) returns the default for any exception the attribute lookup raises, not only AttributeError, and leaves that exception set. CPython uses the default only for AttributeError and raises everything else.
This causes two faults in compiled code:
- The exception never reaches the surrounding
except, so code silently continues with the default. - The pending exception makes the next C-level call fail with
SystemError: ... returned a result with an exception set.
The cause is in BUILTIN_GETATTR (nuitka/build/static_src/HelpersBuiltin.c, about line 739). It returns default_value whether or not the error matched AttributeError, and clears the error only when it did.
if (default_value != NULL) {
if (HAS_ERROR_OCCURRED(tstate)) {
if (EXCEPTION_MATCH_BOOL_SINGLE(tstate, GET_ERROR_OCCURRED(tstate), PyExc_AttributeError)) {
CLEAR_ERROR_OCCURRED(tstate);
}
}
Py_INCREF(default_value);
return default_value;I found this because a compiled package installer treated an sdist whose build failed as having no dependencies. It then crashed with the SystemError instead of reporting the build error.
️ Environment
1. Nuitka Version, Python Version, OS, and Platform
4.3rc3
Update status: newer than stable release '4.2.1' (cached, 5 days old).
Commercial: None
Python: 3.14.7 (main, Sep 1 2026, 14:05:28) [Clang 22.1.3 ]
Flavor: Python Build Standalone
GIL: yes
Executable: ~/.local/share/uv/python/cpython-3.14-macos-aarch64-none/bin/python3.14
OS: Darwin
Arch: arm64
macOSRelease: 26.5.2
Version C compiler: clang (clang 21.0.0).This is develop at 4506e7927. The helper has no version-specific code, and a larger program showed the same SystemError with 3.13.
2. How Nuitka and Python were Installed
Python is installed with uv python install. Nuitka is a git checkout of develop.
3. Relevant PyPI Packages and Versions
None.
️ To Reproduce
1. "Hello World" Test (if applicable)
Hello world compiles and runs.
2. Short, Self-Contained, Correct, Eligible (SSCCE) Example
class Candidate:
@property
def dependencies(self):
raise ValueError("cannot build")
def dependencies_or_none(candidate):
try:
return getattr(candidate, "dependencies", ())
except ValueError:
return None
print(dependencies_or_none(Candidate()))
print(format(1, "d"))3. Nuitka Command Line Options
python -m nuitka getattr_default.pyExpected Behavior
This is CPython's output:
None
1Actual Behavior & Output
Traceback (most recent call last):
File "getattr_default.py", line 4, in dependencies
raise ValueError("cannot build")
ValueError: cannot build
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "getattr_default.py", line 14, in <module>
print(dependencies_or_none(Candidate()))
SystemError: <built-in method write of _io.TextIOWrapper object at 0x...> returned a result with an exception set
()The default () is what gets returned, and the ValueError is still pending.
Plain getattr(obj, name) and obj.name behave correctly, as do the other PyExc_AttributeError checks in static_src that I looked at, since they return the error. It is only the default-value path of this helper.
↩️ Regression (if applicable)
This is not a recent regression. The code has had this shape since at least the 2018 clang-format commit 48f85b513.
Additional Context (Optional)
With this change the reproducer matches CPython, including with a chained exception (raise ... from). I'm happy to open it as a PR if that's welcome.
if (default_value != NULL) {
+ // Only an "AttributeError" means the default, anything else is
+ // raised, as "getattr" does.
if (HAS_ERROR_OCCURRED(tstate)) {
- if (EXCEPTION_MATCH_BOOL_SINGLE(tstate, GET_ERROR_OCCURRED(tstate), PyExc_AttributeError)) {
- CLEAR_ERROR_OCCURRED(tstate);
+ if (!EXCEPTION_MATCH_BOOL_SINGLE(tstate, GET_ERROR_OCCURRED(tstate), PyExc_AttributeError)) {
+ return NULL;
}
+
+ CLEAR_ERROR_OCCURRED(tstate);
}Source: Nuitka/Nuitka