Ok, the patch now works properly, and the full Python test suite pass with PYTHONDEVMODE=1.
@ncoghlan, @pitrou: What do you think of adding sys.flags.dev_mode?
I chose to make sys.flags.dev_mode a boolean, whereas other flags are integer and have a Py_xxx variable in C.