@@ -837,27 +837,18 @@ def test_write_past_end_of_file(self):
837837 self .assertEquals (f .read (), b ("\x00 " )* 25 + b ("EOF" ))
838838
839839 def test_with_statement (self ):
840- # This is a little tricky since 'with' is actually new syntax.
841- # We use eval() to make this method safe for old python versions.
842- import sys
843- if sys .version_info [0 ] >= 2 and sys .version_info [1 ] >= 5 :
844- # A successful 'with' statement
845- contents = "testing the with statement"
846- code = "from __future__ import with_statement\n "
847- code += "with self.fs.open('f.txt','wb-') as testfile:\n "
848- code += " testfile.write(contents)\n "
849- code += "self.assertEquals(self.fs.getcontents('f.txt', 'rb'),contents)"
850- code = compile (code , "<string>" , 'exec' )
851- eval (code )
852- # A 'with' statement raising an error
853- contents = "testing the with statement"
854- code = "from __future__ import with_statement\n "
855- code += "with self.fs.open('f.txt','wb-') as testfile:\n "
856- code += " testfile.write(contents)\n "
857- code += " raise ValueError\n "
858- code = compile (code , "<string>" , 'exec' )
859- self .assertRaises (ValueError , eval , code , globals (), locals ())
860- self .assertEquals (self .fs .getcontents ('f.txt' , 'rb' ), contents )
840+ contents = b"testing the with statement"
841+ # A successful 'with' statement
842+ with self .fs .open ('f.txt' ,'wb-' ) as testfile :
843+ testfile .write (contents )
844+ self .assertEquals (self .fs .getcontents ('f.txt' , 'rb' ), contents )
845+ # A 'with' statement raising an error
846+ def with_error ():
847+ with self .fs .open ('g.txt' ,'wb-' ) as testfile :
848+ testfile .write (contents )
849+ raise ValueError
850+ self .assertRaises (ValueError , with_error )
851+ self .assertEquals (self .fs .getcontents ('g.txt' , 'rb' ), contents )
861852
862853 def test_pickling (self ):
863854 if self .fs .getmeta ('pickle_contents' , True ):
0 commit comments