Debian Codegraph @

src/Gui/PythonConsolePy.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2004 Werner Mayer <wmayer[at]users.sourceforge.net> *
3 * *
4 * This file is part of the FreeCAD CAx development system. *
5 * *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Library General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU Library General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Library General Public *
17 * License along with this library; see the file COPYING.LIB. If not, *
18 * write to the Free Software Foundation, Inc., 59 Temple Place, *
19 * Suite 330, Boston, MA 02111-1307, USA *
20 * *
21 ***************************************************************************/
22
23
24 #include "PythonConsolePy.h"
25 #include "PythonConsole.h"
26
27
28 using namespace Gui;
29
30 void PythonStdout::init_type()
31 {
32 behaviors().name("PythonStdout");
33 behaviors().doc("Redirection of stdout to FreeCAD's Python console window");
34 // you must have overwritten the virtual functions
35 behaviors().supportRepr();
36 add_varargs_method("write", &PythonStdout::write, "write()");
37 add_varargs_method("flush", &PythonStdout::flush, "flush()");
38 add_noargs_method("isatty", &PythonStdout::isatty, "isatty()");
39 }
40
41 PythonStdout::PythonStdout(PythonConsole* pc)
42 : pyConsole(pc)
43 {}
44
45 PythonStdout::~PythonStdout() = default;
46
47 Py::Object PythonStdout::getattr(const char* name)
48 {
49 if (strcmp(name, "softspace") == 0) {
50 int i = 0;
51 return Py::Long(i);
52 }
53 return getattr_methods(name);
54 }
55
56 Py::Object PythonStdout::repr()
57 {
58 return Py::String("PythonStdout");
59 }
60
61 Py::Object PythonStdout::write(const Py::Tuple& args)
62 {
63 PyObject* output;
64 if (!PyArg_ParseTuple(args.ptr(), "O!", &PyUnicode_Type, &output)) {
65 throw Py::TypeError("PythonStdout.write() takes exactly one argument of type str");
66 }
67
68 PyObject* unicode = PyUnicode_AsEncodedString(output, "utf-8", nullptr);
69 if (unicode) {
70 const char* string = PyBytes_AsString(unicode);
71 int maxlen = qstrlen(string) > 10000 ? 10000 : -1;
72 pyConsole->insertPythonOutput(QString::fromUtf8(string, maxlen));
73 Py_DECREF(unicode);
74 }
75
76 return Py::None();
77 }
78
79 Py::Object PythonStdout::flush(const Py::Tuple&)
80 {
81 return Py::None();
82 }
83
84 Py::Object PythonStdout::isatty()
85 {
86 return Py::False();
87 }
88
89 // -------------------------------------------------------------------------
90
91 void PythonStderr::init_type()
92 {
93 behaviors().name("PythonStderr");
94 behaviors().doc("Redirection of stdout to FreeCAD's Python console window");
95 // you must have overwritten the virtual functions
96 behaviors().supportRepr();
97 add_varargs_method("write", &PythonStderr::write, "write()");
98 add_varargs_method("flush", &PythonStderr::flush, "flush()");
99 add_noargs_method("isatty", &PythonStderr::isatty, "isatty()");
100 }
101
102 PythonStderr::PythonStderr(PythonConsole* pc)
103 : pyConsole(pc)
104 {}
105
106 PythonStderr::~PythonStderr() = default;
107
108 Py::Object PythonStderr::getattr(const char* name)
109 {
110 if (strcmp(name, "softspace") == 0) {
111 int i = 0;
112 return Py::Long(i);
113 }
114 return getattr_methods(name);
115 }
116
117 Py::Object PythonStderr::repr()
118 {
119 return Py::String("PythonStderr");
120 }
121
122 Py::Object PythonStderr::write(const Py::Tuple& args)
123 {
124 PyObject* output;
125 if (!PyArg_ParseTuple(args.ptr(), "O!", &PyUnicode_Type, &output)) {
126 throw Py::TypeError("PythonStderr.write() takes exactly one argument of type str");
127 }
128
129 PyObject* unicode = PyUnicode_AsEncodedString(output, "utf-8", nullptr);
130 if (unicode) {
131 const char* string = PyBytes_AsString(unicode);
132 int maxlen = qstrlen(string) > 10000 ? 10000 : -1;
133 pyConsole->insertPythonError(QString::fromUtf8(string, maxlen));
134 Py_DECREF(unicode);
135 }
136
137 return Py::None();
138 }
139
140 Py::Object PythonStderr::flush(const Py::Tuple&)
141 {
142 return Py::None();
143 }
144
145 Py::Object PythonStderr::isatty()
146 {
147 return Py::False();
148 }
149
150 // -------------------------------------------------------------------------
151
152 void OutputStdout::init_type()
153 {
154 behaviors().name("OutputStdout");
155 behaviors().doc("Redirection of stdout to FreeCAD's report view");
156 // you must have overwritten the virtual functions
157 behaviors().supportRepr();
158 add_varargs_method("write", &OutputStdout::write, "write()");
159 add_varargs_method("flush", &OutputStdout::flush, "flush()");
160 add_noargs_method("isatty", &OutputStdout::isatty, "isatty()");
161 }
162
163 OutputStdout::OutputStdout() = default;
164
165 OutputStdout::~OutputStdout() = default;
166
167 Py::Object OutputStdout::getattr(const char* name)
168 {
169 if (strcmp(name, "softspace") == 0) {
170 int i = 0;
171 return Py::Long(i);
172 }
173 return getattr_methods(name);
174 }
175
176 Py::Object OutputStdout::repr()
177 {
178 return Py::String("OutputStdout");
179 }
180
181 Py::Object OutputStdout::write(const Py::Tuple& args)
182 {
183 PyObject* output;
184 if (!PyArg_ParseTuple(args.ptr(), "O!", &PyUnicode_Type, &output)) {
185 throw Py::TypeError("OutputStdout.write() takes exactly one argument of type str");
186 }
187
188 PyObject* unicode = PyUnicode_AsEncodedString(output, "utf-8", nullptr);
189 if (unicode) {
190 const char* string = PyBytes_AsString(unicode);
191 Base::Console().message("%s", string);
192 Py_DECREF(unicode);
193 }
194
195 return Py::None();
196 }
197
198 Py::Object OutputStdout::flush(const Py::Tuple&)
199 {
200 return Py::None();
201 }
202
203 Py::Object OutputStdout::isatty()
204 {
205 return Py::False();
206 }
207
208
209 // -------------------------------------------------------------------------
210
211 void OutputStderr::init_type()
212 {
213 behaviors().name("OutputStderr");
214 behaviors().doc("Redirection of stdout to FreeCAD's report view");
215 // you must have overwritten the virtual functions
216 behaviors().supportRepr();
217 add_varargs_method("write", &OutputStderr::write, "write()");
218 add_varargs_method("flush", &OutputStderr::flush, "flush()");
219 add_noargs_method("isatty", &OutputStderr::isatty, "isatty()");
220 }
221
222 OutputStderr::OutputStderr() = default;
223
224 OutputStderr::~OutputStderr() = default;
225
226 Py::Object OutputStderr::getattr(const char* name)
227 {
228 if (strcmp(name, "softspace") == 0) {
229 int i = 0;
230 return Py::Long(i);
231 }
232 return getattr_methods(name);
233 }
234
235 Py::Object OutputStderr::repr()
236 {
237 return Py::String("OutputStderr");
238 }
239
240 Py::Object OutputStderr::write(const Py::Tuple& args)
241 {
242 PyObject* output;
243 if (!PyArg_ParseTuple(args.ptr(), "O!", &PyUnicode_Type, &output)) {
244 throw Py::TypeError("OutputStderr.write() takes exactly one argument of type str");
245 }
246
247 PyObject* unicode = PyUnicode_AsEncodedString(output, "utf-8", nullptr);
248 if (unicode) {
249 const char* string = PyBytes_AsString(unicode);
250 Base::Console().error("%s", string);
251 Py_DECREF(unicode);
252 }
253
254 return Py::None();
255 }
256
257 Py::Object OutputStderr::flush(const Py::Tuple&)
258 {
259 return Py::None();
260 }
261
262 Py::Object OutputStderr::isatty()
263 {
264 return Py::False();
265 }
266
267
268 // -------------------------------------------------------------------------
269
270 void PythonStdin::init_type()
271 {
272 behaviors().name("PythonStdin");
273 behaviors().doc("Redirection of stdin to FreeCAD to open an input dialog");
274 // you must have overwritten the virtual functions
275 behaviors().supportRepr();
276 behaviors().supportGetattr();
277 add_varargs_method("readline", &PythonStdin::readline, "readline()");
278 }
279
280 PythonStdin::PythonStdin(PythonConsole* pc)
281 : pyConsole(pc)
282 {}
283
284 PythonStdin::~PythonStdin() = default;
285
286 Py::Object PythonStdin::repr()
287 {
288 return Py::String("PythonStdin");
289 }
290
291 Py::Object PythonStdin::getattr(const char* name)
292 {
293 if (strcmp(name, "closed") == 0) {
294 return Py::Boolean(false);
295 }
296 return getattr_methods(name);
297 }
298
299 Py::Object PythonStdin::readline(const Py::Tuple& /*args*/)
300 {
301 return Py::String((const char*)pyConsole->readline().toLatin1());
302 }