_posixsubprocess_helpers.c 4.6 KB

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