This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Additional Argument to Python/C Function
Type: Stage:
Components: None Versions:
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: fdrake Nosy List: fdrake, loewis
Priority: normal Keywords: patch

Created on 2001-07-01 16:24 by anonymous, last changed 2022-04-10 16:04 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
mychanges2.diff nobody, 2001-07-01 16:24 diff file for my patch
Messages (5)
msg36876 - (view) Author: Nobody/Anonymous (nobody) Date: 2001-07-01 16:24
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. 
msg36877 - (view) Author: Fred Drake (fdrake) (Python committer) Date: 2001-07-04 04:48
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.
msg36878 - (view) Author: Fred Drake (fdrake) (Python committer) Date: 2001-07-25 21:54
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.
msg36879 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2001-09-19 21:03
Logged In: YES 
user_id=21627

It appears that the C++ fragment is broken and does not 
work as intended.

Apparently, PyCppFunction is a member pointer, which is 
intended to be passed through to the invocation of 
pycfunction. However, AFAICT, addmethod converts the 
pointer-to-member-function into a void* before passing it 
into the methoddefs. This C++ code has undefined 
behaviour: there is no guarantee that a pointer-to-member 
can fit into a void*. In fact, on g++, a pointer-to-member 
is larger than a void* (8 bytes on a 32-bit machine).

It may be possible to fix this. However, I think there are 
much more issues to integrating C++ classes into Python; 
such a class structure would add little if any value. 
Therefore, I'm in favour of rejecting this patch.
msg36880 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2002-03-09 10:56
Logged In: YES 
user_id=21627

Was that re-opened by mistake? Closing it again.
History
Date User Action Args
2022-04-10 16:04:10adminsetgithub: 34689
2001-07-01 16:24:10anonymouscreate