[Python-porting] A few questions about the psycopg2 porting to Python 3

Daniele Varrazzo daniele.varrazzo at gmail.com
Tue Jan 11 19:41:17 CET 2011


Hello,

I've finished the porting of psycopg2 to Python 3. The porting is
based on the Martin v. Löwis patch (thank you *very* much) back in
2008; unfortunately at that time it wasn't promptly merged and the
code base diverged too much: the library has grown a lot of features
since then (and luckily a big fat test suite). Martin's "abstraction
layer" of macros in python.h has been the base for the new porting
anyway. The code is in available in the python3 branch of my psycopg
repos <https://www.develer.com/gitweb/pub?p=users/piro/psycopg2.git;a=shortlog;h=refs/heads/python3>.

Something I've done in a different way is the "adapters" area: Martin
did much of the processing in str, but there is a critical function
provided by the libpq, PQEscapeString
<http://www.postgresql.org/docs/9.0/static/libpq-exec.html#LIBPQ-PQESCAPESTRINGCONN>,
defined char* -> char*, that we use on strings before passing to the
backend: Py3 strings and Py2 unicode must be converted to bytes (in
the connection encoding) to use it: I feel awkward to go back to
unicode after passing through that function and to go back to bytes
again when pushing data to the socket, so I've used mostly bytes in
the Python->Postgres adaptation code path. OTOH doing my way we need
eventually a "format % args" with bytes in both format and args:
because this function is not provided by the Python API, I made my own
"PyBytes_Format" converting the PyString_Format from the Python 2.7
source code. I understand there is no problem in using parts of the
Python (2.7) source code into an LGPL-licensed work: is this right?
The resulting file is in <http://tinyurl.com/4w5oogn> and includes the
Python license as well: I'd like an opinion about whether the result
is "legal" and/or if the license had to be specified some other way.

As emerged from the discussion in this ML back in 2008, there is
somewhere the need for a python function b('literal') that would
evaluate to 'literal' in Py2 and to b'literal' in py3 (we want to keep
compatibility with Python 2.4). Currently there is an encode()
involved in Py3, so it would be great to have the transformation
b('literal') -> b'literal' performed by 2to3 instead. Looking at the
other fixers it seems easy, but I haven't found how to register a
custom fixer for the use of build_py_2to3 in setup.py. Is there any
reference?

There may be a few other points still open: they are more specifically
psycopg-related as they influence how 3rd party adapters should be
written, so they will be probably discussed in the psycopg mailing
list before releasing what is currently in the python3 branch. If you
want to join you are welcome.

Thank you very much.


-- Daniele


More information about the Python-porting mailing list