Skip to content

Commit bff3c78

Browse files
author
Emanuele Palazzetti
authored
Merge pull request #393 from stj/master
psycopg2: Patch all imports of register_type
2 parents 319e531 + 3a19af7 commit bff3c78

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

ddtrace/contrib/psycopg/patch.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,40 @@ def _unroll_args(obj, scope=None):
9494
return func(obj, scope) if scope else func(obj)
9595

9696

97+
def _extensions_adapt(func, _, args, kwargs):
98+
adapt = func(*args, **kwargs)
99+
if hasattr(adapt, 'prepare'):
100+
return AdapterWrapper(adapt)
101+
return adapt
102+
103+
104+
class AdapterWrapper(wrapt.ObjectProxy):
105+
def prepare(self, *args, **kwargs):
106+
func = self.__wrapped__.prepare
107+
if not args:
108+
return func(*args, **kwargs)
109+
conn = args[0]
110+
111+
# prepare performs a c-level check of the object type so
112+
# we must be sure to pass in the actual db connection
113+
if isinstance(conn, wrapt.ObjectProxy):
114+
conn = conn.__wrapped__
115+
116+
return func(conn, *args[1:], **kwargs)
117+
118+
97119
# extension hooks
98120
_psycopg2_extensions = [
99121
(psycopg2.extensions.register_type,
100122
psycopg2.extensions, 'register_type',
101123
_extensions_register_type),
124+
(psycopg2._psycopg.register_type,
125+
psycopg2._psycopg, 'register_type',
126+
_extensions_register_type),
127+
(psycopg2._json.register_type,
128+
psycopg2._json, 'register_type',
129+
_extensions_register_type),
130+
(psycopg2.extensions.adapt,
131+
psycopg2.extensions, 'adapt',
132+
_extensions_adapt),
102133
]

tests/contrib/psycopg/test_psycopg.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
# 3p
55
import psycopg2
6+
from psycopg2 import _psycopg
7+
from psycopg2 import extensions
68
from psycopg2 import extras
79
from nose.tools import eq_
810

@@ -115,6 +117,28 @@ def test_manual_wrap_extension_types(self):
115117
# TypeError: argument 2 must be a connection, cursor or None
116118
extras.register_uuid(conn_or_curs=conn)
117119

120+
# NOTE: this will crash if it doesn't work.
121+
# _ext.register_default_json(conn)
122+
# TypeError: argument 2 must be a connection, cursor or None
123+
extras.register_default_json(conn)
124+
125+
126+
def test_manual_wrap_extension_adapt(self):
127+
conn, _ = self._get_conn_and_tracer()
128+
# NOTE: this will crash if it doesn't work.
129+
# items = _ext.adapt([1, 2, 3])
130+
# items.prepare(conn)
131+
# TypeError: argument 2 must be a connection, cursor or None
132+
items = extensions.adapt([1, 2, 3])
133+
items.prepare(conn)
134+
135+
# NOTE: this will crash if it doesn't work.
136+
# binary = _ext.adapt(b'12345)
137+
# binary.prepare(conn)
138+
# TypeError: argument 2 must be a connection, cursor or None
139+
binary = extensions.adapt(b'12345')
140+
binary.prepare(conn)
141+
118142
def test_connect_factory(self):
119143
tracer = get_dummy_tracer()
120144

tox.ini

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ deps =
8989
aiohttp20: aiohttp>=2.0,<2.1
9090
aiohttp21: aiohttp>=2.1,<2.2
9191
aiohttp22: aiohttp>=2.2,<2.3
92+
aiohttp23: aiohttp>=2.3,<2.4
9293
tornado40: tornado>=4.0,<4.1
9394
tornado41: tornado>=4.1,<4.2
9495
tornado42: tornado>=4.2,<4.3
@@ -97,6 +98,7 @@ deps =
9798
futures: futures>=3.0,<3.1
9899
aiohttp_jinja012: aiohttp_jinja2>=0.12,<0.13
99100
aiohttp_jinja013: aiohttp_jinja2>=0.13,<0.14
101+
aiohttp_jinja014: aiohttp_jinja2>=0.14,<0.15
100102
blinker: blinker
101103
boto: boto
102104
boto: moto<1.0
@@ -193,8 +195,11 @@ deps =
193195
requests212: requests-mock>=1.3
194196
requests213: requests>=2.13,<2.14
195197
requests213: requests-mock>=1.3
198+
requests218: requests>=2.18,<2.18
199+
requests218: requests-mock>=1.4
196200
sqlalchemy10: sqlalchemy>=1.0,<1.1
197-
sqlalchemy11: sqlalchemy==1.1.0b3
201+
sqlalchemy11: sqlalchemy>=1.1,<1.2
202+
sqlalchemy12: sqlalchemy>=1.2,<1.3
198203
webtest: WebTest
199204

200205
# pass along test env variables

0 commit comments

Comments
 (0)