FixedPointField: bug report and fix
Tamas Decsi <[email protected]> Wed, 21 Jun 2006 17:03:20 +0200 (CEST)
| Newsgroups | gmane.comp.web.zope.plone.archetypes.devel |
|---|---|
| Message-ID | <[email protected]> |
Hello, please find attached a patch for FixedPointField bug where values between -1 and 0 got stored without sign. Basically the source of the problem is that int(-0) = 0, even though the separately handled fraction is nonzero. The patch quality is just as good as the original implementation. :) Best, Tamas Decsi Pressflex _______________________________________________ Archetypes-devel mailing list Archetypes-devel-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org https://lists.sourceforge.net/lists/listinfo/archetypes-devel
fixedpointfield-patch
(text/plain, 1.6 KB)
diff -Naur Archetypes/Field.py Archetypes-fixed/Field.py
--- Archetypes/Field.py 2006-06-21 16:50:24.000000000 +0200
+++ Archetypes-fixed/Field.py 2006-06-21 16:51:55.000000000 +0200
@@ -1481,14 +1481,14 @@
value = value.split('.')
__traceback_info__ = (self, value)
if len(value) < 2:
- value = (int(value[0]), 0)
+ value = (['','-'][value[0][0]=='-'], abs(int(value[0])), 0)
else:
fra = value[1][:self.precision]
fra += '0' * (self.precision - len(fra))
#handle leading comma e.g. .36
if value[0]=='':
value[0]='0'
- value = (int(value[0]), int(fra))
+ value = (['','-'][value[0][0]=='-'], abs(int(value[0])), int(fra))
return value
security.declarePrivate('set')
@@ -1498,7 +1498,7 @@
security.declarePrivate('get')
def get(self, instance, **kwargs):
- template = '%%d.%%0%dd' % self.precision
+ template = '%%s%%d.%%0%dd' % self.precision
value = ObjectField.get(self, instance, **kwargs)
__traceback_info__ = (template, value)
if value is None: return self.getDefault(instance)
@@ -1507,9 +1507,10 @@
security.declarePrivate('validate_required')
def validate_required(self, instance, value, errors):
- value = sum(self._to_tuple(instance, value))
+ value = sum(self._to_tuple(instance, value)[1:])
return ObjectField.validate_required(self, instance, value, errors)
+
class ReferenceField(ObjectField):
"""A field for creating references between objects.