Skip to content

Commit 1cdc5d3

Browse files
Add get_int_max_str_digits and set_int_max_str_digits in sys. (RustPython#5014)
--------- Co-authored-by: DimitrisJim <d.f.hilliard@gmail.com>
1 parent 3d2c519 commit 1cdc5d3

File tree

5 files changed

+35
-11
lines changed

5 files changed

+35
-11
lines changed

.github/workflows/ci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ jobs:
245245
- uses: dtolnay/rust-toolchain@stable
246246
- uses: actions/setup-python@v4
247247
with:
248-
python-version: "3.11"
248+
python-version: "3.11.4"
249249
- name: Set up the Windows environment
250250
shell: bash
251251
run: |

extra_tests/snippets/stdlib_sys.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,17 @@ def recursive_call(n):
9292
# assert winver.major == winver.platform_version[0]
9393
# assert winver.minor == winver.platform_version[1]
9494
# assert winver.build == winver.platform_version[2]
95+
96+
# test int_max_str_digits getter and setter
97+
98+
assert sys.get_int_max_str_digits() == 4300
99+
sys.set_int_max_str_digits(640)
100+
assert sys.get_int_max_str_digits() == 640
101+
sys.set_int_max_str_digits(0)
102+
assert sys.get_int_max_str_digits() == 0
103+
104+
with assert_raises(ValueError):
105+
sys.set_int_max_str_digits(1)
106+
107+
sys.set_int_max_str_digits(1000)
108+
assert sys.get_int_max_str_digits() == 1000
Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,9 @@
11
import os
2-
import platform
32

43
from testutils import assert_raises
54

65
dir_path = os.path.dirname(os.path.realpath(__file__))
76

8-
# TODO: RUSTPYTHON. At some point snippets will fail and it will look confusing
9-
# and out of the blue. This is going to be the cause and it's going to happen when
10-
# the github worker for MacOS starts using Python 3.11.4.
11-
if platform.python_implementation() == "CPython" and platform.system() == 'Darwin':
12-
expectedException = ValueError
13-
else:
14-
expectedException = SyntaxError
15-
16-
with assert_raises(expectedException):
7+
with assert_raises(SyntaxError):
178
with open(os.path.join(dir_path , "non_utf8.txt")) as f:
189
eval(f.read())

vm/src/stdlib/sys.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,23 @@ mod sys {
616616
PyIntInfo::INFO.into_struct_sequence(vm)
617617
}
618618

619+
#[pyfunction]
620+
fn get_int_max_str_digits(vm: &VirtualMachine) -> usize {
621+
vm.state.int_max_str_digits.load()
622+
}
623+
624+
#[pyfunction]
625+
fn set_int_max_str_digits(maxdigits: usize, vm: &VirtualMachine) -> PyResult<()> {
626+
let threshold = PyIntInfo::INFO.str_digits_check_threshold;
627+
if maxdigits == 0 || maxdigits >= threshold {
628+
vm.state.int_max_str_digits.store(maxdigits);
629+
Ok(())
630+
} else {
631+
let error = format!("maxdigits must be 0 or larger than {:?}", threshold);
632+
Err(vm.new_value_error(error))
633+
}
634+
}
635+
619636
#[pyfunction]
620637
fn is_finalizing(vm: &VirtualMachine) -> bool {
621638
vm.state.finalizing.load(Ordering::Acquire)

vm/src/vm/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ pub struct PyGlobalState {
100100
pub before_forkers: PyMutex<Vec<PyObjectRef>>,
101101
pub after_forkers_child: PyMutex<Vec<PyObjectRef>>,
102102
pub after_forkers_parent: PyMutex<Vec<PyObjectRef>>,
103+
pub int_max_str_digits: AtomicCell<usize>,
103104
}
104105

105106
pub fn process_hash_secret_seed() -> u32 {
@@ -180,6 +181,7 @@ impl VirtualMachine {
180181
before_forkers: PyMutex::default(),
181182
after_forkers_child: PyMutex::default(),
182183
after_forkers_parent: PyMutex::default(),
184+
int_max_str_digits: AtomicCell::new(4300),
183185
}),
184186
initialized: false,
185187
recursion_depth: Cell::new(0),

0 commit comments

Comments
 (0)