------------------------------------------------------------------------------
| 642 | |
| 643 | //------------------------------------------------------------------------------ |
| 644 | PyObject* PyVTKObject_FromPointer(PyTypeObject* pytype, PyObject* ghostdict, vtkObjectBase* ptr) |
| 645 | { |
| 646 | // This will be set if we create a new C++ object |
| 647 | bool created = false; |
| 648 | std::string classname = vtkPythonUtil::StripModuleFromType(pytype); |
| 649 | PyVTKClass* cls = nullptr; |
| 650 | |
| 651 | if (ptr) |
| 652 | { |
| 653 | // If constructing from an existing C++ object, use its actual class |
| 654 | classname = ptr->GetClassName(); |
| 655 | cls = vtkPythonUtil::FindClass(classname.c_str()); |
| 656 | } |
| 657 | |
| 658 | if (cls == nullptr) |
| 659 | { |
| 660 | // Use the vtkname of the supplied class type |
| 661 | PyObject* s = PyObject_GetAttrString((PyObject*)pytype, "__vtkname__"); |
| 662 | if (s) |
| 663 | { |
| 664 | PyObject* tmp = PyUnicode_AsUTF8String(s); |
| 665 | if (tmp) |
| 666 | { |
| 667 | Py_DECREF(s); |
| 668 | s = tmp; |
| 669 | } |
| 670 | const char* vtkname_classname = PyBytes_AsString(s); |
| 671 | if (vtkname_classname == nullptr) |
| 672 | { |
| 673 | Py_DECREF(s); |
| 674 | return nullptr; |
| 675 | } |
| 676 | classname = vtkname_classname; |
| 677 | Py_DECREF(s); |
| 678 | } |
| 679 | cls = vtkPythonUtil::FindClass(classname.c_str()); |
| 680 | if (cls == nullptr) |
| 681 | { |
| 682 | PyErr_Format(PyExc_ValueError, "internal error, unknown VTK class %.200s", classname.c_str()); |
| 683 | return nullptr; |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | if (!ptr) |
| 688 | { |
| 689 | // Create a new instance of this class since we were not given one. |
| 690 | if (cls->vtk_new) |
| 691 | { |
| 692 | ptr = cls->vtk_new(); |
| 693 | if (!ptr) |
| 694 | { |
| 695 | // The vtk_new() method returns null when a factory class has no |
| 696 | // implementation (i.e. cannot provide a concrete class instance.) |
| 697 | // NotImplementedError indicates a pure virtual method call. |
| 698 | PyErr_SetString( |
| 699 | PyExc_NotImplementedError, "no concrete implementation exists for this class"); |
| 700 | return nullptr; |
| 701 | } |
no test coverage detected