handling a (python) list to fit in a char** argument

LukenShiro <[email protected]> Sat, 11 Dec 2010 14:41:19 +0100
Newsgroups gmane.comp.python.ctypes
Message-ID <[email protected]>
Hello,

I'm developing a LGPL-licensed wrapper to XForms-toolkit, using ctypes
(a wonderful library, indeed!).

A C function to be wrapped has a "char **" argument, so I've
ctypes.POINTER(ctypes.c_char_p) as an .argtypes
In the wrapping python function I think to use a "list of strings",
corresponding to X11-Pixmap (.xpm file) <*> (as you can see it can
have a variable number of elements and each element is a string who can
have a variable number of chars.

I'd use the following function to do a preliminary type check and
to operate a data conversion from one type to another:

§ def convert_to_ptr_string(paramname):
§    """ Converts paramname (list of str) to a ctypes pointer to
c_char_p """
§    if isinstance(paramname, list):     # list of str
§        for idx in range(0, len(paramname)):
§            if not isinstance(paramname[idx], basestring):
§                # every part must be a str
§                .... raise a TypeError .....
§        myarray = (ctypes.c_char_p * len(paramname))()
§        for idx in range(0, len(paramname)):
§            myarray[idx] = ctypes.c_char_p(paramname[idx])
§        retv = ctypes.POINTER(myarray)
§        return retv
§    else:
§        .... raise a TypeError .....

But, when called, I get:

Traceback (most recent call last):
  File "./test_flbitmap.py", line 300, in test_fl_set_pixmap_data_ok
    xfl.fl_set_pixmap_data(self.pxmobj, self.xpmdatalist)
  File "/usr/lib64/python2.6/site-packages/xformslib/flbitmap.py", line
406, in fl_set_pixmap_data
    pxpmdatalist = library.convert_to_ptr_string(xpmdatalist)
  File "/usr/lib64/python2.6/site-packages/xformslib/library.py", line
  266, in convert_to_ptr_string
    retv = cty.POINTER(myarray)
 TypeError: must be a ctypes type

So 'myarray[idx] = cty.c_char_p(paramname[idx])' seems to give the same
result as 'myarray[idx] = paramname[idx]' (same error)

Am I missing anything obvious? How can I resolve this one?
TIA. Regards



<*> my list can be this one:
mylist = ["16 16 2 1 0 0", "       s none  m none  c none", \
	".      s iconColor3    m black c red", \
	"                ",
	"  ..............",
	" .............. ",
	"   ..           ",
	"  ..            ",
	"  ..            ",
	"  ..            ",
	" ..  . . . .... ",
	" ..  . . . .....",
	" ..  . . . . . .",
	"...  . . . . . .",
	"...  ..... . . .",
	"...   ...  . . .",
	"...   . .  . . .",
	"                ",
	"                "]

transposing this .xpm (c-like) file:

_______mini-twm.xpm_______
/* XPM */
static char * mini_twm_xpm[] = {
/* width height ncolors cpp [x_hot y_hot] */
"16 16 2 1 0 0",
/* colors */
"       s none  m none  c none",
".      s iconColor3    m black c red",
/* pixels */
"                ",
"  ..............",
" .............. ",
"   ..           ",
"  ..            ",
"  ..            ",
"  ..            ",
" ..  . . . .... ",
" ..  . . . .....",
" ..  . . . . . .",
"...  . . . . . .",
"...  ..... . . .",
"...   ...  . . .",
"...   . .  . . .",
"                ",
"                "};
_____________________


-- 
GNU/Linux * Slackware64 current
LU #210970 SU #12583 LM #98222/#412913


------------------------------------------------------------------------------
Oracle to DB2 Conversion Guide: Learn learn about native support for PL/SQL,
new data types, scalar functions, improved concurrency, built-in packages, 
OCI, SQL*Plus, data movement tools, best practices and more.
http://p.sf.net/sfu/oracle-sfdev2dev