Skip to content

GitLab

  • Menu
Projects Groups Snippets
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • cffi cffi
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 73
    • Issues 73
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 4
    • Merge requests 4
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Monitor
    • Monitor
    • Incidents
  • Packages & Registries
    • Packages & Registries
    • Container Registry
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • PyPy
  • cfficffi
  • Issues
  • #213

Closed
Open
Created Jul 08, 2015 by Bitbucket Importer@bitbucket_importerMaintainer

TypeError from simple callback using structs

Created originally on Bitbucket by iuliatamas (Iulia Tamas)

Error:

#!python
Callback in C
Traceback (most recent call last):
  File "cffi-callback-bug.py", line 30, in <module>
    lib._add_callback(set_struct) # this doesn't work
TypeError: initializer for ctype 'struct CStruct *(*)(int)' must be a pointer to same type, not cdata 'struct CStruct *(*)(int)'

Code that leads to it (also attached):

cffi-callback-bug.py:

#!python
from cffi import FFI
ffi = FFI()

ffi.set_source("_example",
    """ 
        #include <stdio.h>

        struct CStruct;
        void _add_callback ( struct CStruct * (* ptr) (int) );
    """,
    runtime_library_dirs=['.'],
    libraries=['mylib'])

ffi.cdef('''

    void _add_callback ( struct CStruct * (* ptr) (int) );

''')

@ffi.callback("struct CStruct * (int)")
def set_struct(x):
    return 0

if __name__ == "__main__":
    ffi.compile()

    from _example import ffi, lib
    lib._add_callback(ffi.cast("void * ", set_struct)) # this works
    lib._add_callback(set_struct) # this doesn't work

cffi-callback-bug.c:

#!c
// To create library:
// gcc -arch i386 -arch x86_64 -dynamiclib -fPIC -lc -lstdc++ cffi-callback-bug.c -o libmylib.dylib

#include <stdio.h>
#include "cffi-callback-bug.h"

struct CStruct * _add_callback (struct CStruct * (*ptr) (int)) {
    printf("Callback in C\n");
    return NULL;
}

cffi-callback-bug.h:

#!c
struct CStruct {
    int hi;
};
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information
Assignee
Assign to
Time tracking