Skip to content

Commit 641ab2e

Browse files
committed
lowlevel: Convert _OpenSlide to a wrapper class
Constructors of c_void_p subclasses are not called when the class is used as a restype. Convert _OpenSlide to a wrapper class that stores the handle in _as_parameter_.
1 parent 2e6ae42 commit 641ab2e

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

openslide/lowlevel.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,25 @@ class OpenSlideError(Exception):
4646
Import this from openslide rather than from openslide.lowlevel.
4747
"""
4848

49-
# validating class to make sure we correctly pass an OpenSlide handle
50-
class _OpenSlide(c_void_p):
49+
class _OpenSlide(object):
50+
"""Wrapper class to make sure we correctly pass an OpenSlide handle."""
51+
52+
def __init__(self, ptr):
53+
self._as_parameter_ = ptr
54+
5155
@classmethod
5256
def from_param(cls, obj):
53-
if not obj:
54-
raise ValueError("Passing undefined slide object")
5557
if obj.__class__ != cls:
5658
raise ValueError("Not an OpenSlide reference")
57-
return super(_OpenSlide, cls).from_param(obj)
59+
if not obj._as_parameter_:
60+
raise ValueError("Passing undefined slide object")
61+
return obj
5862

59-
# check for errors opening an image file
63+
# check for errors opening an image file and wrap the resulting handle
6064
def _check_open(result, _func, _args):
61-
if result.value is None:
65+
if result is None:
6266
raise OpenSlideError("Could not open image file")
63-
return result
67+
return _OpenSlide(result)
6468

6569
# check if the library got into an error state after each library call
6670
def _check_error(result, _func, args):
@@ -102,7 +106,7 @@ def _load_image(buf, size):
102106

103107
can_open = _func('openslide_can_open', c_bool, [c_char_p], None)
104108

105-
open = _func('openslide_open', _OpenSlide, [c_char_p], _check_open)
109+
open = _func('openslide_open', c_void_p, [c_char_p], _check_open)
106110

107111
close = _func('openslide_close', None, [_OpenSlide], None)
108112

0 commit comments

Comments
 (0)