portalocker.exceptions module¶
Exceptions raised when acquiring, holding, or releasing a lock fails.
Hierarchy:
BaseLockException
LockException
AlreadyLocked
FileToLarge
BaseLockException is the shared base and is rarely raised directly; catch LockException to handle any locking failure, or one of its two subclasses to handle a specific cause.
- exception portalocker.exceptions.AlreadyLocked(*args, holder_pid=None, **kwargs)[source]¶
Bases:
LockExceptionRaised when a lock is held elsewhere and cannot be acquired.
This is what Lock.acquire raises immediately when fail_when_locked=True and the first attempt finds the lock already held, and also what it raises when timeout expires while waiting for the lock to become free.
- exception portalocker.exceptions.BaseLockException(*args, fh=None, **kwargs)[source]¶
Bases:
ExceptionCommon base for every exception this module defines.
Not raised directly; LockException and its subclasses are. Beyond the standard Exception payload, instances carry:
- fh: the filehandle (or file descriptor, or file-like object)
that was being locked when the failure happened, if one was available yet. None when the failure happened before a handle existed, e.g. while opening the file.
- strerror: the OS error message, when the failure came from a
system call. None otherwise.
Example
>>> from portalocker import exceptions >>> try: ... raise exceptions.LockException( ... exceptions.LockException.LOCK_FAILED, ... 'Resource temporarily unavailable', ... ) ... except exceptions.LockException as exc: ... exc.strerror 'Resource temporarily unavailable'
- Parameters:
- Return type:
None
- exception portalocker.exceptions.FileToLarge(*args, fh=None, **kwargs)[source]¶
Bases:
LockExceptionRaised when a file is too large for the locking call to handle.
The misspelling in the name (FileToLarge, rather than FileTooLarge) is a long-standing typo in the public API. It is kept exactly as-is for backwards compatibility instead of being silently renamed, which would break except FileToLarge in existing code.
- exception portalocker.exceptions.LockException(*args, fh=None, **kwargs)[source]¶
Bases:
BaseLockExceptionRaised when acquiring or releasing a lock fails.
This is the general-purpose locking failure and the type to catch when any locking error is acceptable to handle uniformly: AlreadyLocked and FileToLarge both derive from it, so except LockException also catches those.
Changed in version 4.0.0: On POSIX, lock failures now populate strerror and pass the OS error message as the second positional argument, matching the contract this module already followed on Windows. Previously, str(exc) on POSIX returned the bare underlying OSError text; it now returns the 2-argument exception repr instead (for example
(1, 'Resource temporarily unavailable')). Code that parsed str(exc) on POSIX should read .strerror instead, which has held the message consistently on both platforms since 4.0.0.