Skip to content

Commit ab1693e

Browse files
committed
Replace new_class to new_exception_type
Replace `new_class` method call for module-level exceptions to newly added `new_exception_type` Signed-off-by: snowapril <sinjihng@gmail.com>
1 parent 59582ed commit ab1693e

7 files changed

Lines changed: 48 additions & 51 deletions

File tree

stdlib/src/binascii.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,16 @@ mod decl {
1313

1414
#[pyattr(name = "Error", once)]
1515
fn error_type(vm: &VirtualMachine) -> PyTypeRef {
16-
vm.ctx.new_class(
17-
Some("binascii"),
16+
vm.ctx.new_exception_type(
17+
"binascii",
1818
"Error",
19-
&vm.ctx.exceptions.value_error,
20-
Default::default(),
19+
Some(vec![vm.ctx.exceptions.value_error.clone()]),
2120
)
2221
}
2322

2423
#[pyattr(name = "Incomplete", once)]
2524
fn incomplete_type(vm: &VirtualMachine) -> PyTypeRef {
26-
vm.ctx.new_class(
27-
Some("binascii"),
28-
"Incomplete",
29-
&vm.ctx.exceptions.exception_type,
30-
Default::default(),
31-
)
25+
vm.ctx.new_exception_type("binascii", "Incomplete", None)
3226
}
3327

3428
fn hex_nibble(n: u8) -> u8 {

stdlib/src/csv.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub(crate) fn make_module(vm: &VirtualMachine) -> PyObjectRef {
1111
mod _csv {
1212
use crate::common::lock::PyMutex;
1313
use crate::vm::{
14-
builtins::{PyStr, PyStrRef, PyType, PyTypeRef},
14+
builtins::{PyStr, PyStrRef, PyTypeRef},
1515
function::{ArgIterable, ArgumentError, FromArgs, FuncArgs},
1616
match_class,
1717
protocol::{PyIter, PyIterReturn},
@@ -32,7 +32,11 @@ mod _csv {
3232

3333
#[pyattr(name = "Error", once)]
3434
fn error(vm: &VirtualMachine) -> PyTypeRef {
35-
PyType::new_simple_ref("_csv.Error", &vm.ctx.exceptions.exception_type).unwrap()
35+
vm.ctx.new_exception_type(
36+
"_csv",
37+
"Error",
38+
Some(vec![vm.ctx.exceptions.exception_type.clone()]),
39+
)
3640
}
3741

3842
#[pyfunction]

stdlib/src/socket.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,29 +82,26 @@ mod _socket {
8282

8383
#[pyattr(once)]
8484
fn timeout(vm: &VirtualMachine) -> PyTypeRef {
85-
vm.ctx.new_class(
86-
Some("socket"),
85+
vm.ctx.new_exception_type(
86+
"socket",
8787
"timeout",
88-
&vm.ctx.exceptions.os_error,
89-
Default::default(),
88+
Some(vec![vm.ctx.exceptions.os_error.clone()]),
9089
)
9190
}
9291
#[pyattr(once)]
9392
fn herror(vm: &VirtualMachine) -> PyTypeRef {
94-
vm.ctx.new_class(
95-
Some("socket"),
93+
vm.ctx.new_exception_type(
94+
"socket",
9695
"herror",
97-
&vm.ctx.exceptions.os_error,
98-
Default::default(),
96+
Some(vec![vm.ctx.exceptions.os_error.clone()]),
9997
)
10098
}
10199
#[pyattr(once)]
102100
fn gaierror(vm: &VirtualMachine) -> PyTypeRef {
103-
vm.ctx.new_class(
104-
Some("socket"),
101+
vm.ctx.new_exception_type(
102+
"socket",
105103
"gaierror",
106-
&vm.ctx.exceptions.os_error,
107-
Default::default(),
104+
Some(vec![vm.ctx.exceptions.os_error.clone()]),
108105
)
109106
}
110107

stdlib/src/ssl.rs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ mod _ssl {
3030
},
3131
socket::{self, PySocket},
3232
vm::{
33-
builtins::{PyBaseException, PyBaseExceptionRef, PyStrRef, PyType, PyTypeRef, PyWeak},
33+
builtins::{PyBaseExceptionRef, PyStrRef, PyType, PyTypeRef, PyWeak},
3434
exceptions,
3535
function::{
3636
ArgBytesLike, ArgCallable, ArgMemoryBuffer, ArgStrOrBytesLike, IntoPyException,
@@ -39,7 +39,7 @@ mod _ssl {
3939
stdlib::os::PyPathLike,
4040
types::Constructor,
4141
utils::{Either, ToCString},
42-
ItemProtocol, PyClassImpl, PyObjectRef, PyRef, PyResult, PyValue, VirtualMachine,
42+
ItemProtocol, PyObjectRef, PyRef, PyResult, PyValue, VirtualMachine,
4343
},
4444
};
4545
use crossbeam_utils::atomic::AtomicCell;
@@ -177,45 +177,49 @@ mod _ssl {
177177
/// An error occurred in the SSL implementation.
178178
#[pyattr(name = "SSLError", once)]
179179
fn ssl_error(vm: &VirtualMachine) -> PyTypeRef {
180-
PyType::new_simple_ref("ssl.SSLError", &vm.ctx.exceptions.os_error).unwrap()
180+
vm.ctx.new_exception_type(
181+
"ssl",
182+
"SSLError",
183+
Some(vec![vm.ctx.exceptions.os_error.clone()]),
184+
)
181185
}
182186

183187
/// A certificate could not be verified.
184188
#[pyattr(name = "SSLCertVerificationError", once)]
185189
fn ssl_cert_verification_error(vm: &VirtualMachine) -> PyTypeRef {
186-
let ssl_error = ssl_error(vm);
187-
PyType::new_ref(
188-
"ssl.SSLCertVerificationError",
189-
vec![ssl_error, vm.ctx.exceptions.value_error.clone()],
190-
Default::default(),
191-
PyBaseException::make_slots(),
192-
vm.ctx.types.type_type.clone(),
190+
vm.ctx.new_exception_type(
191+
"ssl",
192+
"SSLCertVerificationError",
193+
Some(vec![ssl_error(vm), vm.ctx.exceptions.value_error.clone()]),
193194
)
194-
.unwrap()
195195
}
196196

197197
/// SSL/TLS session closed cleanly.
198198
#[pyattr(name = "SSLZeroReturnError", once)]
199199
fn ssl_zero_return_error(vm: &VirtualMachine) -> PyTypeRef {
200-
PyType::new_simple_ref("ssl.SSLZeroReturnError", &ssl_error(vm)).unwrap()
200+
vm.ctx
201+
.new_exception_type("ssl", "SSLZeroReturnError", Some(vec![ssl_error(vm)]))
201202
}
202203

203204
/// Non-blocking SSL socket needs to read more data before the requested operation can be completed.
204205
#[pyattr(name = "SSLWantReadError", once)]
205206
fn ssl_want_read_error(vm: &VirtualMachine) -> PyTypeRef {
206-
PyType::new_simple_ref("ssl.SSLWantReadError", &ssl_error(vm)).unwrap()
207+
vm.ctx
208+
.new_exception_type("ssl", "SSLWantReadError", Some(vec![ssl_error(vm)]))
207209
}
208210

209211
/// Non-blocking SSL socket needs to write more data before the requested operation can be completed.
210212
#[pyattr(name = "SSLWantWriteError", once)]
211213
fn ssl_want_write_error(vm: &VirtualMachine) -> PyTypeRef {
212-
PyType::new_simple_ref("ssl.SSLWantWriteError", &ssl_error(vm)).unwrap()
214+
vm.ctx
215+
.new_exception_type("ssl", "SSLWantWriteError", Some(vec![ssl_error(vm)]))
213216
}
214217

215218
/// System error when attempting SSL operation.
216219
#[pyattr(name = "SSLSyscallError", once)]
217220
fn ssl_syscall_error(vm: &VirtualMachine) -> PyTypeRef {
218-
PyType::new_simple_ref("ssl.SSLSyscallError", &ssl_error(vm)).unwrap()
221+
vm.ctx
222+
.new_exception_type("ssl", "SSLSyscallError", Some(vec![ssl_error(vm)]))
219223
}
220224

221225
/// SSL/TLS connection terminated abruptly.

stdlib/src/termios.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,10 @@ mod termios {
102102

103103
#[pyattr(name = "error", once)]
104104
fn error_type(vm: &VirtualMachine) -> PyTypeRef {
105-
vm.ctx.new_class(
106-
Some("termios"),
105+
vm.ctx.new_exception_type(
106+
"termios",
107107
"error",
108-
&vm.ctx.exceptions.os_error,
109-
Default::default(),
108+
Some(vec![vm.ctx.exceptions.os_error.clone()]),
110109
)
111110
}
112111
}

stdlib/src/zlib.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ pub(crate) use zlib::make_module;
44
mod zlib {
55
use crate::common::lock::PyMutex;
66
use crate::vm::{
7-
builtins::{PyBaseExceptionRef, PyBytes, PyBytesRef, PyIntRef, PyType, PyTypeRef},
7+
builtins::{PyBaseExceptionRef, PyBytes, PyBytesRef, PyIntRef, PyTypeRef},
88
function::{ArgBytesLike, OptionalArg},
99
IntoPyRef, PyResult, PyValue, VirtualMachine,
1010
};
@@ -55,7 +55,11 @@ mod zlib {
5555

5656
#[pyattr(once)]
5757
fn error(vm: &VirtualMachine) -> PyTypeRef {
58-
PyType::new_simple_ref("zlib.error", &vm.ctx.exceptions.exception_type).unwrap()
58+
vm.ctx.new_exception_type(
59+
"zlib",
60+
"error",
61+
Some(vec![vm.ctx.exceptions.exception_type.clone()]),
62+
)
5963
}
6064

6165
/// Compute an Adler-32 checksum of data.

vm/src/stdlib/pystruct.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -962,12 +962,7 @@ pub(crate) mod _struct {
962962

963963
#[pyattr(name = "error", once)]
964964
fn error_type(vm: &VirtualMachine) -> PyTypeRef {
965-
vm.ctx.new_class(
966-
Some("struct"),
967-
"error",
968-
&vm.ctx.exceptions.exception_type,
969-
Default::default(),
970-
)
965+
vm.ctx.new_exception_type("struct", "error", None)
971966
}
972967

973968
fn new_struct_error(vm: &VirtualMachine, msg: String) -> PyBaseExceptionRef {

0 commit comments

Comments
 (0)