Skip to content

Commit 2c99ec4

Browse files
[3.15] gh-40038: Quote imaplib command arguments when necessary (GH-152703) (GH-153066)
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. (cherry picked from commit 600e864) Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 7eb476d commit 2c99ec4

4 files changed

Lines changed: 404 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,
@@ -394,7 +397,7 @@ An :class:`IMAP4` instance has the following methods:
394397
.. versionadded:: 3.14
395398

396399

397-
.. method:: IMAP4.list([directory[, pattern]])
400+
.. method:: IMAP4.list(directory='', pattern='*')
398401

399402
List mailbox names in *directory* matching *pattern*. *directory* defaults to
400403
the top-level mail folder, and *pattern* defaults to match anything. Returned
@@ -424,7 +427,7 @@ An :class:`IMAP4` instance has the following methods:
424427
The method no longer ignores silently arbitrary exceptions.
425428

426429

427-
.. method:: IMAP4.lsub(directory='""', pattern='*')
430+
.. method:: IMAP4.lsub(directory='', pattern='*')
428431

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

0 commit comments

Comments
 (0)