Skip to content

Commit 939a377

Browse files
[3.13] gh-40038: Quote imaplib command arguments when necessary (GH-152703)
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> (cherry picked from commit 600e864)
1 parent c099aa1 commit 939a377

4 files changed

Lines changed: 404 additions & 53 deletions

File tree

Doc/library/imaplib.rst

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

197200
Each command returns a tuple: ``(type, [data, ...])`` where *type* is usually
198201
``'OK'`` or ``'NO'``, and *data* is either the text from the command response,
@@ -314,7 +317,7 @@ An :class:`IMAP4` instance has the following methods:
314317
of the IMAP4 QUOTA extension defined in rfc2087.
315318

316319

317-
.. method:: IMAP4.list([directory[, pattern]])
320+
.. method:: IMAP4.list(directory='', pattern='*')
318321

319322
List mailbox names in *directory* matching *pattern*. *directory* defaults to
320323
the top-level mail folder, and *pattern* defaults to match anything. Returned
@@ -344,7 +347,7 @@ An :class:`IMAP4` instance has the following methods:
344347
The method no longer ignores silently arbitrary exceptions.
345348

346349

347-
.. method:: IMAP4.lsub(directory='""', pattern='*')
350+
.. method:: IMAP4.lsub(directory='', pattern='*')
348351

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

0 commit comments

Comments
 (0)