Wrapping static member functions

445 views
Skip to first unread message

ptao

unread,
Jul 9, 2010, 2:43:35 PM7/9/10
to cython-users
I'm trying to use cython to wrap an existing c/c++ library. One
problem I'm having right now is with static member functions. For
example, the following:

namespace Test
{
class Test
{
public:
static int getInt();
};
}

If I try the following in my pyx:

cdef extern from "../c/test.h" namespace "Test":
cdef cppclass Test:
static int getInt()

def pgetInt():
print Test.getInt()

I get the following error:
test/cython/test.pyx:3:19: Syntax error in C variable declaration
building 'test' extension

If I leave off the static modifier in the pyx, it generates the wrong
c code.

What's the proper way to do this?

Lionel Data

unread,
Jul 12, 2010, 4:10:51 AM7/12/10
to cython...@googlegroups.com
I think I would do some dirty hack to make cython believe getInt() is just a simple function in a namespace. I'm not sure if cython has a notion of "static" methods or not.

I can't test right now, but I would do something like this:

cdef extern from "../c/test.h" namespace "Test::Test":
    int getInt()

def pgetInt():
   print getInt()

I would be interested in a cleaner solution though :)

--
Lionel

2010/7/9 ptao <phill...@gmail.com>

Robert Bradshaw

unread,
Jul 12, 2010, 1:19:39 PM7/12/10
to cython...@googlegroups.com
On Mon, Jul 12, 2010 at 1:10 AM, Lionel Data <lione...@gmail.com> wrote:
> I think I would do some dirty hack to make cython believe getInt() is just a
> simple function in a namespace. I'm not sure if cython has a notion of
> "static" methods or not.

No, it doesn't have that notion, at least not for C++.

> I can't test right now, but I would do something like this:
>
> cdef extern from "../c/test.h" namespace "Test::Test":
>     int getInt()
>
> def pgetInt():
>    print getInt()
>
> I would be interested in a cleaner solution though :)

Yep. That's the only way to do it now.

- Robert

ptao

unread,
Jul 12, 2010, 6:20:48 PM7/12/10
to cython-users
Thanks guys, that solution is a bit hacky but it's working.

On Jul 12, 10:19 am, Robert Bradshaw <rober...@math.washington.edu>
wrote:
Reply all
Reply to author
Forward
0 new messages