summaryrefslogtreecommitdiffstats
path: root/Parser/tokenizer.c
diff options
context:
space:
mode:
authorMartin v. Löwis <martin@v.loewis.de>2014-02-28 14:47:15 (GMT)
committerMartin v. Löwis <martin@v.loewis.de>2014-02-28 14:47:15 (GMT)
commit9c17ff91f347704fd0fc7439675d84370db8b75a (patch)
treed56b4357473d272a01203740af9fc3ad172c2bcf /Parser/tokenizer.c
parentf808df39debb2f5ba8bfe6dee767efd896c4c1bd (diff)
parent78f1e4c865a08e5dc0afe105954b763e3c7b65eb (diff)
downloadcpython-9c17ff91f347704fd0fc7439675d84370db8b75a.zip
cpython-9c17ff91f347704fd0fc7439675d84370db8b75a.tar.gz
cpython-9c17ff91f347704fd0fc7439675d84370db8b75a.tar.bz2
Merge heads
Diffstat (limited to 'Parser/tokenizer.c')
-rw-r--r--Parser/tokenizer.c14
1 files changed, 12 insertions, 2 deletions
diff --git a/Parser/tokenizer.c b/Parser/tokenizer.c
index c32a3bf..7283058 100644
--- a/Parser/tokenizer.c
+++ b/Parser/tokenizer.c
@@ -498,9 +498,13 @@ fp_setreadl(struct tok_state *tok, const char* enc)
fd = fileno(tok->fp);
/* Due to buffering the file offset for fd can be different from the file
- * position of tok->fp. */
+ * position of tok->fp. If tok->fp was opened in text mode on Windows,
+ * its file position counts CRLF as one char and can't be directly mapped
+ * to the file offset for fd. Instead we step back one byte and read to
+ * the end of line.*/
pos = ftell(tok->fp);
- if (pos == -1 || lseek(fd, (off_t)pos, SEEK_SET) == (off_t)-1) {
+ if (pos == -1 ||
+ lseek(fd, (off_t)(pos > 0 ? pos - 1 : pos), SEEK_SET) == (off_t)-1) {
PyErr_SetFromErrnoWithFilename(PyExc_OSError, NULL);
goto cleanup;
}
@@ -513,6 +517,12 @@ fp_setreadl(struct tok_state *tok, const char* enc)
Py_XDECREF(tok->decoding_readline);
readline = _PyObject_GetAttrId(stream, &PyId_readline);
tok->decoding_readline = readline;
+ if (pos > 0) {
+ if (PyObject_CallObject(readline, NULL) == NULL) {
+ readline = NULL;
+ goto cleanup;
+ }
+ }
cleanup:
Py_XDECREF(stream);