Debian Codegraph @

src/cffi/_cffi_errors.h

raw
1 #ifndef CFFI_MESSAGEBOX
2 # ifdef _MSC_VER
3 # define CFFI_MESSAGEBOX 1
4 # else
5 # define CFFI_MESSAGEBOX 0
6 # endif
7 #endif
8
9
10 #if CFFI_MESSAGEBOX
11 /* Windows only: logic to take the Python-CFFI embedding logic
12 initialization errors and display them in a background thread
13 with MessageBox. The idea is that if the whole program closes
14 as a result of this problem, then likely it is already a console
15 program and you can read the stderr output in the console too.
16 If it is not a console program, then it will likely show its own
17 dialog to complain, or generally not abruptly close, and for this
18 case the background thread should stay alive.
19 */
20 static void *volatile _cffi_bootstrap_text;
21
22 static PyObject *_cffi_start_error_capture(void)
23 {
24 PyObject *result = NULL;
25 PyObject *x, *m, *bi;
26
27 if (InterlockedCompareExchangePointer(&_cffi_bootstrap_text,
28 (void *)1, NULL) != NULL)
29 return (PyObject *)1;
30
31 m = PyImport_AddModule("_cffi_error_capture");
32 if (m == NULL)
33 goto error;
34
35 result = PyModule_GetDict(m);
36 if (result == NULL)
37 goto error;
38
39 bi = PyImport_ImportModule("builtins");
40 if (bi == NULL)
41 goto error;
42 PyDict_SetItemString(result, "__builtins__", bi);
43 Py_DECREF(bi);
44
45 x = PyRun_String(
46 "import sys\n"
47 "class FileLike:\n"
48 " def write(self, x):\n"
49 " try:\n"
50 " of.write(x)\n"
51 " except: pass\n"
52 " self.buf += x\n"
53 " def flush(self):\n"
54 " pass\n"
55 "fl = FileLike()\n"
56 "fl.buf = ''\n"
57 "of = sys.stderr\n"
58 "sys.stderr = fl\n"
59 "def done():\n"
60 " sys.stderr = of\n"
61 " return fl.buf\n", /* make sure the returned value stays alive */
62 Py_file_input,
63 result, result);
64 Py_XDECREF(x);
65
66 error:
67 if (PyErr_Occurred())
68 {
69 PyErr_WriteUnraisable(Py_None);
70 PyErr_Clear();
71 }
72 return result;
73 }
74
75 #pragma comment(lib, "user32.lib")
76
77 static DWORD WINAPI _cffi_bootstrap_dialog(LPVOID ignored)
78 {
79 Sleep(666); /* may be interrupted if the whole process is closing */
80 MessageBoxW(NULL, (wchar_t *)_cffi_bootstrap_text,
81 L"Python-CFFI error",
82 MB_OK | MB_ICONERROR);
83 _cffi_bootstrap_text = NULL;
84 return 0;
85 }
86
87 static void _cffi_stop_error_capture(PyObject *ecap)
88 {
89 PyObject *s;
90 void *text;
91
92 if (ecap == (PyObject *)1)
93 return;
94
95 if (ecap == NULL)
96 goto error;
97
98 s = PyRun_String("done()", Py_eval_input, ecap, ecap);
99 if (s == NULL)
100 goto error;
101
102 /* Show a dialog box, but in a background thread, and
103 never show multiple dialog boxes at once. */
104 text = PyUnicode_AsWideCharString(s, NULL);
105
106 _cffi_bootstrap_text = text;
107
108 if (text != NULL)
109 {
110 HANDLE h;
111 h = CreateThread(NULL, 0, _cffi_bootstrap_dialog,
112 NULL, 0, NULL);
113 if (h != NULL)
114 CloseHandle(h);
115 }
116 /* decref the string, but it should stay alive as 'fl.buf'
117 in the small module above. It will really be freed only if
118 we later get another similar error. So it's a leak of at
119 most one copy of the small module. That's fine for this
120 situation which is usually a "fatal error" anyway. */
121 Py_DECREF(s);
122 PyErr_Clear();
123 return;
124
125 error:
126 _cffi_bootstrap_text = NULL;
127 PyErr_Clear();
128 }
129
130 #else
131
132 static PyObject *_cffi_start_error_capture(void) { return NULL; }
133 static void _cffi_stop_error_capture(PyObject *ecap) { }
134
135 #endif