r47332 - Merge adbapi-txchecker-8304: Fix some low-hanging TwistedChecker errors in twisted.enterprise.adbapi.

hawkowl-TA+aISz0psMTMxyoc4vAAJOcrHinNvQL0E9HWUfgJXw@public.gmane.org Wed, 27 Apr 2016 20:21:01 -0600 (MDT)
Newsgroups gmane.comp.python.twisted.commits
Message-ID <[email protected]>
Author: hawkowl
Date: Wed Apr 27 20:20:55 2016
New Revision: 47332

Added:
   trunk/twisted/topfiles/8304.misc
Modified:
   trunk/twisted/enterprise/adbapi.py

Log:
Merge adbapi-txchecker-8304: Fix some low-hanging TwistedChecker errors in twisted.enterprise.adbapi.

Author: hawkowl
Reviewer: adiroiban
Fixes: #8304

Modified: trunk/twisted/enterprise/adbapi.py
==============================================================================
--- trunk/twisted/enterprise/adbapi.py	(original)
+++ trunk/twisted/enterprise/adbapi.py	Wed Apr 27 20:20:55 2016
@@ -3,7 +3,8 @@
 # See LICENSE for details.
 
 """
-An asynchronous mapping to U{DB-API 2.0<http://www.python.org/topics/database/DatabaseAPI-2.0.html>}.
+An asynchronous mapping to U{DB-API
+2.0<http://www.python.org/topics/database/DatabaseAPI-2.0.html>}.
 """
 
 import sys
@@ -25,7 +26,7 @@
     A wrapper for a DB-API connection instance.
 
     The wrapper passes almost everything to the wrapped connection and so has
-    the same API. However, the Connection knows about its pool and also
+    the same API. However, the L{Connection} knows about its pool and also
     handle reconnecting should when the real connection dies.
     """
 
@@ -34,6 +35,7 @@
         self._connection = None
         self.reconnect()
 
+
     def close(self):
         # The way adbapi works right now means that closing a connection is
         # a really bad thing  as it leaves a dead connection associated with
@@ -44,6 +46,7 @@
         # the request
         pass
 
+
     def rollback(self):
         if not self._pool.reconnect:
             self._connection.rollback()
@@ -66,22 +69,26 @@
 
         raise ConnectionLost()
 
+
     def reconnect(self):
         if self._connection is not None:
             self._pool.disconnect(self._connection)
         self._connection = self._pool.connect()
 
+
     def __getattr__(self, name):
         return getattr(self._connection, name)
 
 
+
 class Transaction:
-    """A lightweight wrapper for a DB-API 'cursor' object.
+    """
+    A lightweight wrapper for a DB-API 'cursor' object.
 
     Relays attribute access to the DB cursor. That is, you can call
-    execute(), fetchall(), etc., and they will be called on the
-    underlying DB-API cursor object. Attributes will also be
-    retrieved from there.
+    C{execute()}, C{fetchall()}, etc., and they will be called on the
+    underlying DB-API cursor object. Attributes will also be retrieved from
+    there.
     """
     _cursor = None
 
@@ -90,11 +97,13 @@
         self._connection = connection
         self.reopen()
 
+
     def close(self):
         _cursor = self._cursor
         self._cursor = None
         _cursor.close()
 
+
     def reopen(self):
         if self._cursor is not None:
             self.close()
@@ -114,14 +123,17 @@
         self.reconnect()
         self._cursor = self._connection.cursor()
 
+
     def reconnect(self):
         self._connection.reconnect()
         self._cursor = None
 
+
     def __getattr__(self, name):
         return getattr(self._cursor, name)
 
 
+
 class ConnectionPool:
     """
     Represent a pool of connections to a DB-API 2.0 compliant database.
@@ -133,9 +145,9 @@
         L{Transaction}.
     @type transactionFactory: any callable
 
-    @ivar shutdownID: C{None} or a handle on the shutdown event trigger
-        which will be used to stop the connection pool workers when the
-        reactor stops.
+    @ivar shutdownID: C{None} or a handle on the shutdown event trigger which
+        will be used to stop the connection pool workers when the reactor
+        stops.
 
     @ivar _reactor: The reactor which will be used to schedule startup and
         shutdown events.
@@ -144,15 +156,15 @@
 
     CP_ARGS = "min max name noisy openfun reconnect good_sql".split()
 
-    noisy = False # if true, generate informational log messages
-    min = 3 # minimum number of connections in pool
-    max = 5 # maximum number of connections in pool
+    noisy = False # If true, generate informational log messages
+    min = 3 # Minimum number of connections in pool
+    max = 5 # Maximum number of connections in pool
     name = None # Name to assign to thread pool for debugging
     openfun = None # A function to call on new connections
-    reconnect = False # reconnect when connections fail
-    good_sql = 'select 1' # a query which should always succeed
+    reconnect = False # Reconnect when connections fail
+    good_sql = 'select 1' # A query which should always succeed
 
-    running = False # true when the pool is operating
+    running = False # True when the pool is operating
     connectionFactory = Connection
     transactionFactory = Transaction
 
@@ -161,41 +173,40 @@
     shutdownID = None
 
     def __init__(self, dbapiName, *connargs, **connkw):
-        """Create a new ConnectionPool.
+        """
+        Create a new L{ConnectionPool}.
 
         Any positional or keyword arguments other than those documented here
         are passed to the DB-API object when connecting. Use these arguments to
         pass database names, usernames, passwords, etc.
 
         @param dbapiName: an import string to use to obtain a DB-API compatible
-                          module (e.g. 'pyPgSQL.PgSQL')
+            module (e.g. C{'pyPgSQL.PgSQL'})
 
         @param cp_min: the minimum number of connections in pool (default 3)
 
         @param cp_max: the maximum number of connections in pool (default 5)
 
         @param cp_noisy: generate informational log messages during operation
-                         (default False)
+            (default C{False})
 
-        @param cp_openfun: a callback invoked after every connect() on the
-                           underlying DB-API object. The callback is passed a
-                           new DB-API connection object.  This callback can
-                           setup per-connection state such as charset,
-                           timezone, etc.
+        @param cp_openfun: a callback invoked after every C{connect()} on the
+            underlying DB-API object. The callback is passed a new DB-API
+            connection object. This callback can setup per-connection state
+            such as charset, timezone, etc.
 
         @param cp_reconnect: detect connections which have failed and reconnect
-                             (default False). Failed connections may result in
-                             ConnectionLost exceptions, which indicate the
-                             query may need to be re-sent.
+            (default C{False}). Failed connections may result in
+            L{ConnectionLost} exceptions, which indicate the query may need to
+            be re-sent.
 
         @param cp_good_sql: an sql query which should always succeed and change
-                            no state (default 'select 1')
+            no state (default C{'select 1'})
 
         @param cp_reactor: use this reactor instead of the global reactor
             (added in Twisted 10.2).
         @type cp_reactor: L{IReactorCore} provider
         """
-
         self.dbapiName = dbapiName
         self.dbapi = reflect.namedModule(dbapiName)
 
@@ -214,17 +225,18 @@
         self.connkw = connkw
 
         for arg in self.CP_ARGS:
-            cp_arg = 'cp_%s' % arg
-            if cp_arg in connkw:
-                setattr(self, arg, connkw[cp_arg])
-                del connkw[cp_arg]
+            cpArg = 'cp_%s' % (arg,)
+            if cpArg in connkw:
+                setattr(self, arg, connkw[cpArg])
+                del connkw[cpArg]
 
         self.min = min(self.min, self.max)
         self.max = max(self.min, self.max)
 
-        self.connections = {}  # all connections, hashed on thread id
+        # All connections, hashed on thread id
+        self.connections = {}
 
-        # these are optional so import them here
+        # These are optional so import them here
         from twisted.python import threadpool
         import thread
 
@@ -257,21 +269,22 @@
         Execute a function with a database connection and return the result.
 
         @param func: A callable object of one argument which will be executed
-            in a thread with a connection from the pool.  It will be passed as
+            in a thread with a connection from the pool. It will be passed as
             its first argument a L{Connection} instance (whose interface is
             mostly identical to that of a connection object for your DB-API
-            module of choice), and its results will be returned as a Deferred.
-            If the method raises an exception the transaction will be rolled
-            back.  Otherwise, the transaction will be committed.  B{Note} that
-            this function is B{not} run in the main thread: it must be
-            threadsafe.
+            module of choice), and its results will be returned as a
+            L{Deferred}. If the method raises an exception the transaction will
+            be rolled back. Otherwise, the transaction will be committed.
+            B{Note} that this function is B{not} run in the main thread: it
+            must be threadsafe.
 
         @param *args: positional arguments to be passed to func
 
         @param **kw: keyword arguments to be passed to func
 
-        @return: a Deferred which will fire the return value of
-            C{func(Transaction(...), *args, **kw)}, or a Failure.
+        @return: a L{Deferred} which will fire the return value of
+            C{func(Transaction(...), *args, **kw)}, or a
+            L{twisted.python.failure.Failure}.
         """
         from twisted.internet import reactor
         return threads.deferToThreadPool(reactor, self.threadpool,
@@ -298,30 +311,29 @@
         """
         Interact with the database and return the result.
 
-        The 'interaction' is a callable object which will be executed
-        in a thread using a pooled connection. It will be passed an
-        L{Transaction} object as an argument (whose interface is
-        identical to that of the database cursor for your DB-API
-        module of choice), and its results will be returned as a
-        Deferred. If running the method raises an exception, the
-        transaction will be rolled back. If the method returns a
+        The 'interaction' is a callable object which will be executed in a
+        thread using a pooled connection. It will be passed an L{Transaction}
+        object as an argument (whose interface is identical to that of the
+        database cursor for your DB-API module of choice), and its results will
+        be returned as a L{Deferred}. If running the method raises an
+        exception, the transaction will be rolled back. If the method returns a
         value, the transaction will be committed.
 
-        NOTE that the function you pass is *not* run in the main
-        thread: you may have to worry about thread-safety in the
-        function you pass to this if it tries to use non-local
-        objects.
+        NOTE that the function you pass is *not* run in the main thread: you
+        may have to worry about thread-safety in the function you pass to this
+        if it tries to use non-local objects.
 
-        @param interaction: a callable object whose first argument
-            is an L{adbapi.Transaction}.
+        @param interaction: a callable object whose first argument is an
+            L{adbapi.Transaction}.
 
-        @param *args: additional positional arguments to be passed
-            to interaction
+        @param *args: additional positional arguments to be passed to
+            interaction
 
         @param **kw: keyword arguments to be passed to interaction
 
         @return: a Deferred which will fire the return value of
-            'interaction(Transaction(...), *args, **kw)', or a Failure.
+            C{interaction(Transaction(...), *args, **kw)}, or a
+            L{twisted.python.failure.Failure}.
         """
         from twisted.internet import reactor
         return threads.deferToThreadPool(reactor, self.threadpool,
@@ -330,40 +342,44 @@
 
 
     def runQuery(self, *args, **kw):
-        """Execute an SQL query and return the result.
+        """
+        Execute an SQL query and return the result.
 
-        A DB-API cursor will will be invoked with cursor.execute(*args, **kw).
-        The exact nature of the arguments will depend on the specific flavor
-        of DB-API being used, but the first argument in *args be an SQL
-        statement. The result of a subsequent cursor.fetchall() will be
-        fired to the Deferred which is returned. If either the 'execute' or
+        A DB-API cursor will will be invoked with C{cursor.execute(*args,
+        **kw)}. The exact nature of the arguments will depend on the specific
+        flavor of DB-API being used, but the first argument in C{*args} be an
+        SQL statement. The result of a subsequent C{cursor.fetchall()} will be
+        fired to the L{Deferred} which is returned. If either the 'execute' or
         'fetchall' methods raise an exception, the transaction will be rolled
-        back and a Failure returned.
+        back and a L{twisted.python.failure.Failure} returned.
 
-        The  *args and **kw arguments will be passed to the DB-API cursor's
-        'execute' method.
+        The C{*args} and C{**kw} arguments will be passed to the DB-API
+        cursor's 'execute' method.
 
-        @return: a Deferred which will fire the return value of a DB-API
-        cursor's 'fetchall' method, or a Failure.
+        @return: a L{Deferred} which will fire the return value of a DB-API
+            cursor's 'fetchall' method, or a L{twisted.python.failure.Failure}.
         """
         return self.runInteraction(self._runQuery, *args, **kw)
 
 
     def runOperation(self, *args, **kw):
-        """Execute an SQL query and return None.
+        """
+        Execute an SQL query and return C{None}.
 
-        A DB-API cursor will will be invoked with cursor.execute(*args, **kw).
-        The exact nature of the arguments will depend on the specific flavor
-        of DB-API being used, but the first argument in *args will be an SQL
-        statement. This method will not attempt to fetch any results from the
-        query and is thus suitable for INSERT, DELETE, and other SQL statements
-        which do not return values. If the 'execute' method raises an
-        exception, the transaction will be rolled back and a Failure returned.
+        A DB-API cursor will will be invoked with C{cursor.execute(*args,
+        **kw)}. The exact nature of the arguments will depend on the specific
+        flavor of DB-API being used, but the first argument in C{*args} will be
+        an SQL statement. This method will not attempt to fetch any results
+        from the query and is thus suitable for C{INSERT}, C{DELETE}, and other
+        SQL statements which do not return values. If the 'execute' method
+        raises an exception, the transaction will be rolled back and a
+        L{Failure} returned.
 
-        The args and kw arguments will be passed to the DB-API cursor's
+        The C{*args} and C{*kw} arguments will be passed to the DB-API cursor's
         'execute' method.
 
-        return: a Deferred which will fire None or a Failure.
+        @return: a L{Deferred} which will fire with C{None} or a
+            L{twisted.python.failure.Failure}.
         """
         return self.runInteraction(self._runOperation, *args, **kw)
 
@@ -380,9 +396,11 @@
             self.startID = None
         self.finalClose()
 
-    def finalClose(self):
-        """This should only be called by the shutdown trigger."""
 
+    def finalClose(self):
+        """
+        This should only be called by the shutdown trigger.
+        """
         self.shutdownID = None
         self.threadpool.stop()
         self.running = False
@@ -390,8 +408,10 @@
             self._close(conn)
         self.connections.clear()
 
+
     def connect(self):
-        """Return a database connection when one becomes available.
+        """
+        Return a database connection when one becomes available.
 
         This method blocks and should be run in a thread from the internal
         threadpool. Don't call this method directly from non-threaded code.
@@ -414,12 +434,14 @@
             self.connections[tid] = conn
         return conn
 
+
     def disconnect(self, conn):
-        """Disconnect a database connection associated with this pool.
+        """
+        Disconnect a database connection associated with this pool.
 
-        Note: This function should only be used by the same thread which
-        called connect(). As with connect(), this function is not used
-        in normal non-threaded twisted code.
+        Note: This function should only be used by the same thread which called
+        L{ConnectionPool.connect}. As with C{connect}, this function is not
+        used in normal non-threaded Twisted code.
         """
         tid = self.threadID()
         if conn is not self.connections.get(tid):
@@ -459,9 +481,11 @@
         trans.execute(*args, **kw)
         return trans.fetchall()
 
+
     def _runOperation(self, trans, *args, **kw):
         trans.execute(*args, **kw)
 
+
     def __getstate__(self):
         return {'dbapiName': self.dbapiName,
                 'min': self.min,
@@ -472,9 +496,11 @@
                 'connargs': self.connargs,
                 'connkw': self.connkw}
 
+
     def __setstate__(self, state):
         self.__dict__ = state
         self.__init__(self.dbapiName, *self.connargs, **self.connkw)
 
 
+
 __all__ = ['Transaction', 'ConnectionPool']