]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue 5021: doctest.testfile should set __name__
authorRaymond Hettinger <python@rcn.com>
Tue, 27 Jan 2009 10:30:26 +0000 (10:30 +0000)
committerRaymond Hettinger <python@rcn.com>
Tue, 27 Jan 2009 10:30:26 +0000 (10:30 +0000)
Lib/collections.py
Lib/doctest.py
Misc/NEWS

index ace2b2a446b6229977a81b105755a78acbb7e3c1..81d89282728932378f30304a918c47a17d9e9619 100644 (file)
@@ -103,7 +103,7 @@ def namedtuple(typename, field_names, verbose=False):
     # where the named tuple is created.  Bypass this step in enviroments where
     # sys._getframe is not defined (Jython for example).
     if hasattr(_sys, '_getframe'):
-        result.__module__ = _sys._getframe(1).f_globals['__name__']
+        result.__module__ = _sys._getframe(1).f_globals.get('__name__', '__main__')
 
     return result
 
index 3f2baa54340a13379a987265141328eef41fe50d..aeeb15d1d56d474485382dc4022080468ece7e4c 100644 (file)
@@ -844,6 +844,8 @@ class DocTestFinder:
             globs = globs.copy()
         if extraglobs is not None:
             globs.update(extraglobs)
+        if '__name__' not in globs:
+            globs['__name__'] = '__main__'  # provide a default module name
 
         # Recursively expore `obj`, extracting DocTests.
         tests = []
@@ -1937,6 +1939,8 @@ def testfile(filename, module_relative=True, name=None, package=None,
         globs = globs.copy()
     if extraglobs is not None:
         globs.update(extraglobs)
+    if '__name__' not in globs:
+        globs['__name__'] = '__main__'
 
     if raise_on_error:
         runner = DebugRunner(verbose=verbose, optionflags=optionflags)
index 431a3e08177ea0c4f0bc46cb7209a341b6f6e478..1a39f0ec6bc42a90ea37ae8ea61b05c296957e00 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -76,6 +76,9 @@ Core and Builtins
 Library
 -------
 
+- Issue 5021:  doctest.testfile() did not create __name__ and
+  collections.namedtuple() relied on __name__ being defined.
+
 - Issue #3881: Help Tcl to load even when started through the
   unreadable local symlink to "Program Files" on Vista.