@@ -1254,6 +1254,7 @@ def test_regex_separator(self):
12541254 columns = ['a' , 'b' , 'c' ])
12551255 tm .assert_frame_equal (result , expected )
12561256
1257+ @tm .capture_stdout
12571258 def test_verbose_import (self ):
12581259 text = """a,b,c,d
12591260one,1,2,3
@@ -1265,22 +1266,18 @@ def test_verbose_import(self):
12651266one,1,2,3
12661267two,1,2,3"""
12671268
1268- buf = StringIO ()
1269- sys .stdout = buf
1269+ # Engines are verbose in different ways.
1270+ self .read_csv (StringIO (text ), verbose = True )
1271+ output = sys .stdout .getvalue ()
12701272
1271- try : # engines are verbose in different ways
1272- self .read_csv (StringIO (text ), verbose = True )
1273- if self .engine == 'c' :
1274- self .assertIn ('Tokenization took:' , buf .getvalue ())
1275- self .assertIn ('Parser memory cleanup took:' , buf .getvalue ())
1276- else : # Python engine
1277- self .assertEqual (buf .getvalue (),
1278- 'Filled 3 NA values in column a\n ' )
1279- finally :
1280- sys .stdout = sys .__stdout__
1273+ if self .engine == 'c' :
1274+ assert 'Tokenization took:' in output
1275+ assert 'Parser memory cleanup took:' in output
1276+ else : # Python engine
1277+ assert output == 'Filled 3 NA values in column a\n '
12811278
1282- buf = StringIO ()
1283- sys .stdout = buf
1279+ # Reset the stdout buffer.
1280+ sys .stdout = StringIO ()
12841281
12851282 text = """a,b,c,d
12861283one,1,2,3
@@ -1292,16 +1289,16 @@ def test_verbose_import(self):
12921289seven,1,2,3
12931290eight,1,2,3"""
12941291
1295- try : # engines are verbose in different ways
1296- self . read_csv ( StringIO ( text ), verbose = True , index_col = 0 )
1297- if self . engine == 'c' :
1298- self . assertIn ( 'Tokenization took:' , buf . getvalue ())
1299- self .assertIn ( 'Parser memory cleanup took:' , buf . getvalue ())
1300- else : # Python engine
1301- self . assertEqual ( buf . getvalue (),
1302- 'Filled 1 NA values in column a \n ' )
1303- finally :
1304- sys . stdout = sys . __stdout__
1292+ self . read_csv ( StringIO ( text ), verbose = True , index_col = 0 )
1293+ output = sys . stdout . getvalue ( )
1294+
1295+ # Engines are verbose in different ways.
1296+ if self .engine == 'c' :
1297+ self . assertIn ( 'Tokenization took:' , output )
1298+ self . assertIn ( 'Parser memory cleanup took:' , output )
1299+ else : # Python engine
1300+ exp = 'Filled 1 NA values in column a \n '
1301+ self . assertEqual ( output , exp )
13051302
13061303 def test_iteration_open_handle (self ):
13071304 if PY3 :
@@ -1696,6 +1693,7 @@ class InvalidBuffer(object):
16961693 with tm .assertRaisesRegexp (ValueError , msg ):
16971694 self .read_csv (mock .Mock ())
16981695
1696+ @tm .capture_stderr
16991697 def test_skip_bad_lines (self ):
17001698 # see gh-15925
17011699 data = 'a\n 1\n 1,2,3\n 4\n 5,6,7'
@@ -1706,30 +1704,24 @@ def test_skip_bad_lines(self):
17061704 with tm .assertRaises (ParserError ):
17071705 self .read_csv (StringIO (data ), error_bad_lines = True )
17081706
1709- stderr = sys .stderr
17101707 expected = DataFrame ({'a' : [1 , 4 ]})
17111708
1712- sys .stderr = StringIO ()
1713- try :
1714- out = self .read_csv (StringIO (data ),
1715- error_bad_lines = False ,
1716- warn_bad_lines = False )
1717- tm .assert_frame_equal (out , expected )
1709+ out = self .read_csv (StringIO (data ),
1710+ error_bad_lines = False ,
1711+ warn_bad_lines = False )
1712+ tm .assert_frame_equal (out , expected )
17181713
1719- val = sys .stderr .getvalue ()
1720- self .assertEqual (val , '' )
1721- finally :
1722- sys .stderr = stderr
1714+ val = sys .stderr .getvalue ()
1715+ assert val == ''
17231716
1717+ # Reset the stderr buffer.
17241718 sys .stderr = StringIO ()
1725- try :
1726- out = self .read_csv (StringIO (data ),
1727- error_bad_lines = False ,
1728- warn_bad_lines = True )
1729- tm .assert_frame_equal (out , expected )
17301719
1731- val = sys .stderr .getvalue ()
1732- self .assertTrue ('Skipping line 3' in val )
1733- self .assertTrue ('Skipping line 5' in val )
1734- finally :
1735- sys .stderr = stderr
1720+ out = self .read_csv (StringIO (data ),
1721+ error_bad_lines = False ,
1722+ warn_bad_lines = True )
1723+ tm .assert_frame_equal (out , expected )
1724+
1725+ val = sys .stderr .getvalue ()
1726+ assert 'Skipping line 3' in val
1727+ assert 'Skipping line 5' in val
0 commit comments