Skip to content

gh-145966: Fix _csv DIALECT_GETATTR macro silently masking non-AttributeError exceptions#145974

Open
raminfp wants to merge 3 commits intopython:mainfrom
raminfp:fix-csv-dialect-getattr-exception-masking
Open

gh-145966: Fix _csv DIALECT_GETATTR macro silently masking non-AttributeError exceptions#145974
raminfp wants to merge 3 commits intopython:mainfrom
raminfp:fix-csv-dialect-getattr-exception-masking

Conversation

@raminfp
Copy link
Contributor

@raminfp raminfp commented Mar 15, 2026

Only clear AttributeError in DIALECT_GETATTR; let other exceptions (e.g. MemoryError, KeyboardInterrupt) propagate.

…AttributeError exceptions

The DIALECT_GETATTR macro in dialect_new() unconditionally called
PyErr_Clear() when PyObject_GetAttrString() failed, which suppressed
all exceptions including MemoryError, KeyboardInterrupt, and
RuntimeError. Now only AttributeError is cleared; other exceptions
propagate via the existing error handling path.
@bedevere-app
Copy link

bedevere-app bot commented Mar 15, 2026

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@raminfp raminfp requested a review from picnixz March 15, 2026 10:47
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Copy link
Member

@StanFromIreland StanFromIreland left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@encukou
Copy link
Member

encukou commented Mar 16, 2026

Could you use PyObject_GetOptionalAttrString instead?

Comment on lines +500 to +512
#define DIALECT_GETATTR(v, n) \
do { \
if (v == NULL) { \
v = PyObject_GetAttrString(dialect, n); \
if (v == NULL) { \
if (PyErr_ExceptionMatches(PyExc_AttributeError)) { \
PyErr_Clear(); \
} \
else { \
goto err; \
} \
} \
} \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#define DIALECT_GETATTR(v, n) \
do { \
if (v == NULL) { \
v = PyObject_GetAttrString(dialect, n); \
if (v == NULL) { \
if (PyErr_ExceptionMatches(PyExc_AttributeError)) { \
PyErr_Clear(); \
} \
else { \
goto err; \
} \
} \
} \
#define DIALECT_GETATTR(v, n) \
do { \
if (v == NULL) { \
if (PyObject_GetOptionalAttrString(dialect, n, &v) < 0) { \
goto err; \
} \
} \

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@encukou Something like that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants