/[weechat]/weechat/src/plugins/scripts/python/weechat-python.c
ViewVC logotype

Diff of /weechat/src/plugins/scripts/python/weechat-python.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.4 by flashcode, Mon Oct 17 14:30:03 2005 UTC revision 1.5 by kolter, Wed Oct 19 15:43:13 2005 UTC
# Line 469  weechat_python_get_config (PyObject *sel Line 469  weechat_python_get_config (PyObject *sel
469            
470      /* make gcc happy */      /* make gcc happy */
471      (void) self;      (void) self;
472    
473        option = NULL;
474            
475      if (!PyArg_ParseTuple (args, "s", &option))      if (!PyArg_ParseTuple (args, "s", &option))
476      {      {
# Line 500  weechat_python_get_config (PyObject *sel Line 502  weechat_python_get_config (PyObject *sel
502   */   */
503    
504  static  static
505  PyMethodDef weechat_funcs[] = {  PyMethodDef weechat_python_funcs[] = {
506      { "register", weechat_python_register, METH_VARARGS, "" },      { "register", weechat_python_register, METH_VARARGS, "" },
507      { "prnt", weechat_python_print, METH_VARARGS, "" },      { "prnt", weechat_python_print, METH_VARARGS, "" },
508      { "print_infobar", weechat_python_print_infobar, METH_VARARGS, "" },      { "print_infobar", weechat_python_print_infobar, METH_VARARGS, "" },
# Line 514  PyMethodDef weechat_funcs[] = { Line 516  PyMethodDef weechat_funcs[] = {
516  };  };
517    
518  /*  /*
519     * weechat_python_output : redirection for stdout and stderr
520     */
521    
522    static PyObject *
523    weechat_python_output (PyObject *self, PyObject *args)
524    {
525        char *msg;
526        /* make gcc happy */
527        (void) self;
528        
529        msg = NULL;
530    
531        if (!PyArg_ParseTuple (args, "s", &msg))
532        {
533            python_plugin->printf_server (python_plugin,
534                                          "Python error: unable to get "
535                                          "stdout/stderr message(s)");
536            return NULL;
537        }
538        
539        python_plugin->printf_server (python_plugin,
540                                      "Python stdin/stdout: %s", msg);
541        return Py_BuildValue ("i", 1);
542    }
543    
544    /*
545     * Outputs subroutines
546     */
547    
548    static
549    PyMethodDef weechat_python_output_funcs[] = {
550        { "write", weechat_python_output, METH_VARARGS, "" },
551        { NULL, NULL, 0, NULL }
552    };
553    
554    /*
555   * weechat_python_load: load a Python script   * weechat_python_load: load a Python script
556   */   */
557    
# Line 522  weechat_python_load (t_weechat_plugin *p Line 560  weechat_python_load (t_weechat_plugin *p
560  {  {
561      FILE *fp;      FILE *fp;
562      PyThreadState *python_current_interpreter;      PyThreadState *python_current_interpreter;
563        PyObject *outputs;
564            
565      plugin->printf_server (plugin, "Loading Python script \"%s\"", filename);      plugin->printf_server (plugin, "Loading Python script \"%s\"", filename);
566            
# Line 552  weechat_python_load (t_weechat_plugin *p Line 591  weechat_python_load (t_weechat_plugin *p
591    
592      PyThreadState_Swap (python_current_interpreter);      PyThreadState_Swap (python_current_interpreter);
593    
594      if (Py_InitModule ("weechat", weechat_funcs) == NULL)      if (Py_InitModule ("weechat", weechat_python_funcs) == NULL)
595      {      {
596          plugin->printf_server (plugin,          plugin->printf_server (plugin,
597                                 "Python error: unable to initialize WeeChat module");                                 "Python error: unable to initialize WeeChat module");
# Line 564  weechat_python_load (t_weechat_plugin *p Line 603  weechat_python_load (t_weechat_plugin *p
603          return 0;          return 0;
604      }      }
605    
606      if (PyRun_SimpleString (      outputs = Py_InitModule("weechatOutputs", weechat_python_output_funcs);
607              "import weechat, sys, string\n"      if (outputs == NULL)
   
             "class weechatStdout:\n"  
             "\tdef write(self, str):\n"  
             "\t\tstr = string.strip(str)\n"  
             "\t\tif str != \"\":\n"  
             "\t\t\tweechat.prnt(\"Python stdout : \" + str, \"\")\n"  
   
             "class weechatStderr:\n"  
             "\tdef write(self, str):\n"  
             "\t\tstr = string.strip(str)\n"  
             "\t\tif str != \"\":\n"  
             "\t\t\tweechat.prnt(\"Python stderr : \" + str, \"\")\n"  
   
             "sys.stdout = weechatStdout()\n"  
             "sys.stderr = weechatStderr()\n"  
             ) != 0)  
608      {      {
609          plugin->printf_server (plugin,          plugin->printf_server (plugin,
610                                 "Python warning: unable to redirect stdout and stderr");                                 "Python warning: unable to redirect stdout and stderr");
611      }      }
612            else
613        {
614            if (PySys_SetObject("stdout", outputs) == -1)
615                plugin->printf_server (plugin,
616                                       "Python warning: unable to redirect stdout");
617            if (PySys_SetObject("stderr", outputs) == -1)
618                plugin->printf_server (plugin,
619                                       "Python warning: unable to redirect stderr");
620        }
621            
622      python_current_script_filename = strdup (filename);      python_current_script_filename = strdup (filename);
623            
624      if (PyRun_SimpleFile (fp, filename) != 0)      if (PyRun_SimpleFile (fp, filename) != 0)

Legend:
Removed from v.1.4  
changed lines
  Added in v.1.5

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26