Skip to content

Commit ce546f6

Browse files
authored
feat(sys): add sys.is_finalizing() (RustPython#3776)
1 parent d06950e commit ce546f6

File tree

4 files changed

+13
-1
lines changed

4 files changed

+13
-1
lines changed

Lib/test/test_sys.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -892,7 +892,7 @@ def test_getallocatedblocks(self):
892892
c = sys.getallocatedblocks()
893893
self.assertIn(c, range(b - 50, b + 50))
894894

895-
# TODO: RUSTPYTHON, AttributeError: module 'sys' has no attribute 'is_finalizing'
895+
# TODO: RUSTPYTHON, AtExit.__del__ is not invoked because module destruction is missing.
896896
@unittest.expectedFailure
897897
def test_is_finalizing(self):
898898
self.assertIs(sys.is_finalizing(), False)

vm/src/stdlib/sys.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ mod sys {
1919
AsObject, PyObjectRef, PyRef, PyRefExact, PyResult,
2020
};
2121
use num_traits::ToPrimitive;
22+
use std::sync::atomic::Ordering;
2223
use std::{env, mem, path};
2324

2425
// not the same as CPython (e.g. rust's x86_x64-unknown-linux-gnu is just x86_64-linux-gnu)
@@ -521,6 +522,11 @@ mod sys {
521522
PyIntInfo::INFO.into_struct_sequence(vm)
522523
}
523524

525+
#[pyfunction]
526+
fn is_finalizing(vm: &VirtualMachine) -> bool {
527+
vm.state.finalizing.load(Ordering::Acquire)
528+
}
529+
524530
#[pyfunction]
525531
fn setprofile(profilefunc: PyObjectRef, vm: &VirtualMachine) {
526532
vm.profile_func.replace(profilefunc);

vm/src/vm/interpreter.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::{
33
stdlib::{atexit, sys},
44
PyResult,
55
};
6+
use std::sync::atomic::Ordering;
67

78
/// The general interface for the VM
89
///
@@ -74,6 +75,8 @@ impl Interpreter {
7475

7576
atexit::_run_exitfuncs(vm);
7677

78+
vm.state.finalizing.store(true, Ordering::Release);
79+
7780
flush_std(vm);
7881

7982
exit_code

vm/src/vm/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ use crate::{
3434
signal, stdlib, AsObject, PyObject, PyObjectRef, PyPayload, PyRef, PyResult,
3535
};
3636
use crossbeam_utils::atomic::AtomicCell;
37+
use std::sync::atomic::AtomicBool;
3738
use std::{
3839
borrow::Cow,
3940
cell::{Cell, Ref, RefCell},
@@ -86,6 +87,7 @@ pub struct PyGlobalState {
8687
pub hash_secret: HashSecret,
8788
pub atexit_funcs: PyMutex<Vec<(PyObjectRef, FuncArgs)>>,
8889
pub codec_registry: CodecsRegistry,
90+
pub finalizing: AtomicBool,
8991
}
9092

9193
pub fn process_hash_secret_seed() -> u32 {
@@ -158,6 +160,7 @@ impl VirtualMachine {
158160
hash_secret,
159161
atexit_funcs: PyMutex::default(),
160162
codec_registry,
163+
finalizing: AtomicBool::new(false),
161164
}),
162165
initialized: false,
163166
recursion_depth: Cell::new(0),

0 commit comments

Comments
 (0)