From 2b5bb5cdad387f2c53ca3117e53ad75c0cdd4866 Mon Sep 17 00:00:00 2001 From: Jakub Kaczmarzyk Date: Mon, 24 May 2021 14:06:37 -0400 Subject: [PATCH 1/2] Add support for pathlib.Path instances `pathlib.Path` instances are commonly used, but the `openslide.OpenSlide` class was not compatible with the `pathlib.Path` type. This commit converts the `slide._filename` attribute to a `str` type in `__init__`. This means that the `openslide.OpenSlide` class can now accept a `pathlib.Path` instance. --- openslide/__init__.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/openslide/__init__.py b/openslide/__init__.py index a0567e0e..39a611db 100644 --- a/openslide/__init__.py +++ b/openslide/__init__.py @@ -149,7 +149,9 @@ class OpenSlide(AbstractSlide): def __init__(self, filename): """Open a whole-slide image.""" AbstractSlide.__init__(self) - self._filename = filename + # Filename might be a pathlib.Path instance, but file reader expects + # a string. + self._filename = str(filename) self._osr = lowlevel.open(filename) def __repr__(self): From af3be9987bc0e868eee2a5d433e0873160cf8926 Mon Sep 17 00:00:00 2001 From: Jan Harkes Date: Thu, 3 Jun 2021 11:44:06 -0400 Subject: [PATCH 2/2] Update fix for supporting pathlib.Path instances --- openslide/__init__.py | 8 +++----- tests/__init__.py | 4 +++- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/openslide/__init__.py b/openslide/__init__.py index 39a611db..4bd4f51d 100644 --- a/openslide/__init__.py +++ b/openslide/__init__.py @@ -149,10 +149,8 @@ class OpenSlide(AbstractSlide): def __init__(self, filename): """Open a whole-slide image.""" AbstractSlide.__init__(self) - # Filename might be a pathlib.Path instance, but file reader expects - # a string. - self._filename = str(filename) - self._osr = lowlevel.open(filename) + self._filename = filename + self._osr = lowlevel.open(str(filename)) def __repr__(self): return '%s(%r)' % (self.__class__.__name__, self._filename) @@ -162,7 +160,7 @@ def detect_format(cls, filename): """Return a string describing the format vendor of the specified file. If the file format is not recognized, return None.""" - return lowlevel.detect_vendor(filename) + return lowlevel.detect_vendor(str(filename)) def close(self): """Close the OpenSlide object.""" diff --git a/tests/__init__.py b/tests/__init__.py index 195b9a08..8560df63 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -19,6 +19,8 @@ from functools import wraps import os +from pathlib import Path + from PIL import Image import unittest @@ -52,4 +54,4 @@ def file_path(name): - return os.path.join(os.path.dirname(__file__), name) + return Path(__file__).parent / name