Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fdfb005

Browse files
committed
#18584: make doctest examples in email documentation pass.
With the exception of the 'as_string' call in the policy docs. That one is a separate issue. Note that when building the docs sphinx is complaining about .. testcleanup:: being an invalid directive. I don't know why this is, as I'm following the sphinx docs...but fortunately the action is to omit the text in the body, so the generated documentation is correct.
1 parent c06c0ae commit fdfb005

3 files changed

Lines changed: 67 additions & 23 deletions

File tree

Doc/library/email.iterators.rst

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,22 @@ useful higher level iterations over message object trees.
3333
Thus, by default :func:`typed_subpart_iterator` returns each subpart that has a
3434
MIME type of :mimetype:`text/\*`.
3535

36+
3637
The following function has been added as a useful debugging tool. It should
3738
*not* be considered part of the supported public interface for the package.
3839

39-
4040
.. function:: _structure(msg, fp=None, level=0, include_default=False)
4141

4242
Prints an indented representation of the content types of the message object
43-
structure. For example::
43+
structure. For example:
44+
45+
.. testsetup::
46+
47+
>>> import email
48+
>>> from email.iterators import _structure
49+
>>> somefile = open('Lib/test/test_email/data/msg_02.txt')
50+
51+
.. doctest::
4452

4553
>>> msg = email.message_from_file(somefile)
4654
>>> _structure(msg)
@@ -60,6 +68,10 @@ The following function has been added as a useful debugging tool. It should
6068
text/plain
6169
text/plain
6270

71+
.. testcleanup::
72+
73+
>>> somefile.close()
74+
6375
Optional *fp* is a file-like object to print the output to. It must be
6476
suitable for Python's :func:`print` function. *level* is used internally.
6577
*include_default*, if true, prints the default type as well.

Doc/library/email.message.rst

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -513,16 +513,25 @@ Here are the methods of the :class:`Message` class:
513513
iterator in a ``for`` loop; each iteration returns the next subpart.
514514

515515
Here's an example that prints the MIME type of every part of a multipart
516-
message structure::
517-
518-
>>> for part in msg.walk():
519-
... print(part.get_content_type())
520-
multipart/report
521-
text/plain
522-
message/delivery-status
523-
text/plain
524-
text/plain
525-
message/rfc822
516+
message structure:
517+
518+
.. testsetup::
519+
520+
>>> from email import message_from_binary_file
521+
>>> with open('Lib/test/test_email/data/msg_16.txt', 'rb') as f:
522+
... msg = message_from_binary_file(f)
523+
524+
.. doctest::
525+
526+
>>> for part in msg.walk():
527+
... print(part.get_content_type())
528+
multipart/report
529+
text/plain
530+
message/delivery-status
531+
text/plain
532+
text/plain
533+
message/rfc822
534+
text/plain
526535

527536
:class:`Message` objects can also optionally contain two instance attributes,
528537
which can be used when generating the plain text of a MIME message.

Doc/library/email.policy.rst

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,42 @@ same keyword arguments as the class constructor and returning a new
5656
attributes values changed.
5757

5858
As an example, the following code could be used to read an email message from a
59-
file on disk and pass it to the system ``sendmail`` program on a Unix system::
59+
file on disk and pass it to the system ``sendmail`` program on a Unix system:
6060

61-
>>> from email import msg_from_binary_file
61+
.. testsetup::
62+
63+
>>> from unittest import mock
64+
>>> mocker = mock.patch('subprocess.Popen')
65+
>>> m = mocker.start()
66+
>>> proc = mock.MagicMock()
67+
>>> m.return_value = proc
68+
>>> proc.stdin.close.return_value = None
69+
>>> mymsg = open('mymsg.txt', 'w')
70+
>>> mymsg.write('To: [email protected]\n\n')
71+
17
72+
>>> mymsg.flush()
73+
74+
.. doctest::
75+
76+
>>> from email import message_from_binary_file
6277
>>> from email.generator import BytesGenerator
78+
>>> from email import policy
6379
>>> from subprocess import Popen, PIPE
64-
>>> with open('mymsg.txt', 'b') as f:
65-
... msg = msg_from_binary_file(f)
66-
>>> p = Popen(['sendmail', msg['To'][0].address], stdin=PIPE)
80+
>>> with open('mymsg.txt', 'rb') as f:
81+
... msg = message_from_binary_file(f, policy=policy.default)
82+
>>> p = Popen(['sendmail', msg['To'].addresses[0]], stdin=PIPE)
6783
>>> g = BytesGenerator(p.stdin, policy=msg.policy.clone(linesep='\r\n'))
6884
>>> g.flatten(msg)
6985
>>> p.stdin.close()
7086
>>> rc = p.wait()
7187

88+
.. testcleanup::
89+
90+
>>> mymsg.close()
91+
>>> mocker.stop()
92+
>>> import os
93+
>>> os.remove('mymsg.txt')
94+
7295
Here we are telling :class:`~email.generator.BytesGenerator` to use the RFC
7396
correct line separator characters when creating the binary string to feed into
7497
``sendmail's`` ``stdin``, where the default policy would use ``\n`` line
@@ -82,22 +105,22 @@ separators for the platform on which it is running::
82105

83106
>>> import os
84107
>>> with open('converted.txt', 'wb') as f:
85-
... f.write(msg.as_string(policy=msg.policy.clone(linesep=os.linesep))
108+
... f.write(msg.as_string(policy=msg.policy.clone(linesep=os.linesep)))
86109

87110
Policy objects can also be combined using the addition operator, producing a
88111
policy object whose settings are a combination of the non-default values of the
89112
summed objects::
90113

91-
>>> compat_SMTP = email.policy.clone(linesep='\r\n')
92-
>>> compat_strict = email.policy.clone(raise_on_defect=True)
114+
>>> compat_SMTP = policy.compat32.clone(linesep='\r\n')
115+
>>> compat_strict = policy.compat32.clone(raise_on_defect=True)
93116
>>> compat_strict_SMTP = compat_SMTP + compat_strict
94117

95118
This operation is not commutative; that is, the order in which the objects are
96119
added matters. To illustrate::
97120

98-
>>> policy100 = compat32.clone(max_line_length=100)
99-
>>> policy80 = compat32.clone(max_line_length=80)
100-
>>> apolicy = policy100 + Policy80
121+
>>> policy100 = policy.compat32.clone(max_line_length=100)
122+
>>> policy80 = policy.compat32.clone(max_line_length=80)
123+
>>> apolicy = policy100 + policy80
101124
>>> apolicy.max_line_length
102125
80
103126
>>> apolicy = policy80 + policy100

0 commit comments

Comments
 (0)