Skip to content

Commit 9ad9b54

Browse files
authored
Merge pull request RustPython#4041 from TwoPair/feature-thread-info
Feature: sys.thread_info
2 parents 7f2f4c6 + ec5fd4a commit 9ad9b54

File tree

3 files changed

+51
-9
lines changed

3 files changed

+51
-9
lines changed

Lib/test/test_sys.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -573,8 +573,6 @@ def test_attributes(self):
573573
if not sys.platform.startswith('win'):
574574
self.assertIsInstance(sys.abiflags, str)
575575

576-
# TODO: RUSTPYTHON, AttributeError: module 'sys' has no attribute 'thread_info'
577-
@unittest.expectedFailure
578576
def test_thread_info(self):
579577
info = sys.thread_info
580578
self.assertEqual(len(info), 3)

vm/src/stdlib/sys.rs

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,22 @@ pub(crate) use sys::{UnraisableHookArgs, MAXSIZE, MULTIARCH};
44

55
#[pymodule]
66
mod sys {
7-
use crate::common::{
8-
ascii,
9-
hash::{PyHash, PyUHash},
10-
};
117
use crate::{
128
builtins::{PyDictRef, PyNamespace, PyStr, PyStrRef, PyTupleRef, PyTypeRef},
9+
common::{
10+
ascii,
11+
hash::{PyHash, PyUHash},
12+
},
1313
frame::FrameRef,
1414
function::{FuncArgs, OptionalArg, PosArgs},
15-
stdlib::builtins,
15+
stdlib::{self, builtins},
1616
types::PyStructSequence,
1717
version,
1818
vm::{Settings, VirtualMachine},
1919
AsObject, PyObjectRef, PyRef, PyRefExact, PyResult,
2020
};
2121
use num_traits::ToPrimitive;
22-
use std::sync::atomic::Ordering;
23-
use std::{env, mem, path};
22+
use std::{env, mem, path, sync::atomic::Ordering};
2423

2524
// not the same as CPython (e.g. rust's x86_x64-unknown-linux-gnu is just x86_64-linux-gnu)
2625
// but hopefully that's just an implementation detail? TODO: copy CPython's multiarch exactly,
@@ -562,6 +561,12 @@ mod sys {
562561
update_use_tracing(vm);
563562
}
564563

564+
#[cfg(feature = "threading")]
565+
#[pyattr]
566+
fn thread_info(vm: &VirtualMachine) -> PyTupleRef {
567+
PyThreadInfo::INFO.into_struct_sequence(vm)
568+
}
569+
565570
#[pyattr]
566571
fn version_info(vm: &VirtualMachine) -> PyTupleRef {
567572
VersionInfo::VERSION.into_struct_sequence(vm)
@@ -643,6 +648,27 @@ mod sys {
643648
}
644649
}
645650

651+
#[cfg(feature = "threading")]
652+
#[pyclass(noattr, name = "thread_info")]
653+
#[derive(PyStructSequence)]
654+
pub(super) struct PyThreadInfo {
655+
name: Option<&'static str>,
656+
lock: Option<&'static str>,
657+
version: Option<&'static str>,
658+
}
659+
660+
#[cfg(feature = "threading")]
661+
#[pyclass(with(PyStructSequence))]
662+
impl PyThreadInfo {
663+
const INFO: Self = PyThreadInfo {
664+
name: stdlib::thread::_thread::PYTHREAD_NAME,
665+
/// As I know, there's only way to use lock as "Mutex" in Rust
666+
/// with satisfying python document spec.
667+
lock: Some("mutex+cond"),
668+
version: None,
669+
};
670+
}
671+
646672
#[pyclass(noattr, name = "float_info")]
647673
#[derive(PyStructSequence)]
648674
pub(super) struct PyFloatInfo {
@@ -658,6 +684,7 @@ mod sys {
658684
radix: u32,
659685
rounds: i32,
660686
}
687+
661688
#[pyclass(with(PyStructSequence))]
662689
impl PyFloatInfo {
663690
const INFO: Self = PyFloatInfo {
@@ -713,6 +740,7 @@ mod sys {
713740
bits_per_digit: usize,
714741
sizeof_digit: usize,
715742
}
743+
716744
#[pyclass(with(PyStructSequence))]
717745
impl PyIntInfo {
718746
const INFO: Self = PyIntInfo {
@@ -765,6 +793,7 @@ mod sys {
765793
product_type: u8,
766794
platform_version: (u32, u32, u32),
767795
}
796+
768797
#[cfg(windows)]
769798
#[pyclass(with(PyStructSequence))]
770799
impl WindowsVersion {}

vm/src/stdlib/thread.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,21 @@ pub(crate) mod _thread {
1818
use std::{cell::RefCell, fmt, thread, time::Duration};
1919
use thread_local::ThreadLocal;
2020

21+
// PYTHREAD_NAME: show current thread name
22+
pub const PYTHREAD_NAME: Option<&str> = {
23+
cfg_if::cfg_if! {
24+
if #[cfg(windows)] {
25+
Some("nt")
26+
} else if #[cfg(unix)] {
27+
Some("pthread")
28+
} else if #[cfg(any(target_os = "solaris", target_os = "illumos"))] {
29+
Some("solaris")
30+
} else {
31+
None
32+
}
33+
}
34+
};
35+
2136
// TIMEOUT_MAX_IN_MICROSECONDS is a value in microseconds
2237
#[cfg(not(target_os = "windows"))]
2338
const TIMEOUT_MAX_IN_MICROSECONDS: i64 = i64::MAX / 1_000;

0 commit comments

Comments
 (0)