You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Respect NO_BACKSLASH_ESCAPES for fallback-encoded values in escape() - #1297
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.
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.
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: 4
Check name
Status
Explanation
Description Check
Check skipped - CodeRabbit’s high-level summary is enabled.
Title check
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
Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check
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.
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
NO_BACKSLASH_ESCAPESis enabled.