[issue27035] Cannot set exit code in atexit callback

2016-05-16 Thread Miroslav Matějů

New submission from Miroslav Matějů:

I want to set exit code of my script in a function registered in the atexit 
module. (See https://stackoverflow.com/q/37178636/711006.) Calling sys.exit() 
in that function results in the following error:

Error in atexit._run_exitfuncs:
Traceback (most recent call last):
  File "atexit_test.py", line 3, in myexit
sys.exit(2)
SystemExit: 2

Despite the printed error, the exit code is set to 0. (This might be related 
with #1257.)

This problem seems to affect Python 3.x. I experienced it with Python 3.5.1 on 
Windows 7 x64 and I am able to reproduce it with Python 3.4.3 on Linux (x64). 
Python 2.7.6 on the same Linux machine works as expected: Exits without 
additional messages and the desired exit code is set.

A simple test case:

def myexit():
  import sys
  sys.exit(2)

import atexit
atexit.register(myexit)

--
components: Extension Modules
messages: 265678
nosy: Melebius
priority: normal
severity: normal
status: open
title: Cannot set exit code in atexit callback
type: behavior
versions: Python 3.5

___
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue27035>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue27035] Cannot set exit code in atexit callback

2019-01-24 Thread Miroslav Matějů

Miroslav Matějů  added the comment:

I completely support @gwk’s opinion expressed in his comments. My original 
intention has been to set the exit code in an atexit callback. I tried a way 
and found that it was working in Python 2.7 but not in 3.x (without notice), so 
I filed this bug report. (See also the Stack Overflow link in my first post.)

I don’t find this just a documentation issue since it prevents the user from 
setting the exit code, although (as correctly stated by @gwk): “Ultimately, it 
is the responsibility of the application programmer to return an appropriate 
code for all execution paths” and “returning the correct code is the most 
critical behavior of a process”.

My use case is a testing library. The user calls assertions from my library and 
when their script finishes, my library is responsible for deciding whether the 
test has passed (and finishing the log). Before porting the library to Python 
(3.5 initially), I used to indicate successfulness by the exit code. With 
Python 3.x, I am forced to either:
1) print a result message and let the parent process parse it (implemented 
currently), or
2) force the user of my library to include something like
 sys.exit(testlib.result())
   as the last line of their scripts which I find annoying and error-prone.

I cannot decide whether calling sys.exit() in an atexit callback means breaking 
the contract as @r.david.murray states. However, the user shall be able to set 
the exit code of their application when it finishes and atexit should support 
some way to do that.

--

___
Python tracker 
<https://bugs.python.org/issue27035>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com