summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSylvain <syt@logilab.fr>2008-03-14 15:50:11 +0100
committerSylvain <syt@logilab.fr>2008-03-14 15:50:11 +0100
commit82cd21d93cc316b672be1e5b30d659c0f697162e (patch)
treef04b08137a89dc89817a1b88b7cc5967f36a3ee3
parent2dedaaebb8bafeed354038ca7ea14c2e878224fd (diff)
downloadlogilab-common-82cd21d93cc316b672be1e5b30d659c0f697162e.tar.gz
deal with objects without __dict__
-rw-r--r--ChangeLog1
-rw-r--r--db.py29
2 files changed, 26 insertions, 4 deletions
diff --git a/ChangeLog b/ChangeLog
index 84d45cf..0b6b901 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,6 @@
ChangeLog for logilab.common
============================
+
--
* db: remember logged user on the connection
diff --git a/db.py b/db.py
index dfbbf8f..7e43836 100644
--- a/db.py
+++ b/db.py
@@ -94,8 +94,10 @@ def _import_driver_module(driver, drivers, imported_elements=None, quiet=True):
## Connection and cursor wrappers #############################################
-class PyConnection:
- """A simple connection wrapper in python (useful for profiling)"""
+class SimpleConnectionWrapper:
+ """A simple connection wrapper in python to decorated C-level connections
+ with additional attributes
+ """
def __init__(self, cnx):
"""Wraps the original connection object"""
self._cnx = cnx
@@ -103,7 +105,7 @@ class PyConnection:
# XXX : Would it work if only __getattr__ was defined
def cursor(self):
"""Wraps cursor()"""
- return PyCursor(self._cnx.cursor())
+ return self._cnx.cursor()
def commit(self):
"""Wraps commit()"""
@@ -120,6 +122,20 @@ class PyConnection:
def __getattr__(self, attrname):
return getattr(self._cnx, attrname)
+class PyConnection(SimpleConnectionWrapper):
+ """A simple connection wrapper in python, generating wrapper for cursors as
+ well (useful for profiling)
+ """
+ def __init__(self, cnx):
+ """Wraps the original connection object"""
+ self._cnx = cnx
+
+ def cursor(self):
+ """Wraps cursor()"""
+ return PyCursor(self._cnx.cursor())
+
+
+
class PyCursor:
"""A simple cursor wrapper in python (useful for profiling)"""
def __init__(self, cursor):
@@ -179,7 +195,12 @@ class DBAPIAdapter:
"""
if self._pywrap:
cnx = PyConnection(cnx)
- cnx.logged_user = user
+ try:
+ cnx.logged_user = user
+ except AttributeError:
+ # C or __slots__ object
+ cnx = SimpleConnectionWrapper(cnx)
+ cnx.logged_user = user
return cnx
def __getattr__(self, attrname):