Skip to content

Commit 1c3def2

Browse files
committed
Redo python 3 migration
1 parent d765eaa commit 1c3def2

File tree

6 files changed

+50
-22
lines changed

6 files changed

+50
-22
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ mrclient.h
55
*.pyc
66
build
77
dist
8+
9+
# Cython autogenerated
10+
*.egg-info

README

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22
Installing PyMoira
33
==================
44

5-
To install PyMoira, you will first need to install Pyrex_. It's always
5+
To install PyMoira, you will first need to install Cython. It's always
66
a good idea to install Pyrex through your package manager, if
7-
possible. Your system's Pyrex package may be named ``python-pyrex`` or
8-
``pyrex-py25``. If your package manager doesn't have a package for
7+
possible. Your system's Pyrex package may be named ``python3-Cython`` or
8+
``cython3``. If your package manager doesn't have a package for
99
Pyrex, or if you wish to install Pyrex by hand anyway, you can do so
1010
by running::
1111

12-
$ easy_install Pyrex
12+
$ pip install Cython
1313

1414
Once you've done that, to install PyMoira globally, run::
1515

16-
$ python setup.py install
16+
$ pip install .
1717

1818
If you want to build PyMoira without installing it globally, you may
1919
want to run::
@@ -27,6 +27,8 @@ working directory is the root of the PyMoira source tree.
2727
Alternatively, PyMoira has been packaged for Debian and Ubuntu. To
2828
build the Debian package of the latest release, run::
2929

30+
_(NOTE: deb generation is untested)_
31+
3032
$ git checkout debian
3133
$ git buildpackage
3234
$ sudo debi

_moira.pyx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class MoiraException(Exception):
3535
__connected = False
3636

3737
def _error(code):
38-
raise MoiraException, (code, error_message(code))
38+
raise MoiraException((code, error_message(code)))
3939

4040
def connect(server=''):
4141
"""
@@ -86,9 +86,9 @@ def auth(program, krb4=False):
8686
discouraged
8787
"""
8888
if krb4:
89-
status = mr_auth(program)
89+
status = mr_auth(program.encode())
9090
else:
91-
status = mr_krb5_auth(program)
91+
status = mr_krb5_auth(program.encode())
9292
if status != MR_SUCCESS:
9393
_error(status)
9494

moira.py

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@
2323
_et_cache = {}
2424

2525

26+
def _to_bytes(s):
27+
"""
28+
If given a string, converts it to bytes, otherwise returns it as is
29+
"""
30+
if isinstance(s, str):
31+
return s.encode()
32+
else:
33+
return s
34+
35+
2636
def _clear_caches():
2737
"""Clear query caches.
2838
@@ -34,7 +44,7 @@ def _clear_caches():
3444

3545

3646
def connect(server=''):
37-
_moira.connect(server)
47+
_moira.connect(_to_bytes(server))
3848
version(-1)
3949
connect.__doc__ = _moira.connect.__doc__
4050

@@ -74,8 +84,18 @@ def _list_query(handle, *args):
7484
This bypasses the tuple -> dict conversion done in moira.query()
7585
"""
7686
results = []
77-
_moira._query(handle, results.append, *args)
78-
return results
87+
88+
# Python 3 wants bytes
89+
args_converted = (_to_bytes(arg) for arg in args)
90+
91+
# Perform the query
92+
_moira._query(_to_bytes(handle), results.append, *args_converted)
93+
94+
# We get bytes back, convert back to string
95+
return [
96+
tuple(val.decode() for val in result)
97+
for result in results
98+
]
7999

80100

81101
def _parse_args(handle, args, kwargs):
@@ -99,7 +119,7 @@ def _parse_args(handle, args, kwargs):
99119
return tuple(kwargs.get(i, '*')
100120
for i in _arg_cache[handle])
101121
else:
102-
return args
122+
return tuple(_to_bytes(arg) for arg in args)
103123

104124

105125
def query(handle, *args, **kwargs):
@@ -116,7 +136,10 @@ def query(handle, *args, **kwargs):
116136
the function.
117137
"""
118138
if handle.startswith('_'):
119-
return _list_query(handle, *args)
139+
# TODO: why are we converting twice?
140+
# perhaps if we don't, we wouldn't need this extra function
141+
args_converted = (_to_bytes(arg) for arg in args)
142+
return _list_query(handle, *args_converted)
120143
else:
121144
fmt = kwargs.pop('fmt', dict)
122145

@@ -126,7 +149,7 @@ def query(handle, *args, **kwargs):
126149
results = []
127150

128151
for r in plain_results:
129-
results.append(fmt(zip(_return_cache[handle], r)))
152+
results.append(fmt(list(zip(_return_cache[handle], r))))
130153

131154
return results
132155

@@ -147,9 +170,9 @@ def access(handle, *args, **kwargs):
147170
args = _parse_args(handle, args, kwargs)
148171

149172
try:
150-
_moira._access(handle, *args)
173+
_moira._access(_to_bytes(handle), *args)
151174
return True
152-
except MoiraException, e:
175+
except MoiraException as e:
153176
if e.code != errors()['MR_PERM']:
154177
raise
155178
return False

qy

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ def main():
5353
moira.auth(options.program)
5454

5555
if args[0].startswith('_'):
56-
print '\n'.join(', '.join(x) for x in
57-
moira._list_query(*args))
56+
print('\n'.join(', '.join(x) for x in
57+
moira._list_query(*args)))
5858
else:
5959
results = moira.query(fmt=tuple, *args)
6060

@@ -63,11 +63,11 @@ def main():
6363
r = filter_fields(r, options.fields)
6464

6565
if options.single:
66-
print ', '.join(v for (k, v) in r)
66+
print(', '.join(v for (k, v) in r))
6767
else:
6868
for k, v in r:
69-
print '%-*s: %s' % (keylen, k, v)
70-
print
69+
print('%-*s: %s' % (keylen, k, v))
70+
print()
7171

7272
if __name__ == '__main__':
7373
main()

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from setuptools import setup
44
from distutils.extension import Extension
5-
from Pyrex.Distutils import build_ext
5+
from Cython.Distutils import build_ext
66

77
setup(
88
name="PyMoira",

0 commit comments

Comments
 (0)