diff --git a/opcua/ua/uatypes.py b/opcua/ua/uatypes.py index b86caca6b..ee1a6e36b 100644 --- a/opcua/ua/uatypes.py +++ b/opcua/ua/uatypes.py @@ -821,10 +821,11 @@ def __str__(self): def to_binary(self): b = [] encoding = self.VariantType.value & 0b111111 - if type(self.Value) in (list, tuple): + if self.is_array or type(self.Value) in (list, tuple): + self.is_array = True + encoding = uabin.set_bit(encoding, 7) if self.Dimensions is not None: encoding = uabin.set_bit(encoding, 6) - encoding = uabin.set_bit(encoding, 7) b.append(uabin.Primitives.UInt8.pack(encoding)) b.append(uabin.pack_uatype_array(self.VariantType, flatten(self.Value))) if self.Dimensions is not None: @@ -842,8 +843,6 @@ def from_binary(data): encoding = ord(data.read(1)) int_type = encoding & 0b00111111 vtype = datatype_to_varianttype(int_type) - if vtype == VariantType.Null: - return Variant(None, vtype, encoding) if uabin.test_bit(encoding, 7): value = uabin.unpack_uatype_array(vtype, data) array = True @@ -886,7 +885,9 @@ def flatten_and_get_shape(mylist): def flatten(mylist): - if len(mylist) == 0: + if mylist is None: + return None + elif len(mylist) == 0: return mylist while isinstance(mylist[0], (list, tuple)): mylist = [item for sublist in mylist for item in sublist] diff --git a/tests/tests_unit.py b/tests/tests_unit.py index d1cd3966b..a6bbdd014 100755 --- a/tests/tests_unit.py +++ b/tests/tests_unit.py @@ -25,6 +25,19 @@ class TestUnit(unittest.TestCase): Simple unit test that do not need to setup a server or a client ''' + def test_variant_array_none(self): + v = ua.Variant(None, varianttype=ua.VariantType.Int32, is_array=True) + data = v.to_binary() + v2 = ua.Variant.from_binary(ua.utils.Buffer(data)) + self.assertEqual(v, v2) + self.assertTrue(v2.is_array) + + v = ua.Variant(None, varianttype=ua.VariantType.Null, is_array=True) + data = v.to_binary() + v2 = ua.Variant.from_binary(ua.utils.Buffer(data)) + self.assertEqual(v, v2) + self.assertTrue(v2.is_array) + def test_custom_structs(self): xmlpath = "tests/example.bsd" c = StructGenerator(xmlpath, "structures.py")