Skip to content

Commit d74a402

Browse files
gh-40038: Quote imaplib command arguments when necessary
Argument quoting was inadvertently disabled when imaplib was ported to Python 3 (bpo-1210 commented out the ``_checkquote()`` call, bpo-9638 then removed it), so since Python 3.0 commands failed for arguments containing protocol-sensitive characters, such as a space in a mailbox name. Quoting is restored and reimplemented per the RFC 3501 grammar, so that arguments that need quoting are escaped and quoted, while flags, sequence sets and list wildcards are left intact. For backward compatibility, an argument already enclosed in double quotes is left unchanged, so code that quotes arguments itself keeps working. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent ddb6539 commit d74a402

4 files changed

Lines changed: 355 additions & 52 deletions

File tree

Doc/library/imaplib.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,9 @@ enclosed with either parentheses or double quotes) each string is quoted.
186186
However, the *password* argument to the ``LOGIN`` command is always quoted. If
187187
you want to avoid having an argument string quoted (eg: the *flags* argument to
188188
``STORE``) then enclose the string in parentheses (eg: ``r'(\Deleted)'``).
189+
In general, pass arguments unquoted and let the module quote them as needed.
190+
An argument that is already enclosed in double quotes is left unchanged,
191+
so that code which quotes arguments itself keeps working.
189192

190193
Most commands return a tuple: ``(type, [data, ...])`` where *type* is usually
191194
``'OK'`` or ``'NO'``, and *data* is either the text from the command response,
@@ -410,7 +413,7 @@ An :class:`IMAP4` instance has the following methods:
410413
.. versionadded:: 3.14
411414

412415

413-
.. method:: IMAP4.list([directory[, pattern]])
416+
.. method:: IMAP4.list(directory='', pattern='*')
414417

415418
List mailbox names in *directory* matching *pattern*. *directory* defaults to
416419
the top-level mail folder, and *pattern* defaults to match anything. Returned
@@ -440,7 +443,7 @@ An :class:`IMAP4` instance has the following methods:
440443
The method no longer ignores silently arbitrary exceptions.
441444

442445

443-
.. method:: IMAP4.lsub(directory='""', pattern='*')
446+
.. method:: IMAP4.lsub(directory='', pattern='*')
444447

445448
List subscribed mailbox names in directory matching pattern. *directory*
446449
defaults to the top level directory and *pattern* defaults to match any mailbox.

0 commit comments

Comments
 (0)