2 The LLVM Compiler Infrastructure
4 This file is distributed under the University of Illinois Open Source
5 License. See LICENSE.TXT for details.
7 Provides classes used by the test results reporting infrastructure
8 within the LLDB test suite.
11 This module contains utilities used by the lldb test framwork.
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.
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.
25 wrapped_lock = OptionalWith(thread.Lock())
27 # Do something while the lock is obtained.
31 wrapped_none = OptionalWith(might_be_none)
33 # This code here still works.
36 This prevents having to write code like this when
43 code_fragament_always_run()
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.
52 def __init__(self, wrapped_object):
53 self.wrapped_object = wrapped_object
56 if self.wrapped_object is not None:
57 return self.wrapped_object.__enter__()
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)
65 # Don't suppress any exceptions