The "standard" ways to execute Python code:
(1) ( AOLserver map + PyWX_PageGet ) ------- | --> pywx.py (3) --> 'execfile' (4) | (2) ( AOLserver registry + 00-python.tcl ) -
'ns_param map' in config --> filter --> ns_pywx.cc:PyWX_PageGet() --> ns_pywx.cc:PyWX_PyExecute
With an 'ns_param Map' configuration command in the server.tcl
configuration file, commands can be mapped into the
PyWX_PageGet
function inside PyWX. This function calls
the same routine that the Tcl command 'ns_python exec
'
calls.
'00-python.tcl' in Tcl lib --> ns_sourcepython call --> 'ns_python exec' (Tcl command) --> ns_pywx.cc:Ns_PythonEvalCmd() --> ns_pywx.cc:PyWX_PyExecute
As a result of placing '00-python.tcl' into the server Tcl library
directory, the Tcl function 'ns_sourcepython
' is
registered on startup to handle files with URLs matching
'/*.py
'. 'ns_sourcepython
' calls the Tcl
function 'ns_python
', which tranfers control to the C
function Ns_PythonEvalCmd()
.
Ns_PythonEvalCmd()
, in turn, executes the passed-in
Python string.
This technique can be used to register arbitrary filename patterns to Python.
pywx.fulfill_request()
converts the URL to a
filename, runs the named script inside of a try
statement, and if there is an uncaught exception pretty-prints a
python stack trace.
If pywx.UseCachingExec
is set, an internal
'execfile
' is used that caches code in a dictionary using
the ns_codecache.CodeCache
class.
pywx.execfile
can be used as a replacement for the
builtin execfile
if the appropriate initialization
functions are run, as they are in
pywx.prepare_and_runscript()
.
The Python function 'execfile
' handles the final
execution of the (cached) code in the Python '__main__
'
environment.