From 35d40ccaf1c1d1b7acdf6078da43667ad8cd1ba2 Mon Sep 17 00:00:00 2001 From: falkTX Date: Sat, 6 Oct 2018 07:42:40 +0000 Subject: [PATCH] Add a test case for ctypes callback --- source/tests/Makefile | 10 +++++++++- source/tests/ctypes-test.c | 23 +++++++++++++++++++++++ source/tests/ctypes-test.py | 22 ++++++++++++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 source/tests/ctypes-test.c create mode 100755 source/tests/ctypes-test.py diff --git a/source/tests/Makefile b/source/tests/Makefile index ef8c2a1e9..87d454228 100644 --- a/source/tests/Makefile +++ b/source/tests/Makefile @@ -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 diff --git a/source/tests/ctypes-test.c b/source/tests/ctypes-test.c new file mode 100644 index 000000000..b93bdcddc --- /dev/null +++ b/source/tests/ctypes-test.c @@ -0,0 +1,23 @@ +#include +#include + +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"); +} diff --git a/source/tests/ctypes-test.py b/source/tests/ctypes-test.py new file mode 100755 index 000000000..f03676d15 --- /dev/null +++ b/source/tests/ctypes-test.py @@ -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()