Skip to content

Commit

Permalink
Fix issue
Browse files Browse the repository at this point in the history
* Fix issue

* Make string encoding explicit and attributes
  • Loading branch information
cbourjau authored Jan 13, 2023
1 parent 1121c6f commit 7bc594a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/spox/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
7 changes: 6 additions & 1 deletion tests/test_attr.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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")
4 changes: 4 additions & 0 deletions tests/test_value_propagation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 7bc594a

Please sign in to comment.