diff -r 48943533965e Lib/pydoc.py --- a/Lib/pydoc.py Sun Sep 27 22:38:33 2015 +0300 +++ b/Lib/pydoc.py Sun Sep 27 22:53:49 2015 +0300 @@ -59,6 +59,7 @@ import importlib.util import inspect import io import os +import pathlib import pkgutil import platform import re @@ -660,7 +661,7 @@ class HTMLDoc(Doc): head = '%s' % linkedname try: path = inspect.getabsfile(object) - url = urllib.parse.quote(path) + url = pathlib.Path(path).as_uri()[5:] filelink = self.filelink(url, path) except TypeError: filelink = '(built-in)' diff -r 48943533965e Lib/test/test_pydoc.py --- a/Lib/test/test_pydoc.py Sun Sep 27 22:38:33 2015 +0300 +++ b/Lib/test/test_pydoc.py Sun Sep 27 22:53:49 2015 +0300 @@ -7,9 +7,11 @@ import inspect import pydoc import py_compile import keyword +import pathlib import _pickle import pkgutil import re +import shutil import stat import string import test.support @@ -23,9 +25,9 @@ from io import StringIO from collections import namedtuple from test.support.script_helper import assert_python_ok from test.support import ( - TESTFN, rmtree, + TESTFN, TESTFN_UNDECODABLE, TESTFN_UNENCODABLE, rmtree, reap_children, reap_threads, captured_output, captured_stdout, - captured_stderr, unlink, requires_docstrings + captured_stderr, unlink, requires_docstrings, temp_dir, swap_item ) from test import pydoc_mod @@ -137,7 +139,7 @@ expected_html_pattern = """  
 
test.pydoc_mod (version 1.2.3.4)
index
%s%s
+>index
%s%s

This is a test module for test_pydoc

@@ -347,7 +349,7 @@ def get_pydoc_html(module): "Returns pydoc generated output as html" doc = pydoc.HTMLDoc() output = doc.docmodule(module) - loc = doc.getdocloc(pydoc_mod) or "" + loc = doc.getdocloc(module) or "" if loc: loc = "
Module Docs" return output.strip(), loc @@ -355,7 +357,7 @@ def get_pydoc_html(module): def get_pydoc_text(module): "Returns pydoc generated output as text" doc = pydoc.TextDoc() - loc = doc.getdocloc(pydoc_mod) or "" + loc = doc.getdocloc(module) or "" if loc: loc = "\nMODULE DOCS\n " + loc + "\n" @@ -407,12 +409,61 @@ class PydocDocTest(unittest.TestCase): def test_html_doc(self): result, doc_loc = get_pydoc_html(pydoc_mod) mod_file = inspect.getabsfile(pydoc_mod) - mod_url = urllib.parse.quote(mod_file) + mod_url = pathlib.Path(mod_file).as_uri() expected_html = expected_html_pattern % ( (mod_url, mod_file, doc_loc) + expected_html_data_docstrings) self.assertEqual(result, expected_html) + def load_mod(self, name, path): + loader = importlib._bootstrap_external.SourceFileLoader(name, path) + spec = importlib.util.spec_from_file_location(name, path, loader=loader) + return importlib._bootstrap._load(spec) + + @unittest.skipUnless(TESTFN_UNDECODABLE, + "needed undecodable filename") + @unittest.skipIf(sys.flags.optimize >= 2, + "Docstrings are omitted with -O2 and above") + @unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(), + 'trace function introduces __locals__ unexpectedly') + @requires_docstrings + def test_html_doc_undecodable_path(self): + with swap_item(sys.modules, pydoc_mod.__name__, None), \ + temp_dir(TESTFN_UNDECODABLE): + path = os.path.join(TESTFN_UNDECODABLE, b'pydoc_mod.py') + shutil.copyfile(pydoc_mod.__file__, path) + path = os.path.abspath(os.fsdecode(path)) + mod = self.load_mod(pydoc_mod.__name__, path) + result, doc_loc = get_pydoc_html(mod) + mod_file = inspect.getabsfile(mod) + mod_url = pathlib.Path(mod_file).as_uri() + expected_html = expected_html_pattern % ( + (mod_url, mod_file, doc_loc) + + expected_html_data_docstrings) + self.assertEqual(result, expected_html) + + @unittest.skipUnless(TESTFN_UNENCODABLE, + "needed unencodable filename") + @unittest.skipIf(sys.flags.optimize >= 2, + "Docstrings are omitted with -O2 and above") + @unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(), + 'trace function introduces __locals__ unexpectedly') + @requires_docstrings + def test_html_doc_unencodable_path(self): + with swap_item(sys.modules, pydoc_mod.__name__, None), \ + temp_dir(TESTFN_UNENCODABLE): + path = os.path.join(TESTFN_UNENCODABLE, 'pydoc_mod.py') + shutil.copyfile(pydoc_mod.__file__, path) + path = os.path.abspath(path) + mod = self.load_mod(pydoc_mod.__name__, path) + result, doc_loc = get_pydoc_html(mod) + mod_file = inspect.getabsfile(mod) + mod_url = pathlib.Path(mod_file).as_uri() + expected_html = expected_html_pattern % ( + (mod_url, mod_file, doc_loc) + + expected_html_data_docstrings) + self.assertEqual(result, expected_html) + @unittest.skipIf(sys.flags.optimize >= 2, "Docstrings are omitted with -O2 and above") @unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(),