-
-
Notifications
You must be signed in to change notification settings - Fork 741
COMP: Make ITKVtkGlue wrapping abi3-compatible #6715
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hjmjohnson
wants to merge
1
commit into
InsightSoftwareConsortium:main
Choose a base branch
from
hjmjohnson:abi3-vtkglue
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,3 @@ | ||
| itk_wrap_module(ITKVtkGlue) | ||
| if(ITK_USE_PYTHON_LIMITED_API) | ||
| message( | ||
| FATAL_ERROR | ||
| "The ITKVtkGlue module can only built without Python limited API due to VTK limitations." | ||
| "Please set `ITK_USE_PYTHON_LIMITED_API` to `FALSE`." | ||
| ) | ||
| else() | ||
| list( | ||
| APPEND | ||
| WRAPPER_SWIG_LIBRARY_FILES | ||
| "${CMAKE_CURRENT_SOURCE_DIR}/VtkGlue.i" | ||
| ) | ||
| itk_auto_load_and_end_wrap_submodules() | ||
| endif() | ||
| list(APPEND WRAPPER_SWIG_LIBRARY_FILES "${CMAKE_CURRENT_SOURCE_DIR}/VtkGlue.i") | ||
| itk_auto_load_and_end_wrap_submodules() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| list(FIND ITK_WRAP_IMAGE_DIMS 2 wrap_2_index) | ||
| if( | ||
| ITK_WRAP_PYTHON | ||
| AND | ||
| VTK_WRAP_PYTHON | ||
| AND | ||
| ITK_WRAP_float | ||
| AND | ||
| wrap_2_index | ||
| GREATER | ||
| -1 | ||
| ) | ||
| itk_python_add_test( | ||
| NAME PythonVtkGlueABI3EncodingTest | ||
| COMMAND | ||
| ${CMAKE_CURRENT_SOURCE_DIR}/VtkGlueABI3EncodingTest.py | ||
| ) | ||
| itk_python_add_test( | ||
| NAME PythonVtkGlueRoundTripTest | ||
| COMMAND | ||
| ${CMAKE_CURRENT_SOURCE_DIR}/VtkGlueRoundTripTest.py | ||
| ) | ||
| # itkTestDriver prepends ITK's own entries to whatever PYTHONPATH it inherits. | ||
| set_property( | ||
| TEST | ||
| PythonVtkGlueABI3EncodingTest | ||
| PythonVtkGlueRoundTripTest | ||
| PROPERTY | ||
| ENVIRONMENT | ||
| "PYTHONPATH=${VTK_PREFIX_PATH}/${VTK_PYTHONPATH}" | ||
| ) | ||
| endif() |
100 changes: 100 additions & 0 deletions
100
Modules/Bridge/VtkGlue/wrapping/test/VtkGlueABI3EncodingTest.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| # ========================================================================== | ||
| # | ||
| # Copyright NumFOCUS | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # https://www.apache.org/licenses/LICENSE-2.0.txt | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # | ||
| # ========================================================================== | ||
|
|
||
| """Canary for the two VTK wrapper encodings the ITKVtkGlue abi3 typemaps rely on. | ||
|
|
||
| VtkGlue.i exchanges pointers with VTK through `__this__` and the `Addr=0x...` | ||
| argument to `__new__` rather than through vtkPythonUtil, because vtkPythonUtil's | ||
| header chain is not usable under Py_LIMITED_API. Neither encoding is documented | ||
| VTK API, so this test fails loudly and specifically if VTK changes either one. | ||
|
|
||
| `__new__` is used rather than plain construction because vtkmodules.util.data_model | ||
| registers keyword-only `override` subclasses for vtkImageData and vtkPolyData; | ||
| `cls(addr)` reaches those and raises TypeError. | ||
|
|
||
| Both encodings and the `__new__` string branch are present in every VTK 9.x from | ||
| 9.0.0 onward, so the module's VTK 9.1 floor is unchanged. The branch is gated on | ||
| the type not being a heap type, which is the thing to re-check if VTK ever makes | ||
| its wrapped types heap types. | ||
| """ | ||
|
|
||
| import re | ||
| import sys | ||
|
|
||
| from vtkmodules.vtkCommonDataModel import vtkImageData, vtkPolyData | ||
| from vtkmodules.vtkIOImage import vtkImageExport, vtkImageImport | ||
|
|
||
| # `_<2*sizeof(void*) hex digits>_p_<ClassName>`, per vtkPythonUtil::ManglePointer. | ||
| THIS_RE = re.compile(r"^_([0-9a-fA-F]+)_p_(\w+)$") | ||
|
|
||
| failures = [] | ||
|
|
||
|
|
||
| def check(condition, message): | ||
| if not condition: | ||
| failures.append(message) | ||
|
|
||
|
|
||
| for cls in (vtkImageData, vtkPolyData, vtkImageExport, vtkImageImport): | ||
| name = cls.__name__ | ||
| obj = cls() | ||
|
|
||
| this = getattr(obj, "__this__", None) | ||
| check(this is not None, f"{name}: instance has no __this__ attribute") | ||
| if this is None: | ||
| continue | ||
|
|
||
| match = THIS_RE.match(this) | ||
| check( | ||
| match is not None, | ||
| f"{name}: __this__ {this!r} does not match _<hex>_p_<ClassName>", | ||
| ) | ||
| if match is None: | ||
| continue | ||
|
|
||
| address = int(match.group(1), 16) | ||
| check(address != 0, f"{name}: __this__ encodes a null address") | ||
| check( | ||
| match.group(2) == name, | ||
| f"{name}: __this__ encodes class {match.group(2)!r}, expected {name!r}", | ||
| ) | ||
|
|
||
| # The reconstruction path VtkGlue.i's `out` typemaps drive. | ||
| try: | ||
| rebuilt = cls.__new__(cls, f"Addr=0x{address:x}") | ||
| except Exception as exception: # noqa: BLE001 - report any refusal verbatim | ||
| failures.append(f"{name}: Addr=0x... reconstruction raised {exception!r}") | ||
| continue | ||
|
|
||
| rebuilt_this = getattr(rebuilt, "__this__", None) | ||
| check( | ||
| rebuilt_this == this, | ||
| f"{name}: reconstruction yielded __this__ {rebuilt_this!r}, expected {this!r}", | ||
| ) | ||
| check( | ||
| isinstance(rebuilt, cls), | ||
| f"{name}: reconstruction yielded {type(rebuilt)!r}, not a {name} instance", | ||
| ) | ||
|
|
||
| if failures: | ||
| print("VTK wrapper encodings assumed by VtkGlue.i have changed:", file=sys.stderr) | ||
| for failure in failures: | ||
| print(f" - {failure}", file=sys.stderr) | ||
| sys.exit(1) | ||
|
|
||
| print("VTK __this__ and Addr=0x... encodings are intact.") |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.