Nice, thanks to David for making this public!
I notice that it tests q%r != 0, but it should be r%q != 0.
I understand that most P = (2^p-1)<< p-1 ~ 10^(0.6p) are much larger than r,
and even q=(P-s)/(r+s) will be much larger than r in most cases;
which may suggest the first form to avoid more primality tests,
but I don't think that's useful - at least, technically we would still have to check whether r%q > 0.
There's a much simpler "sieving condition", namely q & 1, that would avoid more than half of the primality checks.
Indeed, q = (P-s)/(r+s) must be odd (because we assume r even), which occurs not very often.
But it turns out that the bottleneck is (r+s) | (P-s) :
for example, using p's up to 5000 and r up to 10^6, there are only 49 cases where this is satisfied,
and q can even be computed -- and in 27 of these, q is even, in only 22 it's odd.
So, for larger p, it might be useful to compute P first only mod r+s to see whether it equals s,
and only in that case, compute the (possibly huge) quotient q = (P-s)/(r+s).
I did so using p's up to 10^6 and r up to 10^7: there are only 105 cases where r+s | P-s,
and in only 38 cases, q is odd.
We can directly test whether P-s is an odd multiple of r+s
by computing P (mod 2(r+s)), which must equal s +- (r+s), or P+r = 0 (mod 2(r+s)).
Finally, there are only very few candidates (r,p) where the primality check takes considerable time.
I don't know for sympy's isprime, but PARI/GP's is(speudo)prime() took more than a second for
{ [37088, 86243], [83416, 21701], [211392, 756839] } (in the above mentioned range).
Actually, I don't know why it takes so long for these, because I found they have small factors:
[37088, 86243]: numdigits(q)=51919, prime: NO (small factors : 1091 · 7487)
[83416, 21701]: numdigits(q)=13060, prime: NO (small factor : 3373)
[211392, 756839]: numdigits(q)=455657, prime: NO (small factor : 143287).
It seems that trial division is done only for really small primes (up to 100 or so)
Maybe it can speed up the search by doing factor(q, 0) which in PARI factors
up to the precomputed primes (by default 10^6 or so).
Also, to avoid the memory voracious pre-computation of sigma(r),
I suggest to use " for r ... : s=sigma(r); ... for p ... : ..."
with sigma = int(divisor_sigma imported from sympy).
That allows me to find the second solution (p = 31, r = 187 268),
in a few seconds on my laptop, with no need for much memory.