Implementing a "classmethod" in an extension type

Mark Franklin mfranklin at tcsi.com
Tue Apr 1 08:59:16 EST 2003


Hi,

I've got an built-in extension type, implemented in C. I want to be able to implement the equivalent of a "classmethod" in that type. 

I thought I might be able to do something like this, which fairly closely mimics what you do in Python:

	PyTypeObject myType = {
		// initialised in the usual way. It's tp_methods field includes a method definition for "MyMethod".......
	};

	PyObject *method = PyObject_GetAttrString(myType, "MyMethod");
	// method is a PyMethodDescrObject
	PyObject *classMethod = PyClassMethod_New(method);
	PyObject_SetAttrString(myType, "MyMethod", classMethod);
	Py_DECREF(method);

	PyType_Ready(myType);


However, when I do this I get a TypeError exception from the PyObject_SetAttrString call: "can't set attributes of built-in/extension type 'MyType' ".

So, how do I get the class method into the type object?


Thanks,
Mark






More information about the Python-list mailing list