Run an Internal script from a handler
Don't run the script directly, instead, you can import the textblock as a python module, and execute a function it defines.

import myscript
myscript.main()


There are some caveats:


The script name must be unique to avoid colliding with existing Python modules.
You will have to define some function in the module to run.


But the advantages are:


Faster (no need to re-parse the script every time), (the compiled bytecode is kept between executions).
More flexible since you can pass in arguments if you like, and have multiple functions too.
The ability for variables to persist between executions (the module could contain a counter for example that increments each time. or values could be cached to avoid re-evaluation each execution).This is also something to watch out for, in case you accidentally re-use global lists, dicts etc...
More useful exception messages (__file__, __module__ for example will be defined)