Skip to content

Avoid UnicodeDecodeError for non-utf8 QueryEvents #465

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions pymysqlreplication/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,9 @@ def __init__(self, from_packet, event_size, table_map, ctl_connection, **kwargs)
self.schema = self.packet.read(self.schema_length)
self.packet.advance(1)

self.query = self.packet.read(event_size - 13 - self.status_vars_length
- self.schema_length - 1).decode("utf-8")
query = self.packet.read(event_size - 13 - self.status_vars_length
- self.schema_length - 1)
self.query = query.decode("utf-8", errors='backslashreplace')
#string[EOF] query

def _dump(self):
Expand Down
4 changes: 2 additions & 2 deletions pymysqlreplication/tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ class PyMySQLReplicationTestCase(base):
def ignoredEvents(self):
return []

def setUp(self):
def setUp(self, charset="utf8"):
# default
self.database = {
"host": os.environ.get("MYSQL_5_7") or "localhost",
"user": "root",
"passwd": "",
"port": 3306,
"use_unicode": True,
"charset": "utf8",
"charset": charset,
"db": "pymysqlreplication_test"
}

Expand Down
18 changes: 18 additions & 0 deletions pymysqlreplication/tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1190,6 +1190,24 @@ def test_rows_query_log_event(self):
event = self.stream.fetchone()
self.assertIsInstance(event, RowsQueryLogEvent)

class TestLatin1(base.PyMySQLReplicationTestCase):

def setUp(self):
super().setUp(charset='latin1')

def test_query_event_latin1(self):
"""
Ensure query events with a non-utf8 encoded query are parsed without errors.
"""
self.stream = BinLogStreamReader(self.database, server_id=1024, only_events=[QueryEvent])
self.execute("CREATE TABLE test_latin1_ÖÆÛ (a INT)")
self.execute("COMMIT")
assert "ÖÆÛ".encode('latin-1') == b'\xd6\xc6\xdb'

event = self.stream.fetchone()
assert event.query.startswith("CREATE TABLE test")
assert event.query == r"CREATE TABLE test_latin1_\xd6\xc6\xdb (a INT)"


if __name__ == "__main__":
import unittest
Expand Down