Core: do not override sys.stdin when using FreeCAD as Python module

This commit is contained in:
wmayer
2023-05-01 17:04:40 +02:00
committed by wwmayer
parent 7f1e979aad
commit d7174ae2b8
4 changed files with 25 additions and 6 deletions

View File

@@ -469,7 +469,13 @@ PythonConsole::PythonConsole(QWidget *parent)
d->_stderrPy = new PythonStderr(this);
d->_stdinPy = new PythonStdin (this);
d->_stdin = PySys_GetObject("stdin");
PySys_SetObject("stdin", d->_stdinPy);
// Don't override stdin when running FreeCAD as Python module
auto& cfg = App::Application::Config();
auto overrideStdIn = cfg.find("DontOverrideStdIn");
if (overrideStdIn == cfg.end()) {
PySys_SetObject("stdin", d->_stdinPy);
}
const char* version = PyUnicode_AsUTF8(PySys_GetObject("version"));
const char* platform = PyUnicode_AsUTF8(PySys_GetObject("platform"));

View File

@@ -296,6 +296,7 @@ void PythonStdin::init_type()
behaviors().doc("Redirection of stdin to FreeCAD to open an input dialog");
// you must have overwritten the virtual functions
behaviors().supportRepr();
behaviors().supportGetattr();
add_varargs_method("readline",&PythonStdin::readline,"readline()");
}
@@ -316,6 +317,14 @@ Py::Object PythonStdin::repr()
return Py::String(s_out.str());
}
Py::Object PythonStdin::getattr(const char *name)
{
if (strcmp(name, "closed") == 0) {
return Py::Boolean(false);
}
return getattr_methods(name);
}
Py::Object PythonStdin::readline(const Py::Tuple& /*args*/)
{
return Py::String( (const char *)pyConsole->readline().toLatin1() );

View File

@@ -145,6 +145,7 @@ public:
~PythonStdin() override;
Py::Object repr() override;
Py::Object getattr(const char *name) override;
Py::Object readline(const Py::Tuple&);
};

View File

@@ -287,19 +287,23 @@ QWidget* setupMainWindow()
}
Base::PyGILStateLocker lock;
PyObject* input = PySys_GetObject("stdin");
// It's sufficient to create the config key
App::Application::Config()["DontOverrideStdIn"] = "";
Gui::MainWindow *mw = new Gui::MainWindow();
hasMainWindow = true;
QIcon icon = qApp->windowIcon();
if (icon.isNull())
if (icon.isNull()) {
qApp->setWindowIcon(Gui::BitmapFactory().pixmap(App::Application::Config()["AppIcon"].c_str()));
}
mw->setWindowIcon(qApp->windowIcon());
QString appName = qApp->applicationName();
if (!appName.isEmpty())
if (!appName.isEmpty()) {
mw->setWindowTitle(appName);
else
}
else {
mw->setWindowTitle(QString::fromLatin1(App::Application::Config()["ExeName"].c_str()));
}
if (!SoDB::isInitialized()) {
// init the Inventor subsystem
@@ -350,7 +354,6 @@ QWidget* setupMainWindow()
Gui::Application::Instance->activateWorkbench(start.c_str());
mw->loadWindowSettings();
PySys_SetObject("stdin", input);
}
else {
Gui::getMainWindow()->show();