Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adodbapi: Simplify dict iterations from Python 2 to 3 migration #2332

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Avasam marked this conversation as resolved.
Show resolved Hide resolved
* 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)

Expand Down
9 changes: 5 additions & 4 deletions adodbapi/adodbapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -326,9 +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 list(self.cursors.values())[
:
]: # copy the list, then close each one
# 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:
Expand Down
2 changes: 1 addition & 1 deletion adodbapi/apibase.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.values())
s = "<SQLrow={"
for name, i in taglist:
s += f"{name}:{self._getValue(i)!r}, "
Expand Down
16 changes: 10 additions & 6 deletions adodbapi/process_connect_string.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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

Expand All @@ -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]
Expand Down Expand Up @@ -124,7 +127,8 @@ def process(
except KeyError:
raise TypeError("Must define 'connection_string' for ado connections")
if expand_macros:
for kwarg in list(kwargs.keys()):
# 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(
Expand Down
Loading