Encoding problem - or bug in couchdb-0.8-py2.7.egg??

I

Ian Hobson

Hi all,

I have hit a problem and I don't know enough about python to diagnose
things further. Trying to use couchDB from Python. This script:-

# coding=utf8
import couchdb
from couchdb.client import Server
server = Server()
dbName = 'python-tests'
try:
db = server.create(dbName)
except couchdb.PreconditionFailed:
del server[dbName]
db = server.create(dbName)
doc_id, doc_rev = db.save({'type': 'Person', 'name': 'John Doe'})

Gives this traceback:-

D:\work\C-U-B>python tes1.py
Traceback (most recent call last):
File "tes1.py", line 11, in <module>
doc_id, doc_rev = db.save({'type': 'Person', 'name': 'John Doe'})
File
"C:\Python27\lib\site-packages\couchdb-0.8-py2.7.egg\couchdb\client.py",
line 407, in save
_, _, data = func(body=doc, **options)
File
"C:\Python27\lib\site-packages\couchdb-0.8-py2.7.egg\couchdb\http.py",
line 399, in post_json
status, headers, data = self.post(*a, **k)
File
"C:\Python27\lib\site-packages\couchdb-0.8-py2.7.egg\couchdb\http.py",
line 381, in post
**params)
File
"C:\Python27\lib\site-packages\couchdb-0.8-py2.7.egg\couchdb\http.py",
line 419, in _request
credentials=self.credentials)
File
"C:\Python27\lib\site-packages\couchdb-0.8-py2.7.egg\couchdb\http.py",
line 310, in request
raise ServerError((status, error))
couchdb.http.ServerError: (400, ('bad_request', 'invalid UTF-8 JSON'))

D:\work\C-U-B>

Why? I've tried adding u to the strings, and removing the # coding line,
and I still get the same error.

Thanks for any help.

Ian
 
D

Diez B. Roggisch

Ian Hobson said:
Hi all,

I have hit a problem and I don't know enough about python to diagnose
things further. Trying to use couchDB from Python. This script:-

# coding=utf8
import couchdb
from couchdb.client import Server
server = Server()
dbName = 'python-tests'
try:
db = server.create(dbName)
except couchdb.PreconditionFailed:
del server[dbName]
db = server.create(dbName)
doc_id, doc_rev = db.save({'type': 'Person', 'name': 'John Doe'})

Gives this traceback:-

D:\work\C-U-B>python tes1.py
Traceback (most recent call last):
File "tes1.py", line 11, in <module>
doc_id, doc_rev = db.save({'type': 'Person', 'name': 'John Doe'})
File
"C:\Python27\lib\site-packages\couchdb-0.8-py2.7.egg\couchdb\client.py",
line 407, in save
_, _, data = func(body=doc, **options)
File
"C:\Python27\lib\site-packages\couchdb-0.8-py2.7.egg\couchdb\http.py",
line 399, in post_json
status, headers, data = self.post(*a, **k)
File
"C:\Python27\lib\site-packages\couchdb-0.8-py2.7.egg\couchdb\http.py",
line 381, in post
**params)
File
"C:\Python27\lib\site-packages\couchdb-0.8-py2.7.egg\couchdb\http.py",
line 419, in _request
credentials=self.credentials)
File
"C:\Python27\lib\site-packages\couchdb-0.8-py2.7.egg\couchdb\http.py",
line 310, in request
raise ServerError((status, error))
couchdb.http.ServerError: (400, ('bad_request', 'invalid UTF-8 JSON'))

D:\work\C-U-B>

Why? I've tried adding u to the strings, and removing the # coding
line, and I still get the same error.

Sounds cargo-cultish. I suggest you read the python introduction on
unicode.

http://docs.python.org/howto/unicode.html

For your actual problem, I have difficulties seeing how it can happen
with the above data - frankly because there is nothing outside the
ascii-range of data, so there is no reason why anything could be wrong
encoded.

But googling the error-message reveals that there seem to be totally
unrelated reasons for this:

http://sindro.me/2010/4/3/couchdb-invalid-utf8-json

Maybe using something like tcpmon or ethereal to capture the actual
HTTP-request helps to see where the issue comes from.

Diez
 
I

Ian

Thanks Diez,

Removing, rebooting and installing the latest version solved the
problem. :)

Your google-foo is better than mine. Google had not turned that up for me.

Thanks again

Regards

Ian



Ian Hobson said:
Hi all,

I have hit a problem and I don't know enough about python to diagnose
things further. Trying to use couchDB from Python. This script:-

# coding=utf8
import couchdb
from couchdb.client import Server
server = Server()
dbName = 'python-tests'
try:
db = server.create(dbName)
except couchdb.PreconditionFailed:
del server[dbName]
db = server.create(dbName)
doc_id, doc_rev = db.save({'type': 'Person', 'name': 'John Doe'})

Gives this traceback:-

D:\work\C-U-B>python tes1.py
Traceback (most recent call last):
File "tes1.py", line 11, in<module>
doc_id, doc_rev = db.save({'type': 'Person', 'name': 'John Doe'})
File
"C:\Python27\lib\site-packages\couchdb-0.8-py2.7.egg\couchdb\client.py",
line 407, in save
_, _, data = func(body=doc, **options)
File
"C:\Python27\lib\site-packages\couchdb-0.8-py2.7.egg\couchdb\http.py",
line 399, in post_json
status, headers, data = self.post(*a, **k)
File
"C:\Python27\lib\site-packages\couchdb-0.8-py2.7.egg\couchdb\http.py",
line 381, in post
**params)
File
"C:\Python27\lib\site-packages\couchdb-0.8-py2.7.egg\couchdb\http.py",
line 419, in _request
credentials=self.credentials)
File
"C:\Python27\lib\site-packages\couchdb-0.8-py2.7.egg\couchdb\http.py",
line 310, in request
raise ServerError((status, error))
couchdb.http.ServerError: (400, ('bad_request', 'invalid UTF-8 JSON'))

D:\work\C-U-B>

Why? I've tried adding u to the strings, and removing the # coding
line, and I still get the same error.
Sounds cargo-cultish. I suggest you read the python introduction on
unicode.

http://docs.python.org/howto/unicode.html

For your actual problem, I have difficulties seeing how it can happen
with the above data - frankly because there is nothing outside the
ascii-range of data, so there is no reason why anything could be wrong
encoded. I came to the same conclusion.
But googling the error-message reveals that there seem to be totally
unrelated reasons for this:

http://sindro.me/2010/4/3/couchdb-invalid-utf8-json

Maybe using something like tcpmon or ethereal to capture the actual
HTTP-request helps to see where the issue comes from.

Diez
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,690
Latest member
MacGyver

Latest Threads

Top