Skip to content

Commit 2eee8ba

Browse files
committed
lowlevel: support optional OpenSlide functions
If a minimum OpenSlide version is specified to _func(), and the requested function doesn't exist, succeed at initialization time but raise an exception if the function is called.
1 parent 8a0b4e6 commit 2eee8ba

1 file changed

Lines changed: 13 additions & 3 deletions

File tree

openslide/lowlevel.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# openslide-python - Python bindings for the OpenSlide library
33
#
44
# Copyright (c) 2010-2013 Carnegie Mellon University
5-
# Copyright (c) 2016 Benjamin Gilbert
5+
# Copyright (c) 2016-2021 Benjamin Gilbert
66
#
77
# This library is free software; you can redistribute it and/or modify it
88
# under the terms of version 2.1 of the GNU Lesser General Public License
@@ -186,8 +186,18 @@ def _check_name_list(result, func, args):
186186

187187

188188
# resolve and return an OpenSlide function with the specified properties
189-
def _func(name, restype, argtypes, errcheck=_check_error):
190-
func = getattr(_lib, name)
189+
def _func(name, restype, argtypes, errcheck=_check_error, minimum_version=None):
190+
try:
191+
func = getattr(_lib, name)
192+
except AttributeError:
193+
if minimum_version is None:
194+
raise
195+
196+
# optional function doesn't exist; fail at runtime
197+
def function_unavailable(*_args):
198+
raise OpenSlideVersionError(minimum_version)
199+
200+
return function_unavailable
191201
func.argtypes = argtypes
192202
func.restype = restype
193203
if errcheck is not None:

0 commit comments

Comments
 (0)