560 |
{ |
{ |
561 |
FILE *fp; |
FILE *fp; |
562 |
PyThreadState *python_current_interpreter; |
PyThreadState *python_current_interpreter; |
563 |
PyObject *outputs; |
PyObject *weechat_module, *weechat_outputs; |
564 |
|
|
565 |
plugin->printf_server (plugin, "Loading Python script \"%s\"", filename); |
plugin->printf_server (plugin, "Loading Python script \"%s\"", filename); |
566 |
|
|
590 |
} |
} |
591 |
|
|
592 |
PyThreadState_Swap (python_current_interpreter); |
PyThreadState_Swap (python_current_interpreter); |
593 |
|
|
594 |
|
weechat_module = Py_InitModule ("weechat", weechat_python_funcs); |
595 |
|
|
596 |
if (Py_InitModule ("weechat", weechat_python_funcs) == NULL) |
if ( weechat_module == NULL) |
597 |
{ |
{ |
598 |
plugin->printf_server (plugin, |
plugin->printf_server (plugin, |
599 |
"Python error: unable to initialize WeeChat module"); |
"Python error: unable to initialize WeeChat module"); |
605 |
return 0; |
return 0; |
606 |
} |
} |
607 |
|
|
608 |
outputs = Py_InitModule("weechatOutputs", weechat_python_output_funcs); |
weechat_outputs = Py_InitModule("weechatOutputs", weechat_python_output_funcs); |
609 |
if (outputs == NULL) |
if (weechat_outputs == NULL) |
610 |
{ |
{ |
611 |
plugin->printf_server (plugin, |
plugin->printf_server (plugin, |
612 |
"Python warning: unable to redirect stdout and stderr"); |
"Python warning: unable to redirect stdout and stderr"); |
613 |
} |
} |
614 |
else |
else |
615 |
{ |
{ |
616 |
if (PySys_SetObject("stdout", outputs) == -1) |
if (PySys_SetObject("stdout", weechat_outputs) == -1) |
617 |
plugin->printf_server (plugin, |
plugin->printf_server (plugin, |
618 |
"Python warning: unable to redirect stdout"); |
"Python warning: unable to redirect stdout"); |
619 |
if (PySys_SetObject("stderr", outputs) == -1) |
if (PySys_SetObject("stderr", weechat_outputs) == -1) |
620 |
plugin->printf_server (plugin, |
plugin->printf_server (plugin, |
621 |
"Python warning: unable to redirect stderr"); |
"Python warning: unable to redirect stderr"); |
622 |
} |
} |
634 |
PyThreadState_Delete (python_current_interpreter); |
PyThreadState_Delete (python_current_interpreter); |
635 |
PyEval_ReleaseLock (); |
PyEval_ReleaseLock (); |
636 |
fclose (fp); |
fclose (fp); |
637 |
|
/* if script was registered, removing from list */ |
638 |
|
if (python_current_script != NULL) |
639 |
|
weechat_script_remove (plugin, &python_scripts, python_current_script); |
640 |
return 0; |
return 0; |
641 |
} |
} |
642 |
|
|
676 |
|
|
677 |
if (script->shutdown_func[0]) |
if (script->shutdown_func[0]) |
678 |
weechat_python_exec (plugin, script, script->shutdown_func, "", ""); |
weechat_python_exec (plugin, script, script->shutdown_func, "", ""); |
679 |
|
|
680 |
|
PyEval_AcquireLock (); |
681 |
PyThreadState_Swap (NULL); |
PyThreadState_Swap (NULL); |
682 |
PyThreadState_Clear (script->interpreter); |
PyThreadState_Clear (script->interpreter); |
683 |
PyThreadState_Delete (script->interpreter); |
PyThreadState_Delete (script->interpreter); |
684 |
|
PyEval_ReleaseLock (); |
685 |
|
|
686 |
weechat_script_remove (plugin, &python_scripts, script); |
weechat_script_remove (plugin, &python_scripts, script); |
687 |
} |
} |
881 |
int |
int |
882 |
weechat_plugin_init (t_weechat_plugin *plugin) |
weechat_plugin_init (t_weechat_plugin *plugin) |
883 |
{ |
{ |
884 |
|
char *argv[] = { "__weechat_plugin__" , NULL }; |
885 |
|
|
886 |
python_plugin = plugin; |
python_plugin = plugin; |
887 |
|
|
888 |
plugin->printf_server (plugin, "Loading Python module \"weechat\""); |
plugin->printf_server (plugin, "Loading Python module \"weechat\""); |
895 |
return 0; |
return 0; |
896 |
} |
} |
897 |
|
|
898 |
|
PySys_SetArgv(1, argv); |
899 |
|
|
900 |
PyEval_InitThreads(); |
PyEval_InitThreads(); |
901 |
|
|
902 |
python_mainThreadState = PyThreadState_Get(); |
python_mainThreadState = PyThreadState_Get(); |
935 |
/* unload all scripts */ |
/* unload all scripts */ |
936 |
weechat_python_unload_all (plugin); |
weechat_python_unload_all (plugin); |
937 |
|
|
938 |
|
PyEval_AcquireLock (); |
939 |
|
|
940 |
/* free Python interpreter */ |
/* free Python interpreter */ |
941 |
if (python_mainThreadState != NULL) |
if (python_mainThreadState != NULL) |
942 |
{ |
{ |
943 |
PyThreadState_Swap (python_mainThreadState); |
PyThreadState_Swap (python_mainThreadState); |
944 |
python_mainThreadState = NULL; |
python_mainThreadState = NULL; |
945 |
} |
} |
946 |
|
|
947 |
Py_Finalize (); |
Py_Finalize (); |
948 |
if (Py_IsInitialized () != 0) |
if (Py_IsInitialized () != 0) |
949 |
python_plugin->printf_server (python_plugin, |
python_plugin->printf_server (python_plugin, |