Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(3137)

Side by Side Diff: Lib/email/parser.py

Issue 4661: email.parser: impossible to read messages encoded in a different encoding
Patch Set: Created 2 years, 7 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Lib/email/message.py ('k') | Lib/email/test/test_email.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright (C) 2001-2007 Python Software Foundation 1 # Copyright (C) 2001-2007 Python Software Foundation
2 # Author: Barry Warsaw, Thomas Wouters, Anthony Baxter 2 # Author: Barry Warsaw, Thomas Wouters, Anthony Baxter
3 # Contact: email-sig@python.org 3 # Contact: email-sig@python.org
4 4
5 """A parser of RFC 2822 and MIME email messages.""" 5 """A parser of RFC 2822 and MIME email messages."""
6 6
7 __all__ = ['Parser', 'HeaderParser'] 7 __all__ = ['Parser', 'HeaderParser']
8 8
9 import warnings 9 import warnings
10 from io import StringIO 10 from io import StringIO, TextIOWrapper
11 11
12 from email.feedparser import FeedParser 12 from email.feedparser import FeedParser
13 from email.message import Message 13 from email.message import Message
14 14
15 15
16 16
17 class Parser: 17 class Parser:
18 def __init__(self, *args, **kws): 18 def __init__(self, *args, **kws):
19 """Parser of RFC 2822 and MIME email messages. 19 """Parser of RFC 2822 and MIME email messages.
20 20
21 Creates an in-memory object tree representing the email message, which 21 Creates an in-memory object tree representing the email message, which
22 can then be manipulated and turned over to a Generator to return the 22 can then be manipulated and turned over to a Generator to return the
23 textual representation of the message. 23 textual representation of the message.
24 24
25 The string must be formatted as a block of RFC 2822 headers and header 25 The string must be formatted as a block of RFC 2822 headers and header
26 continuation lines, optionally preceeded by a `Unix-from' header. The 26 continuation lines, optionally preceeded by a `Unix-from' header. The
27 header block is terminated either by the end of the string or by a 27 header block is terminated either by the end of the string or by a
28 blank line. 28 blank line.
29 29
30 _class is the class to instantiate for new message objects when they 30 _class is the class to instantiate for new message objects when they
31 must be created. This class must have a constructor that can take 31 must be created. This class must have a constructor that can take
32 zero arguments. Default is Message.Message. 32 zero arguments. Default is Message.Message.
33 """ 33 """
34
34 if len(args) >= 1: 35 if len(args) >= 1:
35 if '_class' in kws: 36 if '_class' in kws:
36 raise TypeError("Multiple values for keyword arg '_class'") 37 raise TypeError("Multiple values for keyword arg '_class'")
37 kws['_class'] = args[0] 38 kws['_class'] = args[0]
38 if len(args) == 2: 39 if len(args) == 2:
39 if 'strict' in kws: 40 if 'strict' in kws:
40 raise TypeError("Multiple values for keyword arg 'strict'") 41 raise TypeError("Multiple values for keyword arg 'strict'")
41 kws['strict'] = args[1] 42 kws['strict'] = args[1]
42 if len(args) > 2: 43 if len(args) > 2:
43 raise TypeError('Too many arguments') 44 raise TypeError('Too many arguments')
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
82 return self.parse(StringIO(text), headersonly=headersonly) 83 return self.parse(StringIO(text), headersonly=headersonly)
83 84
84 85
85 86
86 class HeaderParser(Parser): 87 class HeaderParser(Parser):
87 def parse(self, fp, headersonly=True): 88 def parse(self, fp, headersonly=True):
88 return Parser.parse(self, fp, True) 89 return Parser.parse(self, fp, True)
89 90
90 def parsestr(self, text, headersonly=True): 91 def parsestr(self, text, headersonly=True):
91 return Parser.parsestr(self, text, True) 92 return Parser.parsestr(self, text, True)
93
94
95 class BytesParser:
96
97 def __init__(self, *args, **kw):
98 """Parser of binary RFC 2822 and MIME email messages.
99
100 Creates an in-memory object tree representing the email message, which
101 can then be manipulated and turned over to a Generator to return the
102 textual representation of the message.
103
104 The input must be formatted as a block of RFC 2822 headers and header
105 continuation lines, optionally preceeded by a `Unix-from' header. The
106 header block is terminated either by the end of the input or by a
107 blank line.
108
109 _class is the class to instantiate for new message objects when they
110 must be created. This class must have a constructor that can take
111 zero arguments. Default is Message.Message.
112 """
113 self.parser = Parser(*args, **kw)
114
115 def parse(self, fp, headersonly=False):
116 """Create a message structure from the data in a binary file.
117
118 Reads all the data from the file and returns the root of the message
119 structure. Optional headersonly is a flag specifying whether to stop
120 parsing after reading the headers or not. The default is False,
121 meaning it parses the entire contents of the file.
122 """
123 fp = TextIOWrapper(fp, encoding='ascii', errors='surrogateescape')
124 return self.parser.parse(fp, headersonly)
125
126
127 def parsebytes(self, text, headersonly=False):
128 """Create a message structure from a byte string.
129
130 Returns the root of the message structure. Optional headersonly is a
131 flag specifying whether to stop parsing after reading the headers or
132 not. The default is False, meaning it parses the entire contents of
133 the file.
134 """
135 text = text.decode('ASCII', errors='surrogateescape')
136 return self.parser.parsestr(text, headersonly)
OLDNEW
« no previous file with comments | « Lib/email/message.py ('k') | Lib/email/test/test_email.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld cbc36f91f3f7