mirror of
https://github.com/python/cpython.git
synced 2025-07-31 07:04:42 +00:00
Issue #2432: give DictReader the dialect and line_num attributes
advertised in the docs.
This commit is contained in:
parent
331243270d
commit
77354cf5ef
3 changed files with 18 additions and 10 deletions
|
@ -75,6 +75,8 @@ class DictReader:
|
||||||
self.restkey = restkey # key to catch long rows
|
self.restkey = restkey # key to catch long rows
|
||||||
self.restval = restval # default value for short rows
|
self.restval = restval # default value for short rows
|
||||||
self.reader = reader(f, dialect, *args, **kwds)
|
self.reader = reader(f, dialect, *args, **kwds)
|
||||||
|
self.dialect = dialect
|
||||||
|
self.line_num = 0
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
return self
|
return self
|
||||||
|
@ -84,6 +86,7 @@ class DictReader:
|
||||||
if self.fieldnames is None:
|
if self.fieldnames is None:
|
||||||
self.fieldnames = row
|
self.fieldnames = row
|
||||||
row = self.reader.next()
|
row = self.reader.next()
|
||||||
|
self.line_num = self.reader.line_num
|
||||||
|
|
||||||
# unlike the basic reader, we prefer not to return blanks,
|
# unlike the basic reader, we prefer not to return blanks,
|
||||||
# because we will typically wind up with a dict full of None
|
# because we will typically wind up with a dict full of None
|
||||||
|
|
|
@ -269,16 +269,18 @@ class Test_Csv(unittest.TestCase):
|
||||||
csv.field_size_limit(limit)
|
csv.field_size_limit(limit)
|
||||||
|
|
||||||
def test_read_linenum(self):
|
def test_read_linenum(self):
|
||||||
r = csv.reader(['line,1', 'line,2', 'line,3'])
|
for r in (csv.reader(['line,1', 'line,2', 'line,3']),
|
||||||
self.assertEqual(r.line_num, 0)
|
csv.DictReader(['line,1', 'line,2', 'line,3'],
|
||||||
r.next()
|
fieldnames=['a', 'b', 'c'])):
|
||||||
self.assertEqual(r.line_num, 1)
|
self.assertEqual(r.line_num, 0)
|
||||||
r.next()
|
r.next()
|
||||||
self.assertEqual(r.line_num, 2)
|
self.assertEqual(r.line_num, 1)
|
||||||
r.next()
|
r.next()
|
||||||
self.assertEqual(r.line_num, 3)
|
self.assertEqual(r.line_num, 2)
|
||||||
self.assertRaises(StopIteration, r.next)
|
r.next()
|
||||||
self.assertEqual(r.line_num, 3)
|
self.assertEqual(r.line_num, 3)
|
||||||
|
self.assertRaises(StopIteration, r.next)
|
||||||
|
self.assertEqual(r.line_num, 3)
|
||||||
|
|
||||||
class TestDialectRegistry(unittest.TestCase):
|
class TestDialectRegistry(unittest.TestCase):
|
||||||
def test_registry_badargs(self):
|
def test_registry_badargs(self):
|
||||||
|
|
|
@ -51,6 +51,9 @@ Core and builtins
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- Issue #2432: give DictReader the dialect and line_num attributes
|
||||||
|
advertised in the docs.
|
||||||
|
|
||||||
- Issue #2136: urllib2's auth handler now allows single-quoted realms in the
|
- Issue #2136: urllib2's auth handler now allows single-quoted realms in the
|
||||||
WWW-Authenticate header.
|
WWW-Authenticate header.
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue