Pass std::ostream to C++ function via Cython.

60 views
Skip to first unread message

Sandra Hicks

unread,
Nov 25, 2016, 6:33:26 AM11/25/16
to cython-users
I want to wrap the following C++ call with Cython:
    void printVector(std::ostream& stream, const Vector& vector)
which prints a Vector corresponding to the given ostream.
I want to call this function in a Wrapper.pyx:

cdef class Wrapper:
    cdef cppMyClass.MyClass *ptr

    ...

    def printVector(self, ..., WrapperVector vec):
        self.ptr.printVector(..., vec.vecptr)

How can I give the ostream from Python to C++ as a parameter? Do I need to wrap C++ std classes for io? My online research has not given me a clue yet.

Robert Bradshaw

unread,
Nov 26, 2016, 1:49:21 AM11/26/16
to cython...@googlegroups.com
Yes, you'll need to provide an ostream. Depending on how you're using
it, you could take it (appropriately wrapped) as a parameter, have it
be another member of Wrapper, or construct it on the fly.

This is one of those areas where duplicating the C++ API may not be
the most Pythonic. Instead, consider how you would write this in
Python and construct your wrapper to do the appropriate translation to
the C++ API.
Reply all
Reply to author
Forward
0 new messages