diff -r 0d2b840f6df8 lib-python/2.7/test/test_help.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib-python/2.7/test/test_help.py Mon Feb 27 02:27:20 2017 -0800 @@ -0,0 +1,83 @@ +import pydoc +import importlib +import StringIO +import unittest +import test.test_support +from test.script_helper import assert_python_ok +from test.test_support import ( + TESTFN, rmtree, reap_children, captured_stdout) + +from test import pydoc_mod + + +class TestHelpIncludesDocstring(unittest.TestCase): + """issue 2545: help() does not work on Java-implemented modules + http://bugs.jython.org/issue2545 + """ + @classmethod + def setUpClass(cls): + """Back up global variables so they can be restored at the exit of the unit test + Assume this unit test is executed atomically, otherwise it should be wrapped in a lock + to avoid unintended interactions with tests running while the monkey patch is in place. + """ + cls.__defaultpager = pydoc.pager + + @classmethod + def tearDownClass(cls): + pydoc.pager = cls.__defaultpager + + def setUp(self): + """Monkey-patch the pager target + Basically the same as pydoc.plainpager, but capturing the output to StringIO rather than stdout + Changing the pager avoids the environment-dependent behavior of pydoc. + """ + self.__out = StringIO.StringIO() + stringiopager = lambda text: self.__out.write(pydoc.plain(text)) + pydoc.pager = stringiopager + + def tearDown(self): + self.__out.close() + + def checkModule(self, modulename): + module = importlib.import_module(modulename) + help(modulename) + helptext = self.__out.getvalue() + # FIXME: ignore whitespace/formatting when comparing help text + self.assertIn(module.__doc__, helptext) + + + def test_python(self): + """Check that help(module) displays docstring for pure python modules""" + self.checkModule('collections') # lib-python/2.7/collections.py -> dist/Lib/collections.py + + + def test_java(self): + """Check that help(module) displays docstring for modules implemented in Java + where the docstring is defined as a static string. + + public static PyString __doc__ = new PyString(...); + """ + self.checkModule('_imp') # src/org/python/modules/_imp.java -> build/classes/org/python/modules/_imp.class + + def test_java_classDictInit(self): + """Check that help(module) displays docstring for modules implemented in Java + where the docstring is defined by a call to classDictInit. + + public static void classDictInit(PyObject dict) { + dict.__setitem__("__doc__", new PyString(...)); + } + """ + self.checkModule('_weakref') # src/org/python/modules/_weakref/WeakrefModule.java -> build/classes/org/python/modules/_weakref/WeakrefModule.class + + + +def test_main(): + try: + test.test_support.run_unittest( + TestHelpIncludesDocstring + ) + finally: + reap_children() + +if __name__ == "__main__": + test_main()