diff --git a/src/Gui/PythonConsole.cpp b/src/Gui/PythonConsole.cpp index 5d68123f18..42c053fa01 100644 --- a/src/Gui/PythonConsole.cpp +++ b/src/Gui/PythonConsole.cpp @@ -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")); diff --git a/src/Gui/PythonConsolePy.cpp b/src/Gui/PythonConsolePy.cpp index 4845a795b5..8048aa0711 100644 --- a/src/Gui/PythonConsolePy.cpp +++ b/src/Gui/PythonConsolePy.cpp @@ -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() ); diff --git a/src/Gui/PythonConsolePy.h b/src/Gui/PythonConsolePy.h index e4db9a58e9..9e9f919729 100644 --- a/src/Gui/PythonConsolePy.h +++ b/src/Gui/PythonConsolePy.h @@ -145,6 +145,7 @@ public: ~PythonStdin() override; Py::Object repr() override; + Py::Object getattr(const char *name) override; Py::Object readline(const Py::Tuple&); }; diff --git a/src/Main/FreeCADGuiPy.cpp b/src/Main/FreeCADGuiPy.cpp index 54d9571921..a449fc0c97 100644 --- a/src/Main/FreeCADGuiPy.cpp +++ b/src/Main/FreeCADGuiPy.cpp @@ -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();