This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: [EASY] imaplib noop Debug: bytes vs Unicode bug in debug mode
Type: behavior Stage: resolved
Components: email Versions: Python 3.9, Python 3.8, Python 3.7
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: CuriousLearner, Demur Rumed, Stephen.Evans, ankostis, barry, gigaplastik, maciej.szulik, maxking, r.david.murray, rotuna, vstinner
Priority: normal Keywords: easy, patch

Created on 2016-03-11 23:23 by Stephen.Evans, last changed 2022-04-11 14:58 by admin. This issue is now closed.

File name Uploaded Description Edit Stephen.Evans, 2016-03-11 23:23 Python source capable of imaplib.noop() crash.
imaplib.patch Demur Rumed, 2016-03-27 20:12 ignore; has syntax error review
imaplib2.patch Demur Rumed, 2016-03-28 02:23 Includes refactoring to use a generator review
Pull Requests
URL Status Linked Edit
PR 1503 closed ankostis, 2017-05-08 22:39
PR 15206 merged CuriousLearner, 2019-08-11 11:08
PR 19693 closed rotuna, 2020-04-23 22:40
Messages (11)
msg261615 - (view) Author: Stephen Evans (Stephen.Evans) Date: 2016-03-11 23:23
With the imaplib.Debug=3 (or greater) imaplib.noop() will crash if there are any untagged responses present.

The _dump_ur() function has not been converted to expect bytes in the lambda variable x[1]. An abbreviate example of the 'dict' parameter would be:

{'READ-WRITE': [b''], 'FETCH': [b'1 (FLAGS (\\Recent NonJunk))']}

This _dump_ur() function needs converting to Python 3.x, say, change the map/lambda line to:
  l = map(lambda x:'%s: %r' % (x[0], x[1]), l)

A sample crash inducing session produced by the attached file (using another email client to produce untagged responses for NOOP):

  20:01.23 imaplib version 2.58
  20:01.23 new IMAP4 connection, tag=b'LMHB'
Traceback (most recent call last):
  File "E:/temp/imap-idle/", line 18, in <module>
    connection.noop()  # Crashes here.
  File "C:\Python35\lib\", line 656, in noop
  File "C:\Python35\lib\", line 1210, in _dump_ur
    self._mesg('untagged responses dump:%s%s' % (t, t.join(l)))
  File "C:\Python35\lib\", line 1209, in <lambda>
    l = map(lambda x:'%s: "%s"' % (x[0], x[1][0] and '" "'.join(x[1]) or ''), l)
TypeError: sequence item 0: expected str instance, bytes found
msg262533 - (view) Author: Philip Dubé (Demur Rumed) * Date: 2016-03-27 20:12
I've attached a patch file of the proposed change which seems correct
msg262547 - (view) Author: Philip Dubé (Demur Rumed) * Date: 2016-03-28 02:23
Fixes syntax & line length issues in previous patch
msg293240 - (view) Author: Kostis Anagnostopoulos (ankostis) * Date: 2017-05-08 16:24
That is a show-stopper, and the patch works fine.
Should I submit a PR with a special test-case with debug >= 3 just for this?
msg293243 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2017-05-08 17:22
That sounds reasonable, if the debug output doesn't pollute the test run output.
msg349306 - (view) Author: Sanyam Khurana (CuriousLearner) * (Python triager) Date: 2019-08-09 18:33
Removing 3.5 as it is in security fix mode. Also, the PR just contains the test case that would trigger the condition and not the actual code.
msg349307 - (view) Author: Sanyam Khurana (CuriousLearner) * (Python triager) Date: 2019-08-09 18:35
Hey ankostis, 

Please let us know if you've time to complete the PR.

Thank you :)
msg349311 - (view) Author: Kostis Anagnostopoulos (ankostis) * Date: 2019-08-09 19:52
Dear Sanyam,

My apologies, it's been almost a year that i don't need the fix for this
my app has been graciously decommissioned, and i don;t have any time for it.


On Fri, 9 Aug 2019 at 21:35, Sanyam Khurana <> wrote:

> Sanyam Khurana <> added the comment:
> Hey ankostis,
> Please let us know if you've time to complete the PR.
> Thank you :)
> ----------
> _______________________________________
> Python tracker <>
> <>
> _______________________________________
msg349319 - (view) Author: Kostis Anagnostopoulos (ankostis) * Date: 2019-08-09 22:58
> BTW, i went back to my code and pasted what seemed to cure the problem,
added in commit 2b99cec  in my PR.  But i don't have much time to track if
it really works.
I hope it will help you resolve the issue.

msg370587 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-06-02 01:17
New changeset 8a3d2af997e3702eac4c5b012537be39ada36888 by Sanyam Khurana in branch 'master':
bpo-26543: Fix IMAP4.noop when debug mode is enabled (GH-15206)
msg370588 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-06-02 01:18
Merged, thanks for the fix.
Date User Action Args
2022-04-11 14:58:28adminsetgithub: 70730
2020-06-02 01:18:34vstinnersetstatus: open -> closed
nosy: barry, vstinner, r.david.murray, Stephen.Evans, maciej.szulik, maxking, gigaplastik, ankostis, Demur Rumed, CuriousLearner, rotuna
messages: + msg370588

resolution: fixed
stage: patch review -> resolved
2020-06-02 01:17:52vstinnersetnosy: + vstinner
messages: + msg370587
2020-04-23 22:40:39rotunasetnosy: + rotuna
pull_requests: + pull_request19013
2019-08-11 11:08:32CuriousLearnersetpull_requests: + pull_request14935
2019-08-09 22:58:37ankostissetmessages: + msg349319
2019-08-09 19:52:24ankostissetmessages: + msg349311
2019-08-09 18:35:37CuriousLearnersetmessages: + msg349307
2019-08-09 18:33:32CuriousLearnersetversions: + Python 3.7, Python 3.8, Python 3.9, - Python 3.5
nosy: + CuriousLearner

messages: + msg349306

stage: patch review
2019-06-15 02:24:55maxkingsetnosy: + maxking
2018-07-18 09:55:58gov_vjsetversions: - Python 3.6
2018-07-11 14:28:47vstinnersetkeywords: + easy
title: imaplib noop Debug -> [EASY] imaplib noop Debug: bytes vs Unicode bug in debug mode
2018-07-11 07:46:46serhiy.storchakasettype: crash -> behavior
2017-05-08 22:39:08ankostissetpull_requests: + pull_request1605
2017-05-08 17:22:52r.david.murraysetmessages: + msg293243
2017-05-08 16:24:08ankostissetnosy: + ankostis
messages: + msg293240
2016-03-28 02:23:34Demur Rumedsetfiles: + imaplib2.patch

messages: + msg262547
2016-03-27 20:12:58Demur Rumedsetfiles: + imaplib.patch

nosy: + Demur Rumed
messages: + msg262533

keywords: + patch
2016-03-25 14:27:47maciej.szuliksetnosy: + maciej.szulik
2016-03-17 00:46:17gigaplastiksetnosy: + gigaplastik
2016-03-11 23:23:43Stephen.Evanscreate