diff options
author | Charles-François Natali <cf.natali@gmail.com> | 2013-03-24 14:21:49 (GMT) |
---|---|---|
committer | Charles-François Natali <cf.natali@gmail.com> | 2013-03-24 14:21:49 (GMT) |
commit | a655075d162b5b14677bf6620bc255f4391b151a (patch) | |
tree | 85e9e2e3824f9c9043c2a63849abab6e436a3e17 | |
parent | 89e6b3184552e8277b416b848e56ec4a559b29b3 (diff) | |
download | cpython-a655075d162b5b14677bf6620bc255f4391b151a.zip cpython-a655075d162b5b14677bf6620bc255f4391b151a.tar.gz cpython-a655075d162b5b14677bf6620bc255f4391b151a.tar.bz2 |
Issue #17025: Add dumps() and loads() to ForkingPickler.
-rw-r--r-- | Lib/multiprocessing/connection.py | 7 | ||||
-rw-r--r-- | Lib/multiprocessing/forking.py | 11 |
2 files changed, 13 insertions, 5 deletions
diff --git a/Lib/multiprocessing/connection.py b/Lib/multiprocessing/connection.py index 9a357f6..4dd6502 100644 --- a/Lib/multiprocessing/connection.py +++ b/Lib/multiprocessing/connection.py @@ -12,7 +12,6 @@ __all__ = [ 'Client', 'Listener', 'Pipe', 'wait' ] import io import os import sys -import pickle import select import socket import struct @@ -202,9 +201,7 @@ class _ConnectionBase: """Send a (picklable) object""" self._check_closed() self._check_writable() - buf = io.BytesIO() - ForkingPickler(buf, pickle.HIGHEST_PROTOCOL).dump(obj) - self._send_bytes(buf.getbuffer()) + self._send_bytes(ForkingPickler.dumps(obj)) def recv_bytes(self, maxlength=None): """ @@ -249,7 +246,7 @@ class _ConnectionBase: self._check_closed() self._check_readable() buf = self._recv_bytes() - return pickle.loads(buf.getbuffer()) + return ForkingPickler.loads(buf.getbuffer()) def poll(self, timeout=0.0): """Whether there is any input available to be read""" diff --git a/Lib/multiprocessing/forking.py b/Lib/multiprocessing/forking.py index 7bda412..37c9a10 100644 --- a/Lib/multiprocessing/forking.py +++ b/Lib/multiprocessing/forking.py @@ -7,7 +7,9 @@ # Licensed to PSF under a Contributor Agreement. # +import io import os +import pickle import sys import signal import errno @@ -44,6 +46,15 @@ class ForkingPickler(Pickler): def register(cls, type, reduce): cls._extra_reducers[type] = reduce + @staticmethod + def dumps(obj): + buf = io.BytesIO() + ForkingPickler(buf, pickle.HIGHEST_PROTOCOL).dump(obj) + return buf.getbuffer() + + loads = pickle.loads + + def _reduce_method(m): if m.__self__ is None: return getattr, (m.__class__, m.__func__.__name__) |