From 12dd0d862583abd6fab6c67452b0f6fad637e14f Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Mon, 18 Nov 2019 20:33:39 +0900 Subject: [PATCH] Use cp1252 for latin1 charset --- MySQLdb/_mysql.c | 3 +++ MySQLdb/connections.py | 13 +++++++++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/MySQLdb/_mysql.c b/MySQLdb/_mysql.c index 648c7284..64647bf3 100644 --- a/MySQLdb/_mysql.c +++ b/MySQLdb/_mysql.c @@ -217,6 +217,9 @@ _get_encoding(MYSQL *mysql) if (strncmp(utf8, cs.csname, 4) == 0) { // utf8, utf8mb3, utf8mb4 return utf8; } + else if (strncmp("latin1", cs.csname, 6) == 0) { + return "cp1252"; + } else if (strncmp("koi8r", cs.csname, 5) == 0) { return "koi8_r"; } diff --git a/MySQLdb/connections.py b/MySQLdb/connections.py index 04453fad..4c33ec55 100644 --- a/MySQLdb/connections.py +++ b/MySQLdb/connections.py @@ -15,6 +15,14 @@ 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+)") @@ -289,10 +297,7 @@ def set_character_set(self, charset): 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 older version, NotSupportedError will be raised.""" - if charset in ("utf8mb4", "utf8mb3"): - py_charset = "utf8" - else: - py_charset = charset + py_charset = _charset_to_encoding.get(charset, charset) if self.character_set_name() != charset: try: super(Connection, self).set_character_set(charset)