def extensionobject_from_binary(data): """ Convert binary-coded ExtensionObject to a Python object. Returns an object, or None if TypeId is zero """ typeid = nodeid_from_binary(data) encoding = ord(data.read(1)) body = None if encoding & (1 << 0): length = Primitives.Int32.unpack(data) if length < 1: body = Buffer(b"") else: body = data.copy(length) data.skip(length) if typeid.Identifier == 0: return ua.ExtensionObject() if typeid in ua.extension_objects_by_typeid: cls = ua.extension_objects_by_typeid[typeid] if body is None: raise UaError(f'parsing ExtensionObject {cls.__name__} without data') return from_binary(cls, body) e = ua.ExtensionObject() e.TypeId = typeid e.Encoding = encoding if body is not None: e.Body = body.read(len(body)) return e
def test_unknown_extension_object(): obj = ua.ExtensionObject() obj.Body = b'example of data in custom format' obj.TypeId = ua.NodeId.from_string('ns=3;i=42') data = ua.utils.Buffer(extensionobject_to_binary(obj)) obj2 = extensionobject_from_binary(data) assert type(obj2) == ua.ExtensionObject assert obj2.TypeId == obj.TypeId assert obj2.Body == b'example of data in custom format'
def test_empty_extension_object(): obj = ua.ExtensionObject() obj2 = extensionobject_from_binary( ua.utils.Buffer(extensionobject_to_binary(obj))) assert type(obj) == type(obj2) assert obj == obj2