Skip to content

Commit 03d0909

Browse files
[3.14] gh-40038: Quote imaplib command arguments when necessary (GH-152703) (GH-153069)
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 07efb08 commit 03d0909

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
@@ -194,6 +194,9 @@ enclosed with either parentheses or double quotes) each string is quoted.
194194
However, the *password* argument to the ``LOGIN`` command is always quoted. If
195195
you want to avoid having an argument string quoted (eg: the *flags* argument to
196196
``STORE``) then enclose the string in parentheses (eg: ``r'(\Deleted)'``).
197+
In general, pass arguments unquoted and let the module quote them as needed.
198+
An argument that is already enclosed in double quotes is left unchanged,
199+
so that code which quotes arguments itself keeps working.
197200

198201
Most commands return a tuple: ``(type, [data, ...])`` where *type* is usually
199202
``'OK'`` or ``'NO'``, and *data* is either the text from the command response,
@@ -402,7 +405,7 @@ An :class:`IMAP4` instance has the following methods:
402405
.. versionadded:: 3.14
403406

404407

405-
.. method:: IMAP4.list([directory[, pattern]])
408+
.. method:: IMAP4.list(directory='', pattern='*')
406409

407410
List mailbox names in *directory* matching *pattern*. *directory* defaults to
408411
the top-level mail folder, and *pattern* defaults to match anything. Returned
@@ -432,7 +435,7 @@ An :class:`IMAP4` instance has the following methods:
432435
The method no longer ignores silently arbitrary exceptions.
433436

434437

435-
.. method:: IMAP4.lsub(directory='""', pattern='*')
438+
.. method:: IMAP4.lsub(directory='', pattern='*')
436439

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

0 commit comments

Comments
 (0)