Skip to content

Commit 841969f

Browse files
committed
Reduce conditional branches for retrieving server dll.
1 parent 1b15233 commit 841969f

2 files changed

Lines changed: 9 additions & 18 deletions

File tree

comtypes/server/register.py

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -241,11 +241,10 @@ def _unregister(self, cls: Type, force: bool = False) -> None:
241241
_debug("Done")
242242

243243

244-
def _get_serverdll(handle: Optional[int]) -> str:
244+
def _get_serverdll(handle: int) -> str:
245245
"""Return the pathname of the dll hosting the COM object."""
246-
if handle is not None:
247-
return GetModuleFileName(handle, 260)
248-
return _ctypes.__file__
246+
assert isinstance(handle, int)
247+
return GetModuleFileName(handle, 260)
249248

250249

251250
class RegistryEntries(abc.ABC):
@@ -295,7 +294,9 @@ def __iter__(self) -> Iterator[_Entry]:
295294
if localsvr_ctx and self._frozendllhandle is None:
296295
yield from _iter_frozen_local_ctx_entries(self._cls, reg_clsid)
297296
if inprocsvr_ctx and self._frozen == "dll":
298-
yield from _iter_inproc_ctx_entries(reg_clsid, self._frozendllhandle)
297+
assert self._frozendllhandle is not None
298+
frozen_dll = _get_serverdll(self._frozendllhandle)
299+
yield from _iter_inproc_ctx_entries(reg_clsid, frozen_dll)
299300
yield from _iter_inproc_threading_model_entries(self._cls, reg_clsid)
300301
yield from _iter_tlib_entries(self._cls, reg_clsid)
301302

@@ -314,7 +315,7 @@ def __iter__(self) -> Iterator[_Entry]:
314315
if localsvr_ctx:
315316
yield from _iter_interp_local_ctx_entries(self._cls, reg_clsid)
316317
if inprocsvr_ctx:
317-
yield from _iter_inproc_ctx_entries(reg_clsid, None)
318+
yield from _iter_inproc_ctx_entries(reg_clsid, _ctypes.__file__)
318319
# only for non-frozen inproc servers the PythonPath/PythonClass is needed.
319320
yield from _iter_inproc_python_entries(self._cls, reg_clsid)
320321
yield from _iter_inproc_threading_model_entries(self._cls, reg_clsid)
@@ -388,17 +389,10 @@ def _iter_frozen_local_ctx_entries(cls: Type, reg_clsid: str) -> Iterator[_Entry
388389
yield (HKCR, rf"CLSID\{reg_clsid}\LocalServer32", "", f"{exe}")
389390

390391

391-
def _iter_inproc_ctx_entries(
392-
reg_clsid: str, frozendllhandle: Optional[int]
393-
) -> Iterator[_Entry]:
392+
def _iter_inproc_ctx_entries(reg_clsid: str, dllfile: str) -> Iterator[_Entry]:
394393
# Register InprocServer32 only when run from script or from
395394
# py2exe dll server, not from py2exe exe server.
396-
yield (
397-
HKCR,
398-
rf"CLSID\{reg_clsid}\InprocServer32",
399-
"",
400-
_get_serverdll(frozendllhandle),
401-
)
395+
yield (HKCR, rf"CLSID\{reg_clsid}\InprocServer32", "", dllfile)
402396

403397

404398
def _iter_inproc_python_entries(cls: Type, reg_clsid: str) -> Iterator[_Entry]:

comtypes/test/test_server_register.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,6 @@ def test_calls_cls_unregister(self):
195195

196196

197197
class Test_get_serverdll(ut.TestCase):
198-
def test_nonfrozen(self):
199-
self.assertEqual(_ctypes.__file__, _get_serverdll(None))
200-
201198
@mock.patch.object(register, "GetModuleFileName")
202199
def test_frozen(self, GetModuleFileName):
203200
handle, dll_path = 1234, r"path\to\frozen.dll"

0 commit comments

Comments
 (0)