Skip to content

Commit 073007c

Browse files
committed
Fix struct tests
1 parent f0e7427 commit 073007c

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

Lib/test/test_struct.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -718,8 +718,6 @@ def test__struct_types_immutable(self):
718718
cls.x = 1
719719

720720

721-
# TODO: RUSTPYTHON
722-
@unittest.expectedFailure
723721
def test_issue35714(self):
724722
# Embedded null characters should not be allowed in format strings.
725723
for s in '\0', '2\0i', b'\0':
@@ -790,8 +788,6 @@ def __init__(self):
790788
my_struct = MyStruct()
791789
self.assertEqual(my_struct.pack(12345), b'\x30\x39')
792790

793-
# TODO: RUSTPYTHON
794-
@unittest.expectedFailure
795791
def test_repr(self):
796792
s = struct.Struct('=i2H')
797793
self.assertEqual(repr(s), f'Struct({s.format!r})')

stdlib/src/pystruct.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub(crate) mod _struct {
1616
function::{ArgBytesLike, ArgMemoryBuffer, PosArgs},
1717
match_class,
1818
protocol::PyIterReturn,
19-
types::{Constructor, IterNext, Iterable, SelfIter},
19+
types::{Constructor, IterNext, Iterable, Representable, SelfIter},
2020
};
2121
use crossbeam_utils::atomic::AtomicCell;
2222

@@ -251,7 +251,7 @@ pub(crate) mod _struct {
251251
}
252252
}
253253

254-
#[pyclass(with(Constructor))]
254+
#[pyclass(with(Constructor, Representable))]
255255
impl PyStruct {
256256
#[pygetset]
257257
fn format(&self) -> PyStrRef {
@@ -306,6 +306,13 @@ pub(crate) mod _struct {
306306
}
307307
}
308308

309+
impl Representable for PyStruct {
310+
#[inline]
311+
fn repr_str(zelf: &Py<Self>, _vm: &VirtualMachine) -> PyResult<String> {
312+
Ok(format!("Struct('{}')", zelf.format.as_str()))
313+
}
314+
}
315+
309316
// seems weird that this is part of the "public" API, but whatever
310317
// TODO: implement a format code->spec cache like CPython does?
311318
#[pyfunction]

vm/src/buffer.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,12 @@ impl FormatCode {
242242
let c = chars
243243
.next()
244244
.ok_or_else(|| "repeat count given without format specifier".to_owned())?;
245+
246+
// Check for embedded null character
247+
if c == 0 {
248+
return Err("embedded null character".to_owned());
249+
}
250+
245251
let code = FormatType::try_from(c)
246252
.ok()
247253
.filter(|c| match c {

0 commit comments

Comments
 (0)