| 111 | } |
| 112 | |
| 113 | static void gc() { |
| 114 | // Force garbage collection to ensure any pending destructors are invoked: |
| 115 | #if defined(PYPY_VERSION) |
| 116 | PyObject *globals = PyEval_GetGlobals(); |
| 117 | PyObject *result = PyRun_String("import gc\n" |
| 118 | "for i in range(2):\n" |
| 119 | " gc.collect()\n", |
| 120 | Py_file_input, |
| 121 | globals, |
| 122 | globals); |
| 123 | if (result == nullptr) |
| 124 | throw py::error_already_set(); |
| 125 | Py_DECREF(result); |
| 126 | #else |
| 127 | py::module_::import("gc").attr("collect")(); |
| 128 | #endif |
| 129 | } |
| 130 | |
| 131 | int alive() { |
| 132 | gc(); |