9
9
import ctypes .util
10
10
from ctypes import Structure , CFUNCTYPE , POINTER
11
11
from ctypes import c_char , c_char_p , c_int , c_size_t , c_ubyte , c_uint , c_void_p
12
- from typing import List , Optional , Tuple
12
+ from typing import Any , List , NoReturn , Tuple
13
13
14
14
from .misc import find_libpq_full_path
15
15
from ..errors import NotSupportedError
@@ -185,24 +185,24 @@ class PGresAttDesc_struct(Structure):
185
185
PQhost .argtypes = [PGconn_ptr ]
186
186
PQhost .restype = c_char_p
187
187
188
- _PQhostaddr = None
189
188
190
- if libpq_version >= 120000 :
191
- _PQhostaddr = pq .PQhostaddr
192
- _PQhostaddr .argtypes = [PGconn_ptr ]
193
- _PQhostaddr .restype = c_char_p
194
-
195
-
196
- def PQhostaddr (pgconn : PGconn_struct ) -> bytes :
197
- if not _PQhostaddr :
189
+ def not_supported_before (fname : str , pgversion : int ) -> Any :
190
+ def not_supported (* args : Any , ** kwargs : Any ) -> NoReturn :
198
191
raise NotSupportedError (
199
- "PQhostaddr requires libpq from PostgreSQL 12, "
200
- f" { libpq_version } available instead"
192
+ f" { fname } requires libpq from PostgreSQL { pgversion } on the client; "
193
+ f" version { libpq_version // 10000 } available instead"
201
194
)
202
195
203
- return _PQhostaddr ( pgconn )
196
+ return not_supported
204
197
205
198
199
+ if libpq_version >= 120000 :
200
+ PQhostaddr = pq .PQhostaddr
201
+ PQhostaddr .argtypes = [PGconn_ptr ]
202
+ PQhostaddr .restype = c_char_p
203
+ else :
204
+ PQhostaddr = not_supported_before ("PQhostaddr" , 12 )
205
+
206
206
PQport = pq .PQport
207
207
PQport .argtypes = [PGconn_ptr ]
208
208
PQport .restype = c_char_p
@@ -305,36 +305,18 @@ def PQhostaddr(pgconn: PGconn_struct) -> bytes:
305
305
PQdescribePortal .argtypes = [PGconn_ptr , c_char_p ]
306
306
PQdescribePortal .restype = PGresult_ptr
307
307
308
- _PQclosePrepared = None
309
- _PQclosePortal = None
310
-
311
308
if libpq_version >= 170000 :
312
- _PQclosePrepared = pq .PQclosePrepared
313
- _PQclosePrepared .argtypes = [PGconn_ptr , c_char_p ]
314
- _PQclosePrepared .restype = PGresult_ptr
315
-
316
- _PQclosePortal = pq .PQclosePortal
317
- _PQclosePortal .argtypes = [PGconn_ptr , c_char_p ]
318
- _PQclosePortal .restype = PGresult_ptr
309
+ PQclosePrepared = pq .PQclosePrepared
310
+ PQclosePrepared .argtypes = [PGconn_ptr , c_char_p ]
311
+ PQclosePrepared .restype = PGresult_ptr
319
312
313
+ PQclosePortal = pq .PQclosePortal
314
+ PQclosePortal .argtypes = [PGconn_ptr , c_char_p ]
315
+ PQclosePortal .restype = PGresult_ptr
320
316
321
- def PQclosePrepared (pgconn : PGconn_struct , name : str ) -> int :
322
- if not _PQclosePrepared :
323
- raise NotSupportedError (
324
- "PQclosePrepared requires libpq from PostgreSQL 17,"
325
- f" { libpq_version } available instead"
326
- )
327
- return _PQclosePrepared (pgconn , name )
328
-
329
-
330
- def PQclosePortal (pgconn : PGconn_struct , name : str ) -> int :
331
- if not _PQclosePortal :
332
- raise NotSupportedError (
333
- "PQclosePortal requires libpq from PostgreSQL 17,"
334
- f" { libpq_version } available instead"
335
- )
336
- return _PQclosePortal (pgconn , name )
337
-
317
+ else :
318
+ PQclosePrepared = not_supported_before ("PQclosePrepared" , 17 )
319
+ PQclosePortal = not_supported_before ("PQclosePrepared" , 17 )
338
320
339
321
PQresultStatus = pq .PQresultStatus
340
322
PQresultStatus .argtypes = [PGresult_ptr ]
@@ -529,36 +511,18 @@ def PQclosePortal(pgconn: PGconn_struct, name: str) -> int:
529
511
PQsendDescribePortal .argtypes = [PGconn_ptr , c_char_p ]
530
512
PQsendDescribePortal .restype = c_int
531
513
532
- _PQsendClosePrepared = None
533
- _PQsendClosePortal = None
534
-
535
514
if libpq_version >= 170000 :
536
- _PQsendClosePrepared = pq .PQsendClosePrepared
537
- _PQsendClosePrepared .argtypes = [PGconn_ptr , c_char_p ]
538
- _PQsendClosePrepared .restype = c_int
539
-
540
- _PQsendClosePortal = pq .PQsendClosePortal
541
- _PQsendClosePortal .argtypes = [PGconn_ptr , c_char_p ]
542
- _PQsendClosePortal .restype = c_int
543
-
544
-
545
- def PQsendClosePrepared (pgconn : PGconn_struct , name : str ) -> int :
546
- if not _PQsendClosePrepared :
547
- raise NotSupportedError (
548
- "PQsendClosePrepared requires libpq from PostgreSQL 17,"
549
- f" { libpq_version } available instead"
550
- )
551
- return _PQsendClosePrepared (pgconn , name )
552
-
515
+ PQsendClosePrepared = pq .PQsendClosePrepared
516
+ PQsendClosePrepared .argtypes = [PGconn_ptr , c_char_p ]
517
+ PQsendClosePrepared .restype = c_int
553
518
554
- def PQsendClosePortal (pgconn : PGconn_struct , name : str ) -> int :
555
- if not _PQsendClosePortal :
556
- raise NotSupportedError (
557
- "PQsendClosePortal requires libpq from PostgreSQL 17,"
558
- f" { libpq_version } available instead"
559
- )
560
- return _PQsendClosePortal (pgconn , name )
519
+ PQsendClosePortal = pq .PQsendClosePortal
520
+ PQsendClosePortal .argtypes = [PGconn_ptr , c_char_p ]
521
+ PQsendClosePortal .restype = c_int
561
522
523
+ else :
524
+ PQsendClosePrepared = not_supported_before ("PQsendClosePrepared" , 17 )
525
+ PQsendClosePortal = not_supported_before ("PQsendClosePortal" , 17 )
562
526
563
527
PQgetResult = pq .PQgetResult
564
528
PQgetResult .argtypes = [PGconn_ptr ]
@@ -635,23 +599,12 @@ def PQsendClosePortal(pgconn: PGconn_struct, name: str) -> int:
635
599
PQtrace .argtypes = [PGconn_ptr , FILE_ptr ]
636
600
PQtrace .restype = None
637
601
638
- _PQsetTraceFlags = None
639
-
640
602
if libpq_version >= 140000 :
641
- _PQsetTraceFlags = pq .PQsetTraceFlags
642
- _PQsetTraceFlags .argtypes = [PGconn_ptr , c_int ]
643
- _PQsetTraceFlags .restype = None
644
-
645
-
646
- def PQsetTraceFlags (pgconn : PGconn_struct , flags : int ) -> None :
647
- if not _PQsetTraceFlags :
648
- raise NotSupportedError (
649
- "PQsetTraceFlags requires libpq from PostgreSQL 14,"
650
- f" { libpq_version } available instead"
651
- )
652
-
653
- _PQsetTraceFlags (pgconn , flags )
654
-
603
+ PQsetTraceFlags = pq .PQsetTraceFlags
604
+ PQsetTraceFlags .argtypes = [PGconn_ptr , c_int ]
605
+ PQsetTraceFlags .restype = None
606
+ else :
607
+ PQsetTraceFlags = not_supported_before ("PQsetTraceFlags" , 14 )
655
608
656
609
PQuntrace = pq .PQuntrace
657
610
PQuntrace .argtypes = [PGconn_ptr ]
@@ -664,27 +617,16 @@ def PQsetTraceFlags(pgconn: PGconn_struct, flags: int) -> None:
664
617
PQfreemem .restype = None
665
618
666
619
if libpq_version >= 100000 :
667
- _PQencryptPasswordConn = pq .PQencryptPasswordConn
668
- _PQencryptPasswordConn .argtypes = [
620
+ PQencryptPasswordConn = pq .PQencryptPasswordConn
621
+ PQencryptPasswordConn .argtypes = [
669
622
PGconn_ptr ,
670
623
c_char_p ,
671
624
c_char_p ,
672
625
c_char_p ,
673
626
]
674
- _PQencryptPasswordConn .restype = POINTER (c_char )
675
-
676
-
677
- def PQencryptPasswordConn (
678
- pgconn : PGconn_struct , passwd : bytes , user : bytes , algorithm : bytes
679
- ) -> Optional [bytes ]:
680
- if not _PQencryptPasswordConn :
681
- raise NotSupportedError (
682
- "PQencryptPasswordConn requires libpq from PostgreSQL 10,"
683
- f" { libpq_version } available instead"
684
- )
685
-
686
- return _PQencryptPasswordConn (pgconn , passwd , user , algorithm )
687
-
627
+ PQencryptPasswordConn .restype = POINTER (c_char )
628
+ else :
629
+ PQencryptPasswordConn = not_supported_before ("PQencryptPasswordConn" , 10 )
688
630
689
631
PQmakeEmptyPGresult = pq .PQmakeEmptyPGresult
690
632
PQmakeEmptyPGresult .argtypes = [PGconn_ptr , c_int ]
@@ -705,77 +647,33 @@ def PQencryptPasswordConn(
705
647
706
648
# 34.5 Pipeline Mode
707
649
708
- _PQpipelineStatus = None
709
- _PQenterPipelineMode = None
710
- _PQexitPipelineMode = None
711
- _PQpipelineSync = None
712
- _PQsendFlushRequest = None
713
-
714
650
if libpq_version >= 140000 :
715
- _PQpipelineStatus = pq .PQpipelineStatus
716
- _PQpipelineStatus .argtypes = [PGconn_ptr ]
717
- _PQpipelineStatus .restype = c_int
718
-
719
- _PQenterPipelineMode = pq .PQenterPipelineMode
720
- _PQenterPipelineMode .argtypes = [PGconn_ptr ]
721
- _PQenterPipelineMode .restype = c_int
722
-
723
- _PQexitPipelineMode = pq .PQexitPipelineMode
724
- _PQexitPipelineMode .argtypes = [PGconn_ptr ]
725
- _PQexitPipelineMode .restype = c_int
726
-
727
- _PQpipelineSync = pq .PQpipelineSync
728
- _PQpipelineSync .argtypes = [PGconn_ptr ]
729
- _PQpipelineSync .restype = c_int
730
-
731
- _PQsendFlushRequest = pq .PQsendFlushRequest
732
- _PQsendFlushRequest .argtypes = [PGconn_ptr ]
733
- _PQsendFlushRequest .restype = c_int
734
-
735
-
736
- def PQpipelineStatus (pgconn : PGconn_struct ) -> int :
737
- if not _PQpipelineStatus :
738
- raise NotSupportedError (
739
- "PQpipelineStatus requires libpq from PostgreSQL 14,"
740
- f" { libpq_version } available instead"
741
- )
742
- return _PQpipelineStatus (pgconn )
743
-
744
-
745
- def PQenterPipelineMode (pgconn : PGconn_struct ) -> int :
746
- if not _PQenterPipelineMode :
747
- raise NotSupportedError (
748
- "PQenterPipelineMode requires libpq from PostgreSQL 14,"
749
- f" { libpq_version } available instead"
750
- )
751
- return _PQenterPipelineMode (pgconn )
651
+ PQpipelineStatus = pq .PQpipelineStatus
652
+ PQpipelineStatus .argtypes = [PGconn_ptr ]
653
+ PQpipelineStatus .restype = c_int
752
654
655
+ PQenterPipelineMode = pq .PQenterPipelineMode
656
+ PQenterPipelineMode .argtypes = [PGconn_ptr ]
657
+ PQenterPipelineMode .restype = c_int
753
658
754
- def PQexitPipelineMode (pgconn : PGconn_struct ) -> int :
755
- if not _PQexitPipelineMode :
756
- raise NotSupportedError (
757
- "PQexitPipelineMode requires libpq from PostgreSQL 14,"
758
- f" { libpq_version } available instead"
759
- )
760
- return _PQexitPipelineMode (pgconn )
659
+ PQexitPipelineMode = pq .PQexitPipelineMode
660
+ PQexitPipelineMode .argtypes = [PGconn_ptr ]
661
+ PQexitPipelineMode .restype = c_int
761
662
663
+ PQpipelineSync = pq .PQpipelineSync
664
+ PQpipelineSync .argtypes = [PGconn_ptr ]
665
+ PQpipelineSync .restype = c_int
762
666
763
- def PQpipelineSync (pgconn : PGconn_struct ) -> int :
764
- if not _PQpipelineSync :
765
- raise NotSupportedError (
766
- "PQpipelineSync requires libpq from PostgreSQL 14,"
767
- f" { libpq_version } available instead"
768
- )
769
- return _PQpipelineSync (pgconn )
667
+ PQsendFlushRequest = pq .PQsendFlushRequest
668
+ PQsendFlushRequest .argtypes = [PGconn_ptr ]
669
+ PQsendFlushRequest .restype = c_int
770
670
771
-
772
- def PQsendFlushRequest (pgconn : PGconn_struct ) -> int :
773
- if not _PQsendFlushRequest :
774
- raise NotSupportedError (
775
- "PQsendFlushRequest requires libpq from PostgreSQL 14,"
776
- f" { libpq_version } available instead"
777
- )
778
- return _PQsendFlushRequest (pgconn )
671
+ else :
672
+ PQpipelineStatus = not_supported_before ("PQpipelineStatus" , 14 )
673
+ PQenterPipelineMode = not_supported_before ("PQenterPipelineMode" , 14 )
674
+ PQexitPipelineMode = not_supported_before ("PQexitPipelineMode" , 14 )
675
+ PQpipelineSync = not_supported_before ("PQpipelineSync" , 14 )
676
+ PQsendFlushRequest = not_supported_before ("PQsendFlushRequest" , 14 )
779
677
780
678
781
679
# 33.18. SSL Support
@@ -787,9 +685,9 @@ def PQsendFlushRequest(pgconn: PGconn_struct) -> int:
787
685
788
686
def generate_stub () -> None :
789
687
import re
790
- from ctypes import _CFuncPtr # type: ignore
688
+ from ctypes import _CFuncPtr # type: ignore[attr-defined]
791
689
792
- def type2str (fname , narg , t ) :
690
+ def type2str (fname : str , narg : int | None , t : Any ) -> str :
793
691
if t is None :
794
692
return "None"
795
693
elif t is c_void_p :
@@ -810,7 +708,7 @@ def type2str(fname, narg, t):
810
708
if narg is not None :
811
709
return f"Optional[{ t .__name__ [3 :]} ]"
812
710
else :
813
- return t .__name__ [3 :]
711
+ return str ( t .__name__ [3 :])
814
712
815
713
elif t .__name__ in ("LP_PQconninfoOption_struct" ,):
816
714
return f"Sequence[{ t .__name__ [3 :]} ]"
0 commit comments