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

patches/Restricted: allow collections.Counter

parent 426ff95b
...@@ -336,3 +336,16 @@ class TestRestrictedPythonSecurity(ERP5TypeTestCase): ...@@ -336,3 +336,16 @@ class TestRestrictedPythonSecurity(ERP5TypeTestCase):
calendar.HTMLCalendar().getfirstweekday() calendar.HTMLCalendar().getfirstweekday()
'''), '''),
) )
def test_collections_Counter(self):
self.createAndRunScript(
textwrap.dedent('''\
from collections import Counter
c = Counter(["a", "b"])
c["a"] = c["a"] + 1
del c["b"]
c.update({"a": 1})
return c.most_common(1)
'''),
expected=[('a', 3)]
)
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
# #
############################################################################## ##############################################################################
import copy
import sys import sys
import types import types
...@@ -187,6 +188,9 @@ ModuleSecurityInfo('collections').declarePublic('OrderedDict') ...@@ -187,6 +188,9 @@ ModuleSecurityInfo('collections').declarePublic('OrderedDict')
from collections import defaultdict from collections import defaultdict
ModuleSecurityInfo('collections').declarePublic('defaultdict') ModuleSecurityInfo('collections').declarePublic('defaultdict')
from collections import Counter
ModuleSecurityInfo('collections').declarePublic('Counter')
from AccessControl.ZopeGuards import _dict_white_list from AccessControl.ZopeGuards import _dict_white_list
# Attributes cannot be set on defaultdict, thus modify 'safetype' dict # Attributes cannot be set on defaultdict, thus modify 'safetype' dict
...@@ -201,6 +205,12 @@ ContainerAssertions[OrderedDict] = _check_access_wrapper(OrderedDict, _dict_whit ...@@ -201,6 +205,12 @@ ContainerAssertions[OrderedDict] = _check_access_wrapper(OrderedDict, _dict_whit
OrderedDict.__guarded_setitem__ = OrderedDict.__setitem__.__func__ OrderedDict.__guarded_setitem__ = OrderedDict.__setitem__.__func__
OrderedDict.__guarded_delitem__ = OrderedDict.__delitem__.__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 # given as example in Products.PythonScripts.module_access_examples
allow_module('base64') allow_module('base64')
allow_module('binascii') allow_module('binascii')
......
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