]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-145966: Fix _csv DIALECT_GETATTR macro silently masking non-AttributeError excepti...
authorRamin Farajpour Cami <ramin.blackhat@gmail.com>
Tue, 17 Mar 2026 15:08:53 +0000 (18:38 +0330)
committerGitHub <noreply@github.com>
Tue, 17 Mar 2026 15:08:53 +0000 (16:08 +0100)
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.

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Lib/test/test_csv.py
Misc/NEWS.d/next/Library/2026-03-15-00-00-00.gh-issue-145966.tCI0uD4I.rst [new file with mode: 0644]
Modules/_csv.c

index df79840088abc362bc5236169551325012eadf28..2e5b72742c3f4142a377ea49b7a9c2a20c8ef673 100644 (file)
@@ -1280,6 +1280,19 @@ class TestDialectValidity(unittest.TestCase):
                     self.assertRaises(ValueError, create_invalid, field_name, " ",
                                       skipinitialspace=True)
 
+    def test_dialect_getattr_non_attribute_error_propagates(self):
+        # 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 = """\
diff --git a/Misc/NEWS.d/next/Library/2026-03-15-00-00-00.gh-issue-145966.tCI0uD4I.rst b/Misc/NEWS.d/next/Library/2026-03-15-00-00-00.gh-issue-145966.tCI0uD4I.rst
new file mode 100644 (file)
index 0000000..c0d4907
--- /dev/null
@@ -0,0 +1,2 @@
+Non-:exc:`AttributeError` exceptions raised during dialect attribute lookup\r
+in :mod:`csv` are no longer silently suppressed.\r
index 1f41976e95fdb1889ece2befa6c420c89c87ab63..c48f44c0f07867b6e1f0a889e5b4a6884357d594 100644 (file)
@@ -497,13 +497,13 @@ dialect_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
     Py_XINCREF(skipinitialspace);
     Py_XINCREF(strict);
     if (dialect != NULL) {
-#define DIALECT_GETATTR(v, n)                            \
-        do {                                             \
-            if (v == NULL) {                             \
-                v = PyObject_GetAttrString(dialect, n);  \
-                if (v == NULL)                           \
-                    PyErr_Clear();                       \
-            }                                            \
+#define DIALECT_GETATTR(v, n)                                               \
+        do {                                                                \
+            if (v == NULL) {                                                \
+                if (PyObject_GetOptionalAttrString(dialect, n, &v) < 0) {   \
+                    goto err;                                               \
+                }                                                           \
+            }                                                               \
         } while (0)
         DIALECT_GETATTR(delimiter, "delimiter");
         DIALECT_GETATTR(doublequote, "doublequote");