Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for pathlib.Path instances #123

Merged
merged 2 commits into from
Jun 13, 2021
Merged

Conversation

kaczmarj
Copy link
Contributor

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.

`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 Outdated Show resolved Hide resolved
@jaharkes
Copy link
Member

jaharkes commented Jun 3, 2021 via email

@kaczmarj
Copy link
Contributor Author

kaczmarj commented Jun 4, 2021

Thank you for making those changes @jaharkes. This looks good to go on my end.

Copy link
Member

@jaharkes jaharkes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Member

@bgilbert bgilbert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix!

@bgilbert bgilbert merged commit 98c11bd into openslide:main Jun 13, 2021
bgilbert added a commit to bgilbert/openslide-python that referenced this pull request Oct 20, 2024
- Starting in 1.2.0, OpenSlide() and OpenSlide.detect_format() have failed
  to accept filename arguments formatted as pre-encoded bytes because
  str(b'abc') == "b'abc'".  In addition, filename arguments with invalid
  types (such as None) have been stringified and passed to OpenSlide,
  rather than raising an exception during conversion; we even had tests
  for this (!).

- lowlevel has always encoded filename arguments to UTF-8, but on
  non-Windows it should have used the Python filesystem encoding instead
  (usually UTF-8 but not always).  On Windows, OpenSlide 4.0.0+ expects
  UTF-8 rather than arbitrary bytes.  (OpenSlide < 4.0.0 expects the system
  codepage, which isn't very useful in practice because of its limited
  character set, so we ignore that case for now.)

- Type hints did not allow filename arguments to be bytes, nor did they
  allow os.PathLike subclasses which were not pathlib.Path (such as
  pathlib.PurePath).

Accept str, bytes, or os.PathLike for all filename arguments, and properly
convert them to bytes for OpenSlide.

Fixes: 98c11bd ("Add support for pathlib.Path instances (openslide#123)")
Fixes: 5644229 ("tests: test passing invalid types to OpenSlide constructor")
bgilbert added a commit to bgilbert/openslide-python that referenced this pull request Oct 20, 2024
- Starting in 1.2.0, OpenSlide() and OpenSlide.detect_format() have failed
  to accept filename arguments formatted as bytes because str(b'abc') ==
  "b'abc'".  In addition, filename arguments with invalid types (such as
  None) have been stringified and passed to OpenSlide, rather than raising
  an exception during conversion; we even had tests for this (!).

- lowlevel has always encoded filename arguments to UTF-8, but on
  non-Windows it should have used the Python filesystem encoding instead
  (usually UTF-8 but not always).  On Windows, OpenSlide 4.0.0+ expects
  UTF-8 rather than arbitrary bytes.  (OpenSlide < 4.0.0 expects the system
  codepage, which isn't very useful in practice because of its limited
  character set, so we ignore that case for now.)

- Type hints did not allow filename arguments to be bytes, nor did they
  allow os.PathLike subclasses which were not pathlib.Path (such as
  pathlib.PurePath).

Accept str, bytes, or os.PathLike for all filename arguments, and properly
convert them to bytes for OpenSlide.

Fixes: 98c11bd ("Add support for pathlib.Path instances (openslide#123)")
Fixes: 5644229 ("tests: test passing invalid types to OpenSlide constructor")
bgilbert added a commit to bgilbert/openslide-python that referenced this pull request Oct 20, 2024
- Starting in 1.2.0, OpenSlide() and OpenSlide.detect_format() have failed
  to accept filename arguments formatted as bytes because str(b'abc') ==
  "b'abc'".  In addition, filename arguments with invalid types (such as
  None) have been stringified and passed to OpenSlide, rather than raising
  an exception during conversion; we even had tests for this (!).

- lowlevel has always encoded filename arguments to UTF-8, but on
  non-Windows it should have used the Python filesystem encoding instead
  (usually UTF-8 but not always).  On Windows, OpenSlide 4.0.0+ expects
  UTF-8 rather than arbitrary bytes.  (OpenSlide < 4.0.0 expects the system
  codepage, which isn't very useful in practice because of its limited
  character set, so we ignore that case for now.)

- Type hints did not allow filename arguments to be bytes, nor did they
  allow os.PathLike subclasses which were not pathlib.Path (such as
  pathlib.PurePath).

Accept str, bytes, or os.PathLike for all filename arguments, and properly
convert them to bytes for OpenSlide.

Fixes: 98c11bd ("Add support for pathlib.Path instances (openslide#123)")
Fixes: 5644229 ("tests: test passing invalid types to OpenSlide constructor")
Signed-off-by: Benjamin Gilbert <[email protected]>
bgilbert added a commit to bgilbert/openslide-python that referenced this pull request Oct 20, 2024
- Starting in 1.2.0, OpenSlide() and OpenSlide.detect_format() have failed
  to accept filename arguments formatted as bytes because str(b'abc') ==
  "b'abc'".  In addition, filename arguments with invalid types (such as
  None) have been stringified and passed to OpenSlide, rather than raising
  an exception during conversion; we even had tests for this (!).

- lowlevel has always encoded filename arguments to UTF-8, but on
  non-Windows it should have used the Python filesystem encoding instead
  (usually UTF-8 but not always).  On Windows, OpenSlide 4.0.0+ expects
  UTF-8 rather than arbitrary bytes.  (OpenSlide < 4.0.0 expects the system
  codepage, which isn't very useful in practice because of its limited
  character set, so we ignore that case for now.)

- Type hints did not allow filename arguments to be bytes, nor did they
  allow os.PathLike subclasses which were not pathlib.Path (such as
  pathlib.PurePath).

Accept str, bytes, or os.PathLike for all filename arguments, and properly
convert them to bytes for OpenSlide.

Fixes: 98c11bd ("Add support for pathlib.Path instances (openslide#123)")
Fixes: 5644229 ("tests: test passing invalid types to OpenSlide constructor")
Signed-off-by: Benjamin Gilbert <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Unclear error message when using pathlib.Path instance as open_slide argument
3 participants