[Python-checkins] r85476 - in python/branches/py3k: Misc/NEWS Parser/tokenizer.c Parser/tokenizer.h

victor.stinner python-checkins at python.org
Thu Oct 14 14:04:35 CEST 2010


Author: victor.stinner
Date: Thu Oct 14 14:04:34 2010
New Revision: 85476

Log:
Issue #10095: fp_setreadl() doesn't reopen the file, reuse instead the file
descriptor.


Modified:
   python/branches/py3k/Misc/NEWS
   python/branches/py3k/Parser/tokenizer.c
   python/branches/py3k/Parser/tokenizer.h

Modified: python/branches/py3k/Misc/NEWS
==============================================================================
--- python/branches/py3k/Misc/NEWS	(original)
+++ python/branches/py3k/Misc/NEWS	Thu Oct 14 14:04:34 2010
@@ -10,6 +10,9 @@
 Core and Builtins
 -----------------
 
+- Issue #10095: fp_setreadl() doesn't reopen the file, reuse instead the file
+  descriptor.
+
 - Issue #9418: Moved private string methods ``_formatter_parser`` and
   ``_formatter_field_name_split`` into a new ``_string`` module.
 

Modified: python/branches/py3k/Parser/tokenizer.c
==============================================================================
--- python/branches/py3k/Parser/tokenizer.c	(original)
+++ python/branches/py3k/Parser/tokenizer.c	Thu Oct 14 14:04:34 2010
@@ -462,17 +462,20 @@
 fp_setreadl(struct tok_state *tok, const char* enc)
 {
     PyObject *readline = NULL, *stream = NULL, *io = NULL;
+    int fd;
 
     io = PyImport_ImportModuleNoBlock("io");
     if (io == NULL)
         goto cleanup;
 
-    if (tok->filename)
-        stream = PyObject_CallMethod(io, "open", "ssis",
-                                     tok->filename, "r", -1, enc);
-    else
-        stream = PyObject_CallMethod(io, "open", "isisOOO",
-                        fileno(tok->fp), "r", -1, enc, Py_None, Py_None, Py_False);
+    fd = fileno(tok->fp);
+    if (lseek(fd, 0, SEEK_SET) == (off_t)-1) {
+        PyErr_SetFromErrnoWithFilename(PyExc_OSError, NULL);
+        goto cleanup;
+    }
+
+    stream = PyObject_CallMethod(io, "open", "isisOOO",
+                    fd, "r", -1, enc, Py_None, Py_None, Py_False);
     if (stream == NULL)
         goto cleanup;
 

Modified: python/branches/py3k/Parser/tokenizer.h
==============================================================================
--- python/branches/py3k/Parser/tokenizer.h	(original)
+++ python/branches/py3k/Parser/tokenizer.h	Thu Oct 14 14:04:34 2010
@@ -53,7 +53,7 @@
     int cont_line;          /* whether we are in a continuation line. */
     const char* line_start;     /* pointer to start of current line */
 #ifndef PGEN
-    PyObject *decoding_readline; /* codecs.open(...).readline */
+    PyObject *decoding_readline; /* open(...).readline */
     PyObject *decoding_buffer;
 #endif
     const char* enc;        /* Encoding for the current str. */


More information about the Python-checkins mailing list