Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix variable length strings as attributes #1130

Merged
merged 1 commit into from
Dec 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/attributes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,13 @@ end
function write_attribute(attr::Attribute, memtype::Datatype, str::AbstractString)
strbuf = Base.cconvert(Cstring, str)
GC.@preserve strbuf begin
buf = Base.unsafe_convert(Ptr{UInt8}, strbuf)
write_attribute(attr, memtype, buf)
if API.h5t_is_variable_str(memtype)
ptr = Base.unsafe_convert(Cstring, strbuf)
write_attribute(attr, memtype, Ref(ptr))
else
ptr = Base.unsafe_convert(Ptr{UInt8}, strbuf)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we test this side of the conditional? I suppose this is for a fixed length string?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, maybe we should remove this. Don't think we have user-facing API that uses fixed strings

write_attribute(attr, memtype, ptr)
end
end
end
function write_attribute(
Expand Down
10 changes: 10 additions & 0 deletions test/attributes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,13 @@ end
close(f)
end
end

@testset "variable length strings" begin
filename = tempname()
h5open(filename, "w") do f
# https://github.com/JuliaIO/HDF5.jl/issues/1129
attr = create_attribute(f, "attr-name", datatype(String), dataspace(String))
write_attribute(attr, datatype(String), "attr-value")
@test attrs(f)["attr-name"] == "attr-value"
end
end
Loading