[pypy-svn] r77750 - pypy/branch/fast-forward/lib-python/modified-2.7.0

afa at codespeak.net afa at codespeak.net
Sun Oct 10 00:55:51 CEST 2010


Author: afa
Date: Sun Oct 10 00:55:49 2010
New Revision: 77750

Added:
   pypy/branch/fast-forward/lib-python/modified-2.7.0/random.py
      - copied, changed from r77736, pypy/branch/fast-forward/lib-python/2.7.0/random.py
Log:
Use a more pythonic (and less cpython specific)
way to test whether a method has been overriden


Copied: pypy/branch/fast-forward/lib-python/modified-2.7.0/random.py (from r77736, pypy/branch/fast-forward/lib-python/2.7.0/random.py)
==============================================================================
--- pypy/branch/fast-forward/lib-python/2.7.0/random.py	(original)
+++ pypy/branch/fast-forward/lib-python/modified-2.7.0/random.py	Sun Oct 10 00:55:49 2010
@@ -41,7 +41,6 @@
 
 from __future__ import division
 from warnings import warn as _warn
-from types import MethodType as _MethodType, BuiltinMethodType as _BuiltinMethodType
 from math import log as _log, exp as _exp, pi as _pi, e as _e, ceil as _ceil
 from math import sqrt as _sqrt, acos as _acos, cos as _cos, sin as _sin
 from os import urandom as _urandom
@@ -240,8 +239,7 @@
 
         return self.randrange(a, b+1)
 
-    def _randbelow(self, n, _log=_log, int=int, _maxwidth=1L<<BPF,
-                   _Method=_MethodType, _BuiltinMethod=_BuiltinMethodType):
+    def _randbelow(self, n, _log=_log, int=int, _maxwidth=1L<<BPF):
         """Return a random int in the range [0,n)
 
         Handles the case where n has more bits than returned
@@ -256,7 +254,8 @@
             # Only call self.getrandbits if the original random() builtin method
             # has not been overridden or if a new getrandbits() was supplied.
             # This assures that the two methods correspond.
-            if type(self.random) is _BuiltinMethod or type(getrandbits) is _Method:
+            if (self.random == super(Random, self).random or
+                getrandbits != super(Random, self).getrandbits):
                 k = int(1.00001 + _log(n-1, 2.0))   # 2**k > n-1 > 2**(k-2)
                 r = getrandbits(k)
                 while r >= n:



More information about the Pypy-commit mailing list