00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "py_object.h"
00028
00029 py_object::py_object ()
00030 {
00031 Instance = NULL;
00032 Filename = "";
00033 Classname = "";
00034 }
00035
00036 py_object::~py_object ()
00037 {
00038 clear ();
00039 }
00040
00041
00042 void py_object::clear ()
00043 {
00044
00045 Py_XDECREF (Instance);
00046 Instance = NULL;
00047
00048 Filename = "";
00049 Classname = "";
00050 }
00051
00052
00053 bool py_object::create_instance (string file, string classname, PyObject * args)
00054 {
00055
00056 PyObject *module = python::import_module (file);
00057 if (!module) return false;
00058
00059
00060 return instanciate (module, file, classname, args);
00061 }
00062
00063
00064 bool py_object::reload_instance (string file, string classname, PyObject * args)
00065 {
00066
00067 PyObject *module = python::import_module (file);
00068 if (!module) return false;
00069
00070
00071 PyObject *reload = PyImport_ReloadModule (module);
00072 Py_DECREF (module);
00073 if (!reload) return false;
00074
00075 return instanciate (reload, file, classname, args);
00076 }
00077
00078
00079 bool py_object::instanciate (PyObject *module, string file, string classname, PyObject * args)
00080 {
00081
00082 clear ();
00083
00084 PyObject * classobj = PyObject_GetAttrString (module, (char *) classname.c_str ());
00085 Py_DECREF (module);
00086 if (!classobj)
00087 {
00088 python::show_traceback ();
00089 return false;
00090 }
00091
00092
00093 Instance = PyObject_CallObject (classobj, args);
00094 Py_DECREF (classobj);
00095 if (!Instance)
00096 {
00097 python::show_traceback ();
00098 return false;
00099 }
00100
00101 Filename = file;
00102 Classname = classname;
00103
00104 return true;
00105 }
00106
00107
00108 PyObject* py_object::call_method_ret (const string &name, PyObject *args) const
00109 {
00110 PyObject *result = NULL;
00111
00112 if (Instance)
00113 {
00114 PyObject *tocall = PyObject_GetAttrString (Instance, (char *) name.c_str ());
00115
00116 if (PyCallable_Check (tocall) == 1)
00117 {
00118 result = PyObject_CallObject (tocall, args);
00119 Py_DECREF (tocall);
00120 }
00121 #ifdef PY_DEBUG
00122 python::show_traceback ();
00123 #endif
00124 }
00125
00126 return result;
00127 }
00128
00129
00130 bool py_object::has_attribute (const std::string & name)
00131 {
00132 if (Instance)
00133 return PyObject_HasAttrString (Instance, (char *) name.c_str ());
00134 else
00135 return false;
00136 }
00137
00138
00139 PyObject *py_object::get_attribute (const string &name) const
00140 {
00141 if (Instance)
00142 return PyObject_GetAttrString (Instance, (char *) name.c_str ());
00143 else
00144 return NULL;
00145 }
00146
00147
00148 s_int32 py_object::get_attribute_int (const string &name)
00149 {
00150 if (Instance)
00151 {
00152 PyObject *attribute = PyObject_GetAttrString (Instance, (char *) name.c_str ());
00153 if (!attribute) return 0;
00154
00155 s_int32 value = PyInt_AsLong (attribute);
00156 Py_DECREF (attribute);
00157
00158 return value;
00159 }
00160 else
00161 return 0;
00162 }
00163
00164
00165 string py_object::get_attribute_string (const string &name)
00166 {
00167 if (Instance)
00168 {
00169 PyObject *attribute = PyObject_GetAttrString (Instance, (char *) name.c_str ());
00170 if (!attribute) return 0;
00171
00172 string value = PyString_AsString (attribute);
00173 Py_DECREF (attribute);
00174
00175 return value;
00176 }
00177 else
00178 return string ("");
00179 }
00180
00181
00182 void py_object::set_attribute (const string &name, PyObject *value)
00183 {
00184 if (Instance)
00185 if (PyObject_SetAttrString (Instance, (char *) name.c_str (), value) == -1)
00186 python::show_traceback ();
00187 else return;
00188 }
00189
00190
00191 void py_object::set_attribute_int (const string &name, int value)
00192 {
00193 if (Instance)
00194 {
00195 PyObject *val = PyInt_FromLong (value);
00196
00197 if (PyObject_SetAttrString (Instance, (char *) name.c_str (), val) == -1)
00198 python::show_traceback ();
00199
00200 Py_DECREF (val);
00201 }
00202 else return;
00203 }
00204
00205
00206 void py_object::set_attribute_string (const string &name, const string & value)
00207 {
00208 if (Instance)
00209 {
00210 PyObject *val = PyString_FromString (value.c_str ());
00211
00212 if (PyObject_SetAttrString (Instance, (char *) name.c_str (), val) == -1)
00213 python::show_traceback ();
00214
00215 Py_DECREF (val);
00216 }
00217 else return;
00218 }