File: berval.c

package info (click to toggle)
python-ldap 3.4.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,756 kB
  • sloc: python: 9,558; ansic: 3,052; makefile: 139; sh: 79
file content (51 lines) | stat: -rw-r--r-- 1,169 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/* See https://www.python-ldap.org/ for details. */

#include "common.h"
#include "berval.h"

/*
 * Copies out the data from a berval, and returns it as a new Python object,
 * Returns None if the berval pointer is NULL.
 *
 * Note that this function is not the exact inverse of LDAPberval_from_object
 * with regards to the NULL/None conversion.
 *
 * Returns a new Python object on success, or NULL on failure.
 */
PyObject *
LDAPberval_to_object(const struct berval *bv)
{
    PyObject *ret = NULL;

    if (!bv || !bv->bv_val) {
        ret = Py_None;
        Py_INCREF(ret);
    }
    else {
        ret = PyBytes_FromStringAndSize(bv->bv_val, bv->bv_len);
    }

    return ret;
}

/*
 * Same as LDAPberval_to_object, but returns a Unicode PyObject.
 * Use when the value is known to be text (for instance a distinguishedName).
 *
 * Returns a new Python object on success, or NULL on failure.
 */
PyObject *
LDAPberval_to_unicode_object(const struct berval *bv)
{
    PyObject *ret = NULL;

    if (!bv) {
        ret = Py_None;
        Py_INCREF(ret);
    }
    else {
        ret = PyUnicode_FromStringAndSize(bv->bv_val, bv->bv_len);
    }

    return ret;
}