[Python-checkins] cpython (merge 3.2 -> default): (merge 3.2) Issue #12451: pydoc.synopsis() now reads the encoding cookie if

victor.stinner python-checkins at python.org
Thu Jun 30 16:00:05 CEST 2011


http://hg.python.org/cpython/rev/3e627877b5a9
changeset:   71092:3e627877b5a9
parent:      71090:8a7fd54cba01
parent:      71091:1942f7c8f51c
user:        Victor Stinner <victor.stinner at haypocalc.com>
date:        Thu Jun 30 15:58:29 2011 +0200
summary:
  (merge 3.2) Issue #12451: pydoc.synopsis() now reads the encoding cookie if
available, to read the Python script from the right encoding.

files:
  Lib/pydoc.py           |   9 +++++----
  Lib/test/test_pydoc.py |  13 ++++++++++++-
  Misc/NEWS              |   3 +++
  3 files changed, 20 insertions(+), 5 deletions(-)


diff --git a/Lib/pydoc.py b/Lib/pydoc.py
--- a/Lib/pydoc.py
+++ b/Lib/pydoc.py
@@ -51,16 +51,17 @@
 #     the current directory is changed with os.chdir(), an incorrect
 #     path will be displayed.
 
-import os
-import sys
 import builtins
 import imp
+import inspect
 import io
-import inspect
+import os
 import pkgutil
 import platform
 import re
+import sys
 import time
+import tokenize
 import warnings
 from collections import deque
 from reprlib import Repr
@@ -221,7 +222,7 @@
     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
diff --git a/Lib/test/test_pydoc.py b/Lib/test/test_pydoc.py
--- a/Lib/test/test_pydoc.py
+++ b/Lib/test/test_pydoc.py
@@ -16,7 +16,7 @@
 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
 
@@ -395,6 +395,17 @@
         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):
 
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -200,6 +200,9 @@
 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.
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list