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

Skip to content

Commit ab01cb9

Browse files
committed
Merge pull request googleapis#18 from craigcitro/from_encoding
Replace uses of `as_string` from the email lib.
2 parents 9a2db63 + 72389b7 commit ab01cb9

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

googleapiclient/discovery.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727

2828

2929
# Standard library imports
30+
import StringIO
3031
import copy
32+
from email.generator import Generator
3133
from email.mime.multipart import MIMEMultipart
3234
from email.mime.nonmultipart import MIMENonMultipart
3335
import keyword
@@ -728,7 +730,12 @@ def method(self, **kwargs):
728730
payload = media_upload.getbytes(0, media_upload.size())
729731
msg.set_payload(payload)
730732
msgRoot.attach(msg)
731-
body = msgRoot.as_string()
733+
# encode the body: note that we can't use `as_string`, because
734+
# it plays games with `From ` lines.
735+
fp = StringIO.StringIO()
736+
g = Generator(fp, mangle_from_=False)
737+
g.flatten(msgRoot, unixfrom=False)
738+
body = fp.getvalue()
732739

733740
multipart_boundary = msgRoot.get_boundary()
734741
headers['content-type'] = ('multipart/related; '

googleapiclient/http.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1229,7 +1229,12 @@ def _execute(self, http, order, requests):
12291229
msg.set_payload(body)
12301230
message.attach(msg)
12311231

1232-
body = message.as_string()
1232+
# encode the body: note that we can't use `as_string`, because
1233+
# it plays games with `From ` lines.
1234+
fp = StringIO.StringIO()
1235+
g = Generator(fp, mangle_from_=False)
1236+
g.flatten(message, unixfrom=False)
1237+
body = fp.getvalue()
12331238

12341239
headers = {}
12351240
headers['content-type'] = ('multipart/mixed; '

0 commit comments

Comments
 (0)