Skip to content

Commit 551b873

Browse files
authored
Merge pull request RustPython#3762 from youknowone/into-pyexc
distinguish ToPyException and IntoPyException
2 parents 385f6ad + 667ad75 commit 551b873

File tree

12 files changed

+147
-131
lines changed

12 files changed

+147
-131
lines changed

stdlib/src/posixsubprocess.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub(crate) use _posixsubprocess::make_module;
1919
#[pymodule]
2020
mod _posixsubprocess {
2121
use super::{exec, CStrPathLike, ForkExecArgs, ProcArgs};
22-
use crate::vm::{convert::ToPyException, PyResult, VirtualMachine};
22+
use crate::vm::{convert::IntoPyException, PyResult, VirtualMachine};
2323

2424
#[pyfunction]
2525
fn fork_exec(args: ForkExecArgs, vm: &VirtualMachine) -> PyResult<libc::pid_t> {
@@ -37,7 +37,7 @@ mod _posixsubprocess {
3737
let argv = &argv;
3838
let envp = args.env_list.as_ref().map(|s| cstrs_to_ptrs(s));
3939
let envp = envp.as_deref();
40-
match unsafe { nix::unistd::fork() }.map_err(|err| err.to_pyexception(vm))? {
40+
match unsafe { nix::unistd::fork() }.map_err(|err| err.into_pyexception(vm))? {
4141
nix::unistd::ForkResult::Child => exec(&args, ProcArgs { argv, envp }),
4242
nix::unistd::ForkResult::Parent { child } => Ok(child.as_raw()),
4343
}

stdlib/src/socket.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ mod _socket {
1313
use crate::common::lock::{PyMappedRwLockReadGuard, PyRwLock, PyRwLockReadGuard};
1414
use crate::vm::{
1515
builtins::{PyBaseExceptionRef, PyListRef, PyStrRef, PyTupleRef, PyTypeRef},
16-
convert::{ToPyException, ToPyObject, TryFromBorrowedObject, TryFromObject},
16+
convert::{IntoPyException, ToPyObject, TryFromBorrowedObject, TryFromObject},
1717
function::{ArgBytesLike, ArgMemoryBuffer, Either, OptionalArg, OptionalOption},
1818
types::{DefaultConstructor, Initializer},
1919
utils::ToCString,
@@ -540,7 +540,7 @@ mod _socket {
540540
);
541541

542542
fn init(zelf: PyRef<Self>, args: Self::Args, vm: &VirtualMachine) -> PyResult<()> {
543-
Self::_init(zelf, args, vm).map_err(|e| e.to_pyexception(vm))
543+
Self::_init(zelf, args, vm).map_err(|e| e.into_pyexception(vm))
544544
}
545545
}
546546

@@ -577,7 +577,7 @@ mod _socket {
577577
}
578578
if socket_kind == -1 {
579579
// TODO: when socket2 cuts a new release, type will be available on all os
580-
// socket_kind = sock.r#type().map_err(|e| e.to_pyexception(vm))?.into();
580+
// socket_kind = sock.r#type().map_err(|e| e.into_pyexception(vm))?.into();
581581
let res = unsafe {
582582
c::getsockopt(
583583
sock_fileno(&sock) as _,
@@ -1213,13 +1213,13 @@ mod _socket {
12131213
}
12141214
}
12151215
}
1216-
impl ToPyException for IoOrPyException {
1216+
impl IntoPyException for IoOrPyException {
12171217
#[inline]
1218-
fn to_pyexception(self, vm: &VirtualMachine) -> PyBaseExceptionRef {
1218+
fn into_pyexception(self, vm: &VirtualMachine) -> PyBaseExceptionRef {
12191219
match self {
12201220
Self::Timeout => timeout_error(vm),
12211221
Self::Py(exc) => exc,
1222-
Self::Io(err) => err.to_pyexception(vm),
1222+
Self::Io(err) => err.into_pyexception(vm),
12231223
}
12241224
}
12251225
}
@@ -1521,7 +1521,7 @@ mod _socket {
15211521
#[pyfunction]
15221522
fn if_nametoindex(name: PyObjectRef, vm: &VirtualMachine) -> PyResult<IfIndex> {
15231523
let name = crate::vm::stdlib::os::FsPath::try_from(name, true, vm)?;
1524-
let name = ffi::CString::new(name.as_bytes()).map_err(|err| err.to_pyexception(vm))?;
1524+
let name = ffi::CString::new(name.as_bytes()).map_err(|err| err.into_pyexception(vm))?;
15251525

15261526
let ret = unsafe { c::if_nametoindex(name.as_ptr()) };
15271527

@@ -1561,7 +1561,7 @@ mod _socket {
15611561
#[cfg(not(windows))]
15621562
{
15631563
let list = if_nameindex()
1564-
.map_err(|err| err.to_pyexception(vm))?
1564+
.map_err(|err| err.into_pyexception(vm))?
15651565
.to_slice()
15661566
.iter()
15671567
.map(|iface| {
@@ -1625,9 +1625,10 @@ mod _socket {
16251625
{
16261626
use std::ptr;
16271627

1628-
let table = MibTable::get_raw().map_err(|err| err.to_pyexception(vm))?;
1628+
let table = MibTable::get_raw().map_err(|err| err.into_pyexception(vm))?;
16291629
let list = table.as_slice().iter().map(|entry| {
1630-
let name = get_name(&entry.InterfaceLuid).map_err(|err| err.to_pyexception(vm))?;
1630+
let name =
1631+
get_name(&entry.InterfaceLuid).map_err(|err| err.into_pyexception(vm))?;
16311632
let tup = (entry.InterfaceIndex, name.to_string_lossy());
16321633
Ok(tup.to_pyobject(vm))
16331634
});

vm/src/convert/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ mod transmute_from;
44
mod try_from;
55

66
pub use into_object::IntoObject;
7-
pub use to_pyobject::{ToPyException, ToPyObject, ToPyResult};
7+
pub use to_pyobject::{IntoPyException, ToPyException, ToPyObject, ToPyResult};
88
pub use transmute_from::TransmuteFromObject;
99
pub use try_from::{TryFromBorrowedObject, TryFromObject};

vm/src/convert/to_pyobject.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,18 @@ pub trait ToPyResult {
1414
}
1515

1616
pub trait ToPyException {
17-
fn to_pyexception(self, vm: &VirtualMachine) -> PyBaseExceptionRef;
17+
fn to_pyexception(&self, vm: &VirtualMachine) -> PyBaseExceptionRef;
18+
}
19+
20+
pub trait IntoPyException {
21+
fn into_pyexception(self, vm: &VirtualMachine) -> PyBaseExceptionRef;
22+
}
23+
24+
impl<T> IntoPyException for &'_ T
25+
where
26+
T: ToPyException,
27+
{
28+
fn into_pyexception(self, vm: &VirtualMachine) -> PyBaseExceptionRef {
29+
self.to_pyexception(vm)
30+
}
1831
}

vm/src/exceptions.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -986,14 +986,14 @@ pub fn cstring_error(vm: &VirtualMachine) -> PyBaseExceptionRef {
986986
}
987987

988988
impl ToPyException for std::ffi::NulError {
989-
fn to_pyexception(self, vm: &VirtualMachine) -> PyBaseExceptionRef {
989+
fn to_pyexception(&self, vm: &VirtualMachine) -> PyBaseExceptionRef {
990990
cstring_error(vm)
991991
}
992992
}
993993

994994
#[cfg(windows)]
995995
impl<C: widestring::UChar> ToPyException for widestring::NulError<C> {
996-
fn to_pyexception(self, vm: &VirtualMachine) -> PyBaseExceptionRef {
996+
fn to_pyexception(&self, vm: &VirtualMachine) -> PyBaseExceptionRef {
997997
cstring_error(vm)
998998
}
999999
}

vm/src/format.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ pub(crate) enum FormatParseError {
556556
}
557557

558558
impl ToPyException for FormatParseError {
559-
fn to_pyexception(self, vm: &VirtualMachine) -> PyBaseExceptionRef {
559+
fn to_pyexception(&self, vm: &VirtualMachine) -> PyBaseExceptionRef {
560560
match self {
561561
FormatParseError::UnmatchedBracket => {
562562
vm.new_value_error("expected '}' before end of string".to_owned())

vm/src/object/ext.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use super::{
55
use crate::common::lock::PyRwLockReadGuard;
66
use crate::{
77
builtins::{PyBaseExceptionRef, PyStrInterned, PyType},
8-
convert::{ToPyException, ToPyObject, ToPyResult, TryFromObject},
8+
convert::{IntoPyException, ToPyObject, ToPyResult, TryFromObject},
99
VirtualMachine,
1010
};
1111
use std::{borrow::Borrow, fmt, ops::Deref};
@@ -369,18 +369,18 @@ where
369369
impl<T, E> ToPyResult for Result<T, E>
370370
where
371371
T: ToPyObject,
372-
E: ToPyException,
372+
E: IntoPyException,
373373
{
374374
#[inline(always)]
375375
fn to_pyresult(self, vm: &VirtualMachine) -> PyResult {
376376
self.map(|res| T::to_pyobject(res, vm))
377-
.map_err(|e| E::to_pyexception(e, vm))
377+
.map_err(|e| E::into_pyexception(e, vm))
378378
}
379379
}
380380

381-
impl ToPyException for PyBaseExceptionRef {
381+
impl IntoPyException for PyBaseExceptionRef {
382382
#[inline(always)]
383-
fn to_pyexception(self, _vm: &VirtualMachine) -> PyBaseExceptionRef {
383+
fn into_pyexception(self, _vm: &VirtualMachine) -> PyBaseExceptionRef {
384384
self
385385
}
386386
}

vm/src/stdlib/io.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,13 @@ cfg_if::cfg_if! {
1111

1212
use crate::{
1313
builtins::PyBaseExceptionRef,
14-
convert::{ToPyException, ToPyObject},
14+
convert::{IntoPyException, ToPyException, ToPyObject},
1515
PyObjectRef, PyResult, TryFromObject, VirtualMachine,
1616
};
1717
pub use _io::io_open as open;
1818

1919
impl ToPyException for std::io::Error {
20-
fn to_pyexception(self, vm: &VirtualMachine) -> PyBaseExceptionRef {
21-
(&self).to_pyexception(vm)
22-
}
23-
}
24-
25-
impl ToPyException for &'_ std::io::Error {
26-
fn to_pyexception(self, vm: &VirtualMachine) -> PyBaseExceptionRef {
20+
fn to_pyexception(&self, vm: &VirtualMachine) -> PyBaseExceptionRef {
2721
use std::io::ErrorKind;
2822

2923
let excs = &vm.ctx.exceptions;
@@ -44,6 +38,12 @@ impl ToPyException for &'_ std::io::Error {
4438
}
4539
}
4640

41+
impl IntoPyException for std::io::Error {
42+
fn into_pyexception(self, vm: &VirtualMachine) -> PyBaseExceptionRef {
43+
self.to_pyexception(vm)
44+
}
45+
}
46+
4747
pub(crate) fn make_module(vm: &VirtualMachine) -> PyObjectRef {
4848
let ctx = &vm.ctx;
4949

0 commit comments

Comments
 (0)