Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide CFUNCTYPE prototype name for function pointers defined in Structure #13

Open
GoogleCodeExporter opened this issue Mar 13, 2015 · 2 comments
Labels
auto-migrated Priority-Medium Type-Defect validated issue has been validated by a maintainer

Comments

@GoogleCodeExporter
Copy link

When wrapping a function pointer inside a structure, there is not corresponding 
CFUNCTYPE prototype type provided so we can initialize the prototype. Like

struct ui_interface {
    void (*post_warning)(const char *title,const char *statement,...);
    char *(*open_file)(const char *title, const char *defaultfile,
        const char *initial_filter);
}

ctypesgen correctly defined the types for post_warning and open_file as
functype like:

struct_ui_interface._fields_ = [
    ('post_warning', CFUNCTYPE(UNCHECKED(None), String, String)),
    ('open_file', CFUNCTYPE(UNCHECKED(String), String, String, String)),
]

But it should provide struct_ui_interface_post_warning_func and
struct_ui_interface_open_file_func so that we can create the CFUNCTYPE object
easily.

struct_ui_interface_post_warning_func =  CFUNCTYPE(UNCHECKED(None), String, 
String))

struct_ui_interface_open_file_func = CFUNCTYPE(UNCHECKED(String), String, 
String, String))




Original issue reported on code.google.com by mozbug...@gmail.com on 21 May 2011 at 12:42

@Alan-R
Copy link
Collaborator

Alan-R commented Aug 1, 2021

I've certainly run into this myself. It would be handy, and not inconsistent with ctypesgen goals (IMHO).

@Alan-R Alan-R added the validated issue has been validated by a maintainer label Aug 1, 2021
@mara004
Copy link
Contributor

mara004 commented Feb 19, 2023

If I understood this correctly, I'm facing the same in my project. This is my current solution:

def set_callback(struct, fname, callback):
    # hack to extract CFUNCTYPE definition and wrap callback
    setattr(struct, fname, type(getattr(struct, fname))(callback))

Example usage:

# `_buffer_writer` is a callable factory, and `buffer` an io.BufferedReader or similar
writer = pdfium_c.FPDF_FILEWRITE(version=1)
set_callback(writer, "WriteBlock", _buffer_writer(buffer))

It's none too nice this is left to the caller. Ideally, assigning a function to a field should just do the CFUNCTYPE wrapping automatically, if that were possible to implement in ctypes/python.

I don't think such templates belong in ctypesgen, though. Maybe rather in a separate ctypes-extensions package or something, if need be. But overall standalone ctypes is fairly complete these days.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto-migrated Priority-Medium Type-Defect validated issue has been validated by a maintainer
Projects
None yet
Development

No branches or pull requests

3 participants