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

Created on 2016-03-11 23:23 by Stephen.Evans, last changed 2019-06-15 02:24 by maxking.

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 open ankostis, 2017-05-08 22:39
Messages (5)
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: Demur Rumed (Demur Rumed) * Date: 2016-03-27 20:12
I've attached a patch file of the proposed change which seems correct
msg262547 - (view) Author: Demur Rumed (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.
Date User Action Args
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