Skip to content

Commit

Permalink
Show proper error message for not allowed identifiers.
Browse files Browse the repository at this point in the history
Due to interaction between Python(Scripts) and RestrictedPython,
following identifiers are not allowed:
- context
- container
- script
- traverse_subpath

Instead of throwing an "IndexError", now a proper error messsage
is shown.

This closes #33

modified: src/Products/PythonScripts/PythonScript.py
modified: src/Products/PythonScripts/tests/testPythonScript.py
  • Loading branch information
jugmac00 committed Jul 5, 2019
1 parent 0766627 commit 9d1231d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Products/PythonScripts/PythonScript.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,18 @@ class PythonScript(Script, Cacheable):
) + SimpleItem.manage_options + \
Cacheable.manage_options

# following identifiers are not allowed due to interaction with
# RestrictedPython
bad_identifiers = [
'context', 'container', 'script', 'traverse_subpath'
]

def __init__(self, id):
if id in self.bad_identifiers:
raise ValueError(
"%s is not allowed as an identifier. "
"Please choose another name." % id
)
self.id = id
self.ZBindings_edit(defaultBindings)
self._makeFunction()
Expand Down
13 changes: 13 additions & 0 deletions src/Products/PythonScripts/tests/testPythonScript.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,19 @@ def testAttributeAssignment(self):
func = self._newPS(defn + '\n' + asn % name)
self.assertRaises(TypeError, func)

def testBadIdentifiers(self):
"""Some identifiers have to be avoided.
Background:
https://github.com/zopefoundation/Zope/issues/669
"""
bad_identifiers = [
'context', 'container', 'script', 'traverse_subpath'
]
for identifier in bad_identifiers:
with self.assertRaises(ValueError):
PythonScript(identifier)


class TestPythonScriptGlobals(PythonScriptTestBase):

Expand Down

0 comments on commit 9d1231d

Please sign in to comment.