[Python-Dev] Re: Activating `-i' from inside a script?
Guido van Rossum
guido@python.org
Sat, 28 Jun 2003 17:40:11 -0400
> 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;
> }
>
> + /* also check at the end in case the program set PYTHONINSPECT */
> + if (!saw_inspect_flag &&
> + (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
> + inspect = 1;
> +
> if (inspect && stdin_is_interactive &&
> (filename != NULL || command != NULL))
> /* XXX */
>
> 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...)
Looks harmless to me. Thomas Heller's alternative really does
something different. Skip can check it in.
--Guido van Rossum (home page: http://www.python.org/~guido/)