Skip to content

Commit 013a325

Browse files
committed
lowlevel: move unavailable-func handler from closure to callable object
This seems slightly cleaner, especially once typechecking is added. Signed-off-by: Benjamin Gilbert <[email protected]>
1 parent 3e21feb commit 013a325

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

openslide/lowlevel.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -265,22 +265,27 @@ def _check_name_list(result, func, args):
265265
return names
266266

267267

268+
class _FunctionUnavailable:
269+
'''Standin for a missing optional function. Fails when called.'''
270+
271+
def __init__(self, minimum_version):
272+
self._minimum_version = minimum_version
273+
# allow checking for availability without calling the function
274+
self.available = False
275+
276+
def __call__(self, *_args):
277+
raise OpenSlideVersionError(self._minimum_version)
278+
279+
268280
# resolve and return an OpenSlide function with the specified properties
269281
def _func(name, restype, argtypes, errcheck=_check_error, minimum_version=None):
270282
try:
271283
func = getattr(_lib, name)
272284
except AttributeError:
273285
if minimum_version is None:
274286
raise
275-
276-
# optional function doesn't exist; fail at runtime
277-
def function_unavailable(*_args):
278-
raise OpenSlideVersionError(minimum_version)
279-
280-
# allow checking for availability without calling the function
281-
function_unavailable.available = False
282-
283-
return function_unavailable
287+
else:
288+
return _FunctionUnavailable(minimum_version)
284289
func.argtypes = argtypes
285290
func.restype = restype
286291
if errcheck is not None:

0 commit comments

Comments
 (0)