[py-dev] py.execnet example bug

holger krekel hpk at trillke.net
Thu Aug 10 07:11:14 CEST 2006


Hi Ondrej, 

On Wed, Aug 09, 2006 at 15:31 -0400, Ondrej Certik wrote:
> Hi, I tried this example:
> 
> http://codespeak.net/py/current/doc/execnet.html#a-simple-and-useful-example-for-channels
> 
> and it seems it doesn't work for me -
> 
> I had to change this:
> 
>         for fn in channel.receive():
>             f = open(fn, 'rb')
>             try:
>                 channel.send(f.read())
>             finally:
>                 f.close()
> 
> to this:
> 
>             try:
>                 while 1:
>                     fn=channel.receive()
>                     f = open(fn, 'rb')
>                     try:
>                         channel.send(f.read())
>                     finally:
>                         f.close()
>             except:
>                 pass
> 
> to make it work. The problem is, that channel.receive() returns a
> string (a filename), and the for loop then iterates over the
> characters of the filename, which is obviously not what was intended.
> Am I doing something wrong, or is it a bug?

The original intention was: 

    for fn in channel:   # no .receive() here 
        ...

was the original intention and it should work (the iteration over 
a channel will raise StopIteration when the other side closes it). 
I fixed the example.  

Your replacement above is fine, but the "error" handling 
is a bit rough: just swallowing all exceptions makes it 
somewhat hard to detect for the other side to notice
(and make sense out of) the error.  It's probably better to
have a "try:except:" within the for-loop block and send
back a tuple encoding error information. (You cannot send
"user" objects, only marshallable objects, i.e. dicts, tuples, 
lists, of strings/ints/dicts/tuples/lists...)

best and HTH, 

    holger



More information about the Pytest-dev mailing list