From 1f084636840ec5e83c90d9da29fa6c1194622380 Mon Sep 17 00:00:00 2001 From: Elvis Pranskevichus Date: Tue, 12 Sep 2017 12:26:31 -0400 Subject: [PATCH] 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. --- asyncpg/connect_utils.py | 14 +++++++++++--- tests/test_connect.py | 6 ++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/asyncpg/connect_utils.py b/asyncpg/connect_utils.py index 8a3979bc..da616523 100644 --- a/asyncpg/connect_utils.py +++ b/asyncpg/connect_utils.py @@ -9,6 +9,7 @@ import collections import getpass import os +import platform import socket import struct import time @@ -40,6 +41,9 @@ ]) +_system = platform.uname().system + + def _parse_connect_dsn_and_args(*, dsn, host, port, user, password, database, ssl, connect_timeout, server_settings): @@ -123,9 +127,13 @@ def _parse_connect_dsn_and_args(*, dsn, host, port, user, if host is None: host = os.getenv('PGHOST') if not host: - host = ['/tmp', '/private/tmp', - '/var/pgsql_socket', '/run/postgresql', - 'localhost'] + if _system == 'Windows': + host = ['localhost'] + else: + host = ['/tmp', '/private/tmp', + '/var/pgsql_socket', '/run/postgresql', + 'localhost'] + if not isinstance(host, list): host = [host] diff --git a/tests/test_connect.py b/tests/test_connect.py index b5a4dd7d..a19685f1 100644 --- a/tests/test_connect.py +++ b/tests/test_connect.py @@ -513,6 +513,12 @@ async def test_connection_ssl_unix(self): loop=self.loop, ssl=ssl_context) + async def test_connection_implicit_host(self): + conn_spec = self.cluster.get_connection_spec() + con = await asyncpg.connect( + port=conn_spec['port'], database='postgres', loop=self.loop) + await con.close() + @unittest.skipIf(os.environ.get('PGHOST'), 'unmanaged cluster') class TestSSLConnection(tb.ConnectedTestCase):