aboutsummaryrefslogtreecommitdiff
path: root/packages/Python/lldbsuite/support
diff options
context:
space:
mode:
Diffstat (limited to 'packages/Python/lldbsuite/support')
-rw-r--r--packages/Python/lldbsuite/support/encoded_file.py48
-rw-r--r--packages/Python/lldbsuite/support/funcutils.py16
-rw-r--r--packages/Python/lldbsuite/support/gmodules.py30
-rw-r--r--packages/Python/lldbsuite/support/optional_with.py56
4 files changed, 150 insertions, 0 deletions
diff --git a/packages/Python/lldbsuite/support/encoded_file.py b/packages/Python/lldbsuite/support/encoded_file.py
new file mode 100644
index 000000000000..7581564f7e3a
--- /dev/null
+++ b/packages/Python/lldbsuite/support/encoded_file.py
@@ -0,0 +1,48 @@
+"""
+ The LLVM Compiler Infrastructure
+
+This file is distributed under the University of Illinois Open Source
+License. See LICENSE.TXT for details.
+
+Prepares language bindings for LLDB build process. Run with --help
+to see a description of the supported command line arguments.
+"""
+
+# Python modules:
+import io
+
+# Third party modules
+import six
+
+def _encoded_read(old_read, encoding):
+ def impl(size):
+ result = old_read(size)
+ # If this is Python 2 then we need to convert the resulting `unicode` back
+ # into a `str` before returning
+ if six.PY2:
+ result = result.encode(encoding)
+ return result
+ return impl
+
+def _encoded_write(old_write, encoding):
+ def impl(s):
+ # If we were asked to write a `str` (in Py2) or a `bytes` (in Py3) decode it
+ # as unicode before attempting to write.
+ if isinstance(s, six.binary_type):
+ s = s.decode(encoding)
+ return old_write(s)
+ return impl
+
+'''
+Create a Text I/O file object that can be written to with either unicode strings or byte strings
+under Python 2 and Python 3, and automatically encodes and decodes as necessary to return the
+native string type for the current Python version
+'''
+def open(file, encoding, mode='r', buffering=-1, errors=None, newline=None, closefd=True):
+ wrapped_file = io.open(file, mode=mode, buffering=buffering, encoding=encoding,
+ errors=errors, newline=newline, closefd=closefd)
+ new_read = _encoded_read(getattr(wrapped_file, 'read'), encoding)
+ new_write = _encoded_write(getattr(wrapped_file, 'write'), encoding)
+ setattr(wrapped_file, 'read', new_read)
+ setattr(wrapped_file, 'write', new_write)
+ return wrapped_file
diff --git a/packages/Python/lldbsuite/support/funcutils.py b/packages/Python/lldbsuite/support/funcutils.py
new file mode 100644
index 000000000000..53dd1fb370ba
--- /dev/null
+++ b/packages/Python/lldbsuite/support/funcutils.py
@@ -0,0 +1,16 @@
+from __future__ import print_function
+from __future__ import absolute_import
+
+# System modules
+import inspect
+
+# Third-party modules
+
+# LLDB modules
+
+def requires_self(func):
+ func_argc = len(inspect.getargspec(func).args)
+ if func_argc == 0 or (getattr(func,'im_self', None) is not None) or (hasattr(func, '__self__')):
+ return False
+ else:
+ return True
diff --git a/packages/Python/lldbsuite/support/gmodules.py b/packages/Python/lldbsuite/support/gmodules.py
new file mode 100644
index 000000000000..4f2fd9643b14
--- /dev/null
+++ b/packages/Python/lldbsuite/support/gmodules.py
@@ -0,0 +1,30 @@
+from __future__ import absolute_import
+from __future__ import print_function
+
+# System modules
+import os
+import re
+
+
+GMODULES_SUPPORT_MAP = {}
+GMODULES_HELP_REGEX = re.compile(r"\s-gmodules\s")
+
+
+def is_compiler_clang_with_gmodules(compiler_path):
+ # Before computing the result, check if we already have it cached.
+ if compiler_path in GMODULES_SUPPORT_MAP:
+ return GMODULES_SUPPORT_MAP[compiler_path]
+
+ def _gmodules_supported_internal():
+ compiler = os.path.basename(compiler_path)
+ if "clang" not in compiler:
+ return False
+ else:
+ # Check the compiler help for the -gmodules option.
+ clang_help = os.popen("%s --help" % compiler_path).read()
+ return GMODULES_HELP_REGEX.search(clang_help, re.DOTALL) is not None
+
+ GMODULES_SUPPORT_MAP[compiler_path] = _gmodules_supported_internal()
+ return GMODULES_SUPPORT_MAP[compiler_path]
+
+
diff --git a/packages/Python/lldbsuite/support/optional_with.py b/packages/Python/lldbsuite/support/optional_with.py
new file mode 100644
index 000000000000..41342288bc6a
--- /dev/null
+++ b/packages/Python/lldbsuite/support/optional_with.py
@@ -0,0 +1,56 @@
+# ====================================================================
+# Provides a with-style resource handler for optionally-None resources
+# ====================================================================
+
+class optional_with(object):
+ # pylint: disable=too-few-public-methods
+ # This is a wrapper - it is not meant to provide any extra methods.
+ """Provides a wrapper for objects supporting "with", allowing None.
+
+ This lets a user use the "with object" syntax for resource usage
+ (e.g. locks) even when the wrapped with object is None.
+
+ e.g.
+
+ wrapped_lock = optional_with(thread.Lock())
+ with wrapped_lock:
+ # Do something while the lock is obtained.
+ pass
+
+ might_be_none = None
+ wrapped_none = optional_with(might_be_none)
+ with wrapped_none:
+ # This code here still works.
+ pass
+
+ This prevents having to write code like this when
+ a lock is optional:
+
+ if lock:
+ lock.acquire()
+
+ try:
+ code_fragment_always_run()
+ finally:
+ if lock:
+ lock.release()
+
+ And I'd posit it is safer, as it becomes impossible to
+ forget the try/finally using optional_with(), since
+ the with syntax can be used.
+ """
+ def __init__(self, wrapped_object):
+ self.wrapped_object = wrapped_object
+
+ def __enter__(self):
+ if self.wrapped_object is not None:
+ return self.wrapped_object.__enter__()
+ else:
+ return self
+
+ def __exit__(self, the_type, value, traceback):
+ if self.wrapped_object is not None:
+ return self.wrapped_object.__exit__(the_type, value, traceback)
+ else:
+ # Don't suppress any exceptions
+ return False