New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
csv module no longer works as expected when file opened in binary mode #49705
Comments
I just discovered that the csv module's reader class in 3.x doesn't work % python3.1
Python 3.1a0 (py3k:70084M, Feb 28 2009, 20:46:48)
[GCC 4.0.1 (Apple Inc. build 5490)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import csv
>>> next(csv.reader(open("f.csv", "rb")))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
_csv.Error: iterator should return strings, not bytes (did you open the
file in text mode?)
>>> next(csv.reader(open("f.csv", "r")))
['col1', 'col2', 'color'] At the very least the documentation for the csv.reader class is no |
in _csv.c, the check is done here: lineobj = PyIter_Next(self->input_iter);
if (lineobj == NULL) {
/* End of input OR exception */
if (!PyErr_Occurred() && self->field_len != 0)
PyErr_Format(error_obj,
"newline inside string");
return NULL;
}
if (!PyUnicode_Check(lineobj)) {
PyErr_Format(error_obj,
"iterator should return strings, "
"not %.200s "
"(did you open the file in text mode?)",
lineobj->ob_type->tp_name
);
Py_DECREF(lineobj);
return NULL;
} So the returned lineobj is a bytes type and then the PyUnicode_Check |
Hi Skip, Currently, once we are sure the lineobj is a unicode obj we then line = PyUnicode_AsUnicode(lineobj); for the purpose of iterating through the line. is there an opportunity to use: line = PyBytes_AsString(lineobj); (or similar approach if I have quoted an incorrect function) for the |
Jervis> So the returned lineobj is a bytes type and then the Right, but given that fact how do you get a Unicode string out of the bytes |
John> The docs are CORRECT. I agree. I posted a note to python-dev referencing both tickets. Hopefully Skip |
Setting bpo-4847 as superseder. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: