[Tutor] Re: Tutor digest, Vol 1 #1379 - 16 msgs

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Mon, 28 Jan 2002 01:43:38 -0800 (PST)


[Warning: skip this message if you're not familiar with C programming.]


On Mon, 28 Jan 2002, Scott wrote:

> > If you don't want it to create that file, you'll want to configure pyslang
> > to not redirect standard error to that 'pyslang.err' file.  If you use the
> > following:
> > 
> >     './configure --disable-stderr-redir'
> > 
> 
> Recompile!  Heresey.  Kinda weird, you gotta admit.

I agree!


At the moment, pyslang is hardcoded to do this logging at the C level ---
without editing the C wrapper, I don't think there's much else we can do
from a Python standpoint.

However, I think it's very possible to change pyslang so that the
file-logging can be activated by a runtime switch instead.  The module is
a wrapper around slang, and written using the SWIG wrapper generator:

    http://swig.org


I'd need some time to read though the SWIG wrappers before I can
comfortably fiddle with it.  Hmmmm... It looks like the module's
initalizer calls a C function called stderr_redir().  For the curious,
here's what it does:

/*** in slang.i ***/
stderr_redir()
{
    FILE *xstderr = fopen("pyslang.err", "w");
    dup2(fileno(xstderr), 2);
    setvbuf(stderr, NULL, _IONBF,0);
    setvbuf(xstderr, NULL, _IONBF,0);
    fprintf(stderr, "This is init\n");
}
/***/

which is basically saying "Ok, let's set standard error to be this file
'pyslang.err'.  Also, let's make sure that it's in a non-buffered mode,
just like the original stderr.  Finally, let's say something to the stderr
and make sure that it works."  (I'm just paying attention to the UNIX
wrapper; the win32 wrapper probably does something similar.)


The thing is that, as pyslang is written at the moment, it does this
unconditionally, and it's hardcoded at compile time.  Perhaps it might be
better to have this as a user-accessible function instead, to be called
explicitely.  Or maybe have this function trigger if the user sets a
module attribute.  Maybe it would be nice to say something like:

###
pyslang.LOGGING = 1
###

and have pyslang "magically" do the stderr redirection then.


I haven't thought about this enough: I don't know what the "right" thing
to do is yet, since I haven't even played with pyslang yet.  *grin* I
don't think this behavior is "broken", so it's going to be a little harder
to see what a good solution would be.  Perhaps the author had good reasons
for doing it this way.