Repository navigation
time.strptime() with lambda/function call triggers GC finalization error in Python 3.12 (struct_time n_fields missing in tp_clear) #148551
Description
Bug report
Bug description:
Summary
When calling time.strptime() inside a function (including a lambda), CPython 3.12 can emit a GC finalization error during object teardown:
Exception ignored in tp_clear of: <class 'dict'>
TypeError: Missed attribute 'n_fields' of type time.struct_time
The issue is not reproducible when calling time.strptime() directly in assignment context, and appears to depend on temporary object lifetime and function call boundaries.
Environment
- Python: 3.12.3 (also reproducible in locally built 3.12 from source)
- OS: Linux (x86_64)
- Compiler: GCC 13.3.0
- Repro isolation: works under
env -i(no environment variables required)
Minimal Reproducer
python3 -m venv venv && env -i ./venv/bin/python3 -c "
import time
func = lambda: time.strptime(
'Fri, 27 Mar 2026 10:00:00',
'%a, %d %b %Y %H:%M:%S'
)
x = func()
"Observed output
Exception ignored in tp_clear of: <class 'dict'>
TypeError: Missed attribute 'n_fields' of type time.struct_time
Non-reproducible variant
The issue does NOT occur when the same call is executed directly:
import time
x = time.strptime(
'Fri, 27 Mar 2026 10:00:00',
'%a, %d %b %Y %H:%M:%S'
)This version runs cleanly without any GC or finalization errors.
Additional observations
- Reproducible in isolated environment (
env -i) - Reproducible in fresh virtual environments
- Not dependent on external packages or site customization
- Appears sensitive to function/lambda call boundaries and temporary object lifetime
- Error originates during garbage collection finalization (
tp_clear) - Involves
time.struct_timeinternal state (n_fieldsattribute missing during teardown)
Expected behavior
time.strptime() should behave identically regardless of whether it is:
- called directly in assignment context
- returned from a function
- executed inside a lambda
No GC-related exceptions should be emitted during object finalization.
Actual behavior
Only function/lambda invocation triggers:
Exception ignored in tp_clear of: <class 'dict'>
TypeError: Missed attribute 'n_fields' of type time.struct_time
Notes / Hypothesis
This appears to be a CPython 3.12 GC finalization edge case involving:
time.struct_time(C-level object)- temporary object lifetime in function frames
- garbage collector /
tp_clearexecution path - possible refcount vs GC interaction during frame teardown
The issue may be triggered by subtle differences in:
- stack frame lifetime
- temporary object escaping rules
- lambda/function call cleanup ordering
Impact
- Unexpected exceptions during interpreter shutdown / GC
- Potentially indicates memory safety issue in CPython core for
struct_time - Difficult-to-diagnose nondeterministic behavior depending on call context
Notes
- I generated this report with ChatGPT because I'm not familar with CPython / garbage collection
- I came across this issue when using feedparser inside a function and getting:
Exception ignored in tp_clear of: <class 'dict'>
SystemError: ../Objects/longobject.c:577: bad argument to internal function
While attempting to reduce this to a minimal reproducible example, I encountered the mentioned issue and believe they could be related (both only occur when called in a function)
CPython versions tested on:
3.12
Operating systems tested on:
Linux
Metadata
Metadata
Assignees
Labels
Projects
- StatusShow more project fieldsDone
Activity
sergey-miryanov commentedon Apr 14, 2026
I can't reproduce on current 3.12 branch.
StanFromIreland commentedon Apr 14, 2026
I think it was fixed by 4b63cd1, which made it into 3.12.6. Can your reproduce this on that or a newer version?
SimGo123 commentedon Apr 14, 2026
@StanFromIreland Yes, doesn't occur in 3.12.6. Closing it.