File: xpickle_worker.py

package info (click to toggle)
python3.14 3.14.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 130,180 kB
  • sloc: python: 756,125; ansic: 717,520; xml: 31,250; sh: 5,990; cpp: 4,063; makefile: 1,999; objc: 787; lisp: 502; javascript: 136; asm: 75; csh: 12
file content (62 lines) | stat: -rw-r--r-- 2,112 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# This script is called by test_xpickle as a subprocess to load and dump
# pickles in a different Python version.
import os
import pickle
import struct
import sys


# This allows the xpickle worker to import picklecommon.py, which it needs
# since some of the pickle objects hold references to picklecommon.py.
test_mod_path = os.path.abspath(os.path.join(os.path.dirname(__file__),
                                             'picklecommon.py'))
if sys.version_info >= (3, 5):
    import importlib.util
    spec = importlib.util.spec_from_file_location('test.picklecommon', test_mod_path)
    sys.modules['test'] = type(sys)('test')
    test_module = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(test_module)
    sys.modules['test.picklecommon'] = test_module
else:
    test_module = type(sys)('test.picklecommon')
    sys.modules['test.picklecommon'] = test_module
    sys.modules['test'] = type(sys)('test')
    with open(test_mod_path, 'rb') as f:
        sources = f.read()
    exec(sources, vars(test_module))

def read_exact(f, n):
    buf = b''
    while len(buf) < n:
        chunk = f.read(n - len(buf))
        if not chunk:
            raise EOFError
        buf += chunk
    return buf

in_stream = getattr(sys.stdin, 'buffer', sys.stdin)
out_stream = getattr(sys.stdout, 'buffer', sys.stdout)

try:
    while True:
        size, = struct.unpack('!i', read_exact(in_stream, 4))
        if not size:
            break
        data = read_exact(in_stream, size)
        protocol, = struct.unpack('!i', data[:4])
        obj = pickle.loads(data[4:])
        data = pickle.dumps(obj, protocol)
        out_stream.write(struct.pack('!i', len(data)) + data)
        out_stream.flush()
except Exception as exc:
    # dump the exception to stdout and write to stderr, then exit
    try:
        data = pickle.dumps(exc)
        out_stream.write(struct.pack('!i', -len(data)) + data)
        out_stream.flush()
    except Exception:
        out_stream.write(struct.pack('!i', 0))
        out_stream.flush()
        sys.stderr.write(repr(exc))
        sys.stderr.flush()
    sys.exit(1)