From dbcb63c290962eab1830ab77756ff8c5440d3319 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 15 Mar 2019 21:45:54 +0530 Subject: [PATCH] Ensure worker env keys are bytes/unicodeon py2/3 --- src/calibre/utils/ipc/server.py | 9 +++++---- src/calibre/utils/ipc/simple_worker.py | 8 ++++---- src/polyglot/builtins.py | 9 +++++++++ 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/calibre/utils/ipc/server.py b/src/calibre/utils/ipc/server.py index f96d9d11ed..58269ce4a5 100644 --- a/src/calibre/utils/ipc/server.py +++ b/src/calibre/utils/ipc/server.py @@ -27,7 +27,7 @@ from calibre.utils.ipc import eintr_retry_call from calibre.utils.ipc.launch import Worker from calibre.utils.ipc.worker import PARALLEL_FUNCS from calibre.utils.serialize import msgpack_dumps, pickle_loads -from polyglot.builtins import string_or_bytes +from polyglot.builtins import string_or_bytes, environ_item _counter = 0 @@ -219,9 +219,10 @@ class Server(Thread): redirect_output = not gui env = { - 'CALIBRE_WORKER_ADDRESS' : hexlify(msgpack_dumps(self.listener.address)), - 'CALIBRE_WORKER_KEY' : hexlify(self.auth_key), - 'CALIBRE_WORKER_RESULT' : hexlify(rfile.encode('utf-8')), + 'CALIBRE_WORKER_ADDRESS' : environ_item(hexlify(msgpack_dumps( + self.listener.address))), + 'CALIBRE_WORKER_KEY' : environ_item(hexlify(self.auth_key)), + 'CALIBRE_WORKER_RESULT' : environ_item(hexlify(rfile.encode('utf-8'))), } cw = self.do_launch(env, gui, redirect_output, rfile, job_name=job_name) if isinstance(cw, string_or_bytes): diff --git a/src/calibre/utils/ipc/simple_worker.py b/src/calibre/utils/ipc/simple_worker.py index 880ba69665..1a8b91ab4b 100644 --- a/src/calibre/utils/ipc/simple_worker.py +++ b/src/calibre/utils/ipc/simple_worker.py @@ -17,7 +17,7 @@ from calibre.constants import iswindows from calibre.utils.ipc import eintr_retry_call from calibre.utils.ipc.launch import Worker from calibre.utils.serialize import msgpack_loads, msgpack_dumps -from polyglot.builtins import unicode_type, string_or_bytes +from polyglot.builtins import unicode_type, string_or_bytes, environ_item class WorkerError(Exception): @@ -131,9 +131,9 @@ def create_worker(env, priority='normal', cwd=None, func='main'): env = dict(env) env.update({ - 'CALIBRE_WORKER_ADDRESS': hexlify(msgpack_dumps(listener.address)), - 'CALIBRE_WORKER_KEY': hexlify(auth_key), - 'CALIBRE_SIMPLE_WORKER': 'calibre.utils.ipc.simple_worker:%s' % func, + 'CALIBRE_WORKER_ADDRESS': environ_item(hexlify(msgpack_dumps(listener.address))), + 'CALIBRE_WORKER_KEY': environ_item(hexlify(auth_key)), + 'CALIBRE_SIMPLE_WORKER': environ_item('calibre.utils.ipc.simple_worker:%s' % func), }) w = Worker(env) diff --git a/src/polyglot/builtins.py b/src/polyglot/builtins.py index 806807f43c..5209ef698a 100644 --- a/src/polyglot/builtins.py +++ b/src/polyglot/builtins.py @@ -40,6 +40,10 @@ if is_py3: def iterkeys(d): return iter(d) + def environ_item(x): + if isinstance(x, bytes): + x = x.decode('utf-8') + return x else: exec("""def reraise(tp, value, tb=None): try: @@ -64,3 +68,8 @@ else: def itervalues(d): return d.itervalues() + + def environ_item(x): + if isinstance(x, unicode_type): + x = x.encode('utf-8') + return x