diff Lib/email/parser.py @ 69428:2f3fda9d3906

#11731: simplify/enhance parser/generator API by introducing policy objects. This new interface will also allow for future planned enhancements in control over the parser/generator without requiring any additional complexity in the parser/generator API. Patch reviewed by Éric Araujo and Barry Warsaw.
author R David Murray <rdmurray@bitdance.com>
date Mon, 18 Apr 2011 13:59:37 -0400
parents a95d936ce8eb
children 561fc3b4cc2a
line wrap: on
line diff
--- a/Lib/email/parser.py	Mon Apr 18 16:30:17 2011 +0200
+++ b/Lib/email/parser.py	Mon Apr 18 13:59:37 2011 -0400
@@ -11,11 +11,12 @@
 
 from email.feedparser import FeedParser
 from email.message import Message
+from email import policy
 
 
 
 class Parser:
-    def __init__(self, _class=Message):
+    def __init__(self, _class=Message, *, policy=policy.default):
         """Parser of RFC 2822 and MIME email messages.
 
         Creates an in-memory object tree representing the email message, which
@@ -30,8 +31,14 @@
         _class is the class to instantiate for new message objects when they
         must be created.  This class must have a constructor that can take
         zero arguments.  Default is Message.Message.
+
+        The policy keyword specifies a policy object that controls a number of
+        aspects of the parser's operation.  The default policy maintains
+        backward compatibility.
+
         """
         self._class = _class
+        self.policy = policy
 
     def parse(self, fp, headersonly=False):
         """Create a message structure from the data in a file.
@@ -41,7 +48,7 @@
         parsing after reading the headers or not.  The default is False,
         meaning it parses the entire contents of the file.
         """
-        feedparser = FeedParser(self._class)
+        feedparser = FeedParser(self._class, policy=self.policy)
         if headersonly:
             feedparser._set_headersonly()
         while True: