OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
opencog::PythonEval Class Reference

#include <PythonEval.h>

+ Inheritance diagram for opencog::PythonEval:
+ Collaboration diagram for opencog::PythonEval:

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 ()
 
- Public Member Functions inherited from opencog::GenericEval
 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 PythonEvalinstance (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 PythonEvalsingletonInstance = NULL
 
static std::recursive_mutex _mtx
 

Additional Inherited Members

- Protected Attributes inherited from opencog::GenericEval
std::string _input_line
 
bool _pending_input
 
bool _caught_error
 

Detailed Description

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.

Constructor & Destructor Documentation

PythonEval::PythonEval ( AtomSpace atomspace)
PythonEval::~PythonEval ( )

Definition at line 374 of file PythonEval.cc.

References _pyGlobal, _pyLocal, _pyRootModule, and _pySysPath.

Member Function Documentation

void PythonEval::add_module_directory ( const boost::filesystem::path &  directory)
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().

+ Here is the caller graph for this function:

void PythonEval::add_module_file ( const boost::filesystem::path &  file)
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().

+ Here is the caller graph for this function:

void PythonEval::add_modules_from_abspath ( std::string  path)
private

Definition at line 1161 of file PythonEval.cc.

References add_module_directory(), and add_module_file().

+ Here is the caller graph for this function:

void PythonEval::add_modules_from_path ( std::string  pathString)
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().

+ Here is the caller graph for this function:

void PythonEval::add_to_sys_path ( std::string  path)
private

Definition at line 973 of file PythonEval.cc.

References _pySysPath.

+ Here is the caller graph for this function:

Handle PythonEval::apply ( AtomSpace as,
const std::string &  func,
Handle  varargs 
)

Calls the Python function passed in 'func' returning a Handle.

Definition at line 799 of file PythonEval.cc.

References _mtx, and call_user_function().

+ Here is the caller graph for this 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.

+ Here is the caller graph for this function:

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().

+ Here is the caller graph for this function:

int PythonEval::argument_count ( PyObject *  pyFunction)
private

Definition at line 609 of file PythonEval.cc.

References MISSING_FUNC_CODE.

+ Here is the caller graph for this function:

PyObject * PythonEval::atomspace_py_object ( AtomSpace atomspace)
private

Definition at line 491 of file PythonEval.cc.

+ Here is the caller graph for this function:

virtual void opencog::PythonEval::begin_eval ( )
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.

+ Here is the caller graph for this function:

void PythonEval::build_python_error_message ( const char *  function_name,
std::string &  errorMessage 
)
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.

+ Here is the caller graph for this function:

PyObject * PythonEval::call_user_function ( const std::string &  moduleFunction,
Handle  arguments 
)
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().

+ Here is the caller graph for this function:

void PythonEval::create_singleton_instance ( AtomSpace atomspace)
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.

+ Here is the caller graph for this function:

void PythonEval::delete_singleton_instance ( )
static

Delete the singleton instance.

Definition at line 407 of file PythonEval.cc.

References singletonInstance.

+ Here is the caller graph for this function:

std::string opencog::PythonEval::eval ( const std::string &  expr)
inline

Definition at line 171 of file PythonEval.h.

References begin_eval(), eval_expr(), and poll_result().

void PythonEval::eval_expr ( const std::string &  partial_expr)
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().

+ Here is the caller graph for this function:

void PythonEval::execute_string ( const char *  command)
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.

+ Here is the caller graph for this function:

void PythonEval::import_module ( const boost::filesystem::path &  file,
PyObject *  pyFromList 
)
private

Definition at line 992 of file PythonEval.cc.

References _atomspace, _modules, _pyGlobal, _pyLocal, _pyRootModule, ABSOLUTE_IMPORTS_ONLY, and atomspace_py_object().

+ Here is the caller graph for this function:

void PythonEval::initialize_python_objects_and_imports ( void  )
private

Definition at line 451 of file PythonEval.cc.

References _atomspace, _pyGlobal, _pyLocal, _pyRootModule, _pySysPath, and atomspace_py_object().

+ Here is the caller graph for this function:

PythonEval & PythonEval::instance ( AtomSpace atomspace = NULL)
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.

+ Here is the caller graph for this function:

PyObject * PythonEval::module_for_function ( const std::string &  moduleFunction,
std::string &  functionName 
)
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.

+ Here is the caller graph for this function:

std::string PythonEval::poll_result ( )
virtual

Implements opencog::GenericEval.

Definition at line 1264 of file PythonEval.cc.

References _result.

+ Here is the caller graph for this function:

void PythonEval::print_dictionary ( PyObject *  obj)
private

Definition at line 525 of file PythonEval.cc.

+ Here is the caller graph for this function:

void opencog::PythonEval::print_root_dictionary ( )
inline

Definition at line 192 of file PythonEval.h.

References _pyRootModule, and print_dictionary().

Member Data Documentation

AtomSpace* opencog::PythonEval::_atomspace
private

Definition at line 110 of file PythonEval.h.

std::map<std::string, PyObject*> opencog::PythonEval::_modules
private

Definition at line 141 of file PythonEval.h.

std::recursive_mutex PythonEval::_mtx
staticprivate

Definition at line 133 of file PythonEval.h.

int opencog::PythonEval::_paren_count
private

Definition at line 144 of file PythonEval.h.

PyObject* opencog::PythonEval::_pyGlobal
private

Definition at line 135 of file PythonEval.h.

PyObject* opencog::PythonEval::_pyLocal
private

Definition at line 136 of file PythonEval.h.

PyObject* opencog::PythonEval::_pyRootModule
private

Definition at line 137 of file PythonEval.h.

PyObject* opencog::PythonEval::_pySysPath
private

Definition at line 139 of file PythonEval.h.

std::string opencog::PythonEval::_result
private

Definition at line 143 of file PythonEval.h.

PythonEval * PythonEval::singletonInstance = NULL
staticprivate

Definition at line 108 of file PythonEval.h.


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