summaryrefslogtreecommitdiffstats
path: root/lib/kross/python/pythonscript.cpp
blob: 5c2dec8ba20ba049a6ba56099af79c5681bceb05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/***************************************************************************
 * pythonscript.cpp
 * This file is part of the KDE project
 * copyright (C)2004-2005 by Sebastian Sauer (mail@dipe.org)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 * You should have received a copy of the GNU Library General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 ***************************************************************************/

#include "pythonscript.h"
#include "pythonmodule.h"
#include "pythoninterpreter.h"
#include "pythonsecurity.h"
#include "../main/scriptcontainer.h"

//#include <tdeapplication.h>

using namespace Kross::Python;

namespace Kross { namespace Python {

    /// @internal
    class PythonScriptPrivate
    {
        public:

            /**
            * The \a Py::Module instance this \a PythonScript
            * has as local context.
            */
            Py::Module* m_module;

            /**
            * The PyCodeObject object representing the
            * compiled python code. Internaly we first
            * compile the python code and later execute
            * it.
            */
            Py::Object* m_code;

            /**
            * A list of functionnames.
            */
            TQStringList m_functions;

            /**
            * A list of classnames.
            */
            TQStringList m_classes;
    };

}}

PythonScript::PythonScript(Kross::Api::Interpreter* interpreter, Kross::Api::ScriptContainer* scriptcontainer)
    : Kross::Api::Script(interpreter, scriptcontainer)
    , d(new PythonScriptPrivate())
{
#ifdef KROSS_PYTHON_SCRIPT_CTOR_DEBUG
    krossdebug("PythonScript::PythonScript() Constructor.");
#endif
    d->m_module = 0;
    d->m_code = 0;
}

PythonScript::~PythonScript()
{
#ifdef KROSS_PYTHON_SCRIPT_DTOR_DEBUG
    krossdebug("PythonScript::~PythonScript() Destructor.");
#endif
    finalize();
    delete d;
}

void PythonScript::initialize()
{
    finalize();
    clearException(); // clear previously thrown exceptions.

    try {
        if(m_scriptcontainer->getCode().isNull())
            throw Kross::Api::Exception::Ptr( new Kross::Api::Exception(TQString("Invalid scripting code for script '%1'").arg( m_scriptcontainer->getName() )) );

        if(m_scriptcontainer->getName().isNull())
            throw Kross::Api::Exception::Ptr( new Kross::Api::Exception(TQString("Name for the script is invalid!")) );

        PyObject* pymod = PyModule_New( (char*) m_scriptcontainer->getName().latin1() );
        d->m_module = new Py::Module(pymod, true);
        if(! d->m_module)
            throw Kross::Api::Exception::Ptr( new Kross::Api::Exception(TQString("Failed to initialize local module context for script '%1'").arg( m_scriptcontainer->getName() )) );

#ifdef KROSS_PYTHON_SCRIPT_INIT_DEBUG
        krossdebug( TQString("PythonScript::initialize() module='%1' refcount='%2'").arg(d->m_module->as_string().c_str()).arg(d->m_module->reference_count()) );
#endif

        // Set the "self" variable to point to the ScriptContainer
        // we are using for the script. That way we are able to
        // simply access the ScriptContainer itself from within
        // python scripting code.
        Py::Dict moduledict = d->m_module->getDict();
        moduledict["self"] = PythonExtension::toPyObject( m_scriptcontainer );
        //moduledict["parent"] = PythonExtension::toPyObject( m_manager );

/*
        // Prepare the local context.
        TQString s =
            //"import sys\n"
            "if self.has(\"stdout\"):\n"
            "  self.stdout = Redirect( self.get(\"stdout\") )\n"
            "if self.has(\"stderr\"):\n"
            "  self.stderr = Redirect( self.get(\"stderr\") )\n"
            ;
        Py::Dict mainmoduledict = ((PythonInterpreter*)m_interpreter)->mainModule()->getDict();
        PyObject* pyrun = PyRun_StringFlags((char*)s.latin1(), Py_file_input, mainmoduledict.ptr(), moduledict.ptr());
        if(! pyrun)
            throw Py::Exception(); // throw exception
        Py_XDECREF(pyrun); // free the reference.
*/

        // Compile the python script code. It will be later on request
        // executed. That way we cache the compiled code.
        PyObject* code = 0;
        bool restricted = m_scriptcontainer->getOption("restricted", TQVariant(false,0), true).toBool();

        krossdebug( TQString("PythonScript::initialize() name=%1 restricted=%2").arg(m_scriptcontainer->getName()).arg(restricted) );
        if(restricted) {

            // Use the RestrictedPython module wrapped by the PythonSecurity class.
            code = dynamic_cast<PythonInterpreter*>(m_interpreter)->securityModule()->compile_restricted(
                m_scriptcontainer->getCode(),
                m_scriptcontainer->getName(),
                "exec"
            );

        }
        else {
            //PyCompilerFlags* cf = new PyCompilerFlags;
            //cf->cf_flags |= PyCF_SOURCE_IS_UTF8;

            // Just compile the code without any restrictions.
            code = Py_CompileString(
                (char*) m_scriptcontainer->getCode().latin1(),
                (char*) m_scriptcontainer->getName().latin1(),
                Py_file_input
            );
        }

        if(! code)
            throw Py::Exception();
        d->m_code = new Py::Object(code, true);
    }
    catch(Py::Exception& e) {
        TQString err = Py::value(e).as_string().c_str();
        Kross::Api::Exception::Ptr exception = toException( TQString("Failed to compile python code: %1").arg(err) );
        e.clear(); // exception is handled. clear it now.
        throw exception;
    }
}

void PythonScript::finalize()
{
#ifdef KROSS_PYTHON_SCRIPT_FINALIZE_DEBUG
    if(d->m_module)
        krossdebug( TQString("PythonScript::finalize() module='%1' refcount='%2'").arg(d->m_module->as_string().c_str()).arg(d->m_module->reference_count()) );
#endif

    delete d->m_module; d->m_module = 0;
    delete d->m_code; d->m_code = 0;
    d->m_functions.clear();
    d->m_classes.clear();
}

Kross::Api::Exception::Ptr PythonScript::toException(const TQString& error)
{
    long lineno = -1;
    TQStringList errorlist;

    PyObject *type, *value, *traceback;
    PyErr_Fetch(&type, &value, &traceback);
    Py_FlushLine();
    PyErr_NormalizeException(&type, &value, &traceback);

    if(traceback) {
        Py::List tblist;
        try {
            Py::Module tbmodule( PyImport_Import(Py::String("traceback").ptr()), true );
            Py::Dict tbdict = tbmodule.getDict();
            Py::Callable tbfunc(tbdict.getItem("format_tb"));
            Py::Tuple args(1);
            args.setItem(0, Py::Object(traceback));
            tblist = tbfunc.apply(args);
            uint length = tblist.length();
            for(Py::List::size_type i = 0; i < length; ++i)
                errorlist.append( Py::Object(tblist[i]).as_string().c_str() );
        }
        catch(Py::Exception& e) {
            TQString err = Py::value(e).as_string().c_str();
            e.clear(); // exception is handled. clear it now.
            krosswarning( TQString("Kross::Python::PythonScript::toException() Failed to fetch a traceback: %1").arg(err) );
        }

        PyObject *next;
        while (traceback && traceback != Py_None) {
            PyFrameObject *frame = (PyFrameObject*)PyObject_GetAttrString(traceback, "tb_frame");
            Py_DECREF(frame);
            {
                PyObject *getobj = PyObject_GetAttrString(traceback, "tb_lineno");
                lineno = PyInt_AsLong(getobj);
                Py_DECREF(getobj);
            }
            if(Py_OptimizeFlag) {
                PyObject *getobj = PyObject_GetAttrString(traceback, "tb_lasti");
                int lasti = PyInt_AsLong(getobj);
                Py_DECREF(getobj);
                lineno = PyCode_Addr2Line(frame->f_code, lasti);
            }

            //const char* filename = PyString_AsString(frame->f_code->co_filename);
            //const char* name = PyString_AsString(frame->f_code->co_name);
            //errorlist.append( TQString("%1#%2: \"%3\"").arg(filename).arg(lineno).arg(name) );

            next = PyObject_GetAttrString(traceback, "tb_next");
            Py_DECREF(traceback);
            traceback = next;
        }
    }

    if(lineno < 0) {
        if(value) {
            PyObject *getobj = PyObject_GetAttrString(value, "lineno");
            if(getobj) {
                lineno = PyInt_AsLong(getobj);
                Py_DECREF(getobj);
            }
        }
        if(lineno < 0)
            lineno = 0;
    }

    //PyErr_Restore(type, value, traceback);

    Kross::Api::Exception::Ptr exception = new Kross::Api::Exception(error, lineno - 1);
    if(errorlist.count() > 0)
        exception->setTrace( errorlist.join("\n") );
    return exception;
}

const TQStringList& PythonScript::getFunctionNames()
{
    if(! d->m_module)
        initialize(); //TODO catch exception
    return d->m_functions;
    /*
    TQStringList list;
    Py::List l = d->m_module->getDict().keys();
    int length = l.length();
    for(Py::List::size_type i = 0; i < length; ++i)
        list.append( l[i].str().as_string().c_str() );
    return list;
    */
}

Kross::Api::Object::Ptr PythonScript::execute()
{
#ifdef KROSS_PYTHON_SCRIPT_EXEC_DEBUG
    krossdebug( TQString("PythonScript::execute()") );
#endif

    try {
        if(! d->m_module)
            initialize();

        // the main module dictonary.
        Py::Dict mainmoduledict = ((PythonInterpreter*)m_interpreter)->mainModule()->getDict();
        // the local context dictonary.
        Py::Dict moduledict( d->m_module->getDict().ptr() );

        // Initialize context before execution.
        TQString s =
            "import sys\n"
            //"if self.has(\"stdout\"):\n"
            //"  sys.stdout = Redirect( self.get(\"stdout\") )\n"
            //"if self.has(\"stderr\"):\n"
            //"  sys.stderr = Redirect( self.get(\"stderr\") )\n"
            ;

        PyObject* pyrun = PyRun_String(s.latin1(), Py_file_input, mainmoduledict.ptr(), moduledict.ptr());
        if(! pyrun)
            throw Py::Exception(); // throw exception
        Py_XDECREF(pyrun); // free the reference.

        // Acquire interpreter lock*/
        PyGILState_STATE gilstate = PyGILState_Ensure();

        // Evaluate the already compiled code.
        PyObject* pyresult = PyEval_EvalCode(
            (PyCodeObject*)d->m_code->ptr(),
            mainmoduledict.ptr(),
            moduledict.ptr()
        );

        // Free interpreter lock
        PyGILState_Release(gilstate);

        if(! pyresult || PyErr_Occurred()) {
            krosswarning("Kross::Python::PythonScript::execute(): Failed to PyEval_EvalCode");
            throw Py::Exception();
        }
        Py::Object result(pyresult, true);

#ifdef KROSS_PYTHON_SCRIPT_EXEC_DEBUG
        krossdebug( TQString("PythonScript::execute() result=%1").arg(result.as_string().c_str()) );
#endif

        for(Py::Dict::iterator it = moduledict.begin(); it != moduledict.end(); ++it) {
            Py::Dict::value_type vt(*it);
            if(PyClass_Check( vt.second.ptr() )) {
#ifdef KROSS_PYTHON_SCRIPT_EXEC_DEBUG
                krossdebug( TQString("PythonScript::execute() class '%1' added.").arg(vt.first.as_string().c_str()) );
#endif
                d->m_classes.append( vt.first.as_string().c_str() );
            }
            else if(vt.second.isCallable()) {
#ifdef KROSS_PYTHON_SCRIPT_EXEC_DEBUG
                krossdebug( TQString("PythonScript::execute() function '%1' added.").arg(vt.first.as_string().c_str()) );
#endif
                d->m_functions.append( vt.first.as_string().c_str() );
            }
        }

        Kross::Api::Object::Ptr r = PythonExtension::toObject(result);
        return r;
    }
    catch(Py::Exception& e) {
        try {
            Py::Object errobj = Py::value(e);
            if(errobj.ptr() == Py_None) // at least string-exceptions have there errormessage in the type-object
                errobj = Py::type(e);
            TQString err = errobj.as_string().c_str();

            Kross::Api::Exception::Ptr exception = toException( TQString("Failed to execute python code: %1").arg(err) );
            e.clear(); // exception is handled. clear it now.
            setException( exception );
        }
        catch(Py::Exception& e) {
            TQString err = Py::value(e).as_string().c_str();
            Kross::Api::Exception::Ptr exception = toException( TQString("Failed to execute python code: %1").arg(err) );
            e.clear(); // exception is handled. clear it now.
            setException( exception );
        }
    }
    catch(Kross::Api::Exception::Ptr e) {
        setException(e);
    }

    return 0; // return nothing if exception got thrown.
}

Kross::Api::Object::Ptr PythonScript::callFunction(const TQString& name, Kross::Api::List::Ptr args)
{
#ifdef KROSS_PYTHON_SCRIPT_CALLFUNC_DEBUG
    krossdebug( TQString("PythonScript::callFunction(%1, %2)")
                 .arg(name)
                 .arg(args ? TQString::number(args->count()) : TQString("NULL")) );
#endif

    if(hadException()) return 0; // abort if we had an unresolved exception.

    if(! d->m_module) {
        setException( new Kross::Api::Exception(TQString("Script not initialized.")) );
        return 0;
    }

    try {
        Py::Dict moduledict = d->m_module->getDict();

        // Try to determinate the function we like to execute.
        PyObject* func = PyDict_GetItemString(moduledict.ptr(), name.latin1());

        if( (! d->m_functions.contains(name)) || (! func) )
            throw Kross::Api::Exception::Ptr( new Kross::Api::Exception(TQString("No such function '%1'.").arg(name)) );

        Py::Callable funcobject(func, true); // the funcobject takes care of freeing our func pyobject.

        // Check if the object is really a function and therefore callable.
        if(! funcobject.isCallable())
            throw Kross::Api::Exception::Ptr( new Kross::Api::Exception(TQString("Function is not callable.")) );

        // Call the function.
        Py::Object result = funcobject.apply(PythonExtension::toPyTuple(args));
        return PythonExtension::toObject(result);
    }
    catch(Py::Exception& e) {
        TQString err = Py::value(e).as_string().c_str();
        e.clear(); // exception is handled. clear it now.
        setException( new Kross::Api::Exception(TQString("Python Exception: %1").arg(err)) );
    }
    catch(Kross::Api::Exception::Ptr e) {
        setException(e);
    }

    return 0; // return nothing if exception got thrown.
}

const TQStringList& PythonScript::getClassNames()
{
    if(! d->m_module)
        initialize(); //TODO catch exception
    return d->m_classes;
}

Kross::Api::Object::Ptr PythonScript::classInstance(const TQString& name)
{
    if(hadException()) return 0; // abort if we had an unresolved exception.

    if(! d->m_module) {
        setException( new Kross::Api::Exception(TQString("Script not initialized.")) );
        return 0;
    }

    try {
        Py::Dict moduledict = d->m_module->getDict();

        // Try to determinate the class.
        PyObject* pyclass = PyDict_GetItemString(moduledict.ptr(), name.latin1());
        if( (! d->m_classes.contains(name)) || (! pyclass) )
            throw Kross::Api::Exception::Ptr( new Kross::Api::Exception(TQString("No such class '%1'.").arg(name)) );

        PyObject *pyobj = PyInstance_New(pyclass, 0, 0);//aclarg, 0);
        if(! pyobj)
            throw Kross::Api::Exception::Ptr( new Kross::Api::Exception(TQString("Failed to create instance of class '%1'.").arg(name)) );

        Py::Object classobject(pyobj, true);

#ifdef KROSS_PYTHON_SCRIPT_CLASSINSTANCE_DEBUG
        krossdebug( TQString("PythonScript::classInstance() inst='%1'").arg(classobject.as_string().c_str()) );
#endif
        return PythonExtension::toObject(classobject);
    }
    catch(Py::Exception& e) {
        TQString err = Py::value(e).as_string().c_str();
        e.clear(); // exception is handled. clear it now.
        setException( Kross::Api::Exception::Ptr( new Kross::Api::Exception(err) ) );
    }
    catch(Kross::Api::Exception::Ptr e) {
        setException(e);
    }

    return 0; // return nothing if exception got thrown.
}