Skip to content

Commit 918d68a

Browse files
committed
[FIX] handle the '/' operator when evaluating expression
The `__div__` / `__rdiv__` methods are only supported by python2. closes #67 Signed-off-by: Christophe Simonis (chs) <[email protected]>
1 parent 0c9ef1f commit 918d68a

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/base/tests/test_util.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
from odoo.osv.expression import FALSE_LEAF, TRUE_LEAF
1616
from odoo.tools import mute_logger
17+
from odoo.tools.safe_eval import safe_eval
1718

1819
from odoo.addons.base.maintenance.migrations import util
1920
from odoo.addons.base.maintenance.migrations.testing import UnitTestCase, parametrize
@@ -1021,6 +1022,24 @@ def test_expand_braces_failure(self, value):
10211022
with self.assertRaises(ValueError):
10221023
util.expand_braces(value)
10231024

1025+
@parametrize(
1026+
[
1027+
(value,)
1028+
for value in (
1029+
[
1030+
"a",
1031+
"a.b" "a.b()",
1032+
"a.b(c)",
1033+
]
1034+
+ ["a {} 4".format(op) for op in ["+", "-", "*", "/", "//", "%"]]
1035+
+ ["4 {} b".format(op) for op in ["+", "-", "*", "/", "//", "%"]]
1036+
)
1037+
]
1038+
)
1039+
def test_SelfPrint(self, value):
1040+
evaluated = safe_eval(value, util.SelfPrintEvalContext(), nocopy=True)
1041+
self.assertEqual(str(evaluated), value)
1042+
10241043

10251044
class TestQueryFormat(UnitTestCase):
10261045
@parametrize(

src/util/misc.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,12 @@ def __div__(self, other):
303303
def __rdiv__(self, other):
304304
return SelfPrint("%r / %r" % (other, self))
305305

306+
def __truediv__(self, other):
307+
return SelfPrint("%r / %r" % (self, other))
308+
309+
def __rtruediv__(self, other):
310+
return SelfPrint("%r / %r" % (other, self))
311+
306312
def __floordiv__(self, other):
307313
return SelfPrint("%r // %r" % (self, other))
308314

0 commit comments

Comments
 (0)