From: Raymond Hettinger Date: Tue, 27 Jan 2009 10:06:09 +0000 (+0000) Subject: Issue 5021: doctest.testfile should set __name__ X-Git-Tag: v3.1a1~372 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=0f05517d85efcce6fc57cec63ec54a4612ee5382;p=python Issue 5021: doctest.testfile should set __name__ --- diff --git a/Lib/collections.py b/Lib/collections.py index 6831cf1396..4bde5b36b7 100644 --- a/Lib/collections.py +++ b/Lib/collections.py @@ -111,7 +111,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 diff --git a/Lib/doctest.py b/Lib/doctest.py index 74ae795092..5fa588de1d 100644 --- a/Lib/doctest.py +++ b/Lib/doctest.py @@ -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) diff --git a/Misc/NEWS b/Misc/NEWS index ee2e2628b8..3fb8ed9111 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -148,6 +148,12 @@ 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. + +- Backport importlib from Python 3.1. Only the import_module() function has + been backported to help facilitate transitions from 2.7 to 3.1. + - Issue #1885: distutils. When running sdist with --formats=tar,gztar the tar file was overriden by the gztar one.