aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/deprecated/python/subprocess32/_posixsubprocess_helpers.c
blob: 73f6b6c05acc670a6551785c6c2fc6c4f2ab08e8 (plain) (blame)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/* Functions and macros from Python 3.2 not found in 2.x.
   This file is #included by _posixsubprocess.c and the functions
   are declared static to avoid exposing them outside this module. */

/* _posixsubprocess_config.h was already included by _posixsubprocess.c
 * which is #include'ing us despite the .c name.  HAVE_SIGNAL_H comes
 * from there.  Yes, confusing! */
#ifdef HAVE_SIGNAL_H
#include <signal.h>
#endif
#include "unicodeobject.h"

#if (PY_VERSION_HEX < 0x02050000)
#define Py_ssize_t      int
#endif

#define Py_CLEANUP_SUPPORTED 0x20000

/* Issue #1983: pid_t can be longer than a C long on some systems */
#if !defined(SIZEOF_PID_T) || SIZEOF_PID_T == SIZEOF_INT
#define PyLong_FromPid PyLong_FromLong
#elif SIZEOF_PID_T == SIZEOF_LONG
#define PyLong_FromPid PyLong_FromLong
#elif defined(SIZEOF_LONG_LONG) && SIZEOF_PID_T == SIZEOF_LONG_LONG
#define PyLong_FromPid PyLong_FromLongLong
#else
#error "sizeof(pid_t) is neither sizeof(int), sizeof(long) or sizeof(long long)"
#endif /* SIZEOF_PID_T */


static PyObject *PyUnicode_EncodeFSDefault(PyObject *unicode)
{
    if (Py_FileSystemDefaultEncoding)
        return PyUnicode_AsEncodedString(unicode,
                                         Py_FileSystemDefaultEncoding,
                                         "strict");
    else
        return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode),
                                    PyUnicode_GET_SIZE(unicode),
                                    "strict");
}


/* Convert the argument to a bytes object, according to the file
   system encoding.  The addr param must be a PyObject**.
   This is designed to be used with "O&" in PyArg_Parse APIs. */

static int
PyUnicode_FSConverter(PyObject* arg, void* addr)
{
    PyObject *output = NULL;
    Py_ssize_t size;
    void *data;
    if (arg == NULL) {
        Py_DECREF(*(PyObject**)addr);
        return 1;
    }
    if (PyString_Check(arg)) {
        output = arg;
        Py_INCREF(output);
    }
    else {
        arg = PyUnicode_FromObject(arg);
        if (!arg)
            return 0;
        output = PyUnicode_EncodeFSDefault(arg);
        Py_DECREF(arg);
        if (!output)
            return 0;
        if (!PyString_Check(output)) {
            Py_DECREF(output);
            PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes");
            return 0;
        }
    }
    size = PyString_GET_SIZE(output);
    data = PyString_AS_STRING(output);
    if (size != strlen(data)) {
        PyErr_SetString(PyExc_TypeError, "embedded NUL character");
        Py_DECREF(output);
        return 0;
    }
    *(PyObject**)addr = output;
    return Py_CLEANUP_SUPPORTED;
}


/* Free's a NULL terminated char** array of C strings. */
static void
_Py_FreeCharPArray(char *const array[])
{
    Py_ssize_t i;
    for (i = 0; array[i] != NULL; ++i) {
        free(array[i]);
    }
    free((void*)array);
}


/*
 * Flatten a sequence of bytes() objects into a C array of
 * NULL terminated string pointers with a NULL char* terminating the array.
 * (ie: an argv or env list)
 *
 * Memory allocated for the returned list is allocated using malloc() and MUST
 * be freed by the caller using a free() loop or _Py_FreeCharPArray().
 */
static char *const *
_PySequence_BytesToCharpArray(PyObject* self)
{
    char **array;
    Py_ssize_t i, argc;
    PyObject *item = NULL;

    argc = PySequence_Size(self);
    if (argc == -1)
        return NULL;
    /* Avoid 32-bit overflows to malloc() from unreasonable values. */
    if (argc > 0x10000000) {
        PyErr_NoMemory();
        return NULL;
    }

    array = malloc((argc + 1) * sizeof(char *));
    if (array == NULL) {
        PyErr_NoMemory();
        return NULL;
    }
    for (i = 0; i < argc; ++i) {
        char *data;
        item = PySequence_GetItem(self, i);
        data = PyString_AsString(item);
        if (data == NULL) {
            /* NULL terminate before freeing. */
            array[i] = NULL;
            goto fail;
        }
        array[i] = strdup(data);
        if (!array[i]) {
            PyErr_NoMemory();
            goto fail;
        }
        Py_DECREF(item);
    }
    array[argc] = NULL;

    return array;

fail:
    Py_XDECREF(item);
    _Py_FreeCharPArray(array);
    return NULL;
}


/* Restore signals that the interpreter has called SIG_IGN on to SIG_DFL.
 *
 * All of the code in this function must only use async-signal-safe functions,
 * listed at `man 7 signal` or
 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
 */
static void
_Py_RestoreSignals(void)
{
#ifdef SIGPIPE
    PyOS_setsig(SIGPIPE, SIG_DFL);
#endif
#ifdef SIGXFZ
    PyOS_setsig(SIGXFZ, SIG_DFL);
#endif
#ifdef SIGXFSZ
    PyOS_setsig(SIGXFSZ, SIG_DFL);
#endif
}