Skip to content

Commit

Permalink
adodbapi reduce _raiseConnectionError to a single argument
Browse files Browse the repository at this point in the history
  • Loading branch information
Avasam committed Aug 2, 2024
1 parent 377a3ef commit a08add8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 27 deletions.
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ Coming in build 307, as yet unreleased
* General speed and size improvements due to all the removed code. (#2046, #1986, #2050, #1950, #2085, #2087, #2051, #1990, #2106, #2127, #2124, #2126, #2177, #2218, #2202, #2205, #2217)

### adodbapi
* When `errorhandler` is called, the `errorvalue` argument is now always an `Exception` and the `errortype` is the `type` of that specific `errorvalue` (#2351, @Avasam)
* Remove references to outdated IronPython (#2049, @Avasam)
This removes the following public names:
* `adodbapi.adodbapi.onWin32`
Expand Down
56 changes: 29 additions & 27 deletions adodbapi/adodbapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,9 @@ def connect(self, kwargs, connection_maker=make_COM_connecter):
self.connection_string = (
kwargs["connection_string"] % kwargs
) # insert keyword arguments
except Exception as e:
except Exception:
self._raiseConnectionError(
KeyError, "Python string format error in connection string->"
KeyError("Python string format error in connection string->")
)
self.timeout = kwargs.get("timeout", 30)
self.mode = kwargs.get("mode", adc.adModeUnknown)
Expand All @@ -266,8 +266,7 @@ def connect(self, kwargs, connection_maker=make_COM_connecter):
self.connector.Open() # Open the ADO connection
except api.Error:
self._raiseConnectionError(
api.DatabaseError,
"ADO error trying to Open=%s" % self.connection_string,
api.DatabaseError(f"ADO error trying to Open={self.connection_string}")
)

try: # Stefan Fuchs; support WINCCOLEDBProvider
Expand Down Expand Up @@ -298,11 +297,11 @@ def connect(self, kwargs, connection_maker=make_COM_connecter):
if verbose:
print("adodbapi New connection at %X" % id(self))

def _raiseConnectionError(self, errorclass, errorvalue):
def _raiseConnectionError(self, errorvalue: BaseException):
eh = self.errorhandler
if eh is None:
eh = api.standardErrorHandler
eh(self, None, errorclass, errorvalue)
eh(self, None, type(errorvalue), errorvalue)

def _closeAdoConnection(self): # all v2.1 Rose
"""close the underlying ADO Connection object,
Expand Down Expand Up @@ -334,7 +333,7 @@ def close(self):
try:
self._closeAdoConnection() # v2.1 Rose
except Exception as e:
self._raiseConnectionError(sys.exc_info()[0], sys.exc_info()[1])
self._raiseConnectionError(e)

self.connector = None # v2.4.2.2 fix subtle timeout bug
# per M.Hammond: "I expect the benefits of uninitializing are probably fairly small,
Expand Down Expand Up @@ -364,7 +363,7 @@ def commit(self):
# If not, we will have to start a new transaction by this command:
self.transaction_level = self.connector.BeginTrans()
except Exception as e:
self._raiseConnectionError(api.ProgrammingError, e)
self._raiseConnectionError(api.ProgrammingError(e))

def _rollback(self):
"""In case a database does provide transactions this method causes the the database to roll back to
Expand Down Expand Up @@ -400,7 +399,7 @@ def _rollback(self):
): # if self.transaction_level == 0 or self.transaction_level is None:
self.transaction_level = self.connector.BeginTrans()
except Exception as e:
self._raiseConnectionError(api.ProgrammingError, e)
self._raiseConnectionError(api.ProgrammingError(e))

def __setattr__(self, name, value):
if name == "autocommit": # extension: allow user to turn autocommit on or off
Expand All @@ -414,8 +413,9 @@ def __setattr__(self, name, value):
elif name == "paramstyle":
if value not in api.accepted_paramstyles:
self._raiseConnectionError(
api.NotSupportedError,
f"paramstyle={value!r} not in:{api.accepted_paramstyles!r}",
api.NotSupportedError(
f"paramstyle={value!r} not in:{api.accepted_paramstyles!r}"
)
)
elif name == "variantConversions":
# make a new copy -- no changes in the default, please
Expand Down Expand Up @@ -584,15 +584,15 @@ def __enter__(self):
"Allow database cursors to be used with context managers."
return self

def __exit__(self, exc_type, exc_val, exc_tb):
def __exit__(self, exc_type, exc_val, exc_tb) -> None:
"Allow database cursors to be used with context managers."
self.close()

def _raiseCursorError(self, errorclass, errorvalue):
def _raiseCursorError(self, errorvalue: BaseException) -> None:
eh = self.errorhandler
if eh is None:
eh = api.standardErrorHandler
eh(self.connection, self, errorclass, errorvalue)
eh(self.connection, self, type(errorvalue), errorvalue)

def build_column_info(self, recordset):
self.converters = [] # convertion function for each column
Expand All @@ -619,7 +619,7 @@ def build_column_info(self, recordset):
) # conversion function for this column
except KeyError:
self._raiseCursorError(
api.InternalError, "Data column of Unknown ADO type=%s" % f.Type
api.InternalError(f"Data column of Unknown ADO type={f.Type}")
)
self.columnNames[f.Name.lower()] = i # columnNames lookup

Expand Down Expand Up @@ -717,7 +717,7 @@ def _new_command(self, command_type=adc.adCmdText):
self.messages = []

if self.connection is None:
self._raiseCursorError(api.InterfaceError, None)
self._raiseCursorError(api.InterfaceError())
return
try:
self.cmd = Dispatch("ADODB.Command")
Expand All @@ -728,8 +728,9 @@ def _new_command(self, command_type=adc.adCmdText):
self.cmd.Prepared = bool(self._ado_prepared)
except:
self._raiseCursorError(
api.DatabaseError,
f"Error creating new ADODB.Command object for {self.commandText!r}",
api.DatabaseError(
f"Error creating new ADODB.Command object for {self.commandText!r}"
),
)

def _execute_command(self):
Expand All @@ -752,7 +753,7 @@ def _execute_command(self):
format_parameters(self.cmd.Parameters, True),
)
klass = self.connection._suggest_error_class()
self._raiseCursorError(klass, _message)
self._raiseCursorError(klass(_message))
try:
self.rowcount = recordset.RecordCount
except:
Expand Down Expand Up @@ -883,7 +884,7 @@ def _buildADOparameterList(self, parameters, sproc=False):
parameters[pm_name],
)
self._raiseCursorError(
api.DataError, f"{_message}->{e.args!r}"
api.DataError(f"{_message}->{e.args!r}")
)
else: # regular sequence of parameters
for value in parameters:
Expand All @@ -903,7 +904,7 @@ def _buildADOparameterList(self, parameters, sproc=False):
value,
)
self._raiseCursorError(
api.DataError, f"{_message}->{e.args!r}"
api.DataError(f"{_message}->{e.args!r}")
)
i += 1
else: # -- build own parameter list
Expand All @@ -929,7 +930,7 @@ def _buildADOparameterList(self, parameters, sproc=False):
)
)
self._raiseCursorError(
api.DataError, f"{_message}->{e.args!r}"
api.DataError(f"{_message}->{e.args!r}")
)
else: # expecting the usual sequence of parameters
if sproc:
Expand Down Expand Up @@ -957,7 +958,7 @@ def _buildADOparameterList(self, parameters, sproc=False):
)
)
self._raiseCursorError(
api.DataError, f"{_message}->{e.args!r}"
api.DataError(f"{_message}->{e.args!r}")
)
i += 1
if self._ado_prepared == "setup":
Expand Down Expand Up @@ -1038,7 +1039,7 @@ def _fetch(self, limit=None):
"""
if self.connection is None or self.rs is None:
self._raiseCursorError(
api.FetchFailedError, "fetch() on closed connection or empty query set"
api.FetchFailedError("fetch() on closed connection or empty query set")
)
return

Expand Down Expand Up @@ -1119,15 +1120,16 @@ def nextset(self):
self.messages = []
if self.connection is None or self.rs is None:
self._raiseCursorError(
api.OperationalError,
("nextset() on closed connection or empty query set"),
api.OperationalError(
"nextset() on closed connection or empty query set"
),
)
return None

try: # [begin 2.1 ekelund]
rsTuple = self.rs.NextRecordset() #
except pywintypes.com_error as exc: # return appropriate error
self._raiseCursorError(api.NotSupportedError, exc.args) # [end 2.1 ekelund]
self._raiseCursorError(api.NotSupportedError(exc.args)) # [end 2.1 ekelund]
recordset = rsTuple[0]
if recordset is None:
return None
Expand Down

0 comments on commit a08add8

Please sign in to comment.