1

I have a Cython extension which wraps a C++ library. The C++ library prints messages on stderr and I don't know how I can suppress it.

I tried to overwrite sys.stderr which did not help. I am looking for the solution of this problem for Python.

Here is a "minimal" example:

I have the following files:

suppress_stderr_test/
    test.h        # defines C++ function that prints on stderr
    cbindings.pxd # declares C++ function for Cython
    test.pyx      # defines a Python function that calls the C++ function
    setup.py      # compiles Cython wrapper
    main.py       # test script

Here are the contents of the files:

main.py:

from wrapper import my_verbose_function
my_verbose_function() # I want to suppress the output of that function

setup.py:

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

setup(
    name='wrapper',
    ext_modules=[
        Extension("wrapper",
                  sources=["cbindings.pxd", "test.pyx"],
                  language="c++",
                  include_dirs=["."],
                 ),
        ],
    cmdclass={'build_ext': build_ext},
)

test.pyx:

cimport cbindings as cb
def my_verbose_function():
    cb.myVerboseFunction()

cbindings.pxd:

cdef extern from "test.h":
    void myVerboseFunction()

test.h:

#include <iostream>
void myVerboseFunction() { std::cerr << "bla" << std::endl; }
Community
  • 1
  • 1
alfa
  • 2,990
  • 3
  • 22
  • 35
  • 2
    Does [this](http://stackoverflow.com/questions/5081657/how-do-i-prevent-a-c-shared-library-to-print-on-stdout-in-python) help? – Veedrac Apr 16 '14 at 16:27

0 Answers0