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.

classification
Title: time module becomes None after raise SystemExit
Type: behavior Stage:
Components: Interpreter Core Versions: Python 3.4
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: benjamin.peterson, pitrou, ryder.lewis
Priority: normal Keywords:

Created on 2014-05-15 17:50 by ryder.lewis, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (2)
msg218619 - (view) Author: Ryder Lewis (ryder.lewis) * Date: 2014-05-15 17:50
I'm not sure if this is a bug or expected behavior, but basically what I want to do is log the timestamp when an application exits. Sample script to illustrate the issue:

==== try.py ====
import time

class A():
    def __init__(self):
        print("Initializing A. The time is {}".format(time.time()))

    def __del__(self):
        if time is None:
            print("time is None!")
        else:
            print("Deleting A. The time is {}".format(time.time()))

a = A()
raise SystemExit()

=====

When running this script multiple times, there are 3 possible outcomes:

==== outcome #1 (happens about 25% of my trial runs, what I would expect is correct behavior) ====
Initializing A. The time is 1400175810.4806361
Deleting A. The time is 1400175810.483156

==== outcome #2 (happens about 50% of my trial runs) ====
Initializing A. The time is 1400175814.1646852
time is None!

==== outcome #3 (happens about 25% of my trial runs) ====
Initializing A. The time is 1400175809.6802816
Exception ignored in: <bound method A.__del__ of <__main__.A object at 0x7fb125eab5f8>>
Traceback (most recent call last):
  File "./try.py", line 12, in __del__
TypeError: 'NoneType' object is not callable
msg218620 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2014-05-15 18:11
Well, let's call it a limitation rather than a bug.
When creating a global variable (such as "a" in your example), that variable is kept alive at least as long as the module containing it. However, modules usually live until very late in the interpreter shutdown process, *especially* the __main__ module. So, by the time "a" gets garbage-collected, other globals or modules may already have been wiped.

Unfortunately, I don't think there's much we can do to improve this. The answer to your specific problem is to use the atexit module instead, since an atexit callback is guaranteed to be called with a normal execution environment, before it starts being garbage-collected.
History
Date User Action Args
2022-04-11 14:58:03adminsetgithub: 65711
2014-05-15 18:12:28pitrousetstatus: open -> closed
resolution: not a bug -> wont fix
2014-05-15 18:11:49pitrousetstatus: pending -> open

messages: + msg218620
2014-05-15 17:52:30ezio.melottisetstatus: open -> pending
nosy: + pitrou, benjamin.peterson
resolution: not a bug
2014-05-15 17:50:18ryder.lewiscreate