Replace use of pickle for the editor completion worker

This commit is contained in:
Kovid Goyal 2019-03-15 11:59:46 +05:30
parent fc61fc88bc
commit 1e6d9e9583
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -6,7 +6,7 @@ from __future__ import (unicode_literals, division, absolute_import,
__license__ = 'GPL v3' __license__ = 'GPL v3'
__copyright__ = '2014, Kovid Goyal <kovid at kovidgoyal.net>' __copyright__ = '2014, Kovid Goyal <kovid at kovidgoyal.net>'
import cPickle, os, sys import os, sys
from threading import Thread, Event, RLock from threading import Thread, Event, RLock
from Queue import Queue from Queue import Queue
from contextlib import closing from contextlib import closing
@ -16,6 +16,7 @@ from calibre.constants import iswindows
from calibre.gui2.tweak_book.completion.basic import Request from calibre.gui2.tweak_book.completion.basic import Request
from calibre.gui2.tweak_book.completion.utils import DataError from calibre.gui2.tweak_book.completion.utils import DataError
from calibre.utils.ipc import eintr_retry_call from calibre.utils.ipc import eintr_retry_call
from calibre.utils.serialize import msgpack_loads, msgpack_dumps
COMPLETION_REQUEST = 'completion request' COMPLETION_REQUEST = 'completion request'
CLEAR_REQUEST = 'clear request' CLEAR_REQUEST = 'clear request'
@ -46,7 +47,7 @@ class CompletionWorker(Thread):
'from {0} import run_main, {1}; run_main({1})'.format(self.__class__.__module__, self.worker_entry_point)) 'from {0} import run_main, {1}; run_main({1})'.format(self.__class__.__module__, self.worker_entry_point))
auth_key = os.urandom(32) auth_key = os.urandom(32)
address, self.listener = create_listener(auth_key) address, self.listener = create_listener(auth_key)
eintr_retry_call(p.stdin.write, cPickle.dumps((address, auth_key), -1)) eintr_retry_call(p.stdin.write, msgpack_dumps((address, auth_key)))
p.stdin.flush(), p.stdin.close() p.stdin.flush(), p.stdin.close()
self.control_conn = eintr_retry_call(self.listener.accept) self.control_conn = eintr_retry_call(self.listener.accept)
self.data_conn = eintr_retry_call(self.listener.accept) self.data_conn = eintr_retry_call(self.listener.accept)
@ -172,7 +173,8 @@ def completion_worker():
def run_main(func): def run_main(func):
from multiprocessing.connection import Client from multiprocessing.connection import Client
address, key = cPickle.loads(eintr_retry_call(sys.stdin.read)) stdin = getattr(sys.stdin, 'buffer', sys.stdin)
address, key = msgpack_loads(eintr_retry_call(stdin.read))
with closing(Client(address, authkey=key)) as control_conn, closing(Client(address, authkey=key)) as data_conn: with closing(Client(address, authkey=key)) as control_conn, closing(Client(address, authkey=key)) as data_conn:
func(control_conn, data_conn) func(control_conn, data_conn)