[PyKDE] Help on wrapping a C library
Huaicai Mo
huaicai at nanorex.com
Tue May 24 19:47:43 BST 2005
Hi Phil,
I am sending you the files: word.h, word.c, word.sip, configure.py, mytest.c
and clib_makefile(the make file used to create the C library and the test
program). After creating the C library, I added the line
/usr/local/lib
to the /etc/ld.so.conf file, and run:
install word*.so /usr/local/lib
ldconfig
I don't have a python script to test the extension module. I tested
interactively. Here is the snapshot:
Python 2.3.4 (#2, Aug 19 2004, 15:49:40) [GCC 3.4.1 (Mandrakelinux (Alpha
3.4.1-3mdk)] on linux2 Type "help", "copyright", "credits" or "license" for
more information.
>>> import word
>>> print word.anotherReverse("sadkfj;sadf")
fdas;jfkdas
>>> print word.__dict__
{'create_word': <built-in function create_word>, 'Word': <class
'word.Word'>, 'reverse': <built-in function reverse>, '__file__': 'word.so',
'anotherReverse': <built-in function anotherReverse>, '__name__': 'word',
'__doc__': None}
>>> w = word.create_word("How are you?")
>>>print w
<word.Word object at 0x4038fddc>
>>> print word.reverse(w)
Segmentation fault
Thank you very much!
Regards,
Huaicai
> -----Original Message-----
> From: Phil Thompson [mailto:phil at riverbankcomputing.co.uk]
> Sent: Tuesday, May 24, 2005 12:21 PM
> To: pykde at mats.imk.fraunhofer.de
> Cc: Huaicai Mo
> Subject: Re: [PyKDE] Help on wrapping a C library
>
> On Tuesday 24 May 2005 4:40 pm, Huaicai Mo wrote:
> > > -----Original Message-----
> > > From: Phil Thompson [mailto:phil at riverbankcomputing.co.uk]
> > > Sent: Tuesday, May 24, 2005 3:45 AM
> > > To: pykde at mats.imk.fraunhofer.de
> > >..........
> > > In your implementation of reverse() you are freeing the memory
> allocated
> > > to
> > > the Word structure and the word itself. Because the wrapper has
> ownership
> > > it
> > > will also try to free the memory which will result in the seg fault.
> > >
> > > Either remove the calls to free() or add /Transfer/ to the argument to
> > > reverse().
> > >
> > > Phil
> >
> > Thank you Phil for the reply. I did try as you suggested by removing the
> 2
> > free() calls in the reverse() or just simply implement reverse() by
> >
> > return word->the_word;
> >
> > I also tried adding the /Transfer/ annotation to the argument of
> reverse()
> > in the word.sip file without removing the 2 free() calls. I still get
> the
> > segmentation fault.
> >
> > BTW, here is one of the generated functions in sipwordWord.c that I
> guess
> > is used by the wrapper to free the memory:
> >
> > static void dealloc_Word(sipWrapper *sipSelf)
> > {
> > If(sipIsPyOwned(sipSelf))
> > sipFree(sipSelf -> u.cppPtr);
> > }
> >
> > I am not sure if the above function is able to free "the_word" member in
> > the "Word" structure, so I tried keeping "free(word->the_word);" in the
> > reverse() too, it doesn't help either. For the whole generated C files,
> > please see the attachment.
>
> Any memory pointed to by "the_word" member won't get freed. As of
> tonight's
> SIP snapshot you will be able to specify ctors and dtors in C structures
> to
> allow you to write handwritten code to deal with situations like this.
>
> There is a bug in the example in the documentation - create_word() should
> have
> a /Factory/ annotation. This should only cause a memory leak - not the
> problems you are having.
>
> Can you send me the word.c, word.h, word.sip you are currently using and a
> Python script that demonstrates the problem? (And a Makefile if you have
> one
> handy.)
>
> Phil
-------------- next part --------------
A non-text attachment was scrubbed...
Name: clib_makefile
Type: application/octet-stream
Size: 550 bytes
Desc: not available
Url : http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20050524/f0cdaf24/clib_makefile.obj
-------------- next part --------------
A non-text attachment was scrubbed...
Name: mytest.c
Type: application/octet-stream
Size: 369 bytes
Desc: not available
Url : http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20050524/f0cdaf24/mytest.obj
-------------- next part --------------
A non-text attachment was scrubbed...
Name: word.c
Type: application/octet-stream
Size: 1088 bytes
Desc: not available
Url : http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20050524/f0cdaf24/word.obj
-------------- next part --------------
A non-text attachment was scrubbed...
Name: word.h
Type: application/octet-stream
Size: 238 bytes
Desc: not available
Url : http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20050524/f0cdaf24/word-0001.obj
-------------- next part --------------
A non-text attachment was scrubbed...
Name: word.sip
Type: application/octet-stream
Size: 239 bytes
Desc: not available
Url : http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20050524/f0cdaf24/word-0002.obj
-------------- next part --------------
import os
import sipconfig
# The name of the SIP build file generated by SIP and used by the build
# system.
build_file = "word.sbf"
# Get the SIP configuration information.
config = sipconfig.Configuration()
# Run SIP to generate the code.
os.system(" ".join([config.sip_bin, "-c", ".", "-b", build_file, "word.sip"]))
# Create the Makefile.
makefile = sipconfig.SIPModuleMakefile(config, build_file)
# Add the library we are wrapping. The name doesn't include any platform
# specific prefixes or extensions (e.g. the "lib" prefix on UNIX, or the
# ".dll" extension on Windows).
makefile.extra_libs = ["word"]
# Generate the Makefile itself.
makefile.generate()
More information about the PyQt
mailing list