diff --git a/Lib/pydoc.py b/Lib/pydoc.py index 39db391..2f6f761 100644 --- a/Lib/pydoc.py +++ b/Lib/pydoc.py @@ -363,6 +363,25 @@ def safeimport(path, forceload=0, cache={}): # ---------------------------------------------------- formatter base class +_STANDARD_DOCS = None # set of standard module documents + +def _is_standard_module(name): + global _STANDARD_DOCS + if _STANDARD_DOCS is None: + # Lazy building of _standard_docs. + # The file Lib/pydoc_data/libs.txt contains a list of all standard + # modules that have documentation, one module per line. + import pydoc_data + fn = os.path.join(pydoc_data.__path__[0], 'libs.txt') + if os.path.exists(fn): + with open(fn, 'r') as fp: + names = [n.strip() for n in fp] + else: + names = [] + _STANDARD_DOCS = set(names) + return name in _STANDARD_DOCS + + class Doc: PYTHONDOCS = os.environ.get("PYTHONDOCS", @@ -395,30 +414,17 @@ class Doc: docmodule = docclass = docroutine = docother = docproperty = docdata = fail - def getdocloc(self, object, - basedir=os.path.join(sys.base_exec_prefix, "lib", - "python%d.%d" % sys.version_info[:2])): + def getdocloc(self, object, basedir=None): """Return the location of module docs or None""" - try: - file = inspect.getabsfile(object) - except TypeError: - file = '(built-in)' - docloc = os.environ.get("PYTHONDOCS", self.PYTHONDOCS) - basedir = os.path.normcase(basedir) - if (isinstance(object, type(os)) and - (object.__name__ in ('errno', 'exceptions', 'gc', 'imp', - 'marshal', 'posix', 'signal', 'sys', - '_thread', 'zipimport') or - (file.startswith(basedir) and - not file.startswith(os.path.join(basedir, 'site-packages')))) and - object.__name__ not in ('xml.etree', 'test.pydoc_mod')): + name = object.__name__.lower() + if _is_standard_module(name): if docloc.startswith(("http://", "https://")): - docloc = "%s/%s" % (docloc.rstrip("/"), object.__name__.lower()) + docloc = "%s/%s" % (docloc.rstrip("/"), name) else: - docloc = os.path.join(docloc, object.__name__.lower() + ".html") + docloc = os.path.join(docloc, name + ".html") else: docloc = None return docloc diff --git a/Lib/pydoc_data/libs.txt b/Lib/pydoc_data/libs.txt new file mode 100644 index 0000000..4f28c8f --- /dev/null +++ b/Lib/pydoc_data/libs.txt @@ -0,0 +1,304 @@ +2to3 +__future__ +__main__ +_dummy_thread +_thread +abc +aifc +allos +archiving +argparse +array +ast +asynchat +asyncio +asyncio-dev +asyncio-eventloop +asyncio-eventloops +asyncio-protocol +asyncio-queue +asyncio-stream +asyncio-subprocess +asyncio-sync +asyncio-task +asyncore +atexit +audioop +base64 +bdb +binary +binascii +binhex +bisect +builtins +bz2 +calendar +cgi +cgitb +chunk +cmath +cmd +code +codecs +codeop +collections +collections.abc +colorsys +compileall +concurrency +concurrent +concurrent.futures +configparser +constants +contextlib +copy +copyreg +crypt +crypto +csv +ctypes +curses +curses.ascii +curses.panel +custominterp +datatypes +datetime +dbm +debug +decimal +development +difflib +dis +distribution +distutils +doctest +dummy_threading +email +email.charset +email.compat32-message +email.contentmanager +email.encoders +email.errors +email.examples +email.generator +email.header +email.headerregistry +email.iterators +email.message +email.mime +email.parser +email.policy +email.util +ensurepip +enum +errno +exceptions +faulthandler +fcntl +filecmp +fileformats +fileinput +filesys +fnmatch +formatter +fpectl +fractions +frameworks +ftplib +functional +functions +functools +gc +getopt +getpass +gettext +glob +grp +gzip +hashlib +hashlib-blake2 +heapq +hmac +html +html.entities +html.parser +http +http.client +http.cookiejar +http.cookies +http.server +i18n +idle +imaplib +imghdr +imp +importlib +index +inspect +internet +intro +io +ipaddress +ipc +itertools +json +keyword +language +linecache +locale +logging +logging.config +logging.handlers +lzma +macpath +mailbox +mailcap +markup +marshal +math +mimetypes +misc +mm +mmap +modulefinder +modules +msilib +msvcrt +multiprocessing +netdata +netrc +nis +nntplib +numbers +numeric +operator +optparse +os +os.path +ossaudiodev +othergui +parser +pathlib +pdb +persistence +pickle +pickletools +pipes +pkgutil +platform +plistlib +poplib +posix +pprint +profile +pty +pwd +py_compile +pyclbr +pydoc +pyexpat +python +queue +quopri +random +re +readline +reprlib +resource +rlcompleter +runpy +sched +secrets +select +selectors +shelve +shlex +shutil +signal +site +smtpd +smtplib +sndhdr +socket +socketserver +spwd +sqlite3 +ssl +stat +statistics +stdtypes +string +stringprep +struct +subprocess +sunau +superseded +symbol +symtable +sys +sysconfig +syslog +tabnanny +tarfile +telnetlib +tempfile +termios +test +text +textwrap +threading +time +timeit +tk +tkinter +tkinter.scrolledtext +tkinter.tix +tkinter.ttk +token +tokenize +trace +traceback +tracemalloc +tty +turtle +types +typing +undoc +unicodedata +unittest +unittest.mock +unittest.mock-examples +unix +urllib +urllib.error +urllib.parse +urllib.request +urllib.robotparser +uu +uuid +venv +warnings +wave +weakref +webbrowser +windows +winreg +winsound +wsgiref +xdrlib +xml +xml.dom +xml.dom.minidom +xml.dom.pulldom +xml.etree.elementtree +xml.sax +xml.sax.handler +xml.sax.reader +xml.sax.utils +xmlrpc +xmlrpc.client +xmlrpc.server +zipapp +zipfile +zipimport +zlib diff --git a/Lib/test/test_pydoc.py b/Lib/test/test_pydoc.py index ed438d5..101b9ae 100644 --- a/Lib/test/test_pydoc.py +++ b/Lib/test/test_pydoc.py @@ -360,10 +360,8 @@ def get_pydoc_html(module): def get_pydoc_link(module): "Returns a documentation web link of a module" - dirname = os.path.dirname - basedir = dirname(dirname(__file__)) doc = pydoc.TextDoc() - loc = doc.getdocloc(module, basedir=basedir) + loc = doc.getdocloc(module) return loc def get_pydoc_text(module):