Browse files

Implement thread.interrupt_main

  • Loading branch information...
1 parent de89b60 commit 33d5472eaaa81329059707de38cbabd5a1151048 @DinoV DinoV committed May 3, 2011
View
15 Languages/IronPython/IronPython.Modules/thread.cs
@@ -68,9 +68,18 @@ public static class PythonThread {
return t.ManagedThreadId;
}
- public static void interrupt_main() {
- throw PythonOps.NotImplementedError("interrupt_main not implemented");
- //throw new PythonKeyboardInterrupt();
+ /// <summary>
+ /// Stops execution of Python or other .NET code on the main thread. If the thread is
+ /// blocked in native code the thread will be interrupted after it returns back to Python
+ /// or other .NET code.
+ /// </summary>
+ public static void interrupt_main(CodeContext context) {
+ var thread = context.LanguageContext.MainThread;
+ if (thread != null) {
+ thread.Abort(new KeyboardInterruptException(""));
+ } else {
+ throw PythonOps.SystemError("no main thread has been registered");
+ }
}
public static void exit() {
View
1 Languages/IronPython/IronPython/Hosting/PythonCommandLine.cs
@@ -200,6 +200,7 @@ public sealed class PythonCommandLine : CommandLine {
}
PythonContext.InsertIntoPath(0, fullPath);
+ PythonContext.MainThread = Thread.CurrentThread;
}
protected override Scope/*!*/ CreateScope() {
View
13 Languages/IronPython/IronPython/Runtime/PythonContext.cs
@@ -203,6 +203,7 @@ public sealed partial class PythonContext : LanguageContext {
internal readonly List<FunctionStack> _mainThreadFunctionStack;
private CallSite<Func<CallSite, CodeContext, object, object>> _callSite0LightEh;
private List<WeakReference> _weakExtensionMethodSets;
+ private Thread _mainThread;
#region Generated Python Shared Call Sites Storage
@@ -399,6 +400,18 @@ public PythonContext(ScriptDomainManager/*!*/ manager, IDictionary<string, objec
}
}
+ /// <summary>
+ /// Gets or sets the main thread which should be interupted by thread.interrupt_main
+ /// </summary>
+ public Thread MainThread {
+ get {
+ return _mainThread;
+ }
+ set {
+ _mainThread = value;
+ }
+ }
+
public IEqualityComparer<object>/*!*/ EqualityComparer {
get { return _equalityComparer; }
}

0 comments on commit 33d5472

Please sign in to comment.