classification
Title: smtplib._quote_periods triggers spurious type error in re.sub
Type: behavior Stage: resolved
Components: email, Library (Lib) Versions: Python 3.2
process
Status: closed Resolution: duplicate
Dependencies: Superseder: python3.2 smtplib _quote_periods
View: 12283
Assigned To: Nosy List: axel, barry, r.david.murray, serhiy.storchaka, xuanji
Priority: normal Keywords:

Created on 2011-04-12 17:09 by axel, last changed 2013-02-14 16:01 by serhiy.storchaka. This issue is now closed.

Messages (3)
msg133590 - (view) Author: Axel Rau (axel) Date: 2011-04-12 17:09
While debugging this
  http://article.gmane.org/gmane.comp.python.general/687767
email problem, I'm getting:
---
 File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/smtplib.py", line 794, in send_message
    rcpt_options)
  File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/smtplib.py", line 762, in sendmail
    (code, resp) = self.data(msg)
  File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/smtplib.py", line 518, in data
    q = _quote_periods(msg)
  File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/smtplib.py", line 166, in _quote_periods
    return re.sub(br'(?m)^\.', '..', bindata)
  File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/re.py", line 168, in sub
    return _compile(pattern, flags).sub(repl, string, count)
TypeError: sequence item 1: expected bytes, str found
---
The following instrumentation:
---
def sub(pattern, repl, string, count=0, flags=0):
    print('re.sub: pattern=%s, repl=%s, string=%s' % (pattern.__class__.__name__, repl.__class__.__name__, string.__class__.__name__))
    return _compile(pattern, flags).sub(repl, string, count)
---
shows (in my test case with 2nd mail):
---
re.sub: pattern=bytes, repl=str, string=bytes
---
Changing smtplib._quote_periods(bindata) from
---
def _quote_periods(bindata):
    return re.sub(br'(?m)^\.', '..', bindata)
---
to:
---
def _quote_periods(bindata):
    return re.sub(br'(?m)^\.', b'..', bindata)
---
Fixes the problem for me.

Platform is Mac OS X 10.6.7, 64-bit.
Problem happens always on 2nd mail being sent.
Problem still exists with python 3.2.1a
msg134062 - (view) Author: Axel Rau (axel) Date: 2011-04-19 14:52
One more hint: The bug does not happen with plain text mails, but can easily be reproduced with MIME attachments.
msg182105 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2013-02-14 16:01
Already fixed in issue12283.
History
Date User Action Args
2013-02-14 16:01:41serhiy.storchakasetstatus: open -> closed

superseder: python3.2 smtplib _quote_periods
nosy: + barry, serhiy.storchaka, r.david.murray
components: + email

messages: + msg182105
type: crash -> behavior
resolution: duplicate
stage: resolved
2011-04-26 14:56:49xuanjisetnosy: + xuanji
2011-04-19 14:52:51axelsetmessages: + msg134062
2011-04-12 17:22:12axelsettype: behavior -> crash
2011-04-12 17:09:18axelcreate