| 139 | } |
| 140 | |
| 141 | static int _cffi_initialize_python(void) |
| 142 | { |
| 143 | /* This initializes Python, imports _cffi_backend, and then the |
| 144 | present .dll/.so is set up as a CPython C extension module. |
| 145 | */ |
| 146 | int result; |
| 147 | PyGILState_STATE state; |
| 148 | PyObject *pycode=NULL, *global_dict=NULL, *x; |
| 149 | PyObject *builtins; |
| 150 | |
| 151 | state = PyGILState_Ensure(); |
| 152 | |
| 153 | /* Call the initxxx() function from the present module. It will |
| 154 | create and initialize us as a CPython extension module, instead |
| 155 | of letting the startup Python code do it---it might reimport |
| 156 | the same .dll/.so and get maybe confused on some platforms. |
| 157 | It might also have troubles locating the .dll/.so again for all |
| 158 | I know. |
| 159 | */ |
| 160 | (void)_CFFI_PYTHON_STARTUP_FUNC(); |
| 161 | if (PyErr_Occurred()) |
| 162 | goto error; |
| 163 | |
| 164 | /* Now run the Python code provided to ffi.embedding_init_code(). |
| 165 | */ |
| 166 | pycode = Py_CompileString(_CFFI_PYTHON_STARTUP_CODE, |
| 167 | "<init code for '" _CFFI_MODULE_NAME "'>", |
| 168 | Py_file_input); |
| 169 | if (pycode == NULL) |
| 170 | goto error; |
| 171 | global_dict = PyDict_New(); |
| 172 | if (global_dict == NULL) |
| 173 | goto error; |
| 174 | builtins = PyEval_GetBuiltins(); |
| 175 | if (builtins == NULL) |
| 176 | goto error; |
| 177 | if (PyDict_SetItemString(global_dict, "__builtins__", builtins) < 0) |
| 178 | goto error; |
| 179 | x = PyEval_EvalCode( |
| 180 | #if PY_MAJOR_VERSION < 3 |
| 181 | (PyCodeObject *) |
| 182 | #endif |
| 183 | pycode, global_dict, global_dict); |
| 184 | if (x == NULL) |
| 185 | goto error; |
| 186 | Py_DECREF(x); |
| 187 | |
| 188 | /* Done! Now if we've been called from |
| 189 | _cffi_start_and_call_python() in an ``extern "Python"``, we can |
| 190 | only hope that the Python code did correctly set up the |
| 191 | corresponding @ffi.def_extern() function. Otherwise, the |
| 192 | general logic of ``extern "Python"`` functions (inside the |
| 193 | _cffi_backend module) will find that the reference is still |
| 194 | missing and print an error. |
| 195 | */ |
| 196 | result = 0; |
| 197 | done: |
| 198 | Py_XDECREF(pycode); |
no test coverage detected