Skip to content

Commit 5880769

Browse files
committed
Add get and __contains__ methods to all *MetaClass
Fixes: #7588
1 parent f52693d commit 5880769

19 files changed

+356
-12
lines changed

python/architecture.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
import traceback
2222
import ctypes
23-
from typing import Generator, Union, List, Optional, Mapping, Tuple, NewType, Dict, Set
23+
from typing import Generator, Union, List, Optional, Mapping, Tuple, NewType, Dict, Set, Any
2424
from dataclasses import dataclass, field
2525

2626
# Binary Ninja components
@@ -514,6 +514,23 @@ def __getitem__(cls: '_ArchitectureMetaClass', name: str) -> 'Architecture':
514514
raise KeyError(f"'{name}' is not a valid architecture")
515515
return CoreArchitecture._from_cache(arch)
516516

517+
def __contains__(cls: '_ArchitectureMetaClass', name: object) -> bool:
518+
if not isinstance(name, str):
519+
return False
520+
try:
521+
cls[name]
522+
return True
523+
except KeyError:
524+
return False
525+
526+
def get(cls: '_ArchitectureMetaClass', name: str, default: Any = None) -> Optional['Architecture']:
527+
try:
528+
return cls[name]
529+
except KeyError:
530+
if default is not None:
531+
return default
532+
return None
533+
517534

518535
class Architecture(metaclass=_ArchitectureMetaClass):
519536
"""

python/binaryview.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1448,6 +1448,23 @@ def __getitem__(self, value):
14481448
raise KeyError(f"'{value}' is not a valid view type")
14491449
return BinaryViewType(view_type)
14501450

1451+
def __contains__(cls: '_BinaryViewTypeMetaclass', name: object) -> bool:
1452+
if not isinstance(name, str):
1453+
return False
1454+
try:
1455+
cls[name]
1456+
return True
1457+
except KeyError:
1458+
return False
1459+
1460+
def get(cls: '_BinaryViewTypeMetaclass', name: str, default: Any = None) -> Optional['BinaryViewType']:
1461+
try:
1462+
return cls[name]
1463+
except KeyError:
1464+
if default is not None:
1465+
return default
1466+
return None
1467+
14511468

14521469
class BinaryViewType(metaclass=_BinaryViewTypeMetaclass):
14531470
"""

python/constantrenderer.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
import traceback
2222
import ctypes
23-
from typing import Optional
23+
from typing import Optional, Any
2424

2525
import binaryninja
2626
from . import _binaryninjacore as core
@@ -51,6 +51,23 @@ def __getitem__(cls, value):
5151
raise KeyError("'%s' is not a valid renderer" % str(value))
5252
return CoreConstantRenderer(handle=renderer)
5353

54+
def __contains__(cls: '_ConstantRendererMetaClass', name: object) -> bool:
55+
if not isinstance(name, str):
56+
return False
57+
try:
58+
cls[name]
59+
return True
60+
except KeyError:
61+
return False
62+
63+
def get(cls: '_ConstantRendererMetaClass', name: str, default: Any = None) -> Optional['ConstantRenderer']:
64+
try:
65+
return cls[name]
66+
except KeyError:
67+
if default is not None:
68+
return default
69+
return None
70+
5471

5572
class ConstantRenderer(metaclass=_ConstantRendererMetaClass):
5673
"""

python/debuginfo.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
# IN THE SOFTWARE.
2020

2121
import ctypes
22-
from typing import Optional, List, Iterator, Callable, Tuple
22+
from typing import Optional, List, Iterator, Callable, Tuple, Any
2323
import traceback
2424
from dataclasses import dataclass, field
2525

@@ -78,6 +78,23 @@ def __getitem__(cls, value: str) -> 'DebugInfoParser':
7878
assert parser_ref is not None, "core.BNNewDebugInfoParserReference returned None"
7979
return DebugInfoParser(parser_ref)
8080

81+
def __contains__(cls: '_DebugInfoParserMetaClass', name: object) -> bool:
82+
if not isinstance(name, str):
83+
return False
84+
try:
85+
cls[name]
86+
return True
87+
except KeyError:
88+
return False
89+
90+
def get(cls: '_DebugInfoParserMetaClass', name: str, default: Any = None) -> Optional['DebugInfoParser']:
91+
try:
92+
return cls[name]
93+
except KeyError:
94+
if default is not None:
95+
return default
96+
return None
97+
8198
@staticmethod
8299
def get_parsers_for_view(view: 'binaryview.BinaryView') -> List['DebugInfoParser']:
83100
"""Returns a list of debug-info parsers that are valid for the provided binary view"""

python/demangle.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
from .log import log_error_for_exception
3030
from .architecture import Architecture, CoreArchitecture
3131
from .platform import Platform
32-
from typing import Iterable, List, Optional, Union, Tuple
32+
from typing import Iterable, List, Optional, Union, Tuple, Any
3333

3434

3535
def get_qualified_name(names: Iterable[str]):
@@ -300,6 +300,23 @@ def __getitem__(self, value):
300300
raise KeyError(f"'{value}' is not a valid Demangler")
301301
return CoreDemangler(handle)
302302

303+
def __contains__(cls: '_DemanglerMetaclass', name: object) -> bool:
304+
if not isinstance(name, str):
305+
return False
306+
try:
307+
cls[name]
308+
return True
309+
except KeyError:
310+
return False
311+
312+
def get(cls: '_DemanglerMetaclass', name: str, default: Any = None) -> Optional['Demangler']:
313+
try:
314+
return cls[name]
315+
except KeyError:
316+
if default is not None:
317+
return default
318+
return None
319+
303320

304321
class Demangler(metaclass=_DemanglerMetaclass):
305322
"""

python/downloadprovider.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from json import dumps
2424
import sys
2525
import traceback
26+
from typing import Any, Optional
2627
from urllib.parse import urlencode
2728

2829
# Binary Ninja Components
@@ -269,6 +270,23 @@ def __getitem__(self, value):
269270
raise KeyError(f"'{value}' is not a valid download provider")
270271
return DownloadProvider(provider)
271272

273+
def __contains__(cls: '_DownloadProviderMetaclass', name: object) -> bool:
274+
if not isinstance(name, str):
275+
return False
276+
try:
277+
cls[name]
278+
return True
279+
except KeyError:
280+
return False
281+
282+
def get(cls: '_DownloadProviderMetaclass', name: str, default: Any = None) -> Optional['DownloadProvider']:
283+
try:
284+
return cls[name]
285+
except KeyError:
286+
if default is not None:
287+
return default
288+
return None
289+
272290

273291
class DownloadProvider(metaclass=_DownloadProviderMetaclass):
274292
name = None

python/languagerepresentation.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
import ctypes
2222
import traceback
23-
from typing import List, Optional, Union
23+
from typing import List, Optional, Union, Any
2424

2525
# Binary Ninja components
2626
import binaryninja
@@ -664,6 +664,23 @@ def __getitem__(cls, value):
664664
raise KeyError("'%s' is not a valid language" % str(value))
665665
return CoreLanguageRepresentationFunctionType(handle=lang)
666666

667+
def __contains__(cls: '_LanguageRepresentationFunctionTypeMetaClass', name: object) -> bool:
668+
if not isinstance(name, str):
669+
return False
670+
try:
671+
cls[name]
672+
return True
673+
except KeyError:
674+
return False
675+
676+
def get(cls: '_LanguageRepresentationFunctionTypeMetaClass', name: str, default: Any = None) -> Optional['LanguageRepresentationFunctionType']:
677+
try:
678+
return cls[name]
679+
except KeyError:
680+
if default is not None:
681+
return default
682+
return None
683+
667684

668685
class LanguageRepresentationFunctionType(metaclass=_LanguageRepresentationFunctionTypeMetaClass):
669686
"""

python/lineformatter.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import ctypes
2222
import traceback
2323
from dataclasses import dataclass
24-
from typing import List, Optional, Union
24+
from typing import List, Optional, Union, Any
2525

2626
# Binary Ninja components
2727
import binaryninja
@@ -118,6 +118,23 @@ def __getitem__(cls, value):
118118
raise KeyError("'%s' is not a valid formatter" % str(value))
119119
return CoreLineFormatter(handle=lang)
120120

121+
def __contains__(cls: '_LineFormatterMetaClass', name: object) -> bool:
122+
if not isinstance(name, str):
123+
return False
124+
try:
125+
cls[name]
126+
return True
127+
except KeyError:
128+
return False
129+
130+
def get(cls: '_LineFormatterMetaClass', name: str, default: Any = None) -> Optional['LineFormatter']:
131+
try:
132+
return cls[name]
133+
except KeyError:
134+
if default is not None:
135+
return default
136+
return None
137+
121138

122139
class LineFormatter(metaclass=_LineFormatterMetaClass):
123140
"""

python/platform.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import ctypes
2323
import traceback
2424
import warnings
25-
from typing import List, Dict, Optional, Tuple
25+
from typing import List, Dict, Optional, Tuple, Any
2626

2727
# Binary Ninja components
2828
import binaryninja
@@ -56,6 +56,23 @@ def __getitem__(cls, value):
5656
raise KeyError("'%s' is not a valid platform" % str(value))
5757
return CorePlatform(handle=platform)
5858

59+
def __contains__(cls: '_PlatformMetaClass', name: object) -> bool:
60+
if not isinstance(name, str):
61+
return False
62+
try:
63+
cls[name]
64+
return True
65+
except KeyError:
66+
return False
67+
68+
def get(cls: '_PlatformMetaClass', name: str, default: Any = None) -> Optional['Platform']:
69+
try:
70+
return cls[name]
71+
except KeyError:
72+
if default is not None:
73+
return default
74+
return None
75+
5976

6077
class Platform(metaclass=_PlatformMetaClass):
6178
"""

python/renderlayer.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
from . import binaryview
2929
from . import types
3030
from .log import log_error_for_exception
31-
from typing import Iterable, List, Optional, Union, Tuple
31+
from typing import Iterable, List, Optional, Union, Tuple, Any
3232

3333

3434
class _RenderLayerMetaclass(type):
@@ -49,6 +49,23 @@ def __getitem__(self, value):
4949
raise KeyError(f"'{value}' is not a valid RenderLayer")
5050
return self._handle_to_instance(handle)
5151

52+
def __contains__(cls: '_RenderLayerMetaclass', name: object) -> bool:
53+
if not isinstance(name, str):
54+
return False
55+
try:
56+
cls[name]
57+
return True
58+
except KeyError:
59+
return False
60+
61+
def get(cls: '_RenderLayerMetaclass', name: str, default: Any = None) -> Optional['RenderLayer']:
62+
try:
63+
return cls[name]
64+
except KeyError:
65+
if default is not None:
66+
return default
67+
return None
68+
5269
def _handle_to_instance(self, handle):
5370
handle_ptr = ctypes.cast(handle, ctypes.c_void_p)
5471
if handle_ptr.value in RenderLayer._registered_instances:

0 commit comments

Comments
 (0)