Skip to content

PYTHON-5032 - Use PyErr_GetRaisedException instead of deprecated PyEr… - #2795

Merged
NoahStapp merged 5 commits into
mongodb:masterfrom
NoahStapp:PYTHON-5032
May 7, 2026
Merged

NoahStapp merged 5 commits into
mongodb:masterfrom
NoahStapp:PYTHON-5032

Conversation

@NoahStapp

@NoahStapp NoahStapp commented May 6, 2026 •

Copy link
Copy Markdown
Contributor

…r_Fetch on Python 3.12+

PYTHON-5032

Changes in this PR

See title.

Test Plan

Existing BSON test suite.

Checklist

Checklist for Author

  • [ ] Did you update the changelog (if necessary)?
  • Is there test coverage?
  • [ ] Is any followup work tracked in a JIRA ticket? If so, add link(s).

Checklist for Reviewer

  • Does the title of the PR reference a JIRA Ticket?
  • Do you fully understand the implementation? (Would you be comfortable explaining how this code works to someone else?)
  • Is all relevant documentation (README or docstring) updated?

Copilot AI 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.

Pull request overview

This PR updates the BSON C-extension’s exception-handling paths to avoid deprecated PyErr_Fetch usage on Python 3.12+ by using the newer raised-exception APIs, while preserving traceback information when re-wrapping exceptions.

Changes:

  • Added a Python 3.12+ traceback-transfer helper and a shared helper to rewrap exceptions as InvalidBSON(...).
  • Updated datetime_from_millis error augmentation to use PyErr_GetRaisedException / PyErr_SetRaisedException on Python 3.12+.
  • Updated handle_invalid_doc_error to use the Python 3.12+ raised-exception APIs while reconstructing InvalidDocument with the document attached.

@NoahStapp
NoahStapp marked this pull request as ready for review May 6, 2026 15:52
@NoahStapp
NoahStapp requested a review from a team as a code owner May 6, 2026 15:52
@NoahStapp
NoahStapp requested review from Jibola, Copilot and sleepyStick and removed request for Jibola May 6, 2026 15:52
@codecov-commenter

codecov-commenter commented May 6, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@NoahStapp

Copy link
Copy Markdown
Contributor Author

Test failure is a known flaky test that should be resolved by PYTHON-5821.

Comment thread bson/_cbsonmodule.c Outdated
static int write_raw_doc(buffer_t buffer, PyObject* raw, PyObject* _raw);

/* 3.12 */
#if PY_VERSION_HEX >= 0x030C0000

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.

silly question, is there a reason we cant make 0x030C0000 a var like PYTHON_3_12_HEX or something? (i just arbitrarily made a var name so if you can think of a better one please use it)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch, that's a big readability improvement. Done.

sleepyStick
sleepyStick previously approved these changes May 7, 2026

@sleepyStick sleepyStick 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.

otherwise, lgtm (the comments in this file are super helpful!)

@NoahStapp
NoahStapp merged commit b6bac45 into mongodb:master May 7, 2026
95 of 100 checks passed
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.

4 participants