<s> import errno <EOL> import os <EOL> import sys <EOL> from teena import DEFAULT_BUFSIZE , cached_property <EOL> __all__ = [ '<STR_LIT>' ] <EOL> class Pipe ( object ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def __init__ ( self , non_blocking = False ) : <EOL> self . read_fd , self . write_fd = os . pipe ( ) <EOL> if non_blocking : <EOL> self . _set_nonblocking ( self . read_fd ) <EOL> self . _set_nonblocking ( self . write_fd ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . read_fd , self . write_fd ) <EOL> def __enter__ ( self ) : <EOL> return self <EOL> def __exit__ ( self , * exc_info ) : <EOL> self . close ( ) <EOL> def __del__ ( self ) : <EOL> self . close ( ) <EOL> @ staticmethod <EOL> def _set_nonblocking ( fd ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> import fcntl <EOL> except ImportError : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> flags = fcntl . fcntl ( fd , fcntl . F_GETFL ) <EOL> fcntl . fcntl ( fd , fcntl . F_SETFL , flags | os . O_NONBLOCK ) <EOL> @ staticmethod <EOL> def _fd_closed ( fd ) : <EOL> try : <EOL> os . fstat ( fd ) <EOL> except ( OSError , IOError ) , exc : <EOL> if exc . errno == errno . EBADF : <EOL> return True <EOL> raise <EOL> return False <EOL> @ staticmethod <EOL> def _close_fd ( fd ) : <EOL> try : <EOL> os . close ( fd ) <EOL> except ( OSError , IOError ) , exc : <EOL> if exc . errno != errno . EBADF : <EOL> raise <EOL> @ cached_property <EOL> def read_file ( self , buffering = DEFAULT_BUFSIZE ) : <EOL> """<STR_LIT>""" <EOL> return os . fdopen ( self . read_fd , '<STR_LIT:rb>' , buffering ) <EOL> @ cached_property <EOL> def write_file ( self , buffering = DEFAULT_BUFSIZE ) : <EOL> """<STR_LIT>""" <EOL> return os . fdopen ( self . write_fd , '<STR_LIT:wb>' , buffering ) <EOL> @ property <EOL> def read_closed ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _fd_closed ( self . read_fd ) <EOL> @ property <EOL> def write_closed ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _fd_closed ( self . write_fd ) <EOL> def close_read ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _close_fd ( self . read_fd ) <EOL> def close_write ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _close_fd ( self . write_fd ) <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . close_read ( ) <EOL> finally : <EOL> read_exc = sys . exc_info ( ) <EOL> try : <EOL> self . close_write ( ) <EOL> except Exception , exc : <EOL> if read_exc [ <NUM_LIT:0> ] is not None : <EOL> write_unraisable ( self . close , * read_exc ) <EOL> raise <EOL> def write_unraisable ( obj , exc_type , exc_value , exc_traceback ) : <EOL> """<STR_LIT>""" <EOL> print >> sys . stderr , "<STR_LIT>" % ( exc_type , <EOL> exc_value , <EOL> obj ) </s>
