Builtin methods versus Builtin Functions -- help! 
Author Message
 Builtin methods versus Builtin Functions -- help!

Something is going wrong in my understanding here...

I'm trying to define c methods to replace particular methods of the mappedlist
class (only replacing those methods which are called frequently by the walker
engine).  The strange thing is that, when called as a method, the self arg is
_not_ being passed.  Now, of course, I can work around that by wrapping each
function in a python method that appends (self,) to the args, but... well, that
obviates much of the performance benefit of writing in C.

For those who are interested in this kind of stuff, looks like you can create
static C methods (i.e. don't require a self arg) pretty simply, just write them
as below and leave off the first 'O' in the format string.

So, could anyone tell me what how one writes builtin methods that can be used
something like this (incidentally, the ext/embed doc could use a section
explaining how to do this I'd guess):

8<____________ mappedlist.py _________
from mcf.walker import mappedlist
import MappedListHelper

class MappedList( mappedlist.MappedList) :
        '''
        Partially extended version of MappedList using the
        MappedListHelper extension module.  Final goal
        would be to put this in the main module, using
        if statements to decide whether to define the python
        implementations or to use the C versions
        '''
        append = MappedListHelper.append
        last = MappedListHelper.last
        __setslice__ = MappedListHelper.setslice
        lastof = MappedListHelper.lastof

8<_____________ MappedListHelper.c _______
...
static PyObject *
mappedlist_last( PyObject *self, PyObject *args ) {
        /* local vars */
        PyObject *lastmap, *retval, *transform, *selfobj;
        char * gi;
        long id;

        /* initialise vars */
        retval = NULL; /* value to return, defaults to None */
        transform = NULL ;

        /* self.variable initialisation */
        if ( self ) {
                printf( "Self Passed to last\n" );
                selfobj = self;
                /* parse args */
                if (!PyArg_ParseTuple(args, "s|O", &gi, &transform)) {
                        return NULL;
                }
        } else {
                printf( "Self Not Passed to last\n" );
// following only works if you call  q.append( q, 'this', "hello")
                if (!PyArg_ParseTuple(args, "Os|O", &selfobj, &gi, &transform))
{
                        return NULL;
                }
        }
        lastmap = PyObject_GetAttrString( selfobj, "lastmap" );

        /* succeeded in parsing args */

        if ( PyMapping_HasKeyString( lastmap, gi ) == 1 ) {
                retval = PySequence_GetItem( PyMapping_GetItemString( lastmap,
gi ), 0 );
        } /* if was no key, use None as retval */
        if ( retval && transform ) {
                id = (long)&retval;
                if (PyMapping_HasKey( transform, PyInt_FromLong( id)  ) == 1 ) {
                        retval = PyObject_GetItem( transform, PyInt_FromLong(
id) );
                } // /* should raise a KeyError if didn't find transform
value... */
        }
        if ( !retval ) {
                retval = Py_None;
        }
        Py_INCREF( retval );
        return retval;

Quote:
}

________________________________
 M i k e   C .  F l e t c h e r


http://www.*-*-*.com/ ~mcfletch/
________________________________
 Design, Consultation, Training



Wed, 08 Nov 2000 03:00:00 GMT  
 Builtin methods versus Builtin Functions -- help!

Quote:

> Something is going wrong in my understanding here...

> I'm trying to define c methods to replace particular methods of the mappedlist
> class (only replacing those methods which are called frequently by the walker
> engine).  The strange thing is that, when called as a method, the self arg is
> _not_ being passed.  Now, of course, I can work around that by wrapping each
> function in a python method that appends (self,) to the args, but... well, that
> obviates much of the performance benefit of writing in C.

> For those who are interested in this kind of stuff, looks like you can create
> static C methods (i.e. don't require a self arg) pretty simply, just write them
> as below and leave off the first 'O' in the format string.

That's on the todo list for some future version of Python, I guess.
For the time being, the most effective way of getting C methods to work
is by wrapping them into a Python function. Those get magically turned
into bound methods when being requested through the class and then pass
self as first argument.

Another approach would be using ExtensionClasses (see
http://www.digicool.com) for that purpose.

--
Marc-Andre Lemburg
----------------------------------------------------------------------
             | Python Pages:  http://starship.skyport.net/~lemburg/  |
              -------------------------------------------------------



Wed, 08 Nov 2000 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. i need help on builtin function math

2. Pickler that handles modules, functions, and builtin functions -- done

3. generic functions to redefine builtin functions - how ?

4. "builtin" methods

5. How to exchange parts of builtin moudules/methods permanently using C-API

6. DECIMAL Builtin Function

7. Regina vs IBM REXX: builtin functions

8. How to get the Callable of builtin functions

9. builtin functions

10. filterout() builtin function

11. why is open() a builtin function?

12. Name od builtin functions: is there a rule ??

 

 
Powered by phpBB® Forum Software