| 450 | } |
| 451 | |
| 452 | PyObject* DocumentObjectPy::evalExpression(PyObject* self, PyObject* args) |
| 453 | { |
| 454 | const char* expr; |
| 455 | if (!PyArg_ParseTuple(args, "s", &expr)) { |
| 456 | return nullptr; |
| 457 | } |
| 458 | |
| 459 | // HINT: |
| 460 | // The standard behaviour of Python for class methods is to always pass the class |
| 461 | // object as first argument. |
| 462 | // For FreeCAD-specific types the behaviour is a bit different: |
| 463 | // When calling this method for an instance then this is passed as first argument |
| 464 | // and otherwise the class object is passed. |
| 465 | // This behaviour is achieved by the function _getattr() that passed 'this' to |
| 466 | // PyCFunction_New(). |
| 467 | // |
| 468 | // evalExpression() is a class method and thus 'self' can either be an instance of |
| 469 | // DocumentObjectPy or a type object. |
| 470 | App::DocumentObject* obj = nullptr; |
| 471 | if (self && PyObject_TypeCheck(self, &DocumentObjectPy::Type)) { |
| 472 | obj = static_cast<DocumentObjectPy*>(self)->getDocumentObjectPtr(); |
| 473 | } |
| 474 | |
| 475 | PY_TRY |
| 476 | { |
| 477 | std::shared_ptr<Expression> shared_expr(Expression::parse(obj, expr)); |
| 478 | if (shared_expr) { |
| 479 | return Py::new_reference_to(shared_expr->getPyValue()); |
| 480 | } |
| 481 | Py_Return; |
| 482 | } |
| 483 | PY_CATCH |
| 484 | } |
| 485 |
no test coverage detected