This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Rogi
Recipients Rakeka, Rogi, eric.frederich, georg.brandl, mhammond, santoso.wijaya, vstinner
Date 2011-03-28.19:05:55
SpamBayes Score 0.0
Marked as misclassified No
Message-id <1301339156.87.0.102148342932.issue6498@psf.upfronthosting.co.za>
In-reply-to
Content
I reviewed teh problem and rewrote teh text to better explain what is
happening. Also, I noticed teh change to teh docs, and in fact it is
not correct that teh functions call exit() on SystemError.

*** From teh docs:

http://docs.python.org/c-api/veryhigh.html


int Py_Main(int argc, char **argv)¶

    The main program for the standard interpreter. This is made
available for programs which embed Python. The argc and argv parameters
should be prepared exactly as those which are passed to a C program¿s
main() function. It is important to note that the argument list may be
modified (but the contents of the strings pointed to by the argument
list are not). The return value will be the integer passed to the
sys.exit() function, 1 if the interpreter exits due to an exception, or
2 if the parameter list does not represent a valid Python command line.

    Note that if an otherwise unhandled SystemError is raised, this
function will not return 1, but exit the process, as long as
Py_InspectFlag is not set.



*** Teh problem:

	Teh Py_Main() function does not return on any unhandled
exception. If a unhandled exception is caught, teh function prints teh
error and go to teh prompt. Try:

	...
	>>> raise(SystemError)
	Traceback (most recent call last):
	  File "<stdin>", line 1, in <module>
	SystemError
	>>>

Consequently, Py_Main() does not return on unhandled SystemExit. But not
only that. It calls exit() which terminate teh process. Try:

	...
	Py_Main(argc, argv);
	fprintf(stderr, "reached!");
	...

	...
	>>> raise(SystemExit)
	$ 

note it did not print after Py_Main() termination.



*** Analysis:

	After greping python source, I found that this behaviour is
triggered in function PyErr_PrintEx(). For some reason, SystemExit is
handled here, in a function that, from its name, should print, not
handle, and its handling involves a call to exit(), which terminate teh
teh process. Also, not only Py_Main() is affected by this, but any other
function that calls PyErr_PrintEx(), such as PyRun_SimpleString().

	I tried to fix this but it would require some rewriting. I think
this could be accomplished with some set/longjmp, but still some
rewriting would be needed to fix other functions that rely on
PyErr_PrintEx() behaviour.



*** Possible solutions:

* fix teh source to match teh docs
* fix teh docs to match teh source



*** Proposed solution:

	Teh current behaviour hit me when I was writing an app which
had Py_Main() running on a thread. If teh user typed exit() in Python,
teh program would crash from exiting on a bad place. Based on this I
propose a review of teh source that adhere to teh following:

	1 - PyErr_PrintEx() should not be handling any kind of
exception. This should be done by a function liek
PyErr_HandleException().

	2 - In functions liek PyRun_SimpleString(), teh default
exception handling should only occur after giving teh programmer a
chance to handle and clear teh exception himself.

	3 - No calls to exit() should be made by teh Python interpreter,
except, maybe, in some function which is a explicit wrapper.

	Since proposing is easy and doing is not, It would be nice to
have, for now, a warning at teh docs for every bad behaving function,
specially those which call exit().
History
Date User Action Args
2011-03-28 19:05:56Rogisetrecipients: + Rogi, mhammond, georg.brandl, vstinner, Rakeka, santoso.wijaya, eric.frederich
2011-03-28 19:05:56Rogisetmessageid: <1301339156.87.0.102148342932.issue6498@psf.upfronthosting.co.za>
2011-03-28 19:05:56Rogilinkissue6498 messages
2011-03-28 19:05:55Rogicreate