Adonthell  0.4
python_class.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2001 Kai Sterker <kai.sterker@gmail.com>
3  Part of the Adonthell Project <http://adonthell.nongnu.org>
4 
5  Adonthell is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  Adonthell is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with Adonthell. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 
20 /**
21  * @file python_class.h
22  * @author Kai Sterker <kai.sterker@gmail.com>
23  *
24  * @brief Defines the python class. This file is named this way
25  * so it doesn't conflicts with Python.h Python's include
26  * file on non-case aware systems.
27  *
28  *
29  */
30 
31 
32 #ifndef PYTHON_CLASS_H__
33 #define PYTHON_CLASS_H__
34 
35 #include <locale>
36 #include "Python.h"
37 #include "compile.h"
38 #include "eval.h"
39 #include "node.h"
40 #include "fileops.h"
41 
42 #ifndef SWIG
43 #if PY_MAJOR_VERSION >= 3
44 #ifndef PyInt_AsLong
45 #define PyInt_AsLong PyLong_AsLong
46 #endif
47 
48 #ifndef PyInt_Check
49 #define PyInt_Check PyLong_Check
50 #endif
51 
52 #ifndef PyInt_FromLong
53 #define PyInt_FromLong PyLong_FromLong
54 #endif
55 
56 #ifndef PyString_Check
57 #define PyString_Check PyUnicode_Check
58 #endif
59 
60 #ifndef PyString_FromString
61 #define PyString_FromString PyUnicode_FromString
62 #endif
63 #endif
64 #endif
65 
66 /**
67  * Grant simplified access to the Python interpreter.
68  *
69  */
70 class python
71 {
72 public:
73  /**
74  * Initialise Python and insert the Adonthell include paths.
75  *
76  *
77  * @return true in case of success, false otherwise.
78  */
79  static void init ();
80 
81  /**
82  * Cleanup Python.
83  *
84  */
85  static void cleanup ();
86 
87  /**
88  * Adds a directory to Python's include path.
89  *
90  * @param name directory to add to Python's include path.
91  */
92  static void insert_path( char * name);
93 
94  /**
95  * Execute Python statements contained in a string.
96  *
97  * @param s string containing Python statements to execute.
98  */
99  static void exec_string(const char * s);
100 
101  /**
102  * Executes a Python script.
103  *
104  * @param filename name of the file to execute.
105  *
106  * @return true in case of success, false otherwise.
107  */
108  static bool exec_file (string filename);
109 
110  /**
111  * Imports a Python module.
112  *
113  * @param filename file name of the module to import.
114  *
115  * @return pointer to the imported module.
116  */
117  static PyObject *import_module (string filename);
118 
119  /**
120  * Dumps any error information to stderr.
121  *
122  */
123  static void show_traceback( void );
124 
125  /**
126  * Magic function that makes any C object available to Python!
127  *
128  * @param instance pointer to the instance to pass.
129  * @param class_name name of the class of the passed instance.
130  *
131  * @return pointer to the passed %object.
132  */
133  static PyObject *pass_instance (void* instance, const char* class_name);
134 
135  /**
136  * Loads a Python tuple previously saved with put_tuple ().
137  *
138  * @param file Opened file where to load the tuple from.
139  *
140  * @return Restored Python tuple.
141  */
142  static PyObject * get_tuple (igzstream & file);
143 
144  /**
145  * Save a Python tuple into a file.
146  *
147  * @warning The Python tuple MUST ONLY be composed of Python strings
148  * or integers!
149  *
150  * @param tuple Python tuple to save.
151  * @param file Opened file where to save the tuple to.
152  */
153  static void put_tuple (PyObject * tuple, ogzstream & file);
154 
155  static PyObject *module;
156 
157  static string as_string(PyObject *s);
158 };
159 
160 #ifndef SWIG
161 namespace data
162 {
163  /**
164  * Global namespace to use in scripts.
165  *
166  */
167  extern PyObject *globals;
168 }
169 #endif
170 
171 #endif // PYTHON_CLASS_H__
python::exec_file
static bool exec_file(string filename)
Executes a Python script.
Definition: python_class.cc:87
igzstream
Class to read data from a Gzip compressed file.
Definition: fileops.h:135
python::show_traceback
static void show_traceback(void)
Dumps any error information to stderr.
Definition: python_class.cc:107
ogzstream
Class to write data from a Gzip compressed file.
Definition: fileops.h:227
python::insert_path
static void insert_path(char *name)
Adds a directory to Python's include path.
Definition: python_class.cc:64
fileops.h
Declares the igzstream, ogzstream and fileops classes.
python::get_tuple
static PyObject * get_tuple(igzstream &file)
Loads a Python tuple previously saved with put_tuple ().
Definition: python_class.cc:134
python::put_tuple
static void put_tuple(PyObject *tuple, ogzstream &file)
Save a Python tuple into a file.
Definition: python_class.cc:167
python::cleanup
static void cleanup()
Cleanup Python.
Definition: python_class.cc:52
python::exec_string
static void exec_string(const char *s)
Execute Python statements contained in a string.
Definition: python_class.cc:79
python::import_module
static PyObject * import_module(string filename)
Imports a Python module.
Definition: python_class.cc:117
python::pass_instance
static PyObject * pass_instance(void *instance, const char *class_name)
Magic function that makes any C object available to Python!
Definition: python_class.cc:128
python
Grant simplified access to the Python interpreter.
Definition: python_class.h:70
python::init
static void init()
Initialise Python and insert the Adonthell include paths.
Definition: python_class.cc:44