[pygobject] Gtk.Template: Accept PathLike objects as a filename



commit faa5c29f135ca8b70696b0a225542e0e7bd94416
Author: Andrej Shadura <andrew shadura collabora co uk>
Date:   Sun Jan 30 17:57:14 2022 +0100

    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>

 gi/_gtktemplate.py | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
---
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)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]