55import os
66import unittest
77import atexit
8+ import socket
9+ import errno
810
911from ..python .socket import find_available_port
1012
1618from .. import driver as pg_driver
1719from .. import open as pg_open
1820
21+
22+ def check_for_ipv6 ():
23+ result = False
24+ if socket .has_ipv6 :
25+ try :
26+ socket .socket (socket .AF_INET6 , socket .SOCK_STREAM )
27+ result = True
28+ except socket .error as e :
29+ errs = [errno .EAFNOSUPPORT ]
30+ WSAEAFNOSUPPORT = getattr (errno , 'WSAEAFNOSUPPORT' , None )
31+ if WSAEAFNOSUPPORT is not None :
32+ errs .append (WSAEAFNOSUPPORT )
33+ if e .errno not in errs :
34+ raise
35+ return result
36+
37+
1938msw = sys .platform in ('win32' , 'win64' )
2039
40+ # win32 binaries don't appear to be built with ipv6
41+ has_ipv6 = check_for_ipv6 () and not msw
42+
43+ has_unix_sock = not msw
44+
45+
2146class TestCaseWithCluster (unittest .TestCase ):
2247 """
2348 postgresql.driver *interface* tests.
@@ -50,11 +75,15 @@ def configure_cluster(self):
5075 'failed to find a port for the test cluster on localhost' ,
5176 creator = self .cluster
5277 ).raise_exception ()
78+
79+ listen_addresses = '127.0.0.1'
80+ if has_ipv6 :
81+ listen_addresses += ',::1'
5382 self .cluster .settings .update (dict (
5483 port = str (self .cluster_port ),
5584 max_connections = '6' ,
5685 shared_buffers = '24' ,
57- listen_addresses = 'localhost' ,
86+ listen_addresses = listen_addresses ,
5887 log_destination = 'stderr' ,
5988 log_min_messages = 'FATAL' ,
6089 silent_mode = 'off' ,
@@ -123,7 +152,6 @@ def __init__(self, *args, **kw):
123152 super ().__init__ (* args ,** kw )
124153 # 8.4 nixed this.
125154 self .do_crypt = self .cluster .installation .version_info < (8 ,4 )
126- self .do_unix = sys .platform != msw
127155
128156 def configure_cluster (self ):
129157 super ().configure_cluster ()
@@ -135,7 +163,7 @@ def configure_cluster(self):
135163 # Configure the hba file with the supported methods.
136164 with open (self .cluster .hba_file , 'w' ) as hba :
137165 hosts = ['0.0.0.0/0' ,]
138- if not msw :
166+ if has_ipv6 :
139167 hosts .append ('0::0/0' )
140168 methods = ['md5' , 'password' ] + (['crypt' ] if self .do_crypt else [])
141169 for h in hosts :
@@ -148,11 +176,11 @@ def configure_cluster(self):
148176 # trusted
149177 hba .writelines (["local all all trust\n " ])
150178 hba .writelines (["host test trusted 0.0.0.0/0 trust\n " ])
151- if not msw :
179+ if has_ipv6 :
152180 hba .writelines (["host test trusted 0::0/0 trust\n " ])
153181 # admin lines
154182 hba .writelines (["host all test 0.0.0.0/0 trust\n " ])
155- if not msw :
183+ if has_ipv6 :
156184 hba .writelines (["host all test 0::0/0 trust\n " ])
157185
158186 def initialize_database (self ):
@@ -379,9 +407,7 @@ def test_IP4_connect(self):
379407 with C () as c :
380408 self .assertEqual (c .prepare ('select 1' ).first (), 1 )
381409
382- if not msw :
383- # win32 binaries don't appear to be built with ipv6
384- # so filter this test on windows.
410+ if has_ipv6 :
385411 def test_IP6_connect (self ):
386412 C = pg_driver .default .ip6 (
387413 user = 'test' ,
@@ -445,7 +471,7 @@ def test_trusted_connect(self):
445471 self .assertEqual (c .prepare ('select current_user' ).first (), 'trusted' )
446472
447473 def test_Unix_connect (self ):
448- if msw :
474+ if not has_unix_sock :
449475 return
450476 unix_domain_socket = os .path .join (
451477 self .cluster .data_directory ,
@@ -460,7 +486,7 @@ def test_Unix_connect(self):
460486 self .assertEqual (c .client_address , None )
461487
462488 def test_pg_open_unix (self ):
463- if msw :
489+ if not has_unix_sock :
464490 return
465491 unix_domain_socket = os .path .join (
466492 self .cluster .data_directory ,
0 commit comments