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 — [C API] Add PySet_NextEntry() function · Issue #121125 · python/cpython

[C API] Add PySet_NextEntry() function #121125

New issue
Copy link
New issue
Copy link
Closed as not planned
Closed as not planned
[C API] Add PySet_NextEntry() function#121125
Copy link
Labels
topic-C-APItype-featureA feature request or enhancementA feature request or enhancement

Description

@vstinner
vstinner
opened on Jun 28, 2024
Issue body actions

Feature or enhancement

The _PySet_NextEntry() function was removed in Python 3.13 alpha 1, but it's used by the Blender project: https://projects.blender.org/blender/blender/search?q=_PySet_NextEntry

I propose adding a new function to replace it:

int PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key)
  • Set *key to a strong reference, and return 1 on success.
  • Return 0 if there is no more item.
  • Set an exception and return -1 on error.

Differences with _PySet_NextEntry():

  • key is set to a strong reference, rather than a borrowed reference.
  • Remove the hash parameter.

Activity

  1. vstinner
    added
    type-featureA feature request or enhancementA feature request or enhancement
    topic-C-API
    pendingThe issue will be closed if no feedback is providedThe issue will be closed if no feedback is provided
    on Jun 28, 2024
  2. vstinner commented on Jun 28, 2024

    @vstinner
    vstinner
    on Jun 28, 2024
    MemberAuthor
    More actions

    cc @AdamWill

  3. erlend-aasland commented on Jun 28, 2024

    @erlend-aasland
    erlend-aasland
    on Jun 28, 2024
    Contributor
    More actions

    Meta: why the pending label? Are you considering immediately closing your newly opened issue? :)

  4. vstinner
    removed
    pendingThe issue will be closed if no feedback is providedThe issue will be closed if no feedback is provided
    on Jun 28, 2024
  5. vstinner commented on Jun 28, 2024

    @vstinner
    vstinner
    on Jun 28, 2024
    MemberAuthor
    More actions

    I added "pending" label by mistake, I don't know how. Removed.

    Reacted by Erlend E. Aasland
  6. encukou commented on Jun 28, 2024

    @encukou
    encukou
    on Jun 28, 2024
    Member
    More actions

    Why is this better than PyObject_GetIter & PyIter_Next?

  7. vstinner commented on Jun 28, 2024

    @vstinner
    vstinner
    on Jun 28, 2024
    MemberAuthor
    More actions

    Why is this better than PyObject_GetIter & PyIter_Next?

    I'm ok if we decide to not add a new function for Blender.

    Proposed API is similar to PyDict_Next() with a pos parameter set to 0 at start. It's simpler to use, there is no iterator to manage. I also expect it to be faster.

  8. AdamWill commented on Jun 28, 2024

    @AdamWill
    AdamWill
    on Jun 28, 2024
    Contributor
    More actions

    Poking around, it's also used in Numba. PyO3 (Rust bindings for Python) used to use it but changed that recently in PyO3/pyo3#3849 . llvm-opt-benchmark seems to use it, but I think that's just benchmarking how fast it is when you build CPython with LLVM or something; presumably they could just...stop benchmarking that specific function. Everything else I can find at least in github universal search is some kind of fork or copy of CPython or one of the other users, or an alternative Python interpreter providing its own version.

  9. serhiy-storchaka commented on Jun 28, 2024

    @serhiy-storchaka
    serhiy-storchaka
    on Jun 28, 2024
    Member
    More actions

    Do you want to add only PySet_NextEntry(), or also PyFrozenSet_NextEntry() and PyAnySet_NextEntry()?

    See #43090 for discussion about PySet_Next().

    cc @rhettinger

  10. vstinner commented on Jun 29, 2024

    @vstinner
    vstinner
    on Jun 29, 2024
    MemberAuthor
    More actions

    I expect the function to work on set, frozenset and subclasses.

  11. rhettinger commented on Jun 30, 2024

    @rhettinger
    rhettinger
    on Jun 30, 2024
    Contributor
    More actions

    I prefer people to just use __iter__ and __next__. Those are reasonably fast, are standard, are well-understood, and have strong guarantees around them (something that will become even more important in free-threaded code).

    The _PySet_NextEntry API is fragile, hard to use correctly, and too tightly coupled to implementation details. It was intended solely an internal optimization hack. The warning comment for set_next is dead serious. Even for internal calls, we've had bugs related to its use.

    Reacted by Petr Viktorin
  12. rhettinger commented on Jun 30, 2024

    @rhettinger
    rhettinger
    on Jun 30, 2024
    Contributor
    More actions

    FWIW, it was an intentional decision to not make this a public API.

    The API was carefully thought out to provide what most people need from a set object. Implementation details, optimization hacks, and fragile interfaces were avoided. For the most part, this has served us well.

  13. rhettinger
    closed this as not plannedon Jul 5, 2024
  14. vstinner commented on Jul 8, 2024

    @vstinner
    vstinner
    on Jul 8, 2024
    MemberAuthor
    More actions

    @AdamWill: Well, it seems like Blender, other mentioned projects, have to be updated to PyObject_GetIter() and PyIter_Next().

  15. AdamWill commented on Jul 9, 2024

    @AdamWill
    AdamWill
    on Jul 9, 2024
    Contributor
    More actions

    Thanks, I'll update the downstream issue. If anyone wants to help with that it would be great, I like to try and help projects with these transitions where possible but I'm not much of a C coder.

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.