mirror of
https://github.com/psycopg/psycopg2.git
synced 2025-02-12 15:20:32 +03:00
Guard from some info functions not available in some libpq versions
This commit is contained in:
parent
d138e42ee5
commit
704e6797e7
|
@ -383,6 +383,9 @@ used_password_get(connInfoObject *self)
|
||||||
static const char ssl_in_use_doc[] =
|
static const char ssl_in_use_doc[] =
|
||||||
"`!True` if the connection uses SSL, `!False` if not.\n"
|
"`!True` if the connection uses SSL, `!False` if not.\n"
|
||||||
"\n"
|
"\n"
|
||||||
|
"Only available if psycopg was built with libpq >= 9.5; raise\n"
|
||||||
|
"`~psycopg2.NotSupportedError` otherwise.\n"
|
||||||
|
"\n"
|
||||||
":type: `!bool`\n"
|
":type: `!bool`\n"
|
||||||
"\n"
|
"\n"
|
||||||
".. seealso:: libpq docs for `PQsslInUse()`__ for details.\n"
|
".. seealso:: libpq docs for `PQsslInUse()`__ for details.\n"
|
||||||
|
@ -392,10 +395,15 @@ static const char ssl_in_use_doc[] =
|
||||||
static PyObject *
|
static PyObject *
|
||||||
ssl_in_use_get(connInfoObject *self)
|
ssl_in_use_get(connInfoObject *self)
|
||||||
{
|
{
|
||||||
PyObject *rv;
|
PyObject *rv = NULL;
|
||||||
|
|
||||||
|
#if PG_VERSION_NUM >= 90500
|
||||||
rv = PQsslInUse(self->conn->pgconn) ? Py_True : Py_False;
|
rv = PQsslInUse(self->conn->pgconn) ? Py_True : Py_False;
|
||||||
Py_INCREF(rv);
|
Py_INCREF(rv);
|
||||||
|
#else
|
||||||
|
PyErr_SetString(NotSupportedError,
|
||||||
|
"'ssl_in_use' not available in libpq < 9.5");
|
||||||
|
#endif
|
||||||
return rv;
|
return rv;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -408,6 +416,9 @@ static const char ssl_attribute_doc[] =
|
||||||
":return: The attribute value, `!None` if unknown.\n"
|
":return: The attribute value, `!None` if unknown.\n"
|
||||||
":rtype: `!str`\n"
|
":rtype: `!str`\n"
|
||||||
"\n"
|
"\n"
|
||||||
|
"Only available if psycopg was built with libpq >= 9.5; raise\n"
|
||||||
|
"`~psycopg2.NotSupportedError` otherwise.\n"
|
||||||
|
"\n"
|
||||||
"Valid names are available in `ssl_attribute_names`.\n"
|
"Valid names are available in `ssl_attribute_names`.\n"
|
||||||
"\n"
|
"\n"
|
||||||
".. seealso:: libpq docs for `PQsslAttribute()`__ for details.\n"
|
".. seealso:: libpq docs for `PQsslAttribute()`__ for details.\n"
|
||||||
|
@ -419,12 +430,15 @@ ssl_attribute(connInfoObject *self, PyObject *args, PyObject *kwargs)
|
||||||
{
|
{
|
||||||
static char *kwlist[] = {"name", NULL};
|
static char *kwlist[] = {"name", NULL};
|
||||||
const char *name;
|
const char *name;
|
||||||
const char *val;
|
|
||||||
|
|
||||||
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s", kwlist, &name)) {
|
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s", kwlist, &name)) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if PG_VERSION_NUM >= 90500
|
||||||
|
{
|
||||||
|
const char *val;
|
||||||
|
|
||||||
val = PQsslAttribute(self->conn->pgconn, name);
|
val = PQsslAttribute(self->conn->pgconn, name);
|
||||||
|
|
||||||
if (!val) {
|
if (!val) {
|
||||||
|
@ -434,12 +448,21 @@ ssl_attribute(connInfoObject *self, PyObject *args, PyObject *kwargs)
|
||||||
return conn_text_from_chars(self->conn, val);
|
return conn_text_from_chars(self->conn, val);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#else
|
||||||
|
PyErr_SetString(NotSupportedError,
|
||||||
|
"'ssl_attribute()' not available in libpq < 9.5");
|
||||||
|
return NULL;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
static const char ssl_attribute_names_doc[] =
|
static const char ssl_attribute_names_doc[] =
|
||||||
"The list of the SSL attribute names available.\n"
|
"The list of the SSL attribute names available.\n"
|
||||||
"\n"
|
"\n"
|
||||||
":type: `!list` of `!str`\n"
|
":type: `!list` of `!str`\n"
|
||||||
"\n"
|
"\n"
|
||||||
|
"Only available if psycopg was built with libpq >= 9.5; raise\n"
|
||||||
|
"`~psycopg2.NotSupportedError` otherwise.\n"
|
||||||
|
"\n"
|
||||||
".. seealso:: libpq docs for `PQsslAttributeNames()`__ for details.\n"
|
".. seealso:: libpq docs for `PQsslAttributeNames()`__ for details.\n"
|
||||||
".. __: https://www.postgresql.org/docs/current/static/libpq-status.html"
|
".. __: https://www.postgresql.org/docs/current/static/libpq-status.html"
|
||||||
"#LIBPQ-PQSSLATTRIBUTENAMES";
|
"#LIBPQ-PQSSLATTRIBUTENAMES";
|
||||||
|
@ -447,6 +470,7 @@ static const char ssl_attribute_names_doc[] =
|
||||||
static PyObject *
|
static PyObject *
|
||||||
ssl_attribute_names_get(connInfoObject *self)
|
ssl_attribute_names_get(connInfoObject *self)
|
||||||
{
|
{
|
||||||
|
#if PG_VERSION_NUM >= 90500
|
||||||
const char* const* names;
|
const char* const* names;
|
||||||
int i;
|
int i;
|
||||||
PyObject *l = NULL, *s = NULL, *rv = NULL;
|
PyObject *l = NULL, *s = NULL, *rv = NULL;
|
||||||
|
@ -467,6 +491,11 @@ exit:
|
||||||
Py_XDECREF(l);
|
Py_XDECREF(l);
|
||||||
Py_XDECREF(s);
|
Py_XDECREF(s);
|
||||||
return rv;
|
return rv;
|
||||||
|
#else
|
||||||
|
PyErr_SetString(NotSupportedError,
|
||||||
|
"'ssl_attribute_names not available in libpq < 9.5");
|
||||||
|
return NULL;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1803,10 +1803,21 @@ class TestConnectionInfo(ConnectingTestCase):
|
||||||
self.assertIsInstance(self.conn.info.used_password, bool)
|
self.assertIsInstance(self.conn.info.used_password, bool)
|
||||||
self.assertIs(self.bconn.info.used_password, False)
|
self.assertIs(self.bconn.info.used_password, False)
|
||||||
|
|
||||||
|
@skip_before_libpq(9, 5)
|
||||||
def test_ssl_in_use(self):
|
def test_ssl_in_use(self):
|
||||||
self.assertIsInstance(self.conn.info.ssl_in_use, bool)
|
self.assertIsInstance(self.conn.info.ssl_in_use, bool)
|
||||||
self.assertIs(self.bconn.info.ssl_in_use, False)
|
self.assertIs(self.bconn.info.ssl_in_use, False)
|
||||||
|
|
||||||
|
@skip_after_libpq(9, 5)
|
||||||
|
def test_ssl_not_supported(self):
|
||||||
|
with self.assertRaises(psycopg2.NotSupportedError):
|
||||||
|
self.conn.info.ssl_in_use
|
||||||
|
with self.assertRaises(psycopg2.NotSupportedError):
|
||||||
|
self.conn.info.ssl_attribute_names
|
||||||
|
with self.assertRaises(psycopg2.NotSupportedError):
|
||||||
|
self.conn.info.ssl_attribute('wat')
|
||||||
|
|
||||||
|
@skip_before_libpq(9, 5)
|
||||||
def test_ssl_attribute(self):
|
def test_ssl_attribute(self):
|
||||||
attribs = self.conn.info.ssl_attribute_names
|
attribs = self.conn.info.ssl_attribute_names
|
||||||
self.assert_(attribs)
|
self.assert_(attribs)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user