Skip to content

Commit 04de53d

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 51af5de commit 04de53d

5 files changed

Lines changed: 36 additions & 47 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")]
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")]
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/socket.rs

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

8484
#[pyattr]
8585
fn timeout(vm: &VirtualMachine) -> PyTypeRef {
86-
vm.ctx.new_class(
87-
Some("socket"),
86+
vm.ctx.new_exception_type(
87+
"socket",
8888
"timeout",
89-
&vm.ctx.exceptions.os_error,
90-
Default::default(),
89+
Some(vec![vm.ctx.exceptions.os_error.clone()]),
9190
)
9291
}
9392
#[pyattr]
9493
fn herror(vm: &VirtualMachine) -> PyTypeRef {
95-
vm.ctx.new_class(
96-
Some("socket"),
94+
vm.ctx.new_exception_type(
95+
"socket",
9796
"herror",
98-
&vm.ctx.exceptions.os_error,
99-
Default::default(),
97+
Some(vec![vm.ctx.exceptions.os_error.clone()]),
10098
)
10199
}
102100
#[pyattr]
103101
fn gaierror(vm: &VirtualMachine) -> PyTypeRef {
104-
vm.ctx.new_class(
105-
Some("socket"),
102+
vm.ctx.new_exception_type(
103+
"socket",
106104
"gaierror",
107-
&vm.ctx.exceptions.os_error,
108-
Default::default(),
105+
Some(vec![vm.ctx.exceptions.os_error.clone()]),
109106
)
110107
}
111108

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")]
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")]
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")]
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")]
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")]
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")]
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")]
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
}

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")]
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)