summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2014-06-24 22:20:55 (GMT)
committerRaymond Hettinger <python@rcn.com>2014-06-24 22:20:55 (GMT)
commitbc000509e8ae782ef96c656885c3342d11fca3a6 (patch)
treec792799018dbe6157e8c0b1a74bfa102dfc31bdf
parentdf29c4a83d13d96cfcf809e648ee9b31a4174729 (diff)
downloadcpython-bc000509e8ae782ef96c656885c3342d11fca3a6.zip
cpython-bc000509e8ae782ef96c656885c3342d11fca3a6.tar.gz
cpython-bc000509e8ae782ef96c656885c3342d11fca3a6.tar.bz2
Issue 21832: Require named tuple inputs to be exact strings
-rw-r--r--Lib/collections/__init__.py3
1 files changed, 3 insertions, 0 deletions
diff --git a/Lib/collections/__init__.py b/Lib/collections/__init__.py
index d6deb6a..d993fe0 100644
--- a/Lib/collections/__init__.py
+++ b/Lib/collections/__init__.py
@@ -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)