From 7bc594ad36fcc131691038b325a8a3ed9c4a01f2 Mon Sep 17 00:00:00 2001 From: Christian Bourjau Date: Fri, 13 Jan 2023 10:57:38 +0100 Subject: [PATCH] Fix issue * Fix issue * Make string encoding explicit and attributes --- src/spox/_utils.py | 4 +++- tests/test_attr.py | 7 ++++++- tests/test_value_propagation.py | 4 ++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/spox/_utils.py b/src/spox/_utils.py index 268d56f9..ab4ccfb4 100644 --- a/src/spox/_utils.py +++ b/src/spox/_utils.py @@ -56,6 +56,8 @@ def from_array(arr: np.ndarray, name: Optional[str] = None) -> TensorProto: data_type=dtype_to_tensor_type(arr.dtype), dims=arr.shape, # make_tensor fails on scalars. We fix it by calling flatten - vals=(arr.astype(bytes) if cast_to_bytes else arr).flatten(), + vals=( + np.char.encode(arr, encoding="utf-8") if cast_to_bytes else arr + ).flatten(), raw=False, ) diff --git a/tests/test_attr.py b/tests/test_attr.py index 2491a1ef..65672aac 100644 --- a/tests/test_attr.py +++ b/tests/test_attr.py @@ -1,7 +1,7 @@ import numpy as np import pytest -from spox._attributes import AttrTensor +from spox._attributes import AttrString, AttrStrings, AttrTensor def test_bad_attribute_type_cast_fails(op): @@ -35,3 +35,8 @@ def test_tensor_does_not_use_raw_data(vals, field, expected): assert pb.t.raw_data == b"" assert getattr(pb.t, field) == expected assert pb.t.dims == list(np.array(vals).shape) + + +def test_non_ascii_string_attr(): + AttrString("🐍")._to_onnx(key="foo") + AttrStrings(["🐍"])._to_onnx(key="foo") diff --git a/tests/test_value_propagation.py b/tests/test_value_propagation.py index 3072b87e..49cd1e92 100644 --- a/tests/test_value_propagation.py +++ b/tests/test_value_propagation.py @@ -142,3 +142,7 @@ def test_give_up_silently(op): values_int64s=[42], default_int64=-1, ) + + +def test_non_ascii_characters_in_string_tensor(op): + op.cast(op.constant(value_string="FööBär"), to=str)