summaryrefslogtreecommitdiff
path: root/psycopg
diff options
context:
space:
mode:
authorJon Dufresne <jon.dufresne@gmail.com>2018-11-30 16:54:24 -0800
committerJon Dufresne <jon.dufresne@gmail.com>2018-11-30 17:09:30 -0800
commitb796ca0c0a03b75bf7c9212455683c1e3d520466 (patch)
tree18a769730cda535851900a0c356bc2fddb731b78 /psycopg
parent483901ea7bb796141c4d848f3d8b930d8ab8b32e (diff)
downloadpsycopg2-b796ca0c0a03b75bf7c9212455683c1e3d520466.tar.gz
Simplify PyBool usage with Python convenience macros/functions
https://docs.python.org/3/c-api/bool.html
Diffstat (limited to 'psycopg')
-rw-r--r--psycopg/connection_type.c17
-rw-r--r--psycopg/conninfo_type.c15
-rw-r--r--psycopg/cursor_type.c12
-rw-r--r--psycopg/lobject_type.c6
-rw-r--r--psycopg/typecast.c9
5 files changed, 12 insertions, 47 deletions
diff --git a/psycopg/connection_type.c b/psycopg/connection_type.c
index 4499cc4..af9183e 100644
--- a/psycopg/connection_type.c
+++ b/psycopg/connection_type.c
@@ -616,10 +616,7 @@ psyco_conn_set_session(connectionObject *self, PyObject *args, PyObject *kwargs)
static PyObject *
psyco_conn_autocommit_get(connectionObject *self)
{
- PyObject *ret;
- ret = self->autocommit ? Py_True : Py_False;
- Py_INCREF(ret);
- return ret;
+ return PyBool_FromLong(self->autocommit);
}
BORROWED static PyObject *
@@ -1100,25 +1097,21 @@ psyco_conn_isexecuting(connectionObject *self)
{
/* synchronous connections will always return False */
if (self->async == 0) {
- Py_INCREF(Py_False);
- return Py_False;
+ Py_RETURN_FALSE;
}
/* check if the connection is still being built */
if (self->status != CONN_STATUS_READY) {
- Py_INCREF(Py_True);
- return Py_True;
+ Py_RETURN_TRUE;
}
/* check if there is a query being executed */
if (self->async_cursor != NULL) {
- Py_INCREF(Py_True);
- return Py_True;
+ Py_RETURN_TRUE;
}
/* otherwise it's not executing */
- Py_INCREF(Py_False);
- return Py_False;
+ Py_RETURN_FALSE;
}
diff --git a/psycopg/conninfo_type.c b/psycopg/conninfo_type.c
index 740840f..ed193ce 100644
--- a/psycopg/conninfo_type.c
+++ b/psycopg/conninfo_type.c
@@ -352,11 +352,7 @@ static const char needs_password_doc[] =
static PyObject *
needs_password_get(connInfoObject *self)
{
- PyObject *rv;
-
- rv = PQconnectionNeedsPassword(self->conn->pgconn) ? Py_True : Py_False;
- Py_INCREF(rv);
- return rv;
+ return PyBool_FromLong(PQconnectionNeedsPassword(self->conn->pgconn));
}
@@ -372,11 +368,7 @@ static const char used_password_doc[] =
static PyObject *
used_password_get(connInfoObject *self)
{
- PyObject *rv;
-
- rv = PQconnectionUsedPassword(self->conn->pgconn) ? Py_True : Py_False;
- Py_INCREF(rv);
- return rv;
+ return PyBool_FromLong(PQconnectionUsedPassword(self->conn->pgconn));
}
@@ -398,8 +390,7 @@ ssl_in_use_get(connInfoObject *self)
PyObject *rv = NULL;
#if PG_VERSION_NUM >= 90500
- rv = PQsslInUse(self->conn->pgconn) ? Py_True : Py_False;
- Py_INCREF(rv);
+ rv = PyBool_FromLong(PQsslInUse(self->conn->pgconn));
#else
PyErr_SetString(NotSupportedError,
"'ssl_in_use' not available in libpq < 9.5");
diff --git a/psycopg/cursor_type.c b/psycopg/cursor_type.c
index 5920c12..d5d12f0 100644
--- a/psycopg/cursor_type.c
+++ b/psycopg/cursor_type.c
@@ -1667,12 +1667,7 @@ exit:
static PyObject *
psyco_curs_get_closed(cursorObject *self, void *closure)
{
- PyObject *closed;
-
- closed = (self->closed || (self->conn && self->conn->closed)) ?
- Py_True : Py_False;
- Py_INCREF(closed);
- return closed;
+ return PyBool_FromLong(self->closed || (self->conn && self->conn->closed));
}
/* extension: withhold - get or set "WITH HOLD" for named cursors */
@@ -1683,10 +1678,7 @@ psyco_curs_get_closed(cursorObject *self, void *closure)
static PyObject *
psyco_curs_withhold_get(cursorObject *self)
{
- PyObject *ret;
- ret = self->withhold ? Py_True : Py_False;
- Py_INCREF(ret);
- return ret;
+ return PyBool_FromLong(self->withhold);
}
int
diff --git a/psycopg/lobject_type.c b/psycopg/lobject_type.c
index 81c5675..7c27a7d 100644
--- a/psycopg/lobject_type.c
+++ b/psycopg/lobject_type.c
@@ -257,11 +257,7 @@ psyco_lobj_export(lobjectObject *self, PyObject *args)
static PyObject *
psyco_lobj_get_closed(lobjectObject *self, void *closure)
{
- PyObject *closed;
-
- closed = lobject_is_closed(self) ? Py_True : Py_False;
- Py_INCREF(closed);
- return closed;
+ return PyBool_FromLong(lobject_is_closed(self));
}
#define psyco_lobj_truncate_doc \
diff --git a/psycopg/typecast.c b/psycopg/typecast.c
index cf19fcd..005cce0 100644
--- a/psycopg/typecast.c
+++ b/psycopg/typecast.c
@@ -395,18 +395,11 @@ typecast_cmp(PyObject *obj1, PyObject* obj2)
static PyObject*
typecast_richcompare(PyObject *obj1, PyObject* obj2, int opid)
{
- PyObject *result = NULL;
int res = typecast_cmp(obj1, obj2);
if (PyErr_Occurred()) return NULL;
- if ((opid == Py_EQ && res == 0) || (opid != Py_EQ && res != 0))
- result = Py_True;
- else
- result = Py_False;
-
- Py_INCREF(result);
- return result;
+ return PyBool_FromLong((opid == Py_EQ && res == 0) || (opid != Py_EQ && res != 0));
}
static struct PyMemberDef typecastObject_members[] = {