diff --git a/README b/README new file mode 100644 index 0000000..31b3d83 --- /dev/null +++ b/README @@ -0,0 +1,38 @@ +=================== +Installing PyHesiod +=================== + +To install PyHesiod, you will first need to install Cython_. It's +always a good idea to install Cython through your package manager, if +possible. If your package manager doesn't have a package for Cython, +or if you wish to install Cython by hand anyway, you can do so by +running:: + + $ easy_install Cython + +Once you've done that, to install PyHesiod globally, run:: + + $ python setup.py install + +If you want to build PyHesiod without installing it globally, you may +want to run:: + + $ python setup.py build_ext --inplace + +which will build the C extensions in place next to their source, +allowing you to import the various modules, so long as your current +working directory is the root of the PyHesiod source tree. + +Alternatively, PyHesiod has been packaged for Debian and Ubuntu. To +build the Debian package of the latest release, run:: + + $ git checkout debian + $ git buildpackage + $ sudo debi + +You will need the devscripts and git-buildpackage packages installed, +as well as this package's build dependencies (cdbs, debhelper, +python-all-dev, python-support, python-pyrex, python-setuptools, +libhesiod-dev). + +.. _Cython: http://www.cython.org/ diff --git a/_hesiod.pyx b/_hesiod.pyx index f0b41d9..62e36fd 100644 --- a/_hesiod.pyx +++ b/_hesiod.pyx @@ -41,10 +41,17 @@ def bind(hes_name, hes_type): """ cdef object py_result cdef char * c_result - - name_str, type_str = map(str, (hes_name, hes_type)) - - c_result = hesiod_to_bind(__context, name_str, type_str) + + # N.B. libhesiod actually uses the locale, but + # locale.getpreferredencoding is not threadsafe so we have to + # assume utf-8. + + if isinstance(hes_name, unicode): + hes_name = hes_name.encode('utf-8') + if isinstance(hes_type, unicode): + hes_type = hes_type.encode('utf-8') + + c_result = hesiod_to_bind(__context, hes_name, hes_type) if c_result is NULL: raise IOError(errno, strerror(errno)) py_result = c_result @@ -59,14 +66,17 @@ def resolve(hes_name, hes_type): cdef int i cdef object py_result py_result = list() - cdef char ** c_result - - name_str, type_str = map(str, (hes_name, hes_type)) + cdef int err = 0 + cdef char ** c_result = NULL + + if isinstance(hes_name, unicode): + hes_name = hes_name.encode('utf-8') + if isinstance(hes_type, unicode): + hes_type = hes_type.encode('utf-8') - __lookup_lock.acquire() - c_result = hesiod_resolve(__context, name_str, type_str) - err = errno - __lookup_lock.release() + with __lookup_lock: + c_result = hesiod_resolve(__context, hes_name, hes_type) + err = errno if c_result is NULL: raise IOError(err, strerror(err)) @@ -74,7 +84,7 @@ def resolve(hes_name, hes_type): while True: if c_result[i] is NULL: break - py_result.append(c_result[i]) + py_result.append(c_result[i].decode('utf-8', 'replace')) i = i + 1 hesiod_free_list(__context, c_result) diff --git a/hesiod.py b/hesiod.py index 0e4475a..d277150 100755 --- a/hesiod.py +++ b/hesiod.py @@ -110,7 +110,7 @@ def parseRecords(self): class UidLookup(PasswdLookup): def __init__(self, uid): - Lookup.__init__(self, uid, 'uid') + Lookup.__init__(self, b'%d' % (uid,), 'uid') class GroupLookup(Lookup): def __init__(self, group): @@ -130,7 +130,7 @@ def parseRecords(self): class GidLookup(GroupLookup): def __init__(self, gid): - Lookup.__init__(self, gid, 'gid') + Lookup.__init__(self, b'%d' % (gid,), 'gid') __all__ = ['bind', 'resolve', 'Lookup', 'FilsysLookup', 'PasswdLookup', 'UidLookup', diff --git a/setup.py b/setup.py index f15d7b9..570798b 100755 --- a/setup.py +++ b/setup.py @@ -2,11 +2,11 @@ from setuptools import setup from distutils.extension import Extension -from Pyrex.Distutils import build_ext +from Cython.Build import cythonize setup( name="PyHesiod", - version="0.2.12", + version="0.2.13", description="PyHesiod - Python bindings for the Heisod naming library", author="Evan Broder", author_email="broder@mit.edu", @@ -14,12 +14,10 @@ maintainer_email="debathena@mit.edu", url="http://ebroder.net/code/PyHesiod", license="MIT", - requires=['Pyrex'], py_modules=['hesiod'], - ext_modules=[ + ext_modules=cythonize([ Extension("_hesiod", ["_hesiod.pyx"], - libraries=["hesiod"]) - ], - cmdclass= {"build_ext": build_ext} + libraries=["hesiod"]), + ]), )