Skip to content

Commit

Permalink
Decimal checker: __truediv__ was missing, also added tests, did some …
Browse files Browse the repository at this point in the history
…cleanup
  • Loading branch information
agroszer committed Jan 3, 2019
1 parent 6846c5f commit 0660cf0
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 11 deletions.
3 changes: 2 additions & 1 deletion CHANGES.rst
Expand Up @@ -5,7 +5,8 @@
4.3.1 (unreleased)
==================

- Nothing changed yet.
- Fix the decimal.Decimal checker, ``__truediv__`` was missing causing
``ForbiddenAttribute`` on a ``ProxyFactory(Decimal('1')) / 1`` operation


4.3.0 (2018-08-24)
Expand Down
27 changes: 17 additions & 10 deletions src/zope/security/checker.py
Expand Up @@ -816,20 +816,27 @@ def f(): # pragma: no cover
'__add__', '__radd__', ]),
set: _setChecker,
frozenset: _setChecker,
# XXX: actually decimal.Decimal has more methods, which are unlisted here
# so expect ForbiddenAttribute on such
decimal.Decimal: NamesChecker(['__nonzero__', '__cmp__', '__eq__',
'__ne__', 'compare', '__hash__',
'as_tuple', '__str__', 'to_eng_string',
'__ne__', '__hash__',
'__str__',
'__neg__', '__pos__', '__abs__',
'__add__', '__radd__', '__sub__',
'__rsub__', '__mul__', '__rmul__',
'__div__', '__rdiv__', '__rtruediv__',
'__divmod__', '__rdivmod__', '__mod__',
'__rmod__', 'remainder_near',
'__add__', '__radd__',
'__sub__', '__rsub__',
'__mul__', '__rmul__',
'__div__', '__truediv__',
'__rdiv__', '__rtruediv__',
'__divmod__', '__rdivmod__',
'__mod__', '__rmod__',
'__floordiv__', '__rfloordiv__',
'__float__', '__int__', '__long__',
'__pow__', '__rpow__', 'normalize',
'quantize', 'same_quantum', 'to_integral',
'sqrt', 'max', 'min', 'adjusted']),
'__pow__', '__rpow__',
'adjusted', 'as_tuple', 'compare',
'max', 'min', 'normalize',
'quantize', 'remainder_near',
'same_quantum', 'sqrt',
'to_eng_string', 'to_integral' ]),

# YAGNI: () a rock
tuple: NamesChecker(['__getitem__', '__getslice__', '__add__', '__radd__',
Expand Down
42 changes: 42 additions & 0 deletions src/zope/security/tests/test_checker.py
Expand Up @@ -389,6 +389,48 @@ def _check(*args):
finally:
_clear()

def test_Decimal_operations(self):
from zope.security.proxy import Proxy
from zope.security.checker import _default_checkers
from decimal import Decimal
from decimal import DecimalTuple

checker = _default_checkers[Decimal]

value = Decimal('1.1')
proxy = Proxy(value, checker)

self.assertTrue(proxy)
self.assertTrue(proxy < Decimal('2'))
self.assertTrue(proxy == Decimal('1.1'))
self.assertTrue(proxy > Decimal('1'))
self.assertTrue(proxy != Decimal('1'))
self.assertEqual(-proxy, Decimal('-1.1'))
self.assertEqual(proxy + Decimal('1.1'), Decimal('2.2'))
self.assertEqual(Decimal('1.1') + proxy, Decimal('2.2'))
self.assertEqual(proxy - Decimal('1'), Decimal('0.1'))
self.assertEqual(Decimal('2.1') - proxy, Decimal('1'))
self.assertEqual(proxy * 1, Decimal('1.1'))
self.assertEqual(1 * proxy, Decimal('1.1'))
self.assertEqual(proxy * Decimal('1.1'), Decimal('1.21'))
self.assertEqual(Decimal('1.1') * proxy, Decimal('1.21'))
self.assertEqual(proxy / 1, Decimal('1.1'))
self.assertEqual(proxy / Decimal('1'), Decimal('1.1'))
self.assertEqual(proxy // 1, Decimal('1'))
self.assertEqual(proxy // Decimal('1'), Decimal('1'))
self.assertEqual(float(proxy), 1.1)
self.assertEqual(int(proxy), 1)
if PY2:
self.assertEqual(long(proxy), 1)
self.assertEqual(proxy ** 2, Decimal('1.21'))
self.assertEqual(1 ** proxy, Decimal('1'))
self.assertEqual(proxy.adjusted(), 0)
self.assertEqual(
proxy.as_tuple(), DecimalTuple(sign=0, digits=(1, 1), exponent=-1))
self.assertEqual(proxy.compare(Decimal('1.1')), Decimal('0'))
self.assertEqual(proxy.normalize(), Decimal('1.1'))
self.assertEqual(proxy.quantize(Decimal('1.10')), Decimal('1.10'))

def _check_iteration_of_dict_like(self, dict_like):
from zope.security.proxy import Proxy
from zope.security.checker import _default_checkers
Expand Down

0 comments on commit 0660cf0

Please sign in to comment.