Facundo Batista wrote:
> I just don't like a huge try/finally... but as FDs are just ints, do
> you think is there a better way to handle it?

How about a nice 'n shiny context wrapper for the pipe:


import os

class Pipe(object):
    def __enter__(self):
        self.read, self.write = os.pipe()
        return self.read, self.write

    def __exit__(self, *args):
        try:
            os.close(self.read)
        finally:
            # make sure that write is closed even if
            # self.read can't be closed
            os.close(self.write)


with Pipe() as (read, write):
    print read, write

Christian

PS and nit pick:
File descriptor are opaque resource handlers which just happened to be
ints. They should be treated as magic cookies.

_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to