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

Aperta Adatta ai principianti
#751 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
78/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python
Ambito
data

Direzione di ricerca

L’issue individua _ir._serialize_field, _write_single_struct e LowLevelSqw.write_char_array nel codice di serializzazione SQW. Leggi prima queste implementazioni e le funzioni di supporto per la lettura e la scrittura, poi esegui la riproduzione dell’issue e i test SQW pertinenti. Il lavoro è completato quando le stringhe UTF-8 vengono serializzate e deserializzate senza troncamento e il tag di tipo del campo successivo viene letto correttamente.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

_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.

Lingua principale
Python
Stelle
6
Fork
5
Merge medio
1g 22h
PR unite (30g)
6

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di scipp/scippneutron

Tutte le issue di scipp/scippneutron

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.