Skip to content

Commit 8441f15

Browse files
committed
Patch #1530482: add pydoc.render_doc() which returns the documentation
for a thing instead of paging it to stdout, which pydoc.doc() does.
1 parent d9bef35 commit 8441f15

File tree

2 files changed

+29
-22
lines changed

2 files changed

+29
-22
lines changed

Lib/pydoc.py

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1461,31 +1461,35 @@ def resolve(thing, forceload=0):
14611461
else:
14621462
return thing, getattr(thing, '__name__', None)
14631463

1464+
def render_doc(thing, title='Python Library Documentation: %s', forceload=0):
1465+
"""Render text documentation, given an object or a path to an object."""
1466+
object, name = resolve(thing, forceload)
1467+
desc = describe(object)
1468+
module = inspect.getmodule(object)
1469+
if name and '.' in name:
1470+
desc += ' in ' + name[:name.rfind('.')]
1471+
elif module and module is not object:
1472+
desc += ' in module ' + module.__name__
1473+
if type(object) is _OLD_INSTANCE_TYPE:
1474+
# If the passed object is an instance of an old-style class,
1475+
# document its available methods instead of its value.
1476+
object = object.__class__
1477+
elif not (inspect.ismodule(object) or
1478+
inspect.isclass(object) or
1479+
inspect.isroutine(object) or
1480+
inspect.isgetsetdescriptor(object) or
1481+
inspect.ismemberdescriptor(object) or
1482+
isinstance(object, property)):
1483+
# If the passed object is a piece of data or an instance,
1484+
# document its available methods instead of its value.
1485+
object = type(object)
1486+
desc += ' object'
1487+
return title % desc + '\n\n' + text.document(object, name)
1488+
14641489
def doc(thing, title='Python Library Documentation: %s', forceload=0):
14651490
"""Display text documentation, given an object or a path to an object."""
14661491
try:
1467-
object, name = resolve(thing, forceload)
1468-
desc = describe(object)
1469-
module = inspect.getmodule(object)
1470-
if name and '.' in name:
1471-
desc += ' in ' + name[:name.rfind('.')]
1472-
elif module and module is not object:
1473-
desc += ' in module ' + module.__name__
1474-
if type(object) is _OLD_INSTANCE_TYPE:
1475-
# If the passed object is an instance of an old-style class,
1476-
# document its available methods instead of its value.
1477-
object = object.__class__
1478-
elif not (inspect.ismodule(object) or
1479-
inspect.isclass(object) or
1480-
inspect.isroutine(object) or
1481-
inspect.isgetsetdescriptor(object) or
1482-
inspect.ismemberdescriptor(object) or
1483-
isinstance(object, property)):
1484-
# If the passed object is a piece of data or an instance,
1485-
# document its available methods instead of its value.
1486-
object = type(object)
1487-
desc += ' object'
1488-
pager(title % desc + '\n\n' + text.document(object, name))
1492+
pager(render_doc(thing, title, forceload))
14891493
except (ImportError, ErrorDuringImport), value:
14901494
print value
14911495

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,9 @@ Core and builtins
168168
Library
169169
-------
170170

171+
- Patch #1530482: add pydoc.render_doc() which returns the documentation
172+
for a thing instead of paging it to stdout, which pydoc.doc() does.
173+
171174
- Patch #1533909: the timeit module now accepts callables in addition to
172175
strings for the code to time and the setup code. Also added two
173176
convenience functions for instantiating a Timer and calling its methods.

0 commit comments

Comments
 (0)