Skip to content

Commit

Permalink
Fix end_offset of JSONSyntaxError
Browse files Browse the repository at this point in the history
  • Loading branch information
nineteendo committed Feb 5, 2025
1 parent a5bbcc1 commit ea46c86
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
3 changes: 2 additions & 1 deletion docs/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ jsonyx 2.0.0 (unreleased)
- Added :func:`jsonyx.Manipulator`
- Changed error for big integers to :exc:`jsonyx.JSONSyntaxError`
- Fixed line comment detection
- Fixed typo in error message
- Fixed spelling of "commas" in error messages
- Fixed ``end_offset`` of :exc:`jsonyx.JSONSyntaxError`
- Improved documentation
- Made :class:`tuple` JSON serializable
- Merged ``item_separator`` and ``key_separator`` as ``separators`` for
Expand Down
10 changes: 7 additions & 3 deletions src/jsonyx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,16 @@ def format_syntax_error(exc: JSONSyntaxError) -> list[str]:
else:
column_range = f"{exc.colno:d}-{exc.end_colno:d}"

caret_indent: str = " " * (exc.offset - 1) # type: ignore
caret_selection: str = "^" * (exc.end_offset - exc.offset) # type: ignore
indent: str = " " * (exc.offset - 1) # type: ignore
if exc.end_lineno == exc.lineno:
selection: str = "^" * (exc.end_offset - exc.offset) # type: ignore
else:
selection = "^" * (len(exc.text) - exc.offset + 1) # type: ignore

return [
f' File "{exc.filename}", line {line_range}, column {column_range}\n',
f" {exc.text}\n",
f" {caret_indent}{caret_selection}\n",
f" {indent}{selection}\n",
f"{exc.__module__}.{type(exc).__qualname__}: {exc.msg}\n",
]

Expand Down
4 changes: 3 additions & 1 deletion src/jsonyx/_decoder.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""JSON decoder."""
# TODO(Nice Zombies): Fix end_offset in JSONSyntaxError
from __future__ import annotations

__all__: list[str] = ["Decoder", "JSONSyntaxError", "detect_encoding"]
Expand Down Expand Up @@ -175,6 +174,9 @@ def __init__(
doc.rfind("\n", 0, end), doc.rfind("\r", 0, end),
)
offset, text, end_offset = _get_err_context(doc, start, end)
if end_lineno != lineno:
end_offset = end_colno

if sys.version_info >= (3, 10):
super().__init__(
msg, (filename, lineno, offset, text, end_lineno, end_offset),
Expand Down
12 changes: 6 additions & 6 deletions src/jsonyx/test/test_syntax_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,23 +108,23 @@ def test_start_and_end_position(
# No newline
(9, "start-end", 0, 5, 1, "start-end", 6),
# ^^^^^ ^^^^^
(8, "current\nnext", 0, 12, 1, "current", 8),
(8, "current\nnext", 0, 12, 1, "current", 5),
# ^^^^^^^^^^^^^ ^^^^^^^
(8, "current\rnext", 0, 12, 1, "current", 8),
(8, "current\rnext", 0, 12, 1, "current", 5),
# ^^^^^^^^^^^^^ ^^^^^^^
(8, "current\r\nnext", 0, 13, 1, "current", 8),
(8, "current\r\nnext", 0, 13, 1, "current", 5),
# ^^^^^^^^^^^^^^^ ^^^^^^^
# Newline
(8, "current", 7, 7, 8, "current", 9),
# ^ ^
(8, "current", 7, 8, 8, "current", 9),
# ^ ^
(8, "current\nnext", 7, 12, 8, "current", 9),
(8, "current\nnext", 7, 12, 8, "current", 5),
# ^^^^^^ ^
(8, "current\rnext", 7, 12, 8, "current", 9),
(8, "current\rnext", 7, 12, 8, "current", 5),
# ^^^^^^ ^
(8, "current\r\nnext", 7, 13, 8, "current", 9),
(8, "current\r\nnext", 7, 13, 8, "current", 5),
# ^^^^^^^^ ^
# At least one character
Expand Down

0 comments on commit ea46c86

Please sign in to comment.