Debian Codegraph @

Lib/test/xpickle_worker.py

raw
1 # This script is called by test_xpickle as a subprocess to load and dump
2 # pickles in a different Python version.
3 import os
4 import pickle
5 import struct
6 import sys
7
8
9 # This allows the xpickle worker to import picklecommon.py, which it needs
10 # since some of the pickle objects hold references to picklecommon.py.
11 test_mod_path = os.path.abspath(os.path.join(os.path.dirname(__file__),
12 'picklecommon.py'))
13 if sys.version_info >= (3, 5):
14 import importlib.util
15 spec = importlib.util.spec_from_file_location('test.picklecommon', test_mod_path)
16 sys.modules['test'] = type(sys)('test')
17 test_module = importlib.util.module_from_spec(spec)
18 spec.loader.exec_module(test_module)
19 sys.modules['test.picklecommon'] = test_module
20 else:
21 test_module = type(sys)('test.picklecommon')
22 sys.modules['test.picklecommon'] = test_module
23 sys.modules['test'] = type(sys)('test')
24 with open(test_mod_path, 'rb') as f:
25 sources = f.read()
26 exec(sources, vars(test_module))
27
28 def read_exact(f, n):
29 buf = b''
30 while len(buf) < n:
31 chunk = f.read(n - len(buf))
32 if not chunk:
33 raise EOFError
34 buf += chunk
35 return buf
36
37 in_stream = getattr(sys.stdin, 'buffer', sys.stdin)
38 out_stream = getattr(sys.stdout, 'buffer', sys.stdout)
39
40 try:
41 while True:
42 size, = struct.unpack('!i', read_exact(in_stream, 4))
43 if not size:
44 break
45 data = read_exact(in_stream, size)
46 protocol, = struct.unpack('!i', data[:4])
47 obj = pickle.loads(data[4:])
48 data = pickle.dumps(obj, protocol)
49 out_stream.write(struct.pack('!i', len(data)) + data)
50 out_stream.flush()
51 except Exception as exc:
52 # dump the exception to stdout and write to stderr, then exit
53 try:
54 data = pickle.dumps(exc)
55 out_stream.write(struct.pack('!i', -len(data)) + data)
56 out_stream.flush()
57 except Exception:
58 out_stream.write(struct.pack('!i', 0))
59 out_stream.flush()
60 sys.stderr.write(repr(exc))
61 sys.stderr.flush()
62 sys.exit(1)