Skip to content

Commit b90fb27

Browse files
committed
Harden the way that form action modules are loaded and ensure that import statements within an action module work as expected.
1 parent 1430d27 commit b90fb27

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

Form/formactions.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,18 @@
2121
"""
2222
Form action chooser
2323
"""
24+
#-------------------------------------------------------------------------
25+
#
26+
# Standard Python modules
27+
#
28+
#-------------------------------------------------------------------------
29+
import logging
2430
import importlib.util
2531
import inspect
2632
import os
27-
import gobject
33+
import sys
34+
35+
LOG = logging.getLogger('.form')
2836

2937
#------------------------------------------------------------------------
3038
#
@@ -93,9 +101,19 @@ def __init__(self, dbstate, uistate, track, citation):
93101
# for security reasons provide the full path to the actions_module .py file
94102
full_path = os.path.join(os.path.dirname(__file__), '%s.py' % self.form_id)
95103
if os.path.exists(full_path):
96-
spec = importlib.util.spec_from_file_location('form.action.', full_path)
97-
self.actions_module = importlib.util.module_from_spec(spec)
98-
spec.loader.exec_module(self.actions_module)
104+
# temporarily modify sys.path so that any import statements in the module get processed correctly
105+
sys.path.insert(0, os.path.dirname(__file__))
106+
try:
107+
spec = importlib.util.spec_from_file_location('form.actions.%s' % self.form_id, full_path)
108+
self.actions_module = importlib.util.module_from_spec(spec)
109+
spec.loader.exec_module(self.actions_module)
110+
except (ValueError, ImportError, SyntaxError) as err:
111+
self.actions_module = None
112+
LOG.warning("Form plugin error (from '%s'): %s"
113+
% (self.form_id, err))
114+
finally:
115+
# must make sure we restore sys.path
116+
sys.path.pop(0)
99117

100118
self.event = find_form_event(self.db, self.citation)
101119

0 commit comments

Comments
 (0)