Best Python code snippet using slash
phmap_lldb.py
Source:phmap_lldb.py  
...8import re9_MAX_CHILDREN = 25010_MAX_CTRL_INDEX = 1_00011_MODULE_NAME = os.path.basename(__file__).split(".")[0]12def _get_function_name(instance=None):13    """Return the name of the calling function"""14    class_name = f"{type(instance).__name__}." if instance else ""15    return class_name + sys._getframe(1).f_code.co_name16class flat_map_slot_type:17    CLASS_PATTERN = "^phmap::priv::raw_hash_set<phmap::priv::FlatHashMapPolicy.*>::slot_type$"18    HAS_SUMMARY = True19    IS_SYNTHETIC_PROVIDER = False20    @staticmethod21    def summary(valobj, _):22        try:23            valobj = valobj.GetChildMemberWithName('value')24            first = valobj.GetChildMemberWithName('first').GetSummary()25            if not first: first = "{...}"26            second = valobj.GetChildMemberWithName('second').GetSummary()27            if not second: second = "{...}"28            return f"{{{first}, {second}}}"29        except BaseException as ex:30            print(f"{_get_function_name()} -> {ex}")31        return ""32class node_map_slot_type:33    CLASS_PATTERN = r"phmap::priv::raw_hash_set<phmap::priv::NodeHashMapPolicy.*>::slot_type$"34    HAS_SUMMARY = True35    IS_SYNTHETIC_PROVIDER = False36    @staticmethod37    def summary(valobj, _):38        try:39            valobj = valobj.Dereference()40            first = valobj.GetChildMemberWithName('first').GetSummary()41            if not first: first = "{...}"42            second = valobj.GetChildMemberWithName('second').GetSummary()43            if not second: second = "{...}"44            return f"{{{first}, {second}}}"45        except BaseException as ex:46            print(f"{_get_function_name()} -> {ex}")47        return "{?}"48class node_set_slot_type:49    CLASS_PATTERN = r"phmap::priv::raw_hash_set<phmap::priv::NodeHashSetPolicy.*>::slot_type$"50    HAS_SUMMARY = True51    IS_SYNTHETIC_PROVIDER = False52    @staticmethod53    def summary(valobj, _):54        try:55            summary = valobj.Dereference().GetSummary()56            if not summary: summary = "{...}"57            return summary58        except BaseException as ex:59            print(f"{_get_function_name()} -> {ex}")60        return "{?}"61class flat_hash_map_or_set:62    CLASS_PATTERN = "^phmap::flat_hash_(map|set)<.*>$"63    HAS_SUMMARY = True64    IS_SYNTHETIC_PROVIDER = True65    @staticmethod66    def summary(valobj, _):67        try:68            valobj = valobj.GetNonSyntheticValue()69            size = valobj.GetChildMemberWithName('size_').GetValueAsUnsigned()70            capacity = valobj.GetChildMemberWithName('capacity_').GetValueAsUnsigned()71            return f"size = {size} (capacity = {capacity})"72        except BaseException as ex:73            print(f"{_get_function_name()} -> {ex}")74        return "{?}"75    def __init__(self, valobj, _):76        self.valobj = valobj77        self.slots_ = self.slot_type = self.ctrl_ = None78        self.size_ = self.capacity_ = self.slot_size = 079    def num_children(self):80        return min(self.size_, _MAX_CHILDREN)81    def has_children(self):82        return True83    def update(self):84        try:85            self.size_ = self.valobj.GetChildMemberWithName('size_').GetValueAsUnsigned()86            self.capacity_ = self.valobj.GetChildMemberWithName('capacity_').GetValueAsUnsigned()87            self.slots_ = self.valobj.GetChildMemberWithName("slots_")88            self.slot_type = self.slots_.GetType().GetPointeeType()89            self.slot_size = self.slot_type.GetByteSize()90            self.ctrl_ = self.valobj.GetChildMemberWithName("ctrl_")91        except BaseException as ex:92            print(f"{_get_function_name(self)} -> {ex}")93    def get_child_index(self, name):94        try:95            if name in ('size_', 'capacity_'):96                return -197            return int(name.lstrip('[').rstrip(']'))98        except:99            return -1100    def get_child_at_index(self, index):101        try:102            if index < 0:103                return None104            if index >= self.size_ or index >= _MAX_CHILDREN:105                return None106            real_idx = -1107            for idx in range(min(self.capacity_ + 3, _MAX_CTRL_INDEX)):108                ctrl = self.ctrl_.GetChildAtIndex(idx).GetValueAsSigned()109                if ctrl >= -1:110                    real_idx += 1111                    if real_idx == index:112                        return self.slots_.CreateChildAtOffset(f'[{index}]', idx * self.slot_size, self.slot_type)113        except BaseException as ex:114            print(f"{_get_function_name(self)} -> {ex}")115        return None116class parallel_flat_or_node_map_or_set:117    CLASS_PATTERN = "^phmap::parallel_(flat|node)_hash_(map|set)<.*>$"118    HAS_SUMMARY = True119    IS_SYNTHETIC_PROVIDER = True120    REGEX_EXTRACT_ARRAY_SIZE = re.compile(r"std::array\s*<.*,\s*(\d+)\s*>")121    @staticmethod122    def _get_size_and_capacity(valobj):123        try:124            valobj = valobj.GetNonSyntheticValue()125            sets = valobj.GetChildMemberWithName('sets_')126            # sets is an std::array<T, SIZE>.127            # It's not possible to get the size of the array with templates parameters128            # "set.GetType().GetTemplateArgumentType(1)" returns an "unsigned long" type but not the value129            # so we must extract it with a regex130            m = parallel_flat_or_node_map_or_set.REGEX_EXTRACT_ARRAY_SIZE.match(sets.GetType().GetName())131            n_buckets = int(m.group(1))132            # this is dependent on the implementation of the standard library133            buckets = sets.GetChildMemberWithName('_M_elems')134            size = capacity = 0135            for idx in range(n_buckets):136                bucket = buckets.GetChildAtIndex(idx).GetChildMemberWithName('set_')137                size += bucket.GetChildMemberWithName('size_').GetValueAsUnsigned()138                capacity += bucket.GetChildMemberWithName('capacity_').GetValueAsUnsigned()139            return size, capacity, n_buckets140        except:141            return '?', '?', 0142    @staticmethod143    def summary(valobj, _):144        size, capacity, _ = parallel_flat_or_node_map_or_set._get_size_and_capacity(valobj)145        return f"size = {size} (capacity = {capacity})"146    def __init__(self, valobj, _):147        self.valobj = valobj148        self.buckets = self.slot_type = None149        self.size_ = self.capacity_ = self.n_buckets_ = self.slot_type = self.ctrl_size = 0150    def num_children(self):151        return min(self.size_, _MAX_CHILDREN)152    def has_children(self):153        return True154    def update(self):155        try:156            self.size_, self.capacity_, self.n_buckets_ = self._get_size_and_capacity(self.valobj)157            self.buckets = self.valobj.GetChildMemberWithName('sets_').GetChildMemberWithName('_M_elems')158            bucket0 = self.buckets.GetChildAtIndex(0).GetChildMemberWithName('set_')159            self.slot_type = bucket0.GetChildMemberWithName('slots_').GetType().GetPointeeType()160            self.slot_size = self.slot_type.GetByteSize()161        except BaseException as ex:162            print(f"{_get_function_name(self)} -> {ex}")163    def get_child_index(self, name):164        try:165            if name in ('sets_'):166                return -1167            return int(name.lstrip('[').rstrip(']'))168        except:169            return -1170    def get_child_at_index(self, index):171        try:172            if index < 0:173                return None174            if index >= self.size_ or index >= _MAX_CHILDREN:175                return None176            real_idx = -1177            total_idx = 0178            for idx in range(self.n_buckets_):179                bucket = self.buckets.GetChildAtIndex(idx).GetChildMemberWithName('set_')180                size = bucket.GetChildMemberWithName("size_").GetValueAsUnsigned()181                if size:182                    slots_ = bucket.GetChildMemberWithName("slots_")183                    ctrl_ = bucket.GetChildMemberWithName("ctrl_")184                    for jdx in range(size):185                        ctrl = ctrl_.GetChildAtIndex(jdx).GetValueAsSigned()186                        if ctrl >= -1:187                            real_idx += 1188                            if real_idx == index:189                                return slots_.CreateChildAtOffset(f'[{index}]', jdx * self.slot_size, self.slot_type)190                    total_idx += size191                    if total_idx > _MAX_CHILDREN:192                        return None193        except BaseException as ex:194            print(f"{_get_function_name(self)} -> {ex}")195        return None196def __lldb_init_module(debugger, internal_dict):197    for sp in (198            flat_map_slot_type,199            node_map_slot_type,200            node_set_slot_type,201            flat_hash_map_or_set,202            parallel_flat_or_node_map_or_set,203    ):204        if sp.HAS_SUMMARY:205            debugger.HandleCommand(206                f'type summary add --regex "{sp.CLASS_PATTERN}" --python-function {_MODULE_NAME}.{sp.__name__}.summary '207                f'--category phmap --expand')208        if sp.IS_SYNTHETIC_PROVIDER:...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
