| OLD | NEW |
| (Empty) | |
| 1 /* Functions and macros from Python 3.2 not found in 2.x. |
| 2 This file is #included by _posixsubprocess.c and the functions |
| 3 are declared static to avoid exposing them outside this module. */ |
| 4 |
| 5 #include "unicodeobject.h" |
| 6 |
| 7 #if (PY_VERSION_HEX < 0x02050000) |
| 8 #define Py_ssize_t int |
| 9 #endif |
| 10 |
| 11 #define Py_CLEANUP_SUPPORTED 0x20000 |
| 12 |
| 13 /* Issue #1983: pid_t can be longer than a C long on some systems */ |
| 14 #if !defined(SIZEOF_PID_T) || SIZEOF_PID_T == SIZEOF_INT |
| 15 #define PyLong_FromPid PyLong_FromLong |
| 16 #elif SIZEOF_PID_T == SIZEOF_LONG |
| 17 #define PyLong_FromPid PyLong_FromLong |
| 18 #elif defined(SIZEOF_LONG_LONG) && SIZEOF_PID_T == SIZEOF_LONG_LONG |
| 19 #define PyLong_FromPid PyLong_FromLongLong |
| 20 #else |
| 21 #error "sizeof(pid_t) is neither sizeof(int), sizeof(long) or sizeof(long long)" |
| 22 #endif /* SIZEOF_PID_T */ |
| 23 |
| 24 |
| 25 static PyObject *PyUnicode_EncodeFSDefault(PyObject *unicode) |
| 26 { |
| 27 if (Py_FileSystemDefaultEncoding) |
| 28 return PyUnicode_AsEncodedString(unicode, |
| 29 Py_FileSystemDefaultEncoding, |
| 30 "strict"); |
| 31 else |
| 32 return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
| 33 PyUnicode_GET_SIZE(unicode), |
| 34 "strict"); |
| 35 } |
| 36 |
| 37 |
| 38 /* Convert the argument to a bytes object, according to the file |
| 39 system encoding. The addr param must be a PyObject**. |
| 40 This is designed to be used with "O&" in PyArg_Parse APIs. */ |
| 41 |
| 42 static int |
| 43 PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 44 { |
| 45 PyObject *output = NULL; |
| 46 Py_ssize_t size; |
| 47 void *data; |
| 48 if (arg == NULL) { |
| 49 Py_DECREF(*(PyObject**)addr); |
| 50 return 1; |
| 51 } |
| 52 if (PyString_Check(arg)) { |
| 53 output = arg; |
| 54 Py_INCREF(output); |
| 55 } |
| 56 else { |
| 57 arg = PyUnicode_FromObject(arg); |
| 58 if (!arg) |
| 59 return 0; |
| 60 output = PyUnicode_EncodeFSDefault(arg); |
| 61 Py_DECREF(arg); |
| 62 if (!output) |
| 63 return 0; |
| 64 if (!PyString_Check(output)) { |
| 65 Py_DECREF(output); |
| 66 PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 67 return 0; |
| 68 } |
| 69 } |
| 70 size = PyString_GET_SIZE(output); |
| 71 data = PyString_AS_STRING(output); |
| 72 if (size != strlen(data)) { |
| 73 PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 74 Py_DECREF(output); |
| 75 return 0; |
| 76 } |
| 77 *(PyObject**)addr = output; |
| 78 return Py_CLEANUP_SUPPORTED; |
| 79 } |
| 80 |
| 81 |
| 82 /* Free's a NULL terminated char** array of C strings. */ |
| 83 static void |
| 84 _Py_FreeCharPArray(char *const array[]) |
| 85 { |
| 86 Py_ssize_t i; |
| 87 for (i = 0; array[i] != NULL; ++i) { |
| 88 free(array[i]); |
| 89 } |
| 90 free((void*)array); |
| 91 } |
| 92 |
| 93 |
| 94 /* |
| 95 * Flatten a sequence of bytes() objects into a C array of |
| 96 * NULL terminated string pointers with a NULL char* terminating the array. |
| 97 * (ie: an argv or env list) |
| 98 * |
| 99 * Memory allocated for the returned list is allocated using malloc() and MUST |
| 100 * be freed by the caller using a free() loop or _Py_FreeCharPArray(). |
| 101 */ |
| 102 static char *const * |
| 103 _PySequence_BytesToCharpArray(PyObject* self) |
| 104 { |
| 105 char **array; |
| 106 Py_ssize_t i, argc; |
| 107 PyObject *item = NULL; |
| 108 |
| 109 argc = PySequence_Size(self); |
| 110 if (argc == -1) |
| 111 return NULL; |
| 112 |
| 113 array = malloc((argc + 1) * sizeof(char *)); |
| 114 if (array == NULL) { |
| 115 PyErr_NoMemory(); |
| 116 return NULL; |
| 117 } |
| 118 for (i = 0; i < argc; ++i) { |
| 119 char *data; |
| 120 item = PySequence_GetItem(self, i); |
| 121 data = PyString_AsString(item); |
| 122 if (data == NULL) { |
| 123 /* NULL terminate before freeing. */ |
| 124 array[i] = NULL; |
| 125 goto fail; |
| 126 } |
| 127 array[i] = strdup(data); |
| 128 if (!array[i]) { |
| 129 PyErr_NoMemory(); |
| 130 goto fail; |
| 131 } |
| 132 Py_DECREF(item); |
| 133 } |
| 134 array[argc] = NULL; |
| 135 |
| 136 return array; |
| 137 |
| 138 fail: |
| 139 Py_XDECREF(item); |
| 140 _Py_FreeCharPArray(array); |
| 141 return NULL; |
| 142 } |
| 143 |
| 144 |
| 145 /* Restore signals that the interpreter has called SIG_IGN on to SIG_DFL. |
| 146 * |
| 147 * All of the code in this function must only use async-signal-safe functions, |
| 148 * listed at `man 7 signal` or |
| 149 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html. |
| 150 */ |
| 151 static void |
| 152 _Py_RestoreSignals(void) |
| 153 { |
| 154 #ifdef SIGPIPE |
| 155 PyOS_setsig(SIGPIPE, SIG_DFL); |
| 156 #endif |
| 157 #ifdef SIGXFZ |
| 158 PyOS_setsig(SIGXFZ, SIG_DFL); |
| 159 #endif |
| 160 #ifdef SIGXFSZ |
| 161 PyOS_setsig(SIGXFSZ, SIG_DFL); |
| 162 #endif |
| 163 } |
| OLD | NEW |