diff options
author | Oren Milman <orenmn@gmail.com> | 2017-09-28 07:50:01 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2017-09-28 07:50:01 (GMT) |
commit | d780b2d588e68bd7047ef5d1f04e36da38b7a350 (patch) | |
tree | d673be6c4ee93bd5c462fdd0d3f520226a09d623 | |
parent | db50ba7c72bd5f0617c50df3f1a1bb8b26955882 (diff) | |
download | cpython-d780b2d588e68bd7047ef5d1f04e36da38b7a350.zip cpython-d780b2d588e68bd7047ef5d1f04e36da38b7a350.tar.gz cpython-d780b2d588e68bd7047ef5d1f04e36da38b7a350.tar.bz2 |
bpo-31478: Fix an assertion failure in random.seed() in case a seed has a bad __abs__() method. (#3596)
-rw-r--r-- | Lib/test/test_random.py | 11 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Core and Builtins/2017-09-15-09-13-07.bpo-31478.o06iKD.rst | 2 | ||||
-rw-r--r-- | Modules/_randommodule.c | 7 |
3 files changed, 18 insertions, 2 deletions
diff --git a/Lib/test/test_random.py b/Lib/test/test_random.py index 5d12aee..e2db723 100644 --- a/Lib/test/test_random.py +++ b/Lib/test/test_random.py @@ -430,6 +430,17 @@ class MersenneTwister_TestBasicOps(TestBasicOps, unittest.TestCase): ['0x1.b0580f98a7dbep-1', '0x1.84129978f9c1ap-1', '0x1.aeaa51052e978p-2', '0x1.092178fb945a6p-2']) + def test_bug_31478(self): + # There shouldn't be an assertion failure in _random.Random.seed() in + # case the argument has a bad __abs__() method. + class BadInt(int): + def __abs__(self): + return None + try: + self.gen.seed(BadInt()) + except TypeError: + pass + def test_bug_31482(self): # Verify that version 1 seeds are unaffected by hash randomization # when the seeds are expressed as bytes rather than strings. diff --git a/Misc/NEWS.d/next/Core and Builtins/2017-09-15-09-13-07.bpo-31478.o06iKD.rst b/Misc/NEWS.d/next/Core and Builtins/2017-09-15-09-13-07.bpo-31478.o06iKD.rst new file mode 100644 index 0000000..bbeb810 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2017-09-15-09-13-07.bpo-31478.o06iKD.rst @@ -0,0 +1,2 @@ +Fix an assertion failure in `_random.Random.seed()` in case the argument has a +bad ``__abs__()`` method. Patch by Oren Milman. diff --git a/Modules/_randommodule.c b/Modules/_randommodule.c index 747a547..51677f8 100644 --- a/Modules/_randommodule.c +++ b/Modules/_randommodule.c @@ -259,8 +259,11 @@ random_seed(RandomObject *self, PyObject *args) * So: if the arg is a PyLong, use its absolute value. * Otherwise use its hash value, cast to unsigned. */ - if (PyLong_Check(arg)) - n = PyNumber_Absolute(arg); + if (PyLong_Check(arg)) { + /* Calling int.__abs__() prevents calling arg.__abs__(), which might + return an invalid value. See issue #31478. */ + n = PyLong_Type.tp_as_number->nb_absolute(arg); + } else { Py_hash_t hash = PyObject_Hash(arg); if (hash == -1) |