) -> bytearray:
"""Convert a row of adapted data to the data to send for binary copy"""
cdef Py_ssize_t rowlen = len(row)
- cdef uint16_t berowlen = endian.htobe16(rowlen)
+ cdef uint16_t berowlen = endian.htobe16(<int16_t>rowlen)
cdef Py_ssize_t pos # offset in 'out' where to write
if out is None:
size = (<RowDumper>row_dumper).cdumper.cdump(
item, out, pos + sizeof(besize))
# Also add the size of the item, before the item
- besize = endian.htobe32(size)
+ besize = endian.htobe32(<int32_t>size)
target = PyByteArray_AS_STRING(out) # might have been moved by cdump
memcpy(target + pos, <void *>&besize, sizeof(besize))
else:
(<RowDumper>row_dumper).dumpfunc, <PyObject *>item, NULL)
_buffer_as_string_and_size(b, &buf, &size)
target = CDumper.ensure_size(out, pos, size + sizeof(besize))
- besize = endian.htobe32(size)
+ besize = endian.htobe32(<int32_t>size)
memcpy(target, <void *>&besize, sizeof(besize))
memcpy(target + sizeof(besize), buf, size)
if nesc > 0:
tmpsize = size + nesc
target = <unsigned char *>CDumper.ensure_size(out, pos, tmpsize)
- for j in range(size - 1, -1, -1):
+ for j in range(<int>size - 1, -1, -1):
target[j + nesc] = target[j]
if copy_escape_lut[target[j]] != 0:
nesc -= 1
types: Sequence[int], formats: Sequence[Format]) -> None:
self._c_set_row_types(len(types), types, formats)
- cdef void _c_set_row_types(self, int ntypes, list types, list formats):
+ cdef void _c_set_row_types(self, Py_ssize_t ntypes, list types, list formats):
cdef list loaders = PyList_New(ntypes)
# these are used more as Python object than C
cpdef dump_sequence(self, object params, object formats):
# Verify that they are not none and that PyList_GET_ITEM won't blow up
- cdef int nparams = len(params)
+ cdef Py_ssize_t nparams = len(params)
cdef list ps = PyList_New(nparams)
cdef tuple ts = PyTuple_New(nparams)
cdef list fs = PyList_New(nparams)
return record
cpdef object load_sequence(self, record: Sequence[Optional[bytes]]):
- cdef int nfields = len(record)
+ cdef Py_ssize_t nfields = len(record)
out = PyTuple_New(nfields)
cdef PyObject *loader # borrowed RowLoader
cdef int col
@property
def pgconn_ptr(self) -> Optional[int]:
if self._pgconn_ptr:
- return <long><void *>self._pgconn_ptr
+ return <long long><void *>self._pgconn_ptr
else:
return None
) -> PGresult:
_ensure_pgconn(self)
- cdef int cnparams
+ cdef Py_ssize_t cnparams
cdef libpq.Oid *ctypes
cdef char *const *cvalues
cdef int *clengths
cdef libpq.PGresult *pgresult
with nogil:
pgresult = libpq.PQexecParams(
- self._pgconn_ptr, command, cnparams, ctypes,
+ self._pgconn_ptr, command, <int>cnparams, ctypes,
<const char *const *>cvalues, clengths, cformats, result_format)
_clear_query_params(ctypes, cvalues, clengths, cformats)
if pgresult is NULL:
) -> None:
_ensure_pgconn(self)
- cdef int cnparams
+ cdef Py_ssize_t cnparams
cdef libpq.Oid *ctypes
cdef char *const *cvalues
cdef int *clengths
cdef int rv
with nogil:
rv = libpq.PQsendQueryParams(
- self._pgconn_ptr, command, cnparams, ctypes,
+ self._pgconn_ptr, command, <int>cnparams, ctypes,
<const char *const *>cvalues, clengths, cformats, result_format)
_clear_query_params(ctypes, cvalues, clengths, cformats)
if not rv:
_ensure_pgconn(self)
cdef int i
- cdef int nparams = len(param_types) if param_types else 0
+ cdef Py_ssize_t nparams = len(param_types) if param_types else 0
cdef libpq.Oid *atypes = NULL
if nparams:
atypes = <libpq.Oid *>PyMem_Malloc(nparams * sizeof(libpq.Oid))
cdef int rv
with nogil:
rv = libpq.PQsendPrepare(
- self._pgconn_ptr, name, command, nparams, atypes
+ self._pgconn_ptr, name, command, <int>nparams, atypes
)
PyMem_Free(atypes)
if not rv:
) -> None:
_ensure_pgconn(self)
- cdef int cnparams
+ cdef Py_ssize_t cnparams
cdef libpq.Oid *ctypes
cdef char *const *cvalues
cdef int *clengths
cdef int rv
with nogil:
rv = libpq.PQsendQueryPrepared(
- self._pgconn_ptr, name, cnparams, <const char *const *>cvalues,
+ self._pgconn_ptr, name, <int>cnparams, <const char *const *>cvalues,
clengths, cformats, result_format)
_clear_query_params(ctypes, cvalues, clengths, cformats)
if not rv:
_ensure_pgconn(self)
cdef int i
- cdef int nparams = len(param_types) if param_types else 0
+ cdef Py_ssize_t nparams = len(param_types) if param_types else 0
cdef libpq.Oid *atypes = NULL
if nparams:
atypes = <libpq.Oid *>PyMem_Malloc(nparams * sizeof(libpq.Oid))
cdef libpq.PGresult *rv
with nogil:
rv = libpq.PQprepare(
- self._pgconn_ptr, name, command, nparams, atypes)
+ self._pgconn_ptr, name, command, <int>nparams, atypes)
PyMem_Free(atypes)
if rv is NULL:
raise MemoryError("couldn't allocate PGresult")
) -> PGresult:
_ensure_pgconn(self)
- cdef int cnparams
+ cdef Py_ssize_t cnparams
cdef libpq.Oid *ctypes
cdef char *const *cvalues
cdef int *clengths
cdef libpq.PGresult *rv
with nogil:
rv = libpq.PQexecPrepared(
- self._pgconn_ptr, name, cnparams,
+ self._pgconn_ptr, name, <int>cnparams,
<const char *const *>cvalues,
clengths, cformats, result_format)
cdef Py_ssize_t length
_buffer_as_string_and_size(buffer, &cbuffer, &length)
- rv = libpq.PQputCopyData(self._pgconn_ptr, cbuffer, length)
+ rv = libpq.PQputCopyData(self._pgconn_ptr, cbuffer, <int>length)
if rv < 0:
raise e.OperationalError(f"sending copy data failed: {error_message(self)}")
return rv
res._pgresult_ptr = NULL # avoid destroying the pgresult_ptr
-cdef (int, libpq.Oid *, char * const*, int *, int *) _query_params_args(
+cdef (Py_ssize_t, libpq.Oid *, char * const*, int *, int *) _query_params_args(
list param_values: Optional[Sequence[Optional[bytes]]],
param_types: Optional[Sequence[int]],
list param_formats: Optional[Sequence[int]],
else:
tparam_types = param_types
- cdef int nparams = len(param_values) if param_values else 0
+ cdef Py_ssize_t nparams = len(param_values) if param_values else 0
if tparam_types is not None and len(tparam_types) != nparams:
raise ValueError(
"got %d param_values but %d param_types"
# on internal error, e.g. if obj is not a buffer)
_buffer_as_string_and_size(obj, &ptr, &length)
aparams[i] = ptr
- alenghts[i] = length
+ alenghts[i] = <int>length
cdef libpq.Oid *atypes = NULL
if tparam_types:
@property
def pgresult_ptr(self) -> Optional[int]:
if self._pgresult_ptr:
- return <long><void *>self._pgresult_ptr
+ return <long long><void *>self._pgresult_ptr
else:
return None
return libpq.PQoidValue(self._pgresult_ptr)
def set_attributes(self, descriptions: List[PGresAttDesc]):
- cdef int num = len(descriptions)
+ cdef Py_ssize_t num = len(descriptions)
cdef libpq.PGresAttDesc *attrs = <libpq.PGresAttDesc *>PyMem_Malloc(
num * sizeof(libpq.PGresAttDesc))
attrs[i].typlen = descr.typlen
attrs[i].atttypmod = descr.atttypmod
- cdef int res = libpq.PQsetResultAttrs(self._pgresult_ptr, num, attrs)
+ cdef int res = libpq.PQsetResultAttrs(self._pgresult_ptr, <int>num, attrs)
PyMem_Free(attrs)
if (res == 0):
raise e.OperationalError("PQsetResultAttrs failed")
val //= 60
m = val % 60
- h = val // 60
+ h = <int>(val // 60)
try:
return cdt.time_new(h, m, s, us, None)
val //= 60
m = val % 60
- h = val // 60
+ h = <int>(val // 60)
# Python < 3.7 didn't support seconds in the timezones
if PY_VERSION_HEX >= 0x03070000:
secs %= 86_400
try:
- delta = cdt.timedelta_new(days, secs, micros)
+ delta = cdt.timedelta_new(<int>days, <int>secs, <int>micros)
if val > 0:
return pg_datetime_epoch + delta
else:
secs %= 86_400
try:
- delta = cdt.timedelta_new(days, secs, micros)
+ delta = cdt.timedelta_new(<int>days, <int>secs, <int>micros)
if val > 0:
dt = pg_datetimetz_epoch + delta
else:
# Group the micros in biggers stuff or timedelta_new might overflow
with cython.cdivision(True):
- ussecs = aval // 1_000_000
+ ussecs = <int>(aval // 1_000_000)
us = aval % 1_000_000
usdays = ussecs // 86_400
cdef Py_ssize_t cdump(self, obj, bytearray rv, Py_ssize_t offset) except -1:
cdef int16_t *buf = <int16_t *>CDumper.ensure_size(
rv, offset, sizeof(int16_t))
- cdef int16_t val = PyLong_AsLongLong(obj)
+ cdef int16_t val = <int16_t>PyLong_AsLongLong(obj)
# swap bytes if needed
cdef uint16_t *ptvar = <uint16_t *>(&val)
buf[0] = endian.htobe16(ptvar[0])
cdef Py_ssize_t cdump(self, obj, bytearray rv, Py_ssize_t offset) except -1:
cdef int32_t *buf = <int32_t *>CDumper.ensure_size(
rv, offset, sizeof(int32_t))
- cdef int32_t val = PyLong_AsLongLong(obj)
+ cdef int32_t val = <int32_t>PyLong_AsLongLong(obj)
# swap bytes if needed
cdef uint32_t *ptvar = <uint32_t *>(&val)
buf[0] = endian.htobe32(ptvar[0])
return length
cdef int exp = pyexp
- cdef uint16_t ndigits = len(digits)
+ cdef uint16_t ndigits = <uint16_t>len(digits)
# Find the last nonzero digit
cdef int nzdigits = ndigits