From faa5c29f135ca8b70696b0a225542e0e7bd94416 Mon Sep 17 00:00:00 2001 From: Andrej Shadura Date: Sun, 30 Jan 2022 17:57:14 +0100 Subject: 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 --- gi/_gtktemplate.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'gi') 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) -- cgit v1.2.1