Simplify PyBool usage with Python convenience macros/functions

https://docs.python.org/3/c-api/bool.html
This commit is contained in:
Jon Dufresne 2018-11-30 16:54:24 -08:00
parent 483901ea7b
commit b796ca0c0a
5 changed files with 12 additions and 47 deletions

View File

@ -616,10 +616,7 @@ psyco_conn_set_session(connectionObject *self, PyObject *args, PyObject *kwargs)
static PyObject * static PyObject *
psyco_conn_autocommit_get(connectionObject *self) psyco_conn_autocommit_get(connectionObject *self)
{ {
PyObject *ret; return PyBool_FromLong(self->autocommit);
ret = self->autocommit ? Py_True : Py_False;
Py_INCREF(ret);
return ret;
} }
BORROWED static PyObject * BORROWED static PyObject *
@ -1100,25 +1097,21 @@ psyco_conn_isexecuting(connectionObject *self)
{ {
/* synchronous connections will always return False */ /* synchronous connections will always return False */
if (self->async == 0) { if (self->async == 0) {
Py_INCREF(Py_False); Py_RETURN_FALSE;
return Py_False;
} }
/* check if the connection is still being built */ /* check if the connection is still being built */
if (self->status != CONN_STATUS_READY) { if (self->status != CONN_STATUS_READY) {
Py_INCREF(Py_True); Py_RETURN_TRUE;
return Py_True;
} }
/* check if there is a query being executed */ /* check if there is a query being executed */
if (self->async_cursor != NULL) { if (self->async_cursor != NULL) {
Py_INCREF(Py_True); Py_RETURN_TRUE;
return Py_True;
} }
/* otherwise it's not executing */ /* otherwise it's not executing */
Py_INCREF(Py_False); Py_RETURN_FALSE;
return Py_False;
} }

View File

@ -352,11 +352,7 @@ static const char needs_password_doc[] =
static PyObject * static PyObject *
needs_password_get(connInfoObject *self) needs_password_get(connInfoObject *self)
{ {
PyObject *rv; return PyBool_FromLong(PQconnectionNeedsPassword(self->conn->pgconn));
rv = PQconnectionNeedsPassword(self->conn->pgconn) ? Py_True : Py_False;
Py_INCREF(rv);
return rv;
} }
@ -372,11 +368,7 @@ static const char used_password_doc[] =
static PyObject * static PyObject *
used_password_get(connInfoObject *self) used_password_get(connInfoObject *self)
{ {
PyObject *rv; return PyBool_FromLong(PQconnectionUsedPassword(self->conn->pgconn));
rv = PQconnectionUsedPassword(self->conn->pgconn) ? Py_True : Py_False;
Py_INCREF(rv);
return rv;
} }
@ -398,8 +390,7 @@ ssl_in_use_get(connInfoObject *self)
PyObject *rv = NULL; PyObject *rv = NULL;
#if PG_VERSION_NUM >= 90500 #if PG_VERSION_NUM >= 90500
rv = PQsslInUse(self->conn->pgconn) ? Py_True : Py_False; rv = PyBool_FromLong(PQsslInUse(self->conn->pgconn));
Py_INCREF(rv);
#else #else
PyErr_SetString(NotSupportedError, PyErr_SetString(NotSupportedError,
"'ssl_in_use' not available in libpq < 9.5"); "'ssl_in_use' not available in libpq < 9.5");

View File

@ -1667,12 +1667,7 @@ exit:
static PyObject * static PyObject *
psyco_curs_get_closed(cursorObject *self, void *closure) psyco_curs_get_closed(cursorObject *self, void *closure)
{ {
PyObject *closed; return PyBool_FromLong(self->closed || (self->conn && self->conn->closed));
closed = (self->closed || (self->conn && self->conn->closed)) ?
Py_True : Py_False;
Py_INCREF(closed);
return closed;
} }
/* extension: withhold - get or set "WITH HOLD" for named cursors */ /* extension: withhold - get or set "WITH HOLD" for named cursors */
@ -1683,10 +1678,7 @@ psyco_curs_get_closed(cursorObject *self, void *closure)
static PyObject * static PyObject *
psyco_curs_withhold_get(cursorObject *self) psyco_curs_withhold_get(cursorObject *self)
{ {
PyObject *ret; return PyBool_FromLong(self->withhold);
ret = self->withhold ? Py_True : Py_False;
Py_INCREF(ret);
return ret;
} }
int int

View File

@ -257,11 +257,7 @@ psyco_lobj_export(lobjectObject *self, PyObject *args)
static PyObject * static PyObject *
psyco_lobj_get_closed(lobjectObject *self, void *closure) psyco_lobj_get_closed(lobjectObject *self, void *closure)
{ {
PyObject *closed; return PyBool_FromLong(lobject_is_closed(self));
closed = lobject_is_closed(self) ? Py_True : Py_False;
Py_INCREF(closed);
return closed;
} }
#define psyco_lobj_truncate_doc \ #define psyco_lobj_truncate_doc \

View File

@ -395,18 +395,11 @@ typecast_cmp(PyObject *obj1, PyObject* obj2)
static PyObject* static PyObject*
typecast_richcompare(PyObject *obj1, PyObject* obj2, int opid) typecast_richcompare(PyObject *obj1, PyObject* obj2, int opid)
{ {
PyObject *result = NULL;
int res = typecast_cmp(obj1, obj2); int res = typecast_cmp(obj1, obj2);
if (PyErr_Occurred()) return NULL; if (PyErr_Occurred()) return NULL;
if ((opid == Py_EQ && res == 0) || (opid != Py_EQ && res != 0)) return PyBool_FromLong((opid == Py_EQ && res == 0) || (opid != Py_EQ && res != 0));
result = Py_True;
else
result = Py_False;
Py_INCREF(result);
return result;
} }
static struct PyMemberDef typecastObject_members[] = { static struct PyMemberDef typecastObject_members[] = {