Skip to content

Conversation

ankith26
Copy link
Member

@ankith26 ankith26 commented Sep 1, 2025

probably the easiest pr of the lot to review

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

coderabbitai bot commented Sep 1, 2025

📝 Walkthrough

Walkthrough

SDL3-compatibility updates across C and Python: unify pixel-format loss access via PG_FORMAT_* macros and swap SDL_GetSurfacePalette for PG_GetSurfacePalette in image paths; adjust SDL_SetClipboardText return semantics handling in scrap modules; align boolean parsing by using int locals in transform functions; make _debug mixer imports lazy with fallbacks.

Changes

Cohort / File(s) Summary of edits
SDL3 pixel-format and palette access
src_c/image.c
Replaced direct Rloss/Gloss/Bloss/Aloss vs. Rbits/... access with PG_FORMAT_* macros in tobytes_surf_32bpp and image_tobytes; replaced SDL_GetSurfacePalette with PG_GetSurfacePalette in image_tobytes and SaveTGA_RW; removed version-conditional branches for these areas.
Clipboard return-semantics (SDL2 vs SDL3)
src_c/scrap.c, src_c/scrap_sdl2.c
Introduced SDL_VERSION_ATLEAST(3,0,0) guards to invert success checks for SDL_SetClipboardText under SDL3, preserving SDL2 behavior; error handling updated accordingly.
Bool parsing alignment in transforms
src_c/transform.c
Changed local flags from SDL_bool to int for PyArg_ParseTuple("p") compatibility in surf_average_color, surf_box_blur, surf_gaussian_blur; forwarded values unchanged semantically.
Lazy mixer import in debug utilities
src_py/_debug.py
Switched to dynamic attempt_import for mixer functions with no-op/default fallbacks; minor string style updates; avoided import-time failures.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor PyCode as Python caller
  participant Scrap as scrap(_scrap_put_text/pygame_scrap_put)
  participant SDL as SDL

  PyCode->>Scrap: set_clipboard_text(text)
  Scrap->>SDL: SDL_SetClipboardText(text)
  alt SDL >= 3.0.0
    SDL-->>Scrap: return non-zero on success
    alt success (non-zero)
      Scrap-->>PyCode: success (return)
    else failure (zero)
      Scrap-->>PyCode: raise error
    end
  else SDL < 3.0.0
    SDL-->>Scrap: return 0 on success
    alt success (zero)
      Scrap-->>PyCode: success (return)
    else failure (non-zero)
      Scrap-->>PyCode: raise error
    end
  end
Loading
sequenceDiagram
  autonumber
  participant Img as image.c (image_tobytes/SaveTGA_RW)
  participant Surf as SDL_Surface
  participant Fmt as Format details

  Img->>Surf: access pixel format
  note over Img,Fmt: Use PG_FORMAT_* macros for *_LOSS fields
  Img->>Fmt: PG_FORMAT_[RGBA]_LOSS(...)
  Img->>Surf: PG_GetSurfacePalette(surf)
  Img-->>Img: proceed with serialization (unchanged)
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested labels

sdl3

Suggested reviewers

  • MyreMylar
  • 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-5

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

Caution

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

⚠️ Outside diff range comments (2)
src_c/scrap.c (1)

412-420: Handle NULL from SDL_GetClipboardText to avoid crash.

SDL_GetClipboardText can return NULL on failure; dereferencing *text would segfault. Raise on NULL before using *text.

-    char *text = SDL_GetClipboardText();
+    char *text = SDL_GetClipboardText();
+    if (text == NULL) {
+        return RAISE(pgExc_SDLError, SDL_GetError());
+    }
src_c/image.c (1)

699-703: Channel-loss bug in RGB/32bpp path (G/B use Rloss).

image_tobytes(..., "RGB") for 32bpp surfaces assigns G/B using Rloss, producing wrong colors.

-                        data[1] = (char)(((color & Gmask) >> Gshift) << Rloss);
-                        data[2] = (char)(((color & Bmask) >> Bshift) << Rloss);
+                        data[1] = (char)(((color & Gmask) >> Gshift) << Gloss);
+                        data[2] = (char)(((color & Bmask) >> Bshift) << Bloss);
🧹 Nitpick comments (2)
src_py/_debug.py (1)

66-69: Graceful mixer imports + small output nit.

  • Dynamic imports with fallbacks are a solid robustness improvement.
  • Add a trailing newline after “Mixer Driver” for consistent formatting.
-    if mixer_init():
-        debug_str += f"Mixer Driver:\t\t{get_mixer_driver()}"
+    if mixer_init():
+        debug_str += f"Mixer Driver:\t\t{get_mixer_driver()}\n"

Also applies to: 110-120, 121-139, 149-151, 181-184

src_c/transform.c (1)

3741-3744: Switching “p”-parsed flags to int is correct; cast at call sites.

PyArg_ParseTuple(..., "p") writes to int. Consider explicit casts when passing to functions expecting SDL_bool to avoid warnings and make intent clear.

-    average_color(surf, surf_format, x, y, w, h, &r, &g, &b, &a,
-                  consider_alpha);
+    average_color(surf, surf_format, x, y, w, h, &r, &g, &b, &a,
+                  (SDL_bool)consider_alpha);
-    new_surf =
-        blur(src_surf_obj, dst_surf_obj, radius, repeat_edge_pixels, 'b');
+    new_surf =
+        blur(src_surf_obj, dst_surf_obj, radius, (SDL_bool)repeat_edge_pixels, 'b');
-    new_surf =
-        blur(src_surf_obj, dst_surf_obj, radius, repeat_edge_pixels, 'g');
+    new_surf =
+        blur(src_surf_obj, dst_surf_obj, radius, (SDL_bool)repeat_edge_pixels, 'g');

Also applies to: 4055-4064, 4087-4097

📜 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 cbcd37b.

📒 Files selected for processing (5)
  • src_c/image.c (3 hunks)
  • src_c/scrap.c (1 hunks)
  • src_c/scrap_sdl2.c (1 hunks)
  • src_c/transform.c (3 hunks)
  • src_py/_debug.py (2 hunks)
🧰 Additional context used
🧠 Learnings (1)
📓 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.
🔇 Additional comments (3)
src_c/scrap.c (1)

444-450: SDL3/SDL2 return-semantics guard looks correct.

Good: SDL3 treats 0/false as failure; SDL2 treats non‑zero as failure. Error path returns a Python exception.

src_c/scrap_sdl2.c (1)

112-117: Clipboard set: version-aware success check is correct.

SDL3 path treats truthy return as success; SDL2 path treats 0 as success. Function returns 1 on success consistently.

src_c/image.c (1)

457-461: Nice unification: PG_FORMAT_ macros and PG_GetSurfacePalette.*

  • Using PG_FORMAT_* removes SDL2/SDL3 field-branching.
  • Switching to PG_GetSurfacePalette aligns with the rest of the codebase and SDL3.

Also applies to: 555-565, 566-570, 1714-1715

@Starbuck5 Starbuck5 added the sdl3 label Sep 1, 2025
@@ -565,19 +558,15 @@ image_tobytes(PyObject *self, PyObject *arg, PyObject *kwarg)
if (!format_details) {
Copy link
Member

Choose a reason for hiding this comment

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

This whole SDL_VERSION_ATLEAST could be removed by using PG_GetSurfaceDetails, same with the block below (+PG_PixelFormatEnum) for that one, if you're reducing redundancy on these.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point. But the main reason for me doing these loss changes is not reducing redundancy, it is fixing behavior because bits != loss and our compat macro handles the conversion.
I want to keep the diff minimal in these runtime fixes PRs, cleanups and stuff can always be done in future PRs.

@ankith26 ankith26 added this to the 2.5.6 milestone Sep 2, 2025
@ankith26 ankith26 merged commit 3899e64 into main Sep 2, 2025
28 checks passed
@ankith26 ankith26 deleted the ankith26-sdl3-5 branch September 2, 2025 12:27
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