Skip to content

Commit

Permalink
lowlevel: allow bytes in type hints for _utf8_p arguments
Browse files Browse the repository at this point in the history
The high-level API doesn't accept bytes for any of the affected
functionality, but lowlevel does, so encode that in the type signatures.

Signed-off-by: Benjamin Gilbert <[email protected]>
  • Loading branch information
bgilbert committed Oct 20, 2024
1 parent 5f0b7b1 commit 3dce923
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions openslide/lowlevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ def read_icc_profile(slide: _OpenSlide) -> bytes | None:
'openslide_get_property_names', POINTER(c_char_p), [_OpenSlide], _check_name_list
)

get_property_value: _Func[[_OpenSlide, str], str] = _func(
get_property_value: _Func[[_OpenSlide, str | bytes], str] = _func(
'openslide_get_property_value', c_char_p, [_OpenSlide, _utf8_p]
)

Expand All @@ -485,7 +485,7 @@ def read_icc_profile(slide: _OpenSlide) -> bytes | None:
)

_get_associated_image_dimensions: _Func[
[_OpenSlide, str, _Pointer[c_int64], _Pointer[c_int64]], None
[_OpenSlide, str | bytes, _Pointer[c_int64], _Pointer[c_int64]], None
] = _func(
'openslide_get_associated_image_dimensions',
None,
Expand All @@ -494,46 +494,54 @@ def read_icc_profile(slide: _OpenSlide) -> bytes | None:


@_wraps_funcs([_get_associated_image_dimensions])
def get_associated_image_dimensions(slide: _OpenSlide, name: str) -> tuple[int, int]:
def get_associated_image_dimensions(
slide: _OpenSlide, name: str | bytes
) -> tuple[int, int]:
w, h = c_int64(), c_int64()
_get_associated_image_dimensions(slide, name, byref(w), byref(h))
return w.value, h.value


_read_associated_image: _Func[[_OpenSlide, str, _Pointer[c_uint32]], None] = _func(
'openslide_read_associated_image', None, [_OpenSlide, _utf8_p, POINTER(c_uint32)]
_read_associated_image: _Func[[_OpenSlide, str | bytes, _Pointer[c_uint32]], None] = (
_func(
'openslide_read_associated_image',
None,
[_OpenSlide, _utf8_p, POINTER(c_uint32)],
)
)


@_wraps_funcs([get_associated_image_dimensions, _read_associated_image])
def read_associated_image(slide: _OpenSlide, name: str) -> Image.Image:
def read_associated_image(slide: _OpenSlide, name: str | bytes) -> Image.Image:
w, h = get_associated_image_dimensions(slide, name)
buf = (w * h * c_uint32)()
_read_associated_image(slide, name, buf)
return _load_image(buf, (w, h))


get_associated_image_icc_profile_size: _Func[[_OpenSlide, str], int] = _func(
get_associated_image_icc_profile_size: _Func[[_OpenSlide, str | bytes], int] = _func(
'openslide_get_associated_image_icc_profile_size',
c_int64,
[_OpenSlide, _utf8_p],
minimum_version='4.0.0',
)

_read_associated_image_icc_profile: _Func[[_OpenSlide, str, _Pointer[c_char]], None] = (
_func(
'openslide_read_associated_image_icc_profile',
None,
[_OpenSlide, _utf8_p, POINTER(c_char)],
minimum_version='4.0.0',
)
_read_associated_image_icc_profile: _Func[
[_OpenSlide, str | bytes, _Pointer[c_char]], None
] = _func(
'openslide_read_associated_image_icc_profile',
None,
[_OpenSlide, _utf8_p, POINTER(c_char)],
minimum_version='4.0.0',
)


@_wraps_funcs(
[get_associated_image_icc_profile_size, _read_associated_image_icc_profile]
)
def read_associated_image_icc_profile(slide: _OpenSlide, name: str) -> bytes | None:
def read_associated_image_icc_profile(
slide: _OpenSlide, name: str | bytes
) -> bytes | None:
size = get_associated_image_icc_profile_size(slide, name)
if size == 0:
return None
Expand Down

0 comments on commit 3dce923

Please sign in to comment.