Skip to content

Commit 835dc01

Browse files
thewtexhjmjohnson
authored andcommitted
BUG: Use absolute module path PyCapsule_Import
Addresses: ``` ImportError: PyCapsule_Import could not import module "_ITKCommonPython" ``` That can occur when unpickling for Dask, for example. According to the `PyCapsule_Import` documentation, internally it calls `PyImport_ImportModule`. According to the `PyImport_ImportModule` documentation, this requires the absolute path -- we add the `itk.` prefix. In most other contexts, the `_ITKCommonPython` module is likely to already be imported. Since `_ITKCommonPython` gets loaded via PyImport_ImportModule, it is not registered with the ITK lazy loading module system, so we add `from . import _ITKCommon`.
1 parent 4fd5ea0 commit 835dc01

4 files changed

Lines changed: 7 additions & 2 deletions

File tree

Wrapping/Generators/Python/PyBase/pyBase.i

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import collections
77
from sys import version_info as _version_info
88
if _version_info < (3, 7, 0):
99
raise RuntimeError("Python 3.7 or later required")
10+
11+
from . import _ITKCommonPython
1012
%}
1113

1214
//By including pyabc.i and using the -py3 command line option when calling SWIG,

Wrapping/Generators/Python/itkPyITKCommonCAPI.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ static void ** _ITKCommonPython_API;
6767
static int
6868
import__ITKCommonPython()
6969
{
70-
_ITKCommonPython_API = (void **)PyCapsule_Import("_ITKCommonPython._C_API", 0);
70+
_ITKCommonPython_API = (void **)PyCapsule_Import("itk._ITKCommonPython._C_API", 0);
7171
return (_ITKCommonPython_API != NULL) ? 0 : -1;
7272
}
7373

Wrapping/Generators/SwigInterface/igenerator.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1489,7 +1489,10 @@ def generate_headerfile(self, idx_file, wrappers_namespace):
14891489
from sys import version_info as _version_info
14901490
if _version_info < (3, 7, 0):
14911491
raise RuntimeError("Python 3.7 or later required")
1492+
1493+
from . import _ITKCommonPython
14921494
%}
1495+
14931496
"""
14941497
)
14951498
# Also, release the GIL

Wrapping/macro_files/itk_end_wrap_module.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ _ITKCommonPython_API[_ITKCommonPython_GetGlobalSingletonIndex_NUM] = (void *)_IT
293293
294294
/* Create a Capsule containing the API pointer array's address */
295295
PyObject * cAPIObject = PyCapsule_New((void *)_ITKCommonPython_API,
296-
\"_ITKCommonPython._C_API\", NULL);
296+
\"itk._ITKCommonPython._C_API\", NULL);
297297
298298
if(cAPIObject != NULL)
299299
{

0 commit comments

Comments
 (0)