summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2007-10-09 01:36:23 (GMT)
committerRaymond Hettinger <python@rcn.com>2007-10-09 01:36:23 (GMT)
commit163f622c03487a747f89b4359663a8ce52b57f81 (patch)
treee685cc2dbc1523d71c26156256b3902ad1db5ae8
parent4b3074c795c52e5d2327ddf38f71f4bdf38991bd (diff)
downloadcpython-163f622c03487a747f89b4359663a8ce52b57f81.zip
cpython-163f622c03487a747f89b4359663a8ce52b57f81.tar.gz
cpython-163f622c03487a747f89b4359663a8ce52b57f81.tar.bz2
Make the error messages more specific
-rw-r--r--Lib/collections.py15
1 files changed, 9 insertions, 6 deletions
diff --git a/Lib/collections.py b/Lib/collections.py
index 242de60..4515759 100644
--- a/Lib/collections.py
+++ b/Lib/collections.py
@@ -34,12 +34,15 @@ def named_tuple(typename, field_names, verbose=False):
field_names = tuple(field_names)
if not ''.join((typename,) + field_names).replace('_', '').isalnum():
raise ValueError('Type names and field names can only contain alphanumeric characters and underscores')
- if any(name.startswith('__') and name.endswith('__') for name in field_names):
- raise ValueError('Field names cannot start and end with double underscores')
- if any(name[:1].isdigit() for name in field_names):
- raise ValueError('Field names cannot start with a number')
- if len(field_names) != len(set(field_names)):
- raise ValueError('Encountered duplicate field name')
+ seen_names = set()
+ for name in field_names:
+ if name.startswith('__') and name.endswith('__'):
+ raise ValueError('Field names cannot start and end with double underscores: %s' % name)
+ if name[:1].isdigit():
+ raise ValueError('Field names cannot start with a number: %s' % name)
+ if name in seen_names:
+ raise ValueError('Encountered duplicate field name: %s' % name)
+ seen_names.add(name)
# Create and fill-in the class template
argtxt = repr(field_names).replace("'", "")[1:-1] # tuple repr without parens or quotes