Skip to content

Commit fb0bb99

Browse files
committed
Make sure the output from the view server is in utf-8 (issue 81).
1 parent 0bc3d9a commit fb0bb99

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

ChangeLog.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Version 0.7
1+
Version 0.7.0
22
http://couchdb-python.googlecode.com/hg/?r=default
33
(???, from branches/0.7.x)
44

@@ -17,6 +17,7 @@ http://couchdb-python.googlecode.com/hg/?r=default
1717
* `schema.ListField` objects now have correct behavior for slicing operations
1818
and the `pop()` method (issue 92).
1919
* Added a `revisions()` method to the Database class (issue 99).
20+
* Make sure we always return UTF-8 from the view server (issue 81).
2021

2122

2223
Version 0.6

couchdb/tests/view.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ def test_map_doc(self):
3636
'true\n'
3737
'[[[null, {"foo": "bar"}]]]\n')
3838

39+
def test_i18n(self):
40+
input = StringIO('["add_fun", "def fun(doc): yield doc[\\"test\\"], doc"]\n'
41+
'["map_doc", {"test": "b\xc3\xa5r"}]\n')
42+
output = StringIO()
43+
view.run(input=input, output=output)
44+
self.assertEqual(output.getvalue(),
45+
'true\n'
46+
'[[["b\xc3\xa5r", {"test": "b\xc3\xa5r"}]]]\n')
47+
3948
def test_map_doc_with_logging(self):
4049
fun = 'def fun(doc): log(\'running\'); yield None, doc'
4150
input = StringIO('["add_fun", "%s"]\n'

couchdb/view.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,18 @@ def run(input=sys.stdin, output=sys.stdout):
3232
"""
3333
functions = []
3434

35+
def _writejson(obj):
36+
obj = json.encode(obj)
37+
if isinstance(obj, unicode):
38+
obj = obj.encode('utf-8')
39+
output.write(obj)
40+
output.write('\n')
41+
output.flush()
42+
3543
def _log(message):
3644
if not isinstance(message, basestring):
3745
message = json.encode(message)
38-
output.write(json.encode({'log': message}))
39-
output.write('\n')
40-
output.flush()
46+
_writejson({'log': message})
4147

4248
def reset(config=None):
4349
del functions[:]
@@ -135,9 +141,7 @@ def rereduce(*cmd):
135141
else:
136142
retval = handlers[cmd[0]](*cmd[1:])
137143
log.debug('Returning %r', retval)
138-
output.write(json.encode(retval))
139-
output.write('\n')
140-
output.flush()
144+
_writejson(retval)
141145
except KeyboardInterrupt:
142146
return 0
143147
except Exception, e:

0 commit comments

Comments
 (0)