fix(format): flag non-ASCII link names as UTF-8

The writer marked every link name ASCII, so a name such as "größe" was
stored as UTF-8 bytes under the ASCII character set (h5py reports cset 0
for it). Names that are not plain ASCII now carry the UTF-8 flag, as h5py
writes them; ASCII names are unchanged.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
This commit is contained in:
osobh
2026-09-26 08:34:04 -05:00
co-authored by Claude Opus 5.5
parent d102c06306
commit bd36fe883b
3 changed files with 32 additions and 1 deletions
+11 -1
View File
@@ -220,6 +220,16 @@ fn add_refcount(w: &mut ObjectHeaderWriter, refcount: u32) {
}
}
/// The character set a link name is written with: UTF-8 when it is not
/// plain ASCII, as h5py writes it.
fn name_charset(name: &str) -> CharacterSet {
if name.is_ascii() {
CharacterSet::Ascii
} else {
CharacterSet::Utf8
}
}
/// The Link message for `link`, whose group and dataset targets are at the
/// given addresses (indexed as in the writer tree).
fn link_message(link: &writer_tree::Link, group_addrs: &[u64], ds_addrs: &[u64]) -> LinkMessage {
@@ -242,7 +252,7 @@ fn link_message(link: &writer_tree::Link, group_addrs: &[u64], ds_addrs: &[u64])
name: link.name.clone(),
link_target,
creation_order: link.creation_order,
charset: CharacterSet::Ascii,
charset: name_charset(&link.name),
}
}
@@ -592,3 +592,21 @@ fn track_order_lists_members_in_creation_order() {
assert_eq!(out, r#"["aaa", ["soft", "aaa"], 21]"#);
h5dump_ok(&path);
}
#[test]
fn non_ascii_names_are_utf8() {
skip_if_no_python!();
let dir = tempfile::tempdir().unwrap();
let mut b = FileBuilder::new();
b.create_dataset("größe/wert").with_i32_data(&[1]);
let path = write(&dir, "utf8.h5", b);
let out = h5py(
&path,
"with h5py.File(path, 'r') as f:\n\
\x20 l = f.id.links.get_info('größe'.encode())\n\
\x20 print(json.dumps([list(f), list(f['größe']), l.cset], ensure_ascii=False))",
);
assert_eq!(out, r#"[["größe"], ["wert"], 1]"#);
let f = File::open(&path).unwrap();
assert_eq!(f.dataset("größe/wert").unwrap().read_i32().unwrap(), [1]);
}