<commit_before># cython: language_level=3

DEF DEBUG = 1

cimport cython
cimport cpython

import asyncio
import codecs
import collections
import socket

from libc.stdint cimport int16_t, int32_t, uint16_t, uint32_t, int64_t, uint64_t

from asyncpg.protocol cimport record

from asyncpg.protocol.python cimport (
                     PyMem_Malloc, PyMem_Realloc, PyMem_Calloc, PyMem_Free,
                     PyMemoryView_GET_BUFFER, PyMemoryView_Check,
                     PyUnicode_AsUTF8AndSize, PyByteArray_AsString)

from cpython cimport PyBuffer_FillInfo, PyBytes_AsString

from asyncpg.exceptions import _base as apg_exc_base
from asyncpg import types as apg_types
from asyncpg import exceptions as apg_exc

from asyncpg.protocol cimport hton


include "consts.pxi"
include "pgtypes.pxi"

include "encodings.pyx"
include "settings.pyx"
include "buffer.pyx"

include "codecs/base.pyx"
include "codecs/text.pyx"
include "codecs/bytea.pyx"
include "codecs/json.pyx"
include "codecs/datetime.pyx"
include "codecs/float.pyx"
include "codecs/int.pyx"
include "codecs/numeric.pyx"
include "codecs/uuid.pyx"
include "codecs/array.pyx"
include "codecs/record.pyx"
include "codecs/hstore.pyx"
include "codecs/init.pyx"
include "codecs/special.pyx"

include "coreproto.pyx"
include "prepared_stmt.pyx"


cdef class BaseProtocol(CoreProtocol):

    def __init__(self, address, connect_waiter, user, password, database, loop):
        CoreProtocol.__init__(self, user, password, database)
        self._loop = loop
        self._address = address
        self._hash = (self._address, self._database)
        self._settings = ConnectionSettings(self._hash)
        self._last_query = None
        self._connect_waiter = connect_waiter
        self._waiter = None
        self._state = STATE_NOT_CONNECTED
        self._N = 0

        self._prepared_stmt = None

        self._id = 0

    def get_settings(self):
        return self._settings

    def query(self, query):
        self._start_state(STATE_QUERY)
        self._waiter = self._create_future()
        self._last_query = query
        self._query(query)
        return self._waiter

    def prepare(self, name, query):
        self._N = 0
        self._start_state(STATE_PREPARE_DESCRIBE)
        if name is None:
            name = self._gen_id('prepared_statement')
        if self._prepared_stmt is not None:
            raise RuntimeError('another prepared statement is set')

        self._prepared_stmt = PreparedStatementState(name, query, self)

        self._waiter = self._create_future()
        self._prepare(name, query)
        return self._waiter

    def execute(self, state, args, limit):
        if type(state) is not PreparedStatementState:
            raise TypeError(
                'state must be an instance of PreparedStatementState')

        self._start_state(STATE_EXECUTE)
        self._prepared_stmt = <PreparedStatementState>state

        self._last_query = self._prepared_stmt.query

        self._bind(
            "",
            state.name,
            self._prepared_stmt._encode_bind_msg(args),
            limit)

        self._waiter = self._create_future()
        return self._waiter

    def is_closed(self):
        return self._state == STATE_CLOSING or self._state == STATE_CLOSED

    def close_statement(self, state):
        cdef PreparedStatementState s

        if type(state) is not PreparedStatementState:
            raise TypeError(
                'state must be an instance of PreparedStatementState')

        s = <PreparedStatementState>state

        if s.refs != 0:
            raise RuntimeError(
                'cannot close prepared statement; refs == {} != 0'.format(
                    s.refs))

        self._start_state(STATE_EXECUTE)

        self._close(s.name, False)
        s.closed = True

        self._waiter = self._create_future()
        return self._waiter

    def close(self):
        if self._state != STATE_READY:
            # Some operation is in progress; throw an error in any
            # awaiting waiter.<commit_msg>Handle errors during Bind correctly<commit_after># cython: language_level=3

DEF DEBUG = 1

cimport cython
cimport cpython

import asyncio
import codecs
import collections
import socket

from libc.stdint cimport int16_t, int32_t, uint16_t, uint32_t, int64_t, uint64_t

from asyncpg.protocol cimport record

from asyncpg.protocol.python cimport (
                     PyMem_Malloc, PyMem_Realloc, PyMem_Calloc, PyMem_Free,
                     PyMemoryView_GET_BUFFER, PyMemoryView_Check,
                     PyUnicode_AsUTF8AndSize, PyByteArray_AsString)

from cpython cimport PyBuffer_FillInfo, PyBytes_AsString

from asyncpg.exceptions import _base as apg_exc_base
from asyncpg import types as apg_types
from asyncpg import exceptions as apg_exc

from asyncpg.protocol cimport hton


include "consts.pxi"
include "pgtypes.pxi"

include "encodings.pyx"
include "settings.pyx"
include "buffer.pyx"

include "codecs/base.pyx"
include "codecs/text.pyx"
include "codecs/bytea.pyx"
include "codecs/json.pyx"
include "codecs/datetime.pyx"
include "codecs/float.pyx"
include "codecs/int.pyx"
include "codecs/numeric.pyx"
include "codecs/uuid.pyx"
include "codecs/array.pyx"
include "codecs/record.pyx"
include "codecs/hstore.pyx"
include "codecs/init.pyx"
include "codecs/special.pyx"

include "coreproto.pyx"
include "prepared_stmt.pyx"


cdef class BaseProtocol(CoreProtocol):

    def __init__(self, address, connect_waiter, user, password, database, loop):
        CoreProtocol.__init__(self, user, password, database)
        self._loop = loop
        self._address = address
        self._hash = (self._address, self._database)
        self._settings = ConnectionSettings(self._hash)
        self._last_query = None
        self._connect_waiter = connect_waiter
        self._waiter = None
        self._state = STATE_NOT_CONNECTED
        self._N = 0

        self._prepared_stmt = None

        self._id = 0

    def get_settings(self):
        return self._settings

    def query(self, query):
        self._start_state(STATE_QUERY)
        self._waiter = self._create_future()
        self._last_query = query
        self._query(query)
        return self._waiter

    def prepare(self, name, query):
        self._N = 0
        self._start_state(STATE_PREPARE_DESCRIBE)
        if name is None:
            name = self._gen_id('prepared_statement')
        if self._prepared_stmt is not None:
            raise RuntimeError('another prepared statement is set')

        self._prepared_stmt = PreparedStatementState(name, query, self)

        self._waiter = self._create_future()
        self._prepare(name, query)
        return self._waiter

    def execute(self, state, args, limit):
        if type(state) is not PreparedStatementState:
            raise TypeError(
                'state must be an instance of PreparedStatementState')

        self._start_state(STATE_EXECUTE)
        self._prepared_stmt = <PreparedStatementState>state

        self._last_query = self._prepared_stmt.query
        waiter = self._create_future()

        try:
            self._bind(
                "",
                state.name,
                self._prepared_stmt._encode_bind_msg(args),
                limit)
        except Exception as ex:
            waiter.set_exception(ex)
            self._state = STATE_READY
        else:
            self._waiter = waiter

        return waiter

    def is_closed(self):
        return self._state == STATE_CLOSING or self._state == STATE_CLOSED

    def close_statement(self, state):
        cdef PreparedStatementState s

        if type(state) is not PreparedStatementState:
            raise TypeError(
                'state must be an instance of PreparedStatementState')

        s = <PreparedStatementState>state

        if s.refs != 0:
            raise RuntimeError(
                'cannot close prepared statement; refs == {} != 0'.format(
                    s.refs))

        self._start_state(STATE_EXECUTE)

        self._close(s.name, False)
        s.closed = True

        self._waiter = self._create_future()
        return self._waiter

    def close(self):
        if self._state != STATE_READY:
            # Some operation is in progress; throw an error in any
            # awaiting waiter.
{'fraction_non_alphanumeric': 0.07926975738650012, 'fraction_numerical': 0.004804227720393947, 'mean_word_length': 2.575354229282954, 'pattern_counts': {'":': 0, '<': 7, '<?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': '3600992', 'n_tokens_mistral': 2515, 'n_tokens_neox': 2435, 'n_words': 612}