Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
299a2219
Commit
299a2219
authored
Jul 29, 2013
by
R David Murray
Browse files
Options
Browse Files
Download
Plain Diff
Merge #18584: make doctest examples in email documentation pass.
parents
00dc61cd
fdfb0050
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
67 additions
and
23 deletions
+67
-23
Doc/library/email.iterators.rst
Doc/library/email.iterators.rst
+14
-2
Doc/library/email.message.rst
Doc/library/email.message.rst
+19
-10
Doc/library/email.policy.rst
Doc/library/email.policy.rst
+34
-11
No files found.
Doc/library/email.iterators.rst
View file @
299a2219
...
@@ -33,14 +33,22 @@ useful higher level iterations over message object trees.
...
@@ -33,14 +33,22 @@ useful higher level iterations over message object trees.
Thus, by default :func:`typed_subpart_iterator` returns each subpart that has a
Thus, by default :func:`typed_subpart_iterator` returns each subpart that has a
MIME type of :mimetype:`text/\*`.
MIME type of :mimetype:`text/\*`.
The following function has been added as a useful debugging tool. It should
The following function has been added as a useful debugging tool. It should
*not* be considered part of the supported public interface for the package.
*not* be considered part of the supported public interface for the package.
.. function:: _structure(msg, fp=None, level=0, include_default=False)
.. function:: _structure(msg, fp=None, level=0, include_default=False)
Prints an indented representation of the content types of the message object
Prints an indented representation of the content types of the message object
structure. For example::
structure. For example:
.. testsetup::
>>> import email
>>> from email.iterators import _structure
>>> somefile = open('Lib/test/test_email/data/msg_02.txt')
.. doctest::
>>> msg = email.message_from_file(somefile)
>>> msg = email.message_from_file(somefile)
>>> _structure(msg)
>>> _structure(msg)
...
@@ -60,6 +68,10 @@ The following function has been added as a useful debugging tool. It should
...
@@ -60,6 +68,10 @@ The following function has been added as a useful debugging tool. It should
text/plain
text/plain
text/plain
text/plain
.. testcleanup::
>>> somefile.close()
Optional *fp* is a file-like object to print the output to. It must be
Optional *fp* is a file-like object to print the output to. It must be
suitable for Python's :func:`print` function. *level* is used internally.
suitable for Python's :func:`print` function. *level* is used internally.
*include_default*, if true, prints the default type as well.
*include_default*, if true, prints the default type as well.
Doc/library/email.message.rst
View file @
299a2219
...
@@ -513,7 +513,15 @@ Here are the methods of the :class:`Message` class:
...
@@ -513,7 +513,15 @@ Here are the methods of the :class:`Message` class:
iterator in a ``for`` loop; each iteration returns the next subpart.
iterator in a ``for`` loop; each iteration returns the next subpart.
Here's an example that prints the MIME type of every part of a multipart
Here's an example that prints the MIME type of every part of a multipart
message structure::
message structure:
.. testsetup::
>>> from email import message_from_binary_file
>>> with open('Lib/test/test_email/data/msg_16.txt', 'rb') as f:
... msg = message_from_binary_file(f)
.. doctest::
>>> for part in msg.walk():
>>> for part in msg.walk():
... print(part.get_content_type())
... print(part.get_content_type())
...
@@ -523,6 +531,7 @@ Here are the methods of the :class:`Message` class:
...
@@ -523,6 +531,7 @@ Here are the methods of the :class:`Message` class:
text/plain
text/plain
text/plain
text/plain
message/rfc822
message/rfc822
text/plain
:class:`Message` objects can also optionally contain two instance attributes,
:class:`Message` objects can also optionally contain two instance attributes,
which can be used when generating the plain text of a MIME message.
which can be used when generating the plain text of a MIME message.
...
...
Doc/library/email.policy.rst
View file @
299a2219
...
@@ -56,19 +56,42 @@ same keyword arguments as the class constructor and returning a new
...
@@ -56,19 +56,42 @@ same keyword arguments as the class constructor and returning a new
attributes values changed.
attributes values changed.
As an example, the following code could be used to read an email message from a
As an example, the following code could be used to read an email message from a
file on disk and pass it to the system ``sendmail`` program on a Unix system:
:
file on disk and pass it to the system ``sendmail`` program on a Unix system:
>>> from email import msg_from_binary_file
.. testsetup::
>>> from unittest import mock
>>> mocker = mock.patch('subprocess.Popen')
>>> m = mocker.start()
>>> proc = mock.MagicMock()
>>> m.return_value = proc
>>> proc.stdin.close.return_value = None
>>> mymsg = open('mymsg.txt', 'w')
>>> mymsg.write('To: abc@xyz.com\n\n')
17
>>> mymsg.flush()
.. doctest::
>>> from email import message_from_binary_file
>>> from email.generator import BytesGenerator
>>> from email.generator import BytesGenerator
>>> from email import policy
>>> from subprocess import Popen, PIPE
>>> from subprocess import Popen, PIPE
>>> with open('mymsg.txt', 'b') as f:
>>> with open('mymsg.txt', '
r
b') as f:
... msg = m
sg_from_binary_file(f
)
... msg = m
essage_from_binary_file(f, policy=policy.default
)
>>> p = Popen(['sendmail', msg['To']
[0].address
], stdin=PIPE)
>>> p = Popen(['sendmail', msg['To']
.addresses[0]
], stdin=PIPE)
>>> g = BytesGenerator(p.stdin, policy=msg.policy.clone(linesep='\r\n'))
>>> g = BytesGenerator(p.stdin, policy=msg.policy.clone(linesep='\r\n'))
>>> g.flatten(msg)
>>> g.flatten(msg)
>>> p.stdin.close()
>>> p.stdin.close()
>>> rc = p.wait()
>>> rc = p.wait()
.. testcleanup::
>>> mymsg.close()
>>> mocker.stop()
>>> import os
>>> os.remove('mymsg.txt')
Here we are telling :class:`~email.generator.BytesGenerator` to use the RFC
Here we are telling :class:`~email.generator.BytesGenerator` to use the RFC
correct line separator characters when creating the binary string to feed into
correct line separator characters when creating the binary string to feed into
``sendmail's`` ``stdin``, where the default policy would use ``\n`` line
``sendmail's`` ``stdin``, where the default policy would use ``\n`` line
...
@@ -82,22 +105,22 @@ separators for the platform on which it is running::
...
@@ -82,22 +105,22 @@ separators for the platform on which it is running::
>>> import os
>>> import os
>>> with open('converted.txt', 'wb') as f:
>>> with open('converted.txt', 'wb') as f:
... f.write(msg.as_string(policy=msg.policy.clone(linesep=os.linesep))
... f.write(msg.as_string(policy=msg.policy.clone(linesep=os.linesep))
)
Policy objects can also be combined using the addition operator, producing a
Policy objects can also be combined using the addition operator, producing a
policy object whose settings are a combination of the non-default values of the
policy object whose settings are a combination of the non-default values of the
summed objects::
summed objects::
>>> compat_SMTP =
email.policy
.clone(linesep='\r\n')
>>> compat_SMTP =
policy.compat32
.clone(linesep='\r\n')
>>> compat_strict =
email.policy
.clone(raise_on_defect=True)
>>> compat_strict =
policy.compat32
.clone(raise_on_defect=True)
>>> compat_strict_SMTP = compat_SMTP + compat_strict
>>> compat_strict_SMTP = compat_SMTP + compat_strict
This operation is not commutative; that is, the order in which the objects are
This operation is not commutative; that is, the order in which the objects are
added matters. To illustrate::
added matters. To illustrate::
>>> policy100 = compat32.clone(max_line_length=100)
>>> policy100 =
policy.
compat32.clone(max_line_length=100)
>>> policy80 = compat32.clone(max_line_length=80)
>>> policy80 =
policy.
compat32.clone(max_line_length=80)
>>> apolicy = policy100 +
P
olicy80
>>> apolicy = policy100 +
p
olicy80
>>> apolicy.max_line_length
>>> apolicy.max_line_length
80
80
>>> apolicy = policy80 + policy100
>>> apolicy = policy80 + policy100
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment