| --- a/Lib/email/feedparser.py |
| +++ b/Lib/email/feedparser.py |
| @@ -25,6 +25,7 @@ |
| from email import errors |
| from email import message |
| +from email import policy |
| NLCRE = re.compile('\r\n|\r|\n') |
| NLCRE_bol = re.compile('(\r\n|\r|\n)') |
| @@ -137,9 +138,16 @@ |
| class FeedParser: |
| """A feed-style parser of email.""" |
| - def __init__(self, _factory=message.Message): |
| - """_factory is called with no arguments to create a new message obj""" |
| + def __init__(self, _factory=message.Message, *, policy=policy.default): |
| + """_factory is called with no arguments to create a new message obj |
| + |
| + 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._factory = _factory |
| + self.policy = policy |
| self._input = BufferedSubFile() |
| self._msgstack = [] |
| self._parse = self._parsegen().__next__ |
| @@ -171,7 +179,8 @@ |
| # Look for final set of defects |
| if root.get_content_maintype() == 'multipart' \ |
| and not root.is_multipart(): |
| - root.defects.append(errors.MultipartInvariantViolationDefect()) |
| + defect = errors.MultipartInvariantViolationDefect() |
| + self.policy.handle_defect(root, defect) |
| return root |
| def _new_message(self): |
| @@ -284,7 +293,8 @@ |
| # defined a boundary. That's a problem which we'll handle by |
| # reading everything until the EOF and marking the message as |
| # defective. |
| - self._cur.defects.append(errors.NoBoundaryInMultipartDefect()) |
| + defect = errors.NoBoundaryInMultipartDefect() |
| + self.policy.handle_defect(self._cur, defect) |
| lines = [] |
| for line in self._input: |
| if line is NeedMoreData: |
| @@ -388,7 +398,8 @@ |
| # that as a defect and store the captured text as the payload. |
| # Everything from here to the EOF is epilogue. |
| if capturing_preamble: |
| - self._cur.defects.append(errors.StartBoundaryNotFoundDefect()) |
| + defect = errors.StartBoundaryNotFoundDefect() |
| + self.policy.handle_defect(self._cur, defect) |
| self._cur.set_payload(EMPTYSTRING.join(preamble)) |
| epilogue = [] |
| for line in self._input: |
| @@ -440,7 +451,7 @@ |
| # is illegal, so let's note the defect, store the illegal |
| # line, and ignore it for purposes of headers. |
| defect = errors.FirstHeaderLineIsContinuationDefect(line) |
| - self._cur.defects.append(defect) |
| + self.policy.handle_defect(self._cur, defect) |
| continue |
| lastvalue.append(line) |
| continue |