Skip to content

Commit 11ebcd2

Browse files
committed
Allow dynamic import of an entire module
1 parent f04e181 commit 11ebcd2

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

qiling/os/posix/posix.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from qiling.exception import QlErrorSyscallNotFound
2727
from qiling.os.os import QlOs
2828
from qiling.os.posix.const import NR_OPEN, errors
29-
from qiling.utils import ql_get_module_function
29+
from qiling.utils import ql_get_module, ql_get_module_function
3030

3131
SYSCALL_PREF: str = f'ql_syscall_'
3232

qiling/utils.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import importlib, os
1313

1414
from configparser import ConfigParser
15+
from types import ModuleType
1516
from typing import TYPE_CHECKING, Any, Callable, Mapping, Optional, Tuple, TypeVar, Union
1617

1718
from unicorn import UC_ERR_READ_UNMAPPED, UC_ERR_FETCH_UNMAPPED
@@ -66,24 +67,23 @@ def debugger_convert(debugger: str) -> Optional[QL_DEBUGGER]:
6667
def arch_os_convert(arch: QL_ARCH) -> Optional[QL_OS]:
6768
return arch_os_map.get(arch)
6869

69-
# Call `function_name` in `module_name`.
70-
# e.g. map_syscall in qiling.os.linux.map_syscall
71-
def ql_get_module_function(module_name: str, function_name: str):
72-
70+
def ql_get_module(module_name: str) -> ModuleType:
7371
try:
74-
imp_module = importlib.import_module(module_name, 'qiling')
75-
except ModuleNotFoundError:
76-
raise QlErrorModuleNotFound(f'Unable to import module {module_name}')
77-
except KeyError:
72+
module = importlib.import_module(module_name, 'qiling')
73+
except (ModuleNotFoundError, KeyError):
7874
raise QlErrorModuleNotFound(f'Unable to import module {module_name}')
7975

76+
return module
77+
78+
def ql_get_module_function(module_name: str, member_name: str):
79+
module = ql_get_module(module_name)
80+
8081
try:
81-
module_function = getattr(imp_module, function_name)
82+
member = getattr(module, member_name)
8283
except AttributeError:
83-
raise QlErrorModuleFunctionNotFound(f'Unable to import {function_name} from {imp_module}')
84-
85-
return module_function
84+
raise QlErrorModuleFunctionNotFound(f'Unable to import {member_name} from {module_name}')
8685

86+
return member
8787

8888
def __emu_env_from_pathname(path: str) -> Tuple[Optional[QL_ARCH], Optional[QL_OS], Optional[QL_ENDIAN]]:
8989
if os.path.isdir(path) and path.endswith('.kext'):

0 commit comments

Comments
 (0)