Fix py3k compatibility

QubesOS/qubes-issues#853
This commit is contained in:
Marek Marczykowski-Górecki 2017-02-24 01:00:06 +01:00
parent 544870f1ad
commit 8f92250665
No known key found for this signature in database
GPG Key ID: 063938BA42CFA724
3 changed files with 10 additions and 8 deletions

View File

@ -73,6 +73,7 @@ class PropertyHolder(object):
# 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)
@ -87,7 +88,7 @@ class PropertyHolder(object):
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
@ -114,9 +115,10 @@ class PropertyHolder(object):
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)
@ -137,7 +139,7 @@ class PropertyHolder(object):
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):

View File

@ -44,10 +44,10 @@ class VMCollection(object):
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

View File

@ -26,7 +26,7 @@ class TC_00_VMCollection(qubesmgmt.tests.QubesTestCase):
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()