mirror of
https://github.com/NationalSecurityAgency/ghidra.git
synced 2026-05-30 16:47:43 +08:00
Fix Python type annotations in PyGhidra module when using contextmanager
Verifying the type annotations used by PyGhidra with Mypy static type
checker leads to the following error:
core.py:171: error: Argument 1 to "contextmanager" has incompatible
type "Callable[[str | Path, str | Path, str, Any, str, str, str |
JClass, str, Any], AbstractContextManager[Any, bool | None]]";
expected "Callable[[str | Path, str | Path, str, Any, str, str, str
| JClass, str, Any], Iterator[Never]]" [arg-type]
Indeed, in Ghidra/Features/PyGhidra/src/main/py/src/pyghidra/core.py,
function open_program was declared to return a
ContextManager["FlatProgramAPI"]. While this function indeed returns
such a type, the implementation uses decorator @contextlib.contextmanager
which expects the wrapped function to return an generator (with yield).
Use Generator["FlatProgramAPI", None, None] to fix this.
While at it, fix other locations where the type annotation of the
function wrapped with contextmanager was incorrect.
This commit is contained in:
committed by
Ryan Kurtz
parent
a93de758f7
commit
fc0f971c39
@@ -15,7 +15,7 @@
|
|||||||
##
|
##
|
||||||
import sys
|
import sys
|
||||||
import contextlib
|
import contextlib
|
||||||
from typing import Union, TYPE_CHECKING, Tuple, List, Callable, Any, Optional
|
from typing import Union, TYPE_CHECKING, Tuple, List, Callable, Generator, Any, Optional
|
||||||
|
|
||||||
from pyghidra.converters import * # pylint: disable=wildcard-import, unused-wildcard-import
|
from pyghidra.converters import * # pylint: disable=wildcard-import, unused-wildcard-import
|
||||||
|
|
||||||
@@ -142,7 +142,7 @@ def consume_program(
|
|||||||
def program_context(
|
def program_context(
|
||||||
project: "Project",
|
project: "Project",
|
||||||
path: Union[str, Path],
|
path: Union[str, Path],
|
||||||
) -> "Program":
|
) -> Generator["Program", None, None]:
|
||||||
"""
|
"""
|
||||||
Gets the Ghidra program from the given project with the given project path. The returned
|
Gets the Ghidra program from the given project with the given project path. The returned
|
||||||
program's resource cleanup is performed by a context manager.
|
program's resource cleanup is performed by a context manager.
|
||||||
@@ -256,7 +256,7 @@ def ghidra_script(
|
|||||||
def transaction(
|
def transaction(
|
||||||
program: "Program",
|
program: "Program",
|
||||||
description: str = "Unnamed Transaction"
|
description: str = "Unnamed Transaction"
|
||||||
):
|
) -> Generator[int, None, None]:
|
||||||
"""
|
"""
|
||||||
Creates a context for running a Ghidra transaction.
|
Creates a context for running a Ghidra transaction.
|
||||||
|
|
||||||
|
|||||||
@@ -15,7 +15,7 @@
|
|||||||
##
|
##
|
||||||
import contextlib
|
import contextlib
|
||||||
import warnings
|
import warnings
|
||||||
from typing import Union, TYPE_CHECKING, Tuple, ContextManager, List, Optional
|
from typing import Union, TYPE_CHECKING, Tuple, Generator, List, Optional
|
||||||
|
|
||||||
from pyghidra.converters import * # pylint: disable=wildcard-import, unused-wildcard-import
|
from pyghidra.converters import * # pylint: disable=wildcard-import, unused-wildcard-import
|
||||||
|
|
||||||
@@ -179,7 +179,7 @@ def open_program(
|
|||||||
loader: Union[str, JClass] = None,
|
loader: Union[str, JClass] = None,
|
||||||
program_name: str = None,
|
program_name: str = None,
|
||||||
nested_project_location = True
|
nested_project_location = True
|
||||||
) -> ContextManager["FlatProgramAPI"]: # type: ignore
|
) -> Generator["FlatProgramAPI", None, None]:
|
||||||
"""
|
"""
|
||||||
Opens given binary path (or optional program name) in Ghidra and returns FlatProgramAPI object.
|
Opens given binary path (or optional program name) in Ghidra and returns FlatProgramAPI object.
|
||||||
|
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ import sys
|
|||||||
import threading
|
import threading
|
||||||
import types
|
import types
|
||||||
from code import InteractiveConsole
|
from code import InteractiveConsole
|
||||||
|
from typing import Generator
|
||||||
|
|
||||||
from ghidra.app.script import ScriptControls
|
from ghidra.app.script import ScriptControls
|
||||||
from ghidra.framework import Application
|
from ghidra.framework import Application
|
||||||
@@ -276,7 +277,7 @@ class PyConsole(InteractiveConsole):
|
|||||||
self.reset()
|
self.reset()
|
||||||
|
|
||||||
@contextlib.contextmanager
|
@contextlib.contextmanager
|
||||||
def redirect_writer(self):
|
def redirect_writer(self) -> Generator[None, None, None]:
|
||||||
self._writer = self._err
|
self._writer = self._err
|
||||||
try:
|
try:
|
||||||
yield
|
yield
|
||||||
@@ -292,7 +293,7 @@ class PyConsole(InteractiveConsole):
|
|||||||
super().showtraceback()
|
super().showtraceback()
|
||||||
|
|
||||||
@contextlib.contextmanager
|
@contextlib.contextmanager
|
||||||
def _run_context(self):
|
def _run_context(self) -> Generator[None, None, None]:
|
||||||
self._script.start()
|
self._script.start()
|
||||||
success = False
|
success = False
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ import tempfile
|
|||||||
import threading
|
import threading
|
||||||
from importlib.machinery import ModuleSpec
|
from importlib.machinery import ModuleSpec
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import List, NoReturn, Tuple, Union
|
from typing import Generator, List, NoReturn, Tuple, Union
|
||||||
|
|
||||||
import jpype
|
import jpype
|
||||||
from jpype import imports, _jpype
|
from jpype import imports, _jpype
|
||||||
@@ -43,7 +43,7 @@ logger = logging.getLogger(__name__)
|
|||||||
|
|
||||||
|
|
||||||
@contextlib.contextmanager
|
@contextlib.contextmanager
|
||||||
def _silence_java_output(stdout=True, stderr=True):
|
def _silence_java_output(stdout=True, stderr=True) -> Generator[None, None, None]:
|
||||||
from java.io import OutputStream, PrintStream # type:ignore @UnresolvedImport
|
from java.io import OutputStream, PrintStream # type:ignore @UnresolvedImport
|
||||||
from java.lang import System # type:ignore @UnresolvedImport
|
from java.lang import System # type:ignore @UnresolvedImport
|
||||||
out = System.out
|
out = System.out
|
||||||
|
|||||||
Reference in New Issue
Block a user