Skip to content

time.strptime() with lambda/function call triggers GC finalization error in Python 3.12 (struct_time n_fields missing in tp_clear) #148551

@SimGo123

Description

@SimGo123

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_time internal state (n_fields attribute 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_clear execution 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

No one assigned

    Labels

    3.12only security fixesextension-modulesC modules in the Modules dirpendingThe issue will be closed if no feedback is providedtype-bugAn unexpected behavior, bug, or error

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions