Replace uses of `as_string` from the email lib.
The `as_string` function from `email.message.Message` assumes that it's being
used to encode a message for, well, email transmission -- and as a result, it
changes lines beginning with "From " into lines beginning with ">From ". Given
that we're using that module for multipart uploads, this behavior is surely
not what we want.
We basically inline the body of `as_string`, setting `mangle_from_` to
`False`.
diff --git a/googleapiclient/discovery.py b/googleapiclient/discovery.py
index e01386c..ac5feee 100644
--- a/googleapiclient/discovery.py
+++ b/googleapiclient/discovery.py
@@ -27,7 +27,9 @@
# Standard library imports
+import StringIO
import copy
+from email.generator import Generator
from email.mime.multipart import MIMEMultipart
from email.mime.nonmultipart import MIMENonMultipart
import keyword
@@ -728,7 +730,12 @@
payload = media_upload.getbytes(0, media_upload.size())
msg.set_payload(payload)
msgRoot.attach(msg)
- body = msgRoot.as_string()
+ # encode the body: note that we can't use `as_string`, because
+ # it plays games with `From ` lines.
+ fp = StringIO.StringIO()
+ g = Generator(fp, mangle_from_=False)
+ g.flatten(msgRoot, unixfrom=False)
+ body = fp.getvalue()
multipart_boundary = msgRoot.get_boundary()
headers['content-type'] = ('multipart/related; '
diff --git a/googleapiclient/http.py b/googleapiclient/http.py
index 2b65348..23940ff 100644
--- a/googleapiclient/http.py
+++ b/googleapiclient/http.py
@@ -1229,7 +1229,12 @@
msg.set_payload(body)
message.attach(msg)
- body = message.as_string()
+ # encode the body: note that we can't use `as_string`, because
+ # it plays games with `From ` lines.
+ fp = StringIO.StringIO()
+ g = Generator(fp, mangle_from_=False)
+ g.flatten(message, unixfrom=False)
+ body = fp.getvalue()
headers = {}
headers['content-type'] = ('multipart/mixed; '