This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: python -h can't find -R option
Type: Stage:
Components: Documentation Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: docs@python, eryksun, terry.reedy, vstinner
Priority: normal Keywords:

Created on 2021-11-07 03:10 by wangjiahua, last changed 2022-04-11 14:59 by admin.

Messages (9)
msg405891 - (view) Author: jiahua wang (wangjiahua) * Date: 2021-11-07 03:10
I input python -h on the command line, and I can't find any -R option.
msg405903 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2021-11-07 13:48
Apparently -R is still implemented in Python 3, even though hash randomization is enabled by default now. Unlike Python 2, in Python 3 -R overrides the PYTHONHASHSEED environment variable, making it effectively the same as "PYTHONHASHSEED=random". For example:

Python 2.7.18

    $ PYTHONHASHSEED=37 python2
    >>> import sys; sys.flags.hash_randomization
    37
    >>> hash('spam')
    -3063226141675644153

    $ PYTHONHASHSEED=37 python2 -R
    >>> import sys; sys.flags.hash_randomization
    37
    >>> hash('spam')
    -3063226141675644153

Python 3.11

    $ PYTHONHASHSEED=37 python3 -q
    >>> import sys; sys.flags.hash_randomization
    1
    >>> hash('spam')
    7085596773562191897

    $ PYTHONHASHSEED=37 python3 -q
    >>> hash('spam')
    7085596773562191897

    $ PYTHONHASHSEED=37 python3 -qR
    >>> import sys; sys.flags.hash_randomization
    1
    >>> hash('spam')
    -6544739063919843911

    $ PYTHONHASHSEED=37 python3 -qR
    >>> hash('spam')
    5363435507110042548

Python 3 no longer reports the seed value in sys.flags.hash_randomization, so I repeated the 3.11 examples twice to show that PYTHONHASHSEED works, except when -R overrides it.
msg405904 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2021-11-07 13:55
Either the documentation [1] or the behavior needs to be fixed. The following statement is false:

    Turn on hash randomization. This option only has an effect if the
    PYTHONHASHSEED environment variable is set to 0, since hash
    randomization is enabled by default.

---
[1] https://docs.python.org/3.10/using/cmdline.html#cmdoption-R
msg406239 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2021-11-12 21:38
Victor, A sentence about hash randomization you wrote for #32329 in PR-4873 is not currently correct.  Change code or doc?
msg406254 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-11-12 23:16
> I input python -h on the command line, and I can't find any -R option.

If you want to document it, the "usage" is in Python/initconfig.c. See also the -R option. My notes on adding a command line option:
https://pythondev.readthedocs.io/pyconfig.html#add-a-new-command-line-option

> Victor, A sentence about hash randomization you wrote for #32329 in PR-4873 is not currently correct.  Change code or doc?

In Python/initconfig.c, the -R option sets config->use_hash_seed to 0. It overrides PYTHONHASHSEED env var, as documented at:
https://docs.python.org/3.10/using/cmdline.html#cmdoption-R

IMO it's the expected behavior. Cmd line must have the priority over env vars.

Which doc is wrong?
msg406259 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2021-11-13 00:09
> Which doc is wrong?

Currently -R overrides a non-zero seed value in PYTHONHASHSEED. The behavior changed in 3.7. Either the docs or the behavior need to be fixed. To repeat my previous example (this time in Windows):

    C:\>set PYTHONHASHSEED=37

    C:\>py -3.6 -c "print(hash('spam'))"
    -1810080452065497889
    C:\>py -3.6 -Rc "print(hash('spam'))"
    -1810080452065497889

    C:\>py -3.7 -c "print(hash('spam'))"
    -1810080452065497889
    C:\>py -3.7 -Rc "print(hash('spam'))"
    -7199637890859510937

    C:\>py -3.10 -Rc "print(hash('spam'))"
    7684061207513636658
msg406315 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-11-14 10:18
If I understand correctly

https://docs.python.org/3.10/using/cmdline.html#cmdoption-R

"This option only has an effect if the PYTHONHASHSEED environment variable is set to 0"

should be replaced with:

"This option only has an effect if the PYTHONHASHSEED environment variable is set"

Is that correct?

Does someone want to write a PR for that?
msg406316 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2021-11-14 10:59
How about this?

    Enable hash randomization. This option overrides PYTHONHASHSEED
    to use the default behavior.
msg406326 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2021-11-14 17:23
If you two agree on an exact wording, I can make the PR and backport.
History
Date User Action Args
2022-04-11 14:59:52adminsetgithub: 89905
2021-11-14 17:23:34terry.reedysetmessages: + msg406326
2021-11-14 10:59:36eryksunsetmessages: + msg406316
2021-11-14 10:18:17vstinnersetmessages: + msg406315
2021-11-13 00:09:25eryksunsetmessages: + msg406259
2021-11-12 23:16:12vstinnersetmessages: + msg406254
2021-11-12 21:38:25terry.reedysetnosy: + vstinner, terry.reedy
messages: + msg406239
2021-11-07 13:55:54eryksunsetmessages: + msg405904
2021-11-07 13:52:05eryksunsetversions: + Python 3.9, Python 3.11
2021-11-07 13:48:49eryksunsetnosy: + eryksun, - wangjiahua
messages: + msg405903
2021-11-07 03:10:01wangjiahuacreate