ctypes question

Mark Tolonen metolone+gmane at gmail.com
Wed Dec 15 00:53:36 EST 2010


> "News Wombat" <newswombat at gmail.com> wrote in message 
> news:413f5a8f-69a0-4351-acc2-18d7edda8149 at j3g2000vbp.googlegroups.com...
> On Dec 11, 12:59 pm, MrJean1 <mrje... at 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





More information about the Python-list mailing list