Scribus
Open source desktop publishing at your fingertips
pythonize.h
1 /* copyright 2003 Jim Bublitz <jbublitz@nwinternet.com>
2 
3  This library is free software; you can redistribute it and/or
4  modify it under the terms of the GNU General Public License as
5  published by the Free Software Foundation; either version 2 of
6  the License, or (at your option) any later version.
7 
8  This library is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  Library General Public License for more details.
12 
13  You should have received a copy of the GNU Library General Public License
14  along with this library; see the file COPYING.LIB. If not, write to
15  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16  Boston, MA 02110-1301, USA.
17 */
18 
19 #ifndef __pythonize_h__
20 #define __pythonize_h__
21 
22 // Pythonize is a general purpose library that wraps the Python
23 // interpreter with an interface to a set of common operations
24 // used when embedding the interpreter.
25 
26 #include <Python.h>
27 
28 struct ObjectRef
29 {
30  ObjectRef (ObjectRef *oi, PyObject *o);
31  ~ObjectRef () {
32  Py_XDECREF (object);
33  }
34 
35  PyObject *object; // pointer to an object we created
36  ObjectRef *prevObject; // pointer to next object on the stack
37 };
38 
39 class Pythonize
40 {
41 public:
42  Pythonize ();
43  ~Pythonize ();
44 
45  // adds a path to sys.path
46  bool appendToSysPath (const char* newPath);
47 
48  // imports a module into the interpreter
49  // or gets a PyObject for an already loaded module
50  PyObject *importModule (char *moduleName);
51 
52  // returns an object from a loaded module
53  // you must decref the object returned when done with it (new reference returned)
54  PyObject *getNewObjectRef (PyObject *module, char *object) {
55  return PyObject_GetAttrString (module, object);
56  }
57 
58  int getPythonInit () {
59  return pythonInit;
60  }
61 
62  // decrements the ref count of an object
63  void decref (PyObject *object) {
64  Py_XDECREF (object);
65  }
66 
67  // runs a script on the current sys.path
68  bool runScript (char *scriptPath);
69 
70  // executes a string of Python in the interpreter
71  bool runString (char *str);
72 
73  // runs a callable Python object
74  PyObject *runFunction(PyObject *object, PyObject *args);
75  void *runFunctionVoid(PyObject *object, PyObject *args);
76 
77 private:
78  int pythonInit; // status of Py_Initialize
79  ObjectRef *objects; // a stack of PyObjects (used in destructor)
80 };
81 
82 extern "C" {
83  Pythonize *initialize();
84  void finalize();
85 
86  // adds a path to sys.path
87  bool appendToSysPath (const char* newPath);
88 
89  // imports a module into the interpreter
90  // or gets a PyObject for an already loaded module
91  PyObject *importModule (char *moduleName);
92 
93  // returns an object from a loaded module
94  // you must decref the object returned when done with it (new reference returned)
95  PyObject *getNewObjectRef (PyObject *module, char *object);
96 
97  bool getPythonInit();
98 
99  // decrements the ref count of an object
100  void decref (PyObject *object);
101 
102  // runs a script on the current sys.path
103  bool runScript (char *scriptPath);
104 
105  // executes a string of Python in the interpreter
106  bool runString (char *str);
107 
108  // runs a callable Python object
109  PyObject *runFunction (PyObject *object, PyObject *args);
110 }
111 
112 #endif
Definition: pythonize.h:39
Definition: pythonize.h:28