[Python-Dev] Re: C new-style classes and GC

Jim Fulton jim@zope.com
Fri, 16 May 2003 16:30:47 -0400


Jeremy Hylton wrote:
> I'm willing to believe there is a new and better way, but I don't think
> I know what it is. 

You can read the documentation for it here:

http://www.python.org/dev/doc/devel/ext/defining-new-types.html

:)

 > How do we change this code, written using the old
> PyObject_GC_New() to do things the new way?
> 
> Jeremy
> 
> PyObject *
> PyDict_New(void)
> {
> 	register dictobject *mp;
> 	if (dummy == NULL) { /* Auto-initialize dummy */
> 		dummy = PyString_FromString("<dummy key>");
> 		if (dummy == NULL)
> 			return NULL;
> #ifdef SHOW_CONVERSION_COUNTS
> 		Py_AtExit(show_counts);
> #endif
> 	}
> 	mp = PyObject_GC_New(dictobject, &PyDict_Type);
> 	if (mp == NULL)
> 		return NULL;
> 	EMPTY_TO_MINSIZE(mp);
> 	mp->ma_lookup = lookdict_string;
> #ifdef SHOW_CONVERSION_COUNTS
> 	++created;
> #endif
> 	_PyObject_GC_TRACK(mp);
> 	return (PyObject *)mp;
> }

see dict_new in the same file.

The new way to create instances if types is to call the types.

I don't know wht PyDict_New doesn't just call the dict type.
Maybe doing things in-line like this is just an optimization.

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