From 11cca9aed0e5fc129367cd8d6f4a6cd41802fbb0 Mon Sep 17 00:00:00 2001 From: Avasam Date: Thu, 25 Jul 2024 14:35:23 -0400 Subject: [PATCH 1/4] Annotate pywin.framework.interact.edit --- Pythonwin/pywin/framework/interact.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Pythonwin/pywin/framework/interact.py b/Pythonwin/pywin/framework/interact.py index d93934e0e..640b3eccb 100644 --- a/Pythonwin/pywin/framework/interact.py +++ b/Pythonwin/pywin/framework/interact.py @@ -2,6 +2,7 @@ ## ## Interactive Shell Window ## +from __future__ import annotations import array import code @@ -899,7 +900,7 @@ def CreateDockedInteractiveWindow(): InteractiveDocument = winout.WindowOutputDocument # We remember our one and only interactive window in the "edit" variable. -edit = None +edit: CInteractivePython | None = None def CreateInteractiveWindowUserPreference(makeDoc=None, makeFrame=None): From 2d290cdc28ac8d9284cf1f3ef80f557f67aa9e1a Mon Sep 17 00:00:00 2001 From: Avasam Date: Thu, 25 Jul 2024 14:52:57 -0400 Subject: [PATCH 2/4] Annotate more unclear globals --- Pythonwin/pywin/framework/help.py | 5 +++-- Pythonwin/pywin/framework/interact.py | 7 +++---- Pythonwin/pywin/framework/toolmenu.py | 14 ++++++-------- 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/Pythonwin/pywin/framework/help.py b/Pythonwin/pywin/framework/help.py index a042b1865..2ee41850d 100644 --- a/Pythonwin/pywin/framework/help.py +++ b/Pythonwin/pywin/framework/help.py @@ -1,4 +1,6 @@ # help.py - help utilities for PythonWin. +from __future__ import annotations + import os import regutil @@ -122,7 +124,7 @@ def SelectAndRunHelpFile(): OpenHelpFile(helpFiles[index][1]) -helpIDMap = None +helpIDMap: dict[int, tuple[str, str]] | None = None def SetHelpMenuOtherHelp(mainMenu): @@ -144,7 +146,6 @@ def SetHelpMenuOtherHelp(mainMenu): if desc in excludeList: excludeFnames.append(fname) - helpDescs = [] for desc, fname in firstList: if fname not in excludeFnames: helpIDMap[cmdID] = (desc, fname) diff --git a/Pythonwin/pywin/framework/interact.py b/Pythonwin/pywin/framework/interact.py index 640b3eccb..c44689853 100644 --- a/Pythonwin/pywin/framework/interact.py +++ b/Pythonwin/pywin/framework/interact.py @@ -7,6 +7,7 @@ import array import code import os +import re import string import sys import traceback @@ -22,16 +23,14 @@ import win32ui from pywin.mfc import afxres +from . import winout + ## sequential after ID_GOTO_LINE defined in editor.py ID_EDIT_COPY_CODE = 0xE2002 ID_EDIT_EXEC_CLIPBOARD = 0x2003 trace = pywin.scintilla.formatter.trace -import re - -from . import winout - # from IDLE. _is_block_opener = re.compile(r":\s*(#.*)?$").search _is_block_closer = re.compile( diff --git a/Pythonwin/pywin/framework/toolmenu.py b/Pythonwin/pywin/framework/toolmenu.py index d1b7e80bb..66485f322 100644 --- a/Pythonwin/pywin/framework/toolmenu.py +++ b/Pythonwin/pywin/framework/toolmenu.py @@ -1,4 +1,4 @@ -# toolmenu.py +from __future__ import annotations import sys @@ -6,9 +6,7 @@ import win32con import win32ui -from . import app - -tools = {} +tools: dict[int, tuple[str, str, str]] = {} idPos = 100 # The default items should no tools menu exist in the INI file. @@ -115,7 +113,7 @@ def HandleToolCommand(cmd, code): global tools (menuString, pyCmd, desc) = tools[cmd] - win32ui.SetStatusText("Executing tool %s" % desc, 1) + win32ui.SetStatusText(f"Executing tool {desc}", 1) pyCmd = re.sub(r"\\n", "\n", pyCmd) win32ui.DoWaitCursor(1) oldFlag = None @@ -126,13 +124,13 @@ def HandleToolCommand(cmd, code): pass try: - exec("%s\n" % pyCmd) + exec(f"{pyCmd}\n") worked = 1 except SystemExit: # The program raised a SystemExit - ignore it. worked = 1 except: - print("Failed to execute command:\n%s" % pyCmd) + print(f"Failed to execute command:\n{pyCmd}") traceback.print_exc() worked = 0 if oldFlag is not None: @@ -141,7 +139,7 @@ def HandleToolCommand(cmd, code): if worked: text = "Completed successfully." else: - text = "Error executing %s." % desc + text = f"Error executing {desc}." win32ui.SetStatusText(text, 1) From 84d050fa8e2cb7aa447393b40d93bf5db26fc583 Mon Sep 17 00:00:00 2001 From: Avasam Date: Fri, 26 Jul 2024 15:57:19 -0400 Subject: [PATCH 3/4] Add pywin.debugger.currentDebugger --- Pythonwin/pywin/debugger/__init__.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Pythonwin/pywin/debugger/__init__.py b/Pythonwin/pywin/debugger/__init__.py index 4bcb7202a..1b076d016 100644 --- a/Pythonwin/pywin/debugger/__init__.py +++ b/Pythonwin/pywin/debugger/__init__.py @@ -1,4 +1,10 @@ +from __future__ import annotations + import sys +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from . import debugger isInprocApp = -1 @@ -29,7 +35,7 @@ def _CheckNeedGUI(): # Inject some methods in the top level name-space. -currentDebugger = None # Wipe out any old one on reload. +currentDebugger: debugger.Debugger | None = None # Wipe out any old one on reload. def _GetCurrentDebugger(): From bb05cae4a24203b66abf12958ea94f4543ccacc4 Mon Sep 17 00:00:00 2001 From: Avasam Date: Sat, 27 Jul 2024 01:58:27 -0400 Subject: [PATCH 4/4] Add g_debugService --- win32/Lib/win32serviceutil.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/win32/Lib/win32serviceutil.py b/win32/Lib/win32serviceutil.py index 5601e13eb..050ad0e20 100644 --- a/win32/Lib/win32serviceutil.py +++ b/win32/Lib/win32serviceutil.py @@ -5,6 +5,7 @@ # (which is win32service.error, pywintypes.error, etc) # when things go wrong - eg, not enough permissions to hit the # registry etc. +from __future__ import annotations import importlib.machinery import os @@ -572,6 +573,9 @@ def RestartService(serviceName, args=None, waitSeconds=30, machine=None): print("Gave up waiting for the old service to stop!") +g_debugService: ServiceFramework | None = None + + def _DebugCtrlHandler(evt): if evt in (win32con.CTRL_C_EVENT, win32con.CTRL_BREAK_EVENT): assert g_debugService @@ -581,7 +585,7 @@ def _DebugCtrlHandler(evt): return False -def DebugService(cls, argv=[]): +def DebugService(cls: type[ServiceFramework], argv=[]): # Run a service in "debug" mode. Re-implements what pythonservice.exe # does when it sees a "-debug" param. # Currently only used by "frozen" (ie, py2exe) programs (but later may