When marking stages for checkpointing, let us make use of the local set datastructure we already allocate, rather than iterating over it linearly. Apart from the negligible performance improvement, it makes the code quite a lot simpler.
dir_fd
os.scandir()