Ignore custom data codec for internal introspection by fantix · Pull Request #618 · MagicStack/asyncpg · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 33 additions & 12 deletions asyncpg/connection.py
3 changes: 2 additions & 1 deletion asyncpg/protocol/codecs/base.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -166,5 +166,6 @@ cdef class DataCodecConfig:
dict _derived_type_codecs
dict _custom_type_codecs

cdef inline Codec get_codec(self, uint32_t oid, ServerDataFormat format)
cdef inline Codec get_codec(self, uint32_t oid, ServerDataFormat format,
bint ignore_custom_codec=*)
cdef inline Codec get_any_local_codec(self, uint32_t oid)
22 changes: 12 additions & 10 deletions asyncpg/protocol/codecs/base.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -692,18 +692,20 @@ cdef class DataCodecConfig:

return codec

cdef inline Codec get_codec(self, uint32_t oid, ServerDataFormat format):
cdef inline Codec get_codec(self, uint32_t oid, ServerDataFormat format,
bint ignore_custom_codec=False):
cdef Codec codec

codec = self.get_any_local_codec(oid)
if codec is not None:
if codec.format != format:
# The codec for this OID has been overridden by
# set_{builtin}_type_codec with a different format.
# We must respect that and not return a core codec.
return None
else:
return codec
if not ignore_custom_codec:
codec = self.get_any_local_codec(oid)
if codec is not None:
if codec.format != format:
# The codec for this OID has been overridden by
# set_{builtin}_type_codec with a different format.
# We must respect that and not return a core codec.
return None
else:
return codec

codec = get_core_codec(oid, format)
if codec is not None:
Expand Down
1 change: 1 addition & 0 deletions asyncpg/protocol/prepared_stmt.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ cdef class PreparedStatementState:
readonly bint closed
readonly int refs
readonly type record_class
readonly bint ignore_custom_codec


list row_desc
Expand Down
10 changes: 7 additions & 3 deletions asyncpg/protocol/prepared_stmt.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ cdef class PreparedStatementState:
str name,
str query,
BaseProtocol protocol,
type record_class
type record_class,
bint ignore_custom_codec
):
self.name = name
self.query = query
Expand All @@ -28,6 +29,7 @@ cdef class PreparedStatementState:
self.closed = False
self.refs = 0
self.record_class = record_class
self.ignore_custom_codec = ignore_custom_codec

def _get_parameters(self):
cdef Codec codec
Expand Down Expand Up @@ -205,7 +207,8 @@ cdef class PreparedStatementState:
cols_mapping[col_name] = i
cols_names.append(col_name)
oid = row[3]
codec = self.settings.get_data_codec(oid)
codec = self.settings.get_data_codec(
oid, ignore_custom_codec=self.ignore_custom_codec)
if codec is None or not codec.has_decoder():
raise exceptions.InternalClientError(
'no decoder for OID {}'.format(oid))
Expand All @@ -230,7 +233,8 @@ cdef class PreparedStatementState:

for i from 0 <= i < self.args_num:
p_oid = self.parameters_desc[i]
codec = self.settings.get_data_codec(p_oid)
codec = self.settings.get_data_codec(
p_oid, ignore_custom_codec=self.ignore_custom_codec)
if codec is None or not codec.has_encoder():
raise exceptions.InternalClientError(
'no encoder for OID {}'.format(p_oid))
Expand Down
3 changes: 2 additions & 1 deletion asyncpg/protocol/protocol.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ cdef class BaseProtocol(CoreProtocol):
async def prepare(self, stmt_name, query, timeout,
*,
PreparedStatementState state=None,
ignore_custom_codec=False,
record_class):
if self.cancel_waiter is not None:
await self.cancel_waiter
Expand All @@ -161,7 +162,7 @@ cdef class BaseProtocol(CoreProtocol):
self.last_query = query
if state is None:
state = PreparedStatementState(
stmt_name, query, self, record_class)
stmt_name, query, self, record_class, ignore_custom_codec)
self.statement = state
except Exception as ex:
waiter.set_exception(ex)
Expand Down
3 changes: 2 additions & 1 deletion asyncpg/protocol/settings.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ cdef class ConnectionSettings(pgproto.CodecContext):
cpdef inline set_builtin_type_codec(
self, typeoid, typename, typeschema, typekind, alias_to, format)
cpdef inline Codec get_data_codec(
self, uint32_t oid, ServerDataFormat format=*)
self, uint32_t oid, ServerDataFormat format=*,
bint ignore_custom_codec=*)
12 changes: 8 additions & 4 deletions asyncpg/protocol/settings.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,18 @@ cdef class ConnectionSettings(pgproto.CodecContext):
typekind, alias_to, _format)

cpdef inline Codec get_data_codec(self, uint32_t oid,
ServerDataFormat format=PG_FORMAT_ANY):
ServerDataFormat format=PG_FORMAT_ANY,
bint ignore_custom_codec=False):
if format == PG_FORMAT_ANY:
codec = self._data_codecs.get_codec(oid, PG_FORMAT_BINARY)
codec = self._data_codecs.get_codec(
oid, PG_FORMAT_BINARY, ignore_custom_codec)
if codec is None:
codec = self._data_codecs.get_codec(oid, PG_FORMAT_TEXT)
codec = self._data_codecs.get_codec(
oid, PG_FORMAT_TEXT, ignore_custom_codec)
return codec
else:
return self._data_codecs.get_codec(oid, format)
return self._data_codecs.get_codec(
oid, format, ignore_custom_codec)

def __getattr__(self, name):
if not name.startswith('_'):
Expand Down
15 changes: 15 additions & 0 deletions tests/test_introspection.py