From c3bcdadea4a34b7fb39f0ef228aaa3fa3c14a84a Mon Sep 17 00:00:00 2001 From: Avasam Date: Tue, 4 Jun 2024 16:03:23 -0400 Subject: [PATCH 1/3] Replace all old-style `%r` string formatting --- Pythonwin/Scintilla/src/LexGen.py | 6 ++++-- Pythonwin/pywin/framework/interact.py | 4 ++-- com/win32com/client/genpy.py | 2 +- com/win32com/demos/connect.py | 2 +- com/win32com/test/util.py | 7 ++----- com/win32comext/adsi/__init__.py | 2 +- com/win32comext/adsi/demos/scp.py | 6 +++--- com/win32comext/shell/demos/servers/folder_view.py | 2 +- isapi/samples/redirector.py | 4 ++-- setup.py | 2 +- win32/Demos/NetValidatePasswordPolicy.py | 4 ++-- win32/Lib/win32evtlogutil.py | 9 +++------ win32/Lib/win32rcparser.py | 6 ++---- win32/Lib/win32verstamp.py | 2 +- win32/test/testall.py | 2 +- 15 files changed, 27 insertions(+), 33 deletions(-) diff --git a/Pythonwin/Scintilla/src/LexGen.py b/Pythonwin/Scintilla/src/LexGen.py index 7c1d9eb0b7..1c73d344a5 100644 --- a/Pythonwin/Scintilla/src/LexGen.py +++ b/Pythonwin/Scintilla/src/LexGen.py @@ -132,8 +132,10 @@ def Generate(inpath, outpath, commentPrefix, eolType, *lists): file. It should be one of following constants: LF, CRLF, CR, or NATIVE. """ - # print "generate '%s' -> '%s' (comment prefix: %r, eols: %r)"\ - # % (inpath, outpath, commentPrefix, eolType) + # print( + # f"generate '{inpath}' -> '{outpath}' " + # + f"(comment prefix: {commentPrefix!r}, eols: {eolType!r})" + # ) try: infile = open(inpath, "r") except OSError: diff --git a/Pythonwin/pywin/framework/interact.py b/Pythonwin/pywin/framework/interact.py index c4021ebf76..a3d0cacba0 100644 --- a/Pythonwin/pywin/framework/interact.py +++ b/Pythonwin/pywin/framework/interact.py @@ -158,7 +158,7 @@ def ColorizeInteractiveCode(self, cdoc, styleStart, stylePyStart): ch = chNext chNext = cdoc[i + 1 : i + 2].decode("latin-1") - # trace("ch=%r, i=%d, next=%r, state=%s" % (ch, i, chNext, state)) + # trace(f"ch={ch!r}, i={i}, next={chNext!r}, state={state}") if state == STYLE_INTERACTIVE_EOL: if ch not in "\r\n": self.ColorSeg(startSeg, i - 1, state) @@ -357,7 +357,7 @@ def Init(self): ) except: sys.stderr.write( - ">>> \nError executing PYTHONSTARTUP script %r\n" % (rcfile) + f">>> \nError executing PYTHONSTARTUP script {rcfile!r}\n" ) traceback.print_exc(file=sys.stderr) self.AppendToPrompt([]) diff --git a/com/win32com/client/genpy.py b/com/win32com/client/genpy.py index a7c4857f90..0ff673b0bb 100644 --- a/com/win32com/client/genpy.py +++ b/com/win32com/client/genpy.py @@ -276,7 +276,7 @@ def WriteVTableMap(self, generator): if item_num % 5 == 0: print("\n\t\t\t", end=" ", file=stream) print( - "), %d, (%r, %r, [" % (dispid, desc.memid, desc.scodeArray), + f"), {dispid}, ({desc.memid!r}, {desc.scodeArray!r}, [", end=" ", file=stream, ) diff --git a/com/win32com/demos/connect.py b/com/win32com/demos/connect.py index 197d7a6255..7152437c05 100644 --- a/com/win32com/demos/connect.py +++ b/com/win32com/demos/connect.py @@ -65,7 +65,7 @@ def CheckEvent(server, client, val, verbose): if client.last_event_arg != val: raise RuntimeError(f"Sent {val!r}, but got back {client.last_event_arg!r}") if verbose: - print("Sent and received %r" % val) + print(f"Sent and received {val!r}") # A simple test script for all this. diff --git a/com/win32com/test/util.py b/com/win32com/test/util.py index d649b7fcb0..10e185ec8a 100644 --- a/com/win32com/test/util.py +++ b/com/win32com/test/util.py @@ -77,10 +77,7 @@ def RegisterPythonServer(filename, progids=None, verbose=0): # old, less-secure OS - assume *is* admin. is_admin = True if not is_admin: - msg = ( - "%r isn't registered, but I'm not an administrator who can register it." - % progids[0] - ) + msg = f"{progids[0]!r} isn't registered, but I'm not an administrator who can register it." if why_not: msg += "\n(registration check failed as %s)" % why_not # throw a normal "class not registered" exception - we don't report @@ -122,7 +119,7 @@ class Failed(Exception): raise Failed("traceback in program output") return output except Failed as why: - print("Failed to exec command '%r'" % cmd) + print(f"Failed to exec command '{cmd}'") print("Failed as", why) print("** start of program output **") print(output) diff --git a/com/win32comext/adsi/__init__.py b/com/win32comext/adsi/__init__.py index 16afd3b784..d928f4c3a7 100644 --- a/com/win32comext/adsi/__init__.py +++ b/com/win32comext/adsi/__init__.py @@ -43,7 +43,7 @@ def _get_good_ret( if hasattr(ob, "Invoke"): import win32com.client.dynamic - name = "Dispatch wrapper around %r" % ob + name = f"Dispatch wrapper around {ob!r}" return win32com.client.dynamic.Dispatch(ob, name, ADSIDispatch) return ob diff --git a/com/win32comext/adsi/demos/scp.py b/com/win32comext/adsi/demos/scp.py index b2ff016ec9..705ad2af42 100644 --- a/com/win32comext/adsi/demos/scp.py +++ b/com/win32comext/adsi/demos/scp.py @@ -219,9 +219,9 @@ def SpnRegister( spns, # List of SPNs to register operation, # Add, replace, or delete SPNs ): - assert not isinstance(spns, str) and hasattr(spns, "__iter__"), ( - "spns must be a sequence of strings (got %r)" % spns - ) + assert not isinstance(spns, str) and hasattr( + spns, "__iter__" + ), f"spns must be a sequence of strings (got {spns!r})" # Bind to a domain controller. # Get the domain for the current user. samName = win32api.GetUserNameEx(win32api.NameSamCompatible) diff --git a/com/win32comext/shell/demos/servers/folder_view.py b/com/win32comext/shell/demos/servers/folder_view.py index f2d039a15a..6afc9271c6 100644 --- a/com/win32comext/shell/demos/servers/folder_view.py +++ b/com/win32comext/shell/demos/servers/folder_view.py @@ -168,7 +168,7 @@ def DisplayItem(shell_item_array, hwnd_parent=0): else: si = shell_item_array.GetItemAt(0) name = si.GetDisplayName(shellcon.SIGDN_NORMALDISPLAY) - msg = "%d items selected, first is %r" % (shell_item_array.GetCount(), name) + msg = f"{shell_item_array.GetCount()} items selected, first is {name!r}" win32gui.MessageBox(hwnd_parent, msg, "Hello", win32con.MB_OK) diff --git a/isapi/samples/redirector.py b/isapi/samples/redirector.py index e03efcabc4..6ac8b4706f 100644 --- a/isapi/samples/redirector.py +++ b/isapi/samples/redirector.py @@ -41,8 +41,8 @@ def io_callback(ecb, url, cbIO, errcode): # Get the status of our ExecURL httpstatus, substatus, win32 = ecb.GetExecURLStatus() print( - "ExecURL of %r finished with http status %d.%d, win32 status %d (%s)" - % (url, httpstatus, substatus, win32, win32api.FormatMessage(win32).strip()) + f"ExecURL of {url!r} finished with http status {httpstatus}.{substatus}, " + + f"win32 status {win32} ({win32api.FormatMessage(win32).strip()})" ) # nothing more to do! ecb.DoneWithSession() diff --git a/setup.py b/setup.py index 4723757c5b..0cc7f14c83 100644 --- a/setup.py +++ b/setup.py @@ -646,7 +646,7 @@ def build_extensions(self): # Only mfcNNNu DLL is required (mfcmNNNX is Windows Forms, rest is ANSI) mfc_contents = next(filter(None, map(glob.glob, redist_globs)), [])[:1] if not mfc_contents: - raise RuntimeError("MFC redist DLLs not found like %r!" % redist_globs) + raise RuntimeError(f"MFC redist DLLs not found like {redist_globs!r}!") target_dir = os.path.join(self.build_lib, win32ui_ext.get_pywin32_dir()) for mfc_content in mfc_contents: diff --git a/win32/Demos/NetValidatePasswordPolicy.py b/win32/Demos/NetValidatePasswordPolicy.py index be9f3aa345..6bbf411ef1 100644 --- a/win32/Demos/NetValidatePasswordPolicy.py +++ b/win32/Demos/NetValidatePasswordPolicy.py @@ -116,8 +116,8 @@ def main(): pprint(fields) print( - "Result of %r validation is %d: %s" - % (arg, status, win32api.FormatMessage(status).strip()) + f"Result of {arg!r} validation is {status}:", + win32api.FormatMessage(status).strip(), ) return 0 diff --git a/win32/Lib/win32evtlogutil.py b/win32/Lib/win32evtlogutil.py index f7b67a553c..5dcaa444da 100644 --- a/win32/Lib/win32evtlogutil.py +++ b/win32/Lib/win32evtlogutil.py @@ -200,12 +200,9 @@ def SafeFormatMessage(eventLogRecord, logType=None): else: desc = ", ".join(eventLogRecord.StringInserts) return ( - "" - % ( - winerror.HRESULT_CODE(eventLogRecord.EventID), - eventLogRecord.SourceName, - desc, - ) + f"" ) diff --git a/win32/Lib/win32rcparser.py b/win32/Lib/win32rcparser.py index b87bd96feb..f6edcb1522 100644 --- a/win32/Lib/win32rcparser.py +++ b/win32/Lib/win32rcparser.py @@ -618,7 +618,7 @@ def GenerateFrozenResource(rc_name, output_name, h_name=None): out = open(output_name, "wt") out.write("#%s\n" % output_name) out.write("#This is a generated file. Please edit %s instead.\n" % rc_name) - out.write("__version__=%r\n" % __version__) + out.write(f"__version__={__version__!r}\n") out.write( "_rc_size_=%d\n_rc_mtime_=%d\n" % (in_stat[stat.ST_SIZE], in_stat[stat.ST_MTIME]) @@ -630,9 +630,7 @@ def GenerateFrozenResource(rc_name, output_name, h_name=None): out.write("\t\tself.idNum = idNum\n") out.write("\t\tself.value = value\n") out.write("\tdef __repr__(self):\n") - out.write( - '\t\treturn "StringDef(%r, %r, %r)" % (self.id, self.idNum, self.value)\n' - ) + out.write('\t\treturn f"StringDef({self.id!r}, {self.idNum!r}, {self.value!r})"\n') out.write("class FakeParser:\n") diff --git a/win32/Lib/win32verstamp.py b/win32/Lib/win32verstamp.py index 1fe0a6b7f2..01e1cfb2c3 100644 --- a/win32/Lib/win32verstamp.py +++ b/win32/Lib/win32verstamp.py @@ -134,7 +134,7 @@ def stamp(pathname, options): bits = [int(i) for i in ver.split(".")] vmaj, vmin, vsub, vbuild = bits except (IndexError, TypeError, ValueError): - raise ValueError("--version must be a.b.c.d (all integers) - got %r" % ver) + raise ValueError(f"--version must be a.b.c.d (all integers) - got {ver!r}") ifn = options.internal_name if not ifn: diff --git a/win32/test/testall.py b/win32/test/testall.py index 7862bc6f51..38fcbcdaea 100644 --- a/win32/test/testall.py +++ b/win32/test/testall.py @@ -174,7 +174,7 @@ def suite(): try: mod = __import__(base) except: - print("FAILED to import test module %r" % base) + print(f"FAILED to import test module {base!r}") traceback.print_exc() continue if hasattr(mod, "suite"): From 6196b6883c2d8c45c9ab7e148e58f3ae2a4d3e14 Mon Sep 17 00:00:00 2001 From: Avasam Date: Tue, 4 Jun 2024 16:06:32 -0400 Subject: [PATCH 2/3] Update Pythonwin/Scintilla/src/LexGen.py --- Pythonwin/Scintilla/src/LexGen.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Pythonwin/Scintilla/src/LexGen.py b/Pythonwin/Scintilla/src/LexGen.py index 1c73d344a5..7c1d9eb0b7 100644 --- a/Pythonwin/Scintilla/src/LexGen.py +++ b/Pythonwin/Scintilla/src/LexGen.py @@ -132,10 +132,8 @@ def Generate(inpath, outpath, commentPrefix, eolType, *lists): file. It should be one of following constants: LF, CRLF, CR, or NATIVE. """ - # print( - # f"generate '{inpath}' -> '{outpath}' " - # + f"(comment prefix: {commentPrefix!r}, eols: {eolType!r})" - # ) + # print "generate '%s' -> '%s' (comment prefix: %r, eols: %r)"\ + # % (inpath, outpath, commentPrefix, eolType) try: infile = open(inpath, "r") except OSError: From 4cbe9ed555b35be5ba14f22c5ae3e2d8f54d7d06 Mon Sep 17 00:00:00 2001 From: Avasam Date: Mon, 28 Apr 2025 20:16:33 -0400 Subject: [PATCH 3/3] Run Ruff format --- com/win32comext/adsi/demos/scp.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/com/win32comext/adsi/demos/scp.py b/com/win32comext/adsi/demos/scp.py index 8717a0ab6f..6be9e97c76 100644 --- a/com/win32comext/adsi/demos/scp.py +++ b/com/win32comext/adsi/demos/scp.py @@ -219,9 +219,9 @@ def SpnRegister( spns, # List of SPNs to register operation, # Add, replace, or delete SPNs ): - assert not isinstance(spns, str) and hasattr( - spns, "__iter__" - ), f"spns must be a sequence of strings (got {spns!r})" + assert not isinstance(spns, str) and hasattr(spns, "__iter__"), ( + f"spns must be a sequence of strings (got {spns!r})" + ) # Bind to a domain controller. # Get the domain for the current user. samName = win32api.GetUserNameEx(win32api.NameSamCompatible)