From 5880769cbac1362aad46a34faec4c553fa9fcac6 Mon Sep 17 00:00:00 2001 From: Peter LaFosse Date: Thu, 20 Nov 2025 09:58:28 -0500 Subject: Add get and __contains__ methods to all *MetaClass Fixes: https://github.com/Vector35/binaryninja-api/issues/7588 --- python/stringrecognizer.py | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) (limited to 'python/stringrecognizer.py') diff --git a/python/stringrecognizer.py b/python/stringrecognizer.py index 004a58c4..3e6167a0 100644 --- a/python/stringrecognizer.py +++ b/python/stringrecognizer.py @@ -18,7 +18,7 @@ # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS # IN THE SOFTWARE. -from typing import Optional, Union +from typing import Optional, Union, Any from dataclasses import dataclass import ctypes @@ -49,6 +49,23 @@ class _CustomStringTypeMetaClass(type): raise KeyError("'%s' is not a valid type" % str(value)) return CustomStringType(handle=string_type) + def __contains__(cls: '_CustomStringTypeMetaClass', name: object) -> bool: + if not isinstance(name, str): + return False + try: + cls[name] + return True + except KeyError: + return False + + def get(cls: '_CustomStringTypeMetaClass', name: str, default: Any = None) -> Optional['CustomStringType']: + try: + return cls[name] + except KeyError: + if default is not None: + return default + return None + class CustomStringType(metaclass=_CustomStringTypeMetaClass): """ @@ -125,6 +142,23 @@ class _StringRecognizerMetaClass(type): raise KeyError("'%s' is not a valid recognizer" % str(value)) return CoreStringRecognizer(handle=recognizer) + def __contains__(cls: '_StringRecognizerMetaClass', name: object) -> bool: + if not isinstance(name, str): + return False + try: + cls[name] + return True + except KeyError: + return False + + def get(cls: '_StringRecognizerMetaClass', name: str, default: Any = None) -> Optional['StringRecognizer']: + try: + return cls[name] + except KeyError: + if default is not None: + return default + return None + class StringRecognizer(metaclass=_StringRecognizerMetaClass): """ -- cgit v1.3.1