]> granicus.if.org Git - python/commitdiff
PyTokenizer_FindEncoding() always failed because it set the tokenizer state
authorBrett Cannon <bcannon@gmail.com>
Thu, 4 Sep 2008 05:04:25 +0000 (05:04 +0000)
committerBrett Cannon <bcannon@gmail.com>
Thu, 4 Sep 2008 05:04:25 +0000 (05:04 +0000)
with only a file pointer when it called fp_setreadl() which expected a file
path. Changed fp_setreadl() to use either a file path or file descriptor
(derived from the file pointer) to fix the issue.

Closes issue 3594.
Reviewed by Antoine Pitrou and Benjamin Peterson.

Lib/test/test_imp.py
Misc/NEWS
Parser/tokenizer.c

index e2b9e9ab245ddcba4a09c9d0cc9f4dafcb9fc84c..3a3059e1619d0b9578fd363f35c654467f677357 100644 (file)
@@ -1,4 +1,5 @@
 import imp
+import sys
 import unittest
 from test import support
 
@@ -59,6 +60,21 @@ class ImportTests(unittest.TestCase):
                          '"""Tokenization help for Python programs.\n')
         fp.close()
 
+    def test_issue3594(self):
+        temp_mod_name = 'test_imp_helper'
+        sys.path.insert(0, '.')
+        try:
+            with open(temp_mod_name + '.py', 'w') as file:
+                file.write("# coding: cp1252\nu = 'test.test_imp'\n")
+            file, filename, info = imp.find_module(temp_mod_name)
+            file.close()
+            self.assertEquals(file.encoding, 'cp1252')
+        finally:
+            del sys.path[0]
+            support.unlink(temp_mod_name + '.py')
+            support.unlink(temp_mod_name + '.pyc')
+            support.unlink(temp_mod_name + '.pyo')
+
     def test_reload(self):
         import marshal
         imp.reload(marshal)
index e393c06ab95eaceedc6cb9bec0a4d72356a50027..3e77aa2d76d0f95bf29801cde0fdf8ba00ec1719 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,10 @@ What's New in Python 3.0 release candidate 1
 Core and Builtins
 -----------------
 
+- Issue 3594: Fix Parser/tokenizer.c:fp_setreadl() to open the file being
+  tokenized by either a file path or file pointer for the benefit of
+  PyTokenizer_FindEncoding().
+
 - Issue #3696: Error parsing arguments on OpenBSD <= 4.4 and Cygwin. On
   these systems, the mbstowcs() function is slightly buggy and must be
   replaced with strlen() for the purpose of counting of number of wide
index e2da3e5b6b0ba17d5882996696c7f5611b8609dd..e4cf8e4cc5524ff184193f996bd1f2af21ad839b 100644 (file)
@@ -448,8 +448,12 @@ fp_setreadl(struct tok_state *tok, const char* enc)
        if (io == NULL)
                goto cleanup;
 
-       stream = PyObject_CallMethod(io, "open", "ssis",
-                                    tok->filename, "r", -1, enc);
+       if (tok->filename)
+               stream = PyObject_CallMethod(io, "open", "ssis",
+                                            tok->filename, "r", -1, enc);
+       else
+               stream = PyObject_CallMethod(io, "open", "isis",
+                               fileno(tok->fp), "r", -1, enc);
        if (stream == NULL)
                goto cleanup;