@contextmanager
def _carbon(host, port):
 carbon_sock = None
 try:
  carbon_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP)
  carbon_sock.connect((host, port))
 except socket.error as err:
  log.error('Error connecting to {0}:{1}, {2}'.format(host, port, err))
  raise
 else:
  log.debug('Connected to carbon')
  (yield carbon_sock)
 finally:
  if (carbon_sock is not None):
   log.debug('Destroying carbon socket')
   carbon_sock.shutdown(socket.SHUT_RDWR)
   carbon_sock.close()

@ contextmanager def _carbon ( host , port ) : carbon_sock = None try : carbon_sock = socket . socket ( socket . AF_INET , socket . SOCK_STREAM , socket . IPPROTO_TCP ) carbon_sock . connect ( ( host , port ) ) except socket . error as err : log . error ( 'Error connecting to {0}:{1}, {2}' . format ( host , port , err ) ) raise else : log . debug ( 'Connected to carbon' ) ( yield carbon_sock ) finally : if ( carbon_sock is not None ) : log . debug ( 'Destroying carbon socket' ) carbon_sock . shutdown ( socket . SHUT_RDWR ) carbon_sock . close ( )
context manager to ensure the clean creation and destruction of a socket .