Just about the only time you can get away with peeing during a race is during a breaststroke pullout. You spend enough time gliding that if you really gotta go, you probably could. Otherwise, you're too tense and too, well, busy to even think about peeing.
Before a race is an interesting time. It depends on the meet and to some extent the color of the pool deck. I kid you not. You always try to pee before you swim, but sometimes your body defies logic and finds a way to refill your bladder just to spite you. Adrenaline and nerves wreak havoc on your system, and I knew tons of other swimmers that always, regardless of prior planning, had to pee right before a race. What to do if you're desperate? Well, it's not uncommon to splash yourself before you climb up on the blocks, so that extra liquid on yourself and the pool deck affords you an interesting opportunity. (I'll let you finish the rest of that thought.)
Warmup/practice - totally free reign. As a swimmer, you just have to accept that you're swimming in pee. I had a teammate that would sit on the wall and announce "I'm peeing!" which was ... disgusting ... but at least she warned us. I'm sure I've swum directly behind people who were just letting it all out.