Adonthell
0.4
|
00001 /* 00002 $Id: py_object.cc,v 1.17 2003/05/18 21:54:20 ksterker Exp $ 00003 00004 Copyright (C) 1999/2000/2001/2003 Kai Sterker 00005 Copyright (C) 2001 Alexandre Courbot 00006 Part of the Adonthell Project http://adonthell.linuxgames.com 00007 00008 This program is free software; you can redistribute it and/or modify 00009 it under the terms of the GNU General Public License. 00010 This program is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY. 00012 00013 See the COPYING file for more details. 00014 */ 00015 00016 00017 /** 00018 * @file py_object.cc 00019 * @author Kai Sterker <kaisterker@linuxgames.com> 00020 * @author Alexandre Courbot <alexandrecourbot@linuxgames.com> 00021 * 00022 * @brief Defines the py_object class. 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 // Cleanup (and re-initialisation) 00042 void py_object::clear () 00043 { 00044 // Delete our Instance 00045 Py_XDECREF (Instance); 00046 Instance = NULL; 00047 00048 Filename = ""; 00049 Classname = ""; 00050 } 00051 00052 // Pass a (new) Python module to be used 00053 bool py_object::create_instance (string file, string classname, PyObject * args) 00054 { 00055 // Try to import the given script 00056 PyObject *module = python::import_module (file); 00057 if (!module) return false; 00058 00059 // Instanciate! 00060 return instanciate (module, file, classname, args); 00061 } 00062 00063 // Reload a python module in case it has changed on disk 00064 bool py_object::reload_instance (string file, string classname, PyObject * args) 00065 { 00066 // Try to import the given script 00067 PyObject *module = python::import_module (file); 00068 if (!module) return false; 00069 00070 // Now Reload 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 // Instanciate the given class from the module 00079 bool py_object::instanciate (PyObject *module, string file, string classname, PyObject * args) 00080 { 00081 // Cleanup 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 // Create the Instance 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 // Execute a method of the script 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 // check for a certain attribute 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 // Get an attribute of the instance 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 // Get an int attribute of the instance 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 // Get a string attribute of the instance 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 // Set an attribute of the instance 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 // Set an int attribute of the instance 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 // Set a string attribute of the instance 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 }