summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGeorg Brandl <georg@python.org>2007-03-13 20:02:57 (GMT)
committerGeorg Brandl <georg@python.org>2007-03-13 20:02:57 (GMT)
commit8441f15626998df7881f49223948b7e6fef18c08 (patch)
treed801551ebb2ced10e56de64b59107874a627f2a7
parentd9bef35e3c0c4c1dfa5760fd76c720afa1956170 (diff)
downloadcpython-8441f15626998df7881f49223948b7e6fef18c08.zip
cpython-8441f15626998df7881f49223948b7e6fef18c08.tar.gz
cpython-8441f15626998df7881f49223948b7e6fef18c08.tar.bz2
Patch #1530482: add pydoc.render_doc() which returns the documentation
for a thing instead of paging it to stdout, which pydoc.doc() does.
-rwxr-xr-xLib/pydoc.py48
-rw-r--r--Misc/NEWS3
2 files changed, 29 insertions, 22 deletions
diff --git a/Lib/pydoc.py b/Lib/pydoc.py
index 8435175..d7c0d00 100755
--- a/Lib/pydoc.py
+++ b/Lib/pydoc.py
@@ -1461,31 +1461,35 @@ def resolve(thing, forceload=0):
else:
return thing, getattr(thing, '__name__', None)
+def render_doc(thing, title='Python Library Documentation: %s', forceload=0):
+ """Render text documentation, given an object or a path to an object."""
+ object, name = resolve(thing, forceload)
+ desc = describe(object)
+ module = inspect.getmodule(object)
+ if name and '.' in name:
+ desc += ' in ' + name[:name.rfind('.')]
+ elif module and module is not object:
+ desc += ' in module ' + module.__name__
+ if type(object) is _OLD_INSTANCE_TYPE:
+ # If the passed object is an instance of an old-style class,
+ # document its available methods instead of its value.
+ object = object.__class__
+ elif not (inspect.ismodule(object) or
+ inspect.isclass(object) or
+ inspect.isroutine(object) or
+ inspect.isgetsetdescriptor(object) or
+ inspect.ismemberdescriptor(object) or
+ isinstance(object, property)):
+ # If the passed object is a piece of data or an instance,
+ # document its available methods instead of its value.
+ object = type(object)
+ desc += ' object'
+ return title % desc + '\n\n' + text.document(object, name)
+
def doc(thing, title='Python Library Documentation: %s', forceload=0):
"""Display text documentation, given an object or a path to an object."""
try:
- object, name = resolve(thing, forceload)
- desc = describe(object)
- module = inspect.getmodule(object)
- if name and '.' in name:
- desc += ' in ' + name[:name.rfind('.')]
- elif module and module is not object:
- desc += ' in module ' + module.__name__
- if type(object) is _OLD_INSTANCE_TYPE:
- # If the passed object is an instance of an old-style class,
- # document its available methods instead of its value.
- object = object.__class__
- elif not (inspect.ismodule(object) or
- inspect.isclass(object) or
- inspect.isroutine(object) or
- inspect.isgetsetdescriptor(object) or
- inspect.ismemberdescriptor(object) or
- isinstance(object, property)):
- # If the passed object is a piece of data or an instance,
- # document its available methods instead of its value.
- object = type(object)
- desc += ' object'
- pager(title % desc + '\n\n' + text.document(object, name))
+ pager(render_doc(thing, title, forceload))
except (ImportError, ErrorDuringImport), value:
print value
diff --git a/Misc/NEWS b/Misc/NEWS
index ee4b538..634ada1 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -168,6 +168,9 @@ Core and builtins
Library
-------
+- Patch #1530482: add pydoc.render_doc() which returns the documentation
+ for a thing instead of paging it to stdout, which pydoc.doc() does.
+
- Patch #1533909: the timeit module now accepts callables in addition to
strings for the code to time and the setup code. Also added two
convenience functions for instantiating a Timer and calling its methods.