Adonthell  0.4
python_class.cc
Go to the documentation of this file.
1 /*
2  $Id: python_class.cc,v 1.12 2005/04/17 11:35:12 ksterker Exp $
3 
4  Copyright (C) 2001 Kai Sterker <kaisterker@linuxgames.com>
5  Part of the Adonthell Project http://adonthell.linuxgames.com
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 
16 /**
17  * @file python_class.cc
18  * @author Kai Sterker <kaisterker@linuxgames.com>
19  *
20  * @brief Declares the python class.
21  *
22  *
23  */
24 
25 #include "python_class.h"
26 #include "game.h"
27 #include <iostream>
28 
29 PyObject *data::globals;
30 PyObject *python::module;
31 
32 // defined in py_adonthell_wrap.cc
33 PyObject *get_py_obj (void *instance, const char* class_name);
34 
35 using namespace std;
36 
37 /*
38  * Start Python
39  */
40 void python::init ()
41 {
42  Py_Initialize ();
43 }
44 
45 /**
46  * Stop Python
47  */
49 {
50  // Cleanup the global namespace of python interpreter
51  // Note that we don't have to DECREF data::globals, because they're a
52  // borrowed reference of py_module.
53  Py_XDECREF (module);
54  Py_Finalize ();
55 }
56 
57 /*
58  * Insert a string into the module search path.
59  */
60 void python::insert_path( char *name )
61 {
62  char buf[256];
63 
64  sprintf ( buf, "import sys ; sys.path.insert(0, \"%s\")", name );
65  PyRun_SimpleString ( buf );
66 }
67 
68 /*
69  * Some convenience functions
70  */
71 
72 /*
73  * Executes the Python statements in the string
74  */
75 void python::exec_string(char * s)
76 {
77  PyRun_SimpleString(s);
78 }
79 
80 /*
81  * Execute the file given by 'filename'
82  */
83 bool python::exec_file (string filename)
84 {
85  PyObject *mod = python::import_module (filename);
86 
87  if (!mod)
88  {
89  cerr << "exec_file: " << filename << " load failed: " << endl;
90  show_traceback ();
91 
92  return false;
93  }
94 
95  Py_DECREF (mod);
96 
97  return true;
98 }
99 
100 /*
101  * Dump any error information to stderr
102  */
104 {
105  if ( PyErr_Occurred() )
106  {
107  PyErr_Print();
108  fflush (stderr);
109  }
110 }
111 
112 /* Import a module, return module ptr */
113 PyObject *python::import_module (string filename)
114 {
115  PyObject *result = PyImport_ImportModule ((char *) filename.c_str ());
116 
117 #ifdef PY_DEBUG
118  show_traceback ();
119 #endif
120  return result;
121 }
122 
123 // Make a C++ instance available to Python
124 PyObject *python::pass_instance (void *instance, const char *class_name)
125 {
126  string class_ptr = string(class_name) + "*";
127  return get_py_obj (instance, class_ptr.c_str());
128 }
129 
130 PyObject * python::get_tuple (igzstream & file)
131 {
132  PyObject * tuple;
133  u_int32 l;
134  l << file;
135 
136  tuple = PyTuple_New (l);
137 
138  for (u_int32 i = 0; i < l; i++)
139  {
140  string ms;
141  u_int32 j;
142  char c;
143 
144  c << file;
145  switch (c)
146  {
147  case 's':
148  ms << file;
149  // Stolen reference
150  PyTuple_SetItem (tuple, i, PyString_FromString (ms.c_str ()));
151  break;
152 
153  case 'i':
154  j << file;
155  // Stolen reference
156  PyTuple_SetItem (tuple, i, PyInt_FromLong (j));
157  break;
158  }
159  }
160  return tuple;
161 }
162 
163 void python::put_tuple (PyObject * tuple, ogzstream & file)
164 {
165  u_int32 l = PyTuple_Size (tuple);
166  l >> file;
167  for (u_int32 i = 0; i < l; i++)
168  {
169  // Borrowed reference
170  PyObject * item = PyTuple_GetItem (tuple, i);
171 
172  // Check for the type of this object
173  // String?
174  if (PyString_Check (item))
175  {
176  's' >> file;
177  char * s = PyString_AsString (item);
178  string (s) >> file;
179  }
180 
181  // Integer?
182  else if (PyInt_Check (item))
183  {
184  'i' >> file;
185  u_int32 li = PyInt_AsLong (item);
186  li >> file;
187  }
188  }
189 }
Class to write data from a Gzip compressed file.
Definition: fileops.h:223
static bool exec_file(string filename)
Executes a Python script.
Definition: python_class.cc:83
Class to read data from a Gzip compressed file.
Definition: fileops.h:131
Definition: str_hash.h:36
Declares the game class.
static void put_tuple(PyObject *tuple, ogzstream &file)
Save a Python tuple into a file.
#define u_int32
32 bits long unsigned integer
Definition: types.h:35
static void show_traceback(void)
Dumps any error information to stderr.
static PyObject * get_tuple(igzstream &file)
Loads a Python tuple previously saved with put_tuple ().
static PyObject * import_module(string filename)
Imports a Python module.
static PyObject * pass_instance(void *instance, const char *class_name)
Magic function that makes any C object available to Python!
Defines the python class. This file is named this way so it doesn't conflicts with Python...
static void exec_string(char *s)
Execute Python statements contained in a string.
Definition: python_class.cc:75
static void insert_path(char *name)
Adds a directory to Python's include path.
Definition: python_class.cc:60
static void cleanup()
Cleanup Python.
Definition: python_class.cc:48
static void init()
Initialise Python and insert the Adonthell include paths.
Definition: python_class.cc:40