|
28 | 28 | 'key2param', |
29 | 29 | ] |
30 | 30 |
|
31 | | -from six import StringIO |
| 31 | +from six import BytesIO |
32 | 32 | from six.moves import http_client |
33 | 33 | from six.moves.urllib.parse import urlencode, urlparse, urljoin, \ |
34 | 34 | urlunparse, parse_qsl |
35 | 35 |
|
36 | 36 | # Standard library imports |
37 | 37 | import copy |
38 | | -from email.generator import Generator |
| 38 | +try: |
| 39 | + from email.generator import BytesGenerator |
| 40 | +except ImportError: |
| 41 | + from email.generator import Generator as BytesGenerator |
39 | 42 | from email.mime.multipart import MIMEMultipart |
40 | 43 | from email.mime.nonmultipart import MIMENonMultipart |
41 | 44 | import json |
|
102 | 105 | # Library-specific reserved words beyond Python keywords. |
103 | 106 | RESERVED_WORDS = frozenset(['body']) |
104 | 107 |
|
| 108 | +# patch _write_lines to avoid munging '\r' into '\n' |
| 109 | +# ( https://bugs.python.org/issue18886 https://bugs.python.org/issue19003 ) |
| 110 | +class _BytesGenerator(BytesGenerator): |
| 111 | + _write_lines = BytesGenerator.write |
105 | 112 |
|
106 | 113 | def fix_method_name(name): |
107 | 114 | """Fix method names to avoid reserved word conflicts. |
@@ -797,8 +804,8 @@ def method(self, **kwargs): |
797 | 804 | msgRoot.attach(msg) |
798 | 805 | # encode the body: note that we can't use `as_string`, because |
799 | 806 | # it plays games with `From ` lines. |
800 | | - fp = StringIO() |
801 | | - g = Generator(fp, mangle_from_=False) |
| 807 | + fp = BytesIO() |
| 808 | + g = _BytesGenerator(fp, mangle_from_=False) |
802 | 809 | g.flatten(msgRoot, unixfrom=False) |
803 | 810 | body = fp.getvalue() |
804 | 811 |
|
|
0 commit comments