]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - packages/Python/lldbsuite/test/test_runner/lib/lldb_utils.py
Vendor import of lldb trunk r256945:
[FreeBSD/FreeBSD.git] / packages / Python / lldbsuite / test / test_runner / lib / lldb_utils.py
1 """
2 The LLVM Compiler Infrastructure
3
4 This file is distributed under the University of Illinois Open Source
5 License. See LICENSE.TXT for details.
6
7 Provides classes used by the test results reporting infrastructure
8 within the LLDB test suite.
9
10
11 This module contains utilities used by the lldb test framwork.
12 """
13
14
15 class OptionalWith(object):
16     # pylint: disable=too-few-public-methods
17     # This is a wrapper - it is not meant to provide any extra methods.
18     """Provides a wrapper for objects supporting "with", allowing None.
19
20     This lets a user use the "with object" syntax for resource usage
21     (e.g. locks) even when the wrapped with object is None.
22
23     e.g.
24
25     wrapped_lock = OptionalWith(thread.Lock())
26     with wrapped_lock:
27         # Do something while the lock is obtained.
28         pass
29
30     might_be_none = None
31     wrapped_none = OptionalWith(might_be_none)
32     with wrapped_none:
33         # This code here still works.
34         pass
35
36     This prevents having to write code like this when
37     a lock is optional:
38
39     if lock:
40         lock.acquire()
41
42     try:
43         code_fragament_always_run()
44     finally:
45         if lock:
46             lock.release()
47
48     And I'd posit it is safer, as it becomes impossible to
49     forget the try/finally using OptionalWith(), since
50     the with syntax can be used.
51     """
52     def __init__(self, wrapped_object):
53         self.wrapped_object = wrapped_object
54
55     def __enter__(self):
56         if self.wrapped_object is not None:
57             return self.wrapped_object.__enter__()
58         else:
59             return self
60
61     def __exit__(self, the_type, value, traceback):
62         if self.wrapped_object is not None:
63             return self.wrapped_object.__exit__(the_type, value, traceback)
64         else:
65             # Don't suppress any exceptions
66             return False