Skip to content

Commit

Permalink
Don't assume capabilities have been cached when sending literals
Browse files Browse the repository at this point in the history
_send_literal was assuming that _cached_capabilites was populated when
checking for LITERAL+ but this isn't guaranteed. has_capability is now
used to check for LITERAL+. This will populate the capabilities cache if
it hasn't been already.

Fixes #560
  • Loading branch information
Menno Finlay-Smits committed Dec 1, 2023
1 parent 25e665d commit d26aaef
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions imapclient/imapclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -1675,6 +1675,10 @@ def _raw_command(self, command, args, uid=True):
"""
command = command.upper()

# Check for LITERAL+ now because if capabilities haven't been cached
# yet, we can't call CAPABILITY while sending another command.
has_literal_plus = self.has_capability("LITERAL+")

if isinstance(args, tuple):
args = list(args)
if not isinstance(args, list):
Expand Down Expand Up @@ -1702,7 +1706,7 @@ def _raw_command(self, command, args, uid=True):
# Now send the (unquoted) literal
if isinstance(item, _quoted):
item = item.original
self._send_literal(tag, item)
self._send_literal(tag, item, has_literal_plus)
if not is_last:
self._imap.send(b" ")
else:
Expand All @@ -1717,9 +1721,9 @@ def _raw_command(self, command, args, uid=True):

return self._imap._command_complete(to_unicode(command), tag)

def _send_literal(self, tag, item):
def _send_literal(self, tag, item, has_literal_plus):
"""Send a single literal for the command with *tag*."""
if b"LITERAL+" in self._cached_capabilities:
if has_literal_plus:
out = b" {" + str(len(item)).encode("ascii") + b"+}\r\n" + item
logger.debug("> %s", debug_trunc(out, 64))
self._imap.send(out)
Expand Down

0 comments on commit d26aaef

Please sign in to comment.