Skip to content

Start using Py_NewRef in a few modules - #3786

Merged
oddbookworm merged 4 commits into
pygame-community:mainfrom
Starbuck5:newref
May 9, 2026
Merged

oddbookworm merged 4 commits into
pygame-community:mainfrom
Starbuck5:newref

Conversation

@Starbuck5

Copy link
Copy Markdown
Member

When looking at ref counting #3783, I was surprised to see we don't use Py_NewRef at all throughout our codebase.

Docs: https://docs.python.org/3/c-api/refcounting.html#c.Py_NewRef

Instead of assigning something and then incref-ing it, we can use newref inline to return a new reference to the object. Which is the same thing, just more concise. I checked the generated assembly with my add_global_arguments('/FAcsu', language: 'c') trick for MSVC, I did not see any meaningful change in the generated output.

It seems that this is the preferred way of doing things in CPython internally now: python/cpython#99300

@Starbuck5
Starbuck5 requested a review from a team as a code owner May 3, 2026 17:57
@Starbuck5 Starbuck5 added the Code quality/robustness Code quality and resilience to changes label May 3, 2026
@coderabbitai

coderabbitai Bot commented May 3, 2026 •

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 94d2bc57-ff27-4ccc-9fe0-700c70f61000

📥 Commits

Reviewing files that changed from the base of the PR and between 05b1265 and 0dece60.

📒 Files selected for processing (4)
  • src_c/color.c
  • src_c/math.c
  • src_c/surface.c
  • src_c/window.c

📝 Walkthrough

Walkthrough

This PR modernizes reference counting in four C extension modules by replacing manual Py_INCREF(...); return value; patterns with the Py_NewRef(...) macro. Changes are applied to return paths for Py_NotImplemented, owned Python objects in arithmetic/comparison operators, buffer view initialization, and function returns across color, math, surface, and window modules.

Changes

Reference Counting Modernization

Layer / File(s) Summary
Reference Counting Pattern Updates
src_c/color.c, src_c/math.c, src_c/surface.c, src_c/window.c
Replaced manual Py_INCREF(obj); return obj; with return Py_NewRef(obj); throughout operator fallbacks (_color_add, _color_sub, _color_mul, _color_div, _color_mod, _color_richcompare, vector_generic_math, vector_richcompare, vector_elementwiseproxy_richcompare), object returns (get_grabbed_window, window_get_surface, window_from_display_module, surf_get_locks, surf_get_parent, surf_get_abs_parent, surf_subsurface, surf_premul_alpha_ip, math_clamp), and buffer view assignments (_get_buffer_0D, _get_buffer_1D, _get_buffer_2D, _get_buffer_3D, _get_buffer_colorplane).

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 25.93% 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
Title check ✅ Passed The title accurately summarizes the main change—adopting Py_NewRef across multiple modules for cleaner reference counting.
Description check ✅ Passed The description clearly explains the motivation and rationale for replacing Py_INCREF + assignment patterns with Py_NewRef, including references to Python documentation and CPython practices.
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 unit tests (beta)
  • Create PR with unit tests

Tip

💬 Introducing Slack Agent: The best way for teams to turn conversations into code.

Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get started


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
Review rate limit: 7/8 reviews remaining, refill in 7 minutes and 30 seconds.

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

DickerDackel added a commit to DickerDackel/pygame-ce that referenced this pull request May 4, 2026

@ankith26 ankith26 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, thanks 🎉

@oddbookworm oddbookworm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM!

@oddbookworm
oddbookworm merged commit 2f282d0 into pygame-community:main May 9, 2026
34 checks passed
@oddbookworm oddbookworm added this to the 2.5.8 milestone May 9, 2026
@Starbuck5
Starbuck5 deleted the newref branch May 10, 2026 04:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Code quality/robustness Code quality and resilience to changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants