]> git.ipfire.org Git - dbl.git/commitdiff
dnsbl: Allow handling binary file descriptors on export
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 6 Dec 2025 18:43:49 +0000 (18:43 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 6 Dec 2025 18:43:49 +0000 (18:43 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/dnsbl/lists.py
src/scripts/dnsbl.in

index 4caa24689f67e2e758179fc35d3e3dc59a1182e9..2037f960ac84c4151513fca9dcf84fddbe9fe2c1 100644 (file)
@@ -20,6 +20,7 @@
 
 import datetime
 import functools
+import io
 import logging
 import sqlmodel
 import typing
@@ -209,6 +210,13 @@ class List(sqlmodel.SQLModel, database.BackendMixin, table=True):
                """
                        Exports the list
                """
+               detach = False
+
+               # Convert any file handles to handle plain text
+               if not isinstance(f, io.TextIOBase):
+                       f = io.TextIOWrapper(f, encoding="utf-8")
+                       detach = True
+
                # Write the domains as they are
                if format == "domains":
                        for domain in self.domains:
@@ -237,3 +245,8 @@ class List(sqlmodel.SQLModel, database.BackendMixin, table=True):
 
                else:
                        raise ValueError("Unknown output format: %s" % format)
+
+               # Detach the underlying stream. That way, the wrapper won't close
+               # the underlying file handle.
+               if detach:
+                       f.detach()
index 0e127dfe2d121fcc6f4c9ad0fc51b4005cec6fc7..c628a59ca27bbe82fa4b32c50504bab233535f21 100644 (file)
@@ -287,7 +287,7 @@ class CLI(object):
                                name = os.path.join(args.directory, filename % list.slug)
 
                                # Create a new temporary file
-                               with tempfile.NamedTemporaryFile(mode="w", dir=args.directory) as f:
+                               with tempfile.NamedTemporaryFile(dir=args.directory) as f:
                                        list.export(f, format=format)
 
                                        # Remove the previous file (if it exists)