bpo-33144: Fix choosing random.Random._randbelow implementation. (GH-6563)

random() takes precedence over getrandbits() if defined later
in the class tree.
This commit is contained in:
Serhiy Storchaka 2018-05-08 15:45:15 +03:00 committed by GitHub
parent d54cfb160c
commit ec1622d56c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 74 additions and 26 deletions

View file

@ -102,18 +102,16 @@ class Random(_random.Random):
ranges.
"""
if (cls.random is _random.Random.random) or (
cls.getrandbits is not _random.Random.getrandbits):
# The original random() builtin method has not been overridden
# or a new getrandbits() was supplied.
# The subclass can use the getrandbits-dependent implementation
# of _randbelow().
cls._randbelow = cls._randbelow_with_getrandbits
else:
# There's an overridden random() method but no new getrandbits(),
# so the subclass can only use the getrandbits-independent
# implementation of _randbelow().
cls._randbelow = cls._randbelow_without_getrandbits
for c in cls.__mro__:
if '_randbelow' in c.__dict__:
# just inherit it
break
if 'getrandbits' in c.__dict__:
cls._randbelow = cls._randbelow_with_getrandbits
break
if 'random' in c.__dict__:
cls._randbelow = cls._randbelow_without_getrandbits
break
def seed(self, a=None, version=2):
"""Initialize internal state from hashable object.