[Python-checkins] cpython (3.4): Issue #23511: Port email-simple.py to Python 3.

berker.peksag python-checkins at python.org
Wed Feb 25 17:15:05 CET 2015


https://hg.python.org/cpython/rev/89c6a6c2dd1f
changeset:   94747:89c6a6c2dd1f
branch:      3.4
parent:      94745:620cb13008b7
user:        Berker Peksag <berker.peksag at gmail.com>
date:        Wed Feb 25 18:14:09 2015 +0200
summary:
  Issue #23511: Port email-simple.py to Python 3.

Also, update email examples to use the context manager version of open().

Patch by Baptiste Mispelon.

files:
  Doc/includes/email-headers.py                  |  5 +++--
  Doc/includes/email-mime.py                     |  5 ++---
  Doc/includes/email-read-alternative-new-api.py |  3 ++-
  Doc/includes/email-simple.py                   |  7 +++----
  4 files changed, 10 insertions(+), 10 deletions(-)


diff --git a/Doc/includes/email-headers.py b/Doc/includes/email-headers.py
--- a/Doc/includes/email-headers.py
+++ b/Doc/includes/email-headers.py
@@ -1,8 +1,9 @@
 # Import the email modules we'll need
 from email.parser import Parser
 
-#  If the e-mail headers are in a file, uncomment this line:
-#headers = Parser().parse(open(messagefile, 'r'))
+# If the e-mail headers are in a file, uncomment these two lines:
+# with open(messagefile) as fp:
+#     headers = Parser().parse(fp)
 
 #  Or for parsing headers in a string, use:
 headers = Parser().parsestr('From: <user at example.com>\n'
diff --git a/Doc/includes/email-mime.py b/Doc/includes/email-mime.py
--- a/Doc/includes/email-mime.py
+++ b/Doc/includes/email-mime.py
@@ -20,9 +20,8 @@
 for file in pngfiles:
     # Open the files in binary mode.  Let the MIMEImage class automatically
     # guess the specific image type.
-    fp = open(file, 'rb')
-    img = MIMEImage(fp.read())
-    fp.close()
+    with open(file, 'rb') as fp:
+        img = MIMEImage(fp.read())
     msg.attach(img)
 
 # Send the email via our own SMTP server.
diff --git a/Doc/includes/email-read-alternative-new-api.py b/Doc/includes/email-read-alternative-new-api.py
--- a/Doc/includes/email-read-alternative-new-api.py
+++ b/Doc/includes/email-read-alternative-new-api.py
@@ -12,7 +12,8 @@
 from imaginary import magic_html_parser
 
 # In a real program you'd get the filename from the arguments.
-msg = BytesParser(policy=policy.default).parse(open('outgoing.msg', 'rb'))
+with open('outgoing.msg', 'rb') as fp:
+    msg = BytesParser(policy=policy.default).parse(fp)
 
 # Now the header items can be accessed as a dictionary, and any non-ASCII will
 # be converted to unicode:
diff --git a/Doc/includes/email-simple.py b/Doc/includes/email-simple.py
--- a/Doc/includes/email-simple.py
+++ b/Doc/includes/email-simple.py
@@ -6,10 +6,9 @@
 
 # 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())
-fp.close()
+with open(textfile) as fp:
+    # Create a text/plain message
+    msg = MIMEText(fp.read())
 
 # me == the sender's email address
 # you == the recipient's email address

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list