Use open() instead of file()

This commit is contained in:
Andrew M. Kuchling 2006-05-02 21:44:33 +00:00
parent b2045837b6
commit 214db63df8
2 changed files with 23 additions and 23 deletions

View File

@ -294,7 +294,7 @@ class Maildir(Mailbox):
def get_message(self, key): def get_message(self, key):
"""Return a Message representation or raise a KeyError.""" """Return a Message representation or raise a KeyError."""
subpath = self._lookup(key) subpath = self._lookup(key)
f = file(os.path.join(self._path, subpath), 'r') f = open(os.path.join(self._path, subpath), 'r')
try: try:
msg = MaildirMessage(f) msg = MaildirMessage(f)
finally: finally:
@ -308,7 +308,7 @@ class Maildir(Mailbox):
def get_string(self, key): def get_string(self, key):
"""Return a string representation or raise a KeyError.""" """Return a string representation or raise a KeyError."""
f = file(os.path.join(self._path, self._lookup(key)), 'r') f = open(os.path.join(self._path, self._lookup(key)), 'r')
try: try:
return f.read() return f.read()
finally: finally:
@ -316,7 +316,7 @@ class Maildir(Mailbox):
def get_file(self, key): def get_file(self, key):
"""Return a file-like representation or raise a KeyError.""" """Return a file-like representation or raise a KeyError."""
f = file(os.path.join(self._path, self._lookup(key)), 'rb') f = open(os.path.join(self._path, self._lookup(key)), 'rb')
return _ProxyFile(f) return _ProxyFile(f)
def iterkeys(self): def iterkeys(self):
@ -422,7 +422,7 @@ class Maildir(Mailbox):
except OSError, e: except OSError, e:
if e.errno == errno.ENOENT: if e.errno == errno.ENOENT:
Maildir._count += 1 Maildir._count += 1
return file(path, 'wb+') return open(path, 'wb+')
else: else:
raise raise
else: else:
@ -471,15 +471,15 @@ class _singlefileMailbox(Mailbox):
"""Initialize a single-file mailbox.""" """Initialize a single-file mailbox."""
Mailbox.__init__(self, path, factory, create) Mailbox.__init__(self, path, factory, create)
try: try:
f = file(self._path, 'rb+') f = open(self._path, 'rb+')
except IOError, e: except IOError, e:
if e.errno == errno.ENOENT: if e.errno == errno.ENOENT:
if create: if create:
f = file(self._path, 'wb+') f = open(self._path, 'wb+')
else: else:
raise NoSuchMailboxError(self._path) raise NoSuchMailboxError(self._path)
elif e.errno == errno.EACCES: elif e.errno == errno.EACCES:
f = file(self._path, 'rb') f = open(self._path, 'rb')
else: else:
raise raise
self._file = f self._file = f
@ -572,7 +572,7 @@ class _singlefileMailbox(Mailbox):
os.rename(new_file.name, self._path) os.rename(new_file.name, self._path)
else: else:
raise raise
self._file = file(self._path, 'rb+') self._file = open(self._path, 'rb+')
self._toc = new_toc self._toc = new_toc
self._pending = False self._pending = False
if self._locked: if self._locked:
@ -792,7 +792,7 @@ class MH(Mailbox):
"""Remove the keyed message; raise KeyError if it doesn't exist.""" """Remove the keyed message; raise KeyError if it doesn't exist."""
path = os.path.join(self._path, str(key)) path = os.path.join(self._path, str(key))
try: try:
f = file(path, 'rb+') f = open(path, 'rb+')
except IOError, e: except IOError, e:
if e.errno == errno.ENOENT: if e.errno == errno.ENOENT:
raise KeyError('No message with key: %s' % key) raise KeyError('No message with key: %s' % key)
@ -814,7 +814,7 @@ class MH(Mailbox):
"""Replace the keyed message; raise KeyError if it doesn't exist.""" """Replace the keyed message; raise KeyError if it doesn't exist."""
path = os.path.join(self._path, str(key)) path = os.path.join(self._path, str(key))
try: try:
f = file(path, 'rb+') f = open(path, 'rb+')
except IOError, e: except IOError, e:
if e.errno == errno.ENOENT: if e.errno == errno.ENOENT:
raise KeyError('No message with key: %s' % key) raise KeyError('No message with key: %s' % key)
@ -838,9 +838,9 @@ class MH(Mailbox):
"""Return a Message representation or raise a KeyError.""" """Return a Message representation or raise a KeyError."""
try: try:
if self._locked: if self._locked:
f = file(os.path.join(self._path, str(key)), 'r+') f = open(os.path.join(self._path, str(key)), 'r+')
else: else:
f = file(os.path.join(self._path, str(key)), 'r') f = open(os.path.join(self._path, str(key)), 'r')
except IOError, e: except IOError, e:
if e.errno == errno.ENOENT: if e.errno == errno.ENOENT:
raise KeyError('No message with key: %s' % key) raise KeyError('No message with key: %s' % key)
@ -865,9 +865,9 @@ class MH(Mailbox):
"""Return a string representation or raise a KeyError.""" """Return a string representation or raise a KeyError."""
try: try:
if self._locked: if self._locked:
f = file(os.path.join(self._path, str(key)), 'r+') f = open(os.path.join(self._path, str(key)), 'r+')
else: else:
f = file(os.path.join(self._path, str(key)), 'r') f = open(os.path.join(self._path, str(key)), 'r')
except IOError, e: except IOError, e:
if e.errno == errno.ENOENT: if e.errno == errno.ENOENT:
raise KeyError('No message with key: %s' % key) raise KeyError('No message with key: %s' % key)
@ -887,7 +887,7 @@ class MH(Mailbox):
def get_file(self, key): def get_file(self, key):
"""Return a file-like representation or raise a KeyError.""" """Return a file-like representation or raise a KeyError."""
try: try:
f = file(os.path.join(self._path, str(key)), 'rb') f = open(os.path.join(self._path, str(key)), 'rb')
except IOError, e: except IOError, e:
if e.errno == errno.ENOENT: if e.errno == errno.ENOENT:
raise KeyError('No message with key: %s' % key) raise KeyError('No message with key: %s' % key)
@ -911,7 +911,7 @@ class MH(Mailbox):
def lock(self): def lock(self):
"""Lock the mailbox.""" """Lock the mailbox."""
if not self._locked: if not self._locked:
self._file = file(os.path.join(self._path, '.mh_sequences'), 'rb+') self._file = open(os.path.join(self._path, '.mh_sequences'), 'rb+')
_lock_file(self._file) _lock_file(self._file)
self._locked = True self._locked = True
@ -963,7 +963,7 @@ class MH(Mailbox):
def get_sequences(self): def get_sequences(self):
"""Return a name-to-key-list dictionary to define each sequence.""" """Return a name-to-key-list dictionary to define each sequence."""
results = {} results = {}
f = file(os.path.join(self._path, '.mh_sequences'), 'r') f = open(os.path.join(self._path, '.mh_sequences'), 'r')
try: try:
all_keys = set(self.keys()) all_keys = set(self.keys())
for line in f: for line in f:
@ -989,7 +989,7 @@ class MH(Mailbox):
def set_sequences(self, sequences): def set_sequences(self, sequences):
"""Set sequences using the given name-to-key-list dictionary.""" """Set sequences using the given name-to-key-list dictionary."""
f = file(os.path.join(self._path, '.mh_sequences'), 'r+') f = open(os.path.join(self._path, '.mh_sequences'), 'r+')
try: try:
os.close(os.open(f.name, os.O_WRONLY | os.O_TRUNC)) os.close(os.open(f.name, os.O_WRONLY | os.O_TRUNC))
for name, keys in sequences.iteritems(): for name, keys in sequences.iteritems():
@ -1024,7 +1024,7 @@ class MH(Mailbox):
for key in self.iterkeys(): for key in self.iterkeys():
if key - 1 != prev: if key - 1 != prev:
changes.append((key, prev + 1)) changes.append((key, prev + 1))
f = file(os.path.join(self._path, str(key)), 'r+') f = open(os.path.join(self._path, str(key)), 'r+')
try: try:
if self._locked: if self._locked:
_lock_file(f) _lock_file(f)
@ -1864,7 +1864,7 @@ def _create_carefully(path):
"""Create a file if it doesn't exist and open for reading and writing.""" """Create a file if it doesn't exist and open for reading and writing."""
fd = os.open(path, os.O_CREAT | os.O_EXCL | os.O_RDWR) fd = os.open(path, os.O_CREAT | os.O_EXCL | os.O_RDWR)
try: try:
return file(path, 'rb+') return open(path, 'rb+')
finally: finally:
os.close(fd) os.close(fd)

View File

@ -717,7 +717,7 @@ class _TestMboxMMDF(TestMailbox):
self._box._file.seek(0) self._box._file.seek(0)
contents = self._box._file.read() contents = self._box._file.read()
self._box.close() self._box.close()
self.assert_(contents == file(self._path, 'rb').read()) self.assert_(contents == open(self._path, 'rb').read())
self._box = self._factory(self._path) self._box = self._factory(self._path)
@ -1473,7 +1473,7 @@ class TestProxyFile(TestProxyFileBase):
def setUp(self): def setUp(self):
self._path = test_support.TESTFN self._path = test_support.TESTFN
self._file = file(self._path, 'wb+') self._file = open(self._path, 'wb+')
def tearDown(self): def tearDown(self):
self._file.close() self._file.close()
@ -1522,7 +1522,7 @@ class TestPartialFile(TestProxyFileBase):
def setUp(self): def setUp(self):
self._path = test_support.TESTFN self._path = test_support.TESTFN
self._file = file(self._path, 'wb+') self._file = open(self._path, 'wb+')
def tearDown(self): def tearDown(self):
self._file.close() self._file.close()