Made pydoc accept methods on builtin classes.

This commit is contained in:
Jeremy Fincher 2003-10-20 06:43:59 +00:00
parent e760320ca0
commit 2d56e7257e
2 changed files with 36 additions and 22 deletions

View File

@ -49,6 +49,7 @@ sys.stdout = StringIO()
import this import this
sys.stdout = sys.__stdout__ sys.stdout = sys.__stdout__
import debug
import utils import utils
import privmsgs import privmsgs
import callbacks import callbacks
@ -77,12 +78,26 @@ class Python(callbacks.Privmsg):
Returns the __doc__ string for a given Python function. Returns the __doc__ string for a given Python function.
""" """
def normalize(s):
return utils.normalizeWhitespace(s.replace('\n\n', '.'))
funcname = privmsgs.getArgs(args) funcname = privmsgs.getArgs(args)
if funcname.translate(string.ascii, self.modulechars) != '': if funcname.translate(string.ascii, self.modulechars) != '':
irc.error('That\'s not a valid module or function name.') irc.error('That\'s not a valid module or function name.')
return return
if '.' in funcname: if '.' in funcname:
parts = funcname.split('.') parts = funcname.split('.')
if len(parts) == 2 and parts[0] in __builtins__:
(objectname, methodname) = parts
obj = __builtins__[objectname]
if hasattr(obj, methodname):
obj = getattr(obj, methodname)
if hasattr(obj, '__doc__'):
irc.reply(msg, obj.__doc__)
else:
irc.reply(msg, '%s has no documentation' % funcname)
else:
irc.reply(msg, '%s has no method %s' % (parts[0],parts[1]))
else:
functionName = parts.pop() functionName = parts.pop()
path = pythonPath path = pythonPath
for name in parts: for name in parts:
@ -108,9 +123,7 @@ class Python(callbacks.Privmsg):
try: try:
f = __builtins__[funcname] f = __builtins__[funcname]
if hasattr(f, '__doc__'): if hasattr(f, '__doc__'):
s = f.__doc__.replace('\n\n', '. ') irc.reply(msg, normalize(f.__doc__))
s = utils.normalizeWhitespace(s)
irc.reply(msg, s)
else: else:
irc.error(msg, 'That function has no documentation.') irc.error(msg, 'That function has no documentation.')
except SyntaxError: except SyntaxError:

View File

@ -39,6 +39,7 @@ class PythonTestCase(PluginTestCase, PluginDocumentation):
self.assertError('pydoc foobar') self.assertError('pydoc foobar')
self.assertError('pydoc assert') self.assertError('pydoc assert')
self.assertNotError('pydoc str') self.assertNotError('pydoc str')
self.assertNotError('pydoc list.reverse')
if os.name == 'posix': if os.name == 'posix':
self.assertNotRegexp('pydoc crypt.crypt', 'NameError') self.assertNotRegexp('pydoc crypt.crypt', 'NameError')
self.assertNotError('pydoc crypt.crypt') self.assertNotError('pydoc crypt.crypt')