Skip to content

Commit

Permalink
Add support for pathlib.Path instances (#123)
Browse files Browse the repository at this point in the history
`pathlib.Path` instances are commonly used, but the `openslide.OpenSlide`
class was not compatible with the `pathlib.Path` type.  Have `OpenSlide`
stringify paths before passing them to `lowlevel` functions, and update
tests to check this case.

Co-authored-by: Jan Harkes <[email protected]>
  • Loading branch information
kaczmarj and jaharkes authored Jun 13, 2021
1 parent 57cca2d commit 98c11bd
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
4 changes: 2 additions & 2 deletions openslide/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def __init__(self, filename):
"""Open a whole-slide image."""
AbstractSlide.__init__(self)
self._filename = filename
self._osr = lowlevel.open(filename)
self._osr = lowlevel.open(str(filename))

def __repr__(self):
return '%s(%r)' % (self.__class__.__name__, self._filename)
Expand All @@ -160,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."""
Expand Down
4 changes: 3 additions & 1 deletion tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

from functools import wraps
import os
from pathlib import Path

from PIL import Image
import unittest

Expand Down Expand Up @@ -52,4 +54,4 @@


def file_path(name):
return os.path.join(os.path.dirname(__file__), name)
return Path(__file__).parent / name

0 comments on commit 98c11bd

Please sign in to comment.