I have the following c++ function that sets an integer using a string.
#include <sstream>
#include <string>
#include <iostream>
using namespace std;
extern "C" {
int a() {
int number;
string value("100");
std::istringstream strm(value);
strm >> number;
if (strm.fail()) {
cout << "Ouch!" << endl;
}
else {
cout << "Number set to:" << number << endl;
};
return (int)strm.bad();
}
}
int main(int argc, char **argv)
{
a();
}
If I compile this as a program it works.
$ g++ ./streamtest.cc -o streamtest;./streamtest
Number set to:100
But if I call the same function from ctypes it does not set the integer and the "strm" is left in a "bad" state.
$ g++ -shared streamtest.cc -o libstreamtest.so
$ python -c "import ctypes;a = ctypes.CDLL('libstreamtest.so').a();print 'Got [%s] from a()' %a"
Ouch!
Got [1] from a()
This got me puzzled. How can I make this function work under ctypes?