]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Add counting of source iterator lines to the reader object - handy for
authorAndrew McNamara <andrewm@object-craft.com.au>
Wed, 12 Jan 2005 11:17:16 +0000 (11:17 +0000)
committerAndrew McNamara <andrewm@object-craft.com.au>
Wed, 12 Jan 2005 11:17:16 +0000 (11:17 +0000)
user error messages (otherwise difficult to do without instrumenting
the source).

Lib/test/test_csv.py
Misc/NEWS
Modules/_csv.c

index d458332294f001ed0406d82ba4f6522cb7abdd21..21bd8d72b37f159c5343d63662c82f429b2fb6a3 100644 (file)
@@ -242,7 +242,7 @@ class Test_Csv(unittest.TestCase):
         self._read_test(['1,",3,",5'], [['1', '"', '3', '"', '5']],
                         quoting=csv.QUOTE_NONE, escapechar='\\')
         # will this fail where locale uses comma for decimals?
-        self._read_test([',3,"5",7.3'], [['', 3, '5', 7.3]],
+        self._read_test([',3,"5",7.3, 9'], [['', 3, '5', 7.3, 9]],
                         quoting=csv.QUOTE_NONNUMERIC)
         self.assertRaises(ValueError, self._read_test, 
                           ['abc,3'], [[]],
@@ -267,6 +267,18 @@ class Test_Csv(unittest.TestCase):
         finally:
             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)
+
 class TestDialectRegistry(unittest.TestCase):
     def test_registry_badargs(self):
         self.assertRaises(TypeError, csv.list_dialects, None)
index 828063a87909b6412234436ecc052850bdf45697..26edc897046e3d00959d5f41f7cea2eb376e1617 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -66,6 +66,10 @@ Library
   + A new module method csv.field_size_limit() has been added that sets
     the parser field size limit (returning the former limit). The initial
     limit is 128kB.
+  + A line_num attribute has been added to the reader object, which tracks
+    the number of lines read from the source iterator. This is not
+    the same as the number of records returned, as records can span
+    multiple lines.
   + reader and writer objects were not being registered with the cyclic-GC.
     This has been fixed.
 
index 69417145066f788590c8edba4b8226dd1282649d..ce4c73819d46151f8306bbf9abdbe83653253586 100644 (file)
@@ -98,6 +98,7 @@ typedef struct {
        int field_len;          /* length of current field */
        int had_parse_error;    /* did we have a parse error? */
        int numeric_field;      /* treat field as numeric */
+       unsigned long line_num; /* Source-file line number */
 } ReaderObj;
 
 staticforward PyTypeObject Reader_Type;
@@ -734,6 +735,7 @@ parse_process_char(ReaderObj *self, char c)
 
 static struct PyMemberDef Reader_memberlist[] = {
        { "dialect", T_OBJECT, R_OFF(dialect), RO },
+       { "line_num", T_ULONG, R_OFF(line_num), RO },
        { NULL }
 };
 
@@ -753,6 +755,7 @@ Reader_iternext(ReaderObj *self)
                                                     "newline inside string");
                         return NULL;
                 }
+               ++self->line_num;
 
                 if (self->had_parse_error)
                        if (parse_reset(self) < 0) {
@@ -924,6 +927,7 @@ csv_reader(PyObject *module, PyObject *args, PyObject *keyword_args)
         self->input_iter = NULL;
        self->field = NULL;
        self->field_size = 0;
+       self->line_num = 0;
 
        if (parse_reset(self) < 0) {
                 Py_DECREF(self);