FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Honor sys.int_max_str_digits in native json.loads by Federicorao · Pull Request #8087 · RustPython/RustPython · GitHub

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

Filter by extension

Filter by extension .py  (1) .rs  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
12 changes: 8 additions & 4 deletions crates/stdlib/src/json.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

Back | FazBrowse Home | New Git URL