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.

classification
Title: Broken code example in email module documentation
Type: enhancement Stage: resolved
Components: Documentation Versions: Python 3.4, Python 3.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: docs@python Nosy List: berker.peksag, bmispelon, docs@python, python-dev, r.david.murray
Priority: normal Keywords: patch

Created on 2015-02-24 15:15 by bmispelon, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
issue23511.diff bmispelon, 2015-02-24 20:05 review
issue23511_open_cm.diff bmispelon, 2015-02-25 14:05 review
Messages (7)
msg236503 - (view) Author: Baptiste Mispelon (bmispelon) * Date: 2015-02-24 15:15
The first code example at https://docs.python.org/3.5/library/email-examples.html throws an `AttributeError` because `MIMEText`'s constructor expects a `str` object, not a `bytes` one:

>>> # Import smtplib for the actual sending function
... import smtplib
>>> 
>>> # Import the email modules we'll need
... from email.mime.text import MIMEText
>>> 
>>> # Open a plain text file for reading.  For this example, assume that
... # the text file contains only ASCII characters.
... fp = open(textfile, 'rb')
>>> # Create a text/plain message
... msg = MIMEText(fp.read())
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "/usr/lib/python3.4/email/mime/text.py", line 33, in __init__
    _text.encode('us-ascii')
AttributeError: 'bytes' object has no attribute 'encode'
msg236539 - (view) Author: Baptiste Mispelon (bmispelon) * Date: 2015-02-24 20:05
Patch attached.
msg236543 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2015-02-24 20:53
LGTM.
msg236575 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-02-25 13:57
Looks like the example wasn't updated during the python3 transition.

As long as we are changing it, we might as well make it use the file as a context manager.
msg236576 - (view) Author: Baptiste Mispelon (bmispelon) * Date: 2015-02-25 14:05
I wasn't sure if that was going to be out of scope or not.

Here's an attached patch that fixes the remaining usages of `open` inside `Doc/includes` to always use a context manager.
msg236588 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2015-02-25 16:14
New changeset 89c6a6c2dd1f by Berker Peksag in branch '3.4':
Issue #23511: Port email-simple.py to Python 3.
https://hg.python.org/cpython/rev/89c6a6c2dd1f

New changeset cb911e1fb3dc by Berker Peksag in branch 'default':
Issue #23511: Port email-simple.py to Python 3.
https://hg.python.org/cpython/rev/cb911e1fb3dc
msg236590 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2015-02-25 16:16
Thanks for the patch, Baptiste.
History
Date User Action Args
2022-04-11 14:58:13adminsetgithub: 67699
2015-02-25 16:16:47berker.peksagsetstatus: open -> closed
type: enhancement
messages: + msg236590

resolution: fixed
stage: commit review -> resolved
2015-02-25 16:14:41python-devsetnosy: + python-dev
messages: + msg236588
2015-02-25 14:05:44bmispelonsetfiles: + issue23511_open_cm.diff

messages: + msg236576
2015-02-25 13:57:07r.david.murraysetmessages: + msg236575
2015-02-24 20:53:07berker.peksagsetversions: + Python 3.4, Python 3.5
nosy: + r.david.murray, berker.peksag

messages: + msg236543

stage: commit review
2015-02-24 20:05:21bmispelonsetfiles: + issue23511.diff
keywords: + patch
messages: + msg236539
2015-02-24 15:15:02bmispeloncreate