Handle pathlib.Path in FreeTypeFont#7578
Conversation
| @@ -213,6 +214,8 @@ def load_from_bytes(f): | |||
| ) | |||
|
|
|||
| if is_path(font): | |||
There was a problem hiding this comment.
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
There was a problem hiding this comment.
#258 was the original request for bytes as paths.
There was a problem hiding this comment.
I assume load_from_bytes is unreachable here, so we need to remove it, or provide a way to reach it
There was a problem hiding this comment.
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().
There was a problem hiding this comment.
Ok, I see. I believe there is misnaming here, since f is not a bytes, it's file object.
There was a problem hiding this comment.
Oh right, I remember figuring that out after being confused in the past.
Should typing.BinaryIO be added to the type annotation then?
There was a problem hiding this comment.
Yep, I've pushed a commit. Testing in PyCharm, I find it also includes io.BytesIO
There was a problem hiding this comment.
@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.
There was a problem hiding this comment.
I think I was concerned about strings being used, but looking now, that doesn't appear to be a problem.
Co-authored-by: Hugo van Kemenade <[email protected]>
Co-authored-by: Ondrej Baranovič <[email protected]>
Fix documentation link to PIL.ImageFont.Layout
Resolves #7577