OpenCog Framework
Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
|
#include <PythonEval.h>
Classes | |
struct | RAII |
Public Member Functions | |
PythonEval (AtomSpace *) | |
~PythonEval () | |
virtual void | begin_eval () |
virtual void | eval_expr (const std::string &) |
virtual std::string | poll_result () |
std::string | eval (const std::string &expr) |
std::string | apply_script (const std::string &script) |
Handle | apply (AtomSpace *, const std::string &func, Handle varargs) |
TruthValuePtr | apply_tv (AtomSpace *, const std::string &func, Handle varargs) |
void | print_root_dictionary () |
![]() | |
GenericEval (void) | |
virtual | ~GenericEval () |
virtual bool | input_pending () |
virtual void | clear_pending () |
virtual bool | eval_error (void) |
Static Public Member Functions | |
static void | create_singleton_instance (AtomSpace *) |
static void | delete_singleton_instance () |
static PythonEval & | instance (AtomSpace *atomspace=NULL) |
Private Member Functions | |
void | initialize_python_objects_and_imports (void) |
void | import_module (const boost::filesystem::path &file, PyObject *pyFromList) |
void | add_module_directory (const boost::filesystem::path &directory) |
void | add_module_file (const boost::filesystem::path &file) |
void | add_modules_from_path (std::string path) |
void | add_modules_from_abspath (std::string path) |
PyObject * | call_user_function (const std::string &func, Handle varargs) |
void | build_python_error_message (const char *function_name, std::string &errorMessage) |
void | add_to_sys_path (std::string path) |
PyObject * | atomspace_py_object (AtomSpace *) |
void | print_dictionary (PyObject *) |
void | execute_string (const char *command) |
int | argument_count (PyObject *pyFunction) |
PyObject * | module_for_function (const std::string &moduleFunction, std::string &functionName) |
Private Attributes | |
AtomSpace * | _atomspace |
PyObject * | _pyGlobal |
PyObject * | _pyLocal |
PyObject * | _pyRootModule |
PyObject * | _pySysPath |
std::map< std::string, PyObject * > | _modules |
std::string | _result |
int | _paren_count |
Static Private Attributes | |
static PythonEval * | singletonInstance = NULL |
static std::recursive_mutex | _mtx |
Additional Inherited Members | |
![]() | |
std::string | _input_line |
bool | _pending_input |
bool | _caught_error |
Singleton class used to initialize python interpreter in the main thread. It also provides some handy functions, such as getPyAtomspace. These helper functions may need python GIL and you should do this manually.
Definition at line 82 of file PythonEval.h.
PythonEval::PythonEval | ( | AtomSpace * | atomspace | ) |
Definition at line 343 of file PythonEval.cc.
References _atomspace, _paren_count, add_modules_from_path(), python.atom_type_names::atomspace, opencog::global_python_initialize(), initialize_python_objects_and_imports(), and singletonInstance.
PythonEval::~PythonEval | ( | ) |
Definition at line 374 of file PythonEval.cc.
References _pyGlobal, _pyLocal, _pyRootModule, and _pySysPath.
|
private |
Add all the .py files in the given directory as modules to main and keep the references in a dictionary (this->modules)
Definition at line 1056 of file PythonEval.cc.
References add_to_sys_path(), and import_module().
|
private |
Add the .py file in the given path as a module to main and add the reference to the dictionary (this->modules)
Definition at line 1098 of file PythonEval.cc.
References add_to_sys_path(), and import_module().
|
private |
Definition at line 1161 of file PythonEval.cc.
References add_module_directory(), and add_module_file().
|
private |
Get a path and determine if it is a file or directory, then call the corresponding function specific to directories and files.
Definition at line 1125 of file PythonEval.cc.
References add_modules_from_abspath(), and get_module_paths().
|
private |
Definition at line 973 of file PythonEval.cc.
References _pySysPath.
Calls the Python function passed in 'func' returning a Handle.
Definition at line 799 of file PythonEval.cc.
References _mtx, and call_user_function().
std::string PythonEval::apply_script | ( | const std::string & | script | ) |
Runs the Python code contained in 'script'.
Definition at line 902 of file PythonEval.cc.
References _mtx, _pyRootModule, build_python_error_message(), execute_string(), NO_FUNCTION_NAME, and python.bindlink::result.
TruthValuePtr PythonEval::apply_tv | ( | AtomSpace * | as, |
const std::string & | func, | ||
Handle | varargs | ||
) |
Calls the Python function passed in 'func' returning a TruthValuePtr.
Apply the user function to the arguments passed in varargs and return the extracted truth value.
Definition at line 851 of file PythonEval.cc.
References _mtx, and call_user_function().
|
private |
Definition at line 609 of file PythonEval.cc.
References MISSING_FUNC_CODE.
|
private |
|
inlinevirtual |
begin_eval() must be called in the same thread as poll_result() an it must be called before eval_expr(). The eval_expr() method may be called in the same thread, or a different one. The poll_result() method can be called at any time after begin_eval(). The poll_result() method might block, until results are available. It must return the empty string when there are no more results.
Implements opencog::GenericEval.
Definition at line 166 of file PythonEval.h.
|
private |
Build the Python error message for the current error.
Only call this when PyErr_Occurred() returns a non-null PyObject*.
Definition at line 554 of file PythonEval.cc.
References NO_FUNCTION_NAME.
|
private |
Call the user defined function with the arguments passed in the ListLink handle 'arguments'.
On error throws an exception.
Definition at line 668 of file PythonEval.cc.
References _atomspace, _mtx, argument_count(), atomspace_py_object(), build_python_error_message(), opencog::Atom::getType(), opencog::LinkCast(), MISSING_FUNC_CODE, and module_for_function().
|
static |
Create the singleton instance with the supplied atomspace.
Use a singleton instance to avoid initializing python interpreter twice.
Definition at line 399 of file PythonEval.cc.
References PythonEval(), and singletonInstance.
|
static |
Delete the singleton instance.
Definition at line 407 of file PythonEval.cc.
References singletonInstance.
|
inline |
Definition at line 171 of file PythonEval.h.
References begin_eval(), eval_expr(), and poll_result().
|
virtual |
Implements opencog::GenericEval.
Definition at line 1192 of file PythonEval.cc.
References opencog::GenericEval::_input_line, _paren_count, opencog::GenericEval::_pending_input, _result, and apply_script().
|
private |
Execute the python string at the main module context.
This replaces a call to PyRun_SimpleString which clears errors so that a subsequent call to Py_Error() returns false. This version does everything that PyRun_SimpleString does except it does not call PyErr_Print() and PyErr_Clear().
Definition at line 598 of file PythonEval.cc.
References _pyRootModule, and NO_COMPILER_FLAGS.
|
private |
Definition at line 992 of file PythonEval.cc.
References _atomspace, _modules, _pyGlobal, _pyLocal, _pyRootModule, ABSOLUTE_IMPORTS_ONLY, and atomspace_py_object().
|
private |
Definition at line 451 of file PythonEval.cc.
References _atomspace, _pyGlobal, _pyLocal, _pyRootModule, _pySysPath, and atomspace_py_object().
|
static |
Get a reference to the singleton instance.
Definition at line 416 of file PythonEval.cc.
References _atomspace, python.atom_type_names::atomspace, create_singleton_instance(), and singletonInstance.
|
private |
Get the Python module and stripped function name given the identifer of the form 'module.function'.
Definition at line 641 of file PythonEval.cc.
References _modules, and _pyRootModule.
|
virtual |
Implements opencog::GenericEval.
Definition at line 1264 of file PythonEval.cc.
References _result.
|
private |
|
inline |
Definition at line 192 of file PythonEval.h.
References _pyRootModule, and print_dictionary().
|
private |
Definition at line 110 of file PythonEval.h.
|
private |
Definition at line 141 of file PythonEval.h.
|
staticprivate |
Definition at line 133 of file PythonEval.h.
|
private |
Definition at line 144 of file PythonEval.h.
|
private |
Definition at line 135 of file PythonEval.h.
|
private |
Definition at line 136 of file PythonEval.h.
|
private |
Definition at line 137 of file PythonEval.h.
|
private |
Definition at line 139 of file PythonEval.h.
|
private |
Definition at line 143 of file PythonEval.h.
|
staticprivate |
Definition at line 108 of file PythonEval.h.