----------------------------------------------------------------------------
| 571 | |
| 572 | //---------------------------------------------------------------------------- |
| 573 | bool ctkPythonConsolePrivate::push(const QString& code) |
| 574 | { |
| 575 | Q_ASSERT(this->PythonManager); |
| 576 | |
| 577 | bool ret_value = false; |
| 578 | |
| 579 | QString buffer = code; |
| 580 | // The embedded python interpreter cannot handle DOS line-endings, see |
| 581 | // http://sourceforge.net/tracker/?group_id=5470&atid=105470&func=detail&aid=1167922 |
| 582 | buffer.remove('\r'); |
| 583 | PyErr_Clear(); // make sure we are not in an error state before we execute the command |
| 584 | PyObject *res = PyObject_CallMethod(this->InteractiveConsole, |
| 585 | const_cast<char*>("push"), |
| 586 | const_cast<char*>("z"), |
| 587 | #if PY_MAJOR_VERSION >= 3 |
| 588 | buffer.toUtf8().data()); |
| 589 | #else |
| 590 | buffer.toLatin1().data()); |
| 591 | #endif |
| 592 | if (res) |
| 593 | { |
| 594 | int status = 0; |
| 595 | if (PyArg_Parse(res, "i", &status)) |
| 596 | { |
| 597 | ret_value = (status > 0); |
| 598 | } |
| 599 | Py_DECREF(res); |
| 600 | } |
| 601 | else |
| 602 | { |
| 603 | // error occurred |
| 604 | PyErr_Clear(); |
| 605 | } |
| 606 | return ret_value; |
| 607 | } |
| 608 | |
| 609 | //---------------------------------------------------------------------------- |
| 610 | void ctkPythonConsolePrivate::printWelcomeMessage() |