Skip to content

Respect NO_BACKSLASH_ESCAPES for fallback-encoded values in escape() - #1297

Open
nishat-06 wants to merge 1 commit into
PyMySQL:mainfrom
nishat-06:escape-fallback-no-backslash
Open

nishat-06 wants to merge 1 commit into
PyMySQL:mainfrom
nishat-06:escape-fallback-no-backslash

Conversation

@nishat-06

@nishat-06 nishat-06 commented Nov 23, 2026 •

Copy link
Copy Markdown

Connection.escape() hands values whose type has no registered encoder (a pathlib.Path, a UUID, any object with a custom str) to converters.escape_str, which always escapes with backslashes, so once the session runs with NO_BACKSLASH_ESCAPES a value whose str() is x' OR 1=1 -- is rendered as 'x' OR 1=1 -- ' and closes the string literal early. This is the same gap 60eae5c closed for sequences, and the fix routes the default fallback through the connection-aware _escape_string() while leaving custom fallback encoders untouched, with a regression test next to test_escape_sequence.

Summary by CodeRabbit

  • Bug Fixes
    • Improved SQL escaping for custom objects that don’t have a dedicated encoder, including when NO_BACKSLASH_ESCAPES is enabled.

@coderabbitai

coderabbitai Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Connection.escape now uses _escape_string for objects without an exact-type encoder when the str encoder is converters.escape_str. Tests cover custom objects under the default and NO_BACKSLASH_ESCAPES SQL modes.

Changes: SQL string escaping fallback

Layer / File(s) Summary
String escaping fallback and validation
pymysql/connections.py, pymysql/tests/test_connection.py
Connection.escape stringifies eligible objects and uses the SQL-mode-aware _escape_string path. Tests check query values containing quotes and backslashes under both SQL modes.

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

Change: Bug fix

Possibly related PRs

  • PyMySQL/PyMySQL#1290: Adds a bytearray encoder, which changes whether those values reach the fallback path modified here.

Suggested reviewers: methane

Merge Risk: Minimal · up to c1ab7

This change makes escaping of objects without a registered encoder respect NO_BACKSLASH_ESCAPES mode, which closes a quoting gap. Tests cover both SQL modes, and there is no identified merge-blocking risk.

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 behavior change: escape() now respects NO_BACKSLASH_ESCAPES for values that fall back to the default encoder. The raw summary confirms that Connection.escape routes fallback-e…
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 behavior change: escape() now respects NO_BACKSLASH_ESCAPES for values that fall back to the default encoder. The raw summary confirms that Connection.escape routes fallback-encoded objects through the connection-aware _escape_string, and a regression test was added. The title is specific and matches the changeset and the PR objectives.

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: 00ca71d8-bb15-49bd-90ad-ac34c0f1c76e
Commits

Reviewing files that changed from the base of the PR and between 7b9584f and c1ab7c3.

Files selected for processing: 2

  • pymysql/connections.py
  • pymysql/tests/test_connection.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.

This branch has not been deployed

No deployments
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