Skip to content

Commit b32493d

Browse files
committed
marshal ascii/unicode
1 parent 6489ef1 commit b32493d

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

vm/src/stdlib/marshal.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ mod decl {
3636
List = b'[',
3737
Dict = b'{',
3838
Code = b'c',
39-
Str = b'u', // = TYPE_UNICODE
39+
Unicode = b'u',
4040
// Unknown = b'?',
4141
Set = b'<',
4242
FrozenSet = b'>',
43-
// Ascii = b'a',
43+
Ascii = b'a',
4444
// AsciiInterned = b'A',
4545
// SmallTuple = b')',
4646
// ShortAscii = b'z',
@@ -70,11 +70,11 @@ mod decl {
7070
b'[' => List,
7171
b'{' => Dict,
7272
b'c' => Code,
73-
b'u' => Str,
73+
b'u' => Unicode,
7474
// b'?' => Unknown,
7575
b'<' => Set,
7676
b'>' => FrozenSet,
77-
// b'a' => Ascii,
77+
b'a' => Ascii,
7878
// b'A' => AsciiInterned,
7979
// b')' => SmallTuple,
8080
// b'z' => ShortAscii,
@@ -137,7 +137,11 @@ mod decl {
137137
buf.extend(pyfloat.to_f64().to_le_bytes());
138138
}
139139
pystr @ PyStr => {
140-
buf.push(Type::Str as u8);
140+
buf.push(if pystr.is_ascii() {
141+
Type::Ascii
142+
} else {
143+
Type::Unicode
144+
} as u8);
141145
write_size(buf, pystr.as_str().len(), vm)?;
142146
buf.extend(pystr.as_str().as_bytes());
143147
}
@@ -288,7 +292,17 @@ mod decl {
288292
let number = f64::from_le_bytes(bytes.try_into().unwrap());
289293
(vm.ctx.new_float(number).into(), buf)
290294
}
291-
Type::Str => {
295+
Type::Ascii => {
296+
let (len, buf) = read_size(buf, vm)?;
297+
if buf.len() < len {
298+
return Err(too_short_error(vm));
299+
}
300+
let (bytes, buf) = buf.split_at(len);
301+
let s = String::from_utf8(bytes.to_vec())
302+
.map_err(|_| vm.new_value_error("invalid utf8 data".to_owned()))?;
303+
(s.to_pyobject(vm), buf)
304+
}
305+
Type::Unicode => {
292306
let (len, buf) = read_size(buf, vm)?;
293307
if buf.len() < len {
294308
return Err(too_short_error(vm));

0 commit comments

Comments
 (0)