Skip to content

PYTHONHASHSEED=0 python3 -R should enable hash randomization #76510

Closed

Description

@vstinner
BPO 32329
Nosy @vstinner
PRs
  • bpo-32329: Fix -R option for hash randomization #4873
  • [3.6] bpo-32329: Fix sys.flags.hash_randomization #4875
  • bpo-32329: Add versionchanged to -R option doc #4884
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2017-12-15.15:41:41.471>
    created_at = <Date 2017-12-14.23:11:19.999>
    labels = ['3.7']
    title = 'PYTHONHASHSEED=0 python3 -R should enable hash randomization'
    updated_at = <Date 2017-12-15.15:41:41.470>
    user = 'https://github.com/vstinner'

    bugs.python.org fields:

    activity = <Date 2017-12-15.15:41:41.470>
    actor = 'vstinner'
    assignee = 'none'
    closed = True
    closed_date = <Date 2017-12-15.15:41:41.471>
    closer = 'vstinner'
    components = []
    creation = <Date 2017-12-14.23:11:19.999>
    creator = 'vstinner'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 32329
    keywords = ['patch']
    message_count = 5.0
    messages = ['308343', '308348', '308354', '308355', '308405']
    nosy_count = 1.0
    nosy_names = ['vstinner']
    pr_nums = ['4873', '4875', '4884']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue32329'
    versions = ['Python 3.7']

    Activity

    1. vstinner commented on Dec 14, 2017

      @vstinner
      MemberAuthor

      Python pretends that hash randomization is always enabled, but it's not:

      $ PYTHONHASHSEED=0 python3 -c 'import sys; print(sys.flags.hash_randomization)'
      1
      vstinner@apu$ PYTHONHASHSEED=0 python3 -c 'import sys; print(hash("abc"))'
      4596069200710135518
      vstinner@apu$ PYTHONHASHSEED=0 python3 -c 'import sys; print(hash("abc"))'
      4596069200710135518
      vstinner@apu$ PYTHONHASHSEED=0 python3 -c 'import sys; print(hash("abc"))'
      4596069200710135518

      => sys.flags.hash_randomization must be zero

      Moreover, the -R flag is always ignored, it's not possible to override the PYTHONHASHSEED environment variable:

      vstinner@apu$ PYTHONHASHSEED=0 python3 -R -c 'import sys; print(sys.flags.hash_randomization)'
      1
      vstinner@apu$ PYTHONHASHSEED=0 python3 -R -c 'import sys; print(hash("abc"))'
      4596069200710135518
      vstinner@apu$ PYTHONHASHSEED=0 python3 -R -c 'import sys; print(hash("abc"))'
      4596069200710135518
      vstinner@apu$ PYTHONHASHSEED=0 python3 -R -c 'import sys; print(hash("abc"))'
      4596069200710135518

      I expect that -R enables hash randomization and overrides PYTHONHASHSEED environment variable.

      Attached PR fixes these issues and adds an unit test.

    2. vstinner commented on Dec 14, 2017

      @vstinner
      MemberAuthor

      New changeset 358e5e1 by Victor Stinner in branch 'master':
      bpo-32329: Fix -R option for hash randomization (bpo-4873)
      358e5e1

    3. vstinner commented on Dec 15, 2017

      @vstinner
      MemberAuthor

      New changeset 22097aa by Victor Stinner in branch '3.6':
      bpo-32329: Fix sys.flags.hash_randomization (bpo-4875)
      22097aa

    4. vstinner commented on Dec 15, 2017

      @vstinner
      MemberAuthor

      "PYTHONHASHSEED=0 python3 -R" now enables hash randomization on master (Python 3.7), I also fixed the sys.flags.hash_randomization value when using PYTHONHASHSEED=0.

      I chose to not modify the behaviour of the -R option in Python 3.6. I dislike having to document a behaviour change in a minor Python version (3.6.x). I only fixed the value of sys.flags.hash_randomization in Python 3.6.

    5. vstinner commented on Dec 15, 2017

      @vstinner
      MemberAuthor

      New changeset 642d67b by Victor Stinner in branch 'master':
      bpo-32329: Add versionchanged to -R option doc (bpo-4884)
      642d67b

    6. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.7 (EOL)

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions