Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

fix: remove redundant error handling in unpacking functions#2

Open
methane wants to merge 2 commits into
mainmethane/msgpack-python:mainfrom
error-1methane/msgpack-python:error-1Copy head branch name to clipboard
Open

fix: remove redundant error handling in unpacking functions#2
methane wants to merge 2 commits into
mainmethane/msgpack-python:mainfrom
error-1methane/msgpack-python:error-1Copy head branch name to clipboard

Conversation

@methane

@methane methane commented Jun 24, 2026

Copy link
Copy Markdown
Owner

Note

Medium Risk
Changes core msgpack unpack control flow and exception propagation for all unpackb and streaming unpack paths; behavior should match for normal errors but edge cases around C/Python exception interaction deserve regression testing.

Overview
Unpack error handling is simplified so Python exceptions raised inside the C unpack path propagate through Cython instead of being double-handled in Python.

The execute_fn pointer is declared with except -1 (replacing except? -1), and the redundant PyErr_Occurred() / bare raise branches are removed from unpackb and Unpacker._unpack. Callback failures and explicit PyErr_* calls in the unpack template still surface as normal Python exceptions.

Internal unpack setup no longer uses a no-op unpack_callback_root; the root stack slot is initialized with NULL, and dead commented code around an alternate dynamic stack is removed.

Invalid internal parser state (unexpected cs or container type) now sets RuntimeError via PyErr_Format before failing, instead of falling through without a clear exception.

Reviewed by Cursor Bugbot for commit 38927d1. Bugbot is set up for automated code reviews on this repo. Configure here.

Summary by CodeRabbit

  • Bug Fixes
    • Improved unpacking error handling so failures produce clearer, more consistent messages rather than sometimes leaking pending exceptions.
    • Enhanced robustness across both single-value and streaming unpacking by tightening state and stack validation for malformed or unsupported input.
    • Strengthened internal checks to reliably fail when the unpacking state encounters unexpected conditions, improving predictability for invalid data.

@coderabbitai

coderabbitai Bot commented Jun 24, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The PR changes unpacker stack storage and failure reporting in the C templates, removes a dead inline callback, and updates the Cython wrapper to stop re-raising pending Python exceptions in two unpack paths.

Changes

msgpack Unpacker Error Handling and Stack Cleanup

Layer / File(s) Summary
C context struct, dead code removal, and RuntimeError on invalid states
msgpack/unpack.h, msgpack/unpack_template.h
unpack_context now embeds unpack_stack stack[MSGPACK_EMBED_STACK_SIZE]; unpack_init no longer calls the removed root callback; unpack_execute drops the unused stack_size local; invalid cs and c->ct branches now set PyExc_RuntimeError with PyErr_Format before _failed.
Cython exception spec and PyErr_Occurred() removal
msgpack/_unpacker.pyx
execute_fn changes from except? -1 to except -1, and both unpackb() and Unpacker._unpack() remove the PyErr_Occurred() re-raise branch so unexpected return codes now raise ValueError.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐇 I hop through stacks both neat and true,
And toss old callbacks out of view.
When states go odd, RuntimeError speaks,
While Cython trims its error peaks.
The unpacker hums, compact and bright,
With tidy paths and bouncier light.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: simplifying error handling in unpacking functions.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch error-1

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
msgpack/unpack_template.h (1)

301-303: 🎯 Functional Correctness | 🔵 Trivial | 💤 Low value

Good: invalid cs no longer fails silently. Setting a RuntimeError before goto _failed is exactly what makes the downstream Cython except -1 change safe (every -1 now carries an exception).

Minor: cs is unsigned int, so the conversion specifier should be %u rather than %d.

♻️ Optional
-                PyErr_Format(PyExc_RuntimeError, "Invalid state: %d", cs);
+                PyErr_Format(PyExc_RuntimeError, "Invalid state: %u", cs);
🤖 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 `@msgpack/unpack_template.h` around lines 301 - 303, The invalid-state error in
the unpack path should use the correct unsigned format specifier for `cs`;
update the `PyErr_Format` call in `unpack_template.h` so the `Invalid state`
message matches `unsigned int` rather than `%d`. Keep the existing
`RuntimeError` and `goto _failed` flow in the same state-check block so the
Cython `except -1` behavior remains intact.
🤖 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.

Nitpick comments:
In `@msgpack/unpack_template.h`:
- Around line 301-303: The invalid-state error in the unpack path should use the
correct unsigned format specifier for `cs`; update the `PyErr_Format` call in
`unpack_template.h` so the `Invalid state` message matches `unsigned int` rather
than `%d`. Keep the existing `RuntimeError` and `goto _failed` flow in the same
state-check block so the Cython `except -1` behavior remains intact.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 5817ca8b-6ec7-40cd-8d87-fa46d3f036a3

📥 Commits

Reviewing files that changed from the base of the PR and between 33fec11 and b310c08.

📒 Files selected for processing (3)
  • msgpack/_unpacker.pyx
  • msgpack/unpack.h
  • msgpack/unpack_template.h
💤 Files with no reviewable changes (1)
  • msgpack/unpack.h

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
msgpack/unpack_template.h (1)

301-302: 🎯 Functional Correctness | 🟡 Minor

Use %u for cs. cs is an unsigned int, so %d is the wrong format specifier in this error path.

🤖 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 `@msgpack/unpack_template.h` around lines 301 - 302, The error formatting in
the unpack error path uses the wrong specifier for cs, which is an unsigned int.
Update the PyErr_Format call in the unpack logic to use the correct unsigned
format for cs, and keep the change localized to the invalid-state handling in
the unpack template so the error message matches the variable type.
🤖 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.

Outside diff comments:
In `@msgpack/unpack_template.h`:
- Around line 301-302: The error formatting in the unpack error path uses the
wrong specifier for cs, which is an unsigned int. Update the PyErr_Format call
in the unpack logic to use the correct unsigned format for cs, and keep the
change localized to the invalid-state handling in the unpack template so the
error message matches the variable type.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: f1c629a3-b847-469c-b265-9e9e43170b5b

📥 Commits

Reviewing files that changed from the base of the PR and between b310c08 and 38927d1.

📒 Files selected for processing (1)
  • msgpack/unpack_template.h

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.

1 participant

Morty Proxy This is a proxified and sanitized view of the page, visit original site.