trytond-patches/issue9801002_40001.diff
2015-05-15 10:42:04 +02:00

85 lines
2.5 KiB
Diff

Index: trytond/trytond/tests/test_tools.py
===================================================================
--- a/trytond/trytond/tests/test_tools.py
+++ b/trytond/tests/test_tools.py
@@ -3,12 +3,13 @@
#this repository contains the full copyright notices and license terms.
import unittest
+import doctest
import datetime
import sql
import sql.operators
from trytond.tools import reduce_ids, safe_eval, datetime_strftime, \
- reduce_domain
+ reduce_domain, decimal_
class ToolsTestCase(unittest.TestCase):
@@ -117,4 +118,5 @@
suite = unittest.TestSuite()
for testcase in (ToolsTestCase,):
suite.addTests(func(testcase))
+ suite.addTest(doctest.DocTestSuite(decimal_))
return suite
Index: trytond/trytond/tools/__init__.py
===================================================================
--- a/trytond/trytond/tools/__init__.py
+++ b/trytond/trytond/tools/__init__.py
@@ -2,6 +2,7 @@
#this repository contains the full copyright notices and license terms.
from .misc import *
from .datetime_strftime import *
+from .decimal_ import *
class ClassProperty(property):
Index: trytond/trytond/tools/decimal_.py
===================================================================
new file mode 100644
--- /dev/null
+++ b/trytond/trytond/tools/decimal_.py
@@ -0,0 +1,36 @@
+# This file is part of Tryton. The COPYRIGHT file at the top level of
+# this repository contains the full copyright notices and license terms.
+import tokenize
+from io import StringIO
+
+# code snippet taken from http://docs.python.org/library/tokenize.html
+
+
+def decistmt(s):
+ """Substitute Decimals for floats in a string of statements.
+
+ >>> from decimal import Decimal
+ >>> s = 'print +21.3e-5*-.1234/81.7'
+ >>> decistmt(s)
+ u"print +Decimal (u'21.3e-5')*-Decimal (u'.1234')/Decimal (u'81.7')"
+
+ >>> exec(s)
+ -3.21716034272e-07
+ >>> exec(decistmt(s))
+ -3.217160342717258261933904529E-7
+ """
+ result = []
+ # tokenize the string
+ g = tokenize.generate_tokens(StringIO(s.decode('utf-8')).readline)
+ for toknum, tokval, _, _, _ in g:
+ # replace NUMBER tokens
+ if toknum == tokenize.NUMBER and '.' in tokval:
+ result.extend([
+ (tokenize.NAME, 'Decimal'),
+ (tokenize.OP, '('),
+ (tokenize.STRING, repr(tokval)),
+ (tokenize.OP, ')')
+ ])
+ else:
+ result.append((toknum, tokval))
+ return tokenize.untokenize(result)