See: Redirect stdout, stderr to string/stream C++ (embedded) - #4 by stevengj
PS. What library are you importing that does this? Calling display, as opposed to e.g. show(stdout, "text/plain", x), might be a bug. How display shows an object depends on the environment.