Re: ctypes question

2010-12-14 Thread News Wombat
On Dec 11, 12:59 pm, MrJean1 mrje...@gmail.com wrote:

 In general, for shared libraries, you need to define those first as
 prototype using ctypes.CFUNCTYPE() and then instantiate each prototype
 once supplying the necessary parameter flags using
 prototype(func_spec, tuple_of_param_flags).  See sections 15.16.2.3
 and 4 of the ctypes docs*.

I tried the cfuntype and proto steps, and it's not crashing now
(that's good), but now i'm just left with null pointers as a return
object.  I'm still working through all of the examples you sent.  They
were extremely helpful.  Here's where I'm at now...

What is strange is I can actually get smiGetNode to work if I don't
cfunctype/proto it.  If i do, nada.  however, the smiGetNextNode fails
no matter what, usually with a segfault, but depending on how i
construct it, sometimes a null pointer.

constants.py: http://pastebin.com/f3b4Wbf0
libsmi.py: http://pastebin.com/XgtpG6gr
smi.c (the actual function): http://pastebin.com/Pu2vabWM
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ctypes question

2010-12-14 Thread MrJean1
Try again after changing line 16 to

  sn = SmiGetNode(None, 1.3.6.1.2.1.2.2)

Because, SmiGetNode is a Python function which accepts Python objects
as arguments.  Passing is a ctypes object oid is incorrect.

/Jean


On Dec 14, 10:36 am, News Wombat newswom...@gmail.com wrote:
 On Dec 11, 12:59 pm, MrJean1 mrje...@gmail.com wrote:

  In general, for shared libraries, you need to define those first as
  prototype using ctypes.CFUNCTYPE() and then instantiate each prototype
  once supplying the necessary parameter flags using
  prototype(func_spec, tuple_of_param_flags).  See sections 15.16.2.3
  and 4 of the ctypes docs*.

 I tried the cfuntype and proto steps, and it's not crashing now
 (that's good), but now i'm just left with null pointers as a return
 object.  I'm still working through all of the examples you sent.  They
 were extremely helpful.  Here's where I'm at now...

 What is strange is I can actually get smiGetNode to work if I don't
 cfunctype/proto it.  If i do, nada.  however, the smiGetNextNode fails
 no matter what, usually with a segfault, but depending on how i
 construct it, sometimes a null pointer.

 constants.py:http://pastebin.com/f3b4Wbf0
 libsmi.py:http://pastebin.com/XgtpG6gr
 smi.c (the actual function):http://pastebin.com/Pu2vabWM

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ctypes question

2010-12-14 Thread Mark Tolonen
News Wombat newswom...@gmail.com wrote in message 
news:413f5a8f-69a0-4351-acc2-18d7edda8...@j3g2000vbp.googlegroups.com...

On Dec 11, 12:59 pm, MrJean1 mrje...@gmail.com wrote:

 In general, for shared libraries, you need to define those first as
 prototype using ctypes.CFUNCTYPE() and then instantiate each prototype
 once supplying the necessary parameter flags using
 prototype(func_spec, tuple_of_param_flags). See sections 15.16.2.3
 and 4 of the ctypes docs*.

I tried the cfuntype and proto steps, and it's not crashing now
(that's good), but now i'm just left with null pointers as a return
object.  I'm still working through all of the examples you sent.  They
were extremely helpful.  Here's where I'm at now...

What is strange is I can actually get smiGetNode to work if I don't
cfunctype/proto it.  If i do, nada.  however, the smiGetNextNode fails
no matter what, usually with a segfault, but depending on how i
construct it, sometimes a null pointer.

constants.py: http://pastebin.com/f3b4Wbf0
libsmi.py: http://pastebin.com/XgtpG6gr
smi.c (the actual function): http://pastebin.com/Pu2vabWM
--
http://mail.python.org/mailman/listinfo/python-list


constants.py, in SmiNode and SmiModule definitions:
  - Any field defined char* in C should be c_char_p not 
POINTER(c_char_p) (which is char**).


The function definition can be simplified, and 2nd argument corrected 
(c_char_p not POINTER(c_char_p)).  Python strings can be passed directly to 
c_char_p arguments.


  SmiGetNode = clibsmi.smiGetNode
  SmiGetNode.argtypes = [POINTER(SmiModule),c_char_p]
  SmiGetNode.restype = POINTER(SmiNode)
  oid = 1.3.6.1.2.1.2.2
  sn=SmiGetNode(None,oid)

Give these fixes a try...

-Mark


--
http://mail.python.org/mailman/listinfo/python-list


Re: ctypes question

2010-12-11 Thread MrJean1
It is not entirely clear what the functions and especially what their
signatures are in that C library clibsmi.

In general, for shared libraries, you need to define those first as
prototype using ctypes.CFUNCTYPE() and then instantiate each prototype
once supplying the necessary parameter flags using
prototype(func_spec, tuple_of_param_flags).  See sections 15.16.2.3
and 4 of the ctypes docs*.

Take a look the Python bindings** for the VLC library, the file called
vlc.py***.  The function _Cfunction is used to create the Python
callable for each C function in that VLC library.  All the Python
callables are in the second half of the vlc.py file, starting at line
2600.

Hope this helps,

/Jean

*) http://docs.python.org/library/ctypes.html#foreign-functions

**) http://wiki.videolan.org/Python_bindings

***) http://git.videolan.org/?p=vlc/bindings/
python.git;a=tree;f=generated;b=HEAD


On Dec 10, 3:32 pm, News Wombat newswom...@gmail.com wrote:
 Hi everyone,

 I've been experimenting with the ctypes module and think it's great.
 I'm hitting a few snags though with seg faults.  I attached two links
 that holds the code.  The line i'm having problems with is this,

 sn=clibsmi.smiGetNextNode(pointer(sno),SMI_NODEKIND_ANY)

 It will work one time, and if I call it again with the result of the
 previous, even though the result (a c struct) looks ok, it will
 segfault.  I think it's a problem with pointers or maybe the function
 in the c library trying to change a string that python won't let it
 change.  I'm stuck, any tips would be appreciated.  Thanks, and Merry
 Christmas!

 constants.py:http://pastebin.com/HvngjzZN
 libsmi.py:http://pastebin.com/19C9kYEa

-- 
http://mail.python.org/mailman/listinfo/python-list


ctypes question

2010-12-10 Thread News Wombat
Hi everyone,

I've been experimenting with the ctypes module and think it's great.
I'm hitting a few snags though with seg faults.  I attached two links
that holds the code.  The line i'm having problems with is this,

sn=clibsmi.smiGetNextNode(pointer(sno),SMI_NODEKIND_ANY)

It will work one time, and if I call it again with the result of the
previous, even though the result (a c struct) looks ok, it will
segfault.  I think it's a problem with pointers or maybe the function
in the c library trying to change a string that python won't let it
change.  I'm stuck, any tips would be appreciated.  Thanks, and Merry
Christmas!

constants.py: http://pastebin.com/HvngjzZN
libsmi.py: http://pastebin.com/19C9kYEa
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ctypes question

2010-12-10 Thread Mark Tolonen


News Wombat newswom...@gmail.com wrote in message 
news:2abdd9b3-66ec-4125-a5f8-41315008c...@l17g2000yqe.googlegroups.com...

Hi everyone,

I've been experimenting with the ctypes module and think it's great.
I'm hitting a few snags though with seg faults.  I attached two links
that holds the code.  The line i'm having problems with is this,

sn=clibsmi.smiGetNextNode(pointer(sno),SMI_NODEKIND_ANY)

It will work one time, and if I call it again with the result of the
previous, even though the result (a c struct) looks ok, it will
segfault.  I think it's a problem with pointers or maybe the function
in the c library trying to change a string that python won't let it
change.  I'm stuck, any tips would be appreciated.  Thanks, and Merry
Christmas!

constants.py: http://pastebin.com/HvngjzZN
libsmi.py: http://pastebin.com/19C9kYEa


Well, I can't run your code, but I think you should pass the original sn 
pointer from smiGetNode() and not a pointer(sno).  The values are not the 
same and the library probably relies on passing the original pointer back 
into smiGetNextNode.  sn.contents returns a new SmiNode object so its 
pointer will be different.


-Mark


--
http://mail.python.org/mailman/listinfo/python-list


Re: ctypes question

2010-04-15 Thread Brendan Miller
On Wed, Apr 14, 2010 at 12:12 PM, Mark Dickinson dicki...@gmail.com wrote:
 On Apr 14, 7:09 pm, Brendan Miller catph...@catphive.net wrote:
 I'm using python 2.5.2.

 I have a ctypes function with argtypes like this:

 _create_folder.argyptes = [c_void_p, c_int]

 Is that line a cut-and-paste?  If so, try 'argtypes' instead of
 'argyptes'.  :)

Woops! Well, nice to know that ctypes works as I expect.
-- 
http://mail.python.org/mailman/listinfo/python-list


ctypes question

2010-04-14 Thread Brendan Miller
I'm using python 2.5.2.

I have a ctypes function with argtypes like this:

_create_folder.argyptes = [c_void_p, c_int]

The issue I am having is that I can call it like this

_create_folder(some_pointer, asdf)

and it won't raise a TypeError. Why would it accept a string for an
integer argument?

I didn't see this behavior documented when I read through
http://docs.python.org/library/ctypes.html, maybe I'm just missing
it... if that's the case a reference would be appreciated.

Brendan
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ctypes question

2010-04-14 Thread Mark Dickinson
On Apr 14, 7:09 pm, Brendan Miller catph...@catphive.net wrote:
 I'm using python 2.5.2.

 I have a ctypes function with argtypes like this:

 _create_folder.argyptes = [c_void_p, c_int]

Is that line a cut-and-paste?  If so, try 'argtypes' instead of
'argyptes'.  :)

 The issue I am having is that I can call it like this

 _create_folder(some_pointer, asdf)

 and it won't raise a TypeError. Why would it accept a string for an
 integer argument?

I get the expected TypeError in 2.5.4 (OS X) (using printf, for lack
of anything better):

Python 2.5.4 (r254:67916, Feb 11 2010, 00:50:55)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type help, copyright, credits or license for more information.
 from ctypes import *
 libc = CDLL(libc.dylib)
 libc
CDLL 'libc.dylib', handle 8fe46768 at 1a2330
 printf = libc.printf
 printf.argtypes = [c_char_p, c_int]
 printf(%d\n, 53)
53
3
 printf(%d\n, asdf)
Traceback (most recent call last):
  File stdin, line 1, in module
ctypes.ArgumentError: argument 2: type 'exceptions.TypeError': wrong
type

--
Mark
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ctypes question about call by reference

2006-09-26 Thread Gabriel Genellina

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


-- 
http://mail.python.org/mailman/listinfo/python-list

ctypes question about call by reference

2006-09-24 Thread Oliver Andrich
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 [EMAIL PROTECTED] --- http://roughbook.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ctypes question about call by reference

2006-09-24 Thread Lawrence Oluyede
Oliver Andrich [EMAIL PROTECTED] wrote:
 - 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Re: ctypes question about call by reference

2006-09-24 Thread Oliver Andrich
On 9/24/06, Lawrence Oluyede [EMAIL PROTECTED] wrote:
 Oliver Andrich [EMAIL PROTECTED] wrote:
  - 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 [EMAIL PROTECTED] --- http://roughbook.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ctypes question about call by reference

2006-09-24 Thread Lawrence Oluyede
Oliver Andrich [EMAIL PROTECTED] wrote:
 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-structures-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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Re: ctypes question about call by reference

2006-09-24 Thread Oliver Andrich
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 [EMAIL PROTECTED] --- http://roughbook.de/
-- 
http://mail.python.org/mailman/listinfo/python-list