-
Notifications
You must be signed in to change notification settings - Fork 3
Migrate to Python 3 #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
1c3def2
eb26363
c77660b
5be7941
d7e22f5
1ae5fb6
406467f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,6 @@ mrclient.h | |
*.pyc | ||
build | ||
dist | ||
|
||
# Cython autogenerated | ||
*.egg-info |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,15 @@ | |
_et_cache = {} | ||
|
||
|
||
def _to_bytes(s): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not completely sure how necessary this function is, but doing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like |
||
""" | ||
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 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please test it?