Commit 3e46998e authored by Arnaud Fontaine's avatar Arnaud Fontaine

Do not set namespace dict where exec is executed into as a default parameter dict.

When load() was called in checkConsistency where namespace_dict was not given
and thus equal to the default function parameter value, exec would set the
function namespace_dict parameter, thus the next call would have
namespace_dict already set when it was given.

This fixes testInvalidSourceCode when being ran for TestZodbExtensionComponent
and TestZodbDocumentComponent in the same runUnitTest.
parent 1de396b2
......@@ -201,7 +201,7 @@ class ComponentMixin(PropertyRecordableMixin, Base):
else:
message = None
try:
self.load(text_content=text_content)
self.load({}, text_content=text_content)
except SyntaxError, e:
mapping = dict(error_message=str(e),
line_number=e.lineno,
......@@ -266,14 +266,18 @@ class ComponentMixin(PropertyRecordableMixin, Base):
for error in current_workflow.get('error_message', [])]
security.declareProtected(Permissions.ModifyPortalContent, 'load')
def load(self, namespace_dict={}, validated_only=False, text_content=None):
def load(self, namespace_dict, validated_only=False, text_content=None):
"""
Load the source code into the given dict. Using exec() rather than
imp.load_source() as the latter would required creating an intermediary
file. Also, for traceback readability sake, the destination module
__dict__ is given rather than creating an empty dict and returning
it. By default namespace_dict is an empty dict to allow checking the
source code before validate.
__dict__ is given rather than creating an empty dict and returning it.
Initially, namespace_dict default parameter value was an empty dict to
allow checking the source code before validate, but this introduces a bug
when namespace_dict was not given because the first call would exec
directly into function namespace_dict default parameter, thus the second
call would have namespace_dict default value to the previous call.
"""
if text_content is None:
text_content = self.getTextContent(validated_only=validated_only)
......
......@@ -1474,12 +1474,14 @@ class _TestZodbComponent(SecurityTestCase):
self.assertEquals(component.getTextContent(validated_only=True), valid_code)
self.assertModuleImportable('TestComponentWithSyntaxError')
invalid_code_dict = {
None: ComponentMixin._message_text_content_not_set,
'def foobar(*args, **kwargs)\n return 42': 'Syntax error in source code:',
'foobar': 'Source code:'}
for invalid_code, error_message in invalid_code_dict.iteritems():
# Make sure that foobar NameError is at the end to make sure that after
# defining foobar function, the symbol is not available anymore
invalid_code_dict = (
(None, ComponentMixin._message_text_content_not_set),
('def foobar(*args, **kwargs)\n return 42', 'Syntax error in source code:'),
('foobar', 'Source code:'))
for invalid_code, error_message in invalid_code_dict:
ComponentTool.reset = assertResetNotCalled
try:
component.setTextContent(invalid_code)
......
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