99import  ctypes .util 
1010from  ctypes  import  Structure , CFUNCTYPE , POINTER 
1111from  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 
1313
1414from  .misc  import  find_libpq_full_path 
1515from  ..errors  import  NotSupportedError 
@@ -185,24 +185,24 @@ class PGresAttDesc_struct(Structure):
185185PQhost .argtypes  =  [PGconn_ptr ]
186186PQhost .restype  =  c_char_p 
187187
188- _PQhostaddr  =  None 
189188
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 :
198191        raise  NotSupportedError (
199-             "PQhostaddr  requires libpq from PostgreSQL 12, "
200-             f" { libpq_version }  
192+             f" { fname } { pgversion }  on the client; "
193+             f" version  { libpq_version   //   10000 }  
201194        )
202195
203-     return  _PQhostaddr ( pgconn ) 
196+     return  not_supported 
204197
205198
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+ 
206206PQport  =  pq .PQport 
207207PQport .argtypes  =  [PGconn_ptr ]
208208PQport .restype  =  c_char_p 
@@ -305,36 +305,18 @@ def PQhostaddr(pgconn: PGconn_struct) -> bytes:
305305PQdescribePortal .argtypes  =  [PGconn_ptr , c_char_p ]
306306PQdescribePortal .restype  =  PGresult_ptr 
307307
308- _PQclosePrepared  =  None 
309- _PQclosePortal  =  None 
310- 
311308if  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 
319312
313+     PQclosePortal  =  pq .PQclosePortal 
314+     PQclosePortal .argtypes  =  [PGconn_ptr , c_char_p ]
315+     PQclosePortal .restype  =  PGresult_ptr 
320316
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 }  
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 }  
335-         )
336-     return  _PQclosePortal (pgconn , name )
337- 
317+ else :
318+     PQclosePrepared  =  not_supported_before ("PQclosePrepared" , 17 )
319+     PQclosePortal  =  not_supported_before ("PQclosePrepared" , 17 )
338320
339321PQresultStatus  =  pq .PQresultStatus 
340322PQresultStatus .argtypes  =  [PGresult_ptr ]
@@ -529,36 +511,18 @@ def PQclosePortal(pgconn: PGconn_struct, name: str) -> int:
529511PQsendDescribePortal .argtypes  =  [PGconn_ptr , c_char_p ]
530512PQsendDescribePortal .restype  =  c_int 
531513
532- _PQsendClosePrepared  =  None 
533- _PQsendClosePortal  =  None 
534- 
535514if  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 }  
550-         )
551-     return  _PQsendClosePrepared (pgconn , name )
552- 
515+     PQsendClosePrepared  =  pq .PQsendClosePrepared 
516+     PQsendClosePrepared .argtypes  =  [PGconn_ptr , c_char_p ]
517+     PQsendClosePrepared .restype  =  c_int 
553518
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 }  
559-         )
560-     return  _PQsendClosePortal (pgconn , name )
519+     PQsendClosePortal  =  pq .PQsendClosePortal 
520+     PQsendClosePortal .argtypes  =  [PGconn_ptr , c_char_p ]
521+     PQsendClosePortal .restype  =  c_int 
561522
523+ else :
524+     PQsendClosePrepared  =  not_supported_before ("PQsendClosePrepared" , 17 )
525+     PQsendClosePortal  =  not_supported_before ("PQsendClosePortal" , 17 )
562526
563527PQgetResult  =  pq .PQgetResult 
564528PQgetResult .argtypes  =  [PGconn_ptr ]
@@ -635,23 +599,12 @@ def PQsendClosePortal(pgconn: PGconn_struct, name: str) -> int:
635599PQtrace .argtypes  =  [PGconn_ptr , FILE_ptr ]
636600PQtrace .restype  =  None 
637601
638- _PQsetTraceFlags  =  None 
639- 
640602if  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 }  
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 )
655608
656609PQuntrace  =  pq .PQuntrace 
657610PQuntrace .argtypes  =  [PGconn_ptr ]
@@ -664,27 +617,16 @@ def PQsetTraceFlags(pgconn: PGconn_struct, flags: int) -> None:
664617PQfreemem .restype  =  None 
665618
666619if  libpq_version  >=  100000 :
667-     _PQencryptPasswordConn  =  pq .PQencryptPasswordConn 
668-     _PQencryptPasswordConn .argtypes  =  [
620+     PQencryptPasswordConn  =  pq .PQencryptPasswordConn 
621+     PQencryptPasswordConn .argtypes  =  [
669622        PGconn_ptr ,
670623        c_char_p ,
671624        c_char_p ,
672625        c_char_p ,
673626    ]
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 }  
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 )
688630
689631PQmakeEmptyPGresult  =  pq .PQmakeEmptyPGresult 
690632PQmakeEmptyPGresult .argtypes  =  [PGconn_ptr , c_int ]
@@ -705,77 +647,33 @@ def PQencryptPasswordConn(
705647
706648# 34.5 Pipeline Mode 
707649
708- _PQpipelineStatus  =  None 
709- _PQenterPipelineMode  =  None 
710- _PQexitPipelineMode  =  None 
711- _PQpipelineSync  =  None 
712- _PQsendFlushRequest  =  None 
713- 
714650if  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 }  
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 }  
750-         )
751-     return  _PQenterPipelineMode (pgconn )
651+     PQpipelineStatus  =  pq .PQpipelineStatus 
652+     PQpipelineStatus .argtypes  =  [PGconn_ptr ]
653+     PQpipelineStatus .restype  =  c_int 
752654
655+     PQenterPipelineMode  =  pq .PQenterPipelineMode 
656+     PQenterPipelineMode .argtypes  =  [PGconn_ptr ]
657+     PQenterPipelineMode .restype  =  c_int 
753658
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 }  
759-         )
760-     return  _PQexitPipelineMode (pgconn )
659+     PQexitPipelineMode  =  pq .PQexitPipelineMode 
660+     PQexitPipelineMode .argtypes  =  [PGconn_ptr ]
661+     PQexitPipelineMode .restype  =  c_int 
761662
663+     PQpipelineSync  =  pq .PQpipelineSync 
664+     PQpipelineSync .argtypes  =  [PGconn_ptr ]
665+     PQpipelineSync .restype  =  c_int 
762666
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 }  
768-         )
769-     return  _PQpipelineSync (pgconn )
667+     PQsendFlushRequest  =  pq .PQsendFlushRequest 
668+     PQsendFlushRequest .argtypes  =  [PGconn_ptr ]
669+     PQsendFlushRequest .restype  =  c_int 
770670
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 }  
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 )
779677
780678
781679# 33.18. SSL Support 
@@ -787,9 +685,9 @@ def PQsendFlushRequest(pgconn: PGconn_struct) -> int:
787685
788686def  generate_stub () ->  None :
789687    import  re 
790-     from  ctypes  import  _CFuncPtr   # type: ignore 
688+     from  ctypes  import  _CFuncPtr   # type: ignore[attr-defined]  
791689
792-     def  type2str (fname , narg , t ) :
690+     def  type2str (fname :  str , narg :  int   |   None , t :  Any )  ->   str :
793691        if  t  is  None :
794692            return  "None" 
795693        elif  t  is  c_void_p :
@@ -810,7 +708,7 @@ def type2str(fname, narg, t):
810708            if  narg  is  not None :
811709                return  f"Optional[{ t .__name__ [3 :]}  
812710            else :
813-                 return  t .__name__ [3 :]
711+                 return  str ( t .__name__ [3 :]) 
814712
815713        elif  t .__name__  in  ("LP_PQconninfoOption_struct" ,):
816714            return  f"Sequence[{ t .__name__ [3 :]}  
0 commit comments