@@ -18,11 +18,6 @@ class PoolConnectionProxyMeta(type):
18
18
19
19
def __new__ (mcls , name , bases , dct , * , wrap = False ):
20
20
if wrap :
21
- def get_wrapper (meth ):
22
- def wrapper (self , * args , ** kwargs ):
23
- return self ._dispatch_method_call (meth , args , kwargs )
24
- return wrapper
25
-
26
21
for attrname in dir (connection .Connection ):
27
22
if attrname .startswith ('_' ) or attrname in dct :
28
23
continue
@@ -31,7 +26,7 @@ def wrapper(self, *args, **kwargs):
31
26
if not inspect .isfunction (meth ):
32
27
continue
33
28
34
- wrapper = get_wrapper ( meth )
29
+ wrapper = mcls . _wrap_connection_method ( attrname )
35
30
wrapper = functools .update_wrapper (wrapper , meth )
36
31
dct [attrname ] = wrapper
37
32
@@ -44,6 +39,21 @@ def __init__(cls, name, bases, dct, *, wrap=False):
44
39
# Needed for Python 3.5 to handle `wrap` class keyword argument.
45
40
super ().__init__ (name , bases , dct )
46
41
42
+ @staticmethod
43
+ def _wrap_connection_method (meth_name ):
44
+ def call_con_method (self , * args , ** kwargs ):
45
+ # This method will be owned by PoolConnectionProxy class.
46
+ if self ._con is None :
47
+ raise exceptions .InterfaceError (
48
+ 'cannot call Connection.{}(): '
49
+ 'connection has been released back to the pool' .format (
50
+ meth_name ))
51
+
52
+ meth = getattr (self ._con .__class__ , meth_name )
53
+ return meth (self ._con , * args , ** kwargs )
54
+
55
+ return call_con_method
56
+
47
57
48
58
class PoolConnectionProxy (connection ._ConnectionProxy ,
49
59
metaclass = PoolConnectionProxyMeta ,
@@ -57,6 +67,10 @@ def __init__(self, holder: 'PoolConnectionHolder',
57
67
self ._holder = holder
58
68
con ._set_proxy (self )
59
69
70
+ def __getattr__ (self , attr ):
71
+ # Proxy all unresolved attributes to the wrapped Connection object.
72
+ return getattr (self ._con , attr )
73
+
60
74
def _detach (self ):
61
75
if self ._con is None :
62
76
raise exceptions .InterfaceError (
@@ -65,15 +79,6 @@ def _detach(self):
65
79
con , self ._con = self ._con , None
66
80
con ._set_proxy (None )
67
81
68
- def _dispatch_method_call (self , meth , args , kwargs ):
69
- if self ._con is None :
70
- raise exceptions .InterfaceError (
71
- 'cannot call Connection.{}(): '
72
- 'connection has been released back to the pool' .format (
73
- meth .__name__ ))
74
-
75
- return meth (self ._con , * args , ** kwargs )
76
-
77
82
def __repr__ (self ):
78
83
if self ._con is None :
79
84
return '<{classname} [released] {id:#x}>' .format (
0 commit comments