Commit 40881a0
Fix AttributeError from __del__ during interpreter shutdown
Discussion/issue GH-706 reports an "AttributeError: 'NoneType' object
has no attribute 'info'" emitted from Client.__del__ when the program
exits. Root cause: at interpreter shutdown Python may replace module
globals (e.g. snap7.client.logger) with None before __del__ runs, so
disconnect()'s logger.info / logger.debug calls blow up mid-
finalization. The error doesn't crash the process but pollutes stderr
and can mask real bugs.
Add snap7._finalize.safe_finalize(cleanup) as the canonical helper
for __del__-style cleanup: it early-returns under sys.is_finalizing()
and swallows any exception the cleanup path raises (a __del__ that
raises just prints "Exception ignored" anyway, so swallowing improves
signal without hiding well-caught code-path errors).
Wire Client.__del__ and Partner.__del__ through it. Partner already
had a narrower try/except, so this is a small tightening there and a
real fix for Client. Any future class that needs a destructor should
use the same helper.
Regression test reproduces the reported failure by patching the
module-level logger to None and asserting __del__ is a no-op; without
the fix the test surfaces the same AttributeError the reporter sees.
Longer term the recommended pattern stays: call disconnect() / stop()
explicitly or use the context-manager protocol. __del__ is a safety
net, not the primary lifecycle path.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>1 parent 376910f commit 40881a0
4 files changed
Lines changed: 61 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| 13 | + | |
13 | 14 | | |
14 | 15 | | |
15 | 16 | | |
| |||
2656 | 2657 | | |
2657 | 2658 | | |
2658 | 2659 | | |
2659 | | - | |
2660 | | - | |
| 2660 | + | |
| 2661 | + | |
| 2662 | + | |
| 2663 | + | |
| 2664 | + | |
| 2665 | + | |
| 2666 | + | |
| 2667 | + | |
| 2668 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
12 | 13 | | |
13 | 14 | | |
14 | 15 | | |
| |||
1097 | 1098 | | |
1098 | 1099 | | |
1099 | 1100 | | |
1100 | | - | |
| 1101 | + | |
| 1102 | + | |
| 1103 | + | |
| 1104 | + | |
| 1105 | + | |
1101 | 1106 | | |
1102 | 1107 | | |
1103 | 1108 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| 11 | + | |
11 | 12 | | |
12 | 13 | | |
13 | 14 | | |
| |||
2487 | 2488 | | |
2488 | 2489 | | |
2489 | 2490 | | |
| 2491 | + | |
| 2492 | + | |
| 2493 | + | |
| 2494 | + | |
| 2495 | + | |
| 2496 | + | |
| 2497 | + | |
| 2498 | + | |
| 2499 | + | |
| 2500 | + | |
| 2501 | + | |
2490 | 2502 | | |
2491 | 2503 | | |
2492 | 2504 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
0 commit comments