From 7f4728e29cf5f959de6b977116a46c24059621de Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 10 Apr 2019 15:38:29 +0530 Subject: [PATCH] py3: More IPC fixes --- src/calibre/utils/ipc/launch.py | 37 ++++++++++++++------------ src/calibre/utils/ipc/server.py | 3 +-- src/calibre/utils/ipc/simple_worker.py | 2 +- 3 files changed, 22 insertions(+), 20 deletions(-) diff --git a/src/calibre/utils/ipc/launch.py b/src/calibre/utils/ipc/launch.py index 63743a7062..5e51ac9717 100644 --- a/src/calibre/utils/ipc/launch.py +++ b/src/calibre/utils/ipc/launch.py @@ -13,7 +13,7 @@ from calibre.constants import iswindows, isosx, isfrozen, filesystem_encoding, i from calibre.utils.config import prefs from calibre.ptempfile import PersistentTemporaryFile, base_dir from calibre.utils.serialize import msgpack_dumps -from polyglot.builtins import iteritems, unicode_type, string_or_bytes, environ_item +from polyglot.builtins import iteritems, unicode_type, string_or_bytes, environ_item, native_string_type from polyglot.binary import as_hex_unicode if iswindows: @@ -111,9 +111,9 @@ class Worker(object): env[key] = val except: pass - env[str('CALIBRE_WORKER')] = environ_item('1') + env[native_string_type('CALIBRE_WORKER')] = environ_item('1') td = as_hex_unicode(msgpack_dumps(base_dir())) - env[str('CALIBRE_WORKER_TEMP_DIR')] = environ_item(td) + env[native_string_type('CALIBRE_WORKER_TEMP_DIR')] = environ_item(td) env.update(self._env) return env @@ -161,19 +161,22 @@ class Worker(object): self._env = {} self.gui = gui self.job_name = job_name - # Windows cannot handle unicode env vars - for k, v in iteritems(env): - try: - if isinstance(k, unicode_type): - k = k.encode('ascii') - if isinstance(v, unicode_type): - try: - v = v.encode(filesystem_encoding) - except: - v = v.encode('utf-8') - self._env[k] = v - except: - pass + if ispy3: + self._env = env.copy() + else: + # Windows cannot handle unicode env vars + for k, v in iteritems(env): + try: + if isinstance(k, unicode_type): + k = k.encode('ascii') + if isinstance(v, unicode_type): + try: + v = v.encode(filesystem_encoding) + except: + v = v.encode('utf-8') + self._env[k] = v + except: + pass def __call__(self, redirect_output=True, cwd=None, priority=None): ''' @@ -187,7 +190,7 @@ class Worker(object): except EnvironmentError: # cwd no longer exists origwd = cwd or os.path.expanduser(u'~') - env[str('ORIGWD')] = environ_item(as_hex_unicode(msgpack_dumps(origwd))) + env[native_string_type('ORIGWD')] = environ_item(as_hex_unicode(msgpack_dumps(origwd))) _cwd = cwd if priority is None: priority = prefs['worker_process_priority'] diff --git a/src/calibre/utils/ipc/server.py b/src/calibre/utils/ipc/server.py index 743bb7cf3c..bc6d80b1ff 100644 --- a/src/calibre/utils/ipc/server.py +++ b/src/calibre/utils/ipc/server.py @@ -221,8 +221,7 @@ class Server(Thread): redirect_output = not gui env = { - 'CALIBRE_WORKER_ADDRESS' : environ_item(as_hex_unicode(msgpack_dumps( - self.listener.address))), + 'CALIBRE_WORKER_ADDRESS' : environ_item(as_hex_unicode(msgpack_dumps(self.address))), 'CALIBRE_WORKER_KEY' : environ_item(as_hex_unicode(self.auth_key)), 'CALIBRE_WORKER_RESULT' : environ_item(as_hex_unicode(rfile)), } diff --git a/src/calibre/utils/ipc/simple_worker.py b/src/calibre/utils/ipc/simple_worker.py index 26e4173653..3ae7a5e53e 100644 --- a/src/calibre/utils/ipc/simple_worker.py +++ b/src/calibre/utils/ipc/simple_worker.py @@ -131,7 +131,7 @@ def create_worker(env, priority='normal', cwd=None, func='main'): env = dict(env) env.update({ - 'CALIBRE_WORKER_ADDRESS': environ_item(as_hex_unicode(msgpack_dumps(listener.address))), + 'CALIBRE_WORKER_ADDRESS': environ_item(as_hex_unicode(msgpack_dumps(address))), 'CALIBRE_WORKER_KEY': environ_item(as_hex_unicode(auth_key)), 'CALIBRE_SIMPLE_WORKER': environ_item('calibre.utils.ipc.simple_worker:%s' % func), })