Skip to content

Commit 79c4439

Browse files
committed
Use cfg_select in a bunch more places
1 parent 6e895fb commit 79c4439

23 files changed

Lines changed: 338 additions & 442 deletions

File tree

crates/common/src/rc.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
1-
#[cfg(not(feature = "threading"))]
2-
use alloc::rc::Rc;
3-
#[cfg(feature = "threading")]
4-
use alloc::sync::Arc;
5-
61
// type aliases instead of new-types because you can't do `fn method(self: PyRc<Self>)` with a
72
// newtype; requires the arbitrary_self_types unstable feature
83

9-
#[cfg(feature = "threading")]
10-
pub type PyRc<T> = Arc<T>;
11-
#[cfg(not(feature = "threading"))]
12-
pub type PyRc<T> = Rc<T>;
4+
pub type PyRc<T> = cfg_select! {
5+
feature = "threading" => alloc::sync::Arc::<T>,
6+
_ => alloc::rc::Rc::<T>,
7+
};

crates/common/src/refcount.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ const WEAK_COUNT: usize = 1 << STRONG_WIDTH;
2020
reason = "refcount overflow must preserve upstream abort semantics"
2121
)]
2222
fn refcount_overflow() -> ! {
23-
#[cfg(feature = "std")]
24-
std::process::abort();
25-
#[cfg(not(feature = "std"))]
26-
core::panic!("refcount overflow");
23+
cfg_select! {
24+
feature = "std" => std::process::abort(),
25+
_ => core::panic!("refcount overflow"),
26+
}
2727
}
2828

2929
/// State wraps reference count + flags in a single word (platform usize)

crates/common/src/str.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@ use core::fmt;
77
use core::ops::{Bound, RangeBounds};
88
use core::sync::atomic::Ordering::Relaxed;
99

10-
#[cfg(not(target_arch = "wasm32"))]
1110
#[allow(non_camel_case_types)]
12-
pub type wchar_t = libc::wchar_t;
13-
#[cfg(target_arch = "wasm32")]
14-
#[allow(non_camel_case_types)]
15-
pub type wchar_t = u32;
11+
pub type wchar_t = cfg_select! {
12+
target_arch = "wasm32" => u32,
13+
_ => libc::wchar_t,
14+
};
1615

1716
/// Utf8 + state.ascii (+ PyUnicode_Kind in future)
1817
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]

crates/host_env/src/crt_fd.rs

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ mod c {
2626

2727
// this is basically what CPython has for Py_off_t; windows uses long long
2828
// for offsets, other platforms just use off_t
29-
#[cfg(not(windows))]
30-
pub type Offset = c::off_t;
31-
#[cfg(windows)]
32-
pub type Offset = c::c_longlong;
29+
pub type Offset = cfg_select! {
30+
windows => c::c_longlong,
31+
_ => c::off_t,
32+
};
3333

34-
#[cfg(not(windows))]
35-
pub type Raw = RawFd;
36-
#[cfg(windows)]
37-
pub type Raw = i32;
34+
pub type Raw = cfg_select! {
35+
windows => i32,
36+
_ => RawFd,
37+
};
3838

3939
#[inline]
4040
fn cvt<I: num_traits::PrimInt>(ret: I) -> io::Result<I> {
@@ -341,18 +341,16 @@ pub fn ftruncate(fd: Borrowed<'_>, len: Offset) -> io::Result<()> {
341341
let ret = unsafe { suppress_iph!(c::ftruncate(fd.as_raw(), len)) };
342342
// On Windows, _chsize_s returns 0 on success, or a positive error code (errno value) on failure.
343343
// On other platforms, ftruncate returns 0 on success, or -1 on failure with errno set.
344-
#[cfg(windows)]
345-
{
346-
if ret != 0 {
347-
// _chsize_s returns errno directly, convert to Windows error code
348-
let winerror = crate::os::errno_to_winerror(ret);
349-
return Err(io::Error::from_raw_os_error(winerror));
344+
cfg_select! {
345+
windows => {
346+
if ret != 0 {
347+
// _chsize_s returns errno directly, convert to Windows error code
348+
let winerror = crate::os::errno_to_winerror(ret);
349+
return Err(io::Error::from_raw_os_error(winerror));
350+
}
350351
}
351-
}
352-
#[cfg(not(windows))]
353-
{
354-
cvt(ret)?;
355-
}
352+
_ => cvt(ret)?,
353+
};
356354
Ok(())
357355
}
358356

crates/stdlib/src/faulthandler.rs

Lines changed: 29 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -119,29 +119,14 @@ mod decl {
119119
// PUTS macro
120120
#[cfg(any(unix, windows))]
121121
fn puts(fd: i32, s: &str) {
122-
let _ = unsafe {
123-
#[cfg(windows)]
124-
{
125-
libc::write(fd, s.as_ptr() as *const libc::c_void, s.len() as u32)
126-
}
127-
#[cfg(not(windows))]
128-
{
129-
libc::write(fd, s.as_ptr() as *const libc::c_void, s.len())
130-
}
131-
};
122+
puts_bytes(fd, s.as_bytes())
132123
}
133124

134125
#[cfg(any(unix, windows))]
135126
fn puts_bytes(fd: i32, s: &[u8]) {
136-
let _ = unsafe {
137-
#[cfg(windows)]
138-
{
139-
libc::write(fd, s.as_ptr() as *const libc::c_void, s.len() as u32)
140-
}
141-
#[cfg(not(windows))]
142-
{
143-
libc::write(fd, s.as_ptr() as *const libc::c_void, s.len())
144-
}
127+
let _ = cfg_select! {
128+
windows => unsafe { libc::write(fd, s.as_ptr() as *const libc::c_void, s.len() as u32) },
129+
_ => unsafe { libc::write(fd, s.as_ptr() as *const libc::c_void, s.len()) },
145130
};
146131
}
147132

@@ -158,16 +143,7 @@ mod decl {
158143
buf[2 + i] = HEX_CHARS[digit];
159144
}
160145

161-
let _ = unsafe {
162-
#[cfg(windows)]
163-
{
164-
libc::write(fd, buf.as_ptr() as *const libc::c_void, (2 + width) as u32)
165-
}
166-
#[cfg(not(windows))]
167-
{
168-
libc::write(fd, buf.as_ptr() as *const libc::c_void, 2 + width)
169-
}
170-
};
146+
puts_bytes(fd, &buf[..2 + width]);
171147
}
172148

173149
// _Py_DumpDecimal (traceback.c)
@@ -188,17 +164,7 @@ mod decl {
188164
v /= 10;
189165
}
190166

191-
let len = buf.len() - i;
192-
let _ = unsafe {
193-
#[cfg(windows)]
194-
{
195-
libc::write(fd, buf[i..].as_ptr() as *const libc::c_void, len as u32)
196-
}
197-
#[cfg(not(windows))]
198-
{
199-
libc::write(fd, buf[i..].as_ptr() as *const libc::c_void, len)
200-
}
201-
};
167+
puts_bytes(fd, &buf[..buf.len() - i]);
202168
}
203169

204170
/// Get current thread ID
@@ -857,30 +823,31 @@ mod decl {
857823
drop(guard); // Release lock before I/O
858824

859825
// Timeout occurred, dump traceback
860-
#[cfg(target_arch = "wasm32")]
861-
let _ = (exit, fd, &header);
862-
863-
#[cfg(not(target_arch = "wasm32"))]
864-
{
865-
puts_bytes(fd, header.as_bytes());
866-
867-
// Use thread frame slots when threading is enabled (includes all threads).
868-
// Fall back to live frame walking for non-threaded builds.
869-
#[cfg(feature = "threading")]
870-
{
871-
for (tid, slot) in &thread_frame_slots {
872-
let frames = slot.frames.lock();
873-
dump_traceback_thread_frames(fd, *tid, false, &frames);
874-
}
875-
}
876-
#[cfg(not(feature = "threading"))]
877-
{
878-
write_thread_id(fd, current_thread_id(), false);
879-
dump_live_frames(fd);
826+
cfg_select! {
827+
target_arch = "wasm32" => {
828+
let _ = (exit, fd, &header);
880829
}
830+
_ => {
831+
puts_bytes(fd, header.as_bytes());
832+
833+
// Use thread frame slots when threading is enabled (includes all threads).
834+
// Fall back to live frame walking for non-threaded builds.
835+
cfg_select! {
836+
feature = "threading" => {
837+
for (tid, slot) in &thread_frame_slots {
838+
let frames = slot.frames.lock();
839+
dump_traceback_thread_frames(fd, *tid, false, &frames);
840+
}
841+
}
842+
_ => {
843+
write_thread_id(fd, current_thread_id(), false);
844+
dump_live_frames(fd);
845+
}
846+
}
881847

882-
if exit {
883-
rustpython_host_env::os::exit(1);
848+
if exit {
849+
rustpython_host_env::os::exit(1);
850+
}
884851
}
885852
}
886853

crates/stdlib/src/openssl.rs

Lines changed: 46 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,15 +1307,13 @@ mod _ssl {
13071307
#[pygetset]
13081308
fn num_tickets(&self, _vm: &VirtualMachine) -> PyResult<usize> {
13091309
// Only supported for TLS 1.3
1310-
#[cfg(ossl110)]
1311-
{
1312-
let ctx = self.ctx();
1313-
let num = unsafe { sys::SSL_CTX_get_num_tickets(ctx.as_ptr()) };
1314-
Ok(num)
1315-
}
1316-
#[cfg(not(ossl110))]
1317-
{
1318-
Ok(0)
1310+
cfg_select! {
1311+
ossl110 => {
1312+
let ctx = self.ctx();
1313+
let num = unsafe { sys::SSL_CTX_get_num_tickets(ctx.as_ptr()) };
1314+
Ok(num)
1315+
}
1316+
_ => Ok(0),
13191317
}
13201318
}
13211319
#[pygetset(setter)]
@@ -1330,19 +1328,19 @@ mod _ssl {
13301328
return Err(vm.new_value_error("SSLContext is not a server context."));
13311329
}
13321330

1333-
#[cfg(ossl110)]
1334-
{
1335-
let ctx = self.builder();
1336-
let result = unsafe { sys::SSL_CTX_set_num_tickets(ctx.as_ptr(), value as usize) };
1337-
if result != 1 {
1338-
return Err(vm.new_value_error("failed to set num tickets."));
1331+
cfg_select! {
1332+
ossl110 => {
1333+
let ctx = self.builder();
1334+
let result = unsafe { sys::SSL_CTX_set_num_tickets(ctx.as_ptr(), value as usize) };
1335+
if result != 1 {
1336+
return Err(vm.new_value_error("failed to set num tickets."));
1337+
}
1338+
Ok(())
1339+
}
1340+
_ => {
1341+
let _ = (value, vm);
1342+
Ok(())
13391343
}
1340-
Ok(())
1341-
}
1342-
#[cfg(not(ossl110))]
1343-
{
1344-
let _ = (value, vm);
1345-
Ok(())
13461344
}
13471345
}
13481346

@@ -1565,9 +1563,10 @@ mod _ssl {
15651563
let binary_form = args.binary_form.unwrap_or(false);
15661564
let ctx = self.ctx();
15671565
#[cfg(ossl300)]
1568-
let certs = ctx.cert_store().all_certificates();
1569-
#[cfg(not(ossl300))]
1570-
let certs = ctx.cert_store().objects().iter().filter_map(|x| x.x509());
1566+
let certs = cfg_select! {
1567+
ossl300 => ctx.cert_store().all_certificates(),
1568+
_ => ctx.cert_store().objects().iter().filter_map(|x| x.x509()),
1569+
};
15711570

15721571
// Filter to only include CA certificates (Basic Constraints: CA=TRUE)
15731572
let certs = certs
@@ -2792,21 +2791,21 @@ mod _ssl {
27922791

27932792
#[pymethod]
27942793
fn verify_client_post_handshake(&self, vm: &VirtualMachine) -> PyResult<()> {
2795-
#[cfg(ossl111)]
2796-
{
2797-
let stream = self.connection.read();
2798-
let result = unsafe { SSL_verify_client_post_handshake(stream.ssl().as_ptr()) };
2799-
if result == 0 {
2800-
Err(convert_openssl_error(vm, openssl::error::ErrorStack::get()))
2801-
} else {
2802-
Ok(())
2794+
cfg_select! {
2795+
ossl111 => {
2796+
let stream = self.connection.read();
2797+
let result = unsafe { SSL_verify_client_post_handshake(stream.ssl().as_ptr()) };
2798+
if result == 0 {
2799+
Err(convert_openssl_error(vm, openssl::error::ErrorStack::get()))
2800+
} else {
2801+
Ok(())
2802+
}
2803+
}
2804+
_ => {
2805+
Err(vm.new_not_implemented_error(
2806+
"Post-handshake auth is not supported by your OpenSSL version.",
2807+
))
28032808
}
2804-
}
2805-
#[cfg(not(ossl111))]
2806-
{
2807-
Err(vm.new_not_implemented_error(
2808-
"Post-handshake auth is not supported by your OpenSSL version.",
2809-
))
28102809
}
28112810
}
28122811

@@ -3681,15 +3680,9 @@ mod _ssl {
36813680
impl PySslSession {
36823681
#[pygetset]
36833682
fn time(&self) -> i64 {
3684-
unsafe {
3685-
#[cfg(ossl330)]
3686-
{
3687-
sys::SSL_SESSION_get_time(self.session) as i64
3688-
}
3689-
#[cfg(not(ossl330))]
3690-
{
3691-
sys::SSL_SESSION_get_time(self.session) as i64
3692-
}
3683+
cfg_select! {
3684+
ossl330 => unsafe { sys::SSL_SESSION_get_time(self.session) as i64 },
3685+
_ => unsafe { sys::SSL_SESSION_get_time(self.session) as i64 },
36933686
}
36943687
}
36953688

@@ -3701,14 +3694,10 @@ mod _ssl {
37013694
#[pygetset]
37023695
fn ticket_lifetime_hint(&self) -> u64 {
37033696
// SSL_SESSION_get_ticket_lifetime_hint available in OpenSSL 1.1.0+
3704-
#[cfg(ossl110)]
3705-
{
3706-
unsafe { SSL_SESSION_get_ticket_lifetime_hint(self.session) as u64 }
3707-
}
3708-
#[cfg(not(ossl110))]
3709-
{
3697+
cfg_select! {
3698+
ossl110 => unsafe { SSL_SESSION_get_ticket_lifetime_hint(self.session) as u64 },
37103699
// Not available in older OpenSSL versions
3711-
0
3700+
_ => 0,
37123701
}
37133702
}
37143703

@@ -3725,14 +3714,10 @@ mod _ssl {
37253714
#[pygetset]
37263715
fn has_ticket(&self) -> bool {
37273716
// SSL_SESSION_has_ticket available in OpenSSL 1.1.0+
3728-
#[cfg(ossl110)]
3729-
{
3730-
unsafe { SSL_SESSION_has_ticket(self.session) != 0 }
3731-
}
3732-
#[cfg(not(ossl110))]
3733-
{
3717+
cfg_select! {
3718+
ossl110 => unsafe { SSL_SESSION_has_ticket(self.session) != 0 },
37343719
// Not available in older OpenSSL versions
3735-
false
3720+
_ => false,
37363721
}
37373722
}
37383723
}

crates/stdlib/src/socket.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3264,7 +3264,7 @@ mod _socket {
32643264
fn sock_from_raw(fileno: RawSocket, vm: &VirtualMachine) -> PyResult<Socket> {
32653265
let invalid = cfg_select! {
32663266
windows => fileno == INVALID_SOCKET,
3267-
_ => fileno < 0
3267+
_ => fileno < 0,
32683268
};
32693269
if invalid {
32703270
return Err(vm.new_value_error("negative file descriptor"));

0 commit comments

Comments
 (0)