Skip to content

Commit afcc7a7

Browse files
committed
refactor: clean up jpype imports in stubgen test and main module
1 parent 7fe31af commit afcc7a7

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

src/scyjava/_stubs/_genstubs.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@
88
from importlib import import_module
99
from itertools import chain
1010
from pathlib import Path, PurePath
11+
import sys
1112
from typing import TYPE_CHECKING, Any
1213
from unittest.mock import patch
1314
from zipfile import ZipFile
1415

16+
import jpype
17+
1518
import scyjava
1619
import scyjava.config
1720
import stubgenj
@@ -70,11 +73,6 @@ def generate_stubs(
7073
"""
7174
import jpype
7275

73-
# FIXME: either remove the _JImportLoader from sys.meta_path after this is done
74-
# (if it wasn't there to begin with), or replace the import_module calls below
75-
# with a more direct JPackage call
76-
import jpype.imports
77-
7876
startJVM = jpype.startJVM
7977

8078
scyjava.config.endpoints.extend(endpoints)
@@ -99,7 +97,16 @@ def _patched_start(*args: Any, **kwargs: Any) -> None:
9997
logger.info(f"Generating stubs for: {prefixes}")
10098
logger.info(f"Writing stubs to: {output_dir}")
10199

102-
jmodules = [import_module(prefix) for prefix in prefixes]
100+
metapath = sys.meta_path
101+
try:
102+
import jpype.imports
103+
104+
jmodules = [import_module(prefix) for prefix in prefixes]
105+
finally:
106+
# remove the jpype.imports magic from the import system
107+
# if it wasn't there to begin with
108+
sys.meta_path = metapath
109+
103110
stubgenj.generateJavaStubs(
104111
jmodules,
105112
useStubsSuffix=False,

tests/test_stubgen.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from unittest.mock import patch
66

77
import jpype
8-
import jpype.imports
98

109
import scyjava
1110
from scyjava._stubs import _cli

0 commit comments

Comments
 (0)