Browse Source

Add a test case for ctypes callback

tags/v2.1-alpha1-winvst
falkTX 6 years ago
parent
commit
35d40ccaf1
3 changed files with 54 additions and 1 deletions
  1. +9
    -1
      source/tests/Makefile
  2. +23
    -0
      source/tests/ctypes-test.c
  3. +22
    -0
      source/tests/ctypes-test.py

+ 9
- 1
source/tests/Makefile View File

@@ -26,7 +26,7 @@ BASE_FLAGS += -Wpointer-arith -Wabi -Winit-self -Wuninitialized -Wunused-paramet

LINK_FLAGS =

ifneq ($(CC),clang-3.4)
ifneq ($(CC),clang)
BASE_FLAGS += -Wlogical-op -Wunsafe-loop-optimizations
endif

@@ -121,6 +121,12 @@ ansi-pedantic-test_cxxlang: ansi-pedantic-test.cpp ../backend/Carla*.h ../includ

# --------------------------------------------------------------

ctypes-test.so: ctypes-test.c .FORCED
$(CC) $< $(PEDANTIC_C_FLAGS) -shared -o $@
set -e; ./ctypes-test.py

# --------------------------------------------------------------

CachedPlugins: CachedPlugins.cpp
$(CXX) $< $(PEDANTIC_CXX_FLAGS) -o $@ $(MODULEDIR)/juce_core.a $(MODULEDIR)/lilv.a -ldl -lpthread
ifneq ($(WIN32),true)
@@ -269,3 +275,5 @@ DGL: DGL.cpp ../modules/distrho/dgl/src/Window.cpp
# valgrind ./DGL

# --------------------------------------------------------------

.PHONY: .FORCED

+ 23
- 0
source/tests/ctypes-test.c View File

@@ -0,0 +1,23 @@
#include <assert.h>
#include <stdio.h>

typedef void (*PythonSideFn)(int checker);

PythonSideFn pyFn = NULL;

__attribute__ ((visibility("default")))
void set_python_side_fn(PythonSideFn fn)
{
printf("set_python_side_fn(%p)\n", fn);
pyFn = fn;
}

__attribute__ ((visibility("default")))
void call_python_side_fn(void)
{
assert(pyFn != NULL);

printf("going to call python function now, ptr: %p\n", pyFn);
pyFn(1337);
printf("done!\n");
}

+ 22
- 0
source/tests/ctypes-test.py View File

@@ -0,0 +1,22 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from ctypes import *

PythonSideFn = CFUNCTYPE(None, c_int)

lib = CDLL("./ctypes-test.so", RTLD_LOCAL)

lib.set_python_side_fn.argtypes = [PythonSideFn]
lib.set_python_side_fn.restype = None
lib.call_python_side_fn.argtypes = None
lib.call_python_side_fn.restype = None

def pyFn(checker):
print("Python function called from C code, checker:", checker)

_pyFn = PythonSideFn(pyFn)
lib.set_python_side_fn(_pyFn)

print("Python side ready, calling C function now")
lib.call_python_side_fn()

Loading…
Cancel
Save