xml2py problem with CvQualifiedType

John Popplewell <[email protected]>
Newsgroups gmane.comp.python.ctypes
Message-ID <[email protected]>
Hi,

I've been trying to generate bindings for ImageMagick-6.6.1-Q8 on
Windows for Python 2.5.4. After installing 'gccxml 0.9' and SVN revision
81278 I managed to get h2xml.py to work using this command-line:

h2xml.py /dev/pymagick/include/wand/MagickWand.h -oMagickWand.xml -I/dev/pymagick/include -c

However, when I tried the next step I ran into problems:

xml2py.py MagickWand.xml -omagickwand.py -llib/CORE_RL_magick_.dll -llib/CORE_RL_wand_.dll
# unresolved alias: ssize_t = long
# unresolved alias: sys_errlist = _sys_errlist
# unresolved alias: environ = _environ
# unresolved alias: sys_nerr = _sys_nerr
# unresolved alias: _W64 = __w64
# unresolved alias: inline = __inline
# unresolved alias: MB_CUR_MAX = __mb_cur_max
Traceback (most recent call last):
  File "C:\Python25\scripts\xml2py.py", line 6, in <module>
    sys.exit(main())
  File "C:\Python25\Lib\site-packages\ctypeslib\xml2py.py", line 199, in main
    types=options.kind)
  File "C:\Python25\lib\site-packages\ctypeslib\codegen\codegenerator.py", line 874, in generate_code
    loops = gen.generate_code(items)
  File "C:\Python25\lib\site-packages\ctypeslib\codegen\codegenerator.py", line 785, in generate_code
    loops = self.generate_items(items)
  File "C:\Python25\lib\site-packages\ctypeslib\codegen\codegenerator.py", line 774, in generate_items
    self.generate_all(sorted(items, self.cmpitems))
  File "C:\Python25\lib\site-packages\ctypeslib\codegen\codegenerator.py", line 758, in generate_all
    self.generate(item)
  File "C:\Python25\lib\site-packages\ctypeslib\codegen\codegenerator.py", line 754, in generate
    mth(item)
  File "C:\Python25\lib\site-packages\ctypeslib\codegen\codegenerator.py", line 402, in Structure
    self.generate(struct.get_body())
  File "C:\Python25\lib\site-packages\ctypeslib\codegen\codegenerator.py", line 754, in generate
    mth(item)
  File "C:\Python25\lib\site-packages\ctypeslib\codegen\codegenerator.py", line 561, in StructureBody
    pack = calc_packing(body.struct, fields)
  File "C:\Python25\lib\site-packages\ctypeslib\codegen\codegenerator.py", line 125, in calc_packing
    _calc_packing(struct, fields, pack, isStruct)
  File "C:\Python25\lib\site-packages\ctypeslib\codegen\codegenerator.py", line 98, in _calc_packing
    s, a = storage(f.typ)
  File "C:\Python25\lib\site-packages\ctypeslib\codegen\codegenerator.py", line 76, in storage
    return int(t.size), int(t.align)
AttributeError: 'CvQualifiedType' object has no attribute 'size'

I rooted about, stuck some prints in, slept on it and found these lines in
ctypeslib/codegen/codegenerator.py in the function storage():

if isinstance(t, typedesc.Typedef):
    return storage(t.typ)

which looks like the behavior required when handling a CvQualifiedType. I'd
describe it as looking up the base-type from the fancy CvQualifiedType, but I'm
pretty much making all this up :-)

Anyway, here's my fix:

Index: ctypeslib/codegen/codegenerator.py
===================================================================
--- ctypeslib/codegen/codegenerator.py  (revision 81278)
+++ ctypeslib/codegen/codegenerator.py  (working copy)
@@ -65,7 +65,7 @@

 def storage(t):
     # return the size and alignment of a type
-    if isinstance(t, typedesc.Typedef):
+    if isinstance(t, typedesc.Typedef) or isinstance(t, typedesc.CvQualifiedType):
         return storage(t.typ)
     elif isinstance(t, typedesc.ArrayType):
         s, a = storage(t.typ)

Works for me, makes some kind of sense and now I can finally get on with using an
up to date version of ImageMagick in Python :-)

best regards,
John Popplewell.


------------------------------------------------------------------------------
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.