@contextmanager
def attempt_effective_uid(username, suppress_errors=False):
 original_euid = os.geteuid()
 new_euid = pwd.getpwnam(username).pw_uid
 restore_euid = False
 if (original_euid != new_euid):
  try:
   os.seteuid(new_euid)
  except OSError as e:
   if ((not suppress_errors) or (e.errno != 1)):
    raise
  else:
   restore_euid = True
 try:
  (yield)
 finally:
  if restore_euid:
   os.seteuid(original_euid)

@ contextmanager def attempt_effective_uid ( username , suppress_errors = False ) : original_euid = os . geteuid ( ) new_euid = pwd . getpwnam ( username ) . pw_uid restore_euid = False if ( original_euid != new_euid ) : try : os . seteuid ( new_euid ) except OSError as e : if ( ( not suppress_errors ) or ( e . errno != 1 ) ) : raise else : restore_euid = True try : ( yield ) finally : if restore_euid : os . seteuid ( original_euid )
a context manager to temporarily change the effective user id .