Skip to content

Commit

Permalink
set_character_set() sends "SET NAMES" always. (#509)
Browse files Browse the repository at this point in the history
Fix: #504.
  • Loading branch information
methane authored Oct 19, 2021
1 parent 3ee07a0 commit dee7d88
Showing 1 changed file with 9 additions and 29 deletions.
38 changes: 9 additions & 29 deletions MySQLdb/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,6 @@ class object, used to create cursors (keyword only)
)

self.encoding = "ascii" # overridden in set_character_set()
db = proxy(self)

def unicode_literal(u, dummy=None):
return db.string_literal(u.encode(db.encoding))

if not charset:
charset = self.character_set_name()
Expand All @@ -227,7 +223,13 @@ def unicode_literal(u, dummy=None):
# MySQL may return JSON with charset==binary.
self.converter[FIELD_TYPE.JSON] = str

db = proxy(self)

def unicode_literal(u, dummy=None):
return db.string_literal(u.encode(db.encoding))

self.encoders[str] = unicode_literal

self._transactional = self.server_capabilities & CLIENT.TRANSACTIONS
if self._transactional:
if autocommit is not None:
Expand Down Expand Up @@ -300,32 +302,10 @@ def begin(self):
"""
self.query(b"BEGIN")

if not hasattr(_mysql.connection, "warning_count"):

def warning_count(self):
"""Return the number of warnings generated from the
last query. This is derived from the info() method."""
info = self.info()
if info:
return int(info.split()[-1])
else:
return 0

def set_character_set(self, charset):
"""Set the connection character set to charset. The character
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."""
py_charset = _charset_to_encoding.get(charset, charset)
if self.character_set_name() != charset:
try:
super().set_character_set(charset)
except AttributeError:
if self._server_version < (4, 1):
raise NotSupportedError("server is too old to set charset")
self.query("SET NAMES %s" % charset)
self.store_result()
self.encoding = py_charset
"""Set the connection character set to charset."""
super().set_character_set(charset)
self.encoding = _charset_to_encoding.get(charset, charset)

def set_sql_mode(self, sql_mode):
"""Set the connection sql_mode. See MySQL documentation for
Expand Down

0 comments on commit dee7d88

Please sign in to comment.