fixed (?) some problems with stringstreams and std::ends
This commit is contained in:
parent
07a3e56db1
commit
eaabc7ae3b
10 changed files with 57 additions and 50 deletions
|
|
@ -53,6 +53,7 @@ std::string rng_to_string(const eoRng& _rng)
|
|||
return os.str();
|
||||
}
|
||||
|
||||
|
||||
void rng_from_string(eoRng& _rng, std::string s)
|
||||
{
|
||||
#ifdef HAVE_SSTREAM
|
||||
|
|
@ -63,6 +64,21 @@ void rng_from_string(eoRng& _rng, std::string s)
|
|||
_rng.readFrom(is);
|
||||
}
|
||||
|
||||
struct RNG_pickle_suite : boost::python::pickle_suite
|
||||
{
|
||||
static
|
||||
boost::python::tuple getstate(const eoRng& _rng)
|
||||
{
|
||||
return boost::python::make_tuple(str(rng_to_string(_rng)));
|
||||
}
|
||||
static
|
||||
void setstate(eoRng& _rng, boost::python::tuple pickled)
|
||||
{
|
||||
std::string state = extract<std::string>(pickled[0]);
|
||||
rng_from_string(_rng, state);
|
||||
}
|
||||
};
|
||||
|
||||
int spin(eoRng& _rng, numeric::array values, double total)
|
||||
{
|
||||
if (total == 0.0)
|
||||
|
|
@ -97,6 +113,7 @@ void random_numbers()
|
|||
.def("to_string", rng_to_string)
|
||||
.def("from_string", rng_from_string)
|
||||
.def("roulette_wheel", spin)
|
||||
.def_pickle(RNG_pickle_suite())
|
||||
;
|
||||
|
||||
def("rng", get_rng, return_value_policy<reference_existing_object>());
|
||||
|
|
|
|||
|
|
@ -69,28 +69,7 @@ class TestPickling(unittest.TestCase):
|
|||
for i in range(10):
|
||||
rng().rand()
|
||||
|
||||
filename = tempfile.mktemp()
|
||||
file = open(filename, 'wb')
|
||||
pickler = cPickle.Pickler(file)
|
||||
|
||||
s = rng().to_string()
|
||||
|
||||
pickler.dump(s);
|
||||
del pickler
|
||||
file.close()
|
||||
|
||||
file = open(filename)
|
||||
|
||||
unpickler = cPickle.Unpickler(file)
|
||||
|
||||
s = unpickler.load()
|
||||
|
||||
rng2 = eoRng(1)
|
||||
rng2.from_string(s)
|
||||
|
||||
del unpickler
|
||||
file.close()
|
||||
os.remove(filename)
|
||||
rng2 = self.do_pickle(rng())
|
||||
|
||||
for i in range(100):
|
||||
a = rng().rand()
|
||||
|
|
@ -99,19 +78,7 @@ class TestPickling(unittest.TestCase):
|
|||
|
||||
def vParam(self,v):
|
||||
|
||||
filename = tempfile.mktemp()
|
||||
file = open(filename,'wb')
|
||||
pickler = cPickle.Pickler(file)
|
||||
|
||||
pickler.dump(v)
|
||||
del pickler
|
||||
file.close()
|
||||
file = open(filename)
|
||||
|
||||
unpickler = cPickle.Unpickler(file)
|
||||
|
||||
v2 = unpickler.load();
|
||||
|
||||
v2 = self.do_pickle(v);
|
||||
self.failUnlessEqual(v.value, v2.value)
|
||||
|
||||
def testValueParam(self):
|
||||
|
|
|
|||
Reference in a new issue