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]
This commit is contained in:
T. Wouters 2021-07-14 00:56:45 +02:00 committed by GitHub
parent 054e9c84ac
commit 0093876328
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 4 deletions

View file

@ -458,9 +458,15 @@ class TestDialectRegistry(unittest.TestCase):
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')