//Our implementation of WinSignalsHandler private bool WindowsEventHandler(uint winSignal) { bool retVal; int pySignal; switch (winSignal) { case CTRL_C_EVENT: pySignal = SIGINT; break; case CTRL_BREAK_EVENT: pySignal = SIGBREAK; break; case CTRL_CLOSE_EVENT: pySignal = SIGBREAK; break; case CTRL_LOGOFF_EVENT: pySignal = SIGBREAK; break; case CTRL_SHUTDOWN_EVENT: pySignal = SIGBREAK; break; default: throw new Exception("unreachable"); } lock (PySignalToPyHandler) { if (PySignalToPyHandler[pySignal].GetType() == typeof(int)) { int tempId = (int)PySignalToPyHandler[pySignal]; if (tempId == SIG_DFL) { //SIG_DFL - we let Windows do whatever it normally would retVal = false; } else if (tempId == SIG_IGN) { //SIG_IGN - we do nothing, but tell Windows we handled the signal retVal = true; } else { throw new Exception("unreachable"); } } else if (PySignalToPyHandler[pySignal] == default_int_handler) { if (pySignal != SIGINT) { //We're dealing with the default_int_handlerImpl which we //know doesn't care about the frame parameter retVal = true; default_int_handlerImpl(pySignal, null); } else { //Let the real interrupt handler throw a KeyboardInterrupt for SIGINT. //It handles this far more gracefully than we can retVal = false; } } else { //We're dealing with a callable matching PySignalHandler's signature retVal = true; PySignalHandler temp = (PySignalHandler)Converter.ConvertToDelegate(PySignalToPyHandler[pySignal], typeof(PySignalHandler)); try { if (SignalPythonContext.PythonOptions.Frames) { temp.Invoke(pySignal, SysModule._getframeImpl(null, 0, SignalPythonContext._mainThreadFunctionStack)); } else { temp.Invoke(pySignal, null); } } catch (Exception e) { System.Console.WriteLine(SignalPythonContext.FormatException(e)); } } } return(retVal); }
public static void warn(CodeContext context, object message, PythonType category = null, int stacklevel = 1) { PythonContext pContext = context.LanguageContext; List argv = pContext.GetSystemStateValue("argv") as List; if (PythonOps.IsInstance(message, PythonExceptions.Warning)) { category = DynamicHelpers.GetPythonType(message); } if (category == null) { category = PythonExceptions.UserWarning; } if (!category.IsSubclassOf(PythonExceptions.Warning)) { throw PythonOps.ValueError("category is not a subclass of Warning"); } TraceBackFrame caller = null; PythonDictionary globals; int lineno; if (context.LanguageContext.PythonOptions.Frames) { try { caller = SysModule._getframeImpl(context, stacklevel - 1); } catch (ValueErrorException) { } } if (caller == null) { globals = Builtin.globals(context) as PythonDictionary; lineno = 1; } else { globals = caller.f_globals; lineno = (int)caller.f_lineno; } string module; string filename; if (globals != null && globals.ContainsKey("__name__")) { module = (string)globals.get("__name__"); } else { module = "<string>"; } filename = globals.get("__file__") as string; if (filename == null || filename == "") { if (module == "__main__") { if (argv != null && argv.Count > 0) { filename = argv[0] as string; } else { // interpreter lacks sys.argv filename = "__main__"; } } if (filename == null || filename == "") { filename = module; } } PythonDictionary registry = (PythonDictionary)globals.setdefault("__warningregistry__", new PythonDictionary()); warn_explicit(context, message, category, filename, lineno, module, registry, globals); }
void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e) { int pySignal; switch (e.SpecialKey) { case ConsoleSpecialKey.ControlC: pySignal = SIGINT; break; case ConsoleSpecialKey.ControlBreak: pySignal = SIGBREAK; break; default: throw new InvalidOperationException("unreachable"); } lock (PySignalToPyHandler) { if (PySignalToPyHandler[pySignal].GetType() == typeof(int)) { int tempId = (int)PySignalToPyHandler[pySignal]; if (tempId == SIG_DFL) { //SIG_DFL - do whatever it normally would return; } else if (tempId == SIG_IGN) { //SIG_IGN - we do nothing, but tell the OS we handled the signal e.Cancel = false; return; } else { throw new Exception("unreachable"); } } else if (PySignalToPyHandler[pySignal] == default_int_handler) { if (pySignal != SIGINT) { //We're dealing with the default_int_handlerImpl which we //know doesn't care about the frame parameter e.Cancel = true; default_int_handlerImpl(pySignal, null); return; } else { //Let the real interrupt handler throw a KeyboardInterrupt for SIGINT. //It handles this far more gracefully than we can return; } } else { //We're dealing with a callable matching PySignalHandler's signature PySignalHandler temp = (PySignalHandler)Converter.ConvertToDelegate(PySignalToPyHandler[pySignal], typeof(PySignalHandler)); try { if (SignalPythonContext.PythonOptions.Frames) { temp.Invoke(pySignal, SysModule._getframeImpl(null, 0, SignalPythonContext._mainThreadFunctionStack)); } else { temp.Invoke(pySignal, null); } } catch (Exception ex) { System.Console.WriteLine(SignalPythonContext.FormatException(ex)); } e.Cancel = true; return; } } }