473,555 Members | 2,365 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Python Function pointer in C

I'm trying to maintain the interface to KQueue for FreeBSD. At one
point, the C module stores an opaque object in a C structure such that
the opaque pointer can be used later by the application.

In Python, I want to say something like:

kev = [KEvent(1000, filter=EVFILT_T IMER, flags=EV_ADD|EV _ONESHOT,
udata=lambda x: getJob(x, con))]
kq.event(kev, 0, 0)

and later, this is used by

kev = kq.event(None, 1, 0)
print "Kevent Triggered! %s" % str(kev)

# Call event's udata
kev.udata(kev)

.... so this doesn't work. Specifically:

[3:4:304]dgilbert@canoe: ~/devel/SoftGrabber> python
Python 2.3.4 (#2, Nov 2 2004, 16:03:35)
[GCC 3.4.2 [FreeBSD] 20040728] on freebsd5
Type "help", "copyright" , "credits" or "license" for more information.
import kqsyscall
from kqsyscall import *
kq = kqueue()
kev = kevent(1000, EVFILT_TIMER, EV_ADD+EV_ONESH OT, 0, 0, lambda x: x+1)
kev <KQEvent ident=1000 filter=-7 flags=11 fflags=0 data=0 udata=0x81b4fb4 > kev.udata <function <lambda> at 0x81b4fb4> kev.udata(1)

Segmentation fault (core dumped)

Now the code in the C module (kqsyscall above) defines the kevent()
return as the following object:

static struct memberlist KQEvent_memberl ist[] = {
{"ident", T_UINT, OFF(e.ident)},
{"filter", T_SHORT, OFF(e.filter)},
{"flags", T_USHORT, OFF(e.flags)},
{"fflags", T_UINT, OFF(e.fflags)},
{"data", T_INT, OFF(e.data)},
{"udata", T_OBJECT, OFF(e.udata)},
{NULL} /* Sentinel */
};

And it also defines various support functions.

How do I define "udata" such that I can stuff the lambda reference in
there?

Now... I've wondered if this is a reference counting issue --- but
this still happens when I use the name of a module global function as
udata.

Dave.

--
=============== =============== =============== =============== =============== =
|David Gilbert, Independent Contractor. | Two things can only be |
|Mail: da**@daveg.ca | equal if and only if they |
|http://daveg.ca | are precisely opposite. |
=============== =============== =============== ============GLO =============== =
Jul 18 '05 #1
1 2636
David Gilbert wrote:
I'm trying to maintain the interface to KQueue for FreeBSD. At one
point, the C module stores an opaque object in a C structure such that
the opaque pointer can be used later by the application.
In this case, a Py_IncRef/DecRef pair is definitely required - the 'borrowed'
reference from the Python function call only lasts until the function returns.
You need your own reference if you're going to hang on to the pointer.
How do I define "udata" such that I can stuff the lambda reference in
there?
PyObject* should be fine so long as the references are handled correctly.
Now... I've wondered if this is a reference counting issue --- but
this still happens when I use the name of a module global function as
udata.


I'd suggest adding the IncRef/DecRef pair and trying it out again. I can't see
anything else obviously wrong in the fragments you've posted.

If that was the sole problem though, I would have expected the same as you - for
the module global function to work correctly.

Sorry I can't be more help. . .

Cheers,
Nick.
Jul 18 '05 #2

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

Similar topics

1
4234
by: JW | last post by:
Hi, Below is a description of what I would like to do. Would someone tell me if it is possible and if so, how? I have an array (presumably 'large') that is mallocced in a C function and its values are initialized in that same function. I would like to pass just the pointer to the beginning of the array
25
2107
by: Allenabethea | last post by:
Jython is 100% java coded python. Python objects work with javacompilers and Virtual machines. I am a hobby programmer so the technical reasons are probably beyond me... Why is python incompatible with .net? Why can't python be coded in C#? I see the the MONO project on linux/unix is advancing. .Net seems to be a juggernaut especially if...
30
3414
by: Christian Seberino | last post by:
How does Ruby compare to Python?? How good is DESIGN of Ruby compared to Python? Python's design is godly. I'm wondering if Ruby's is godly too. I've heard it has solid OOP design but then I've also heard there are lots of weird ways to do some things kinda like Perl which is bad for me. Any other ideas?
4
2614
by: Edmond Rusjan | last post by:
Hi All, I'd like to use Python-2.3.4 on OSF1 V4.0, but have trouble installing. With a plain "./configure; make" build, I cannot import socket. If I uncomment the socketmodule in Modules/Setup, the build fails. Using gcc 3.4.2. Had no problem on OSF1 V5.1, Linux and Sun. Have not been able to find reference to a similar problem in...
0
1712
by: Jeff | last post by:
I'm trying to pass a proxy class instance (SWIG generated) of CClass, to a python callback function from C++. The proxy class instance of CClass is created from a pointer to the C++ class CClass. Using the code below, I receive the error message: "AttributeError: 'PySwigObject' object has no attribute 'GetName'" The python callback...
2
4433
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c */ #include <time.h>
9
4980
by: grbgooglefan | last post by:
I am trying to pass a C++ object to Python function. This Python function then calls another C++ function which then uses this C++ object to call methods of that object's class. I tried something like this, but it did not work, gave core dump. class myclass { public: myclass(){}; ~myclass(){};
8
1314
by: Kurda Yon | last post by:
I would like to know what are advantages of Python in comparison with C ++? In which cases and why Python can be a better tool than C++? Thank you!
0
1020
by: zhouyangpku | last post by:
Dear friends: I'm trying to install python on irix 6, the lab server. It fail at ' make ' procedure for Modules/posixmodule.c. I've just configured the prefix for my own path and no other option changed. make clean didn't help. Here it is: gcc -c -fno-strict-aliasing -DNDEBUG -g -O3 -Wall -Wstrict- prototypes -I. -IInclude -I./Include...
0
7622
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7547
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8060
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7588
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7904
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6176
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5171
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3575
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2037
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.