Add tests for _source to importable and exec'able.

Move __name__ back out of the template; the responsibility
for setting __name__ lies with the caller (which knows
something about the new namespace), not with the class
definition (which doesn't know about the namespace it is
being built in).
This commit is contained in:
Raymond Hettinger 2011-03-23 20:33:30 -07:00
parent 5d43cff623
commit f6d3e8eaef
2 changed files with 37 additions and 4 deletions

View file

@ -234,8 +234,6 @@ class OrderedDict(dict):
################################################################################
_class_template = '''\
__name__ = 'namedtuple_{typename}'
from builtins import property as _property, tuple as _tuple
from operator import itemgetter as _itemgetter
from collections import OrderedDict
@ -353,8 +351,9 @@ def namedtuple(typename, field_names, verbose=False, rename=False):
for index, name in enumerate(field_names))
)
# Execute the class definition string in a temporary namespace
namespace = {}
# Execute the template string in a temporary namespace and
# support tracing utilities by setting a value for frame.f_globals['__name__']
namespace = dict(__name__='namedtuple_%s' % typename)
try:
exec(class_definition, namespace)
except SyntaxError as e: