Skip to content

Commit 7602a0e

Browse files
committed
Make vtkCollection implement iterable in the python wrapping
1 parent 0442298 commit 7602a0e

File tree

4 files changed

+125
-3
lines changed

4 files changed

+125
-3
lines changed

Common/Core/Testing/Python/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ vtk_add_test_python(
99
TestEnums.py
1010
TestExecuteMethodFinalizeCrash.py
1111
TestGhost.py
12+
TestIterateCollection.py
1213
TestMutable.py
1314
TestNumpySupport.py
1415
TestNumpyInterface.py
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"""Test iterating through a vtkCollection with the standard python syntax"""
2+
3+
4+
import vtk
5+
from vtk.test import Testing
6+
7+
8+
class TestIterateCollection(Testing.vtkTest):
9+
def setUp(self):
10+
self.vtkObjs = [vtk.vtkObject() for _ in range(30)]
11+
self.collection = vtk.vtkCollection()
12+
for obj in self.vtkObjs:
13+
self.collection.AddItem(obj)
14+
15+
self.emptyCollection = vtk.vtkCollection()
16+
17+
def testIterateCollection(self):
18+
newObjsList = [obj for obj in self.collection]
19+
self.assertEqual(self.vtkObjs, newObjsList)
20+
21+
counter = 0
22+
for _ in self.collection:
23+
counter += 1
24+
self.assertEqual(counter, 30)
25+
26+
counter = 0
27+
for _ in self.emptyCollection:
28+
counter += 1
29+
self.assertEqual(counter, 0)
30+
31+
def testCollectionChild(self):
32+
#check that iteration is being inherited correctly
33+
dataArray = vtk.vtkIntArray()
34+
35+
dataArrayCollection = vtk.vtkDataArrayCollection()
36+
dataArrayCollection.AddItem(dataArray)
37+
38+
self.assertEqual([obj for obj in dataArrayCollection],
39+
[dataArray])
40+
41+
def testOperators(self):
42+
self.assertTrue(self.vtkObjs[0] in self.collection)
43+
self.assertEqual(list(self.collection), self.vtkObjs)
44+
45+
46+
if __name__ == "__main__":
47+
Testing.main([(TestIterateCollection, 'test')])

Wrapping/Tools/vtkWrapPythonClass.c

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -550,9 +550,29 @@ void vtkWrapPython_GenerateObjectType(
550550
" PyVTKObject_Traverse, // tp_traverse\n"
551551
" 0, // tp_clear\n"
552552
" 0, // tp_richcompare\n"
553-
" offsetof(PyVTKObject, vtk_weakreflist), // tp_weaklistoffset\n"
554-
" 0, // tp_iter\n"
555-
" 0, // tp_iternext\n"
553+
" offsetof(PyVTKObject, vtk_weakreflist), // tp_weaklistoffset\n");
554+
if (strcmp(classname, "vtkCollection") == 0)
555+
{
556+
fprintf(fp,
557+
" PyvtkCollection_iter, // tp_iter\n"
558+
" 0, // tp_iternext\n");
559+
}
560+
else
561+
{
562+
if(strcmp(classname, "vtkCollectionIterator") == 0)
563+
{
564+
fprintf(fp,
565+
" PyvtkCollectionIterator_iter, // tp_iter\n"
566+
" PyvtkCollectionIterator_next, // tp_iternext\n");
567+
}
568+
else
569+
{
570+
fprintf(fp,
571+
" 0, // tp_iter\n"
572+
" 0, // tp_iternext\n");
573+
}
574+
}
575+
fprintf(fp,
556576
" 0, // tp_methods\n"
557577
" 0, // tp_members\n"
558578
" PyVTKObject_GetSet, // tp_getset\n"

Wrapping/Tools/vtkWrapPythonMethodDef.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -873,4 +873,58 @@ static void vtkWrapPython_CustomMethods(
873873
"\n",
874874
classname, data->Name, data->Name, data->Name);
875875
}
876+
if (strcmp("vtkCollection", data->Name) == 0 &&
877+
do_constructors == 0)
878+
{
879+
fprintf(fp,
880+
"static PyObject *\n"
881+
"PyvtkCollection_iter(PyObject *self)\n"
882+
"{\n"
883+
" PyVTKObject* vp = (PyVTKObject *) self;\n"
884+
" vtkCollection *op = (vtkCollection* ) vp->vtk_ptr; \n"
885+
" \n"
886+
" PyObject *result = NULL;\n"
887+
" \n"
888+
" if (op)\n"
889+
" {\n"
890+
" vtkObject *tempr = (vtkObject *) op->NewIterator();\n"
891+
" if (tempr != NULL)\n"
892+
" {\n"
893+
" result = vtkPythonArgs::BuildVTKObject(tempr);\n"
894+
" }\n"
895+
" }\n"
896+
"\n"
897+
" return result;\n"
898+
"}\n");
899+
}
900+
if (strcmp("vtkCollectionIterator", data->Name) == 0 &&
901+
do_constructors == 0)
902+
{
903+
fprintf(fp,
904+
"static PyObject * PyvtkCollectionIterator_next(PyObject *self)\n"
905+
"{\n"
906+
" PyVTKObject* vp = (PyVTKObject *) self;\n"
907+
" vtkCollectionIterator *op = (vtkCollectionIterator* ) vp->vtk_ptr; \n"
908+
" \n"
909+
" PyObject *result = NULL;\n"
910+
" \n"
911+
" if (op)\n"
912+
" {\n"
913+
" vtkObject *tempr = op->GetCurrentObject();\n"
914+
" op->GoToNextItem();\n"
915+
" if (tempr != NULL)\n"
916+
" {\n"
917+
" result = vtkPythonArgs::BuildVTKObject(tempr);\n"
918+
" }\n"
919+
" }\n"
920+
"\n"
921+
" return result;\n"
922+
"}\n"
923+
"\n"
924+
"static PyObject *PyvtkCollectionIterator_iter(PyObject *self)\n"
925+
"{\n"
926+
" Py_INCREF(self);\n"
927+
" return self;\n"
928+
"}\n");
929+
}
876930
}

0 commit comments

Comments
 (0)