diff --git a/.gitignore b/.gitignore index 0b12521..f59d22e 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,6 @@ mrclient.h *.pyc build dist + +# Cython autogenerated +*.egg-info \ No newline at end of file diff --git a/README b/README index 5fde85a..a01f4a9 100644 --- a/README +++ b/README @@ -2,18 +2,23 @@ Installing PyMoira ================== -To install PyMoira, you will first need to install Pyrex_. It's always +To install PyMoira, you will first need to install Cython. It's always a good idea to install Pyrex through your package manager, if -possible. Your system's Pyrex package may be named ``python-pyrex`` or -``pyrex-py25``. If your package manager doesn't have a package for +possible. Your system's Pyrex package may be named ``python3-Cython`` or +``cython3``. If your package manager doesn't have a package for Pyrex, or if you wish to install Pyrex by hand anyway, you can do so by running:: - $ easy_install Pyrex + $ pip install Cython + +Other dependencies you need is wheel and setuptools, which seem to +not be installed by default anymore. + + $ pip install --upgrade setuptools wheel Once you've done that, to install PyMoira globally, run:: - $ python setup.py install + $ pip install . If you want to build PyMoira without installing it globally, you may want to run:: @@ -27,6 +32,8 @@ working directory is the root of the PyMoira source tree. Alternatively, PyMoira has been packaged for Debian and Ubuntu. To build the Debian package of the latest release, run:: +_(NOTE: deb generation is untested)_ + $ git checkout debian $ git buildpackage $ sudo debi diff --git a/_moira.pyx b/_moira.pyx index 80af823..18a8eae 100644 --- a/_moira.pyx +++ b/_moira.pyx @@ -35,7 +35,7 @@ class MoiraException(Exception): __connected = False def _error(code): - raise MoiraException, (code, error_message(code)) + raise MoiraException(code, error_message(code)) def connect(server=''): """ @@ -86,9 +86,9 @@ def auth(program, krb4=False): discouraged """ if krb4: - status = mr_auth(program) + status = mr_auth(program.encode()) else: - status = mr_krb5_auth(program) + status = mr_krb5_auth(program.encode()) if status != MR_SUCCESS: _error(status) @@ -197,7 +197,7 @@ def version(ver): else: _error(status) -cdef int _call_python_callback(int argc, char ** argv, void * hint): +cdef int _call_python_callback(int argc, char ** argv, void * hint) noexcept: cdef object callback callback = hint result = [] diff --git a/moira.py b/moira.py index 7090a2d..c229dd0 100644 --- a/moira.py +++ b/moira.py @@ -23,6 +23,15 @@ _et_cache = {} +def _to_bytes(s): + """ + If given a string, converts it to bytes, otherwise returns it as is + """ + if isinstance(s, str): + return s.encode() + return s + + def _clear_caches(): """Clear query caches. @@ -34,7 +43,7 @@ def _clear_caches(): def connect(server=''): - _moira.connect(server) + _moira.connect(_to_bytes(server)) version(-1) connect.__doc__ = _moira.connect.__doc__ @@ -74,8 +83,18 @@ def _list_query(handle, *args): This bypasses the tuple -> dict conversion done in moira.query() """ results = [] - _moira._query(handle, results.append, *args) - return results + + # Python 3 wants bytes + args_converted = (_to_bytes(arg) for arg in args) + + # Perform the query + _moira._query(_to_bytes(handle), results.append, *args_converted) + + # We get bytes back, convert back to string + return [ + tuple(val.decode() for val in result) + for result in results + ] def _parse_args(handle, args, kwargs): @@ -99,7 +118,7 @@ def _parse_args(handle, args, kwargs): return tuple(kwargs.get(i, '*') for i in _arg_cache[handle]) else: - return args + return tuple(_to_bytes(arg) for arg in args) def query(handle, *args, **kwargs): @@ -126,7 +145,7 @@ def query(handle, *args, **kwargs): results = [] for r in plain_results: - results.append(fmt(zip(_return_cache[handle], r))) + results.append(fmt(list(zip(_return_cache[handle], r)))) return results @@ -147,9 +166,9 @@ def access(handle, *args, **kwargs): args = _parse_args(handle, args, kwargs) try: - _moira._access(handle, *args) + _moira._access(_to_bytes(handle), *args) return True - except MoiraException, e: + except MoiraException as e: if e.code != errors()['MR_PERM']: raise return False diff --git a/qy b/qy index baeccbb..63a7087 100755 --- a/qy +++ b/qy @@ -53,8 +53,8 @@ def main(): moira.auth(options.program) if args[0].startswith('_'): - print '\n'.join(', '.join(x) for x in - moira._list_query(*args)) + print('\n'.join(', '.join(x) for x in + moira._list_query(*args))) else: results = moira.query(fmt=tuple, *args) @@ -63,11 +63,11 @@ def main(): r = filter_fields(r, options.fields) if options.single: - print ', '.join(v for (k, v) in r) + print(', '.join(v for (k, v) in r)) else: for k, v in r: - print '%-*s: %s' % (keylen, k, v) - print + print('%-*s: %s' % (keylen, k, v)) + print() if __name__ == '__main__': main() diff --git a/setup.py b/setup.py index 4ee2946..d468e63 100755 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ from setuptools import setup from distutils.extension import Extension -from Pyrex.Distutils import build_ext +from Cython.Distutils import build_ext setup( name="PyMoira",