From 323de6506dc83e5e49577700af81771d90ea3465 Mon Sep 17 00:00:00 2001 From: Zackery Spytz Date: Fri, 29 Nov 2019 13:22:34 -0700 Subject: [PATCH] bpo-38942: Fix possible assertion failures in csv.Dialect() --- .../2019-11-29-13-22-14.bpo-38942.0zWIcK.rst | 1 + Modules/_csv.c | 11 ++++++++--- 2 files changed, 9 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2019-11-29-13-22-14.bpo-38942.0zWIcK.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2019-11-29-13-22-14.bpo-38942.0zWIcK.rst b/Misc/NEWS.d/next/Core and Builtins/2019-11-29-13-22-14.bpo-38942.0zWIcK.rst new file mode 100644 index 00000000000..65aeaf79f44 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2019-11-29-13-22-14.bpo-38942.0zWIcK.rst @@ -0,0 +1 @@ +Fix possible assertion failures when creating a :class:`csv.Dialect` object. diff --git a/Modules/_csv.c b/Modules/_csv.c index aaf377650c0..fd42a848645 100644 --- a/Modules/_csv.c +++ b/Modules/_csv.c @@ -394,8 +394,14 @@ dialect_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) Py_XINCREF(strict); if (dialect != NULL) { #define DIALECT_GETATTR(v, n) \ - if (v == NULL) \ - v = PyObject_GetAttrString(dialect, n) + if (v == NULL) { \ + if ((v = PyObject_GetAttrString(dialect, n)) == NULL) { \ + if (!PyErr_ExceptionMatches(PyExc_AttributeError)) { \ + goto err; \ + } \ + PyErr_Clear(); \ + } \ + } DIALECT_GETATTR(delimiter, "delimiter"); DIALECT_GETATTR(doublequote, "doublequote"); DIALECT_GETATTR(escapechar, "escapechar"); @@ -404,7 +410,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 */