Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions crates/stdlib/src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ mod _json {
builtins::{PyBaseExceptionRef, PyStrRef, PyType},
convert::ToPyResult,
function::{IntoFuncArgs, OptionalArg},
protocol::PyIterReturn,
protocol::{handle_bytes_to_int_err, PyIterReturn},
types::{Callable, Constructor},
};
use core::str::FromStr;
use malachite_bigint::BigInt;
use rustpython_common::wtf8::Wtf8Buf;
use rustpython_common::{int::bytes_to_int, wtf8::Wtf8Buf};
use std::collections::HashMap;

/// Skip JSON whitespace characters (space, tab, newline, carriage return).
Expand Down Expand Up @@ -243,7 +242,12 @@ mod _json {
} else if let Some(ref parse_int) = self.parse_int {
parse_int.call((buf,), vm)
} else {
Ok(vm.new_pyobj(BigInt::from_str(buf).unwrap()))
bytes_to_int(buf.as_bytes(), 10, vm.state.int_max_str_digits.load())
.map(|value| vm.new_pyobj(value))
.map_err(|e| {
let obj = vm.ctx.new_str(buf);
handle_bytes_to_int_err(e, obj.as_object(), vm)
})
};
Some((ret, buf.len()))
}
Expand Down
13 changes: 13 additions & 0 deletions extra_tests/snippets/stdlib_json.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import sys
from io import BytesIO, StringIO

from testutils import assert_raises
Expand Down Expand Up @@ -261,3 +262,15 @@ def assert_no_native_stack_overflow(func):
assert e.pos == 5, f"expected pos=5, got {e.pos}"
else:
raise AssertionError("expected JSONDecodeError")

# Test that json.loads honors sys.int_max_str_digits
_min_limit = sys.int_info.str_digits_check_threshold
_orig_limit = sys.get_int_max_str_digits()
try:
sys.set_int_max_str_digits(_min_limit)
with assert_raises(ValueError):
json.loads("1" * (_min_limit + 1))
assert json.loads("1" * _min_limit) == int("1" * _min_limit)
assert json.loads('42') == 42
finally:
sys.set_int_max_str_digits(_orig_limit)
Loading