Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add Interrupt method in PythonEngine
  • Loading branch information
gpetrou committed Jan 21, 2021
commit 8b848b3d7bc0ab10c6b0fe573b519f1c981ff895
5 changes: 3 additions & 2 deletions AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@
- Ville M. Vainio ([@vivainio](https://github.com/vivainio))
- Virgil Dupras ([@hsoft](https://github.com/hsoft))
- Wenguang Yang ([@yagweb](https://github.com/yagweb))
- William Sardar ([@williamsardar])(https://github.com/williamsardar)
- William Sardar ([@williamsardar](https://github.com/williamsardar))
- Xavier Dupré ([@sdpython](https://github.com/sdpython))
- Zane Purvis ([@zanedp](https://github.com/zanedp))
- ([@amos402]https://github.com/amos402)
- ([@amos402](https://github.com/amos402))
- ([@bltribble](https://github.com/bltribble))
- ([@civilx64](https://github.com/civilx64))
- ([@GSPP](https://github.com/GSPP))
Expand All @@ -82,3 +82,4 @@
- ([@testrunner123](https://github.com/testrunner123))
- ([@DanBarzilian](https://github.com/DanBarzilian))
- ([@alxnull](https://github.com/alxnull))
- ([@gpetrou](https://github.com/gpetrou))
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
### Added

- Ability to instantiate new .NET arrays using `Array[T](dim1, dim2, ...)` syntax
<<<<<<< HEAD
- Python operator method will call C# operator method for supported binary and unary operators ([#1324][p1324]).
=======
- Add Interrupt method in PythonEngine
>>>>>>> Add Interrupt method in PythonEngine

### Changed
- Drop support for Python 2, 3.4, and 3.5
Expand Down
83 changes: 83 additions & 0 deletions src/embed_tests/TestInterrupt.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@

using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;

using NUnit.Framework;

using Python.Runtime;

namespace Python.EmbeddingTest
{
public class TestInterrupt
{
private IntPtr _threadState;

[DllImport("Kernel32", EntryPoint = "GetCurrentThreadId", ExactSpelling = true)]
private static extern uint GetCurrentThreadId();

[DllImport("libc", EntryPoint = "pthread_self")]
private static extern IntPtr pthread_selfLinux();

[DllImport("pthread", EntryPoint = "pthread_self", CallingConvention = CallingConvention.Cdecl)]
private static extern ulong pthread_selfOSX();

[OneTimeSetUp]
public void SetUp()
{
PythonEngine.Initialize();
_threadState = PythonEngine.BeginAllowThreads();
}

[OneTimeTearDown]
public void Dispose()
{
PythonEngine.EndAllowThreads(_threadState);
PythonEngine.Shutdown();
}

[Test]
public void InterruptTest()
{
int runSimpleStringReturnValue = int.MinValue;
ulong nativeThreadId = 0;
Task.Factory.StartNew(() =>
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
nativeThreadId = GetCurrentThreadId();
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
nativeThreadId = (ulong)pthread_selfLinux();
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
nativeThreadId = pthread_selfOSX();
}

using (Py.GIL())
{
runSimpleStringReturnValue = PythonEngine.RunSimpleString(@"
import time

while True:
time.sleep(0.2)");
}
});

Thread.Sleep(200);

using (Py.GIL())
{
int interruptReturnValue = PythonEngine.Interrupt(nativeThreadId);
Assert.AreEqual(1, interruptReturnValue);
}

Thread.Sleep(300);

Assert.AreEqual(-1, runSimpleStringReturnValue);
}
}
}
9 changes: 9 additions & 0 deletions src/runtime/pythonengine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,15 @@ public static void Exec(string code, IntPtr? globals = null, IntPtr? locals = nu
}
}

/// <summary>
/// Interrupts the execution of a thread.
/// </summary>
/// <param name="nativeThreadId">The native thread id.</param>
/// <returns>The number of thread states modified; this is normally one, but will be zero if the thread id isn’t found.</returns>
public static int Interrupt(ulong nativeThreadId)
{
return Runtime.PyThreadState_SetAsyncExc(nativeThreadId, Exceptions.KeyboardInterrupt);
}

/// <summary>
/// RunString Method. Function has been deprecated and will be removed.
Expand Down
3 changes: 3 additions & 0 deletions src/runtime/runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2143,6 +2143,9 @@ internal static void Py_CLEAR(ref IntPtr ob)
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern int Py_AddPendingCall(IntPtr func, IntPtr arg);

[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern int PyThreadState_SetAsyncExc(ulong id, IntPtr exc);

[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern int Py_MakePendingCalls();

Expand Down