embedded python and interpreter threads

Charlie DeTar chazen at gmail.com
Tue Dec 7 00:17:28 EST 2004


Quick correction, sorry - the command should read like this (I had 
omitted the "log" before 'CaptureStdout' and 'CaptureStderr').  Same 
problems exist.

      PyRun_SimpleString(
          "import log\n"
          "import sys\n"
          "class StdoutCatcher:\n"
          "\tdef write(self, str):\n"
          "\t\tlog.CaptureStdout(str)\n"
          "class StderrCatcher:\n"
          "\tdef write(self, str):\n"
          "\t\tlog.CaptureStderr(str)\n"
          "sys.stdout = StdoutCatcher()\n"
          "sys.stderr = StderrCatcher()\n");

=Charlie

Charlie DeTar wrote:
> So, I have an amazing, functioning foobar class that embeds python. Only 
> trouble is, I want to be able to have multiple foobar objects at once, 
> each with their own interpreters, stdouts, and stderrs.
> 
> My initialization of the python interpreter in the class "x" is as follows:
> [snip]
>     if (!Py_IsInitialized()) {
>         PyEval_InitThreads();
>         Py_Initialize();
>     }   
>     
>     // Start and switch to a new interpreter thread.
>     x->thread = Py_NewInterpreter();
> 
>     // initialize the module 'logMethods', defined elsewhere,
>     // which contains my stdout and stderr definitions
>     Py_InitModule("log", logMethods);
>     
>     // overwrite Python's stdout and stderr
>     PyRun_SimpleString(
>         "import log\n"
>         "import sys\n"
>         "class StdoutCatcher:\n"
>         "\tdef write(self, str):\n"
>         "\t\t.CaptureStdout(str)\n"
>         "class StderrCatcher:\n"
>         "\tdef write(self, str):\n"
>         "\t\t.CaptureStderr(str)\n"
>         "sys.stdout = StdoutCatcher()\n"
>         "sys.stderr = StderrCatcher()\n");



More information about the Python-list mailing list