[Python-checkins] r61713 - in python/branches/release25-maint: Lib/csv.py Lib/test/test_csv.py Misc/NEWS

georg.brandl python-checkins at python.org
Fri Mar 21 21:01:55 CET 2008


Author: georg.brandl
Date: Fri Mar 21 21:01:55 2008
New Revision: 61713

Modified:
   python/branches/release25-maint/Lib/csv.py
   python/branches/release25-maint/Lib/test/test_csv.py
   python/branches/release25-maint/Misc/NEWS
Log:
Issue #2432: give DictReader the dialect and line_num attributes
advertised in the docs.
 (backport from r61712)

Modified: python/branches/release25-maint/Lib/csv.py
==============================================================================
--- python/branches/release25-maint/Lib/csv.py	(original)
+++ python/branches/release25-maint/Lib/csv.py	Fri Mar 21 21:01:55 2008
@@ -75,6 +75,8 @@
         self.restkey = restkey          # key to catch long rows
         self.restval = restval          # default value for short rows
         self.reader = reader(f, dialect, *args, **kwds)
+        self.dialect = dialect
+        self.line_num = 0
 
     def __iter__(self):
         return self
@@ -84,6 +86,7 @@
         if self.fieldnames is None:
             self.fieldnames = row
             row = self.reader.next()
+        self.line_num = self.reader.line_num
 
         # unlike the basic reader, we prefer not to return blanks,
         # because we will typically wind up with a dict full of None

Modified: python/branches/release25-maint/Lib/test/test_csv.py
==============================================================================
--- python/branches/release25-maint/Lib/test/test_csv.py	(original)
+++ python/branches/release25-maint/Lib/test/test_csv.py	Fri Mar 21 21:01:55 2008
@@ -269,16 +269,18 @@
             csv.field_size_limit(limit)
 
     def test_read_linenum(self):
-        r = csv.reader(['line,1', 'line,2', 'line,3'])
-        self.assertEqual(r.line_num, 0)
-        r.next()
-        self.assertEqual(r.line_num, 1)
-        r.next()
-        self.assertEqual(r.line_num, 2)
-        r.next()
-        self.assertEqual(r.line_num, 3)
-        self.assertRaises(StopIteration, r.next)
-        self.assertEqual(r.line_num, 3)
+        for r in (csv.reader(['line,1', 'line,2', 'line,3']),
+                  csv.DictReader(['line,1', 'line,2', 'line,3'],
+                                 fieldnames=['a', 'b', 'c'])):
+            self.assertEqual(r.line_num, 0)
+            r.next()
+            self.assertEqual(r.line_num, 1)
+            r.next()
+            self.assertEqual(r.line_num, 2)
+            r.next()
+            self.assertEqual(r.line_num, 3)
+            self.assertRaises(StopIteration, r.next)
+            self.assertEqual(r.line_num, 3)
 
 class TestDialectRegistry(unittest.TestCase):
     def test_registry_badargs(self):

Modified: python/branches/release25-maint/Misc/NEWS
==============================================================================
--- python/branches/release25-maint/Misc/NEWS	(original)
+++ python/branches/release25-maint/Misc/NEWS	Fri Mar 21 21:01:55 2008
@@ -20,6 +20,9 @@
 Library
 -------
 
+- Issue #2432: give DictReader the dialect and line_num attributes
+  advertised in the docs.
+
 - Issue #1747858: Fix chown to work with large uid's and gid's on 64-bit
   platforms.
 


More information about the Python-checkins mailing list