From df22cf7aea5572ce25b7b9cd75057a6bff5950c9 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Tue, 10 Mar 2015 02:19:11 +0900 Subject: [PATCH] Refactoring --- pymysql/connections.py | 57 ++++++++++++++++++++---------------------- setup.cfg | 1 + 2 files changed, 28 insertions(+), 30 deletions(-) diff --git a/pymysql/connections.py b/pymysql/connections.py index 3b9aa4c..b9d0c42 100644 --- a/pymysql/connections.py +++ b/pymysql/connections.py @@ -4,7 +4,6 @@ # http://dev.mysql.com/doc/refman/5.5/en/error-messages-client.html from __future__ import print_function from ._compat import PY2, range_type, text_type, str_type, JYTHON, IRONPYTHON -DEBUG = False import errno from functools import partial @@ -17,6 +16,14 @@ import sys import traceback import warnings +from .charset import MBLENGTH, charset_by_name, charset_by_id +from .cursors import Cursor +from .constants import CLIENT, COMMAND, FIELD_TYPE, SERVER_STATUS +from .util import byte2int, int2byte +from .converters import ( + escape_item, encoders, decoders, escape_string, through) +from . import err + try: import ssl SSL_ENABLED = True @@ -37,13 +44,7 @@ except ImportError: DEFAULT_USER = None -from .charset import MBLENGTH, charset_by_name, charset_by_id -from .cursors import Cursor -from .constants import CLIENT, COMMAND, FIELD_TYPE, SERVER_STATUS -from .util import byte2int, int2byte -from .converters import ( - escape_item, encoders, decoders, escape_string, through) -from . import err +DEBUG = False _py_version = sys.version_info[:2] @@ -55,6 +56,7 @@ if _py_version == (2, 7) and not IRONPYTHON: # read method of file-like returned by sock.makefile() is very slow. # So we copy io-based one from Python 3. from ._socketio import SocketIO + def _makefile(sock, mode): return io.BufferedReader(SocketIO(sock, mode)) elif _py_version == (2, 6): @@ -63,6 +65,7 @@ elif _py_version == (2, 6): class SockFile(object): def __init__(self, sock): self._sock = sock + def read(self, n): read = self._sock.recv(n) if len(read) == n: @@ -137,7 +140,7 @@ def dump_packet(data): def _scramble(password, message): if not password: return b'\0' - if DEBUG: print('password=' + password) + if DEBUG: print('password=' + str(password)) stage1 = sha_new(password).digest() stage2 = sha_new(stage1).digest() s = sha_new() @@ -197,14 +200,12 @@ def _hash_password_323(password): nr2 = 0x12345671 for c in [byte2int(x) for x in password if x not in (' ', '\t')]: - nr^= (((nr & 63)+add)*c)+ (nr << 8) & 0xFFFFFFFF - nr2= (nr2 + ((nr2 << 8) ^ nr)) & 0xFFFFFFFF - add= (add + c) & 0xFFFFFFFF + nr ^= (((nr & 63) + add) * c) + (nr << 8) & 0xFFFFFFFF + nr2 = (nr2 + ((nr2 << 8) ^ nr)) & 0xFFFFFFFF + add = (add + c) & 0xFFFFFFFF - r1 = nr & ((1 << 31) - 1) # kill sign bits + r1 = nr & ((1 << 31) - 1) # kill sign bits r2 = nr2 & ((1 << 31) - 1) - - # pack return struct.pack(">LL", r1, r2) @@ -646,7 +647,7 @@ class Connection(object): self._connect() def close(self): - ''' Send the quit message and close the socket ''' + """Send the quit message and close the socket""" if self.socket is None: raise err.Error("Already closed") send_data = struct.pack('