diff options
author | Skip Montanaro <skip@pobox.com> | 2008-08-09 19:44:22 (GMT) |
---|---|---|
committer | Skip Montanaro <skip@pobox.com> | 2008-08-09 19:44:22 (GMT) |
commit | af8fcfaef58ee93fd20cfc9e20574f904a7ef802 (patch) | |
tree | 76564b238e0fe27c7d48461ced47d4bacf0c388a /Lib/test/test_csv.py | |
parent | bea655e53bac02bb873b2957332f8ff5ad8fc8e5 (diff) | |
download | cpython-af8fcfaef58ee93fd20cfc9e20574f904a7ef802.zip cpython-af8fcfaef58ee93fd20cfc9e20574f904a7ef802.tar.gz cpython-af8fcfaef58ee93fd20cfc9e20574f904a7ef802.tar.bz2 |
Merged revisions 65605 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r65605 | skip.montanaro | 2008-08-08 17:52:51 -0500 (Fri, 08 Aug 2008) | 1 line
accept issue 3436
........
Diffstat (limited to 'Lib/test/test_csv.py')
-rw-r--r-- | Lib/test/test_csv.py | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/Lib/test/test_csv.py b/Lib/test/test_csv.py index 1dbb71a..9c9840b 100644 --- a/Lib/test/test_csv.py +++ b/Lib/test/test_csv.py @@ -544,6 +544,29 @@ class TestDictFields(unittest.TestCase): fileobj.seek(0) reader = csv.DictReader(fileobj) self.assertEqual(next(reader), {"f1": '1', "f2": '2', "f3": 'abc'}) + self.assertEqual(reader.fieldnames, ["f1", "f2", "f3"]) + + # Two test cases to make sure existing ways of implicitly setting + # fieldnames continue to work. Both arise from discussion in issue3436. + def test_read_dict_fieldnames_from_file(self): + with TemporaryFile("w+") as fileobj: + fileobj.write("f1,f2,f3\r\n1,2,abc\r\n") + fileobj.seek(0) + reader = csv.DictReader(fileobj, + fieldnames=next(csv.reader(fileobj))) + self.assertEqual(reader.fieldnames, ["f1", "f2", "f3"]) + self.assertEqual(next(reader), {"f1": '1', "f2": '2', "f3": 'abc'}) + + def test_read_dict_fieldnames_chain(self): + import itertools + with TemporaryFile("w+") as fileobj: + fileobj.write("f1,f2,f3\r\n1,2,abc\r\n") + fileobj.seek(0) + reader = csv.DictReader(fileobj) + first = next(reader) + for row in itertools.chain([first], reader): + self.assertEqual(reader.fieldnames, ["f1", "f2", "f3"]) + self.assertEqual(row, {"f1": '1', "f2": '2', "f3": 'abc'}) def test_read_long(self): with TemporaryFile("w+") as fileobj: @@ -568,6 +591,7 @@ class TestDictFields(unittest.TestCase): fileobj.write("f1,f2\r\n1,2,abc,4,5,6\r\n") fileobj.seek(0) reader = csv.DictReader(fileobj, restkey="_rest") + self.assertEqual(reader.fieldnames, ["f1", "f2"]) self.assertEqual(next(reader), {"f1": '1', "f2": '2', "_rest": ["abc", "4", "5", "6"]}) |