Adonthell  0.4
py_object.cc
Go to the documentation of this file.
1 /*
2  Copyright (C) 1999/2000/2001/2003 Kai Sterker
3  Copyright (C) 2001 Alexandre Courbot
4  Part of the Adonthell Project <http://adonthell.nongnu.org>
5 
6  Adonthell is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  Adonthell is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with Adonthell. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 
21 /**
22  * @file py_object.cc
23  * @author Kai Sterker <kai.sterker@gmail.com>
24  * @author Alexandre Courbot <alexandrecourbot@linuxgames.com>
25  *
26  * @brief Defines the py_object class.
27  *
28  *
29  */
30 
31 #include "py_object.h"
32 
34 {
35  Instance = NULL;
36  Filename = "";
37  Classname = "";
38 }
39 
41 {
42  clear ();
43 }
44 
45 // Cleanup (and re-initialisation)
47 {
48  // Delete our Instance
49  Py_XDECREF (Instance);
50  Instance = NULL;
51 
52  Filename = "";
53  Classname = "";
54 }
55 
56 // Pass a (new) Python module to be used
57 bool py_object::create_instance (string file, string classname, PyObject * args)
58 {
59  // Try to import the given script
60  PyObject *module = python::import_module (file);
61  if (!module) return false;
62 
63  // Instanciate!
64  return instanciate (module, file, classname, args);
65 }
66 
67 // Reload a python module in case it has changed on disk
68 bool py_object::reload_instance (string file, string classname, PyObject * args)
69 {
70  // Try to import the given script
71  PyObject *module = python::import_module (file);
72  if (!module) return false;
73 
74  // Now Reload
75  PyObject *reload = PyImport_ReloadModule (module);
76  Py_DECREF (module);
77  if (!reload) return false;
78 
79  return instanciate (reload, file, classname, args);
80 }
81 
82 // Instanciate the given class from the module
83 bool py_object::instanciate (PyObject *module, string file, string classname, PyObject * args)
84 {
85  // Cleanup
86  clear ();
87 
88  PyObject * classobj = PyObject_GetAttrString (module, (char *) classname.c_str ());
89  Py_DECREF (module);
90  if (!classobj)
91  {
93  return false;
94  }
95 
96  // Create the Instance
97  Instance = PyObject_CallObject (classobj, args);
98  Py_DECREF (classobj);
99  if (!Instance)
100  {
102  return false;
103  }
104 
105  Filename = file;
106  Classname = classname;
107 
108  return true;
109 }
110 
111 // Execute a method of the script
112 PyObject* py_object::call_method_ret (const string &name, PyObject *args) const
113 {
114  PyObject *result = NULL;
115 
116  if (Instance)
117  {
118  PyObject *tocall = PyObject_GetAttrString (Instance, (char *) name.c_str ());
119 
120  if (PyCallable_Check (tocall) == 1)
121  {
122  result = PyObject_CallObject (tocall, args);
123  Py_DECREF (tocall);
124  }
125 #ifdef PY_DEBUG
127 #endif
128  }
129 
130  return result;
131 }
132 
133 // check for a certain attribute
134 bool py_object::has_attribute (const std::string & name)
135 {
136  if (Instance)
137  return PyObject_HasAttrString (Instance, (char *) name.c_str ());
138  else
139  return false;
140 }
141 
142 // Get an attribute of the instance
143 PyObject *py_object::get_attribute (const string &name) const
144 {
145  if (Instance)
146  return PyObject_GetAttrString (Instance, (char *) name.c_str ());
147  else
148  return NULL;
149 }
150 
151 // Get an int attribute of the instance
153 {
154  if (Instance)
155  {
156  PyObject *attribute = PyObject_GetAttrString (Instance, (char *) name.c_str ());
157  if (!attribute) return 0;
158 
159  s_int32 value = PyInt_AsLong (attribute);
160  Py_DECREF (attribute);
161 
162  return value;
163  }
164  else
165  return 0;
166 }
167 
168  // Get a string attribute of the instance
169 string py_object::get_attribute_string (const string &name)
170 {
171  if (Instance)
172  {
173  PyObject *attribute = PyObject_GetAttrString (Instance, (char *) name.c_str ());
174  if (!attribute) return 0;
175 
176  string value = python::as_string (attribute);
177  Py_DECREF (attribute);
178 
179  return value;
180  }
181  else
182  return string ("");
183 }
184 
185 // Set an attribute of the instance
186 void py_object::set_attribute (const string &name, PyObject *value)
187 {
188  if (Instance)
189  if (PyObject_SetAttrString (Instance, (char *) name.c_str (), value) == -1)
191 }
192 
193 // Set an int attribute of the instance
194 void py_object::set_attribute_int (const string &name, int value)
195 {
196  if (Instance)
197  {
198  PyObject *val = PyInt_FromLong (value);
199 
200  if (PyObject_SetAttrString (Instance, (char *) name.c_str (), val) == -1)
202 
203  Py_DECREF (val);
204  }
205  else return;
206 }
207 
208 // Set a string attribute of the instance
209 void py_object::set_attribute_string (const string &name, const string & value)
210 {
211  if (Instance)
212  {
213  PyObject *val = PyString_FromString (value.c_str ());
214 
215  if (PyObject_SetAttrString (Instance, (char *) name.c_str (), val) == -1)
217 
218  Py_DECREF (val);
219  }
220  else return;
221 }
#define s_int32
32 bits long signed integer
Definition: types.h:50
PyObject * call_method_ret(const string &name, PyObject *args=NULL) const
Call a method of this object.
Definition: py_object.cc:112
void clear()
Resets the script to it&#39;s post-constructor state.
Definition: py_object.cc:46
py_object()
Default constructor.
Definition: py_object.cc:33
~py_object()
Destructor.
Definition: py_object.cc:40
string get_attribute_string(const string &name)
Returns the given attribute as string value.
Definition: py_object.cc:169
void set_attribute(const string &name, PyObject *value)
Assign a new attribute to the module, overriding an existing attribute of the same name...
Definition: py_object.cc:186
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...
Definition: py_object.cc:194
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.
Definition: py_object.cc:57
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.
Definition: py_object.cc:143
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...
Definition: py_object.cc:209
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...
Definition: py_object.cc:68
PyObject * Instance
The python class instance wrapped by py_object.
Definition: py_object.h:244
s_int32 get_attribute_int(const string &name)
Returns the given attribute as integer value.
Definition: py_object.cc:152
bool has_attribute(const std::string &name)
Tests whether the object contains a certain attribute (i.e.
Definition: py_object.cc:134