Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
9616a82e
Commit
9616a82e
authored
Apr 21, 2017
by
bladebryan
Committed by
Serhiy Storchaka
Apr 22, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpo-29960 _random.Random corrupted on exception in setstate(). (#1019)
parent
1a5856bf
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
13 additions
and
1 deletion
+13
-1
Lib/test/test_random.py
Lib/test/test_random.py
+5
-0
Misc/ACKS
Misc/ACKS
+1
-0
Misc/NEWS
Misc/NEWS
+3
-0
Modules/_randommodule.c
Modules/_randommodule.c
+4
-1
No files found.
Lib/test/test_random.py
View file @
9616a82e
...
...
@@ -423,6 +423,7 @@ class MersenneTwister_TestBasicOps(TestBasicOps, unittest.TestCase):
self
.
assertRaises
(
ValueError
,
self
.
gen
.
setstate
,
(
1
,
None
,
None
))
def
test_setstate_middle_arg
(
self
):
start_state
=
self
.
gen
.
getstate
()
# Wrong type, s/b tuple
self
.
assertRaises
(
TypeError
,
self
.
gen
.
setstate
,
(
2
,
None
,
None
))
# Wrong length, s/b 625
...
...
@@ -436,6 +437,10 @@ class MersenneTwister_TestBasicOps(TestBasicOps, unittest.TestCase):
self
.
gen
.
setstate
((
2
,
(
1
,)
*
624
+
(
625
,),
None
))
with
self
.
assertRaises
((
ValueError
,
OverflowError
)):
self
.
gen
.
setstate
((
2
,
(
1
,)
*
624
+
(
-
1
,),
None
))
# Failed calls to setstate() should not have changed the state.
bits100
=
self
.
gen
.
getrandbits
(
100
)
self
.
gen
.
setstate
(
start_state
)
self
.
assertEqual
(
self
.
gen
.
getrandbits
(
100
),
bits100
)
# Little trick to make "tuple(x % (2**32) for x in internalstate)"
# raise ValueError. I cannot think of a simple way to achieve this, so
...
...
Misc/ACKS
View file @
9616a82e
...
...
@@ -1110,6 +1110,7 @@ Milan Oberkirch
Pascal Oberndoerfer
Jeffrey Ollie
Adam Olsen
Bryan Olson
Grant Olson
Koray Oner
Piet van Oostrum
...
...
Misc/NEWS
View file @
9616a82e
...
...
@@ -317,6 +317,9 @@ Extension Modules
Library
-------
- bpo-29960: Preserve generator state when _random.Random.setstate()
raises an exception. Patch by Bryan Olson.
- bpo-29802: Fixed reference counting in module-level struct functions when
pass arguments of wrong type.
...
...
Modules/_randommodule.c
View file @
9616a82e
...
...
@@ -348,6 +348,7 @@ random_setstate(RandomObject *self, PyObject *state)
int
i
;
unsigned
long
element
;
long
index
;
uint32_t
new_state
[
N
];
if
(
!
PyTuple_Check
(
state
))
{
PyErr_SetString
(
PyExc_TypeError
,
...
...
@@ -364,7 +365,7 @@ random_setstate(RandomObject *self, PyObject *state)
element
=
PyLong_AsUnsignedLong
(
PyTuple_GET_ITEM
(
state
,
i
));
if
(
element
==
(
unsigned
long
)
-
1
&&
PyErr_Occurred
())
return
NULL
;
self
->
state
[
i
]
=
(
uint32_t
)
element
;
new_
state
[
i
]
=
(
uint32_t
)
element
;
}
index
=
PyLong_AsLong
(
PyTuple_GET_ITEM
(
state
,
i
));
...
...
@@ -375,6 +376,8 @@ random_setstate(RandomObject *self, PyObject *state)
return
NULL
;
}
self
->
index
=
(
int
)
index
;
for
(
i
=
0
;
i
<
N
;
i
++
)
self
->
state
[
i
]
=
new_state
[
i
];
Py_RETURN_NONE
;
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment