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()))
let value = bytes_to_int(buf.as_bytes(), 10, vm.state.int_max_str_digits.load())
.map_err(|e| {
let obj = vm.ctx.new_str(buf);
handle_bytes_to_int_err(e, obj.as_object(), vm)
})?;
Ok(vm.new_pyobj(value))
};
Some((ret, buf.len()))
}
Expand Down
14 changes: 14 additions & 0 deletions extra_tests/snippets/stdlib_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,17 @@ 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
import sys

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
python - <<'PY'
import ast
from pathlib import Path

path = Path("extra_tests/snippets/stdlib_json.py")
tree = ast.parse(path.read_text(), filename=str(path))

seen_non_import = False
late_imports = []
for node in tree.body:
    if (
        isinstance(node, ast.Expr)
        and isinstance(getattr(node, "value", None), ast.Constant)
        and isinstance(node.value.value, str)
        and not seen_non_import
    ):
        continue
    if isinstance(node, (ast.Import, ast.ImportFrom)):
        if seen_non_import:
            late_imports.append(node.lineno)
    else:
        seen_non_import = True

print({"late_import_lines": late_imports})
PY

Repository: RustPython/RustPython

Length of output: 94


Move import sys into the top-level import section (Ruff E402)

extra_tests/snippets/stdlib_json.py has import sys on line 266, after non-import top-level statements; this layout can trigger Ruff E402. Move import sys up into the module’s import block.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@extra_tests/snippets/stdlib_json.py` at line 266, The import statement
"import sys" is located after top-level code causing Ruff E402; remove the
"import sys" from its current spot and add it to the module's top-level import
block alongside other stdlib imports (i.e., the file's initial import section)
so that "sys" is imported before any non-import top-level statements.

Source: Coding guidelines


_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