diff options
author | Martin v. Löwis <martin@v.loewis.de> | 2007-12-03 19:20:02 (GMT) |
---|---|---|
committer | Martin v. Löwis <martin@v.loewis.de> | 2007-12-03 19:20:02 (GMT) |
commit | 6b449f4f2bd86c104a8b57547428eb9bb3a182b0 (patch) | |
tree | 6ca8ed3d8b4ab7fa6175675e78fba0c481d30d10 /Lib/random.py | |
parent | 2ec7415db5ad63c4e4b27ee793214071454f6fe5 (diff) | |
download | cpython-6b449f4f2bd86c104a8b57547428eb9bb3a182b0.zip cpython-6b449f4f2bd86c104a8b57547428eb9bb3a182b0.tar.gz cpython-6b449f4f2bd86c104a8b57547428eb9bb3a182b0.tar.bz2 |
Issue #1727780: Support loading pickles of random.Random objects created
on 32-bit systems on 64-bit systems, and vice versa. As a consequence
of the change, Random pickles created by Python 2.6 cannot be loaded
in Python 2.5.
Diffstat (limited to 'Lib/random.py')
-rw-r--r-- | Lib/random.py | 15 |
1 files changed, 13 insertions, 2 deletions
diff --git a/Lib/random.py b/Lib/random.py index ed87ddd..ec0f386 100644 --- a/Lib/random.py +++ b/Lib/random.py @@ -83,7 +83,7 @@ class Random(_random.Random): """ - VERSION = 2 # used by getstate/setstate + VERSION = 3 # used by getstate/setstate def __init__(self, x=None): """Initialize an instance. @@ -120,9 +120,20 @@ class Random(_random.Random): def setstate(self, state): """Restore internal state from object returned by getstate().""" version = state[0] - if version == 2: + if version == 3: version, internalstate, self.gauss_next = state super(Random, self).setstate(internalstate) + elif version == 2: + version, internalstate, self.gauss_next = state + # In version 2, the state was saved as signed ints, which causes + # inconsistencies between 32/64-bit systems. The state is + # really unsigned 32-bit ints, so we convert negative ints from + # version 2 to positive longs for version 3. + try: + internalstate = tuple( long(x) % (2**32) for x in internalstate ) + except ValueError, e: + raise TypeError, e + super(Random, self).setstate(internalstate) else: raise ValueError("state with version %s passed to " "Random.setstate() of version %s" % |