Skip to content

Commit df7439c

Browse files
committed
Do not look for a port in a Unix socket domain path
Contributes to #419 Signed-off-by: Sylvain Hellegouarch <[email protected]>
1 parent ae5a89d commit df7439c

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

asyncpg/connect_utils.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -179,14 +179,19 @@ def _parse_hostlist(hostlist, port):
179179
port = _validate_port_spec(hostspecs, port)
180180

181181
for i, hostspec in enumerate(hostspecs):
182-
addr, _, hostspec_port = hostspec.partition(':')
183-
hosts.append(addr)
182+
if hostspec.startswith('/'):
183+
hosts.append(hostspec)
184+
# keep sequence ordering
185+
hostlist_ports.append(5432)
186+
else:
187+
addr, _, hostspec_port = hostspec.partition(':')
188+
hosts.append(addr)
184189

185-
if not port:
186-
if hostspec_port:
187-
hostlist_ports.append(int(hostspec_port))
188-
else:
189-
hostlist_ports.append(default_port[i])
190+
if not port:
191+
if hostspec_port:
192+
hostlist_ports.append(int(hostspec_port))
193+
else:
194+
hostlist_ports.append(default_port[i])
190195

191196
if not port:
192197
port = hostlist_ports

tests/test_connect.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,34 @@ class TestConnectParams(tb.TestCase):
475475
}
476476
)
477477
},
478+
{
479+
'dsn': 'postgres:///db?host=/cloudsql/'
480+
'project:region:instance-name&user=spam',
481+
'result': (
482+
[os.path.join(
483+
'/cloudsql/project:region:instance-name',
484+
'.s.PGSQL.5432'
485+
)], {
486+
'user': 'spam',
487+
'database': 'db'
488+
}
489+
)
490+
},
491+
{
492+
'dsn': 'postgres:///db?host=127.0.0.1:5432,/cloudsql/'
493+
'project:region:instance-name,localhost:5433&user=spam',
494+
'result': (
495+
[('127.0.0.1', 5432),
496+
os.path.join(
497+
'/cloudsql/project:region:instance-name',
498+
'.s.PGSQL.5432'
499+
),
500+
('localhost', 5433)], {
501+
'user': 'spam',
502+
'database': 'db'
503+
}
504+
)
505+
},
478506
]
479507

480508
@contextlib.contextmanager

0 commit comments

Comments
 (0)