@@ -186,7 +186,7 @@ def _read(filepath_or_buffer, kwds):
186
186
kwds ['parse_dates' ] = True
187
187
188
188
# Extract some of the arguments (pass chunksize on).
189
- iterator = kwds .pop ('iterator' , False )
189
+ iterator = kwds .get ('iterator' , False )
190
190
nrows = kwds .pop ('nrows' , None )
191
191
chunksize = kwds .get ('chunksize' , None )
192
192
@@ -569,8 +569,11 @@ def _clean_options(self, options, engine):
569
569
570
570
def __iter__ (self ):
571
571
try :
572
- while True :
573
- yield self .read (self .chunksize )
572
+ if self .chunksize :
573
+ while True :
574
+ yield self .read (self .chunksize )
575
+ else :
576
+ yield self .read ()
574
577
except StopIteration :
575
578
pass
576
579
@@ -1594,47 +1597,58 @@ def _rows_to_cols(self, content):
1594
1597
def _get_lines (self , rows = None ):
1595
1598
source = self .data
1596
1599
lines = self .buf
1600
+ new_rows = None
1597
1601
1598
1602
# already fetched some number
1599
1603
if rows is not None :
1600
- rows -= len (self .buf )
1601
1604
1602
- if isinstance (source , list ):
1603
- if self .pos > len (source ):
1604
- raise StopIteration
1605
- if rows is None :
1606
- lines .extend (source [self .pos :])
1607
- self .pos = len (source )
1605
+ # we already have the lines in the buffer
1606
+ if len (self .buf ) >= rows :
1607
+ new_rows , self .buf = self .buf [:rows ], self .buf [rows :]
1608
+
1609
+ # need some lines
1608
1610
else :
1609
- lines . extend ( source [ self .pos : self . pos + rows ] )
1610
- self . pos += rows
1611
- else :
1612
- new_rows = []
1613
- try :
1614
- if rows is not None :
1615
- for _ in xrange ( rows ) :
1616
- new_rows . append ( next ( source ) )
1617
- lines . extend ( new_rows )
1611
+ rows -= len ( self .buf )
1612
+
1613
+ if new_rows is None :
1614
+ if isinstance ( source , list ):
1615
+ if self . pos > len ( source ) :
1616
+ raise StopIteration
1617
+ if rows is None :
1618
+ lines . extend ( source [ self . pos :] )
1619
+ self . pos = len ( source )
1618
1620
else :
1619
- rows = 0
1620
- while True :
1621
- try :
1621
+ lines .extend (source [self .pos :self .pos + rows ])
1622
+ self .pos += rows
1623
+ else :
1624
+ new_rows = []
1625
+ try :
1626
+ if rows is not None :
1627
+ for _ in xrange (rows ):
1622
1628
new_rows .append (next (source ))
1623
- rows += 1
1624
- except csv .Error , inst :
1625
- if 'newline inside string' in str (inst ):
1626
- row_num = str (self .pos + rows )
1627
- msg = ('EOF inside string starting with line '
1628
- + row_num )
1629
- raise Exception (msg )
1630
- raise
1631
- except StopIteration :
1632
- lines .extend (new_rows )
1633
- if len (lines ) == 0 :
1634
- raise
1635
- self .pos += len (new_rows )
1629
+ lines .extend (new_rows )
1630
+ else :
1631
+ rows = 0
1632
+ while True :
1633
+ try :
1634
+ new_rows .append (next (source ))
1635
+ rows += 1
1636
+ except csv .Error , inst :
1637
+ if 'newline inside string' in str (inst ):
1638
+ row_num = str (self .pos + rows )
1639
+ msg = ('EOF inside string starting with line '
1640
+ + row_num )
1641
+ raise Exception (msg )
1642
+ raise
1643
+ except StopIteration :
1644
+ lines .extend (new_rows )
1645
+ if len (lines ) == 0 :
1646
+ raise
1647
+ self .pos += len (new_rows )
1636
1648
1637
- self .buf = []
1649
+ self .buf = []
1650
+ else :
1651
+ lines = new_rows
1638
1652
1639
1653
if self .skip_footer :
1640
1654
lines = lines [:- self .skip_footer ]
0 commit comments