Index: Lib/doctest.py =================================================================== --- Lib/doctest.py (revision 54419) +++ Lib/doctest.py (working copy) @@ -911,6 +911,20 @@ self._find(tests, val, valname, module, source_lines, globs, seen) + # Look for nested classes and functions in functions. + code = obj + if inspect.isfunction(obj): + code = obj.func_code + if inspect.iscode(code) and self._recurse: + for const in code.co_consts: + # [XX] this should check for _from_module(), but the best + # we could do would be to compare filenames, and that is + # unreliable because of custom importers. + if inspect.iscode(const): + valname = '%s..%s' % (name, const.co_name) + self._find(tests, const, valname, module, source_lines, + globs, seen) + def _get_test(self, obj, name, module, globs, source_lines): """ Return a DocTest for the given object, if it defines a docstring; @@ -920,6 +934,11 @@ # then return None (no test for this object). if isinstance(obj, basestring): docstring = obj + elif inspect.iscode(obj): + if obj.co_consts and isinstance(obj.co_consts[0], basestring): + docstring = obj.co_consts[0] + else: + docstring = '' else: try: if obj.__doc__ is None: Index: Lib/test/test_doctest.py =================================================================== --- Lib/test/test_doctest.py (revision 54419) +++ Lib/test/test_doctest.py (working copy) @@ -127,6 +127,23 @@ """ return self.val + def nesting_in_functions(self, val): + """ + >>> print SampleNewStyleClass(1).nesting_in_functions(1) + 1 + """ + class NestedClass: + """ + >>> print SampleNewStyleClass(3).nesting_in_functions(3) + 3 + """ + def nested_function(): + """ + >>> print SampleNewStyleClass(2).nesting_in_functions(2) + 2 + """ + return val + ###################################################################### ## Fake stdin (for testing interactive debugging) ###################################################################### @@ -440,6 +457,9 @@ 1 SampleNewStyleClass.__init__ 1 SampleNewStyleClass.double 1 SampleNewStyleClass.get + 1 SampleNewStyleClass.nesting_in_functions + 1 SampleNewStyleClass.nesting_in_functions..NestedClass + 1 SampleNewStyleClass.nesting_in_functions..nested_function Finding Tests in Modules ~~~~~~~~~~~~~~~~~~~~~~~~