Opened 16 years ago

Last modified 14 years ago

#6783 closed

DecimalField tests with locale — at Version 6

Reported by: Dirk Datzert <dummy@…> Owned by: Philippe Raoult
Component: Internationalization Version: dev
Severity: Keywords: i18n-fixed
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description (last modified by Ramiro Morales)

I ran into an python locale issue with the DecimalField. During INSERTs and UPDATEs invalid sql-statements are generated since a comma-seperator ',' is used for formating DecimalField instead of the expected dot-seperator '.'

decimal-python.py (for simple testing):

from django.db.models.fields import DecimalField
from locale import setlocale, LC_NUMERIC
d = DecimalField(max_digits=6, decimal_places=3)
setlocale(LC_NUMERIC,'de_DE')
print d.format_number(3.456)
setlocale(LC_NUMERIC,'en_US')
print d.format_number(3.456)

linux-f426d:/home/admin/django/projects # export DJANGO_SETTINGS_MODULE=ais.settings
linux-f426d:/home/admin/django/projects # python decimal-python.py              
3,456
3.456

Environment:

  • Django-SVN
  • SuSE Linux Enterprise Server 10
  • Python 2.4.2

On my private notebook this is no issue both outputs will be

3.456
3.456

Private Environment:

  • Django-SVN
  • OpenSuSe 10.3
  • Python 2.5.1

Change History (11)

by Dirk Datzert <dummy@…>, 16 years ago

short work-around for DecimalField format problems in db_prep methods

by Dirk Datzert <dummy@…>, 16 years ago

short work-around for DecimalField format problems in db_prep methods

by Philippe Raoult, 16 years ago

Attachment: 6783_tests.diff added

base tests for the issue

comment:1 by Philippe Raoult, 16 years ago

my test machine doesn't have the appropriate locales but I'll run the tests tomorrow.

by Philippe Raoult, 16 years ago

Attachment: 6783_tests.2.diff added

slightly better tests

comment:2 by Philippe Raoult, 16 years ago

Summary: DecimalField python locale issueDecimalField tests with locale
Triage Stage: UnreviewedReady for checkin

The tests all pass with sqlite and mysql. I tried with both python 2.4.4 and 2.5.1. I'm marking ready for checking so we can commit the regression tests and hopefully get those run with other backends/versions.

Please note that the tests need the 'fr_FR' locale to run. If it's not available the test will be omitted (no failure though). If anyone has a more elegant solution I'd be happy to update the patch :)

comment:3 by Philippe Raoult, 16 years ago

Owner: changed from nobody to Philippe Raoult

comment:4 by Malcolm Tredinnick, 16 years ago

Patch needs improvement: set
Triage Stage: Ready for checkinAccepted

Two problems here:

  1. On a triage level, the test changes should have been part of the main patch. It took a while to realise that this "ready for checkin" thing was actually the second and the fourth patch.
  2. More importantly, the solution isn't correct. setlocale() cannot be used in multi-threaded programs, since it changes the global locale, so it will affect the operation of other threads.

by yazzgoth, 16 years ago

Attachment: simple_patch.diff added

comment:5 by yazzgoth, 16 years ago

I'm sorry to hijack this ticket.

I've uploaded a simple_patch.diff that should is a quick and dirty way of sorting this out.

comment:6 by Ramiro Morales, 16 years ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top