| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -348,6 +348,7 @@ def test_setstate_first_arg(self): | |||
| 348 | 348 | self.assertRaises(ValueError, self.gen.setstate, (1, None, None)) | |
| 349 | 349 | ||
| 350 | 350 | def test_setstate_middle_arg(self): | |
| 351 | + start_state = self.gen.getstate() | ||
| 351 | 352 | # Wrong type, s/b tuple | |
| 352 | 353 | self.assertRaises(TypeError, self.gen.setstate, (2, None, None)) | |
| 353 | 354 | # Wrong length, s/b 625 | |
@@ -361,6 +362,10 @@ def test_setstate_middle_arg(self): | |||
| 361 | 362 | self.gen.setstate((2, (1,)*624+(625,), None)) | |
| 362 | 363 | with self.assertRaises((ValueError, OverflowError)): | |
| 363 | 364 | self.gen.setstate((2, (1,)*624+(-1,), None)) | |
| 365 | + # Failed calls to setstate() should not have changed the state. | ||
| 366 | + bits100 = self.gen.getrandbits(100) | ||
| 367 | + self.gen.setstate(start_state) | ||
| 368 | + self.assertEqual(self.gen.getrandbits(100), bits100) | ||
| 364 | 369 | ||
| 365 | 370 | # Little trick to make "tuple(x % (2**32) for x in internalstate)" | |
| 366 | 371 | # raise ValueError. I cannot think of a simple way to achieve this, so | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1090,6 +1090,7 @@ Milan Oberkirch | |||
| 1090 | 1090 | Pascal Oberndoerfer | |
| 1091 | 1091 | Jeffrey Ollie | |
| 1092 | 1092 | Adam Olsen | |
| 1093 | + Bryan Olson | ||
| 1093 | 1094 | Grant Olson | |
| 1094 | 1095 | Koray Oner | |
| 1095 | 1096 | Piet van Oostrum | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -56,6 +56,9 @@ Extension Modules | |||
| 56 | 56 | Library | |
| 57 | 57 | ------- | |
| 58 | 58 | ||
| 59 | + - bpo-29960: Preserve generator state when _random.Random.setstate() | ||
| 60 | + raises an exception. Patch by Bryan Olson. | ||
| 61 | + | ||
| 59 | 62 | - bpo-30414: multiprocessing.Queue._feed background running | |
| 60 | 63 | thread do not break from main loop on exception. | |
| 61 | 64 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -313,6 +313,7 @@ random_setstate(RandomObject *self, PyObject *state) | |||
| 313 | 313 | int i; | |
| 314 | 314 | unsigned long element; | |
| 315 | 315 | long index; | |
| 316 | + PY_UINT32_T new_state[N]; | ||
| 316 | 317 | ||
| 317 | 318 | if (!PyTuple_Check(state)) { | |
| 318 | 319 | PyErr_SetString(PyExc_TypeError, | |
@@ -329,7 +330,7 @@ random_setstate(RandomObject *self, PyObject *state) | |||
| 329 | 330 | element = PyLong_AsUnsignedLong(PyTuple_GET_ITEM(state, i)); | |
| 330 | 331 | if (element == (unsigned long)-1 && PyErr_Occurred()) | |
| 331 | 332 | return NULL; | |
| 332 | - self->state[i] = (PY_UINT32_T)element; | ||
| 333 | + new_state[i] = (PY_UINT32_T)element; | ||
| 333 | 334 | } | |
| 334 | 335 | ||
| 335 | 336 | index = PyLong_AsLong(PyTuple_GET_ITEM(state, i)); | |
@@ -340,6 +341,8 @@ random_setstate(RandomObject *self, PyObject *state) | |||
| 340 | 341 | return NULL; | |
| 341 | 342 | } | |
| 342 | 343 | self->index = (int)index; | |
| 344 | + for (i = 0; i < N; i++) | ||
| 345 | + self->state[i] = new_state[i]; | ||
| 343 | 346 | ||
| 344 | 347 | Py_INCREF(Py_None); | |
| 345 | 348 | return Py_None; | |
| Back | FazBrowse Home | New Git URL |
0 commit comments