New issue 209 by emmanuel...@gmail.com: couchdb-python doesn't support
locales
http://code.google.com/p/couchdb-python/issues/detail?id=209
The couchdb-python cache system is based on the RFC 2822 Date system for
object expiration. When the locale changes, strptime() is no more able to
parse this kind of date.
What steps will reproduce the problem?
1. Run this (or see attachment):
import locale
locale.setlocale(locale.LC_ALL, '')
import couchdb
import couchdb.http
# disable cache. 0 means force cache validation
# on every request.
couchdb.http.CACHE_SIZE = 10, 0
couch = couchdb.Server()
try:
db = couch.create('test')
db['foo'] = {'bar': 'test'}
print db['foo']
finally:
couch.delete('test')
What is the expected output? What do you see instead?
Expected:
<Document u'foo'@u'1-1d1b1ba13d97badec02a57adb942cd42' {u'bar': u'test'}>
Instead:
Traceback (most recent call last):
File "plop.py", line 15, in <module>
print db['foo']
File "c:\Pyramid\lib\site-packages\couchdb\client.py", line 322, in
__getitem__
_, _, data = self.resource.get_json(id)
File "c:\Pyramid\lib\site-packages\couchdb\http.py", line 393, in get_json
status, headers, data = self.get(*a, **k)
File "c:\Pyramid\lib\site-packages\couchdb\http.py", line 374, in get
return self._request('GET', path, headers=headers, **params)
File "c:\Pyramid\lib\site-packages\couchdb\http.py", line 419, in _request
credentials=self.credentials)
File "c:\Pyramid\lib\site-packages\couchdb\http.py", line 316, in request
self._clean_cache()
File "c:\Pyramid\lib\site-packages\couchdb\http.py", line 324, in
_clean_cache
ls = sorted(self.cache.iteritems(), key=cache_sort)
File "c:\Pyramid\lib\site-packages\couchdb\http.py", line 84, in
cache_sort
t =
time.mktime(time.strptime(i[1][1]['Date'][5:-4], '%d %b %Y %H:%M:%S'))
File "c:\Python27\Lib\_strptime.py", line 454, in _strptime_time
return _strptime(data_string, format)[0]
File "c:\Python27\Lib\_strptime.py", line 325, in _strptime
(data_string, format))
ValueError: time data '21 Feb 2012 15:18:38' does not match
format '%d %b %Y %H:%M:%S'
What version of the product are you using? On what operating system?
- CouchDB-0.8-py2.7
- WinXP32 SP3
Please provide any additional information below.
IMHO the best solution is to remove the month part of the date (so as to
only have numbers which should be locale independant) before passing it to
strptime() (and to adjust the parse pattern accordingly)
Attachments:
plop.py 351 bytes
Looks like windows-only bug or I couldn't reproduce it on Linux system, but
could on Windows one.
There are two options: wait for @Matt http module and cache system
refactoring or quick fix current behavior. I'm for first one.
Something is missing: the default locale, or the choosen locale shouldn't
be in english. Since the locale name is OS dependant (fr_FR on Linux,
French_France on Windows), I didn't include it in the example.
This is important note, thanks!
# Quick fix for your example:
import time
from email.Utils import parsedate
from datetime import datetime
def cache_sort(i):
t = time.mktime(parsedate(i[1][1]['Date']))
return datetime.fromtimestamp(t)
# And monkey-patch cache_sort function:
couchdb.http.cache_sort = cache_sort
This should work! At least for me with ('ru_RU', 'cp1251') locale info.
I'll prepare patch and tests later.