Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Lib/test/test_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -1281,6 +1281,20 @@ class mydialect(csv.Dialect):
skipinitialspace=True)


def test_dialect_getattr_non_attribute_error_propagates(self):
Comment thread
raminfp marked this conversation as resolved.
# gh-145966: non-AttributeError exceptions raised by __getattr__
# during dialect attribute lookup must propagate, not be silenced.
class BadDialect:
def __getattr__(self, name):
raise RuntimeError("boom")

with self.assertRaises(RuntimeError):
csv.reader([], dialect=BadDialect())

with self.assertRaises(RuntimeError):
csv.writer(StringIO(), dialect=BadDialect())


class TestSniffer(unittest.TestCase):
sample1 = """\
Harry's, Arlington Heights, IL, 2/1/03, Kimi Hayes
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Fixed ``DIALECT_GETATTR`` macro in :mod:`csv` to only clear
Comment thread
raminfp marked this conversation as resolved.
Outdated
:exc:`AttributeError` exceptions. Previously, all exceptions (including
:exc:`MemoryError` and :exc:`KeyboardInterrupt`) were silently suppressed
when looking up dialect attributes.
9 changes: 7 additions & 2 deletions Modules/_csv.c
Original file line number Diff line number Diff line change
Expand Up @@ -501,8 +501,13 @@ dialect_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
do { \
if (v == NULL) { \
v = PyObject_GetAttrString(dialect, n); \
if (v == NULL) \
PyErr_Clear(); \
if (v == NULL) { \
if (PyErr_ExceptionMatches( \
Comment thread
raminfp marked this conversation as resolved.
Outdated
PyExc_AttributeError)) \
PyErr_Clear(); \
else \
goto err; \
} \
} \
} while (0)
DIALECT_GETATTR(delimiter, "delimiter");
Expand Down
Loading