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

Handle pathlib.Path in FreeTypeFont #7578

Merged
merged 8 commits into from Dec 6, 2023
Merged

Conversation

radarhere
Copy link
Member

Resolves #7577

@@ -213,6 +214,8 @@ def load_from_bytes(f):
)

if is_path(font):
Copy link
Member

Choose a reason for hiding this comment

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

I'm looking at _util.is_path, it checks isinstance(f, (bytes, str, Path)), so bytes are also handled as paths, but next I see: else: load_from_bytes(font).

Do we really want consider bytes as paths? I believe this is obsolete in Python3

Copy link
Member Author

@radarhere radarhere Nov 28, 2023

Choose a reason for hiding this comment

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

#258 was the original request for bytes as paths.

Copy link
Member

Choose a reason for hiding this comment

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

I assume load_from_bytes is unreachable here, so we need to remove it, or provide a way to reach it

Copy link
Member Author

Choose a reason for hiding this comment

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

I think it's reachable. The logic hasn't changed since load_from_bytes() was added in #3785.

If it is a path, then pass the path to the C layer. If we're on Windows and the path is not ASCII, then open a file pointer to the path and read the contents of the file pointer with load_from_bytes().
If it is not a path, then it is a file pointer, read the contents of the file pointer with load_from_bytes().

Copy link
Member

Choose a reason for hiding this comment

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

Ok, I see. I believe there is misnaming here, since f is not a bytes, it's file object.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh right, I remember figuring that out after being confused in the past.
Should typing.BinaryIO be added to the type annotation then?

Copy link
Member Author

@radarhere radarhere Dec 4, 2023

Choose a reason for hiding this comment

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

Yep, I've pushed a commit. Testing in PyCharm, I find it also includes io.BytesIO

Copy link
Contributor

@nulano nulano Dec 27, 2023

Choose a reason for hiding this comment

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

@radarhere I see you've used typing.IO. This causes a mypy error, see #7642.

Testing with both PyCharm and mypy, I find that a io.BytesIO value is accepted for a typing.BinaryIO type hint (which seems correct here), so I'm not sure I understand your last message.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think I was concerned about strings being used, but looking now, that doesn't appear to be a problem.

src/PIL/ImageFont.py Outdated Show resolved Hide resolved
Co-authored-by: Hugo van Kemenade <hugovk@users.noreply.github.com>
src/PIL/ImageFont.py Outdated Show resolved Hide resolved
radarhere and others added 2 commits December 1, 2023 08:42
Co-authored-by: Ondrej Baranovič <nulano@nulano.eu>
@radarhere radarhere merged commit e43dd66 into python-pillow:main Dec 6, 2023
57 checks passed
@radarhere radarhere deleted the font branch December 6, 2023 01:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ImageFont.truetype() function does not accept Path objects
4 participants