[SciPy-dev] How to get extensions to affect local state

Pat Miller pnmiller at pacbell.net
Fri Feb 8 14:38:02 EST 2002


Another topic that came up at Python 10

Eric wants weave.inline to affect the local state
such that

sum = 0
n = 10
print 'before',sum
weave.inline("sum = 0; for(i=0;i<n;++i) sum+=i;",['sum'])
print 'after',sum

would output

before 0
after 55

It is a bit tricky as you cannot just use python tricks to update
the local dictionary for affect:

That is if inside weave.inline, it makes a call to locals() (or
rather gets it from the caller's frame) you can't make changes.

For example:

def f(n):
    i = 3
    L = locals()
    print locals()
    L[i] = 99
    print locals()
    print 'but i is still',i

would output
{ 'i' : 3 }
{ 'i' : 99 }
but i is still 3

All is not lost!!!!

You can root out the data offsets of local and global data items
from within Python's C API.  I've attached a sample of extension
code that shows how to get and manipulate Python locals/globals
from within the extension.

Pat
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: functionpeek.c
URL: <http://mail.python.org/pipermail/scipy-dev/attachments/20020208/f0b5b5e6/attachment.c>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: test.py
URL: <http://mail.python.org/pipermail/scipy-dev/attachments/20020208/f0b5b5e6/attachment.ksh>


More information about the SciPy-Dev mailing list