Issue 21832: Require named tuple inputs to be exact strings

This commit is contained in:
Raymond Hettinger 2014-06-24 13:44:03 -07:00
parent 4c945fe9e9
commit 7c59613a75
1 changed files with 3 additions and 0 deletions

View File

@ -323,6 +323,7 @@ def namedtuple(typename, field_names, verbose=False, rename=False):
if isinstance(field_names, str):
field_names = field_names.replace(',', ' ').split()
field_names = list(map(str, field_names))
typename = str(typename)
if rename:
seen = set()
for index, name in enumerate(field_names):
@ -333,6 +334,8 @@ def namedtuple(typename, field_names, verbose=False, rename=False):
field_names[index] = '_%d' % index
seen.add(name)
for name in [typename] + field_names:
if type(name) != str:
raise TypeError('Type names and field names must be strings')
if not name.isidentifier():
raise ValueError('Type names and field names must be valid '
'identifiers: %r' % name)