Repository navigation
Fix crash on connection attribute lookup with non-string names - #810
Conversation
|
No actionable comments were generated in the recent review.
|
| Layer / File(s) | Summary |
|---|---|
Unicode name handling and tests src/MySQLdb/_mysql.c, tests/test__mysql.py |
The handler checks that an attribute name is Unicode before comparing it with "closed". Tests cover invalid name types, missing and invalid names, and access to "closed" through both connection classes. |
Priority:
Estimated code review effort: 2 (Simple) | ~10 minutes
Severity of issue fixed: Medium
Merge Risk: 
Minimal · up to 74b6d
-
No actionable merge-blocking risk remains in the reviewed changes.
Pre-merge checks: 
4 · 
1
-
Failed checks: 1 warning Passed checks: 4 passed-
Full details: Linked Issues check
-
Explanation
Issue [
#809] requires that non-string names no longer crash connection attribute lookup. The reviewed source uses a Unicode check, comparesclosedwithPyUnicode_CompareWithASCIIString(), and delegates other names toPyObject_GenericGetAttr(). The reviewed tests call__getattribute__on both_mysql.connectionandConnectionwith non-string names and requireTypeError. They also cover missing names, embedded NULs, lone surrogates, string subclasses, and normalclosedlookup.
Full details: Out of Scope Changes check
-
Explanation
The source change directly fixes the attribute lookup path from issue [
#809]. The added tests exercise the affected behavior on both connection types. The reviewed changes show no unrelated product behavior or unrelated test changes.
-
Full details: Docstring Coverage
-
Explanation
Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. 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 1 functions across 2 files.
Resolution
Write docstrings for the functions missing them to satisfy the coverage threshold.
-
Finishing Touches: 💡 1
-
📝 Generate docstrings: 💡
-
- Commit to this branch
- Create a new PR
🧪 Generate unit tests (beta)
-
- Commit to this branch
- 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.
Comment @coderabbitai help to get the list of available commands.
Calling
Connection.__new__(Connection).__getattribute__(5)segfaults becausePyUnicode_AsUTF8()returns NULL and the attribute getter passes it tostrcmp().Check that the attribute name is Unicode and compare it directly with
"closed"usingPyUnicode_CompareWithASCIIString(). Other names fall through toPyObject_GenericGetAttr(), which raises the normalTypeErrorfor non-string names. This also handles lone surrogates without UTF-8 conversion and avoids treating"closed\0suffix"as"closed".Add database-independent regression tests for both
_mysql.connectionandConnection, covering non-string names, missing names, embedded NULs, lone surrogates, string subclasses, and normal attribute lookup.Fixes #809.
Validation:
pytest --cov=MySQLdb tests -q— 161 passed.ruff check src/ tests/test__mysql.pyandruff format --check tests/test__mysql.pypassed.Summary by CodeRabbit
closedattribute can be accessed with regular strings andstrsubclasses, while invalid or non-string attribute names now produce the expected exceptions. This makes connection attribute lookups more consistent.