Retrieve pickle string of an object generated by a child Python process Pickle the input data into a buffer, send it to a subprocess via stdin, expect the subprocess to unpickle, re-pickle that data back and send it back to the parent process via stdout for final unpickling. >>> te
(input_data, protocol=None, timeout=TIMEOUT, add_env=None)
| 41 | |
| 42 | |
| 43 | def subprocess_pickle_string(input_data, protocol=None, timeout=TIMEOUT, add_env=None): |
| 44 | """Retrieve pickle string of an object generated by a child Python process |
| 45 | |
| 46 | Pickle the input data into a buffer, send it to a subprocess via |
| 47 | stdin, expect the subprocess to unpickle, re-pickle that data back |
| 48 | and send it back to the parent process via stdout for final unpickling. |
| 49 | |
| 50 | >>> testutils.subprocess_pickle_string([1, 'a', None], protocol=2) |
| 51 | b'\x80\x02]q\x00(K\x01X\x01\x00\x00\x00aq\x01Ne.' |
| 52 | |
| 53 | """ |
| 54 | # run then pickle_echo(protocol=protocol) in __main__: |
| 55 | |
| 56 | # Protect stderr from any warning, as we will assume an error will happen |
| 57 | # if it is not empty. A concrete example is pytest using the imp module, |
| 58 | # which is deprecated in python 3.8 |
| 59 | cmd = [sys.executable, "-W ignore", __file__, "--protocol", str(protocol)] |
| 60 | cwd, env = _make_cwd_env() |
| 61 | if add_env: |
| 62 | env.update(add_env) |
| 63 | proc = Popen( |
| 64 | cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, cwd=cwd, env=env, bufsize=4096 |
| 65 | ) |
| 66 | pickle_string = dumps(input_data, protocol=protocol) |
| 67 | try: |
| 68 | comm_kwargs = {} |
| 69 | comm_kwargs["timeout"] = timeout |
| 70 | out, err = proc.communicate(pickle_string, **comm_kwargs) |
| 71 | if proc.returncode != 0 or len(err): |
| 72 | message = "Subprocess returned %d: " % proc.returncode |
| 73 | message += err.decode("utf-8") |
| 74 | raise RuntimeError(message) |
| 75 | return out |
| 76 | except TimeoutExpired as e: |
| 77 | proc.kill() |
| 78 | out, err = proc.communicate() |
| 79 | message = "\n".join([out.decode("utf-8"), err.decode("utf-8")]) |
| 80 | raise RuntimeError(message) from e |
| 81 | |
| 82 | |
| 83 | def subprocess_pickle_echo(input_data, protocol=None, timeout=TIMEOUT, add_env=None): |
no test coverage detected
searching dependent graphs…