473,785 Members | 2,639 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Function References

Greetings,

I'm trying to wrap a function in a C library as a compiled C Python
module. Everything is going great, but I've hit a snag. There's a
function in the form of this:

First the typedef:
typedef void(*FPtr_Devi ceMessageHandle r) (const DeviceMessage, const
char*);

Then the actual function prototype:
FPtr_DeviceMess ageHandler
RegisterDeviceM essageHandler(F Ptr_DeviceMessa geHandler);

Whenever this USB device I'm using generates a message, it's then sent
to that function whose reference you passed to
RegisterDeviceM essageHandler() . Here's an example:

void testfunc() {
printf("test");
}
....on to main()
RegisterDeviceM essageHandler(& testfunc)

So I've defined a similar function on my C module to do just this,
using the passed function reference to allow the device to send
messages to a Python function of the user's choice. I'm just not sure
how to do this, and the tutorials I've been digging through don't
offer any help. I've found some functions that look promising, but
can't figure out how to cast the function reference from Python into
something the C RegisterDevice. .. function can handle. Here's what
I've got:

static PyObject *
Py1_RegisterDev iceMessageHandl er(PyObject *self, PyObject *args)
{
PyObject *handle, *parsed;

if(!PyArg_Parse Tuple(args, "O", handle)) {
return NULL;
}

parsed = PyMethod_Functi on(handle);
I1_RegisterDevi ceMessageHandle r(PyMethod_Func tion(handle));

Py_RETURN_TRUE;
} // end Py1_RegisterDev iceMessageHandl er()

This fails since PyMethod_Functi on returns a PyObject. Is there a way
to cast this to something generic? Casting to (void*) didn't seem to
work.

Thanks in advance!
Jul 31 '08 #1
14 1321
sq***********@g mail.com wrote:
Greetings,

I'm trying to wrap a function in a C library as a compiled C Python
module. Everything is going great, but I've hit a snag. There's a
function in the form of this:

First the typedef:
typedef void(*FPtr_Devi ceMessageHandle r) (const DeviceMessage, const
char*);

Then the actual function prototype:
FPtr_DeviceMess ageHandler
RegisterDeviceM essageHandler(F Ptr_DeviceMessa geHandler);

Whenever this USB device I'm using generates a message, it's then sent
to that function whose reference you passed to
RegisterDeviceM essageHandler() . Here's an example:

void testfunc() {
printf("test");
}
...on to main()
RegisterDeviceM essageHandler(& testfunc)

So I've defined a similar function on my C module to do just this,
using the passed function reference to allow the device to send
messages to a Python function of the user's choice. I'm just not sure
how to do this, and the tutorials I've been digging through don't
offer any help. I've found some functions that look promising, but
can't figure out how to cast the function reference from Python into
something the C RegisterDevice. .. function can handle. Here's what
I've got:

static PyObject *
Py1_RegisterDev iceMessageHandl er(PyObject *self, PyObject *args)
{
PyObject *handle, *parsed;

if(!PyArg_Parse Tuple(args, "O", handle)) {
return NULL;
}

parsed = PyMethod_Functi on(handle);
I1_RegisterDevi ceMessageHandle r(PyMethod_Func tion(handle));

Py_RETURN_TRUE;
} // end Py1_RegisterDev iceMessageHandl er()

This fails since PyMethod_Functi on returns a PyObject. Is there a way
to cast this to something generic? Casting to (void*) didn't seem to
work.

Thanks in advance!
May I suggest you move to ctypes for wrapping? It's easier, pure python and
callbacks are already built-in.

Diez
Jul 31 '08 #2
Hello Diez.
May I suggest you move to ctypes for wrapping? It's easier, pure python and
callbacks are already built-in.
I'm pretty new to extending Python in C, I don't understand what
you're saying. Are there any examples or a brief explanation/URL you
could point me to?

Jul 31 '08 #3
sq***********@g mail.com wrote:
Hello Diez.
>May I suggest you move to ctypes for wrapping? It's easier, pure python
and callbacks are already built-in.

I'm pretty new to extending Python in C, I don't understand what
you're saying. Are there any examples or a brief explanation/URL you
could point me to?
Is google dead today?

http://www.google.com/search?q=pytho...UTF-8&oe=UTF-8

First hit.

Ctypes is a since python2.5 built-in module that allows to declare
interfaces to C-libraries in pure python. You declare datatypes and
function prototypes, load a DLL/SO and then happily work with it. No C, no
compiler, no refcounts, no nothing.

And you can pass python-functions as callbacks.

See the module docs for examples.

Diez
Jul 31 '08 #4
Ctypes is a since python2.5 built-in module that allows to declare
interfaces to C-libraries in pure python. You declare datatypes and
function prototypes, load a DLL/SO and then happily work with it. No C, no
compiler, no refcounts, no nothing.

And you can pass python-functions as callbacks.
The first sentence (and some really crummy licensing restrictions
imposed by the library distributor) alone here excludes this as a
valid option for this particular case, I definitely need Python 2.4
support.

So the question comes back around to being how is this same desired
behavior duplicated in the Python/C API?
Jul 31 '08 #5
sq***********@g mail.com wrote:
>Ctypes is a since python2.5 built-in module that allows to declare
interfaces to C-libraries in pure python. You declare datatypes and
function prototypes, load a DLL/SO and then happily work with it. No C,
no compiler, no refcounts, no nothing.

And you can pass python-functions as callbacks.

The first sentence (and some really crummy licensing restrictions
imposed by the library distributor) alone here excludes this as a
valid option for this particular case, I definitely need Python 2.4
support.
How much more liberal can it get than MIT-licensed?

"""
to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
"""

But then, if you insist, go down the hard road.

Diez

Jul 31 '08 #6
Diez B. Roggisch wrote:
sq***********@g mail.com wrote:
>>Ctypes is a since python2.5 built-in module that allows to declare
interfaces to C-libraries in pure python. You declare datatypes and
function prototypes, load a DLL/SO and then happily work with it. No C,
no compiler, no refcounts, no nothing.

And you can pass python-functions as callbacks.

The first sentence (and some really crummy licensing restrictions
imposed by the library distributor) alone here excludes this as a
valid option for this particular case, I definitely need Python 2.4
support.

How much more liberal can it get than MIT-licensed?

"""
to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
"""
Sorry, missed the paragraph

"""
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
"""

Still, AFAIK MIT is pretty liberal.

Diez
Jul 31 '08 #7
How much more liberal can it get than MIT-licensed?

Again, the licensing issue is everything to do with the original
library distributor, NOT ctypes.
But then, if you insist, go down the hard road.
Irrelevant and unnecessary. If you don't want to help, don't please
don't reply.
Jul 31 '08 #8
sq***********@g mail.com wrote:
>How much more liberal can it get than MIT-licensed?

Again, the licensing issue is everything to do with the original
library distributor, NOT ctypes.
I read library distributor as "ctypes-library distributor" because it is
3rd-party under 2.4. Which was the reason I quotet it's MIT-license.

And it escapes me what accessing the lib from ctypes is any different than
from your own compiled code.
>But then, if you insist, go down the hard road.

Irrelevant and unnecessary. If you don't want to help, don't please
don't reply.
I take the freedom to do so as I see fit - this is usenet...

Diez
Jul 31 '08 #9
On Jul 31, 10:47*am, "Diez B. Roggisch" <de...@nospam.w eb.dewrote:
I take the freedom to do so as I see fit - this is usenet...
Fine, then keep beating a dead horse by replying to this thread with
things that do nobody any good. It seems like there are a lot better
way to waste time, though.

The Python/C API can get me back further without reliance on third-
party libraries than ctypes. It also isn't subject to the quirks that
ctypes is on platforms other than Windows (the target application runs
on Windows, Mac, and eventually Linux once the original distributor
has drivers for the device). I'm not even sure ctypes could load the
lib/driver the distributor packaged.

So really, I appreciate the option in ctypes, it's good stuff. But
it's not for this project.

Once again, the original question stands for anyone who has experience
with the Python/C API callbacks.
Jul 31 '08 #10

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

Similar topics

3
14952
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) { document.images.src = eval("mt" +menu+ ".src") } alert("imgOff_hidemenu"); hideMenu=setTimeout('Hide(menu,num)',500);
3
1355
by: RobG | last post by:
I am playing with a script that will allow columns of a table to be moved by dragging them left or right. To do this with functions is fairly straight forward, however after looking at Richard Cornford's version of Table Highlighter I decided to do it using an object. Richard's basic code structure is: var SomeObject = (function() { function someFn01(){
18
9047
by: Steve | last post by:
Hi I have a really weird problem and any assistance would be welcome. I have developed an app in Access 2002. The app runs perfectly on the development machine. I have packaged the app using the Microsoft XP Developer Packaging Wizard (Service Pack 1). The 1st 3 releases of the app ran perfectly on site.
19
2515
by: Deniz Bahar | last post by:
Hi, I would like to call one of my functions the exact name as an existing C library function (for example K&R2 exercises asks me to make an atof function). If I don't include the header with the declaration for the C library function (stdlib.h in this case) then define/declare my own function with the same name, am I safe? It seems to work on my compiler, but I wonder if this is portable or even acceptable? thx
28
4336
by: Larax | last post by:
Best explanation of my question will be an example, look below at this simple function: function SetEventHandler(element) { // some operations on element element.onclick = function(event) {
0
9645
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10336
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10155
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10095
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9953
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8978
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5383
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4054
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
2
3655
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.