Python Forum
link variable to exc blocks garbage collection
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
link variable to exc blocks garbage collection
#1
I found this strange behavior. Why is the error variable not garbage collected? If error is another object, this wouldn't happen.
import gc import weakref class MyException(Exception): pass def func(): try: raise MyException() except MyException as exc: error = exc return error # Only for inspection import dis dis.dis(func) # Only keep a weak reference to the exception err = func() ref = weakref.ref(err) del err # Check if anything refers to the exception refs = gc.get_referrers(ref()) for ref_ in refs: if ref_.__class__.__name__ == "frame": print(ref_)
The output with Python 3.12 is:
Output:
7 0 RESUME 0 8 2 NOP 9 4 LOAD_GLOBAL 1 (NULL + MyException) 14 CALL 0 22 RAISE_VARARGS 1 >> 24 PUSH_EXC_INFO 10 26 LOAD_GLOBAL 0 (MyException) 36 CHECK_EXC_MATCH 38 POP_JUMP_IF_FALSE 13 (to 66) 40 STORE_FAST 0 (exc) 11 42 LOAD_FAST 0 (exc) 44 STORE_FAST 1 (error) 46 POP_EXCEPT 48 LOAD_CONST 0 (None) 50 STORE_FAST 0 (exc) 52 DELETE_FAST 0 (exc) 12 54 LOAD_FAST 1 (error) 56 RETURN_VALUE >> 58 LOAD_CONST 0 (None) 60 STORE_FAST 0 (exc) 62 DELETE_FAST 0 (exc) 64 RERAISE 1 10 >> 66 RERAISE 0 >> 68 COPY 3 70 POP_EXCEPT 72 RERAISE 1 ExceptionTable: 4 to 22 -> 24 [0] 24 to 40 -> 68 [1] lasti 42 to 44 -> 58 [1] lasti 58 to 66 -> 68 [1] lasti <frame at 0x7f37f9f4d3c0, file '/home/XXX/test.py', line 12, code func>
Reply
#2
When I add this at line 22
import gc gc.collect()
the error does not happen.

It means that err has not yet been collected when you print the results.
Pedroski55 likes this post
« We can solve any problem by introducing an extra level of indirection »
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Photo Converting Pandas DataFrame to a table of hourly blocks Abedin 1 1,422 Apr-24-2025, 01:05 PM
Last Post: snippsat
  RSA Cipher with blocks Paragoon2 0 1,698 Nov-26-2023, 04:35 PM
Last Post: Paragoon2
  How to properly format rows and columns in excel data from parsed .txt blocks jh67 7 5,261 Dec-12-2022, 08:22 PM
Last Post: jh67
  threadlocals are garbage collected before thread ends akv1597 0 2,825 Mar-09-2021, 12:13 PM
Last Post: akv1597
  Am I a retard - else and finally blocks in a try statement RubenF85 6 4,528 Jan-12-2021, 05:56 PM
Last Post: bowlofred
  Images are storing in RAM and don't get garbage collected MaxRicik 4 5,724 Jan-07-2021, 02:59 AM
Last Post: deanhystad
  How to tabulate correctly repeated blocks? Xiesxes 4 4,777 Mar-21-2020, 04:57 PM
Last Post: Xiesxes
  try/except blocks newbieAuggie2019 11 8,538 Oct-05-2019, 05:55 PM
Last Post: newbieAuggie2019
  Understanding program blocks newbieAuggie2019 2 3,332 Oct-02-2019, 06:22 PM
Last Post: newbieAuggie2019
  Why doesn't gc delete an object without forcing a garbage collection call? AlekseyPython 5 6,045 Mar-19-2019, 02:10 AM
Last Post: micseydel

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.