]> 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:13:45 +0000 (10:13 +0000)
committerRaymond Hettinger <python@rcn.com>
Tue, 27 Jan 2009 10:13:45 +0000 (10:13 +0000)
Lib/collections.py
Lib/doctest.py
Misc/NEWS

index c3faa9a44dffce5ff38c72b2934d116d6195ddb9..fd13bd8a89c5efc76e90618da951bc3dc8538a7b 100644 (file)
@@ -108,7 +108,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 74ae79509285a65032e7ea5922d6bca95a64b84c..5fa588de1d522a8918d1b00b59a6f5ca6d67aea9 100644 (file)
@@ -837,6 +837,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 = []
@@ -1947,6 +1949,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 41feffdb141d24b27ed3f1bc5d2c9ea848625ac4..a300fc09bfe914c47dae7e78abd76594f23d0574 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -104,6 +104,9 @@ Library
   operator module; use the abstract base classes instead.  Also removed
   the repeat() function; use mul() instead.
 
+- Issue 5021:  doctest.testfile() did not create __name__ and
+  collections.namedtuple() relied on __name__ being defined.
+
 - Issue #1885: distutils. When running sdist with --formats=tar,gztar
   the tar file was overriden by the gztar one.