[Django] #10933: Avoid " TypeError: Cannot convert Decimal("0.0000") to Decimal " when the decimal module has been reloaded

23 views
Skip to first unread message

Django

unread,
Apr 27, 2009, 5:54:32 AM4/27/09
to djang...@holovaty.com, django-...@googlegroups.com
#10933: Avoid " TypeError: Cannot convert Decimal("0.0000") to Decimal " when the
decimal module has been reloaded
------------------------------------------+---------------------------------
Reporter: gagravarr | Owner: nobody
Status: new | Milestone:
Component: Database layer (models, ORM) | Version: 1.0
Keywords: | Stage: Unreviewed
Has_patch: 1 |
------------------------------------------+---------------------------------
If for some reason the decimal module gets reloaded (seems fairly easy to
trigger when using runserver, but we've seen it once or twice wiht apache
+ mod_python too), then calling db_obj.save() on a model with a decimal
field will blow up (see http://groups.google.com/group/django-
users/browse_thread/thread/7da92d7f5d6e2a53 for example)

One workaround is to have extra code in the decimal field logic in django,
to detect when the value is no longer being recognised as a Decimal but is
one, and port it over to the new decimal object.

{{{
--- django/db/models/fields/__init__.py (revision 9643)
+++ django/db/models/fields/__init__.py (working copy)
@@ -579,6 +579,11 @@
def to_python(self, value):
if value is None:
return value
+ # Work around reload(decimal) problems
+ if not isinstance(value, decimal.Decimal) and \
+ len(str(value.__class__).split("'")) == 3 and \
+ str(value.__class__).split("'")[1] == 'decimal.Decimal':
+ return decimal.Decimal( value.to_eng_string() )
try:
return decimal.Decimal(value)
except decimal.InvalidOperation:
}}}

I'm not sure if this is an ideal fix or not, but it'd be great to have
this (or something like it) in django to help avoid the issue

--
Ticket URL: <http://code.djangoproject.com/ticket/10933>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
May 17, 2009, 6:18:30 PM5/17/09
to djang...@holovaty.com, django-...@googlegroups.com
#10933: Avoid " TypeError: Cannot convert Decimal("0.0000") to Decimal " when the
decimal module has been reloaded
---------------------------------------------------+------------------------
Reporter: gagravarr | Owner: nobody
Status: new | Milestone:
Component: Database layer (models, ORM) | Version: 1.0
Resolution: | Keywords:
Stage: Unreviewed | Has_patch: 1
Needs_docs: 0 | Needs_tests: 0
Needs_better_patch: 0 |
---------------------------------------------------+------------------------
Changes (by orzel):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0

Comment:

hello. I'm hit by this same problem. I'm using django trunk (mainly
because I hoped this bug was fixed), and python 2.6.2.

--
Ticket URL: <http://code.djangoproject.com/ticket/10933#comment:1>

Django

unread,
Sep 11, 2009, 7:40:25 AM9/11/09
to djang...@holovaty.com, django-...@googlegroups.com
#10933: Avoid " TypeError: Cannot convert Decimal("0.0000") to Decimal " when the
decimal module has been reloaded
---------------------------------------------------+------------------------
Reporter: gagravarr | Owner: nobody
Status: new | Milestone:
Component: Database layer (models, ORM) | Version: 1.0
Resolution: | Keywords:
Stage: Unreviewed | Has_patch: 1
Needs_docs: 0 | Needs_tests: 0
Needs_better_patch: 0 |
---------------------------------------------------+------------------------
Comment (by lukeplant):

If you could reproduce the error using a reload() statement in a test
(probably put it in regressiontests/model_fields/tests.py), that would be
great. After that we could decide on a fix. The fix above is probably
OK. I'd really like to know why 'decimal' is being reloaded, but given
that this is causing genuine problems for people, it's better to fix it
than leave it until we know the root cause.

--
Ticket URL: <http://code.djangoproject.com/ticket/10933#comment:2>

Django

unread,
Dec 22, 2009, 3:33:35 PM12/22/09
to djang...@holovaty.com, django-...@googlegroups.com
#10933: Avoid " TypeError: Cannot convert Decimal("0.0000") to Decimal " when the
decimal module has been reloaded
---------------------------------------------------+------------------------
Reporter: gagravarr | Owner: nobody
Status: new | Milestone:
Component: Database layer (models, ORM) | Version: 1.0
Resolution: | Keywords:
Stage: Accepted | Has_patch: 1
Needs_docs: 0 | Needs_tests: 1
Needs_better_patch: 0 |
---------------------------------------------------+------------------------
Changes (by SmileyChris):

* needs_tests: 0 => 1
* stage: Unreviewed => Accepted

--
Ticket URL: <http://code.djangoproject.com/ticket/10933#comment:3>

Django

unread,
Jan 12, 2010, 10:58:48 AM1/12/10
to djang...@holovaty.com, django-...@googlegroups.com
#10933: Avoid " TypeError: Cannot convert Decimal("0.0000") to Decimal " when the
decimal module has been reloaded
---------------------------------------------------+------------------------
Reporter: gagravarr | Owner: nobody
Status: new | Milestone:
Component: Database layer (models, ORM) | Version: SVN
Resolution: | Keywords:
Stage: Accepted | Has_patch: 1
Needs_docs: 0 | Needs_tests: 1
Needs_better_patch: 0 |
---------------------------------------------------+------------------------
Changes (by orzel):

* version: 1.0 => SVN

Comment:

Hello. Just to confirm that the bug (really annoying if you have 'send
error mail' on) in django trunk as of today, although it's really, really
worse. If i disable the above patch (that i'm using for very long), i got
such a mail for almost every save(). I think it happenned less often in
may.
I'm using apache + mod_python.
The updated patch is :


{{{
--- a/django/db/models/fields/__init__.py Tue Jan 12 16:33:40 2010
+0100
+++ b/django/db/models/fields/__init__.py Tue Jan 12 16:57:48 2010
+0100
@@ -761,6 +761,11 @@
def to_python(self, value):
if value is None:
return value
+ # Work around reload(decimal) problems
+ if not isinstance(value, decimal.Decimal) and \
+ len(str(value.__class__).split("'")) == 3 and \
+ str(value.__class__).split("'")[1] == 'decimal.Decimal':
+ return decimal.Decimal( value.to_eng_string() )
try:
return decimal.Decimal(value)
except decimal.InvalidOperation:

}}}

--
Ticket URL: <http://code.djangoproject.com/ticket/10933#comment:4>

Django

unread,
Apr 29, 2010, 1:11:57 PM4/29/10
to djang...@holovaty.com, django-...@googlegroups.com
#10933: Avoid " TypeError: Cannot convert Decimal("0.0000") to Decimal " when the
decimal module has been reloaded
---------------------------------------------------+------------------------
Reporter: gagravarr | Owner: nobody
Status: new | Milestone:
Component: Database layer (models, ORM) | Version: SVN
Resolution: | Keywords:
Stage: Accepted | Has_patch: 1
Needs_docs: 0 | Needs_tests: 1
Needs_better_patch: 0 |
---------------------------------------------------+------------------------
Comment (by hotani):

I'm having this problem with latest SVN (13053). The occasional errors are
e-mailed to me and I am unable to reproduce. I'm going to try applying the
patch above and seeing if that helps.

Using apache/mod_wsgi

--
Ticket URL: <http://code.djangoproject.com/ticket/10933#comment:5>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

--
You received this message because you are subscribed to the Google Groups "Django updates" group.
To post to this group, send email to django-...@googlegroups.com.
To unsubscribe from this group, send email to django-update...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-updates?hl=en.

Django

unread,
Mar 5, 2011, 5:25:46 AM3/5/11
to djang...@holovaty.com, django-...@googlegroups.com
#10933: Avoid " TypeError: Cannot convert Decimal("0.0000") to Decimal " when the
decimal module has been reloaded
-------------------------------------+-------------------------------------
Reporter: gagravarr | Owner: nobody
Status: new | Milestone:
Component: Database | Version: SVN
layer (models, ORM) | Keywords:
Resolution: | Has patch: 1
Triage Stage: Accepted | Needs tests: 1
Needs documentation: 0 |
Patch needs improvement: 0 |
-------------------------------------+-------------------------------------

Comment (by rikwade):

I believe I am encountering this problem with my Django application. Last
couple of lines of Django debug:

{{{
[...]
File "/usr/lib/python2.5/decimal.py", line 656, in __new__
raise TypeError("Cannot convert %r to Decimal" % value)

TypeError: Cannot convert Decimal("10.5") to Decimal
}}}

I am running Django 1.2.5 with Apache 2 and WSGI. A code fix or
documentation note with supported workaround would be appreciated.

--
Ticket URL: <http://code.djangoproject.com/ticket/10933#comment:6>

Reply all
Reply to author
Forward
0 new messages