From 3202b5b5ce49b6be80bcc5cc29d02b95b515520a Mon Sep 17 00:00:00 2001 From: Brett Cannon Date: Fri, 30 May 2014 14:55:29 -0400 Subject: Issue #20383: Introduce importlib.util.module_from_spec(). Along the way, dismantle importlib._bootstrap._SpecMethods as it was no longer relevant and constructing the new function required partially dismantling the class anyway. --- Lib/pydoc.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) (limited to 'Lib/pydoc.py') diff --git a/Lib/pydoc.py b/Lib/pydoc.py index 220a3cb1cc..42f65dcf79 100755 --- a/Lib/pydoc.py +++ b/Lib/pydoc.py @@ -263,9 +263,8 @@ def synopsis(filename, cache={}): # XXX We probably don't need to pass in the loader here. spec = importlib.util.spec_from_file_location('__temp__', filename, loader=loader) - _spec = importlib._bootstrap._SpecMethods(spec) try: - module = _spec.load() + module = importlib._bootstrap._load(spec) except: return None del sys.modules['__temp__'] @@ -297,9 +296,8 @@ def importfile(path): loader = importlib._bootstrap.SourceFileLoader(name, path) # XXX We probably don't need to pass in the loader here. spec = importlib.util.spec_from_file_location(name, path, loader=loader) - _spec = importlib._bootstrap._SpecMethods(spec) try: - return _spec.load() + return importlib._bootstrap._load(spec) except: raise ErrorDuringImport(path, sys.exc_info()) @@ -2057,9 +2055,8 @@ class ModuleScanner: else: path = None else: - _spec = importlib._bootstrap._SpecMethods(spec) try: - module = _spec.load() + module = importlib._bootstrap._load(spec) except ImportError: if onerror: onerror(modname) -- cgit v1.2.1 From bcd8dccf51c716a73884baa8eb3ad84065685a8b Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Fri, 25 Jul 2014 23:36:00 +0300 Subject: Issue #22033: Reprs of most Python implemened classes now contain actual class name instead of hardcoded one. --- Lib/pydoc.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'Lib/pydoc.py') diff --git a/Lib/pydoc.py b/Lib/pydoc.py index 4c67835179..a577543218 100755 --- a/Lib/pydoc.py +++ b/Lib/pydoc.py @@ -1811,7 +1811,8 @@ class Helper: if inspect.stack()[1][3] == '?': self() return '' - return '' + return '<%s.%s instance>' % (self.__class__.__module__, + self.__class__.__qualname__) _GoInteractive = object() def __call__(self, request=_GoInteractive): -- cgit v1.2.1 From 9be780ff36c2be65bc9388e57bda1288890911c0 Mon Sep 17 00:00:00 2001 From: Georg Brandl Date: Tue, 30 Sep 2014 22:56:38 +0200 Subject: Move Doc/tools/sphinxext content to Doc/tools, there is no need for the nested subdirectory anymore. --- Lib/pydoc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Lib/pydoc.py') diff --git a/Lib/pydoc.py b/Lib/pydoc.py index 8f3393b011..2921f3d4fa 100755 --- a/Lib/pydoc.py +++ b/Lib/pydoc.py @@ -1636,7 +1636,7 @@ class Helper: # in pydoc_data/topics.py. # # CAUTION: if you change one of these dictionaries, be sure to adapt the - # list of needed labels in Doc/tools/sphinxext/pyspecific.py and + # list of needed labels in Doc/tools/pyspecific.py and # regenerate the pydoc_data/topics.py file by running # make pydoc-topics # in Doc/ and copying the output file into the Lib/ directory. -- cgit v1.2.1 From b0fe5e531ea0a4a9738637850f37d48312ab17d7 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Sun, 1 Mar 2015 00:42:54 +0200 Subject: Issue #19980: Improved help() for non-recognized strings. help('') now shows the help on str. help('help') now shows the help on help(). Original patch by Mark Lawrence. --- Lib/pydoc.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'Lib/pydoc.py') diff --git a/Lib/pydoc.py b/Lib/pydoc.py index b762389cf3..c92b324afd 100755 --- a/Lib/pydoc.py +++ b/Lib/pydoc.py @@ -1577,7 +1577,10 @@ def resolve(thing, forceload=0): if isinstance(thing, str): object = locate(thing, forceload) if not object: - raise ImportError('no Python documentation found for %r' % thing) + raise ImportError('''\ +No Python documentation found for %r. +Use help() to get the interactive help utility. +Use help(str) for help on the str class.''' % thing) return object, thing else: name = getattr(thing, '__name__', None) @@ -1848,7 +1851,10 @@ has the same effect as typing a particular string at the help> prompt. break request = replace(request, '"', '', "'", '').strip() if request.lower() in ('q', 'quit'): break - self.help(request) + if request == 'help': + self.intro() + else: + self.help(request) def getline(self, prompt): """Read one line, using input() when appropriate.""" @@ -1862,8 +1868,7 @@ has the same effect as typing a particular string at the help> prompt. def help(self, request): if type(request) is type(''): request = request.strip() - if request == 'help': self.intro() - elif request == 'keywords': self.listkeywords() + if request == 'keywords': self.listkeywords() elif request == 'symbols': self.listsymbols() elif request == 'topics': self.listtopics() elif request == 'modules': self.listmodules() @@ -1876,6 +1881,7 @@ has the same effect as typing a particular string at the help> prompt. elif request in self.keywords: self.showtopic(request) elif request in self.topics: self.showtopic(request) elif request: doc(request, 'Help on %s:', output=self._output) + else: doc(str, 'Help on %s:', output=self._output) elif isinstance(request, Helper): self() else: doc(request, 'Help on %s:', output=self._output) self.output.write('\n') -- cgit v1.2.1 From d6c289dad8f6c9979a3bec08cc003acab661bef2 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Sat, 4 Apr 2015 11:01:02 +0300 Subject: Issue #22831: Use "with" to avoid possible fd leaks. --- Lib/pydoc.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'Lib/pydoc.py') diff --git a/Lib/pydoc.py b/Lib/pydoc.py index 9f3401f766..d77ed002ec 100755 --- a/Lib/pydoc.py +++ b/Lib/pydoc.py @@ -1639,9 +1639,8 @@ def writedoc(thing, forceload=0): try: object, name = resolve(thing, forceload) page = html.page(describe(object), html.document(object, name)) - file = open(name + '.html', 'w', encoding='utf-8') - file.write(page) - file.close() + with open(name + '.html', 'w', encoding='utf-8') as file: + file.write(page) print('wrote', name + '.html') except (ImportError, ErrorDuringImport) as value: print(value) -- cgit v1.2.1 From b09b34b211300f68cc98f6b882f7d2e2f4a76f10 Mon Sep 17 00:00:00 2001 From: Brett Cannon Date: Mon, 13 Apr 2015 14:21:02 -0400 Subject: Issue #23731: Implement PEP 488. The concept of .pyo files no longer exists. Now .pyc files have an optional `opt-` tag which specifies if any extra optimizations beyond the peepholer were applied. --- Lib/pydoc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Lib/pydoc.py') diff --git a/Lib/pydoc.py b/Lib/pydoc.py index d77ed002ec..264e407e47 100755 --- a/Lib/pydoc.py +++ b/Lib/pydoc.py @@ -213,7 +213,7 @@ def classify_class_attrs(object): def ispackage(path): """Guess whether a path refers to a package directory.""" if os.path.isdir(path): - for ext in ('.py', '.pyc', '.pyo'): + for ext in ('.py', '.pyc'): if os.path.isfile(os.path.join(path, '__init__' + ext)): return True return False -- cgit v1.2.1 From 470268ae1d0e919df838db5b652ab9a8608fc2b8 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Sat, 2 May 2015 19:15:18 -0600 Subject: Issue #23911: Move path-based bootstrap code to a separate frozen module. --- Lib/pydoc.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'Lib/pydoc.py') diff --git a/Lib/pydoc.py b/Lib/pydoc.py index 306dcafae0..ee558bfea3 100755 --- a/Lib/pydoc.py +++ b/Lib/pydoc.py @@ -53,6 +53,7 @@ Richard Chamberlain, for the first implementation of textdoc. import builtins import importlib._bootstrap +import importlib._bootstrap_external import importlib.machinery import importlib.util import inspect @@ -292,9 +293,9 @@ def importfile(path): filename = os.path.basename(path) name, ext = os.path.splitext(filename) if is_bytecode: - loader = importlib._bootstrap.SourcelessFileLoader(name, path) + loader = importlib._bootstrap_external.SourcelessFileLoader(name, path) else: - loader = importlib._bootstrap.SourceFileLoader(name, path) + loader = importlib._bootstrap_external.SourceFileLoader(name, path) # XXX We probably don't need to pass in the loader here. spec = importlib.util.spec_from_file_location(name, path, loader=loader) try: -- cgit v1.2.1 From 161585817b3f8aa994b4ccfe02961c5238d1d5b8 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Tue, 18 Aug 2015 22:25:16 -0700 Subject: Issue #24879: Teach pydoc to display named tuple fields in the order they were defined. --- Lib/pydoc.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) (limited to 'Lib/pydoc.py') diff --git a/Lib/pydoc.py b/Lib/pydoc.py index ee558bfea3..f4f253010f 100755 --- a/Lib/pydoc.py +++ b/Lib/pydoc.py @@ -209,6 +209,18 @@ def classify_class_attrs(object): results.append((name, kind, cls, value)) return results +def sort_attributes(attrs, object): + 'Sort the attrs list in-place by _fields and then alphabetically by name' + # This allows data descriptors to be ordered according + # to a _fields attribute if present. + fields = getattr(object, '_fields', []) + try: + field_order = {name : i-len(fields) for (i, name) in enumerate(fields)} + except TypeError: + field_order = {} + keyfunc = lambda attr: (field_order.get(attr[0], 0), attr[0]) + attrs.sort(key=keyfunc) + # ----------------------------------------------------- module manipulation def ispackage(path): @@ -867,8 +879,7 @@ class HTMLDoc(Doc): object.__module__) tag += ':
\n' - # Sort attrs by name. - attrs.sort(key=lambda t: t[0]) + sort_attributes(attrs, object) # Pump out the attrs, segregated by kind. attrs = spill('Methods %s' % tag, attrs, @@ -1286,8 +1297,8 @@ location listed above. else: tag = "inherited from %s" % classname(thisclass, object.__module__) - # Sort attrs by name. - attrs.sort() + + sort_attributes(attrs, object) # Pump out the attrs, segregated by kind. attrs = spill("Methods %s:\n" % tag, attrs, -- cgit v1.2.1 From 2b442f7d5b61dcefc5a7a18a6e7af9de5983790c Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Thu, 11 Feb 2016 13:10:36 +0200 Subject: Issue #25985: sys.version_info is now used instead of sys.version to format short Python version. --- Lib/pydoc.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'Lib/pydoc.py') diff --git a/Lib/pydoc.py b/Lib/pydoc.py index a73298d715..5e5a8aeaa5 100644 --- a/Lib/pydoc.py +++ b/Lib/pydoc.py @@ -1911,10 +1911,10 @@ has the same effect as typing a particular string at the help> prompt. def intro(self): self.output.write(''' -Welcome to Python %s's help utility! +Welcome to Python {0}'s help utility! If this is your first time using Python, you should definitely check out -the tutorial on the Internet at http://docs.python.org/%s/tutorial/. +the tutorial on the Internet at http://docs.python.org/{0}/tutorial/. Enter the name of any module, keyword, or topic to get help on writing Python programs and using Python modules. To quit this help utility and @@ -1924,7 +1924,7 @@ To get a list of available modules, keywords, symbols, or topics, type "modules", "keywords", "symbols", or "topics". Each module also comes with a one-line summary of what it does; to list the modules whose name or summary contain a given string such as "spam", type "modules spam". -''' % tuple([sys.version[:3]]*2)) +'''.format('%d.%d' % sys.version_info[:2])) def list(self, items, columns=4, width=80): items = list(sorted(items)) -- cgit v1.2.1 From cb3e3ba86a4aaf964aa574377dd38be0505a9d3f Mon Sep 17 00:00:00 2001 From: doko Date: Tue, 14 Jun 2016 08:39:31 +0200 Subject: - Issue #8637: Honor a pager set by the env var MANPAGER (in preference to one set by the env var PAGER). --- Lib/pydoc.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'Lib/pydoc.py') diff --git a/Lib/pydoc.py b/Lib/pydoc.py index 063aa9cf07..de0084c3e5 100644 --- a/Lib/pydoc.py +++ b/Lib/pydoc.py @@ -1429,7 +1429,8 @@ def getpager(): return plainpager if not sys.stdin.isatty() or not sys.stdout.isatty(): return plainpager - if 'PAGER' in os.environ: + use_pager = os.environ.get('MANPAGER') or os.environ.get('PAGER') + if use_pager: if sys.platform == 'win32': # pipes completely broken in Windows return lambda text: tempfilepager(plain(text), os.environ['PAGER']) elif os.environ.get('TERM') in ('dumb', 'emacs'): -- cgit v1.2.1 From cca66dc999934b30748111bc26cee19af1ea02cc Mon Sep 17 00:00:00 2001 From: doko Date: Tue, 14 Jun 2016 09:03:52 +0200 Subject: - Issue #8637: Honor a pager set by the env var MANPAGER (in preference to one set by the env var PAGER). --- Lib/pydoc.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'Lib/pydoc.py') diff --git a/Lib/pydoc.py b/Lib/pydoc.py index de0084c3e5..d7a177f1a2 100644 --- a/Lib/pydoc.py +++ b/Lib/pydoc.py @@ -1432,11 +1432,11 @@ def getpager(): use_pager = os.environ.get('MANPAGER') or os.environ.get('PAGER') if use_pager: if sys.platform == 'win32': # pipes completely broken in Windows - return lambda text: tempfilepager(plain(text), os.environ['PAGER']) + return lambda text: tempfilepager(plain(text), use_pager) elif os.environ.get('TERM') in ('dumb', 'emacs'): - return lambda text: pipepager(plain(text), os.environ['PAGER']) + return lambda text: pipepager(plain(text), use_pager) else: - return lambda text: pipepager(text, os.environ['PAGER']) + return lambda text: pipepager(text, use_pager) if os.environ.get('TERM') in ('dumb', 'emacs'): return plainpager if sys.platform == 'win32': -- cgit v1.2.1 From 54dc09e399e40109e5b61f0ed2e94558cc07aaa8 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Wed, 7 Sep 2016 16:56:15 -0700 Subject: Issue #15767: Use ModuleNotFoundError. --- Lib/pydoc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Lib/pydoc.py') diff --git a/Lib/pydoc.py b/Lib/pydoc.py index d7a177f1a2..39db3915dc 100644 --- a/Lib/pydoc.py +++ b/Lib/pydoc.py @@ -350,7 +350,7 @@ def safeimport(path, forceload=0, cache={}): elif exc is SyntaxError: # A SyntaxError occurred before we could execute the module. raise ErrorDuringImport(value.filename, info) - elif exc is ImportError and value.name == path: + elif issubclass(exc, ImportError) and value.name == path: # No such module in the path. return None else: -- cgit v1.2.1