Commit b97b33a9 authored by Vincent Michel's avatar Vincent Michel

Handle bytes/string conversion

parent e03c7672
Pipeline #794 passed with stages
in 1 minute and 16 seconds
......@@ -7,11 +7,29 @@ import numpy
from .error import check_return_value, check_error
from ._cffi import handel, ffi
__all__ = ['init', 'init_handel', 'exit',
'new_detector', 'get_num_detectors', 'get_detectors',
'start_run', 'stop_run', 'get_run_data_length', 'get_run_data',
'load_system', 'save_system', 'start_system',
'enable_log_output', 'disable_log_output',
'set_log_output', 'set_log_level', 'close_log',
'set_acquisition_value', 'get_acquisition_value',
'get_handel_version']
# Helpers
def to_bytes(arg):
if isinstance(arg, bytes):
return arg
return arg.encode()
# Initializing handel
@check_return_value
def init(filename):
filename = to_bytes(filename)
return handel.xiaInit(filename)
......@@ -29,6 +47,7 @@ def exit():
@check_return_value
def new_detector(alias):
alias = to_bytes(alias)
return handel.xiaNewDetector(alias)
......@@ -42,7 +61,7 @@ def get_detectors():
n = get_num_detectors()
arg = [ffi.new('char []', 80) for _ in range(n)]
check_error(handel.xiaGetDetectors(arg))
return tuple(ffi.string(x) for x in arg)
return tuple(ffi.string(x).decode() for x in arg)
# int xiaAddDetectorItem(char *alias, char *name, void *value);
......@@ -88,12 +107,14 @@ def get_run_data(channel):
@check_return_value
def load_system(filename):
# Is this an alias to xiaInit?
return handel.xiaLoadSystem('handel_ini', filename)
filename = to_bytes(filename)
return handel.xiaLoadSystem(b'handel_ini', filename)
@check_return_value
def save_system(filename):
return handel.xiaSaveSystem('handel_ini', filename)
filename = to_bytes(filename)
return handel.xiaSaveSystem(b'handel_ini', filename)
@check_return_value
......@@ -120,6 +141,7 @@ def set_log_level(level):
@check_return_value
def set_log_output(filename):
filename = to_bytes(filename)
return handel.xiaSetLogOutput(filename)
......@@ -165,11 +187,13 @@ def close_log():
# Parameters
def set_acquisition_value(channel, name, value):
name = to_bytes(name)
pointer = ffi.new('double *', value)
check_error(handel.xiaSetAcquisitionValues(channel, name, pointer))
def get_acquisition_value(channel, name):
name = to_bytes(name)
pointer = ffi.new('double *')
check_error(handel.xiaGetAcquisitionValues(channel, name, pointer))
return pointer[0]
......@@ -202,7 +226,7 @@ def get_acquisition_value(channel, name):
# Debugging
def get_version_info():
def get_handel_version():
rel = ffi.new('int *')
min = ffi.new('int *')
maj = ffi.new('int *')
......
......@@ -17,7 +17,11 @@ def test_init(interface):
m = interface.handel.xiaInit
m.return_value = 0
assert interface.init('somefile') is None
m.assert_called_once_with('somefile')
m.assert_called_once_with(b'somefile')
m.reset_mock()
# Should work with bytes too
assert interface.init(b'somefile') is None
m.assert_called_once_with(b'somefile')
def test_init_handel(interface):
......@@ -40,7 +44,7 @@ def test_new_detector(interface):
m = interface.handel.xiaNewDetector
m.return_value = 0
assert interface.new_detector('somealias') is None
m.assert_called_once_with('somealias')
m.assert_called_once_with(b'somealias')
def test_get_num_detectors(interface):
......@@ -73,7 +77,7 @@ def test_get_detectors(interface):
m1.side_effect = side_effect_1
m2.side_effect = side_effect_2
expected = b'name1', b'name2', b'name3'
expected = 'name1', 'name2', 'name3'
assert interface.get_detectors() == expected
m2.assert_called_once()
arg = m2.call_args[0][0]
......@@ -144,14 +148,14 @@ def test_load_system(interface):
m = interface.handel.xiaLoadSystem
m.return_value = 0
assert interface.load_system('somefile') is None
m.assert_called_once_with('handel_ini', 'somefile')
m.assert_called_once_with(b'handel_ini', b'somefile')
def test_save_system(interface):
m = interface.handel.xiaSaveSystem
m.return_value = 0
assert interface.save_system('somefile') is None
m.assert_called_once_with('handel_ini', 'somefile')
m.assert_called_once_with(b'handel_ini', b'somefile')
def test_start_system(interface):
......@@ -188,7 +192,7 @@ def test_set_log_output(interface):
m = interface.handel.xiaSetLogOutput
m.return_value = 0
assert interface.set_log_output('somefile') is None
m.assert_called_once_with('somefile')
m.assert_called_once_with(b'somefile')
def test_close_log(interface):
......@@ -205,7 +209,7 @@ def test_set_acquisition_value(interface):
m.return_value = 0
assert interface.set_acquisition_value(1, 'test', 2.3) is None
arg = m.call_args[0][2]
m.assert_called_once_with(1, 'test', arg)
m.assert_called_once_with(1, b'test', arg)
assert arg[0] == 2.3
......@@ -219,17 +223,17 @@ def test_get_acquistion_value(interface):
m.side_effect = side_effect
assert interface.get_acquisition_value(1, 'test') == 2.3
arg = m.call_args[0][2]
m.assert_called_once_with(1, 'test', arg)
m.assert_called_once_with(1, b'test', arg)
# Debugging
def test_get_version_info(interface):
def test_get_handel_version(interface):
m = interface.handel.xiaGetVersionInfo
def side_effect(a, b, c, d):
d[0], c[0], b[0], a[0] = b'v', 1, 2, 3
m.side_effect = side_effect
assert interface.get_version_info() == (1, 2, 3)
assert interface.get_handel_version() == (1, 2, 3)
m.assert_called_once()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment