The safest way is to use a 4PDT (4 pole double throw) switch that
cuts out either side of the programming track when in programming
track mode. The auto switch still allows a loco to creep off the end
of the programming track.
Other guys have put physical blocks on both ends of the programming
track such as having a solenoid operated rod that comes up between
the rails (at both ends). This also needs a multi-pole double throw
switch. Its advantage is that the loco can never walk clear off the
programming track when doing a lot of reads such as when doing a
read-all-sheets in DecoderPro. The disadvantage is it is more
complicated and so more work and also slightly more likely to fail.