61 if (!module)
return false;
64 return instanciate (module, file, classname, args);
72 if (!module)
return false;
75 PyObject *reload = PyImport_ReloadModule (module);
77 if (!reload)
return false;
79 return instanciate (reload, file, classname, args);
83 bool py_object::instanciate (PyObject *module,
string file,
string classname, PyObject * args)
88 PyObject * classobj = PyObject_GetAttrString (module, (
char *) classname.c_str ());
97 Instance = PyObject_CallObject (classobj, args);
106 Classname = classname;
114 PyObject *result = NULL;
118 PyObject *tocall = PyObject_GetAttrString (
Instance, (
char *) name.c_str ());
120 if (PyCallable_Check (tocall) == 1)
122 result = PyObject_CallObject (tocall, args);
137 return PyObject_HasAttrString (
Instance, (
char *) name.c_str ());
146 return PyObject_GetAttrString (
Instance, (
char *) name.c_str ());
156 PyObject *attribute = PyObject_GetAttrString (
Instance, (
char *) name.c_str ());
157 if (!attribute)
return 0;
159 s_int32 value = PyInt_AsLong (attribute);
160 Py_DECREF (attribute);
173 PyObject *attribute = PyObject_GetAttrString (
Instance, (
char *) name.c_str ());
174 if (!attribute)
return 0;
176 string value = python::as_string (attribute);
177 Py_DECREF (attribute);
189 if (PyObject_SetAttrString (
Instance, (
char *) name.c_str (), value) == -1)
198 PyObject *val = PyInt_FromLong (value);
200 if (PyObject_SetAttrString (
Instance, (
char *) name.c_str (), val) == -1)
213 PyObject *val = PyString_FromString (value.c_str ());
215 if (PyObject_SetAttrString (
Instance, (
char *) name.c_str (), val) == -1)
#define s_int32
32 bits long signed integer
PyObject * call_method_ret(const string &name, PyObject *args=NULL) const
Call a method of this object.
void clear()
Resets the script to it's post-constructor state.
py_object()
Default constructor.
string get_attribute_string(const string &name)
Returns the given attribute as string value.
void set_attribute(const string &name, PyObject *value)
Assign a new attribute to the module, overriding an existing attribute of the same name...
void set_attribute_int(const string &name, s_int32 value)
Assign a new integer attribute to the module, overriding an existing attribute of the same name...
static void show_traceback(void)
Dumps any error information to stderr.
bool create_instance(string file, string classname, PyObject *args=NULL)
Creates an instance of a Python class.
static PyObject * import_module(string filename)
Imports a Python module.
PyObject * get_attribute(const string &name) const
Returns a new reference to an attribute of this object.
void set_attribute_string(const string &name, const string &value)
Assign a new string attribute to the module, overriding an existing attribute of the same name...
Declares the py_object class.
bool reload_instance(string file, string classname, PyObject *args=NULL)
Similar to create_instance, except that it will reload the module from disk, in case it has been chan...
PyObject * Instance
The python class instance wrapped by py_object.
s_int32 get_attribute_int(const string &name)
Returns the given attribute as integer value.
bool has_attribute(const std::string &name)
Tests whether the object contains a certain attribute (i.e.