Skip to content

Conversation

cherez
Copy link
Contributor

@cherez cherez commented Sep 29, 2025

Following the template of #3590, adding weak reference support to freetype.Font.

This aims to bring freetype.Font to parity with pygame.Font with respect to weak references.

@cherez cherez requested a review from a team as a code owner September 29, 2025 19:57
Copy link
Contributor

coderabbitai bot commented Sep 29, 2025

📝 Walkthrough

Walkthrough

Adds weak-reference support to the FreeType Font type by introducing a weak reference list field in pgFontObject, wiring tp_weaklistoffset, initializing the field in the constructor, and clearing weak refs in deallocation. A new test verifies weakref clearing upon GC.

Changes

Cohort / File(s) Summary
Freetype Font weakref support (C impl)
src_c/_freetype.c, src_c/freetype.h
Add PyObject *weakreflist to pgFontObject; set .tp_weaklistoffset = offsetof(pgFontObject, weakreflist) on pgFont_Type; initialize weakreflist = NULL in constructor; call PyObject_ClearWeakRefs in dealloc when present.
Tests
test/freetype_test.py
Extend GC test to create a font, take a weakref, delete strong ref, force GC twice, and assert weakref is cleared.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor Py as Python code
  participant FT as ft.Font
  participant Obj as pgFontObject
  participant WR as weakref.ref
  participant GC as PyGC
  Py->>FT: create Font(path)
  FT-->>Obj: allocate + init (weakreflist=NULL)
  Py->>WR: weakref.ref(Obj)
  Py->>Py: del strong ref to Obj
  Py->>GC: gc.collect() x2
  GC->>Obj: dealloc()
  note right of Obj: PyObject_ClearWeakRefs(self)
  Obj-->>WR: invalidate target
  WR-->>Py: returns None on call/deref
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested labels

freetype

Suggested reviewers

  • ankith26

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Title Check ✅ Passed The PR title "Add weak reference support to freetype.Font" directly and accurately describes the main change in the pull request. The changeset modifies the pgFontObject structure, type definition, and lifecycle methods specifically to enable weak reference support for the freetype.Font type, and adds corresponding test coverage. The title is concise, clear, and fully captures the primary purpose of the changes without being overly broad or including unnecessary details.
Description Check ✅ Passed The PR description is clearly related to the changeset. It explains that the changes follow the template of PR #3590 and add weak reference support to freetype.Font to achieve parity with pygame.Font. This directly corresponds to the actual changes made in the code, which implement weak reference infrastructure for the Font type. While brief, the description provides meaningful context about the motivation and approach for the changes.
✨ Finishing touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d6d41c2 and f638639.

📒 Files selected for processing (3)
  • src_c/_freetype.c (3 hunks)
  • src_c/freetype.h (1 hunks)
  • test/freetype_test.py (1 hunks)
🧰 Additional context used
🪛 Ruff (0.13.1)
test/freetype_test.py

1611-1611: Loop control variable i not used within loop body

(B007)

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (20)
  • GitHub Check: x86_64
  • GitHub Check: x86
  • GitHub Check: x86_64
  • GitHub Check: i686
  • GitHub Check: AMD64
  • GitHub Check: aarch64
  • GitHub Check: build (ubuntu-22.04)
  • GitHub Check: Debian (Bookworm - 12) [armv7]
  • GitHub Check: build (macos-14)
  • GitHub Check: Debian (Bookworm - 12) [ppc64le]
  • GitHub Check: Debian (Bookworm - 12) [armv6]
  • GitHub Check: Debian (Bookworm - 12) [s390x]
  • GitHub Check: build (ubuntu-24.04)
  • GitHub Check: msys2 (clang64, clang-x86_64)
  • GitHub Check: msys2 (mingw64, x86_64)
  • GitHub Check: msys2 (ucrt64, ucrt-x86_64)
  • GitHub Check: dev-check
  • GitHub Check: debug_coverage (ubuntu-24.04, 3.14.0rc1)
  • GitHub Check: debug_coverage (ubuntu-24.04, 3.13.5)
  • GitHub Check: debug_coverage (ubuntu-24.04, 3.9.23)
🔇 Additional comments (5)
test/freetype_test.py (1)

1608-1613: LGTM! Weak reference test correctly validates Font GC behavior.

The test properly verifies that Font objects support weak references and are collected when no strong references remain. The pattern matches the existing GC test idiom in this file.

Note: The static analysis hint about unused loop variable i on line 1611 is a false positive—this is a standard Python idiom for forcing multiple GC cycles, especially needed for PyPy compatibility (see lines 1602-1603 for the same pattern).

src_c/freetype.h (1)

106-106: LGTM! Weakref list field correctly added to Font object.

The PyObject *weakreflist field is properly placed in the pgFontObject struct to support weak references, following standard Python C API conventions.

src_c/_freetype.c (3)

635-635: LGTM! Type offset correctly configured for weak reference support.

The tp_weaklistoffset field is properly set using offsetof(pgFontObject, weakreflist), enabling Python's runtime to locate and manage the weak reference list.


678-678: LGTM! Weakref list correctly initialized to NULL.

The weakreflist field is properly initialized to NULL in the Font object constructor, ensuring a clean initial state.


694-696: LGTM! Weak references properly cleared during deallocation.

The code correctly clears weak references before freeing the Font object, following Python C API requirements. The NULL check is good defensive programming, and the placement (after resource cleanup but before tp_free) is correct.


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
🧪 Early access (Sonnet 4.5): enabled

We are currently testing the Sonnet 4.5 model, which is expected to improve code review quality. However, this model may lead to increased noise levels in the review comments. Please disable the early access features if the noise level causes any inconvenience.

Note:

  • Public repositories are always opted into early access features.
  • You can enable or disable early access features from the CodeRabbit UI or by updating the CodeRabbit configuration file.

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

Copy link
Member

@MightyJosip MightyJosip left a comment

Choose a reason for hiding this comment

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

Same as your last commit. Thank you. +1

Copy link
Member

@ankith26 ankith26 left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for contributing! 🎉

@ankith26 ankith26 added this to the 2.5.6 milestone Oct 1, 2025
@ankith26 ankith26 merged commit f572e93 into pygame-community:main Oct 1, 2025
28 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.

3 participants