473,395 Members | 1,863 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,395 software developers and data experts.

ctypes question about call by reference

Hi,

hopefully someone with some ctypes experience can help me. I guess
this is a trivial task again, but I have been googling, reading,
experimenting the whole afternoon without any success.

I have a given C function signature:

char *MagickGetException(MagickWand *wand,ExceptionType *severity)

- ExceptionType is an enum
- MagickWand is somewhat strange, but so far it works fine without any
type mangling.

How would I wrap this thing using ctypes? Can anybody help me with that?

Best regards,
Oliver

--
Oliver Andrich <ol************@gmail.com--- http://roughbook.de/
Sep 24 '06 #1
5 2000
Oliver Andrich <ol************@gmail.comwrote:
- ExceptionType is an enum
- MagickWand is somewhat strange, but so far it works fine without any
type mangling.

How would I wrap this thing using ctypes? Can anybody help me with that?
First thing first: you have to identify how ExceptionType and MagickWand
are composed of. Then you can start wrapping them following the
instructions in the tutorial:
http://docs.python.org/lib/ctypes-ctypes-tutorial.html

--
Lawrence - http://www.oluyede.org/blog
"Nothing is more dangerous than an idea
if it's the only one you have" - E. A. Chartier
Sep 24 '06 #2
On 9/24/06, Lawrence Oluyede <rh****@myself.comwrote:
Oliver Andrich <ol************@gmail.comwrote:
- ExceptionType is an enum
- MagickWand is somewhat strange, but so far it works fine without any
type mangling.

How would I wrap this thing using ctypes? Can anybody help me with that?

First thing first: you have to identify how ExceptionType and MagickWand
are composed of. Then you can start wrapping them following the
instructions in the tutorial:
http://docs.python.org/lib/ctypes-ctypes-tutorial.html
Well, what I learned so far from the documentation, which I already
have read more then once today, is that there is no example for an
enum in this situation. But looking at pygame-sdl and another project,
it looks like the enum is just an c_int or c_long.

And concerning MagickWand, I think I have learned, that I don't need
to define the structure, if I don't want to access it. And I don't
want to, cause this is handled by the library itself. Another point in
this context is also, that it is not defined in the header files
distributed with the library, but only in the source from which the
library is built. As the file is named magick-wand-private.h it is
also not meant to be edited.

Is it at all possbile to use a struct without defining it with ctypes?
Is it okay, to just use the int which is returned by default? Most of
my library works with that configuration, so I thought it is working.

Based on this, can you give me some more hints?

Best regards,
Oliver
--
Oliver Andrich <ol************@gmail.com--- http://roughbook.de/
Sep 24 '06 #3
Oliver Andrich <ol************@gmail.comwrote:
Well, what I learned so far from the documentation, which I already
have read more then once today, is that there is no example for an
enum in this situation. But looking at pygame-sdl and another project,
it looks like the enum is just an c_int or c_long.
The documentation explains how to build an enum to pass to a function:
http://docs.python.org/lib/ctypes-st...es-unions.html

See the first sentence.

class ExceptionType(Union):
_fields_ =[list_of_tuples]
Is it at all possbile to use a struct without defining it with ctypes?
If you want to use it you have to define it somewhere...
Is it okay, to just use the int which is returned by default? Most of
my library works with that configuration, so I thought it is working.
I see your functions returns a "char *" so I don't understand what are
you saying here...
Based on this, can you give me some more hints?
It's not really clear :-)

--
Lawrence - http://www.oluyede.org/blog
"Nothing is more dangerous than an idea
if it's the only one you have" - E. A. Chartier
Sep 24 '06 #4
After a walk outside, some fresh air around my nose and some time to
relax, I finally found out how to do, what I want to do.

First, define the argument types and the result type.
_dll.MagickGetException.argtypes = (c_long, POINTER(c_long))
_dll.MagickGetException.restype = c_char_p

Second, write correct code as it is documented. :)

def get_magick_exception(wand):
severity = c_long()
description = _dll.MagickGetException(wand, byref(severity))
return (severity, description)

And thats all. I just did the call to POINTER(c_long) in the wrong
location. Now I do it as documented, and I get everything I want.

I can btw live perfectly without definining what actually a MagickWand
is, cause from the point of the developer just using the library and
documentation, he doesn't know about the actual structure of it. The
definition is hidden the C source of the library, and is not
documented in the public interface.

Thanks and Regards,
Oliver.

--
Oliver Andrich <ol************@gmail.com--- http://roughbook.de/
Sep 24 '06 #5
At Sunday 24/9/2006 15:49, Lawrence Oluyede wrote:
Is it at all possbile to use a struct without defining it with ctypes?

If you want to use it you have to define it somewhere...
If it's an "opaque" thing, totally managed by the external code, yes
- treat it as a pointer.
That is, if you never have to access its fields, or create/destroy it
in the "python" code (using an external function would be ok).

Gabriel Genellina
Softlab SRL

__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas

Sep 26 '06 #6

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

Similar topics

8
by: achrist | last post by:
I'm aving some trouble getting callbacks from a Delphi DLL back to python working through ctypes. The call from python to the DLL works fine. In the Delphi(5) code: type TCallbackFunc =...
2
by: Thomas Heller | last post by:
It's release day ;-) ctypes 0.6.3 released ===================== Overview 'ctypes' is a Python package to create and manipulate C data types in Python, and to call functions in dynamic...
2
by: zapazap | last post by:
Dear Snake Charming Gurus, (Was: http://mail.python.org/pipermail/python-list/2004-January/204454.html) First, a thank you to Tim Golden, Thomas Heller, and Mark Hammond for your earlier help...
1
by: Thomas Heller | last post by:
ctypes 0.9.1 released - Sept 14, 2004 ===================================== Overview ctypes is a ffi (Foreign Function Interface) package for Python 2.3 and higher. ctypes allows to call...
19
by: Thomas Heller | last post by:
ctypes 0.9.2 released - Oct 28, 2004 ==================================== Overview ctypes is a ffi (Foreign Function Interface) package for Python 2.3 and higher. ctypes allows to call...
0
by: follower | last post by:
This post is mostly Google-bait for anyone else that might want to compile SpiderMonkey ( libjs / libjs.so / libjs.dylib ) for OS X (10.4.5 in my case) and then use it with Python's ctypes. I can't...
4
by: Richard Jones | last post by:
Currently ctypes can't play well with any C code that requires use of setjmp as part of its API. libpng is one of those libraries. Can anyone think of a reasonable solution to this? Perhaps...
6
by: Mudcat | last post by:
Hi, I can't figure out why ctypes won't load the DLL I need to use. I've tried everything I can find (and the ctypes website is down at the moment). Here's what I've seen so far. I've added...
2
by: luis | last post by:
I'm using ctypes to call a fortran dll from python. I have no problems passing integer and double arryas, but I have an error with str arrys. For example: ..... StringVector = c_char_p *...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...

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.