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

Honor sys.int_max_str_digits in native json.loads#8087

Open
Federicorao wants to merge 2 commits into
RustPython:mainfrom
Federicorao:codex/issue-8051-1781265710
Open

Honor sys.int_max_str_digits in native json.loads#8087
Federicorao wants to merge 2 commits into
RustPython:mainfrom
Federicorao:codex/issue-8051-1781265710

Conversation

@Federicorao

@Federicorao Federicorao commented Jun 12, 2026

Copy link
Copy Markdown

Refs #8051.

Routes the native _json scanner integer path through rustpython_common::int::bytes_to_int with the live vm.state.int_max_str_digits value, matching the int() conversion path and the pure-Python JSON decoder. Adds snippet coverage for the boundary where an integer with exactly the configured digit limit parses, while one digit over the limit raises ValueError.

Local verification:

  • python3.14 -m py_compile extra_tests/snippets/stdlib_json.py
  • python3.14 extra_tests/snippets/stdlib_json.py

Could not run local Rust checks because cargo is not installed in this environment; CI will validate the Rust crate build and formatting.

AI assistance disclosure: OpenAI GPT-5 was used to inspect the issue and review feedback, draft the patch, and run local verification; the changes are disclosed in the commit with an Assisted-by trailer.

Summary by CodeRabbit

  • Bug Fixes

    • JSON integer parsing now enforces the VM's integer-string length limit, returning an error for overly long integer literals while preserving correct parsing for valid integers.
  • Tests

    • Added a regression test that verifies the length limit is enforced and that valid integer strings continue to parse successfully.

@coderabbitai

coderabbitai Bot commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

@ShaharNaveh ShaharNaveh left a comment

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.

Hi, thanks for the PR!

Can you please read our AI policy, and check the box you've read it in your PR comment (I've edited your comment)


Rust fix required but cannot be provided without original file content.

Not sure what you mean by that, you can create your own MRE and test it against that. ATM we cannot merge your test as it would only make the CI fail, it doesn't fix anything unless I'm missing something

Comment thread extra_tests/snippets/stdlib_json.py Outdated
import sys
_orig_limit = sys.get_int_max_str_digits()
try:
sys.set_int_max_str_digits(100)

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.

Also, this wouldn't work as the minimum value for it is 600

Route native _json integer parsing through bytes_to_int with the live VM digit limit, matching int() and the pure-Python JSON path. Add snippet coverage for the boundary.

Assisted-by: OpenAI GPT-5:gpt-5
@Federicorao Federicorao force-pushed the codex/issue-8051-1781265710 branch from 0902b83 to 16679d3 Compare June 12, 2026 13:45
@Federicorao Federicorao changed the title Add test to verify that json.loads honors sys.int_max_str_digits; Rust fix required but cannot be provided without origi Honor sys.int_max_str_digits in native json.loads Jun 12, 2026

@coderabbitai coderabbitai Bot left a comment

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.

Actionable comments posted: 1

🤖 Prompt for all review comments with 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.

Inline comments:
In `@extra_tests/snippets/stdlib_json.py`:
- 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.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

Run ID: 5e75a5c2-6d28-44aa-adec-79c58ec2a330

📥 Commits

Reviewing files that changed from the base of the PR and between 0902b83 and 16679d3.

📒 Files selected for processing (2)
  • crates/stdlib/src/json.rs
  • extra_tests/snippets/stdlib_json.py

Comment thread extra_tests/snippets/stdlib_json.py Outdated
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

@fanninpm

Copy link
Copy Markdown
Contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants