Skip to content

Commit

Permalink
[server\register.py] Replace * import with specific imports
Browse files Browse the repository at this point in the history
- Prefer `from import` over `import`
- Use LPCWSTR instead of c_wchar_p.
- Don't directly import `comtypes.server.localserver` in a function.
  • Loading branch information
moi15moi committed Jan 13, 2025
1 parent d72f57d commit c09d9bc
Showing 1 changed file with 17 additions and 24 deletions.
41 changes: 17 additions & 24 deletions comtypes/server/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,19 @@
"""

import _ctypes
import ctypes
import logging
import os
import sys
import winreg
from ctypes import WinDLL, WinError
from ctypes.wintypes import HKEY, LONG
from ctypes.wintypes import HKEY, LONG, LPCWSTR
from os.path import abspath, basename, dirname, splitext
from typing import Iterator, List, Optional, Tuple, Type, Union

import comtypes
import comtypes.server.inprocserver
from comtypes.hresult import *
from comtypes.server import w_getopt
from comtypes import CLSCTX_INPROC_SERVER, CLSCTX_LOCAL_SERVER
from comtypes.hresult import TYPE_E_CANTLOADLIBRARY, TYPE_E_REGISTRYACCESS
from comtypes.server.inprocserver import _clsid_to_class
from comtypes.server.localserver import run as run_localserver
from comtypes.server.w_getopt import w_getopt
from comtypes.typeinfo import (
REGKIND_REGISTER,
GetModuleFileName,
Expand Down Expand Up @@ -78,7 +78,7 @@ def _non_zero(retval, func, args):
LSTATUS = LONG
SHDeleteKey = _shlwapi.SHDeleteKeyW
SHDeleteKey.errcheck = _non_zero
SHDeleteKey.argtypes = HKEY, ctypes.c_wchar_p
SHDeleteKey.argtypes = HKEY, LPCWSTR
SHDeleteKey.restype = LSTATUS


Expand Down Expand Up @@ -266,14 +266,14 @@ def _get_full_classname(self, cls: Type) -> str:
"""Return <modulename>.<classname> for 'cls'."""
modname = cls.__module__
if modname == "__main__":
modname = os.path.splitext(os.path.basename(sys.argv[0]))[0]
modname = splitext(basename(sys.argv[0]))[0]
return f"{modname}.{cls.__name__}"

def _get_pythonpath(self, cls: Type) -> str:
"""Return the filesystem path of the module containing 'cls'."""
modname = cls.__module__
dirname = os.path.dirname(sys.modules[modname].__file__) # type: ignore
return os.path.abspath(dirname)
dir_name = dirname(sys.modules[modname].__file__) # type: ignore
return abspath(dir_name)

def __iter__(self) -> Iterator[Tuple[int, str, str, str]]:
"""Return a iterator of tuples containing registry entries.
Expand Down Expand Up @@ -338,8 +338,8 @@ def __iter__(self) -> Iterator[Tuple[int, str, str, str]]:
yield (HKCR, f"{reg_novers_progid}\\CLSID", "", reg_clsid) # 3a

clsctx: int = getattr(cls, "_reg_clsctx_", 0)
localsvr_ctx = bool(clsctx & comtypes.CLSCTX_LOCAL_SERVER)
inprocsvr_ctx = bool(clsctx & comtypes.CLSCTX_INPROC_SERVER)
localsvr_ctx = bool(clsctx & CLSCTX_LOCAL_SERVER)
inprocsvr_ctx = bool(clsctx & CLSCTX_INPROC_SERVER)

if localsvr_ctx and self._frozendllhandle is None:
exe = sys.executable
Expand All @@ -348,7 +348,7 @@ def __iter__(self) -> Iterator[Tuple[int, str, str, str]]:
if self._frozen is None:
if not __debug__:
exe = f"{exe} -O"
script = os.path.abspath(sys.modules[cls.__module__].__file__) # type: ignore
script = abspath(sys.modules[cls.__module__].__file__) # type: ignore
if " " in script:
script = f'"{script}"'
yield (HKCR, rf"CLSID\{reg_clsid}\LocalServer32", "", f"{exe} {script}")
Expand All @@ -365,10 +365,7 @@ def __iter__(self) -> Iterator[Tuple[int, str, str, str]]:
_get_serverdll(self._frozendllhandle),
)
# only for non-frozen inproc servers the PythonPath/PythonClass is needed.
if (
self._frozendllhandle is None
or not comtypes.server.inprocserver._clsid_to_class
):
if self._frozendllhandle is None or not _clsid_to_class:
yield (
HKCR,
rf"CLSID\{reg_clsid}\InprocServer32",
Expand Down Expand Up @@ -409,9 +406,7 @@ def unregister(cls: Type) -> None:

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

if runit:
import comtypes.server.localserver

comtypes.server.localserver.run(classes)
run_localserver(classes)

return 1 # we have done something

Expand Down

0 comments on commit c09d9bc

Please sign in to comment.