cpython: b2ee3fe195e2 (original) (raw)
Mercurial > cpython
changeset 89759:b2ee3fe195e2 3.4
Issue #20710: The pydoc summary line no longer displays the "self" parameter for bound methods. Previous to this change, it displayed "self" for methods implemented in Python but not methods implemented in C; it is now both internally consistent and consistent with inspect.Signature. [#20710]
Larry Hastings larry@hastings.org | |
---|---|
date | Thu, 20 Feb 2014 23:34:46 -0800 |
parents | ed1059f5507b |
children | 1597d5e3cf2e |
files | Lib/pydoc.py Lib/test/test_pydoc.py Misc/NEWS |
diffstat | 3 files changed, 54 insertions(+), 10 deletions(-)[+] [-] Lib/pydoc.py 21 Lib/test/test_pydoc.py 40 Misc/NEWS 3 |
line wrap: on
line diff
--- a/Lib/pydoc.py +++ b/Lib/pydoc.py @@ -137,6 +137,19 @@ def _is_some_method(obj): inspect.isbuiltin(obj) or inspect.ismethoddescriptor(obj)) +def _is_bound_method(fn):
- """
- Returns True if fn is a bound method, regardless of whether
- fn was implemented in Python or in C.
- """
- if inspect.ismethod(fn):
return True[](#l1.13)
- if inspect.isbuiltin(fn):
self = getattr(fn, '__self__', None)[](#l1.15)
return not (inspect.ismodule(self) or (self is None))[](#l1.16)
- return False
+ + def allmethods(cl): methods = {} for key, value in inspect.getmembers(cl, _is_some_method): @@ -898,7 +911,7 @@ class HTMLDoc(Doc): anchor = (cl and cl.name or '') + '-' + name note = '' skipdocs = 0
if inspect.ismethod(object):[](#l1.27)
if _is_bound_method(object):[](#l1.28) imclass = object.__self__.__class__[](#l1.29) if cl:[](#l1.30) if imclass is not cl:[](#l1.31)
@@ -909,7 +922,6 @@ class HTMLDoc(Doc): object.self.class, mod) else: note = ' unbound %s method' % self.classlink(imclass,mod)
object = object.__func__[](#l1.36)
if name == realname: title = '%s' % (anchor, realname) @@ -924,7 +936,7 @@ class HTMLDoc(Doc): title = '%s = %s' % ( anchor, name, reallink) argspec = None
if inspect.isfunction(object) or inspect.isbuiltin(object):[](#l1.44)
if inspect.isroutine(object):[](#l1.45) try:[](#l1.46) signature = inspect.signature(object)[](#l1.47) except (ValueError, TypeError):[](#l1.48)
@@ -1301,7 +1313,7 @@ location listed above. name = name or realname note = '' skipdocs = 0
if inspect.ismethod(object):[](#l1.53)
if _is_bound_method(object):[](#l1.54) imclass = object.__self__.__class__[](#l1.55) if cl:[](#l1.56) if imclass is not cl:[](#l1.57)
@@ -1312,7 +1324,6 @@ location listed above. object.self.class, mod) else: note = ' unbound %s method' % classname(imclass,mod)
object = object.__func__[](#l1.62)
if name == realname: title = self.bold(realname)
--- a/Lib/test/test_pydoc.py +++ b/Lib/test/test_pydoc.py @@ -6,6 +6,7 @@ import difflib import inspect import pydoc import keyword +import _pickle import pkgutil import re import string @@ -689,12 +690,41 @@ class TestDescriptions(unittest.TestCase self.assertIsNone(pydoc.locate(name)) self.assertRaises(ImportError, pydoc.render_doc, name)
- @staticmethod
- def _get_summary_line(o):
text = pydoc.plain(pydoc.render_doc(o))[](#l2.17)
lines = text.split('\n')[](#l2.18)
assert len(lines) >= 2[](#l2.19)
return lines[2][](#l2.20)
these should include "self"
- def test_unbound_python_method(self):
self.assertEqual(self._get_summary_line(textwrap.TextWrapper.wrap),[](#l2.24)
"wrap(self, text)")[](#l2.25)
- def test_builtin_signatures(self):
# test producing signatures from builtins[](#l2.29)
stat_sig = pydoc.render_doc(os.stat)[](#l2.30)
self.assertEqual(pydoc.plain(stat_sig).splitlines()[2],[](#l2.31)
'stat(path, *, dir_fd=None, follow_symlinks=True)')[](#l2.32)
- def test_unbound_builtin_method(self):
self.assertEqual(self._get_summary_line(_pickle.Pickler.dump),[](#l2.34)
"dump(self, obj, /)")[](#l2.35)
these no longer include "self"
- def test_bound_python_method(self):
t = textwrap.TextWrapper()[](#l2.39)
self.assertEqual(self._get_summary_line(t.wrap),[](#l2.40)
"wrap(text) method of textwrap.TextWrapper instance")[](#l2.41)
- @requires_docstrings
- def test_bound_builtin_method(self):
s = StringIO()[](#l2.45)
p = _pickle.Pickler(s)[](#l2.46)
self.assertEqual(self._get_summary_line(p.dump),[](#l2.47)
"dump(obj, /) method of _pickle.Pickler instance")[](#l2.48)
this should never include self- @requires_docstrings
- def test_module_level_callable(self):
self.assertEqual(self._get_summary_line(os.stat),[](#l2.53)
"stat(path, *, dir_fd=None, follow_symlinks=True)")[](#l2.54)
@unittest.skipUnless(threading, 'Threading required for this test.')
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -26,6 +26,9 @@ Core and Builtins Library ------- +- Issue #20710: The pydoc summary line no longer displays the "self" parameter