Package wtf :: Module osutil
[hide private]
[frames] | no frames]

Module osutil

source code

Certain utilities to make the life more easy.


Author: André Malo

Classes [hide private]
  IdentityWarning
The attempt to change identity caused a soft error
  IdentityError
The attempt to change identity caused a hard error
  SocketError
Socket error
  AddressError
Address resolution error
  TimeoutError
Timeout error
  SSLError
SSL error
Functions [hide private]
 
raise_socket_error(timeout=None)
Convert a socket error into an appropriate module exception
source code
 
unlink_silent(filename)
Unlink a filename, but ignore if it does not exist
source code
 
close_on_exec(descriptor, close=True)
Mark descriptor to be closed on exec (or not)
source code
int
safe_fd(fd)
Ensure that file descriptor fd is >= 3
source code
 
close_descriptors(*keep)
Close all file descriptors >= 3
source code
tuple
disable_nagle(sock, peername=None, _flag=1)
Disable nagle algorithm for a TCP socket
source code
socket.socket
connect(spec, timeout=None, nagle_off=True, cache=0, _cache={}, _lock=_connect_cache_lock)
Create and connect a socket to a peer
source code
 
change_identity(user, group)
Change identity of the current process
source code
 
initgroups(username, gid)
Implement initgroups(3)
source code
Variables [hide private]
  _myflag = 1
  __package__ = 'wtf'
Function Details [hide private]

raise_socket_error(timeout=None)

source code 

Convert a socket error into an appropriate module exception

This function needs an already raised socket.error.

raise_socket_error.EAIS is a mapping from GAI error numbers to their names ({int: 'name', ...})

Parameters:
  • timeout (float) - applied timeout in seconds, used for the TimeoutError description
Raises:

unlink_silent(filename)

source code 
Unlink a filename, but ignore if it does not exist
Parameters:
  • filename (basestring) - The filename to remove

close_on_exec(descriptor, close=True)

source code 
Mark descriptor to be closed on exec (or not)
Parameters:
  • descriptor (file or int) - An object with fileno method or an int representing a low level file descriptor
  • close (bool) - Mark being closed on exec?
Raises:
  • IOError - Something went wrong

Warning: This function is not thread safe (race condition)

safe_fd(fd)

source code 

Ensure that file descriptor fd is >= 3

This is done by dup(2) calls until it's greater than 2. After success the duped descriptors are closed.

Parameters:
  • fd (int) - The file descriptor to process
Returns: int
The new file descriptor (>=3)
Raises:
  • OSError - Duping went wrong

disable_nagle(sock, peername=None, _flag=1)

source code 
Disable nagle algorithm for a TCP socket
Parameters:
  • sock (socket.socket) - Socket to process
  • peername (str or tuple) - The name of the remote socket, if str, it's a UNIX domain socket and the function does nothing
Returns: tuple
The socket and the peername again (if the latter was passed as None, it will be set to something useful
Raises:
  • socket.error - The socket was probably not connected. If setting of the option fails, no socket error is thrown though. It's ignored.

connect(spec, timeout=None, nagle_off=True, cache=0, _cache={}, _lock=_connect_cache_lock)

source code 
Create and connect a socket to a peer
Parameters:
  • spec (tuple or str) - The peer specification ((host, port) or str)
  • timeout (float) - Timeout in seconds
  • nagle_off (bool) - Disable Nagle's algorithm. This option does not apply to UNIX domain sockets.
Returns: socket.socket
The connected socket or None if no connectable address could be found
Raises:
  • SocketError - socket error (maybe a subclass of SocketError)
  • NotImplementedError - UNIX domain sockets are not supported in this platform

change_identity(user, group)

source code 

Change identity of the current process

This only works if the effective user ID of the current process is 0.

Parameters:
  • user (str) - User identification, if it is interpretable as int, it's assumed to be a numeric user ID
  • group (str) - Group identification, if it is interpretable as int, it's asummed to be a numeric group ID
Raises:

initgroups(username, gid)

source code 
Implement initgroups(3)
Parameters:
  • username (str) - The user name
  • gid (int) - The group id
Raises:
  • OSError - initgroups() didn't succeed
  • NotImplementedError - initgroups is not implemented (needs c-extension)