bpo-20028: Improve error message of csv.Dialect when initializing #28705
Conversation
|
@smontanaro Can you please take a look? |
Modules/_csv.c
Outdated
@@ -457,7 +465,7 @@ dialect_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) | |||
goto err; | |||
if (self->delimiter == 0) { | |||
PyErr_SetString(PyExc_TypeError, | |||
"\"delimiter\" must be a 1-character string"); | |||
"delimiter must be set"); |
I think "set" might confuse naive readers. What was wrong with the previous spelling? Also, note that "delimiter" was quoted because it was explicitly referring to the "dellimiter" attribute.
Okay, I agree that the message doesn't need to be changed.
It was made consistent with error messages for other arguments.
@iritkatriel While the new test ("!=" vs ">") is technically more correct and might be worthwhile in 3.11, I don't think that change can be backported, since it might break existing code. |
@iritkatriel gentle ping :) |
Looks fine, would be nice to add a coauthored-by crediting the original patch author. |
@serhiy-storchaka self._read_test(['a,\\b,c'], [['a', '\\b', 'c']], escapechar='')
# TypeError: "escapechar" must be a 1-character string |
Modules/_csv.c
Outdated
PyErr_Format(PyExc_TypeError, | ||
"\"%s\" must be string, not %.200s", name, | ||
Py_TYPE(src)->tp_name); | ||
if (strcmp(name, "delimiter") == 0) { |
If we tweak this error message, we should also raise the same error if delimiter=None
.
It may be simpler to introduce different function _set_char_or_none()
.
@serhiy-storchaka
I follow your code review, Please take a look.
This PR will not allow zero-length string, is this should be allowed? self._read_test(['a,\0b,c'], [['a', 'b', 'c']], escapechar='\0') -> allowed Please let me know your intention, I will update the PR to follow your intention :) |
This is undocumented behavior, and I believe it is not intentional. I think that the change for more correct error message could be backported to 3.9 and 3.10, but the change in handling empty strings should be only in 3.11. Also there is a bug, error in So you can split this PR on 2 or 3 PRs (better error message, handle error in Ah, and it would be nice to disallow empty lineterminator, and disallow conflicts (e.g. the same char for delimiter, escapechar or quotechar), but this is a different issue. |
Okay let's separate the PR into 3 PRs
|
Thanks @corona10 for the PR |
GH-28830 is a backport of this pull request to the 3.10 branch. |
GH-28831 is a backport of this pull request to the 3.9 branch. |
…thonGH-28705) (cherry picked from commit 34bbc87) Co-authored-by: Dong-hee Na <donghee.na@python.org>
…thonGH-28705) (cherry picked from commit 34bbc87) Co-authored-by: Dong-hee Na <donghee.na@python.org>
|
|
https://bugs.python.org/issue20028
The text was updated successfully, but these errors were encountered: