Use cp1252 for latin1 charset (#398)

This commit is contained in:
Inada Naoki
2019-11-18 21:22:39 +09:00
committed by GitHub
parent 6c67620bc6
commit edeab3040d
2 changed files with 12 additions and 4 deletions

View File

@ -217,6 +217,9 @@ _get_encoding(MYSQL *mysql)
if (strncmp(utf8, cs.csname, 4) == 0) { // utf8, utf8mb3, utf8mb4 if (strncmp(utf8, cs.csname, 4) == 0) { // utf8, utf8mb3, utf8mb4
return utf8; return utf8;
} }
else if (strncmp("latin1", cs.csname, 6) == 0) {
return "cp1252";
}
else if (strncmp("koi8r", cs.csname, 5) == 0) { else if (strncmp("koi8r", cs.csname, 5) == 0) {
return "koi8_r"; return "koi8_r";
} }

View File

@ -15,6 +15,14 @@ from MySQLdb._exceptions import (
NotSupportedError, ProgrammingError, NotSupportedError, ProgrammingError,
) )
# Mapping from MySQL charset name to Python codec name
_charset_to_encoding = {
"utf8mb4": "utf8",
"utf8mb3": "utf8",
"latin1": "cp1252",
"koi8r": "koi8_r",
"koi8u": "koi8_u",
}
re_numeric_part = re.compile(r"^(\d+)") re_numeric_part = re.compile(r"^(\d+)")
@ -289,10 +297,7 @@ class Connection(_mysql.connection):
set can only be changed in MySQL-4.1 and newer. If you try set can only be changed in MySQL-4.1 and newer. If you try
to change the character set from the current value in an to change the character set from the current value in an
older version, NotSupportedError will be raised.""" older version, NotSupportedError will be raised."""
if charset in ("utf8mb4", "utf8mb3"): py_charset = _charset_to_encoding.get(charset, charset)
py_charset = "utf8"
else:
py_charset = charset
if self.character_set_name() != charset: if self.character_set_name() != charset:
try: try:
super(Connection, self).set_character_set(charset) super(Connection, self).set_character_set(charset)