Commit Graph

599 Commits

Author SHA1 Message Date
R David Murray 638d40b433 #15249: Mangle From lines correctly when body contains invalid bytes.
Fix by Colin Su.  Test by me, based on a test written by Petri Lehtinen.
2012-08-24 11:14:13 -04:00
Alexander Belopolsky f9bd9141c5 Issue #665194: Added a small optimization 2012-08-22 23:02:36 -04:00
R David Murray 097a1208bc #665194: fix variable name in exception code path.
It was correct in the original patch and I foobared it
when I restructured part of the code.
2012-08-22 21:52:31 -04:00
R David Murray b8687df653 #665194: Update email.utils.localtime to use astimezone, and fix bug.
The new code correctly handles historic changes in UTC offsets.
A test for this should follow.

Original patch by Alexander Belopolsky.
2012-08-22 21:34:00 -04:00
R David Murray 970bef295d Merge #15232: correctly mangle From lines in MIME preamble and epilogue 2012-07-22 21:53:54 -04:00
R David Murray 6a31bc6d81 #15232: correctly mangle From lines in MIME preamble and epilogue 2012-07-22 21:47:53 -04:00
R David Murray 97f43c019f #15160: Extend the new email parser to handle MIME headers.
This code passes all the same tests that the existing RFC mime header
parser passes, plus a bunch of additional ones.

There are a couple of commented out tests where there are issues with the
folding.  The folding doesn't normally get invoked for headers parsed from
source, and the cases are marginal anyway (headers with invalid binary data)
so I'm not worried about them, but will fix them after the beta.

There are things that can be done to make this API even more convenient, but I
think this is a solid foundation worth having.  And the parser is a full RFC
parser, so it handles cases that the current parser doesn't.  (There are also
probably cases where it fails when the current parser doesn't, but I haven't
found them yet ;)

Oh, yeah, and there are some really ugly bits in the parser for handling some
'postel' cases that are unfortunately common.

I hope/plan to to eventually refactor a lot of the code in the parser which
should reduce the line count...but there is no escaping the fact that the
error recovery is welter of special cases.
2012-06-24 05:03:27 -04:00
Alexander Belopolsky 76935b9c8c Issue #14653: email.utils.mktime_tz() no longer relies on system
mktime() when timezone offest is supplied.
2012-06-21 20:48:23 -04:00
Alexander Belopolsky a07548e97b Issue #14653: email.utils.mktime_tz() no longer relies on system
mktime() when timezone offest is supplied.
2012-06-21 20:34:09 -04:00
R David Murray 7636b19366 Merge: Update out of date docstring. 2012-06-17 15:27:21 -04:00
R David Murray 3ac8c780f4 Update out of date docstring. 2012-06-17 15:26:35 -04:00
R David Murray 6e50b699ac Now that Defects are Exception subclasses, call super.
The behavior of MessageDefect is legacy behavior.  The chances anyone is
actually using the undocumented 'line' attribute is low, but it costs
little to retain backward compatibility.  Although one of the costs is
having to restore normal exception behavior in HeaderDefect.  On the
other hand, I'll probably add some specialized behavior there later.
2012-06-08 22:45:46 -04:00
R David Murray 07ea53cb21 #1079: Fix parsing of encoded words.
This is a behavior change: before this leading and trailing spaces were
stripped from ASCII parts, now they are preserved.  Without this fix we didn't
parse the examples in the RFC correctly, so I think breaking backward
compatibility here is justified.

Patch by Ralf Schlatterbeck.
2012-06-02 17:56:49 -04:00
R David Murray 1be413e366 Don't use metaclasses when class decorators can do the job.
Thanks to Nick Coghlan for pointing out that I'd forgotten about class
decorators.
2012-05-31 18:00:45 -04:00
R David Murray abfc37491c #10839: raise an error on add of duplicate unique headers in new email policies
This feature was supposed to be part of the initial email6 checkin, but it got
lost in my big refactoring.

In this patch I'm not providing an easy way to turn off the errors, but they
only happen when a header is added programmatically, and it is almost never
the right thing to do to allow the duplicate to be added.  An application that
needs to add duplicates of unique headers can create a policy subclass to
allow it.
2012-05-29 09:14:44 -04:00
R David Murray 7ef3ff3f2e #12515: email now registers a defect if the MIME end boundary is missing.
This commit also restores the news item for 167256 that it looks like
Terry inadvertently deleted.  (Either that, or I don't understand
now merging works...which is equally possible.)
2012-05-27 22:20:42 -04:00
R David Murray 80e0aee95b #1672568: email now registers defects for base64 payload format errors.
Which also means that it is now producing *something* for any base64
payload, which is what leads to the couple of older test changes in
test_email.  This is a slightly backward incompatible behavior change,
but the new behavior is so much more useful than the old (you can now
*reliably* detect errors, and any program that was detecting errors by
sniffing for a base64 return from get_payload(decode=True) and then doing
its own error-recovery decode will just get the error-recovery decode
right away).  So this seems to me to be worth the small risk inherent
in this behavior change.

This patch also refactors the defect tests into a separate test file,
since they are no longer just parser tests.
2012-05-27 21:23:34 -04:00
R David Murray adbdcdbd95 #14925: email now registers a defect for missing header/body separator.
This patch also deprecates the MalformedHeaderDefect.  My best guess is that
this defect was rendered obsolete by a refactoring of the parser, and the
corresponding defect for the new parser (which this patch introduces) was
overlooked.
2012-05-27 20:45:01 -04:00
R David Murray ea9766897b Make headerregistry fully part of the provisional api.
When I made the checkin of the provisional email policy, I knew that
Address and Group needed to be made accessible from somewhere.  The more
I looked at it, though, the more it became clear that since this is a
provisional API anyway, there's no good reason to hide headerregistry as
a private API.  It was designed to ultimately be part of the public API,
and so it should be part of the provisional API.

This patch fully documents the headerregistry API, and deletes the
abbreviated version of those docs I had added to the provisional policy
docs.
2012-05-27 15:03:38 -04:00
R David Murray 36432ea783 Add '__all__' to _encoded_words and mark QByteMap as private. 2012-05-27 12:39:54 -04:00
R David Murray 032eed3c4a Recognize '<>' as a special case of an angle-addr in header_value_parser.
Although '<>' is invalid according to RFC 5322, SMTP uses it for various
things, and it sometimes ends up in email headers.  This patch changes
get_angle_addr to recognize it and just register a Defect instead of raising a
parsing error.
2012-05-26 14:31:12 -04:00
R David Murray d2d521eafd #665194: Add a localtime function to email.utils.
Without this function people would be tempted to use the other date functions
in email.utils to compute an aware localtime, and those functions are not as
good for that purpose as this code.  The code is Alexander Belopolsy's from
his proposed patch for issue 9527, with a fix (and additional tests) by Brian
K. Jones.
2012-05-25 23:22:59 -04:00
R David Murray dcaf2ece6c #12586: Fix a small oversight in the new email policy header setting code.
This is a danger of focusing on unit tests: sometimes you forget
to do the integration tests.
2012-05-25 22:53:12 -04:00
R David Murray 0b6f6c82b5 #12586: add provisional email policy with new header parsing and folding.
When the new policies are used (and only when the new policies are explicitly
used) headers turn into objects that have attributes based on their parsed
values, and can be set using objects that encapsulate the values, as well as
set directly from unicode strings.  The folding algorithm then takes care of
encoding unicode where needed, and folding according to the highest level
syntactic objects.

With this patch only date and time headers are parsed as anything other than
unstructured, but that is all the helper methods in the existing API handle.
I do plan to add more parsers, and complete the set specified in the RFC
before the package becomes stable.
2012-05-25 18:42:14 -04:00
R David Murray c27e52265b #14731: refactor email policy framework.
This patch primarily does two things: (1) it adds some internal-interface
methods to Policy that allow for Policy to control the parsing and folding of
headers in such a way that we can construct a backward compatibility policy
that is 100% compatible with the 3.2 API, while allowing a new policy to
implement the email6 API.  (2) it adds that backward compatibility policy and
refactors the test suite so that the only differences between the 3.2
test_email.py file and the 3.3 test_email.py file is some small changes in
test framework and the addition of tests for bugs fixed that apply to the 3.2
API.

There are some additional teaks, such as moving just the code needed for the
compatibility policy into _policybase, so that the library code can import
only _policybase.  That way the new code that will be added for email6
will only get imported when a non-compatibility policy is imported.
2012-05-25 15:01:48 -04:00
R David Murray 42243c4dca #14380: Make actual default match docs, fix __init__ order.
Éric pointed out that given that the default was documented as None, someone
would reasonably pass that to get the default behavior.  In fixing the code to
use None, I noticed that the change to _charset was being done after it had
already been passed to MIMENonMultipart.  The change to the test verifies that
the order is now correct.
2012-03-22 22:40:44 -04:00
R David Murray 8680bcc5db #14380: Have MIMEText defaults to utf-8 when passed non-ASCII unicode
Previously it would just accept the unicode, which would wind up as unicode in
the transfer-encoded message object, which is just wrong.

Patch by Jeff Knupp.
2012-03-22 22:17:51 -04:00
R David Murray bd53809ee7 #14344: fixed the repr of email.policy objects. 2012-03-17 15:11:59 -04:00
R David Murray 80e22b56d3 Merge #11686: add missing entries to email __all__ lists.
Original patch by Steffen Daode Nurpmeso
2012-03-16 22:46:14 -04:00
R David Murray 1b6c724df0 #11686: add missing entries to email __all__ lists.
Original patch by Steffen Daode Nurpmeso
2012-03-16 22:43:05 -04:00
R David Murray b53319f509 #12818: remove escaping of () in quoted strings in formataddr
The quoting of ()s inside quoted strings is allowed by the RFC, but is not
needed.  There seems to be no reason to add needless escapes.
2012-03-14 15:31:47 -04:00
R David Murray 8d8f110492 #14062: fix BytesParser handling of Header objects
This is a different fix than the 3.2 fix, but the new tests are the same.

This also affected smtplib.SMTP.send_message, which calls BytesParser.
2012-03-14 14:24:22 -04:00
R David Murray 9fd170e2d0 #14062: fix BytesParser handling of linesep for Header objects
This also affected smtplib.SMTP.send_message, which calls BytesParser.
2012-03-14 14:05:03 -04:00
R David Murray e2922835b0 Merge #14291: if a header has non-ascii unicode, default to CTE using utf-8
In Python2, if a unicode string was assigned as the value of a header,
email would automatically CTE encode it using the UTF8 charset.
This capability was lost in the Python3 translation, and this patch
restores it.

Patch by Ali Ikinci, assisted by R. David Murray.

I also added a fix for the mailbox test that was depending (with a comment
that it was a bad idea to so depend) on non-ASCII causing message_from_string
to raise an error.  It now uses support.patch to induce an error during
message serialization.
2012-03-14 03:03:27 -04:00
R David Murray 7441a7aedd #14291: if a header has non-ascii unicode, default to CTE using utf-8
In Python2, if a unicode string was assigned as the value of a header,
email would automatically CTE encode it using the UTF8 charset.
This capability was lost in the Python3 translation, and this patch
restores it.

Patch by Ali Ikinci, assisted by R. David Murray.

I also added a fix for the mailbox test that was depending (with a comment
that it was a bad idea to so depend) on non-ASCII causing message_from_string
to raise an error.  It now uses support.patch to induce an error during
message serialization.
2012-03-14 02:59:51 -04:00
Florent Xicluna e41f0de84d Merge 3.2, fix typos. 2011-11-11 19:39:25 +01:00
Florent Xicluna 992d9e081f Fix few typos. 2011-11-11 19:35:42 +01:00
R David Murray 875048bd4c #665194: support roundtripping RFC2822 date stamps in the email.utils module 2011-07-20 11:41:21 -04:00
R David Murray 749073af13 #1874: detect invalid multipart CTE and report it as a defect. 2011-06-22 13:47:53 -04:00
R David Murray e76ff4081a merge #11584: make Header and make_header handle binary unknown-8bit input 2011-06-18 13:02:42 -04:00
R David Murray e5e366c856 #11584: make Header and make_header handle binary unknown-8bit input
Analogous to the decode_header fix, this fix makes Header.append and
make_header correctly handle the unknown-8bit charset introduced by email5.1,
when the input to them is binary strings.  Previous to this fix the
make_header(decode_header(x)) == x invariant was broken in the face of the
unknown-8bit charset.
2011-06-18 12:57:28 -04:00
R David Murray 7df08379c6 merge #11584: make decode_header handle Header objects correctly
This updates 12e39cd7a0e4 (merge of b21fdfa0019c), which fixed this bug
incorrectly.
2011-06-18 12:32:27 -04:00
R David Murray 6bdb1769dc #11584: make decode_header handle Header objects correctly
This updates b21fdfa0019c, which fixed this bug incorrectly.
2011-06-18 12:30:55 -04:00
R David Murray 3edd22ac95 #11731: simplify/enhance parser/generator API by introducing policy objects.
This new interface will also allow for future planned enhancements
in control over the parser/generator without requiring any additional
complexity in the parser/generator API.

Patch reviewed by Éric Araujo and Barry Warsaw.
2011-04-18 13:59:37 -04:00
R David Murray f3299989a2 Merge: #11492: rewrite header folding algorithm. Less code, more passing tests. 2011-04-18 10:11:06 -04:00
R David Murray 01581ee0b7 #11492: rewrite header folding algorithm. Less code, more passing tests. 2011-04-18 10:04:34 -04:00
R David Murray a2860e8b51 Improve message.py test coverage to 100%.
coverage.py reports 99% on branch coverage, but that appears to be
a bug or limitation in coverage.py.
2011-04-16 09:20:30 -04:00
R David Murray 8437fe2708 Remove unused method from internal class. 2011-04-15 14:55:04 -04:00
R David Murray b35c850a3f #11684: Complete parser bytes interface by adding BytesHeaderParser
Patch by Steffen Daode Nurpmeso.
2011-04-13 16:46:05 -04:00
Ezio Melotti 5e610def7d Merge with 3.2. 2011-04-13 16:45:00 +03:00
Ezio Melotti a3d0be456c Merge with 3.1. 2011-04-13 16:44:18 +03:00
Ezio Melotti ce073cdac7 Fix typo in docstring. 2011-04-13 16:43:21 +03:00
R David Murray b30f1b4106 Merge: Add maxlinelen to docstring, delete obsolete wording 2011-04-12 15:02:07 -04:00
R David Murray 94f90dd4a1 Merge: Add maxlinelen to docstring, delete obsolete wording 2011-04-12 15:01:28 -04:00
R David Murray 308f14aeae Add maxlinelen to docstring, delete obsolete wording 2011-04-12 15:00:44 -04:00
R David Murray 7ede59d77a Merge #11492: fix header truncation on folding when there are runs of split chars.
Not a complete fix for this issue.
2011-04-07 21:00:33 -04:00
R David Murray 80221ed0c0 Merge #11492: fix header truncation on folding when there are runs of split chars.
Not a complete fix for this issue.
2011-04-07 20:56:31 -04:00
R David Murray e1292a25d8 #11492: fix header truncation on folding when there are runs of split chars.
Not a complete fix for this issue.
2011-04-07 20:54:03 -04:00
R David Murray b65df26830 Merge: Improve test coverage of _split_ascii method. 2011-04-07 20:40:01 -04:00
R David Murray 7da4db118e Improve test coverage of _split_ascii method. 2011-04-07 20:37:17 -04:00
R David Murray 8debacb51c #1690608: make formataddr RFC2047 aware.
Patch by Torsten Becker.
2011-04-06 09:35:57 -04:00
R David Murray a0b1c77a19 Merge #11605: don't use set/get_payload in feedparser; they do conversions. 2011-04-06 08:16:13 -04:00
R David Murray c5c1472895 #11605: don't use set/get_payload in feedparser; they do conversions.
Really the whole API needs to be gone over to restore the
separation of concerns; but that's what email6 is about.
2011-04-06 08:13:02 -04:00
R David Murray 86cc82e36f Remove the 'strict' argument to Parser, deprecated since 2.4. 2011-03-29 11:32:35 -04:00
R David Murray 961355a56a Merge #11584: Since __getitem__ returns headers, make decode_header handle them. 2011-03-25 15:31:52 -04:00
R David Murray 041015cc70 #11584: Since __getitem__ returns headers, make decode_header handle them.
Why I consider this a bug rather than an API change: the API change was
to Message, which didn't used to return Headers unless you added them
yourself.  Now it does (for 8bit binary header input), so decode_header
needs to be able to handle them.
2011-03-25 15:10:55 -04:00
R David Murray 73bd0448b9 Merge #11606: improved body_encode algorithm, no longer produces overlong lines 2011-03-24 12:28:39 -04:00
R David Murray c7d28be62f Merge #11606: improved body_encode algorithm, no longer produces overlong lines 2011-03-24 12:27:23 -04:00
R David Murray b938c8c253 #11606: improved body_encode algorithm, no longer produces overlong lines
Algorithm and initial patch by Michael Henry.
2011-03-24 12:19:26 -04:00
R David Murray 5839b9635c Merge #11590: fix quoprimime decode handling of empty strings and line endings. 2011-03-23 15:37:26 -04:00
R David Murray 523b41c4b3 Merge #11590: fix quoprimime decode handling of empty strings and line endings. 2011-03-23 15:27:04 -04:00
R David Murray cafd79d904 #11590: fix quoprimime decode handling of empty strings and line endings. 2011-03-23 15:25:55 -04:00
R David Murray 3d9e973abd Merge #11589: add additional tests for the email quoprimime module. 2011-03-23 14:27:18 -04:00
R David Murray ec1b5b88ee #11589: add additional tests for the email quoprimime module.
Patch by Michael Henry.
2011-03-23 14:19:05 -04:00
R David Murray 9aaba789b3 Correctly move email package tests to Lib/test. 2011-03-21 17:17:06 -04:00
R David Murray a215023b78 #11243: tests and fixes for handling of 'dirty data' in additional methods 2011-03-16 21:11:23 -04:00
R David Murray 4e4326829f Merge #11401 fix from 3.1. 2011-03-16 18:27:34 -04:00
R David Murray de91276a5c #11401: handle headers with no value. 2011-03-16 18:26:23 -04:00
R David Murray 78099bb153 Merge #9298 fix. 2011-03-16 16:13:07 -04:00
R David Murray 6d94bd470e #9298: restore proper folding of base64 encoded bodies.
Patch by Yves Dorfsman.
2011-03-16 15:52:22 -04:00
Ezio Melotti 3b3499ba69 #11565: Merge with 3.1. 2011-03-16 11:35:38 +02:00
Ezio Melotti 13925008dc #11565: Fix several typos. Patch by Piotr Kasprzyk. 2011-03-16 11:05:33 +02:00
R David Murray 4a0b60c527 Merge #11555 as_string doc fix from 3.1. 2011-03-15 14:02:53 -04:00
R David Murray 7dedcb4644 #11555: update doc for 3.x change to as_string mangle_from default. 2011-03-15 14:01:18 -04:00
R David Murray 106f8e3ea4 Remove obsolete comment. 2011-03-15 12:48:41 -04:00
R David Murray 56a9d7e3da #11554: reactivate test_email_codecs, and make it pass.
The fix is to charset.py, which was not doing the encoding to the
correct output character set when doing a body_encode for either
the shift-jis or euc-jp charsets.  There's also a fix for handling
a bytes input in encoders.py.

Patch by Michael Henry, comment changes by me.
2011-03-15 12:20:02 -04:00
Ezio Melotti 4969f709cc #11515: Merge with 3.1. 2011-03-15 05:59:46 +02:00
Ezio Melotti 42da663e6f #11515: fix several typos. Patch by Piotr Kasprzyk. 2011-03-15 05:18:48 +02:00
R David Murray 3a6152f3b3 Fix header encoding of long headers when using euc-jp and shift_jis.
When a header was long enough to need to be split across lines, the
input charset name was used instead of the output charset name in
the encoded words.  This make a difference only for the two charsets
above.
2011-03-14 21:13:03 -04:00
R David Murray cd37dfcfac Harmonize linesep docstrings, and fix the quoting of \r\n 2011-03-14 18:35:56 -04:00
Nick Coghlan ce4ee1f34d Merge from 3.2 2011-03-16 21:48:30 -04:00
R David Murray cfec350e63 Merge #11401 fix from 3.2. 2011-03-16 18:28:07 -04:00
R David Murray bb35299fcd Merge #9298 fix. 2011-03-16 16:14:43 -04:00
Ezio Melotti b88ed1549e #11565: Merge with 3.2. 2011-03-16 11:38:59 +02:00
R David Murray ab1dfd68e1 Merge #11555 as_string doc fix from 3.2. 2011-03-15 14:04:00 -04:00
R David Murray e1773cd3cb Merge obsolete comment removal from 3.2. 2011-03-15 12:49:33 -04:00
R David Murray df1d00a1e3 Merge #11554 test_email_codecs activation from 3.2. 2011-03-15 12:24:47 -04:00
Ezio Melotti 373089239b #11515: Merge with 3.2. 2011-03-15 06:03:08 +02:00
R David Murray 7d9129c147 Merge long Japanese header fix from 3.2. 2011-03-14 21:14:53 -04:00
R David Murray 4572572718 Merge linesep docstring changes from 3.2. 2011-03-14 18:39:41 -04:00