]> SALOME platform Git repositories - modules/kernel.git/commitdiff
Salome HOME
bos #26455 Alternative solution to get traceback V9_8_0a1 V9_8_0a2
authorvsr <vsr@opencascade.com>
Thu, 14 Oct 2021 16:44:14 +0000 (19:44 +0300)
committervsr <vsr@opencascade.com>
Thu, 14 Oct 2021 16:44:14 +0000 (19:44 +0300)
src/Container/Container_i.cxx

index e7674b57e2c59520e4a4e81e9d116cf2cdc6e453..8d23b70ccf30011dca432eaf0797e5a5f37982d0 100644 (file)
@@ -63,6 +63,7 @@ int SIGUSR1 = 1000;
 #endif
 
 #include <Python.h>
+#include <structmember.h>
 #include "Container_init_python.hxx"
 #ifdef BOS26455_WITH_BOOST_PYTHON
 #include <boost/python.hpp>
@@ -372,6 +373,113 @@ CORBA::Long Abstract_Engines_Container_i::getNumberOfCPUCores()
 */
 //=============================================================================
 namespace {
+  typedef struct
+  {
+    PyObject_HEAD
+    int softspace;
+    std::string *out;
+  } PyStdOut;
+
+  static void
+  PyStdOut_dealloc(PyStdOut *self)
+  {
+    PyObject_Del(self);
+  }
+
+  static PyObject*
+  PyStdOut_write(PyStdOut* self, PyObject* args)
+  {
+    char *c;
+    if (!PyArg_ParseTuple(args, "s", &c))
+      return NULL;
+
+    *(self->out) = *(self->out) + c;
+
+    Py_INCREF(Py_None);
+    return Py_None;
+  }
+
+  static PyMethodDef PyStdOut_methods[] =
+  {
+    {"write",  (PyCFunction)PyStdOut_write,  METH_VARARGS,
+      PyDoc_STR("write(string) -> None")},
+    {0, 0, 0, 0}  /* sentinel */
+  };
+
+  static PyMemberDef PyStdOut_memberlist[] =
+  {
+    {(char*)"softspace", T_INT, offsetof(PyStdOut, softspace), 0,
+     (char*)"flag indicating that a space needs to be printed; used by print"},
+    {0, 0, 0, 0, 0}   /* sentinel */
+  };
+
+  static PyTypeObject PyStdOut_Type =
+  {
+    /* The ob_type field must be initialized in the module init function
+     * to be portable to Windows without using C++. */
+    PyVarObject_HEAD_INIT(NULL, 0)
+    /* 0, */                      /*ob_size*/
+    "PyOut",                      /*tp_name*/
+    sizeof(PyStdOut),             /*tp_basicsize*/
+    0,                            /*tp_itemsize*/
+    /* methods */
+    (destructor)PyStdOut_dealloc, /*tp_dealloc*/
+    0,                            /*tp_print*/
+    0,                            /*tp_getattr*/
+    0,                            /*tp_setattr*/
+    0,                            /*tp_compare*/
+    0,                            /*tp_repr*/
+    0,                            /*tp_as_number*/
+    0,                            /*tp_as_sequence*/
+    0,                            /*tp_as_mapping*/
+    0,                            /*tp_hash*/
+    0,                            /*tp_call*/
+    0,                            /*tp_str*/
+    PyObject_GenericGetAttr,      /*tp_getattro*/
+    /* softspace is writable:  we must supply tp_setattro */
+    PyObject_GenericSetAttr,      /* tp_setattro */
+    0,                            /*tp_as_buffer*/
+    Py_TPFLAGS_DEFAULT,           /*tp_flags*/
+    0,                            /*tp_doc*/
+    0,                            /*tp_traverse*/
+    0,                            /*tp_clear*/
+    0,                            /*tp_richcompare*/
+    0,                            /*tp_weaklistoffset*/
+    0,                            /*tp_iter*/
+    0,                            /*tp_iternext*/
+    PyStdOut_methods,             /*tp_methods*/
+    PyStdOut_memberlist,          /*tp_members*/
+    0,                            /*tp_getset*/
+    0,                            /*tp_base*/
+    0,                            /*tp_dict*/
+    0,                            /*tp_descr_get*/
+    0,                            /*tp_descr_set*/
+    0,                            /*tp_dictoffset*/
+    0,                            /*tp_init*/
+    0,                            /*tp_alloc*/
+    0,                            /*tp_new*/
+    0,                            /*tp_free*/
+    0,                            /*tp_is_gc*/
+    0,                            /*tp_bases*/
+    0,                            /*tp_mro*/
+    0,                            /*tp_cache*/
+    0,                            /*tp_subclasses*/
+    0,                            /*tp_weaklist*/
+    0,                            /*tp_del*/
+    0,                            /*tp_version_tag*/
+    0,                            /*tp_finalize*/
+  };
+
+  PyObject* newPyStdOut(std::string& out)
+  {
+    PyStdOut* self = PyObject_New(PyStdOut, &PyStdOut_Type);
+    if (self) {
+      self->softspace = 0;
+      self->out=&out;
+    }
+    return (PyObject*)self;
+  }
+
   std::string parseException()
   {
     std::string error;
@@ -395,7 +503,13 @@ namespace {
       boost::python::object formatted = format_exc(htype, hvalue, htraceback);
       error = boost::python::extract<std::string>(boost::python::str("\n").join(formatted));
 #else
-      error = "Python exception is caught";
+      PyObject* new_stderr = newPyStdOut(error);
+      PyObject* old_stderr = PySys_GetObject((char*)"stderr");
+      Py_INCREF(old_stderr);
+      PySys_SetObject((char*)"stderr", new_stderr);
+      PyErr_Print();
+      PySys_SetObject((char*)"stderr", old_stderr);
+      Py_DECREF(new_stderr);
 #endif
     }
     return error;