Reputation: 7096
I'd like to include Python scripting in one of my applications, that is written in Python itself.
My application must be able to call external Python functions (written by the user) as callbacks. There must be some control on code execution; for example, if the user provided code with syntax errors, the application must signal that.
What is the best way to do this?
Thanks.
edit: question was unclear. I need a mechanism similar to events of VBA, where there is a "declarations" section (where you define global variables) and events, with scripted code, that fire at specific points.
Upvotes: 5
Views: 2805
Reputation: 392070
"My application must be able to call external Python functions (written by the user) as callbacks".
There's an alternative that's often simpler.
Define classes which call method functions at specific points. You provide a default implementation.
Your user can then extended the classes and provide appropriate method functions instead of callbacks.
This rarely requires global variables. It's also simpler to implement because your user does something like the following
import core_classes
class MyExtension( core_classes.SomeClass ):
def event_1( self, args ):
# override the default behavior for event_1.
core_classes.main( MyExtension )
This works very smoothly, and allows maximum flexibility. Errors will always be in their code, since their code is the "main" module.
Upvotes: 0
Reputation: 27482
If you'd like the user to interactively enter commands, I can highly recommend the code module, part of the standard library. The InteractiveConsole and InteractiveInterpreter objects allow for easy entry and evaluation of user input, and errors are handled very nicely, with tracebacks available to help the user get it right.
Just make sure to catch SystemExit!
$ python
Python 2.5.1 (r251:54863, Jan 17 2008, 19:35:17)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> shared_var = "Set in main console"
>>> import code
>>> ic = code.InteractiveConsole({ 'shared_var': shared_var })
>>> try:
... ic.interact("My custom console banner!")
... except SystemExit, e:
... print "Got SystemExit!"
...
My custom console banner!
>>> shared_var
'Set in main console'
>>> shared_var = "Set in sub-console"
>>> sys.exit()
Got SystemExit!
>>> shared_var
'Set in main console'
Upvotes: 8
Reputation: 46839
Use __import__
to import the files provided by the user. This function will return a module. Use that to call the functions from the imported file.
Use try..except
both on __import__
and on the actual call to catch errors.
Example:
m = None
try:
m = __import__("external_module")
except:
# invalid module - show error
if m:
try:
m.user_defined_func()
except:
# some error - display it
Upvotes: 8
Reputation: 86502
RestrictedPython provides a restricted execution environment for Python, e.g. for running untrusted code.
Upvotes: 4