1
/***************************************************************************
2
* Copyright (c) 2004 Werner Mayer <wmayer[at]users.sourceforge.net> *
4
* This file is part of the FreeCAD CAx development system. *
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. *
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. *
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 *
21
***************************************************************************/
23
#include "PreCompiled.h"
25
#include "PythonConsolePy.h"
26
#include "PythonConsole.h"
31
void PythonStdout::init_type()
33
behaviors().name("PythonStdout");
34
behaviors().doc("Redirection of stdout to FreeCAD's Python console window");
35
// you must have overwritten the virtual functions
36
behaviors().supportRepr();
37
add_varargs_method("write",&PythonStdout::write,"write()");
38
add_varargs_method("flush",&PythonStdout::flush,"flush()");
39
add_noargs_method("isatty",&PythonStdout::isatty,"isatty()");
42
PythonStdout::PythonStdout(PythonConsole *pc)
47
PythonStdout::~PythonStdout() = default;
49
Py::Object PythonStdout::getattr(const char *name)
51
if (strcmp(name, "softspace") == 0) {
55
return getattr_methods(name);
58
Py::Object PythonStdout::repr()
61
std::ostringstream s_out;
62
s_out << "PythonStdout";
63
return Py::String(s_out.str());
66
Py::Object PythonStdout::write(const Py::Tuple& args)
69
if (!PyArg_ParseTuple(args.ptr(), "O!",&PyUnicode_Type, &output))
70
throw Py::TypeError("PythonStdout.write() takes exactly one argument of type str");
72
PyObject* unicode = PyUnicode_AsEncodedString(output, "utf-8", nullptr);
74
const char* string = PyBytes_AsString(unicode);
75
int maxlen = qstrlen(string) > 10000 ? 10000 : -1;
76
pyConsole->insertPythonOutput(QString::fromUtf8(string, maxlen));
83
Py::Object PythonStdout::flush(const Py::Tuple&)
88
Py::Object PythonStdout::isatty()
93
// -------------------------------------------------------------------------
95
void PythonStderr::init_type()
97
behaviors().name("PythonStderr");
98
behaviors().doc("Redirection of stdout to FreeCAD's Python console window");
99
// you must have overwritten the virtual functions
100
behaviors().supportRepr();
101
add_varargs_method("write",&PythonStderr::write,"write()");
102
add_varargs_method("flush",&PythonStderr::flush,"flush()");
103
add_noargs_method("isatty",&PythonStderr::isatty,"isatty()");
106
PythonStderr::PythonStderr(PythonConsole *pc)
111
PythonStderr::~PythonStderr() = default;
113
Py::Object PythonStderr::getattr(const char *name)
115
if (strcmp(name, "softspace") == 0) {
119
return getattr_methods(name);
122
Py::Object PythonStderr::repr()
125
std::ostringstream s_out;
126
s_out << "PythonStderr";
127
return Py::String(s_out.str());
130
Py::Object PythonStderr::write(const Py::Tuple& args)
133
if (!PyArg_ParseTuple(args.ptr(), "O!",&PyUnicode_Type, &output))
134
throw Py::TypeError("PythonStderr.write() takes exactly one argument of type str");
136
PyObject* unicode = PyUnicode_AsEncodedString(output, "utf-8", nullptr);
138
const char* string = PyBytes_AsString(unicode);
139
int maxlen = qstrlen(string) > 10000 ? 10000 : -1;
140
pyConsole->insertPythonError(QString::fromUtf8(string, maxlen));
147
Py::Object PythonStderr::flush(const Py::Tuple&)
152
Py::Object PythonStderr::isatty()
157
// -------------------------------------------------------------------------
159
void OutputStdout::init_type()
161
behaviors().name("OutputStdout");
162
behaviors().doc("Redirection of stdout to FreeCAD's report view");
163
// you must have overwritten the virtual functions
164
behaviors().supportRepr();
165
add_varargs_method("write",&OutputStdout::write,"write()");
166
add_varargs_method("flush",&OutputStdout::flush,"flush()");
167
add_noargs_method("isatty",&OutputStdout::isatty,"isatty()");
170
OutputStdout::OutputStdout() = default;
172
OutputStdout::~OutputStdout() = default;
174
Py::Object OutputStdout::getattr(const char *name)
176
if (strcmp(name, "softspace") == 0) {
180
return getattr_methods(name);
183
Py::Object OutputStdout::repr()
186
std::ostringstream s_out;
187
s_out << "OutputStdout";
188
return Py::String(s_out.str());
191
Py::Object OutputStdout::write(const Py::Tuple& args)
194
if (!PyArg_ParseTuple(args.ptr(), "O!",&PyUnicode_Type, &output))
195
throw Py::TypeError("OutputStdout.write() takes exactly one argument of type str");
197
PyObject* unicode = PyUnicode_AsEncodedString(output, "utf-8", nullptr);
199
const char* string = PyBytes_AsString(unicode);
200
Base::Console().Message("%s",string);
207
Py::Object OutputStdout::flush(const Py::Tuple&)
212
Py::Object OutputStdout::isatty()
218
// -------------------------------------------------------------------------
220
void OutputStderr::init_type()
222
behaviors().name("OutputStderr");
223
behaviors().doc("Redirection of stdout to FreeCAD's report view");
224
// you must have overwritten the virtual functions
225
behaviors().supportRepr();
226
add_varargs_method("write",&OutputStderr::write,"write()");
227
add_varargs_method("flush",&OutputStderr::flush,"flush()");
228
add_noargs_method("isatty",&OutputStderr::isatty,"isatty()");
231
OutputStderr::OutputStderr() = default;
233
OutputStderr::~OutputStderr() = default;
235
Py::Object OutputStderr::getattr(const char *name)
237
if (strcmp(name, "softspace") == 0) {
241
return getattr_methods(name);
244
Py::Object OutputStderr::repr()
247
std::ostringstream s_out;
248
s_out << "OutputStderr";
249
return Py::String(s_out.str());
252
Py::Object OutputStderr::write(const Py::Tuple& args)
255
if (!PyArg_ParseTuple(args.ptr(), "O!",&PyUnicode_Type, &output))
256
throw Py::TypeError("OutputStderr.write() takes exactly one argument of type str");
258
PyObject* unicode = PyUnicode_AsEncodedString(output, "utf-8", nullptr);
260
const char* string = PyBytes_AsString(unicode);
261
Base::Console().Error("%s",string);
268
Py::Object OutputStderr::flush(const Py::Tuple&)
273
Py::Object OutputStderr::isatty()
279
// -------------------------------------------------------------------------
281
void PythonStdin::init_type()
283
behaviors().name("PythonStdin");
284
behaviors().doc("Redirection of stdin to FreeCAD to open an input dialog");
285
// you must have overwritten the virtual functions
286
behaviors().supportRepr();
287
behaviors().supportGetattr();
288
add_varargs_method("readline",&PythonStdin::readline,"readline()");
291
PythonStdin::PythonStdin(PythonConsole *pc)
296
PythonStdin::~PythonStdin() = default;
298
Py::Object PythonStdin::repr()
301
std::ostringstream s_out;
302
s_out << "PythonStdin";
303
return Py::String(s_out.str());
306
Py::Object PythonStdin::getattr(const char *name)
308
if (strcmp(name, "closed") == 0) {
309
return Py::Boolean(false);
311
return getattr_methods(name);
314
Py::Object PythonStdin::readline(const Py::Tuple& /*args*/)
316
return Py::String( (const char *)pyConsole->readline().toLatin1() );