[pypy-svn] pypy default: Don't call RPython code from the Ctrl-C handler routine.

amauryfa commits-noreply at bitbucket.org
Mon Jan 17 18:06:24 CET 2011


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: 
Changeset: r40791:bf432d01d9f9
Date: 2011-01-17 18:04 +0100
http://bitbucket.org/pypy/pypy/changeset/bf432d01d9f9/

Log:	Don't call RPython code from the Ctrl-C handler routine. Just set
	the event in C code and avoid lots of troubles in
	"after_external_call".

diff --git a/pypy/module/rctime/interp_time.py b/pypy/module/rctime/interp_time.py
--- a/pypy/module/rctime/interp_time.py
+++ b/pypy/module/rctime/interp_time.py
@@ -24,43 +24,37 @@
     from pypy.interpreter.error import wrap_windowserror, wrap_oserror
     from pypy.module.thread import ll_thread as thread
 
-    # This is needed because the handler function must have the "WINAPI"
-    # calling convention, which is not supported by lltype.Ptr.
-    CtrlHandler_type = lltype.Ptr(lltype.FuncType([], rwin32.BOOL))
     eci = ExternalCompilationInfo(
         separate_module_sources=['''
             #include <windows.h>
 
-            static BOOL (*CtrlHandlerRoutine)(
-                DWORD dwCtrlType);
+            static HANDLE interrupt_event;
 
-            static BOOL WINAPI winapi_CtrlHandlerRoutine(
+            static BOOL WINAPI CtrlHandlerRoutine(
               DWORD dwCtrlType)
             {
-                return CtrlHandlerRoutine(dwCtrlType);
+                SetEvent(interrupt_event);
+                /* allow other default handlers to be called.
+                 * Default Python handler will setup the
+                 * KeyboardInterrupt exception.
+                 */
+                return 0;
             }
 
-            BOOL pypy_timemodule_setCtrlHandlerRoutine(BOOL (*f)(DWORD))
+            BOOL pypy_timemodule_setCtrlHandler(HANDLE event)
             {
-                CtrlHandlerRoutine = f;
-                SetConsoleCtrlHandler(winapi_CtrlHandlerRoutine, TRUE);
+                interrupt_event = event;
+                return SetConsoleCtrlHandler(CtrlHandlerRoutine, TRUE);
             }
 
         '''],
-        export_symbols=['pypy_timemodule_setCtrlHandlerRoutine'],
+        export_symbols=['pypy_timemodule_setCtrlHandler'],
         )
     _setCtrlHandlerRoutine = rffi.llexternal(
-        'pypy_timemodule_setCtrlHandlerRoutine',
-        [CtrlHandler_type], rwin32.BOOL,
+        'pypy_timemodule_setCtrlHandler',
+        [rwin32.HANDLE], rwin32.BOOL,
         compilation_info=eci)
 
-    def ProcessingCtrlHandler():
-        rwin32.SetEvent(globalState.interrupt_event)
-        # allow other default handlers to be called.
-        # Default Python handler will setup the
-        # KeyboardInterrupt exception.
-        return 0
-
     class GlobalState:
         def __init__(self):
             self.init()
@@ -75,7 +69,7 @@
                     rffi.NULL, True, False, rffi.NULL)
             except WindowsError, e:
                 raise wrap_windowserror(space, e)
-            if not _setCtrlHandlerRoutine(ProcessingCtrlHandler):
+            if not _setCtrlHandlerRoutine(globalState.interrupt_event):
                 raise wrap_windowserror(
                     space, rwin32.lastWindowsError("SetConsoleCtrlHandler"))
 


More information about the Pypy-commit mailing list