@@ -373,6 +373,22 @@ cdef codec_decode_func_ex(ConnectionSettings settings, FastReadBuffer buf,
373373 return (< Codec> arg).decode(settings, buf)
374374
375375
376+ cdef uint32_t pylong_as_oid(val) except ? 0xFFFFFFFFl :
377+ cdef:
378+ int64_t oid = 0
379+ bint overflow = False
380+
381+ try :
382+ oid = cpython.PyLong_AsLongLong(val)
383+ except OverflowError :
384+ overflow = True
385+
386+ if overflow or (oid < 0 or oid > UINT32_MAX):
387+ raise OverflowError (' OID value too large: {!r}' .format(val))
388+
389+ return < uint32_t> val
390+
391+
376392cdef class DataCodecConfig:
377393 def __init__ (self , cache_key ):
378394 try :
@@ -523,9 +539,10 @@ cdef class DataCodecConfig:
523539 Codec core_codec
524540 encode_func c_encoder = NULL
525541 decode_func c_decoder = NULL
542+ uint32_t oid = pylong_as_oid(typeoid)
526543
527544 if xformat == PG_XFORMAT_TUPLE:
528- core_codec = get_any_core_codec(typeoid , format, xformat)
545+ core_codec = get_any_core_codec(oid , format, xformat)
529546 if core_codec is None :
530547 raise ValueError (
531548 " {} type does not support 'tuple' exchange format" .format(
@@ -538,7 +555,7 @@ cdef class DataCodecConfig:
538555 self .remove_python_codec(typeoid, typename, typeschema)
539556
540557 self ._local_type_codecs[typeoid] = \
541- Codec.new_python_codec(typeoid , typename, typeschema, typekind,
558+ Codec.new_python_codec(oid , typename, typeschema, typekind,
542559 encoder, decoder, c_encoder, c_decoder,
543560 format, xformat)
544561
@@ -551,19 +568,18 @@ cdef class DataCodecConfig:
551568 cdef:
552569 Codec codec
553570 Codec target_codec
571+ uint32_t oid = pylong_as_oid(typeoid)
572+ uint32_t alias_pid
554573
555574 if format == PG_FORMAT_ANY:
556575 formats = (PG_FORMAT_BINARY, PG_FORMAT_TEXT)
557576 else :
558577 formats = (format,)
559578
560579 for format in formats:
561- if self .get_codec(typeoid, format) is not None :
562- raise ValueError (' cannot override codec for type {}' .format(
563- typeoid))
564-
565580 if isinstance (alias_to, int ):
566- target_codec = self .get_codec(alias_to, format)
581+ alias_oid = pylong_as_oid(alias_to)
582+ target_codec = self .get_codec(alias_oid, format)
567583 else :
568584 target_codec = get_extra_codec(alias_to, format)
569585
0 commit comments