Ensure worker env keys are bytes/unicodeon py2/3

This commit is contained in:
Kovid Goyal 2019-03-15 21:45:54 +05:30
parent e73e782b56
commit dbcb63c290
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 18 additions and 8 deletions

View File

@ -27,7 +27,7 @@ from calibre.utils.ipc import eintr_retry_call
from calibre.utils.ipc.launch import Worker from calibre.utils.ipc.launch import Worker
from calibre.utils.ipc.worker import PARALLEL_FUNCS from calibre.utils.ipc.worker import PARALLEL_FUNCS
from calibre.utils.serialize import msgpack_dumps, pickle_loads 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 _counter = 0
@ -219,9 +219,10 @@ class Server(Thread):
redirect_output = not gui redirect_output = not gui
env = { env = {
'CALIBRE_WORKER_ADDRESS' : hexlify(msgpack_dumps(self.listener.address)), 'CALIBRE_WORKER_ADDRESS' : environ_item(hexlify(msgpack_dumps(
'CALIBRE_WORKER_KEY' : hexlify(self.auth_key), self.listener.address))),
'CALIBRE_WORKER_RESULT' : hexlify(rfile.encode('utf-8')), '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) cw = self.do_launch(env, gui, redirect_output, rfile, job_name=job_name)
if isinstance(cw, string_or_bytes): if isinstance(cw, string_or_bytes):

View File

@ -17,7 +17,7 @@ from calibre.constants import iswindows
from calibre.utils.ipc import eintr_retry_call from calibre.utils.ipc import eintr_retry_call
from calibre.utils.ipc.launch import Worker from calibre.utils.ipc.launch import Worker
from calibre.utils.serialize import msgpack_loads, msgpack_dumps 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): class WorkerError(Exception):
@ -131,9 +131,9 @@ def create_worker(env, priority='normal', cwd=None, func='main'):
env = dict(env) env = dict(env)
env.update({ env.update({
'CALIBRE_WORKER_ADDRESS': hexlify(msgpack_dumps(listener.address)), 'CALIBRE_WORKER_ADDRESS': environ_item(hexlify(msgpack_dumps(listener.address))),
'CALIBRE_WORKER_KEY': hexlify(auth_key), 'CALIBRE_WORKER_KEY': environ_item(hexlify(auth_key)),
'CALIBRE_SIMPLE_WORKER': 'calibre.utils.ipc.simple_worker:%s' % func, 'CALIBRE_SIMPLE_WORKER': environ_item('calibre.utils.ipc.simple_worker:%s' % func),
}) })
w = Worker(env) w = Worker(env)

View File

@ -40,6 +40,10 @@ if is_py3:
def iterkeys(d): def iterkeys(d):
return iter(d) return iter(d)
def environ_item(x):
if isinstance(x, bytes):
x = x.decode('utf-8')
return x
else: else:
exec("""def reraise(tp, value, tb=None): exec("""def reraise(tp, value, tb=None):
try: try:
@ -64,3 +68,8 @@ else:
def itervalues(d): def itervalues(d):
return d.itervalues() return d.itervalues()
def environ_item(x):
if isinstance(x, unicode_type):
x = x.encode('utf-8')
return x