Skip to content

Commit 3dd954d

Browse files
committed
Don't try to decode binary data (see djc#246)
1 parent 8336362 commit 3dd954d

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

couchdb/multipart.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,6 @@ def add(self, mimetype, content, headers=None):
152152
else:
153153
content = content.encode('utf-8')
154154
mimetype = mimetype + ';charset=utf-8'
155-
elif 'charset' not in params:
156-
try:
157-
content.decode('utf-8')
158-
finally:
159-
mimetype = mimetype + ';charset=utf-8'
160155

161156
headers['Content-Type'] = mimetype
162157
if content:
@@ -206,15 +201,15 @@ def write_multipart(fileobj, subtype='mixed', boundary=None):
206201
207202
>>> buf = StringIO()
208203
>>> envelope = write_multipart(buf, boundary='==123456789==')
209-
>>> envelope.add('text/plain', 'Just testing')
204+
>>> envelope.add('text/plain', b'Just testing')
210205
>>> envelope.close()
211206
>>> print(buf.getvalue().replace(b'\r\n', b'\n').decode('utf-8'))
212207
Content-Type: multipart/mixed; boundary="==123456789=="
213208
<BLANKLINE>
214209
--==123456789==
215210
Content-Length: 12
216211
Content-MD5: nHmX4a6el41B06x2uCpglQ==
217-
Content-Type: text/plain;charset=utf-8
212+
Content-Type: text/plain
218213
<BLANKLINE>
219214
Just testing
220215
--==123456789==--
@@ -230,7 +225,7 @@ def write_multipart(fileobj, subtype='mixed', boundary=None):
230225
>>> buf = StringIO()
231226
>>> envelope = write_multipart(buf, boundary='==123456789==')
232227
>>> part = envelope.open(boundary='==abcdefghi==')
233-
>>> part.add('text/plain', 'Just testing')
228+
>>> part.add('text/plain', u'Just testing')
234229
>>> part.close()
235230
>>> envelope.close()
236231
>>> print(buf.getvalue().replace(b'\r\n', b'\n').decode('utf-8')) #:doctest +ELLIPSIS

couchdb/tests/multipart.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,25 @@ def test_unicode_headers(self):
194194
buf = StringIO()
195195
envelope = multipart.write_multipart(buf, boundary='==123456789==')
196196
envelope.add('application/json',
197+
'{"_rev": "3-bc27b6930ca514527d8954c7c43e6a09",'
198+
u' "_id": "文档"}',
199+
headers={'Content-ID': u"文档"})
200+
self.assertEqual(u'''Content-Type: multipart/mixed; boundary="==123456789=="
201+
202+
--==123456789==
203+
Content-ID: =?utf-8?b?5paH5qGj?=
204+
Content-Length: 63
205+
Content-MD5: Cpw3iC3xPua8YzKeWLzwvw==
206+
Content-Type: application/json;charset=utf-8
207+
208+
{"_rev": "3-bc27b6930ca514527d8954c7c43e6a09", "_id": "文档"}
209+
'''.encode('utf-8'), buf.getvalue().replace(b'\r\n', b'\n'))
210+
211+
def test_unicode_headers_charset(self):
212+
# http://code.google.com/p/couchdb-python/issues/detail?id=179
213+
buf = StringIO()
214+
envelope = multipart.write_multipart(buf, boundary='==123456789==')
215+
envelope.add('application/json;charset=utf-8',
197216
'{"_rev": "3-bc27b6930ca514527d8954c7c43e6a09",'
198217
' "_id": "文档"}',
199218
headers={'Content-ID': u"文档"})

0 commit comments

Comments
 (0)