Skip to content

Commit 455004a

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 c393ab6 commit 455004a

4 files changed

Lines changed: 878 additions & 69 deletions

File tree

Doc/library/imaplib.rst

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

183186
Most commands return a tuple: ``(type, [data, ...])`` where *type* is usually
184187
``'OK'`` or ``'NO'``, and *data* is either the text from the command response,
@@ -392,7 +395,7 @@ An :class:`IMAP4` instance has the following methods:
392395
.. versionadded:: 3.14
393396

394397

395-
.. method:: IMAP4.list([directory[, pattern]])
398+
.. method:: IMAP4.list(directory='', pattern='*')
396399

397400
List mailbox names in *directory* matching *pattern*. *directory* defaults to
398401
the top-level mail folder, and *pattern* defaults to match anything. Returned
@@ -422,7 +425,7 @@ An :class:`IMAP4` instance has the following methods:
422425
The method no longer ignores silently arbitrary exceptions.
423426

424427

425-
.. method:: IMAP4.lsub(directory='""', pattern='*')
428+
.. method:: IMAP4.lsub(directory='', pattern='*')
426429

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

0 commit comments

Comments
 (0)