Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Re: Stop Python from exiting upon error in Windows (http://www.velocityreviews.com/forums/t319687-re-stop-python-from-exiting-upon-error-in-windows.html)

Tom Plunket 07-15-2003 02:06 AM

Re: Stop Python from exiting upon error in Windows
 
Robert wrote:

> How can I stop the Python interpreter from exiting when an error occurs?


create a batchfile, tell Windows that the association of Python
files is to that batch file, and put this in the file:

python.exe %1
pause


Or- catch the error in your mainline, and do a sys.raw_input()
call on exception.

-tom!

07-15-2003 02:28 AM

Re: Stop Python from exiting upon error in Windows
 
Can you nest exceptions? I already have a few legitimate try/except blocks
where I know there's a chance the act could go bad, do I just stick a try:
at the beginning of the whole body of code and an except: at the end?

- Robert

"Tom Plunket" <tomas@fancy.org> wrote in message
news:n2o6hvcahk7dlmn9t26tvis8umnfg1tdeo@4ax.com...
> Robert wrote:
>
> > How can I stop the Python interpreter from exiting when an error occurs?

>
> create a batchfile, tell Windows that the association of Python
> files is to that batch file, and put this in the file:
>
> python.exe %1
> pause
>
>
> Or- catch the error in your mainline, and do a sys.raw_input()
> call on exception.
>
> -tom!




Peter Hansen 07-15-2003 02:50 AM

Re: Stop Python from exiting upon error in Windows
 
Tom Plunket wrote:
>
> Or- catch the error in your mainline, and do a sys.raw_input()
> call on exception.


Tom meant just "raw_input()", which is a builtin, rather than
sys.raw_input which does not exist, of course.

To answer your question in the other reply, yes, you can
nest exceptions. If you have a try/except and the raw_input
in the except, however, you won't see any exception traceback
printed at the console so you'll need something like the
traceback module and one of the functions from it, like
traceback.print_exc().

-Peter

Conrad 07-15-2003 03:19 AM

Re: Stop Python from exiting upon error in Windows
 
Years ago, Nostradamus predicted that on Mon, 14 Jul 2003 20:06:14 -0700,
Tom Plunket would write, saying:

> Robert wrote:
>
>> How can I stop the Python interpreter from exiting when an error occurs?

>
> create a batchfile, tell Windows that the association of Python
> files is to that batch file, and put this in the file:
>
> python.exe %1
> pause
>
>
> Or- catch the error in your mainline, and do a sys.raw_input()
> call on exception.
>
> -tom!


Or the third, and admittedly brute force solution
I use is to fire up the DOS shell, (click on START,
then RUN, then type "command"). Depending on which
Win you're running, you may want to run DOSKEY,
which lets you cursor back up to previous commands.

Once you've got the command window up (and doskeyed),
cd to your python source directory, and type in
something like *C:\python22\python.exe mypythonfile.py*
(leave out the *s and be sure python is in the same
place on your machine.)

This doesn't keep the python interpreter from exiting,
but it does keep the DOS window open to let you see
your error messages.

I admit it's ugly, but hey, I mostly develop in
FreeBSD and Linux, where the CLI is your buddy ;-)

Conrad



07-15-2003 04:02 AM

Re: Stop Python from exiting upon error in Windows
 
I hear ya. That's what I'm doing at the moment to spot something, but I
wanted to "polish" the program a little...

- Robert

"Conrad" <zneptune321@zexcite.zcom> wrote in message
news:pan.2003.07.15.03.21.44.980226@zexcite.zcom.. .
> Years ago, Nostradamus predicted that on Mon, 14 Jul 2003 20:06:14 -0700,
> Tom Plunket would write, saying:
>
> > Robert wrote:
> >
> >> How can I stop the Python interpreter from exiting when an error

occurs?
> >
> > create a batchfile, tell Windows that the association of Python
> > files is to that batch file, and put this in the file:
> >
> > python.exe %1
> > pause
> >
> >
> > Or- catch the error in your mainline, and do a sys.raw_input()
> > call on exception.
> >
> > -tom!

>
> Or the third, and admittedly brute force solution
> I use is to fire up the DOS shell, (click on START,
> then RUN, then type "command"). Depending on which
> Win you're running, you may want to run DOSKEY,
> which lets you cursor back up to previous commands.
>
> Once you've got the command window up (and doskeyed),
> cd to your python source directory, and type in
> something like *C:\python22\python.exe mypythonfile.py*
> (leave out the *s and be sure python is in the same
> place on your machine.)
>
> This doesn't keep the python interpreter from exiting,
> but it does keep the DOS window open to let you see
> your error messages.
>
> I admit it's ugly, but hey, I mostly develop in
> FreeBSD and Linux, where the CLI is your buddy ;-)
>
> Conrad
>
>




07-15-2003 04:21 PM

Re: Stop Python from exiting upon error in Windows
 
Ok, I figured out the IDLE block indent (duh - RTFMenu), and now it works
great in DOS. Again, thanks to all who helped.

However, I'd still like to know how to determine what environment I'm
running under inside my code. Any suggestions?

- Robert

<Robert@AbilitySys.com> wrote in message
news:vh6v443q2fjab3@corp.supernews.com...
> Thanks! I'm getting closer to what I want, but it raises two questions:
>
> 1. Is there an IDLE keystroke to indent a block of code? (just putting

try:
> at the start of my program causes an error, expecting an indented block to
> follow which is my entire program!)
>
> 2. Is there a way to tell the environment I'm running under (python
> interpreter, IDLE window, or other)? I'd like to put a pause at the end of
> my program if and only if I'm running under the python.exe DOS-like
> program...
>
> - Robert
>
> "Peter Hansen" <peter@engcorp.com> wrote in message
> news:3F136BEA.5B56562@engcorp.com...
> > Tom Plunket wrote:
> > >
> > > Or- catch the error in your mainline, and do a sys.raw_input()
> > > call on exception.

> >
> > Tom meant just "raw_input()", which is a builtin, rather than
> > sys.raw_input which does not exist, of course.
> >
> > To answer your question in the other reply, yes, you can
> > nest exceptions. If you have a try/except and the raw_input
> > in the except, however, you won't see any exception traceback
> > printed at the console so you'll need something like the
> > traceback module and one of the functions from it, like
> > traceback.print_exc().
> >
> > -Peter

>
>




Peter Hansen 07-15-2003 04:26 PM

Re: Stop Python from exiting upon error in Windows
 
Robert@AbilitySys.com wrote:
>
> Ok, I figured out the IDLE block indent (duh - RTFMenu), and now it works
> great in DOS. Again, thanks to all who helped.
>
> However, I'd still like to know how to determine what environment I'm
> running under inside my code. Any suggestions?


I doubt if there's any particular way to do it consistently. In
principle one could write a cute module which would be able to check
the "signature" of the environment to figure it all out. Check if
certain modules are in __builtins__ to see if you are running inside
an application that uses Python for scripting, check for sys.argv[0]
to learn whether you were launched in a way that suggests running as
a script with python.exe, etc.

It's probably not a great idea to write anything which depends on this
however, and it sounds like more time than it's worth.

-Peter


All times are GMT. The time now is 10:48 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.