MNT dont pervert BUILD if state_setter is specified by pierreglaser · Pull Request #2 · pierreglaser/cpython
@ogrisel @pitrou @ncoghlan here is another way of saving an object with a custom state_setter.
This is more compact than the other option, and has the advantage of being fully backward compatible, but it involves some Unpickler stack manipulation:
stack before the state handling part of save_reduce
(1) [..., obj]
if state_setter is not None, we write state_setter and its args, with the resulting stack
(2) [..., obj, state_setter, (obj, state)]
after state_setter is called, the stack is
(3) [..., obj, state_setter(obj, state)]
and we finally have to discard the top of stack (as state_setter's purpose is to do in-place modification of obj), by writing a POP opcode.
WDYT?