Skip to content

Commit

Permalink
Fix py3k compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
marmarek committed Mar 1, 2017
1 parent 544870f commit 8f92250
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
10 changes: 6 additions & 4 deletions qubesmgmt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def _parse_qubesd_response(response_data):
# drop last field because of terminating '\x00'
args = [arg.decode() for arg in args.split(b'\x00')[:-1]]
format_string = format_string.decode('utf-8')
exc_type = exc_type.decode('ascii')
exc_class = getattr(qubesmgmt.exc, exc_type, 'QubesException')
# TODO: handle traceback if given
raise exc_class(format_string, *args)
Expand All @@ -87,7 +88,7 @@ def property_list(self):
self._method_prefix + 'List',
None,
None)
self._properties = properties_str.splitlines()
self._properties = properties_str.decode('ascii').splitlines()
# TODO: make it somehow immutable
return self._properties

Expand All @@ -114,9 +115,10 @@ def __getattr__(self, item):
self._method_prefix + 'Get',
item,
None)
(_default, value) = property_str.split(' ', 1)
(_default, value) = property_str.split(b' ', 1)
value = value.decode()
if value[0] == '\'':
return ast.literal_eval('b' + value)
return ast.literal_eval('u' + value)
else:
return ast.literal_eval(value)

Expand All @@ -137,7 +139,7 @@ def __setattr__(self, key, value):
self._method_dest,
self._method_prefix + 'Set',
key,
bytes(value))
str(value).encode('utf-8'))

def __delattr__(self, name):
if name.startswith('_') or name in dir(self):
Expand Down
6 changes: 3 additions & 3 deletions qubesmgmt/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ def refresh_cache(self, force=False):
new_vm_list = {}
# FIXME: this will probably change
for vm_data in vm_list_data.splitlines():
vm_name, props = vm_data.split(b' ', 1)
props = props.split(b' ')
vm_name, props = vm_data.decode('ascii').split(' ', 1)
props = props.split(' ')
new_vm_list[vm_name] = dict(
[vm_prop.split(b'=', 1) for vm_prop in props])
[vm_prop.split('=', 1) for vm_prop in props])

self._vm_list = new_vm_list

Expand Down
2 changes: 1 addition & 1 deletion qubesmgmt/tests/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def test_000_list(self):
self.app.expected_calls[('dom0', 'mgmt.vm.List', None, None)] = \
b'0\x00test-vm class=AppVM state=running\n'
self.assertEqual(
self.app.domains.keys(),
list(self.app.domains.keys()),
['test-vm'])
self.assertAllCalled()

Expand Down

0 comments on commit 8f92250

Please sign in to comment.