~ubuntu-branches/ubuntu/maverick/freecad/maverick

« back to all changes in this revision

Viewing changes to src/Base/Interpreter.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Teemu Ikonen
  • Date: 2009-07-16 18:37:41 UTC
  • Revision ID: james.westby@ubuntu.com-20090716183741-oww9kcxqrk991i1n
Tags: upstream-0.8.2237
ImportĀ upstreamĀ versionĀ 0.8.2237

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   (c) Jļæ½rgen Riegel (juergen.riegel@web.de) 2002                        *
 
3
 *                                                                         *
 
4
 *   This file is part of the FreeCAD CAx development system.              *
 
5
 *                                                                         *
 
6
 *   This program is free software; you can redistribute it and/or modify  *
 
7
 *   it under the terms of the GNU Library General Public License (LGPL)   *
 
8
 *   as published by the Free Software Foundation; either version 2 of     *
 
9
 *   the License, or (at your option) any later version.                   *
 
10
 *   for detail see the LICENCE text file.                                 *
 
11
 *                                                                         *
 
12
 *   FreeCAD is distributed in the hope that it will be useful,            *
 
13
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 
14
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 
15
 *   GNU Library General Public License for more details.                  *
 
16
 *                                                                         *
 
17
 *   You should have received a copy of the GNU Library General Public     *
 
18
 *   License along with FreeCAD; if not, write to the Free Software        *
 
19
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
 
20
 *   USA                                                                   *
 
21
 *                                                                         *
 
22
 *   Juergen Riegel 2002                                                   *
 
23
 ***************************************************************************/
 
24
 
 
25
 
 
26
#include "PreCompiled.h"
 
27
 
 
28
#ifndef _PreComp_
 
29
#   include <Python.h>
 
30
#   include <sstream>
 
31
#endif
 
32
 
 
33
#include "Console.h"
 
34
#include "Interpreter.h"
 
35
#include "FileInfo.h"
 
36
#include "Stream.h"
 
37
#include "PyTools.h"
 
38
#include "Exception.h"
 
39
#include "PyObjectBase.h"
 
40
 
 
41
 
 
42
char format2[1024];  //Warning! Can't go over 512 characters!!!
 
43
unsigned int format2_len = 1024;
 
44
 
 
45
using namespace Base;
 
46
 
 
47
#if PY_VERSION_HEX <= 0x02050000
 
48
#error "Use Python2.5.x or higher"
 
49
#endif
 
50
 
 
51
 
 
52
PyException::PyException(void)
 
53
{
 
54
    PP_Fetch_Error_Text();    /* fetch (and clear) exception */
 
55
    std::string prefix = PP_last_error_type; /* exception name text */
 
56
//  prefix += ": ";
 
57
    std::string error = PP_last_error_info;            /* exception data text */
 
58
#if 0
 
59
    // The Python exceptions might be thrown from nested functions, so take
 
60
    // into account not to add the same prefix several times
 
61
    std::string::size_type pos = error.find(prefix);
 
62
    if (pos == std::string::npos)
 
63
        _sErrMsg = prefix + error;
 
64
    else
 
65
        _sErrMsg = error;
 
66
#endif
 
67
    _sErrMsg = error;
 
68
    _errorType = prefix;
 
69
 
 
70
 
 
71
    _stackTrace = PP_last_error_trace;     /* exception traceback text */
 
72
 
 
73
    PyErr_Clear(); // must be called to keep Python interpreter in a valid state (Werner)
 
74
 
 
75
}
 
76
 
 
77
// ---------------------------------------------------------
 
78
 
 
79
SystemExitException::SystemExitException()
 
80
{
 
81
    _sErrMsg = "System exit";
 
82
}
 
83
 
 
84
SystemExitException::SystemExitException(const SystemExitException &inst)
 
85
        : Exception(inst)
 
86
{
 
87
}
 
88
 
 
89
// ---------------------------------------------------------
 
90
 
 
91
 
 
92
InterpreterSingleton::InterpreterSingleton()
 
93
{
 
94
    //Py_Initialize();
 
95
}
 
96
 
 
97
InterpreterSingleton::~InterpreterSingleton()
 
98
{
 
99
 
 
100
}
 
101
 
 
102
 
 
103
std::string InterpreterSingleton::runString(const char *sCmd)
 
104
{
 
105
    PyObject *module, *dict, *presult;          /* "exec code in d, d" */
 
106
 
 
107
    PyGILStateLocker locker;
 
108
    module = PP_Load_Module("__main__");         /* get module, init python */
 
109
    if (module == NULL)
 
110
        throw PyException();                         /* not incref'd */
 
111
    dict = PyModule_GetDict(module);            /* get dict namespace */
 
112
    if (dict == NULL)
 
113
        throw PyException();                           /* not incref'd */
 
114
 
 
115
 
 
116
    presult = PyRun_String(sCmd, Py_file_input, dict, dict); /* eval direct */
 
117
    if (!presult) {
 
118
        throw PyException();
 
119
    }
 
120
 
 
121
    PyObject* repr = PyObject_Repr(presult);
 
122
    Py_DECREF(presult);
 
123
    if (repr) {
 
124
        std::string ret(PyString_AsString(repr));
 
125
        Py_DECREF(repr);
 
126
        return ret;
 
127
    }
 
128
    else {
 
129
        PyErr_Clear();
 
130
        return std::string();
 
131
    }
 
132
}
 
133
 
 
134
void InterpreterSingleton::systemExit(void)
 
135
{
 
136
    /* This code is taken from the original Python code */
 
137
    PyObject *exception, *value, *tb;
 
138
    int exitcode = 0;
 
139
 
 
140
    PyErr_Fetch(&exception, &value, &tb);
 
141
    if (Py_FlushLine())
 
142
        PyErr_Clear();
 
143
    fflush(stdout);
 
144
    if (value == NULL || value == Py_None)
 
145
        goto done;
 
146
    if (PyInstance_Check(value)) {
 
147
        /* The error code should be in the `code' attribute. */
 
148
        PyObject *code = PyObject_GetAttrString(value, "code");
 
149
        if (code) {
 
150
            Py_DECREF(value);
 
151
            value = code;
 
152
            if (value == Py_None)
 
153
                goto done;
 
154
        }
 
155
        /* If we failed to dig out the 'code' attribute,
 
156
           just let the else clause below print the error. */
 
157
    }
 
158
    if (PyInt_Check(value))
 
159
        exitcode = (int)PyInt_AsLong(value);
 
160
    else {
 
161
        PyObject_Print(value, stderr, Py_PRINT_RAW);
 
162
        PySys_WriteStderr("\n");
 
163
        exitcode = 1;
 
164
    }
 
165
done:
 
166
    /* Restore and clear the exception info, in order to properly decref
 
167
     * the exception, value, and traceback.  If we just exit instead,
 
168
     * these leak, which confuses PYTHONDUMPREFS output, and may prevent
 
169
     * some finalizers from running.
 
170
     */
 
171
    PyErr_Restore(exception, value, tb);
 
172
    PyErr_Clear();
 
173
    Py_Exit(exitcode);
 
174
    /* NOTREACHED */
 
175
}
 
176
 
 
177
void InterpreterSingleton::runInteractiveString(const char *sCmd)
 
178
{
 
179
    PyObject *module, *dict, *presult;          /* "exec code in d, d" */
 
180
 
 
181
    PyGILStateLocker locker;
 
182
    module = PP_Load_Module("__main__");         /* get module, init python */
 
183
    if (module == NULL)
 
184
        throw PyException();                         /* not incref'd */
 
185
    dict = PyModule_GetDict(module);            /* get dict namespace */
 
186
    if (dict == NULL)
 
187
        throw PyException();                           /* not incref'd */
 
188
 
 
189
    presult = PyRun_String(sCmd, Py_single_input, dict, dict); /* eval direct */
 
190
    if (!presult) {
 
191
        if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
 
192
            //systemExit();
 
193
            throw SystemExitException();
 
194
        }
 
195
        /* get latest python exception information */
 
196
        /* and print the error to the error output */
 
197
        PyObject *errobj, *errdata, *errtraceback;
 
198
        PyErr_Fetch(&errobj, &errdata, &errtraceback);
 
199
 
 
200
        Exception exc; // do not use PyException since this clears the error indicator
 
201
        if (PyString_Check(errdata))
 
202
            exc.setMessage(PyString_AsString(errdata));
 
203
        PyErr_Restore(errobj, errdata, errtraceback);
 
204
        if (PyErr_Occurred())
 
205
            PyErr_Print();
 
206
        throw exc;
 
207
    }
 
208
    else
 
209
        Py_DECREF(presult);
 
210
}
 
211
 
 
212
void InterpreterSingleton::runFile(const char*pxFileName)
 
213
{
 
214
    FileInfo fi(pxFileName);
 
215
    Base::ifstream file(fi, std::ios::in);
 
216
    if (file) {
 
217
        std::stringbuf buf;
 
218
        file >> &buf;
 
219
        file.close();
 
220
 
 
221
        runString(buf.str().c_str());
 
222
    }
 
223
    else {
 
224
        std::string err = "Unknown file: ";
 
225
        err += pxFileName;
 
226
        err += "\n";
 
227
        throw Exception(err);
 
228
    }
 
229
}
 
230
 
 
231
bool InterpreterSingleton::loadModule(const char* psModName)
 
232
{
 
233
    // buffer acrobatics
 
234
    //PyBuf ModName(psModName);
 
235
    PyObject *module;
 
236
 
 
237
    PyGILStateLocker locker;
 
238
    module = PP_Load_Module(psModName);
 
239
 
 
240
    if (!module)
 
241
        throw PyException();
 
242
 
 
243
    return true;
 
244
}
 
245
 
 
246
void InterpreterSingleton::addType(PyTypeObject* Type,PyObject* Module, const char * Name)
 
247
{
 
248
    // NOTE: To finish the initialization of our own type objects we must
 
249
    // call PyType_Ready, otherwise we run into a segmentation fault, later on.
 
250
    // This function is responsible for adding inherited slots from a type's base class.
 
251
    if (PyType_Ready(Type) < 0) return;
 
252
    union PyType_Object pyType = {Type};
 
253
    PyModule_AddObject(Module, Name, pyType.o);
 
254
}
 
255
 
 
256
void InterpreterSingleton::addPythonPath(const char* Path)
 
257
{
 
258
    PyGILStateLocker locker;
 
259
    PyObject *list = PySys_GetObject("path");
 
260
    PyObject *path = PyString_FromString(Path);
 
261
    PyList_Append(list, path);
 
262
    Py_DECREF(path);
 
263
    PySys_SetObject("path", list);
 
264
}
 
265
 
 
266
const char* InterpreterSingleton::init(int argc,char *argv[])
 
267
{
 
268
    if (!Py_IsInitialized()) {
 
269
        Py_SetProgramName(argv[0]);
 
270
        PyEval_InitThreads();
 
271
        Py_Initialize();
 
272
        PySys_SetArgv(argc, argv);
 
273
        this->_global = PyEval_SaveThread();
 
274
    }
 
275
 
 
276
    return Py_GetPath();
 
277
}
 
278
 
 
279
int InterpreterSingleton::cleanup(void (*func)(void))
 
280
{
 
281
    return Py_AtExit( func );
 
282
}
 
283
 
 
284
void InterpreterSingleton::finalize()
 
285
{
 
286
    try {
 
287
        PyEval_RestoreThread(this->_global);
 
288
        Py_Finalize();
 
289
    }
 
290
    catch (...) {
 
291
    }
 
292
}
 
293
 
 
294
void InterpreterSingleton::runStringArg(const char * psCom,...)
 
295
{
 
296
    // va stuff
 
297
    va_list namelessVars;
 
298
    va_start(namelessVars, psCom);  // Get the "..." vars
 
299
    int len = vsnprintf(format2, format2_len, psCom, namelessVars);
 
300
    va_end(namelessVars);
 
301
    if ( len == -1 ) {
 
302
        // argument too long
 
303
        assert(false);
 
304
    }
 
305
 
 
306
    runString(format2);
 
307
}
 
308
 
 
309
 
 
310
// Singelton:
 
311
 
 
312
InterpreterSingleton * InterpreterSingleton::_pcSingelton = 0;
 
313
 
 
314
InterpreterSingleton & InterpreterSingleton::Instance(void)
 
315
{
 
316
    // not initialized!
 
317
    if (!_pcSingelton)
 
318
        _pcSingelton = new InterpreterSingleton();
 
319
    return *_pcSingelton;
 
320
}
 
321
 
 
322
void InterpreterSingleton::Destruct(void)
 
323
{
 
324
    // not initialized or double destruct!
 
325
    assert(_pcSingelton);
 
326
    delete _pcSingelton;
 
327
    _pcSingelton = 0;
 
328
}
 
329
 
 
330
int InterpreterSingleton::runCommandLine(const char *prompt)
 
331
{
 
332
    PyGILStateLocker locker;
 
333
    return PP_Run_Command_Line(prompt);
 
334
}
 
335
 
 
336
/**
 
337
 *  Runs a member method of an object with no parameter and no return value
 
338
 *  void (void). There are other methods to run with returns
 
339
 */
 
340
void InterpreterSingleton::runMethodVoid(PyObject *pobject, const char *method)
 
341
{
 
342
    PyGILStateLocker locker;
 
343
    if (PP_Run_Method(pobject ,    // object
 
344
                      method,  // run method
 
345
                      0,                           // no return type
 
346
                      0,                       // so no return object
 
347
                      "()")                // no arguments
 
348
            != 0)
 
349
        throw PyException(/*"Error running InterpreterSingleton::RunMethodVoid()"*/);
 
350
 
 
351
}
 
352
 
 
353
PyObject* InterpreterSingleton::runMethodObject(PyObject *pobject, const char *method)
 
354
{
 
355
    PyObject *pcO;
 
356
 
 
357
    PyGILStateLocker locker;
 
358
    if (PP_Run_Method(pobject ,    // object
 
359
                      method,  // run method
 
360
                      "O",                 // return type
 
361
                      &pcO,                // return object
 
362
                      "()")                // no arguments
 
363
            != 0)
 
364
        throw PyException(/*"Error runing InterpreterSingleton::RunMethodObject()"*/);
 
365
 
 
366
    return pcO;
 
367
}
 
368
 
 
369
void InterpreterSingleton::runMethod(PyObject *pobject, const char *method,
 
370
                                     const char *resfmt,   void *cresult,        /* convert to c/c++ */
 
371
                                     const char *argfmt,   ...  )                /* convert to python */
 
372
{
 
373
    PyObject *pmeth, *pargs, *presult;
 
374
    va_list argslist;                              /* "pobject.method(args)" */
 
375
    va_start(argslist, argfmt);
 
376
 
 
377
    PyGILStateLocker locker;
 
378
    pmeth = PyObject_GetAttrString(pobject, method);
 
379
    if (pmeth == NULL)                             /* get callable object */
 
380
        throw Exception("Error runing InterpreterSingleton::RunMethod() method not defined");                                 /* bound method? has self */
 
381
 
 
382
    pargs = Py_VaBuildValue(argfmt, argslist);     /* args: c->python */
 
383
 
 
384
    if (pargs == NULL) {
 
385
        Py_DECREF(pmeth);
 
386
        throw Exception("InterpreterSingleton::RunMethod() wrong arguments");
 
387
    }
 
388
 
 
389
    presult = PyEval_CallObject(pmeth, pargs);   /* run interpreter */
 
390
 
 
391
    Py_DECREF(pmeth);
 
392
    Py_DECREF(pargs);
 
393
    if (PP_Convert_Result(presult, resfmt, cresult)!= 0) {
 
394
        if ( PyErr_Occurred() )
 
395
            PyErr_Print();
 
396
        throw Exception("Error runing InterpreterSingleton::RunMethod() exception in called method");
 
397
    }
 
398
}
 
399
 
 
400
 
 
401
void InterpreterSingleton::dbgObserveFile(const char* sFileName)
 
402
{
 
403
    if (sFileName)
 
404
        _cDebugFileName = sFileName;
 
405
    else
 
406
        _cDebugFileName = "";
 
407
}
 
408
 
 
409
void InterpreterSingleton::dbgSetBreakPoint(unsigned int /*uiLineNumber*/)
 
410
{
 
411
 
 
412
}
 
413
 
 
414
void InterpreterSingleton::dbgUnsetBreakPoint(unsigned int /*uiLineNumber*/)
 
415
{
 
416
 
 
417
}
 
418
 
 
419
void InterpreterSingleton::dbgStep(void)
 
420
{
 
421
 
 
422
}
 
423
 
 
424
const std::string InterpreterSingleton::strToPython(const char* Str)
 
425
{
 
426
    std::string result;
 
427
    const char *It=Str;
 
428
 
 
429
    while (*It != '\0') {
 
430
        switch (*It) {
 
431
        case '\\':
 
432
            result += "\\\\";
 
433
            break;
 
434
        case '\"':
 
435
            result += "\\\"";
 
436
            break;
 
437
        case '\'':
 
438
            result += "\\\'";
 
439
            break;
 
440
        default:
 
441
            result += *It;
 
442
        }
 
443
        It++;
 
444
    }
 
445
 
 
446
    return result;
 
447
}
 
448
 
 
449
// --------------------------------------------------------------------
 
450
 
 
451
#if (defined(HAVE_SWIG) && (HAVE_SWIG == 1))
 
452
namespace Swig_python { extern int createSWIGPointerObj_T(const char* TypeName, void* obj, PyObject** ptr, int own); }
 
453
#endif
 
454
namespace Swig_1_3_25 { extern int createSWIGPointerObj_T(const char* TypeName, void* obj, PyObject** ptr, int own); }
 
455
namespace Swig_1_3_33 { extern int createSWIGPointerObj_T(const char* TypeName, void* obj, PyObject** ptr, int own); }
 
456
namespace Swig_1_3_36 { extern int createSWIGPointerObj_T(const char* TypeName, void* obj, PyObject** ptr, int own); }
 
457
namespace Swig_1_3_38 { extern int createSWIGPointerObj_T(const char* TypeName, void* obj, PyObject** ptr, int own); }
 
458
 
 
459
PyObject* InterpreterSingleton::createSWIGPointerObj(const char* TypeName, void* Pointer, int own)
 
460
{
 
461
    int result = 0;
 
462
    PyObject* proxy=0;
 
463
    PyGILStateLocker locker;
 
464
#if (defined(HAVE_SWIG) && (HAVE_SWIG == 1))
 
465
    result = Swig_python::createSWIGPointerObj_T(TypeName, Pointer, &proxy, own);
 
466
    if (result == 0) return proxy;
 
467
#endif
 
468
    result = Swig_1_3_25::createSWIGPointerObj_T(TypeName, Pointer, &proxy, own);
 
469
    if (result == 0) return proxy;
 
470
    result = Swig_1_3_33::createSWIGPointerObj_T(TypeName, Pointer, &proxy, own);
 
471
    if (result == 0) return proxy;
 
472
    result = Swig_1_3_36::createSWIGPointerObj_T(TypeName, Pointer, &proxy, own);
 
473
    if (result == 0) return proxy;
 
474
    result = Swig_1_3_38::createSWIGPointerObj_T(TypeName, Pointer, &proxy, own);
 
475
    if (result == 0) return proxy;
 
476
 
 
477
    // none of the SWIG's succeeded
 
478
    throw Base::Exception("No SWIG wrapped library loaded");
 
479
}
 
480
 
 
481
#if (defined(HAVE_SWIG) && (HAVE_SWIG == 1))
 
482
namespace Swig_python { extern int convertSWIGPointerObj_T(const char* TypeName, PyObject* obj, void** ptr, int flags); }
 
483
#endif
 
484
namespace Swig_1_3_25 { extern int convertSWIGPointerObj_T(const char* TypeName, PyObject* obj, void** ptr, int flags); }
 
485
namespace Swig_1_3_33 { extern int convertSWIGPointerObj_T(const char* TypeName, PyObject* obj, void** ptr, int flags); }
 
486
namespace Swig_1_3_36 { extern int convertSWIGPointerObj_T(const char* TypeName, PyObject* obj, void** ptr, int flags); }
 
487
namespace Swig_1_3_38 { extern int convertSWIGPointerObj_T(const char* TypeName, PyObject* obj, void** ptr, int flags); }
 
488
 
 
489
bool InterpreterSingleton::convertSWIGPointerObj(const char* TypeName, PyObject* obj, void** ptr, int flags)
 
490
{
 
491
    int result = 0;
 
492
    PyGILStateLocker locker;
 
493
#if (defined(HAVE_SWIG) && (HAVE_SWIG == 1))
 
494
    result = Swig_python::convertSWIGPointerObj_T(TypeName, obj, ptr, flags);
 
495
    if (result == 0) return true;
 
496
#endif
 
497
    result = Swig_1_3_25::convertSWIGPointerObj_T(TypeName, obj, ptr, flags);
 
498
    if (result == 0) return true;
 
499
    result = Swig_1_3_33::convertSWIGPointerObj_T(TypeName, obj, ptr, flags);
 
500
    if (result == 0) return true;
 
501
    result = Swig_1_3_36::convertSWIGPointerObj_T(TypeName, obj, ptr, flags);
 
502
    if (result == 0) return true;
 
503
    result = Swig_1_3_38::convertSWIGPointerObj_T(TypeName, obj, ptr, flags);
 
504
    if (result == 0) return true;
 
505
 
 
506
    // none of the SWIG's succeeded
 
507
    throw Base::Exception("No SWIG wrapped library loaded");
 
508
}
 
509
 
 
510
#if (defined(HAVE_SWIG) && (HAVE_SWIG == 1))
 
511
namespace Swig_python { extern void cleanupSWIG_T(const char* TypeName); }
 
512
#endif
 
513
namespace Swig_1_3_25 { extern void cleanupSWIG_T(const char* TypeName); }
 
514
namespace Swig_1_3_33 { extern void cleanupSWIG_T(const char* TypeName); }
 
515
namespace Swig_1_3_36 { extern void cleanupSWIG_T(const char* TypeName); }
 
516
namespace Swig_1_3_38 { extern void cleanupSWIG_T(const char* TypeName); }
 
517
 
 
518
void InterpreterSingleton::cleanupSWIG(const char* TypeName)
 
519
{
 
520
    PyGILStateLocker locker;
 
521
#if (defined(HAVE_SWIG) && (HAVE_SWIG == 1))
 
522
    Swig_python::cleanupSWIG_T(TypeName);
 
523
#endif
 
524
    Swig_1_3_25::cleanupSWIG_T(TypeName);
 
525
    Swig_1_3_33::cleanupSWIG_T(TypeName);
 
526
    Swig_1_3_36::cleanupSWIG_T(TypeName);
 
527
    Swig_1_3_38::cleanupSWIG_T(TypeName);
 
528
}