[Python-Dev] Re: Activating `-i' from inside a script? (original) (raw)

Skip Montanaro skip@pobox.com
Fri, 27 Jun 2003 06:56:46 -0500


On c.l.py someone asked about forcing a tailend interactive shell from within the program. Try Melhase proposed:

import os

if __name__ == '__main__':
    discrepancy = True
    if discrepancy:
        os.environ['PYTHONINSPECT'] = "why yes, i'd like that"

but observed that it doesn't work because the environment variable is only checked at program start.

This change to Modules/main.c makes the above work:

% cvs diff main.c Index: main.c

RCS file: /cvsroot/python/python/dist/src/Modules/main.c,v retrieving revision 1.77 diff -c -r1.77 main.c *** main.c 30 Mar 2003 17:09:58 -0000 1.77 --- main.c 27 Jun 2003 11:50:42 -0000


*** 418,423 **** --- 418,428 ---- filename != NULL, &cf) != 0; }

I can't see that it would affect the functioning of any programs other than those which set PYTHONINSPECT now. (But why would they do that and what's the likelihood such programs exist?)

Any chance this could be squeezed in? It seems rather elegant:

try:
    something which fails
except:
    os.environ["PYTHONINSPECT"] = 1
    raise

(I know we're at beta1, and the programmer could re-run with -i. But still...)

Skip