annotate upreckon/_unixmodule.cpp @ 253:d06e57b182a9

Embraced clock_gettime and mach_absolute_time in _unix Also made _unix wall clock time reports more precise; this was necessitated by the main changes.
author Oleg Oshmyan <chortos@inbox.lv>
date Fri, 14 Mar 2014 15:25:06 +0000
parents 65b5c9390010
children 393b4689ac2f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
253
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
1 // Copyright (c) 2011-2014 Chortos-2 <chortos@inbox.lv>
136
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
253
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
16 #ifdef __APPLE__
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
17 #include <mach/mach_time.h>
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
18 #endif
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
19
136
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
20 #ifdef HAVE_SIGNAL_H
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
21 #include <signal.h>
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
22 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
23
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
24 #ifdef HAVE_SPAWN_H
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
25 #include <spawn.h>
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
26 #ifdef __APPLE__
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
27 #pragma weak_import posix_spawnp
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
28 #endif
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_RESOURCE_H
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
32 #include <sys/resource.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_SYS_WAIT_H
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
36 #include <sys/wait.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 #ifdef HAVE_TERMIOS_H
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
40 #include <termios.h>
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
41 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
42
253
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
43 #ifdef HAVE_CLOCK_GETTIME
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
44 #include <time.h>
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
45 #endif
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
46
136
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
47 #if !(defined __cplusplus) && !(defined bool)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
48 #ifdef HAVE_C99_BOOL
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
49 #define bool _Bool
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
50 #else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
51 #define bool char
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
52 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
53 #undef true
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
54 #define true 1
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
55 #undef false
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
56 #define false 0
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
57 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
58
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
59 // 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
60 #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
61 #define USE_WAKEUP_FD
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
62 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
63
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
64 #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
65 #define RLIMIT_AS RLIMIT_VMEM
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
66 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
67
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
68 // 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
69 #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
70 //#ifdef HAVE_FORK1
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
71 #define fork fork1
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 // 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
75 #ifdef WITH_NEXT_FRAMEWORK
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
76 #include <crt_externs.h>
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
77 static char **environ = NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
78 #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
79 extern char **environ;
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
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
82 #ifndef Py_PYTIME_H
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
83 typedef struct timeval _PyTime_timeval;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
84 #ifndef GETTIMEOFDAY_NO_TZ
253
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
85 #define _PyTime_gettimeofday(tvp) gettimeofday(tvp, NULL)
136
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
86 #else
253
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
87 #define _PyTime_gettimeofday(tvp) gettimeofday(tvp)
136
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
88 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
89 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
90
253
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
91 static inline void timerget(_PyTime_timeval *tvp)
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
92 {
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
93 #if defined HAVE_CLOCK_GETTIME && (defined CLOCK_MONOTONIC_RAW || defined CLOCK_HIGHRES || defined CLOCK_MONOTONIC)
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
94 struct timespec ts;
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
95 #ifdef CLOCK_MONOTONIC_RAW
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
96 clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
97 #elif defined CLOCK_HIGHRES
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
98 clock_gettime(CLOCK_HIGHRES, &ts);
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
99 #else
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
100 clock_gettime(CLOCK_MONOTONIC, &ts);
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
101 #endif
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
102 tvp->tv_sec = ts.tv_sec;
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
103 tvp->tv_usec = ts.tv_nsec / 1000;
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
104 #elif defined __APPLE__
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
105 static mach_timebase_info_data_t base;
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
106 if (!base.numer)
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
107 {
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
108 mach_timebase_info(&base);
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
109 base.denom *= 1000;
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
110 }
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
111 uint64_t t = mach_absolute_time() * base.numer / base.denom;
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
112 tvp->tv_sec = t / 1000000;
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
113 tvp->tv_usec = t % 1000000;
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
114 #else
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
115 _PyTime_gettimeofday(tvp);
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
116 #endif
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
117 }
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
118
136
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
119 #if PY_MAJOR_VERSION >= 3
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
120 #define PyInt_AsLong PyLong_AsLong
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
121 #define PyInt_FromLong PyLong_FromLong
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
122 #define PyNumber_Int PyNumber_Long
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
123 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
124
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
125 #define TESTEE_SPAWNED 0
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
126 #define TESTEE_SPAWN_FAILED 1
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
127 #define TESTEE_REPORT_STATUS(status) \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
128 do \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
129 { \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
130 const char c = (status); \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
131 write(c2ppipe[1], &c, 1); \
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 while (0)
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 #if !(defined SIGKILL) && defined SIGTERM
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
136 #define SIGKILL SIGTERM
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
137 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
138
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
139 #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
140 #ifdef HAVE_WAITPID
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
141 #define TERM_TESTEE \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
142 do \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
143 { \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
144 kill(-curpid, SIGKILL); \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
145 kill(-curpid, SIGCONT); \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
146 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
147 } \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
148 while (0)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
149 #else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
150 #define TERM_TESTEE \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
151 do \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
152 { \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
153 kill(-curpid, SIGKILL); \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
154 kill(-curpid, SIGCONT); \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
155 while (wait(&retstat) != curpid); \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
156 } \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
157 while (0)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
158 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
159 #else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
160 #define TERM_TESTEE
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
161 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
162
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
163 #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
164 #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
165 #else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
166 #define PROPAGATE_SIGINT
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
167 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
168
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
169 struct child_stats
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
170 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
171 int returncode;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
172 _PyTime_timeval walltime;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
173 #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
174 _PyTime_timeval cputime;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
175 Py_ssize_t memory;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
176 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
177 };
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
178
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
179 static pid_t curpid;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
180 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
181 static PyObject *CannotStartTestee, *CanceledByUser, *WallTimeLimitExceeded,
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
182 *CPUTimeLimitExceeded, *MemoryLimitExceeded;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
183 static _PyTime_timeval time_end;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
184
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
185 #ifdef USE_WAKEUP_FD
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
186 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
187 static int intpipe[2] = { 0 };
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
188 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
189
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
190 #ifdef HAVE_TERMIOS_H
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
191 static bool catch_escape = false;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
192 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
193 #ifdef O_ASYNC
65b5c9390010 With _unix, Escape presses now cancel test data unarchiving
Oleg Oshmyan <chortos@inbox.lv>
parents: 210
diff changeset
194 static int orig_stdout_fl;
65b5c9390010 With _unix, Escape presses now cancel test data unarchiving
Oleg Oshmyan <chortos@inbox.lv>
parents: 210
diff changeset
195 #endif
136
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
196 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
197
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
198 typedef struct
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
199 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
200 PyObject_HEAD
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
201 int returncode;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
202 } _unix__PopenPlaceholderObject;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
203
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
204 static PyMemberDef _PopenPlaceholder_members[] =
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
205 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
206 { "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
207 { NULL }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
208 };
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
209
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
210 static PyTypeObject _unix__PopenPlaceholderType =
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 #if PY_MAJOR_VERSION >= 3
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
213 PyVarObject_HEAD_INIT(NULL, 0)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
214 #else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
215 PyObject_HEAD_INIT(NULL)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
216 0, /*ob_size*/
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
217 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
218 "_unix._PopenPlaceholder", /*tp_name*/
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
219 sizeof(_unix__PopenPlaceholderObject), /*tp_basicsize*/
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
220 0, /*tp_itemsize*/
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
221 0, /*tp_dealloc*/
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
222 0, /*tp_print*/
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
223 0, /*tp_getattr*/
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
224 0, /*tp_setattr*/
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
225 0, /*tp_compare*/
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
226 0, /*tp_repr*/
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
227 0, /*tp_as_number*/
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
228 0, /*tp_as_sequence*/
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
229 0, /*tp_as_mapping*/
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
230 0, /*tp_hash */
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
231 0, /*tp_call*/
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
232 0, /*tp_str*/
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
233 0, /*tp_getattro*/
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
234 0, /*tp_setattro*/
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
235 0, /*tp_as_buffer*/
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
236 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
237 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
238 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
239 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
240 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
241 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
242 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
243 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
244 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
245 _PopenPlaceholder_members, /*tp_members*/
136
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
246 };
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
247
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
248 #ifndef timeradd
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
249 #define timeradd(a, b, res) \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
250 do \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
251 { \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
252 (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
253 (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
254 if ((res)->tv_usec >= 1000000) \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
255 { \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
256 ++(res)->tv_sec; \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
257 (res)->tv_usec -= 1000000; \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
258 } \
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 while (0)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
261 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
262
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
263 #ifndef timersub
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
264 #define timersub(a, b, res) \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
265 do \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
266 { \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
267 (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
268 (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
269 if ((res)->tv_usec < 0) \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
270 { \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
271 --(res)->tv_sec; \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
272 (res)->tv_usec += 1000000; \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
273 } \
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 while (0)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
276 #endif
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 #ifndef timerclear
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
279 #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
280 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
281
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
282 #ifndef timerisset
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
283 #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
284 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
285
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
286 #ifndef timercmp
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
287 #define timercmp(a, b, cmp) \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
288 (((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
289 ? ((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
290 : ((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
291 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
292
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
293 // 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
294 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
295 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
296 Py_ssize_t i;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
297 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
298 PyMem_Free(array[i]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
299 PyMem_DEL(array);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
300 }
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 // 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
303 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
304 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
305 int fd = PyObject_AsFileDescriptor(obj);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
306 if (fd >= 0)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
307 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
308 *((int *) p) = fd;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
309 return 1;
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 return 0;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
312 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
313
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
314 // 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
315 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
316 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
317 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
318 static PyObject *dummy_args = NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
319 Py_ssize_t i, argc;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
320 char **argv;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
321 bool own_args = false;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
322 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
323
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
324 if (dummy_args == NULL)
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 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
327 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
328 return -1;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
329 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
330 }
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 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
333 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
334 return -1;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
335 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
336 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
337 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
338 return -1;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
339 }
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 #if PY_MAJOR_VERSION >= 3
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
342 if (PyUnicode_Check(args))
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
343 #else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
344 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
345 #endif
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 argc = 1;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
348 args = PyTuple_Pack(1, args);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
349 if (args == NULL)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
350 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
351 return -1;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
352 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
353 own_args = true;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
354 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
355 else if (!PySequence_Check(args))
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 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
358 return -1;
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 else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
361 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
362 argc = PySequence_Size(args);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
363 if (argc < 1)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
364 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
365 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
366 return -1;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
367 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
368 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
369
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
370 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
371 if (argv == NULL)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
372 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
373 if (own_args)
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 Py_DECREF(args);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
376 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
377 PyErr_NoMemory();
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
378 return -1;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
379 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
380
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
381 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
382 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
383 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
384 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
385 free_string_array(argv, i);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
386 if (own_args)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
387 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
388 Py_DECREF(args);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
389 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
390 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
391 return -1;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
392 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
393 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
394 argv[argc] = NULL;
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 curpid = fork();
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
397 if (!curpid)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
398 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
399 pid_t pid;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
400 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
401 struct child_stats stats;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
402 _PyTime_timeval tvstart, tvend;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
403 #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
404 struct rusage rusage;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
405 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
406 #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
407 struct rlimit rlimit;
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
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
410 /*
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
411 Assume no errors occur:
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
412 * 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
413 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
414 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
415 * 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
416 for setpgid can occur.
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
417 */
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
418 #ifdef HAVE_SETPGID
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
419 setpgid(0, 0);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
420 #else //if defined HAVE_SETPGRP
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
421 #ifdef SETPGRP_HAVE_ARG
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
422 setpgrp(0, 0);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
423 #else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
424 setpgrp();
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
425 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
426 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
427
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
428 #ifdef SIGINT
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
429 signal(SIGINT, SIG_DFL);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
430 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
431
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
432 #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
433 _Py_RestoreSignals();
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
434 #else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
435 #ifdef SIGPIPE
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
436 signal(SIGPIPE, SIG_DFL);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
437 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
438 #ifdef SIGXFSZ
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
439 signal(SIGXFSZ, SIG_DFL);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
440 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
441 #ifdef SIGXFZ
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
442 signal(SIGXFZ, SIG_DFL);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
443 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
444 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
445
218
65b5c9390010 With _unix, Escape presses now cancel test data unarchiving
Oleg Oshmyan <chortos@inbox.lv>
parents: 210
diff changeset
446 #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
447 signal(SIGIO, SIG_DFL);
65b5c9390010 With _unix, Escape presses now cancel test data unarchiving
Oleg Oshmyan <chortos@inbox.lv>
parents: 210
diff changeset
448 #endif
65b5c9390010 With _unix, Escape presses now cancel test data unarchiving
Oleg Oshmyan <chortos@inbox.lv>
parents: 210
diff changeset
449
136
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
450 if (c2ppipe[1] < 3)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
451 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
452 int newfd;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
453 #ifdef F_DUPFD_CLOEXEC
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
454 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
455 if (newfd == -1)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
456 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
457 spawn_errno = errno;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
458 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
459 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
460 _exit(127);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
461 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
462 c2ppipe[1] = newfd;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
463 #else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
464 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
465 // 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
466 if (newfd == -1
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
467 || 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
468 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
469 spawn_errno = errno;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
470 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
471 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
472 _exit(127);
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 c2ppipe[1] = newfd;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
475 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
476 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
477 // 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
478 // 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
479 // FIXME: error handling
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
480 fddupped[0] = dup(fdin);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
481 fddupped[1] = dup(fdout);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
482 fddupped[2] = dup(fderr);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
483 dup2(fddupped[0], 0);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
484 dup2(fddupped[1], 1);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
485 dup2(fddupped[2], 2);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
486 // 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
487 // 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
488 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
489 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
490 close(fd);
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 while (--fd >= 3)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
493 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
494 close(fd);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
495 }
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 #ifdef RLIMIT_AS
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
498 if (maxmemory)
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 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
501 setrlimit(RLIMIT_AS, &rlimit);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
502 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
503 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
504 #ifdef RLIMIT_CPU
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
505 if (maxcputime)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
506 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
507 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
508 setrlimit(RLIMIT_CPU, &rlimit);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
509 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
510 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
511
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
512 #ifdef HAVE_SPAWN_H
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
513 #ifdef __APPLE__
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
514 if (posix_spawnp != NULL)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
515 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
516 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
517 spawn_errno = posix_spawnp(&pid, argv[0], NULL, NULL, argv, environ);
253
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
518 timerget(&tvstart);
136
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
519
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
520 if (spawn_errno)
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 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
523 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
524 _exit(127);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
525 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
526 #ifdef __APPLE__
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
527 }
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 #endif
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 #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
532 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
533 pid = fork();
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
534 if (!pid)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
535 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
536 execvp(argv[0], argv);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
537 spawn_errno = errno;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
538 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
539 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
540 _exit(127);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
541 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
542 else if (pid == -1)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
543 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
544 spawn_errno = errno;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
545 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
546 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
547 _exit(127);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
548 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
549 else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
550 {
253
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
551 timerget(&tvstart);
136
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 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
554 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
555 TESTEE_REPORT_STATUS(TESTEE_SPAWNED);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
556 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
557
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
558 #ifdef HAVE_WAIT4
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
559 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
560 #elif defined HAVE_WAIT3
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
561 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
562 #elif defined HAVE_WAITPID
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
563 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
564 #else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
565 while (wait(&status) != pid);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
566 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
567
253
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
568 timerget(&tvend);
136
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
569 #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
570 getrusage(RUSAGE_CHILDREN, &rusage);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
571 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
572
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
573 stats = zero_stats;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
574
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
575 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
576 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
577 else stats.returncode = WEXITSTATUS(status);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
578
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
579 timersub(&tvend, &tvstart, &stats.walltime);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
580 #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
581 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
582 #ifdef __APPLE__
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
583 stats.memory = rusage.ru_maxrss;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
584 #else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
585 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
586 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
587 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
588
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
589 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
590 _exit(0);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
591 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
592 else if (curpid == -1)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
593 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
594 PyErr_SetFromErrno(PyExc_OSError);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
595 free_string_array(argv, argc);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
596 if (own_args)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
597 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
598 Py_DECREF(args);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
599 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
600 return 0;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
601 }
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 /*
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
604 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
605 * 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
606 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
607 * 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
608 for setpgid can occur.
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 #ifdef HAVE_SETPGID
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
611 setpgid(curpid, 0);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
612 #elif defined SETPGRP_HAVE_ARG
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
613 setpgrp(curpid, 0);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
614 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
615
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
616 free_string_array(argv, argc);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
617 if (own_args)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
618 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
619 Py_DECREF(args);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
620 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
621 return 1;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
622 }
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 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
625 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
626 #ifdef HAVE_LONG_LONG
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
627 long long i_whole;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
628 #else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
629 long i_whole;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
630 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
631 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
632 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
633 if (member == NULL)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
634 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
635 return false;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
636 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
637 if (member == Py_None)
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 Py_DECREF(member);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
640 timerclear(ptv);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
641 return true;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
642 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
643 whole = PyNumber_Int(member);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
644 if (whole == NULL)
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 Py_DECREF(member);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
647 return false;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
648 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
649 #ifdef HAVE_LONG_LONG
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
650 i_whole = PyLong_AsLongLong(whole);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
651 #else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
652 i_whole = PyInt_AsLong(whole);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
653 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
654 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
655 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
656 Py_DECREF(whole);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
657 Py_DECREF(member);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
658 return false;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
659 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
660 // FIXME: detect time_t overflow
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
661 ptv->tv_sec = i_whole;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
662 frac = PyNumber_Subtract(member, whole);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
663 Py_DECREF(whole);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
664 Py_DECREF(member);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
665 if (frac == NULL)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
666 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
667 return false;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
668 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
669 million = PyInt_FromLong(1000000);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
670 if (million == NULL)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
671 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
672 Py_DECREF(frac);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
673 return false;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
674 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
675 usec = PyNumber_InPlaceMultiply(frac, million);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
676 Py_DECREF(million);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
677 Py_DECREF(frac);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
678 if (usec == NULL)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
679 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
680 return false;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
681 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
682 usec_whole = PyNumber_Int(usec);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
683 Py_DECREF(usec);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
684 if (usec_whole == NULL)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
685 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
686 return false;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
687 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
688 // 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
689 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
690 Py_DECREF(usec_whole);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
691 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
692 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
693
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
694 #ifdef __cplusplus
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
695 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
696 static char is_int(char);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
697 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
698 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
699 static char is_int(short);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
700 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
701 static char is_int(int);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
702 static char is_int(unsigned);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
703 static char is_int(long);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
704 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
705 #ifdef HAVE_LONG_LONG
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
706 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
707 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
708 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
709 static two_chars is_int(...);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
710 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
711
253
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
712 static inline PyObject *timeval_to_obj(const _PyTime_timeval *ptv)
136
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
713 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
714 #ifdef __cplusplus
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
715 // 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
716 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
717 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
718 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
719 {
253
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
720 return PyInt_FromLong(ptv->tv_sec);
136
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
721 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
722 // 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
723 #ifdef HAVE_LONG_LONG
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
724 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
725 {
253
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
726 return PyLong_FromUnsignedLongLong(ptv->tv_sec);
136
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
727 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
728 #else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
729 // 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
730 // {
253
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
731 // return PyLong_FromUnsignedLong(ptv->tv_sec);
136
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 //#endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
734 else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
735 {
253
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
736 return PyFloat_FromDouble(ptv->tv_sec);
136
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
737 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
738 //
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
739 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
740 //
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
741 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
742 else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
743 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
744 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
745 // TODO: use decimal.Decimal or fractions.Fraction
253
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
746 return PyFloat_FromDouble(ptv->tv_sec + ptv->tv_usec * 0.000001);
136
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
747 }
253
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
748 }
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
749
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
750 static inline bool timeval_to_attr(const _PyTime_timeval *ptv, PyObject *obj, const char *attr)
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
751 {
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
752 PyObject *value = timeval_to_obj(ptv);
136
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
753 if (value == NULL)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
754 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
755 return false;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
756 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
757 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
758 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
759 return false;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
760 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
761 Py_DECREF(value);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
762 return true;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
763 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
764
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 TODO/FIXME:
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
767 * 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
768 */
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
769 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
770 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
771 PyObject *testcase = NULL, *obj;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
772 _unix__PopenPlaceholderObject *Popen_placeholder;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
773 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
774 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
775 _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
776 Py_ssize_t maxmemory, r;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
777 size_t stats_read = 0;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
778 fd_set readfds;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
779 char c;
253
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
780 bool have_maxwalltime, wall_time_exceeded;
136
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 if (kwds != NULL)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
783 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
784 testcase = PyDict_GetItemString(kwds, "case");
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
785 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
786 if (testcase == NULL)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
787 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
788 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
789 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
790 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
791 Py_INCREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
792 PyDict_DelItemString(kwds, "case");
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
793
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
794 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
795 || !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
796 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
797 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
798 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
799 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
800
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
801 obj = PyObject_GetAttrString(testcase, "maxmemory");
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
802 if (obj == NULL)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
803 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
804 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
805 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
806 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
807 if (PyObject_IsTrue(obj))
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 PyObject *factor, *bytes;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
810 factor = PyInt_FromLong(1024 * 1024);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
811 if (factor == 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 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
814 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
815 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
816 bytes = PyNumber_Multiply(obj, factor);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
817 Py_DECREF(factor);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
818 if (bytes == NULL)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
819 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
820 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
821 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
822 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
823 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
824 Py_DECREF(bytes);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
825 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
826 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
827 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
828 return NULL;
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 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
831 else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
832 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
833 maxmemory = 0;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
834 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
835 Py_DECREF(obj);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
836
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
837 #ifdef HAVE_PIPE2
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
838 if (pipe2(c2ppipe, O_CLOEXEC))
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
839 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
840 PyErr_SetFromErrno(PyExc_IOError);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
841 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
842 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
843 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
844 #else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
845 if (pipe(c2ppipe))
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
846 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
847 PyErr_SetFromErrno(PyExc_IOError);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
848 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
849 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
850 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
851 // 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
852 // (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
853 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
854 || 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
855 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
856 PyErr_SetFromErrno(PyExc_IOError);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
857 close(c2ppipe[0]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
858 close(c2ppipe[1]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
859 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
860 return NULL;
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 #endif
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 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
865 close(c2ppipe[1]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
866 if (!spawn_status)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
867 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
868 PyObject *type, *value, *traceback, *e;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
869 close(c2ppipe[0]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
870 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
871 PyErr_Fetch(&type, &value, &traceback);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
872 PyErr_NormalizeException(&type, &value, &traceback);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
873 Py_XDECREF(traceback);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
874 Py_DECREF(type);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
875 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
876 Py_DECREF(value);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
877 PyErr_SetObject(CannotStartTestee, e);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
878 Py_DECREF(e);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
879 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
880 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
881 else if (spawn_status < 0)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
882 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
883 close(c2ppipe[0]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
884 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
885 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
886 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
887
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
888 // 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
889 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
890 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
891 if (PyErr_CheckSignals() == -1)
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 PROPAGATE_SIGINT;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
894 close(c2ppipe[0]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
895 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
896 TERM_TESTEE;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
897 return NULL;
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 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
900 if (r == 1)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
901 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
902 if (c == TESTEE_SPAWNED)
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 size_t got = 0;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
905 while (got < sizeof time_start)
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 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
908 if (r > 0)
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 got += r;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
911 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
912 else if (!r)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
913 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
914 errno = 0;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
915 PyErr_SetFromErrno(PyExc_IOError);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
916 goto spawn_failed;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
917 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
918 else if (errno == EINTR)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
919 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
920 if (PyErr_CheckSignals() == -1)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
921 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
922 PROPAGATE_SIGINT;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
923 close(c2ppipe[0]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
924 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
925 TERM_TESTEE;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
926 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
927 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
928 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
929 else
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 PyErr_SetFromErrno(PyExc_IOError);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
932 goto spawn_failed;
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 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
935 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
936 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
937 close(c2ppipe[0]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
938 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
939 TERM_TESTEE;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
940 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
941 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
942 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
943 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
944 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
945 size_t got = 0;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
946 while (got < sizeof spawn_errno)
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 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
949 if (r > 0)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
950 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
951 got += r;
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 if (!r)
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 // 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
956 spawn_errno = 0;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
957 break;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
958 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
959 else if (errno == EINTR)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
960 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
961 if (PyErr_CheckSignals() == -1)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
962 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
963 PROPAGATE_SIGINT;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
964 close(c2ppipe[0]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
965 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
966 TERM_TESTEE;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
967 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
968 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
969 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
970 else
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 PyErr_SetFromErrno(PyExc_IOError);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
973 goto spawn_failed;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
974 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
975 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
976 errno = spawn_errno;
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 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
979 || 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
980 || errno == ENOEXEC || errno == ETXTBSY)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
981 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
982 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
983 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
984 else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
985 {*/
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
986 PyErr_SetFromErrno(PyExc_OSError);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
987 //}
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
988 goto spawn_failed;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
989 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
990 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
991 else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
992 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
993 PyObject *type, *value, *traceback, *e;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
994 if (!r) errno = 0;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
995 PyErr_SetFromErrno(PyExc_IOError);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
996 spawn_failed:
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
997 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
998 close(c2ppipe[0]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
999 TERM_TESTEE;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1000 PyErr_Fetch(&type, &value, &traceback);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1001 PyErr_NormalizeException(&type, &value, &traceback);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1002 Py_XDECREF(traceback);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1003 Py_DECREF(type);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1004 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
1005 Py_DECREF(value);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1006 PyErr_SetObject(CannotStartTestee, e);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1007 Py_DECREF(e);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1008 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1009 }
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 Py_BEGIN_ALLOW_THREADS
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1012 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
1013 FD_ZERO(&readfds);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1014 have_maxwalltime = timerisset(&maxwalltime);
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 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
1017 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
1018 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
1019 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
1020 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
1021 actual timeout value.
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1022 (POSIX:2008)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1023 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
1024 */
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1025 for (;;)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1026 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1027 _PyTime_timeval now;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1028 int maxfd = c2ppipe[0];
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1029 #ifdef HAVE_TERMIOS_H
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1030 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
1031 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1032 #ifdef USE_WAKEUP_FD
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1033 FD_SET(intpipe[0], &readfds);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1034 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
1035 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1036 FD_SET(c2ppipe[0], &readfds);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1037
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1038 if (have_maxwalltime)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1039 {
253
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
1040 timerget(&now);
136
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1041 if (timercmp(&time_end, &now, <))
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1042 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1043 timerclear(&timeout);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1044 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1045 else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1046 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1047 timersub(&time_end, &now, &timeout);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1048 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1049
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1050 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
1051
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1052 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
1053 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1054 close(c2ppipe[0]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1055 TERM_TESTEE;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1056 Py_BLOCK_THREADS
253
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
1057 timeval_to_attr(&now, testcase, "time_stopped");
136
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1058 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1059 PyErr_SetObject(WallTimeLimitExceeded, NULL);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1060 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1061 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1062 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1063 else
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 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
1066 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1067
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1068 if (s < 0 && errno == EINTR)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1069 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1070 Py_BLOCK_THREADS
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1071 if (PyErr_CheckSignals() == -1)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1072 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1073 PROPAGATE_SIGINT;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1074 close(c2ppipe[0]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1075 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1076 TERM_TESTEE;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1077 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1078 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1079 Py_UNBLOCK_THREADS
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1080 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1081 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
1082 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1083 Py_BLOCK_THREADS
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1084 PyErr_SetFromErrno(PyExc_IOError);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1085 close(c2ppipe[0]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1086 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1087 TERM_TESTEE;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1088 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1089 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1090 #ifdef USE_WAKEUP_FD
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1091 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
1092 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1093 // FIXME: is error handling needed?
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1094 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
1095 Py_BLOCK_THREADS
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1096 if (PyErr_CheckSignals() == -1)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1097 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1098 PROPAGATE_SIGINT;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1099 close(c2ppipe[0]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1100 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1101 TERM_TESTEE;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1102 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1103 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1104 Py_UNBLOCK_THREADS
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 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1107 #ifdef HAVE_TERMIOS_H
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1108 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
1109 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1110 // 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
1111 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
1112 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1113 if (c == '\33')
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 close(c2ppipe[0]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1116 TERM_TESTEE;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1117 Py_BLOCK_THREADS
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1118 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1119 PyErr_SetObject(CanceledByUser, NULL);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1120 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1121 }
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 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
1124 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1125 if (PyErr_CheckSignals() == -1)
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 PROPAGATE_SIGINT;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1128 close(c2ppipe[0]);
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 TERM_TESTEE;
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 }
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 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1136 else if (s > 0)
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 bool blocked_threads = false;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1139 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
1140 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1141 Py_BLOCK_THREADS
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1142 blocked_threads = true;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1143 if (PyErr_CheckSignals() == -1)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1144 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1145 PROPAGATE_SIGINT;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1146 close(c2ppipe[0]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1147 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1148 TERM_TESTEE;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1149 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1150 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1151 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1152 if (r > 0)
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 stats_read += r;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1155 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1156 else if (!r)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1157 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1158 break;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1159 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1160 else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1161 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1162 close(c2ppipe[0]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1163 TERM_TESTEE;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1164 if (!blocked_threads)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1165 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1166 Py_BLOCK_THREADS
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1167 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1168 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1169 PyErr_SetFromErrno(PyExc_IOError);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1170 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1171 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1172 if (blocked_threads)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1173 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1174 Py_UNBLOCK_THREADS
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 }
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 close(c2ppipe[0]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1179 Py_END_ALLOW_THREADS
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1180
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1181 #ifdef HAVE_WAITPID
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1182 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
1183 #else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1184 while (wait(&retstat) != curpid)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1185 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1186 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1187 if (PyErr_CheckSignals() == -1)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1188 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1189 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1190 return NULL;
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
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1194 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
1195 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1196 PyObject *type, *value, *traceback, *e;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1197 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1198 errno = 0;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1199 PyErr_SetFromErrno(PyExc_OSError);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1200 PyErr_Fetch(&type, &value, &traceback);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1201 PyErr_NormalizeException(&type, &value, &traceback);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1202 Py_XDECREF(traceback);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1203 Py_DECREF(type);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1204 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
1205 Py_DECREF(value);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1206 PyErr_SetObject(CannotStartTestee, e);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1207 Py_DECREF(e);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1208 return 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 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
1211 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1212 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1213 if (WIFSTOPPED(retstat))
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 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
1216 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1217 else if (WIFSIGNALED(retstat))
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1218 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1219 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
1220 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1221 else if (WIFEXITED(retstat))
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 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
1224 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1225 else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1226 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1227 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
1228 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1229 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1230 }
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 if (stats_read != sizeof stats)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1233 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1234 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1235 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
1236 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1237 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1238
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1239 obj = PyInt_FromLong(0);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1240 if (obj == NULL)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1241 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1242 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1243 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1244 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1245 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
1246 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1247 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1248 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1249 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1250 Py_DECREF(obj);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1251
253
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
1252 wall_time_exceeded = timerisset(&maxwalltime) && timercmp(&stats.walltime, &maxwalltime, >);
136
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1253 #if defined HAVE_SYS_RESOURCE_H || defined HAVE_WAIT4 || defined HAVE_WAIT3
253
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
1254 if (!wall_time_exceeded && (timerisset(&maxcputime) || !timerisset(&maxwalltime)))
136
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 PyObject *cputls;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1257 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
1258 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1259 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1260 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1261 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1262 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
1263 if (cputls == 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 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1266 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1267 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1268 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
1269 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1270 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1271 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1272 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1273 Py_DECREF(cputls);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1274 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
1275 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1276 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1277 PyErr_SetObject(CPUTimeLimitExceeded, NULL);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1278 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1279 }
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 else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1282 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1283 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1284 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
1285 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1286 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1287 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1288 }
253
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
1289 if (wall_time_exceeded)
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
1290 {
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
1291 Py_DECREF(testcase);
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
1292 PyErr_SetObject(WallTimeLimitExceeded, NULL);
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
1293 return NULL;
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
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
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1297 #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
1298 if (maxmemory && stats.memory > maxmemory)
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 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1301 PyErr_SetObject(MemoryLimitExceeded, NULL);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1302 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1303 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1304 #endif
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 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
1307 if (Popen_placeholder == NULL)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1308 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1309 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1310 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1311 Popen_placeholder->returncode = stats.returncode;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1312 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
1313 Py_DECREF(Popen_placeholder);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1314 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1315 Py_RETURN_NONE;
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
253
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
1318 static PyObject *_unix_clock(PyObject *self)
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
1319 {
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
1320 _PyTime_timeval tv;
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
1321 timerget(&tv);
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
1322 return timeval_to_obj(&tv);
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
1323 }
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
1324
136
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1325 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
1326 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1327 #ifdef HAVE_TERMIOS_H
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1328 if (catch_escape)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1329 {
218
65b5c9390010 With _unix, Escape presses now cancel test data unarchiving
Oleg Oshmyan <chortos@inbox.lv>
parents: 210
diff changeset
1330 #ifdef O_ASYNC
65b5c9390010 With _unix, Escape presses now cancel test data unarchiving
Oleg Oshmyan <chortos@inbox.lv>
parents: 210
diff changeset
1331 signal(SIGIO, SIG_DFL);
65b5c9390010 With _unix, Escape presses now cancel test data unarchiving
Oleg Oshmyan <chortos@inbox.lv>
parents: 210
diff changeset
1332 #endif
136
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1333 char c;
210
8c4e92fb32d8 Keyboard interrupts now satisfy the Press any key to exit... prompt
Oleg Oshmyan <chortos@inbox.lv>
parents: 191
diff changeset
1334 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
1335 {
8c4e92fb32d8 Keyboard interrupts now satisfy the Press any key to exit... prompt
Oleg Oshmyan <chortos@inbox.lv>
parents: 191
diff changeset
1336 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
1337 {
8c4e92fb32d8 Keyboard interrupts now satisfy the Press any key to exit... prompt
Oleg Oshmyan <chortos@inbox.lv>
parents: 191
diff changeset
1338 return NULL;
8c4e92fb32d8 Keyboard interrupts now satisfy the Press any key to exit... prompt
Oleg Oshmyan <chortos@inbox.lv>
parents: 191
diff changeset
1339 }
8c4e92fb32d8 Keyboard interrupts now satisfy the Press any key to exit... prompt
Oleg Oshmyan <chortos@inbox.lv>
parents: 191
diff changeset
1340 }
136
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1341 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1342 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1343 Py_RETURN_NONE;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1344 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1345
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1346 static PyMethodDef _unixMethods[] =
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1347 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1348 { "call", (PyCFunction) _unix_call, METH_VARARGS | METH_KEYWORDS, "Call a process." },
253
d06e57b182a9 Embraced clock_gettime and mach_absolute_time in _unix
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
1349 { "clock", (PyCFunction) _unix_clock, METH_NOARGS, "Return the current time in seconds since an unspecified reference point." },
136
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1350 { "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
1351 { NULL }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1352 };
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1353
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1354 #ifdef USE_WAKEUP_FD
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1355 static void close_intpipe(void)
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 close(intpipe[0]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1358 close(intpipe[1]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1359 intpipe[0] = intpipe[1] = 0;
191
4f69e30abbd5 Made sure the wakeup FD is always valid
Oleg Oshmyan <chortos@inbox.lv>
parents: 190
diff changeset
1360 PySignal_SetWakeupFd(-1);
136
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1361 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1362 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1363
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1364 #ifdef HAVE_TERMIOS_H
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1365 static void restore_termios(void)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1366 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1367 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
1368 #ifdef O_ASYNC
65b5c9390010 With _unix, Escape presses now cancel test data unarchiving
Oleg Oshmyan <chortos@inbox.lv>
parents: 210
diff changeset
1369 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
1370 #endif
136
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1371 #ifdef USE_WAKEUP_FD
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1372 close_intpipe();
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1373 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1374 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1375 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1376
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1377 #if PY_MAJOR_VERSION >= 3
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1378 #define INIT_FAIL return NULL
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1379 static PyModuleDef _unixmodule =
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 PyModuleDef_HEAD_INIT,
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1382 "_unix",
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1383 NULL,
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1384 -1,
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1385 _unixMethods
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1386 };
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1387
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1388 PyMODINIT_FUNC PyInit__unix(void)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1389 #else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1390 #define INIT_FAIL return
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1391 PyMODINIT_FUNC init_unix(void)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1392 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1393 {
190
3ab18984e3e3 Fixed compilation on systems without termios.h
Oleg Oshmyan <chortos@inbox.lv>
parents: 186
diff changeset
1394 #ifdef HAVE_TERMIOS_H
136
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1395 struct termios new_termios;
190
3ab18984e3e3 Fixed compilation on systems without termios.h
Oleg Oshmyan <chortos@inbox.lv>
parents: 186
diff changeset
1396 #endif
185
9ed34ef740a1 The interrupt handler pipe is now closed on module creation failure
Oleg Oshmyan <chortos@inbox.lv>
parents: 146
diff changeset
1397 PyObject *exceptions, *module;
136
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1398
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1399 _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
1400 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
1401 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1402 INIT_FAIL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1403 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1404
146
d5b6708c1955 Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents: 137
diff changeset
1405 exceptions = PyImport_ImportModule("upreckon.exceptions");
d5b6708c1955 Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents: 137
diff changeset
1406 if (exceptions == NULL
d5b6708c1955 Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents: 137
diff changeset
1407 || (CannotStartTestee = PyObject_GetAttrString(exceptions, "CannotStartTestee")) == NULL
d5b6708c1955 Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents: 137
diff changeset
1408 || (CanceledByUser = PyObject_GetAttrString(exceptions, "CanceledByUser")) == NULL
d5b6708c1955 Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents: 137
diff changeset
1409 || (WallTimeLimitExceeded = PyObject_GetAttrString(exceptions, "WallTimeLimitExceeded")) == NULL
d5b6708c1955 Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents: 137
diff changeset
1410 || (CPUTimeLimitExceeded = PyObject_GetAttrString(exceptions, "CPUTimeLimitExceeded")) == NULL
d5b6708c1955 Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents: 137
diff changeset
1411 || (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
1412 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1413 Py_XDECREF(MemoryLimitExceeded);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1414 Py_XDECREF(CPUTimeLimitExceeded);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1415 Py_XDECREF(WallTimeLimitExceeded);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1416 Py_XDECREF(CanceledByUser);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1417 Py_XDECREF(CannotStartTestee);
146
d5b6708c1955 Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents: 137
diff changeset
1418 Py_XDECREF(exceptions);
136
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1419 INIT_FAIL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1420 }
146
d5b6708c1955 Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents: 137
diff changeset
1421 Py_DECREF(exceptions);
136
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1422
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1423 #ifdef WITH_NEXT_FRAMEWORK
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1424 if (environ == NULL)
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 environ = *_NSGetEnviron();
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1427 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1428 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1429
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1430 #ifdef USE_WAKEUP_FD
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1431 if (!intpipe[0] || !intpipe[1])
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1432 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1433 #ifdef HAVE_PIPE2
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1434 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
1435 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1436 PyErr_SetFromErrno(PyExc_IOError);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1437 Py_DECREF(MemoryLimitExceeded);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1438 Py_DECREF(CPUTimeLimitExceeded);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1439 Py_DECREF(WallTimeLimitExceeded);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1440 Py_DECREF(CanceledByUser);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1441 Py_DECREF(CannotStartTestee);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1442 INIT_FAIL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1443 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1444 #else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1445 if (pipe(intpipe))
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1446 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1447 PyErr_SetFromErrno(PyExc_IOError);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1448 Py_DECREF(MemoryLimitExceeded);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1449 Py_DECREF(CPUTimeLimitExceeded);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1450 Py_DECREF(WallTimeLimitExceeded);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1451 Py_DECREF(CanceledByUser);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1452 Py_DECREF(CannotStartTestee);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1453 INIT_FAIL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1454 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1455 // 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
1456 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
1457 || 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
1458 || 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
1459 || 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
1460 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1461 PyErr_SetFromErrno(PyExc_IOError);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1462 close(intpipe[0]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1463 close(intpipe[1]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1464 Py_DECREF(MemoryLimitExceeded);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1465 Py_DECREF(CPUTimeLimitExceeded);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1466 Py_DECREF(WallTimeLimitExceeded);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1467 Py_DECREF(CanceledByUser);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1468 Py_DECREF(CannotStartTestee);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1469 INIT_FAIL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1470 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1471 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1472 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1473 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1474
185
9ed34ef740a1 The interrupt handler pipe is now closed on module creation failure
Oleg Oshmyan <chortos@inbox.lv>
parents: 146
diff changeset
1475 #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
1476 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
1477 #else
9ed34ef740a1 The interrupt handler pipe is now closed on module creation failure
Oleg Oshmyan <chortos@inbox.lv>
parents: 146
diff changeset
1478 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
1479 #endif
9ed34ef740a1 The interrupt handler pipe is now closed on module creation failure
Oleg Oshmyan <chortos@inbox.lv>
parents: 146
diff changeset
1480 if (module == NULL)
9ed34ef740a1 The interrupt handler pipe is now closed on module creation failure
Oleg Oshmyan <chortos@inbox.lv>
parents: 146
diff changeset
1481 {
186
0bd782fd41dc Fixed a possible compilation error introduced in the last changeset
Oleg Oshmyan <chortos@inbox.lv>
parents: 185
diff changeset
1482 #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
1483 close(intpipe[0]);
9ed34ef740a1 The interrupt handler pipe is now closed on module creation failure
Oleg Oshmyan <chortos@inbox.lv>
parents: 146
diff changeset
1484 close(intpipe[1]);
186
0bd782fd41dc Fixed a possible compilation error introduced in the last changeset
Oleg Oshmyan <chortos@inbox.lv>
parents: 185
diff changeset
1485 #endif
185
9ed34ef740a1 The interrupt handler pipe is now closed on module creation failure
Oleg Oshmyan <chortos@inbox.lv>
parents: 146
diff changeset
1486 Py_DECREF(MemoryLimitExceeded);
9ed34ef740a1 The interrupt handler pipe is now closed on module creation failure
Oleg Oshmyan <chortos@inbox.lv>
parents: 146
diff changeset
1487 Py_DECREF(CPUTimeLimitExceeded);
9ed34ef740a1 The interrupt handler pipe is now closed on module creation failure
Oleg Oshmyan <chortos@inbox.lv>
parents: 146
diff changeset
1488 Py_DECREF(WallTimeLimitExceeded);
9ed34ef740a1 The interrupt handler pipe is now closed on module creation failure
Oleg Oshmyan <chortos@inbox.lv>
parents: 146
diff changeset
1489 Py_DECREF(CanceledByUser);
9ed34ef740a1 The interrupt handler pipe is now closed on module creation failure
Oleg Oshmyan <chortos@inbox.lv>
parents: 146
diff changeset
1490 Py_DECREF(CannotStartTestee);
9ed34ef740a1 The interrupt handler pipe is now closed on module creation failure
Oleg Oshmyan <chortos@inbox.lv>
parents: 146
diff changeset
1491 INIT_FAIL;
9ed34ef740a1 The interrupt handler pipe is now closed on module creation failure
Oleg Oshmyan <chortos@inbox.lv>
parents: 146
diff changeset
1492 }
9ed34ef740a1 The interrupt handler pipe is now closed on module creation failure
Oleg Oshmyan <chortos@inbox.lv>
parents: 146
diff changeset
1493
136
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1494 #ifdef HAVE_TERMIOS_H
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1495 if (!tcgetattr(0, &orig_termios))
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1496 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1497 new_termios = orig_termios;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1498 // 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
1499 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
1500 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
1501 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
1502 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
1503 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1504 catch_escape = true;
218
65b5c9390010 With _unix, Escape presses now cancel test data unarchiving
Oleg Oshmyan <chortos@inbox.lv>
parents: 210
diff changeset
1505 #ifdef O_ASYNC
65b5c9390010 With _unix, Escape presses now cancel test data unarchiving
Oleg Oshmyan <chortos@inbox.lv>
parents: 210
diff changeset
1506 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
1507 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
1508 #endif
136
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1509 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1510 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1511 #ifdef USE_WAKEUP_FD
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1512 else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1513 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1514 Py_AtExit(close_intpipe);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1515 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1516 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1517 #elif defined USE_WAKEUP_FD
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1518 Py_AtExit(close_intpipe);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1519 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1520
191
4f69e30abbd5 Made sure the wakeup FD is always valid
Oleg Oshmyan <chortos@inbox.lv>
parents: 190
diff changeset
1521 #ifdef USE_WAKEUP_FD
4f69e30abbd5 Made sure the wakeup FD is always valid
Oleg Oshmyan <chortos@inbox.lv>
parents: 190
diff changeset
1522 PySignal_SetWakeupFd(intpipe[1]);
4f69e30abbd5 Made sure the wakeup FD is always valid
Oleg Oshmyan <chortos@inbox.lv>
parents: 190
diff changeset
1523 #endif
4f69e30abbd5 Made sure the wakeup FD is always valid
Oleg Oshmyan <chortos@inbox.lv>
parents: 190
diff changeset
1524
146
d5b6708c1955 Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents: 137
diff changeset
1525 #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
1526 return module;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1527 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1528 }