annotate upreckon/_unixmodule.cpp @ 146:d5b6708c1955

Distutils support, reorganization and cleaning up * Removed command-line options -t and -u. * Reorganized code: o all modules are now in package upreckon; o TestCaseNotPassed and its descendants now live in a separate module exceptions; o load_problem now lives in module problem. * Commented out mentions of command-line option -c in --help. * Added a distutils-based setup.py.
author Oleg Oshmyan <chortos@inbox.lv>
date Sat, 28 May 2011 14:24:25 +0100
parents _unixmodule.cpp@f4361d557929
children 9ed34ef740a1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
136
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1 // Copyright (c) 2011 Chortos-2 <chortos@inbox.lv>
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
2
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
3 #include <Python.h>
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
4 #include <structmember.h>
146
d5b6708c1955 Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents: 137
diff changeset
5 #include <stdio.h>
136
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
6
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
7 #ifdef HAVE_SYS_TYPES_H
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
8 #include <sys/types.h>
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
9 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
10
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
11 #ifdef HAVE_FCNTL_H
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
12 #include <fcntl.h>
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
13 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
14
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
15 #include <limits.h>
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
16
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
17 #ifdef HAVE_SIGNAL_H
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
18 #include <signal.h>
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
19 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
20
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
21 #ifdef HAVE_SPAWN_H
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
22 #include <spawn.h>
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
23 #ifdef __APPLE__
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
24 #pragma weak_import posix_spawnp
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
25 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
26 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
27
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
28 #ifdef HAVE_SYS_RESOURCE_H
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
29 #include <sys/resource.h>
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
30 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
31
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
32 #ifdef HAVE_SYS_WAIT_H
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
33 #include <sys/wait.h>
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
34 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
35
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
36 #ifdef HAVE_TERMIOS_H
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
37 #include <termios.h>
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
38 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
39
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
40 #if !(defined __cplusplus) && !(defined bool)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
41 #ifdef HAVE_C99_BOOL
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
42 #define bool _Bool
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
43 #else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
44 #define bool char
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
45 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
46 #undef true
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
47 #define true 1
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
48 #undef false
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
49 #define false 0
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
50 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
51
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
52 // 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
53 #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
54 #define USE_WAKEUP_FD
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
55 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
56
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
57 #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
58 #define RLIMIT_AS RLIMIT_VMEM
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
59 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
60
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
61 // 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
62 #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
63 //#ifdef HAVE_FORK1
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
64 #define fork fork1
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
65 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
66
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
67 // 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
68 #ifdef WITH_NEXT_FRAMEWORK
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
69 #include <crt_externs.h>
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
70 static char **environ = NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
71 #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
72 extern char **environ;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
73 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
74
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
75 #ifndef Py_PYTIME_H
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
76 typedef struct timeval _PyTime_timeval;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
77 #ifndef GETTIMEOFDAY_NO_TZ
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
78 #define _PyTime_gettimeofday(tvp) gettimeofday((tvp), NULL)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
79 #else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
80 #define _PyTime_gettimeofday(tvp) gettimeofday((tvp))
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
81 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
82 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
83
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
84 #if PY_MAJOR_VERSION >= 3
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
85 #define PyInt_AsLong PyLong_AsLong
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
86 #define PyInt_FromLong PyLong_FromLong
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
87 #define PyNumber_Int PyNumber_Long
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
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
90 #define TESTEE_SPAWNED 0
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
91 #define TESTEE_SPAWN_FAILED 1
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
92 #define TESTEE_REPORT_STATUS(status) \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
93 do \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
94 { \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
95 const char c = (status); \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
96 write(c2ppipe[1], &c, 1); \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
97 } \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
98 while (0)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
99
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
100 #if !(defined SIGKILL) && defined SIGTERM
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
101 #define SIGKILL SIGTERM
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
102 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
103
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
104 #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
105 #ifdef HAVE_WAITPID
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
106 #define TERM_TESTEE \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
107 do \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
108 { \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
109 kill(-curpid, SIGKILL); \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
110 kill(-curpid, SIGCONT); \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
111 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
112 } \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
113 while (0)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
114 #else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
115 #define TERM_TESTEE \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
116 do \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
117 { \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
118 kill(-curpid, SIGKILL); \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
119 kill(-curpid, SIGCONT); \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
120 while (wait(&retstat) != curpid); \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
121 } \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
122 while (0)
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 #else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
125 #define TERM_TESTEE
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
126 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
127
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
128 #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
129 #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
130 #else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
131 #define PROPAGATE_SIGINT
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
132 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
133
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
134 struct child_stats
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
135 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
136 int returncode;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
137 _PyTime_timeval walltime;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
138 #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
139 _PyTime_timeval cputime;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
140 Py_ssize_t memory;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
141 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
142 };
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
143
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
144 static pid_t curpid;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
145 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
146 static PyObject *CannotStartTestee, *CanceledByUser, *WallTimeLimitExceeded,
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
147 *CPUTimeLimitExceeded, *MemoryLimitExceeded;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
148 static _PyTime_timeval time_end;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
149
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
150 #ifdef USE_WAKEUP_FD
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
151 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
152 static int intpipe[2] = { 0 };
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
153 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
154
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
155 #ifdef HAVE_TERMIOS_H
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
156 static bool catch_escape = false;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
157 static struct termios orig_termios;
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
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
160 typedef struct
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
161 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
162 PyObject_HEAD
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
163 int returncode;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
164 } _unix__PopenPlaceholderObject;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
165
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
166 static PyMemberDef _PopenPlaceholder_members[] =
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
167 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
168 { "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
169 { NULL }
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
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
172 static PyTypeObject _unix__PopenPlaceholderType =
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
173 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
174 #if PY_MAJOR_VERSION >= 3
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
175 PyVarObject_HEAD_INIT(NULL, 0)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
176 #else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
177 PyObject_HEAD_INIT(NULL)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
178 0, /*ob_size*/
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
179 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
180 "_unix._PopenPlaceholder", /*tp_name*/
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
181 sizeof(_unix__PopenPlaceholderObject), /*tp_basicsize*/
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
182 0, /*tp_itemsize*/
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
183 0, /*tp_dealloc*/
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
184 0, /*tp_print*/
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
185 0, /*tp_getattr*/
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
186 0, /*tp_setattr*/
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
187 0, /*tp_compare*/
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
188 0, /*tp_repr*/
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
189 0, /*tp_as_number*/
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
190 0, /*tp_as_sequence*/
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
191 0, /*tp_as_mapping*/
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
192 0, /*tp_hash */
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
193 0, /*tp_call*/
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
194 0, /*tp_str*/
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
195 0, /*tp_getattro*/
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
196 0, /*tp_setattro*/
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
197 0, /*tp_as_buffer*/
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
198 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
199 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
200 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
201 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
202 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
203 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
204 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
205 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
206 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
207 _PopenPlaceholder_members, /*tp_members*/
136
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 #ifndef timeradd
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
211 #define timeradd(a, b, res) \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
212 do \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
213 { \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
214 (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
215 (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
216 if ((res)->tv_usec >= 1000000) \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
217 { \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
218 ++(res)->tv_sec; \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
219 (res)->tv_usec -= 1000000; \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
220 } \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
221 } \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
222 while (0)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
223 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
224
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
225 #ifndef timersub
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
226 #define timersub(a, b, res) \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
227 do \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
228 { \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
229 (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
230 (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
231 if ((res)->tv_usec < 0) \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
232 { \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
233 --(res)->tv_sec; \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
234 (res)->tv_usec += 1000000; \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
235 } \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
236 } \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
237 while (0)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
238 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
239
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
240 #ifndef timerclear
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
241 #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
242 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
243
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
244 #ifndef timerisset
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
245 #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
246 #endif
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 timercmp
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
249 #define timercmp(a, b, cmp) \
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
250 (((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
251 ? ((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
252 : ((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
253 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
254
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
255 // 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
256 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
257 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
258 Py_ssize_t i;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
259 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
260 PyMem_Free(array[i]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
261 PyMem_DEL(array);
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
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
264 // 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
265 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
266 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
267 int fd = PyObject_AsFileDescriptor(obj);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
268 if (fd >= 0)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
269 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
270 *((int *) p) = fd;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
271 return 1;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
272 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
273 return 0;
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
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
276 // 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
277 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
278 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
279 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
280 static PyObject *dummy_args = NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
281 Py_ssize_t i, argc;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
282 char **argv;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
283 bool own_args = false;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
284 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
285
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
286 if (dummy_args == NULL)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
287 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
288 if (!(dummy_args = PyTuple_New(0)))
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
289 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
290 return -1;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
291 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
292 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
293
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
294 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
295 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
296 return -1;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
297 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
298 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
299 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
300 return -1;
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
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
303 #if PY_MAJOR_VERSION >= 3
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
304 if (PyUnicode_Check(args))
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
305 #else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
306 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
307 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
308 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
309 argc = 1;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
310 args = PyTuple_Pack(1, args);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
311 if (args == NULL)
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 return -1;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
314 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
315 own_args = true;
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 else if (!PySequence_Check(args))
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
318 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
319 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
320 return -1;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
321 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
322 else
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 argc = PySequence_Size(args);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
325 if (argc < 1)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
326 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
327 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
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 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
333 if (argv == NULL)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
334 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
335 if (own_args)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
336 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
337 Py_DECREF(args);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
338 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
339 PyErr_NoMemory();
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
340 return -1;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
341 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
342
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
343 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
344 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
345 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
346 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
347 free_string_array(argv, i);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
348 if (own_args)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
349 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
350 Py_DECREF(args);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
351 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
352 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
353 return -1;
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 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
356 argv[argc] = NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
357
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
358 curpid = fork();
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
359 if (!curpid)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
360 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
361 pid_t pid;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
362 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
363 struct child_stats stats;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
364 _PyTime_timeval tvstart, tvend;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
365 #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
366 struct rusage rusage;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
367 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
368 #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
369 struct rlimit rlimit;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
370 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
371
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 Assume no errors occur:
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
374 * 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
375 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
376 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
377 * 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
378 for setpgid can occur.
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 #ifdef HAVE_SETPGID
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
381 setpgid(0, 0);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
382 #else //if defined HAVE_SETPGRP
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
383 #ifdef SETPGRP_HAVE_ARG
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
384 setpgrp(0, 0);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
385 #else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
386 setpgrp();
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
387 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
388 #endif
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 #ifdef SIGINT
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
391 signal(SIGINT, SIG_DFL);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
392 #endif
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 #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
395 _Py_RestoreSignals();
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
396 #else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
397 #ifdef SIGPIPE
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
398 signal(SIGPIPE, SIG_DFL);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
399 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
400 #ifdef SIGXFSZ
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
401 signal(SIGXFSZ, SIG_DFL);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
402 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
403 #ifdef SIGXFZ
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
404 signal(SIGXFZ, SIG_DFL);
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 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
407
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
408 if (c2ppipe[1] < 3)
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 int newfd;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
411 #ifdef F_DUPFD_CLOEXEC
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
412 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
413 if (newfd == -1)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
414 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
415 spawn_errno = errno;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
416 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
417 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
418 _exit(127);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
419 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
420 c2ppipe[1] = newfd;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
421 #else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
422 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
423 // 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
424 if (newfd == -1
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
425 || 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
426 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
427 spawn_errno = errno;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
428 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
429 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
430 _exit(127);
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 c2ppipe[1] = newfd;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
433 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
434 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
435 // 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
436 // 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
437 // FIXME: error handling
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
438 fddupped[0] = dup(fdin);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
439 fddupped[1] = dup(fdout);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
440 fddupped[2] = dup(fderr);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
441 dup2(fddupped[0], 0);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
442 dup2(fddupped[1], 1);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
443 dup2(fddupped[2], 2);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
444 // 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
445 // 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
446 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
447 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
448 close(fd);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
449 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
450 while (--fd >= 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 close(fd);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
453 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
454
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
455 #ifdef RLIMIT_AS
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
456 if (maxmemory)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
457 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
458 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
459 setrlimit(RLIMIT_AS, &rlimit);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
460 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
461 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
462 #ifdef RLIMIT_CPU
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
463 if (maxcputime)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
464 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
465 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
466 setrlimit(RLIMIT_CPU, &rlimit);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
467 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
468 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
469
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
470 #ifdef HAVE_SPAWN_H
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
471 #ifdef __APPLE__
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
472 if (posix_spawnp != NULL)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
473 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
474 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
475 spawn_errno = posix_spawnp(&pid, argv[0], NULL, NULL, argv, environ);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
476 _PyTime_gettimeofday(&tvstart);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
477
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
478 if (spawn_errno)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
479 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
480 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
481 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
482 _exit(127);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
483 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
484 #ifdef __APPLE__
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
485 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
486 else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
487 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
488 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
489 #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
490 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
491 pid = fork();
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
492 if (!pid)
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 execvp(argv[0], argv);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
495 spawn_errno = errno;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
496 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
497 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
498 _exit(127);
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 else if (pid == -1)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
501 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
502 spawn_errno = errno;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
503 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
504 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
505 _exit(127);
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 else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
508 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
509 _PyTime_gettimeofday(&tvstart);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
510 }
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 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
513 TESTEE_REPORT_STATUS(TESTEE_SPAWNED);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
514 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
515
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
516 #ifdef HAVE_WAIT4
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
517 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
518 #elif defined HAVE_WAIT3
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
519 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
520 #elif defined HAVE_WAITPID
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
521 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
522 #else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
523 while (wait(&status) != pid);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
524 #endif
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 _PyTime_gettimeofday(&tvend);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
527 #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
528 getrusage(RUSAGE_CHILDREN, &rusage);
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
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
531 stats = zero_stats;
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 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
534 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
535 else stats.returncode = WEXITSTATUS(status);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
536
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
537 timersub(&tvend, &tvstart, &stats.walltime);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
538 #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
539 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
540 #ifdef __APPLE__
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
541 stats.memory = rusage.ru_maxrss;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
542 #else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
543 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
544 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
545 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
546
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
547 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
548 _exit(0);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
549 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
550 else if (curpid == -1)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
551 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
552 PyErr_SetFromErrno(PyExc_OSError);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
553 free_string_array(argv, argc);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
554 if (own_args)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
555 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
556 Py_DECREF(args);
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 return 0;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
559 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
560
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
561 /*
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
562 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
563 * 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
564 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
565 * 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
566 for setpgid can occur.
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
567 */
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
568 #ifdef HAVE_SETPGID
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
569 setpgid(curpid, 0);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
570 #elif defined SETPGRP_HAVE_ARG
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
571 setpgrp(curpid, 0);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
572 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
573
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
574 free_string_array(argv, argc);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
575 if (own_args)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
576 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
577 Py_DECREF(args);
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 return 1;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
580 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
581
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
582 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
583 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
584 #ifdef HAVE_LONG_LONG
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
585 long long i_whole;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
586 #else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
587 long i_whole;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
588 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
589 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
590 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
591 if (member == NULL)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
592 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
593 return false;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
594 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
595 if (member == Py_None)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
596 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
597 Py_DECREF(member);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
598 timerclear(ptv);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
599 return true;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
600 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
601 whole = PyNumber_Int(member);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
602 if (whole == NULL)
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 Py_DECREF(member);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
605 return false;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
606 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
607 #ifdef HAVE_LONG_LONG
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
608 i_whole = PyLong_AsLongLong(whole);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
609 #else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
610 i_whole = PyInt_AsLong(whole);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
611 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
612 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
613 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
614 Py_DECREF(whole);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
615 Py_DECREF(member);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
616 return false;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
617 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
618 // FIXME: detect time_t overflow
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
619 ptv->tv_sec = i_whole;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
620 frac = PyNumber_Subtract(member, whole);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
621 Py_DECREF(whole);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
622 Py_DECREF(member);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
623 if (frac == NULL)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
624 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
625 return false;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
626 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
627 million = PyInt_FromLong(1000000);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
628 if (million == NULL)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
629 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
630 Py_DECREF(frac);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
631 return false;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
632 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
633 usec = PyNumber_InPlaceMultiply(frac, million);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
634 Py_DECREF(million);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
635 Py_DECREF(frac);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
636 if (usec == NULL)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
637 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
638 return false;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
639 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
640 usec_whole = PyNumber_Int(usec);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
641 Py_DECREF(usec);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
642 if (usec_whole == NULL)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
643 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
644 return false;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
645 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
646 // 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
647 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
648 Py_DECREF(usec_whole);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
649 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
650 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
651
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
652 #ifdef __cplusplus
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
653 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
654 static char is_int(char);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
655 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
656 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
657 static char is_int(short);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
658 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
659 static char is_int(int);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
660 static char is_int(unsigned);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
661 static char is_int(long);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
662 static char is_int(unsigned long);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
663 #ifdef HAVE_LONG_LONG
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
664 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
665 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
666 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
667 static two_chars is_int(...);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
668 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
669
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
670 static inline bool timeval_to_attr(_PyTime_timeval *ptv, PyObject *obj, const char *attr)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
671 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
672 PyObject *value;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
673 #ifdef __cplusplus
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
674 // 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
675 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
676 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
677 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
678 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
679 value = PyInt_FromLong(ptv->tv_sec);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
680 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
681 // 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
682 #ifdef HAVE_LONG_LONG
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
683 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
684 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
685 value = PyLong_FromUnsignedLongLong(ptv->tv_sec);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
686 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
687 #else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
688 // 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
689 // {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
690 // value = PyLong_FromUnsignedLong(ptv->tv_sec);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
691 // }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
692 //#endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
693 else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
694 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
695 value = PyFloat_FromDouble(ptv->tv_sec);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
696 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
697 //
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
698 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
699 //
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
700 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
701 else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
702 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
703 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
704 // TODO: use decimal.Decimal or fractions.Fraction
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
705 value = PyFloat_FromDouble(ptv->tv_sec + ptv->tv_usec * 0.000001);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
706 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
707 if (value == NULL)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
708 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
709 return false;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
710 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
711 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
712 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
713 return false;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
714 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
715 Py_DECREF(value);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
716 return true;
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
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
719 /*
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
720 TODO/FIXME:
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
721 * Replace timeval->timespec and select->pselect if pselect is available
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
722 (preferably only if pselect is not a wrapper around select).
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
723 * 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
724 */
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
725 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
726 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
727 PyObject *testcase = NULL, *obj;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
728 _unix__PopenPlaceholderObject *Popen_placeholder;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
729 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
730 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
731 _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
732 Py_ssize_t maxmemory, r;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
733 size_t stats_read = 0;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
734 fd_set readfds;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
735 char c;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
736 bool have_maxwalltime;
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 if (kwds != NULL)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
739 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
740 testcase = PyDict_GetItemString(kwds, "case");
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 if (testcase == NULL)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
743 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
744 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
745 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
746 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
747 Py_INCREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
748 PyDict_DelItemString(kwds, "case");
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
749
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
750 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
751 || !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
752 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
753 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
754 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
755 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
756
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
757 obj = PyObject_GetAttrString(testcase, "maxmemory");
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
758 if (obj == NULL)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
759 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
760 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
761 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
762 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
763 if (PyObject_IsTrue(obj))
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 PyObject *factor, *bytes;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
766 factor = PyInt_FromLong(1024 * 1024);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
767 if (factor == NULL)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
768 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
769 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
770 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
771 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
772 bytes = PyNumber_Multiply(obj, factor);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
773 Py_DECREF(factor);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
774 if (bytes == NULL)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
775 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
776 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
777 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
778 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
779 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
780 Py_DECREF(bytes);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
781 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
782 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
783 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
784 return NULL;
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 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
787 else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
788 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
789 maxmemory = 0;
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_DECREF(obj);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
792
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
793 #ifdef HAVE_PIPE2
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
794 if (pipe2(c2ppipe, O_CLOEXEC))
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
795 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
796 PyErr_SetFromErrno(PyExc_IOError);
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 #else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
801 if (pipe(c2ppipe))
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
802 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
803 PyErr_SetFromErrno(PyExc_IOError);
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 // 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
808 // (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
809 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
810 || 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
811 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
812 PyErr_SetFromErrno(PyExc_IOError);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
813 close(c2ppipe[0]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
814 close(c2ppipe[1]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
815 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
816 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
817 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
818 #endif
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 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
821 close(c2ppipe[1]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
822 if (!spawn_status)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
823 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
824 PyObject *type, *value, *traceback, *e;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
825 close(c2ppipe[0]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
826 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
827 PyErr_Fetch(&type, &value, &traceback);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
828 PyErr_NormalizeException(&type, &value, &traceback);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
829 Py_XDECREF(traceback);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
830 Py_DECREF(type);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
831 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
832 Py_DECREF(value);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
833 PyErr_SetObject(CannotStartTestee, e);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
834 Py_DECREF(e);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
835 return NULL;
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 else if (spawn_status < 0)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
838 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
839 close(c2ppipe[0]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
840 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
841 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
842 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
843
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
844 // 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
845 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
846 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
847 if (PyErr_CheckSignals() == -1)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
848 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
849 PROPAGATE_SIGINT;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
850 close(c2ppipe[0]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
851 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
852 TERM_TESTEE;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
853 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
854 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
855 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
856 if (r == 1)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
857 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
858 if (c == TESTEE_SPAWNED)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
859 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
860 size_t got = 0;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
861 while (got < sizeof time_start)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
862 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
863 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
864 if (r > 0)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
865 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
866 got += r;
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 else if (!r)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
869 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
870 errno = 0;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
871 PyErr_SetFromErrno(PyExc_IOError);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
872 goto spawn_failed;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
873 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
874 else if (errno == EINTR)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
875 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
876 if (PyErr_CheckSignals() == -1)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
877 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
878 PROPAGATE_SIGINT;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
879 close(c2ppipe[0]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
880 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
881 TERM_TESTEE;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
882 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
883 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
884 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
885 else
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 PyErr_SetFromErrno(PyExc_IOError);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
888 goto spawn_failed;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
889 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
890 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
891 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
892 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
893 close(c2ppipe[0]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
894 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
895 TERM_TESTEE;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
896 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
897 }
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 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
900 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
901 size_t got = 0;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
902 while (got < sizeof spawn_errno)
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 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
905 if (r > 0)
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 got += r;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
908 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
909 else if (!r)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
910 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
911 // 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
912 spawn_errno = 0;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
913 break;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
914 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
915 else if (errno == EINTR)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
916 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
917 if (PyErr_CheckSignals() == -1)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
918 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
919 PROPAGATE_SIGINT;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
920 close(c2ppipe[0]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
921 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
922 TERM_TESTEE;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
923 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
924 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
925 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
926 else
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 PyErr_SetFromErrno(PyExc_IOError);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
929 goto spawn_failed;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
930 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
931 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
932 errno = spawn_errno;
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 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
935 || 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
936 || errno == ENOEXEC || errno == ETXTBSY)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
937 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
938 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
939 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
940 else
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 PyErr_SetFromErrno(PyExc_OSError);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
943 //}
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
944 goto spawn_failed;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
945 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
946 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
947 else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
948 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
949 PyObject *type, *value, *traceback, *e;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
950 if (!r) errno = 0;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
951 PyErr_SetFromErrno(PyExc_IOError);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
952 spawn_failed:
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
953 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
954 close(c2ppipe[0]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
955 TERM_TESTEE;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
956 PyErr_Fetch(&type, &value, &traceback);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
957 PyErr_NormalizeException(&type, &value, &traceback);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
958 Py_XDECREF(traceback);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
959 Py_DECREF(type);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
960 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
961 Py_DECREF(value);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
962 PyErr_SetObject(CannotStartTestee, e);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
963 Py_DECREF(e);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
964 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
965 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
966
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
967 Py_BEGIN_ALLOW_THREADS
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
968 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
969 FD_ZERO(&readfds);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
970 have_maxwalltime = timerisset(&maxwalltime);
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 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
973 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
974 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
975 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
976 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
977 actual timeout value.
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
978 (POSIX:2008)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
979 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
980 */
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
981 for (;;)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
982 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
983 _PyTime_timeval now;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
984 int maxfd = c2ppipe[0];
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
985 #ifdef HAVE_TERMIOS_H
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
986 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
987 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
988 #ifdef USE_WAKEUP_FD
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
989 FD_SET(intpipe[0], &readfds);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
990 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
991 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
992 FD_SET(c2ppipe[0], &readfds);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
993
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
994 if (have_maxwalltime)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
995 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
996 _PyTime_gettimeofday(&now);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
997 if (timercmp(&time_end, &now, <))
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
998 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
999 timerclear(&timeout);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1000 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1001 else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1002 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1003 timersub(&time_end, &now, &timeout);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1004 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1005
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1006 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
1007
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1008 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
1009 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1010 close(c2ppipe[0]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1011 TERM_TESTEE;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1012 Py_BLOCK_THREADS
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1013 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1014 PyErr_SetObject(WallTimeLimitExceeded, NULL);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1015 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1016 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1017 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1018 else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1019 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1020 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
1021 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1022
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1023 if (s < 0 && errno == EINTR)
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 Py_BLOCK_THREADS
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1026 if (PyErr_CheckSignals() == -1)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1027 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1028 PROPAGATE_SIGINT;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1029 close(c2ppipe[0]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1030 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1031 TERM_TESTEE;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1032 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1033 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1034 Py_UNBLOCK_THREADS
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1035 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1036 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
1037 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1038 Py_BLOCK_THREADS
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1039 PyErr_SetFromErrno(PyExc_IOError);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1040 close(c2ppipe[0]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1041 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1042 TERM_TESTEE;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1043 return NULL;
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 #ifdef USE_WAKEUP_FD
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1046 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
1047 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1048 // FIXME: is error handling needed?
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1049 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
1050 Py_BLOCK_THREADS
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1051 if (PyErr_CheckSignals() == -1)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1052 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1053 PROPAGATE_SIGINT;
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 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1056 TERM_TESTEE;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1057 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1058 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1059 Py_UNBLOCK_THREADS
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1060 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1061 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1062 #ifdef HAVE_TERMIOS_H
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1063 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
1064 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1065 // 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
1066 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
1067 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1068 if (c == '\33')
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 close(c2ppipe[0]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1071 TERM_TESTEE;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1072 Py_BLOCK_THREADS
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1073 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1074 PyErr_SetObject(CanceledByUser, NULL);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1075 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1076 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1077 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1078 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
1079 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1080 if (PyErr_CheckSignals() == -1)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1081 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1082 PROPAGATE_SIGINT;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1083 close(c2ppipe[0]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1084 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1085 TERM_TESTEE;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1086 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1087 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1088 }
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 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1091 else if (s > 0)
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 bool blocked_threads = false;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1094 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
1095 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1096 Py_BLOCK_THREADS
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1097 blocked_threads = true;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1098 if (PyErr_CheckSignals() == -1)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1099 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1100 PROPAGATE_SIGINT;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1101 close(c2ppipe[0]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1102 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1103 TERM_TESTEE;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1104 return NULL;
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 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1107 if (r > 0)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1108 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1109 stats_read += r;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1110 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1111 else if (!r)
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 break;
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 else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1116 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1117 close(c2ppipe[0]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1118 TERM_TESTEE;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1119 if (!blocked_threads)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1120 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1121 Py_BLOCK_THREADS
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 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1124 PyErr_SetFromErrno(PyExc_IOError);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1125 return NULL;
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 if (blocked_threads)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1128 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1129 Py_UNBLOCK_THREADS
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1130 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1131 }
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 close(c2ppipe[0]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1134 Py_END_ALLOW_THREADS
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1135
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1136 #ifdef HAVE_WAITPID
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1137 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
1138 #else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1139 while (wait(&retstat) != curpid)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1140 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1141 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1142 if (PyErr_CheckSignals() == -1)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1143 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1144 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1145 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1146 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1147 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1148
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1149 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
1150 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1151 PyObject *type, *value, *traceback, *e;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1152 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1153 errno = 0;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1154 PyErr_SetFromErrno(PyExc_OSError);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1155 PyErr_Fetch(&type, &value, &traceback);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1156 PyErr_NormalizeException(&type, &value, &traceback);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1157 Py_XDECREF(traceback);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1158 Py_DECREF(type);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1159 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
1160 Py_DECREF(value);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1161 PyErr_SetObject(CannotStartTestee, e);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1162 Py_DECREF(e);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1163 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1164 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1165 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
1166 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1167 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1168 if (WIFSTOPPED(retstat))
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1169 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1170 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
1171 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1172 else if (WIFSIGNALED(retstat))
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 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
1175 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1176 else if (WIFEXITED(retstat))
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1177 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1178 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
1179 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1180 else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1181 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1182 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
1183 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1184 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1185 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1186
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1187 if (stats_read != sizeof stats)
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 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
1191 return NULL;
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 (timerisset(&maxwalltime) && timercmp(&stats.walltime, &maxwalltime, >))
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1195 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1196 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1197 PyErr_SetObject(WallTimeLimitExceeded, NULL);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1198 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1199 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1200
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1201 obj = PyInt_FromLong(0);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1202 if (obj == NULL)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1203 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1204 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1205 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1206 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1207 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
1208 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1209 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1210 return NULL;
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(obj);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1213
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1214 #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
1215 if (timerisset(&maxcputime) || !timerisset(&maxwalltime))
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 PyObject *cputls;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1218 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
1219 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1220 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1221 return NULL;
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 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
1224 if (cputls == NULL)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1225 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1226 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1227 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1228 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1229 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
1230 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1231 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1232 return NULL;
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(cputls);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1235 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
1236 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1237 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1238 PyErr_SetObject(CPUTimeLimitExceeded, NULL);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1239 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1240 }
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 else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1243 #endif
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 (!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
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 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1251
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1252 #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
1253 if (maxmemory && stats.memory > maxmemory)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1254 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1255 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1256 PyErr_SetObject(MemoryLimitExceeded, NULL);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1257 return NULL;
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 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1260
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1261 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
1262 if (Popen_placeholder == NULL)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1263 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1264 return NULL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1265 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1266 Popen_placeholder->returncode = stats.returncode;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1267 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
1268 Py_DECREF(Popen_placeholder);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1269 Py_DECREF(testcase);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1270 Py_RETURN_NONE;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1271 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1272
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1273 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
1274 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1275 #ifdef HAVE_TERMIOS_H
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1276 if (catch_escape)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1277 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1278 char c;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1279 while (read(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
1280 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1281 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1282 Py_RETURN_NONE;
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
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1285 static PyMethodDef _unixMethods[] =
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1286 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1287 { "call", (PyCFunction) _unix_call, METH_VARARGS | METH_KEYWORDS, "Call a process." },
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1288 { "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
1289 { NULL }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1290 };
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1291
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1292 #ifdef USE_WAKEUP_FD
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1293 static void close_intpipe(void)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1294 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1295 close(intpipe[0]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1296 close(intpipe[1]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1297 intpipe[0] = intpipe[1] = 0;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1298 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1299 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1300
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1301 #ifdef HAVE_TERMIOS_H
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1302 static void restore_termios(void)
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 tcsetattr(0, TCSAFLUSH, &orig_termios);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1305 #ifdef USE_WAKEUP_FD
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1306 close_intpipe();
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1307 #endif
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 #endif
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 #if PY_MAJOR_VERSION >= 3
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1312 #define INIT_FAIL return NULL
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1313 static PyModuleDef _unixmodule =
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1314 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1315 PyModuleDef_HEAD_INIT,
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1316 "_unix",
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1317 NULL,
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1318 -1,
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1319 _unixMethods
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1320 };
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1321
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1322 PyMODINIT_FUNC PyInit__unix(void)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1323 #else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1324 #define INIT_FAIL return
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1325 PyMODINIT_FUNC init_unix(void)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1326 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1327 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1328 struct termios new_termios;
146
d5b6708c1955 Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents: 137
diff changeset
1329 PyObject *exceptions;
136
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1330
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1331 _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
1332 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
1333 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1334 INIT_FAIL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1335 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1336
146
d5b6708c1955 Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents: 137
diff changeset
1337 exceptions = PyImport_ImportModule("upreckon.exceptions");
d5b6708c1955 Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents: 137
diff changeset
1338 if (exceptions == NULL
d5b6708c1955 Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents: 137
diff changeset
1339 || (CannotStartTestee = PyObject_GetAttrString(exceptions, "CannotStartTestee")) == NULL
d5b6708c1955 Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents: 137
diff changeset
1340 || (CanceledByUser = PyObject_GetAttrString(exceptions, "CanceledByUser")) == NULL
d5b6708c1955 Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents: 137
diff changeset
1341 || (WallTimeLimitExceeded = PyObject_GetAttrString(exceptions, "WallTimeLimitExceeded")) == NULL
d5b6708c1955 Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents: 137
diff changeset
1342 || (CPUTimeLimitExceeded = PyObject_GetAttrString(exceptions, "CPUTimeLimitExceeded")) == NULL
d5b6708c1955 Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents: 137
diff changeset
1343 || (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
1344 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1345 Py_XDECREF(MemoryLimitExceeded);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1346 Py_XDECREF(CPUTimeLimitExceeded);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1347 Py_XDECREF(WallTimeLimitExceeded);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1348 Py_XDECREF(CanceledByUser);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1349 Py_XDECREF(CannotStartTestee);
146
d5b6708c1955 Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents: 137
diff changeset
1350 Py_XDECREF(exceptions);
136
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1351 INIT_FAIL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1352 }
146
d5b6708c1955 Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents: 137
diff changeset
1353 Py_DECREF(exceptions);
136
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1354
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1355 #ifdef WITH_NEXT_FRAMEWORK
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1356 if (environ == NULL)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1357 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1358 environ = *_NSGetEnviron();
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1359 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1360 #endif
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 #ifdef USE_WAKEUP_FD
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1363 if (!intpipe[0] || !intpipe[1])
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1364 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1365 #ifdef HAVE_PIPE2
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1366 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
1367 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1368 PyErr_SetFromErrno(PyExc_IOError);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1369 Py_DECREF(MemoryLimitExceeded);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1370 Py_DECREF(CPUTimeLimitExceeded);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1371 Py_DECREF(WallTimeLimitExceeded);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1372 Py_DECREF(CanceledByUser);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1373 Py_DECREF(CannotStartTestee);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1374 INIT_FAIL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1375 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1376 #else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1377 if (pipe(intpipe))
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1378 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1379 PyErr_SetFromErrno(PyExc_IOError);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1380 Py_DECREF(MemoryLimitExceeded);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1381 Py_DECREF(CPUTimeLimitExceeded);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1382 Py_DECREF(WallTimeLimitExceeded);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1383 Py_DECREF(CanceledByUser);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1384 Py_DECREF(CannotStartTestee);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1385 INIT_FAIL;
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 // 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
1388 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
1389 || 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
1390 || 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
1391 || 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
1392 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1393 PyErr_SetFromErrno(PyExc_IOError);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1394 close(intpipe[0]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1395 close(intpipe[1]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1396 Py_DECREF(MemoryLimitExceeded);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1397 Py_DECREF(CPUTimeLimitExceeded);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1398 Py_DECREF(WallTimeLimitExceeded);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1399 Py_DECREF(CanceledByUser);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1400 Py_DECREF(CannotStartTestee);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1401 INIT_FAIL;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1402 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1403 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1404 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1405
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1406 PySignal_SetWakeupFd(intpipe[1]);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1407 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1408
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1409 #ifdef HAVE_TERMIOS_H
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1410 if (!tcgetattr(0, &orig_termios))
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1411 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1412 new_termios = orig_termios;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1413 // 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
1414 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
1415 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
1416 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
1417 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
1418 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1419 catch_escape = true;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1420 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1421 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1422 #ifdef USE_WAKEUP_FD
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1423 else
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1424 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1425 Py_AtExit(close_intpipe);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1426 }
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1427 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1428 #elif defined USE_WAKEUP_FD
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1429 Py_AtExit(close_intpipe);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1430 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1431
146
d5b6708c1955 Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents: 137
diff changeset
1432 PyObject *module;
136
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1433 #if PY_MAJOR_VERSION >= 3
146
d5b6708c1955 Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents: 137
diff changeset
1434 module = PyModule_Create(&_unixmodule);
d5b6708c1955 Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents: 137
diff changeset
1435 #else
d5b6708c1955 Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents: 137
diff changeset
1436 module = Py_InitModule("_unix", _unixMethods);
d5b6708c1955 Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents: 137
diff changeset
1437 #endif
136
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1438 if (module == NULL)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1439 {
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1440 Py_DECREF(MemoryLimitExceeded);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1441 Py_DECREF(CPUTimeLimitExceeded);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1442 Py_DECREF(WallTimeLimitExceeded);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1443 Py_DECREF(CanceledByUser);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1444 Py_DECREF(CannotStartTestee);
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1445 }
146
d5b6708c1955 Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents: 137
diff changeset
1446 #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
1447 return module;
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1448 #endif
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1449 }