Skip to content

Navigation Menu

Sign in
Sign up

Commit 0290d94

Browse files
committed
repository/IMAP: handle errors reading remotepassfile gracefully
Replace the bare open()/close() pattern with a context manager and wrap I/O and encoding failures in OfflineImapError so the user gets a clear, actionable message instead of a raw Python exception. Before this change, a missing, unreadable, or non-UTF-8 password file would surface as an unhandled FileNotFoundError, PermissionError or UnicodeDecodeError with no indication of which repository was affected. The new error message includes the expanded file path and the repository name: Unable to read remotepassfile '/path/to/pass' for repository 'Name': ... Closes: #248
1 parent b03c565 commit 0290d94

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

‎offlineimap/repository/IMAP.py‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -620,10 +620,16 @@ def getpassword(self, ignore_keyring=False):
620620
# 3. Read password from file specified in Repository 'remotepassfile'.
621621
passfile = self.getconf('remotepassfile', None)
622622
if passfile is not None:
623-
file_desc = open(os.path.expanduser(passfile), 'r',
624-
encoding='utf-8')
625-
password = file_desc.readline().strip()
626-
file_desc.close()
623+
passfile = os.path.expanduser(passfile)
624+
try:
625+
with open(passfile, 'r', encoding='utf-8') as file_desc:
626+
password = file_desc.readline().strip()
627+
except (IOError, OSError, UnicodeError) as e:
628+
raise OfflineImapError(
629+
"Unable to read remotepassfile '{}' for repository '{}': {}"
630+
.format(passfile, self.name, e),
631+
OfflineImapError.ERROR.FOLDER,
632+
)
627633

628634
# We need a str password
629635
if isinstance(password, bytes):

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /