Skip to content

Commit fb1c680

Browse files
committed
Note UNICODE typecaster registration; #7
I didn't move this into __init__ because the psycopg2 docs say to do this "as soon as" it's imported.
1 parent c856bc6 commit fb1c680

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

postgres.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@
88
99
$ pip install postgres
1010
11+
We `test <https://travis-ci.org/gittip/postgres.py>`_ against Python 2.6, 2.7,
12+
3.2, and 3.3. We don't yet have a testing matrix for different versions of
13+
:py:mod:`psycopg2` or PostgreSQL.
14+
15+
Importing :py:mod:`postgres` under Python 2 will cause the registration of
16+
typecasters in psycopg2 to ensure that you get unicode instead of bytestrings
17+
for text data, according to `this advice`_.
18+
1119
1220
Tutorial
1321
--------
@@ -128,6 +136,7 @@
128136
.. _psycopg2: http://initd.org/psycopg/
129137
.. _GitHub: https://github.com/gittip/postgres
130138
.. _PyPI: https://pypi.python.org/pypi/postgres
139+
.. _this advice: http://initd.org/psycopg/docs/usage.html#unicode-handling
131140
.. _DB-API 2.0: http://www.python.org/dev/peps/pep-0249/
132141
.. _SQL injection: http://en.wikipedia.org/wiki/SQL_injection
133142
@@ -136,20 +145,20 @@
136145

137146
try: # Python 2
138147
import urlparse
139-
except ImportError: # Python 3
140-
import urllib.parse as urlparse
141148

142-
import psycopg2
149+
# "Note: In Python 2, if you want to uniformly receive all your database
150+
# input in Unicode, you can register the related typecasters globally as
151+
# soon as Psycopg is imported."
152+
# -- http://initd.org/psycopg/docs/usage.html#unicode-handling
143153

144-
# "Note: In Python 2, if you want to uniformly receive all your database input
145-
# in Unicode, you can register the related typecasters globally as soon as
146-
# Psycopg is imported."
147-
# -- http://initd.org/psycopg/docs/usage.html#unicode-handling
154+
import psycopg2.extensions
155+
psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
156+
psycopg2.extensions.register_type(psycopg2.extensions.UNICODEARRAY)
148157

149-
import psycopg2.extensions
150-
psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
151-
psycopg2.extensions.register_type(psycopg2.extensions.UNICODEARRAY)
158+
except ImportError: # Python 3
159+
import urllib.parse as urlparse
152160

161+
import psycopg2
153162
from psycopg2.extras import RealDictCursor
154163
from psycopg2.pool import ThreadedConnectionPool as ConnectionPool
155164

@@ -225,10 +234,6 @@ class Postgres(object):
225234
not our simple API (:py:meth:`~postgres.Postgres.run` /
226235
:py:meth:`~postgres.Postgres.one` / :py:meth:`~postgres.Postgres.rows`).
227236
228-
Features:
229-
230-
- Get back unicode instead of bytestrings.
231-
232237
>>> import postgres
233238
>>> db = postgres.Postgres("postgres://jrandom@localhost/test")
234239

0 commit comments

Comments
 (0)