Changeset 2230
- Timestamp:
- 07/01/07 08:45:42 (1 year ago)
- Files:
-
- plugins/python-module/src/python_module.c (modified) (14 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plugins/python-module/src/python_module.c
r1788 r2230 36 36 37 37 typedef struct MemberData { 38 PyThreadState *interp_thread;39 38 PyObject *osync_module; 40 39 PyObject *module; … … 170 169 osync_bool report_error = TRUE; 171 170 172 Py Eval_AcquireThread(data->interp_thread);171 PyGILState_STATE pystate = PyGILState_Ensure(); 173 172 174 173 PyObject *pyinfo = pm_make_info(data->osync_module, info, &error); … … 205 204 Py_DECREF(pycontext); 206 205 Py_DECREF(ret); 207 Py Eval_ReleaseThread(data->interp_thread);206 PyGILState_Release(pystate); 208 207 osync_context_report_success(ctx); 209 208 osync_trace(TRACE_EXIT, "%s", __func__); … … 260 259 261 260 error: 262 Py Eval_ReleaseThread(data->interp_thread);261 PyGILState_Release(pystate); 263 262 if (report_error) 264 263 osync_context_report_osyncerror(ctx, error); … … 333 332 osync_plugin_set_data(plugin, NULL); 334 333 335 data->interp_thread = Py_NewInterpreter(); 336 if (!data->interp_thread) { 337 PYERR_CLEAR(); 338 osync_error_set(error, OSYNC_ERROR_GENERIC, "Couldn't initialize python sub interpreter"); 339 free(modulename); 340 goto error; 341 } 334 PyGILState_STATE pystate = PyGILState_Ensure(); 342 335 343 336 if (!(data->module = PyImport_ImportModule(modulename))) { … … 376 369 } 377 370 378 PyEval_ReleaseThread(data->interp_thread); 379 371 PyGILState_Release(pystate); 380 372 return data; 381 373 … … 383 375 Py_XDECREF(data->module); 384 376 Py_XDECREF(data->osync_module); 385 if (data->interp_thread) 386 Py_EndInterpreter(data->interp_thread); 377 PyGILState_Release(pystate); 387 378 free(data); 388 379 return NULL; … … 393 384 MemberData *data = data_in; 394 385 395 Py Eval_AcquireThread(data->interp_thread);386 PyGILState_STATE pystate = PyGILState_Ensure(); 396 387 397 388 PyObject *pyinfo = pm_make_info(data->osync_module, info, error); … … 405 396 406 397 Py_DECREF(ret); 407 Py Eval_ReleaseThread(data->interp_thread);398 PyGILState_Release(pystate); 408 399 return TRUE; 409 400 … … 411 402 osync_error_set(error, OSYNC_ERROR_GENERIC, "Couldn't call discover method"); 412 403 PYERR_CLEAR(); 413 Py Eval_ReleaseThread(data->interp_thread);404 PyGILState_Release(pystate); 414 405 return FALSE; 415 406 } … … 419 410 osync_trace(TRACE_ENTRY, "%s(%p)", __func__, data); 420 411 MemberData *mydata = data; 421 Py Eval_AcquireThread(mydata->interp_thread);412 PyGILState_STATE pystate = PyGILState_Ensure(); 422 413 423 414 /* free all sink objects */ … … 429 420 Py_DECREF(mydata->module); 430 421 Py_DECREF(mydata->osync_module); 431 Py_EndInterpreter(mydata->interp_thread);432 422 free(mydata); 423 PyGILState_Release(pystate); 433 424 osync_trace(TRACE_EXIT, "%s", __func__); 434 425 } … … 528 519 } 529 520 521 /* set python search path to look in our module directory first */ 522 static osync_bool set_search_path(OSyncError **error) 523 { 524 PyObject *sys_module = PyImport_ImportModule("sys"); 525 if (!sys_module) { 526 osync_error_set(error, OSYNC_ERROR_GENERIC, "Couldn't import sys module"); 527 PYERR_CLEAR(); 528 return FALSE; 529 } 530 531 PyObject *path = PyObject_GetAttrString(sys_module, "path"); 532 if (!path) { 533 osync_error_set(error, OSYNC_ERROR_GENERIC, "sys module has no path attribute?"); 534 PYERR_CLEAR(); 535 Py_DECREF(sys_module); 536 return FALSE; 537 } 538 539 if (!PyList_Check(path)) { 540 osync_error_set(error, OSYNC_ERROR_GENERIC, "sys.path is not a list?"); 541 Py_DECREF(sys_module); 542 Py_DECREF(path); 543 return FALSE; 544 } 545 546 PyObject *plugindir = Py_BuildValue("s", OPENSYNC_PYTHONPLG_DIR); 547 if (!plugindir) { 548 osync_error_set(error, OSYNC_ERROR_GENERIC, "Error constructing plugindir string for sys.path"); 549 PYERR_CLEAR(); 550 Py_DECREF(sys_module); 551 Py_DECREF(path); 552 return FALSE; 553 } 554 555 int r = PySequence_Contains(path, plugindir); 556 if (r < 0) { 557 osync_error_set(error, OSYNC_ERROR_GENERIC, "Error checking for 'plugindir in sys.path'"); 558 PYERR_CLEAR(); 559 Py_DECREF(sys_module); 560 Py_DECREF(path); 561 Py_DECREF(plugindir); 562 return FALSE; 563 } 564 565 if (r == 0 && PyList_Insert(path, 0, plugindir) != 0) { 566 osync_error_set(error, OSYNC_ERROR_GENERIC, "Error inserting plugin directory into sys.path"); 567 PYERR_CLEAR(); 568 Py_DECREF(sys_module); 569 Py_DECREF(path); 570 Py_DECREF(plugindir); 571 return FALSE; 572 } 573 574 Py_DECREF(sys_module); 575 Py_DECREF(path); 576 Py_DECREF(plugindir); 577 578 return TRUE; 579 } 580 530 581 osync_bool get_sync_info(OSyncPluginEnv *env, OSyncError **error) 531 582 { … … 536 587 */ 537 588 538 static PyThreadState *mainthread; 539 540 if (!Py_IsInitialized() || !PyEval_ThreadsInitialized()) { 541 /* set python search path to look in our module directory first */ 542 char *pypath = g_build_path(":", OPENSYNC_PYTHONPLG_DIR, getenv("PYTHONPATH"), NULL); 543 if (pypath == NULL || setenv("PYTHONPATH", pypath, 1) != 0) { 544 osync_error_set(error, OSYNC_ERROR_GENERIC, "Couldn't set PYTHONPATH"); 545 return FALSE; 546 } 547 g_free(pypath); 548 549 /* init python */ 589 if (!Py_IsInitialized()) { 590 /* we're the first user of python in this process */ 550 591 Py_InitializeEx(0); 551 592 PyEval_InitThreads(); 552 mainthread = PyThreadState_Get(); 553 } else { 554 PyEval_AcquireThread(mainthread); 555 } 593 } else if (!PyEval_ThreadsInitialized()) { 594 /* The python interpreter has been initialised, but threads are not 595 * I'm going to assume we're the only thread and continue, but this 596 * is possibly unsafe! */ 597 PyEval_InitThreads(); 598 } 599 600 PyGILState_STATE pystate = PyGILState_Ensure(); 601 osync_bool ret = FALSE; 602 603 if (!set_search_path(error)) 604 goto out; 556 605 557 606 /* import opensync module */ 558 607 PyObject *osync_module = pm_load_opensync(error); 559 608 if (!osync_module) 560 return FALSE;561 562 osync_boolret = scan_for_plugins(env, osync_module, error);609 goto out; 610 611 ret = scan_for_plugins(env, osync_module, error); 563 612 Py_DECREF(osync_module); 564 613 565 PyEval_ReleaseThread(mainthread); 614 out: 615 PyGILState_Release(pystate); 616 566 617 return ret; 567 618 }
