File pyzor-67b471dd168db9468548aef3ffadca9554164ac0.patch of Package pyzor
From 67b471dd168db9468548aef3ffadca9554164ac0 Mon Sep 17 00:00:00 2001
From: AVPS <andreivplesa@gmail.com>
Date: Tue, 19 Jan 2016 15:35:48 +0200
Subject: [PATCH] Refs #38 - The temporary file is first opened in binary form
and then read, thus avoiding encoding issues.
---
scripts/pyzor | 23 ++++++++++++++++++++++-
1 file changed, 22 insertions(+), 1 deletion(-)
diff --git a/scripts/pyzor b/scripts/pyzor
index d739ada..567a7f9 100755
--- a/scripts/pyzor
+++ b/scripts/pyzor
@@ -176,9 +176,30 @@ def _get_input_msg(digester):
yield digested
+def _is_binary_reader(stream, default=False):
+ try:
+ return isinstance(stream.read(0), bytes)
+ except Exception:
+ return default
+
+
+def get_binary_stdin():
+ # sys.stdin might or might not be binary in some extra cases. By
+ # default it's obviously non binary which is the core of the
+ # problem but the docs recommend changing it to binary for such
+ # cases so we need to deal with it.
+ is_binary = _is_binary_reader(sys.stdin, False)
+ if is_binary:
+ return sys.stdin
+ buf = getattr(sys.stdin, 'buffer', None)
+ if buf is not None and _is_binary_reader(buf, True):
+ return buf
+ raise RuntimeError('Did not manage to get binary stdin')
+
+
def _get_input_mbox(digester):
tfile = tempfile.NamedTemporaryFile()
- tfile.write(sys.stdin.read().encode("utf8"))
+ tfile.write(get_binary_stdin().read())
tfile.seek(0)
mbox = mailbox.mbox(tfile.name)
for msg in mbox: