[Python-porting] Control of hash randomization

Aaron Meurer asmeurer at gmail.com
Sun May 27 07:26:53 CEST 2012


I found half of my answer.  You can set it with the PYTHONHASHSEED
environment variable:

$PYTHONHASHSEED=42 python3.3
hashPython 3.3.0a3 (v3.3.0a3:0b53b70a40a0, May  1 2012, 11:39:35)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> hash('a')
-5486454195961207828

$PYTHONHASHSEED=42 python3.3
Python 3.3.0a3 (v3.3.0a3:0b53b70a40a0, May  1 2012, 11:39:35)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> hash('a')
-5486454195961207828

I'm still couldn't find how to actually get that seed, though.

Aaron Meurer

On Sat, May 26, 2012 at 10:58 PM, Aaron Meurer <asmeurer at gmail.com> wrote:
> Hi.
>
> First, I want to apologize if this is the wrong list for this
> question. If it is, kindly direct me to the correct one and I'll be on
> my way.
>
> I am attempting to "port" SymPy to Python 3.3.  We already have full
> support for Python 3.2, but the new hash randomization in Python 3.3
> has brought up a bunch of new failures in our test suite.  This is not
> a surprise, as a lot of operations in SymPy are dependent on hash
> values.  But expecting the problem does not entirely help use to solve
> it.
>
> My question is this: is there a way to get the random seed used for
> hash randomization, and to print it out, and then to later input that
> seed to the interpreter?  Without this ability, it becomes extremely
> difficult to reproduce test failures.  The best bet is to restart the
> interpreter multiple times until it occurs.  We already have
> experienced in SymPy several odd bugs that were caused by very rare
> seeds in the random module.  This seed is easy to set, though, and we
> print it with each test run, so the handful of issues that have
> cropped up we've been able to fix.  I'd like to have that same ability
> with hash randomization.
>
> I figured that this is a new feature, and people on this list are
> supposed to be experts on new Python features, so my hope is that
> either someone here will know the answer, or can point me in the right
> direction to one.
>
> If it is the case, as I fear/expect, that this is not possible, should
> I open an issue in the Python bug tracker?  Should I raise the issue
> on python-dev?  Has it been discussed before?  I realize that there
> may be a security risk (imho a low one, though) in such a feature, so
> it might not be immediately accepted.
>
> Aaron Meurer


More information about the Python-porting mailing list