Skip to content

Commit 8a32fc4

Browse files
committed
Do not attempt to connect to Unix sockets on Windows when host is not set
When the host is not passed explicitly to `connect()`, asyncpg tries to find a suitable socket in a few known directories. On Windows, where there are no file sockets, we should connect directly to `localhost` instead. Fixes: #184.
1 parent 4b0eb5c commit 8a32fc4

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

asyncpg/connect_utils.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import collections
1010
import getpass
1111
import os
12+
import platform
1213
import socket
1314
import struct
1415
import time
@@ -40,6 +41,9 @@
4041
])
4142

4243

44+
_system = platform.uname().system
45+
46+
4347
def _parse_connect_dsn_and_args(*, dsn, host, port, user,
4448
password, database, ssl, connect_timeout,
4549
server_settings):
@@ -123,9 +127,13 @@ def _parse_connect_dsn_and_args(*, dsn, host, port, user,
123127
if host is None:
124128
host = os.getenv('PGHOST')
125129
if not host:
126-
host = ['/tmp', '/private/tmp',
127-
'/var/pgsql_socket', '/run/postgresql',
128-
'localhost']
130+
if _system == 'Windows':
131+
host = ['localhost']
132+
else:
133+
host = ['/tmp', '/private/tmp',
134+
'/var/pgsql_socket', '/run/postgresql',
135+
'localhost']
136+
129137
if not isinstance(host, list):
130138
host = [host]
131139

tests/test_connect.py

+6
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,12 @@ async def test_connection_ssl_unix(self):
513513
loop=self.loop,
514514
ssl=ssl_context)
515515

516+
async def test_connection_implicit_host(self):
517+
conn_spec = self.cluster.get_connection_spec()
518+
con = await asyncpg.connect(
519+
port=conn_spec['port'], database='postgres', loop=self.loop)
520+
await con.close()
521+
516522

517523
@unittest.skipIf(os.environ.get('PGHOST'), 'unmanaged cluster')
518524
class TestSSLConnection(tb.ConnectedTestCase):

0 commit comments

Comments
 (0)