diff -r 282f8322fc1b Lib/doctest.py --- a/Lib/doctest.py Fri Oct 30 02:47:48 2015 -0400 +++ b/Lib/doctest.py Sun Nov 08 17:19:35 2015 +1100 @@ -1019,6 +1019,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.__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; @@ -1028,6 +1042,14 @@ # then return None (no test for this object). if isinstance(obj, str): docstring = obj + elif inspect.iscode(obj): + if obj.co_consts and isinstance(obj.co_consts[0], str): + if obj.co_consts[0] == name and isinstance(obj.co_consts[1], str): + docstring = obj.co_consts[1] + else: + docstring = obj.co_consts[0] + else: + docstring = '' else: try: if obj.__doc__ is None: diff -r 282f8322fc1b Lib/test/test_doctest.py --- a/Lib/test/test_doctest.py Fri Oct 30 02:47:48 2015 -0400 +++ b/Lib/test/test_doctest.py Sun Nov 08 17:19:35 2015 +1100 @@ -133,6 +133,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) ###################################################################### @@ -506,6 +523,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 ~~~~~~~~~~~~~~~~~~~~~~~~