Qore python Module  1.0.2
Python::PythonProgram Class Reference

Program for embedding and executing Python code. More...

Public Member Methods

 aliasDefinition (string python_source_path, string python_target_path)
 Creates an alias in the Python program for the given symbol. More...
 
auto callFunction (string func_name,...)
 Call the given function and return the result. More...
 
auto callFunctionArgs (string func_name, *softlist< auto > argv)
 Call the given function and return the result. More...
 
auto callMethod (string class_name, string method_name,...)
 Call the given method and return the result. More...
 
auto callMethodArgs (string class_name, string method_name, *softlist< auto > argv)
 Call the given method and return the result. More...
 
 constructor (string source_code, string source_label)
 Creates the object and parses and runs the given source code. More...
 
 destructor ()
 Destroys the interpreter context and invalidates the object.
 
auto evalExpression (string source_code, string source_label="exp")
 Parse, compile, and evaluate the given expression and return any result. More...
 
auto evalStatement (string source_code, string source_label="stmt")
 Parse, compile, and evaluate the given statement and return any result; declarations are not persistent. More...
 
auto evalStatementKeep (string source_code, string source_label="stmt")
 Parse, compile, and evaluate the given statement and return any result; declarations are persistent. More...
 
 importNamespace (string qore_ns_path, string py_module_path)
 Imports the given Qore namespace to the Python program object under the given module path. More...
 
auto run ()
 Runs the program. More...
 
 setSaveObjectCallback (*code save_object_callback)
 Sets the "save object" callback for Qore objects created from Python code. More...
 

Static Public Member Methods

static auto evalExpression (string source_code, string source_label="exp")
 Parse, compile, and evaluate the given expression and return any result. More...
 
static auto evalStatement (string source_code, string source_label="stmt")
 Parse, compile, and evaluate one or more Python statements and return any result. More...
 
static auto evalStatementKeep (string source_code, string source_label="stmt")
 Parse, compile, and evaluate the given code and leave declarations in the Python program object. More...
 
static setSaveObjectCallback (*code save_object_callback)
 Sets the "save object" callback for Qore objects created from Python code in the root Qore Program context. More...
 

Detailed Description

Program for embedding and executing Python code.

Restrictions:
Qore::PO_NO_EMBEDDED_LOGIC

Member Function Documentation

◆ aliasDefinition()

Python::PythonProgram::aliasDefinition ( string  python_source_path,
string  python_target_path 
)

Creates an alias in the Python program for the given symbol.

Parameters
python_source_paththe dotted path to the source definition (ex: "some.path.to.sometehing")
python_target_paththe dotted path to the target definition (ex: "a.different.path")
Exceptions
PYTHON-ALIAS-ERRORthere was an error finding the source object or creating the target

◆ callFunction()

auto Python::PythonProgram::callFunction ( string  func_name,
  ... 
)

Call the given function and return the result.

Parameters
func_namethe function name to call
...arguments to the function should follow the name converted to Python values as per Type Conversions From Qore To Python
Returns
the return value of the Python function converted to Qore as per Type Conversions From Python to Qore
Exceptions
NO-FUNCTIONthe given function cannot be found in the Python program's module dictionary
See also
Python Exception Handling

◆ callFunctionArgs()

auto Python::PythonProgram::callFunctionArgs ( string  func_name,
*softlist< auto >  argv 
)

Call the given function and return the result.

Parameters
func_namethe function name to call
argvarguments to the function as a list converted to Python values as per Type Conversions From Qore To Python
Returns
the return value of the Python function converted to Qore as per Type Conversions From Python to Qore
Exceptions
NO-FUNCTIONthe given function cannot be found in the Python program's module dictionary
See also
Python Exception Handling

◆ callMethod()

auto Python::PythonProgram::callMethod ( string  class_name,
string  method_name,
  ... 
)

Call the given method and return the result.

Parameters
class_namethe name of the class implementing the method to call
method_namethe method to call
...arguments to the method should follow the name converted to Python values as per Type Conversions From Qore To Python
Returns
the return value of the Python method call converted to Qore as per Type Conversions From Python to Qore
Exceptions
NO-CLASSthe given class cannot be found in the Python program's module dictionary
NO-METHODthe given method cannot be found in the class
See also
Python Exception Handling

◆ callMethodArgs()

auto Python::PythonProgram::callMethodArgs ( string  class_name,
string  method_name,
*softlist< auto >  argv 
)

Call the given method and return the result.

Parameters
class_namethe name of the class implementing the method to call
method_namethe method to call
argvarguments to the method as a list converted to Python values as per Type Conversions From Qore To Python
Returns
the return value of the Python method call converted to Qore as per Type Conversions From Python to Qore
Exceptions
NO-CLASSthe given class cannot be found in the Python program's module dictionary
NO-METHODthe given method cannot be found in the class
See also
Python Exception Handling

◆ constructor()

Python::PythonProgram::constructor ( string  source_code,
string  source_label 
)

Creates the object and parses and runs the given source code.

Parameters
source_codethe Python source to parse and compile
source_labelthe label or file name of the source; this is used as the module name for the compiled Python code as well
Note
  • The code is executed after parsing and compiling
  • Exceptions parsing, compiling, or running the Python code are thrown according to Python Exception Handling

◆ evalExpression() [1/2]

auto Python::PythonProgram::evalExpression ( string  source_code,
string  source_label = "exp" 
)

Parse, compile, and evaluate the given expression and return any result.

Parameters
source_codethe Python source to parse and compile
source_labelthe label or file name of the source
Exceptions
PYTHON-COMPILE-ERRORerror parsing or compiling the given source
Note
This method uses a shared static PythonProgram object to parse, compile, and evaluate the input string

◆ evalExpression() [2/2]

static auto Python::PythonProgram::evalExpression ( string  source_code,
string  source_label = "exp" 
)
static

Parse, compile, and evaluate the given expression and return any result.

Parameters
source_codethe Python source to parse and compile
source_labelthe label or file name of the source
Exceptions
PYTHON-COMPILE-ERRORerror parsing or compiling the given source
Note
This method uses a shared static PythonProgram object to parse, compile, and evaluate the input string

◆ evalStatement() [1/2]

auto Python::PythonProgram::evalStatement ( string  source_code,
string  source_label = "stmt" 
)

Parse, compile, and evaluate the given statement and return any result; declarations are not persistent.

Parameters
source_codethe Python source to parse and compile
source_labelthe label or file name of the source
Exceptions
PYTHON-COMPILE-ERRORerror parsing or compiling the given source
See also
evalStatementKeep()

◆ evalStatement() [2/2]

static auto Python::PythonProgram::evalStatement ( string  source_code,
string  source_label = "stmt" 
)
static

Parse, compile, and evaluate one or more Python statements and return any result.

Parameters
source_codethe Python source to parse and compile
source_labelthe label or file name of the source
Exceptions
PYTHON-COMPILE-ERRORerror parsing or compiling the given source
Note
This method uses the PythonProgram context in the current program to parse, compile, and evaluate the input string
See also
evalStatementKeep()

◆ evalStatementKeep() [1/2]

auto Python::PythonProgram::evalStatementKeep ( string  source_code,
string  source_label = "stmt" 
)

Parse, compile, and evaluate the given statement and return any result; declarations are persistent.

Parameters
source_codethe Python source to parse and compile
source_labelthe label or file name of the source
Exceptions
PYTHON-COMPILE-ERRORerror parsing or compiling the given source
See also
evalStatement()

◆ evalStatementKeep() [2/2]

static auto Python::PythonProgram::evalStatementKeep ( string  source_code,
string  source_label = "stmt" 
)
static

Parse, compile, and evaluate the given code and leave declarations in the Python program object.

Parameters
source_codethe Python source to parse and compile
source_labelthe label or file name of the source
Exceptions
PYTHON-COMPILE-ERRORerror parsing or compiling the given source
Note
This method uses the PythonProgram context in the current program to parse, compile, and evaluate the input string
See also
evalStatement()

◆ importNamespace()

Python::PythonProgram::importNamespace ( string  qore_ns_path,
string  py_module_path 
)

Imports the given Qore namespace to the Python program object under the given module path.

Parameters
qore_ns_paththe path to the Qore namespace (ex: "::Qore::Thread")
py_module_paththe dot-separated path to the target Python module to be created (ex: "qore.qore.thread")
Exceptions
IMPORT-NS-ERRORQore namespace could not be found or the root namespace was given for import

◆ run()

auto Python::PythonProgram::run ( )

Runs the program.

Note
Exceptions parsing or compiling the Python code are thrown according to Python Exception Handling

◆ setSaveObjectCallback() [1/2]

Python::PythonProgram::setSaveObjectCallback ( *code  save_object_callback)

Sets the "save object" callback for Qore objects created from Python code.

Restrictions:
Qore::PO_NO_PROCESS_CONTROL
Example:
hash<string, object> object_cache;
code callback = sub (object obj) {
# save object in object cache, so it doesn't go out of scope
object_cache{obj.uniqueHash()} = obj;
}
pypgm.setSaveObjectCallback(callback);
Parameters
save_object_callbackthe callback to save any Qore objects created in Python code, must take an argument of type object

Due to the differences in garbage collection approaches between Qore and Python, Qore objects must be managed with a deterministic life cycle; JaPythona objects have only weak references to Qore objects due to the lack of destructors in Python and the lack of determinism in the Python runtime for object lifecycle management.

The callback set here will be called any time a Qore object is created from Python code; if no callback is set, then the standard thread-local implementation is used where Qore objects are saved in a thread-local hash.

See also
Managing the Lifecycle of Qore objects from Python for more information

◆ setSaveObjectCallback() [2/2]

static Python::PythonProgram::setSaveObjectCallback ( *code  save_object_callback)
static

Sets the "save object" callback for Qore objects created from Python code in the root Qore Program context.

Restrictions:
Qore::PO_NO_PROCESS_CONTROL
Example:
hash<string, object> object_cache;
code callback = sub (object obj) {
# save object in object cache, so it doesn't go out of scope
object_cache{obj.uniqueHash()} = obj;
}
PythonProgram::setSaveObjectCallback(callback);
Parameters
save_object_callbackthe callback to save any Qore objects created in Python code, must take an argument of type object

Due to the differences in garbage collection approaches between Qore and Python, Qore objects must be managed with a deterministic life cycle; JaPythona objects have only weak references to Qore objects due to the lack of destructors in Python and the lack of determinism in the Python runtime for object lifecycle management.

The callback set here will be called any time a Qore object is created from Python code; if no callback is set, then the standard thread-local implementation is used where Qore objects are saved in a thread-local hash.

See also
Managing the Lifecycle of Qore objects from Python for more information

The documentation for this class was generated from the following file: