mirror of
https://gitlab.com/libvirt/libvirt-python.git
synced 2025-08-09 05:49:26 +03:00
virPyDictToTypedParams: packing lists of values
Pack a list or a tuple of values passed to a Python method to the multi-value parameter.
This commit is contained in:
committed by
Michal Privoznik
parent
7acf1422c5
commit
9896626b82
@ -278,46 +278,17 @@ typedef virPyTypedParamsHint *virPyTypedParamsHintPtr;
|
||||
# define libvirt_PyString_Check PyString_Check
|
||||
# endif
|
||||
|
||||
/* Automatically convert dict into type parameters based on types reported
|
||||
* by python. All integer types are converted into LLONG (in case of a negative
|
||||
* value) or ULLONG (in case of a positive value). If you need different
|
||||
* handling, use @hints to explicitly specify what types should be used for
|
||||
* specific parameters.
|
||||
*/
|
||||
static int
|
||||
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3)
|
||||
virPyDictToTypedParams(PyObject *dict,
|
||||
virTypedParameterPtr *ret_params,
|
||||
int *ret_nparams,
|
||||
virPyDictToTypedParamOne(virTypedParameterPtr *params,
|
||||
int *n,
|
||||
int *max,
|
||||
virPyTypedParamsHintPtr hints,
|
||||
int nhints)
|
||||
int nhints,
|
||||
const char *keystr,
|
||||
PyObject *value)
|
||||
{
|
||||
PyObject *key;
|
||||
PyObject *value;
|
||||
#if PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION <= 4
|
||||
int pos = 0;
|
||||
#else
|
||||
Py_ssize_t pos = 0;
|
||||
#endif
|
||||
virTypedParameterPtr params = NULL;
|
||||
int rv = -1, type = -1;
|
||||
size_t i;
|
||||
int n = 0;
|
||||
int max = 0;
|
||||
int ret = -1;
|
||||
char *keystr = NULL;
|
||||
|
||||
*ret_params = NULL;
|
||||
*ret_nparams = 0;
|
||||
|
||||
if (PyDict_Size(dict) < 0)
|
||||
return -1;
|
||||
|
||||
while (PyDict_Next(dict, &pos, &key, &value)) {
|
||||
int type = -1;
|
||||
|
||||
if (libvirt_charPtrUnwrap(key, &keystr) < 0 ||
|
||||
!keystr)
|
||||
goto cleanup;
|
||||
|
||||
for (i = 0; i < nhints; i++) {
|
||||
if (STREQ(hints[i].name, keystr)) {
|
||||
@ -360,7 +331,7 @@ virPyDictToTypedParams(PyObject *dict,
|
||||
{
|
||||
int val;
|
||||
if (libvirt_intUnwrap(value, &val) < 0 ||
|
||||
virTypedParamsAddInt(¶ms, &n, &max, keystr, val) < 0)
|
||||
virTypedParamsAddInt(params, n, max, keystr, val) < 0)
|
||||
goto cleanup;
|
||||
break;
|
||||
}
|
||||
@ -368,7 +339,7 @@ virPyDictToTypedParams(PyObject *dict,
|
||||
{
|
||||
unsigned int val;
|
||||
if (libvirt_uintUnwrap(value, &val) < 0 ||
|
||||
virTypedParamsAddUInt(¶ms, &n, &max, keystr, val) < 0)
|
||||
virTypedParamsAddUInt(params, n, max, keystr, val) < 0)
|
||||
goto cleanup;
|
||||
break;
|
||||
}
|
||||
@ -376,7 +347,7 @@ virPyDictToTypedParams(PyObject *dict,
|
||||
{
|
||||
long long val;
|
||||
if (libvirt_longlongUnwrap(value, &val) < 0 ||
|
||||
virTypedParamsAddLLong(¶ms, &n, &max, keystr, val) < 0)
|
||||
virTypedParamsAddLLong(params, n, max, keystr, val) < 0)
|
||||
goto cleanup;
|
||||
break;
|
||||
}
|
||||
@ -384,7 +355,7 @@ virPyDictToTypedParams(PyObject *dict,
|
||||
{
|
||||
unsigned long long val;
|
||||
if (libvirt_ulonglongUnwrap(value, &val) < 0 ||
|
||||
virTypedParamsAddULLong(¶ms, &n, &max, keystr, val) < 0)
|
||||
virTypedParamsAddULLong(params, n, max, keystr, val) < 0)
|
||||
goto cleanup;
|
||||
break;
|
||||
}
|
||||
@ -392,7 +363,7 @@ virPyDictToTypedParams(PyObject *dict,
|
||||
{
|
||||
double val;
|
||||
if (libvirt_doubleUnwrap(value, &val) < 0 ||
|
||||
virTypedParamsAddDouble(¶ms, &n, &max, keystr, val) < 0)
|
||||
virTypedParamsAddDouble(params, n, max, keystr, val) < 0)
|
||||
goto cleanup;
|
||||
break;
|
||||
}
|
||||
@ -400,7 +371,7 @@ virPyDictToTypedParams(PyObject *dict,
|
||||
{
|
||||
bool val;
|
||||
if (libvirt_boolUnwrap(value, &val) < 0 ||
|
||||
virTypedParamsAddBoolean(¶ms, &n, &max, keystr, val) < 0)
|
||||
virTypedParamsAddBoolean(params, n, max, keystr, val) < 0)
|
||||
goto cleanup;
|
||||
break;
|
||||
}
|
||||
@ -409,7 +380,7 @@ virPyDictToTypedParams(PyObject *dict,
|
||||
char *val;;
|
||||
if (libvirt_charPtrUnwrap(value, &val) < 0 ||
|
||||
!val ||
|
||||
virTypedParamsAddString(¶ms, &n, &max, keystr, val) < 0) {
|
||||
virTypedParamsAddString(params, n, max, keystr, val) < 0) {
|
||||
VIR_FREE(val);
|
||||
goto cleanup;
|
||||
}
|
||||
@ -419,6 +390,65 @@ virPyDictToTypedParams(PyObject *dict,
|
||||
case VIR_TYPED_PARAM_LAST:
|
||||
break; /* unreachable */
|
||||
}
|
||||
|
||||
rv = 0;
|
||||
|
||||
cleanup:
|
||||
return rv;
|
||||
}
|
||||
|
||||
|
||||
/* Automatically convert dict into type parameters based on types reported
|
||||
* by python. All integer types are converted into LLONG (in case of a negative
|
||||
* value) or ULLONG (in case of a positive value). If you need different
|
||||
* handling, use @hints to explicitly specify what types should be used for
|
||||
* specific parameters.
|
||||
*/
|
||||
static int
|
||||
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3)
|
||||
virPyDictToTypedParams(PyObject *dict,
|
||||
virTypedParameterPtr *ret_params,
|
||||
int *ret_nparams,
|
||||
virPyTypedParamsHintPtr hints,
|
||||
int nhints)
|
||||
{
|
||||
PyObject *key;
|
||||
PyObject *value;
|
||||
#if PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION <= 4
|
||||
int pos = 0;
|
||||
#else
|
||||
Py_ssize_t pos = 0;
|
||||
#endif
|
||||
virTypedParameterPtr params = NULL;
|
||||
int n = 0;
|
||||
int max = 0;
|
||||
int ret = -1;
|
||||
char *keystr = NULL;
|
||||
|
||||
*ret_params = NULL;
|
||||
*ret_nparams = 0;
|
||||
|
||||
if (PyDict_Size(dict) < 0)
|
||||
return -1;
|
||||
|
||||
while (PyDict_Next(dict, &pos, &key, &value)) {
|
||||
if (libvirt_charPtrUnwrap(key, &keystr) < 0 ||
|
||||
!keystr)
|
||||
goto cleanup;
|
||||
|
||||
if (PyList_Check(value) || PyTuple_Check(value)) {
|
||||
Py_ssize_t i, size = PySequence_Size(value);
|
||||
|
||||
for (i = 0; i < size; i++) {
|
||||
PyObject *v = PySequence_ITEM(value, i);
|
||||
if (virPyDictToTypedParamOne(¶ms, &n, &max,
|
||||
hints, nhints, keystr, v) < 0)
|
||||
goto cleanup;
|
||||
}
|
||||
} else if (virPyDictToTypedParamOne(¶ms, &n, &max,
|
||||
hints, nhints, keystr, value) < 0)
|
||||
goto cleanup;
|
||||
|
||||
VIR_FREE(keystr);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user