Subversion Repositories Scribus

Rev

Rev 17560 | Rev 22499 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
14647 cbradney 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 Library General Public
5
   License as published by the Free Software Foundation; either
6
   version 2 of 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
18122 mrdocs 15
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16
   Boston, MA 02110-1301, USA.
14647 cbradney 17
*/
18
 
19
#include <stdarg.h>
20
#include <string.h>
21
 
22
// this just gets rid of a warning when Python.h redefines it
23
#undef _POSIX_C_SOURCE
24
 
25
#include <pythonize.h>
26
 
27
#define debug 1
28
 
29
ObjectRef::ObjectRef (ObjectRef *oi, PyObject *o)
30
{
31
    prevObject = oi;
32
    object     = o;
33
}
34
 
35
 
36
Pythonize::Pythonize ()
37
{
38
    pythonInit  = 1;
39
    objects    = NULL;
40
 
41
    if (debug) printf ("\n\nPythonize constructor -- pid = %i\n", getpid ());
42
 
43
    if (!Py_IsInitialized ())
44
    {
45
        PyEval_InitThreads ();
46
        Py_Initialize ();
47
        if (!Py_IsInitialized ())
48
        {
49
            pythonInit = 0;
50
            return;
51
        }
52
 
53
        if (debug) printf ("Python interpreter initialized!\n\n");
54
 
55
        // free the lock
56
        PyEval_ReleaseLock();
57
    }
58
}
59
 
60
bool Pythonize::runScript (char *scriptPath)
61
{
62
    FILE *f;
63
    int res;
64
 
65
    if (debug) printf ("Running script: %s\n", scriptPath);
66
 
67
    if (scriptPath == NULL || strlen (scriptPath) == 0) return false;
68
 
69
    f = fopen (scriptPath, "r");
70
    if (f == NULL) return false;
71
 
72
    res = PyRun_SimpleFile (f, scriptPath);
73
 
74
    fclose (f);
75
    return res == 0;
76
}
77
 
78
 
79
PyObject * Pythonize::runFunction (PyObject *object, PyObject *args)
80
{
81
 
82
    if (!PyCallable_Check (object))
83
        return NULL;
84
 
85
    PyObject *res = PyObject_CallObject (object, args ? args : PyTuple_New (0));
86
    Py_XINCREF (res);
87
 
88
    return res;
89
}
90
 
91
void * Pythonize::runFunctionVoid (PyObject *object, PyObject *args)
92
{
93
 
94
    if (!PyCallable_Check (object))
95
        return NULL;
96
 
97
    PyObject *pyRes = PyObject_CallObject (object, args ? args : PyTuple_New (0));
98
    void *res = PyLong_AsVoidPtr (pyRes);
99
 
100
    return res;
101
}
102
 
103
bool Pythonize::runString (char *str)
104
{
105
 
106
    if (str == NULL || strlen (str) == 0) return false;
107
 
108
    int res = PyRun_SimpleString (str);
109
 
110
    return res == 0;
111
}
112
 
113
bool Pythonize::appendToSysPath (const char* newPath)
114
{
115
    if (newPath == NULL || strlen (newPath) == 0) return false;
116
 
117
    char *fmtString = "import sys\nif not '%s' in sys.path:\n\tsys.path.append ('%s')\n"; //print sys.path\n";
118
    int length      = strlen (fmtString) + 2*strlen (newPath) + 1;
119
    char *line      =  new char [length];
120
    if (!line) return false;
121
    snprintf (line, length, fmtString, newPath, newPath);
122
 
123
    int res = PyRun_SimpleString (line);
124
 
17560 jainbasil 125
    delete line;
14647 cbradney 126
    return res == 0;
127
}
128
 
129
PyObject *Pythonize::importModule (char *moduleName)
130
{
131
    if (moduleName == NULL || strlen (moduleName) == 0) return NULL;
132
 
133
    PyObject *module = PyImport_ImportModule (moduleName);
134
 
135
    objects = new ObjectRef (objects, module);
136
    if (!objects) return NULL;
137
 
138
    return module;
139
}
140
 
141
 
142
Pythonize::~Pythonize ()
143
{
144
 
145
    if (debug) printf ("Pythonize destructor\n");
146
    ObjectRef *top;
147
 
148
    while (objects)
149
    {
150
        top = objects;
151
        objects = objects->prevObject;
152
        delete top;
153
    }
154
    if (debug) printf (" --- Objects destroyed\n");
155
 
156
    Py_Finalize();
157
 
158
    if (debug) printf (" --- Py_Finalized\n");
159
}
160
 
161
//// XXX: disabled
162
//// The global Pythonize instance
163
//Pythonize *pyize = new Pythonize ();
164
 
165
extern "C"
166
{
167
    Pythonize *_pythonize;
168
 
169
    Pythonize *initialize ()
170
    {
171
        if (_pythonize) return _pythonize;
172
 
173
        _pythonize = new Pythonize ();
174
        if (!_pythonize || !_pythonize->getPythonInit ())
175
        {
176
            if (_pythonize) delete _pythonize;
177
            return NULL;
178
        }
179
 
180
        return _pythonize;
181
    }
182
 
183
    void finalize ()
184
    {
185
        if (_pythonize) {
186
            if (debug) printf(" --- Pythonize finalize()\n");
187
            delete _pythonize;
188
        }
189
    }
190
 
191
    // adds a path to sys.path
192
    bool appendToSysPath (const char* newPath)
193
    {
194
        return _pythonize ? _pythonize->appendToSysPath (newPath) : false;
195
    }
196
 
197
    // imports a module into the interpreter
198
    // or gets a PyObject for an already loaded module
199
    PyObject *importModule (char *moduleName)
200
    {
201
        return _pythonize ? _pythonize->importModule (moduleName) : NULL;
202
    }
203
 
204
    // returns an object from a loaded module
205
    // you must decref the object returned when done with it (new reference returned)
206
    PyObject *getNewObjectRef (PyObject *module, char *object)
207
    {
208
        return _pythonize ? _pythonize->getNewObjectRef (module, object) : NULL;
209
    }
210
 
211
    bool getPythonInit ()
212
    {
213
        return _pythonize ? _pythonize->getPythonInit () : false;
214
    }
215
 
216
    // decrements the ref count of an object
217
    void decref (PyObject *object)
218
    {
219
        Py_XDECREF (object);
220
    }
221
 
222
    // runs a script on the current sys.path
223
    bool runScript (char *scriptPath)
224
    {
225
        return _pythonize ? _pythonize->runScript (scriptPath) : false;
226
    }
227
 
228
    // executes a string of Python in the interpreter
229
    bool runString (char *str)
230
    {
231
        return _pythonize ? _pythonize->runString (str) : false;
232
    }
233
 
234
    // runs a callable Python object
235
    PyObject *runFunction (PyObject *object, PyObject *args)
236
    {
237
        return _pythonize ? _pythonize->runFunction (object, args) : NULL;
238
    }
239
}