473,378 Members | 1,152 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Adding "proxy" functions to a type

Hello all.

Is there a convenient scheme within a C extension to add
methods to a type in such a way as to allow me to
transparently add a "proxy" around them? For example:

typedef PyObject* (*PyMethodCall)(PyObject*, PyObject*);

PyObject* middleMan(PyObject* self, PyObject* args) {
printf("Before call to wrapped funct\n");
PyMethodCall actualFunc = getRealFunction(...);
PyObject* retval = actualFunc(self, args);
printf("After call to wrapped func\n");
return retval;
}

void addMethod(PyTypeObject* t, PyMethodCall* m,
char* name, char* doc) {
// code to forward calls to "middleMan" while putting the
// pointer to "m" somewhere convenient
...
}

My current solution is cumbersome and involves adding a
special field to the PyObject associated with my type, a
custom tp_getattro function and the "middleMan" function:

struct MyPyObj {
PyObject_HEAD
PyMethodCall realfunc;
};

// when adding the method def for "m" instead of pointing it to
// the given function, I point it to the middle man and save
// the "m" function somewhere I can find it later.
void addMethod(PyTypeObject* t, PyMethodCall m,
char* name, char* doc) {

PyMethodDef* def = allocPyMethodDef(t, name);
def->ml_name = name;
def->ml_doc = doc;
def->ml_meth = middleMan;
def->ml_flags = METH_VARARGS;

saveTargetFunction(name, m);
// note I add these here so that documentation is
// available within the interpreter
PyObject* methobj = PyDescr_NewMethod(t, def);
PyDict_SetItemString(t->tp_dict, def->ml_name, methobj);
Py_DECREF(methobj);
}

// when the interpreter does a lookup on an instance of my
// type I set the "realfunc" member of my PyObject and return
// a bound method (which will call into middleMan).
PyObject* customGetaAttro(PyObject* self, PyObject* name) {
MyPyObj* rself = (MyPyObj*)self;
rself->realfunc = findSavedTargetFunc(name);
PyMethodDef* mdef = getMethodDef(self->ob_type, name);
return PyCFunction_New(def, self);
}

// finally, when the middle man is called it extracts the "real"
// function from self and calls that.
PyObject* middleMan(PyObject* self, PyObject* args) {
MyPyObj* rself = (MyPyObj*)(self);
printf("pre call\n");
PyObject* rv = rself->realfunc(rself->obj, args);
printf("post call\n");
rself->realfunc = 0;
return rv;
}

The problem here is that this doesn't work for static functions which
lack a self argument, or for module level functions.

Is there a better way?

Thanks,
Iker Arizmendi

Jul 19 '05 #1
0 1174

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Tomas Christiansen | last post by:
Im trying to make a simple TCP socket "relay" or "proxy", but my skills in Python are not high (yet). The only thing it should do, is to open the connection on behalf of the client, and when the...
0
by: Jéjé | last post by:
Hi, I'm looking for a component which can act as a "proxy". I mean when a user access a virtual folder I want to get the query then I'll send it to a back end server, then I display the result...
0
by: lpinho | last post by:
Hi There, I've generated a C# file from a wsdl file using wsdl.exe utility. Then I created a console application and made a call to the method generated, first I got the error: "The request...
0
by: Henrik Gøttig | last post by:
Hi group I am investingating how I can turn on proxy type sharing from the IDE. I am aware that I can do that from the sharetypes switch on the command-line to wsdl.exe If I have a ASP.NET...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.