From 1c553bb703d5b087e1f5b7966584c70b0b8a196a Mon Sep 17 00:00:00 2001
From: Marco De Paoli <depaolim@gmail.com>
Date: Fri, 5 Oct 2018 09:23:13 +0200
Subject: [PATCH 1/2] Added connection.host

Return the server host name of the current connect.
---
 NEWS                      |  1 +
 doc/src/connection.rst    | 18 ++++++++++++++++++
 psycopg/connection_type.c | 22 ++++++++++++++++++++++
 tests/test_connection.py  | 15 ++++++++++++++-
 4 files changed, 55 insertions(+), 1 deletion(-)

diff --git a/NEWS b/NEWS
index eabb8392..a47b2a56 100644
--- a/NEWS
+++ b/NEWS
@@ -13,6 +13,7 @@ New features:
   (:ticket:`#773`).
 - `~psycopg2.extras.DictCursor` and `~psycopg2.extras.RealDictCursor` rows
   maintain columns order (:ticket:`#177`).
+- Added `connection.host` property (:ticket:`#726`).
 
 Other changes:
 
diff --git a/doc/src/connection.rst b/doc/src/connection.rst
index 2910f301..2231995a 100644
--- a/doc/src/connection.rst
+++ b/doc/src/connection.rst
@@ -599,6 +599,24 @@ The ``connection`` class
         .. versionadded:: 2.5
 
 
+    .. index::
+        pair: Backend; Host
+
+    .. attribute:: host
+
+        Returns the server host name of the active connection.
+
+        This can be a host name, an IP address, or a directory path if the
+        connection is via Unix socket. (The path case can be distinguished
+        because it will always be an absolute path, beginning with /.)
+
+        .. seealso:: libpq docs for `PQhost()`__ for details.
+
+            .. __: http://www.postgresql.org/docs/current/static/libpq-status.html#LIBPQ-PQHOST
+
+        .. versionadded:: 2.8.0
+
+
     .. index::
         pair: Backend; PID
 
diff --git a/psycopg/connection_type.c b/psycopg/connection_type.c
index 6a66d48d..da421c01 100644
--- a/psycopg/connection_type.c
+++ b/psycopg/connection_type.c
@@ -992,6 +992,25 @@ psyco_conn_get_backend_pid(connectionObject *self)
     return PyInt_FromLong((long)PQbackendPID(self->pgconn));
 }
 
+/* get the current host */
+
+#define psyco_conn_host_get_doc \
+"host -- Get the host name."
+
+static PyObject *
+psyco_conn_host_get(connectionObject *self)
+{
+    const char *val = NULL;
+
+    EXC_IF_CONN_CLOSED(self);
+
+    val = PQhost(self->pgconn);
+    if (!val) {
+        Py_RETURN_NONE;
+    }
+    return conn_text_from_chars(self, val);
+}
+
 /* reset the currect connection */
 
 #define psyco_conn_reset_doc \
@@ -1243,6 +1262,9 @@ static struct PyGetSetDef connectionObject_getsets[] = {
         (getter)psyco_conn_deferrable_get,
         (setter)psyco_conn_deferrable_set,
         psyco_conn_deferrable_doc },
+    { "host",
+        (getter)psyco_conn_host_get, NULL,
+        psyco_conn_host_get_doc },
     {NULL}
 };
 #undef EXCEPTION_GETTER
diff --git a/tests/test_connection.py b/tests/test_connection.py
index 1342f9f8..498f3513 100755
--- a/tests/test_connection.py
+++ b/tests/test_connection.py
@@ -39,7 +39,7 @@ from .testutils import (
     skip_after_postgres, skip_before_libpq, skip_after_libpq,
     ConnectingTestCase, skip_if_tpc_disabled, skip_if_windows, slow)
 
-from .testconfig import dsn, dbname
+from .testconfig import dbhost, dsn, dbname
 
 
 class ConnectionTests(ConnectingTestCase):
@@ -1682,6 +1682,19 @@ while True:
         self.assert_(not err, err)
 
 
+class TestConnectionProps(ConnectingTestCase):
+    def test_host(self):
+        self.assertFalse(self.conn.closed)
+        expected = dbhost if dbhost else "/"
+        self.assertIn(expected, self.conn.host)
+
+    def test_host_readonly(self):
+        self.assertFalse(self.conn.closed)
+        with self.assertRaises(AttributeError):
+            self.conn.host = 'override'
+
+
+
 def test_suite():
     return unittest.TestLoader().loadTestsFromName(__name__)
 

From 382eeccab8cd7a13abd71f6ee5dcc3583af2d8ca Mon Sep 17 00:00:00 2001
From: Daniele Varrazzo <daniele.varrazzo@gmail.com>
Date: Wed, 10 Oct 2018 22:02:20 +0100
Subject: [PATCH 2/2] Small tweaks to connection.host docs

---
 NEWS                   | 2 +-
 doc/src/connection.rst | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/NEWS b/NEWS
index a47b2a56..cd7884ec 100644
--- a/NEWS
+++ b/NEWS
@@ -7,13 +7,13 @@ What's new in psycopg 2.8
 New features:
 
 - Added `~psycopg2.extensions.encrypt_password()` function (:ticket:`#576`).
+- Added `connection.host` property (:ticket:`#726`).
 - `~psycopg2.sql.Identifier` can represent qualified names in SQL composition
   (:ticket:`#732`).
 - `!str()` on `~psycopg2.extras.Range` produces a human-readable representation
   (:ticket:`#773`).
 - `~psycopg2.extras.DictCursor` and `~psycopg2.extras.RealDictCursor` rows
   maintain columns order (:ticket:`#177`).
-- Added `connection.host` property (:ticket:`#726`).
 
 Other changes:
 
diff --git a/doc/src/connection.rst b/doc/src/connection.rst
index 2231995a..f6e3f4ad 100644
--- a/doc/src/connection.rst
+++ b/doc/src/connection.rst
@@ -604,11 +604,11 @@ The ``connection`` class
 
     .. attribute:: host
 
-        Returns the server host name of the active connection.
+        The server host name of the active connection.
 
         This can be a host name, an IP address, or a directory path if the
         connection is via Unix socket. (The path case can be distinguished
-        because it will always be an absolute path, beginning with /.)
+        because it will always be an absolute path, beginning with ``/``.)
 
         .. seealso:: libpq docs for `PQhost()`__ for details.