Skip to content

Expose SQLSTATE on MySQL protocol exceptions without changing exception formatting#1236

Merged
methane merged 3 commits into
mainfrom
copilot/fix-sqlstate-issue
May 15, 2026
Merged

Expose SQLSTATE on MySQL protocol exceptions without changing exception formatting#1236
methane merged 3 commits into
mainfrom
copilot/fix-sqlstate-issue

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 15, 2026

MySQL ERR packets can include a 5-byte SQLSTATE, but PyMySQL currently drops it when raising exceptions. This change preserves that protocol metadata on the exception object while keeping the existing exception args and string representation stable.

  • Error packet parsing

    • Parse SQLSTATE from ERR packets in pymysql.err.raise_mysql_exception().
    • Continue decoding the human-readable message exactly as before.
  • Exception surface

    • Attach the parsed value as exc.sqlstate.
    • Leave exc.args unchanged as (errno, message) to avoid breaking existing callers that depend on current formatting/tuple shape.
    • Default sqlstate to None when the server packet does not include it.
  • Focused coverage

    • Add unit assertions for both packet shapes:
      • ERR packet with SQLSTATE
      • ERR packet without SQLSTATE

Example:

from pymysql import err

data = b"\xff\x15\x04#28000Access denied"

try:
    err.raise_mysql_exception(data)
except err.OperationalError as exc:
    assert exc.args == (1045, "Access denied")
    assert exc.sqlstate == "28000"

Copilot AI linked an issue May 15, 2026 that may be closed by this pull request
Copilot AI changed the title [WIP] Fix sqlstate handling in error exceptions Expose SQLSTATE on MySQL protocol exceptions without changing exception formatting May 15, 2026
Copilot AI requested a review from methane May 15, 2026 11:36
Comment thread pymysql/err.py Outdated
Copilot AI requested a review from methane May 15, 2026 11:46
@methane methane marked this pull request as ready for review May 15, 2026 11:52
@methane methane merged commit 113e252 into main May 15, 2026
23 of 25 checks passed
@methane methane deleted the copilot/fix-sqlstate-issue branch May 15, 2026 11:58
methane pushed a commit that referenced this pull request May 15, 2026
- update `CHANGELOG.md` for `v1.2.0` release preparation
- add `Release date: TBD`
- add changelog entries for user-visible changes since `v1.1.3` (#1234,
#1235, #1236, #1237)
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.

sqlstate missing from previous minor release

2 participants