[Python-checkins] cpython: Issue #14815: Bugfix: the PyLong fed into the seed generator must be unsigned.

larry.hastings python-checkins at python.org
Sun Jun 24 11:52:32 CEST 2012


http://hg.python.org/cpython/rev/4445608cf434
changeset:   77684:4445608cf434
user:        Larry Hastings <larry at hastings.org>
date:        Sun Jun 24 02:52:21 2012 -0700
summary:
  Issue #14815: Bugfix: the PyLong fed into the seed generator must be unsigned.

files:
  Modules/_randommodule.c |  9 +++++----
  1 files changed, 5 insertions(+), 4 deletions(-)


diff --git a/Modules/_randommodule.c b/Modules/_randommodule.c
--- a/Modules/_randommodule.c
+++ b/Modules/_randommodule.c
@@ -228,16 +228,17 @@
         Py_INCREF(Py_None);
         return Py_None;
     }
-    /* If the arg is an int or long, use its absolute value; else use
-     * the absolute value of its hash code.
+    /* This algorithm relies on the number being unsigned.
+     * 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);
     else {
-        Py_ssize_t hash = PyObject_Hash(arg);
+        Py_hash_t hash = PyObject_Hash(arg);
         if (hash == -1)
             goto Done;
-        n = PyLong_FromSsize_t(hash);
+        n = PyLong_FromSize_t((size_t)hash);
     }
     if (n == NULL)
         goto Done;

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list