<commit_before># Python implementation of the MySQL client-server protocol
#   http://forge.mysql.com/wiki/MySQL_Internals_ClientServer_Protocol

import sys
import struct
from cymysql.err import raise_mysql_exception, OperationalError
from cymysql.constants import SERVER_STATUS
from cymysql.converters import convert_characters

from libc.stdint cimport uint16_t, uint32_t
cdef int PYTHON3 = sys.version_info[0] > 2

MBLENGTH = {
        8:1,
        33:3,
        88:2,
        91:2
        }

cdef int FIELD_TYPE_VAR_STRING=253

cdef int UNSIGNED_CHAR_COLUMN = 251
cdef int UNSIGNED_SHORT_COLUMN = 252
cdef int UNSIGNED_INT24_COLUMN = 253
cdef int UNSIGNED_INT64_COLUMN = 254

cdef int SERVER_MORE_RESULTS_EXISTS = SERVER_STATUS.SERVER_MORE_RESULTS_EXISTS

cdef uint16_t unpack_uint16(bytes s):
    cdef unsigned char* n = s
    return n[0] + (n[1] << 8)

cdef uint32_t unpack_uint24(bytes s):
    cdef unsigned char* n = s
    return n[0] + (n[1] << 8) + (n[2] << 16)

cdef uint32_t unpack_uint32(bytes s):
    cdef unsigned char* n = s
    return n[0] + (n[1] << 8) + (n[2] << 16) + (n[3] << 24)

cdef long long unpack_uint64(bytes n):
    return struct.unpack('<Q', n)[0]

cdef get_decode_values(values, charset, fields, use_unicode, decoders):
    cdef Py_ssize_t i
    r = [None] * len(values)
    for i, value in enumerate(values):
        if value is not None:
            decoder = decoders[fields[i].type_code]
            if decoder is convert_characters:
                r[i] = decoder(value, charset, fields[i], use_unicode)
            else:
                r[i] = decoder(value)
    return r


cdef class MysqlPacket(object):
    """Representation of a MySQL response packet.  Reads in the packet
    from the network socket, removes packet header and provides an interface
    for reading/parsing the packet results."""
    cdef object connection
    cdef bytes __data
    cdef int __data_length
    cdef int __position

    def __init__(self, connection):
        cdef int is_error
        cdef int errno
        self.connection = connection
        self.__position = 0
        self.__recv_packet()
        is_error = (<unsigned char>(self.__data[0])) == 0xff
        if is_error:
            self.__position += 1  # field_count == error (we already know that)
            errno = unpack_uint16(self._read(2))
            raise_mysql_exception(self.__data)

    cdef bytes __recv_from_socket(self, int size):
        cdef bytes r
        cdef int recieved
<commit_msg>refactoring<commit_after># Python implementation of the MySQL client-server protocol
#   http://forge.mysql.com/wiki/MySQL_Internals_ClientServer_Protocol

import sys
import struct
from cymysql.err import raise_mysql_exception, OperationalError
from cymysql.constants import SERVER_STATUS
from cymysql.converters import convert_characters

from libc.stdint cimport uint16_t, uint32_t
cdef int PYTHON3 = sys.version_info[0] > 2

MBLENGTH = {
        8:1,
        33:3,
        88:2,
        91:2
        }

cdef int FIELD_TYPE_VAR_STRING=253

cdef int UNSIGNED_CHAR_COLUMN = 251
cdef int UNSIGNED_SHORT_COLUMN = 252
cdef int UNSIGNED_INT24_COLUMN = 253
cdef int UNSIGNED_INT64_COLUMN = 254

cdef int SERVER_MORE_RESULTS_EXISTS = SERVER_STATUS.SERVER_MORE_RESULTS_EXISTS

cdef uint16_t unpack_uint16(bytes s):
    cdef unsigned char* n = s
    return n[0] + (n[1] << 8)

cdef uint32_t unpack_uint24(bytes s):
    cdef unsigned char* n = s
    return n[0] + (n[1] << 8) + (n[2] << 16)

cdef uint32_t unpack_uint32(bytes s):
    cdef unsigned char* n = s
    return n[0] + (n[1] << 8) + (n[2] << 16) + (n[3] << 24)

cdef long long unpack_uint64(bytes n):
    return struct.unpack('<Q', n)[0]

cdef get_decode_values(values, charset, fields, use_unicode, decoders):
    cdef Py_ssize_t i
    r = [None] * len(values)
    for i, value in enumerate(values):
        if value is None:
            continue
        decoder = decoders[fields[i].type_code]
        if decoder is convert_characters:
            r[i] = decoder(value, charset, fields[i], use_unicode)
        else:
            r[i] = decoder(value)
    return r


cdef class MysqlPacket(object):
    """Representation of a MySQL response packet.  Reads in the packet
    from the network socket, removes packet header and provides an interface
    for reading/parsing the packet results."""
    cdef object connection
    cdef bytes __data
    cdef int __data_length
    cdef int __position

    def __init__(self, connection):
        cdef int is_error
        cdef int errno
        self.connection = connection
        self.__position = 0
        self.__recv_packet()
        is_error = (<unsigned char>(self.__data[0])) == 0xff
        if is_error:
            self.__position += 1  # field_count == error (we already know that)
            errno = unpack_uint16(self._read(2))
            raise_mysql_exception(self.__data)

    cdef bytes __recv_from_socket(self, int size):
        cdef bytes r
        cdef int recieved

{'fraction_non_alphanumeric': 0.08653652646407728, 'fraction_numerical': 0.030992151338297445, 'mean_word_length': 2.998390989541432, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1632451', 'n_tokens_mistral': 1789, 'n_tokens_neox': 1642, 'n_words': 476}