Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit edeab30

Browse files
authoredNov 18, 2019
Use cp1252 for latin1 charset (#398)
1 parent 6c67620 commit edeab30

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed
 

‎MySQLdb/_mysql.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,9 @@ _get_encoding(MYSQL *mysql)
217217
if (strncmp(utf8, cs.csname, 4) == 0) { // utf8, utf8mb3, utf8mb4
218218
return utf8;
219219
}
220+
else if (strncmp("latin1", cs.csname, 6) == 0) {
221+
return "cp1252";
222+
}
220223
else if (strncmp("koi8r", cs.csname, 5) == 0) {
221224
return "koi8_r";
222225
}

‎MySQLdb/connections.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@
1515
NotSupportedError, ProgrammingError,
1616
)
1717

18+
# Mapping from MySQL charset name to Python codec name
19+
_charset_to_encoding = {
20+
"utf8mb4": "utf8",
21+
"utf8mb3": "utf8",
22+
"latin1": "cp1252",
23+
"koi8r": "koi8_r",
24+
"koi8u": "koi8_u",
25+
}
1826

1927
re_numeric_part = re.compile(r"^(\d+)")
2028

@@ -289,10 +297,7 @@ def set_character_set(self, charset):
289297
set can only be changed in MySQL-4.1 and newer. If you try
290298
to change the character set from the current value in an
291299
older version, NotSupportedError will be raised."""
292-
if charset in ("utf8mb4", "utf8mb3"):
293-
py_charset = "utf8"
294-
else:
295-
py_charset = charset
300+
py_charset = _charset_to_encoding.get(charset, charset)
296301
if self.character_set_name() != charset:
297302
try:
298303
super(Connection, self).set_character_set(charset)

0 commit comments

Comments
 (0)
Please sign in to comment.