Skip to content

Commit d8a9627

Browse files
committed
BUG33093116: Cache the default connection attributes
This patch enables the caching of the platform arch and OS version, which is used in the default connection attributes.
1 parent 2fe05c5 commit d8a9627

File tree

2 files changed

+32
-21
lines changed

2 files changed

+32
-21
lines changed

lib/mysql/connector/connection.py

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import datetime
3535
import logging
3636
import os
37-
import platform
3837
import socket
3938
import struct
4039
import time
@@ -55,7 +54,7 @@
5554
MySQLCursorBufferedNamedTuple)
5655
from .network import MySQLUnixSocket, MySQLTCPSocket
5756
from .protocol import MySQLProtocol
58-
from .utils import int1store, int4store, lc_int, linux_distribution
57+
from .utils import int1store, int4store, lc_int, get_platform
5958
from .abstracts import MySQLConnectionAbstract
6059

6160
logging.getLogger(__name__).addHandler(logging.NullHandler())
@@ -120,34 +119,23 @@ def __init__(self, *args, **kwargs):
120119

121120
def _add_default_conn_attrs(self):
122121
"""Add the default connection attributes."""
123-
if os.name == "nt":
124-
if "64" in platform.architecture()[0]:
125-
platform_arch = "x86_64"
126-
elif "32" in platform.architecture()[0]:
127-
platform_arch = "i386"
128-
else:
129-
platform_arch = platform.architecture()
130-
os_ver = "Windows-{}".format(platform.win32_ver()[1])
131-
else:
132-
platform_arch = platform.machine()
133-
if platform.system() == "Darwin":
134-
os_ver = "{}-{}".format("macOS", platform.mac_ver()[0])
135-
else:
136-
os_ver = "-".join(linux_distribution()[0:2])
137-
122+
platform = get_platform()
138123
license_chunks = version.LICENSE.split(" ")
139124
if license_chunks[0] == "GPLv2":
140125
client_license = "GPL-2.0"
141126
else:
142127
client_license = "Commercial"
143128
default_conn_attrs = {
144129
"_pid": str(os.getpid()),
145-
"_platform": platform_arch,
130+
"_platform": platform["arch"],
146131
"_source_host": socket.gethostname(),
147132
"_client_name": "mysql-connector-python",
148133
"_client_license": client_license,
149-
"_client_version": ".".join([str(x) for x in version.VERSION[0:3]]),
150-
"_os": os_ver}
134+
"_client_version": ".".join(
135+
[str(x) for x in version.VERSION[0:3]]
136+
),
137+
"_os": platform["version"],
138+
}
151139

152140
self._conn_attrs.update((default_conn_attrs))
153141

lib/mysql/connector/utils.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2009, 2020, Oracle and/or its affiliates.
1+
# Copyright (c) 2009, 2021, Oracle and/or its affiliates.
22
#
33
# This program is free software; you can redistribute it and/or modify
44
# it under the terms of the GNU General Public License, version 2.0, as
@@ -35,11 +35,13 @@
3535
in_table_c21_c22, in_table_c3, in_table_c4, in_table_c5,
3636
in_table_c6, in_table_c7, in_table_c8, in_table_c9,
3737
in_table_c12, in_table_d1, in_table_d2)
38+
import platform
3839
import struct
3940
import sys
4041
import unicodedata
4142

4243
from decimal import Decimal
44+
from functools import lru_cache
4345

4446
from .custom_types import HexLiteral
4547

@@ -609,3 +611,24 @@ def init_bytearray(payload=b'', encoding='utf-8'):
609611

610612
return bytearray(payload)
611613

614+
615+
@lru_cache()
616+
def get_platform():
617+
"""Return a dict with the platform arch and OS version."""
618+
plat = {"arch": None, "version": None}
619+
if os.name == "nt":
620+
if "64" in platform.architecture()[0]:
621+
plat["arch"] = "x86_64"
622+
elif "32" in platform.architecture()[0]:
623+
plat["arch"] = "i386"
624+
else:
625+
plat["arch"] = platform.architecture()
626+
plat["version"] = "Windows-{}".format(platform.win32_ver()[1])
627+
else:
628+
plat["arch"] = platform.machine()
629+
if platform.system() == "Darwin":
630+
plat["version"] = "{}-{}".format("macOS", platform.mac_ver()[0])
631+
else:
632+
plat["version"] = "-".join(linux_distribution()[0:2])
633+
634+
return plat

0 commit comments

Comments
 (0)