@@ -373,6 +373,22 @@ cdef codec_decode_func_ex(ConnectionSettings settings, FastReadBuffer buf,
373
373
return (< Codec> arg).decode(settings, buf)
374
374
375
375
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
+
376
392
cdef class DataCodecConfig:
377
393
def __init__ (self , cache_key ):
378
394
try :
@@ -523,9 +539,10 @@ cdef class DataCodecConfig:
523
539
Codec core_codec
524
540
encode_func c_encoder = NULL
525
541
decode_func c_decoder = NULL
542
+ uint32_t oid = pylong_as_oid(typeoid)
526
543
527
544
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)
529
546
if core_codec is None :
530
547
raise ValueError (
531
548
" {} type does not support 'tuple' exchange format" .format(
@@ -538,7 +555,7 @@ cdef class DataCodecConfig:
538
555
self .remove_python_codec(typeoid, typename, typeschema)
539
556
540
557
self ._local_type_codecs[typeoid] = \
541
- Codec.new_python_codec(typeoid , typename, typeschema, typekind,
558
+ Codec.new_python_codec(oid , typename, typeschema, typekind,
542
559
encoder, decoder, c_encoder, c_decoder,
543
560
format, xformat)
544
561
@@ -551,19 +568,18 @@ cdef class DataCodecConfig:
551
568
cdef:
552
569
Codec codec
553
570
Codec target_codec
571
+ uint32_t oid = pylong_as_oid(typeoid)
572
+ uint32_t alias_pid
554
573
555
574
if format == PG_FORMAT_ANY:
556
575
formats = (PG_FORMAT_BINARY, PG_FORMAT_TEXT)
557
576
else :
558
577
formats = (format,)
559
578
560
579
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
-
565
580
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)
567
583
else :
568
584
target_codec = get_extra_codec(alias_to, format)
569
585
0 commit comments