classification
Title: csv: Confusing error message when giving invalid quotechar in initializing dialect
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: iritkatriel, r.david.murray, serhiy.storchaka, vajrasky
Priority: normal Keywords: easy

Created on 2013-12-20 04:38 by vajrasky, last changed 2021-09-10 18:01 by iritkatriel.

Files
File name Uploaded Description Edit
fix_handling_invalid_quotechar.patch vajrasky, 2013-12-20 07:44 For Python 3.3 and 3.4 review
fix_handling_invalid_quotechar_python27.patch vajrasky, 2013-12-20 07:44 For Python 2.7 review
Messages (4)
msg206663 - (view) Author: Vajrasky Kok (vajrasky) * Date: 2013-12-20 04:38
Python 3.4.0b1 (default:13a505260f17, Dec 20 2013, 12:02:44) 
[GCC 4.7.2] on linux
>>> import _csv
>>> import csv
>>> _csv.Dialect(quotechar=b'+')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: "quotechar" must be string, not bytes

Hey, that's not true. Quotechar can be None.

>>> _csv.Dialect(quotechar=None)
<_csv.Dialect object at 0x7f64a8534790>

>>> _csv.Dialect(quotechar="cutecat")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: "quotechar" must be an 1-character string

That's not strictly true. Quotechar can be 0-character string in certain situation.

>>> _csv.Dialect(quotechar="", quoting=csv.QUOTE_NONE)
<_csv.Dialect object at 0x7f64a85345f0>

Python 2.7 suffers the same issue.
msg206667 - (view) Author: Vajrasky Kok (vajrasky) * Date: 2013-12-20 07:29
Here is the preliminary patch for Python 3.3 and 3.4.
msg206668 - (view) Author: Vajrasky Kok (vajrasky) * Date: 2013-12-20 07:36
Here is the preliminary patch for Python 2.7.
msg401605 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-09-10 18:01
The patch needs to be converted to a GitHub PR.

Reproduced on 3.11: 

>>> import _csv
>>> import csv
>>> _csv.Dialect(quotechar=b'+')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: "quotechar" must be string, not bytes
>>> _csv.Dialect(quotechar=None)
<_csv.Dialect object at 0x105acfe40>
>>> _csv.Dialect(quotechar="cutecat")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: "quotechar" must be a 1-character string
>>> _csv.Dialect(quotechar="", quoting=csv.QUOTE_NONE)
<_csv.Dialect object at 0x105948c80>
History
Date User Action Args
2021-09-10 18:01:26iritkatrielsetnosy: + iritkatriel
versions: + Python 3.9, Python 3.10, Python 3.11, - Python 2.7, Python 3.3, Python 3.4
messages: + msg401605

keywords: + easy, - patch
title: Confusing error message when giving invalid quotechar in initializing csv dialect -> csv: Confusing error message when giving invalid quotechar in initializing dialect
2013-12-20 07:44:25vajraskysetfiles: + fix_handling_invalid_quotechar_python27.patch
2013-12-20 07:44:13vajraskysetfiles: - fix_handling_invalid_quotechar_python27.patch
2013-12-20 07:44:05vajraskysetfiles: + fix_handling_invalid_quotechar.patch
2013-12-20 07:43:48vajraskysetfiles: - fix_handling_invalid_quotechar.patch
2013-12-20 07:36:27vajraskysetfiles: + fix_handling_invalid_quotechar_python27.patch

messages: + msg206668
2013-12-20 07:29:15vajraskysetfiles: + fix_handling_invalid_quotechar.patch
keywords: + patch
messages: + msg206667
2013-12-20 04:38:14vajraskycreate