Commit d9d1caa3 authored by Jérome Perrin's avatar Jérome Perrin

patches/Restricted: allow collections.Counter

parent f25ef810
......@@ -12,6 +12,7 @@
##############################################################################
import sys
import copy
from RestrictedPython.RestrictionMutator import RestrictionMutator
......@@ -181,6 +182,9 @@ ModuleSecurityInfo('collections').declarePublic('OrderedDict')
from collections import defaultdict
ModuleSecurityInfo('collections').declarePublic('defaultdict')
from collections import Counter
ModuleSecurityInfo('collections').declarePublic('Counter')
from AccessControl.ZopeGuards import _dict_white_list
# Attributes cannot be set on defaultdict, thus modify 'safetype' dict
......@@ -195,6 +199,12 @@ ContainerAssertions[OrderedDict] = _check_access_wrapper(OrderedDict, _dict_whit
OrderedDict.__guarded_setitem__ = OrderedDict.__setitem__.__func__
OrderedDict.__guarded_delitem__ = OrderedDict.__delitem__.__func__
_counter_white_list = copy.copy(_dict_white_list)
_counter_white_list['most_common'] = 1
ContainerAssertions[Counter] = _check_access_wrapper(Counter, _counter_white_list)
Counter.__guarded_setitem__ = dict.__setitem__
Counter.__guarded_delitem__ = dict.__delitem__
# given as example in Products.PythonScripts.module_access_examples
allow_module('base64')
allow_module('binascii')
......
......@@ -136,4 +136,12 @@ class TestRestrictedPythonSecurity(ERP5TypeTestCase):
self.assertRaises(Unauthorized,
self.createAndRunScript, 'import os',
'return os.system')
def test_collections_Counter(self):
self.createAndRunScript(
'from collections import Counter',
'c = Counter(["a", "b"])',
'c["a"] = c["a"] + 1',
'c.update({"a": 1})',
'return c.most_common(1)',
expected=[('a', 3)]
)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment