Skip to content

Encode bytearray values inside sequence parameters - #1290

Closed
arieleven wants to merge 1 commit into
PyMySQL:mainfrom
arieleven:fix/bytearray-in-sequence-params
Closed

arieleven wants to merge 1 commit into
PyMySQL:mainfrom
arieleven:fix/bytearray-in-sequence-params

Conversation

@arieleven

@arieleven arieleven commented Nov 1, 2026 •

Copy link
Copy Markdown

Requested by Taltal

A bytearray passed as a single parameter is encoded as binary, but one inside a list or tuple is not:

cursor.execute("SELECT * FROM t WHERE data IN %s", ([bytearray(b"ab")],))
# ... WHERE data IN ('bytearray(b\'ab\')')

Connection.escape() checks for bytes and bytearray itself, but sequences go through converters.escape_item(), whose encoders table only has bytes. A bytearray falls back to escape_str, so the query silently compares against the text of its repr.

This adds bytearray: escape_bytes to encoders, so it gets the same _binary X'...' output as bytes.

The new test in test_converters.py fails on main and passes with the fix, and ruff check and ruff format --check are clean. I did not run the suites that need a MySQL server.

Summary by CodeRabbit

  • Bug Fixes
    • Byte arrays are now escaped consistently with byte strings when included in SQL values.

Connection.escape() handles a bytearray passed as a single parameter, but
escape_item() has no encoder for it, so a bytearray inside a list or tuple
fell back to escape_str and was sent as the text "bytearray(b'ab')".
@coderabbitai

coderabbitai Bot commented Nov 1, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review.

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: 6d830c80-ce5b-4a41-85ad-450f337fd0b3
Commits

Reviewing files that changed from the base of the PR and between 68eccae and 81901ce.

Files selected for processing: 2
  • pymysql/converters.py
  • pymysql/tests/test_converters.py

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

Walkthrough

The encoder map now routes bytearray values through escape_bytes. A test checks that sequences containing bytes or bytearray produce the same SQL literal.

Changes: Bytearray SQL escaping

Layer / File(s) Summary
Encoder mapping and output test
pymysql/converters.py, pymysql/tests/test_converters.py
The encoder map assigns bytearray to escape_bytes. The test checks that sequences containing bytes or bytearray produce (_binary X'6162').

Estimated code review effort: 1 (Trivial) | ~5 minutes

Change: Bug fix

Suggested reviewers: methane

Merge Risk: Minimal · up to 81901

No actionable merge-blocking risk remains for this narrowly scoped encoder change.

Pre-merge checks: Passed 5
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: encoding bytearray values inside sequence parameters.
Docstring Coverage Passed No eligible functions found in the changed files. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat…
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: Docstring Coverage

Explanation

No eligible functions found in the changed files. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 0 functions across 2 files.

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

@methane

methane commented Nov 9, 2026

Copy link
Copy Markdown
Member

fixed in 60eae5c

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.

2 participants