_cffi_errors.h 3.5 KB

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