473,394 Members | 1,802 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

aborting without killing the python interpreter

I wrote a simple little function for exiting with an error message:

def error ( message ): print_stack(); exit ("\nERROR: " + message +
"\n")

It works fine for executing as a script, but when I run it
interactively in the python interpreter it kills the interpreter.
That's not what I want. Is there a simple way to have a script
terminate but not have it kill the python interpreter when I run it
interactively? I suspect I may need to use exceptions, but I'm hoping
not to need them. Thanks.

Feb 19 '06 #1
5 4601

"Russ" <uy*******@sneakemail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
I wrote a simple little function for exiting with an error message:

def error ( message ): print_stack(); exit ("\nERROR: " + message +
"\n")

It works fine for executing as a script,
How? In the standard interpreter, 'exit' is bound to the string
'Use Ctrl-Z plus Return to exit.'
so trying to call it as a function fails.

but when I run it interactively in the python interpreter it kills the interpreter.
That's not what I want. Is there a simple way to have a script
terminate but not have it kill the python interpreter when I run it
interactively? I suspect I may need to use exceptions, but I'm hoping
not to need them. Thanks.


The interactive interpreter runs a statement at a time and gives a prompt
after any output. From a command shell, you can use a flag (-i I think) to
enter interactive mode after the script end.

Terry Jan Reedy

Feb 19 '06 #2
<correction>

"Terry Reedy" <tj*****@udel.edu> wrote in message
news:dt**********@sea.gmane.org...
How? In the standard interpreter, 'exit' is bound to the string
'Use Ctrl-Z plus Return to exit.'


This is, of course, Windows specific. Other systems have other strings.

Feb 19 '06 #3
Terry Reedy wrote:
How? In the standard interpreter, 'exit' is bound to the string
'Use Ctrl-Z plus Return to exit.'
so trying to call it as a function fails.


I'm _presuming_ there was a hidden `from sys import *` in there. Hence
calling exit with the string (the help for sys.exit shows that if a
string is passed in, it will be printed before the process exits with
failure -- something I wasn't aware of actually).

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
God heals, and the doctor takes the fee.
-- Benjamin Franklin
Feb 19 '06 #4
Russ wrote:
I wrote a simple little function for exiting with an error message:

def error ( message ): print_stack(); exit ("\nERROR: " + message +
"\n")

It works fine for executing as a script, but when I run it
interactively in the python interpreter it kills the interpreter.
That's not what I want. Is there a simple way to have a script
terminate but not have it kill the python interpreter when I run it
interactively? I suspect I may need to use exceptions, but I'm hoping
not to need them. Thanks.


Exceptions do *exactly* what you want in a very clean and simple way. They are a
fundamental feature of Python. Do not fear them. They are your friends.

--
Robert Kern
ro*********@gmail.com

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

Feb 19 '06 #5
import sys

def main():
print 'exiting'
sys.exit()

try:
main()
except SystemExit:
pass

I suspect I may need to use exceptions, but I'm hoping
not to need them. Thanks.


Use the Exceptions!
Feb 19 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: vincent Salaun | last post by:
hi all, here's my problem : I've embedded a python interpreter in our java application (based on the NetBeans palteforrm) using the Jython API : http://www.jython.org/docs/javadoc/index.html...
147
by: Sateesh | last post by:
Hi, I am a beginner in Python, and am wondering what is it about the indentation in Python, without which python scripts do not work properly. Why can't the indentation not so strict so as to give...
12
by: Anon | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello all I am a beginner teaching myself python, and I am enjoying it immensely :) As a language it is great, I real treat to study, I actually...
35
by: Michael Kearns | last post by:
I've been using python to write a simple 'launcher' for one of our Java applications for quite a while now. I recently updated it to use python 2.4, and all seemed well. Today, one of my...
6
by: Mike Brown | last post by:
Hello I am calling a stored procedure in a MSDE/SQLServer DB form within my Visual C++ 6.0 program along the lines CCommand<CAccessor<CdboMyAccessor>>::Open(m_session, NULL); With...
0
by: Jerry LeVan | last post by:
From libpq, is it possible to safely terminate a long running query? (without killing the application) The only thing I could see that might do the trick was PQreset. Jerry ...
1
by: Petr Prikryl | last post by:
Do you think that the following could became PEP (pre PEP). Please, read it, comment it, reformulate it,... Abstract Introduction of the mechanism for language extensions via modules...
113
by: John Nagle | last post by:
The major complaint I have about Python is that the packages which connect it to other software components all seem to have serious problems. As long as you don't need to talk to anything outside...
3
by: Marcin Kalicinski | last post by:
How do I use multiple Python interpreters within the same process? I know there's a function Py_NewInterpreter. However, how do I use functions like Py_RunString etc. with it? They don't take any...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.