summaryrefslogtreecommitdiffstats
path: root/Lib/random.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2018-05-08 12:45:15 (GMT)
committerGitHub <noreply@github.com>2018-05-08 12:45:15 (GMT)
commitec1622d56c80d15740f7f8459c9a79fd55f5d3c7 (patch)
tree746876d28f338443003d4de44d8d8ddaaada016b /Lib/random.py
parentd54cfb160c626626394e2f171d3ccfe03309f34e (diff)
downloadcpython-ec1622d56c80d15740f7f8459c9a79fd55f5d3c7.zip
cpython-ec1622d56c80d15740f7f8459c9a79fd55f5d3c7.tar.gz
cpython-ec1622d56c80d15740f7f8459c9a79fd55f5d3c7.tar.bz2
bpo-33144: Fix choosing random.Random._randbelow implementation. (GH-6563)
random() takes precedence over getrandbits() if defined later in the class tree.
Diffstat (limited to 'Lib/random.py')
-rw-r--r--Lib/random.py22
1 files changed, 10 insertions, 12 deletions
diff --git a/Lib/random.py b/Lib/random.py
index 0ed5511..1e0dcc8 100644
--- a/Lib/random.py
+++ b/Lib/random.py
@@ -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.