Skip to content

imaplib misquotes *params* for Shift_JIS, Big5, GBK and ISO-2022-JP #163000

Backlog

Description

@fedonman

Bug description:

With a search charset such as Shift_JIS, Big5, GBK or ISO-2022-JP, IMAP4.search(), sort(), thread() and uid() escape a params value before encoding it to the charset. A \ or " byte inside an encoded character is then left unescaped, so the quoted string ends early and swallows the next criterion. The docs say the values are "substituted, and quoted as required".

A fake server that prints the quoted strings it parses from the command:

import imaplib, re
class Server(imaplib.IMAP4):
    def open(self, *args): self.replies = [b'* PREAUTH\r\n']
    def readline(self): return self.replies.pop(0)
    def send(self, data):
        self.replies += [b'* CAPABILITY IMAP4rev1\r\n', data.split()[0] + b' OK done\r\n']
        if b'SEARCH' in data:
            print([re.sub(rb'\\(.)', rb'\1', s).decode('shift_jis', 'replace')
                   for s in re.findall(rb'"((?:[^"\\]|\\.)*)"', data)])
imap = Server()
imap.select()
imap.search('shift_jis', 'SUBJECT ? FROM ?', params=['表', 'John Smith'])
$ python repro.py
['�" FROM ']

Expected: ['表', 'John Smith'], which is what UTF-8 gives. params is new in 3.16 (gh-153521).

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs

Activity

  1. added
    stdlib
    type-bug
    on Dec 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibtopic-emailtype-bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions