[Python-checkins] bpo-44630: Fix assertion errors in csv module (GH-27127)
miss-islington
webhook-mailer at python.org
Tue Jul 13 19:20:33 EDT 2021
https://github.com/python/cpython/commit/fe73509c5ba3844b45a2253967522531ab80c849
commit: fe73509c5ba3844b45a2253967522531ab80c849
branch: 3.9
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2021-07-13T16:20:28-07:00
summary:
bpo-44630: Fix assertion errors in csv module (GH-27127)
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 0093876328afa330224c9d887c18dee0b3117852)
Co-authored-by: T. Wouters <thomas at python.org>
files:
M Lib/test/test_csv.py
M Modules/_csv.c
diff --git a/Lib/test/test_csv.py b/Lib/test/test_csv.py
index 2dab17dc073b1b..89fec4b22e07b2 100644
--- a/Lib/test/test_csv.py
+++ b/Lib/test/test_csv.py
@@ -442,9 +442,15 @@ class testC(csv.excel):
class testUni(csv.excel):
delimiter = "\u039B"
+ class unspecified():
+ # A class to pass as dialect but with no dialect 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 59109b018e7562..7e06db46b496fb 100644
--- a/Modules/_csv.c
+++ b/Modules/_csv.c
@@ -399,9 +399,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");
@@ -410,7 +415,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 */
More information about the Python-checkins
mailing list