1 /**
2 * Python Interpreter wrapper for Inkscape
3 *
4 * Authors:
5 * Bob Jamison <rjamison@titan.com>
6 *
7 * Copyright (C) 2004 Authors
8 *
9 * Released under GNU GPL, read the file 'COPYING' for more information
10 */
13 #include <Python.h>
15 #include "InkscapePython.h"
18 #include <stdio.h>
20 #include "inkscape_py.py.h"
23 /*
24 * Generated by SWIG
25 */
26 extern "C" void init_inkscape_py(void);
29 namespace Inkscape {
30 namespace Extension {
31 namespace Script {
33 /*
34 *
35 */
36 InkscapePython::InkscapePython()
37 {
38 }
42 /*
43 *
44 */
45 InkscapePython::~InkscapePython()
46 {
48 }
52 static bool initialized = false;
53 /*
54 * Interpret an in-memory string
55 */
56 bool InkscapePython::interpretScript(const Glib::ustring &script,
57 Glib::ustring &output,
58 Glib::ustring &error)
59 {
60 if (!initialized)
61 {
62 Py_Initialize();
63 init_inkscape_py();
64 initialized = true;
65 }
66 char *codeStr = (char *)script.raw().c_str();
67 PyRun_SimpleString(inkscape_module_script);
68 PyRun_SimpleString("inkscape = _inkscape_py.getInkscape()\n");
69 PyRun_SimpleString(codeStr);
71 //## Check for errors
72 if (PyErr_Occurred())
73 {
74 PyObject *errobj = NULL;
75 PyObject *errdata = NULL;
76 PyObject *errtraceback = NULL;
78 PyErr_Fetch(&errobj, &errdata, &errtraceback);
79 //PyErr_Clear();
81 if (errobj && PyString_Check(errobj))
82 {
83 PyObject *pystring = PyObject_Str(errobj);
84 char *errStr = PyString_AsString(pystring);
85 error = errStr;
86 Py_XDECREF(pystring);
87 }
88 else
89 {
90 error = "Error occurred";
91 }
92 Py_XDECREF(errobj);
93 Py_XDECREF(errdata);
94 Py_XDECREF(errtraceback);
95 return false;
96 }
97 //Py_Finalize();
98 return true;
99 }
107 } // namespace Script
108 } // namespace Extension
109 } // namespace Inkscape
111 /*
112 Local Variables:
113 mode:c++
114 c-file-style:"stroustrup"
115 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
116 indent-tabs-mode:nil
117 fill-column:99
118 End:
119 */
120 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :