Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e6c910e

Browse files
author
Victor Stinner
committed
Issue #12451: pydoc.synopsis() now reads the encoding cookie if available, to
read the Python script from the right encoding.
1 parent dc9b1ea commit e6c910e

3 files changed

Lines changed: 20 additions & 5 deletions

File tree

Lib/pydoc.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,17 @@ class or function within a module or module in a package. If the
5757
# the current directory is changed with os.chdir(), an incorrect
5858
# path will be displayed.
5959

60-
import os
61-
import sys
6260
import builtins
6361
import imp
64-
import io
6562
import inspect
63+
import io
64+
import os
6665
import pkgutil
6766
import platform
6867
import re
68+
import sys
6969
import time
70+
import tokenize
7071
import warnings
7172
from collections import deque
7273
from reprlib import Repr
@@ -227,7 +228,7 @@ def synopsis(filename, cache={}):
227228
if lastupdate < mtime:
228229
info = inspect.getmoduleinfo(filename)
229230
try:
230-
file = open(filename)
231+
file = tokenize.open(filename)
231232
except IOError:
232233
# module can't be opened, so skip it
233234
return None

Lib/test/test_pydoc.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from collections import namedtuple
1717
from contextlib import contextmanager
1818
from test.support import TESTFN, forget, rmtree, EnvironmentVarGuard, \
19-
reap_children, captured_output, captured_stdout
19+
reap_children, captured_output, captured_stdout, unlink
2020

2121
from test import pydoc_mod
2222

@@ -389,6 +389,17 @@ def test_namedtuple_public_underscore(self):
389389
self.assertIn('_replace', helptext)
390390
self.assertIn('_asdict', helptext)
391391

392+
def test_synopsis(self):
393+
self.addCleanup(unlink, TESTFN)
394+
for encoding in ('ISO-8859-1', 'UTF-8'):
395+
with open(TESTFN, 'w', encoding=encoding) as script:
396+
if encoding != 'UTF-8':
397+
print('#coding: {}'.format(encoding), file=script)
398+
print('"""line 1: h\xe9', file=script)
399+
print('line 2: hi"""', file=script)
400+
synopsis = pydoc.synopsis(TESTFN, {})
401+
self.assertEqual(synopsis, 'line 1: h\xe9')
402+
392403

393404
class TestDescriptions(unittest.TestCase):
394405

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ Core and Builtins
2525
Library
2626
-------
2727

28+
- Issue #12451: pydoc.synopsis() now reads the encoding cookie if available,
29+
to read the Python script from the right encoding.
30+
2831
- Issue #12451: distutils now opens the setup script in binary mode to read the
2932
encoding cookie, instead of opening it in UTF-8.
3033

0 commit comments

Comments
 (0)