Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4581cfa

Browse files
committed
Patch #486438: Make module argument to testmod optional.
1 parent f86e8ef commit 4581cfa

3 files changed

Lines changed: 20 additions & 5 deletions

File tree

Doc/lib/libdoctest.tex

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@ \subsection{Normal Usage}
152152
_test()
153153
\end{verbatim}
154154

155+
If you want to test the module as the main module, you don't need to
156+
pass M to \function{testmod}; in this case, it will test the current
157+
module.
158+
155159
Then running the module as a script causes the examples in the docstrings
156160
to get executed and verified:
157161

@@ -392,7 +396,7 @@ \subsection{Warnings}
392396
\begin{verbatim}
393397
def _test():
394398
import doctest, sys
395-
doctest.testmod(sys.modules["__main__"])
399+
doctest.testmod()
396400
\end{verbatim}
397401
\end{enumerate}
398402

Lib/doctest.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,12 +1044,13 @@ def __runone(self, target, name):
10441044

10451045
master = None
10461046

1047-
def testmod(m, name=None, globs=None, verbose=None, isprivate=None,
1047+
def testmod(m=None, name=None, globs=None, verbose=None, isprivate=None,
10481048
report=1):
1049-
"""m, name=None, globs=None, verbose=None, isprivate=None, report=1
1049+
"""m=None, name=None, globs=None, verbose=None, isprivate=None, report=1
10501050
1051-
Test examples in docstrings in functions and classes reachable from
1052-
module m, starting with m.__doc__. Private names are skipped.
1051+
Test examples in docstrings in functions and classes reachable
1052+
from module m (or the current module if m is not supplied), starting
1053+
with m.__doc__. Private names are skipped.
10531054
10541055
Also test examples reachable from dict m.__test__ if it exists and is
10551056
not None. m.__dict__ maps names to functions, classes and strings;
@@ -1090,6 +1091,13 @@ class doctest.Tester, then merges the results into (or creates)
10901091

10911092
global master
10921093

1094+
if m is None:
1095+
import sys
1096+
# DWA - m will still be None if this wasn't invoked from the command
1097+
# line, in which case the following TypeError is about as good an error
1098+
# as we should expect
1099+
m = sys.modules.get('__main__')
1100+
10931101
if not _ismodule(m):
10941102
raise TypeError("testmod: module required; " + `m`)
10951103
if name is None:

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,9 @@ Extension modules
389389
Library
390390
-------
391391

392+
- doctest.testmod can now be called without argument, which means to
393+
test the current module.
394+
392395
- When cancelling a server that implemented threading with a keyboard
393396
interrupt, the server would shut down but not terminate (waiting on
394397
client threads). A new member variable, daemon_threads, was added to

0 commit comments

Comments
 (0)