mirror of
https://github.com/python/cpython.git
synced 2025-08-30 13:38:43 +00:00
Extend stripid() to handle strings ending in more than one '>'.
Add resolve() to handle looking up objects and names (fix SF bug 586931). Add a nicer error message when given a filename that doesn't exist.
This commit is contained in:
parent
3cb8e54da0
commit
45daeb093f
1 changed files with 35 additions and 36 deletions
71
Lib/pydoc.py
71
Lib/pydoc.py
|
@ -107,9 +107,9 @@ def cram(text, maxlen):
|
||||||
def stripid(text):
|
def stripid(text):
|
||||||
"""Remove the hexadecimal id from a Python object representation."""
|
"""Remove the hexadecimal id from a Python object representation."""
|
||||||
# The behaviour of %p is implementation-dependent; we check two cases.
|
# The behaviour of %p is implementation-dependent; we check two cases.
|
||||||
for pattern in [' at 0x[0-9a-f]{6,}>$', ' at [0-9A-F]{8,}>$']:
|
for pattern in [' at 0x[0-9a-f]{6,}(>+)$', ' at [0-9A-F]{8,}(>+)$']:
|
||||||
if re.search(pattern, repr(Exception)):
|
if re.search(pattern, repr(Exception)):
|
||||||
return re.sub(pattern, '>', text)
|
return re.sub(pattern, '\\1', text)
|
||||||
return text
|
return text
|
||||||
|
|
||||||
def _is_some_method(object):
|
def _is_some_method(object):
|
||||||
|
@ -1325,45 +1325,41 @@ def locate(path, forceload=0):
|
||||||
text = TextDoc()
|
text = TextDoc()
|
||||||
html = HTMLDoc()
|
html = HTMLDoc()
|
||||||
|
|
||||||
|
def resolve(thing, forceload=0):
|
||||||
|
"""Given an object or a path to an object, get the object and its name."""
|
||||||
|
if isinstance(thing, str):
|
||||||
|
object = locate(thing, forceload)
|
||||||
|
if not object:
|
||||||
|
raise ImportError, 'no Python documentation found for %r' % thing
|
||||||
|
return object, thing
|
||||||
|
else:
|
||||||
|
return thing, getattr(thing, '__name__', None)
|
||||||
|
|
||||||
def doc(thing, title='Python Library Documentation: %s', forceload=0):
|
def doc(thing, title='Python Library Documentation: %s', forceload=0):
|
||||||
"""Display text documentation, given an object or a path to an object."""
|
"""Display text documentation, given an object or a path to an object."""
|
||||||
suffix, name = '', None
|
try:
|
||||||
if type(thing) is type(''):
|
object, name = resolve(thing, forceload)
|
||||||
try:
|
desc = describe(object)
|
||||||
object = locate(thing, forceload)
|
module = inspect.getmodule(object)
|
||||||
except ErrorDuringImport, value:
|
if name and '.' in name:
|
||||||
print value
|
desc += ' in ' + name[:name.rfind('.')]
|
||||||
return
|
elif module and module is not object:
|
||||||
if not object:
|
desc += ' in module ' + module.__name__
|
||||||
print 'no Python documentation found for %s' % repr(thing)
|
pager(title % desc + '\n\n' + text.document(object, name))
|
||||||
return
|
except (ImportError, ErrorDuringImport), value:
|
||||||
parts = split(thing, '.')
|
print value
|
||||||
if len(parts) > 1: suffix = ' in ' + join(parts[:-1], '.')
|
|
||||||
name = parts[-1]
|
|
||||||
thing = object
|
|
||||||
|
|
||||||
desc = describe(thing)
|
def writedoc(thing, forceload=0):
|
||||||
module = inspect.getmodule(thing)
|
|
||||||
if not suffix and module and module is not thing:
|
|
||||||
suffix = ' in module ' + module.__name__
|
|
||||||
pager(title % (desc + suffix) + '\n\n' + text.document(thing, name))
|
|
||||||
|
|
||||||
def writedoc(key, forceload=0):
|
|
||||||
"""Write HTML documentation to a file in the current directory."""
|
"""Write HTML documentation to a file in the current directory."""
|
||||||
try:
|
try:
|
||||||
object = locate(key, forceload)
|
object, name = resolve(thing, forceload)
|
||||||
except ErrorDuringImport, value:
|
page = html.page(describe(object), html.document(object, name))
|
||||||
|
file = open(name + '.html', 'w')
|
||||||
|
file.write(page)
|
||||||
|
file.close()
|
||||||
|
print 'wrote', name + '.html'
|
||||||
|
except (ImportError, ErrorDuringImport), value:
|
||||||
print value
|
print value
|
||||||
else:
|
|
||||||
if object:
|
|
||||||
page = html.page(describe(object),
|
|
||||||
html.document(object, object.__name__))
|
|
||||||
file = open(key + '.html', 'w')
|
|
||||||
file.write(page)
|
|
||||||
file.close()
|
|
||||||
print 'wrote', key + '.html'
|
|
||||||
else:
|
|
||||||
print 'no Python documentation found for %s' % repr(key)
|
|
||||||
|
|
||||||
def writedocs(dir, pkgpath='', done=None):
|
def writedocs(dir, pkgpath='', done=None):
|
||||||
"""Write out HTML documentation for all modules in a directory tree."""
|
"""Write out HTML documentation for all modules in a directory tree."""
|
||||||
|
@ -2034,7 +2030,7 @@ def gui():
|
||||||
# -------------------------------------------------- command-line interface
|
# -------------------------------------------------- command-line interface
|
||||||
|
|
||||||
def ispath(x):
|
def ispath(x):
|
||||||
return type(x) is types.StringType and find(x, os.sep) >= 0
|
return isinstance(x, str) and find(x, os.sep) >= 0
|
||||||
|
|
||||||
def cli():
|
def cli():
|
||||||
"""Command-line interface (looks at sys.argv to decide what to do)."""
|
"""Command-line interface (looks at sys.argv to decide what to do)."""
|
||||||
|
@ -2074,6 +2070,9 @@ def cli():
|
||||||
|
|
||||||
if not args: raise BadUsage
|
if not args: raise BadUsage
|
||||||
for arg in args:
|
for arg in args:
|
||||||
|
if ispath(arg) and not os.path.exists(arg):
|
||||||
|
print 'file %r does not exist' % arg
|
||||||
|
break
|
||||||
try:
|
try:
|
||||||
if ispath(arg) and os.path.isfile(arg):
|
if ispath(arg) and os.path.isfile(arg):
|
||||||
arg = importfile(arg)
|
arg = importfile(arg)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue