Bug 1168607 - Add a read() method to File; r=glandium

Passing raw file handles around is a bit dangerous because it could lead
to leaking file descriptors. Add a read() method that handles the simple
case of obtaining the full contents of a File instance.

This should ideally be a method on BaseFile. But this would require
extra work and isn't needed. So we've deferred it until bug 1170329.
This commit is contained in:
Gregory Szorc 2015-06-02 09:37:45 -07:00
parent b4a637bb54
commit 1c6e12f560

View File

@ -189,6 +189,9 @@ class BaseFile(object):
assert self.path is not None
return open(self.path, 'rb')
def read(self):
raise NotImplementedError('BaseFile.read() not implemented. Bug 1170329.')
@property
def mode(self):
'''
@ -227,6 +230,12 @@ class File(BaseFile):
# - leave away sticky bit, setuid, setgid
return ret
def read(self):
'''Return the contents of the file.'''
with open(self.path, 'rb') as fh:
return fh.read()
class ExecutableFile(File):
'''
File class for executable and library files on OS/2, OS/X and ELF systems.