oserrors is a Python 3 module which provides constructors for OSError exception subclasses.
oserrors is designed as a drop-in module for any Python library that needs to return OSError subclasses, for providing a low-level style API.
This module contains helpers for properly raising OSError subclasses.
>>> raise file_exists()
Traceback (most recent call last):
...
FileExistsError: [Errno 17] File exists
>>> raise file_exists('foo')
Traceback (most recent call last):
...
FileExistsError: [Errno 17] File exists: 'foo'
>>> raise file_exists('foo', 'bar')
Traceback (most recent call last):
...
FileExistsError: [Errno 17] File exists: 'foo' -> 'bar'
Error codes and message text is handled automatically, and the attributes filename and filename2 are set on the error object; see Python docs on OSError for details.
BlockingIOError takes an extra argument to set the attribute characters_written; see Python docs on BlockingIOError for details.
>>> raise blocking_io_eagain('foo', 'bar', 5)
Traceback (most recent call last):
...
BlockingIOError: [Errno 11] Resource temporarily unavailable: 'foo' -> 'bar'
Arguments can be passed by parameter name:
>>> raise blocking_io_eagain(filename='foo', filename2='bar', written=5)
Traceback (most recent call last):
...
BlockingIOError: [Errno 11] Resource temporarily unavailable: 'foo' -> 'bar'
>>> raise blocking_io_eagain(written=5)
Traceback (most recent call last):
...
BlockingIOError: [Errno 11] Resource temporarily unavailable
This module also exports the following pre-made constructor functions, which return an OSError object with the given error code:
blocking_io_eagain():errno.EAGAINblocking_io_ealready():errno.EALREADYblocking_io_ewouldblock():errno.EWOULDBLOCKblocking_io_einprogress():errno.EINPROGRESSchild_process():errno.ECHILDbroken_pipe_eshutdown():errno.ESHUTDOWNbroken_pipe_epipe():errno.EPIPEconnection_aborted():errno.ECONNABORTEDconnection_refused():errno.ECONNREFUSEDconnection_reset():errno.ECONNRESETfile_exists():errno.EEXISTfile_not_found():errno.ENOENTinterrupted():errno.EINTRis_a_directory():errno.EISDIRnot_a_directory():errno.ENOTDIRpermission_eacces():errno.EACCESpermission_eperm():errno.EPERMprocess_lookup():errno.ESRCHtimeout():errno.ETIMEDOUT
You can also make your own OSError builders by supplying an error code:
>>> import errno
>>> enoent_error = oserror(errno.ENOENT)
>>> raise enoent_error('foo')
Traceback (most recent call last):
...
FileNotFoundError: [Errno 2] No such file or directory: 'foo'