]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #12451: pydoc.synopsis() now reads the encoding cookie if available, to
authorVictor Stinner <victor.stinner@haypocalc.com>
Thu, 30 Jun 2011 13:55:43 +0000 (15:55 +0200)
committerVictor Stinner <victor.stinner@haypocalc.com>
Thu, 30 Jun 2011 13:55:43 +0000 (15:55 +0200)
read the Python script from the right encoding.

Lib/pydoc.py
Lib/test/test_pydoc.py
Misc/NEWS

index 2391013ed6b5c7581e965a9eb7bfce8d831d85a5..1619446c2d66be090987d80617d0ac8445cfd7b7 100755 (executable)
@@ -57,16 +57,17 @@ Richard Chamberlain, for the first implementation of textdoc.
 #     the current directory is changed with os.chdir(), an incorrect
 #     path will be displayed.
 
-import os
-import sys
 import builtins
 import imp
-import io
 import inspect
+import io
+import os
 import pkgutil
 import platform
 import re
+import sys
 import time
+import tokenize
 import warnings
 from collections import deque
 from reprlib import Repr
@@ -227,7 +228,7 @@ def synopsis(filename, cache={}):
     if lastupdate < mtime:
         info = inspect.getmoduleinfo(filename)
         try:
-            file = open(filename)
+            file = tokenize.open(filename)
         except IOError:
             # module can't be opened, so skip it
             return None
index 08ba86e697be853012b228e959c96c4c190a6d18..a8f9fbfc5ffbe5f434268993ae15aff2ef094ad0 100644 (file)
@@ -16,7 +16,7 @@ from io import StringIO
 from collections import namedtuple
 from contextlib import contextmanager
 from test.support import TESTFN, forget, rmtree, EnvironmentVarGuard, \
-     reap_children, captured_output, captured_stdout
+     reap_children, captured_output, captured_stdout, unlink
 
 from test import pydoc_mod
 
@@ -389,6 +389,17 @@ class PydocDocTest(unittest.TestCase):
         self.assertIn('_replace', helptext)
         self.assertIn('_asdict', helptext)
 
+    def test_synopsis(self):
+        self.addCleanup(unlink, TESTFN)
+        for encoding in ('ISO-8859-1', 'UTF-8'):
+            with open(TESTFN, 'w', encoding=encoding) as script:
+                if encoding != 'UTF-8':
+                    print('#coding: {}'.format(encoding), file=script)
+                print('"""line 1: h\xe9', file=script)
+                print('line 2: hi"""', file=script)
+            synopsis = pydoc.synopsis(TESTFN, {})
+            self.assertEqual(synopsis, 'line 1: h\xe9')
+
 
 class TestDescriptions(unittest.TestCase):
 
index 9419f67b246dc1f574bb809c7fbc0ebd19e77049..086c8da03dc304b908c97836bba5c89be872735e 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -25,6 +25,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #12451: pydoc.synopsis() now reads the encoding cookie if available,
+  to read the Python script from the right encoding.
+
 - Issue #12451: distutils now opens the setup script in binary mode to read the
   encoding cookie, instead of opening it in UTF-8.