[Patches] [ python-Patches-437733 ] Additional Argument to Python/C Function

noreply@sourceforge.net noreply@sourceforge.net
Wed, 25 Jul 2001 14:54:27 -0700


Patches item #437733, was opened at 2001-07-01 09:24
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=305470&aid=437733&group_id=5470

Category: None
Group: None
>Status: Open
Resolution: Rejected
Priority: 5
Submitted By: Nobody/Anonymous (nobody)
>Assigned to: Fred L. Drake, Jr. (fdrake)
Summary: Additional Argument to Python/C Function

Initial Comment:
this patch makes it possible that a python/c function 
gets an aditional void* argument. this makes it easier 
to use python with c++. 

PS:i'm a bad despriptor,so please look at the diff 
file. 

----------------------------------------------------------------------

>Comment By: Fred L. Drake, Jr. (fdrake)
Date: 2001-07-25 14:54

Message:
Logged In: YES 
user_id=3066

Just for the record, I'm not ignoring your emailed plea for re-consideration; I just haven't had time to dig back into this matter.

Here's the sample class from the email, so it will be easier to keep track of and for others to comment on the approach:

class PyClass{
public:
	PyClass();
	typedef PyObject* (PyClass::*PyCppFunction)
(PyObject*);
	void addmethod(const char* name,PyCppFunction func);
	~PyClass();
	operator PyObject*(){return (PyObject*)obj;}
private:
	struct PyClassObject{
		PyObject_HEAD
		PyClass *self;
	};
	std::vector<PyMethodDef> methods;
	static PyObject *pycfunc(PyClassObject 
*self,PyObject *arg,void *p);
	static PyObject *getattr(PyClassObject *self,char 
*name);
	static void dealloc(PyClassObject *){}
	PyTypeObject typeobject;
	PyClassObject *obj;
};

void PyClass::addmethod(const char *name,PyCppFunction func)
{
	PyMethodDef meth={
		strdup(name),
		(PyCFunction)pycfunc,
		METH_VARARGS|METH_USERARG,
		NULL,
		*(void**)&func
	};
	methods.insert(methods.begin(),meth);
}

PyClass::~PyClass(){
	for(vector<PyMethodDef>::iterator i=methods.begin
();i!=methods.end();i++)
		free(i->ml_name);
	methods.resize(0);
}

PyObject *PyClass::pycfunc(PyClassObject *self,PyObject 
*arg,void *p){
	PyCppFunction func=*(PyCppFunction*)&p;
	return (self->self->*func)(arg);
}

PyClass::PyClass(){
	PyTypeObject Xxtype = {
		PyObject_HEAD_INIT(&PyType_Type)
		0,			/*ob_size*/
		"xx",			/*tp_name*/
		sizeof(PyClassObject),	/*tp_basicsize*/
		0,			/*tp_itemsize*/
		/* methods */
		(destructor)dealloc, /*tp_dealloc*/
		0,			/*tp_print*/
		(getattrfunc)getattr, /*tp_getattr*/
		(setattrfunc)0, /*tp_setattr*/
		0,			/*tp_compare*/
		0,			/*tp_repr*/
		0,			/*tp_as_number*/
		0,			/*tp_as_sequence*/
		0,			/*tp_as_mappi ng*/
		0,			/*tp_hash*/
	};
	typeobject=Xxtype;
	obj=PyObject_NEW(PyClassObject,&typeobject);
	obj->self=this;
	PyMethodDef md={0};
	methods.push_back(md);
}

PyObject *PyClass::getattr(PyClassObject *self,char *name){
	return Py_FindMethod(&self->self->methods[0],
(PyObject*)self,name);
}

This class is meant to be a base class for other classes 
that represent python types.


----------------------------------------------------------------------

Comment By: Fred L. Drake, Jr. (fdrake)
Date: 2001-07-03 21:48

Message:
Logged In: YES 
user_id=3066

The patch is easy enough to understand, but the motivation
for this is not at all clear.  Rejecting as code bloat.

----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=305470&aid=437733&group_id=5470