Re: Activating `-i' from inside a script?
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...) Skip
Skip Montanaro <skip@pobox.com> writes:
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: [...]
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...)
For a different approach to the same (?) problem see http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65287 Thomas
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/)
On Sat, 2003-06-28 at 17:40, Guido van Rossum wrote:
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.
I just committed this for 2.3b2. -Barry
Guido> Looks harmless to me. Thomas Heller's alternative really does Guido> something different. Skip can check it in. Barry> I just committed this for 2.3b2. Thanks, I've been offline most of the weekend. Skip
participants (4)
-
Barry Warsaw
-
Guido van Rossum
-
Skip Montanaro
-
Thomas Heller