SQW: string lengths are written in characters but the strings in UTF-8 bytes

オープン 初心者向け
#751 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
78/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
python
領域
data

調査の方向性

Issue では、SQW のシリアライズコードにある _ir._serialize_field、_write_single_struct、LowLevelSqw.write_char_array が挙げられています。まずこれらの実装と読み書きのヘルパーを読み、その後、Issue の再現手順と関連する SQW テストを実行してください。UTF-8 文字列が切り詰められずに往復し、次のフィールドの型タグが正しく読み取られれば完了です。

索引モデルが issue の本文から書いたものです。

説明

_ir._serialize_field declares an ir.String's length as len(field.value) (characters), while LowLevelSqw.write_chars writes value.encode("utf-8") (bytes). The reader reads that many bytes, so one non-ASCII character truncates the string and shifts every field after it.

import io
from scippneutron.io.sqw import _ir as ir
from scippneutron.io.sqw._bytes import Byteorder
from scippneutron.io.sqw._low_level_io import LowLevelSqw
from scippneutron.io.sqw._read_write import read_object_array, write_object_array

for text in ('malinger', 'målinger'):
    buf = io.BytesIO()
    w = LowLevelSqw(buf, path=None, byteorder=Byteorder.little)
    write_object_array(w, ir._serialize_field(ir.String(text)))
    w.write_u8(ir.TypeTag.f64.value)  # next field's type tag
    r = LowLevelSqw(io.BytesIO(buf.getvalue()), path=None, byteorder=Byteorder.little)
    print(repr(read_object_array(r).data[0].value), r.read_u8())
# 'malinger' 3
# 'målinge' 114   <- truncated; 114 is ord('r'), read as the next type tag

In a real file, a run file path containing e.g. å makes experiment_info unreadable (ValueError: 101 is not a valid TypeTag). _write_single_struct has the same pattern, so far harmless because field names are ASCII. LowLevelSqw.write_char_array already uses the byte length.

Suggestion: len(field.value.encode("utf-8")) in both places.

Checked on scipp 26.11.0, scippneutron 26.10.0, essreduce 26.11.1, essspectroscopy 26.11.3.

主要言語
Python
スター
6
フォーク
5
平均マージ
1日 22時間
マージ済み PR(30日)
6

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

scipp/scippneutron のほかの issue

scipp/scippneutron の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。