|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2020 - 2021 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a context manager locking and unlocking a mutex. |
|
8 """ |
|
9 |
|
10 import contextlib |
|
11 |
|
12 |
|
13 class EricMutexLocker(contextlib.AbstractContextManager): |
|
14 """ |
|
15 Class implementing a context manager locking and unlocking a mutex. |
|
16 """ |
|
17 def __init__(self, mutex): |
|
18 """ |
|
19 Constructor |
|
20 |
|
21 @param mutex reference to the mutex to be locked |
|
22 @type QMutex or QRecursiveMutex |
|
23 """ |
|
24 self.__mutex = mutex |
|
25 |
|
26 def __enter__(self): |
|
27 """ |
|
28 Special method called when entering the runtime ccontext. |
|
29 |
|
30 @return reference to the context manager object |
|
31 @rtype E5OverrideCursor |
|
32 """ |
|
33 self.__mutex.lock() |
|
34 |
|
35 return self |
|
36 |
|
37 def __exit__(self, exc_type, exc_value, traceback): |
|
38 """ |
|
39 Special method called when exiting the runtime ccontext. |
|
40 |
|
41 @param exc_type type of an exception raised in the runtime context |
|
42 @param exc_value value of an exception raised in the runtime context |
|
43 @param traceback traceback of an exception raised in the runtime |
|
44 context |
|
45 @return always returns None to not suppress any exception |
|
46 @rtype None |
|
47 """ |
|
48 self.__mutex.unlock() |
|
49 |
|
50 return None # __IGNORE_WARNING_M831__ |