Skip to content

Commit 0cb1cf9

Browse files
committed
test
1 parent 21c8e69 commit 0cb1cf9

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

stdlib/src/binascii.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ pub(super) use decl::crc32;
66
mod decl {
77
use crate::vm::{
88
builtins::{PyIntRef, PyTypeRef},
9+
exceptions::SimpleException,
910
function::{ArgAsciiBuffer, ArgBytesLike, OptionalArg},
1011
PyResult, VirtualMachine,
1112
};
@@ -57,18 +58,24 @@ mod decl {
5758

5859
#[pyfunction(name = "a2b_hex")]
5960
#[pyfunction]
60-
fn unhexlify(data: ArgAsciiBuffer, vm: &VirtualMachine) -> PyResult<Vec<u8>> {
61+
fn unhexlify(data: ArgAsciiBuffer, vm: &VirtualMachine) -> Result<Vec<u8>, SimpleException> {
6162
data.with_ref(|hex_bytes| {
6263
if hex_bytes.len() % 2 != 0 {
63-
return Err(vm.new_value_error("Odd-length string".to_owned()));
64+
return Err(SimpleException::with_message(
65+
vm.ctx.exceptions.value_error,
66+
"Odd-length string",
67+
));
6468
}
6569

6670
let mut unhex = Vec::<u8>::with_capacity(hex_bytes.len() / 2);
6771
for (n1, n2) in hex_bytes.iter().tuples() {
6872
if let (Some(n1), Some(n2)) = (unhex_nibble(*n1), unhex_nibble(*n2)) {
6973
unhex.push(n1 << 4 | n2);
7074
} else {
71-
return Err(vm.new_value_error("Non-hexadecimal digit found".to_owned()));
75+
return Err(SimpleException::with_message(
76+
vm.ctx.exceptions.value_error,
77+
"Non-hexadecimal digit found",
78+
));
7279
}
7380
}
7481

vm/src/exceptions.rs

+46
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,52 @@ impl TryFromObject for ExceptionCtor {
280280
}
281281
}
282282

283+
pub struct SimpleException {
284+
typ: &'static Py<PyType>,
285+
message: Option<&'static str>,
286+
}
287+
288+
impl SimpleException {
289+
#[inline]
290+
pub fn new(typ: &'static Py<PyType>) -> Self {
291+
Self { typ, message: None }
292+
}
293+
#[inline]
294+
pub fn with_message(typ: &'static Py<PyType>, message: &'static str) -> Self {
295+
let message = Some(message);
296+
Self { typ, message }
297+
}
298+
}
299+
300+
impl ToPyException for SimpleException {
301+
#[inline]
302+
fn to_pyexception(self, vm: &VirtualMachine) -> PyBaseExceptionRef {
303+
let Self { typ, message } = self;
304+
match message {
305+
Some(message) => vm.new_exception_msg(typ.to_owned(), message.to_owned()),
306+
None => vm.new_exception_empty(typ.to_owned()),
307+
}
308+
}
309+
}
310+
311+
pub struct DeferredMessageException<'vm, M>
312+
where
313+
M: FnOnce(&VirtualMachine) -> String,
314+
{
315+
typ: &'vm PyTypeRef,
316+
build_message: M,
317+
}
318+
319+
impl<'vm, M> ToPyException for DeferredMessageException<'vm, M>
320+
where
321+
M: FnOnce(&VirtualMachine) -> String,
322+
{
323+
fn to_pyexception(self, vm: &VirtualMachine) -> PyBaseExceptionRef {
324+
let Self { typ, build_message } = self;
325+
vm.new_exception_msg(typ.clone(), build_message(vm))
326+
}
327+
}
328+
283329
impl ExceptionCtor {
284330
pub fn instantiate(self, vm: &VirtualMachine) -> PyResult<PyBaseExceptionRef> {
285331
match self {

0 commit comments

Comments
 (0)