From ec22e7d1de644772be79ddab423537309af282af Mon Sep 17 00:00:00 2001 From: Thomas Wouters Date: Tue, 13 Jul 2021 23:36:57 +0200 Subject: [PATCH 1/2] Fix incorrect handling of exceptions when interpreting dialect objects in the csv module. Not clearing exceptions between calls to PyObject_GetAttrString() causes assertion failures in pydebug mode (or with assertions enabled). Add a minimal test that would've caught this (passing None as dialect, or any object that isn't a csv.Dialect subclass, which the csv module allows and caters to, even though it is not documented.) In pydebug mode, the test triggers the assertion failure in the old code. --- Lib/test/test_csv.py | 6 ++++++ Modules/_csv.c | 12 ++++++++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_csv.py b/Lib/test/test_csv.py index 225f5c7289081a0..05fea12e8a0717c 100644 --- a/Lib/test/test_csv.py +++ b/Lib/test/test_csv.py @@ -458,9 +458,15 @@ class testC(csv.excel): class testUni(csv.excel): delimiter = "\u039B" + class unspecified(): + # A class to pass as dialect but with no attributes attributes. + pass + csv.register_dialect('testC', testC) try: self.compare_dialect_123("1,2,3\r\n") + self.compare_dialect_123("1,2,3\r\n", dialect=None) + self.compare_dialect_123("1,2,3\r\n", dialect=unspecified) self.compare_dialect_123("1\t2\t3\r\n", testA) self.compare_dialect_123("1:2:3\r\n", dialect=testB()) self.compare_dialect_123("1|2|3\r\n", dialect='testC') diff --git a/Modules/_csv.c b/Modules/_csv.c index 78855b871352c5e..3109fd16bc744bf 100644 --- a/Modules/_csv.c +++ b/Modules/_csv.c @@ -421,9 +421,14 @@ dialect_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) Py_XINCREF(skipinitialspace); Py_XINCREF(strict); if (dialect != NULL) { -#define DIALECT_GETATTR(v, n) \ - if (v == NULL) \ - v = PyObject_GetAttrString(dialect, n) +#define DIALECT_GETATTR(v, n) \ + do { \ + if (v == NULL) { \ + v = PyObject_GetAttrString(dialect, n); \ + if (v == NULL) \ + PyErr_Clear(); \ + } \ + } while (0) DIALECT_GETATTR(delimiter, "delimiter"); DIALECT_GETATTR(doublequote, "doublequote"); DIALECT_GETATTR(escapechar, "escapechar"); @@ -432,7 +437,6 @@ dialect_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) DIALECT_GETATTR(quoting, "quoting"); DIALECT_GETATTR(skipinitialspace, "skipinitialspace"); DIALECT_GETATTR(strict, "strict"); - PyErr_Clear(); } /* check types and convert to C values */ From 412d620e747dc6ea936478468f65d6cc4b83fac9 Mon Sep 17 00:00:00 2001 From: Thomas Wouters Date: Tue, 13 Jul 2021 23:57:28 +0200 Subject: [PATCH 2/2] Typo fix. --- Lib/test/test_csv.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_csv.py b/Lib/test/test_csv.py index 05fea12e8a0717c..18b86aa71a531f3 100644 --- a/Lib/test/test_csv.py +++ b/Lib/test/test_csv.py @@ -459,7 +459,7 @@ class testUni(csv.excel): delimiter = "\u039B" class unspecified(): - # A class to pass as dialect but with no attributes attributes. + # A class to pass as dialect but with no dialect attributes. pass csv.register_dialect('testC', testC)