[Python-Dev] Re: C new-style classes and GC
Jim Fulton
jim@zope.com
Fri, 16 May 2003 17:08:27 -0400
Jim Fulton wrote:
> Lately I've been re-learning how to write new types in C. Things
> changed drastically (for the better) in 2.2. I've been updating the
> documentation on writing new types as I go:
>
> http://www.python.org/dev/doc/devel/ext/defining-new-types.html
>
> (I'm also updating modulator.)
>
> I'm starting to try to figure out how to integrate support for GC.
> The current documentation in the section "Supporting the Cycle
> Collector" doesn't reflect new-style types and is, thus, out of date.
>
> Frankly, I'm taking the approach that there is only One Way to create
> types in C, the new way, based on new-style types as now documented
> in the manual.
>
> I'll also note that most new-style types don't need and thus don't
> implement custom allocators. They leave the tp_alloc and tp_free slots
> empty.
>
> So given that we have a new style type, to add support for GC, we need
> to:
>
> - Set the Py_TPFLAGS_HAVE_GC type flag,
>
> - Provide implementations of tp_traverse and tp_clear, as described in
> the section "Supporting the Cycle Collector" section of the docs.
>
> - Call PyObject_GC_UnTrack at the beginning of the deallocator,
> before decrefing any members.
>
> I think that that is *all* we have to do.
It looks like the answer is "no". :)
I tried to write a type using this formula and segfaulted.
Looking at other types, I found that if I want to support GC and
am using the default allocator, which I get for free, I have to fill
the tp_free slot with PyObject_GC_Del (_PyObject_GC_Del if I want to
support Python 2.2 and 2.3).
I *think* this is all I have to do.
Jim
--
Jim Fulton mailto:jim@zope.com Python Powered!
CTO (703) 361-1714 http://www.python.org
Zope Corporation http://www.zope.com http://www.zope.org