Simplify code to take advantage of feature added in Python 3.3.

This commit is contained in:
Anthony Tuininga 2021-04-23 13:48:53 -06:00
parent 0edb436abf
commit cccfa322c7
7 changed files with 49 additions and 104 deletions

View File

@ -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, &params.subscrNamespace, &params.protocol, &callback,
&params.timeout, &params.operations, &params.portNumber,
&params.qos, &ipAddress, &params.groupingClass,
&params.groupingValue, &params.groupingType, &name,
&clientInitiatedObj))
&params.clientInitiated))
return NULL;
if (cxoConnection_isConnected(conn) < 0)
return NULL;
if (cxoUtils_getBooleanValue(clientInitiatedObj, 0,
&params.clientInitiated) < 0)
return NULL;
// populate IP address in parameters, if applicable
cxoBuffer_init(&ipAddressBuffer);

View File

@ -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;

View File

@ -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);

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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.