Skip to content

Points from the Bad C API document #28

Open

Activity

  1. vstinner commented on Oct 13, 2023

    @vstinner
    Contributor

    No public C functions if it can’t be done in Python

    That was a request from PyPy devs. By the way, the specific case of Py_buffer was fixed in Python 3.12 by PEP 688 – Making the buffer protocol accessible in Python.

    PyObject** must not be exposed: PyObject** PySequence_Fast_ITEMS(ob) has to go.

    I propose adding a PyResource API:

    But I'm not convinced myself by this approach. A more promising approach may be to support PyObject* type in the Py_buffer protocol. Apparently, some projects have an unofficial support for that.

  2. pitrou commented on Oct 14, 2023

    @pitrou

    I think this could be solved with a "sequence unpacking API" that would return an array of owned refs:

    /*
     * Try to unpack `nitems` items from `seq`, starting at position `start`, into `items`.
     *
     * Returns the number of items actually unpacked:
     *  - is less than `nitems` if sequence is too short;
     *  - is 0 if past sequence end;
     *  - is -1 on error.
     *
     * The unpacked items are all owned references and must be released
     * with `PySequence_ClearUnpacked` when done.
     */
    Py_ssize_t PySequence_Unpack(PyObject* seq, Py_ssize_t start, Py_ssize_t nitems, PyObject** items);
    void PySequence_ClearUnpacked(PyObject** items, Py_ssize_t start, Py_ssize_t nitems);

    Example usage:

    // Iterate over the items in sequence `seq`, in 128-element batches
    #define BATCH_SIZE 128
    PyObject *items[BATCH_SIZE];
    Py_ssize_t start = 0, nitems;
    while ((nitems = PySequence_Unpack(seq, start, BATCH_SIZE, items)) > 0) {
      // Do something with the `nitems` objects in `items`...
      ...
      // Clear owned refs
      PySequence_ClearUnpacked(items, start, nitems);
      // Move to next batch
      start += nitems;
    }
    if (nitems < 0) {
      // PySequence_Unpack failed, handle error
      ...
    }

    Batching solves the performance problem of calling PySequence_GetItem one item at a time, but without compromising with error handling and safety.

  3. pitrou commented on Oct 14, 2023

    @pitrou

    A more promising approach may be to support PyObject* type in the Py_buffer protocol. Apparently, some projects have an unofficial support for that.

    This is already part of PEP 3118 (see the O typecode for Python objects), and has been supported by NumPy for a long time.

    But I don't think using the buffer protocol is adequate for quick sequence access over potentially short sequences. The use cases are different. Also, this would require tuple and list to implement the buffer protocol which is potentially disruptive.

  4. vstinner commented on Oct 14, 2023

    @vstinner
    Contributor

    Yeah, batching seems to be preferred over my PyResource API based on "create a single view, use the view, delete the view" knowning that the creation could require to copy the whole array, create new strong references, and then decrement ref count.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions