Skip to content

Commit bb260c2

Browse files
[3.10] bpo-44630: Fix assertion errors in csv module (GH-27127) (GH-27129)
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. Contributed-By: T. Wouters [Google] (cherry picked from commit 0093876) Co-authored-by: T. Wouters <[email protected]> Automerge-Triggered-By: GH:gpshead
1 parent 425756a commit bb260c2

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

Lib/test/test_csv.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,9 +458,15 @@ class testC(csv.excel):
458458
class testUni(csv.excel):
459459
delimiter = "\u039B"
460460

461+
class unspecified():
462+
# A class to pass as dialect but with no dialect attributes.
463+
pass
464+
461465
csv.register_dialect('testC', testC)
462466
try:
463467
self.compare_dialect_123("1,2,3\r\n")
468+
self.compare_dialect_123("1,2,3\r\n", dialect=None)
469+
self.compare_dialect_123("1,2,3\r\n", dialect=unspecified)
464470
self.compare_dialect_123("1\t2\t3\r\n", testA)
465471
self.compare_dialect_123("1:2:3\r\n", dialect=testB())
466472
self.compare_dialect_123("1|2|3\r\n", dialect='testC')

Modules/_csv.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -421,9 +421,14 @@ dialect_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
421421
Py_XINCREF(skipinitialspace);
422422
Py_XINCREF(strict);
423423
if (dialect != NULL) {
424-
#define DIALECT_GETATTR(v, n) \
425-
if (v == NULL) \
426-
v = PyObject_GetAttrString(dialect, n)
424+
#define DIALECT_GETATTR(v, n) \
425+
do { \
426+
if (v == NULL) { \
427+
v = PyObject_GetAttrString(dialect, n); \
428+
if (v == NULL) \
429+
PyErr_Clear(); \
430+
} \
431+
} while (0)
427432
DIALECT_GETATTR(delimiter, "delimiter");
428433
DIALECT_GETATTR(doublequote, "doublequote");
429434
DIALECT_GETATTR(escapechar, "escapechar");
@@ -432,7 +437,6 @@ dialect_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
432437
DIALECT_GETATTR(quoting, "quoting");
433438
DIALECT_GETATTR(skipinitialspace, "skipinitialspace");
434439
DIALECT_GETATTR(strict, "strict");
435-
PyErr_Clear();
436440
}
437441

438442
/* check types and convert to C values */

0 commit comments

Comments
 (0)