Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9b18f26

Browse files
committed
Use get_ident and LLP64 and LP64 in DllImport names and ulong in public methods and assert nativeThreadID value
1 parent 279d8b0 commit 9b18f26

File tree

4 files changed

+12
-62
lines changed

4 files changed

+12
-62
lines changed

CHANGELOG.md

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,8 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
1010
### Added
1111

1212
- Ability to instantiate new .NET arrays using `Array[T](dim1, dim2, ...)` syntax
13-
<<<<<<< HEAD
14-
<<<<<<< HEAD
1513
- Python operator method will call C# operator method for supported binary and unary operators ([#1324][p1324]).
16-
=======
17-
- Add Interrupt method in PythonEngine
18-
>>>>>>> Add Interrupt method in PythonEngine
19-
=======
2014
- Add GetNativeThreadID and Interrupt methods in PythonEngine
21-
>>>>>>> Add GetNativeThreadID method in PythonEngine and different PyThreadState_SetAsyncExc calls for OS and Python version
2215

2316
### Changed
2417
- Drop support for Python 2, 3.4, and 3.5

src/embed_tests/TestInterrupt.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ public void Dispose()
3131
public void InterruptTest()
3232
{
3333
int runSimpleStringReturnValue = int.MinValue;
34-
ulong nativeThreadId = 0;
34+
ulong nativeThreadID = ulong.MinValue;
3535
Task.Factory.StartNew(() =>
3636
{
3737
using (Py.GIL())
3838
{
39-
nativeThreadId = PythonEngine.GetNativeThreadID();
39+
nativeThreadID = PythonEngine.GetNativeThreadID();
4040
runSimpleStringReturnValue = PythonEngine.RunSimpleString(@"
4141
import time
4242
@@ -47,13 +47,15 @@ import time
4747

4848
Thread.Sleep(200);
4949

50+
Assert.AreNotEqual(ulong.MinValue, nativeThreadID);
51+
5052
using (Py.GIL())
5153
{
52-
int interruptReturnValue = PythonEngine.Interrupt(nativeThreadId);
54+
int interruptReturnValue = PythonEngine.Interrupt(nativeThreadID);
5355
Assert.AreEqual(1, interruptReturnValue);
5456
}
5557

56-
Thread.Sleep(500);
58+
Thread.Sleep(300);
5759

5860
Assert.AreEqual(-1, runSimpleStringReturnValue);
5961
}

src/runtime/pythonengine.cs

Lines changed: 4 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,6 @@ namespace Python.Runtime
1212
/// </summary>
1313
public class PythonEngine : IDisposable
1414
{
15-
[DllImport("Kernel32", EntryPoint = "GetCurrentThreadId", ExactSpelling = true)]
16-
private static extern uint GetCurrentThreadId();
17-
18-
[DllImport("libc", EntryPoint = "pthread_self")]
19-
private static extern IntPtr pthread_selfLinux();
20-
21-
[DllImport("pthread", EntryPoint = "pthread_self", CallingConvention = CallingConvention.Cdecl)]
22-
private static extern ulong pthread_selfOSX();
23-
2415
public static ShutdownMode ShutdownMode
2516
{
2617
get => Runtime.ShutdownMode;
@@ -582,28 +573,8 @@ public static void Exec(string code, IntPtr? globals = null, IntPtr? locals = nu
582573
/// <returns>The native thread ID.</returns>
583574
public static ulong GetNativeThreadID()
584575
{
585-
if (Runtime.PyVersion >= new Version(3, 8))
586-
{
587-
dynamic threading = Py.Import("threading");
588-
return threading.get_native_id();
589-
}
590-
591-
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
592-
{
593-
return GetCurrentThreadId();
594-
}
595-
596-
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
597-
{
598-
return (ulong)pthread_selfLinux();
599-
}
600-
601-
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
602-
{
603-
return pthread_selfOSX();
604-
}
605-
606-
return 0;
576+
dynamic threading = Py.Import("threading");
577+
return threading.get_ident();
607578
}
608579

609580
/// <summary>
@@ -613,22 +584,12 @@ public static ulong GetNativeThreadID()
613584
/// <returns>The number of thread states modified; this is normally one, but will be zero if the thread id isn’t found.</returns>
614585
public static int Interrupt(ulong nativeThreadID)
615586
{
616-
if (Runtime.PyVersion >= new Version(3, 7))
617-
{
618-
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
619-
{
620-
return Runtime.PyThreadState_SetAsyncExc37Windows(nativeThreadID, Exceptions.KeyboardInterrupt);
621-
}
622-
623-
return Runtime.PyThreadState_SetAsyncExc37NonWindows((UIntPtr)nativeThreadID, Exceptions.KeyboardInterrupt);
624-
}
625-
626587
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
627588
{
628-
return Runtime.PyThreadState_SetAsyncExc36Windows((long)nativeThreadID, Exceptions.KeyboardInterrupt);
589+
return Runtime.PyThreadState_SetAsyncExcLLP64((uint)nativeThreadID, Exceptions.KeyboardInterrupt);
629590
}
630591

631-
return Runtime.PyThreadState_SetAsyncExc36NonWindows((IntPtr)nativeThreadID, Exceptions.KeyboardInterrupt);
592+
return Runtime.PyThreadState_SetAsyncExcLP64(nativeThreadID, Exceptions.KeyboardInterrupt);
632593
}
633594

634595
/// <summary>

src/runtime/runtime.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2147,16 +2147,10 @@ internal static void Py_CLEAR(ref IntPtr ob)
21472147
internal static extern int Py_AddPendingCall(IntPtr func, IntPtr arg);
21482148

21492149
[DllImport(_PythonDll, EntryPoint = "PyThreadState_SetAsyncExc", CallingConvention = CallingConvention.Cdecl)]
2150-
internal static extern int PyThreadState_SetAsyncExc37Windows(ulong id, IntPtr exc);
2150+
internal static extern int PyThreadState_SetAsyncExcLLP64(uint id, IntPtr exc);
21512151

21522152
[DllImport(_PythonDll, EntryPoint = "PyThreadState_SetAsyncExc", CallingConvention = CallingConvention.Cdecl)]
2153-
internal static extern int PyThreadState_SetAsyncExc36Windows(long id, IntPtr exc);
2154-
2155-
[DllImport(_PythonDll, EntryPoint = "PyThreadState_SetAsyncExc", CallingConvention = CallingConvention.Cdecl)]
2156-
internal static extern int PyThreadState_SetAsyncExc37NonWindows(UIntPtr id, IntPtr exc);
2157-
2158-
[DllImport(_PythonDll, EntryPoint = "PyThreadState_SetAsyncExc", CallingConvention = CallingConvention.Cdecl)]
2159-
internal static extern int PyThreadState_SetAsyncExc36NonWindows(IntPtr id, IntPtr exc);
2153+
internal static extern int PyThreadState_SetAsyncExcLP64(ulong id, IntPtr exc);
21602154

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

0 commit comments

Comments
 (0)