From cccfa322c70585a2d3c5a540c9638efdac6a93d9 Mon Sep 17 00:00:00 2001 From: Anthony Tuininga Date: Fri, 23 Apr 2021 13:48:53 -0600 Subject: [PATCH] Simplify code to take advantage of feature added in Python 3.3. --- src/cxoConnection.c | 57 ++++++++++++++++------------------------- src/cxoCursor.c | 9 +++---- src/cxoModule.h | 1 - src/cxoSessionPool.c | 45 +++++++++++--------------------- src/cxoSodaCollection.c | 10 +++----- src/cxoSodaDatabase.c | 14 ++++------ src/cxoUtils.c | 17 ------------ 7 files changed, 49 insertions(+), 104 deletions(-) diff --git a/src/cxoConnection.c b/src/cxoConnection.c index 69c53ff..2474edf 100644 --- a/src/cxoConnection.c +++ b/src/cxoConnection.c @@ -467,14 +467,14 @@ static int cxoConnection_splitComponent(PyObject *sourceObj, static int cxoConnection_init(cxoConnection *conn, PyObject *args, PyObject *keywordArgs) { - PyObject *tagObj, *matchAnyTagObj, *threadedObj, *eventsObj, *contextObj; PyObject *usernameObj, *passwordObj, *dsnObj, *cclassObj, *editionObj; + int status, temp, invokeSessionCallback, threaded, events; PyObject *shardingKeyObj, *superShardingKeyObj, *tempObj; - int status, temp, invokeSessionCallback; PyObject *beforePartObj, *afterPartObj; dpiCommonCreateParams dpiCommonParams; dpiConnCreateParams dpiCreateParams; unsigned long long externalHandle; + PyObject *tagObj, *contextObj; unsigned int stmtCacheSize; cxoConnectionParams params; PyObject *newPasswordObj; @@ -490,10 +490,11 @@ static int cxoConnection_init(cxoConnection *conn, PyObject *args, // parse arguments pool = NULL; tagObj = Py_None; + threaded = 0; externalHandle = 0; + newPasswordObj = usernameObj = NULL; passwordObj = dsnObj = cclassObj = editionObj = NULL; - threadedObj = eventsObj = newPasswordObj = usernameObj = NULL; - matchAnyTagObj = contextObj = shardingKeyObj = superShardingKeyObj = NULL; + contextObj = shardingKeyObj = superShardingKeyObj = NULL; stmtCacheSize = DPI_DEFAULT_STMT_CACHE_SIZE; if (cxoUtils_initializeDPI(NULL) < 0) return -1; @@ -502,26 +503,19 @@ static int cxoConnection_init(cxoConnection *conn, PyObject *args, if (dpiContext_initConnCreateParams(cxoDpiContext, &dpiCreateParams) < 0) return cxoError_raiseAndReturnInt(); if (!PyArg_ParseTupleAndKeywords(args, keywordArgs, - "|OOOiKO!OOOiOssOOOOOOI", keywordList, &usernameObj, &passwordObj, + "|OOOiKO!ppOiOssOOOpOOI", keywordList, &usernameObj, &passwordObj, &dsnObj, &dpiCreateParams.authMode, &externalHandle, - &cxoPyTypeSessionPool, &pool, &threadedObj, &eventsObj, &cclassObj, + &cxoPyTypeSessionPool, &pool, &threaded, &events, &cclassObj, &dpiCreateParams.purity, &newPasswordObj, &dpiCommonParams.encoding, &dpiCommonParams.nencoding, &editionObj, - &contextObj, &tagObj, &matchAnyTagObj, &shardingKeyObj, - &superShardingKeyObj, &stmtCacheSize)) + &contextObj, &tagObj, &dpiCreateParams.matchAnyTag, + &shardingKeyObj, &superShardingKeyObj, &stmtCacheSize)) return -1; dpiCreateParams.externalHandle = (void*) externalHandle; - if (cxoUtils_getBooleanValue(threadedObj, 0, &temp) < 0) - return -1; - if (temp) + if (threaded) dpiCommonParams.createMode |= DPI_MODE_CREATE_THREADED; - if (cxoUtils_getBooleanValue(eventsObj, 0, &temp) < 0) - return -1; - if (temp) + if (events) dpiCommonParams.createMode |= DPI_MODE_CREATE_EVENTS; - if (cxoUtils_getBooleanValue(matchAnyTagObj, 0, - &dpiCreateParams.matchAnyTag) < 0) - return -1; // keep a copy of the user name and connect string (DSN) Py_XINCREF(usernameObj); @@ -1494,26 +1488,22 @@ static PyObject *cxoConnection_startup(cxoConnection *conn, PyObject* args, PyObject* keywordArgs) { static char *keywordList[] = { "force", "restrict", "pfile", NULL }; - PyObject *forceObj, *restrictObj, *pfileObj; + int temp, force, restrictStartup; cxoBuffer pfileBuffer; dpiStartupMode mode; - int temp; + PyObject *pfileObj; // parse arguments - forceObj = restrictObj = pfileObj = NULL; - if (!PyArg_ParseTupleAndKeywords(args, keywordArgs, "|OOO", keywordList, - &forceObj, &restrictObj, &pfileObj)) + pfileObj = NULL; + if (!PyArg_ParseTupleAndKeywords(args, keywordArgs, "|ppO", keywordList, + &force, &restrictStartup, &pfileObj)) return NULL; // set the flags to use during startup mode = DPI_MODE_STARTUP_DEFAULT; - if (cxoUtils_getBooleanValue(forceObj, 0, &temp) < 0) - return NULL; - if (temp) + if (force) mode |= DPI_MODE_STARTUP_FORCE; - if (cxoUtils_getBooleanValue(restrictObj, 0, &temp) < 0) - return NULL; - if (temp) + if (restrictStartup) mode |= DPI_MODE_STARTUP_RESTRICT; // check the pfile parameter @@ -1549,7 +1539,7 @@ static PyObject *cxoConnection_subscribe(cxoConnection *conn, PyObject* args, "timeout", "operations", "port", "qos", "ipAddress", "groupingClass", "groupingValue", "groupingType", "name", "clientInitiated", NULL }; - PyObject *callback, *ipAddress, *name, *clientInitiatedObj; + PyObject *callback, *ipAddress, *name; cxoBuffer ipAddressBuffer, nameBuffer; dpiSubscrCreateParams params; cxoSubscr *subscr; @@ -1559,19 +1549,16 @@ static PyObject *cxoConnection_subscribe(cxoConnection *conn, PyObject* args, return cxoError_raiseAndReturnNull(); // validate parameters - callback = name = ipAddress = clientInitiatedObj = NULL; - if (!PyArg_ParseTupleAndKeywords(args, keywordArgs, "|IIOIIIIObIbOO", + callback = name = ipAddress = NULL; + if (!PyArg_ParseTupleAndKeywords(args, keywordArgs, "|IIOIIIIObIbOp", keywordList, ¶ms.subscrNamespace, ¶ms.protocol, &callback, ¶ms.timeout, ¶ms.operations, ¶ms.portNumber, ¶ms.qos, &ipAddress, ¶ms.groupingClass, ¶ms.groupingValue, ¶ms.groupingType, &name, - &clientInitiatedObj)) + ¶ms.clientInitiated)) return NULL; if (cxoConnection_isConnected(conn) < 0) return NULL; - if (cxoUtils_getBooleanValue(clientInitiatedObj, 0, - ¶ms.clientInitiated) < 0) - return NULL; // populate IP address in parameters, if applicable cxoBuffer_init(&ipAddressBuffer); diff --git a/src/cxoCursor.c b/src/cxoCursor.c index 6988b73..a558835 100644 --- a/src/cxoCursor.c +++ b/src/cxoCursor.c @@ -34,15 +34,12 @@ static int cxoCursor_init(cxoCursor *cursor, PyObject *args, { static char *keywordList[] = { "connection", "scrollable", NULL }; cxoConnection *connection; - PyObject *scrollableObj; int isScrollable; // parse arguments - scrollableObj = NULL; - if (!PyArg_ParseTupleAndKeywords(args, keywordArgs, "O!|O", keywordList, - &cxoPyTypeConnection, &connection, &scrollableObj)) - return -1; - if (cxoUtils_getBooleanValue(scrollableObj, 0, &isScrollable) < 0) + isScrollable = 0; + if (!PyArg_ParseTupleAndKeywords(args, keywordArgs, "O!|p", keywordList, + &cxoPyTypeConnection, &connection, &isScrollable)) return -1; cursor->isScrollable = (char) isScrollable; diff --git a/src/cxoModule.h b/src/cxoModule.h index 736d71e..d449923 100644 --- a/src/cxoModule.h +++ b/src/cxoModule.h @@ -573,7 +573,6 @@ int cxoUtils_convertPythonValueToOciAttr(PyObject *value, unsigned attrType, uint32_t *ociValueLength, const char *encoding); PyObject *cxoUtils_formatString(const char *format, PyObject *args); const char *cxoUtils_getAdjustedEncoding(const char *encoding); -int cxoUtils_getBooleanValue(PyObject *obj, int defaultValue, int *value); int cxoUtils_getModuleAndName(PyTypeObject *type, PyObject **module, PyObject **name); int cxoUtils_initializeDPI(dpiContextCreateParams *params); diff --git a/src/cxoSessionPool.c b/src/cxoSessionPool.c index f3010cd..2e85e69 100644 --- a/src/cxoSessionPool.c +++ b/src/cxoSessionPool.c @@ -34,18 +34,17 @@ static int cxoSessionPool_init(cxoSessionPool *pool, PyObject *args, { uint32_t minSessions, maxSessions, sessionIncrement, maxSessionsPerShard; uint32_t waitTimeoutDeprecated, maxSessionsPerShardDeprecated; - PyObject *externalAuthObj, *editionObj, *sessionCallbackObjDeprecated; cxoBuffer userNameBuffer, passwordBuffer, dsnBuffer, editionBuffer; - PyObject *threadedObj, *eventsObj, *homogeneousObj, *passwordObj; - PyObject *usernameObj, *dsnObj, *sessionCallbackObj; + PyObject *usernameObj, *dsnObj, *sessionCallbackObj, *passwordObj; + PyObject *editionObj, *sessionCallbackObjDeprecated; dpiCommonCreateParams dpiCommonParams; uint32_t maxLifetimeSessionDeprecated; dpiPoolCreateParams dpiCreateParams; cxoBuffer sessionCallbackBuffer; + int status, threaded, events; PyTypeObject *connectionType; unsigned int stmtCacheSize; const char *encoding; - int status, temp; // define keyword arguments static char *keywordList[] = { "user", "password", "dsn", "min", "max", @@ -58,8 +57,7 @@ static int cxoSessionPool_init(cxoSessionPool *pool, PyObject *args, // parse arguments and keywords usernameObj = passwordObj = dsnObj = editionObj = Py_None; - externalAuthObj = sessionCallbackObj = sessionCallbackObjDeprecated = NULL; - threadedObj = eventsObj = homogeneousObj = passwordObj = NULL; + sessionCallbackObj = sessionCallbackObjDeprecated = passwordObj = NULL; connectionType = &cxoPyTypeConnection; minSessions = 1; maxSessions = 2; @@ -75,11 +73,11 @@ static int cxoSessionPool_init(cxoSessionPool *pool, PyObject *args, if (dpiContext_initPoolCreateParams(cxoDpiContext, &dpiCreateParams) < 0) return cxoError_raiseAndReturnInt(); if (!PyArg_ParseTupleAndKeywords(args, keywordArgs, - "|OOOiiiOObOOOssOiiiOiIiiOi", keywordList, &usernameObj, + "|OOOiiiOpbpppssOiiiOiIiiOi", keywordList, &usernameObj, &passwordObj, &dsnObj, &minSessions, &maxSessions, - &sessionIncrement, &connectionType, &threadedObj, - &dpiCreateParams.getMode, &eventsObj, &homogeneousObj, - &externalAuthObj, &dpiCommonParams.encoding, + &sessionIncrement, &connectionType, &threaded, + &dpiCreateParams.getMode, &events, &dpiCreateParams.homogeneous, + &dpiCreateParams.externalAuth, &dpiCommonParams.encoding, &dpiCommonParams.nencoding, &editionObj, &dpiCreateParams.timeout, &dpiCreateParams.waitTimeout, &dpiCreateParams.maxLifetimeSession, &sessionCallbackObj, &maxSessionsPerShard, &stmtCacheSize, @@ -96,20 +94,10 @@ static int cxoSessionPool_init(cxoSessionPool *pool, PyObject *args, "connectiontype must be a subclass of Connection"); return -1; } - if (cxoUtils_getBooleanValue(threadedObj, 0, &temp) < 0) - return -1; - if (temp) + if (threaded) dpiCommonParams.createMode |= DPI_MODE_CREATE_THREADED; - if (cxoUtils_getBooleanValue(eventsObj, 0, &temp) < 0) - return -1; - if (temp) + if (events) dpiCommonParams.createMode |= DPI_MODE_CREATE_EVENTS; - if (cxoUtils_getBooleanValue(externalAuthObj, 0, - &dpiCreateParams.externalAuth) < 0) - return -1; - if (cxoUtils_getBooleanValue(homogeneousObj, 1, - &dpiCreateParams.homogeneous) < 0) - return -1; // check duplicate parameters to ensure that both are not specified if (waitTimeoutDeprecated > 0) { @@ -294,18 +282,15 @@ static PyObject *cxoSessionPool_close(cxoSessionPool *pool, PyObject *args, PyObject *keywordArgs) { static char *keywordList[] = { "force", NULL }; - PyObject *forceObj; uint32_t closeMode; - int temp, status; + int status, force; // parse arguments - forceObj = NULL; - if (!PyArg_ParseTupleAndKeywords(args, keywordArgs, "|O", keywordList, - &forceObj)) + force = 0; + if (!PyArg_ParseTupleAndKeywords(args, keywordArgs, "|p", keywordList, + &force)) return NULL; - if (cxoUtils_getBooleanValue(forceObj, 0, &temp) < 0) - return NULL; - closeMode = (temp) ? DPI_MODE_POOL_CLOSE_FORCE : + closeMode = (force) ? DPI_MODE_POOL_CLOSE_FORCE : DPI_MODE_POOL_CLOSE_DEFAULT; // close pool diff --git a/src/cxoSodaCollection.c b/src/cxoSodaCollection.c index 13632dc..d78d31d 100644 --- a/src/cxoSodaCollection.c +++ b/src/cxoSodaCollection.c @@ -168,16 +168,14 @@ static PyObject *cxoSodaCollection_dropIndex(cxoSodaCollection *coll, { static char *keywordList[] = { "name", "force", NULL }; int status, isDropped, force; - PyObject *nameObj, *forceObj; cxoBuffer nameBuffer; + PyObject *nameObj; uint32_t flags; // parse arguments - forceObj = NULL; - if (!PyArg_ParseTupleAndKeywords(args, keywordArgs, "O|O", keywordList, - &nameObj, &forceObj)) - return NULL; - if (cxoUtils_getBooleanValue(forceObj, 0, &force) < 0) + force = 0; + if (!PyArg_ParseTupleAndKeywords(args, keywordArgs, "O|p", keywordList, + &nameObj, &force)) return NULL; // drop index diff --git a/src/cxoSodaDatabase.c b/src/cxoSodaDatabase.c index d4bea13..f1d02d2 100644 --- a/src/cxoSodaDatabase.c +++ b/src/cxoSodaDatabase.c @@ -99,8 +99,8 @@ static PyObject *cxoSodaDatabase_createCollection(cxoSodaDatabase *db, PyObject *args, PyObject *keywordArgs) { static char *keywordList[] = { "name", "metadata", "mapMode", NULL }; - PyObject *nameObj, *metadataObj, *mapModeObj; cxoBuffer nameBuffer, metadataBuffer; + PyObject *nameObj, *metadataObj; cxoSodaCollection *coll; const char *encoding; dpiSodaColl *handle; @@ -108,9 +108,10 @@ static PyObject *cxoSodaDatabase_createCollection(cxoSodaDatabase *db, uint32_t flags; // parse arguments - nameObj = metadataObj = mapModeObj = NULL; - if (!PyArg_ParseTupleAndKeywords(args, keywordArgs, "O|OO", keywordList, - &nameObj, &metadataObj, &mapModeObj)) + mapMode = 0; + nameObj = metadataObj = NULL; + if (!PyArg_ParseTupleAndKeywords(args, keywordArgs, "O|Op", keywordList, + &nameObj, &metadataObj, &mapMode)) return NULL; encoding = db->connection->encodingInfo.encoding; if (cxoBuffer_fromObject(&nameBuffer, nameObj, encoding) < 0) @@ -119,11 +120,6 @@ static PyObject *cxoSodaDatabase_createCollection(cxoSodaDatabase *db, cxoBuffer_clear(&nameBuffer); return NULL; } - if (cxoUtils_getBooleanValue(mapModeObj, 0, &mapMode) < 0) { - cxoBuffer_clear(&nameBuffer); - cxoBuffer_clear(&metadataBuffer); - return NULL; - } // create collection if (cxoConnection_getSodaFlags(db->connection, &flags) < 0) diff --git a/src/cxoUtils.c b/src/cxoUtils.c index c723b42..9bec666 100644 --- a/src/cxoUtils.c +++ b/src/cxoUtils.c @@ -178,23 +178,6 @@ const char *cxoUtils_getAdjustedEncoding(const char *encoding) } -//----------------------------------------------------------------------------- -// cxoUtils_getBooleanValue() -// Get a boolean value from a Python object. -//----------------------------------------------------------------------------- -int cxoUtils_getBooleanValue(PyObject *obj, int defaultValue, int *value) -{ - if (!obj) - *value = defaultValue; - else { - *value = PyObject_IsTrue(obj); - if (*value < 0) - return -1; - } - return 0; -} - - //----------------------------------------------------------------------------- // cxoUtils_getModuleAndName() // Return the module and name for the type.