New issue 210 by lee.w...@gmail.com: Database.save() with a Python
datetime.datetime value raises the wrong exception
http://code.google.com/p/couchdb-python/issues/detail?id=210
What steps will reproduce the problem?
>> import datetime
>> import couchdb
>> couch = couchdb.Server()
>> db = couch.create('datetime_error')
>> data = {'the_time': datetime.datetime.now() }
>> db.save(data)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\dev\python27\lib\site-packages\couchdb\client.py", line 407, in
save
_, _, data = func(body=doc, **options)
File "C:\dev\python27\lib\site-packages\couchdb\http.py", line 399, in
post_json
status, headers, data = self.post(*a, **k)
File "C:\dev\python27\lib\site-packages\couchdb\http.py", line 381, in
post
**params)
File "C:\dev\python27\lib\site-packages\couchdb\http.py", line 419, in
_request
credentials=self.credentials)
File "C:\dev\python27\lib\site-packages\couchdb\http.py", line 239, in
request
resp = _try_request_with_retries(iter(self.retry_delays))
File "C:\dev\python27\lib\site-packages\couchdb\http.py", line 196, in
_try_request_with_retries
return _try_request()
File "C:\dev\python27\lib\site-packages\couchdb\http.py", line 222, in
_try_request
chunk = body.read(CHUNK_SIZE)
AttributeError: 'dict' object has no attribute 'read'
What is the expected output? What do you see instead?
Expect the real error to be raised:
>>> import json
>>> json.dumps(data)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\dev\python27\lib\json\__init__.py", line 231, in dumps
return _default_encoder.encode(obj)
File "C:\dev\python27\lib\json\encoder.py", line 201, in encode
chunks = self.iterencode(o, _one_shot=True)
File "C:\dev\python27\lib\json\encoder.py", line 264, in iterencode
return _iterencode(o, 0)
File "C:\dev\python27\lib\json\encoder.py", line 178, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: datetime.datetime(2012, 4, 12, 21, 18, 42, 696000) is not JSON
serializable
Exception is swallowed in http.py request method which has the following:
try:
body = json.encode(body).encode('utf-8')
except TypeError, e:
pass
What version of the product are you using? On what operating system?
* Windows 7 Enterprise
* CouchDB 1.2.0
* couchdb-python 0.8
Please provide any additional information below.
Not sure if it affects other JSON implementation. I'm using the standard
python library version.