add __enter__ and __exit__

This commit is contained in:
David Rose 2010-03-03 18:32:46 +00:00
parent 6785a61f3c
commit 991d5179a3

View File

@ -179,7 +179,8 @@ class file:
if not self.__reader: if not self.__reader:
if not self.__writer: if not self.__writer:
# The stream is not even open at all. # The stream is not even open at all.
raise ValueError message = 'I/O operation on closed file'
raise ValueError, message
# The stream is open only in write mode. # The stream is open only in write mode.
message = 'Attempt to read from write-only stream' message = 'Attempt to read from write-only stream'
raise IOError, message raise IOError, message
@ -198,7 +199,8 @@ class file:
if not self.__reader: if not self.__reader:
if not self.__writer: if not self.__writer:
# The stream is not even open at all. # The stream is not even open at all.
raise ValueError message = 'I/O operation on closed file'
raise ValueError, message
# The stream is open only in write mode. # The stream is open only in write mode.
message = 'Attempt to read from write-only stream' message = 'Attempt to read from write-only stream'
raise IOError, message raise IOError, message
@ -229,7 +231,8 @@ class file:
else: else:
if self.__reader: if self.__reader:
return self.__stream.tellg() return self.__stream.tellg()
raise ValueError message = 'I/O operation on closed file'
raise ValueError, message
def truncate(self): def truncate(self):
""" Sorry, this isn't supported by Panda's low-level I/O, """ Sorry, this isn't supported by Panda's low-level I/O,
@ -240,7 +243,8 @@ class file:
if not self.__writer: if not self.__writer:
if not self.__reader: if not self.__reader:
# The stream is not even open at all. # The stream is not even open at all.
raise ValueError message = 'I/O operation on closed file'
raise ValueError, message
# The stream is open only in read mode. # The stream is open only in read mode.
message = 'Attempt to write to read-only stream' message = 'Attempt to write to read-only stream'
raise IOError, message raise IOError, message
@ -251,7 +255,8 @@ class file:
if not self.__writer: if not self.__writer:
if not self.__reader: if not self.__reader:
# The stream is not even open at all. # The stream is not even open at all.
raise ValueError message = 'I/O operation on closed file'
raise ValueError, message
# The stream is open only in read mode. # The stream is open only in read mode.
message = 'Attempt to write to read-only stream' message = 'Attempt to write to read-only stream'
raise IOError, message raise IOError, message
@ -259,6 +264,12 @@ class file:
self.__writer.appendData(line) self.__writer.appendData(line)
self.__lastWrite = True self.__lastWrite = True
def __enter__(self):
return self
def __exit__(self, t, v, tb):
self.close()
open = file open = file
def listdir(path): def listdir(path):