Skip to content

Commit 5ef61b7

Browse files
committed
update
1 parent aa25401 commit 5ef61b7

1 file changed

Lines changed: 18 additions & 9 deletions

File tree

Mailman/Mailbox.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,27 +45,36 @@ class BinaryGenerator(email.generator.Generator):
4545
"""A generator that writes to a binary file."""
4646
def __init__(self, outfp, mangle_from_=True, maxheaderlen=78, *args, **kwargs):
4747
# Create a text buffer that we'll write to first
48-
self._buffer = BytesIO()
48+
self._buffer = StringIO()
4949
super().__init__(self._buffer, mangle_from_, maxheaderlen, *args, **kwargs)
5050
# Store the binary file object
5151
self._binary_fp = outfp
5252

53+
def write(self, s):
54+
"""Override write to handle string-to-bytes conversion."""
55+
if isinstance(s, str):
56+
s = s.encode('utf-8', 'replace')
57+
self._buffer.write(s)
58+
5359
def _write_lines(self, lines):
54-
# Override to handle both string and bytes input
60+
"""Override to handle both string and bytes input."""
5561
for line in lines:
56-
if isinstance(line, str):
57-
line = line.encode('utf-8', 'replace')
62+
if isinstance(line, bytes):
63+
try:
64+
line = line.decode('utf-8', 'replace')
65+
except UnicodeError:
66+
line = line.decode('latin-1', 'replace')
5867
self._buffer.write(line)
5968

6069
def flatten(self, msg, unixfrom=False, linesep='\n'):
61-
# Override to write the buffer contents to binary file
62-
super().flatten(msg, unixfrom=unixfrom, linesep=linesep)
63-
# Get the content and write it directly to binary file
64-
content = self._buffer.getvalue()
70+
"""Override to write the buffer contents to binary file."""
6571
# Reset the buffer
6672
self._buffer.seek(0)
6773
self._buffer.truncate()
68-
# Write to binary file
74+
# Write the message to the buffer
75+
super().flatten(msg, unixfrom=unixfrom, linesep=linesep)
76+
# Get the content and write it to binary file
77+
content = self._buffer.getvalue().encode('utf-8', 'replace')
6978
self._binary_fp.write(content)
7079

7180

0 commit comments

Comments
 (0)