diff options
author | Andrej Shadura <andrew.shadura@collabora.co.uk> | 2022-01-30 17:57:14 +0100 |
---|---|---|
committer | Andrej Shadura <andrew.shadura@collabora.co.uk> | 2022-03-27 10:41:25 +0200 |
commit | faa5c29f135ca8b70696b0a225542e0e7bd94416 (patch) | |
tree | 1b70a0d788c8c03e050af0b818ee3b515e0dbd0b | |
parent | ad2e90f62d930433e7a28f08edd99ef85579393f (diff) | |
download | pygobject-faa5c29f135ca8b70696b0a225542e0e7bd94416.tar.gz |
Gtk.Template: Accept PathLike objects as a filename
An attempt to pass a Path object as a filename results in a TypeError:
"Must be bytes, not PosixPath". A simple way to accept Path objects
would be unconditionally turning them into strings, but since native
paths may be bytes as well, this might break applications relying on
being able to bytes-based filename. Instead, convert the path into an
appropriate representation (string or bytes) using os.fspath(). It
internally tests whether the path is a PathLike object and does the
right thing.
Signed-off-by: Andrej Shadura <andrew.shadura@collabora.co.uk>
-rw-r--r-- | gi/_gtktemplate.py | 3 |
1 files changed, 2 insertions, 1 deletions
diff --git a/gi/_gtktemplate.py b/gi/_gtktemplate.py index 925aa0dd..f253887e 100644 --- a/gi/_gtktemplate.py +++ b/gi/_gtktemplate.py @@ -17,6 +17,7 @@ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 # USA +import os from collections import abc from functools import partial @@ -296,7 +297,7 @@ class Template(object): return cls else: assert self.filename is not None - file_ = Gio.File.new_for_path(self.filename) + file_ = Gio.File.new_for_path(os.fspath(self.filename)) bytes_ = GLib.Bytes.new(file_.load_contents()[1]) cls.set_template(bytes_) register_template(cls) |