|
| 1 | +# ========================================================================== |
| 2 | +# |
| 3 | +# Copyright NumFOCUS |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# https://www.apache.org/licenses/LICENSE-2.0.txt |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | +# ========================================================================== |
| 18 | +"""GradientImageFilter.OverrideBoundaryCondition takes ownership of its argument. |
| 19 | +
|
| 20 | +Without a DISOWN typemap, Python and the filter's unique_ptr member both free the |
| 21 | +boundary condition, and the interpreter aborts at shutdown. A non-zero exit code |
| 22 | +from this script is itself part of the regression check. |
| 23 | +""" |
| 24 | + |
| 25 | +import itk |
| 26 | + |
| 27 | +itk.auto_progress(2) |
| 28 | + |
| 29 | +ImageType = itk.Image[itk.F, 2] |
| 30 | + |
| 31 | +filt = itk.GradientImageFilter[ImageType, itk.F, itk.F].New() |
| 32 | +boundaryCondition = itk.PeriodicBoundaryCondition[ImageType]() |
| 33 | + |
| 34 | +assert ( |
| 35 | + boundaryCondition.thisown |
| 36 | +), "Python should own a freshly constructed boundary condition" |
| 37 | + |
| 38 | +filt.OverrideBoundaryCondition(boundaryCondition) |
| 39 | + |
| 40 | +assert ( |
| 41 | + not boundaryCondition.thisown |
| 42 | +), "OverrideBoundaryCondition must transfer ownership away from Python" |
| 43 | + |
| 44 | +# The filter must remain usable with the adopted boundary condition. |
| 45 | +image = ImageType.New() |
| 46 | +region = itk.ImageRegion[2]() |
| 47 | +region.SetSize([8, 8]) |
| 48 | +image.SetRegions(region) |
| 49 | +image.Allocate() |
| 50 | +image.FillBuffer(1.0) |
| 51 | + |
| 52 | +filt.SetInput(image) |
| 53 | +filt.Update() |
| 54 | + |
| 55 | +assert filt.GetOutput().GetLargestPossibleRegion().GetSize()[0] == 8 |
| 56 | + |
| 57 | +print("Test finished.") |
0 commit comments