Calling external libraries

57 views
Skip to first unread message

Carlos

unread,
Nov 15, 2010, 9:00:45 PM11/15/10
to cython-users
Hi

Is it possible to call an external library without having to wrap it?

I'm trying to wrap a library that uses another library. So i need to
use this second library but only on the Cython file, i don't need to
call it in my Python code.

Thanks.

Robert Bradshaw

unread,
Nov 15, 2010, 9:18:38 PM11/15/10
to cython...@googlegroups.com

Yes. Just use it directly in the Cython file the same as you're using
the first library (and, of course, don't forget to link it). Wrapping
is only necessary if you want to expose it to Python.

- Robert

Carlos

unread,
Nov 16, 2010, 5:49:04 PM11/16/10
to cython-users
Hi again,

Thanks for your fast reply.
I'm just not sure if i'm doing things right.
To clarify things a bit, the second library i talked about is Qt4 and
i need to use some types from Qt4 like QString. This is how i'm
importing:

cdef extern from "qstring.h":
cdef cppclass QString:
QString()

It works, but do i need to add all the methods provided by QString or
is there another way?

Thanks again.

On 16 nov, 02:18, Robert Bradshaw <rober...@math.washington.edu>
wrote:

Robert Bradshaw

unread,
Nov 16, 2010, 6:15:26 PM11/16/10
to cython...@googlegroups.com
On Tue, Nov 16, 2010 at 2:49 PM, Carlos <carlos...@gmail.com> wrote:
> Hi again,
>
> Thanks for your fast reply.
> I'm just not sure if i'm doing things right.
> To clarify things a bit, the second library i talked about is Qt4 and
> i need to use some types from Qt4 like QString. This is how i'm
> importing:
>
> cdef extern from "qstring.h":
>    cdef cppclass QString:
>        QString()

Yep.

> It works, but do i need to add all the methods provided by QString or
> is there another way?

Only the ones you need to use.

- Robert

Carlos

unread,
Nov 16, 2010, 11:31:48 PM11/16/10
to cython-users
Hi,

Sorry to bother you again, but i'm having trouble with the Qt4 types.
I have this:

cdef extern from "Instance.h":
cdef cppclass VlcInstance:
VlcInstance(QString)

cdef class PyVlcInstance:
cdef VlcInstance *thisptr # hold a C++ instance which we're
wrapping
def __cinit__(self, args):
#cdef args = args
self.thisptr = new VlcInstance(args)
def __dealloc__(self):
del self.thisptr

I'm getting the error: "Cannot convert Python object to 'QString' ".

Thanks.

On 16 nov, 23:15, Robert Bradshaw <rober...@math.washington.edu>
wrote:

Robert Bradshaw

unread,
Nov 17, 2010, 12:24:13 AM11/17/10
to cython...@googlegroups.com
On Tue, Nov 16, 2010 at 8:31 PM, Carlos <carlos...@gmail.com> wrote:
> Hi,
>
> Sorry to bother you again, but i'm having trouble with the Qt4 types.
> I have this:
>
> cdef extern from "Instance.h":
>    cdef cppclass VlcInstance:
>        VlcInstance(QString)
>
> cdef class PyVlcInstance:
>    cdef VlcInstance *thisptr      # hold a C++ instance which we're
> wrapping
>    def __cinit__(self, args):
>        #cdef args = args
>        self.thisptr = new VlcInstance(args)
>    def __dealloc__(self):
>        del self.thisptr
>
> I'm getting the error: "Cannot convert Python object to  'QString' ".

VlcInstance takes a QString, but args is a Python object. You have to
create a QString before passing it into the VlcInstance constructor.
(If QString is just a special kind of string, it probably has a
constructor taking a char*, right? The usual caveats about unicode vs.
byte strings apply.)

- Robert

Carlos

unread,
Nov 17, 2010, 11:16:42 PM11/17/10
to cython-users
Hi,

I tried again, but i can't seem to find a way to send the QString
variable to the Application's constructor neither convert it
afterwards.
Here's the small code i have:

cdef class PyVlcInstance:
cdef QString *word
def __cinit__(self, args, parent):
self.word.append(args) #Cannot convert Python object to
'QString'
self.word = new QString("hello")#Compilation errors*
def __dealloc__(self):
del self.thisptr

*
/home/carlos/Instance.cpp:727: multiple definition of `initInstance'
build/temp.linux-i686-2.6/Instance.o:/home/carlos/Instance.cpp:727:
first defined here
build/temp.linux-i686-2.6/Instance.o: In function
`__Pyx_AddTraceback':
/home/carlos/Instance.cpp:1337: multiple definition of
`__pyx_module_is_main_Instance'
build/temp.linux-i686-2.6/Instance.o:/home/carlos/Instance.cpp:1337:
first defined here
build/temp.linux-i686-2.6/Instance.o:(.data.rel.local+0x0): multiple
definition of `__pyx_type_8Instance_PyVlcInstance'
build/temp.linux-i686-2.6/Instance.o:(.data.rel.local+0x0): first
defined here

Can you give me some more help?

On 17 nov, 05:24, Robert Bradshaw <rober...@math.washington.edu>
wrote:

Robert Bradshaw

unread,
Nov 17, 2010, 11:21:19 PM11/17/10
to cython...@googlegroups.com

Could you perhaps post all your files (including your .cpp file)?

- Robert

Carlos

unread,
Nov 17, 2010, 11:38:19 PM11/17/10
to cython-users
Okay, which .cpp are you talking about?

########## pyx #################

cdef extern from "qstring.h":
cdef cppclass QString:
QString()
QString(char*)
append(QString)

cdef extern from "qlist.h":
cdef cppclass QList:
QList()
QList(QList)

cdef extern from "qobject.h":
cdef cppclass QObject:
QObject()

cdef extern from "Instance.h":
cdef cppclass VlcInstance:
VlcInstance(QString, QObject)

cdef class PyVlcInstance:
#cdef VlcInstance *thisptr # hold a C++ instance which we're
wrapping
#cdef QList *arguments
cdef QString *word
def __cinit__(self, args, parent):
#self.word.append(args)
self.word = new QString("hello")
#self.thisptr = new VlcInstance(args, parent)
def __dealloc__(self):
del self.thisptr

######## setup.py ##################

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

ext_modules = [
Extension(
"Instance", # name of extension
["Instance.pyx", "Instance.cpp"], # filename of our Cython
source
language="c++", # this causes Cython to create C++
source
include_dirs=["/usr/include/qt4/Qt", "/usr/include/qt4/", "/usr/
include/vlc-qt"], # usual stuff
libraries=["stdc++", "QtCore"], # ditto
#extra_link_args=[...], # if needed
)
]


setup(
name = 'vlc-qt',
ext_modules = ext_modules,
cmdclass = {'build_ext': build_ext}
)

On 18 nov, 04:21, Robert Bradshaw <rober...@math.washington.edu>
wrote:

Robert Bradshaw

unread,
Nov 17, 2010, 11:42:31 PM11/17/10
to cython...@googlegroups.com

Instance.pyx will automatically generate Instance.cpp, don't list the
latter. This is probably what your problem is.

Carlos

unread,
Nov 18, 2010, 12:23:17 AM11/18/10
to cython-users
It worked, thanks. I'm still struggling with Qt4 types though. For
example, how do i return a QString?

On 18 nov, 04:42, Robert Bradshaw <rober...@math.washington.edu>
wrote:
Message has been deleted

Robert Bradshaw

unread,
Nov 18, 2010, 12:31:49 AM11/18/10
to cython...@googlegroups.com
On Wed, Nov 17, 2010 at 9:23 PM, Carlos <carlos...@gmail.com> wrote:
> It worked, thanks. I'm still struggling with Qt4 types though. For
> example, how do i return a QString?

Well, what is a QString? Is it some kind of a string with special
structure attached? The two most obvious solutions to me would be to
either provide a QString wrapper, or convert it into a Python unicode
object and return that (the latter if the conversion is lossless).

- Robert


> On 18 nov, 04:42, Robert Bradshaw <rober...@math.washington.edu>
> wrote:

Carlos

unread,
Nov 18, 2010, 1:16:58 AM11/18/10
to cython-users
QStrings are basically unicode strings. For now i'm only testing with
QString, but i'll need some other types like QList, QObject, etc.

I tried converting from QString to a Python string like this:
str(self.word) or unicode(self.word)

self.word is defined like:
cdef QString *word

On 18 nov, 05:31, Robert Bradshaw <rober...@math.washington.edu>
wrote:

Robert Bradshaw

unread,
Nov 18, 2010, 1:28:14 AM11/18/10
to cython...@googlegroups.com
On Wed, Nov 17, 2010 at 10:16 PM, Carlos <carlos...@gmail.com> wrote:
> QStrings are basically unicode strings. For now i'm only testing with
> QString, but i'll need some other types like QList, QObject, etc.
>
> I tried converting from QString to a Python string like this:
> str(self.word) or unicode(self.word)
>
> self.word is defined like:
> cdef QString *word

Cython doesn't know anything about QString, so doesn't natively know
how to turn a QString* into a Python unicode object. Cython can
however convert between Python objects and char*. Surely there's some
QString method to get an encoded representation of the string as a
char*, say UTF-8, which you can then turn into a Python unicode. See
http://docs.cython.org/src/tutorial/strings.html for more than enough
information regarding how to properly handle unicode.

The same goes for QList, etc. You'll probably want to write utility
methods that convert back and forth between Python objects and all of
these types. I'd imagine such a thing would be generically useful to
others as well.

- Robert

Reply all
Reply to author
Forward
0 new messages