From d69e30851a23c6baf28dae938c84868ecec8a67d Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Sat, 6 Dec 2025 18:43:49 +0000 Subject: [PATCH] dnsbl: Allow handling binary file descriptors on export Signed-off-by: Michael Tremer --- src/dnsbl/lists.py | 13 +++++++++++++ src/scripts/dnsbl.in | 2 +- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/dnsbl/lists.py b/src/dnsbl/lists.py index 4caa246..2037f96 100644 --- a/src/dnsbl/lists.py +++ b/src/dnsbl/lists.py @@ -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() diff --git a/src/scripts/dnsbl.in b/src/scripts/dnsbl.in index 0e127df..c628a59 100644 --- a/src/scripts/dnsbl.in +++ b/src/scripts/dnsbl.in @@ -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) -- 2.47.3