@@ -80,6 +80,14 @@ Here is the API for the :class:`FeedParser`:
8080 if you feed more data to a closed :class: `FeedParser `.
8181
8282
83+ .. class :: BytesFeedParser(_factory=email.message.Message)
84+
85+ Works exactly like :class: `FeedParser ` except that the input to the
86+ :meth: `~FeedParser.feed ` method must be bytes and not string.
87+
88+ .. versionadded :: 3.2
89+
90+
8391Parser class API
8492^^^^^^^^^^^^^^^^
8593
@@ -131,33 +139,86 @@ class.
131139
132140 Similar to the :meth: `parse ` method, except it takes a string object
133141 instead of a file-like object. Calling this method on a string is exactly
134- equivalent to wrapping *text * in a :class: `StringIO ` instance first and
142+ equivalent to wrapping *text * in a :class: `~io. StringIO ` instance first and
135143 calling :meth: `parse `.
136144
137145 Optional *headersonly * is a flag specifying whether to stop parsing after
138146 reading the headers or not. The default is ``False ``, meaning it parses
139147 the entire contents of the file.
140148
141149
150+ .. class :: BytesParser(_class=email.message.Message, strict=None)
151+
152+ This class is exactly parallel to :class: `Parser `, but handles bytes input.
153+ The *_class * and *strict * arguments are interpreted in the same way as for
154+ the :class: `Parser ` constructor. *strict * is supported only to make porting
155+ code easier; it is deprecated.
156+
157+ .. method :: parse(fp, headeronly=False)
158+
159+ Read all the data from the binary file-like object *fp *, parse the
160+ resulting bytes, and return the message object. *fp * must support
161+ both the :meth: `readline ` and the :meth: `read ` methods on file-like
162+ objects.
163+
164+ The bytes contained in *fp * must be formatted as a block of :rfc: `2822 `
165+ style headers and header continuation lines, optionally preceded by a
166+ envelope header. The header block is terminated either by the end of the
167+ data or by a blank line. Following the header block is the body of the
168+ message (which may contain MIME-encoded subparts, including subparts
169+ with a :mailheader: `Content-Transfer-Encoding ` of ``8bit ``.
170+
171+ Optional *headersonly * is a flag specifying whether to stop parsing after
172+ reading the headers or not. The default is ``False ``, meaning it parses
173+ the entire contents of the file.
174+
175+ .. method :: parsebytes(bytes, headersonly=False)
176+
177+ Similar to the :meth: `parse ` method, except it takes a byte string object
178+ instead of a file-like object. Calling this method on a byte string is
179+ exactly equivalent to wrapping *text * in a :class: `~io.BytesIO ` instance
180+ first and calling :meth: `parse `.
181+
182+ Optional *headersonly * is as with the :meth: `parse ` method.
183+
184+ .. versionadded :: 3.2
185+
186+
142187Since creating a message object structure from a string or a file object is such
143- a common task, two functions are provided as a convenience. They are available
188+ a common task, four functions are provided as a convenience. They are available
144189in the top-level :mod: `email ` package namespace.
145190
146191.. currentmodule :: email
147192
148- .. function :: message_from_string(s[ , _class][ , strict] )
193+ .. function :: message_from_string(s, _class=email.message.Message , strict=None )
149194
150195 Return a message object structure from a string. This is exactly equivalent to
151196 ``Parser().parsestr(s) ``. Optional *_class * and *strict * are interpreted as
152197 with the :class: `Parser ` class constructor.
153198
199+ .. function :: message_from_bytes(s, _class=email.message.Message, strict=None)
200+
201+ Return a message object structure from a byte string. This is exactly
202+ equivalent to ``BytesParser().parsebytes(s) ``. Optional *_class * and
203+ *strict * are interpreted as with the :class: `Parser ` class constructor.
204+
205+ .. versionadded :: 3.2
154206
155- .. function :: message_from_file(fp[ , _class][ , strict] )
207+ .. function :: message_from_file(fp, _class=email.message.Message , strict=None )
156208
157209 Return a message object structure tree from an open :term: `file object `.
158210 This is exactly equivalent to ``Parser().parse(fp) ``. Optional *_class *
159211 and *strict * are interpreted as with the :class: `Parser ` class constructor.
160212
213+ .. function :: message_from_binary_file(fp, _class=email.message.Message, strict=None)
214+
215+ Return a message object structure tree from an open binary :term: `file
216+ object `. This is exactly equivalent to ``BytesParser().parse(fp) ``.
217+ Optional *_class * and *strict * are interpreted as with the :class: `Parser `
218+ class constructor.
219+
220+ .. versionadded :: 3.2
221+
161222Here's an example of how you might use this at an interactive Python prompt::
162223
163224 >>> import email
0 commit comments