GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Consider restoring _PyHASH_INF/BITS/MODULUS/IMAG as public · Issue #111389 · python/cpython

Consider restoring _PyHASH_INF/BITS/MODULUS/IMAG as public #111389

New issue
Copy link
New issue
Copy link
Closed
Closed
Consider restoring _PyHASH_INF/BITS/MODULUS/IMAG as public#111389
Copy link
Labels
topic-C-APItype-featureA feature request or enhancementA feature request or enhancement

Description

@skirpichev
skirpichev
opened on Oct 27, 2023
Issue body actions

This is used to implement CPython-like hashing of numeric types e.g. in Sage or in gmpy2. Actually, the same info is available via sys.hash_info, so I don't see reasons to hide this in Python.h.

See #106320 and #107026.

Linked PRs

  • gh-111389: expose _PyHASH_INF/BITS/MODULUS/IMAG macros as public #111418
  • gh-111389: Add PyHASH_MULTIPLIER constant #119214
  • [3.13] gh-111389: Add PyHASH_MULTIPLIER constant (GH-119214) #119334
  • gh-111389: replace deprecated occurrences of _PyHASH_* macros #141236

Activity

Next
  1. skirpichev
    added
    type-featureA feature request or enhancementA feature request or enhancement
    on Oct 27, 2023
  2. hugovk
    added
    topic-C-API
    on Oct 27, 2023
  3. sobolevn commented on Oct 27, 2023

    @sobolevn
    sobolevn
    on Oct 27, 2023
    Member
    More actions

    CC @vstinner

  4. vstinner commented on Oct 27, 2023

    @vstinner
    vstinner
    on Oct 27, 2023
    Member
    More actions

    Python 3.12 has 5 constants implemented as macros:

    • _PyHASH_BITS
    • _PyHASH_IMAG
    • _PyHASH_INF
    • _PyHASH_MODULUS
    • _PyHASH_MULTIPLIER

    Significant commits related to these constants:

    • commit 985ecdc: new Include/pyhash.h header file
    • commit 27cbcd6: in Include/pyport.h, use unsigned integer, replace Py_hash_t with Py_uhash_t
    • commit 63e6c32: add _PyHASH_MULTIPLIER constant
    • commit 8035bc5: add Py_uhash_t type, replace unsigned long with size_t
    • commit dc787d2: add _PyHASH_BITS, _PyHASH_IMAG, _PyHASH_INF and _PyHASH_MODULUS constants

    the same info is available via sys.hash_info, so I don't see reasons to hide this in Python.h.

    Right, it makes sense to make them public. I'm not sure why it was private before.

    We can keep them as macros, but maybe we should consider declaring them as variables (using with dlsym for example) to make them usable in programming languages and use cases which cannot use macros.

  5. vstinner commented on Oct 27, 2023

    @vstinner
    vstinner
    on Oct 27, 2023
    Member
    More actions

    We can keep them as macros, but maybe we should consider declaring them as variables (using with dlsym for example) to make them usable in programming languages and use cases which cannot use macros.

    I created capi-workgroup/api-evolution#37 to discuss that.

  6. skirpichev
    added a commit that references this issue on Oct 28, 2023

    pythongh-111389: restoring _PyHASH_INF/BITS/MODULUS/IMAG macroses as …

    1edec34
  7. skirpichev commented on Oct 28, 2023

    @skirpichev
    skirpichev
    on Oct 28, 2023
    MemberAuthor
    More actions

    @vstinner, I did a quick reversion in #111418

  8. skirpichev
    added a commit that references this issue on Oct 28, 2023

    pythongh-111389: restoring _PyHASH_INF/BITS/MODULUS/IMAG macroses as …

    0ce377a
  9. vstinner commented on Oct 28, 2023

    @vstinner
    vstinner
    on Oct 28, 2023
    Member
    More actions

    Should we also add some functions like Py_HashPointer()?

  10. vstinner commented on Oct 28, 2023

    @vstinner
    vstinner
    on Oct 28, 2023
    Member
    More actions

    @skirpichev:

    are you about documenting these macroses somewhere (I propose https://docs.python.org/3/library/sys.html#sys.hash_info, where we could mention them in descriptions for different fields) AND about renaming them too (add PyUnstable_ aliases)?

    If constants are made public, yes, they must be documented.

    I don't think that PyUnstable prefix is needed, these constants seem stable.

  11. skirpichev commented on Oct 29, 2023

    @skirpichev
    skirpichev
    on Oct 29, 2023
    MemberAuthor
    More actions

    Should we also add some functions like Py_HashPointer()?

    Maybe others (I don't know examples), but not this function. It's used currently in the gmpy2, but this could be replaced by PyBaseObject_Type.tp_hash. And this is a documented replacement:

    cpython/Doc/library/stdtypes.rst

    Lines 789 to 790 in 63acf78

    if math.isnan(x):
    return object.__hash__(x)

    If constants are made public, yes, they must be documented.

    Ok, I'll go with suggested above sys.hash_info. Let me know if you have in mind a better place to put the docs.

    these constants seem stable

    Yep. But can be removed in any minor release, as _PyHASH_NAN.

  12. vstinner commented on Oct 30, 2023

    @vstinner
    vstinner
    on Oct 30, 2023
    Member
    More actions

    https://docs.python.org/dev/library/sys.html is the Python API.

    For the C API, I would prefer to create a new page under https://docs.python.org/dev/c-api/ category.

  13. skirpichev commented on Nov 7, 2023

    @skirpichev
    skirpichev
    on Nov 7, 2023
    MemberAuthor
    More actions

    Should we also add some functions like Py_HashPointer()?

    UPD:

    The one drawback with the PyBaseObject_Type.tp_hash solution above is that there is no guarantee that this function does accept an arbitrary pointer (not only a pointer to some PyObject subclass). (In gmpy2 we pass mpfr_t: https://github.com/aleaxit/gmpy/blob/e002e2c28994f6c9edd411c1f1e09d0bbbbfca9b/src/gmpy2_hash.c#L122)

    On one hand, this is unlikely to be changed. On another hand, this situation is not documented. Maybe it should?

  14. vstinner commented on Nov 15, 2023

    @vstinner
    vstinner
    on Nov 15, 2023
    Member
    More actions

    I restored _PyHASH constants with PR #112115.

  15. vstinner commented on Nov 15, 2023

    @vstinner
    vstinner
    on Nov 15, 2023
    Member
    More actions

    Should we also add some functions like Py_HashPointer()?

    I proposed PR #112096 to add Py_HashPointer() function.

  16. 1 remaining item

  17. vstinner
    added a commit that references this issue on Mar 9, 2024

    gh-111389: expose PyHASH_INF/BITS/MODULUS/IMAG macros as public (#111418

    1e68c4b
  18. vstinner
    closed this as completedon Mar 9, 2024
  19. vstinner commented on Mar 9, 2024

    @vstinner
    vstinner
    on Mar 9, 2024
    Member
    More actions

    This is used to implement CPython-like hashing of numeric types e.g. in Sage or in gmpy2.

    You can now propose changes for these projects to use the public names :-) I added the constants to pythoncapi-compat: python/pythoncapi-compat@7539c7f

  20. adorilson
    added a commit that references this issue on Mar 25, 2024

    pythongh-111389: expose PyHASH_INF/BITS/MODULUS/IMAG macros as public (…

    84abeb8
  21. diegorusso
    added a commit that references this issue on Apr 17, 2024

    pythongh-111389: expose PyHASH_INF/BITS/MODULUS/IMAG macros as public (…

    ab7da30
  22. vstinner
    added 5 commits that reference this issue on May 20, 2024

    pythongh-111389: Make PyHASH_MULTIPLIER constant public

    ca28127

    pythongh-111389: Make PyHASH_MULTIPLIER constant public

    cd662fb

    pythongh-111389: Add PyHASH_MULTIPLIER constant

    2883954

    pythongh-111389: Add PyHASH_MULTIPLIER constant

    65f4c31

    gh-111389: Add PyHASH_MULTIPLIER constant (#119214)

    f6da790
  23. miss-islington
    added a commit that references this issue on May 21, 2024

    pythongh-111389: Add PyHASH_MULTIPLIER constant (pythonGH-119214)

    110109f
  24. vstinner
    added a commit that references this issue on Jun 4, 2024

    [3.13] gh-111389: Add PyHASH_MULTIPLIER constant (GH-119214) (#119334)

    1177897
  25. estyxx
    added a commit that references this issue on Jul 17, 2024

    pythongh-111389: Add PyHASH_MULTIPLIER constant (python#119214)

    20fd8d3
  26. picnixz
    added a commit that references this issue on Nov 9, 2025

    gh-111389: replace deprecated occurrences of `_PyHASH_*` macros (#141236

    ae1f435
  27. StanFromIreland
    added a commit that references this issue on Dec 6, 2025

    pythongh-111389: replace deprecated occurrences of `_PyHASH_*` macros (…

    f617e8b
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

    topic-C-APItype-featureA feature request or enhancementA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.