Issue 5021: doctest.testfile should set __name__
diff --git a/Lib/collections.py b/Lib/collections.py
index ace2b2a..81d8928 100644
--- a/Lib/collections.py
+++ b/Lib/collections.py
@@ -103,7 +103,7 @@
     # 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
 
diff --git a/Lib/doctest.py b/Lib/doctest.py
index 3f2baa5..aeeb15d 100644
--- a/Lib/doctest.py
+++ b/Lib/doctest.py
@@ -844,6 +844,8 @@
             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 @@
         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)