Skip to content

Conversation

ankith26
Copy link
Member

@ankith26 ankith26 commented Sep 1, 2025

  • Add deprecation warning for calling set_relative_mode without set_mode on SDL2, on SDL3 it errors.
  • Use the PG_IntersectFRectAndLine compat code on SDL3 for now, for complete SDL2 FRect compat (this can be changed in the future)

@ankith26 ankith26 requested a review from a team as a code owner September 1, 2025 14:22
Copy link
Contributor

coderabbitai bot commented Sep 1, 2025

📝 Walkthrough

Walkthrough

Introduces SDL-version-aware behavior across input and rect compatibility: adds deprecation note in a stub, updates keycode mapping for AC Back depending on SDL version, revises mouse relative-mode and visibility handling with explicit NULL-window logic, broadens rect intersection compatibility for SDL3, and adjusts tests accordingly.

Changes

Cohort / File(s) Summary of changes
Docs/stubs update
buildconfig/stubs/pygame/mouse.pyi
Added versionchanged note (2.5.6) in set_relative_mode docstring about calling before display.set_mode being deprecated and potentially erroring later. No API change.
Key mapping (SDL-version aware) + tests
src_c/key.c, test/key_test.py
key.c: Conditional AC Back keycode mapping for SDL < 3.0.0 vs >= 3.0.0. Tests: add SKIPPED_KEYS_NEW for "K_MODE", ensure clean init, and skip specific assertion for new key.
Mouse relative mode/visibility behavior
src_c/mouse.c, test/mouse_test.py
mouse.c: SDL3 path now errors if no window on set_relative_mode; SDL2 path warns but proceeds. Adjust get_visible to respect cursor visibility even with NULL window. Tests add version-gated expectations and ensure display is initialized where required.
Rect intersection SDL3 compatibility
src_c/pgcompat_rect.c, src_c/pgcompat_rect.h
Extend compatibility guards so PG_IntersectFRectAndLine uses project implementation for SDL < 2.0.22 and SDL >= 3.0.0; keep alias only for SDL 2.0.22..<3.0.0. Updated comments and conditions.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor Py as Python
  participant PG as pygame.mouse (C)
  participant SDL as SDL

  rect rgba(200,230,255,0.3)
    note right of Py: set_relative_mode(mode)
    Py->>PG: mouse_set_relative_mode(mode)
    alt SDL >= 3.0.0
      PG->>PG: win = default window
      alt win is NULL
        PG-->>Py: raise SDLError ("display.set_mode not called")
      else win present
        PG->>SDL: SDL_SetWindowRelativeMouseMode(win, mode)
        SDL-->>PG: status / error
        PG-->>Py: return / raise on error
      end
    else SDL < 3.0.0
      PG->>PG: win may be NULL
      alt win is NULL
        PG-->>Py: DeprecationWarning
      end
      PG->>SDL: SDL_SetRelativeMouseMode(mode)
      SDL-->>PG: status / error
      PG-->>Py: return / raise on error
    end
  end

  rect rgba(230,255,230,0.3)
    note right of Py: get_visible()
    Py->>PG: mouse_get_visible()
    PG->>PG: visible = PG_CursorVisible()
    PG->>PG: win = default window (may be NULL)
    PG->>SDL: SDL_GetWindowRelativeMouseMode(win or NULL)
    SDL-->>PG: false if NULL
    PG-->>Py: visible && !relative_mode
  end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Suggested labels

sdl3

Suggested reviewers

  • Starbuck5
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch ankith26-sdl3-4

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

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

Other keywords and placeholders

  • Add @coderabbitai ignore or @coderabbit ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@ankith26
Copy link
Member Author

ankith26 commented Sep 1, 2025

@coderabbitai review

Copy link
Contributor

coderabbitai bot commented Sep 1, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (8)
buildconfig/stubs/pygame/mouse.pyi (1)

295-296: Clarify SDL2 vs SDL3 behavior in versionchanged note.

Docs say “deprecated and may error in the future” but runtime already errors on SDL3 and warns on SDL2. Suggest making this explicit.

-    .. versionchanged:: 2.5.6 calling this function before calling
-        :func:`pygame.display.set_mode` is deprecated and may error in the future.
+    .. versionchanged:: 2.5.6
+        On SDL 2.x, calling this function before :func:`pygame.display.set_mode`
+        emits a ``DeprecationWarning``. On SDL 3.x, it raises ``pygame.error``.
test/key_test.py (2)

11-11: Name this to reflect intent and make it immutable.

SKIPPED_KEYS_NEW doesn’t convey scope; also make it a frozenset.

-SKIPPED_KEYS_NEW = {"K_MODE"}
+# Keys whose alt-name roundtrip (use_compat=False) is layout/SDL-version dependent.
+SKIPPED_ALTNAME_ROUNDTRIP = frozenset({"K_MODE"})

And update references accordingly (see below).

-            if const_name not in SKIPPED_KEYS_NEW:
+            if const_name not in SKIPPED_ALTNAME_ROUNDTRIP:
                 self.assertEqual(pygame.key.key_code(alt_name), const_val)

173-175: Explain the quit-before-init pattern.

Add a short comment so future readers know we’re ensuring a clean slate before init.

-        pygame.quit()
+        # Ensure a fully clean SDL/Pygame state before initializing for this suite.
+        pygame.quit()
src_c/key.c (1)

392-396: Version-gated K_AC_BACK mapping looks correct.

Using SDL_VERSION_ATLEAST to select 1073742106 (SDL3) vs 1073742094 (SDL2) preserves stable name while tracking keycode changes.

Consider adding a brief comment citing the SDL3 remap for future maintainers:

 #if SDL_VERSION_ATLEAST(3, 0, 0)
-    {1073742106, "AC Back"}, /* K_AC_BACK */
+    {1073742106, "AC Back"}, /* K_AC_BACK (SDL3 moved from 2094 -> 2106) */
 #else
     {1073742094, "AC Back"}, /* K_AC_BACK */
 #endif
src_c/mouse.c (1)

327-331: Changed no-window semantics for get_visible; confirm intended.

Now returns PG_CursorVisible() when no window (SDL3 path), whereas older logic returned 0. Looks sensible, but please confirm tests cover get_visible without a window.

Consider an inline note to document this behavior:

-    /* If win is NULL, SDL_GetWindowRelativeMouseMode returns false */
+    /* If win is NULL, SDL_GetWindowRelativeMouseMode(NULL) returns false,
+       so result reduces to PG_CursorVisible() (works pre-display.set_mode). */
test/mouse_test.py (1)

387-395: Version-gated assertions look right; consider subTest for clearer failures.

Wrapping the loop body in subTest(val=val) improves diagnostics if one of the two cases fails without changing behavior.

-        for val in (True, False):
-            if pygame.version.SDL >= (3, 0, 0):
-                with self.assertRaises(pygame.error):
-                    pygame.mouse.set_relative_mode(val)
-            else:
-                with self.assertWarns(DeprecationWarning):
-                    pygame.mouse.set_relative_mode(val)
+        for val in (True, False):
+            with self.subTest(val=val):
+                if pygame.version.SDL >= (3, 0, 0):
+                    with self.assertRaises(pygame.error):
+                        pygame.mouse.set_relative_mode(val)
+                else:
+                    with self.assertWarns(DeprecationWarning):
+                        pygame.mouse.set_relative_mode(val)
src_c/pgcompat_rect.h (1)

11-18: Broadened guard to include SDL3 is correct; define a shared feature macro to avoid drift with .c.

Use a single PG_NEEDS_FRECT_COMPAT macro to centralize the condition used here and in pgcompat_rect.c.

-/* SDL 2.0.22 provides some utility functions for FRects */
-#if !(SDL_VERSION_ATLEAST(2, 0, 22)) || SDL_VERSION_ATLEAST(3, 0, 0)
+/* SDL 2.0.22 provides some utility functions for FRects.
+ * For SDL < 2.0.22 and SDL >= 3.0.0 we provide our own for SDL2-compat. */
+#if !(SDL_VERSION_ATLEAST(2, 0, 22)) || SDL_VERSION_ATLEAST(3, 0, 0)
+#define PG_NEEDS_FRECT_COMPAT 1
+#endif
+
+#ifdef PG_NEEDS_FRECT_COMPAT
 SDL_bool
 PG_IntersectFRectAndLine(SDL_FRect *rect, float *X1, float *Y1, float *X2,
                          float *Y2);
 #else
 #define PG_IntersectFRectAndLine SDL_IntersectFRectAndLine
-#endif /* !(SDL_VERSION_ATLEAST(2, 0, 22)) || SDL_VERSION_ATLEAST(3, 0, 0) */
+#endif /* PG_NEEDS_FRECT_COMPAT */
src_c/pgcompat_rect.c (1)

4-9: Good rationale on SDL3 edge semantics; switch to the shared guard to keep h/c in sync.

Adopt PG_NEEDS_FRECT_COMPAT (defined in the header) here to prevent conditional mismatches between files.

-/* SDL3 changed how the edges are handled. Previously right/bottom edges were
+/* SDL3 changed how the edges are handled. Previously right/bottom edges were
  * considered excluded from the FRect but now they aren't.
  * For now do SDL2 compat, but consider changing this in the future.
  * See: https://github.com/pygame-community/pygame-ce/issues/3571 */
-#if !(SDL_VERSION_ATLEAST(2, 0, 22)) || SDL_VERSION_ATLEAST(3, 0, 0)
+#ifdef PG_NEEDS_FRECT_COMPAT
 ...
-#endif /* !(SDL_VERSION_ATLEAST(2, 0, 22)) || SDL_VERSION_ATLEAST(3, 0, 0) */
+#endif /* PG_NEEDS_FRECT_COMPAT */

Optional: add a focused unit test that hits lines exactly on the right/bottom edges to lock the intended SDL2 semantics while SDL3 fallback is active.

Also applies to: 183-183

📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between c7e9c15 and 379f5b6.

📒 Files selected for processing (7)
  • buildconfig/stubs/pygame/mouse.pyi (1 hunks)
  • src_c/key.c (1 hunks)
  • src_c/mouse.c (3 hunks)
  • src_c/pgcompat_rect.c (2 hunks)
  • src_c/pgcompat_rect.h (1 hunks)
  • test/key_test.py (3 hunks)
  • test/mouse_test.py (2 hunks)
🧰 Additional context used
🧠 Learnings (3)
📓 Common learnings
Learnt from: Starbuck5
PR: pygame-community/pygame-ce#3573
File: src_c/_camera.c:129-131
Timestamp: 2025-08-30T21:11:00.196Z
Learning: When doing SDL2 to SDL3 compatibility changes, Starbuck5 prefers to maintain exact existing behaviors even when other improvements could be made, focusing solely on the compatibility aspect rather than mixing in behavioral fixes.
📚 Learning: 2025-08-30T21:11:00.196Z
Learnt from: Starbuck5
PR: pygame-community/pygame-ce#3573
File: src_c/_camera.c:129-131
Timestamp: 2025-08-30T21:11:00.196Z
Learning: When doing SDL2 to SDL3 compatibility changes, Starbuck5 prefers to maintain exact existing behaviors even when other improvements could be made, focusing solely on the compatibility aspect rather than mixing in behavioral fixes.

Applied to files:

  • src_c/pgcompat_rect.c
  • src_c/pgcompat_rect.h
📚 Learning: 2025-08-14T02:59:43.863Z
Learnt from: oddbookworm
PR: pygame-community/pygame-ce#0
File: :0-0
Timestamp: 2025-08-14T02:59:43.863Z
Learning: In pygame-ce's C unit testing framework using Unity, the RUN_TEST_PG_INTERNAL macro automatically handles setUp() and tearDown() calls for each test execution, providing automatic test isolation without requiring explicit reset calls between tests.

Applied to files:

  • test/key_test.py
🔇 Additional comments (3)
test/key_test.py (1)

291-292: Good, targeted skip for layout-dependent key.

Conditionally skipping only the alt-name roundtrip for K_MODE keeps coverage strong without fragile assertions.

src_c/mouse.c (1)

623-645: SDL3: error on missing window; SDL2: deprecate-and-continue — LGTM.

The split matches the docs intent and provides clear guidance via DeprecationWarning on SDL2.

Ensure test/mouse_test.py asserts:

  • SDL3: set_relative_mode(True) without display raises pygame.error.
  • SDL2: same call emits DeprecationWarning and succeeds.
test/mouse_test.py (1)

410-410: Initializing a window before toggling relative mode on SDL3 is correct.

This avoids the NULL-window error and aligns tests with the new runtime behavior.

@Starbuck5 Starbuck5 added the sdl3 label Sep 1, 2025
@ankith26 ankith26 added this to the 2.5.6 milestone Sep 2, 2025
@ankith26 ankith26 merged commit 014f7d0 into main Sep 2, 2025
28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants