1
0
mirror of https://github.com/samba-team/samba.git synced 2024-12-23 17:34:34 +03:00

pyldb: Fixes and Python3 compat for Dn component accessors

Use "s#"/"z#" argument specifiers in set_component and
set_extended_component instead of converting strings manually.
(Under Python 3, This means both text strings and bytes are accepted.)

Raise error on set_component(None), instead of crashing.

Return text strings from get_{extended}_component under Python 3.

Signed-off-by: Petr Viktorin <pviktori@redhat.com>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
Reviewed-by: Stefan Metzmacher <metze@samba.org>
This commit is contained in:
Petr Viktorin 2015-08-21 10:10:28 +02:00 committed by Stefan Metzmacher
parent a4d9c87ced
commit dd7baa2ae2

View File

@ -253,6 +253,11 @@ static PyObject *PyObject_FromLdbValue(const struct ldb_val *val)
return PyBytes_FromStringAndSize((const char *)val->data, val->length); return PyBytes_FromStringAndSize((const char *)val->data, val->length);
} }
static PyObject *PyStr_FromLdbValue(const struct ldb_val *val)
{
return PyStr_FromStringAndSize((const char *)val->data, val->length);
}
/** /**
* Create a Python object from a ldb_result. * Create a Python object from a ldb_result.
* *
@ -481,23 +486,19 @@ static PyObject *py_ldb_dn_get_extended_component(PyLdbDnObject *self, PyObject
static PyObject *py_ldb_dn_set_extended_component(PyLdbDnObject *self, PyObject *args) static PyObject *py_ldb_dn_set_extended_component(PyLdbDnObject *self, PyObject *args)
{ {
char *name; char *name;
PyObject *value; int err;
int err, result; uint8_t *value;
Py_ssize_t size; int size = 0;
if (!PyArg_ParseTuple(args, "sO", &name, &value)) if (!PyArg_ParseTuple(args, "sz#", &name, (const char**)&value, &size))
return NULL; return NULL;
if (value == Py_None) { if (value == NULL) {
err = ldb_dn_set_extended_component(self->dn, name, NULL); err = ldb_dn_set_extended_component(self->dn, name, NULL);
} else { } else {
struct ldb_val val; struct ldb_val val;
result = PyBytes_AsStringAndSize(value, (char **) &val.data, &size); val.data = (uint8_t *)value;
val.length = size; val.length = size;
if (result != 0) {
PyErr_SetString(PyExc_TypeError, "Expected a bytestring argument");
return NULL;
}
err = ldb_dn_set_extended_component(self->dn, name, &val); err = ldb_dn_set_extended_component(self->dn, name, &val);
} }
@ -663,29 +664,22 @@ static PyObject *py_ldb_dn_get_component_value(PyLdbDnObject *self, PyObject *ar
Py_RETURN_NONE; Py_RETURN_NONE;
} }
return PyObject_FromLdbValue(val); return PyStr_FromLdbValue(val);
} }
static PyObject *py_ldb_dn_set_component(PyLdbDnObject *self, PyObject *args) static PyObject *py_ldb_dn_set_component(PyLdbDnObject *self, PyObject *args)
{ {
unsigned int num = 0; unsigned int num = 0;
char *name = NULL; char *name = NULL, *value = NULL;
PyObject *value = Py_None;
struct ldb_val val = { NULL, }; struct ldb_val val = { NULL, };
int err, ret; int err;
Py_ssize_t size; Py_ssize_t size = 0;
if (!PyArg_ParseTuple(args, "IsO", &num, &name, &value)) if (!PyArg_ParseTuple(args, "Iss#", &num, &name, &value, &size))
return NULL; return NULL;
if (value != Py_None) { val.data = (unsigned char*) value;
ret = PyBytes_AsStringAndSize(value, (char **) &val.data, &size); val.length = size;
if (ret != 0) {
PyErr_SetString(PyExc_TypeError, "Expected a bytestring argument");
return NULL;
}
val.length = size;
}
err = ldb_dn_set_component(self->dn, num, name, val); err = ldb_dn_set_component(self->dn, num, name, val);
if (err != LDB_SUCCESS) { if (err != LDB_SUCCESS) {
@ -723,7 +717,7 @@ static PyObject *py_ldb_dn_get_rdn_value(PyLdbDnObject *self)
Py_RETURN_NONE; Py_RETURN_NONE;
} }
return PyObject_FromLdbValue(val); return PyStr_FromLdbValue(val);
} }
static PyMethodDef py_ldb_dn_methods[] = { static PyMethodDef py_ldb_dn_methods[] = {