Skip to content

Commit 2182847

Browse files
committed
[server\register.py] Replace * import with specific imports
- Prefer `from import` over `import` - Use LPCWSTR instead of c_wchar_p. - Don't directly import `comtypes.server.localserver` in a function.
1 parent d72f57d commit 2182847

1 file changed

Lines changed: 12 additions & 19 deletions

File tree

comtypes/server/register.py

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,19 @@
3737
"""
3838

3939
import _ctypes
40-
import ctypes
4140
import logging
4241
import os
4342
import sys
4443
import winreg
4544
from ctypes import WinDLL, WinError
46-
from ctypes.wintypes import HKEY, LONG
45+
from ctypes.wintypes import HKEY, LONG, LPCWSTR
4746
from typing import Iterator, List, Optional, Tuple, Type, Union
4847

49-
import comtypes
50-
import comtypes.server.inprocserver
51-
from comtypes.hresult import *
52-
from comtypes.server import w_getopt
48+
from comtypes import CLSCTX_INPROC_SERVER, CLSCTX_LOCAL_SERVER
49+
from comtypes.hresult import TYPE_E_CANTLOADLIBRARY, TYPE_E_REGISTRYACCESS
50+
from comtypes.server.inprocserver import _clsid_to_class
51+
from comtypes.server.localserver import run as run_localserver
52+
from comtypes.server.w_getopt import w_getopt
5353
from comtypes.typeinfo import (
5454
REGKIND_REGISTER,
5555
GetModuleFileName,
@@ -78,7 +78,7 @@ def _non_zero(retval, func, args):
7878
LSTATUS = LONG
7979
SHDeleteKey = _shlwapi.SHDeleteKeyW
8080
SHDeleteKey.errcheck = _non_zero
81-
SHDeleteKey.argtypes = HKEY, ctypes.c_wchar_p
81+
SHDeleteKey.argtypes = HKEY, LPCWSTR
8282
SHDeleteKey.restype = LSTATUS
8383

8484

@@ -338,8 +338,8 @@ def __iter__(self) -> Iterator[Tuple[int, str, str, str]]:
338338
yield (HKCR, f"{reg_novers_progid}\\CLSID", "", reg_clsid) # 3a
339339

340340
clsctx: int = getattr(cls, "_reg_clsctx_", 0)
341-
localsvr_ctx = bool(clsctx & comtypes.CLSCTX_LOCAL_SERVER)
342-
inprocsvr_ctx = bool(clsctx & comtypes.CLSCTX_INPROC_SERVER)
341+
localsvr_ctx = bool(clsctx & CLSCTX_LOCAL_SERVER)
342+
inprocsvr_ctx = bool(clsctx & CLSCTX_INPROC_SERVER)
343343

344344
if localsvr_ctx and self._frozendllhandle is None:
345345
exe = sys.executable
@@ -365,10 +365,7 @@ def __iter__(self) -> Iterator[Tuple[int, str, str, str]]:
365365
_get_serverdll(self._frozendllhandle),
366366
)
367367
# only for non-frozen inproc servers the PythonPath/PythonClass is needed.
368-
if (
369-
self._frozendllhandle is None
370-
or not comtypes.server.inprocserver._clsid_to_class
371-
):
368+
if self._frozendllhandle is None or not _clsid_to_class:
372369
yield (
373370
HKCR,
374371
rf"CLSID\{reg_clsid}\InprocServer32",
@@ -409,9 +406,7 @@ def unregister(cls: Type) -> None:
409406

410407
def UseCommandLine(*classes: Type) -> int:
411408
usage = f"""Usage: {sys.argv[0]} [-regserver] [-unregserver] [-nodebug] [-f logformat] [-l loggername=level]"""
412-
opts, args = w_getopt.w_getopt(
413-
sys.argv[1:], "regserver unregserver embedding l: f: nodebug"
414-
)
409+
opts, args = w_getopt(sys.argv[1:], "regserver unregserver embedding l: f: nodebug")
415410
if not opts:
416411
sys.stderr.write(usage + "\n")
417412
return 0 # nothing for us to do
@@ -444,9 +439,7 @@ def UseCommandLine(*classes: Type) -> int:
444439
Registrar().nodebug(cls)
445440

446441
if runit:
447-
import comtypes.server.localserver
448-
449-
comtypes.server.localserver.run(classes)
442+
run_localserver(classes)
450443

451444
return 1 # we have done something
452445

0 commit comments

Comments
 (0)