Using C ++ Standard Streams in Binary Mode via Swig / Python on MinGW
I have a library written in C ++ that uses standard streams to read and write objects. I also have a Python interface built with Swig that I use to access the library. Everything works fine on Linux, but on Windows (on MinGW) it seems impossible to use standard C ++ streams in binary mode through the Python interface. If streams are used in text mode, additional CR characters break the library. The standard streams are completely wrapped inside the C ++ library, meaning I don't pass them through the Python interface.
I have tried a solution that works in C ++ programs that use the library, i.e. adding lines
#include <fcntl.h>
int _CRT_fmode = _O_BINARY;
inside the block
%{
#define SWIG_FILE_WITH_INIT
...
%}
in Swig file libfoo.i but it has no effect. Also adding function
void set_binary_mode() {
assert(stdin == freopen(0, "rb", stdin));
assert(stdout == freopen(0, "wb", stdout));
}
and calling it at the beginning of the Python program doesn't seem to do anything. Also at the beginning of the Python program the lines are executed
sys.stdin = os.fdopen(sys.stdin.fileno(), 'rb', 0)
sys.stdout = os.fdopen(sys.stdout.fileno(), 'wb', 0)
and / or
if sys.platform == "win32":
import msvcrt
msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
msvcrt.setmode(sys.stderr.fileno(), os.O_BINARY)
do not work. Any combination of the previous one does not work.
I am compiling a C ++ library and Swig / Python bindings on MinGW32 (version loaded in 20120426) on Windows XP version 2002. The versions of the tools I am using are Python (2.7.3), Swig (2.0.8) and gcc ( 4.6.2).
source to share
No one has answered this question yet
See similar questions:
or similar: