| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff 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 | - | ||
| 6 | 1 | // type aliases instead of new-types because you can't do `fn method(self: PyRc<Self>)` with a | |
| 7 | 2 | // newtype; requires the arbitrary_self_types unstable feature | |
| 8 | 3 | ||
| 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 | + }; | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -20,10 +20,10 @@ const WEAK_COUNT: usize = 1 << STRONG_WIDTH; | |||
| 20 | 20 | reason = "refcount overflow must preserve upstream abort semantics" | |
| 21 | 21 | )] | |
| 22 | 22 | 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 | + } | ||
| 27 | 27 | } | |
| 28 | 28 | ||
| 29 | 29 | /// State wraps reference count + flags in a single word (platform usize) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,12 +7,11 @@ use core::fmt; | |||
| 7 | 7 | use core::ops::{Bound, RangeBounds}; | |
| 8 | 8 | use core::sync::atomic::Ordering::Relaxed; | |
| 9 | 9 | ||
| 10 | - #[cfg(not(target_arch = "wasm32"))] | ||
| 11 | 10 | #[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 | + }; | ||
| 16 | 15 | ||
| 17 | 16 | /// Utf8 + state.ascii (+ PyUnicode_Kind in future) | |
| 18 | 17 | #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)] | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -26,15 +26,15 @@ mod c { | |||
| 26 | 26 | ||
| 27 | 27 | // this is basically what CPython has for Py_off_t; windows uses long long | |
| 28 | 28 | // 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 | + }; | ||
| 33 | 33 | ||
| 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 | + }; | ||
| 38 | 38 | ||
| 39 | 39 | #[inline] | |
| 40 | 40 | fn cvt<I: num_traits::PrimInt>(ret: I) -> io::Result<I> { | |
@@ -351,18 +351,16 @@ pub fn ftruncate(fd: Borrowed<'_>, len: Offset) -> io::Result<()> { | |||
| 351 | 351 | let ret = unsafe { suppress_iph!(c::ftruncate(fd.as_raw(), len)) }; | |
| 352 | 352 | // On Windows, _chsize_s returns 0 on success, or a positive error code (errno value) on failure. | |
| 353 | 353 | // On other platforms, ftruncate returns 0 on success, or -1 on failure with errno set. | |
| 354 | - #[cfg(windows)] | ||
| 355 | - { | ||
| 356 | - if ret != 0 { | ||
| 357 | - // _chsize_s returns errno directly, convert to Windows error code | ||
| 358 | - let winerror = crate::os::errno_to_winerror(ret); | ||
| 359 | - return Err(io::Error::from_raw_os_error(winerror)); | ||
| 354 | + cfg_select! { | ||
| 355 | + windows => { | ||
| 356 | + if ret != 0 { | ||
| 357 | + // _chsize_s returns errno directly, convert to Windows error code | ||
| 358 | + let winerror = crate::os::errno_to_winerror(ret); | ||
| 359 | + return Err(io::Error::from_raw_os_error(winerror)); | ||
| 360 | + } | ||
| 360 | 361 | } | |
| 361 | - } | ||
| 362 | - #[cfg(not(windows))] | ||
| 363 | - { | ||
| 364 | - cvt(ret)?; | ||
| 365 | - } | ||
| 362 | + _ => cvt(ret)?, | ||
| 363 | + }; | ||
| 366 | 364 | Ok(()) | |
| 367 | 365 | } | |
| 368 | 366 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -119,30 +119,12 @@ mod decl { | |||
| 119 | 119 | // PUTS macro | |
| 120 | 120 | #[cfg(any(unix, windows))] | |
| 121 | 121 | 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()) | ||
| 132 | 123 | } | |
| 133 | 124 | ||
| 134 | 125 | #[cfg(any(unix, windows))] | |
| 135 | 126 | 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 | - } | ||
| 145 | - }; | ||
| 127 | + let _ = unsafe { libc::write(fd, s.as_ptr().cast::<libc::c_void>(), s.len() as _) }; | ||
| 146 | 128 | } | |
| 147 | 129 | ||
| 148 | 130 | // _Py_DumpHexadecimal (traceback.c) | |
@@ -158,16 +140,7 @@ mod decl { | |||
| 158 | 140 | buf[2 + i] = HEX_CHARS[digit]; | |
| 159 | 141 | } | |
| 160 | 142 | ||
| 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 | - }; | ||
| 143 | + puts_bytes(fd, &buf[..2 + width]); | ||
| 171 | 144 | } | |
| 172 | 145 | ||
| 173 | 146 | // _Py_DumpDecimal (traceback.c) | |
@@ -188,17 +161,7 @@ mod decl { | |||
| 188 | 161 | v /= 10; | |
| 189 | 162 | } | |
| 190 | 163 | ||
| 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 | - }; | ||
| 164 | + puts_bytes(fd, &buf[i..]); | ||
| 202 | 165 | } | |
| 203 | 166 | ||
| 204 | 167 | /// Get current thread ID | |
@@ -857,30 +820,31 @@ mod decl { | |||
| 857 | 820 | drop(guard); // Release lock before I/O | |
| 858 | 821 | ||
| 859 | 822 | // 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); | ||
| 823 | + cfg_select! { | ||
| 824 | + target_arch = "wasm32" => { | ||
| 825 | + let _ = (exit, fd, &header); | ||
| 880 | 826 | } | |
| 827 | + _ => { | ||
| 828 | + puts_bytes(fd, header.as_bytes()); | ||
| 829 | + | ||
| 830 | + // Use thread frame slots when threading is enabled (includes all threads). | ||
| 831 | + // Fall back to live frame walking for non-threaded builds. | ||
| 832 | + cfg_select! { | ||
| 833 | + feature = "threading" => { | ||
| 834 | + for (tid, slot) in &thread_frame_slots { | ||
| 835 | + let frames = slot.frames.lock(); | ||
| 836 | + dump_traceback_thread_frames(fd, *tid, false, &frames); | ||
| 837 | + } | ||
| 838 | + } | ||
| 839 | + _ => { | ||
| 840 | + write_thread_id(fd, current_thread_id(), false); | ||
| 841 | + dump_live_frames(fd); | ||
| 842 | + } | ||
| 843 | + } | ||
| 881 | 844 | ||
| 882 | - if exit { | ||
| 883 | - rustpython_host_env::os::exit(1); | ||
| 845 | + if exit { | ||
| 846 | + rustpython_host_env::os::exit(1); | ||
| 847 | + } | ||
| 884 | 848 | } | |
| 885 | 849 | } | |
| 886 | 850 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1307,15 +1307,13 @@ mod _ssl { | |||
| 1307 | 1307 | #[pygetset] | |
| 1308 | 1308 | fn num_tickets(&self, _vm: &VirtualMachine) -> PyResult<usize> { | |
| 1309 | 1309 | // 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 | + ossl111 => { | ||
| 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), | ||
| 1319 | 1317 | } | |
| 1320 | 1318 | } | |
| 1321 | 1319 | #[pygetset(setter)] | |
@@ -1330,19 +1328,19 @@ mod _ssl { | |||
| 1330 | 1328 | return Err(vm.new_value_error("SSLContext is not a server context.")); | |
| 1331 | 1329 | } | |
| 1332 | 1330 | ||
| 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(()) | ||
| 1339 | 1343 | } | |
| 1340 | - Ok(()) | ||
| 1341 | - } | ||
| 1342 | - #[cfg(not(ossl110))] | ||
| 1343 | - { | ||
| 1344 | - let _ = (value, vm); | ||
| 1345 | - Ok(()) | ||
| 1346 | 1344 | } | |
| 1347 | 1345 | } | |
| 1348 | 1346 | ||
@@ -1564,10 +1562,10 @@ mod _ssl { | |||
| 1564 | 1562 | ) -> PyResult<Vec<PyObjectRef>> { | |
| 1565 | 1563 | let binary_form = args.binary_form.unwrap_or(false); | |
| 1566 | 1564 | let ctx = self.ctx(); | |
| 1567 | - #[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()); | ||
| 1565 | + let certs = cfg_select! { | ||
| 1566 | + ossl300 => ctx.cert_store().all_certificates(), | ||
| 1567 | + _ => ctx.cert_store().objects().iter().filter_map(|x| x.x509()), | ||
| 1568 | + }; | ||
| 1571 | 1569 | ||
| 1572 | 1570 | // Filter to only include CA certificates (Basic Constraints: CA=TRUE) | |
| 1573 | 1571 | let certs = certs | |
@@ -2792,21 +2790,21 @@ mod _ssl { | |||
| 2792 | 2790 | ||
| 2793 | 2791 | #[pymethod] | |
| 2794 | 2792 | 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(()) | ||
| 2793 | + cfg_select! { | ||
| 2794 | + ossl111 => { | ||
| 2795 | + let stream = self.connection.read(); | ||
| 2796 | + let result = unsafe { SSL_verify_client_post_handshake(stream.ssl().as_ptr()) }; | ||
| 2797 | + if result == 0 { | ||
| 2798 | + Err(convert_openssl_error(vm, openssl::error::ErrorStack::get())) | ||
| 2799 | + } else { | ||
| 2800 | + Ok(()) | ||
| 2801 | + } | ||
| 2802 | + } | ||
| 2803 | + _ => { | ||
| 2804 | + Err(vm.new_not_implemented_error( | ||
| 2805 | + "Post-handshake auth is not supported by your OpenSSL version.", | ||
| 2806 | + )) | ||
| 2803 | 2807 | } | |
| 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 | - )) | ||
| 2810 | 2808 | } | |
| 2811 | 2809 | } | |
| 2812 | 2810 | ||
@@ -3681,15 +3679,9 @@ mod _ssl { | |||
| 3681 | 3679 | impl PySslSession { | |
| 3682 | 3680 | #[pygetset] | |
| 3683 | 3681 | 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 | - } | ||
| 3682 | + cfg_select! { | ||
| 3683 | + ossl330 => unsafe { sys::SSL_SESSION_get_time(self.session) as i64 }, | ||
| 3684 | + _ => unsafe { sys::SSL_SESSION_get_time(self.session) as i64 }, | ||
| 3693 | 3685 | } | |
| 3694 | 3686 | } | |
| 3695 | 3687 | ||
@@ -3701,14 +3693,10 @@ mod _ssl { | |||
| 3701 | 3693 | #[pygetset] | |
| 3702 | 3694 | fn ticket_lifetime_hint(&self) -> u64 { | |
| 3703 | 3695 | // 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 | - { | ||
| 3696 | + cfg_select! { | ||
| 3697 | + ossl110 => unsafe { SSL_SESSION_get_ticket_lifetime_hint(self.session) as u64 }, | ||
| 3710 | 3698 | // Not available in older OpenSSL versions | |
| 3711 | - 0 | ||
| 3699 | + _ => 0, | ||
| 3712 | 3700 | } | |
| 3713 | 3701 | } | |
| 3714 | 3702 | ||
@@ -3725,14 +3713,10 @@ mod _ssl { | |||
| 3725 | 3713 | #[pygetset] | |
| 3726 | 3714 | fn has_ticket(&self) -> bool { | |
| 3727 | 3715 | // 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 | - { | ||
| 3716 | + cfg_select! { | ||
| 3717 | + ossl110 => unsafe { SSL_SESSION_has_ticket(self.session) != 0 }, | ||
| 3734 | 3718 | // Not available in older OpenSSL versions | |
| 3735 | - false | ||
| 3719 | + _ => false, | ||
| 3736 | 3720 | } | |
| 3737 | 3721 | } | |
| 3738 | 3722 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3273,7 +3273,7 @@ mod _socket { | |||
| 3273 | 3273 | fn sock_from_raw(fileno: RawSocket, vm: &VirtualMachine) -> PyResult<Socket> { | |
| 3274 | 3274 | let invalid = cfg_select! { | |
| 3275 | 3275 | windows => fileno == INVALID_SOCKET, | |
| 3276 | - _ => fileno < 0 | ||
| 3276 | + _ => fileno < 0, | ||
| 3277 | 3277 | }; | |
| 3278 | 3278 | if invalid { | |
| 3279 | 3279 | return Err(vm.new_value_error("negative file descriptor")); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments