Check that two pickle output are bitwise equal. If it is not the case, print the diff between the disassembled pickle payloads. This helper is useful to investigate non-deterministic pickling.
(a, b)
| 217 | |
| 218 | |
| 219 | def check_deterministic_pickle(a, b): |
| 220 | """Check that two pickle output are bitwise equal. |
| 221 | |
| 222 | If it is not the case, print the diff between the disassembled pickle |
| 223 | payloads. |
| 224 | |
| 225 | This helper is useful to investigate non-deterministic pickling. |
| 226 | """ |
| 227 | if a != b: |
| 228 | with io.StringIO() as out: |
| 229 | pickletools.dis(pickletools.optimize(a), out) |
| 230 | a_out = out.getvalue() |
| 231 | # Remove the 11 first characters of each line to remove the bytecode offset |
| 232 | # of each object, which is different on each line for very small differences, |
| 233 | # making the diff very hard to read. |
| 234 | a_out = "\n".join(ll[11:] for ll in a_out.splitlines()) |
| 235 | with io.StringIO() as out: |
| 236 | pickletools.dis(pickletools.optimize(b), out) |
| 237 | b_out = out.getvalue() |
| 238 | b_out = "\n".join(ll[11:] for ll in b_out.splitlines()) |
| 239 | assert a_out == b_out |
| 240 | full_diff = difflib.context_diff( |
| 241 | a_out.splitlines(keepends=True), b_out.splitlines(keepends=True) |
| 242 | ) |
| 243 | full_diff = "".join(full_diff) |
| 244 | if len(full_diff) > 1500: |
| 245 | full_diff = full_diff[:1494] + " [...]" |
| 246 | raise AssertionError( |
| 247 | "Pickle payloads are not bitwise equal:\n" |
| 248 | + full_diff |
| 249 | ) |
| 250 | |
| 251 | |
| 252 | if __name__ == "__main__": |
no test coverage detected
searching dependent graphs…