Kaydet (Commit) b7deff1d authored tarafından R. David Murray's avatar R. David Murray

#9124: mailbox now accepts binary input and uses binary internally

Although this patch contains API changes and is rather weighty for an
RC phase, the mailbox module was essentially unusable without the patch
since it would produce UnicodeErrors when handling non-ascii input
at arbitrary and somewhat mysterious places, and any non-trivial amount
of email processing will encounter messages with non-ascii bytes.
The release manager approved the patch application.

The changes allow binary input, and reject non-ASCII string input early
with a useful message instead of failing mysteriously later.  Binary
is used internally for reading and writing the mailbox files.  StringIO
and Text file input are deprecated.

Initial patch by Victor Stinner, validated and expanded by R. David Murray.
üst b02f7c00
...@@ -81,13 +81,16 @@ Maildir, mbox, MH, Babyl, and MMDF. ...@@ -81,13 +81,16 @@ Maildir, mbox, MH, Babyl, and MMDF.
it. it.
Parameter *message* may be a :class:`Message` instance, an Parameter *message* may be a :class:`Message` instance, an
:class:`email.Message.Message` instance, a string, or a file-like object :class:`email.Message.Message` instance, a string, a byte string, or a
(which should be open in text mode). If *message* is an instance of the file-like object (which should be open in binary mode). If *message* is
an instance of the
appropriate format-specific :class:`Message` subclass (e.g., if it's an appropriate format-specific :class:`Message` subclass (e.g., if it's an
:class:`mboxMessage` instance and this is an :class:`mbox` instance), its :class:`mboxMessage` instance and this is an :class:`mbox` instance), its
format-specific information is used. Otherwise, reasonable defaults for format-specific information is used. Otherwise, reasonable defaults for
format-specific information are used. format-specific information are used.
.. versionchanged:: 3.2 support for binary input
.. method:: remove(key) .. method:: remove(key)
__delitem__(key) __delitem__(key)
...@@ -108,8 +111,9 @@ Maildir, mbox, MH, Babyl, and MMDF. ...@@ -108,8 +111,9 @@ Maildir, mbox, MH, Babyl, and MMDF.
:exc:`KeyError` exception if no message already corresponds to *key*. :exc:`KeyError` exception if no message already corresponds to *key*.
As with :meth:`add`, parameter *message* may be a :class:`Message` As with :meth:`add`, parameter *message* may be a :class:`Message`
instance, an :class:`email.Message.Message` instance, a string, or a instance, an :class:`email.Message.Message` instance, a string, a byte
file-like object (which should be open in text mode). If *message* is an string, or a file-like object (which should be open in binary mode). If
*message* is an
instance of the appropriate format-specific :class:`Message` subclass instance of the appropriate format-specific :class:`Message` subclass
(e.g., if it's an :class:`mboxMessage` instance and this is an (e.g., if it's an :class:`mboxMessage` instance and this is an
:class:`mbox` instance), its format-specific information is :class:`mbox` instance), its format-specific information is
...@@ -171,10 +175,20 @@ Maildir, mbox, MH, Babyl, and MMDF. ...@@ -171,10 +175,20 @@ Maildir, mbox, MH, Babyl, and MMDF.
raise a :exc:`KeyError` exception if no such message exists. raise a :exc:`KeyError` exception if no such message exists.
.. method:: get_bytes(key)
Return a byte representation of the message corresponding to *key*, or
raise a :exc:`KeyError` exception if no such message exists.
.. versionadded:: 3.2
.. method:: get_string(key) .. method:: get_string(key)
Return a string representation of the message corresponding to *key*, or Return a string representation of the message corresponding to *key*, or
raise a :exc:`KeyError` exception if no such message exists. raise a :exc:`KeyError` exception if no such message exists. The
message is processed through :class:`email.message.Message` to
convert it to a 7bit clean representation.
.. method:: get_file(key) .. method:: get_file(key)
...@@ -184,9 +198,11 @@ Maildir, mbox, MH, Babyl, and MMDF. ...@@ -184,9 +198,11 @@ Maildir, mbox, MH, Babyl, and MMDF.
file-like object behaves as if open in binary mode. This file should be file-like object behaves as if open in binary mode. This file should be
closed once it is no longer needed. closed once it is no longer needed.
.. versionadded:: 3.2 .. versionchanged:: 3.2
The file-like object supports the context manager protocol, so that The file object really is a binary file; previously it was incorrectly
you can use a :keyword:`with` statement to automatically close it. returned in text mode. Also, the file-like object now supports the
context manager protocol: you can use a :keyword:`with` statement to
automatically close it.
.. note:: .. note::
...@@ -746,9 +762,11 @@ Maildir, mbox, MH, Babyl, and MMDF. ...@@ -746,9 +762,11 @@ Maildir, mbox, MH, Babyl, and MMDF.
If *message* is omitted, the new instance is created in a default, empty state. If *message* is omitted, the new instance is created in a default, empty state.
If *message* is an :class:`email.Message.Message` instance, its contents are If *message* is an :class:`email.Message.Message` instance, its contents are
copied; furthermore, any format-specific information is converted insofar as copied; furthermore, any format-specific information is converted insofar as
possible if *message* is a :class:`Message` instance. If *message* is a string possible if *message* is a :class:`Message` instance. If *message* is a string,
a byte string,
or a file, it should contain an :rfc:`2822`\ -compliant message, which is read or a file, it should contain an :rfc:`2822`\ -compliant message, which is read
and parsed. and parsed. Files should be open in binary mode, but text mode files
are accepted for backward compatibility.
The format-specific state and behaviors offered by subclasses vary, but in The format-specific state and behaviors offered by subclasses vary, but in
general it is only the properties that are not specific to a particular general it is only the properties that are not specific to a particular
......
This diff is collapsed.
This diff is collapsed.
...@@ -16,6 +16,12 @@ Core and Builtins ...@@ -16,6 +16,12 @@ Core and Builtins
Library Library
------- -------
- Issue #9124: mailbox now accepts binary input and reads and writes mailbox
files in binary mode, using the email package's binary support to parse
arbitrary email messages. StringIO and text file input is deprecated,
and string input fails early if non-ASCII characters are used, where
previously it would fail when the email was processed in a later step.
- Issue #10845: Mitigate the incompatibility between the multiprocessing - Issue #10845: Mitigate the incompatibility between the multiprocessing
module on Windows and the use of package, zipfile or directory execution module on Windows and the use of package, zipfile or directory execution
by special casing main modules that actually *are* called __main__.py. by special casing main modules that actually *are* called __main__.py.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment