summaryrefslogtreecommitdiffstats
path: root/Lib/collections
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2013-03-02 07:43:48 (GMT)
committerRaymond Hettinger <python@rcn.com>2013-03-02 07:43:48 (GMT)
commit4f4ba166771843a2fbadd4d62771967cfdbedb72 (patch)
treeb6682518cc2ff12571a52affda85de3e2d7a40ab /Lib/collections
parent409f6630918732eb9c088faf51c8210b6ce08b03 (diff)
downloadcpython-4f4ba166771843a2fbadd4d62771967cfdbedb72.zip
cpython-4f4ba166771843a2fbadd4d62771967cfdbedb72.tar.gz
cpython-4f4ba166771843a2fbadd4d62771967cfdbedb72.tar.bz2
Issue #17331: Use isidentifier() instead of isalnum() to check for valid identifiers.
Diffstat (limited to 'Lib/collections')
-rw-r--r--Lib/collections/__init__.py13
1 files changed, 4 insertions, 9 deletions
diff --git a/Lib/collections/__init__.py b/Lib/collections/__init__.py
index e5f9599..2dcc395 100644
--- a/Lib/collections/__init__.py
+++ b/Lib/collections/__init__.py
@@ -323,24 +323,19 @@ def namedtuple(typename, field_names, verbose=False, rename=False):
if rename:
seen = set()
for index, name in enumerate(field_names):
- if (not all(c.isalnum() or c=='_' for c in name)
+ if (not name.isidentifier()
or _iskeyword(name)
- or not name
- or name[0].isdigit()
or name.startswith('_')
or name in seen):
field_names[index] = '_%d' % index
seen.add(name)
for name in [typename] + field_names:
- if not all(c.isalnum() or c=='_' for c in name):
- raise ValueError('Type names and field names can only contain '
- 'alphanumeric characters and underscores: %r' % name)
+ if not name.isidentifier():
+ raise ValueError('Type names and field names must be valid '
+ 'identifiers: %r' % name)
if _iskeyword(name):
raise ValueError('Type names and field names cannot be a '
'keyword: %r' % name)
- if name[0].isdigit():
- raise ValueError('Type names and field names cannot start with '
- 'a number: %r' % name)
seen = set()
for name in field_names:
if name.startswith('_') and not rename: