| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#ifdef __cplusplus |
| 5 |
extern "C" { |
| 6 |
#endif |
| 7 |
|
| 8 |
|
| 9 |
#if defined(_WIN32) |
| 10 |
# define CFFI_DLLEXPORT __declspec(dllexport) |
| 11 |
#elif defined(__GNUC__) |
| 12 |
# define CFFI_DLLEXPORT __attribute__((visibility("default"))) |
| 13 |
#else |
| 14 |
# define CFFI_DLLEXPORT |
| 15 |
#endif |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
#undef _cffi_call_python |
| 35 |
typedef void (*_cffi_call_python_fnptr)(struct _cffi_externpy_s *, char *); |
| 36 |
static void _cffi_start_and_call_python(struct _cffi_externpy_s *, char *); |
| 37 |
static _cffi_call_python_fnptr _cffi_call_python = &_cffi_start_and_call_python; |
| 38 |
|
| 39 |
|
| 40 |
#ifndef _MSC_VER |
| 41 |
|
| 42 |
# define cffi_compare_and_swap(l,o,n) __sync_bool_compare_and_swap(l,o,n) |
| 43 |
# define cffi_write_barrier() __sync_synchronize() |
| 44 |
# if !defined(__amd64__) && !defined(__x86_64__) && \ |
| 45 |
!defined(__i386__) && !defined(__i386) |
| 46 |
# define cffi_read_barrier() __sync_synchronize() |
| 47 |
# else |
| 48 |
# define cffi_read_barrier() (void)0 |
| 49 |
# endif |
| 50 |
#else |
| 51 |
|
| 52 |
# include <Windows.h> |
| 53 |
# define cffi_compare_and_swap(l,o,n) \ |
| 54 |
(InterlockedCompareExchangePointer(l,n,o) == (o)) |
| 55 |
# define cffi_write_barrier() InterlockedCompareExchange(&_cffi_dummy,0,0) |
| 56 |
# define cffi_read_barrier() (void)0 |
| 57 |
static volatile LONG _cffi_dummy; |
| 58 |
#endif |
| 59 |
|
| 60 |
#ifdef WITH_THREAD |
| 61 |
# ifndef _MSC_VER |
| 62 |
# include <pthread.h> |
| 63 |
static pthread_mutex_t _cffi_embed_startup_lock; |
| 64 |
# else |
| 65 |
static CRITICAL_SECTION _cffi_embed_startup_lock; |
| 66 |
# endif |
| 67 |
static char _cffi_embed_startup_lock_ready = 0; |
| 68 |
#endif |
| 69 |
|
| 70 |
static void _cffi_acquire_reentrant_mutex(void) |
| 71 |
{ |
| 72 |
static void *volatile lock = NULL; |
| 73 |
|
| 74 |
while (!cffi_compare_and_swap(&lock, NULL, (void *)1)) { |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
} |
| 80 |
|
| 81 |
#ifdef WITH_THREAD |
| 82 |
if (!_cffi_embed_startup_lock_ready) { |
| 83 |
# ifndef _MSC_VER |
| 84 |
pthread_mutexattr_t attr; |
| 85 |
pthread_mutexattr_init(&attr); |
| 86 |
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); |
| 87 |
pthread_mutex_init(&_cffi_embed_startup_lock, &attr); |
| 88 |
# else |
| 89 |
InitializeCriticalSection(&_cffi_embed_startup_lock); |
| 90 |
# endif |
| 91 |
_cffi_embed_startup_lock_ready = 1; |
| 92 |
} |
| 93 |
#endif |
| 94 |
|
| 95 |
while (!cffi_compare_and_swap(&lock, (void *)1, NULL)) |
| 96 |
; |
| 97 |
|
| 98 |
#ifndef _MSC_VER |
| 99 |
pthread_mutex_lock(&_cffi_embed_startup_lock); |
| 100 |
#else |
| 101 |
EnterCriticalSection(&_cffi_embed_startup_lock); |
| 102 |
#endif |
| 103 |
} |
| 104 |
|
| 105 |
static void _cffi_release_reentrant_mutex(void) |
| 106 |
{ |
| 107 |
#ifndef _MSC_VER |
| 108 |
pthread_mutex_unlock(&_cffi_embed_startup_lock); |
| 109 |
#else |
| 110 |
LeaveCriticalSection(&_cffi_embed_startup_lock); |
| 111 |
#endif |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
#ifndef PYPY_VERSION |
| 117 |
|
| 118 |
#include "_cffi_errors.h" |
| 119 |
|
| 120 |
|
| 121 |
#define _cffi_call_python_org _cffi_exports[_CFFI_CPIDX] |
| 122 |
|
| 123 |
PyMODINIT_FUNC _CFFI_PYTHON_STARTUP_FUNC(void); |
| 124 |
|
| 125 |
static void _cffi_py_initialize(void) |
| 126 |
{ |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
Py_InitializeEx(0); |
| 139 |
} |
| 140 |
|
| 141 |
static int _cffi_initialize_python(void) |
| 142 |
{ |
| 143 |
|
| 144 |
|
| 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 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
(void)_CFFI_PYTHON_STARTUP_FUNC(); |
| 161 |
if (PyErr_Occurred()) |
| 162 |
goto error; |
| 163 |
|
| 164 |
|
| 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 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
result = 0; |
| 197 |
done: |
| 198 |
Py_XDECREF(pycode); |
| 199 |
Py_XDECREF(global_dict); |
| 200 |
PyGILState_Release(state); |
| 201 |
return result; |
| 202 |
|
| 203 |
error:; |
| 204 |
{ |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
PyObject *ecap; |
| 209 |
PyObject *exception, *v, *tb, *f, *modules, *mod; |
| 210 |
PyErr_Fetch(&exception, &v, &tb); |
| 211 |
ecap = _cffi_start_error_capture(); |
| 212 |
f = PySys_GetObject((char *)"stderr"); |
| 213 |
if (f != NULL && f != Py_None) { |
| 214 |
PyFile_WriteString( |
| 215 |
"Failed to initialize the Python-CFFI embedding logic:\n\n", f); |
| 216 |
} |
| 217 |
|
| 218 |
if (exception != NULL) { |
| 219 |
PyErr_NormalizeException(&exception, &v, &tb); |
| 220 |
PyErr_Display(exception, v, tb); |
| 221 |
} |
| 222 |
Py_XDECREF(exception); |
| 223 |
Py_XDECREF(v); |
| 224 |
Py_XDECREF(tb); |
| 225 |
|
| 226 |
if (f != NULL && f != Py_None) { |
| 227 |
PyFile_WriteString("\nFrom: " _CFFI_MODULE_NAME |
| 228 |
"\ncompiled with cffi version: 1.18.0.dev0" |
| 229 |
"\n_cffi_backend module: ", f); |
| 230 |
modules = PyImport_GetModuleDict(); |
| 231 |
mod = PyDict_GetItemString(modules, "_cffi_backend"); |
| 232 |
if (mod == NULL) { |
| 233 |
PyFile_WriteString("not loaded", f); |
| 234 |
} |
| 235 |
else { |
| 236 |
v = PyObject_GetAttrString(mod, "__file__"); |
| 237 |
PyFile_WriteObject(v, f, 0); |
| 238 |
Py_XDECREF(v); |
| 239 |
} |
| 240 |
PyFile_WriteString("\nsys.path: ", f); |
| 241 |
PyFile_WriteObject(PySys_GetObject((char *)"path"), f, 0); |
| 242 |
PyFile_WriteString("\n\n", f); |
| 243 |
} |
| 244 |
_cffi_stop_error_capture(ecap); |
| 245 |
} |
| 246 |
result = -1; |
| 247 |
goto done; |
| 248 |
} |
| 249 |
|
| 250 |
#if PY_VERSION_HEX < 0x03080000 |
| 251 |
PyAPI_DATA(char *) _PyParser_TokenNames[]; |
| 252 |
#endif |
| 253 |
|
| 254 |
static int _cffi_carefully_make_gil(void) |
| 255 |
{ |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
#ifdef WITH_THREAD |
| 298 |
# if PY_VERSION_HEX < 0x03080000 |
| 299 |
char *volatile *lock = (char *volatile *)_PyParser_TokenNames; |
| 300 |
char *old_value, *locked_value; |
| 301 |
|
| 302 |
while (1) { |
| 303 |
old_value = *lock; |
| 304 |
locked_value = old_value + 1; |
| 305 |
if (old_value[0] == 'E') { |
| 306 |
assert(old_value[1] == 'N'); |
| 307 |
if (cffi_compare_and_swap(lock, old_value, locked_value)) |
| 308 |
break; |
| 309 |
} |
| 310 |
else { |
| 311 |
assert(old_value[0] == 'N'); |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
} |
| 317 |
} |
| 318 |
# else |
| 319 |
# if PY_VERSION_HEX < 0x030C0000 |
| 320 |
int volatile *lock = (int volatile *)&PyCapsule_Type.tp_version_tag; |
| 321 |
int old_value, locked_value = -42; |
| 322 |
assert(!(PyCapsule_Type.tp_flags & Py_TPFLAGS_HAVE_VERSION_TAG)); |
| 323 |
# else |
| 324 |
static struct ebp_s { PyBufferProcs buf; int mark; } empty_buffer_procs; |
| 325 |
empty_buffer_procs.mark = -42; |
| 326 |
PyBufferProcs *volatile *lock = (PyBufferProcs *volatile *) |
| 327 |
&PyCapsule_Type.tp_as_buffer; |
| 328 |
PyBufferProcs *old_value, *locked_value = &empty_buffer_procs.buf; |
| 329 |
# endif |
| 330 |
|
| 331 |
while (1) { |
| 332 |
old_value = *lock; |
| 333 |
if (old_value == 0) { |
| 334 |
if (cffi_compare_and_swap(lock, old_value, locked_value)) |
| 335 |
break; |
| 336 |
} |
| 337 |
else { |
| 338 |
# if PY_VERSION_HEX < 0x030C0000 |
| 339 |
assert(old_value == locked_value); |
| 340 |
# else |
| 341 |
|
| 342 |
|
| 343 |
assert(((struct ebp_s *)old_value)->mark == -42); |
| 344 |
# endif |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
} |
| 350 |
} |
| 351 |
# endif |
| 352 |
#endif |
| 353 |
|
| 354 |
|
| 355 |
if (!Py_IsInitialized()) { |
| 356 |
_cffi_py_initialize(); |
| 357 |
#if PY_VERSION_HEX < 0x03070000 |
| 358 |
PyEval_InitThreads(); |
| 359 |
#endif |
| 360 |
PyEval_SaveThread(); |
| 361 |
|
| 362 |
|
| 363 |
} |
| 364 |
else { |
| 365 |
#if PY_VERSION_HEX < 0x03070000 |
| 366 |
|
| 367 |
PyGILState_STATE state = PyGILState_Ensure(); |
| 368 |
PyEval_InitThreads(); |
| 369 |
PyGILState_Release(state); |
| 370 |
#endif |
| 371 |
} |
| 372 |
|
| 373 |
#ifdef WITH_THREAD |
| 374 |
|
| 375 |
while (!cffi_compare_and_swap(lock, locked_value, old_value)) |
| 376 |
; |
| 377 |
#endif |
| 378 |
|
| 379 |
return 0; |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
#else |
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
PyMODINIT_FUNC _CFFI_PYTHON_STARTUP_FUNC(const void *[]); |
| 391 |
|
| 392 |
static struct _cffi_pypy_init_s { |
| 393 |
const char *name; |
| 394 |
void *func; |
| 395 |
const char *code; |
| 396 |
} _cffi_pypy_init = { |
| 397 |
_CFFI_MODULE_NAME, |
| 398 |
_CFFI_PYTHON_STARTUP_FUNC, |
| 399 |
_CFFI_PYTHON_STARTUP_CODE, |
| 400 |
}; |
| 401 |
|
| 402 |
extern int pypy_carefully_make_gil(const char *); |
| 403 |
extern int pypy_init_embedded_cffi_module(int, struct _cffi_pypy_init_s *); |
| 404 |
|
| 405 |
static int _cffi_carefully_make_gil(void) |
| 406 |
{ |
| 407 |
return pypy_carefully_make_gil(_CFFI_MODULE_NAME); |
| 408 |
} |
| 409 |
|
| 410 |
static int _cffi_initialize_python(void) |
| 411 |
{ |
| 412 |
return pypy_init_embedded_cffi_module(0xB011, &_cffi_pypy_init); |
| 413 |
} |
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
#endif |
| 419 |
|
| 420 |
|
| 421 |
#ifdef __GNUC__ |
| 422 |
__attribute__((noinline)) |
| 423 |
#endif |
| 424 |
static _cffi_call_python_fnptr _cffi_start_python(void) |
| 425 |
{ |
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
static char called = 0; |
| 456 |
|
| 457 |
if (_cffi_carefully_make_gil() != 0) |
| 458 |
return NULL; |
| 459 |
|
| 460 |
_cffi_acquire_reentrant_mutex(); |
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
if (!called) { |
| 468 |
called = 1; |
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
if (_cffi_initialize_python() == 0) { |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
cffi_write_barrier(); |
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
assert(_cffi_call_python_org != NULL); |
| 488 |
_cffi_call_python = (_cffi_call_python_fnptr)_cffi_call_python_org; |
| 489 |
} |
| 490 |
else { |
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
_cffi_call_python_org = NULL; |
| 496 |
} |
| 497 |
} |
| 498 |
|
| 499 |
_cffi_release_reentrant_mutex(); |
| 500 |
|
| 501 |
return (_cffi_call_python_fnptr)_cffi_call_python_org; |
| 502 |
} |
| 503 |
|
| 504 |
static |
| 505 |
void _cffi_start_and_call_python(struct _cffi_externpy_s *externpy, char *args) |
| 506 |
{ |
| 507 |
_cffi_call_python_fnptr fnptr; |
| 508 |
int current_err = errno; |
| 509 |
#ifdef _MSC_VER |
| 510 |
int current_lasterr = GetLastError(); |
| 511 |
#endif |
| 512 |
fnptr = _cffi_start_python(); |
| 513 |
if (fnptr == NULL) { |
| 514 |
fprintf(stderr, "function %s() called, but initialization code " |
| 515 |
"failed. Returning 0.\n", externpy->name); |
| 516 |
memset(args, 0, externpy->size_of_result); |
| 517 |
} |
| 518 |
#ifdef _MSC_VER |
| 519 |
SetLastError(current_lasterr); |
| 520 |
#endif |
| 521 |
errno = current_err; |
| 522 |
|
| 523 |
if (fnptr != NULL) |
| 524 |
fnptr(externpy, args); |
| 525 |
} |
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
_CFFI_UNUSED_FN |
| 534 |
static int cffi_start_python(void) |
| 535 |
{ |
| 536 |
if (_cffi_call_python == &_cffi_start_and_call_python) { |
| 537 |
if (_cffi_start_python() == NULL) |
| 538 |
return -1; |
| 539 |
} |
| 540 |
cffi_read_barrier(); |
| 541 |
return 0; |
| 542 |
} |
| 543 |
|
| 544 |
#undef cffi_compare_and_swap |
| 545 |
#undef cffi_write_barrier |
| 546 |
#undef cffi_read_barrier |
| 547 |
|
| 548 |
#ifdef __cplusplus |
| 549 |
} |
| 550 |
#endif |