Debian Codegraph @

pypy/module/_cffi_backend/errorbox.py

raw symbols SCIP
1 # for Windows only
2 import sys
3 from rpython.rlib import jit
4 from rpython.rtyper.lltypesystem import lltype, rffi
5 from rpython.translator.tool.cbuild import ExternalCompilationInfo
6
7
8 MESSAGEBOX = sys.platform == "win32"
9
10 MODULE = r"""
11 #include <Windows.h>
12 #pragma comment(lib, "user32.lib")
13
14 static void *volatile _cffi_bootstrap_text;
15
16 RPY_EXTERN int _cffi_errorbox1(void)
17 {
18 return InterlockedCompareExchangePointer(&_cffi_bootstrap_text,
19 (void *)1, NULL) == NULL;
20 }
21
22 static DWORD WINAPI _cffi_bootstrap_dialog(LPVOID ignored)
23 {
24 Sleep(666); /* may be interrupted if the whole process is closing */
25 MessageBoxW(NULL, (wchar_t *)_cffi_bootstrap_text,
26 L"PyPy: Python-CFFI error",
27 MB_OK | MB_ICONERROR);
28 _cffi_bootstrap_text = NULL;
29 return 0;
30 }
31
32 RPY_EXTERN void _cffi_errorbox(wchar_t *text)
33 {
34 /* Show a dialog box, but in a background thread, and
35 never show multiple dialog boxes at once. */
36 HANDLE h;
37
38 _cffi_bootstrap_text = text;
39 h = CreateThread(NULL, 0, _cffi_bootstrap_dialog,
40 NULL, 0, NULL);
41 if (h != NULL)
42 CloseHandle(h);
43 }
44 """
45
46 if MESSAGEBOX:
47
48 eci = ExternalCompilationInfo(
49 separate_module_sources=[MODULE],
50 post_include_bits=["#include <wchar.h>\n",
51 "RPY_EXTERN int _cffi_errorbox1(void);\n",
52 "RPY_EXTERN void _cffi_errorbox(wchar_t *);\n"])
53
54 cffi_errorbox1 = rffi.llexternal("_cffi_errorbox1", [],
55 rffi.INT, compilation_info=eci)
56 cffi_errorbox = rffi.llexternal("_cffi_errorbox", [rffi.CWCHARP],
57 lltype.Void, compilation_info=eci)
58
59 class Message:
60 def __init__(self, space):
61 self.space = space
62 self.text_p = lltype.nullptr(rffi.CWCHARP.TO)
63
64 def start_error_capture(self):
65 ok = cffi_errorbox1()
66 if rffi.cast(lltype.Signed, ok) != 1:
67 return None
68
69 return self.space.appexec([], """():
70 import sys
71 class FileLike:
72 def write(self, x):
73 try:
74 of.write(x)
75 except:
76 pass
77 self.buf += x
78 fl = FileLike()
79 fl.buf = ''
80 of = sys.stderr
81 sys.stderr = fl
82 def done():
83 sys.stderr = of
84 return fl.buf
85 return done
86 """)
87
88 def stop_error_capture(self, w_done):
89 if w_done is None:
90 return
91
92 w_text = self.space.call_function(w_done)
93 p = rffi.utf82wcharp(self.space.utf8_w(w_text),
94 self.space.len_w(w_text),
95 track_allocation=False)
96 if self.text_p:
97 rffi.free_wcharp(self.text_p, track_allocation=False)
98 self.text_p = p # keepalive
99
100 cffi_errorbox(p)
101
102
103 @jit.dont_look_inside
104 def start_error_capture(space):
105 msg = space.fromcache(Message)
106 return msg.start_error_capture()
107
108 @jit.dont_look_inside
109 def stop_error_capture(space, x):
110 msg = space.fromcache(Message)
111 msg.stop_error_capture(x)
112
113 else:
114 def start_error_capture(space):
115 return None
116 def stop_error_capture(space, nothing):
117 pass