Skip to content

Commit

Permalink
Merge topic 'update-const-correct'
Browse files Browse the repository at this point in the history
54d9372 Add a test for default args in python wrappers.
f2786d4 Handle default values for array parameters in python.
000e36f Minor tweak to signature to improve const correctness.

Acked-by: Kitware Robot <kwrobot@kitware.com>
Reviewed-by: Berk Geveci <berk.geveci@kitware.com>
Merge-request: !1201
  • Loading branch information
dgobbi authored and kwrobot committed Feb 10, 2016
2 parents 031288a + 54d9372 commit 19c7504
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 18 deletions.
1 change: 1 addition & 0 deletions Common/Core/Testing/Python/CMakeLists.txt
Expand Up @@ -4,6 +4,7 @@ vtk_add_test_python(
TestArrays.py
TestArrayArguments.py
TestBuffer.py
TestDefaultArgs.py
TestEmptyInput.py
TestEnums.py
TestExecuteMethodFinalizeCrash.py
Expand Down
63 changes: 63 additions & 0 deletions Common/Core/Testing/Python/TestDefaultArgs.py
@@ -0,0 +1,63 @@
"""Test methods that use default parameter values.
Created on Feb 9, 2016 by David Gobbi
"""

import sys
import vtk
from vtk.test import Testing

class TestDefaultArgs(Testing.vtkTest):
def testDefaultInt(self):
"""Simple test of an integer arg with default value."""
image = vtk.vtkImageData()
image.SetExtent(0,9,0,9,0,9)
image.AllocateScalars(vtk.VTK_UNSIGNED_CHAR, 1)
ipi = vtk.vtkImagePointIterator()
# call this method with the threadId parameter set to 0
ipi.Initialize(image, (0,9,0,9,0,9), None, None, 0)
# call this method without the threadId parameter
ipi.Initialize(image, (0,9,0,9,0,9), None, None)

def testDefaultObjectPointer(self):
"""Test a vtkObject pointer arg with default value of 0."""
image = vtk.vtkImageData()
image.SetExtent(0,9,0,9,0,9)
image.AllocateScalars(vtk.VTK_UNSIGNED_CHAR, 1)
ipi = vtk.vtkImagePointIterator()
# call this method with the stencil parameter set to None
ipi.Initialize(image, (0,9,0,9,0,9), None)
# call this method without the stencil parameter
ipi.Initialize(image, (0,9,0,9,0,9))

def testDefaultArray(self):
"""Test an array arg with default value of 0."""
image = vtk.vtkImageData()
image.SetExtent(0,9,0,9,0,9)
image.AllocateScalars(vtk.VTK_UNSIGNED_CHAR, 1)
ipi = vtk.vtkImagePointIterator()
# call this method with the parameter set
ipi.Initialize(image, (0,9,0,9,0,9))
# call this method without extent parameter
ipi.Initialize(image)
# do another method for good measure
source = vtk.vtkImageGridSource()
source.SetDataExtent((0,99,0,99,0,0))
# set the parameter
source.UpdateExtent((0,50,0,50,0,0))
# use default parameter value
source.UpdateExtent()

def testDefaultPointer(self):
"""Test a POD pointer arg with default value of 0."""
a = vtk.vtkIntArray()
a.SetNumberOfComponents(3)
# pass an int pointer arg, expect something back
inc = [0]
vtk.vtkImagePointDataIterator.GetVoidPointer(a, 0, inc)
self.assertEqual(inc, [3])
# do not pass the pointer arg, default value 0 is passed
vtk.vtkImagePointDataIterator.GetVoidPointer(a, 0)

if __name__ == "__main__":
Testing.main([(TestDefaultArgs, 'test')])
6 changes: 3 additions & 3 deletions Common/ExecutionModel/vtkAlgorithm.cxx
Expand Up @@ -1481,7 +1481,7 @@ int vtkAlgorithm::Update(vtkInformation* requests)

//----------------------------------------------------------------------------
int vtkAlgorithm::UpdatePiece(
int piece, int numPieces, int ghostLevels, int* extents)
int piece, int numPieces, int ghostLevels, const int extents[6])
{
typedef vtkStreamingDemandDrivenPipeline vtkSDDP;

Expand All @@ -1497,7 +1497,7 @@ int vtkAlgorithm::UpdatePiece(
}

//----------------------------------------------------------------------------
int vtkAlgorithm::UpdateExtent(int* extents)
int vtkAlgorithm::UpdateExtent(const int extents[6])
{
typedef vtkStreamingDemandDrivenPipeline vtkSDDP;

Expand All @@ -1508,7 +1508,7 @@ int vtkAlgorithm::UpdateExtent(int* extents)

//----------------------------------------------------------------------------
int vtkAlgorithm::UpdateTimeStep(
double time, int piece, int numPieces, int ghostLevels, int* extents)
double time, int piece, int numPieces, int ghostLevels, const int extents[6])
{
typedef vtkStreamingDemandDrivenPipeline vtkSDDP;

Expand Down
6 changes: 3 additions & 3 deletions Common/ExecutionModel/vtkAlgorithm.h
Expand Up @@ -494,21 +494,21 @@ class VTKCOMMONEXECUTIONMODEL_EXPORT vtkAlgorithm : public vtkObject
// Update(int port, vtkInformationVector* requests) for details.
// Supports piece and extent (optional) requests.
virtual int UpdatePiece(
int piece, int numPieces, int ghostLevels, int* extents=0);
int piece, int numPieces, int ghostLevels, const int extents[6]=0);

// Description:
// Convenience method to update an algorithm after passing requests
// to its first output port.
// Supports extent request.
virtual int UpdateExtent(int* extents);
virtual int UpdateExtent(const int extents[6]=0);

// Description:
// Convenience method to update an algorithm after passing requests
// to its first output port. See documentation for
// Update(int port, vtkInformationVector* requests) for details.
// Supports time, piece (optional) and extent (optional) requests.
virtual int UpdateTimeStep(double time,
int piece=-1, int numPieces=1, int ghostLevels=0, int* extents=0);
int piece=-1, int numPieces=1, int ghostLevels=0, const int extents[6]=0);

// Description:
// Bring the algorithm's information up-to-date.
Expand Down
11 changes: 6 additions & 5 deletions Wrapping/Tools/vtkWrap.c
Expand Up @@ -422,7 +422,6 @@ int vtkWrap_CountRequiredArguments(FunctionInfo *f)
for (i = 0; i < totalArgs; i++)
{
if (f->Parameters[i]->Value == NULL ||
vtkWrap_IsArray(f->Parameters[i]) ||
vtkWrap_IsNArray(f->Parameters[i]))
{
requiredArgs = i+1;
Expand Down Expand Up @@ -1012,7 +1011,8 @@ void vtkWrap_DeclareVariable(
fprintf(fp, "*");
}
/* arrays of unknown size are handled via pointers */
else if (val->CountHint || vtkWrap_IsPODPointer(val))
else if (val->CountHint || vtkWrap_IsPODPointer(val) ||
(vtkWrap_IsArray(val) && val->Value))
{
fprintf(fp, "*");
}
Expand All @@ -1037,7 +1037,8 @@ void vtkWrap_DeclareVariable(
aType != VTK_PARSE_OBJECT_PTR &&
!vtkWrap_IsQtObject(val) &&
val->CountHint == NULL &&
!vtkWrap_IsPODPointer(val))
!vtkWrap_IsPODPointer(val) &&
!(vtkWrap_IsArray(val) && val->Value))
{
if (val->NumberOfDimensions == 1 && val->Count > 0)
{
Expand Down Expand Up @@ -1114,8 +1115,8 @@ void vtkWrap_DeclareVariableSize(
{
fprintf(fp,
" %sint %s%s = %d;\n",
(val->Count == 0 ? "" : "const "), name, idx,
(val->Count == 0 ? 0 : val->Count));
((val->Count == 0 || val->Value != 0) ? "" : "const "),
name, idx, (val->Count == 0 ? 0 : val->Count));
}
else if (val->NumberOfDimensions == 1)
{
Expand Down
27 changes: 20 additions & 7 deletions Wrapping/Tools/vtkWrapPythonMethod.c
Expand Up @@ -113,15 +113,16 @@ void vtkWrapPython_DeclareVariables(
if (vtkWrap_IsArray(arg) || vtkWrap_IsNArray(arg) ||
vtkWrap_IsPODPointer(arg))
{
storageSize = 4;
storageSize = (arg->Count ? arg->Count : 4);
if (!vtkWrap_IsConst(arg) &&
!vtkWrap_IsSetVectorMethod(theFunc))
{
/* for saving a copy of the array */
vtkWrap_DeclareVariable(fp, data, arg, "save", i, VTK_WRAP_ARG);
storageSize *= 2;
}
if (arg->CountHint || vtkWrap_IsPODPointer(arg))
if (arg->CountHint || vtkWrap_IsPODPointer(arg) ||
(vtkWrap_IsArray(arg) && arg->Value))
{
fprintf(fp,
" %s small%d[%d];\n",
Expand Down Expand Up @@ -167,7 +168,8 @@ static void vtkWrapPython_GetSizesForArrays(
{
arg = theFunc->Parameters[i];

if (arg->CountHint || vtkWrap_IsPODPointer(arg))
if (arg->CountHint || vtkWrap_IsPODPointer(arg) ||
(vtkWrap_IsArray(arg) && arg->Value))
{
if (j == 1)
{
Expand Down Expand Up @@ -200,18 +202,29 @@ static void vtkWrapPython_GetSizesForArrays(
fprintf(fp,
"%s if (size%d > 0)\n"
"%s {\n"
"%s temp%d = small%d;\n"
"%s temp%d = small%d;\n",
indentation, i,
indentation,
indentation, i, i);

if (arg->CountHint || vtkWrap_IsPODPointer(arg))
{
fprintf(fp,
"%s if (size%d > 4)\n"
"%s {\n"
"%s temp%d = new %s[%ssize%d];\n"
"%s }\n",
indentation, i,
indentation,
indentation, i, i,
indentation, i,
indentation,
indentation, i, vtkWrap_GetTypeName(arg), mtwo, i,
indentation);
}
else
{
fprintf(fp,
"%s size%d = %d;\n",
indentation, i, arg->Count);
}

if (*mtwo)
{
Expand Down

0 comments on commit 19c7504

Please sign in to comment.