Skip to content

Escape bytearray contents as bytes in string literals - #817

Merged
methane merged 1 commit into
PyMySQL:mainfrom
methane:codex/bytearray-string-literal
Nov 18, 2026
Merged

methane merged 1 commit into
PyMySQL:mainfrom
methane:codex/bytearray-string-literal

Conversation

@methane

@methane methane commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

Passing bytearray(b'ab') as a query parameter currently sends the Python text bytearray(b'ab') to the server. Escape the underlying two bytes so the parameter behaves like b'ab'. This also applies to bytearray subclasses. On free-threaded builds, the copy is protected by the bytearray's critical section.

The new low-level tests failed before the fix and passed with the rebuilt CPython 3.14 wheel (32 tests passed locally). The updated database test checks the returned value with binary_prefix enabled and disabled and is left to CI. Ruff and git diff --check passed.

Related discussion in #816.

Summary by CodeRabbit

  • Bug Fixes
    • bytearray values are now handled consistently with bytes when creating SQL literals, including values containing quotes, null bytes, or backslashes. Subclasses are processed from their byte contents rather than their string representation.
  • Documentation
    • Clarified how string_literal handles byte-like values versus other objects.

@coderabbitai

coderabbitai Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

_mysql.string_literal now escapes bytearray contents through a bytes copy. The documentation describes this behavior, and tests cover empty and special-byte inputs, subclasses that reject string conversion, and query results.

Changes: Bytearray literal escaping

Layer / File(s) Summary
Bytearray escaping and validation
src/MySQLdb/_mysql.c, tests/test__mysql.py, tests/test_connection.py
_mysql.string_literal copies bytearray contents to bytes before escaping. Tests check equivalent literals for bytes and bytearray inputs, including subclasses, and verify query results with and without binary_prefix.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Possibly related PRs

  • PyMySQL/mysqlclient#814: PR #814 changes built-in dispatch for bytearray subclasses, while this PR changes how the selected byte escape path handles them.
Merge Risk: Minimal · up to 0b6ee

This change makes bytearray query parameters behave like bytes, escaping their contents instead of sending their Python text representation. It adds tests for special-byte inputs, subclasses, and query results. No concrete merge-blocking risk remains.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: string literals now escape bytearray contents as bytes. The raw summary and PR objectives both describe this change in _mysql.c, the new bytearray path, and the relate…
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.
Full details: Title check

Explanation

The title names the main change: string literals now escape bytearray contents as bytes. The raw summary and PR objectives both describe this change in _mysql.c, the new bytearray path, and the related tests. The title is specific and matches the changeset.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 24cf5f73-ecbf-41b9-ae1e-c8539a6e0b99
Commits

Reviewing files that changed from the base of the PR and between eef31ea and 0b6ee04.

Files selected for processing: 3

  • src/MySQLdb/_mysql.c
  • tests/test__mysql.py
  • tests/test_connection.py

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 5 remain after this review.

@codecov

codecov Bot commented Nov 18, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.83%. Comparing base (e2a908b) to head (0b6ee04).
⚠️ Report is 64 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #817      +/-   ##
==========================================
+ Coverage   88.86%   91.83%   +2.96%     
==========================================
  Files           7        7              
  Lines         548      686     +138     
==========================================
+ Hits          487      630     +143     
+ Misses         61       56       -5     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@methane
methane merged commit 41f49cf into PyMySQL:main Nov 18, 2026
16 checks passed
@methane
methane deleted the codex/bytearray-string-literal branch November 18, 2026 09:57
@methane methane mentioned this pull request Nov 18, 2026
methane added a commit that referenced this pull request Nov 18, 2026
Prepare mysqlclient 2.3.2 with release notes dated 2026-11-18 for
subclass literal conversion (#814), buffered-result deallocation (#815),
and bytearray parameter escaping (#817), and update both version fields.
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.

1 participant