Kaydet (Commit) 6ee7bc04 authored tarafından Raymond Hettinger's avatar Raymond Hettinger

Fix namedtuple bug reported by Glenn Linderman. Template did not form correctly…

Fix namedtuple bug reported by Glenn Linderman.  Template did not form correctly if the field names were input in Unicode.
üst 17617a07
...@@ -38,7 +38,7 @@ def namedtuple(typename, field_names, verbose=False): ...@@ -38,7 +38,7 @@ def namedtuple(typename, field_names, verbose=False):
# generating informative error messages and preventing template injection attacks. # generating informative error messages and preventing template injection attacks.
if isinstance(field_names, basestring): if isinstance(field_names, basestring):
field_names = field_names.replace(',', ' ').split() # names separated by whitespace and/or commas field_names = field_names.replace(',', ' ').split() # names separated by whitespace and/or commas
field_names = tuple(field_names) field_names = tuple(map(str, field_names))
for name in (typename,) + field_names: for name in (typename,) + field_names:
if not all(c.isalnum() or c=='_' for c in name): 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) raise ValueError('Type names and field names can only contain alphanumeric characters and underscores: %r' % name)
......
...@@ -34,6 +34,11 @@ class TestNamedTuple(unittest.TestCase): ...@@ -34,6 +34,11 @@ class TestNamedTuple(unittest.TestCase):
namedtuple('Point0', 'x1 y2') # Verify that numbers are allowed in names namedtuple('Point0', 'x1 y2') # Verify that numbers are allowed in names
namedtuple('_', 'a b c') # Test leading underscores in a typename namedtuple('_', 'a b c') # Test leading underscores in a typename
nt = namedtuple('nt', u'the quick brown fox') # check unicode input
self.assert_("u'" not in repr(nt._fields))
nt = namedtuple('nt', (u'the', u'quick')) # check unicode input
self.assert_("u'" not in repr(nt._fields))
self.assertRaises(TypeError, Point._make, [11]) # catch too few args self.assertRaises(TypeError, Point._make, [11]) # catch too few args
self.assertRaises(TypeError, Point._make, [11, 22, 33]) # catch too many args self.assertRaises(TypeError, Point._make, [11, 22, 33]) # catch too many args
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment