ctypes question

Discussion in 'Python' started by News Wombat, Dec 10, 2010.

  1. News Wombat

    News Wombat Guest

    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
     
    News Wombat, Dec 10, 2010
    #1
    1. Advertising

  2. News Wombat

    Mark Tolonen Guest

    "News Wombat" <> wrote in message
    news:...
    > 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
     
    Mark Tolonen, Dec 11, 2010
    #2
    1. Advertising

  3. News Wombat

    MrJean1 Guest

    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 <> 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
     
    MrJean1, Dec 11, 2010
    #3
  4. News Wombat

    News Wombat Guest

    On Dec 11, 12:59 pm, MrJean1 <> 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
     
    News Wombat, Dec 14, 2010
    #4
  5. News Wombat

    MrJean1 Guest

    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 <> wrote:
    > On Dec 11, 12:59 pm, MrJean1 <> 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
     
    MrJean1, Dec 15, 2010
    #5
  6. News Wombat

    Mark Tolonen Guest

    > "News Wombat" <> wrote in message
    > news:...
    > On Dec 11, 12:59 pm, MrJean1 <> 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
     
    Mark Tolonen, Dec 15, 2010
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Henk Punt
    Replies:
    0
    Views:
    397
    Henk Punt
    Jul 23, 2004
  2. Efrat Regev

    Newbie Question on ctypes

    Efrat Regev, Sep 21, 2005, in forum: Python
    Replies:
    1
    Views:
    267
    Sybren Stuvel
    Sep 21, 2005
  3. Oliver Andrich

    ctypes question about call by reference

    Oliver Andrich, Sep 24, 2006, in forum: Python
    Replies:
    5
    Views:
    381
    Gabriel Genellina
    Sep 26, 2006
  4. Tzury

    ctypes, windll question

    Tzury, Aug 14, 2007, in forum: Python
    Replies:
    0
    Views:
    303
    Tzury
    Aug 14, 2007
  5. Replies:
    0
    Views:
    503
Loading...

Share This Page