From 2ab3e276324ffbd7e6781923f017c312357eca0b Mon Sep 17 00:00:00 2001 From: Avasam Date: Fri, 26 Jul 2024 19:56:26 -0400 Subject: [PATCH 01/10] adodbapi: Simplify dict iterations from Python 2 to 3 migration --- adodbapi/adodbapi.py | 4 +--- adodbapi/apibase.py | 2 +- adodbapi/process_connect_string.py | 2 +- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/adodbapi/adodbapi.py b/adodbapi/adodbapi.py index 4e85bc525..949fcfe54 100644 --- a/adodbapi/adodbapi.py +++ b/adodbapi/adodbapi.py @@ -326,9 +326,7 @@ def close(self): an Error (or subclass) exception will be raised if any operation is attempted with the connection. The same applies to all cursor objects trying to use the connection. """ - for crsr in list(self.cursors.values())[ - : - ]: # copy the list, then close each one + for crsr in self.cursors.values(): crsr.close(dont_tell_me=True) # close without back-link clearing self.messages = [] try: diff --git a/adodbapi/apibase.py b/adodbapi/apibase.py index 749c378d3..b7d1f3ae3 100644 --- a/adodbapi/apibase.py +++ b/adodbapi/apibase.py @@ -564,7 +564,7 @@ def __next__(self): yield self._getValue(n) def __repr__(self): # create a human readable representation - taglist = sorted(list(self.rows.columnNames.items()), key=lambda x: x[1]) + taglist = sorted(self.rows.columnNames.items(), key=lambda x: x[1]) s = " Date: Fri, 26 Jul 2024 20:02:34 -0400 Subject: [PATCH 02/10] Add changes --- CHANGES.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.txt b/CHANGES.txt index 0e11d0643..ecc218f3a 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -181,6 +181,7 @@ Coming in build 307, as yet unreleased * `adodbapi.apibase.memoryViewType` * Remove outdated and unused remote feature (#2098, @Avasam) * Migrated from `distutils` to `setuptools` (#2133, @Avasam) +* Simplify dict iterations from Python 2 to 3 migration (#2332, @Avasam) Build 306, released 2023-03-26 ------------------------------ From eb0380393ff168455a1e34fe64ed53e2f6f18b19 Mon Sep 17 00:00:00 2001 From: Avasam Date: Sat, 27 Jul 2024 04:06:49 -0400 Subject: [PATCH 03/10] explicitly type cursors --- adodbapi/adodbapi.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/adodbapi/adodbapi.py b/adodbapi/adodbapi.py index 949fcfe54..eb8153c33 100644 --- a/adodbapi/adodbapi.py +++ b/adodbapi/adodbapi.py @@ -234,7 +234,9 @@ def __init__(self): # now define the instance attributes self.paramstyle = api.paramstyle self.supportsTransactions = False self.connection_string = "" - self.cursors = weakref.WeakValueDictionary() + self.cursors: weakref.WeakValueDictionary[int, Cursor] = ( + weakref.WeakValueDictionary() + ) self.dbms_name = "" self.dbms_version = "" self.errorhandler = None # use the standard error handler for this instance From 9dd4f4f01fc0f074af6c9250e4e1ccf204aec62c Mon Sep 17 00:00:00 2001 From: Avasam Date: Sat, 27 Jul 2024 04:19:33 -0400 Subject: [PATCH 04/10] avoid size changing during iteration --- adodbapi/adodbapi.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/adodbapi/adodbapi.py b/adodbapi/adodbapi.py index eb8153c33..f31d3f3fd 100644 --- a/adodbapi/adodbapi.py +++ b/adodbapi/adodbapi.py @@ -328,7 +328,8 @@ def close(self): an Error (or subclass) exception will be raised if any operation is attempted with the connection. The same applies to all cursor objects trying to use the connection. """ - for crsr in self.cursors.values(): + # copy the list of cursors to avoid size changing during iteration, then close each one + for crsr in list(self.cursors.values()): crsr.close(dont_tell_me=True) # close without back-link clearing self.messages = [] try: From 572ac529d4cb1f30891c430685cc58cc4e0ea490 Mon Sep 17 00:00:00 2001 From: Avasam Date: Fri, 18 Oct 2024 13:41:32 -0400 Subject: [PATCH 05/10] Update CHANGES.txt --- CHANGES.txt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 50c7bb255..58c5978c7 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -24,7 +24,7 @@ Coming in build 309, as yet unreleased Import `DispatcherWin32trace` and `DispatcherTrace` from `win32com.server.dispatcher` instead. * Fixed `win32timezone.TimeZoneInfo` initialization from a `[DYNAMIC_]TIME_ZONE_INFORMATION` (#2339, @Avasam) * Added runtime deprecation warning of `win2kras`, use `win32ras` instead (#2356, @Avasam) -* Improved handling of dict iterations and fallbacks (removes Python 2 support code, small general speed improvement) (#2332, #2330, @Avasam) +* Improved handling of dict iterations and fallbacks (removes Python 2 support code, small general speed improvement) (#2332, #2330, #2332 @Avasam) * Fixed "Open GL Demo" (`Pythonwin/pywin/Demos/openGLDemo.py`) and restore "Font" demo in `Pythonwin/pywin/Demos/guidemo.py` (#2345, @Avasam) * Fixed accidentally trying to raise an undefined name instead of an `Exception` in `Pythonwin/pywin/debugger/debugger.py` (#2326, @Avasam) @@ -216,7 +216,6 @@ as the .chm file, certain MAPI libraries etc, and .exe installers. * `adodbapi.apibase.memoryViewType` * Remove outdated and unused remote feature (#2098, @Avasam) * Migrated from `distutils` to `setuptools` (#2133, @Avasam) -* Simplify dict iterations from Python 2 to 3 migration (#2332, @Avasam) Build 306, released 2023-03-26 ------------------------------ From 6309ef1e9283d2c156a58e283f769c3cacdfaf5b Mon Sep 17 00:00:00 2001 From: Avasam Date: Fri, 18 Oct 2024 13:45:32 -0400 Subject: [PATCH 06/10] Simplify another items to values --- adodbapi/apibase.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adodbapi/apibase.py b/adodbapi/apibase.py index b7d1f3ae3..d22173c46 100644 --- a/adodbapi/apibase.py +++ b/adodbapi/apibase.py @@ -564,7 +564,7 @@ def __next__(self): yield self._getValue(n) def __repr__(self): # create a human readable representation - taglist = sorted(self.rows.columnNames.items(), key=lambda x: x[1]) + taglist = sorted(self.rows.columnNames.values()) s = " Date: Fri, 18 Oct 2024 13:46:15 -0400 Subject: [PATCH 07/10] typo --- CHANGES.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES.txt b/CHANGES.txt index 58c5978c7..ebed84aab 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -24,7 +24,7 @@ Coming in build 309, as yet unreleased Import `DispatcherWin32trace` and `DispatcherTrace` from `win32com.server.dispatcher` instead. * Fixed `win32timezone.TimeZoneInfo` initialization from a `[DYNAMIC_]TIME_ZONE_INFORMATION` (#2339, @Avasam) * Added runtime deprecation warning of `win2kras`, use `win32ras` instead (#2356, @Avasam) -* Improved handling of dict iterations and fallbacks (removes Python 2 support code, small general speed improvement) (#2332, #2330, #2332 @Avasam) +* Improved handling of dict iterations and fallbacks (removes Python 2 support code, small general speed improvement) (#2332, #2330, #2332, @Avasam) * Fixed "Open GL Demo" (`Pythonwin/pywin/Demos/openGLDemo.py`) and restore "Font" demo in `Pythonwin/pywin/Demos/guidemo.py` (#2345, @Avasam) * Fixed accidentally trying to raise an undefined name instead of an `Exception` in `Pythonwin/pywin/debugger/debugger.py` (#2326, @Avasam) From 9d661974cc3d18500e6e5df9e6a71311a116aedc Mon Sep 17 00:00:00 2001 From: Avasam Date: Fri, 18 Oct 2024 14:00:26 -0400 Subject: [PATCH 08/10] Avoid changing size during iteration --- adodbapi/process_connect_string.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/adodbapi/process_connect_string.py b/adodbapi/process_connect_string.py index 75b30395f..6db077ec0 100644 --- a/adodbapi/process_connect_string.py +++ b/adodbapi/process_connect_string.py @@ -1,4 +1,9 @@ -""" a clumsy attempt at a macro language to let the programmer execute code on the server (ex: determine 64bit)""" +"""a clumsy attempt at a macro language to let the programmer execute code on the server (ex: determine 64bit)""" + +from __future__ import annotations + +from collections.abc import MutableMapping +from typing import Any from . import is64bit @@ -74,7 +79,7 @@ def macro_call(macro_name, args, kwargs): def process( - args, kwargs, expand_macros=False + args, kwargs: MutableMapping[str, Any], expand_macros=False ): # --> connection string with keyword arguments processed. """attempts to inject arguments into a connection string using Python "%" operator for strings @@ -90,9 +95,7 @@ def process( dsn, dict ): # as a convenience the first argument may be django settings kwargs.update(dsn) - elif ( - dsn - ): # the connection string is passed to the connection as part of the keyword dictionary + elif dsn: # the connection string is passed to the connection as part of the keyword dictionary kwargs["connection_string"] = dsn try: a1 = args[1] @@ -124,7 +127,8 @@ def process( except KeyError: raise TypeError("Must define 'connection_string' for ado connections") if expand_macros: - for kwarg in kwargs: + # copy the list to avoid size changing during iteration + for kwarg in list(kwargs): if kwarg.startswith("macro_"): # If a key defines a macro macro_name = kwarg[6:] # name without the "macro_" macro_code = kwargs.pop( From 5ca980eca028607d7bbca90d1a74f2c0916cd2e4 Mon Sep 17 00:00:00 2001 From: Avasam Date: Fri, 18 Oct 2024 14:01:58 -0400 Subject: [PATCH 09/10] Update CHANGES.txt --- CHANGES.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES.txt b/CHANGES.txt index ebed84aab..907d5a0de 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -24,7 +24,7 @@ Coming in build 309, as yet unreleased Import `DispatcherWin32trace` and `DispatcherTrace` from `win32com.server.dispatcher` instead. * Fixed `win32timezone.TimeZoneInfo` initialization from a `[DYNAMIC_]TIME_ZONE_INFORMATION` (#2339, @Avasam) * Added runtime deprecation warning of `win2kras`, use `win32ras` instead (#2356, @Avasam) -* Improved handling of dict iterations and fallbacks (removes Python 2 support code, small general speed improvement) (#2332, #2330, #2332, @Avasam) +* Improved handling of dict iterations and fallbacks (removes Python 2 support code, small general speed improvement) (#2330, #2331, #2332, @Avasam) * Fixed "Open GL Demo" (`Pythonwin/pywin/Demos/openGLDemo.py`) and restore "Font" demo in `Pythonwin/pywin/Demos/guidemo.py` (#2345, @Avasam) * Fixed accidentally trying to raise an undefined name instead of an `Exception` in `Pythonwin/pywin/debugger/debugger.py` (#2326, @Avasam) From 8c11ebc48fa28dfcdc9b05bd4dfdd6a19add6950 Mon Sep 17 00:00:00 2001 From: Avasam Date: Fri, 18 Oct 2024 15:07:30 -0400 Subject: [PATCH 10/10] Move back 2 comments --- adodbapi/process_connect_string.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/adodbapi/process_connect_string.py b/adodbapi/process_connect_string.py index 6db077ec0..42795d297 100644 --- a/adodbapi/process_connect_string.py +++ b/adodbapi/process_connect_string.py @@ -91,11 +91,11 @@ def process( dsn = args[0] except IndexError: dsn = None - if isinstance( - dsn, dict - ): # as a convenience the first argument may be django settings + # as a convenience the first argument may be django settings + if isinstance(dsn, dict): kwargs.update(dsn) - elif dsn: # the connection string is passed to the connection as part of the keyword dictionary + # the connection string is passed to the connection as part of the keyword dictionary + elif dsn: kwargs["connection_string"] = dsn try: a1 = args[1]