57 if (!module)
return false;
60 return instanciate (module, file, classname, args);
68 if (!module)
return false;
71 PyObject *reload = PyImport_ReloadModule (module);
73 if (!reload)
return false;
75 return instanciate (reload, file, classname, args);
79 bool py_object::instanciate (PyObject *module,
string file,
string classname, PyObject * args)
84 PyObject * classobj = PyObject_GetAttrString (module, (
char *) classname.c_str ());
93 Instance = PyObject_CallObject (classobj, args);
102 Classname = classname;
110 PyObject *result = NULL;
114 PyObject *tocall = PyObject_GetAttrString (
Instance, (
char *) name.c_str ());
116 if (PyCallable_Check (tocall) == 1)
118 result = PyObject_CallObject (tocall, args);
133 return PyObject_HasAttrString (
Instance, (
char *) name.c_str ());
142 return PyObject_GetAttrString (
Instance, (
char *) name.c_str ());
152 PyObject *attribute = PyObject_GetAttrString (
Instance, (
char *) name.c_str ());
153 if (!attribute)
return 0;
155 s_int32 value = PyInt_AsLong (attribute);
156 Py_DECREF (attribute);
169 PyObject *attribute = PyObject_GetAttrString (
Instance, (
char *) name.c_str ());
170 if (!attribute)
return 0;
172 string value = PyString_AsString (attribute);
173 Py_DECREF (attribute);
185 if (PyObject_SetAttrString (
Instance, (
char *) name.c_str (), value) == -1)
195 PyObject *val = PyInt_FromLong (value);
197 if (PyObject_SetAttrString (
Instance, (
char *) name.c_str (), val) == -1)
210 PyObject *val = PyString_FromString (value.c_str ());
212 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.