Commit 42046a26 authored by David Gobbi's avatar David Gobbi Committed by Kitware Robot

Merge topic '17112-py-vs-singleton'

74ea5d0e 17112: Wrapper fix for New() that returns singleton
Acked-by: Kitware Robot's avatarKitware Robot <kwrobot@kitware.com>
Reviewed-by: Utkarsh Ayachit's avatarUtkarsh Ayachit <utkarsh.ayachit@kitware.com>
Reviewed-by: Ben Boeckel's avatarBen Boeckel <ben.boeckel@kitware.com>
Merge-request: !3223
parents 9b8b4ac9 74ea5d0e
......@@ -19,6 +19,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