Skip to content

Commit

Permalink
Make the signal handling code less duplicated
Browse files Browse the repository at this point in the history
  • Loading branch information
dfangl committed Nov 19, 2024
1 parent e919b98 commit eaa2a48
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 42 deletions.
11 changes: 2 additions & 9 deletions native/common/jp_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -446,17 +446,10 @@ extern "C" JNIEXPORT void JNICALL Java_org_jpype_JPypeContext_onShutdown

static int interruptState = 0;
extern "C" JNIEXPORT void JNICALL Java_org_jpype_JPypeSignal_interruptPy
(JNIEnv *env, jclass cls)
{
interruptState = 1;
PyErr_SetInterrupt();
}

extern "C" JNIEXPORT void JNICALL Java_org_jpype_JPypeSignal_interruptPyTerm
(JNIEnv *env, jclass cls)
(JNIEnv *env, jclass cls, jint signal)
{
interruptState = 1;
PyErr_SetInterruptEx(15);
PyErr_SetInterruptEx((int) signal);
}

extern "C" JNIEXPORT void JNICALL Java_org_jpype_JPypeSignal_acknowledgePy
Expand Down
47 changes: 15 additions & 32 deletions native/java/org/jpype/JPypeSignal.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
**************************************************************************** */
package org.jpype;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
Expand All @@ -31,6 +30,17 @@ public class JPypeSignal

static Thread main;

static Object getSignalHandler(Class signalHandlerClazz, int signal) throws ClassNotFoundException {
return Proxy.newProxyInstance(ClassLoader.getSystemClassLoader(), new Class[]
{
signalHandlerClazz
}, (proxy, method, args) -> {
main.interrupt();
interruptPy(signal);
return null;
});
}

static void installHandlers()
{
try
Expand All @@ -39,44 +49,17 @@ static void installHandlers()
Class SignalHandler = Class.forName("sun.misc.SignalHandler");
main = Thread.currentThread();
Method method = Signal.getMethod("handle", Signal, SignalHandler);

Object handler = Proxy.newProxyInstance(ClassLoader.getSystemClassLoader(), new Class[]
{
SignalHandler
}, new InvocationHandler()
{
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
{
main.interrupt();
interruptPy();
return null;
}
});
Object termHandler = Proxy.newProxyInstance(ClassLoader.getSystemClassLoader(), new Class[]
{
SignalHandler
}, new InvocationHandler()
{
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
{
main.interrupt();
interruptPyTerm();
return null;
}
});
Object intr = Signal.getDeclaredConstructor(String.class).newInstance("INT");
method.invoke(null, intr, handler);
method.invoke(null, intr, getSignalHandler(SignalHandler, 2));
Object intrTerm = Signal.getDeclaredConstructor(String.class).newInstance("TERM");
method.invoke(null, intrTerm, termHandler);
method.invoke(null, intrTerm, getSignalHandler(SignalHandler, 15));
} catch (InvocationTargetException | IllegalArgumentException | IllegalAccessException | InstantiationException | ClassNotFoundException | NoSuchMethodException | SecurityException ex)
{
// If we don't get the signal handler run without it. (ANDROID)
}
}

native static void interruptPy();
native static void interruptPyTerm();
native static void interruptPy(int signal);

native static void acknowledgePy();
}
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "jpype1-ext"
version = '0.0.1'
version = '0.0.2'
authors = [
{name = "Steve Menard", email = "[email protected]"},
]
Expand Down

0 comments on commit eaa2a48

Please sign in to comment.