Commit 74ea5d0e authored by David Gobbi's avatar David Gobbi

17112: Wrapper fix for New() that returns singleton

The Python wrappers assumed that the VTK New() method would always
create a new instance, but there are cases where that is not true,
such as for singletons like vtkOutputWindow.

Closes #17112
parent 587abee9
......@@ -18,6 +18,7 @@ vtk_add_test_python(
TestOverloads.py
TestPointers.py
TestSetGet.py
TestSingleton.py
TestSOADataArrays.py
TestStrings.py
TestSubClass.py
......
"""Test support of VTK singleton objects
Created on Aug 30, 2017 by David Gobbi
"""
import sys
import vtk
from vtk.test import Testing
class TestSingleton(Testing.vtkTest):
def testOutputWindow(self):
a = vtk.vtkOutputWindow()
b = vtk.vtkOutputWindow()
self.assertIs(a, b)
def testObject(self):
a = vtk.vtkObject()
b = vtk.vtkObject()
self.assertNotEqual(a, b)
if __name__ == "__main__":
Testing.main([(TestSingleton, 'test')])
......@@ -552,6 +552,17 @@ PyObject *PyVTKObject_FromPointer(
"no concrete implementation exists for this class");
return nullptr;
}
// Check if the VTK object already has a Python object
// (e.g. vtk_new() might return a singleton instance)
PyObject *obj = vtkPythonUtil::FindObject(ptr);
if (obj)
{
ptr->Delete();
return obj;
}
// flag to indicate that the VTK object is a new instance
created = true;
// Check the type of the newly-created object
......
......@@ -433,7 +433,7 @@ void vtkPythonUtil::RemoveObjectFromMap(PyObject *obj)
}
//--------------------------------------------------------------------
PyObject *vtkPythonUtil::GetObjectFromPointer(vtkObjectBase *ptr)
PyObject *vtkPythonUtil::FindObject(vtkObjectBase *ptr)
{
PyObject *obj = nullptr;
......@@ -472,6 +472,14 @@ PyObject *vtkPythonUtil::GetObjectFromPointer(vtkObjectBase *ptr)
vtkPythonMap->GhostMap->erase(j);
}
return obj;
}
//--------------------------------------------------------------------
PyObject *vtkPythonUtil::GetObjectFromPointer(vtkObjectBase *ptr)
{
PyObject *obj = vtkPythonUtil::FindObject(ptr);
if (obj == nullptr)
{
// create a new object
......
......@@ -127,6 +127,13 @@ public:
*/
static void RemoveObjectFromMap(PyObject *obj);
/**
* Find the PyObject for a VTK object, return nullptr if not found.
* If the object is found, then it is returned as a new reference.
* Special behavior: If "ptr" is nullptr, then Py_None is returned.
*/
static PyObject *FindObject(vtkObjectBase *ptr);
/**
* Add a special VTK type to the type lookup table, this allows us to
* later create object given only the class name.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment