diff options
author | Martin v. Löwis <martin@v.loewis.de> | 2003-01-03 19:16:14 (GMT) |
---|---|---|
committer | Martin v. Löwis <martin@v.loewis.de> | 2003-01-03 19:16:14 (GMT) |
commit | af6a27a704cd832f91e25b1052f4b6cc52a4e704 (patch) | |
tree | ff34bd57ae9ef045d524b8acdded723131fd20e8 /Objects | |
parent | bb0246ac2528fbce1cbe25a26f852a4badb4baeb (diff) | |
download | cpython-af6a27a704cd832f91e25b1052f4b6cc52a4e704.zip cpython-af6a27a704cd832f91e25b1052f4b6cc52a4e704.tar.gz cpython-af6a27a704cd832f91e25b1052f4b6cc52a4e704.tar.bz2 |
Allow PyFile_GetLine() to return Unicode objects. Fixes #660165.
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/fileobject.c | 25 |
1 files changed, 24 insertions, 1 deletions
diff --git a/Objects/fileobject.c b/Objects/fileobject.c index fb73385..fb8ec90 100644 --- a/Objects/fileobject.c +++ b/Objects/fileobject.c @@ -1212,7 +1212,8 @@ PyFile_GetLine(PyObject *f, int n) result = PyEval_CallObject(reader, args); Py_DECREF(reader); Py_DECREF(args); - if (result != NULL && !PyString_Check(result)) { + if (result != NULL && !PyString_Check(result) && + !PyUnicode_Check(result)) { Py_DECREF(result); result = NULL; PyErr_SetString(PyExc_TypeError, @@ -1240,6 +1241,28 @@ PyFile_GetLine(PyObject *f, int n) } } } +#ifdef Py_USING_UNICODE + if (n < 0 && result != NULL && PyUnicode_Check(result)) { + Py_UNICODE *s = PyUnicode_AS_UNICODE(result); + int len = PyUnicode_GET_SIZE(result); + if (len == 0) { + Py_DECREF(result); + result = NULL; + PyErr_SetString(PyExc_EOFError, + "EOF when reading a line"); + } + else if (s[len-1] == '\n') { + if (result->ob_refcnt == 1) + PyUnicode_Resize(&result, len-1); + else { + PyObject *v; + v = PyUnicode_FromUnicode(s, len-1); + Py_DECREF(result); + result = v; + } + } + } +#endif return result; } |