Skip to content

Commit

Permalink
Show proper error message for not allowed identifiers. (#34)
Browse files Browse the repository at this point in the history
Due to interaction between Script(Python) 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
modified:   CHANGES.rst
  • Loading branch information
jugmac00 committed Jul 7, 2019
1 parent 0766627 commit f89eed4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ Changelog

4.8 (unreleased)
----------------
- Show proper error message for not allowed identifiers.
(`#33 <https://github.com/zopefoundation/Products.PythonScripts/issues/33>`_)


4.7 (2019-05-21)
Expand Down
10 changes: 9 additions & 1 deletion src/Products/PythonScripts/PythonScript.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,16 @@ class PythonScript(Script, Cacheable):
Cacheable.manage_options

def __init__(self, id):
self.id = id
self.ZBindings_edit(defaultBindings)
bind_names = self.getBindingAssignments().getAssignedNamesInOrder()
if id in bind_names:
raise ValueError(
'Following names are not allowed as identifiers, as they'
'have a special meaning for PythonScript: '
'%s.'
'Please choose another name.' % ', '.join(bind_names),
)
self.id = id
self._makeFunction()

security = ClassSecurityInfo()
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 f89eed4

Please sign in to comment.