1
#define NPY_NO_DEPRECATED_API NPY_API_VERSION
2

3
#define PY_SSIZE_T_CLEAN
4
#include <Python.h>
5

6
#include <locale.h>
7
#include <stdio.h>
8

9
#define _MULTIARRAYMODULE
10
#include "numpy/arrayobject.h"
11
#include "numpy/npy_math.h"
12

13
#include "npy_config.h"
14

15
#include "npy_pycompat.h"
16
#include "ctors.h"
17

18
/*
19
 * This file originally contained functions only needed on narrow builds of
20
 * Python for converting back and forth between the NumPy Unicode data-type
21
 * (always 4-bytes) and the Python Unicode scalar (2-bytes on a narrow build).
22
 *
23
 * This "narrow" interface is now deprecated in python and unused in NumPy.
24
 */
25

26
/*
27
 * Returns a PyUnicodeObject initialized from a buffer containing
28
 * UCS4 unicode.
29
 *
30
 * Parameters
31
 * ----------
32
 *  src: char *
33
 *      Pointer to buffer containing UCS4 unicode.
34
 *  size: Py_ssize_t
35
 *      Size of buffer in bytes.
36
 *  swap: int
37
 *      If true, the data will be swapped.
38
 *  align: int
39
 *      If true, the data will be aligned.
40
 *
41
 * Returns
42
 * -------
43
 * new_reference: PyUnicodeObject
44
 */
45
NPY_NO_EXPORT PyUnicodeObject *
46 1
PyUnicode_FromUCS4(char const *src_char, Py_ssize_t size, int swap, int align)
47
{
48 1
    Py_ssize_t ucs4len = size / sizeof(npy_ucs4);
49 1
    npy_ucs4 const *src = (npy_ucs4 const *)src_char;
50 1
    npy_ucs4 *buf = NULL;
51

52
    /* swap and align if needed */
53 1
    if (swap || align) {
54 0
        buf = (npy_ucs4 *)malloc(size);
55 0
        if (buf == NULL) {
56 0
            PyErr_NoMemory();
57 0
            return NULL;
58
        }
59 0
        memcpy(buf, src, size);
60 0
        if (swap) {
61 0
            byte_swap_vector(buf, ucs4len, sizeof(npy_ucs4));
62
        }
63
        src = buf;
64
    }
65

66
    /* trim trailing zeros */
67 1
    while (ucs4len > 0 && src[ucs4len - 1] == 0) {
68 1
        ucs4len--;
69
    }
70 1
    PyUnicodeObject *ret = (PyUnicodeObject *)PyUnicode_FromKindAndData(
71
        PyUnicode_4BYTE_KIND, src, ucs4len);
72 1
    free(buf);
73 1
    return ret;
74
}

Read our documentation on viewing source code .

Loading