8000 Allow choosing a Psycopg implementation, add support for psycopg2cffi. · wulczer/txpostgres@f019075 · GitHub
[go: up one dir, main page]

Skip to content

Commit f019075

Browse files
committed
Allow choosing a Psycopg implementation, add support for psycopg2cffi.
Centralize code that detects available Psycopg implementations. A particular implementation can now be forced using an environment variable. A new fallback option, psycopg2cffi, has been added. Fixes #30.
1 parent c50459c commit f019075

File tree

6 files changed

+88
-21
lines changed

6 files changed

+88
-21
lines changed

NEWS

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
What's new in txpostgres 1.3.0
2+
------------------------------
3+
4+
- try using psycopg2cffi before falling back to psycopg2-ctypes
5+
(while still prefering straingh psycopg2)
6+
- allow choosing the exact psycopg2 implementation using an
7+
environment variable
8+
19
What's new in txpostgres 1.2.0
210
------------------------------
311

doc/usage.rst

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,29 @@ Here's an example of using automatic reconnection in txpostgres:
9797

9898
You can run this snippet and then try restarting the database. Logging lines
9999
should appear, as the connection gets automatically recovered.
100+
101+
Choosing a Psycopg implementation
102+
---------------------------------
103+
104+
To use txpostgres, you will need a recent enough version of Psycopg_, namely
105+
2.2.0 or later. Since parts of Psycopg are written in C, it is not available
106+
on some Python implementations, like PyPy. When first imported, txpostgres
107+
will try to detect if an API-compatible implementation of Psycopg is available.
108+
109+
You can force a certain implementation to be used by exporing an environment
110+
variable `TXPOSTGRES_PSYCOPG_IMPL`. Recognized values are:
111+
112+
psycopg2
113+
Force using Psycopg_, do not try any fallbacks.
114+
115+
psycopg2cffi
116+
Use psycopg2cffi_, a psycopg2 implementation based on cffi, known to work on
117+
PyPy.
118+
119+
psycopg2ct
120+
Use psycopg2ct_, an older psycopg2 implementation using ctypes, also
121+
compatible with PyPy.
122+
123+
.. _Psycopg: http://initd.org/psycopg/
124+
.. _psycopg2cffi: https://github.com/chtd/psycopg2cffi
125+
.. _psycopg2ct: https://github.com/mvantellingen/psycopg2-ctypes

test/test_txpostgres.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
1-
import os
1+
from __future__ import absolute_import
22

3-
try:
4-
import psycopg2
5-
import psycopg2.extensions
6-
except ImportError:
7-
# try psycopg2-ctypes
8-
try:
9-
import psycopg2ct as psycopg2
10-
except ImportError:
11-
raise ImportError('no module named psycopg2 or psycopg2ct')
3+
import os
124

5+
from txpostgres.psycopg2_impl import psycopg2
136
from txpostgres import txpostgres, reconnection
147

158
from twisted.trial import unittest

txpostgres/psycopg2_impl.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"""
2+
Choose the implementation of psycopg2.
3+
"""
4+
import os
5+
6+
7+
def _try_psycopg2_impl():
8+
# check for explicit request to use a specific implementation
9+
_library = os.getenv("TXPOSTGRES_PSYCOPG_IMPL")
10+
11+
if _library == "psycopg2":
12+
import psycopg2
13+
return psycopg2
14+
15+
if _library == "psycopg2cffi":
16+
import psycopg2cffi as psycopg2
17+
return psycopg2
18+
19+
if _library == "psycopg2ct":
20+
import psycopg2ct as psycopg2
21+
return psycopg2
22+
23+
# try various implementations until one works
24+
try:
25+
import psycopg2
26+
return psycopg2
27+
except ImportError:
28+
pass
29+
30+
try:
31+
import psycopg2cffi as psycopg2
32+
return psycopg2
33+
except ImportError:
34+
pass
35+
36+
try:
37+
import psycopg2ct as psycopg2
38+
return psycopg2
39+
except ImportError:
40+
pass
41+
42+
raise ImportError('no module named psycopg2, psycopg2cffi or psycopg2ct')
43+
44+
45+
psycopg2 = _try_psycopg2_impl()
46+
47+
del _try_psycopg2_impl

txpostgres/reconnection.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33
"""
44
from __future__ import absolute_import
55

6-
import psycopg2
7-
86
from twisted.internet import defer
97
from twisted.python import log
108

119
from txpostgres import retrying
10+
from txpostgres.psycopg2_impl import psycopg2
1211
from txpostgres.txpostgres import RollbackFailed
1312

1413

txpostgres/txpostgres.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,15 @@
4040
.. _issue tracker: https://github.com/wulczer/txpostgres/issues
4141
.. _documentation: http://txpostgres.readthedocs.org/
4242
"""
43-
44-
try:
45-
import psycopg2
46-
except ImportError:
47-
# try psycopg2-ctypes
48-
try:
49-
import psycopg2ct as psycopg2
50-
except ImportError:
51-
raise ImportError('no module named psycopg2 or psycopg2ct')
43+
from __future__ import absolute_import
5244

5345
from zope.interface import implements
5446

5547
from twisted.internet import interfaces, main, defer, task
5648
from twisted.python import failure, log
5749

50+
from txpostgres.psycopg2_impl import psycopg2
51+
5852
try:
5953
psycopg2.extensions.POLL_OK
6054
except AttributeError:

0 commit comments

Comments
 (0)
0