annotate upreckon/_unixmodule.cpp @ 218:65b5c9390010

With _unix, Escape presses now cancel test data unarchiving
author Oleg Oshmyan <chortos@inbox.lv>
date Mon, 22 Aug 2011 22:34:09 +0300
parents 8c4e92fb32d8
children d06e57b182a9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
136
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1 // Copyright (c) 2011 Chortos-2 <chortos@inbox.lv>
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
2
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
3 #include <Python.h>
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
4 #include <structmember.h>
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
5
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
6 #ifdef HAVE_SYS_TYPES_H
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
7 #include <sys/types.h>
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
8 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
9
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
10 #ifdef HAVE_FCNTL_H
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
11 #include <fcntl.h>
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
12 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
13
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
14 #include <limits.h>
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
15
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
16 #ifdef HAVE_SIGNAL_H
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
17 #include <signal.h>
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
18 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
19
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
20 #ifdef HAVE_SPAWN_H
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
21 #include <spawn.h>
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
22 #ifdef __APPLE__
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
23 #pragma weak_import posix_spawnp
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
24 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
25 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
26
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
27 #ifdef HAVE_SYS_RESOURCE_H
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
28 #include <sys/resource.h>
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
29 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
30
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
31 #ifdef HAVE_SYS_WAIT_H
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
32 #include <sys/wait.h>
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
33 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
34
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
35 #ifdef HAVE_TERMIOS_H
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
36 #include <termios.h>
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
37 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
38
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
39 #if !(defined __cplusplus) && !(defined bool)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
40 #ifdef HAVE_C99_BOOL
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
41 #define bool _Bool
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
42 #else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
43 #define bool char
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
44 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
45 #undef true
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
46 #define true 1
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
47 #undef false
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
48 #define false 0
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
49 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
50
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
51 // On Python 2.5, SIGINT handling may get delayed until we return to Python
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
52 #if PY_MAJOR_VERSION > 2 || PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION >= 6
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
53 #define USE_WAKEUP_FD
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
54 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
55
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
56 #if !(defined RLIMIT_AS) && defined RLIMIT_VMEM
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
57 #define RLIMIT_AS RLIMIT_VMEM
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
58 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
59
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
60 // Condition stolen from posixmodule.c of Python 2.7.1
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
61 #if defined __USLC__ && defined __SCO_VERSION__ // SCO UDK Compiler
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
62 //#ifdef HAVE_FORK1
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
63 #define fork fork1
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
64 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
65
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
66 // Stolen from posixmodule.c of Python 2.7.1
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
67 #ifdef WITH_NEXT_FRAMEWORK
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
68 #include <crt_externs.h>
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
69 static char **environ = NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
70 #elif !(defined _MSC_VER) && (!(defined __WATCOMC__) || defined __QNX__)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
71 extern char **environ;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
72 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
73
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
74 #ifndef Py_PYTIME_H
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
75 typedef struct timeval _PyTime_timeval;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
76 #ifndef GETTIMEOFDAY_NO_TZ
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
77 #define _PyTime_gettimeofday(tvp) gettimeofday((tvp), NULL)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
78 #else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
79 #define _PyTime_gettimeofday(tvp) gettimeofday((tvp))
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
80 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
81 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
82
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
83 #if PY_MAJOR_VERSION >= 3
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
84 #define PyInt_AsLong PyLong_AsLong
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
85 #define PyInt_FromLong PyLong_FromLong
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
86 #define PyNumber_Int PyNumber_Long
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
87 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
88
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
89 #define TESTEE_SPAWNED 0
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
90 #define TESTEE_SPAWN_FAILED 1
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
91 #define TESTEE_REPORT_STATUS(status) \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
92 do \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
93 { \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
94 const char c = (status); \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
95 write(c2ppipe[1], &c, 1); \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
96 } \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
97 while (0)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
98
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
99 #if !(defined SIGKILL) && defined SIGTERM
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
100 #define SIGKILL SIGTERM
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
101 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
102
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
103 #if defined HAVE_KILL && defined SIGKILL
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
104 #ifdef HAVE_WAITPID
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
105 #define TERM_TESTEE \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
106 do \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
107 { \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
108 kill(-curpid, SIGKILL); \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
109 kill(-curpid, SIGCONT); \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
110 while (waitpid(curpid, &retstat, 0) != curpid); \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
111 } \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
112 while (0)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
113 #else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
114 #define TERM_TESTEE \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
115 do \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
116 { \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
117 kill(-curpid, SIGKILL); \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
118 kill(-curpid, SIGCONT); \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
119 while (wait(&retstat) != curpid); \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
120 } \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
121 while (0)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
122 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
123 #else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
124 #define TERM_TESTEE
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
125 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
126
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
127 #if defined HAVE_KILL && defined SIGINT
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
128 #define PROPAGATE_SIGINT ((void) kill(-curpid, SIGINT))
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
129 #else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
130 #define PROPAGATE_SIGINT
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
131 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
132
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
133 struct child_stats
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
134 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
135 int returncode;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
136 _PyTime_timeval walltime;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
137 #if defined HAVE_SYS_RESOURCE_H || defined HAVE_WAIT4 || defined HAVE_WAIT3
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
138 _PyTime_timeval cputime;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
139 Py_ssize_t memory;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
140 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
141 };
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
142
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
143 static pid_t curpid;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
144 static const struct child_stats zero_stats = { 0 };
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
145 static PyObject *CannotStartTestee, *CanceledByUser, *WallTimeLimitExceeded,
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
146 *CPUTimeLimitExceeded, *MemoryLimitExceeded;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
147 static _PyTime_timeval time_end;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
148
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
149 #ifdef USE_WAKEUP_FD
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
150 static char dont_care_buffer[512];
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
151 static int intpipe[2] = { 0 };
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
152 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
153
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
154 #ifdef HAVE_TERMIOS_H
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
155 static bool catch_escape = false;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
156 static struct termios orig_termios;
218
65b5c9390010 With _unix, Escape presses now cancel test data unarchiving
Oleg Oshmyan <chortos@inbox.lv>
parents: 210
diff changeset
157 #ifdef O_ASYNC
65b5c9390010 With _unix, Escape presses now cancel test data unarchiving
Oleg Oshmyan <chortos@inbox.lv>
parents: 210
diff changeset
158 static int orig_stdout_fl;
65b5c9390010 With _unix, Escape presses now cancel test data unarchiving
Oleg Oshmyan <chortos@inbox.lv>
parents: 210
diff changeset
159 #endif
136
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
160 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
161
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
162 typedef struct
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
163 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
164 PyObject_HEAD
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
165 int returncode;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
166 } _unix__PopenPlaceholderObject;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
167
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
168 static PyMemberDef _PopenPlaceholder_members[] =
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
169 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
170 { "returncode", T_INT, offsetof(_unix__PopenPlaceholderObject, returncode), READONLY, NULL },
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
171 { NULL }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
172 };
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
173
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
174 static PyTypeObject _unix__PopenPlaceholderType =
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
175 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
176 #if PY_MAJOR_VERSION >= 3
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
177 PyVarObject_HEAD_INIT(NULL, 0)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
178 #else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
179 PyObject_HEAD_INIT(NULL)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
180 0, /*ob_size*/
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
181 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
182 "_unix._PopenPlaceholder", /*tp_name*/
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
183 sizeof(_unix__PopenPlaceholderObject), /*tp_basicsize*/
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
184 0, /*tp_itemsize*/
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
185 0, /*tp_dealloc*/
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
186 0, /*tp_print*/
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
187 0, /*tp_getattr*/
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
188 0, /*tp_setattr*/
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
189 0, /*tp_compare*/
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
190 0, /*tp_repr*/
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
191 0, /*tp_as_number*/
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
192 0, /*tp_as_sequence*/
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
193 0, /*tp_as_mapping*/
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
194 0, /*tp_hash */
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
195 0, /*tp_call*/
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
196 0, /*tp_str*/
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
197 0, /*tp_getattro*/
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
198 0, /*tp_setattro*/
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
199 0, /*tp_as_buffer*/
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
200 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
201 0, /*tp_doc*/
137
f4361d557929 Fixed a few lines with wrong indentation in _unix (don't copy&paste code, kids)
Oleg Oshmyan <chortos@inbox.lv>
parents: 136
diff changeset
202 0, /*tp_traverse*/
f4361d557929 Fixed a few lines with wrong indentation in _unix (don't copy&paste code, kids)
Oleg Oshmyan <chortos@inbox.lv>
parents: 136
diff changeset
203 0, /*tp_clear*/
f4361d557929 Fixed a few lines with wrong indentation in _unix (don't copy&paste code, kids)
Oleg Oshmyan <chortos@inbox.lv>
parents: 136
diff changeset
204 0, /*tp_richcompare*/
f4361d557929 Fixed a few lines with wrong indentation in _unix (don't copy&paste code, kids)
Oleg Oshmyan <chortos@inbox.lv>
parents: 136
diff changeset
205 0, /*tp_weaklistoffset*/
f4361d557929 Fixed a few lines with wrong indentation in _unix (don't copy&paste code, kids)
Oleg Oshmyan <chortos@inbox.lv>
parents: 136
diff changeset
206 0, /*tp_iter*/
f4361d557929 Fixed a few lines with wrong indentation in _unix (don't copy&paste code, kids)
Oleg Oshmyan <chortos@inbox.lv>
parents: 136
diff changeset
207 0, /*tp_iternext*/
f4361d557929 Fixed a few lines with wrong indentation in _unix (don't copy&paste code, kids)
Oleg Oshmyan <chortos@inbox.lv>
parents: 136
diff changeset
208 0, /*tp_methods*/
f4361d557929 Fixed a few lines with wrong indentation in _unix (don't copy&paste code, kids)
Oleg Oshmyan <chortos@inbox.lv>
parents: 136
diff changeset
209 _PopenPlaceholder_members, /*tp_members*/
136
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
210 };
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
211
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
212 #ifndef timeradd
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
213 #define timeradd(a, b, res) \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
214 do \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
215 { \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
216 (res)->tv_sec = (a)->tv_sec + (b)->tv_sec; \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
217 (res)->tv_usec = (a)->tv_usec + (b)->tv_usec; \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
218 if ((res)->tv_usec >= 1000000) \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
219 { \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
220 ++(res)->tv_sec; \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
221 (res)->tv_usec -= 1000000; \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
222 } \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
223 } \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
224 while (0)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
225 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
226
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
227 #ifndef timersub
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
228 #define timersub(a, b, res) \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
229 do \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
230 { \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
231 (res)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
232 (res)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
233 if ((res)->tv_usec < 0) \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
234 { \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
235 --(res)->tv_sec; \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
236 (res)->tv_usec += 1000000; \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
237 } \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
238 } \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
239 while (0)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
240 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
241
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
242 #ifndef timerclear
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
243 #define timerclear(tvp) ((void) ((tvp)->tv_sec = (tvp)->tv_usec = 0))
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
244 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
245
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
246 #ifndef timerisset
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
247 #define timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
248 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
249
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
250 #ifndef timercmp
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
251 #define timercmp(a, b, cmp) \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
252 (((a)->tv_sec == (b)->tv_sec) \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
253 ? ((a)->tv_usec cmp (b)->tv_usec) \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
254 : ((a)->tv_sec cmp (b)->tv_sec))
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
255 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
256
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
257 // Stolen from posixmodule.c of Python 2.7.1
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
258 static void free_string_array(char **array, Py_ssize_t count)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
259 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
260 Py_ssize_t i;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
261 for (i = 0; i < count; ++i)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
262 PyMem_Free(array[i]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
263 PyMem_DEL(array);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
264 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
265
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
266 // Stolen from termios.c of Python 2.7.1
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
267 static int fdconv(PyObject *obj, void *p)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
268 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
269 int fd = PyObject_AsFileDescriptor(obj);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
270 if (fd >= 0)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
271 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
272 *((int *) p) = fd;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
273 return 1;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
274 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
275 return 0;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
276 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
277
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
278 // Parts stolen from bltinmodule.c, posixmodule.c and termios.c of Python 2.7.1
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
279 static int my_spawn(PyObject *args, PyObject *kwds, int c2ppipe[2], int maxcputime, Py_ssize_t maxmemory)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
280 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
281 static const char *const kwlist[] = { "stdin", "stdout", "stderr", NULL };
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
282 static PyObject *dummy_args = NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
283 Py_ssize_t i, argc;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
284 char **argv;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
285 bool own_args = false;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
286 int fdin = 0, fdout = 1, fderr = 2;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
287
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
288 if (dummy_args == NULL)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
289 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
290 if (!(dummy_args = PyTuple_New(0)))
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
291 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
292 return -1;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
293 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
294 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
295
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
296 if (!PyArg_ParseTuple(args, "O:call", &args))
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
297 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
298 return -1;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
299 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
300 if (!PyArg_ParseTupleAndKeywords(dummy_args, kwds, "|O&O&O&:call", (char **) kwlist, fdconv, &fdin, fdconv, &fdout, fdconv, &fderr))
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
301 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
302 return -1;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
303 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
304
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
305 #if PY_MAJOR_VERSION >= 3
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
306 if (PyUnicode_Check(args))
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
307 #else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
308 if (PyString_Check(args) || PyUnicode_Check(args))
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
309 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
310 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
311 argc = 1;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
312 args = PyTuple_Pack(1, args);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
313 if (args == NULL)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
314 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
315 return -1;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
316 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
317 own_args = true;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
318 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
319 else if (!PySequence_Check(args))
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
320 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
321 PyErr_SetString(PyExc_TypeError, "call() argument must be a sequence or string");
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
322 return -1;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
323 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
324 else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
325 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
326 argc = PySequence_Size(args);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
327 if (argc < 1)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
328 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
329 PyErr_SetString(PyExc_TypeError, "call() argument must not be empty");
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
330 return -1;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
331 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
332 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
333
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
334 argv = PyMem_NEW(char *, argc + 1);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
335 if (argv == NULL)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
336 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
337 if (own_args)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
338 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
339 Py_DECREF(args);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
340 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
341 PyErr_NoMemory();
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
342 return -1;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
343 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
344
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
345 for (i = 0; i < argc; ++i)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
346 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
347 if (!PyArg_Parse(PySequence_ITEM(args, i), "et", Py_FileSystemDefaultEncoding, &argv[i]))
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
348 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
349 free_string_array(argv, i);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
350 if (own_args)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
351 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
352 Py_DECREF(args);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
353 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
354 PyErr_SetString(PyExc_TypeError, "call() argument must contain only strings");
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
355 return -1;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
356 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
357 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
358 argv[argc] = NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
359
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
360 curpid = fork();
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
361 if (!curpid)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
362 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
363 pid_t pid;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
364 int spawn_errno, status, fd, fddupped[3];
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
365 struct child_stats stats;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
366 _PyTime_timeval tvstart, tvend;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
367 #if defined HAVE_SYS_RESOURCE_H || defined HAVE_WAIT4 || defined HAVE_WAIT3
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
368 struct rusage rusage;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
369 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
370 #if defined RLIMIT_AS || defined RLIMIT_CPU
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
371 struct rlimit rlimit;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
372 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
373
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
374 /*
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
375 Assume no errors occur:
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
376 * POSIX:2008 doesn't even define any errors for setpgrp,
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
377 nor does the (probably copied-verbatim-from-FreeBSD) man page
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
378 on Mac OS X 10.6;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
379 * none of the error conditions POSIX:2008 does define
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
380 for setpgid can occur.
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
381 */
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
382 #ifdef HAVE_SETPGID
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
383 setpgid(0, 0);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
384 #else //if defined HAVE_SETPGRP
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
385 #ifdef SETPGRP_HAVE_ARG
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
386 setpgrp(0, 0);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
387 #else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
388 setpgrp();
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
389 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
390 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
391
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
392 #ifdef SIGINT
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
393 signal(SIGINT, SIG_DFL);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
394 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
395
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
396 #if PY_MAJOR_VERSION > 3 || PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 2
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
397 _Py_RestoreSignals();
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
398 #else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
399 #ifdef SIGPIPE
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
400 signal(SIGPIPE, SIG_DFL);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
401 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
402 #ifdef SIGXFSZ
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
403 signal(SIGXFSZ, SIG_DFL);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
404 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
405 #ifdef SIGXFZ
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
406 signal(SIGXFZ, SIG_DFL);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
407 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
408 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
409
218
65b5c9390010 With _unix, Escape presses now cancel test data unarchiving
Oleg Oshmyan <chortos@inbox.lv>
parents: 210
diff changeset
410 #if defined HAVE_TERMIOS_H && defined O_ASYNC
65b5c9390010 With _unix, Escape presses now cancel test data unarchiving
Oleg Oshmyan <chortos@inbox.lv>
parents: 210
diff changeset
411 signal(SIGIO, SIG_DFL);
65b5c9390010 With _unix, Escape presses now cancel test data unarchiving
Oleg Oshmyan <chortos@inbox.lv>
parents: 210
diff changeset
412 #endif
65b5c9390010 With _unix, Escape presses now cancel test data unarchiving
Oleg Oshmyan <chortos@inbox.lv>
parents: 210
diff changeset
413
136
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
414 if (c2ppipe[1] < 3)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
415 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
416 int newfd;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
417 #ifdef F_DUPFD_CLOEXEC
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
418 newfd = fcntl(c2ppipe[1], F_DUPFD_CLOEXEC, 3);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
419 if (newfd == -1)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
420 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
421 spawn_errno = errno;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
422 TESTEE_REPORT_STATUS(TESTEE_SPAWN_FAILED);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
423 write(c2ppipe[1], &spawn_errno, sizeof spawn_errno);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
424 _exit(127);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
425 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
426 c2ppipe[1] = newfd;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
427 #else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
428 newfd = fcntl(c2ppipe[1], F_DUPFD, 3);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
429 // Other threads should not fork/spawn right now
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
430 if (newfd == -1
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
431 || fcntl(newfd, F_SETFD, fcntl(newfd, F_GETFD) | FD_CLOEXEC) == -1)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
432 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
433 spawn_errno = errno;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
434 TESTEE_REPORT_STATUS(TESTEE_SPAWN_FAILED);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
435 write(c2ppipe[1], &spawn_errno, sizeof spawn_errno);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
436 _exit(127);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
437 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
438 c2ppipe[1] = newfd;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
439 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
440 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
441 // Yes, this works as intended even if fdin == fdout == fderr == 0
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
442 // and there are no open file descriptors except 0 and c2ppipe
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
443 // FIXME: error handling
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
444 fddupped[0] = dup(fdin);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
445 fddupped[1] = dup(fdout);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
446 fddupped[2] = dup(fderr);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
447 dup2(fddupped[0], 0);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
448 dup2(fddupped[1], 1);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
449 dup2(fddupped[2], 2);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
450 // FIXME: close() may fail with EINTR or EIO; is setting CLOEXEC safer?
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
451 // Bear in mind we still want to close them in _this_ process
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
452 for (fd = sysconf(_SC_OPEN_MAX); --fd > c2ppipe[1]; )
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
453 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
454 close(fd);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
455 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
456 while (--fd >= 3)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
457 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
458 close(fd);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
459 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
460
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
461 #ifdef RLIMIT_AS
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
462 if (maxmemory)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
463 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
464 rlimit.rlim_cur = rlimit.rlim_max = maxmemory;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
465 setrlimit(RLIMIT_AS, &rlimit);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
466 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
467 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
468 #ifdef RLIMIT_CPU
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
469 if (maxcputime)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
470 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
471 rlimit.rlim_cur = rlimit.rlim_max = maxcputime;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
472 setrlimit(RLIMIT_CPU, &rlimit);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
473 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
474 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
475
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
476 #ifdef HAVE_SPAWN_H
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
477 #ifdef __APPLE__
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
478 if (posix_spawnp != NULL)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
479 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
480 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
481 spawn_errno = posix_spawnp(&pid, argv[0], NULL, NULL, argv, environ);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
482 _PyTime_gettimeofday(&tvstart);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
483
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
484 if (spawn_errno)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
485 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
486 TESTEE_REPORT_STATUS(TESTEE_SPAWN_FAILED);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
487 write(c2ppipe[1], &spawn_errno, sizeof spawn_errno);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
488 _exit(127);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
489 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
490 #ifdef __APPLE__
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
491 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
492 else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
493 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
494 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
495 #if !(defined HAVE_SPAWN_H) || defined __APPLE__
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
496 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
497 pid = fork();
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
498 if (!pid)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
499 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
500 execvp(argv[0], argv);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
501 spawn_errno = errno;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
502 TESTEE_REPORT_STATUS(TESTEE_SPAWN_FAILED);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
503 write(c2ppipe[1], &spawn_errno, sizeof spawn_errno);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
504 _exit(127);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
505 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
506 else if (pid == -1)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
507 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
508 spawn_errno = errno;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
509 TESTEE_REPORT_STATUS(TESTEE_SPAWN_FAILED);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
510 write(c2ppipe[1], &spawn_errno, sizeof spawn_errno);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
511 _exit(127);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
512 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
513 else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
514 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
515 _PyTime_gettimeofday(&tvstart);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
516 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
517 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
518 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
519 TESTEE_REPORT_STATUS(TESTEE_SPAWNED);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
520 write(c2ppipe[1], &tvstart, sizeof tvstart);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
521
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
522 #ifdef HAVE_WAIT4
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
523 while (wait4(pid, &status, 0, &rusage) != pid);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
524 #elif defined HAVE_WAIT3
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
525 while (wait3(&status, 0, &rusage) != pid);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
526 #elif defined HAVE_WAITPID
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
527 while (waitpid(pid, &status, 0) != pid);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
528 #else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
529 while (wait(&status) != pid);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
530 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
531
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
532 _PyTime_gettimeofday(&tvend);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
533 #if defined HAVE_SYS_RESOURCE_H && !(defined HAVE_WAIT4 || defined HAVE_WAIT3)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
534 getrusage(RUSAGE_CHILDREN, &rusage);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
535 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
536
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
537 stats = zero_stats;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
538
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
539 if (WIFEXITED(status) && WEXITSTATUS(status) == 127) _exit(127);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
540 else if (WIFSIGNALED(status)) stats.returncode = -WTERMSIG(status);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
541 else stats.returncode = WEXITSTATUS(status);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
542
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
543 timersub(&tvend, &tvstart, &stats.walltime);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
544 #if defined HAVE_SYS_RESOURCE_H || defined HAVE_WAIT4 || defined HAVE_WAIT3
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
545 timeradd(&rusage.ru_utime, &rusage.ru_stime, &stats.cputime);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
546 #ifdef __APPLE__
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
547 stats.memory = rusage.ru_maxrss;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
548 #else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
549 stats.memory = rusage.ru_maxrss << 10;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
550 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
551 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
552
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
553 write(c2ppipe[1], &stats, sizeof stats);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
554 _exit(0);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
555 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
556 else if (curpid == -1)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
557 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
558 PyErr_SetFromErrno(PyExc_OSError);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
559 free_string_array(argv, argc);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
560 if (own_args)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
561 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
562 Py_DECREF(args);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
563 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
564 return 0;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
565 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
566
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
567 /*
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
568 Assume no errors occur if the child is still alive:
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
569 * the (probably copied-verbatim-from-FreeBSD) man page
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
570 on Mac OS X 10.6 doesn't even define any errors for setpgrp;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
571 * none of the error conditions POSIX:2008 defines
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
572 for setpgid can occur.
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
573 */
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
574 #ifdef HAVE_SETPGID
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
575 setpgid(curpid, 0);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
576 #elif defined SETPGRP_HAVE_ARG
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
577 setpgrp(curpid, 0);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
578 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
579
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
580 free_string_array(argv, argc);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
581 if (own_args)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
582 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
583 Py_DECREF(args);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
584 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
585 return 1;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
586 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
587
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
588 static inline bool attr_to_timeval(PyObject *obj, const char *attr, _PyTime_timeval *ptv)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
589 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
590 #ifdef HAVE_LONG_LONG
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
591 long long i_whole;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
592 #else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
593 long i_whole;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
594 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
595 PyObject *whole, *frac, *million, *usec, *usec_whole;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
596 PyObject *member = PyObject_GetAttrString(obj, attr);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
597 if (member == NULL)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
598 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
599 return false;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
600 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
601 if (member == Py_None)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
602 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
603 Py_DECREF(member);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
604 timerclear(ptv);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
605 return true;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
606 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
607 whole = PyNumber_Int(member);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
608 if (whole == NULL)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
609 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
610 Py_DECREF(member);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
611 return false;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
612 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
613 #ifdef HAVE_LONG_LONG
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
614 i_whole = PyLong_AsLongLong(whole);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
615 #else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
616 i_whole = PyInt_AsLong(whole);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
617 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
618 if (i_whole == -1 && PyErr_Occurred() != NULL)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
619 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
620 Py_DECREF(whole);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
621 Py_DECREF(member);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
622 return false;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
623 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
624 // FIXME: detect time_t overflow
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
625 ptv->tv_sec = i_whole;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
626 frac = PyNumber_Subtract(member, whole);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
627 Py_DECREF(whole);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
628 Py_DECREF(member);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
629 if (frac == NULL)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
630 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
631 return false;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
632 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
633 million = PyInt_FromLong(1000000);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
634 if (million == NULL)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
635 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
636 Py_DECREF(frac);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
637 return false;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
638 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
639 usec = PyNumber_InPlaceMultiply(frac, million);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
640 Py_DECREF(million);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
641 Py_DECREF(frac);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
642 if (usec == NULL)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
643 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
644 return false;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
645 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
646 usec_whole = PyNumber_Int(usec);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
647 Py_DECREF(usec);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
648 if (usec_whole == NULL)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
649 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
650 return false;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
651 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
652 // FIXME: a sanity check (0 <= value < 1000000) here wouldn't harm
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
653 ptv->tv_usec = PyInt_AsLong(usec_whole);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
654 Py_DECREF(usec_whole);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
655 return ptv->tv_usec != -1 || PyErr_Occurred() == NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
656 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
657
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
658 #ifdef __cplusplus
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
659 typedef struct { char a[2]; } two_chars;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
660 static char is_int(char);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
661 static char is_int(signed char);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
662 static char is_int(unsigned char);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
663 static char is_int(short);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
664 static char is_int(unsigned short);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
665 static char is_int(int);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
666 static char is_int(unsigned);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
667 static char is_int(long);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
668 static char is_int(unsigned long);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
669 #ifdef HAVE_LONG_LONG
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
670 static char is_int(long long);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
671 static char is_int(unsigned long long);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
672 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
673 static two_chars is_int(...);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
674 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
675
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
676 static inline bool timeval_to_attr(_PyTime_timeval *ptv, PyObject *obj, const char *attr)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
677 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
678 PyObject *value;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
679 #ifdef __cplusplus
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
680 // If tv_sec has an integral type and !tv_usec, try to create a Python int
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
681 if (sizeof is_int(ptv->tv_sec) == sizeof(char) && !ptv->tv_usec)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
682 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
683 if (ptv->tv_sec <= LONG_MAX)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
684 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
685 value = PyInt_FromLong(ptv->tv_sec);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
686 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
687 // FIXME: signed/unsigned comparisons ruin everything
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
688 #ifdef HAVE_LONG_LONG
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
689 else// if (ptv->tv_sec <= ULLONG_MAX)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
690 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
691 value = PyLong_FromUnsignedLongLong(ptv->tv_sec);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
692 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
693 #else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
694 // else if (ptv->tv_sec <= ULONG_MAX)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
695 // {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
696 // value = PyLong_FromUnsignedLong(ptv->tv_sec);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
697 // }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
698 //#endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
699 else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
700 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
701 value = PyFloat_FromDouble(ptv->tv_sec);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
702 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
703 //
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
704 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
705 //
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
706 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
707 else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
708 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
709 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
710 // TODO: use decimal.Decimal or fractions.Fraction
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
711 value = PyFloat_FromDouble(ptv->tv_sec + ptv->tv_usec * 0.000001);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
712 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
713 if (value == NULL)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
714 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
715 return false;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
716 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
717 if (PyObject_SetAttrString(obj, attr, value) == -1)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
718 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
719 return false;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
720 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
721 Py_DECREF(value);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
722 return true;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
723 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
724
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
725 /*
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
726 TODO/FIXME:
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
727 * Replace timeval->timespec and select->pselect if pselect is available
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
728 (preferably only if pselect is not a wrapper around select).
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
729 * File descriptors might be >= FD_SETSIZE?
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
730 */
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
731 static PyObject *_unix_call(PyObject *self, PyObject *args, PyObject *kwds)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
732 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
733 PyObject *testcase = NULL, *obj;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
734 _unix__PopenPlaceholderObject *Popen_placeholder;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
735 int spawn_errno = 0, spawn_status, s, c2ppipe[2], retstat;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
736 struct child_stats stats = zero_stats;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
737 _PyTime_timeval maxwalltime, maxcputime, timeout, time_start;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
738 Py_ssize_t maxmemory, r;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
739 size_t stats_read = 0;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
740 fd_set readfds;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
741 char c;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
742 bool have_maxwalltime;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
743
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
744 if (kwds != NULL)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
745 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
746 testcase = PyDict_GetItemString(kwds, "case");
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
747 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
748 if (testcase == NULL)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
749 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
750 PyErr_SetString(PyExc_TypeError, "call() requires a keyword argument 'case'");
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
751 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
752 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
753 Py_INCREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
754 PyDict_DelItemString(kwds, "case");
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
755
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
756 if (!attr_to_timeval(testcase, "maxwalltime", &maxwalltime)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
757 || !attr_to_timeval(testcase, "maxcputime", &maxcputime))
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
758 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
759 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
760 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
761 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
762
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
763 obj = PyObject_GetAttrString(testcase, "maxmemory");
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
764 if (obj == NULL)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
765 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
766 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
767 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
768 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
769 if (PyObject_IsTrue(obj))
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
770 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
771 PyObject *factor, *bytes;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
772 factor = PyInt_FromLong(1024 * 1024);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
773 if (factor == NULL)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
774 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
775 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
776 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
777 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
778 bytes = PyNumber_Multiply(obj, factor);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
779 Py_DECREF(factor);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
780 if (bytes == NULL)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
781 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
782 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
783 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
784 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
785 maxmemory = PyNumber_AsSsize_t(bytes, PyExc_OverflowError);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
786 Py_DECREF(bytes);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
787 if (maxmemory == -1 && PyErr_Occurred() != NULL)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
788 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
789 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
790 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
791 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
792 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
793 else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
794 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
795 maxmemory = 0;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
796 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
797 Py_DECREF(obj);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
798
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
799 #ifdef HAVE_PIPE2
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
800 if (pipe2(c2ppipe, O_CLOEXEC))
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
801 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
802 PyErr_SetFromErrno(PyExc_IOError);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
803 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
804 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
805 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
806 #else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
807 if (pipe(c2ppipe))
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
808 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
809 PyErr_SetFromErrno(PyExc_IOError);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
810 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
811 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
812 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
813 // Does any other thread fork/spawn right now? Please shoot it in the head
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
814 // (well, if this ends up causing trouble, anyway)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
815 if (fcntl(c2ppipe[0], F_SETFD, fcntl(c2ppipe[0], F_GETFD) | FD_CLOEXEC) == -1
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
816 || fcntl(c2ppipe[1], F_SETFD, fcntl(c2ppipe[1], F_GETFD) | FD_CLOEXEC) == -1)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
817 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
818 PyErr_SetFromErrno(PyExc_IOError);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
819 close(c2ppipe[0]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
820 close(c2ppipe[1]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
821 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
822 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
823 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
824 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
825
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
826 spawn_status = my_spawn(args, kwds, c2ppipe, maxcputime.tv_sec + (maxcputime.tv_usec > 0), maxmemory);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
827 close(c2ppipe[1]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
828 if (!spawn_status)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
829 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
830 PyObject *type, *value, *traceback, *e;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
831 close(c2ppipe[0]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
832 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
833 PyErr_Fetch(&type, &value, &traceback);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
834 PyErr_NormalizeException(&type, &value, &traceback);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
835 Py_XDECREF(traceback);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
836 Py_DECREF(type);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
837 e = PyObject_CallFunctionObjArgs(CannotStartTestee, value, NULL);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
838 Py_DECREF(value);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
839 PyErr_SetObject(CannotStartTestee, e);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
840 Py_DECREF(e);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
841 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
842 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
843 else if (spawn_status < 0)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
844 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
845 close(c2ppipe[0]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
846 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
847 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
848 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
849
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
850 // FIXME: use select in order not to miss SIGINT
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
851 while ((r = read(c2ppipe[0], &c, 1)) == -1 && errno == EINTR)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
852 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
853 if (PyErr_CheckSignals() == -1)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
854 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
855 PROPAGATE_SIGINT;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
856 close(c2ppipe[0]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
857 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
858 TERM_TESTEE;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
859 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
860 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
861 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
862 if (r == 1)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
863 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
864 if (c == TESTEE_SPAWNED)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
865 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
866 size_t got = 0;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
867 while (got < sizeof time_start)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
868 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
869 r = read(c2ppipe[0], got + (char *) &time_start, sizeof time_start - got);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
870 if (r > 0)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
871 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
872 got += r;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
873 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
874 else if (!r)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
875 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
876 errno = 0;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
877 PyErr_SetFromErrno(PyExc_IOError);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
878 goto spawn_failed;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
879 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
880 else if (errno == EINTR)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
881 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
882 if (PyErr_CheckSignals() == -1)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
883 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
884 PROPAGATE_SIGINT;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
885 close(c2ppipe[0]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
886 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
887 TERM_TESTEE;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
888 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
889 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
890 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
891 else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
892 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
893 PyErr_SetFromErrno(PyExc_IOError);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
894 goto spawn_failed;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
895 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
896 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
897 if (!timeval_to_attr(&time_start, testcase, "time_started"))
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
898 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
899 close(c2ppipe[0]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
900 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
901 TERM_TESTEE;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
902 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
903 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
904 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
905 else // if (c == TESTEE_SPAWN_FAILED)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
906 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
907 size_t got = 0;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
908 while (got < sizeof spawn_errno)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
909 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
910 r = read(c2ppipe[0], got + (char *) &spawn_errno, sizeof spawn_errno - got);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
911 if (r > 0)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
912 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
913 got += r;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
914 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
915 else if (!r)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
916 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
917 // Can't get the real error; use zero instead
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
918 spawn_errno = 0;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
919 break;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
920 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
921 else if (errno == EINTR)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
922 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
923 if (PyErr_CheckSignals() == -1)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
924 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
925 PROPAGATE_SIGINT;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
926 close(c2ppipe[0]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
927 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
928 TERM_TESTEE;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
929 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
930 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
931 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
932 else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
933 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
934 PyErr_SetFromErrno(PyExc_IOError);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
935 goto spawn_failed;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
936 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
937 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
938 errno = spawn_errno;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
939 /*
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
940 if (errno == EACCES || errno == EINVAL || errno == ELOOP
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
941 || errno == ENAMETOOLONG || errno == ENOENT || errno == ENOTDIR
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
942 || errno == ENOEXEC || errno == ETXTBSY)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
943 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
944 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, PySequence_ITEM(args, 0));
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
945 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
946 else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
947 {*/
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
948 PyErr_SetFromErrno(PyExc_OSError);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
949 //}
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
950 goto spawn_failed;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
951 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
952 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
953 else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
954 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
955 PyObject *type, *value, *traceback, *e;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
956 if (!r) errno = 0;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
957 PyErr_SetFromErrno(PyExc_IOError);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
958 spawn_failed:
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
959 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
960 close(c2ppipe[0]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
961 TERM_TESTEE;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
962 PyErr_Fetch(&type, &value, &traceback);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
963 PyErr_NormalizeException(&type, &value, &traceback);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
964 Py_XDECREF(traceback);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
965 Py_DECREF(type);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
966 e = PyObject_CallFunctionObjArgs(CannotStartTestee, value, NULL);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
967 Py_DECREF(value);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
968 PyErr_SetObject(CannotStartTestee, e);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
969 Py_DECREF(e);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
970 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
971 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
972
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
973 Py_BEGIN_ALLOW_THREADS
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
974 timeradd(&time_start, &maxwalltime, &time_end);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
975 FD_ZERO(&readfds);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
976 have_maxwalltime = timerisset(&maxwalltime);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
977 /*
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
978 Implementations may place limitations on the maximum timeout
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
979 interval supported. All implementations shall support a maximum
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
980 timeout interval of at least 31 days. If the timeout argument
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
981 specifies a timeout interval greater than the implementation-
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
982 defined maximum value, the maximum value shall be used as the
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
983 actual timeout value.
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
984 (POSIX:2008)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
985 Therefore the loop and the && timercmp(&time_end, &now, <).
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
986 */
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
987 for (;;)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
988 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
989 _PyTime_timeval now;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
990 int maxfd = c2ppipe[0];
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
991 #ifdef HAVE_TERMIOS_H
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
992 if (catch_escape) FD_SET(0, &readfds);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
993 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
994 #ifdef USE_WAKEUP_FD
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
995 FD_SET(intpipe[0], &readfds);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
996 if (intpipe[0] > maxfd) maxfd = intpipe[0];
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
997 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
998 FD_SET(c2ppipe[0], &readfds);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
999
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1000 if (have_maxwalltime)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1001 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1002 _PyTime_gettimeofday(&now);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1003 if (timercmp(&time_end, &now, <))
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1004 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1005 timerclear(&timeout);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1006 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1007 else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1008 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1009 timersub(&time_end, &now, &timeout);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1010 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1011
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1012 s = select(maxfd + 1, &readfds, NULL, NULL, &timeout);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1013
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1014 if (!s && timercmp(&time_end, &now, <))
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1015 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1016 close(c2ppipe[0]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1017 TERM_TESTEE;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1018 Py_BLOCK_THREADS
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1019 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1020 PyErr_SetObject(WallTimeLimitExceeded, NULL);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1021 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1022 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1023 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1024 else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1025 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1026 s = select(maxfd + 1, &readfds, NULL, NULL, NULL);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1027 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1028
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1029 if (s < 0 && errno == EINTR)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1030 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1031 Py_BLOCK_THREADS
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1032 if (PyErr_CheckSignals() == -1)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1033 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1034 PROPAGATE_SIGINT;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1035 close(c2ppipe[0]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1036 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1037 TERM_TESTEE;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1038 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1039 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1040 Py_UNBLOCK_THREADS
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1041 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1042 else if (s < 0 && errno != EAGAIN)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1043 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1044 Py_BLOCK_THREADS
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1045 PyErr_SetFromErrno(PyExc_IOError);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1046 close(c2ppipe[0]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1047 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1048 TERM_TESTEE;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1049 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1050 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1051 #ifdef USE_WAKEUP_FD
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1052 else if (s > 0 && FD_ISSET(intpipe[0], &readfds))
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1053 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1054 // FIXME: is error handling needed?
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1055 while (read(intpipe[0], dont_care_buffer, sizeof dont_care_buffer) > 0);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1056 Py_BLOCK_THREADS
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1057 if (PyErr_CheckSignals() == -1)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1058 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1059 PROPAGATE_SIGINT;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1060 close(c2ppipe[0]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1061 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1062 TERM_TESTEE;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1063 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1064 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1065 Py_UNBLOCK_THREADS
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1066 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1067 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1068 #ifdef HAVE_TERMIOS_H
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1069 else if (s > 0 && !FD_ISSET(c2ppipe[0], &readfds))
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1070 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1071 // FIXME: is error and EOF handling needed?
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1072 if ((r = read(0, &c, 1)) == 1)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1073 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1074 if (c == '\33')
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1075 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1076 close(c2ppipe[0]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1077 TERM_TESTEE;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1078 Py_BLOCK_THREADS
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1079 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1080 PyErr_SetObject(CanceledByUser, NULL);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1081 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1082 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1083 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1084 else if (r == -1 && errno == EINTR)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1085 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1086 if (PyErr_CheckSignals() == -1)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1087 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1088 PROPAGATE_SIGINT;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1089 close(c2ppipe[0]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1090 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1091 TERM_TESTEE;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1092 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1093 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1094 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1095 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1096 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1097 else if (s > 0)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1098 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1099 bool blocked_threads = false;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1100 while ((r = read(c2ppipe[0], stats_read + (char *) &stats, sizeof stats - stats_read)) == -1 && errno == EINTR)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1101 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1102 Py_BLOCK_THREADS
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1103 blocked_threads = true;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1104 if (PyErr_CheckSignals() == -1)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1105 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1106 PROPAGATE_SIGINT;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1107 close(c2ppipe[0]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1108 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1109 TERM_TESTEE;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1110 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1111 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1112 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1113 if (r > 0)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1114 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1115 stats_read += r;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1116 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1117 else if (!r)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1118 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1119 break;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1120 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1121 else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1122 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1123 close(c2ppipe[0]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1124 TERM_TESTEE;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1125 if (!blocked_threads)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1126 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1127 Py_BLOCK_THREADS
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1128 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1129 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1130 PyErr_SetFromErrno(PyExc_IOError);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1131 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1132 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1133 if (blocked_threads)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1134 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1135 Py_UNBLOCK_THREADS
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1136 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1137 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1138 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1139 close(c2ppipe[0]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1140 Py_END_ALLOW_THREADS
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1141
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1142 #ifdef HAVE_WAITPID
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1143 while (waitpid(curpid, &retstat, 0) != curpid)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1144 #else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1145 while (wait(&retstat) != curpid)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1146 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1147 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1148 if (PyErr_CheckSignals() == -1)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1149 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1150 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1151 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1152 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1153 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1154
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1155 if (WIFEXITED(retstat) && WEXITSTATUS(retstat) == 127)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1156 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1157 PyObject *type, *value, *traceback, *e;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1158 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1159 errno = 0;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1160 PyErr_SetFromErrno(PyExc_OSError);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1161 PyErr_Fetch(&type, &value, &traceback);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1162 PyErr_NormalizeException(&type, &value, &traceback);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1163 Py_XDECREF(traceback);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1164 Py_DECREF(type);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1165 e = PyObject_CallFunctionObjArgs(CannotStartTestee, value, NULL);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1166 Py_DECREF(value);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1167 PyErr_SetObject(CannotStartTestee, e);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1168 Py_DECREF(e);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1169 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1170 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1171 else if (!WIFEXITED(retstat) || WEXITSTATUS(retstat))
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1172 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1173 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1174 if (WIFSTOPPED(retstat))
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1175 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1176 return PyErr_Format(PyExc_EnvironmentError, "unexpected exit status from worker: stopped by signal %d", WSTOPSIG(retstat));
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1177 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1178 else if (WIFSIGNALED(retstat))
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1179 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1180 return PyErr_Format(PyExc_EnvironmentError, "unexpected exit status from worker: terminated by signal %d", WTERMSIG(retstat));
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1181 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1182 else if (WIFEXITED(retstat))
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1183 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1184 return PyErr_Format(PyExc_EnvironmentError, "unexpected exit status from worker: %d", WEXITSTATUS(retstat));
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1185 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1186 else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1187 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1188 PyErr_SetString(PyExc_EnvironmentError, "unexpected exit status from worker: not exited, signaled or stopped");
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1189 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1190 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1191 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1192
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1193 if (stats_read != sizeof stats)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1194 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1195 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1196 PyErr_SetString(PyExc_EnvironmentError, "unexpectedly early end of output from worker");
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1197 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1198 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1199
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1200 if (timerisset(&maxwalltime) && timercmp(&stats.walltime, &maxwalltime, >))
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1201 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1202 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1203 PyErr_SetObject(WallTimeLimitExceeded, NULL);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1204 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1205 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1206
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1207 obj = PyInt_FromLong(0);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1208 if (obj == NULL)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1209 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1210 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1211 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1212 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1213 if (PyObject_SetAttrString(testcase, "time_started", obj) == -1)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1214 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1215 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1216 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1217 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1218 Py_DECREF(obj);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1219
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1220 #if defined HAVE_SYS_RESOURCE_H || defined HAVE_WAIT4 || defined HAVE_WAIT3
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1221 if (timerisset(&maxcputime) || !timerisset(&maxwalltime))
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1222 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1223 PyObject *cputls;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1224 if (!timeval_to_attr(&stats.cputime, testcase, "time_stopped"))
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1225 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1226 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1227 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1228 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1229 cputls = PyObject_GetAttrString(testcase, "cpu_time_limit_string");
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1230 if (cputls == NULL)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1231 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1232 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1233 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1234 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1235 if (PyObject_SetAttrString(testcase, "time_limit_string", cputls) == -1)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1236 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1237 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1238 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1239 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1240 Py_DECREF(cputls);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1241 if (timerisset(&maxcputime) && timercmp(&stats.cputime, &maxcputime, >))
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1242 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1243 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1244 PyErr_SetObject(CPUTimeLimitExceeded, NULL);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1245 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1246 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1247 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1248 else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1249 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1250 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1251 if (!timeval_to_attr(&stats.walltime, testcase, "time_stopped"))
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1252 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1253 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1254 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1255 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1256 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1257
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1258 #if defined HAVE_SYS_RESOURCE_H || defined HAVE_WAIT4 || defined HAVE_WAIT3
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1259 if (maxmemory && stats.memory > maxmemory)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1260 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1261 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1262 PyErr_SetObject(MemoryLimitExceeded, NULL);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1263 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1264 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1265 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1266
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1267 Popen_placeholder = PyObject_New(_unix__PopenPlaceholderObject, &_unix__PopenPlaceholderType);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1268 if (Popen_placeholder == NULL)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1269 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1270 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1271 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1272 Popen_placeholder->returncode = stats.returncode;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1273 PyObject_SetAttrString(testcase, "process", (PyObject *) Popen_placeholder);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1274 Py_DECREF(Popen_placeholder);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1275 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1276 Py_RETURN_NONE;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1277 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1278
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1279 static PyObject *_unix_pause(PyObject *self)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1280 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1281 #ifdef HAVE_TERMIOS_H
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1282 if (catch_escape)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1283 {
218
65b5c9390010 With _unix, Escape presses now cancel test data unarchiving
Oleg Oshmyan <chortos@inbox.lv>
parents: 210
diff changeset
1284 #ifdef O_ASYNC
65b5c9390010 With _unix, Escape presses now cancel test data unarchiving
Oleg Oshmyan <chortos@inbox.lv>
parents: 210
diff changeset
1285 signal(SIGIO, SIG_DFL);
65b5c9390010 With _unix, Escape presses now cancel test data unarchiving
Oleg Oshmyan <chortos@inbox.lv>
parents: 210
diff changeset
1286 #endif
136
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1287 char c;
210
8c4e92fb32d8 Keyboard interrupts now satisfy the Press any key to exit... prompt
Oleg Oshmyan <chortos@inbox.lv>
parents: 191
diff changeset
1288 while (read(0, &c, 1) == -1 && errno == EINTR)
8c4e92fb32d8 Keyboard interrupts now satisfy the Press any key to exit... prompt
Oleg Oshmyan <chortos@inbox.lv>
parents: 191
diff changeset
1289 {
8c4e92fb32d8 Keyboard interrupts now satisfy the Press any key to exit... prompt
Oleg Oshmyan <chortos@inbox.lv>
parents: 191
diff changeset
1290 if (PyErr_CheckSignals() == -1)
8c4e92fb32d8 Keyboard interrupts now satisfy the Press any key to exit... prompt
Oleg Oshmyan <chortos@inbox.lv>
parents: 191
diff changeset
1291 {
8c4e92fb32d8 Keyboard interrupts now satisfy the Press any key to exit... prompt
Oleg Oshmyan <chortos@inbox.lv>
parents: 191
diff changeset
1292 return NULL;
8c4e92fb32d8 Keyboard interrupts now satisfy the Press any key to exit... prompt
Oleg Oshmyan <chortos@inbox.lv>
parents: 191
diff changeset
1293 }
8c4e92fb32d8 Keyboard interrupts now satisfy the Press any key to exit... prompt
Oleg Oshmyan <chortos@inbox.lv>
parents: 191
diff changeset
1294 }
136
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1295 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1296 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1297 Py_RETURN_NONE;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1298 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1299
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1300 static PyMethodDef _unixMethods[] =
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1301 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1302 { "call", (PyCFunction) _unix_call, METH_VARARGS | METH_KEYWORDS, "Call a process." },
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1303 { "pause", (PyCFunction) _unix_pause, METH_NOARGS, "Block until a key is pressed." },
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1304 { NULL }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1305 };
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1306
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1307 #ifdef USE_WAKEUP_FD
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1308 static void close_intpipe(void)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1309 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1310 close(intpipe[0]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1311 close(intpipe[1]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1312 intpipe[0] = intpipe[1] = 0;
191
4f69e30abbd5 Made sure the wakeup FD is always valid
Oleg Oshmyan <chortos@inbox.lv>
parents: 190
diff changeset
1313 PySignal_SetWakeupFd(-1);
136
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1314 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1315 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1316
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1317 #ifdef HAVE_TERMIOS_H
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1318 static void restore_termios(void)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1319 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1320 tcsetattr(0, TCSAFLUSH, &orig_termios);
218
65b5c9390010 With _unix, Escape presses now cancel test data unarchiving
Oleg Oshmyan <chortos@inbox.lv>
parents: 210
diff changeset
1321 #ifdef O_ASYNC
65b5c9390010 With _unix, Escape presses now cancel test data unarchiving
Oleg Oshmyan <chortos@inbox.lv>
parents: 210
diff changeset
1322 fcntl(0, F_SETFL, orig_stdout_fl);
65b5c9390010 With _unix, Escape presses now cancel test data unarchiving
Oleg Oshmyan <chortos@inbox.lv>
parents: 210
diff changeset
1323 #endif
136
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1324 #ifdef USE_WAKEUP_FD
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1325 close_intpipe();
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1326 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1327 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1328 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1329
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1330 #if PY_MAJOR_VERSION >= 3
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1331 #define INIT_FAIL return NULL
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1332 static PyModuleDef _unixmodule =
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1333 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1334 PyModuleDef_HEAD_INIT,
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1335 "_unix",
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1336 NULL,
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1337 -1,
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1338 _unixMethods
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1339 };
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1340
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1341 PyMODINIT_FUNC PyInit__unix(void)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1342 #else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1343 #define INIT_FAIL return
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1344 PyMODINIT_FUNC init_unix(void)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1345 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1346 {
190
3ab18984e3e3 Fixed compilation on systems without termios.h
Oleg Oshmyan <chortos@inbox.lv>
parents: 186
diff changeset
1347 #ifdef HAVE_TERMIOS_H
136
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1348 struct termios new_termios;
190
3ab18984e3e3 Fixed compilation on systems without termios.h
Oleg Oshmyan <chortos@inbox.lv>
parents: 186
diff changeset
1349 #endif
185
9ed34ef740a1 The interrupt handler pipe is now closed on module creation failure
Oleg Oshmyan <chortos@inbox.lv>
parents: 146
diff changeset
1350 PyObject *exceptions, *module;
136
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1351
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1352 _unix__PopenPlaceholderType.tp_new = PyType_GenericNew;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1353 if (PyType_Ready(&_unix__PopenPlaceholderType) == -1)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1354 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1355 INIT_FAIL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1356 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1357
146
d5b6708c1955 Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents: 137
diff changeset
1358 exceptions = PyImport_ImportModule("upreckon.exceptions");
d5b6708c1955 Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents: 137
diff changeset
1359 if (exceptions == NULL
d5b6708c1955 Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents: 137
diff changeset
1360 || (CannotStartTestee = PyObject_GetAttrString(exceptions, "CannotStartTestee")) == NULL
d5b6708c1955 Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents: 137
diff changeset
1361 || (CanceledByUser = PyObject_GetAttrString(exceptions, "CanceledByUser")) == NULL
d5b6708c1955 Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents: 137
diff changeset
1362 || (WallTimeLimitExceeded = PyObject_GetAttrString(exceptions, "WallTimeLimitExceeded")) == NULL
d5b6708c1955 Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents: 137
diff changeset
1363 || (CPUTimeLimitExceeded = PyObject_GetAttrString(exceptions, "CPUTimeLimitExceeded")) == NULL
d5b6708c1955 Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents: 137
diff changeset
1364 || (MemoryLimitExceeded = PyObject_GetAttrString(exceptions, "MemoryLimitExceeded")) == NULL)
136
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1365 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1366 Py_XDECREF(MemoryLimitExceeded);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1367 Py_XDECREF(CPUTimeLimitExceeded);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1368 Py_XDECREF(WallTimeLimitExceeded);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1369 Py_XDECREF(CanceledByUser);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1370 Py_XDECREF(CannotStartTestee);
146
d5b6708c1955 Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents: 137
diff changeset
1371 Py_XDECREF(exceptions);
136
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1372 INIT_FAIL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1373 }
146
d5b6708c1955 Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents: 137
diff changeset
1374 Py_DECREF(exceptions);
136
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1375
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1376 #ifdef WITH_NEXT_FRAMEWORK
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1377 if (environ == NULL)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1378 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1379 environ = *_NSGetEnviron();
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1380 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1381 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1382
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1383 #ifdef USE_WAKEUP_FD
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1384 if (!intpipe[0] || !intpipe[1])
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1385 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1386 #ifdef HAVE_PIPE2
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1387 if (pipe2(intpipe, O_CLOEXEC | O_NONBLOCK))
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1388 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1389 PyErr_SetFromErrno(PyExc_IOError);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1390 Py_DECREF(MemoryLimitExceeded);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1391 Py_DECREF(CPUTimeLimitExceeded);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1392 Py_DECREF(WallTimeLimitExceeded);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1393 Py_DECREF(CanceledByUser);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1394 Py_DECREF(CannotStartTestee);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1395 INIT_FAIL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1396 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1397 #else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1398 if (pipe(intpipe))
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1399 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1400 PyErr_SetFromErrno(PyExc_IOError);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1401 Py_DECREF(MemoryLimitExceeded);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1402 Py_DECREF(CPUTimeLimitExceeded);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1403 Py_DECREF(WallTimeLimitExceeded);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1404 Py_DECREF(CanceledByUser);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1405 Py_DECREF(CannotStartTestee);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1406 INIT_FAIL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1407 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1408 // Other threads must not fork now
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1409 if (fcntl(intpipe[0], F_SETFD, fcntl(intpipe[0], F_GETFD) | FD_CLOEXEC) == -1
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1410 || fcntl(intpipe[1], F_SETFD, fcntl(intpipe[1], F_GETFD) | FD_CLOEXEC) == -1
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1411 || fcntl(intpipe[0], F_SETFL, fcntl(intpipe[0], F_GETFL) | O_NONBLOCK) == -1
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1412 || fcntl(intpipe[1], F_SETFL, fcntl(intpipe[1], F_GETFL) | O_NONBLOCK) == -1)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1413 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1414 PyErr_SetFromErrno(PyExc_IOError);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1415 close(intpipe[0]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1416 close(intpipe[1]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1417 Py_DECREF(MemoryLimitExceeded);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1418 Py_DECREF(CPUTimeLimitExceeded);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1419 Py_DECREF(WallTimeLimitExceeded);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1420 Py_DECREF(CanceledByUser);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1421 Py_DECREF(CannotStartTestee);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1422 INIT_FAIL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1423 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1424 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1425 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1426 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1427
185
9ed34ef740a1 The interrupt handler pipe is now closed on module creation failure
Oleg Oshmyan <chortos@inbox.lv>
parents: 146
diff changeset
1428 #if PY_MAJOR_VERSION >= 3
9ed34ef740a1 The interrupt handler pipe is now closed on module creation failure
Oleg Oshmyan <chortos@inbox.lv>
parents: 146
diff changeset
1429 module = PyModule_Create(&_unixmodule);
9ed34ef740a1 The interrupt handler pipe is now closed on module creation failure
Oleg Oshmyan <chortos@inbox.lv>
parents: 146
diff changeset
1430 #else
9ed34ef740a1 The interrupt handler pipe is now closed on module creation failure
Oleg Oshmyan <chortos@inbox.lv>
parents: 146
diff changeset
1431 module = Py_InitModule("_unix", _unixMethods);
9ed34ef740a1 The interrupt handler pipe is now closed on module creation failure
Oleg Oshmyan <chortos@inbox.lv>
parents: 146
diff changeset
1432 #endif
9ed34ef740a1 The interrupt handler pipe is now closed on module creation failure
Oleg Oshmyan <chortos@inbox.lv>
parents: 146
diff changeset
1433 if (module == NULL)
9ed34ef740a1 The interrupt handler pipe is now closed on module creation failure
Oleg Oshmyan <chortos@inbox.lv>
parents: 146
diff changeset
1434 {
186
0bd782fd41dc Fixed a possible compilation error introduced in the last changeset
Oleg Oshmyan <chortos@inbox.lv>
parents: 185
diff changeset
1435 #ifdef USE_WAKEUP_FD
185
9ed34ef740a1 The interrupt handler pipe is now closed on module creation failure
Oleg Oshmyan <chortos@inbox.lv>
parents: 146
diff changeset
1436 close(intpipe[0]);
9ed34ef740a1 The interrupt handler pipe is now closed on module creation failure
Oleg Oshmyan <chortos@inbox.lv>
parents: 146
diff changeset
1437 close(intpipe[1]);
186
0bd782fd41dc Fixed a possible compilation error introduced in the last changeset
Oleg Oshmyan <chortos@inbox.lv>
parents: 185
diff changeset
1438 #endif
185
9ed34ef740a1 The interrupt handler pipe is now closed on module creation failure
Oleg Oshmyan <chortos@inbox.lv>
parents: 146
diff changeset
1439 Py_DECREF(MemoryLimitExceeded);
9ed34ef740a1 The interrupt handler pipe is now closed on module creation failure
Oleg Oshmyan <chortos@inbox.lv>
parents: 146
diff changeset
1440 Py_DECREF(CPUTimeLimitExceeded);
9ed34ef740a1 The interrupt handler pipe is now closed on module creation failure
Oleg Oshmyan <chortos@inbox.lv>
parents: 146
diff changeset
1441 Py_DECREF(WallTimeLimitExceeded);
9ed34ef740a1 The interrupt handler pipe is now closed on module creation failure
Oleg Oshmyan <chortos@inbox.lv>
parents: 146
diff changeset
1442 Py_DECREF(CanceledByUser);
9ed34ef740a1 The interrupt handler pipe is now closed on module creation failure
Oleg Oshmyan <chortos@inbox.lv>
parents: 146
diff changeset
1443 Py_DECREF(CannotStartTestee);
9ed34ef740a1 The interrupt handler pipe is now closed on module creation failure
Oleg Oshmyan <chortos@inbox.lv>
parents: 146
diff changeset
1444 INIT_FAIL;
9ed34ef740a1 The interrupt handler pipe is now closed on module creation failure
Oleg Oshmyan <chortos@inbox.lv>
parents: 146
diff changeset
1445 }
9ed34ef740a1 The interrupt handler pipe is now closed on module creation failure
Oleg Oshmyan <chortos@inbox.lv>
parents: 146
diff changeset
1446
136
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1447 #ifdef HAVE_TERMIOS_H
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1448 if (!tcgetattr(0, &orig_termios))
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1449 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1450 new_termios = orig_termios;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1451 // Stolen from tty.py of Python 2.7.1
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1452 new_termios.c_lflag &= ~(ECHO | ICANON);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1453 new_termios.c_cc[VMIN] = 1;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1454 new_termios.c_cc[VTIME] = 0;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1455 if (!Py_AtExit(restore_termios) && !tcsetattr(0, TCSAFLUSH, &new_termios))
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1456 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1457 catch_escape = true;
218
65b5c9390010 With _unix, Escape presses now cancel test data unarchiving
Oleg Oshmyan <chortos@inbox.lv>
parents: 210
diff changeset
1458 #ifdef O_ASYNC
65b5c9390010 With _unix, Escape presses now cancel test data unarchiving
Oleg Oshmyan <chortos@inbox.lv>
parents: 210
diff changeset
1459 orig_stdout_fl = fcntl(0, F_GETFL);
65b5c9390010 With _unix, Escape presses now cancel test data unarchiving
Oleg Oshmyan <chortos@inbox.lv>
parents: 210
diff changeset
1460 fcntl(0, F_SETFL, orig_stdout_fl | O_ASYNC);
65b5c9390010 With _unix, Escape presses now cancel test data unarchiving
Oleg Oshmyan <chortos@inbox.lv>
parents: 210
diff changeset
1461 #endif
136
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1462 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1463 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1464 #ifdef USE_WAKEUP_FD
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1465 else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1466 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1467 Py_AtExit(close_intpipe);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1468 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1469 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1470 #elif defined USE_WAKEUP_FD
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1471 Py_AtExit(close_intpipe);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1472 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1473
191
4f69e30abbd5 Made sure the wakeup FD is always valid
Oleg Oshmyan <chortos@inbox.lv>
parents: 190
diff changeset
1474 #ifdef USE_WAKEUP_FD
4f69e30abbd5 Made sure the wakeup FD is always valid
Oleg Oshmyan <chortos@inbox.lv>
parents: 190
diff changeset
1475 PySignal_SetWakeupFd(intpipe[1]);
4f69e30abbd5 Made sure the wakeup FD is always valid
Oleg Oshmyan <chortos@inbox.lv>
parents: 190
diff changeset
1476 #endif
4f69e30abbd5 Made sure the wakeup FD is always valid
Oleg Oshmyan <chortos@inbox.lv>
parents: 190
diff changeset
1477
146
d5b6708c1955 Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents: 137
diff changeset
1478 #if PY_MAJOR_VERSION >= 3
136
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1479 return module;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1480 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1481 }