python / python/cpython

Add support for defining variadic callbacks via ctypes

Open
#135,620 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

extension-modules topic-ctypes type-feature
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Feature or enhancement

Proposal:

When defining a callback with ctypes (CFUNCTYPE) it would be helpful to be able to replicate variadic functionality
for instance, take the following c code

void FormatPrintf(datap* format, char* fmt, ...) {

    va_list args;
    int length = 0;

    va_start(args, fmt);
    length = vsnprintf(NULL, 0, fmt, args);
    va_end(args);
    if (format->length + length > format->size) {
        return;
    }

    va_start(args, fmt);
    (void)vsnprintf(format->buffer, length, fmt, args);
    va_end(args);
    return;
}

Making something compatible with ctypes

@CFUNCTYPE(None, ctypes.POINTER(datap), ctypes.c_char_p, ctypes.c_variadic)
def FormatPrintf(format, fmt, variadic)
    args = variadic.va_list
    ...

Maybe even could be handled as a pyobject list of ctypes objects as the variadic on the python side. Not sure what all the options are explicitly.

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the ctypes CFUNCTYPE callback entry point and the C variadic callback example in the issue. Clarify the supported representation and behavior for variadic arguments, then determine what tests are needed to show that defining and invoking such a callback works.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, python
Domain
api
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.