[python-ldap] Python3 compatibility

Raphaël Barrois raphael.barrois at m4x.org
Tue Mar 25 00:43:33 CET 2014


I've been working on a Python3 patch for the python-ldap library, with the goal
of having a single codebase for both Py2 and Py3 families.

The patch applies cleanly on master, and the whole test suite passes (except a
test that was already broken).

Obviously, the biggest issue is on the "bytes vs. unicode" side; I had to make
a few choices:

* Query results (from search_ext et al.) return a list of (dn, fields) tuples,
  - dn is bytes (UTF-8 encoded) in both Py2 (str) and Py3 (bytes)
  - fields is a dict mapping a field (as text, unicode in Py2 and str in Py3)
to a list of values, as bytes in both versions (this can contain images, etc.)

* Query filters is expected:
  - As text (unicode) in Py3
  - As bytes or text in Py2, with text being "in the default encoding"

* When sending data, the value of attributes *must* be bytes in both Py2 (str)
and Py3 (bytes)

This means that the same Python code should be able to run identically on Py2
and Py3 with the same version of Python-ldap, provided the "default encoding"
is UTF-8.

We've been running both forms of this patch in production for a couple of
months without any issue.

The source of the patch is available on my Github fork, split in several
commits for readability: https://github.com/rbarrois/python-ldap/compare/py3

Could this be merged into the master?

Note: I know that a "python3-ldap" lib exists, but it isn't as integrated as
python-ldap in most libraries, and lacks a few useful features (SASL
connection, paged queries, etc.).


-------------- next part --------------
A non-text attachment was scrubbed...
Name: py3.patch
Type: text/x-patch
Size: 106877 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-ldap/attachments/20140325/fb14fcc6/attachment-0001.bin>

More information about the python-ldap mailing list