Opened 13 years ago
Closed 13 years ago
#18461 closed Bug (fixed)
UnicodeDecodeError in sql logger
| Reported by: | zw0rk | Owned by: | nobody | 
|---|---|---|---|
| Component: | Core (Other) | Version: | dev | 
| Severity: | Normal | Keywords: | |
| Cc: | charette.s@… | Triage Stage: | Accepted | 
| Has patch: | yes | Needs documentation: | no | 
| Needs tests: | no | Patch needs improvement: | no | 
| Easy pickings: | no | UI/UX: | no | 
Description
Trying to filter a queryset with unicode values causes UnicodeDecodeError in sql logger.
>>> User.objects.filter(last_name=u'Z')
[]
>>> User.objects.filter(last_name=u'й')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/lib/python2.7/site-packages/django/db/models/query.py", line 74, in __repr__
    data = list(self[:REPR_OUTPUT_SIZE + 1])
  File "/usr/lib/python2.7/site-packages/django/db/models/query.py", line 89, in __len__
    self._result_cache.extend(self._iter)
  File "/usr/lib/python2.7/site-packages/django/db/models/query.py", line 294, in iterator
    for row in compiler.results_iter():
  File "/usr/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 764, in results_iter
    for rows in self.execute_sql(MULTI):
  File "/usr/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 819, in execute_sql
    cursor.execute(sql, params)
  File "/usr/lib/python2.7/site-packages/django/db/backends/util.py", line 51, in execute
    logger.debug('(%.3f) %s; args=%s' % (duration, sql, params),
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd0 in position 850: ordinal not in range(128)
This happens even right after syncdb, when django tries to create admin_permission objects and models have unicode verbose_name's.
No custom loggers were installed in settings.py.
Attachments (1)
Change History (10)
comment:1 by , 13 years ago
| Has patch: | set | 
|---|---|
| Triage Stage: | Unreviewed → Accepted | 
comment:2 by , 13 years ago
| Cc: | added | 
|---|
I've got bit by that using master and trying to load fixtures with non-ascii character.
comment:3 by , 13 years ago
| Resolution: | → fixed | 
|---|---|
| Status: | new → closed | 
comment:5 by , 13 years ago
For future reference: the problem with the test was that it used .filter(name='й'). This caused problems on MySQL when TEST_CHARSET = 'UTF8' is not in use. I didn't have that, and didn't spot that this is required (documented here). So, the original test was correct. I don't see a need to change the test back, as the removed .filter() doesn't make the test incorrect either. Of course, I am not objecting changing the test back if that is wanted...
comment:6 by , 13 years ago
This is doesn't work when you have connection not in utf8 (for example in mysql OPTIONS' : {"charset": "cp1251"})
'utf8' codec can't decode byte 0xcd in position 401: invalid continuation byte
comment:7 by , 13 years ago
| Resolution: | fixed | 
|---|---|
| Status: | closed → new | 
comment:8 by , 13 years ago
<         encoding = cursor.connection.character_set_name()
<         return cursor._last_executed.decode(encoding)
---
>         return cursor._last_executed.decode('utf-8')
I guess that it will help
comment:9 by , 13 years ago
| Resolution: | → fixed | 
|---|---|
| Status: | new → closed | 
Please reopen a new ticket with your specific issue.
It seems that at least PostgreSQL and MySQL are returning their last executed query as an 'utf-8' encoded byte string. Needs confirmation by ORM guys, and check with Oracle.