1 |
|
---|
2 | /* POSIX module implementation */
|
---|
3 |
|
---|
4 | /* This file is also used for Windows NT/MS-Win and OS/2. In that case the
|
---|
5 | module actually calls itself 'nt' or 'os2', not 'posix', and a few
|
---|
6 | functions are either unimplemented or implemented differently. The source
|
---|
7 | assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent
|
---|
8 | of the compiler used. Different compilers define their own feature
|
---|
9 | test macro, e.g. '__BORLANDC__' or '_MSC_VER'. For OS/2, the compiler
|
---|
10 | independent macro PYOS_OS2 should be defined. On OS/2 the default
|
---|
11 | compiler is assumed to be IBM's VisualAge C++ (VACPP). PYCC_GCC is used
|
---|
12 | as the compiler specific macro for the EMX port of gcc to OS/2. */
|
---|
13 |
|
---|
14 | /* See also ../Dos/dosmodule.c */
|
---|
15 |
|
---|
16 | #ifdef __APPLE__
|
---|
17 | /*
|
---|
18 | * Step 1 of support for weak-linking a number of symbols existing on
|
---|
19 | * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block
|
---|
20 | * at the end of this file for more information.
|
---|
21 | */
|
---|
22 | # pragma weak lchown
|
---|
23 | # pragma weak statvfs
|
---|
24 | # pragma weak fstatvfs
|
---|
25 |
|
---|
26 | #endif /* __APPLE__ */
|
---|
27 |
|
---|
28 | #define PY_SSIZE_T_CLEAN
|
---|
29 |
|
---|
30 | #include "Python.h"
|
---|
31 | #include "structseq.h"
|
---|
32 |
|
---|
33 | #if defined(__VMS)
|
---|
34 | # include <unixio.h>
|
---|
35 | #endif /* defined(__VMS) */
|
---|
36 |
|
---|
37 | #ifdef __cplusplus
|
---|
38 | extern "C" {
|
---|
39 | #endif
|
---|
40 |
|
---|
41 | PyDoc_STRVAR(posix__doc__,
|
---|
42 | "This module provides access to operating system functionality that is\n\
|
---|
43 | standardized by the C Standard and the POSIX standard (a thinly\n\
|
---|
44 | disguised Unix interface). Refer to the library manual and\n\
|
---|
45 | corresponding Unix manual entries for more information on calls.");
|
---|
46 |
|
---|
47 | #ifndef Py_USING_UNICODE
|
---|
48 | /* This is used in signatures of functions. */
|
---|
49 | #define Py_UNICODE void
|
---|
50 | #endif
|
---|
51 |
|
---|
52 | #if defined(PYOS_OS2)
|
---|
53 | #define INCL_DOS
|
---|
54 | #define INCL_DOSERRORS
|
---|
55 | #define INCL_DOSPROCESS
|
---|
56 | #define INCL_NOPMAPI
|
---|
57 | #include <os2.h>
|
---|
58 | #if defined(PYCC_GCC)
|
---|
59 | #include <ctype.h>
|
---|
60 | #include <io.h>
|
---|
61 | #include <stdio.h>
|
---|
62 | #include <process.h>
|
---|
63 | #endif
|
---|
64 | #include "osdefs.h"
|
---|
65 | #endif
|
---|
66 |
|
---|
67 | #ifdef HAVE_SYS_TYPES_H
|
---|
68 | #include <sys/types.h>
|
---|
69 | #endif /* HAVE_SYS_TYPES_H */
|
---|
70 |
|
---|
71 | #ifdef HAVE_SYS_STAT_H
|
---|
72 | #include <sys/stat.h>
|
---|
73 | #endif /* HAVE_SYS_STAT_H */
|
---|
74 |
|
---|
75 | #ifdef HAVE_SYS_WAIT_H
|
---|
76 | #include <sys/wait.h> /* For WNOHANG */
|
---|
77 | #endif
|
---|
78 |
|
---|
79 | #ifdef HAVE_SIGNAL_H
|
---|
80 | #include <signal.h>
|
---|
81 | #endif
|
---|
82 |
|
---|
83 | #ifdef HAVE_FCNTL_H
|
---|
84 | #include <fcntl.h>
|
---|
85 | #endif /* HAVE_FCNTL_H */
|
---|
86 |
|
---|
87 | #ifdef HAVE_GRP_H
|
---|
88 | #include <grp.h>
|
---|
89 | #endif
|
---|
90 |
|
---|
91 | #ifdef HAVE_SYSEXITS_H
|
---|
92 | #include <sysexits.h>
|
---|
93 | #endif /* HAVE_SYSEXITS_H */
|
---|
94 |
|
---|
95 | #ifdef HAVE_SYS_LOADAVG_H
|
---|
96 | #include <sys/loadavg.h>
|
---|
97 | #endif
|
---|
98 |
|
---|
99 | /* Various compilers have only certain posix functions */
|
---|
100 | /* XXX Gosh I wish these were all moved into pyconfig.h */
|
---|
101 | #if defined(PYCC_VACPP) && defined(PYOS_OS2)
|
---|
102 | #include <process.h>
|
---|
103 | #else
|
---|
104 | #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
|
---|
105 | #define HAVE_GETCWD 1
|
---|
106 | #define HAVE_OPENDIR 1
|
---|
107 | #define HAVE_SYSTEM 1
|
---|
108 | #if defined(__OS2__)
|
---|
109 | #define HAVE_EXECV 1
|
---|
110 | #define HAVE_WAIT 1
|
---|
111 | #endif
|
---|
112 | #include <process.h>
|
---|
113 | #else
|
---|
114 | #ifdef __BORLANDC__ /* Borland compiler */
|
---|
115 | #define HAVE_EXECV 1
|
---|
116 | #define HAVE_GETCWD 1
|
---|
117 | #define HAVE_OPENDIR 1
|
---|
118 | #define HAVE_PIPE 1
|
---|
119 | #define HAVE_POPEN 1
|
---|
120 | #define HAVE_SYSTEM 1
|
---|
121 | #define HAVE_WAIT 1
|
---|
122 | #else
|
---|
123 | #ifdef _MSC_VER /* Microsoft compiler */
|
---|
124 | #define HAVE_GETCWD 1
|
---|
125 | #define HAVE_SPAWNV 1
|
---|
126 | #define HAVE_EXECV 1
|
---|
127 | #define HAVE_PIPE 1
|
---|
128 | #define HAVE_POPEN 1
|
---|
129 | #define HAVE_SYSTEM 1
|
---|
130 | #define HAVE_CWAIT 1
|
---|
131 | #define HAVE_FSYNC 1
|
---|
132 | #define fsync _commit
|
---|
133 | #else
|
---|
134 | #if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS)
|
---|
135 | /* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */
|
---|
136 | #else /* all other compilers */
|
---|
137 | /* Unix functions that the configure script doesn't check for */
|
---|
138 | #define HAVE_EXECV 1
|
---|
139 | #define HAVE_FORK 1
|
---|
140 | #if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
|
---|
141 | #define HAVE_FORK1 1
|
---|
142 | #endif
|
---|
143 | #define HAVE_GETCWD 1
|
---|
144 | #define HAVE_GETEGID 1
|
---|
145 | #define HAVE_GETEUID 1
|
---|
146 | #define HAVE_GETGID 1
|
---|
147 | #define HAVE_GETPPID 1
|
---|
148 | #define HAVE_GETUID 1
|
---|
149 | #define HAVE_KILL 1
|
---|
150 | #define HAVE_OPENDIR 1
|
---|
151 | #define HAVE_PIPE 1
|
---|
152 | #ifndef __rtems__
|
---|
153 | #define HAVE_POPEN 1
|
---|
154 | #endif
|
---|
155 | #define HAVE_SYSTEM 1
|
---|
156 | #define HAVE_WAIT 1
|
---|
157 | #define HAVE_TTYNAME 1
|
---|
158 | #endif /* PYOS_OS2 && PYCC_GCC && __VMS */
|
---|
159 | #endif /* _MSC_VER */
|
---|
160 | #endif /* __BORLANDC__ */
|
---|
161 | #endif /* ! __WATCOMC__ || __QNX__ */
|
---|
162 | #endif /* ! __IBMC__ */
|
---|
163 |
|
---|
164 | #ifndef _MSC_VER
|
---|
165 |
|
---|
166 | #if defined(__sgi)&&_COMPILER_VERSION>=700
|
---|
167 | /* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
|
---|
168 | (default) */
|
---|
169 | extern char *ctermid_r(char *);
|
---|
170 | #endif
|
---|
171 |
|
---|
172 | #ifndef HAVE_UNISTD_H
|
---|
173 | #if defined(PYCC_VACPP)
|
---|
174 | extern int mkdir(char *);
|
---|
175 | #else
|
---|
176 | #if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
|
---|
177 | extern int mkdir(const char *);
|
---|
178 | #else
|
---|
179 | extern int mkdir(const char *, mode_t);
|
---|
180 | #endif
|
---|
181 | #endif
|
---|
182 | #if defined(__IBMC__) || defined(__IBMCPP__)
|
---|
183 | extern int chdir(char *);
|
---|
184 | extern int rmdir(char *);
|
---|
185 | #else
|
---|
186 | extern int chdir(const char *);
|
---|
187 | extern int rmdir(const char *);
|
---|
188 | #endif
|
---|
189 | #ifdef __BORLANDC__
|
---|
190 | extern int chmod(const char *, int);
|
---|
191 | #else
|
---|
192 | extern int chmod(const char *, mode_t);
|
---|
193 | #endif
|
---|
194 | /*#ifdef HAVE_FCHMOD
|
---|
195 | extern int fchmod(int, mode_t);
|
---|
196 | #endif*/
|
---|
197 | /*#ifdef HAVE_LCHMOD
|
---|
198 | extern int lchmod(const char *, mode_t);
|
---|
199 | #endif*/
|
---|
200 | extern int chown(const char *, uid_t, gid_t);
|
---|
201 | extern char *getcwd(char *, int);
|
---|
202 | extern char *strerror(int);
|
---|
203 | extern int link(const char *, const char *);
|
---|
204 | extern int rename(const char *, const char *);
|
---|
205 | extern int stat(const char *, struct stat *);
|
---|
206 | extern int unlink(const char *);
|
---|
207 | extern int pclose(FILE *);
|
---|
208 | #ifdef HAVE_SYMLINK
|
---|
209 | extern int symlink(const char *, const char *);
|
---|
210 | #endif /* HAVE_SYMLINK */
|
---|
211 | #ifdef HAVE_LSTAT
|
---|
212 | extern int lstat(const char *, struct stat *);
|
---|
213 | #endif /* HAVE_LSTAT */
|
---|
214 | #endif /* !HAVE_UNISTD_H */
|
---|
215 |
|
---|
216 | #endif /* !_MSC_VER */
|
---|
217 |
|
---|
218 | #ifdef HAVE_UTIME_H
|
---|
219 | #include <utime.h>
|
---|
220 | #endif /* HAVE_UTIME_H */
|
---|
221 |
|
---|
222 | #ifdef HAVE_SYS_UTIME_H
|
---|
223 | #include <sys/utime.h>
|
---|
224 | #define HAVE_UTIME_H /* pretend we do for the rest of this file */
|
---|
225 | #endif /* HAVE_SYS_UTIME_H */
|
---|
226 |
|
---|
227 | #ifdef HAVE_SYS_TIMES_H
|
---|
228 | #include <sys/times.h>
|
---|
229 | #endif /* HAVE_SYS_TIMES_H */
|
---|
230 |
|
---|
231 | #ifdef HAVE_SYS_PARAM_H
|
---|
232 | #include <sys/param.h>
|
---|
233 | #endif /* HAVE_SYS_PARAM_H */
|
---|
234 |
|
---|
235 | #ifdef HAVE_SYS_UTSNAME_H
|
---|
236 | #include <sys/utsname.h>
|
---|
237 | #endif /* HAVE_SYS_UTSNAME_H */
|
---|
238 |
|
---|
239 | #ifdef HAVE_DIRENT_H
|
---|
240 | #include <dirent.h>
|
---|
241 | #define NAMLEN(dirent) strlen((dirent)->d_name)
|
---|
242 | #else
|
---|
243 | #if defined(__WATCOMC__) && !defined(__QNX__)
|
---|
244 | #include <direct.h>
|
---|
245 | #define NAMLEN(dirent) strlen((dirent)->d_name)
|
---|
246 | #else
|
---|
247 | #define dirent direct
|
---|
248 | #define NAMLEN(dirent) (dirent)->d_namlen
|
---|
249 | #endif
|
---|
250 | #ifdef HAVE_SYS_NDIR_H
|
---|
251 | #include <sys/ndir.h>
|
---|
252 | #endif
|
---|
253 | #ifdef HAVE_SYS_DIR_H
|
---|
254 | #include <sys/dir.h>
|
---|
255 | #endif
|
---|
256 | #ifdef HAVE_NDIR_H
|
---|
257 | #include <ndir.h>
|
---|
258 | #endif
|
---|
259 | #endif
|
---|
260 |
|
---|
261 | #ifdef _MSC_VER
|
---|
262 | #ifdef HAVE_DIRECT_H
|
---|
263 | #include <direct.h>
|
---|
264 | #endif
|
---|
265 | #ifdef HAVE_IO_H
|
---|
266 | #include <io.h>
|
---|
267 | #endif
|
---|
268 | #ifdef HAVE_PROCESS_H
|
---|
269 | #include <process.h>
|
---|
270 | #endif
|
---|
271 | #include "osdefs.h"
|
---|
272 | #include <windows.h>
|
---|
273 | #include <shellapi.h> /* for ShellExecute() */
|
---|
274 | #define popen _popen
|
---|
275 | #define pclose _pclose
|
---|
276 | #endif /* _MSC_VER */
|
---|
277 |
|
---|
278 | #if defined(PYCC_VACPP) && defined(PYOS_OS2)
|
---|
279 | #include <io.h>
|
---|
280 | #endif /* OS2 */
|
---|
281 |
|
---|
282 | #ifndef MAXPATHLEN
|
---|
283 | #if defined(PATH_MAX) && PATH_MAX > 1024
|
---|
284 | #define MAXPATHLEN PATH_MAX
|
---|
285 | #else
|
---|
286 | #define MAXPATHLEN 1024
|
---|
287 | #endif
|
---|
288 | #endif /* MAXPATHLEN */
|
---|
289 |
|
---|
290 | #ifdef UNION_WAIT
|
---|
291 | /* Emulate some macros on systems that have a union instead of macros */
|
---|
292 |
|
---|
293 | #ifndef WIFEXITED
|
---|
294 | #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
|
---|
295 | #endif
|
---|
296 |
|
---|
297 | #ifndef WEXITSTATUS
|
---|
298 | #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
|
---|
299 | #endif
|
---|
300 |
|
---|
301 | #ifndef WTERMSIG
|
---|
302 | #define WTERMSIG(u_wait) ((u_wait).w_termsig)
|
---|
303 | #endif
|
---|
304 |
|
---|
305 | #define WAIT_TYPE union wait
|
---|
306 | #define WAIT_STATUS_INT(s) (s.w_status)
|
---|
307 |
|
---|
308 | #else /* !UNION_WAIT */
|
---|
309 | #define WAIT_TYPE int
|
---|
310 | #define WAIT_STATUS_INT(s) (s)
|
---|
311 | #endif /* UNION_WAIT */
|
---|
312 |
|
---|
313 | /* Issue #1983: pid_t can be longer than a C long on some systems */
|
---|
314 | #if !defined(SIZEOF_PID_T) || SIZEOF_PID_T == SIZEOF_INT
|
---|
315 | #define PARSE_PID "i"
|
---|
316 | #define PyLong_FromPid PyInt_FromLong
|
---|
317 | #define PyLong_AsPid PyInt_AsLong
|
---|
318 | #elif SIZEOF_PID_T == SIZEOF_LONG
|
---|
319 | #define PARSE_PID "l"
|
---|
320 | #define PyLong_FromPid PyInt_FromLong
|
---|
321 | #define PyLong_AsPid PyInt_AsLong
|
---|
322 | #elif defined(SIZEOF_LONG_LONG) && SIZEOF_PID_T == SIZEOF_LONG_LONG
|
---|
323 | #define PARSE_PID "L"
|
---|
324 | #define PyLong_FromPid PyLong_FromLongLong
|
---|
325 | #define PyLong_AsPid PyInt_AsLongLong
|
---|
326 | #else
|
---|
327 | #error "sizeof(pid_t) is neither sizeof(int), sizeof(long) or sizeof(long long)"
|
---|
328 | #endif /* SIZEOF_PID_T */
|
---|
329 |
|
---|
330 | /* Don't use the "_r" form if we don't need it (also, won't have a
|
---|
331 | prototype for it, at least on Solaris -- maybe others as well?). */
|
---|
332 | #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
|
---|
333 | #define USE_CTERMID_R
|
---|
334 | #endif
|
---|
335 |
|
---|
336 | #if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD)
|
---|
337 | #define USE_TMPNAM_R
|
---|
338 | #endif
|
---|
339 |
|
---|
340 | /* choose the appropriate stat and fstat functions and return structs */
|
---|
341 | #undef STAT
|
---|
342 | #if defined(MS_WIN64) || defined(MS_WINDOWS)
|
---|
343 | # define STAT win32_stat
|
---|
344 | # define FSTAT win32_fstat
|
---|
345 | # define STRUCT_STAT struct win32_stat
|
---|
346 | #else
|
---|
347 | # define STAT stat
|
---|
348 | # define FSTAT fstat
|
---|
349 | # define STRUCT_STAT struct stat
|
---|
350 | #endif
|
---|
351 |
|
---|
352 | #if defined(MAJOR_IN_MKDEV)
|
---|
353 | #include <sys/mkdev.h>
|
---|
354 | #else
|
---|
355 | #if defined(MAJOR_IN_SYSMACROS)
|
---|
356 | #include <sys/sysmacros.h>
|
---|
357 | #endif
|
---|
358 | #if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
|
---|
359 | #include <sys/mkdev.h>
|
---|
360 | #endif
|
---|
361 | #endif
|
---|
362 |
|
---|
363 | /* Return a dictionary corresponding to the POSIX environment table */
|
---|
364 | #ifdef WITH_NEXT_FRAMEWORK
|
---|
365 | /* On Darwin/MacOSX a shared library or framework has no access to
|
---|
366 | ** environ directly, we must obtain it with _NSGetEnviron().
|
---|
367 | */
|
---|
368 | #include <crt_externs.h>
|
---|
369 | static char **environ;
|
---|
370 | #elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
|
---|
371 | extern char **environ;
|
---|
372 | #endif /* !_MSC_VER */
|
---|
373 |
|
---|
374 | static PyObject *
|
---|
375 | convertenviron(void)
|
---|
376 | {
|
---|
377 | PyObject *d;
|
---|
378 | char **e;
|
---|
379 | d = PyDict_New();
|
---|
380 | if (d == NULL)
|
---|
381 | return NULL;
|
---|
382 | #ifdef WITH_NEXT_FRAMEWORK
|
---|
383 | if (environ == NULL)
|
---|
384 | environ = *_NSGetEnviron();
|
---|
385 | #endif
|
---|
386 | if (environ == NULL)
|
---|
387 | return d;
|
---|
388 | /* This part ignores errors */
|
---|
389 | for (e = environ; *e != NULL; e++) {
|
---|
390 | PyObject *k;
|
---|
391 | PyObject *v;
|
---|
392 | char *p = strchr(*e, '=');
|
---|
393 | if (p == NULL)
|
---|
394 | continue;
|
---|
395 | k = PyString_FromStringAndSize(*e, (int)(p-*e));
|
---|
396 | if (k == NULL) {
|
---|
397 | PyErr_Clear();
|
---|
398 | continue;
|
---|
399 | }
|
---|
400 | v = PyString_FromString(p+1);
|
---|
401 | if (v == NULL) {
|
---|
402 | PyErr_Clear();
|
---|
403 | Py_DECREF(k);
|
---|
404 | continue;
|
---|
405 | }
|
---|
406 | if (PyDict_GetItem(d, k) == NULL) {
|
---|
407 | if (PyDict_SetItem(d, k, v) != 0)
|
---|
408 | PyErr_Clear();
|
---|
409 | }
|
---|
410 | Py_DECREF(k);
|
---|
411 | Py_DECREF(v);
|
---|
412 | }
|
---|
413 | #if defined(PYOS_OS2)
|
---|
414 | {
|
---|
415 | APIRET rc;
|
---|
416 | char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
|
---|
417 |
|
---|
418 | rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
|
---|
419 | if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
|
---|
420 | PyObject *v = PyString_FromString(buffer);
|
---|
421 | PyDict_SetItemString(d, "BEGINLIBPATH", v);
|
---|
422 | Py_DECREF(v);
|
---|
423 | }
|
---|
424 | rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
|
---|
425 | if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
|
---|
426 | PyObject *v = PyString_FromString(buffer);
|
---|
427 | PyDict_SetItemString(d, "ENDLIBPATH", v);
|
---|
428 | Py_DECREF(v);
|
---|
429 | }
|
---|
430 | }
|
---|
431 | #endif
|
---|
432 | return d;
|
---|
433 | }
|
---|
434 |
|
---|
435 |
|
---|
436 | /* Set a POSIX-specific error from errno, and return NULL */
|
---|
437 |
|
---|
438 | static PyObject *
|
---|
439 | posix_error(void)
|
---|
440 | {
|
---|
441 | return PyErr_SetFromErrno(PyExc_OSError);
|
---|
442 | }
|
---|
443 | static PyObject *
|
---|
444 | posix_error_with_filename(char* name)
|
---|
445 | {
|
---|
446 | return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
|
---|
447 | }
|
---|
448 |
|
---|
449 | #ifdef Py_WIN_WIDE_FILENAMES
|
---|
450 | static PyObject *
|
---|
451 | posix_error_with_unicode_filename(Py_UNICODE* name)
|
---|
452 | {
|
---|
453 | return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name);
|
---|
454 | }
|
---|
455 | #endif /* Py_WIN_WIDE_FILENAMES */
|
---|
456 |
|
---|
457 |
|
---|
458 | static PyObject *
|
---|
459 | posix_error_with_allocated_filename(char* name)
|
---|
460 | {
|
---|
461 | PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
|
---|
462 | PyMem_Free(name);
|
---|
463 | return rc;
|
---|
464 | }
|
---|
465 |
|
---|
466 | #ifdef MS_WINDOWS
|
---|
467 | static PyObject *
|
---|
468 | win32_error(char* function, char* filename)
|
---|
469 | {
|
---|
470 | /* XXX We should pass the function name along in the future.
|
---|
471 | (_winreg.c also wants to pass the function name.)
|
---|
472 | This would however require an additional param to the
|
---|
473 | Windows error object, which is non-trivial.
|
---|
474 | */
|
---|
475 | errno = GetLastError();
|
---|
476 | if (filename)
|
---|
477 | return PyErr_SetFromWindowsErrWithFilename(errno, filename);
|
---|
478 | else
|
---|
479 | return PyErr_SetFromWindowsErr(errno);
|
---|
480 | }
|
---|
481 |
|
---|
482 | #ifdef Py_WIN_WIDE_FILENAMES
|
---|
483 | static PyObject *
|
---|
484 | win32_error_unicode(char* function, Py_UNICODE* filename)
|
---|
485 | {
|
---|
486 | /* XXX - see win32_error for comments on 'function' */
|
---|
487 | errno = GetLastError();
|
---|
488 | if (filename)
|
---|
489 | return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
|
---|
490 | else
|
---|
491 | return PyErr_SetFromWindowsErr(errno);
|
---|
492 | }
|
---|
493 |
|
---|
494 | static PyObject *_PyUnicode_FromFileSystemEncodedObject(register PyObject *obj)
|
---|
495 | {
|
---|
496 | }
|
---|
497 |
|
---|
498 | static int
|
---|
499 | convert_to_unicode(PyObject **param)
|
---|
500 | {
|
---|
501 | if (PyUnicode_CheckExact(*param))
|
---|
502 | Py_INCREF(*param);
|
---|
503 | else if (PyUnicode_Check(*param))
|
---|
504 | /* For a Unicode subtype that's not a Unicode object,
|
---|
505 | return a true Unicode object with the same data. */
|
---|
506 | *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param),
|
---|
507 | PyUnicode_GET_SIZE(*param));
|
---|
508 | else
|
---|
509 | *param = PyUnicode_FromEncodedObject(*param,
|
---|
510 | Py_FileSystemDefaultEncoding,
|
---|
511 | "strict");
|
---|
512 | return (*param) != NULL;
|
---|
513 | }
|
---|
514 |
|
---|
515 | #endif /* Py_WIN_WIDE_FILENAMES */
|
---|
516 |
|
---|
517 | #endif
|
---|
518 |
|
---|
519 | #if defined(PYOS_OS2)
|
---|
520 | /**********************************************************************
|
---|
521 | * Helper Function to Trim and Format OS/2 Messages
|
---|
522 | **********************************************************************/
|
---|
523 | static void
|
---|
524 | os2_formatmsg(char *msgbuf, int msglen, char *reason)
|
---|
525 | {
|
---|
526 | msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
|
---|
527 |
|
---|
528 | if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
|
---|
529 | char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
|
---|
530 |
|
---|
531 | while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
|
---|
532 | *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
|
---|
533 | }
|
---|
534 |
|
---|
535 | /* Add Optional Reason Text */
|
---|
536 | if (reason) {
|
---|
537 | strcat(msgbuf, " : ");
|
---|
538 | strcat(msgbuf, reason);
|
---|
539 | }
|
---|
540 | }
|
---|
541 |
|
---|
542 | /**********************************************************************
|
---|
543 | * Decode an OS/2 Operating System Error Code
|
---|
544 | *
|
---|
545 | * A convenience function to lookup an OS/2 error code and return a
|
---|
546 | * text message we can use to raise a Python exception.
|
---|
547 | *
|
---|
548 | * Notes:
|
---|
549 | * The messages for errors returned from the OS/2 kernel reside in
|
---|
550 | * the file OSO001.MSG in the \OS2 directory hierarchy.
|
---|
551 | *
|
---|
552 | **********************************************************************/
|
---|
553 | static char *
|
---|
554 | os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
|
---|
555 | {
|
---|
556 | APIRET rc;
|
---|
557 | ULONG msglen;
|
---|
558 |
|
---|
559 | /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
|
---|
560 | Py_BEGIN_ALLOW_THREADS
|
---|
561 | rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
|
---|
562 | errorcode, "oso001.msg", &msglen);
|
---|
563 | Py_END_ALLOW_THREADS
|
---|
564 |
|
---|
565 | if (rc == NO_ERROR)
|
---|
566 | os2_formatmsg(msgbuf, msglen, reason);
|
---|
567 | else
|
---|
568 | PyOS_snprintf(msgbuf, msgbuflen,
|
---|
569 | "unknown OS error #%d", errorcode);
|
---|
570 |
|
---|
571 | return msgbuf;
|
---|
572 | }
|
---|
573 |
|
---|
574 | /* Set an OS/2-specific error and return NULL. OS/2 kernel
|
---|
575 | errors are not in a global variable e.g. 'errno' nor are
|
---|
576 | they congruent with posix error numbers. */
|
---|
577 |
|
---|
578 | static PyObject * os2_error(int code)
|
---|
579 | {
|
---|
580 | char text[1024];
|
---|
581 | PyObject *v;
|
---|
582 |
|
---|
583 | os2_strerror(text, sizeof(text), code, "");
|
---|
584 |
|
---|
585 | v = Py_BuildValue("(is)", code, text);
|
---|
586 | if (v != NULL) {
|
---|
587 | PyErr_SetObject(PyExc_OSError, v);
|
---|
588 | Py_DECREF(v);
|
---|
589 | }
|
---|
590 | return NULL; /* Signal to Python that an Exception is Pending */
|
---|
591 | }
|
---|
592 |
|
---|
593 | #endif /* OS2 */
|
---|
594 |
|
---|
595 | /* POSIX generic methods */
|
---|
596 |
|
---|
597 | static PyObject *
|
---|
598 | posix_fildes(PyObject *fdobj, int (*func)(int))
|
---|
599 | {
|
---|
600 | int fd;
|
---|
601 | int res;
|
---|
602 | fd = PyObject_AsFileDescriptor(fdobj);
|
---|
603 | if (fd < 0)
|
---|
604 | return NULL;
|
---|
605 | Py_BEGIN_ALLOW_THREADS
|
---|
606 | res = (*func)(fd);
|
---|
607 | Py_END_ALLOW_THREADS
|
---|
608 | if (res < 0)
|
---|
609 | return posix_error();
|
---|
610 | Py_INCREF(Py_None);
|
---|
611 | return Py_None;
|
---|
612 | }
|
---|
613 |
|
---|
614 | #ifdef Py_WIN_WIDE_FILENAMES
|
---|
615 | static int
|
---|
616 | unicode_file_names(void)
|
---|
617 | {
|
---|
618 | static int canusewide = -1;
|
---|
619 | if (canusewide == -1) {
|
---|
620 | /* As per doc for ::GetVersion(), this is the correct test for
|
---|
621 | the Windows NT family. */
|
---|
622 | canusewide = (GetVersion() < 0x80000000) ? 1 : 0;
|
---|
623 | }
|
---|
624 | return canusewide;
|
---|
625 | }
|
---|
626 | #endif
|
---|
627 |
|
---|
628 | static PyObject *
|
---|
629 | posix_1str(PyObject *args, char *format, int (*func)(const char*))
|
---|
630 | {
|
---|
631 | char *path1 = NULL;
|
---|
632 | int res;
|
---|
633 | if (!PyArg_ParseTuple(args, format,
|
---|
634 | Py_FileSystemDefaultEncoding, &path1))
|
---|
635 | return NULL;
|
---|
636 | Py_BEGIN_ALLOW_THREADS
|
---|
637 | res = (*func)(path1);
|
---|
638 | Py_END_ALLOW_THREADS
|
---|
639 | if (res < 0)
|
---|
640 | return posix_error_with_allocated_filename(path1);
|
---|
641 | PyMem_Free(path1);
|
---|
642 | Py_INCREF(Py_None);
|
---|
643 | return Py_None;
|
---|
644 | }
|
---|
645 |
|
---|
646 | static PyObject *
|
---|
647 | posix_2str(PyObject *args,
|
---|
648 | char *format,
|
---|
649 | int (*func)(const char *, const char *))
|
---|
650 | {
|
---|
651 | char *path1 = NULL, *path2 = NULL;
|
---|
652 | int res;
|
---|
653 | if (!PyArg_ParseTuple(args, format,
|
---|
654 | Py_FileSystemDefaultEncoding, &path1,
|
---|
655 | Py_FileSystemDefaultEncoding, &path2))
|
---|
656 | return NULL;
|
---|
657 | Py_BEGIN_ALLOW_THREADS
|
---|
658 | res = (*func)(path1, path2);
|
---|
659 | Py_END_ALLOW_THREADS
|
---|
660 | PyMem_Free(path1);
|
---|
661 | PyMem_Free(path2);
|
---|
662 | if (res != 0)
|
---|
663 | /* XXX how to report both path1 and path2??? */
|
---|
664 | return posix_error();
|
---|
665 | Py_INCREF(Py_None);
|
---|
666 | return Py_None;
|
---|
667 | }
|
---|
668 |
|
---|
669 | #ifdef Py_WIN_WIDE_FILENAMES
|
---|
670 | static PyObject*
|
---|
671 | win32_1str(PyObject* args, char* func,
|
---|
672 | char* format, BOOL (__stdcall *funcA)(LPCSTR),
|
---|
673 | char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
|
---|
674 | {
|
---|
675 | PyObject *uni;
|
---|
676 | char *ansi;
|
---|
677 | BOOL result;
|
---|
678 | if (unicode_file_names()) {
|
---|
679 | if (!PyArg_ParseTuple(args, wformat, &uni))
|
---|
680 | PyErr_Clear();
|
---|
681 | else {
|
---|
682 | Py_BEGIN_ALLOW_THREADS
|
---|
683 | result = funcW(PyUnicode_AsUnicode(uni));
|
---|
684 | Py_END_ALLOW_THREADS
|
---|
685 | if (!result)
|
---|
686 | return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
|
---|
687 | Py_INCREF(Py_None);
|
---|
688 | return Py_None;
|
---|
689 | }
|
---|
690 | }
|
---|
691 | if (!PyArg_ParseTuple(args, format, &ansi))
|
---|
692 | return NULL;
|
---|
693 | Py_BEGIN_ALLOW_THREADS
|
---|
694 | result = funcA(ansi);
|
---|
695 | Py_END_ALLOW_THREADS
|
---|
696 | if (!result)
|
---|
697 | return win32_error(func, ansi);
|
---|
698 | Py_INCREF(Py_None);
|
---|
699 | return Py_None;
|
---|
700 |
|
---|
701 | }
|
---|
702 |
|
---|
703 | /* This is a reimplementation of the C library's chdir function,
|
---|
704 | but one that produces Win32 errors instead of DOS error codes.
|
---|
705 | chdir is essentially a wrapper around SetCurrentDirectory; however,
|
---|
706 | it also needs to set "magic" environment variables indicating
|
---|
707 | the per-drive current directory, which are of the form =<drive>: */
|
---|
708 | BOOL __stdcall
|
---|
709 | win32_chdir(LPCSTR path)
|
---|
710 | {
|
---|
711 | char new_path[MAX_PATH+1];
|
---|
712 | int result;
|
---|
713 | char env[4] = "=x:";
|
---|
714 |
|
---|
715 | if(!SetCurrentDirectoryA(path))
|
---|
716 | return FALSE;
|
---|
717 | result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
|
---|
718 | if (!result)
|
---|
719 | return FALSE;
|
---|
720 | /* In the ANSI API, there should not be any paths longer
|
---|
721 | than MAX_PATH. */
|
---|
722 | assert(result <= MAX_PATH+1);
|
---|
723 | if (strncmp(new_path, "\\\\", 2) == 0 ||
|
---|
724 | strncmp(new_path, "//", 2) == 0)
|
---|
725 | /* UNC path, nothing to do. */
|
---|
726 | return TRUE;
|
---|
727 | env[1] = new_path[0];
|
---|
728 | return SetEnvironmentVariableA(env, new_path);
|
---|
729 | }
|
---|
730 |
|
---|
731 | /* The Unicode version differs from the ANSI version
|
---|
732 | since the current directory might exceed MAX_PATH characters */
|
---|
733 | BOOL __stdcall
|
---|
734 | win32_wchdir(LPCWSTR path)
|
---|
735 | {
|
---|
736 | wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
|
---|
737 | int result;
|
---|
738 | wchar_t env[4] = L"=x:";
|
---|
739 |
|
---|
740 | if(!SetCurrentDirectoryW(path))
|
---|
741 | return FALSE;
|
---|
742 | result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
|
---|
743 | if (!result)
|
---|
744 | return FALSE;
|
---|
745 | if (result > MAX_PATH+1) {
|
---|
746 | new_path = malloc(result * sizeof(wchar_t));
|
---|
747 | if (!new_path) {
|
---|
748 | SetLastError(ERROR_OUTOFMEMORY);
|
---|
749 | return FALSE;
|
---|
750 | }
|
---|
751 | result = GetCurrentDirectoryW(result, new_path);
|
---|
752 | if (!result) {
|
---|
753 | free(new_path);
|
---|
754 | return FALSE;
|
---|
755 | }
|
---|
756 | }
|
---|
757 | if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
|
---|
758 | wcsncmp(new_path, L"//", 2) == 0)
|
---|
759 | /* UNC path, nothing to do. */
|
---|
760 | return TRUE;
|
---|
761 | env[1] = new_path[0];
|
---|
762 | result = SetEnvironmentVariableW(env, new_path);
|
---|
763 | if (new_path != _new_path)
|
---|
764 | free(new_path);
|
---|
765 | return result;
|
---|
766 | }
|
---|
767 | #endif
|
---|
768 |
|
---|
769 | #ifdef MS_WINDOWS
|
---|
770 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation:
|
---|
771 | - time stamps are restricted to second resolution
|
---|
772 | - file modification times suffer from forth-and-back conversions between
|
---|
773 | UTC and local time
|
---|
774 | Therefore, we implement our own stat, based on the Win32 API directly.
|
---|
775 | */
|
---|
776 | #define HAVE_STAT_NSEC 1
|
---|
777 |
|
---|
778 | struct win32_stat{
|
---|
779 | int st_dev;
|
---|
780 | __int64 st_ino;
|
---|
781 | unsigned short st_mode;
|
---|
782 | int st_nlink;
|
---|
783 | int st_uid;
|
---|
784 | int st_gid;
|
---|
785 | int st_rdev;
|
---|
786 | __int64 st_size;
|
---|
787 | int st_atime;
|
---|
788 | int st_atime_nsec;
|
---|
789 | int st_mtime;
|
---|
790 | int st_mtime_nsec;
|
---|
791 | int st_ctime;
|
---|
792 | int st_ctime_nsec;
|
---|
793 | };
|
---|
794 |
|
---|
795 | static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
|
---|
796 |
|
---|
797 | static void
|
---|
798 | FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out)
|
---|
799 | {
|
---|
800 | /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
|
---|
801 | /* Cannot simply cast and dereference in_ptr,
|
---|
802 | since it might not be aligned properly */
|
---|
803 | __int64 in;
|
---|
804 | memcpy(&in, in_ptr, sizeof(in));
|
---|
805 | *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
|
---|
806 | /* XXX Win32 supports time stamps past 2038; we currently don't */
|
---|
807 | *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int);
|
---|
808 | }
|
---|
809 |
|
---|
810 | static void
|
---|
811 | time_t_to_FILE_TIME(int time_in, int nsec_in, FILETIME *out_ptr)
|
---|
812 | {
|
---|
813 | /* XXX endianness */
|
---|
814 | __int64 out;
|
---|
815 | out = time_in + secs_between_epochs;
|
---|
816 | out = out * 10000000 + nsec_in / 100;
|
---|
817 | memcpy(out_ptr, &out, sizeof(out));
|
---|
818 | }
|
---|
819 |
|
---|
820 | /* Below, we *know* that ugo+r is 0444 */
|
---|
821 | #if _S_IREAD != 0400
|
---|
822 | #error Unsupported C library
|
---|
823 | #endif
|
---|
824 | static int
|
---|
825 | attributes_to_mode(DWORD attr)
|
---|
826 | {
|
---|
827 | int m = 0;
|
---|
828 | if (attr & FILE_ATTRIBUTE_DIRECTORY)
|
---|
829 | m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
|
---|
830 | else
|
---|
831 | m |= _S_IFREG;
|
---|
832 | if (attr & FILE_ATTRIBUTE_READONLY)
|
---|
833 | m |= 0444;
|
---|
834 | else
|
---|
835 | m |= 0666;
|
---|
836 | return m;
|
---|
837 | }
|
---|
838 |
|
---|
839 | static int
|
---|
840 | attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result)
|
---|
841 | {
|
---|
842 | memset(result, 0, sizeof(*result));
|
---|
843 | result->st_mode = attributes_to_mode(info->dwFileAttributes);
|
---|
844 | result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
|
---|
845 | FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
|
---|
846 | FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
|
---|
847 | FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
|
---|
848 |
|
---|
849 | return 0;
|
---|
850 | }
|
---|
851 |
|
---|
852 | /* Emulate GetFileAttributesEx[AW] on Windows 95 */
|
---|
853 | static int checked = 0;
|
---|
854 | static BOOL (CALLBACK *gfaxa)(LPCSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
|
---|
855 | static BOOL (CALLBACK *gfaxw)(LPCWSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
|
---|
856 | static void
|
---|
857 | check_gfax()
|
---|
858 | {
|
---|
859 | HINSTANCE hKernel32;
|
---|
860 | if (checked)
|
---|
861 | return;
|
---|
862 | checked = 1;
|
---|
863 | hKernel32 = GetModuleHandle("KERNEL32");
|
---|
864 | *(FARPROC*)&gfaxa = GetProcAddress(hKernel32, "GetFileAttributesExA");
|
---|
865 | *(FARPROC*)&gfaxw = GetProcAddress(hKernel32, "GetFileAttributesExW");
|
---|
866 | }
|
---|
867 |
|
---|
868 | static BOOL
|
---|
869 | attributes_from_dir(LPCSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
|
---|
870 | {
|
---|
871 | HANDLE hFindFile;
|
---|
872 | WIN32_FIND_DATAA FileData;
|
---|
873 | hFindFile = FindFirstFileA(pszFile, &FileData);
|
---|
874 | if (hFindFile == INVALID_HANDLE_VALUE)
|
---|
875 | return FALSE;
|
---|
876 | FindClose(hFindFile);
|
---|
877 | pfad->dwFileAttributes = FileData.dwFileAttributes;
|
---|
878 | pfad->ftCreationTime = FileData.ftCreationTime;
|
---|
879 | pfad->ftLastAccessTime = FileData.ftLastAccessTime;
|
---|
880 | pfad->ftLastWriteTime = FileData.ftLastWriteTime;
|
---|
881 | pfad->nFileSizeHigh = FileData.nFileSizeHigh;
|
---|
882 | pfad->nFileSizeLow = FileData.nFileSizeLow;
|
---|
883 | return TRUE;
|
---|
884 | }
|
---|
885 |
|
---|
886 | static BOOL
|
---|
887 | attributes_from_dir_w(LPCWSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
|
---|
888 | {
|
---|
889 | HANDLE hFindFile;
|
---|
890 | WIN32_FIND_DATAW FileData;
|
---|
891 | hFindFile = FindFirstFileW(pszFile, &FileData);
|
---|
892 | if (hFindFile == INVALID_HANDLE_VALUE)
|
---|
893 | return FALSE;
|
---|
894 | FindClose(hFindFile);
|
---|
895 | pfad->dwFileAttributes = FileData.dwFileAttributes;
|
---|
896 | pfad->ftCreationTime = FileData.ftCreationTime;
|
---|
897 | pfad->ftLastAccessTime = FileData.ftLastAccessTime;
|
---|
898 | pfad->ftLastWriteTime = FileData.ftLastWriteTime;
|
---|
899 | pfad->nFileSizeHigh = FileData.nFileSizeHigh;
|
---|
900 | pfad->nFileSizeLow = FileData.nFileSizeLow;
|
---|
901 | return TRUE;
|
---|
902 | }
|
---|
903 |
|
---|
904 | static BOOL WINAPI
|
---|
905 | Py_GetFileAttributesExA(LPCSTR pszFile,
|
---|
906 | GET_FILEEX_INFO_LEVELS level,
|
---|
907 | LPVOID pv)
|
---|
908 | {
|
---|
909 | BOOL result;
|
---|
910 | LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv;
|
---|
911 | /* First try to use the system's implementation, if that is
|
---|
912 | available and either succeeds to gives an error other than
|
---|
913 | that it isn't implemented. */
|
---|
914 | check_gfax();
|
---|
915 | if (gfaxa) {
|
---|
916 | result = gfaxa(pszFile, level, pv);
|
---|
917 | if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
|
---|
918 | return result;
|
---|
919 | }
|
---|
920 | /* It's either not present, or not implemented.
|
---|
921 | Emulate using FindFirstFile. */
|
---|
922 | if (level != GetFileExInfoStandard) {
|
---|
923 | SetLastError(ERROR_INVALID_PARAMETER);
|
---|
924 | return FALSE;
|
---|
925 | }
|
---|
926 | /* Use GetFileAttributes to validate that the file name
|
---|
927 | does not contain wildcards (which FindFirstFile would
|
---|
928 | accept). */
|
---|
929 | if (GetFileAttributesA(pszFile) == 0xFFFFFFFF)
|
---|
930 | return FALSE;
|
---|
931 | return attributes_from_dir(pszFile, pfad);
|
---|
932 | }
|
---|
933 |
|
---|
934 | static BOOL WINAPI
|
---|
935 | Py_GetFileAttributesExW(LPCWSTR pszFile,
|
---|
936 | GET_FILEEX_INFO_LEVELS level,
|
---|
937 | LPVOID pv)
|
---|
938 | {
|
---|
939 | BOOL result;
|
---|
940 | LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv;
|
---|
941 | /* First try to use the system's implementation, if that is
|
---|
942 | available and either succeeds to gives an error other than
|
---|
943 | that it isn't implemented. */
|
---|
944 | check_gfax();
|
---|
945 | if (gfaxa) {
|
---|
946 | result = gfaxw(pszFile, level, pv);
|
---|
947 | if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
|
---|
948 | return result;
|
---|
949 | }
|
---|
950 | /* It's either not present, or not implemented.
|
---|
951 | Emulate using FindFirstFile. */
|
---|
952 | if (level != GetFileExInfoStandard) {
|
---|
953 | SetLastError(ERROR_INVALID_PARAMETER);
|
---|
954 | return FALSE;
|
---|
955 | }
|
---|
956 | /* Use GetFileAttributes to validate that the file name
|
---|
957 | does not contain wildcards (which FindFirstFile would
|
---|
958 | accept). */
|
---|
959 | if (GetFileAttributesW(pszFile) == 0xFFFFFFFF)
|
---|
960 | return FALSE;
|
---|
961 | return attributes_from_dir_w(pszFile, pfad);
|
---|
962 | }
|
---|
963 |
|
---|
964 | static int
|
---|
965 | win32_stat(const char* path, struct win32_stat *result)
|
---|
966 | {
|
---|
967 | WIN32_FILE_ATTRIBUTE_DATA info;
|
---|
968 | int code;
|
---|
969 | char *dot;
|
---|
970 | /* XXX not supported on Win95 and NT 3.x */
|
---|
971 | if (!Py_GetFileAttributesExA(path, GetFileExInfoStandard, &info)) {
|
---|
972 | if (GetLastError() != ERROR_SHARING_VIOLATION) {
|
---|
973 | /* Protocol violation: we explicitly clear errno, instead of
|
---|
974 | setting it to a POSIX error. Callers should use GetLastError. */
|
---|
975 | errno = 0;
|
---|
976 | return -1;
|
---|
977 | } else {
|
---|
978 | /* Could not get attributes on open file. Fall back to
|
---|
979 | reading the directory. */
|
---|
980 | if (!attributes_from_dir(path, &info)) {
|
---|
981 | /* Very strange. This should not fail now */
|
---|
982 | errno = 0;
|
---|
983 | return -1;
|
---|
984 | }
|
---|
985 | }
|
---|
986 | }
|
---|
987 | code = attribute_data_to_stat(&info, result);
|
---|
988 | if (code != 0)
|
---|
989 | return code;
|
---|
990 | /* Set S_IFEXEC if it is an .exe, .bat, ... */
|
---|
991 | dot = strrchr(path, '.');
|
---|
992 | if (dot) {
|
---|
993 | if (stricmp(dot, ".bat") == 0 ||
|
---|
994 | stricmp(dot, ".cmd") == 0 ||
|
---|
995 | stricmp(dot, ".exe") == 0 ||
|
---|
996 | stricmp(dot, ".com") == 0)
|
---|
997 | result->st_mode |= 0111;
|
---|
998 | }
|
---|
999 | return code;
|
---|
1000 | }
|
---|
1001 |
|
---|
1002 | static int
|
---|
1003 | win32_wstat(const wchar_t* path, struct win32_stat *result)
|
---|
1004 | {
|
---|
1005 | int code;
|
---|
1006 | const wchar_t *dot;
|
---|
1007 | WIN32_FILE_ATTRIBUTE_DATA info;
|
---|
1008 | /* XXX not supported on Win95 and NT 3.x */
|
---|
1009 | if (!Py_GetFileAttributesExW(path, GetFileExInfoStandard, &info)) {
|
---|
1010 | if (GetLastError() != ERROR_SHARING_VIOLATION) {
|
---|
1011 | /* Protocol violation: we explicitly clear errno, instead of
|
---|
1012 | setting it to a POSIX error. Callers should use GetLastError. */
|
---|
1013 | errno = 0;
|
---|
1014 | return -1;
|
---|
1015 | } else {
|
---|
1016 | /* Could not get attributes on open file. Fall back to
|
---|
1017 | reading the directory. */
|
---|
1018 | if (!attributes_from_dir_w(path, &info)) {
|
---|
1019 | /* Very strange. This should not fail now */
|
---|
1020 | errno = 0;
|
---|
1021 | return -1;
|
---|
1022 | }
|
---|
1023 | }
|
---|
1024 | }
|
---|
1025 | code = attribute_data_to_stat(&info, result);
|
---|
1026 | if (code < 0)
|
---|
1027 | return code;
|
---|
1028 | /* Set IFEXEC if it is an .exe, .bat, ... */
|
---|
1029 | dot = wcsrchr(path, '.');
|
---|
1030 | if (dot) {
|
---|
1031 | if (_wcsicmp(dot, L".bat") == 0 ||
|
---|
1032 | _wcsicmp(dot, L".cmd") == 0 ||
|
---|
1033 | _wcsicmp(dot, L".exe") == 0 ||
|
---|
1034 | _wcsicmp(dot, L".com") == 0)
|
---|
1035 | result->st_mode |= 0111;
|
---|
1036 | }
|
---|
1037 | return code;
|
---|
1038 | }
|
---|
1039 |
|
---|
1040 | static int
|
---|
1041 | win32_fstat(int file_number, struct win32_stat *result)
|
---|
1042 | {
|
---|
1043 | BY_HANDLE_FILE_INFORMATION info;
|
---|
1044 | HANDLE h;
|
---|
1045 | int type;
|
---|
1046 |
|
---|
1047 | h = (HANDLE)_get_osfhandle(file_number);
|
---|
1048 |
|
---|
1049 | /* Protocol violation: we explicitly clear errno, instead of
|
---|
1050 | setting it to a POSIX error. Callers should use GetLastError. */
|
---|
1051 | errno = 0;
|
---|
1052 |
|
---|
1053 | if (h == INVALID_HANDLE_VALUE) {
|
---|
1054 | /* This is really a C library error (invalid file handle).
|
---|
1055 | We set the Win32 error to the closes one matching. */
|
---|
1056 | SetLastError(ERROR_INVALID_HANDLE);
|
---|
1057 | return -1;
|
---|
1058 | }
|
---|
1059 | memset(result, 0, sizeof(*result));
|
---|
1060 |
|
---|
1061 | type = GetFileType(h);
|
---|
1062 | if (type == FILE_TYPE_UNKNOWN) {
|
---|
1063 | DWORD error = GetLastError();
|
---|
1064 | if (error != 0) {
|
---|
1065 | return -1;
|
---|
1066 | }
|
---|
1067 | /* else: valid but unknown file */
|
---|
1068 | }
|
---|
1069 |
|
---|
1070 | if (type != FILE_TYPE_DISK) {
|
---|
1071 | if (type == FILE_TYPE_CHAR)
|
---|
1072 | result->st_mode = _S_IFCHR;
|
---|
1073 | else if (type == FILE_TYPE_PIPE)
|
---|
1074 | result->st_mode = _S_IFIFO;
|
---|
1075 | return 0;
|
---|
1076 | }
|
---|
1077 |
|
---|
1078 | if (!GetFileInformationByHandle(h, &info)) {
|
---|
1079 | return -1;
|
---|
1080 | }
|
---|
1081 |
|
---|
1082 | /* similar to stat() */
|
---|
1083 | result->st_mode = attributes_to_mode(info.dwFileAttributes);
|
---|
1084 | result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow;
|
---|
1085 | FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
|
---|
1086 | FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
|
---|
1087 | FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
|
---|
1088 | /* specific to fstat() */
|
---|
1089 | result->st_nlink = info.nNumberOfLinks;
|
---|
1090 | result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
|
---|
1091 | return 0;
|
---|
1092 | }
|
---|
1093 |
|
---|
1094 | #endif /* MS_WINDOWS */
|
---|
1095 |
|
---|
1096 | PyDoc_STRVAR(stat_result__doc__,
|
---|
1097 | "stat_result: Result from stat or lstat.\n\n\
|
---|
1098 | This object may be accessed either as a tuple of\n\
|
---|
1099 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
|
---|
1100 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
|
---|
1101 | \n\
|
---|
1102 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
|
---|
1103 | or st_flags, they are available as attributes only.\n\
|
---|
1104 | \n\
|
---|
1105 | See os.stat for more information.");
|
---|
1106 |
|
---|
1107 | static PyStructSequence_Field stat_result_fields[] = {
|
---|
1108 | {"st_mode", "protection bits"},
|
---|
1109 | {"st_ino", "inode"},
|
---|
1110 | {"st_dev", "device"},
|
---|
1111 | {"st_nlink", "number of hard links"},
|
---|
1112 | {"st_uid", "user ID of owner"},
|
---|
1113 | {"st_gid", "group ID of owner"},
|
---|
1114 | {"st_size", "total size, in bytes"},
|
---|
1115 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */
|
---|
1116 | {NULL, "integer time of last access"},
|
---|
1117 | {NULL, "integer time of last modification"},
|
---|
1118 | {NULL, "integer time of last change"},
|
---|
1119 | {"st_atime", "time of last access"},
|
---|
1120 | {"st_mtime", "time of last modification"},
|
---|
1121 | {"st_ctime", "time of last change"},
|
---|
1122 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
|
---|
1123 | {"st_blksize", "blocksize for filesystem I/O"},
|
---|
1124 | #endif
|
---|
1125 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
|
---|
1126 | {"st_blocks", "number of blocks allocated"},
|
---|
1127 | #endif
|
---|
1128 | #ifdef HAVE_STRUCT_STAT_ST_RDEV
|
---|
1129 | {"st_rdev", "device type (if inode device)"},
|
---|
1130 | #endif
|
---|
1131 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS
|
---|
1132 | {"st_flags", "user defined flags for file"},
|
---|
1133 | #endif
|
---|
1134 | #ifdef HAVE_STRUCT_STAT_ST_GEN
|
---|
1135 | {"st_gen", "generation number"},
|
---|
1136 | #endif
|
---|
1137 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
|
---|
1138 | {"st_birthtime", "time of creation"},
|
---|
1139 | #endif
|
---|
1140 | {0}
|
---|
1141 | };
|
---|
1142 |
|
---|
1143 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
|
---|
1144 | #define ST_BLKSIZE_IDX 13
|
---|
1145 | #else
|
---|
1146 | #define ST_BLKSIZE_IDX 12
|
---|
1147 | #endif
|
---|
1148 |
|
---|
1149 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
|
---|
1150 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
|
---|
1151 | #else
|
---|
1152 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX
|
---|
1153 | #endif
|
---|
1154 |
|
---|
1155 | #ifdef HAVE_STRUCT_STAT_ST_RDEV
|
---|
1156 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
|
---|
1157 | #else
|
---|
1158 | #define ST_RDEV_IDX ST_BLOCKS_IDX
|
---|
1159 | #endif
|
---|
1160 |
|
---|
1161 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS
|
---|
1162 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1)
|
---|
1163 | #else
|
---|
1164 | #define ST_FLAGS_IDX ST_RDEV_IDX
|
---|
1165 | #endif
|
---|
1166 |
|
---|
1167 | #ifdef HAVE_STRUCT_STAT_ST_GEN
|
---|
1168 | #define ST_GEN_IDX (ST_FLAGS_IDX+1)
|
---|
1169 | #else
|
---|
1170 | #define ST_GEN_IDX ST_FLAGS_IDX
|
---|
1171 | #endif
|
---|
1172 |
|
---|
1173 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
|
---|
1174 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
|
---|
1175 | #else
|
---|
1176 | #define ST_BIRTHTIME_IDX ST_GEN_IDX
|
---|
1177 | #endif
|
---|
1178 |
|
---|
1179 | static PyStructSequence_Desc stat_result_desc = {
|
---|
1180 | "stat_result", /* name */
|
---|
1181 | stat_result__doc__, /* doc */
|
---|
1182 | stat_result_fields,
|
---|
1183 | 10
|
---|
1184 | };
|
---|
1185 |
|
---|
1186 | PyDoc_STRVAR(statvfs_result__doc__,
|
---|
1187 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\
|
---|
1188 | This object may be accessed either as a tuple of\n\
|
---|
1189 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
|
---|
1190 | or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
|
---|
1191 | \n\
|
---|
1192 | See os.statvfs for more information.");
|
---|
1193 |
|
---|
1194 | static PyStructSequence_Field statvfs_result_fields[] = {
|
---|
1195 | {"f_bsize", },
|
---|
1196 | {"f_frsize", },
|
---|
1197 | {"f_blocks", },
|
---|
1198 | {"f_bfree", },
|
---|
1199 | {"f_bavail", },
|
---|
1200 | {"f_files", },
|
---|
1201 | {"f_ffree", },
|
---|
1202 | {"f_favail", },
|
---|
1203 | {"f_flag", },
|
---|
1204 | {"f_namemax",},
|
---|
1205 | {0}
|
---|
1206 | };
|
---|
1207 |
|
---|
1208 | static PyStructSequence_Desc statvfs_result_desc = {
|
---|
1209 | "statvfs_result", /* name */
|
---|
1210 | statvfs_result__doc__, /* doc */
|
---|
1211 | statvfs_result_fields,
|
---|
1212 | 10
|
---|
1213 | };
|
---|
1214 |
|
---|
1215 | static int initialized;
|
---|
1216 | static PyTypeObject StatResultType;
|
---|
1217 | static PyTypeObject StatVFSResultType;
|
---|
1218 | static newfunc structseq_new;
|
---|
1219 |
|
---|
1220 | static PyObject *
|
---|
1221 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
---|
1222 | {
|
---|
1223 | PyStructSequence *result;
|
---|
1224 | int i;
|
---|
1225 |
|
---|
1226 | result = (PyStructSequence*)structseq_new(type, args, kwds);
|
---|
1227 | if (!result)
|
---|
1228 | return NULL;
|
---|
1229 | /* If we have been initialized from a tuple,
|
---|
1230 | st_?time might be set to None. Initialize it
|
---|
1231 | from the int slots. */
|
---|
1232 | for (i = 7; i <= 9; i++) {
|
---|
1233 | if (result->ob_item[i+3] == Py_None) {
|
---|
1234 | Py_DECREF(Py_None);
|
---|
1235 | Py_INCREF(result->ob_item[i]);
|
---|
1236 | result->ob_item[i+3] = result->ob_item[i];
|
---|
1237 | }
|
---|
1238 | }
|
---|
1239 | return (PyObject*)result;
|
---|
1240 | }
|
---|
1241 |
|
---|
1242 |
|
---|
1243 |
|
---|
1244 | /* If true, st_?time is float. */
|
---|
1245 | static int _stat_float_times = 1;
|
---|
1246 |
|
---|
1247 | PyDoc_STRVAR(stat_float_times__doc__,
|
---|
1248 | "stat_float_times([newval]) -> oldval\n\n\
|
---|
1249 | Determine whether os.[lf]stat represents time stamps as float objects.\n\
|
---|
1250 | If newval is True, future calls to stat() return floats, if it is False,\n\
|
---|
1251 | future calls return ints. \n\
|
---|
1252 | If newval is omitted, return the current setting.\n");
|
---|
1253 |
|
---|
1254 | static PyObject*
|
---|
1255 | stat_float_times(PyObject* self, PyObject *args)
|
---|
1256 | {
|
---|
1257 | int newval = -1;
|
---|
1258 | if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
|
---|
1259 | return NULL;
|
---|
1260 | if (newval == -1)
|
---|
1261 | /* Return old value */
|
---|
1262 | return PyBool_FromLong(_stat_float_times);
|
---|
1263 | _stat_float_times = newval;
|
---|
1264 | Py_INCREF(Py_None);
|
---|
1265 | return Py_None;
|
---|
1266 | }
|
---|
1267 |
|
---|
1268 | static void
|
---|
1269 | fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
|
---|
1270 | {
|
---|
1271 | PyObject *fval,*ival;
|
---|
1272 | #if SIZEOF_TIME_T > SIZEOF_LONG
|
---|
1273 | ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
|
---|
1274 | #else
|
---|
1275 | ival = PyInt_FromLong((long)sec);
|
---|
1276 | #endif
|
---|
1277 | if (!ival)
|
---|
1278 | return;
|
---|
1279 | if (_stat_float_times) {
|
---|
1280 | fval = PyFloat_FromDouble(sec + 1e-9*nsec);
|
---|
1281 | } else {
|
---|
1282 | fval = ival;
|
---|
1283 | Py_INCREF(fval);
|
---|
1284 | }
|
---|
1285 | PyStructSequence_SET_ITEM(v, index, ival);
|
---|
1286 | PyStructSequence_SET_ITEM(v, index+3, fval);
|
---|
1287 | }
|
---|
1288 |
|
---|
1289 | /* pack a system stat C structure into the Python stat tuple
|
---|
1290 | (used by posix_stat() and posix_fstat()) */
|
---|
1291 | static PyObject*
|
---|
1292 | _pystat_fromstructstat(STRUCT_STAT *st)
|
---|
1293 | {
|
---|
1294 | unsigned long ansec, mnsec, cnsec;
|
---|
1295 | PyObject *v = PyStructSequence_New(&StatResultType);
|
---|
1296 | if (v == NULL)
|
---|
1297 | return NULL;
|
---|
1298 |
|
---|
1299 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st->st_mode));
|
---|
1300 | #ifdef HAVE_LARGEFILE_SUPPORT
|
---|
1301 | PyStructSequence_SET_ITEM(v, 1,
|
---|
1302 | PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
|
---|
1303 | #else
|
---|
1304 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st->st_ino));
|
---|
1305 | #endif
|
---|
1306 | #if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
|
---|
1307 | PyStructSequence_SET_ITEM(v, 2,
|
---|
1308 | PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
|
---|
1309 | #else
|
---|
1310 | PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st->st_dev));
|
---|
1311 | #endif
|
---|
1312 | PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st->st_nlink));
|
---|
1313 | PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st->st_uid));
|
---|
1314 | PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st->st_gid));
|
---|
1315 | #ifdef HAVE_LARGEFILE_SUPPORT
|
---|
1316 | PyStructSequence_SET_ITEM(v, 6,
|
---|
1317 | PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
|
---|
1318 | #else
|
---|
1319 | PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong(st->st_size));
|
---|
1320 | #endif
|
---|
1321 |
|
---|
1322 | #if defined(HAVE_STAT_TV_NSEC)
|
---|
1323 | ansec = st->st_atim.tv_nsec;
|
---|
1324 | mnsec = st->st_mtim.tv_nsec;
|
---|
1325 | cnsec = st->st_ctim.tv_nsec;
|
---|
1326 | #elif defined(HAVE_STAT_TV_NSEC2)
|
---|
1327 | ansec = st->st_atimespec.tv_nsec;
|
---|
1328 | mnsec = st->st_mtimespec.tv_nsec;
|
---|
1329 | cnsec = st->st_ctimespec.tv_nsec;
|
---|
1330 | #elif defined(HAVE_STAT_NSEC)
|
---|
1331 | ansec = st->st_atime_nsec;
|
---|
1332 | mnsec = st->st_mtime_nsec;
|
---|
1333 | cnsec = st->st_ctime_nsec;
|
---|
1334 | #else
|
---|
1335 | ansec = mnsec = cnsec = 0;
|
---|
1336 | #endif
|
---|
1337 | fill_time(v, 7, st->st_atime, ansec);
|
---|
1338 | fill_time(v, 8, st->st_mtime, mnsec);
|
---|
1339 | fill_time(v, 9, st->st_ctime, cnsec);
|
---|
1340 |
|
---|
1341 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
|
---|
1342 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
|
---|
1343 | PyInt_FromLong((long)st->st_blksize));
|
---|
1344 | #endif
|
---|
1345 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
|
---|
1346 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
|
---|
1347 | PyInt_FromLong((long)st->st_blocks));
|
---|
1348 | #endif
|
---|
1349 | #ifdef HAVE_STRUCT_STAT_ST_RDEV
|
---|
1350 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
|
---|
1351 | PyInt_FromLong((long)st->st_rdev));
|
---|
1352 | #endif
|
---|
1353 | #ifdef HAVE_STRUCT_STAT_ST_GEN
|
---|
1354 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
|
---|
1355 | PyInt_FromLong((long)st->st_gen));
|
---|
1356 | #endif
|
---|
1357 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
|
---|
1358 | {
|
---|
1359 | PyObject *val;
|
---|
1360 | unsigned long bsec,bnsec;
|
---|
1361 | bsec = (long)st->st_birthtime;
|
---|
1362 | #ifdef HAVE_STAT_TV_NSEC2
|
---|
1363 | bnsec = st->st_birthtimespec.tv_nsec;
|
---|
1364 | #else
|
---|
1365 | bnsec = 0;
|
---|
1366 | #endif
|
---|
1367 | if (_stat_float_times) {
|
---|
1368 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
|
---|
1369 | } else {
|
---|
1370 | val = PyInt_FromLong((long)bsec);
|
---|
1371 | }
|
---|
1372 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
|
---|
1373 | val);
|
---|
1374 | }
|
---|
1375 | #endif
|
---|
1376 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS
|
---|
1377 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
|
---|
1378 | PyInt_FromLong((long)st->st_flags));
|
---|
1379 | #endif
|
---|
1380 |
|
---|
1381 | if (PyErr_Occurred()) {
|
---|
1382 | Py_DECREF(v);
|
---|
1383 | return NULL;
|
---|
1384 | }
|
---|
1385 |
|
---|
1386 | return v;
|
---|
1387 | }
|
---|
1388 |
|
---|
1389 | #ifdef MS_WINDOWS
|
---|
1390 |
|
---|
1391 | /* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\,
|
---|
1392 | where / can be used in place of \ and the trailing slash is optional.
|
---|
1393 | Both SERVER and SHARE must have at least one character.
|
---|
1394 | */
|
---|
1395 |
|
---|
1396 | #define ISSLASHA(c) ((c) == '\\' || (c) == '/')
|
---|
1397 | #define ISSLASHW(c) ((c) == L'\\' || (c) == L'/')
|
---|
1398 | #ifndef ARRAYSIZE
|
---|
1399 | #define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))
|
---|
1400 | #endif
|
---|
1401 |
|
---|
1402 | static BOOL
|
---|
1403 | IsUNCRootA(char *path, int pathlen)
|
---|
1404 | {
|
---|
1405 | #define ISSLASH ISSLASHA
|
---|
1406 |
|
---|
1407 | int i, share;
|
---|
1408 |
|
---|
1409 | if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
|
---|
1410 | /* minimum UNCRoot is \\x\y */
|
---|
1411 | return FALSE;
|
---|
1412 | for (i = 2; i < pathlen ; i++)
|
---|
1413 | if (ISSLASH(path[i])) break;
|
---|
1414 | if (i == 2 || i == pathlen)
|
---|
1415 | /* do not allow \\\SHARE or \\SERVER */
|
---|
1416 | return FALSE;
|
---|
1417 | share = i+1;
|
---|
1418 | for (i = share; i < pathlen; i++)
|
---|
1419 | if (ISSLASH(path[i])) break;
|
---|
1420 | return (i != share && (i == pathlen || i == pathlen-1));
|
---|
1421 |
|
---|
1422 | #undef ISSLASH
|
---|
1423 | }
|
---|
1424 |
|
---|
1425 | #ifdef Py_WIN_WIDE_FILENAMES
|
---|
1426 | static BOOL
|
---|
1427 | IsUNCRootW(Py_UNICODE *path, int pathlen)
|
---|
1428 | {
|
---|
1429 | #define ISSLASH ISSLASHW
|
---|
1430 |
|
---|
1431 | int i, share;
|
---|
1432 |
|
---|
1433 | if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
|
---|
1434 | /* minimum UNCRoot is \\x\y */
|
---|
1435 | return FALSE;
|
---|
1436 | for (i = 2; i < pathlen ; i++)
|
---|
1437 | if (ISSLASH(path[i])) break;
|
---|
1438 | if (i == 2 || i == pathlen)
|
---|
1439 | /* do not allow \\\SHARE or \\SERVER */
|
---|
1440 | return FALSE;
|
---|
1441 | share = i+1;
|
---|
1442 | for (i = share; i < pathlen; i++)
|
---|
1443 | if (ISSLASH(path[i])) break;
|
---|
1444 | return (i != share && (i == pathlen || i == pathlen-1));
|
---|
1445 |
|
---|
1446 | #undef ISSLASH
|
---|
1447 | }
|
---|
1448 | #endif /* Py_WIN_WIDE_FILENAMES */
|
---|
1449 | #endif /* MS_WINDOWS */
|
---|
1450 |
|
---|
1451 | static PyObject *
|
---|
1452 | posix_do_stat(PyObject *self, PyObject *args,
|
---|
1453 | char *format,
|
---|
1454 | #ifdef __VMS
|
---|
1455 | int (*statfunc)(const char *, STRUCT_STAT *, ...),
|
---|
1456 | #else
|
---|
1457 | int (*statfunc)(const char *, STRUCT_STAT *),
|
---|
1458 | #endif
|
---|
1459 | char *wformat,
|
---|
1460 | int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
|
---|
1461 | {
|
---|
1462 | STRUCT_STAT st;
|
---|
1463 | char *path = NULL; /* pass this to stat; do not free() it */
|
---|
1464 | char *pathfree = NULL; /* this memory must be free'd */
|
---|
1465 | int res;
|
---|
1466 | PyObject *result;
|
---|
1467 |
|
---|
1468 | #ifdef Py_WIN_WIDE_FILENAMES
|
---|
1469 | /* If on wide-character-capable OS see if argument
|
---|
1470 | is Unicode and if so use wide API. */
|
---|
1471 | if (unicode_file_names()) {
|
---|
1472 | PyUnicodeObject *po;
|
---|
1473 | if (PyArg_ParseTuple(args, wformat, &po)) {
|
---|
1474 | Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
|
---|
1475 |
|
---|
1476 | Py_BEGIN_ALLOW_THREADS
|
---|
1477 | /* PyUnicode_AS_UNICODE result OK without
|
---|
1478 | thread lock as it is a simple dereference. */
|
---|
1479 | res = wstatfunc(wpath, &st);
|
---|
1480 | Py_END_ALLOW_THREADS
|
---|
1481 |
|
---|
1482 | if (res != 0)
|
---|
1483 | return win32_error_unicode("stat", wpath);
|
---|
1484 | return _pystat_fromstructstat(&st);
|
---|
1485 | }
|
---|
1486 | /* Drop the argument parsing error as narrow strings
|
---|
1487 | are also valid. */
|
---|
1488 | PyErr_Clear();
|
---|
1489 | }
|
---|
1490 | #endif
|
---|
1491 |
|
---|
1492 | if (!PyArg_ParseTuple(args, format,
|
---|
1493 | Py_FileSystemDefaultEncoding, &path))
|
---|
1494 | return NULL;
|
---|
1495 | pathfree = path;
|
---|
1496 |
|
---|
1497 | Py_BEGIN_ALLOW_THREADS
|
---|
1498 | res = (*statfunc)(path, &st);
|
---|
1499 | Py_END_ALLOW_THREADS
|
---|
1500 |
|
---|
1501 | if (res != 0) {
|
---|
1502 | #ifdef MS_WINDOWS
|
---|
1503 | result = win32_error("stat", pathfree);
|
---|
1504 | #else
|
---|
1505 | result = posix_error_with_filename(pathfree);
|
---|
1506 | #endif
|
---|
1507 | }
|
---|
1508 | else
|
---|
1509 | result = _pystat_fromstructstat(&st);
|
---|
1510 |
|
---|
1511 | PyMem_Free(pathfree);
|
---|
1512 | return result;
|
---|
1513 | }
|
---|
1514 |
|
---|
1515 | /* POSIX methods */
|
---|
1516 |
|
---|
1517 | PyDoc_STRVAR(posix_access__doc__,
|
---|
1518 | "access(path, mode) -> True if granted, False otherwise\n\n\
|
---|
1519 | Use the real uid/gid to test for access to a path. Note that most\n\
|
---|
1520 | operations will use the effective uid/gid, therefore this routine can\n\
|
---|
1521 | be used in a suid/sgid environment to test if the invoking user has the\n\
|
---|
1522 | specified access to the path. The mode argument can be F_OK to test\n\
|
---|
1523 | existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
|
---|
1524 |
|
---|
1525 | static PyObject *
|
---|
1526 | posix_access(PyObject *self, PyObject *args)
|
---|
1527 | {
|
---|
1528 | char *path;
|
---|
1529 | int mode;
|
---|
1530 |
|
---|
1531 | #ifdef Py_WIN_WIDE_FILENAMES
|
---|
1532 | DWORD attr;
|
---|
1533 | if (unicode_file_names()) {
|
---|
1534 | PyUnicodeObject *po;
|
---|
1535 | if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
|
---|
1536 | Py_BEGIN_ALLOW_THREADS
|
---|
1537 | /* PyUnicode_AS_UNICODE OK without thread lock as
|
---|
1538 | it is a simple dereference. */
|
---|
1539 | attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
|
---|
1540 | Py_END_ALLOW_THREADS
|
---|
1541 | goto finish;
|
---|
1542 | }
|
---|
1543 | /* Drop the argument parsing error as narrow strings
|
---|
1544 | are also valid. */
|
---|
1545 | PyErr_Clear();
|
---|
1546 | }
|
---|
1547 | if (!PyArg_ParseTuple(args, "eti:access",
|
---|
1548 | Py_FileSystemDefaultEncoding, &path, &mode))
|
---|
1549 | return 0;
|
---|
1550 | Py_BEGIN_ALLOW_THREADS
|
---|
1551 | attr = GetFileAttributesA(path);
|
---|
1552 | Py_END_ALLOW_THREADS
|
---|
1553 | PyMem_Free(path);
|
---|
1554 | finish:
|
---|
1555 | if (attr == 0xFFFFFFFF)
|
---|
1556 | /* File does not exist, or cannot read attributes */
|
---|
1557 | return PyBool_FromLong(0);
|
---|
1558 | /* Access is possible if either write access wasn't requested, or
|
---|
1559 | the file isn't read-only, or if it's a directory, as there are
|
---|
1560 | no read-only directories on Windows. */
|
---|
1561 | return PyBool_FromLong(!(mode & 2)
|
---|
1562 | || !(attr & FILE_ATTRIBUTE_READONLY)
|
---|
1563 | || (attr & FILE_ATTRIBUTE_DIRECTORY));
|
---|
1564 | #else
|
---|
1565 | int res;
|
---|
1566 | if (!PyArg_ParseTuple(args, "eti:access",
|
---|
1567 | Py_FileSystemDefaultEncoding, &path, &mode))
|
---|
1568 | return NULL;
|
---|
1569 | Py_BEGIN_ALLOW_THREADS
|
---|
1570 | res = access(path, mode);
|
---|
1571 | Py_END_ALLOW_THREADS
|
---|
1572 | PyMem_Free(path);
|
---|
1573 | return PyBool_FromLong(res == 0);
|
---|
1574 | #endif
|
---|
1575 | }
|
---|
1576 |
|
---|
1577 | #ifndef F_OK
|
---|
1578 | #define F_OK 0
|
---|
1579 | #endif
|
---|
1580 | #ifndef R_OK
|
---|
1581 | #define R_OK 4
|
---|
1582 | #endif
|
---|
1583 | #ifndef W_OK
|
---|
1584 | #define W_OK 2
|
---|
1585 | #endif
|
---|
1586 | #ifndef X_OK
|
---|
1587 | #define X_OK 1
|
---|
1588 | #endif
|
---|
1589 |
|
---|
1590 | #ifdef HAVE_TTYNAME
|
---|
1591 | PyDoc_STRVAR(posix_ttyname__doc__,
|
---|
1592 | "ttyname(fd) -> string\n\n\
|
---|
1593 | Return the name of the terminal device connected to 'fd'.");
|
---|
1594 |
|
---|
1595 | static PyObject *
|
---|
1596 | posix_ttyname(PyObject *self, PyObject *args)
|
---|
1597 | {
|
---|
1598 | int id;
|
---|
1599 | char *ret;
|
---|
1600 |
|
---|
1601 | if (!PyArg_ParseTuple(args, "i:ttyname", &id))
|
---|
1602 | return NULL;
|
---|
1603 |
|
---|
1604 | #if defined(__VMS)
|
---|
1605 | /* file descriptor 0 only, the default input device (stdin) */
|
---|
1606 | if (id == 0) {
|
---|
1607 | ret = ttyname();
|
---|
1608 | }
|
---|
1609 | else {
|
---|
1610 | ret = NULL;
|
---|
1611 | }
|
---|
1612 | #else
|
---|
1613 | ret = ttyname(id);
|
---|
1614 | #endif
|
---|
1615 | if (ret == NULL)
|
---|
1616 | return posix_error();
|
---|
1617 | return PyString_FromString(ret);
|
---|
1618 | }
|
---|
1619 | #endif
|
---|
1620 |
|
---|
1621 | #ifdef HAVE_CTERMID
|
---|
1622 | PyDoc_STRVAR(posix_ctermid__doc__,
|
---|
1623 | "ctermid() -> string\n\n\
|
---|
1624 | Return the name of the controlling terminal for this process.");
|
---|
1625 |
|
---|
1626 | static PyObject *
|
---|
1627 | posix_ctermid(PyObject *self, PyObject *noargs)
|
---|
1628 | {
|
---|
1629 | char *ret;
|
---|
1630 | char buffer[L_ctermid];
|
---|
1631 |
|
---|
1632 | #ifdef USE_CTERMID_R
|
---|
1633 | ret = ctermid_r(buffer);
|
---|
1634 | #else
|
---|
1635 | ret = ctermid(buffer);
|
---|
1636 | #endif
|
---|
1637 | if (ret == NULL)
|
---|
1638 | return posix_error();
|
---|
1639 | return PyString_FromString(buffer);
|
---|
1640 | }
|
---|
1641 | #endif
|
---|
1642 |
|
---|
1643 | PyDoc_STRVAR(posix_chdir__doc__,
|
---|
1644 | "chdir(path)\n\n\
|
---|
1645 | Change the current working directory to the specified path.");
|
---|
1646 |
|
---|
1647 | static PyObject *
|
---|
1648 | posix_chdir(PyObject *self, PyObject *args)
|
---|
1649 | {
|
---|
1650 | #ifdef MS_WINDOWS
|
---|
1651 | return win32_1str(args, "chdir", "s:chdir", win32_chdir, "U:chdir", win32_wchdir);
|
---|
1652 | #elif defined(PYOS_OS2) && defined(PYCC_GCC)
|
---|
1653 | return posix_1str(args, "et:chdir", _chdir2);
|
---|
1654 | #elif defined(__VMS)
|
---|
1655 | return posix_1str(args, "et:chdir", (int (*)(const char *))chdir);
|
---|
1656 | #else
|
---|
1657 | return posix_1str(args, "et:chdir", chdir);
|
---|
1658 | #endif
|
---|
1659 | }
|
---|
1660 |
|
---|
1661 | #ifdef HAVE_FCHDIR
|
---|
1662 | PyDoc_STRVAR(posix_fchdir__doc__,
|
---|
1663 | "fchdir(fildes)\n\n\
|
---|
1664 | Change to the directory of the given file descriptor. fildes must be\n\
|
---|
1665 | opened on a directory, not a file.");
|
---|
1666 |
|
---|
1667 | static PyObject *
|
---|
1668 | posix_fchdir(PyObject *self, PyObject *fdobj)
|
---|
1669 | {
|
---|
1670 | return posix_fildes(fdobj, fchdir);
|
---|
1671 | }
|
---|
1672 | #endif /* HAVE_FCHDIR */
|
---|
1673 |
|
---|
1674 |
|
---|
1675 | PyDoc_STRVAR(posix_chmod__doc__,
|
---|
1676 | "chmod(path, mode)\n\n\
|
---|
1677 | Change the access permissions of a file.");
|
---|
1678 |
|
---|
1679 | static PyObject *
|
---|
1680 | posix_chmod(PyObject *self, PyObject *args)
|
---|
1681 | {
|
---|
1682 | char *path = NULL;
|
---|
1683 | int i;
|
---|
1684 | int res;
|
---|
1685 | #ifdef Py_WIN_WIDE_FILENAMES
|
---|
1686 | DWORD attr;
|
---|
1687 | if (unicode_file_names()) {
|
---|
1688 | PyUnicodeObject *po;
|
---|
1689 | if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
|
---|
1690 | Py_BEGIN_ALLOW_THREADS
|
---|
1691 | attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
|
---|
1692 | if (attr != 0xFFFFFFFF) {
|
---|
1693 | if (i & _S_IWRITE)
|
---|
1694 | attr &= ~FILE_ATTRIBUTE_READONLY;
|
---|
1695 | else
|
---|
1696 | attr |= FILE_ATTRIBUTE_READONLY;
|
---|
1697 | res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
|
---|
1698 | }
|
---|
1699 | else
|
---|
1700 | res = 0;
|
---|
1701 | Py_END_ALLOW_THREADS
|
---|
1702 | if (!res)
|
---|
1703 | return win32_error_unicode("chmod",
|
---|
1704 | PyUnicode_AS_UNICODE(po));
|
---|
1705 | Py_INCREF(Py_None);
|
---|
1706 | return Py_None;
|
---|
1707 | }
|
---|
1708 | /* Drop the argument parsing error as narrow strings
|
---|
1709 | are also valid. */
|
---|
1710 | PyErr_Clear();
|
---|
1711 | }
|
---|
1712 | if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
|
---|
1713 | &path, &i))
|
---|
1714 | return NULL;
|
---|
1715 | Py_BEGIN_ALLOW_THREADS
|
---|
1716 | attr = GetFileAttributesA(path);
|
---|
1717 | if (attr != 0xFFFFFFFF) {
|
---|
1718 | if (i & _S_IWRITE)
|
---|
1719 | attr &= ~FILE_ATTRIBUTE_READONLY;
|
---|
1720 | else
|
---|
1721 | attr |= FILE_ATTRIBUTE_READONLY;
|
---|
1722 | res = SetFileAttributesA(path, attr);
|
---|
1723 | }
|
---|
1724 | else
|
---|
1725 | res = 0;
|
---|
1726 | Py_END_ALLOW_THREADS
|
---|
1727 | if (!res) {
|
---|
1728 | win32_error("chmod", path);
|
---|
1729 | PyMem_Free(path);
|
---|
1730 | return NULL;
|
---|
1731 | }
|
---|
1732 | PyMem_Free(path);
|
---|
1733 | Py_INCREF(Py_None);
|
---|
1734 | return Py_None;
|
---|
1735 | #else /* Py_WIN_WIDE_FILENAMES */
|
---|
1736 | if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding,
|
---|
1737 | &path, &i))
|
---|
1738 | return NULL;
|
---|
1739 | Py_BEGIN_ALLOW_THREADS
|
---|
1740 | res = chmod(path, i);
|
---|
1741 | Py_END_ALLOW_THREADS
|
---|
1742 | if (res < 0)
|
---|
1743 | return posix_error_with_allocated_filename(path);
|
---|
1744 | PyMem_Free(path);
|
---|
1745 | Py_INCREF(Py_None);
|
---|
1746 | return Py_None;
|
---|
1747 | #endif
|
---|
1748 | }
|
---|
1749 |
|
---|
1750 | #ifdef HAVE_FCHMOD
|
---|
1751 | PyDoc_STRVAR(posix_fchmod__doc__,
|
---|
1752 | "fchmod(fd, mode)\n\n\
|
---|
1753 | Change the access permissions of the file given by file\n\
|
---|
1754 | descriptor fd.");
|
---|
1755 |
|
---|
1756 | static PyObject *
|
---|
1757 | posix_fchmod(PyObject *self, PyObject *args)
|
---|
1758 | {
|
---|
1759 | int fd, mode, res;
|
---|
1760 | if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode))
|
---|
1761 | return NULL;
|
---|
1762 | Py_BEGIN_ALLOW_THREADS
|
---|
1763 | res = fchmod(fd, mode);
|
---|
1764 | Py_END_ALLOW_THREADS
|
---|
1765 | if (res < 0)
|
---|
1766 | return posix_error();
|
---|
1767 | Py_RETURN_NONE;
|
---|
1768 | }
|
---|
1769 | #endif /* HAVE_FCHMOD */
|
---|
1770 |
|
---|
1771 | #ifdef HAVE_LCHMOD
|
---|
1772 | PyDoc_STRVAR(posix_lchmod__doc__,
|
---|
1773 | "lchmod(path, mode)\n\n\
|
---|
1774 | Change the access permissions of a file. If path is a symlink, this\n\
|
---|
1775 | affects the link itself rather than the target.");
|
---|
1776 |
|
---|
1777 | static PyObject *
|
---|
1778 | posix_lchmod(PyObject *self, PyObject *args)
|
---|
1779 | {
|
---|
1780 | char *path = NULL;
|
---|
1781 | int i;
|
---|
1782 | int res;
|
---|
1783 | if (!PyArg_ParseTuple(args, "eti:lchmod", Py_FileSystemDefaultEncoding,
|
---|
1784 | &path, &i))
|
---|
1785 | return NULL;
|
---|
1786 | Py_BEGIN_ALLOW_THREADS
|
---|
1787 | res = lchmod(path, i);
|
---|
1788 | Py_END_ALLOW_THREADS
|
---|
1789 | if (res < 0)
|
---|
1790 | return posix_error_with_allocated_filename(path);
|
---|
1791 | PyMem_Free(path);
|
---|
1792 | Py_RETURN_NONE;
|
---|
1793 | }
|
---|
1794 | #endif /* HAVE_LCHMOD */
|
---|
1795 |
|
---|
1796 |
|
---|
1797 | #ifdef HAVE_CHFLAGS
|
---|
1798 | PyDoc_STRVAR(posix_chflags__doc__,
|
---|
1799 | "chflags(path, flags)\n\n\
|
---|
1800 | Set file flags.");
|
---|
1801 |
|
---|
1802 | static PyObject *
|
---|
1803 | posix_chflags(PyObject *self, PyObject *args)
|
---|
1804 | {
|
---|
1805 | char *path;
|
---|
1806 | unsigned long flags;
|
---|
1807 | int res;
|
---|
1808 | if (!PyArg_ParseTuple(args, "etk:chflags",
|
---|
1809 | Py_FileSystemDefaultEncoding, &path, &flags))
|
---|
1810 | return NULL;
|
---|
1811 | Py_BEGIN_ALLOW_THREADS
|
---|
1812 | res = chflags(path, flags);
|
---|
1813 | Py_END_ALLOW_THREADS
|
---|
1814 | if (res < 0)
|
---|
1815 | return posix_error_with_allocated_filename(path);
|
---|
1816 | PyMem_Free(path);
|
---|
1817 | Py_INCREF(Py_None);
|
---|
1818 | return Py_None;
|
---|
1819 | }
|
---|
1820 | #endif /* HAVE_CHFLAGS */
|
---|
1821 |
|
---|
1822 | #ifdef HAVE_LCHFLAGS
|
---|
1823 | PyDoc_STRVAR(posix_lchflags__doc__,
|
---|
1824 | "lchflags(path, flags)\n\n\
|
---|
1825 | Set file flags.\n\
|
---|
1826 | This function will not follow symbolic links.");
|
---|
1827 |
|
---|
1828 | static PyObject *
|
---|
1829 | posix_lchflags(PyObject *self, PyObject *args)
|
---|
1830 | {
|
---|
1831 | char *path;
|
---|
1832 | unsigned long flags;
|
---|
1833 | int res;
|
---|
1834 | if (!PyArg_ParseTuple(args, "etk:lchflags",
|
---|
1835 | Py_FileSystemDefaultEncoding, &path, &flags))
|
---|
1836 | return NULL;
|
---|
1837 | Py_BEGIN_ALLOW_THREADS
|
---|
1838 | res = lchflags(path, flags);
|
---|
1839 | Py_END_ALLOW_THREADS
|
---|
1840 | if (res < 0)
|
---|
1841 | return posix_error_with_allocated_filename(path);
|
---|
1842 | PyMem_Free(path);
|
---|
1843 | Py_INCREF(Py_None);
|
---|
1844 | return Py_None;
|
---|
1845 | }
|
---|
1846 | #endif /* HAVE_LCHFLAGS */
|
---|
1847 |
|
---|
1848 | #ifdef HAVE_CHROOT
|
---|
1849 | PyDoc_STRVAR(posix_chroot__doc__,
|
---|
1850 | "chroot(path)\n\n\
|
---|
1851 | Change root directory to path.");
|
---|
1852 |
|
---|
1853 | static PyObject *
|
---|
1854 | posix_chroot(PyObject *self, PyObject *args)
|
---|
1855 | {
|
---|
1856 | return posix_1str(args, "et:chroot", chroot);
|
---|
1857 | }
|
---|
1858 | #endif
|
---|
1859 |
|
---|
1860 | #ifdef HAVE_FSYNC
|
---|
1861 | PyDoc_STRVAR(posix_fsync__doc__,
|
---|
1862 | "fsync(fildes)\n\n\
|
---|
1863 | force write of file with filedescriptor to disk.");
|
---|
1864 |
|
---|
1865 | static PyObject *
|
---|
1866 | posix_fsync(PyObject *self, PyObject *fdobj)
|
---|
1867 | {
|
---|
1868 | return posix_fildes(fdobj, fsync);
|
---|
1869 | }
|
---|
1870 | #endif /* HAVE_FSYNC */
|
---|
1871 |
|
---|
1872 | #ifdef HAVE_FDATASYNC
|
---|
1873 |
|
---|
1874 | #ifdef __hpux
|
---|
1875 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
|
---|
1876 | #endif
|
---|
1877 |
|
---|
1878 | PyDoc_STRVAR(posix_fdatasync__doc__,
|
---|
1879 | "fdatasync(fildes)\n\n\
|
---|
1880 | force write of file with filedescriptor to disk.\n\
|
---|
1881 | does not force update of metadata.");
|
---|
1882 |
|
---|
1883 | static PyObject *
|
---|
1884 | posix_fdatasync(PyObject *self, PyObject *fdobj)
|
---|
1885 | {
|
---|
1886 | return posix_fildes(fdobj, fdatasync);
|
---|
1887 | }
|
---|
1888 | #endif /* HAVE_FDATASYNC */
|
---|
1889 |
|
---|
1890 |
|
---|
1891 | #ifdef HAVE_CHOWN
|
---|
1892 | PyDoc_STRVAR(posix_chown__doc__,
|
---|
1893 | "chown(path, uid, gid)\n\n\
|
---|
1894 | Change the owner and group id of path to the numeric uid and gid.");
|
---|
1895 |
|
---|
1896 | static PyObject *
|
---|
1897 | posix_chown(PyObject *self, PyObject *args)
|
---|
1898 | {
|
---|
1899 | char *path = NULL;
|
---|
1900 | long uid, gid;
|
---|
1901 | int res;
|
---|
1902 | if (!PyArg_ParseTuple(args, "etll:chown",
|
---|
1903 | Py_FileSystemDefaultEncoding, &path,
|
---|
1904 | &uid, &gid))
|
---|
1905 | return NULL;
|
---|
1906 | Py_BEGIN_ALLOW_THREADS
|
---|
1907 | res = chown(path, (uid_t) uid, (gid_t) gid);
|
---|
1908 | Py_END_ALLOW_THREADS
|
---|
1909 | if (res < 0)
|
---|
1910 | return posix_error_with_allocated_filename(path);
|
---|
1911 | PyMem_Free(path);
|
---|
1912 | Py_INCREF(Py_None);
|
---|
1913 | return Py_None;
|
---|
1914 | }
|
---|
1915 | #endif /* HAVE_CHOWN */
|
---|
1916 |
|
---|
1917 | #ifdef HAVE_FCHOWN
|
---|
1918 | PyDoc_STRVAR(posix_fchown__doc__,
|
---|
1919 | "fchown(fd, uid, gid)\n\n\
|
---|
1920 | Change the owner and group id of the file given by file descriptor\n\
|
---|
1921 | fd to the numeric uid and gid.");
|
---|
1922 |
|
---|
1923 | static PyObject *
|
---|
1924 | posix_fchown(PyObject *self, PyObject *args)
|
---|
1925 | {
|
---|
1926 | int fd;
|
---|
1927 | long uid, gid;
|
---|
1928 | int res;
|
---|
1929 | if (!PyArg_ParseTuple(args, "ill:chown", &fd, &uid, &gid))
|
---|
1930 | return NULL;
|
---|
1931 | Py_BEGIN_ALLOW_THREADS
|
---|
1932 | res = fchown(fd, (uid_t) uid, (gid_t) gid);
|
---|
1933 | Py_END_ALLOW_THREADS
|
---|
1934 | if (res < 0)
|
---|
1935 | return posix_error();
|
---|
1936 | Py_RETURN_NONE;
|
---|
1937 | }
|
---|
1938 | #endif /* HAVE_FCHOWN */
|
---|
1939 |
|
---|
1940 | #ifdef HAVE_LCHOWN
|
---|
1941 | PyDoc_STRVAR(posix_lchown__doc__,
|
---|
1942 | "lchown(path, uid, gid)\n\n\
|
---|
1943 | Change the owner and group id of path to the numeric uid and gid.\n\
|
---|
1944 | This function will not follow symbolic links.");
|
---|
1945 |
|
---|
1946 | static PyObject *
|
---|
1947 | posix_lchown(PyObject *self, PyObject *args)
|
---|
1948 | {
|
---|
1949 | char *path = NULL;
|
---|
1950 | long uid, gid;
|
---|
1951 | int res;
|
---|
1952 | if (!PyArg_ParseTuple(args, "etll:lchown",
|
---|
1953 | Py_FileSystemDefaultEncoding, &path,
|
---|
1954 | &uid, &gid))
|
---|
1955 | return NULL;
|
---|
1956 | Py_BEGIN_ALLOW_THREADS
|
---|
1957 | res = lchown(path, (uid_t) uid, (gid_t) gid);
|
---|
1958 | Py_END_ALLOW_THREADS
|
---|
1959 | if (res < 0)
|
---|
1960 | return posix_error_with_allocated_filename(path);
|
---|
1961 | PyMem_Free(path);
|
---|
1962 | Py_INCREF(Py_None);
|
---|
1963 | return Py_None;
|
---|
1964 | }
|
---|
1965 | #endif /* HAVE_LCHOWN */
|
---|
1966 |
|
---|
1967 |
|
---|
1968 | #ifdef HAVE_GETCWD
|
---|
1969 | PyDoc_STRVAR(posix_getcwd__doc__,
|
---|
1970 | "getcwd() -> path\n\n\
|
---|
1971 | Return a string representing the current working directory.");
|
---|
1972 |
|
---|
1973 | static PyObject *
|
---|
1974 | posix_getcwd(PyObject *self, PyObject *noargs)
|
---|
1975 | {
|
---|
1976 | int bufsize_incr = 1024;
|
---|
1977 | int bufsize = 0;
|
---|
1978 | char *tmpbuf = NULL;
|
---|
1979 | char *res = NULL;
|
---|
1980 | PyObject *dynamic_return;
|
---|
1981 |
|
---|
1982 | Py_BEGIN_ALLOW_THREADS
|
---|
1983 | do {
|
---|
1984 | bufsize = bufsize + bufsize_incr;
|
---|
1985 | tmpbuf = malloc(bufsize);
|
---|
1986 | if (tmpbuf == NULL) {
|
---|
1987 | break;
|
---|
1988 | }
|
---|
1989 | #if defined(PYOS_OS2) && defined(PYCC_GCC)
|
---|
1990 | res = _getcwd2(tmpbuf, bufsize);
|
---|
1991 | #else
|
---|
1992 | res = getcwd(tmpbuf, bufsize);
|
---|
1993 | #endif
|
---|
1994 |
|
---|
1995 | if (res == NULL) {
|
---|
1996 | free(tmpbuf);
|
---|
1997 | }
|
---|
1998 | } while ((res == NULL) && (errno == ERANGE));
|
---|
1999 | Py_END_ALLOW_THREADS
|
---|
2000 |
|
---|
2001 | if (res == NULL)
|
---|
2002 | return posix_error();
|
---|
2003 |
|
---|
2004 | dynamic_return = PyString_FromString(tmpbuf);
|
---|
2005 | free(tmpbuf);
|
---|
2006 |
|
---|
2007 | return dynamic_return;
|
---|
2008 | }
|
---|
2009 |
|
---|
2010 | #ifdef Py_USING_UNICODE
|
---|
2011 | PyDoc_STRVAR(posix_getcwdu__doc__,
|
---|
2012 | "getcwdu() -> path\n\n\
|
---|
2013 | Return a unicode string representing the current working directory.");
|
---|
2014 |
|
---|
2015 | static PyObject *
|
---|
2016 | posix_getcwdu(PyObject *self, PyObject *noargs)
|
---|
2017 | {
|
---|
2018 | char buf[1026];
|
---|
2019 | char *res;
|
---|
2020 |
|
---|
2021 | #ifdef Py_WIN_WIDE_FILENAMES
|
---|
2022 | DWORD len;
|
---|
2023 | if (unicode_file_names()) {
|
---|
2024 | wchar_t wbuf[1026];
|
---|
2025 | wchar_t *wbuf2 = wbuf;
|
---|
2026 | PyObject *resobj;
|
---|
2027 | Py_BEGIN_ALLOW_THREADS
|
---|
2028 | len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
|
---|
2029 | /* If the buffer is large enough, len does not include the
|
---|
2030 | terminating \0. If the buffer is too small, len includes
|
---|
2031 | the space needed for the terminator. */
|
---|
2032 | if (len >= sizeof wbuf/ sizeof wbuf[0]) {
|
---|
2033 | wbuf2 = malloc(len * sizeof(wchar_t));
|
---|
2034 | if (wbuf2)
|
---|
2035 | len = GetCurrentDirectoryW(len, wbuf2);
|
---|
2036 | }
|
---|
2037 | Py_END_ALLOW_THREADS
|
---|
2038 | if (!wbuf2) {
|
---|
2039 | PyErr_NoMemory();
|
---|
2040 | return NULL;
|
---|
2041 | }
|
---|
2042 | if (!len) {
|
---|
2043 | if (wbuf2 != wbuf) free(wbuf2);
|
---|
2044 | return win32_error("getcwdu", NULL);
|
---|
2045 | }
|
---|
2046 | resobj = PyUnicode_FromWideChar(wbuf2, len);
|
---|
2047 | if (wbuf2 != wbuf) free(wbuf2);
|
---|
2048 | return resobj;
|
---|
2049 | }
|
---|
2050 | #endif
|
---|
2051 |
|
---|
2052 | Py_BEGIN_ALLOW_THREADS
|
---|
2053 | #if defined(PYOS_OS2) && defined(PYCC_GCC)
|
---|
2054 | res = _getcwd2(buf, sizeof buf);
|
---|
2055 | #else
|
---|
2056 | res = getcwd(buf, sizeof buf);
|
---|
2057 | #endif
|
---|
2058 | Py_END_ALLOW_THREADS
|
---|
2059 | if (res == NULL)
|
---|
2060 | return posix_error();
|
---|
2061 | return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict");
|
---|
2062 | }
|
---|
2063 | #endif
|
---|
2064 | #endif
|
---|
2065 |
|
---|
2066 |
|
---|
2067 | #ifdef HAVE_LINK
|
---|
2068 | PyDoc_STRVAR(posix_link__doc__,
|
---|
2069 | "link(src, dst)\n\n\
|
---|
2070 | Create a hard link to a file.");
|
---|
2071 |
|
---|
2072 | static PyObject *
|
---|
2073 | posix_link(PyObject *self, PyObject *args)
|
---|
2074 | {
|
---|
2075 | return posix_2str(args, "etet:link", link);
|
---|
2076 | }
|
---|
2077 | #endif /* HAVE_LINK */
|
---|
2078 |
|
---|
2079 |
|
---|
2080 | PyDoc_STRVAR(posix_listdir__doc__,
|
---|
2081 | "listdir(path) -> list_of_strings\n\n\
|
---|
2082 | Return a list containing the names of the entries in the directory.\n\
|
---|
2083 | \n\
|
---|
2084 | path: path of directory to list\n\
|
---|
2085 | \n\
|
---|
2086 | The list is in arbitrary order. It does not include the special\n\
|
---|
2087 | entries '.' and '..' even if they are present in the directory.");
|
---|
2088 |
|
---|
2089 | static PyObject *
|
---|
2090 | posix_listdir(PyObject *self, PyObject *args)
|
---|
2091 | {
|
---|
2092 | /* XXX Should redo this putting the (now four) versions of opendir
|
---|
2093 | in separate files instead of having them all here... */
|
---|
2094 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
|
---|
2095 |
|
---|
2096 | PyObject *d, *v;
|
---|
2097 | HANDLE hFindFile;
|
---|
2098 | BOOL result;
|
---|
2099 | WIN32_FIND_DATA FileData;
|
---|
2100 | char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
|
---|
2101 | char *bufptr = namebuf;
|
---|
2102 | Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
|
---|
2103 |
|
---|
2104 | #ifdef Py_WIN_WIDE_FILENAMES
|
---|
2105 | /* If on wide-character-capable OS see if argument
|
---|
2106 | is Unicode and if so use wide API. */
|
---|
2107 | if (unicode_file_names()) {
|
---|
2108 | PyObject *po;
|
---|
2109 | if (PyArg_ParseTuple(args, "U:listdir", &po)) {
|
---|
2110 | WIN32_FIND_DATAW wFileData;
|
---|
2111 | Py_UNICODE *wnamebuf;
|
---|
2112 | Py_UNICODE wch;
|
---|
2113 | /* Overallocate for \\*.*\0 */
|
---|
2114 | len = PyUnicode_GET_SIZE(po);
|
---|
2115 | wnamebuf = malloc((len + 5) * sizeof(wchar_t));
|
---|
2116 | if (!wnamebuf) {
|
---|
2117 | PyErr_NoMemory();
|
---|
2118 | return NULL;
|
---|
2119 | }
|
---|
2120 | wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po));
|
---|
2121 | wch = len > 0 ? wnamebuf[len-1] : '\0';
|
---|
2122 | if (wch != L'/' && wch != L'\\' && wch != L':')
|
---|
2123 | wnamebuf[len++] = L'\\';
|
---|
2124 | wcscpy(wnamebuf + len, L"*.*");
|
---|
2125 | if ((d = PyList_New(0)) == NULL) {
|
---|
2126 | free(wnamebuf);
|
---|
2127 | return NULL;
|
---|
2128 | }
|
---|
2129 | hFindFile = FindFirstFileW(wnamebuf, &wFileData);
|
---|
2130 | if (hFindFile == INVALID_HANDLE_VALUE) {
|
---|
2131 | int error = GetLastError();
|
---|
2132 | if (error == ERROR_FILE_NOT_FOUND) {
|
---|
2133 | free(wnamebuf);
|
---|
2134 | return d;
|
---|
2135 | }
|
---|
2136 | Py_DECREF(d);
|
---|
2137 | win32_error_unicode("FindFirstFileW", wnamebuf);
|
---|
2138 | free(wnamebuf);
|
---|
2139 | return NULL;
|
---|
2140 | }
|
---|
2141 | do {
|
---|
2142 | /* Skip over . and .. */
|
---|
2143 | if (wcscmp(wFileData.cFileName, L".") != 0 &&
|
---|
2144 | wcscmp(wFileData.cFileName, L"..") != 0) {
|
---|
2145 | v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
|
---|
2146 | if (v == NULL) {
|
---|
2147 | Py_DECREF(d);
|
---|
2148 | d = NULL;
|
---|
2149 | break;
|
---|
2150 | }
|
---|
2151 | if (PyList_Append(d, v) != 0) {
|
---|
2152 | Py_DECREF(v);
|
---|
2153 | Py_DECREF(d);
|
---|
2154 | d = NULL;
|
---|
2155 | break;
|
---|
2156 | }
|
---|
2157 | Py_DECREF(v);
|
---|
2158 | }
|
---|
2159 | Py_BEGIN_ALLOW_THREADS
|
---|
2160 | result = FindNextFileW(hFindFile, &wFileData);
|
---|
2161 | Py_END_ALLOW_THREADS
|
---|
2162 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if
|
---|
2163 | it got to the end of the directory. */
|
---|
2164 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
|
---|
2165 | Py_DECREF(d);
|
---|
2166 | win32_error_unicode("FindNextFileW", wnamebuf);
|
---|
2167 | FindClose(hFindFile);
|
---|
2168 | free(wnamebuf);
|
---|
2169 | return NULL;
|
---|
2170 | }
|
---|
2171 | } while (result == TRUE);
|
---|
2172 |
|
---|
2173 | if (FindClose(hFindFile) == FALSE) {
|
---|
2174 | Py_DECREF(d);
|
---|
2175 | win32_error_unicode("FindClose", wnamebuf);
|
---|
2176 | free(wnamebuf);
|
---|
2177 | return NULL;
|
---|
2178 | }
|
---|
2179 | free(wnamebuf);
|
---|
2180 | return d;
|
---|
2181 | }
|
---|
2182 | /* Drop the argument parsing error as narrow strings
|
---|
2183 | are also valid. */
|
---|
2184 | PyErr_Clear();
|
---|
2185 | }
|
---|
2186 | #endif
|
---|
2187 |
|
---|
2188 | if (!PyArg_ParseTuple(args, "et#:listdir",
|
---|
2189 | Py_FileSystemDefaultEncoding, &bufptr, &len))
|
---|
2190 | return NULL;
|
---|
2191 | if (len > 0) {
|
---|
2192 | char ch = namebuf[len-1];
|
---|
2193 | if (ch != SEP && ch != ALTSEP && ch != ':')
|
---|
2194 | namebuf[len++] = '/';
|
---|
2195 | }
|
---|
2196 | strcpy(namebuf + len, "*.*");
|
---|
2197 |
|
---|
2198 | if ((d = PyList_New(0)) == NULL)
|
---|
2199 | return NULL;
|
---|
2200 |
|
---|
2201 | hFindFile = FindFirstFile(namebuf, &FileData);
|
---|
2202 | if (hFindFile == INVALID_HANDLE_VALUE) {
|
---|
2203 | int error = GetLastError();
|
---|
2204 | if (error == ERROR_FILE_NOT_FOUND)
|
---|
2205 | return d;
|
---|
2206 | Py_DECREF(d);
|
---|
2207 | return win32_error("FindFirstFile", namebuf);
|
---|
2208 | }
|
---|
2209 | do {
|
---|
2210 | /* Skip over . and .. */
|
---|
2211 | if (strcmp(FileData.cFileName, ".") != 0 &&
|
---|
2212 | strcmp(FileData.cFileName, "..") != 0) {
|
---|
2213 | v = PyString_FromString(FileData.cFileName);
|
---|
2214 | if (v == NULL) {
|
---|
2215 | Py_DECREF(d);
|
---|
2216 | d = NULL;
|
---|
2217 | break;
|
---|
2218 | }
|
---|
2219 | if (PyList_Append(d, v) != 0) {
|
---|
2220 | Py_DECREF(v);
|
---|
2221 | Py_DECREF(d);
|
---|
2222 | d = NULL;
|
---|
2223 | break;
|
---|
2224 | }
|
---|
2225 | Py_DECREF(v);
|
---|
2226 | }
|
---|
2227 | Py_BEGIN_ALLOW_THREADS
|
---|
2228 | result = FindNextFile(hFindFile, &FileData);
|
---|
2229 | Py_END_ALLOW_THREADS
|
---|
2230 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if
|
---|
2231 | it got to the end of the directory. */
|
---|
2232 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
|
---|
2233 | Py_DECREF(d);
|
---|
2234 | win32_error("FindNextFile", namebuf);
|
---|
2235 | FindClose(hFindFile);
|
---|
2236 | return NULL;
|
---|
2237 | }
|
---|
2238 | } while (result == TRUE);
|
---|
2239 |
|
---|
2240 | if (FindClose(hFindFile) == FALSE) {
|
---|
2241 | Py_DECREF(d);
|
---|
2242 | return win32_error("FindClose", namebuf);
|
---|
2243 | }
|
---|
2244 |
|
---|
2245 | return d;
|
---|
2246 |
|
---|
2247 | #elif defined(PYOS_OS2)
|
---|
2248 |
|
---|
2249 | #ifndef MAX_PATH
|
---|
2250 | #define MAX_PATH CCHMAXPATH
|
---|
2251 | #endif
|
---|
2252 | char *name, *pt;
|
---|
2253 | Py_ssize_t len;
|
---|
2254 | PyObject *d, *v;
|
---|
2255 | char namebuf[MAX_PATH+5];
|
---|
2256 | HDIR hdir = 1;
|
---|
2257 | ULONG srchcnt = 1;
|
---|
2258 | FILEFINDBUF3 ep;
|
---|
2259 | APIRET rc;
|
---|
2260 |
|
---|
2261 | if (!PyArg_ParseTuple(args, "t#:listdir", &name, &len))
|
---|
2262 | return NULL;
|
---|
2263 | if (len >= MAX_PATH) {
|
---|
2264 | PyErr_SetString(PyExc_ValueError, "path too long");
|
---|
2265 | return NULL;
|
---|
2266 | }
|
---|
2267 | strcpy(namebuf, name);
|
---|
2268 | for (pt = namebuf; *pt; pt++)
|
---|
2269 | if (*pt == ALTSEP)
|
---|
2270 | *pt = SEP;
|
---|
2271 | if (namebuf[len-1] != SEP)
|
---|
2272 | namebuf[len++] = SEP;
|
---|
2273 | strcpy(namebuf + len, "*.*");
|
---|
2274 |
|
---|
2275 | if ((d = PyList_New(0)) == NULL)
|
---|
2276 | return NULL;
|
---|
2277 |
|
---|
2278 | rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
|
---|
2279 | &hdir, /* Handle to Use While Search Directory */
|
---|
2280 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
|
---|
2281 | &ep, sizeof(ep), /* Structure to Receive Directory Entry */
|
---|
2282 | &srchcnt, /* Max and Actual Count of Entries Per Iteration */
|
---|
2283 | FIL_STANDARD); /* Format of Entry (EAs or Not) */
|
---|
2284 |
|
---|
2285 | if (rc != NO_ERROR) {
|
---|
2286 | errno = ENOENT;
|
---|
2287 | return posix_error_with_filename(name);
|
---|
2288 | }
|
---|
2289 |
|
---|
2290 | if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
|
---|
2291 | do {
|
---|
2292 | if (ep.achName[0] == '.'
|
---|
2293 | && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
|
---|
2294 | continue; /* Skip Over "." and ".." Names */
|
---|
2295 |
|
---|
2296 | strcpy(namebuf, ep.achName);
|
---|
2297 |
|
---|
2298 | /* Leave Case of Name Alone -- In Native Form */
|
---|
2299 | /* (Removed Forced Lowercasing Code) */
|
---|
2300 |
|
---|
2301 | v = PyString_FromString(namebuf);
|
---|
2302 | if (v == NULL) {
|
---|
2303 | Py_DECREF(d);
|
---|
2304 | d = NULL;
|
---|
2305 | break;
|
---|
2306 | }
|
---|
2307 | if (PyList_Append(d, v) != 0) {
|
---|
2308 | Py_DECREF(v);
|
---|
2309 | Py_DECREF(d);
|
---|
2310 | d = NULL;
|
---|
2311 | break;
|
---|
2312 | }
|
---|
2313 | Py_DECREF(v);
|
---|
2314 | } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
|
---|
2315 | }
|
---|
2316 |
|
---|
2317 | return d;
|
---|
2318 | #else
|
---|
2319 |
|
---|
2320 | char *name = NULL;
|
---|
2321 | PyObject *d, *v;
|
---|
2322 | DIR *dirp;
|
---|
2323 | struct dirent *ep;
|
---|
2324 | int arg_is_unicode = 1;
|
---|
2325 |
|
---|
2326 | errno = 0;
|
---|
2327 | if (!PyArg_ParseTuple(args, "U:listdir", &v)) {
|
---|
2328 | arg_is_unicode = 0;
|
---|
2329 | PyErr_Clear();
|
---|
2330 | }
|
---|
2331 | if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name))
|
---|
2332 | return NULL;
|
---|
2333 | if ((dirp = opendir(name)) == NULL) {
|
---|
2334 | return posix_error_with_allocated_filename(name);
|
---|
2335 | }
|
---|
2336 | if ((d = PyList_New(0)) == NULL) {
|
---|
2337 | closedir(dirp);
|
---|
2338 | PyMem_Free(name);
|
---|
2339 | return NULL;
|
---|
2340 | }
|
---|
2341 | for (;;) {
|
---|
2342 | errno = 0;
|
---|
2343 | Py_BEGIN_ALLOW_THREADS
|
---|
2344 | ep = readdir(dirp);
|
---|
2345 | Py_END_ALLOW_THREADS
|
---|
2346 | if (ep == NULL) {
|
---|
2347 | if (errno == 0) {
|
---|
2348 | break;
|
---|
2349 | } else {
|
---|
2350 | closedir(dirp);
|
---|
2351 | Py_DECREF(d);
|
---|
2352 | return posix_error_with_allocated_filename(name);
|
---|
2353 | }
|
---|
2354 | }
|
---|
2355 | if (ep->d_name[0] == '.' &&
|
---|
2356 | (NAMLEN(ep) == 1 ||
|
---|
2357 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
|
---|
2358 | continue;
|
---|
2359 | v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep));
|
---|
2360 | if (v == NULL) {
|
---|
2361 | Py_DECREF(d);
|
---|
2362 | d = NULL;
|
---|
2363 | break;
|
---|
2364 | }
|
---|
2365 | #ifdef Py_USING_UNICODE
|
---|
2366 | if (arg_is_unicode) {
|
---|
2367 | PyObject *w;
|
---|
2368 |
|
---|
2369 | w = PyUnicode_FromEncodedObject(v,
|
---|
2370 | Py_FileSystemDefaultEncoding,
|
---|
2371 | "strict");
|
---|
2372 | if (w != NULL) {
|
---|
2373 | Py_DECREF(v);
|
---|
2374 | v = w;
|
---|
2375 | }
|
---|
2376 | else {
|
---|
2377 | /* fall back to the original byte string, as
|
---|
2378 | discussed in patch #683592 */
|
---|
2379 | PyErr_Clear();
|
---|
2380 | }
|
---|
2381 | }
|
---|
2382 | #endif
|
---|
2383 | if (PyList_Append(d, v) != 0) {
|
---|
2384 | Py_DECREF(v);
|
---|
2385 | Py_DECREF(d);
|
---|
2386 | d = NULL;
|
---|
2387 | break;
|
---|
2388 | }
|
---|
2389 | Py_DECREF(v);
|
---|
2390 | }
|
---|
2391 | closedir(dirp);
|
---|
2392 | PyMem_Free(name);
|
---|
2393 |
|
---|
2394 | return d;
|
---|
2395 |
|
---|
2396 | #endif /* which OS */
|
---|
2397 | } /* end of posix_listdir */
|
---|
2398 |
|
---|
2399 | #ifdef MS_WINDOWS
|
---|
2400 | /* A helper function for abspath on win32 */
|
---|
2401 | static PyObject *
|
---|
2402 | posix__getfullpathname(PyObject *self, PyObject *args)
|
---|
2403 | {
|
---|
2404 | /* assume encoded strings won't more than double no of chars */
|
---|
2405 | char inbuf[MAX_PATH*2];
|
---|
2406 | char *inbufp = inbuf;
|
---|
2407 | Py_ssize_t insize = sizeof(inbuf);
|
---|
2408 | char outbuf[MAX_PATH*2];
|
---|
2409 | char *temp;
|
---|
2410 | #ifdef Py_WIN_WIDE_FILENAMES
|
---|
2411 | if (unicode_file_names()) {
|
---|
2412 | PyUnicodeObject *po;
|
---|
2413 | if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
|
---|
2414 | Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
|
---|
2415 | Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf;
|
---|
2416 | Py_UNICODE *wtemp;
|
---|
2417 | DWORD result;
|
---|
2418 | PyObject *v;
|
---|
2419 | result = GetFullPathNameW(wpath,
|
---|
2420 | sizeof(woutbuf)/sizeof(woutbuf[0]),
|
---|
2421 | woutbuf, &wtemp);
|
---|
2422 | if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) {
|
---|
2423 | woutbufp = malloc(result * sizeof(Py_UNICODE));
|
---|
2424 | if (!woutbufp)
|
---|
2425 | return PyErr_NoMemory();
|
---|
2426 | result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
|
---|
2427 | }
|
---|
2428 | if (result)
|
---|
2429 | v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp));
|
---|
2430 | else
|
---|
2431 | v = win32_error_unicode("GetFullPathNameW", wpath);
|
---|
2432 | if (woutbufp != woutbuf)
|
---|
2433 | free(woutbufp);
|
---|
2434 | return v;
|
---|
2435 | }
|
---|
2436 | /* Drop the argument parsing error as narrow strings
|
---|
2437 | are also valid. */
|
---|
2438 | PyErr_Clear();
|
---|
2439 | }
|
---|
2440 | #endif
|
---|
2441 | if (!PyArg_ParseTuple (args, "et#:_getfullpathname",
|
---|
2442 | Py_FileSystemDefaultEncoding, &inbufp,
|
---|
2443 | &insize))
|
---|
2444 | return NULL;
|
---|
2445 | if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]),
|
---|
2446 | outbuf, &temp))
|
---|
2447 | return win32_error("GetFullPathName", inbuf);
|
---|
2448 | if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
|
---|
2449 | return PyUnicode_Decode(outbuf, strlen(outbuf),
|
---|
2450 | Py_FileSystemDefaultEncoding, NULL);
|
---|
2451 | }
|
---|
2452 | return PyString_FromString(outbuf);
|
---|
2453 | } /* end of posix__getfullpathname */
|
---|
2454 | #endif /* MS_WINDOWS */
|
---|
2455 |
|
---|
2456 | PyDoc_STRVAR(posix_mkdir__doc__,
|
---|
2457 | "mkdir(path [, mode=0777])\n\n\
|
---|
2458 | Create a directory.");
|
---|
2459 |
|
---|
2460 | static PyObject *
|
---|
2461 | posix_mkdir(PyObject *self, PyObject *args)
|
---|
2462 | {
|
---|
2463 | int res;
|
---|
2464 | char *path = NULL;
|
---|
2465 | int mode = 0777;
|
---|
2466 |
|
---|
2467 | #ifdef Py_WIN_WIDE_FILENAMES
|
---|
2468 | if (unicode_file_names()) {
|
---|
2469 | PyUnicodeObject *po;
|
---|
2470 | if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
|
---|
2471 | Py_BEGIN_ALLOW_THREADS
|
---|
2472 | /* PyUnicode_AS_UNICODE OK without thread lock as
|
---|
2473 | it is a simple dereference. */
|
---|
2474 | res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
|
---|
2475 | Py_END_ALLOW_THREADS
|
---|
2476 | if (!res)
|
---|
2477 | return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
|
---|
2478 | Py_INCREF(Py_None);
|
---|
2479 | return Py_None;
|
---|
2480 | }
|
---|
2481 | /* Drop the argument parsing error as narrow strings
|
---|
2482 | are also valid. */
|
---|
2483 | PyErr_Clear();
|
---|
2484 | }
|
---|
2485 | if (!PyArg_ParseTuple(args, "et|i:mkdir",
|
---|
2486 | Py_FileSystemDefaultEncoding, &path, &mode))
|
---|
2487 | return NULL;
|
---|
2488 | Py_BEGIN_ALLOW_THREADS
|
---|
2489 | /* PyUnicode_AS_UNICODE OK without thread lock as
|
---|
2490 | it is a simple dereference. */
|
---|
2491 | res = CreateDirectoryA(path, NULL);
|
---|
2492 | Py_END_ALLOW_THREADS
|
---|
2493 | if (!res) {
|
---|
2494 | win32_error("mkdir", path);
|
---|
2495 | PyMem_Free(path);
|
---|
2496 | return NULL;
|
---|
2497 | }
|
---|
2498 | PyMem_Free(path);
|
---|
2499 | Py_INCREF(Py_None);
|
---|
2500 | return Py_None;
|
---|
2501 | #else
|
---|
2502 |
|
---|
2503 | if (!PyArg_ParseTuple(args, "et|i:mkdir",
|
---|
2504 | Py_FileSystemDefaultEncoding, &path, &mode))
|
---|
2505 | return NULL;
|
---|
2506 | Py_BEGIN_ALLOW_THREADS
|
---|
2507 | #if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
|
---|
2508 | res = mkdir(path);
|
---|
2509 | #else
|
---|
2510 | res = mkdir(path, mode);
|
---|
2511 | #endif
|
---|
2512 | Py_END_ALLOW_THREADS
|
---|
2513 | if (res < 0)
|
---|
2514 | return posix_error_with_allocated_filename(path);
|
---|
2515 | PyMem_Free(path);
|
---|
2516 | Py_INCREF(Py_None);
|
---|
2517 | return Py_None;
|
---|
2518 | #endif
|
---|
2519 | }
|
---|
2520 |
|
---|
2521 |
|
---|
2522 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
|
---|
2523 | #if defined(HAVE_SYS_RESOURCE_H)
|
---|
2524 | #include <sys/resource.h>
|
---|
2525 | #endif
|
---|
2526 |
|
---|
2527 |
|
---|
2528 | #ifdef HAVE_NICE
|
---|
2529 | PyDoc_STRVAR(posix_nice__doc__,
|
---|
2530 | "nice(inc) -> new_priority\n\n\
|
---|
2531 | Decrease the priority of process by inc and return the new priority.");
|
---|
2532 |
|
---|
2533 | static PyObject *
|
---|
2534 | posix_nice(PyObject *self, PyObject *args)
|
---|
2535 | {
|
---|
2536 | int increment, value;
|
---|
2537 |
|
---|
2538 | if (!PyArg_ParseTuple(args, "i:nice", &increment))
|
---|
2539 | return NULL;
|
---|
2540 |
|
---|
2541 | /* There are two flavours of 'nice': one that returns the new
|
---|
2542 | priority (as required by almost all standards out there) and the
|
---|
2543 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices
|
---|
2544 | the use of getpriority() to get the new priority.
|
---|
2545 |
|
---|
2546 | If we are of the nice family that returns the new priority, we
|
---|
2547 | need to clear errno before the call, and check if errno is filled
|
---|
2548 | before calling posix_error() on a returnvalue of -1, because the
|
---|
2549 | -1 may be the actual new priority! */
|
---|
2550 |
|
---|
2551 | errno = 0;
|
---|
2552 | value = nice(increment);
|
---|
2553 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
|
---|
2554 | if (value == 0)
|
---|
2555 | value = getpriority(PRIO_PROCESS, 0);
|
---|
2556 | #endif
|
---|
2557 | if (value == -1 && errno != 0)
|
---|
2558 | /* either nice() or getpriority() returned an error */
|
---|
2559 | return posix_error();
|
---|
2560 | return PyInt_FromLong((long) value);
|
---|
2561 | }
|
---|
2562 | #endif /* HAVE_NICE */
|
---|
2563 |
|
---|
2564 | PyDoc_STRVAR(posix_rename__doc__,
|
---|
2565 | "rename(old, new)\n\n\
|
---|
2566 | Rename a file or directory.");
|
---|
2567 |
|
---|
2568 | static PyObject *
|
---|
2569 | posix_rename(PyObject *self, PyObject *args)
|
---|
2570 | {
|
---|
2571 | #ifdef MS_WINDOWS
|
---|
2572 | PyObject *o1, *o2;
|
---|
2573 | char *p1, *p2;
|
---|
2574 | BOOL result;
|
---|
2575 | if (unicode_file_names()) {
|
---|
2576 | if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2))
|
---|
2577 | goto error;
|
---|
2578 | if (!convert_to_unicode(&o1))
|
---|
2579 | goto error;
|
---|
2580 | if (!convert_to_unicode(&o2)) {
|
---|
2581 | Py_DECREF(o1);
|
---|
2582 | goto error;
|
---|
2583 | }
|
---|
2584 | Py_BEGIN_ALLOW_THREADS
|
---|
2585 | result = MoveFileW(PyUnicode_AsUnicode(o1),
|
---|
2586 | PyUnicode_AsUnicode(o2));
|
---|
2587 | Py_END_ALLOW_THREADS
|
---|
2588 | Py_DECREF(o1);
|
---|
2589 | Py_DECREF(o2);
|
---|
2590 | if (!result)
|
---|
2591 | return win32_error("rename", NULL);
|
---|
2592 | Py_INCREF(Py_None);
|
---|
2593 | return Py_None;
|
---|
2594 | error:
|
---|
2595 | PyErr_Clear();
|
---|
2596 | }
|
---|
2597 | if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
|
---|
2598 | return NULL;
|
---|
2599 | Py_BEGIN_ALLOW_THREADS
|
---|
2600 | result = MoveFileA(p1, p2);
|
---|
2601 | Py_END_ALLOW_THREADS
|
---|
2602 | if (!result)
|
---|
2603 | return win32_error("rename", NULL);
|
---|
2604 | Py_INCREF(Py_None);
|
---|
2605 | return Py_None;
|
---|
2606 | #else
|
---|
2607 | return posix_2str(args, "etet:rename", rename);
|
---|
2608 | #endif
|
---|
2609 | }
|
---|
2610 |
|
---|
2611 |
|
---|
2612 | PyDoc_STRVAR(posix_rmdir__doc__,
|
---|
2613 | "rmdir(path)\n\n\
|
---|
2614 | Remove a directory.");
|
---|
2615 |
|
---|
2616 | static PyObject *
|
---|
2617 | posix_rmdir(PyObject *self, PyObject *args)
|
---|
2618 | {
|
---|
2619 | #ifdef MS_WINDOWS
|
---|
2620 | return win32_1str(args, "rmdir", "s:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
|
---|
2621 | #else
|
---|
2622 | return posix_1str(args, "et:rmdir", rmdir);
|
---|
2623 | #endif
|
---|
2624 | }
|
---|
2625 |
|
---|
2626 |
|
---|
2627 | PyDoc_STRVAR(posix_stat__doc__,
|
---|
2628 | "stat(path) -> stat result\n\n\
|
---|
2629 | Perform a stat system call on the given path.");
|
---|
2630 |
|
---|
2631 | static PyObject *
|
---|
2632 | posix_stat(PyObject *self, PyObject *args)
|
---|
2633 | {
|
---|
2634 | #ifdef MS_WINDOWS
|
---|
2635 | return posix_do_stat(self, args, "et:stat", STAT, "U:stat", win32_wstat);
|
---|
2636 | #else
|
---|
2637 | return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL);
|
---|
2638 | #endif
|
---|
2639 | }
|
---|
2640 |
|
---|
2641 |
|
---|
2642 | #ifdef HAVE_SYSTEM
|
---|
2643 | PyDoc_STRVAR(posix_system__doc__,
|
---|
2644 | "system(command) -> exit_status\n\n\
|
---|
2645 | Execute the command (a string) in a subshell.");
|
---|
2646 |
|
---|
2647 | static PyObject *
|
---|
2648 | posix_system(PyObject *self, PyObject *args)
|
---|
2649 | {
|
---|
2650 | char *command;
|
---|
2651 | long sts;
|
---|
2652 | if (!PyArg_ParseTuple(args, "s:system", &command))
|
---|
2653 | return NULL;
|
---|
2654 | Py_BEGIN_ALLOW_THREADS
|
---|
2655 | sts = system(command);
|
---|
2656 | Py_END_ALLOW_THREADS
|
---|
2657 | return PyInt_FromLong(sts);
|
---|
2658 | }
|
---|
2659 | #endif
|
---|
2660 |
|
---|
2661 |
|
---|
2662 | PyDoc_STRVAR(posix_umask__doc__,
|
---|
2663 | "umask(new_mask) -> old_mask\n\n\
|
---|
2664 | Set the current numeric umask and return the previous umask.");
|
---|
2665 |
|
---|
2666 | static PyObject *
|
---|
2667 | posix_umask(PyObject *self, PyObject *args)
|
---|
2668 | {
|
---|
2669 | int i;
|
---|
2670 | if (!PyArg_ParseTuple(args, "i:umask", &i))
|
---|
2671 | return NULL;
|
---|
2672 | i = (int)umask(i);
|
---|
2673 | if (i < 0)
|
---|
2674 | return posix_error();
|
---|
2675 | return PyInt_FromLong((long)i);
|
---|
2676 | }
|
---|
2677 |
|
---|
2678 |
|
---|
2679 | PyDoc_STRVAR(posix_unlink__doc__,
|
---|
2680 | "unlink(path)\n\n\
|
---|
2681 | Remove a file (same as remove(path)).");
|
---|
2682 |
|
---|
2683 | PyDoc_STRVAR(posix_remove__doc__,
|
---|
2684 | "remove(path)\n\n\
|
---|
2685 | Remove a file (same as unlink(path)).");
|
---|
2686 |
|
---|
2687 | static PyObject *
|
---|
2688 | posix_unlink(PyObject *self, PyObject *args)
|
---|
2689 | {
|
---|
2690 | #ifdef MS_WINDOWS
|
---|
2691 | return win32_1str(args, "remove", "s:remove", DeleteFileA, "U:remove", DeleteFileW);
|
---|
2692 | #else
|
---|
2693 | return posix_1str(args, "et:remove", unlink);
|
---|
2694 | #endif
|
---|
2695 | }
|
---|
2696 |
|
---|
2697 |
|
---|
2698 | #ifdef HAVE_UNAME
|
---|
2699 | PyDoc_STRVAR(posix_uname__doc__,
|
---|
2700 | "uname() -> (sysname, nodename, release, version, machine)\n\n\
|
---|
2701 | Return a tuple identifying the current operating system.");
|
---|
2702 |
|
---|
2703 | static PyObject *
|
---|
2704 | posix_uname(PyObject *self, PyObject *noargs)
|
---|
2705 | {
|
---|
2706 | struct utsname u;
|
---|
2707 | int res;
|
---|
2708 |
|
---|
2709 | Py_BEGIN_ALLOW_THREADS
|
---|
2710 | res = uname(&u);
|
---|
2711 | Py_END_ALLOW_THREADS
|
---|
2712 | if (res < 0)
|
---|
2713 | return posix_error();
|
---|
2714 | return Py_BuildValue("(sssss)",
|
---|
2715 | u.sysname,
|
---|
2716 | u.nodename,
|
---|
2717 | u.release,
|
---|
2718 | u.version,
|
---|
2719 | u.machine);
|
---|
2720 | }
|
---|
2721 | #endif /* HAVE_UNAME */
|
---|
2722 |
|
---|
2723 | static int
|
---|
2724 | extract_time(PyObject *t, long* sec, long* usec)
|
---|
2725 | {
|
---|
2726 | long intval;
|
---|
2727 | if (PyFloat_Check(t)) {
|
---|
2728 | double tval = PyFloat_AsDouble(t);
|
---|
2729 | PyObject *intobj = Py_TYPE(t)->tp_as_number->nb_int(t);
|
---|
2730 | if (!intobj)
|
---|
2731 | return -1;
|
---|
2732 | intval = PyInt_AsLong(intobj);
|
---|
2733 | Py_DECREF(intobj);
|
---|
2734 | if (intval == -1 && PyErr_Occurred())
|
---|
2735 | return -1;
|
---|
2736 | *sec = intval;
|
---|
2737 | *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
|
---|
2738 | if (*usec < 0)
|
---|
2739 | /* If rounding gave us a negative number,
|
---|
2740 | truncate. */
|
---|
2741 | *usec = 0;
|
---|
2742 | return 0;
|
---|
2743 | }
|
---|
2744 | intval = PyInt_AsLong(t);
|
---|
2745 | if (intval == -1 && PyErr_Occurred())
|
---|
2746 | return -1;
|
---|
2747 | *sec = intval;
|
---|
2748 | *usec = 0;
|
---|
2749 | return 0;
|
---|
2750 | }
|
---|
2751 |
|
---|
2752 | PyDoc_STRVAR(posix_utime__doc__,
|
---|
2753 | "utime(path, (atime, mtime))\n\
|
---|
2754 | utime(path, None)\n\n\
|
---|
2755 | Set the access and modified time of the file to the given values. If the\n\
|
---|
2756 | second form is used, set the access and modified times to the current time.");
|
---|
2757 |
|
---|
2758 | static PyObject *
|
---|
2759 | posix_utime(PyObject *self, PyObject *args)
|
---|
2760 | {
|
---|
2761 | #ifdef Py_WIN_WIDE_FILENAMES
|
---|
2762 | PyObject *arg;
|
---|
2763 | PyUnicodeObject *obwpath;
|
---|
2764 | wchar_t *wpath = NULL;
|
---|
2765 | char *apath = NULL;
|
---|
2766 | HANDLE hFile;
|
---|
2767 | long atimesec, mtimesec, ausec, musec;
|
---|
2768 | FILETIME atime, mtime;
|
---|
2769 | PyObject *result = NULL;
|
---|
2770 |
|
---|
2771 | if (unicode_file_names()) {
|
---|
2772 | if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
|
---|
2773 | wpath = PyUnicode_AS_UNICODE(obwpath);
|
---|
2774 | Py_BEGIN_ALLOW_THREADS
|
---|
2775 | hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
|
---|
2776 | NULL, OPEN_EXISTING,
|
---|
2777 | FILE_FLAG_BACKUP_SEMANTICS, NULL);
|
---|
2778 | Py_END_ALLOW_THREADS
|
---|
2779 | if (hFile == INVALID_HANDLE_VALUE)
|
---|
2780 | return win32_error_unicode("utime", wpath);
|
---|
2781 | } else
|
---|
2782 | /* Drop the argument parsing error as narrow strings
|
---|
2783 | are also valid. */
|
---|
2784 | PyErr_Clear();
|
---|
2785 | }
|
---|
2786 | if (!wpath) {
|
---|
2787 | if (!PyArg_ParseTuple(args, "etO:utime",
|
---|
2788 | Py_FileSystemDefaultEncoding, &apath, &arg))
|
---|
2789 | return NULL;
|
---|
2790 | Py_BEGIN_ALLOW_THREADS
|
---|
2791 | hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
|
---|
2792 | NULL, OPEN_EXISTING,
|
---|
2793 | FILE_FLAG_BACKUP_SEMANTICS, NULL);
|
---|
2794 | Py_END_ALLOW_THREADS
|
---|
2795 | if (hFile == INVALID_HANDLE_VALUE) {
|
---|
2796 | win32_error("utime", apath);
|
---|
2797 | PyMem_Free(apath);
|
---|
2798 | return NULL;
|
---|
2799 | }
|
---|
2800 | PyMem_Free(apath);
|
---|
2801 | }
|
---|
2802 |
|
---|
2803 | if (arg == Py_None) {
|
---|
2804 | SYSTEMTIME now;
|
---|
2805 | GetSystemTime(&now);
|
---|
2806 | if (!SystemTimeToFileTime(&now, &mtime) ||
|
---|
2807 | !SystemTimeToFileTime(&now, &atime)) {
|
---|
2808 | win32_error("utime", NULL);
|
---|
2809 | goto done;
|
---|
2810 | }
|
---|
2811 | }
|
---|
2812 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
|
---|
2813 | PyErr_SetString(PyExc_TypeError,
|
---|
2814 | "utime() arg 2 must be a tuple (atime, mtime)");
|
---|
2815 | goto done;
|
---|
2816 | }
|
---|
2817 | else {
|
---|
2818 | if (extract_time(PyTuple_GET_ITEM(arg, 0),
|
---|
2819 | &atimesec, &ausec) == -1)
|
---|
2820 | goto done;
|
---|
2821 | time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
|
---|
2822 | if (extract_time(PyTuple_GET_ITEM(arg, 1),
|
---|
2823 | &mtimesec, &musec) == -1)
|
---|
2824 | goto done;
|
---|
2825 | time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
|
---|
2826 | }
|
---|
2827 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
|
---|
2828 | /* Avoid putting the file name into the error here,
|
---|
2829 | as that may confuse the user into believing that
|
---|
2830 | something is wrong with the file, when it also
|
---|
2831 | could be the time stamp that gives a problem. */
|
---|
2832 | win32_error("utime", NULL);
|
---|
2833 | }
|
---|
2834 | Py_INCREF(Py_None);
|
---|
2835 | result = Py_None;
|
---|
2836 | done:
|
---|
2837 | CloseHandle(hFile);
|
---|
2838 | return result;
|
---|
2839 | #else /* Py_WIN_WIDE_FILENAMES */
|
---|
2840 |
|
---|
2841 | char *path = NULL;
|
---|
2842 | long atime, mtime, ausec, musec;
|
---|
2843 | int res;
|
---|
2844 | PyObject* arg;
|
---|
2845 |
|
---|
2846 | #if defined(HAVE_UTIMES)
|
---|
2847 | struct timeval buf[2];
|
---|
2848 | #define ATIME buf[0].tv_sec
|
---|
2849 | #define MTIME buf[1].tv_sec
|
---|
2850 | #elif defined(HAVE_UTIME_H)
|
---|
2851 | /* XXX should define struct utimbuf instead, above */
|
---|
2852 | struct utimbuf buf;
|
---|
2853 | #define ATIME buf.actime
|
---|
2854 | #define MTIME buf.modtime
|
---|
2855 | #define UTIME_ARG &buf
|
---|
2856 | #else /* HAVE_UTIMES */
|
---|
2857 | time_t buf[2];
|
---|
2858 | #define ATIME buf[0]
|
---|
2859 | #define MTIME buf[1]
|
---|
2860 | #define UTIME_ARG buf
|
---|
2861 | #endif /* HAVE_UTIMES */
|
---|
2862 |
|
---|
2863 |
|
---|
2864 | if (!PyArg_ParseTuple(args, "etO:utime",
|
---|
2865 | Py_FileSystemDefaultEncoding, &path, &arg))
|
---|
2866 | return NULL;
|
---|
2867 | if (arg == Py_None) {
|
---|
2868 | /* optional time values not given */
|
---|
2869 | Py_BEGIN_ALLOW_THREADS
|
---|
2870 | res = utime(path, NULL);
|
---|
2871 | Py_END_ALLOW_THREADS
|
---|
2872 | }
|
---|
2873 | else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
|
---|
2874 | PyErr_SetString(PyExc_TypeError,
|
---|
2875 | "utime() arg 2 must be a tuple (atime, mtime)");
|
---|
2876 | PyMem_Free(path);
|
---|
2877 | return NULL;
|
---|
2878 | }
|
---|
2879 | else {
|
---|
2880 | if (extract_time(PyTuple_GET_ITEM(arg, 0),
|
---|
2881 | &atime, &ausec) == -1) {
|
---|
2882 | PyMem_Free(path);
|
---|
2883 | return NULL;
|
---|
2884 | }
|
---|
2885 | if (extract_time(PyTuple_GET_ITEM(arg, 1),
|
---|
2886 | &mtime, &musec) == -1) {
|
---|
2887 | PyMem_Free(path);
|
---|
2888 | return NULL;
|
---|
2889 | }
|
---|
2890 | ATIME = atime;
|
---|
2891 | MTIME = mtime;
|
---|
2892 | #ifdef HAVE_UTIMES
|
---|
2893 | buf[0].tv_usec = ausec;
|
---|
2894 | buf[1].tv_usec = musec;
|
---|
2895 | Py_BEGIN_ALLOW_THREADS
|
---|
2896 | res = utimes(path, buf);
|
---|
2897 | Py_END_ALLOW_THREADS
|
---|
2898 | #else
|
---|
2899 | Py_BEGIN_ALLOW_THREADS
|
---|
2900 | res = utime(path, UTIME_ARG);
|
---|
2901 | Py_END_ALLOW_THREADS
|
---|
2902 | #endif /* HAVE_UTIMES */
|
---|
2903 | }
|
---|
2904 | if (res < 0) {
|
---|
2905 | return posix_error_with_allocated_filename(path);
|
---|
2906 | }
|
---|
2907 | PyMem_Free(path);
|
---|
2908 | Py_INCREF(Py_None);
|
---|
2909 | return Py_None;
|
---|
2910 | #undef UTIME_ARG
|
---|
2911 | #undef ATIME
|
---|
2912 | #undef MTIME
|
---|
2913 | #endif /* Py_WIN_WIDE_FILENAMES */
|
---|
2914 | }
|
---|
2915 |
|
---|
2916 |
|
---|
2917 | /* Process operations */
|
---|
2918 |
|
---|
2919 | PyDoc_STRVAR(posix__exit__doc__,
|
---|
2920 | "_exit(status)\n\n\
|
---|
2921 | Exit to the system with specified status, without normal exit processing.");
|
---|
2922 |
|
---|
2923 | static PyObject *
|
---|
2924 | posix__exit(PyObject *self, PyObject *args)
|
---|
2925 | {
|
---|
2926 | int sts;
|
---|
2927 | if (!PyArg_ParseTuple(args, "i:_exit", &sts))
|
---|
2928 | return NULL;
|
---|
2929 | _exit(sts);
|
---|
2930 | return NULL; /* Make gcc -Wall happy */
|
---|
2931 | }
|
---|
2932 |
|
---|
2933 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
|
---|
2934 | static void
|
---|
2935 | free_string_array(char **array, Py_ssize_t count)
|
---|
2936 | {
|
---|
2937 | Py_ssize_t i;
|
---|
2938 | for (i = 0; i < count; i++)
|
---|
2939 | PyMem_Free(array[i]);
|
---|
2940 | PyMem_DEL(array);
|
---|
2941 | }
|
---|
2942 | #endif
|
---|
2943 |
|
---|
2944 |
|
---|
2945 | #ifdef HAVE_EXECV
|
---|
2946 | PyDoc_STRVAR(posix_execv__doc__,
|
---|
2947 | "execv(path, args)\n\n\
|
---|
2948 | Execute an executable path with arguments, replacing current process.\n\
|
---|
2949 | \n\
|
---|
2950 | path: path of executable file\n\
|
---|
2951 | args: tuple or list of strings");
|
---|
2952 |
|
---|
2953 | static PyObject *
|
---|
2954 | posix_execv(PyObject *self, PyObject *args)
|
---|
2955 | {
|
---|
2956 | char *path;
|
---|
2957 | PyObject *argv;
|
---|
2958 | char **argvlist;
|
---|
2959 | Py_ssize_t i, argc;
|
---|
2960 | PyObject *(*getitem)(PyObject *, Py_ssize_t);
|
---|
2961 |
|
---|
2962 | /* execv has two arguments: (path, argv), where
|
---|
2963 | argv is a list or tuple of strings. */
|
---|
2964 |
|
---|
2965 | if (!PyArg_ParseTuple(args, "etO:execv",
|
---|
2966 | Py_FileSystemDefaultEncoding,
|
---|
2967 | &path, &argv))
|
---|
2968 | return NULL;
|
---|
2969 | if (PyList_Check(argv)) {
|
---|
2970 | argc = PyList_Size(argv);
|
---|
2971 | getitem = PyList_GetItem;
|
---|
2972 | }
|
---|
2973 | else if (PyTuple_Check(argv)) {
|
---|
2974 | argc = PyTuple_Size(argv);
|
---|
2975 | getitem = PyTuple_GetItem;
|
---|
2976 | }
|
---|
2977 | else {
|
---|
2978 | PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
|
---|
2979 | PyMem_Free(path);
|
---|
2980 | return NULL;
|
---|
2981 | }
|
---|
2982 |
|
---|
2983 | argvlist = PyMem_NEW(char *, argc+1);
|
---|
2984 | if (argvlist == NULL) {
|
---|
2985 | PyMem_Free(path);
|
---|
2986 | return PyErr_NoMemory();
|
---|
2987 | }
|
---|
2988 | for (i = 0; i < argc; i++) {
|
---|
2989 | if (!PyArg_Parse((*getitem)(argv, i), "et",
|
---|
2990 | Py_FileSystemDefaultEncoding,
|
---|
2991 | &argvlist[i])) {
|
---|
2992 | free_string_array(argvlist, i);
|
---|
2993 | PyErr_SetString(PyExc_TypeError,
|
---|
2994 | "execv() arg 2 must contain only strings");
|
---|
2995 | PyMem_Free(path);
|
---|
2996 | return NULL;
|
---|
2997 |
|
---|
2998 | }
|
---|
2999 | }
|
---|
3000 | argvlist[argc] = NULL;
|
---|
3001 |
|
---|
3002 | execv(path, argvlist);
|
---|
3003 |
|
---|
3004 | /* If we get here it's definitely an error */
|
---|
3005 |
|
---|
3006 | free_string_array(argvlist, argc);
|
---|
3007 | PyMem_Free(path);
|
---|
3008 | return posix_error();
|
---|
3009 | }
|
---|
3010 |
|
---|
3011 |
|
---|
3012 | PyDoc_STRVAR(posix_execve__doc__,
|
---|
3013 | "execve(path, args, env)\n\n\
|
---|
3014 | Execute a path with arguments and environment, replacing current process.\n\
|
---|
3015 | \n\
|
---|
3016 | path: path of executable file\n\
|
---|
3017 | args: tuple or list of arguments\n\
|
---|
3018 | env: dictionary of strings mapping to strings");
|
---|
3019 |
|
---|
3020 | static PyObject *
|
---|
3021 | posix_execve(PyObject *self, PyObject *args)
|
---|
3022 | {
|
---|
3023 | char *path;
|
---|
3024 | PyObject *argv, *env;
|
---|
3025 | char **argvlist;
|
---|
3026 | char **envlist;
|
---|
3027 | PyObject *key, *val, *keys=NULL, *vals=NULL;
|
---|
3028 | Py_ssize_t i, pos, argc, envc;
|
---|
3029 | PyObject *(*getitem)(PyObject *, Py_ssize_t);
|
---|
3030 | Py_ssize_t lastarg = 0;
|
---|
3031 |
|
---|
3032 | /* execve has three arguments: (path, argv, env), where
|
---|
3033 | argv is a list or tuple of strings and env is a dictionary
|
---|
3034 | like posix.environ. */
|
---|
3035 |
|
---|
3036 | if (!PyArg_ParseTuple(args, "etOO:execve",
|
---|
3037 | Py_FileSystemDefaultEncoding,
|
---|
3038 | &path, &argv, &env))
|
---|
3039 | return NULL;
|
---|
3040 | if (PyList_Check(argv)) {
|
---|
3041 | argc = PyList_Size(argv);
|
---|
3042 | getitem = PyList_GetItem;
|
---|
3043 | }
|
---|
3044 | else if (PyTuple_Check(argv)) {
|
---|
3045 | argc = PyTuple_Size(argv);
|
---|
3046 | getitem = PyTuple_GetItem;
|
---|
3047 | }
|
---|
3048 | else {
|
---|
3049 | PyErr_SetString(PyExc_TypeError,
|
---|
3050 | "execve() arg 2 must be a tuple or list");
|
---|
3051 | goto fail_0;
|
---|
3052 | }
|
---|
3053 | if (!PyMapping_Check(env)) {
|
---|
3054 | PyErr_SetString(PyExc_TypeError,
|
---|
3055 | "execve() arg 3 must be a mapping object");
|
---|
3056 | goto fail_0;
|
---|
3057 | }
|
---|
3058 |
|
---|
3059 | argvlist = PyMem_NEW(char *, argc+1);
|
---|
3060 | if (argvlist == NULL) {
|
---|
3061 | PyErr_NoMemory();
|
---|
3062 | goto fail_0;
|
---|
3063 | }
|
---|
3064 | for (i = 0; i < argc; i++) {
|
---|
3065 | if (!PyArg_Parse((*getitem)(argv, i),
|
---|
3066 | "et;execve() arg 2 must contain only strings",
|
---|
3067 | Py_FileSystemDefaultEncoding,
|
---|
3068 | &argvlist[i]))
|
---|
3069 | {
|
---|
3070 | lastarg = i;
|
---|
3071 | goto fail_1;
|
---|
3072 | }
|
---|
3073 | }
|
---|
3074 | lastarg = argc;
|
---|
3075 | argvlist[argc] = NULL;
|
---|
3076 |
|
---|
3077 | i = PyMapping_Size(env);
|
---|
3078 | if (i < 0)
|
---|
3079 | goto fail_1;
|
---|
3080 | envlist = PyMem_NEW(char *, i + 1);
|
---|
3081 | if (envlist == NULL) {
|
---|
3082 | PyErr_NoMemory();
|
---|
3083 | goto fail_1;
|
---|
3084 | }
|
---|
3085 | envc = 0;
|
---|
3086 | keys = PyMapping_Keys(env);
|
---|
3087 | vals = PyMapping_Values(env);
|
---|
3088 | if (!keys || !vals)
|
---|
3089 | goto fail_2;
|
---|
3090 | if (!PyList_Check(keys) || !PyList_Check(vals)) {
|
---|
3091 | PyErr_SetString(PyExc_TypeError,
|
---|
3092 | "execve(): env.keys() or env.values() is not a list");
|
---|
3093 | goto fail_2;
|
---|
3094 | }
|
---|
3095 |
|
---|
3096 | for (pos = 0; pos < i; pos++) {
|
---|
3097 | char *p, *k, *v;
|
---|
3098 | size_t len;
|
---|
3099 |
|
---|
3100 | key = PyList_GetItem(keys, pos);
|
---|
3101 | val = PyList_GetItem(vals, pos);
|
---|
3102 | if (!key || !val)
|
---|
3103 | goto fail_2;
|
---|
3104 |
|
---|
3105 | if (!PyArg_Parse(
|
---|
3106 | key,
|
---|
3107 | "s;execve() arg 3 contains a non-string key",
|
---|
3108 | &k) ||
|
---|
3109 | !PyArg_Parse(
|
---|
3110 | val,
|
---|
3111 | "s;execve() arg 3 contains a non-string value",
|
---|
3112 | &v))
|
---|
3113 | {
|
---|
3114 | goto fail_2;
|
---|
3115 | }
|
---|
3116 |
|
---|
3117 | #if defined(PYOS_OS2)
|
---|
3118 | /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
|
---|
3119 | if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
|
---|
3120 | #endif
|
---|
3121 | len = PyString_Size(key) + PyString_Size(val) + 2;
|
---|
3122 | p = PyMem_NEW(char, len);
|
---|
3123 | if (p == NULL) {
|
---|
3124 | PyErr_NoMemory();
|
---|
3125 | goto fail_2;
|
---|
3126 | }
|
---|
3127 | PyOS_snprintf(p, len, "%s=%s", k, v);
|
---|
3128 | envlist[envc++] = p;
|
---|
3129 | #if defined(PYOS_OS2)
|
---|
3130 | }
|
---|
3131 | #endif
|
---|
3132 | }
|
---|
3133 | envlist[envc] = 0;
|
---|
3134 |
|
---|
3135 | execve(path, argvlist, envlist);
|
---|
3136 |
|
---|
3137 | /* If we get here it's definitely an error */
|
---|
3138 |
|
---|
3139 | (void) posix_error();
|
---|
3140 |
|
---|
3141 | fail_2:
|
---|
3142 | while (--envc >= 0)
|
---|
3143 | PyMem_DEL(envlist[envc]);
|
---|
3144 | PyMem_DEL(envlist);
|
---|
3145 | fail_1:
|
---|
3146 | free_string_array(argvlist, lastarg);
|
---|
3147 | Py_XDECREF(vals);
|
---|
3148 | Py_XDECREF(keys);
|
---|
3149 | fail_0:
|
---|
3150 | PyMem_Free(path);
|
---|
3151 | return NULL;
|
---|
3152 | }
|
---|
3153 | #endif /* HAVE_EXECV */
|
---|
3154 |
|
---|
3155 |
|
---|
3156 | #ifdef HAVE_SPAWNV
|
---|
3157 | PyDoc_STRVAR(posix_spawnv__doc__,
|
---|
3158 | "spawnv(mode, path, args)\n\n\
|
---|
3159 | Execute the program 'path' in a new process.\n\
|
---|
3160 | \n\
|
---|
3161 | mode: mode of process creation\n\
|
---|
3162 | path: path of executable file\n\
|
---|
3163 | args: tuple or list of strings");
|
---|
3164 |
|
---|
3165 | static PyObject *
|
---|
3166 | posix_spawnv(PyObject *self, PyObject *args)
|
---|
3167 | {
|
---|
3168 | char *path;
|
---|
3169 | PyObject *argv;
|
---|
3170 | char **argvlist;
|
---|
3171 | int mode, i;
|
---|
3172 | Py_ssize_t argc;
|
---|
3173 | Py_intptr_t spawnval;
|
---|
3174 | PyObject *(*getitem)(PyObject *, Py_ssize_t);
|
---|
3175 |
|
---|
3176 | /* spawnv has three arguments: (mode, path, argv), where
|
---|
3177 | argv is a list or tuple of strings. */
|
---|
3178 |
|
---|
3179 | if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode,
|
---|
3180 | Py_FileSystemDefaultEncoding,
|
---|
3181 | &path, &argv))
|
---|
3182 | return NULL;
|
---|
3183 | if (PyList_Check(argv)) {
|
---|
3184 | argc = PyList_Size(argv);
|
---|
3185 | getitem = PyList_GetItem;
|
---|
3186 | }
|
---|
3187 | else if (PyTuple_Check(argv)) {
|
---|
3188 | argc = PyTuple_Size(argv);
|
---|
3189 | getitem = PyTuple_GetItem;
|
---|
3190 | }
|
---|
3191 | else {
|
---|
3192 | PyErr_SetString(PyExc_TypeError,
|
---|
3193 | "spawnv() arg 2 must be a tuple or list");
|
---|
3194 | PyMem_Free(path);
|
---|
3195 | return NULL;
|
---|
3196 | }
|
---|
3197 |
|
---|
3198 | argvlist = PyMem_NEW(char *, argc+1);
|
---|
3199 | if (argvlist == NULL) {
|
---|
3200 | PyMem_Free(path);
|
---|
3201 | return PyErr_NoMemory();
|
---|
3202 | }
|
---|
3203 | for (i = 0; i < argc; i++) {
|
---|
3204 | if (!PyArg_Parse((*getitem)(argv, i), "et",
|
---|
3205 | Py_FileSystemDefaultEncoding,
|
---|
3206 | &argvlist[i])) {
|
---|
3207 | free_string_array(argvlist, i);
|
---|
3208 | PyErr_SetString(
|
---|
3209 | PyExc_TypeError,
|
---|
3210 | "spawnv() arg 2 must contain only strings");
|
---|
3211 | PyMem_Free(path);
|
---|
3212 | return NULL;
|
---|
3213 | }
|
---|
3214 | }
|
---|
3215 | argvlist[argc] = NULL;
|
---|
3216 |
|
---|
3217 | #if defined(PYOS_OS2) && defined(PYCC_GCC)
|
---|
3218 | Py_BEGIN_ALLOW_THREADS
|
---|
3219 | spawnval = spawnv(mode, path, argvlist);
|
---|
3220 | Py_END_ALLOW_THREADS
|
---|
3221 | #else
|
---|
3222 | if (mode == _OLD_P_OVERLAY)
|
---|
3223 | mode = _P_OVERLAY;
|
---|
3224 |
|
---|
3225 | Py_BEGIN_ALLOW_THREADS
|
---|
3226 | spawnval = _spawnv(mode, path, argvlist);
|
---|
3227 | Py_END_ALLOW_THREADS
|
---|
3228 | #endif
|
---|
3229 |
|
---|
3230 | free_string_array(argvlist, argc);
|
---|
3231 | PyMem_Free(path);
|
---|
3232 |
|
---|
3233 | if (spawnval == -1)
|
---|
3234 | return posix_error();
|
---|
3235 | else
|
---|
3236 | #if SIZEOF_LONG == SIZEOF_VOID_P
|
---|
3237 | return Py_BuildValue("l", (long) spawnval);
|
---|
3238 | #else
|
---|
3239 | return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
|
---|
3240 | #endif
|
---|
3241 | }
|
---|
3242 |
|
---|
3243 |
|
---|
3244 | PyDoc_STRVAR(posix_spawnve__doc__,
|
---|
3245 | "spawnve(mode, path, args, env)\n\n\
|
---|
3246 | Execute the program 'path' in a new process.\n\
|
---|
3247 | \n\
|
---|
3248 | mode: mode of process creation\n\
|
---|
3249 | path: path of executable file\n\
|
---|
3250 | args: tuple or list of arguments\n\
|
---|
3251 | env: dictionary of strings mapping to strings");
|
---|
3252 |
|
---|
3253 | static PyObject *
|
---|
3254 | posix_spawnve(PyObject *self, PyObject *args)
|
---|
3255 | {
|
---|
3256 | char *path;
|
---|
3257 | PyObject *argv, *env;
|
---|
3258 | char **argvlist;
|
---|
3259 | char **envlist;
|
---|
3260 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
|
---|
3261 | int mode, pos, envc;
|
---|
3262 | Py_ssize_t argc, i;
|
---|
3263 | Py_intptr_t spawnval;
|
---|
3264 | PyObject *(*getitem)(PyObject *, Py_ssize_t);
|
---|
3265 | Py_ssize_t lastarg = 0;
|
---|
3266 |
|
---|
3267 | /* spawnve has four arguments: (mode, path, argv, env), where
|
---|
3268 | argv is a list or tuple of strings and env is a dictionary
|
---|
3269 | like posix.environ. */
|
---|
3270 |
|
---|
3271 | if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode,
|
---|
3272 | Py_FileSystemDefaultEncoding,
|
---|
3273 | &path, &argv, &env))
|
---|
3274 | return NULL;
|
---|
3275 | if (PyList_Check(argv)) {
|
---|
3276 | argc = PyList_Size(argv);
|
---|
3277 | getitem = PyList_GetItem;
|
---|
3278 | }
|
---|
3279 | else if (PyTuple_Check(argv)) {
|
---|
3280 | argc = PyTuple_Size(argv);
|
---|
3281 | getitem = PyTuple_GetItem;
|
---|
3282 | }
|
---|
3283 | else {
|
---|
3284 | PyErr_SetString(PyExc_TypeError,
|
---|
3285 | "spawnve() arg 2 must be a tuple or list");
|
---|
3286 | goto fail_0;
|
---|
3287 | }
|
---|
3288 | if (!PyMapping_Check(env)) {
|
---|
3289 | PyErr_SetString(PyExc_TypeError,
|
---|
3290 | "spawnve() arg 3 must be a mapping object");
|
---|
3291 | goto fail_0;
|
---|
3292 | }
|
---|
3293 |
|
---|
3294 | argvlist = PyMem_NEW(char *, argc+1);
|
---|
3295 | if (argvlist == NULL) {
|
---|
3296 | PyErr_NoMemory();
|
---|
3297 | goto fail_0;
|
---|
3298 | }
|
---|
3299 | for (i = 0; i < argc; i++) {
|
---|
3300 | if (!PyArg_Parse((*getitem)(argv, i),
|
---|
3301 | "et;spawnve() arg 2 must contain only strings",
|
---|
3302 | Py_FileSystemDefaultEncoding,
|
---|
3303 | &argvlist[i]))
|
---|
3304 | {
|
---|
3305 | lastarg = i;
|
---|
3306 | goto fail_1;
|
---|
3307 | }
|
---|
3308 | }
|
---|
3309 | lastarg = argc;
|
---|
3310 | argvlist[argc] = NULL;
|
---|
3311 |
|
---|
3312 | i = PyMapping_Size(env);
|
---|
3313 | if (i < 0)
|
---|
3314 | goto fail_1;
|
---|
3315 | envlist = PyMem_NEW(char *, i + 1);
|
---|
3316 | if (envlist == NULL) {
|
---|
3317 | PyErr_NoMemory();
|
---|
3318 | goto fail_1;
|
---|
3319 | }
|
---|
3320 | envc = 0;
|
---|
3321 | keys = PyMapping_Keys(env);
|
---|
3322 | vals = PyMapping_Values(env);
|
---|
3323 | if (!keys || !vals)
|
---|
3324 | goto fail_2;
|
---|
3325 | if (!PyList_Check(keys) || !PyList_Check(vals)) {
|
---|
3326 | PyErr_SetString(PyExc_TypeError,
|
---|
3327 | "spawnve(): env.keys() or env.values() is not a list");
|
---|
3328 | goto fail_2;
|
---|
3329 | }
|
---|
3330 |
|
---|
3331 | for (pos = 0; pos < i; pos++) {
|
---|
3332 | char *p, *k, *v;
|
---|
3333 | size_t len;
|
---|
3334 |
|
---|
3335 | key = PyList_GetItem(keys, pos);
|
---|
3336 | val = PyList_GetItem(vals, pos);
|
---|
3337 | if (!key || !val)
|
---|
3338 | goto fail_2;
|
---|
3339 |
|
---|
3340 | if (!PyArg_Parse(
|
---|
3341 | key,
|
---|
3342 | "s;spawnve() arg 3 contains a non-string key",
|
---|
3343 | &k) ||
|
---|
3344 | !PyArg_Parse(
|
---|
3345 | val,
|
---|
3346 | "s;spawnve() arg 3 contains a non-string value",
|
---|
3347 | &v))
|
---|
3348 | {
|
---|
3349 | goto fail_2;
|
---|
3350 | }
|
---|
3351 | len = PyString_Size(key) + PyString_Size(val) + 2;
|
---|
3352 | p = PyMem_NEW(char, len);
|
---|
3353 | if (p == NULL) {
|
---|
3354 | PyErr_NoMemory();
|
---|
3355 | goto fail_2;
|
---|
3356 | }
|
---|
3357 | PyOS_snprintf(p, len, "%s=%s", k, v);
|
---|
3358 | envlist[envc++] = p;
|
---|
3359 | }
|
---|
3360 | envlist[envc] = 0;
|
---|
3361 |
|
---|
3362 | #if defined(PYOS_OS2) && defined(PYCC_GCC)
|
---|
3363 | Py_BEGIN_ALLOW_THREADS
|
---|
3364 | spawnval = spawnve(mode, path, argvlist, envlist);
|
---|
3365 | Py_END_ALLOW_THREADS
|
---|
3366 | #else
|
---|
3367 | if (mode == _OLD_P_OVERLAY)
|
---|
3368 | mode = _P_OVERLAY;
|
---|
3369 |
|
---|
3370 | Py_BEGIN_ALLOW_THREADS
|
---|
3371 | spawnval = _spawnve(mode, path, argvlist, envlist);
|
---|
3372 | Py_END_ALLOW_THREADS
|
---|
3373 | #endif
|
---|
3374 |
|
---|
3375 | if (spawnval == -1)
|
---|
3376 | (void) posix_error();
|
---|
3377 | else
|
---|
3378 | #if SIZEOF_LONG == SIZEOF_VOID_P
|
---|
3379 | res = Py_BuildValue("l", (long) spawnval);
|
---|
3380 | #else
|
---|
3381 | res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
|
---|
3382 | #endif
|
---|
3383 |
|
---|
3384 | fail_2:
|
---|
3385 | while (--envc >= 0)
|
---|
3386 | PyMem_DEL(envlist[envc]);
|
---|
3387 | PyMem_DEL(envlist);
|
---|
3388 | fail_1:
|
---|
3389 | free_string_array(argvlist, lastarg);
|
---|
3390 | Py_XDECREF(vals);
|
---|
3391 | Py_XDECREF(keys);
|
---|
3392 | fail_0:
|
---|
3393 | PyMem_Free(path);
|
---|
3394 | return res;
|
---|
3395 | }
|
---|
3396 |
|
---|
3397 | /* OS/2 supports spawnvp & spawnvpe natively */
|
---|
3398 | #if defined(PYOS_OS2)
|
---|
3399 | PyDoc_STRVAR(posix_spawnvp__doc__,
|
---|
3400 | "spawnvp(mode, file, args)\n\n\
|
---|
3401 | Execute the program 'file' in a new process, using the environment\n\
|
---|
3402 | search path to find the file.\n\
|
---|
3403 | \n\
|
---|
3404 | mode: mode of process creation\n\
|
---|
3405 | file: executable file name\n\
|
---|
3406 | args: tuple or list of strings");
|
---|
3407 |
|
---|
3408 | static PyObject *
|
---|
3409 | posix_spawnvp(PyObject *self, PyObject *args)
|
---|
3410 | {
|
---|
3411 | char *path;
|
---|
3412 | PyObject *argv;
|
---|
3413 | char **argvlist;
|
---|
3414 | int mode, i, argc;
|
---|
3415 | Py_intptr_t spawnval;
|
---|
3416 | PyObject *(*getitem)(PyObject *, Py_ssize_t);
|
---|
3417 |
|
---|
3418 | /* spawnvp has three arguments: (mode, path, argv), where
|
---|
3419 | argv is a list or tuple of strings. */
|
---|
3420 |
|
---|
3421 | if (!PyArg_ParseTuple(args, "ietO:spawnvp", &mode,
|
---|
3422 | Py_FileSystemDefaultEncoding,
|
---|
3423 | &path, &argv))
|
---|
3424 | return NULL;
|
---|
3425 | if (PyList_Check(argv)) {
|
---|
3426 | argc = PyList_Size(argv);
|
---|
3427 | getitem = PyList_GetItem;
|
---|
3428 | }
|
---|
3429 | else if (PyTuple_Check(argv)) {
|
---|
3430 | argc = PyTuple_Size(argv);
|
---|
3431 | getitem = PyTuple_GetItem;
|
---|
3432 | }
|
---|
3433 | else {
|
---|
3434 | PyErr_SetString(PyExc_TypeError,
|
---|
3435 | "spawnvp() arg 2 must be a tuple or list");
|
---|
3436 | PyMem_Free(path);
|
---|
3437 | return NULL;
|
---|
3438 | }
|
---|
3439 |
|
---|
3440 | argvlist = PyMem_NEW(char *, argc+1);
|
---|
3441 | if (argvlist == NULL) {
|
---|
3442 | PyMem_Free(path);
|
---|
3443 | return PyErr_NoMemory();
|
---|
3444 | }
|
---|
3445 | for (i = 0; i < argc; i++) {
|
---|
3446 | if (!PyArg_Parse((*getitem)(argv, i), "et",
|
---|
3447 | Py_FileSystemDefaultEncoding,
|
---|
3448 | &argvlist[i])) {
|
---|
3449 | free_string_array(argvlist, i);
|
---|
3450 | PyErr_SetString(
|
---|
3451 | PyExc_TypeError,
|
---|
3452 | "spawnvp() arg 2 must contain only strings");
|
---|
3453 | PyMem_Free(path);
|
---|
3454 | return NULL;
|
---|
3455 | }
|
---|
3456 | }
|
---|
3457 | argvlist[argc] = NULL;
|
---|
3458 |
|
---|
3459 | Py_BEGIN_ALLOW_THREADS
|
---|
3460 | #if defined(PYCC_GCC)
|
---|
3461 | spawnval = spawnvp(mode, path, argvlist);
|
---|
3462 | #else
|
---|
3463 | spawnval = _spawnvp(mode, path, argvlist);
|
---|
3464 | #endif
|
---|
3465 | Py_END_ALLOW_THREADS
|
---|
3466 |
|
---|
3467 | free_string_array(argvlist, argc);
|
---|
3468 | PyMem_Free(path);
|
---|
3469 |
|
---|
3470 | if (spawnval == -1)
|
---|
3471 | return posix_error();
|
---|
3472 | else
|
---|
3473 | return Py_BuildValue("l", (long) spawnval);
|
---|
3474 | }
|
---|
3475 |
|
---|
3476 |
|
---|
3477 | PyDoc_STRVAR(posix_spawnvpe__doc__,
|
---|
3478 | "spawnvpe(mode, file, args, env)\n\n\
|
---|
3479 | Execute the program 'file' in a new process, using the environment\n\
|
---|
3480 | search path to find the file.\n\
|
---|
3481 | \n\
|
---|
3482 | mode: mode of process creation\n\
|
---|
3483 | file: executable file name\n\
|
---|
3484 | args: tuple or list of arguments\n\
|
---|
3485 | env: dictionary of strings mapping to strings");
|
---|
3486 |
|
---|
3487 | static PyObject *
|
---|
3488 | posix_spawnvpe(PyObject *self, PyObject *args)
|
---|
3489 | {
|
---|
3490 | char *path;
|
---|
3491 | PyObject *argv, *env;
|
---|
3492 | char **argvlist;
|
---|
3493 | char **envlist;
|
---|
3494 | PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
|
---|
3495 | int mode, i, pos, argc, envc;
|
---|
3496 | Py_intptr_t spawnval;
|
---|
3497 | PyObject *(*getitem)(PyObject *, Py_ssize_t);
|
---|
3498 | int lastarg = 0;
|
---|
3499 |
|
---|
3500 | /* spawnvpe has four arguments: (mode, path, argv, env), where
|
---|
3501 | argv is a list or tuple of strings and env is a dictionary
|
---|
3502 | like posix.environ. */
|
---|
3503 |
|
---|
3504 | if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
|
---|
3505 | Py_FileSystemDefaultEncoding,
|
---|
3506 | &path, &argv, &env))
|
---|
3507 | return NULL;
|
---|
3508 | if (PyList_Check(argv)) {
|
---|
3509 | argc = PyList_Size(argv);
|
---|
3510 | getitem = PyList_GetItem;
|
---|
3511 | }
|
---|
3512 | else if (PyTuple_Check(argv)) {
|
---|
3513 | argc = PyTuple_Size(argv);
|
---|
3514 | getitem = PyTuple_GetItem;
|
---|
3515 | }
|
---|
3516 | else {
|
---|
3517 | PyErr_SetString(PyExc_TypeError,
|
---|
3518 | "spawnvpe() arg 2 must be a tuple or list");
|
---|
3519 | goto fail_0;
|
---|
3520 | }
|
---|
3521 | if (!PyMapping_Check(env)) {
|
---|
3522 | PyErr_SetString(PyExc_TypeError,
|
---|
3523 | "spawnvpe() arg 3 must be a mapping object");
|
---|
3524 | goto fail_0;
|
---|
3525 | }
|
---|
3526 |
|
---|
3527 | argvlist = PyMem_NEW(char *, argc+1);
|
---|
3528 | if (argvlist == NULL) {
|
---|
3529 | PyErr_NoMemory();
|
---|
3530 | goto fail_0;
|
---|
3531 | }
|
---|
3532 | for (i = 0; i < argc; i++) {
|
---|
3533 | if (!PyArg_Parse((*getitem)(argv, i),
|
---|
3534 | "et;spawnvpe() arg 2 must contain only strings",
|
---|
3535 | Py_FileSystemDefaultEncoding,
|
---|
3536 | &argvlist[i]))
|
---|
3537 | {
|
---|
3538 | lastarg = i;
|
---|
3539 | goto fail_1;
|
---|
3540 | }
|
---|
3541 | }
|
---|
3542 | lastarg = argc;
|
---|
3543 | argvlist[argc] = NULL;
|
---|
3544 |
|
---|
3545 | i = PyMapping_Size(env);
|
---|
3546 | if (i < 0)
|
---|
3547 | goto fail_1;
|
---|
3548 | envlist = PyMem_NEW(char *, i + 1);
|
---|
3549 | if (envlist == NULL) {
|
---|
3550 | PyErr_NoMemory();
|
---|
3551 | goto fail_1;
|
---|
3552 | }
|
---|
3553 | envc = 0;
|
---|
3554 | keys = PyMapping_Keys(env);
|
---|
3555 | vals = PyMapping_Values(env);
|
---|
3556 | if (!keys || !vals)
|
---|
3557 | goto fail_2;
|
---|
3558 | if (!PyList_Check(keys) || !PyList_Check(vals)) {
|
---|
3559 | PyErr_SetString(PyExc_TypeError,
|
---|
3560 | "spawnvpe(): env.keys() or env.values() is not a list");
|
---|
3561 | goto fail_2;
|
---|
3562 | }
|
---|
3563 |
|
---|
3564 | for (pos = 0; pos < i; pos++) {
|
---|
3565 | char *p, *k, *v;
|
---|
3566 | size_t len;
|
---|
3567 |
|
---|
3568 | key = PyList_GetItem(keys, pos);
|
---|
3569 | val = PyList_GetItem(vals, pos);
|
---|
3570 | if (!key || !val)
|
---|
3571 | goto fail_2;
|
---|
3572 |
|
---|
3573 | if (!PyArg_Parse(
|
---|
3574 | key,
|
---|
3575 | "s;spawnvpe() arg 3 contains a non-string key",
|
---|
3576 | &k) ||
|
---|
3577 | !PyArg_Parse(
|
---|
3578 | val,
|
---|
3579 | "s;spawnvpe() arg 3 contains a non-string value",
|
---|
3580 | &v))
|
---|
3581 | {
|
---|
3582 | goto fail_2;
|
---|
3583 | }
|
---|
3584 | len = PyString_Size(key) + PyString_Size(val) + 2;
|
---|
3585 | p = PyMem_NEW(char, len);
|
---|
3586 | if (p == NULL) {
|
---|
3587 | PyErr_NoMemory();
|
---|
3588 | goto fail_2;
|
---|
3589 | }
|
---|
3590 | PyOS_snprintf(p, len, "%s=%s", k, v);
|
---|
3591 | envlist[envc++] = p;
|
---|
3592 | }
|
---|
3593 | envlist[envc] = 0;
|
---|
3594 |
|
---|
3595 | Py_BEGIN_ALLOW_THREADS
|
---|
3596 | #if defined(PYCC_GCC)
|
---|
3597 | spawnval = spawnvpe(mode, path, argvlist, envlist);
|
---|
3598 | #else
|
---|
3599 | spawnval = _spawnvpe(mode, path, argvlist, envlist);
|
---|
3600 | #endif
|
---|
3601 | Py_END_ALLOW_THREADS
|
---|
3602 |
|
---|
3603 | if (spawnval == -1)
|
---|
3604 | (void) posix_error();
|
---|
3605 | else
|
---|
3606 | res = Py_BuildValue("l", (long) spawnval);
|
---|
3607 |
|
---|
3608 | fail_2:
|
---|
3609 | while (--envc >= 0)
|
---|
3610 | PyMem_DEL(envlist[envc]);
|
---|
3611 | PyMem_DEL(envlist);
|
---|
3612 | fail_1:
|
---|
3613 | free_string_array(argvlist, lastarg);
|
---|
3614 | Py_XDECREF(vals);
|
---|
3615 | Py_XDECREF(keys);
|
---|
3616 | fail_0:
|
---|
3617 | PyMem_Free(path);
|
---|
3618 | return res;
|
---|
3619 | }
|
---|
3620 | #endif /* PYOS_OS2 */
|
---|
3621 | #endif /* HAVE_SPAWNV */
|
---|
3622 |
|
---|
3623 |
|
---|
3624 | #ifdef HAVE_FORK1
|
---|
3625 | PyDoc_STRVAR(posix_fork1__doc__,
|
---|
3626 | "fork1() -> pid\n\n\
|
---|
3627 | Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
|
---|
3628 | \n\
|
---|
3629 | Return 0 to child process and PID of child to parent process.");
|
---|
3630 |
|
---|
3631 | static PyObject *
|
---|
3632 | posix_fork1(PyObject *self, PyObject *noargs)
|
---|
3633 | {
|
---|
3634 | pid_t pid;
|
---|
3635 | int result = 0;
|
---|
3636 | _PyImport_AcquireLock();
|
---|
3637 | pid = fork1();
|
---|
3638 | if (pid == 0) {
|
---|
3639 | /* child: this clobbers and resets the import lock. */
|
---|
3640 | PyOS_AfterFork();
|
---|
3641 | } else {
|
---|
3642 | /* parent: release the import lock. */
|
---|
3643 | result = _PyImport_ReleaseLock();
|
---|
3644 | }
|
---|
3645 | if (pid == -1)
|
---|
3646 | return posix_error();
|
---|
3647 | if (result < 0) {
|
---|
3648 | /* Don't clobber the OSError if the fork failed. */
|
---|
3649 | PyErr_SetString(PyExc_RuntimeError,
|
---|
3650 | "not holding the import lock");
|
---|
3651 | return NULL;
|
---|
3652 | }
|
---|
3653 | return PyLong_FromPid(pid);
|
---|
3654 | }
|
---|
3655 | #endif
|
---|
3656 |
|
---|
3657 |
|
---|
3658 | #ifdef HAVE_FORK
|
---|
3659 | PyDoc_STRVAR(posix_fork__doc__,
|
---|
3660 | "fork() -> pid\n\n\
|
---|
3661 | Fork a child process.\n\
|
---|
3662 | Return 0 to child process and PID of child to parent process.");
|
---|
3663 |
|
---|
3664 | static PyObject *
|
---|
3665 | posix_fork(PyObject *self, PyObject *noargs)
|
---|
3666 | {
|
---|
3667 | pid_t pid;
|
---|
3668 | int result = 0;
|
---|
3669 | _PyImport_AcquireLock();
|
---|
3670 | pid = fork();
|
---|
3671 | if (pid == 0) {
|
---|
3672 | /* child: this clobbers and resets the import lock. */
|
---|
3673 | PyOS_AfterFork();
|
---|
3674 | } else {
|
---|
3675 | /* parent: release the import lock. */
|
---|
3676 | result = _PyImport_ReleaseLock();
|
---|
3677 | }
|
---|
3678 | if (pid == -1)
|
---|
3679 | return posix_error();
|
---|
3680 | if (result < 0) {
|
---|
3681 | /* Don't clobber the OSError if the fork failed. */
|
---|
3682 | PyErr_SetString(PyExc_RuntimeError,
|
---|
3683 | "not holding the import lock");
|
---|
3684 | return NULL;
|
---|
3685 | }
|
---|
3686 | return PyLong_FromPid(pid);
|
---|
3687 | }
|
---|
3688 | #endif
|
---|
3689 |
|
---|
3690 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
|
---|
3691 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
|
---|
3692 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
|
---|
3693 | #define DEV_PTY_FILE "/dev/ptc"
|
---|
3694 | #define HAVE_DEV_PTMX
|
---|
3695 | #else
|
---|
3696 | #define DEV_PTY_FILE "/dev/ptmx"
|
---|
3697 | #endif
|
---|
3698 |
|
---|
3699 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
|
---|
3700 | #ifdef HAVE_PTY_H
|
---|
3701 | #include <pty.h>
|
---|
3702 | #else
|
---|
3703 | #ifdef HAVE_LIBUTIL_H
|
---|
3704 | #include <libutil.h>
|
---|
3705 | #endif /* HAVE_LIBUTIL_H */
|
---|
3706 | #endif /* HAVE_PTY_H */
|
---|
3707 | #ifdef HAVE_STROPTS_H
|
---|
3708 | #include <stropts.h>
|
---|
3709 | #endif
|
---|
3710 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
|
---|
3711 |
|
---|
3712 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
|
---|
3713 | PyDoc_STRVAR(posix_openpty__doc__,
|
---|
3714 | "openpty() -> (master_fd, slave_fd)\n\n\
|
---|
3715 | Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
|
---|
3716 |
|
---|
3717 | static PyObject *
|
---|
3718 | posix_openpty(PyObject *self, PyObject *noargs)
|
---|
3719 | {
|
---|
3720 | int master_fd, slave_fd;
|
---|
3721 | #ifndef HAVE_OPENPTY
|
---|
3722 | char * slave_name;
|
---|
3723 | #endif
|
---|
3724 | #if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
|
---|
3725 | PyOS_sighandler_t sig_saved;
|
---|
3726 | #ifdef sun
|
---|
3727 | extern char *ptsname(int fildes);
|
---|
3728 | #endif
|
---|
3729 | #endif
|
---|
3730 |
|
---|
3731 | #ifdef HAVE_OPENPTY
|
---|
3732 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
|
---|
3733 | return posix_error();
|
---|
3734 | #elif defined(HAVE__GETPTY)
|
---|
3735 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
|
---|
3736 | if (slave_name == NULL)
|
---|
3737 | return posix_error();
|
---|
3738 |
|
---|
3739 | slave_fd = open(slave_name, O_RDWR);
|
---|
3740 | if (slave_fd < 0)
|
---|
3741 | return posix_error();
|
---|
3742 | #else
|
---|
3743 | master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
|
---|
3744 | if (master_fd < 0)
|
---|
3745 | return posix_error();
|
---|
3746 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
|
---|
3747 | /* change permission of slave */
|
---|
3748 | if (grantpt(master_fd) < 0) {
|
---|
3749 | PyOS_setsig(SIGCHLD, sig_saved);
|
---|
3750 | return posix_error();
|
---|
3751 | }
|
---|
3752 | /* unlock slave */
|
---|
3753 | if (unlockpt(master_fd) < 0) {
|
---|
3754 | PyOS_setsig(SIGCHLD, sig_saved);
|
---|
3755 | return posix_error();
|
---|
3756 | }
|
---|
3757 | PyOS_setsig(SIGCHLD, sig_saved);
|
---|
3758 | slave_name = ptsname(master_fd); /* get name of slave */
|
---|
3759 | if (slave_name == NULL)
|
---|
3760 | return posix_error();
|
---|
3761 | slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
|
---|
3762 | if (slave_fd < 0)
|
---|
3763 | return posix_error();
|
---|
3764 | #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
|
---|
3765 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
|
---|
3766 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
|
---|
3767 | #ifndef __hpux
|
---|
3768 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
|
---|
3769 | #endif /* __hpux */
|
---|
3770 | #endif /* HAVE_CYGWIN */
|
---|
3771 | #endif /* HAVE_OPENPTY */
|
---|
3772 |
|
---|
3773 | return Py_BuildValue("(ii)", master_fd, slave_fd);
|
---|
3774 |
|
---|
3775 | }
|
---|
3776 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
|
---|
3777 |
|
---|
3778 | #ifdef HAVE_FORKPTY
|
---|
3779 | PyDoc_STRVAR(posix_forkpty__doc__,
|
---|
3780 | "forkpty() -> (pid, master_fd)\n\n\
|
---|
3781 | Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
|
---|
3782 | Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
|
---|
3783 | To both, return fd of newly opened pseudo-terminal.\n");
|
---|
3784 |
|
---|
3785 | static PyObject *
|
---|
3786 | posix_forkpty(PyObject *self, PyObject *noargs)
|
---|
3787 | {
|
---|
3788 | int master_fd = -1, result = 0;
|
---|
3789 | pid_t pid;
|
---|
3790 |
|
---|
3791 | _PyImport_AcquireLock();
|
---|
3792 | pid = forkpty(&master_fd, NULL, NULL, NULL);
|
---|
3793 | if (pid == 0) {
|
---|
3794 | /* child: this clobbers and resets the import lock. */
|
---|
3795 | PyOS_AfterFork();
|
---|
3796 | } else {
|
---|
3797 | /* parent: release the import lock. */
|
---|
3798 | result = _PyImport_ReleaseLock();
|
---|
3799 | }
|
---|
3800 | if (pid == -1)
|
---|
3801 | return posix_error();
|
---|
3802 | if (result < 0) {
|
---|
3803 | /* Don't clobber the OSError if the fork failed. */
|
---|
3804 | PyErr_SetString(PyExc_RuntimeError,
|
---|
3805 | "not holding the import lock");
|
---|
3806 | return NULL;
|
---|
3807 | }
|
---|
3808 | return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
|
---|
3809 | }
|
---|
3810 | #endif
|
---|
3811 |
|
---|
3812 | #ifdef HAVE_GETEGID
|
---|
3813 | PyDoc_STRVAR(posix_getegid__doc__,
|
---|
3814 | "getegid() -> egid\n\n\
|
---|
3815 | Return the current process's effective group id.");
|
---|
3816 |
|
---|
3817 | static PyObject *
|
---|
3818 | posix_getegid(PyObject *self, PyObject *noargs)
|
---|
3819 | {
|
---|
3820 | return PyInt_FromLong((long)getegid());
|
---|
3821 | }
|
---|
3822 | #endif
|
---|
3823 |
|
---|
3824 |
|
---|
3825 | #ifdef HAVE_GETEUID
|
---|
3826 | PyDoc_STRVAR(posix_geteuid__doc__,
|
---|
3827 | "geteuid() -> euid\n\n\
|
---|
3828 | Return the current process's effective user id.");
|
---|
3829 |
|
---|
3830 | static PyObject *
|
---|
3831 | posix_geteuid(PyObject *self, PyObject *noargs)
|
---|
3832 | {
|
---|
3833 | return PyInt_FromLong((long)geteuid());
|
---|
3834 | }
|
---|
3835 | #endif
|
---|
3836 |
|
---|
3837 |
|
---|
3838 | #ifdef HAVE_GETGID
|
---|
3839 | PyDoc_STRVAR(posix_getgid__doc__,
|
---|
3840 | "getgid() -> gid\n\n\
|
---|
3841 | Return the current process's group id.");
|
---|
3842 |
|
---|
3843 | static PyObject *
|
---|
3844 | posix_getgid(PyObject *self, PyObject *noargs)
|
---|
3845 | {
|
---|
3846 | return PyInt_FromLong((long)getgid());
|
---|
3847 | }
|
---|
3848 | #endif
|
---|
3849 |
|
---|
3850 |
|
---|
3851 | PyDoc_STRVAR(posix_getpid__doc__,
|
---|
3852 | "getpid() -> pid\n\n\
|
---|
3853 | Return the current process id");
|
---|
3854 |
|
---|
3855 | static PyObject *
|
---|
3856 | posix_getpid(PyObject *self, PyObject *noargs)
|
---|
3857 | {
|
---|
3858 | return PyLong_FromPid(getpid());
|
---|
3859 | }
|
---|
3860 |
|
---|
3861 |
|
---|
3862 | #ifdef HAVE_GETGROUPS
|
---|
3863 | PyDoc_STRVAR(posix_getgroups__doc__,
|
---|
3864 | "getgroups() -> list of group IDs\n\n\
|
---|
3865 | Return list of supplemental group IDs for the process.");
|
---|
3866 |
|
---|
3867 | static PyObject *
|
---|
3868 | posix_getgroups(PyObject *self, PyObject *noargs)
|
---|
3869 | {
|
---|
3870 | PyObject *result = NULL;
|
---|
3871 |
|
---|
3872 | #ifdef NGROUPS_MAX
|
---|
3873 | #define MAX_GROUPS NGROUPS_MAX
|
---|
3874 | #else
|
---|
3875 | /* defined to be 16 on Solaris7, so this should be a small number */
|
---|
3876 | #define MAX_GROUPS 64
|
---|
3877 | #endif
|
---|
3878 | gid_t grouplist[MAX_GROUPS];
|
---|
3879 | int n;
|
---|
3880 |
|
---|
3881 | n = getgroups(MAX_GROUPS, grouplist);
|
---|
3882 | if (n < 0)
|
---|
3883 | posix_error();
|
---|
3884 | else {
|
---|
3885 | result = PyList_New(n);
|
---|
3886 | if (result != NULL) {
|
---|
3887 | int i;
|
---|
3888 | for (i = 0; i < n; ++i) {
|
---|
3889 | PyObject *o = PyInt_FromLong((long)grouplist[i]);
|
---|
3890 | if (o == NULL) {
|
---|
3891 | Py_DECREF(result);
|
---|
3892 | result = NULL;
|
---|
3893 | break;
|
---|
3894 | }
|
---|
3895 | PyList_SET_ITEM(result, i, o);
|
---|
3896 | }
|
---|
3897 | }
|
---|
3898 | }
|
---|
3899 |
|
---|
3900 | return result;
|
---|
3901 | }
|
---|
3902 | #endif
|
---|
3903 |
|
---|
3904 | #ifdef HAVE_GETPGID
|
---|
3905 | PyDoc_STRVAR(posix_getpgid__doc__,
|
---|
3906 | "getpgid(pid) -> pgid\n\n\
|
---|
3907 | Call the system call getpgid().");
|
---|
3908 |
|
---|
3909 | static PyObject *
|
---|
3910 | posix_getpgid(PyObject *self, PyObject *args)
|
---|
3911 | {
|
---|
3912 | pid_t pid, pgid;
|
---|
3913 | if (!PyArg_ParseTuple(args, PARSE_PID ":getpgid", &pid))
|
---|
3914 | return NULL;
|
---|
3915 | pgid = getpgid(pid);
|
---|
3916 | if (pgid < 0)
|
---|
3917 | return posix_error();
|
---|
3918 | return PyLong_FromPid(pgid);
|
---|
3919 | }
|
---|
3920 | #endif /* HAVE_GETPGID */
|
---|
3921 |
|
---|
3922 |
|
---|
3923 | #ifdef HAVE_GETPGRP
|
---|
3924 | PyDoc_STRVAR(posix_getpgrp__doc__,
|
---|
3925 | "getpgrp() -> pgrp\n\n\
|
---|
3926 | Return the current process group id.");
|
---|
3927 |
|
---|
3928 | static PyObject *
|
---|
3929 | posix_getpgrp(PyObject *self, PyObject *noargs)
|
---|
3930 | {
|
---|
3931 | #ifdef GETPGRP_HAVE_ARG
|
---|
3932 | return PyLong_FromPid(getpgrp(0));
|
---|
3933 | #else /* GETPGRP_HAVE_ARG */
|
---|
3934 | return PyLong_FromPid(getpgrp());
|
---|
3935 | #endif /* GETPGRP_HAVE_ARG */
|
---|
3936 | }
|
---|
3937 | #endif /* HAVE_GETPGRP */
|
---|
3938 |
|
---|
3939 |
|
---|
3940 | #ifdef HAVE_SETPGRP
|
---|
3941 | PyDoc_STRVAR(posix_setpgrp__doc__,
|
---|
3942 | "setpgrp()\n\n\
|
---|
3943 | Make this process a session leader.");
|
---|
3944 |
|
---|
3945 | static PyObject *
|
---|
3946 | posix_setpgrp(PyObject *self, PyObject *noargs)
|
---|
3947 | {
|
---|
3948 | #ifdef SETPGRP_HAVE_ARG
|
---|
3949 | if (setpgrp(0, 0) < 0)
|
---|
3950 | #else /* SETPGRP_HAVE_ARG */
|
---|
3951 | if (setpgrp() < 0)
|
---|
3952 | #endif /* SETPGRP_HAVE_ARG */
|
---|
3953 | return posix_error();
|
---|
3954 | Py_INCREF(Py_None);
|
---|
3955 | return Py_None;
|
---|
3956 | }
|
---|
3957 |
|
---|
3958 | #endif /* HAVE_SETPGRP */
|
---|
3959 |
|
---|
3960 | #ifdef HAVE_GETPPID
|
---|
3961 | PyDoc_STRVAR(posix_getppid__doc__,
|
---|
3962 | "getppid() -> ppid\n\n\
|
---|
3963 | Return the parent's process id.");
|
---|
3964 |
|
---|
3965 | static PyObject *
|
---|
3966 | posix_getppid(PyObject *self, PyObject *noargs)
|
---|
3967 | {
|
---|
3968 | return PyLong_FromPid(getppid());
|
---|
3969 | }
|
---|
3970 | #endif
|
---|
3971 |
|
---|
3972 |
|
---|
3973 | #ifdef HAVE_GETLOGIN
|
---|
3974 | PyDoc_STRVAR(posix_getlogin__doc__,
|
---|
3975 | "getlogin() -> string\n\n\
|
---|
3976 | Return the actual login name.");
|
---|
3977 |
|
---|
3978 | static PyObject *
|
---|
3979 | posix_getlogin(PyObject *self, PyObject *noargs)
|
---|
3980 | {
|
---|
3981 | PyObject *result = NULL;
|
---|
3982 | char *name;
|
---|
3983 | int old_errno = errno;
|
---|
3984 |
|
---|
3985 | errno = 0;
|
---|
3986 | name = getlogin();
|
---|
3987 | if (name == NULL) {
|
---|
3988 | if (errno)
|
---|
3989 | posix_error();
|
---|
3990 | else
|
---|
3991 | PyErr_SetString(PyExc_OSError,
|
---|
3992 | "unable to determine login name");
|
---|
3993 | }
|
---|
3994 | else
|
---|
3995 | result = PyString_FromString(name);
|
---|
3996 | errno = old_errno;
|
---|
3997 |
|
---|
3998 | return result;
|
---|
3999 | }
|
---|
4000 | #endif
|
---|
4001 |
|
---|
4002 | #ifdef HAVE_GETUID
|
---|
4003 | PyDoc_STRVAR(posix_getuid__doc__,
|
---|
4004 | "getuid() -> uid\n\n\
|
---|
4005 | Return the current process's user id.");
|
---|
4006 |
|
---|
4007 | static PyObject *
|
---|
4008 | posix_getuid(PyObject *self, PyObject *noargs)
|
---|
4009 | {
|
---|
4010 | return PyInt_FromLong((long)getuid());
|
---|
4011 | }
|
---|
4012 | #endif
|
---|
4013 |
|
---|
4014 |
|
---|
4015 | #ifdef HAVE_KILL
|
---|
4016 | PyDoc_STRVAR(posix_kill__doc__,
|
---|
4017 | "kill(pid, sig)\n\n\
|
---|
4018 | Kill a process with a signal.");
|
---|
4019 |
|
---|
4020 | static PyObject *
|
---|
4021 | posix_kill(PyObject *self, PyObject *args)
|
---|
4022 | {
|
---|
4023 | pid_t pid;
|
---|
4024 | int sig;
|
---|
4025 | if (!PyArg_ParseTuple(args, PARSE_PID "i:kill", &pid, &sig))
|
---|
4026 | return NULL;
|
---|
4027 | #if defined(PYOS_OS2) && !defined(PYCC_GCC)
|
---|
4028 | if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
|
---|
4029 | APIRET rc;
|
---|
4030 | if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
|
---|
4031 | return os2_error(rc);
|
---|
4032 |
|
---|
4033 | } else if (sig == XCPT_SIGNAL_KILLPROC) {
|
---|
4034 | APIRET rc;
|
---|
4035 | if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
|
---|
4036 | return os2_error(rc);
|
---|
4037 |
|
---|
4038 | } else
|
---|
4039 | return NULL; /* Unrecognized Signal Requested */
|
---|
4040 | #else
|
---|
4041 | if (kill(pid, sig) == -1)
|
---|
4042 | return posix_error();
|
---|
4043 | #endif
|
---|
4044 | Py_INCREF(Py_None);
|
---|
4045 | return Py_None;
|
---|
4046 | }
|
---|
4047 | #endif
|
---|
4048 |
|
---|
4049 | #ifdef HAVE_KILLPG
|
---|
4050 | PyDoc_STRVAR(posix_killpg__doc__,
|
---|
4051 | "killpg(pgid, sig)\n\n\
|
---|
4052 | Kill a process group with a signal.");
|
---|
4053 |
|
---|
4054 | static PyObject *
|
---|
4055 | posix_killpg(PyObject *self, PyObject *args)
|
---|
4056 | {
|
---|
4057 | int sig;
|
---|
4058 | pid_t pgid;
|
---|
4059 | /* XXX some man pages make the `pgid` parameter an int, others
|
---|
4060 | a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
|
---|
4061 | take the same type. Moreover, pid_t is always at least as wide as
|
---|
4062 | int (else compilation of this module fails), which is safe. */
|
---|
4063 | if (!PyArg_ParseTuple(args, PARSE_PID "i:killpg", &pgid, &sig))
|
---|
4064 | return NULL;
|
---|
4065 | if (killpg(pgid, sig) == -1)
|
---|
4066 | return posix_error();
|
---|
4067 | Py_INCREF(Py_None);
|
---|
4068 | return Py_None;
|
---|
4069 | }
|
---|
4070 | #endif
|
---|
4071 |
|
---|
4072 | #ifdef HAVE_PLOCK
|
---|
4073 |
|
---|
4074 | #ifdef HAVE_SYS_LOCK_H
|
---|
4075 | #include <sys/lock.h>
|
---|
4076 | #endif
|
---|
4077 |
|
---|
4078 | PyDoc_STRVAR(posix_plock__doc__,
|
---|
4079 | "plock(op)\n\n\
|
---|
4080 | Lock program segments into memory.");
|
---|
4081 |
|
---|
4082 | static PyObject *
|
---|
4083 | posix_plock(PyObject *self, PyObject *args)
|
---|
4084 | {
|
---|
4085 | int op;
|
---|
4086 | if (!PyArg_ParseTuple(args, "i:plock", &op))
|
---|
4087 | return NULL;
|
---|
4088 | if (plock(op) == -1)
|
---|
4089 | return posix_error();
|
---|
4090 | Py_INCREF(Py_None);
|
---|
4091 | return Py_None;
|
---|
4092 | }
|
---|
4093 | #endif
|
---|
4094 |
|
---|
4095 |
|
---|
4096 | #ifdef HAVE_POPEN
|
---|
4097 | PyDoc_STRVAR(posix_popen__doc__,
|
---|
4098 | "popen(command [, mode='r' [, bufsize]]) -> pipe\n\n\
|
---|
4099 | Open a pipe to/from a command returning a file object.");
|
---|
4100 |
|
---|
4101 | #if defined(PYOS_OS2)
|
---|
4102 | #if defined(PYCC_VACPP)
|
---|
4103 | static int
|
---|
4104 | async_system(const char *command)
|
---|
4105 | {
|
---|
4106 | char errormsg[256], args[1024];
|
---|
4107 | RESULTCODES rcodes;
|
---|
4108 | APIRET rc;
|
---|
4109 |
|
---|
4110 | char *shell = getenv("COMSPEC");
|
---|
4111 | if (!shell)
|
---|
4112 | shell = "cmd";
|
---|
4113 |
|
---|
4114 | /* avoid overflowing the argument buffer */
|
---|
4115 | if (strlen(shell) + 3 + strlen(command) >= 1024)
|
---|
4116 | return ERROR_NOT_ENOUGH_MEMORY
|
---|
4117 |
|
---|
4118 | args[0] = '\0';
|
---|
4119 | strcat(args, shell);
|
---|
4120 | strcat(args, "/c ");
|
---|
4121 | strcat(args, command);
|
---|
4122 |
|
---|
4123 | /* execute asynchronously, inheriting the environment */
|
---|
4124 | rc = DosExecPgm(errormsg,
|
---|
4125 | sizeof(errormsg),
|
---|
4126 | EXEC_ASYNC,
|
---|
4127 | args,
|
---|
4128 | NULL,
|
---|
4129 | &rcodes,
|
---|
4130 | shell);
|
---|
4131 | return rc;
|
---|
4132 | }
|
---|
4133 |
|
---|
4134 | static FILE *
|
---|
4135 | popen(const char *command, const char *mode, int pipesize, int *err)
|
---|
4136 | {
|
---|
4137 | int oldfd, tgtfd;
|
---|
4138 | HFILE pipeh[2];
|
---|
4139 | APIRET rc;
|
---|
4140 |
|
---|
4141 | /* mode determines which of stdin or stdout is reconnected to
|
---|
4142 | * the pipe to the child
|
---|
4143 | */
|
---|
4144 | if (strchr(mode, 'r') != NULL) {
|
---|
4145 | tgt_fd = 1; /* stdout */
|
---|
4146 | } else if (strchr(mode, 'w')) {
|
---|
4147 | tgt_fd = 0; /* stdin */
|
---|
4148 | } else {
|
---|
4149 | *err = ERROR_INVALID_ACCESS;
|
---|
4150 | return NULL;
|
---|
4151 | }
|
---|
4152 |
|
---|
4153 | /* setup the pipe */
|
---|
4154 | if ((rc = DosCreatePipe(&pipeh[0], &pipeh[1], pipesize)) != NO_ERROR) {
|
---|
4155 | *err = rc;
|
---|
4156 | return NULL;
|
---|
4157 | }
|
---|
4158 |
|
---|
4159 | /* prevent other threads accessing stdio */
|
---|
4160 | DosEnterCritSec();
|
---|
4161 |
|
---|
4162 | /* reconnect stdio and execute child */
|
---|
4163 | oldfd = dup(tgtfd);
|
---|
4164 | close(tgtfd);
|
---|
4165 | if (dup2(pipeh[tgtfd], tgtfd) == 0) {
|
---|
4166 | DosClose(pipeh[tgtfd]);
|
---|
4167 | rc = async_system(command);
|
---|
4168 | }
|
---|
4169 |
|
---|
4170 | /* restore stdio */
|
---|
4171 | dup2(oldfd, tgtfd);
|
---|
4172 | close(oldfd);
|
---|
4173 |
|
---|
4174 | /* allow other threads access to stdio */
|
---|
4175 | DosExitCritSec();
|
---|
4176 |
|
---|
4177 | /* if execution of child was successful return file stream */
|
---|
4178 | if (rc == NO_ERROR)
|
---|
4179 | return fdopen(pipeh[1 - tgtfd], mode);
|
---|
4180 | else {
|
---|
4181 | DosClose(pipeh[1 - tgtfd]);
|
---|
4182 | *err = rc;
|
---|
4183 | return NULL;
|
---|
4184 | }
|
---|
4185 | }
|
---|
4186 |
|
---|
4187 | static PyObject *
|
---|
4188 | posix_popen(PyObject *self, PyObject *args)
|
---|
4189 | {
|
---|
4190 | char *name;
|
---|
4191 | char *mode = "r";
|
---|
4192 | int err, bufsize = -1;
|
---|
4193 | FILE *fp;
|
---|
4194 | PyObject *f;
|
---|
4195 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
|
---|
4196 | return NULL;
|
---|
4197 | Py_BEGIN_ALLOW_THREADS
|
---|
4198 | fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err);
|
---|
4199 | Py_END_ALLOW_THREADS
|
---|
4200 | if (fp == NULL)
|
---|
4201 | return os2_error(err);
|
---|
4202 |
|
---|
4203 | f = PyFile_FromFile(fp, name, mode, fclose);
|
---|
4204 | if (f != NULL)
|
---|
4205 | PyFile_SetBufSize(f, bufsize);
|
---|
4206 | return f;
|
---|
4207 | }
|
---|
4208 |
|
---|
4209 | #elif defined(PYCC_GCC)
|
---|
4210 |
|
---|
4211 | /* standard posix version of popen() support */
|
---|
4212 | static PyObject *
|
---|
4213 | posix_popen(PyObject *self, PyObject *args)
|
---|
4214 | {
|
---|
4215 | char *name;
|
---|
4216 | char *mode = "r";
|
---|
4217 | int bufsize = -1;
|
---|
4218 | FILE *fp;
|
---|
4219 | PyObject *f;
|
---|
4220 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
|
---|
4221 | return NULL;
|
---|
4222 | Py_BEGIN_ALLOW_THREADS
|
---|
4223 | fp = popen(name, mode);
|
---|
4224 | Py_END_ALLOW_THREADS
|
---|
4225 | if (fp == NULL)
|
---|
4226 | return posix_error();
|
---|
4227 | f = PyFile_FromFile(fp, name, mode, pclose);
|
---|
4228 | if (f != NULL)
|
---|
4229 | PyFile_SetBufSize(f, bufsize);
|
---|
4230 | return f;
|
---|
4231 | }
|
---|
4232 |
|
---|
4233 | /* fork() under OS/2 has lots'o'warts
|
---|
4234 | * EMX supports pipe() and spawn*() so we can synthesize popen[234]()
|
---|
4235 | * most of this code is a ripoff of the win32 code, but using the
|
---|
4236 | * capabilities of EMX's C library routines
|
---|
4237 | */
|
---|
4238 |
|
---|
4239 | /* These tell _PyPopen() whether to return 1, 2, or 3 file objects. */
|
---|
4240 | #define POPEN_1 1
|
---|
4241 | #define POPEN_2 2
|
---|
4242 | #define POPEN_3 3
|
---|
4243 | #define POPEN_4 4
|
---|
4244 |
|
---|
4245 | static PyObject *_PyPopen(char *, int, int, int);
|
---|
4246 | static int _PyPclose(FILE *file);
|
---|
4247 |
|
---|
4248 | /*
|
---|
4249 | * Internal dictionary mapping popen* file pointers to process handles,
|
---|
4250 | * for use when retrieving the process exit code. See _PyPclose() below
|
---|
4251 | * for more information on this dictionary's use.
|
---|
4252 | */
|
---|
4253 | static PyObject *_PyPopenProcs = NULL;
|
---|
4254 |
|
---|
4255 | /* os2emx version of popen2()
|
---|
4256 | *
|
---|
4257 | * The result of this function is a pipe (file) connected to the
|
---|
4258 | * process's stdin, and a pipe connected to the process's stdout.
|
---|
4259 | */
|
---|
4260 |
|
---|
4261 | static PyObject *
|
---|
4262 | os2emx_popen2(PyObject *self, PyObject *args)
|
---|
4263 | {
|
---|
4264 | PyObject *f;
|
---|
4265 | int tm=0;
|
---|
4266 |
|
---|
4267 | char *cmdstring;
|
---|
4268 | char *mode = "t";
|
---|
4269 | int bufsize = -1;
|
---|
4270 | if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
|
---|
4271 | return NULL;
|
---|
4272 |
|
---|
4273 | if (*mode == 't')
|
---|
4274 | tm = O_TEXT;
|
---|
4275 | else if (*mode != 'b') {
|
---|
4276 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
|
---|
4277 | return NULL;
|
---|
4278 | } else
|
---|
4279 | tm = O_BINARY;
|
---|
4280 |
|
---|
4281 | f = _PyPopen(cmdstring, tm, POPEN_2, bufsize);
|
---|
4282 |
|
---|
4283 | return f;
|
---|
4284 | }
|
---|
4285 |
|
---|
4286 | /*
|
---|
4287 | * Variation on os2emx.popen2
|
---|
4288 | *
|
---|
4289 | * The result of this function is 3 pipes - the process's stdin,
|
---|
4290 | * stdout and stderr
|
---|
4291 | */
|
---|
4292 |
|
---|
4293 | static PyObject *
|
---|
4294 | os2emx_popen3(PyObject *self, PyObject *args)
|
---|
4295 | {
|
---|
4296 | PyObject *f;
|
---|
4297 | int tm = 0;
|
---|
4298 |
|
---|
4299 | char *cmdstring;
|
---|
4300 | char *mode = "t";
|
---|
4301 | int bufsize = -1;
|
---|
4302 | if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
|
---|
4303 | return NULL;
|
---|
4304 |
|
---|
4305 | if (*mode == 't')
|
---|
4306 | tm = O_TEXT;
|
---|
4307 | else if (*mode != 'b') {
|
---|
4308 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
|
---|
4309 | return NULL;
|
---|
4310 | } else
|
---|
4311 | tm = O_BINARY;
|
---|
4312 |
|
---|
4313 | f = _PyPopen(cmdstring, tm, POPEN_3, bufsize);
|
---|
4314 |
|
---|
4315 | return f;
|
---|
4316 | }
|
---|
4317 |
|
---|
4318 | /*
|
---|
4319 | * Variation on os2emx.popen2
|
---|
4320 | *
|
---|
4321 | * The result of this function is 2 pipes - the processes stdin,
|
---|
4322 | * and stdout+stderr combined as a single pipe.
|
---|
4323 | */
|
---|
4324 |
|
---|
4325 | static PyObject *
|
---|
4326 | os2emx_popen4(PyObject *self, PyObject *args)
|
---|
4327 | {
|
---|
4328 | PyObject *f;
|
---|
4329 | int tm = 0;
|
---|
4330 |
|
---|
4331 | char *cmdstring;
|
---|
4332 | char *mode = "t";
|
---|
4333 | int bufsize = -1;
|
---|
4334 | if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
|
---|
4335 | return NULL;
|
---|
4336 |
|
---|
4337 | if (*mode == 't')
|
---|
4338 | tm = O_TEXT;
|
---|
4339 | else if (*mode != 'b') {
|
---|
4340 | PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'");
|
---|
4341 | return NULL;
|
---|
4342 | } else
|
---|
4343 | tm = O_BINARY;
|
---|
4344 |
|
---|
4345 | f = _PyPopen(cmdstring, tm, POPEN_4, bufsize);
|
---|
4346 |
|
---|
4347 | return f;
|
---|
4348 | }
|
---|
4349 |
|
---|
4350 | /* a couple of structures for convenient handling of multiple
|
---|
4351 | * file handles and pipes
|
---|
4352 | */
|
---|
4353 | struct file_ref
|
---|
4354 | {
|
---|
4355 | int handle;
|
---|
4356 | int flags;
|
---|
4357 | };
|
---|
4358 |
|
---|
4359 | struct pipe_ref
|
---|
4360 | {
|
---|
4361 | int rd;
|
---|
4362 | int wr;
|
---|
4363 | };
|
---|
4364 |
|
---|
4365 | /* The following code is derived from the win32 code */
|
---|
4366 |
|
---|
4367 | static PyObject *
|
---|
4368 | _PyPopen(char *cmdstring, int mode, int n, int bufsize)
|
---|
4369 | {
|
---|
4370 | struct file_ref stdio[3];
|
---|
4371 | struct pipe_ref p_fd[3];
|
---|
4372 | FILE *p_s[3];
|
---|
4373 | int file_count, i, pipe_err;
|
---|
4374 | pid_t pipe_pid;
|
---|
4375 | char *shell, *sh_name, *opt, *rd_mode, *wr_mode;
|
---|
4376 | PyObject *f, *p_f[3];
|
---|
4377 |
|
---|
4378 | /* file modes for subsequent fdopen's on pipe handles */
|
---|
4379 | if (mode == O_TEXT)
|
---|
4380 | {
|
---|
4381 | rd_mode = "rt";
|
---|
4382 | wr_mode = "wt";
|
---|
4383 | }
|
---|
4384 | else
|
---|
4385 | {
|
---|
4386 | rd_mode = "rb";
|
---|
4387 | wr_mode = "wb";
|
---|
4388 | }
|
---|
4389 |
|
---|
4390 | /* prepare shell references */
|
---|
4391 | if ((shell = getenv("EMXSHELL")) == NULL)
|
---|
4392 | if ((shell = getenv("COMSPEC")) == NULL)
|
---|
4393 | {
|
---|
4394 | errno = ENOENT;
|
---|
4395 | return posix_error();
|
---|
4396 | }
|
---|
4397 |
|
---|
4398 | sh_name = _getname(shell);
|
---|
4399 | if (stricmp(sh_name, "cmd.exe") == 0 || stricmp(sh_name, "4os2.exe") == 0)
|
---|
4400 | opt = "/c";
|
---|
4401 | else
|
---|
4402 | opt = "-c";
|
---|
4403 |
|
---|
4404 | /* save current stdio fds + their flags, and set not inheritable */
|
---|
4405 | i = pipe_err = 0;
|
---|
4406 | while (pipe_err >= 0 && i < 3)
|
---|
4407 | {
|
---|
4408 | pipe_err = stdio[i].handle = dup(i);
|
---|
4409 | stdio[i].flags = fcntl(i, F_GETFD, 0);
|
---|
4410 | fcntl(stdio[i].handle, F_SETFD, stdio[i].flags | FD_CLOEXEC);
|
---|
4411 | i++;
|
---|
4412 | }
|
---|
4413 | if (pipe_err < 0)
|
---|
4414 | {
|
---|
4415 | /* didn't get them all saved - clean up and bail out */
|
---|
4416 | int saved_err = errno;
|
---|
4417 | while (i-- > 0)
|
---|
4418 | {
|
---|
4419 | close(stdio[i].handle);
|
---|
4420 | }
|
---|
4421 | errno = saved_err;
|
---|
4422 | return posix_error();
|
---|
4423 | }
|
---|
4424 |
|
---|
4425 | /* create pipe ends */
|
---|
4426 | file_count = 2;
|
---|
4427 | if (n == POPEN_3)
|
---|
4428 | file_count = 3;
|
---|
4429 | i = pipe_err = 0;
|
---|
4430 | while ((pipe_err == 0) && (i < file_count))
|
---|
4431 | pipe_err = pipe((int *)&p_fd[i++]);
|
---|
4432 | if (pipe_err < 0)
|
---|
4433 | {
|
---|
4434 | /* didn't get them all made - clean up and bail out */
|
---|
4435 | while (i-- > 0)
|
---|
4436 | {
|
---|
4437 | close(p_fd[i].wr);
|
---|
4438 | close(p_fd[i].rd);
|
---|
4439 | }
|
---|
4440 | errno = EPIPE;
|
---|
4441 | return posix_error();
|
---|
4442 | }
|
---|
4443 |
|
---|
4444 | /* change the actual standard IO streams over temporarily,
|
---|
4445 | * making the retained pipe ends non-inheritable
|
---|
4446 | */
|
---|
4447 | pipe_err = 0;
|
---|
4448 |
|
---|
4449 | /* - stdin */
|
---|
4450 | if (dup2(p_fd[0].rd, 0) == 0)
|
---|
4451 | {
|
---|
4452 | close(p_fd[0].rd);
|
---|
4453 | i = fcntl(p_fd[0].wr, F_GETFD, 0);
|
---|
4454 | fcntl(p_fd[0].wr, F_SETFD, i | FD_CLOEXEC);
|
---|
4455 | if ((p_s[0] = fdopen(p_fd[0].wr, wr_mode)) == NULL)
|
---|
4456 | {
|
---|
4457 | close(p_fd[0].wr);
|
---|
4458 | pipe_err = -1;
|
---|
4459 | }
|
---|
4460 | }
|
---|
4461 | else
|
---|
4462 | {
|
---|
4463 | pipe_err = -1;
|
---|
4464 | }
|
---|
4465 |
|
---|
4466 | /* - stdout */
|
---|
4467 | if (pipe_err == 0)
|
---|
4468 | {
|
---|
4469 | if (dup2(p_fd[1].wr, 1) == 1)
|
---|
4470 | {
|
---|
4471 | close(p_fd[1].wr);
|
---|
4472 | i = fcntl(p_fd[1].rd, F_GETFD, 0);
|
---|
4473 | fcntl(p_fd[1].rd, F_SETFD, i | FD_CLOEXEC);
|
---|
4474 | if ((p_s[1] = fdopen(p_fd[1].rd, rd_mode)) == NULL)
|
---|
4475 | {
|
---|
4476 | close(p_fd[1].rd);
|
---|
4477 | pipe_err = -1;
|
---|
4478 | }
|
---|
4479 | }
|
---|
4480 | else
|
---|
4481 | {
|
---|
4482 | pipe_err = -1;
|
---|
4483 | }
|
---|
4484 | }
|
---|
4485 |
|
---|
4486 | /* - stderr, as required */
|
---|
4487 | if (pipe_err == 0)
|
---|
4488 | switch (n)
|
---|
4489 | {
|
---|
4490 | case POPEN_3:
|
---|
4491 | {
|
---|
4492 | if (dup2(p_fd[2].wr, 2) == 2)
|
---|
4493 | {
|
---|
4494 | close(p_fd[2].wr);
|
---|
4495 | i = fcntl(p_fd[2].rd, F_GETFD, 0);
|
---|
4496 | fcntl(p_fd[2].rd, F_SETFD, i | FD_CLOEXEC);
|
---|
4497 | if ((p_s[2] = fdopen(p_fd[2].rd, rd_mode)) == NULL)
|
---|
4498 | {
|
---|
4499 | close(p_fd[2].rd);
|
---|
4500 | pipe_err = -1;
|
---|
4501 | }
|
---|
4502 | }
|
---|
4503 | else
|
---|
4504 | {
|
---|
4505 | pipe_err = -1;
|
---|
4506 | }
|
---|
4507 | break;
|
---|
4508 | }
|
---|
4509 |
|
---|
4510 | case POPEN_4:
|
---|
4511 | {
|
---|
4512 | if (dup2(1, 2) != 2)
|
---|
4513 | {
|
---|
4514 | pipe_err = -1;
|
---|
4515 | }
|
---|
4516 | break;
|
---|
4517 | }
|
---|
4518 | }
|
---|
4519 |
|
---|
4520 | /* spawn the child process */
|
---|
4521 | if (pipe_err == 0)
|
---|
4522 | {
|
---|
4523 | pipe_pid = spawnlp(P_NOWAIT, shell, shell, opt, cmdstring, (char *)0);
|
---|
4524 | if (pipe_pid == -1)
|
---|
4525 | {
|
---|
4526 | pipe_err = -1;
|
---|
4527 | }
|
---|
4528 | else
|
---|
4529 | {
|
---|
4530 | /* save the PID into the FILE structure
|
---|
4531 | * NOTE: this implementation doesn't actually
|
---|
4532 | * take advantage of this, but do it for
|
---|
4533 | * completeness - AIM Apr01
|
---|
4534 | */
|
---|
4535 | for (i = 0; i < file_count; i++)
|
---|
4536 | p_s[i]->_pid = pipe_pid;
|
---|
4537 | }
|
---|
4538 | }
|
---|
4539 |
|
---|
4540 | /* reset standard IO to normal */
|
---|
4541 | for (i = 0; i < 3; i++)
|
---|
4542 | {
|
---|
4543 | dup2(stdio[i].handle, i);
|
---|
4544 | fcntl(i, F_SETFD, stdio[i].flags);
|
---|
4545 | close(stdio[i].handle);
|
---|
4546 | }
|
---|
4547 |
|
---|
4548 | /* if any remnant problems, clean up and bail out */
|
---|
4549 | if (pipe_err < 0)
|
---|
4550 | {
|
---|
4551 | for (i = 0; i < 3; i++)
|
---|
4552 | {
|
---|
4553 | close(p_fd[i].rd);
|
---|
4554 | close(p_fd[i].wr);
|
---|
4555 | }
|
---|
4556 | errno = EPIPE;
|
---|
4557 | return posix_error_with_filename(cmdstring);
|
---|
4558 | }
|
---|
4559 |
|
---|
4560 | /* build tuple of file objects to return */
|
---|
4561 | if ((p_f[0] = PyFile_FromFile(p_s[0], cmdstring, wr_mode, _PyPclose)) != NULL)
|
---|
4562 | PyFile_SetBufSize(p_f[0], bufsize);
|
---|
4563 | if ((p_f[1] = PyFile_FromFile(p_s[1], cmdstring, rd_mode, _PyPclose)) != NULL)
|
---|
4564 | PyFile_SetBufSize(p_f[1], bufsize);
|
---|
4565 | if (n == POPEN_3)
|
---|
4566 | {
|
---|
4567 | if ((p_f[2] = PyFile_FromFile(p_s[2], cmdstring, rd_mode, _PyPclose)) != NULL)
|
---|
4568 | PyFile_SetBufSize(p_f[0], bufsize);
|
---|
4569 | f = PyTuple_Pack(3, p_f[0], p_f[1], p_f[2]);
|
---|
4570 | }
|
---|
4571 | else
|
---|
4572 | f = PyTuple_Pack(2, p_f[0], p_f[1]);
|
---|
4573 |
|
---|
4574 | /*
|
---|
4575 | * Insert the files we've created into the process dictionary
|
---|
4576 | * all referencing the list with the process handle and the
|
---|
4577 | * initial number of files (see description below in _PyPclose).
|
---|
4578 | * Since if _PyPclose later tried to wait on a process when all
|
---|
4579 | * handles weren't closed, it could create a deadlock with the
|
---|
4580 | * child, we spend some energy here to try to ensure that we
|
---|
4581 | * either insert all file handles into the dictionary or none
|
---|
4582 | * at all. It's a little clumsy with the various popen modes
|
---|
4583 | * and variable number of files involved.
|
---|
4584 | */
|
---|
4585 | if (!_PyPopenProcs)
|
---|
4586 | {
|
---|
4587 | _PyPopenProcs = PyDict_New();
|
---|
4588 | }
|
---|
4589 |
|
---|
4590 | if (_PyPopenProcs)
|
---|
4591 | {
|
---|
4592 | PyObject *procObj, *pidObj, *intObj, *fileObj[3];
|
---|
4593 | int ins_rc[3];
|
---|
4594 |
|
---|
4595 | fileObj[0] = fileObj[1] = fileObj[2] = NULL;
|
---|
4596 | ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
|
---|
4597 |
|
---|
4598 | procObj = PyList_New(2);
|
---|
4599 | pidObj = PyLong_FromPid(pipe_pid);
|
---|
4600 | intObj = PyInt_FromLong((long) file_count);
|
---|
4601 |
|
---|
4602 | if (procObj && pidObj && intObj)
|
---|
4603 | {
|
---|
4604 | PyList_SetItem(procObj, 0, pidObj);
|
---|
4605 | PyList_SetItem(procObj, 1, intObj);
|
---|
4606 |
|
---|
4607 | fileObj[0] = PyLong_FromVoidPtr(p_s[0]);
|
---|
4608 | if (fileObj[0])
|
---|
4609 | {
|
---|
4610 | ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
|
---|
4611 | fileObj[0],
|
---|
4612 | procObj);
|
---|
4613 | }
|
---|
4614 | fileObj[1] = PyLong_FromVoidPtr(p_s[1]);
|
---|
4615 | if (fileObj[1])
|
---|
4616 | {
|
---|
4617 | ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
|
---|
4618 | fileObj[1],
|
---|
4619 | procObj);
|
---|
4620 | }
|
---|
4621 | if (file_count >= 3)
|
---|
4622 | {
|
---|
4623 | fileObj[2] = PyLong_FromVoidPtr(p_s[2]);
|
---|
4624 | if (fileObj[2])
|
---|
4625 | {
|
---|
4626 | ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
|
---|
4627 | fileObj[2],
|
---|
4628 | procObj);
|
---|
4629 | }
|
---|
4630 | }
|
---|
4631 |
|
---|
4632 | if (ins_rc[0] < 0 || !fileObj[0] ||
|
---|
4633 | ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
|
---|
4634 | ins_rc[2] < 0 || (file_count > 2 && !fileObj[2]))
|
---|
4635 | {
|
---|
4636 | /* Something failed - remove any dictionary
|
---|
4637 | * entries that did make it.
|
---|
4638 | */
|
---|
4639 | if (!ins_rc[0] && fileObj[0])
|
---|
4640 | {
|
---|
4641 | PyDict_DelItem(_PyPopenProcs,
|
---|
4642 | fileObj[0]);
|
---|
4643 | }
|
---|
4644 | if (!ins_rc[1] && fileObj[1])
|
---|
4645 | {
|
---|
4646 | PyDict_DelItem(_PyPopenProcs,
|
---|
4647 | fileObj[1]);
|
---|
4648 | }
|
---|
4649 | if (!ins_rc[2] && fileObj[2])
|
---|
4650 | {
|
---|
4651 | PyDict_DelItem(_PyPopenProcs,
|
---|
4652 | fileObj[2]);
|
---|
4653 | }
|
---|
4654 | }
|
---|
4655 | }
|
---|
4656 |
|
---|
4657 | /*
|
---|
4658 | * Clean up our localized references for the dictionary keys
|
---|
4659 | * and value since PyDict_SetItem will Py_INCREF any copies
|
---|
4660 | * that got placed in the dictionary.
|
---|
4661 | */
|
---|
4662 | Py_XDECREF(procObj);
|
---|
4663 | Py_XDECREF(fileObj[0]);
|
---|
4664 | Py_XDECREF(fileObj[1]);
|
---|
4665 | Py_XDECREF(fileObj[2]);
|
---|
4666 | }
|
---|
4667 |
|
---|
4668 | /* Child is launched. */
|
---|
4669 | return f;
|
---|
4670 | }
|
---|
4671 |
|
---|
4672 | /*
|
---|
4673 | * Wrapper for fclose() to use for popen* files, so we can retrieve the
|
---|
4674 | * exit code for the child process and return as a result of the close.
|
---|
4675 | *
|
---|
4676 | * This function uses the _PyPopenProcs dictionary in order to map the
|
---|
4677 | * input file pointer to information about the process that was
|
---|
4678 | * originally created by the popen* call that created the file pointer.
|
---|
4679 | * The dictionary uses the file pointer as a key (with one entry
|
---|
4680 | * inserted for each file returned by the original popen* call) and a
|
---|
4681 | * single list object as the value for all files from a single call.
|
---|
4682 | * The list object contains the Win32 process handle at [0], and a file
|
---|
4683 | * count at [1], which is initialized to the total number of file
|
---|
4684 | * handles using that list.
|
---|
4685 | *
|
---|
4686 | * This function closes whichever handle it is passed, and decrements
|
---|
4687 | * the file count in the dictionary for the process handle pointed to
|
---|
4688 | * by this file. On the last close (when the file count reaches zero),
|
---|
4689 | * this function will wait for the child process and then return its
|
---|
4690 | * exit code as the result of the close() operation. This permits the
|
---|
4691 | * files to be closed in any order - it is always the close() of the
|
---|
4692 | * final handle that will return the exit code.
|
---|
4693 | *
|
---|
4694 | * NOTE: This function is currently called with the GIL released.
|
---|
4695 | * hence we use the GILState API to manage our state.
|
---|
4696 | */
|
---|
4697 |
|
---|
4698 | static int _PyPclose(FILE *file)
|
---|
4699 | {
|
---|
4700 | int result;
|
---|
4701 | int exit_code;
|
---|
4702 | pid_t pipe_pid;
|
---|
4703 | PyObject *procObj, *pidObj, *intObj, *fileObj;
|
---|
4704 | int file_count;
|
---|
4705 | #ifdef WITH_THREAD
|
---|
4706 | PyGILState_STATE state;
|
---|
4707 | #endif
|
---|
4708 |
|
---|
4709 | /* Close the file handle first, to ensure it can't block the
|
---|
4710 | * child from exiting if it's the last handle.
|
---|
4711 | */
|
---|
4712 | result = fclose(file);
|
---|
4713 |
|
---|
4714 | #ifdef WITH_THREAD
|
---|
4715 | state = PyGILState_Ensure();
|
---|
4716 | #endif
|
---|
4717 | if (_PyPopenProcs)
|
---|
4718 | {
|
---|
4719 | if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
|
---|
4720 | (procObj = PyDict_GetItem(_PyPopenProcs,
|
---|
4721 | fileObj)) != NULL &&
|
---|
4722 | (pidObj = PyList_GetItem(procObj,0)) != NULL &&
|
---|
4723 | (intObj = PyList_GetItem(procObj,1)) != NULL)
|
---|
4724 | {
|
---|
4725 | pipe_pid = (pid_t) PyLong_AsPid(pidObj);
|
---|
4726 | file_count = (int) PyInt_AsLong(intObj);
|
---|
4727 |
|
---|
4728 | if (file_count > 1)
|
---|
4729 | {
|
---|
4730 | /* Still other files referencing process */
|
---|
4731 | file_count--;
|
---|
4732 | PyList_SetItem(procObj,1,
|
---|
4733 | PyInt_FromLong((long) file_count));
|
---|
4734 | }
|
---|
4735 | else
|
---|
4736 | {
|
---|
4737 | /* Last file for this process */
|
---|
4738 | if (result != EOF &&
|
---|
4739 | waitpid(pipe_pid, &exit_code, 0) == pipe_pid)
|
---|
4740 | {
|
---|
4741 | /* extract exit status */
|
---|
4742 | if (WIFEXITED(exit_code))
|
---|
4743 | {
|
---|
4744 | result = WEXITSTATUS(exit_code);
|
---|
4745 | }
|
---|
4746 | else
|
---|
4747 | {
|
---|
4748 | errno = EPIPE;
|
---|
4749 | result = -1;
|
---|
4750 | }
|
---|
4751 | }
|
---|
4752 | else
|
---|
4753 | {
|
---|
4754 | /* Indicate failure - this will cause the file object
|
---|
4755 | * to raise an I/O error and translate the last
|
---|
4756 | * error code from errno. We do have a problem with
|
---|
4757 | * last errors that overlap the normal errno table,
|
---|
4758 | * but that's a consistent problem with the file object.
|
---|
4759 | */
|
---|
4760 | result = -1;
|
---|
4761 | }
|
---|
4762 | }
|
---|
4763 |
|
---|
4764 | /* Remove this file pointer from dictionary */
|
---|
4765 | PyDict_DelItem(_PyPopenProcs, fileObj);
|
---|
4766 |
|
---|
4767 | if (PyDict_Size(_PyPopenProcs) == 0)
|
---|
4768 | {
|
---|
4769 | Py_DECREF(_PyPopenProcs);
|
---|
4770 | _PyPopenProcs = NULL;
|
---|
4771 | }
|
---|
4772 |
|
---|
4773 | } /* if object retrieval ok */
|
---|
4774 |
|
---|
4775 | Py_XDECREF(fileObj);
|
---|
4776 | } /* if _PyPopenProcs */
|
---|
4777 |
|
---|
4778 | #ifdef WITH_THREAD
|
---|
4779 | PyGILState_Release(state);
|
---|
4780 | #endif
|
---|
4781 | return result;
|
---|
4782 | }
|
---|
4783 |
|
---|
4784 | #endif /* PYCC_??? */
|
---|
4785 |
|
---|
4786 | #elif defined(MS_WINDOWS)
|
---|
4787 |
|
---|
4788 | /*
|
---|
4789 | * Portable 'popen' replacement for Win32.
|
---|
4790 | *
|
---|
4791 | * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks
|
---|
4792 | * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com>
|
---|
4793 | * Return code handling by David Bolen <db3l@fitlinxx.com>.
|
---|
4794 | */
|
---|
4795 |
|
---|
4796 | #include <malloc.h>
|
---|
4797 | #include <io.h>
|
---|
4798 | #include <fcntl.h>
|
---|
4799 |
|
---|
4800 | /* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */
|
---|
4801 | #define POPEN_1 1
|
---|
4802 | #define POPEN_2 2
|
---|
4803 | #define POPEN_3 3
|
---|
4804 | #define POPEN_4 4
|
---|
4805 |
|
---|
4806 | static PyObject *_PyPopen(char *, int, int);
|
---|
4807 | static int _PyPclose(FILE *file);
|
---|
4808 |
|
---|
4809 | /*
|
---|
4810 | * Internal dictionary mapping popen* file pointers to process handles,
|
---|
4811 | * for use when retrieving the process exit code. See _PyPclose() below
|
---|
4812 | * for more information on this dictionary's use.
|
---|
4813 | */
|
---|
4814 | static PyObject *_PyPopenProcs = NULL;
|
---|
4815 |
|
---|
4816 |
|
---|
4817 | /* popen that works from a GUI.
|
---|
4818 | *
|
---|
4819 | * The result of this function is a pipe (file) connected to the
|
---|
4820 | * processes stdin or stdout, depending on the requested mode.
|
---|
4821 | */
|
---|
4822 |
|
---|
4823 | static PyObject *
|
---|
4824 | posix_popen(PyObject *self, PyObject *args)
|
---|
4825 | {
|
---|
4826 | PyObject *f;
|
---|
4827 | int tm = 0;
|
---|
4828 |
|
---|
4829 | char *cmdstring;
|
---|
4830 | char *mode = "r";
|
---|
4831 | int bufsize = -1;
|
---|
4832 | if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize))
|
---|
4833 | return NULL;
|
---|
4834 |
|
---|
4835 | if (*mode == 'r')
|
---|
4836 | tm = _O_RDONLY;
|
---|
4837 | else if (*mode != 'w') {
|
---|
4838 | PyErr_SetString(PyExc_ValueError, "popen() arg 2 must be 'r' or 'w'");
|
---|
4839 | return NULL;
|
---|
4840 | } else
|
---|
4841 | tm = _O_WRONLY;
|
---|
4842 |
|
---|
4843 | if (bufsize != -1) {
|
---|
4844 | PyErr_SetString(PyExc_ValueError, "popen() arg 3 must be -1");
|
---|
4845 | return NULL;
|
---|
4846 | }
|
---|
4847 |
|
---|
4848 | if (*(mode+1) == 't')
|
---|
4849 | f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
|
---|
4850 | else if (*(mode+1) == 'b')
|
---|
4851 | f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1);
|
---|
4852 | else
|
---|
4853 | f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1);
|
---|
4854 |
|
---|
4855 | return f;
|
---|
4856 | }
|
---|
4857 |
|
---|
4858 | /* Variation on win32pipe.popen
|
---|
4859 | *
|
---|
4860 | * The result of this function is a pipe (file) connected to the
|
---|
4861 | * process's stdin, and a pipe connected to the process's stdout.
|
---|
4862 | */
|
---|
4863 |
|
---|
4864 | static PyObject *
|
---|
4865 | win32_popen2(PyObject *self, PyObject *args)
|
---|
4866 | {
|
---|
4867 | PyObject *f;
|
---|
4868 | int tm=0;
|
---|
4869 |
|
---|
4870 | char *cmdstring;
|
---|
4871 | char *mode = "t";
|
---|
4872 | int bufsize = -1;
|
---|
4873 | if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize))
|
---|
4874 | return NULL;
|
---|
4875 |
|
---|
4876 | if (*mode == 't')
|
---|
4877 | tm = _O_TEXT;
|
---|
4878 | else if (*mode != 'b') {
|
---|
4879 | PyErr_SetString(PyExc_ValueError, "popen2() arg 2 must be 't' or 'b'");
|
---|
4880 | return NULL;
|
---|
4881 | } else
|
---|
4882 | tm = _O_BINARY;
|
---|
4883 |
|
---|
4884 | if (bufsize != -1) {
|
---|
4885 | PyErr_SetString(PyExc_ValueError, "popen2() arg 3 must be -1");
|
---|
4886 | return NULL;
|
---|
4887 | }
|
---|
4888 |
|
---|
4889 | f = _PyPopen(cmdstring, tm, POPEN_2);
|
---|
4890 |
|
---|
4891 | return f;
|
---|
4892 | }
|
---|
4893 |
|
---|
4894 | /*
|
---|
4895 | * Variation on <om win32pipe.popen>
|
---|
4896 | *
|
---|
4897 | * The result of this function is 3 pipes - the process's stdin,
|
---|
4898 | * stdout and stderr
|
---|
4899 | */
|
---|
4900 |
|
---|
4901 | static PyObject *
|
---|
4902 | win32_popen3(PyObject *self, PyObject *args)
|
---|
4903 | {
|
---|
4904 | PyObject *f;
|
---|
4905 | int tm = 0;
|
---|
4906 |
|
---|
4907 | char *cmdstring;
|
---|
4908 | char *mode = "t";
|
---|
4909 | int bufsize = -1;
|
---|
4910 | if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize))
|
---|
4911 | return NULL;
|
---|
4912 |
|
---|
4913 | if (*mode == 't')
|
---|
4914 | tm = _O_TEXT;
|
---|
4915 | else if (*mode != 'b') {
|
---|
4916 | PyErr_SetString(PyExc_ValueError, "popen3() arg 2 must be 't' or 'b'");
|
---|
4917 | return NULL;
|
---|
4918 | } else
|
---|
4919 | tm = _O_BINARY;
|
---|
4920 |
|
---|
4921 | if (bufsize != -1) {
|
---|
4922 | PyErr_SetString(PyExc_ValueError, "popen3() arg 3 must be -1");
|
---|
4923 | return NULL;
|
---|
4924 | }
|
---|
4925 |
|
---|
4926 | f = _PyPopen(cmdstring, tm, POPEN_3);
|
---|
4927 |
|
---|
4928 | return f;
|
---|
4929 | }
|
---|
4930 |
|
---|
4931 | /*
|
---|
4932 | * Variation on win32pipe.popen
|
---|
4933 | *
|
---|
4934 | * The result of this function is 2 pipes - the processes stdin,
|
---|
4935 | * and stdout+stderr combined as a single pipe.
|
---|
4936 | */
|
---|
4937 |
|
---|
4938 | static PyObject *
|
---|
4939 | win32_popen4(PyObject *self, PyObject *args)
|
---|
4940 | {
|
---|
4941 | PyObject *f;
|
---|
4942 | int tm = 0;
|
---|
4943 |
|
---|
4944 | char *cmdstring;
|
---|
4945 | char *mode = "t";
|
---|
4946 | int bufsize = -1;
|
---|
4947 | if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize))
|
---|
4948 | return NULL;
|
---|
4949 |
|
---|
4950 | if (*mode == 't')
|
---|
4951 | tm = _O_TEXT;
|
---|
4952 | else if (*mode != 'b') {
|
---|
4953 | PyErr_SetString(PyExc_ValueError, "popen4() arg 2 must be 't' or 'b'");
|
---|
4954 | return NULL;
|
---|
4955 | } else
|
---|
4956 | tm = _O_BINARY;
|
---|
4957 |
|
---|
4958 | if (bufsize != -1) {
|
---|
4959 | PyErr_SetString(PyExc_ValueError, "popen4() arg 3 must be -1");
|
---|
4960 | return NULL;
|
---|
4961 | }
|
---|
4962 |
|
---|
4963 | f = _PyPopen(cmdstring, tm, POPEN_4);
|
---|
4964 |
|
---|
4965 | return f;
|
---|
4966 | }
|
---|
4967 |
|
---|
4968 | static BOOL
|
---|
4969 | _PyPopenCreateProcess(char *cmdstring,
|
---|
4970 | HANDLE hStdin,
|
---|
4971 | HANDLE hStdout,
|
---|
4972 | HANDLE hStderr,
|
---|
4973 | HANDLE *hProcess)
|
---|
4974 | {
|
---|
4975 | PROCESS_INFORMATION piProcInfo;
|
---|
4976 | STARTUPINFO siStartInfo;
|
---|
4977 | DWORD dwProcessFlags = 0; /* no NEW_CONSOLE by default for Ctrl+C handling */
|
---|
4978 | char *s1,*s2, *s3 = " /c ";
|
---|
4979 | const char *szConsoleSpawn = "w9xpopen.exe";
|
---|
4980 | int i;
|
---|
4981 | Py_ssize_t x;
|
---|
4982 |
|
---|
4983 | if (i = GetEnvironmentVariable("COMSPEC",NULL,0)) {
|
---|
4984 | char *comshell;
|
---|
4985 |
|
---|
4986 | s1 = (char *)alloca(i);
|
---|
4987 | if (!(x = GetEnvironmentVariable("COMSPEC", s1, i)))
|
---|
4988 | /* x < i, so x fits into an integer */
|
---|
4989 | return (int)x;
|
---|
4990 |
|
---|
4991 | /* Explicitly check if we are using COMMAND.COM. If we are
|
---|
4992 | * then use the w9xpopen hack.
|
---|
4993 | */
|
---|
4994 | comshell = s1 + x;
|
---|
4995 | while (comshell >= s1 && *comshell != '\\')
|
---|
4996 | --comshell;
|
---|
4997 | ++comshell;
|
---|
4998 |
|
---|
4999 | if (GetVersion() < 0x80000000 &&
|
---|
5000 | _stricmp(comshell, "command.com") != 0) {
|
---|
5001 | /* NT/2000 and not using command.com. */
|
---|
5002 | x = i + strlen(s3) + strlen(cmdstring) + 1;
|
---|
5003 | s2 = (char *)alloca(x);
|
---|
5004 | ZeroMemory(s2, x);
|
---|
5005 | PyOS_snprintf(s2, x, "%s%s%s", s1, s3, cmdstring);
|
---|
5006 | }
|
---|
5007 | else {
|
---|
5008 | /*
|
---|
5009 | * Oh gag, we're on Win9x or using COMMAND.COM. Use
|
---|
5010 | * the workaround listed in KB: Q150956
|
---|
5011 | */
|
---|
5012 | char modulepath[_MAX_PATH];
|
---|
5013 | struct stat statinfo;
|
---|
5014 | GetModuleFileName(NULL, modulepath, sizeof(modulepath));
|
---|
5015 | for (x = i = 0; modulepath[i]; i++)
|
---|
5016 | if (modulepath[i] == SEP)
|
---|
5017 | x = i+1;
|
---|
5018 | modulepath[x] = '\0';
|
---|
5019 | /* Create the full-name to w9xpopen, so we can test it exists */
|
---|
5020 | strncat(modulepath,
|
---|
5021 | szConsoleSpawn,
|
---|
5022 | (sizeof(modulepath)/sizeof(modulepath[0]))
|
---|
5023 | -strlen(modulepath));
|
---|
5024 | if (stat(modulepath, &statinfo) != 0) {
|
---|
5025 | size_t mplen = sizeof(modulepath)/sizeof(modulepath[0]);
|
---|
5026 | /* Eeek - file-not-found - possibly an embedding
|
---|
5027 | situation - see if we can locate it in sys.prefix
|
---|
5028 | */
|
---|
5029 | strncpy(modulepath,
|
---|
5030 | Py_GetExecPrefix(),
|
---|
5031 | mplen);
|
---|
5032 | modulepath[mplen-1] = '\0';
|
---|
5033 | if (modulepath[strlen(modulepath)-1] != '\\')
|
---|
5034 | strcat(modulepath, "\\");
|
---|
5035 | strncat(modulepath,
|
---|
5036 | szConsoleSpawn,
|
---|
5037 | mplen-strlen(modulepath));
|
---|
5038 | /* No where else to look - raise an easily identifiable
|
---|
5039 | error, rather than leaving Windows to report
|
---|
5040 | "file not found" - as the user is probably blissfully
|
---|
5041 | unaware this shim EXE is used, and it will confuse them.
|
---|
5042 | (well, it confused me for a while ;-)
|
---|
5043 | */
|
---|
5044 | if (stat(modulepath, &statinfo) != 0) {
|
---|
5045 | PyErr_Format(PyExc_RuntimeError,
|
---|
5046 | "Can not locate '%s' which is needed "
|
---|
5047 | "for popen to work with your shell "
|
---|
5048 | "or platform.",
|
---|
5049 | szConsoleSpawn);
|
---|
5050 | return FALSE;
|
---|
5051 | }
|
---|
5052 | }
|
---|
5053 | x = i + strlen(s3) + strlen(cmdstring) + 1 +
|
---|
5054 | strlen(modulepath) +
|
---|
5055 | strlen(szConsoleSpawn) + 1;
|
---|
5056 |
|
---|
5057 | s2 = (char *)alloca(x);
|
---|
5058 | ZeroMemory(s2, x);
|
---|
5059 | /* To maintain correct argument passing semantics,
|
---|
5060 | we pass the command-line as it stands, and allow
|
---|
5061 | quoting to be applied. w9xpopen.exe will then
|
---|
5062 | use its argv vector, and re-quote the necessary
|
---|
5063 | args for the ultimate child process.
|
---|
5064 | */
|
---|
5065 | PyOS_snprintf(
|
---|
5066 | s2, x,
|
---|
5067 | "\"%s\" %s%s%s",
|
---|
5068 | modulepath,
|
---|
5069 | s1,
|
---|
5070 | s3,
|
---|
5071 | cmdstring);
|
---|
5072 | /* Not passing CREATE_NEW_CONSOLE has been known to
|
---|
5073 | cause random failures on win9x. Specifically a
|
---|
5074 | dialog:
|
---|
5075 | "Your program accessed mem currently in use at xxx"
|
---|
5076 | and a hopeful warning about the stability of your
|
---|
5077 | system.
|
---|
5078 | Cost is Ctrl+C won't kill children, but anyone
|
---|
5079 | who cares can have a go!
|
---|
5080 | */
|
---|
5081 | dwProcessFlags |= CREATE_NEW_CONSOLE;
|
---|
5082 | }
|
---|
5083 | }
|
---|
5084 |
|
---|
5085 | /* Could be an else here to try cmd.exe / command.com in the path
|
---|
5086 | Now we'll just error out.. */
|
---|
5087 | else {
|
---|
5088 | PyErr_SetString(PyExc_RuntimeError,
|
---|
5089 | "Cannot locate a COMSPEC environment variable to "
|
---|
5090 | "use as the shell");
|
---|
5091 | return FALSE;
|
---|
5092 | }
|
---|
5093 |
|
---|
5094 | ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
|
---|
5095 | siStartInfo.cb = sizeof(STARTUPINFO);
|
---|
5096 | siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
|
---|
5097 | siStartInfo.hStdInput = hStdin;
|
---|
5098 | siStartInfo.hStdOutput = hStdout;
|
---|
5099 | siStartInfo.hStdError = hStderr;
|
---|
5100 | siStartInfo.wShowWindow = SW_HIDE;
|
---|
5101 |
|
---|
5102 | if (CreateProcess(NULL,
|
---|
5103 | s2,
|
---|
5104 | NULL,
|
---|
5105 | NULL,
|
---|
5106 | TRUE,
|
---|
5107 | dwProcessFlags,
|
---|
5108 | NULL,
|
---|
5109 | NULL,
|
---|
5110 | &siStartInfo,
|
---|
5111 | &piProcInfo) ) {
|
---|
5112 | /* Close the handles now so anyone waiting is woken. */
|
---|
5113 | CloseHandle(piProcInfo.hThread);
|
---|
5114 |
|
---|
5115 | /* Return process handle */
|
---|
5116 | *hProcess = piProcInfo.hProcess;
|
---|
5117 | return TRUE;
|
---|
5118 | }
|
---|
5119 | win32_error("CreateProcess", s2);
|
---|
5120 | return FALSE;
|
---|
5121 | }
|
---|
5122 |
|
---|
5123 | /* The following code is based off of KB: Q190351 */
|
---|
5124 |
|
---|
5125 | static PyObject *
|
---|
5126 | _PyPopen(char *cmdstring, int mode, int n)
|
---|
5127 | {
|
---|
5128 | HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr,
|
---|
5129 | hChildStderrRd, hChildStderrWr, hChildStdinWrDup, hChildStdoutRdDup,
|
---|
5130 | hChildStderrRdDup, hProcess; /* hChildStdoutWrDup; */
|
---|
5131 |
|
---|
5132 | SECURITY_ATTRIBUTES saAttr;
|
---|
5133 | BOOL fSuccess;
|
---|
5134 | int fd1, fd2, fd3;
|
---|
5135 | FILE *f1, *f2, *f3;
|
---|
5136 | long file_count;
|
---|
5137 | PyObject *f;
|
---|
5138 |
|
---|
5139 | saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
|
---|
5140 | saAttr.bInheritHandle = TRUE;
|
---|
5141 | saAttr.lpSecurityDescriptor = NULL;
|
---|
5142 |
|
---|
5143 | if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0))
|
---|
5144 | return win32_error("CreatePipe", NULL);
|
---|
5145 |
|
---|
5146 | /* Create new output read handle and the input write handle. Set
|
---|
5147 | * the inheritance properties to FALSE. Otherwise, the child inherits
|
---|
5148 | * these handles; resulting in non-closeable handles to the pipes
|
---|
5149 | * being created. */
|
---|
5150 | fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr,
|
---|
5151 | GetCurrentProcess(), &hChildStdinWrDup, 0,
|
---|
5152 | FALSE,
|
---|
5153 | DUPLICATE_SAME_ACCESS);
|
---|
5154 | if (!fSuccess)
|
---|
5155 | return win32_error("DuplicateHandle", NULL);
|
---|
5156 |
|
---|
5157 | /* Close the inheritable version of ChildStdin
|
---|
5158 | that we're using. */
|
---|
5159 | CloseHandle(hChildStdinWr);
|
---|
5160 |
|
---|
5161 | if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0))
|
---|
5162 | return win32_error("CreatePipe", NULL);
|
---|
5163 |
|
---|
5164 | fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd,
|
---|
5165 | GetCurrentProcess(), &hChildStdoutRdDup, 0,
|
---|
5166 | FALSE, DUPLICATE_SAME_ACCESS);
|
---|
5167 | if (!fSuccess)
|
---|
5168 | return win32_error("DuplicateHandle", NULL);
|
---|
5169 |
|
---|
5170 | /* Close the inheritable version of ChildStdout
|
---|
5171 | that we're using. */
|
---|
5172 | CloseHandle(hChildStdoutRd);
|
---|
5173 |
|
---|
5174 | if (n != POPEN_4) {
|
---|
5175 | if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0))
|
---|
5176 | return win32_error("CreatePipe", NULL);
|
---|
5177 | fSuccess = DuplicateHandle(GetCurrentProcess(),
|
---|
5178 | hChildStderrRd,
|
---|
5179 | GetCurrentProcess(),
|
---|
5180 | &hChildStderrRdDup, 0,
|
---|
5181 | FALSE, DUPLICATE_SAME_ACCESS);
|
---|
5182 | if (!fSuccess)
|
---|
5183 | return win32_error("DuplicateHandle", NULL);
|
---|
5184 | /* Close the inheritable version of ChildStdErr that we're using. */
|
---|
5185 | CloseHandle(hChildStderrRd);
|
---|
5186 | }
|
---|
5187 |
|
---|
5188 | switch (n) {
|
---|
5189 | case POPEN_1:
|
---|
5190 | switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) {
|
---|
5191 | case _O_WRONLY | _O_TEXT:
|
---|
5192 | /* Case for writing to child Stdin in text mode. */
|
---|
5193 | fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
|
---|
5194 | f1 = _fdopen(fd1, "w");
|
---|
5195 | f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose);
|
---|
5196 | PyFile_SetBufSize(f, 0);
|
---|
5197 | /* We don't care about these pipes anymore, so close them. */
|
---|
5198 | CloseHandle(hChildStdoutRdDup);
|
---|
5199 | CloseHandle(hChildStderrRdDup);
|
---|
5200 | break;
|
---|
5201 |
|
---|
5202 | case _O_RDONLY | _O_TEXT:
|
---|
5203 | /* Case for reading from child Stdout in text mode. */
|
---|
5204 | fd1 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
|
---|
5205 | f1 = _fdopen(fd1, "r");
|
---|
5206 | f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose);
|
---|
5207 | PyFile_SetBufSize(f, 0);
|
---|
5208 | /* We don't care about these pipes anymore, so close them. */
|
---|
5209 | CloseHandle(hChildStdinWrDup);
|
---|
5210 | CloseHandle(hChildStderrRdDup);
|
---|
5211 | break;
|
---|
5212 |
|
---|
5213 | case _O_RDONLY | _O_BINARY:
|
---|
5214 | /* Case for readinig from child Stdout in binary mode. */
|
---|
5215 | fd1 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
|
---|
5216 | f1 = _fdopen(fd1, "rb");
|
---|
5217 | f = PyFile_FromFile(f1, cmdstring, "rb", _PyPclose);
|
---|
5218 | PyFile_SetBufSize(f, 0);
|
---|
5219 | /* We don't care about these pipes anymore, so close them. */
|
---|
5220 | CloseHandle(hChildStdinWrDup);
|
---|
5221 | CloseHandle(hChildStderrRdDup);
|
---|
5222 | break;
|
---|
5223 |
|
---|
5224 | case _O_WRONLY | _O_BINARY:
|
---|
5225 | /* Case for writing to child Stdin in binary mode. */
|
---|
5226 | fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
|
---|
5227 | f1 = _fdopen(fd1, "wb");
|
---|
5228 | f = PyFile_FromFile(f1, cmdstring, "wb", _PyPclose);
|
---|
5229 | PyFile_SetBufSize(f, 0);
|
---|
5230 | /* We don't care about these pipes anymore, so close them. */
|
---|
5231 | CloseHandle(hChildStdoutRdDup);
|
---|
5232 | CloseHandle(hChildStderrRdDup);
|
---|
5233 | break;
|
---|
5234 | }
|
---|
5235 | file_count = 1;
|
---|
5236 | break;
|
---|
5237 |
|
---|
5238 | case POPEN_2:
|
---|
5239 | case POPEN_4:
|
---|
5240 | {
|
---|
5241 | char *m1, *m2;
|
---|
5242 | PyObject *p1, *p2;
|
---|
5243 |
|
---|
5244 | if (mode & _O_TEXT) {
|
---|
5245 | m1 = "r";
|
---|
5246 | m2 = "w";
|
---|
5247 | } else {
|
---|
5248 | m1 = "rb";
|
---|
5249 | m2 = "wb";
|
---|
5250 | }
|
---|
5251 |
|
---|
5252 | fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
|
---|
5253 | f1 = _fdopen(fd1, m2);
|
---|
5254 | fd2 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
|
---|
5255 | f2 = _fdopen(fd2, m1);
|
---|
5256 | p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
|
---|
5257 | PyFile_SetBufSize(p1, 0);
|
---|
5258 | p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
|
---|
5259 | PyFile_SetBufSize(p2, 0);
|
---|
5260 |
|
---|
5261 | if (n != 4)
|
---|
5262 | CloseHandle(hChildStderrRdDup);
|
---|
5263 |
|
---|
5264 | f = PyTuple_Pack(2,p1,p2);
|
---|
5265 | Py_XDECREF(p1);
|
---|
5266 | Py_XDECREF(p2);
|
---|
5267 | file_count = 2;
|
---|
5268 | break;
|
---|
5269 | }
|
---|
5270 |
|
---|
5271 | case POPEN_3:
|
---|
5272 | {
|
---|
5273 | char *m1, *m2;
|
---|
5274 | PyObject *p1, *p2, *p3;
|
---|
5275 |
|
---|
5276 | if (mode & _O_TEXT) {
|
---|
5277 | m1 = "r";
|
---|
5278 | m2 = "w";
|
---|
5279 | } else {
|
---|
5280 | m1 = "rb";
|
---|
5281 | m2 = "wb";
|
---|
5282 | }
|
---|
5283 |
|
---|
5284 | fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode);
|
---|
5285 | f1 = _fdopen(fd1, m2);
|
---|
5286 | fd2 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode);
|
---|
5287 | f2 = _fdopen(fd2, m1);
|
---|
5288 | fd3 = _open_osfhandle((Py_intptr_t)hChildStderrRdDup, mode);
|
---|
5289 | f3 = _fdopen(fd3, m1);
|
---|
5290 | p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose);
|
---|
5291 | p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose);
|
---|
5292 | p3 = PyFile_FromFile(f3, cmdstring, m1, _PyPclose);
|
---|
5293 | PyFile_SetBufSize(p1, 0);
|
---|
5294 | PyFile_SetBufSize(p2, 0);
|
---|
5295 | PyFile_SetBufSize(p3, 0);
|
---|
5296 | f = PyTuple_Pack(3,p1,p2,p3);
|
---|
5297 | Py_XDECREF(p1);
|
---|
5298 | Py_XDECREF(p2);
|
---|
5299 | Py_XDECREF(p3);
|
---|
5300 | file_count = 3;
|
---|
5301 | break;
|
---|
5302 | }
|
---|
5303 | }
|
---|
5304 |
|
---|
5305 | if (n == POPEN_4) {
|
---|
5306 | if (!_PyPopenCreateProcess(cmdstring,
|
---|
5307 | hChildStdinRd,
|
---|
5308 | hChildStdoutWr,
|
---|
5309 | hChildStdoutWr,
|
---|
5310 | &hProcess))
|
---|
5311 | return NULL;
|
---|
5312 | }
|
---|
5313 | else {
|
---|
5314 | if (!_PyPopenCreateProcess(cmdstring,
|
---|
5315 | hChildStdinRd,
|
---|
5316 | hChildStdoutWr,
|
---|
5317 | hChildStderrWr,
|
---|
5318 | &hProcess))
|
---|
5319 | return NULL;
|
---|
5320 | }
|
---|
5321 |
|
---|
5322 | /*
|
---|
5323 | * Insert the files we've created into the process dictionary
|
---|
5324 | * all referencing the list with the process handle and the
|
---|
5325 | * initial number of files (see description below in _PyPclose).
|
---|
5326 | * Since if _PyPclose later tried to wait on a process when all
|
---|
5327 | * handles weren't closed, it could create a deadlock with the
|
---|
5328 | * child, we spend some energy here to try to ensure that we
|
---|
5329 | * either insert all file handles into the dictionary or none
|
---|
5330 | * at all. It's a little clumsy with the various popen modes
|
---|
5331 | * and variable number of files involved.
|
---|
5332 | */
|
---|
5333 | if (!_PyPopenProcs) {
|
---|
5334 | _PyPopenProcs = PyDict_New();
|
---|
5335 | }
|
---|
5336 |
|
---|
5337 | if (_PyPopenProcs) {
|
---|
5338 | PyObject *procObj, *hProcessObj, *intObj, *fileObj[3];
|
---|
5339 | int ins_rc[3];
|
---|
5340 |
|
---|
5341 | fileObj[0] = fileObj[1] = fileObj[2] = NULL;
|
---|
5342 | ins_rc[0] = ins_rc[1] = ins_rc[2] = 0;
|
---|
5343 |
|
---|
5344 | procObj = PyList_New(2);
|
---|
5345 | hProcessObj = PyLong_FromVoidPtr(hProcess);
|
---|
5346 | intObj = PyInt_FromLong(file_count);
|
---|
5347 |
|
---|
5348 | if (procObj && hProcessObj && intObj) {
|
---|
5349 | PyList_SetItem(procObj,0,hProcessObj);
|
---|
5350 | PyList_SetItem(procObj,1,intObj);
|
---|
5351 |
|
---|
5352 | fileObj[0] = PyLong_FromVoidPtr(f1);
|
---|
5353 | if (fileObj[0]) {
|
---|
5354 | ins_rc[0] = PyDict_SetItem(_PyPopenProcs,
|
---|
5355 | fileObj[0],
|
---|
5356 | procObj);
|
---|
5357 | }
|
---|
5358 | if (file_count >= 2) {
|
---|
5359 | fileObj[1] = PyLong_FromVoidPtr(f2);
|
---|
5360 | if (fileObj[1]) {
|
---|
5361 | ins_rc[1] = PyDict_SetItem(_PyPopenProcs,
|
---|
5362 | fileObj[1],
|
---|
5363 | procObj);
|
---|
5364 | }
|
---|
5365 | }
|
---|
5366 | if (file_count >= 3) {
|
---|
5367 | fileObj[2] = PyLong_FromVoidPtr(f3);
|
---|
5368 | if (fileObj[2]) {
|
---|
5369 | ins_rc[2] = PyDict_SetItem(_PyPopenProcs,
|
---|
5370 | fileObj[2],
|
---|
5371 | procObj);
|
---|
5372 | }
|
---|
5373 | }
|
---|
5374 |
|
---|
5375 | if (ins_rc[0] < 0 || !fileObj[0] ||
|
---|
5376 | ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) ||
|
---|
5377 | ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) {
|
---|
5378 | /* Something failed - remove any dictionary
|
---|
5379 | * entries that did make it.
|
---|
5380 | */
|
---|
5381 | if (!ins_rc[0] && fileObj[0]) {
|
---|
5382 | PyDict_DelItem(_PyPopenProcs,
|
---|
5383 | fileObj[0]);
|
---|
5384 | }
|
---|
5385 | if (!ins_rc[1] && fileObj[1]) {
|
---|
5386 | PyDict_DelItem(_PyPopenProcs,
|
---|
5387 | fileObj[1]);
|
---|
5388 | }
|
---|
5389 | if (!ins_rc[2] && fileObj[2]) {
|
---|
5390 | PyDict_DelItem(_PyPopenProcs,
|
---|
5391 | fileObj[2]);
|
---|
5392 | }
|
---|
5393 | }
|
---|
5394 | }
|
---|
5395 |
|
---|
5396 | /*
|
---|
5397 | * Clean up our localized references for the dictionary keys
|
---|
5398 | * and value since PyDict_SetItem will Py_INCREF any copies
|
---|
5399 | * that got placed in the dictionary.
|
---|
5400 | */
|
---|
5401 | Py_XDECREF(procObj);
|
---|
5402 | Py_XDECREF(fileObj[0]);
|
---|
5403 | Py_XDECREF(fileObj[1]);
|
---|
5404 | Py_XDECREF(fileObj[2]);
|
---|
5405 | }
|
---|
5406 |
|
---|
5407 | /* Child is launched. Close the parents copy of those pipe
|
---|
5408 | * handles that only the child should have open. You need to
|
---|
5409 | * make sure that no handles to the write end of the output pipe
|
---|
5410 | * are maintained in this process or else the pipe will not close
|
---|
5411 | * when the child process exits and the ReadFile will hang. */
|
---|
5412 |
|
---|
5413 | if (!CloseHandle(hChildStdinRd))
|
---|
5414 | return win32_error("CloseHandle", NULL);
|
---|
5415 |
|
---|
5416 | if (!CloseHandle(hChildStdoutWr))
|
---|
5417 | return win32_error("CloseHandle", NULL);
|
---|
5418 |
|
---|
5419 | if ((n != 4) && (!CloseHandle(hChildStderrWr)))
|
---|
5420 | return win32_error("CloseHandle", NULL);
|
---|
5421 |
|
---|
5422 | return f;
|
---|
5423 | }
|
---|
5424 |
|
---|
5425 | /*
|
---|
5426 | * Wrapper for fclose() to use for popen* files, so we can retrieve the
|
---|
5427 | * exit code for the child process and return as a result of the close.
|
---|
5428 | *
|
---|
5429 | * This function uses the _PyPopenProcs dictionary in order to map the
|
---|
5430 | * input file pointer to information about the process that was
|
---|
5431 | * originally created by the popen* call that created the file pointer.
|
---|
5432 | * The dictionary uses the file pointer as a key (with one entry
|
---|
5433 | * inserted for each file returned by the original popen* call) and a
|
---|
5434 | * single list object as the value for all files from a single call.
|
---|
5435 | * The list object contains the Win32 process handle at [0], and a file
|
---|
5436 | * count at [1], which is initialized to the total number of file
|
---|
5437 | * handles using that list.
|
---|
5438 | *
|
---|
5439 | * This function closes whichever handle it is passed, and decrements
|
---|
5440 | * the file count in the dictionary for the process handle pointed to
|
---|
5441 | * by this file. On the last close (when the file count reaches zero),
|
---|
5442 | * this function will wait for the child process and then return its
|
---|
5443 | * exit code as the result of the close() operation. This permits the
|
---|
5444 | * files to be closed in any order - it is always the close() of the
|
---|
5445 | * final handle that will return the exit code.
|
---|
5446 | *
|
---|
5447 | * NOTE: This function is currently called with the GIL released.
|
---|
5448 | * hence we use the GILState API to manage our state.
|
---|
5449 | */
|
---|
5450 |
|
---|
5451 | static int _PyPclose(FILE *file)
|
---|
5452 | {
|
---|
5453 | int result;
|
---|
5454 | DWORD exit_code;
|
---|
5455 | HANDLE hProcess;
|
---|
5456 | PyObject *procObj, *hProcessObj, *intObj, *fileObj;
|
---|
5457 | long file_count;
|
---|
5458 | #ifdef WITH_THREAD
|
---|
5459 | PyGILState_STATE state;
|
---|
5460 | #endif
|
---|
5461 |
|
---|
5462 | /* Close the file handle first, to ensure it can't block the
|
---|
5463 | * child from exiting if it's the last handle.
|
---|
5464 | */
|
---|
5465 | result = fclose(file);
|
---|
5466 | #ifdef WITH_THREAD
|
---|
5467 | state = PyGILState_Ensure();
|
---|
5468 | #endif
|
---|
5469 | if (_PyPopenProcs) {
|
---|
5470 | if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
|
---|
5471 | (procObj = PyDict_GetItem(_PyPopenProcs,
|
---|
5472 | fileObj)) != NULL &&
|
---|
5473 | (hProcessObj = PyList_GetItem(procObj,0)) != NULL &&
|
---|
5474 | (intObj = PyList_GetItem(procObj,1)) != NULL) {
|
---|
5475 |
|
---|
5476 | hProcess = PyLong_AsVoidPtr(hProcessObj);
|
---|
5477 | file_count = PyInt_AsLong(intObj);
|
---|
5478 |
|
---|
5479 | if (file_count > 1) {
|
---|
5480 | /* Still other files referencing process */
|
---|
5481 | file_count--;
|
---|
5482 | PyList_SetItem(procObj,1,
|
---|
5483 | PyInt_FromLong(file_count));
|
---|
5484 | } else {
|
---|
5485 | /* Last file for this process */
|
---|
5486 | if (result != EOF &&
|
---|
5487 | WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED &&
|
---|
5488 | GetExitCodeProcess(hProcess, &exit_code)) {
|
---|
5489 | /* Possible truncation here in 16-bit environments, but
|
---|
5490 | * real exit codes are just the lower byte in any event.
|
---|
5491 | */
|
---|
5492 | result = exit_code;
|
---|
5493 | } else {
|
---|
5494 | /* Indicate failure - this will cause the file object
|
---|
5495 | * to raise an I/O error and translate the last Win32
|
---|
5496 | * error code from errno. We do have a problem with
|
---|
5497 | * last errors that overlap the normal errno table,
|
---|
5498 | * but that's a consistent problem with the file object.
|
---|
5499 | */
|
---|
5500 | if (result != EOF) {
|
---|
5501 | /* If the error wasn't from the fclose(), then
|
---|
5502 | * set errno for the file object error handling.
|
---|
5503 | */
|
---|
5504 | errno = GetLastError();
|
---|
5505 | }
|
---|
5506 | result = -1;
|
---|
5507 | }
|
---|
5508 |
|
---|
5509 | /* Free up the native handle at this point */
|
---|
5510 | CloseHandle(hProcess);
|
---|
5511 | }
|
---|
5512 |
|
---|
5513 | /* Remove this file pointer from dictionary */
|
---|
5514 | PyDict_DelItem(_PyPopenProcs, fileObj);
|
---|
5515 |
|
---|
5516 | if (PyDict_Size(_PyPopenProcs) == 0) {
|
---|
5517 | Py_DECREF(_PyPopenProcs);
|
---|
5518 | _PyPopenProcs = NULL;
|
---|
5519 | }
|
---|
5520 |
|
---|
5521 | } /* if object retrieval ok */
|
---|
5522 |
|
---|
5523 | Py_XDECREF(fileObj);
|
---|
5524 | } /* if _PyPopenProcs */
|
---|
5525 |
|
---|
5526 | #ifdef WITH_THREAD
|
---|
5527 | PyGILState_Release(state);
|
---|
5528 | #endif
|
---|
5529 | return result;
|
---|
5530 | }
|
---|
5531 |
|
---|
5532 | #else /* which OS? */
|
---|
5533 | static PyObject *
|
---|
5534 | posix_popen(PyObject *self, PyObject *args)
|
---|
5535 | {
|
---|
5536 | char *name;
|
---|
5537 | char *mode = "r";
|
---|
5538 | int bufsize = -1;
|
---|
5539 | FILE *fp;
|
---|
5540 | PyObject *f;
|
---|
5541 | if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize))
|
---|
5542 | return NULL;
|
---|
5543 | /* Strip mode of binary or text modifiers */
|
---|
5544 | if (strcmp(mode, "rb") == 0 || strcmp(mode, "rt") == 0)
|
---|
5545 | mode = "r";
|
---|
5546 | else if (strcmp(mode, "wb") == 0 || strcmp(mode, "wt") == 0)
|
---|
5547 | mode = "w";
|
---|
5548 | Py_BEGIN_ALLOW_THREADS
|
---|
5549 | fp = popen(name, mode);
|
---|
5550 | Py_END_ALLOW_THREADS
|
---|
5551 | if (fp == NULL)
|
---|
5552 | return posix_error();
|
---|
5553 | f = PyFile_FromFile(fp, name, mode, pclose);
|
---|
5554 | if (f != NULL)
|
---|
5555 | PyFile_SetBufSize(f, bufsize);
|
---|
5556 | return f;
|
---|
5557 | }
|
---|
5558 |
|
---|
5559 | #endif /* PYOS_??? */
|
---|
5560 | #endif /* HAVE_POPEN */
|
---|
5561 |
|
---|
5562 |
|
---|
5563 | #ifdef HAVE_SETUID
|
---|
5564 | PyDoc_STRVAR(posix_setuid__doc__,
|
---|
5565 | "setuid(uid)\n\n\
|
---|
5566 | Set the current process's user id.");
|
---|
5567 |
|
---|
5568 | static PyObject *
|
---|
5569 | posix_setuid(PyObject *self, PyObject *args)
|
---|
5570 | {
|
---|
5571 | long uid_arg;
|
---|
5572 | uid_t uid;
|
---|
5573 | if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg))
|
---|
5574 | return NULL;
|
---|
5575 | uid = uid_arg;
|
---|
5576 | if (uid != uid_arg) {
|
---|
5577 | PyErr_SetString(PyExc_OverflowError, "user id too big");
|
---|
5578 | return NULL;
|
---|
5579 | }
|
---|
5580 | if (setuid(uid) < 0)
|
---|
5581 | return posix_error();
|
---|
5582 | Py_INCREF(Py_None);
|
---|
5583 | return Py_None;
|
---|
5584 | }
|
---|
5585 | #endif /* HAVE_SETUID */
|
---|
5586 |
|
---|
5587 |
|
---|
5588 | #ifdef HAVE_SETEUID
|
---|
5589 | PyDoc_STRVAR(posix_seteuid__doc__,
|
---|
5590 | "seteuid(uid)\n\n\
|
---|
5591 | Set the current process's effective user id.");
|
---|
5592 |
|
---|
5593 | static PyObject *
|
---|
5594 | posix_seteuid (PyObject *self, PyObject *args)
|
---|
5595 | {
|
---|
5596 | long euid_arg;
|
---|
5597 | uid_t euid;
|
---|
5598 | if (!PyArg_ParseTuple(args, "l", &euid_arg))
|
---|
5599 | return NULL;
|
---|
5600 | euid = euid_arg;
|
---|
5601 | if (euid != euid_arg) {
|
---|
5602 | PyErr_SetString(PyExc_OverflowError, "user id too big");
|
---|
5603 | return NULL;
|
---|
5604 | }
|
---|
5605 | if (seteuid(euid) < 0) {
|
---|
5606 | return posix_error();
|
---|
5607 | } else {
|
---|
5608 | Py_INCREF(Py_None);
|
---|
5609 | return Py_None;
|
---|
5610 | }
|
---|
5611 | }
|
---|
5612 | #endif /* HAVE_SETEUID */
|
---|
5613 |
|
---|
5614 | #ifdef HAVE_SETEGID
|
---|
5615 | PyDoc_STRVAR(posix_setegid__doc__,
|
---|
5616 | "setegid(gid)\n\n\
|
---|
5617 | Set the current process's effective group id.");
|
---|
5618 |
|
---|
5619 | static PyObject *
|
---|
5620 | posix_setegid (PyObject *self, PyObject *args)
|
---|
5621 | {
|
---|
5622 | long egid_arg;
|
---|
5623 | gid_t egid;
|
---|
5624 | if (!PyArg_ParseTuple(args, "l", &egid_arg))
|
---|
5625 | return NULL;
|
---|
5626 | egid = egid_arg;
|
---|
5627 | if (egid != egid_arg) {
|
---|
5628 | PyErr_SetString(PyExc_OverflowError, "group id too big");
|
---|
5629 | return NULL;
|
---|
5630 | }
|
---|
5631 | if (setegid(egid) < 0) {
|
---|
5632 | return posix_error();
|
---|
5633 | } else {
|
---|
5634 | Py_INCREF(Py_None);
|
---|
5635 | return Py_None;
|
---|
5636 | }
|
---|
5637 | }
|
---|
5638 | #endif /* HAVE_SETEGID */
|
---|
5639 |
|
---|
5640 | #ifdef HAVE_SETREUID
|
---|
5641 | PyDoc_STRVAR(posix_setreuid__doc__,
|
---|
5642 | "setreuid(ruid, euid)\n\n\
|
---|
5643 | Set the current process's real and effective user ids.");
|
---|
5644 |
|
---|
5645 | static PyObject *
|
---|
5646 | posix_setreuid (PyObject *self, PyObject *args)
|
---|
5647 | {
|
---|
5648 | long ruid_arg, euid_arg;
|
---|
5649 | uid_t ruid, euid;
|
---|
5650 | if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg))
|
---|
5651 | return NULL;
|
---|
5652 | if (ruid_arg == -1)
|
---|
5653 | ruid = (uid_t)-1; /* let the compiler choose how -1 fits */
|
---|
5654 | else
|
---|
5655 | ruid = ruid_arg; /* otherwise, assign from our long */
|
---|
5656 | if (euid_arg == -1)
|
---|
5657 | euid = (uid_t)-1;
|
---|
5658 | else
|
---|
5659 | euid = euid_arg;
|
---|
5660 | if ((euid_arg != -1 && euid != euid_arg) ||
|
---|
5661 | (ruid_arg != -1 && ruid != ruid_arg)) {
|
---|
5662 | PyErr_SetString(PyExc_OverflowError, "user id too big");
|
---|
5663 | return NULL;
|
---|
5664 | }
|
---|
5665 | if (setreuid(ruid, euid) < 0) {
|
---|
5666 | return posix_error();
|
---|
5667 | } else {
|
---|
5668 | Py_INCREF(Py_None);
|
---|
5669 | return Py_None;
|
---|
5670 | }
|
---|
5671 | }
|
---|
5672 | #endif /* HAVE_SETREUID */
|
---|
5673 |
|
---|
5674 | #ifdef HAVE_SETREGID
|
---|
5675 | PyDoc_STRVAR(posix_setregid__doc__,
|
---|
5676 | "setregid(rgid, egid)\n\n\
|
---|
5677 | Set the current process's real and effective group ids.");
|
---|
5678 |
|
---|
5679 | static PyObject *
|
---|
5680 | posix_setregid (PyObject *self, PyObject *args)
|
---|
5681 | {
|
---|
5682 | long rgid_arg, egid_arg;
|
---|
5683 | gid_t rgid, egid;
|
---|
5684 | if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg))
|
---|
5685 | return NULL;
|
---|
5686 | if (rgid_arg == -1)
|
---|
5687 | rgid = (gid_t)-1; /* let the compiler choose how -1 fits */
|
---|
5688 | else
|
---|
5689 | rgid = rgid_arg; /* otherwise, assign from our long */
|
---|
5690 | if (egid_arg == -1)
|
---|
5691 | egid = (gid_t)-1;
|
---|
5692 | else
|
---|
5693 | egid = egid_arg;
|
---|
5694 | if ((egid_arg != -1 && egid != egid_arg) ||
|
---|
5695 | (rgid_arg != -1 && rgid != rgid_arg)) {
|
---|
5696 | PyErr_SetString(PyExc_OverflowError, "group id too big");
|
---|
5697 | return NULL;
|
---|
5698 | }
|
---|
5699 | if (setregid(rgid, egid) < 0) {
|
---|
5700 | return posix_error();
|
---|
5701 | } else {
|
---|
5702 | Py_INCREF(Py_None);
|
---|
5703 | return Py_None;
|
---|
5704 | }
|
---|
5705 | }
|
---|
5706 | #endif /* HAVE_SETREGID */
|
---|
5707 |
|
---|
5708 | #ifdef HAVE_SETGID
|
---|
5709 | PyDoc_STRVAR(posix_setgid__doc__,
|
---|
5710 | "setgid(gid)\n\n\
|
---|
5711 | Set the current process's group id.");
|
---|
5712 |
|
---|
5713 | static PyObject *
|
---|
5714 | posix_setgid(PyObject *self, PyObject *args)
|
---|
5715 | {
|
---|
5716 | long gid_arg;
|
---|
5717 | gid_t gid;
|
---|
5718 | if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg))
|
---|
5719 | return NULL;
|
---|
5720 | gid = gid_arg;
|
---|
5721 | if (gid != gid_arg) {
|
---|
5722 | PyErr_SetString(PyExc_OverflowError, "group id too big");
|
---|
5723 | return NULL;
|
---|
5724 | }
|
---|
5725 | if (setgid(gid) < 0)
|
---|
5726 | return posix_error();
|
---|
5727 | Py_INCREF(Py_None);
|
---|
5728 | return Py_None;
|
---|
5729 | }
|
---|
5730 | #endif /* HAVE_SETGID */
|
---|
5731 |
|
---|
5732 | #ifdef HAVE_SETGROUPS
|
---|
5733 | PyDoc_STRVAR(posix_setgroups__doc__,
|
---|
5734 | "setgroups(list)\n\n\
|
---|
5735 | Set the groups of the current process to list.");
|
---|
5736 |
|
---|
5737 | static PyObject *
|
---|
5738 | posix_setgroups(PyObject *self, PyObject *groups)
|
---|
5739 | {
|
---|
5740 | int i, len;
|
---|
5741 | gid_t grouplist[MAX_GROUPS];
|
---|
5742 |
|
---|
5743 | if (!PySequence_Check(groups)) {
|
---|
5744 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
|
---|
5745 | return NULL;
|
---|
5746 | }
|
---|
5747 | len = PySequence_Size(groups);
|
---|
5748 | if (len > MAX_GROUPS) {
|
---|
5749 | PyErr_SetString(PyExc_ValueError, "too many groups");
|
---|
5750 | return NULL;
|
---|
5751 | }
|
---|
5752 | for(i = 0; i < len; i++) {
|
---|
5753 | PyObject *elem;
|
---|
5754 | elem = PySequence_GetItem(groups, i);
|
---|
5755 | if (!elem)
|
---|
5756 | return NULL;
|
---|
5757 | if (!PyInt_Check(elem)) {
|
---|
5758 | if (!PyLong_Check(elem)) {
|
---|
5759 | PyErr_SetString(PyExc_TypeError,
|
---|
5760 | "groups must be integers");
|
---|
5761 | Py_DECREF(elem);
|
---|
5762 | return NULL;
|
---|
5763 | } else {
|
---|
5764 | unsigned long x = PyLong_AsUnsignedLong(elem);
|
---|
5765 | if (PyErr_Occurred()) {
|
---|
5766 | PyErr_SetString(PyExc_TypeError,
|
---|
5767 | "group id too big");
|
---|
5768 | Py_DECREF(elem);
|
---|
5769 | return NULL;
|
---|
5770 | }
|
---|
5771 | grouplist[i] = x;
|
---|
5772 | /* read back to see if it fits in gid_t */
|
---|
5773 | if (grouplist[i] != x) {
|
---|
5774 | PyErr_SetString(PyExc_TypeError,
|
---|
5775 | "group id too big");
|
---|
5776 | Py_DECREF(elem);
|
---|
5777 | return NULL;
|
---|
5778 | }
|
---|
5779 | }
|
---|
5780 | } else {
|
---|
5781 | long x = PyInt_AsLong(elem);
|
---|
5782 | grouplist[i] = x;
|
---|
5783 | if (grouplist[i] != x) {
|
---|
5784 | PyErr_SetString(PyExc_TypeError,
|
---|
5785 | "group id too big");
|
---|
5786 | Py_DECREF(elem);
|
---|
5787 | return NULL;
|
---|
5788 | }
|
---|
5789 | }
|
---|
5790 | Py_DECREF(elem);
|
---|
5791 | }
|
---|
5792 |
|
---|
5793 | if (setgroups(len, grouplist) < 0)
|
---|
5794 | return posix_error();
|
---|
5795 | Py_INCREF(Py_None);
|
---|
5796 | return Py_None;
|
---|
5797 | }
|
---|
5798 | #endif /* HAVE_SETGROUPS */
|
---|
5799 |
|
---|
5800 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
|
---|
5801 | static PyObject *
|
---|
5802 | wait_helper(pid_t pid, int status, struct rusage *ru)
|
---|
5803 | {
|
---|
5804 | PyObject *result;
|
---|
5805 | static PyObject *struct_rusage;
|
---|
5806 |
|
---|
5807 | if (pid == -1)
|
---|
5808 | return posix_error();
|
---|
5809 |
|
---|
5810 | if (struct_rusage == NULL) {
|
---|
5811 | PyObject *m = PyImport_ImportModuleNoBlock("resource");
|
---|
5812 | if (m == NULL)
|
---|
5813 | return NULL;
|
---|
5814 | struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
|
---|
5815 | Py_DECREF(m);
|
---|
5816 | if (struct_rusage == NULL)
|
---|
5817 | return NULL;
|
---|
5818 | }
|
---|
5819 |
|
---|
5820 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
|
---|
5821 | result = PyStructSequence_New((PyTypeObject*) struct_rusage);
|
---|
5822 | if (!result)
|
---|
5823 | return NULL;
|
---|
5824 |
|
---|
5825 | #ifndef doubletime
|
---|
5826 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
|
---|
5827 | #endif
|
---|
5828 |
|
---|
5829 | PyStructSequence_SET_ITEM(result, 0,
|
---|
5830 | PyFloat_FromDouble(doubletime(ru->ru_utime)));
|
---|
5831 | PyStructSequence_SET_ITEM(result, 1,
|
---|
5832 | PyFloat_FromDouble(doubletime(ru->ru_stime)));
|
---|
5833 | #define SET_INT(result, index, value)\
|
---|
5834 | PyStructSequence_SET_ITEM(result, index, PyInt_FromLong(value))
|
---|
5835 | SET_INT(result, 2, ru->ru_maxrss);
|
---|
5836 | SET_INT(result, 3, ru->ru_ixrss);
|
---|
5837 | SET_INT(result, 4, ru->ru_idrss);
|
---|
5838 | SET_INT(result, 5, ru->ru_isrss);
|
---|
5839 | SET_INT(result, 6, ru->ru_minflt);
|
---|
5840 | SET_INT(result, 7, ru->ru_majflt);
|
---|
5841 | SET_INT(result, 8, ru->ru_nswap);
|
---|
5842 | SET_INT(result, 9, ru->ru_inblock);
|
---|
5843 | SET_INT(result, 10, ru->ru_oublock);
|
---|
5844 | SET_INT(result, 11, ru->ru_msgsnd);
|
---|
5845 | SET_INT(result, 12, ru->ru_msgrcv);
|
---|
5846 | SET_INT(result, 13, ru->ru_nsignals);
|
---|
5847 | SET_INT(result, 14, ru->ru_nvcsw);
|
---|
5848 | SET_INT(result, 15, ru->ru_nivcsw);
|
---|
5849 | #undef SET_INT
|
---|
5850 |
|
---|
5851 | if (PyErr_Occurred()) {
|
---|
5852 | Py_DECREF(result);
|
---|
5853 | return NULL;
|
---|
5854 | }
|
---|
5855 |
|
---|
5856 | return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
|
---|
5857 | }
|
---|
5858 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */
|
---|
5859 |
|
---|
5860 | #ifdef HAVE_WAIT3
|
---|
5861 | PyDoc_STRVAR(posix_wait3__doc__,
|
---|
5862 | "wait3(options) -> (pid, status, rusage)\n\n\
|
---|
5863 | Wait for completion of a child process.");
|
---|
5864 |
|
---|
5865 | static PyObject *
|
---|
5866 | posix_wait3(PyObject *self, PyObject *args)
|
---|
5867 | {
|
---|
5868 | pid_t pid;
|
---|
5869 | int options;
|
---|
5870 | struct rusage ru;
|
---|
5871 | WAIT_TYPE status;
|
---|
5872 | WAIT_STATUS_INT(status) = 0;
|
---|
5873 |
|
---|
5874 | if (!PyArg_ParseTuple(args, "i:wait3", &options))
|
---|
5875 | return NULL;
|
---|
5876 |
|
---|
5877 | Py_BEGIN_ALLOW_THREADS
|
---|
5878 | pid = wait3(&status, options, &ru);
|
---|
5879 | Py_END_ALLOW_THREADS
|
---|
5880 |
|
---|
5881 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
|
---|
5882 | }
|
---|
5883 | #endif /* HAVE_WAIT3 */
|
---|
5884 |
|
---|
5885 | #ifdef HAVE_WAIT4
|
---|
5886 | PyDoc_STRVAR(posix_wait4__doc__,
|
---|
5887 | "wait4(pid, options) -> (pid, status, rusage)\n\n\
|
---|
5888 | Wait for completion of a given child process.");
|
---|
5889 |
|
---|
5890 | static PyObject *
|
---|
5891 | posix_wait4(PyObject *self, PyObject *args)
|
---|
5892 | {
|
---|
5893 | pid_t pid;
|
---|
5894 | int options;
|
---|
5895 | struct rusage ru;
|
---|
5896 | WAIT_TYPE status;
|
---|
5897 | WAIT_STATUS_INT(status) = 0;
|
---|
5898 |
|
---|
5899 | if (!PyArg_ParseTuple(args, PARSE_PID "i:wait4", &pid, &options))
|
---|
5900 | return NULL;
|
---|
5901 |
|
---|
5902 | Py_BEGIN_ALLOW_THREADS
|
---|
5903 | pid = wait4(pid, &status, options, &ru);
|
---|
5904 | Py_END_ALLOW_THREADS
|
---|
5905 |
|
---|
5906 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
|
---|
5907 | }
|
---|
5908 | #endif /* HAVE_WAIT4 */
|
---|
5909 |
|
---|
5910 | #ifdef HAVE_WAITPID
|
---|
5911 | PyDoc_STRVAR(posix_waitpid__doc__,
|
---|
5912 | "waitpid(pid, options) -> (pid, status)\n\n\
|
---|
5913 | Wait for completion of a given child process.");
|
---|
5914 |
|
---|
5915 | static PyObject *
|
---|
5916 | posix_waitpid(PyObject *self, PyObject *args)
|
---|
5917 | {
|
---|
5918 | pid_t pid;
|
---|
5919 | int options;
|
---|
5920 | WAIT_TYPE status;
|
---|
5921 | WAIT_STATUS_INT(status) = 0;
|
---|
5922 |
|
---|
5923 | if (!PyArg_ParseTuple(args, PARSE_PID "i:waitpid", &pid, &options))
|
---|
5924 | return NULL;
|
---|
5925 | Py_BEGIN_ALLOW_THREADS
|
---|
5926 | pid = waitpid(pid, &status, options);
|
---|
5927 | Py_END_ALLOW_THREADS
|
---|
5928 | if (pid == -1)
|
---|
5929 | return posix_error();
|
---|
5930 |
|
---|
5931 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
|
---|
5932 | }
|
---|
5933 |
|
---|
5934 | #elif defined(HAVE_CWAIT)
|
---|
5935 |
|
---|
5936 | /* MS C has a variant of waitpid() that's usable for most purposes. */
|
---|
5937 | PyDoc_STRVAR(posix_waitpid__doc__,
|
---|
5938 | "waitpid(pid, options) -> (pid, status << 8)\n\n"
|
---|
5939 | "Wait for completion of a given process. options is ignored on Windows.");
|
---|
5940 |
|
---|
5941 | static PyObject *
|
---|
5942 | posix_waitpid(PyObject *self, PyObject *args)
|
---|
5943 | {
|
---|
5944 | Py_intptr_t pid;
|
---|
5945 | int status, options;
|
---|
5946 |
|
---|
5947 | if (!PyArg_ParseTuple(args, PARSE_PID "i:waitpid", &pid, &options))
|
---|
5948 | return NULL;
|
---|
5949 | Py_BEGIN_ALLOW_THREADS
|
---|
5950 | pid = _cwait(&status, pid, options);
|
---|
5951 | Py_END_ALLOW_THREADS
|
---|
5952 | if (pid == -1)
|
---|
5953 | return posix_error();
|
---|
5954 |
|
---|
5955 | /* shift the status left a byte so this is more like the POSIX waitpid */
|
---|
5956 | return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8);
|
---|
5957 | }
|
---|
5958 | #endif /* HAVE_WAITPID || HAVE_CWAIT */
|
---|
5959 |
|
---|
5960 | #ifdef HAVE_WAIT
|
---|
5961 | PyDoc_STRVAR(posix_wait__doc__,
|
---|
5962 | "wait() -> (pid, status)\n\n\
|
---|
5963 | Wait for completion of a child process.");
|
---|
5964 |
|
---|
5965 | static PyObject *
|
---|
5966 | posix_wait(PyObject *self, PyObject *noargs)
|
---|
5967 | {
|
---|
5968 | pid_t pid;
|
---|
5969 | WAIT_TYPE status;
|
---|
5970 | WAIT_STATUS_INT(status) = 0;
|
---|
5971 |
|
---|
5972 | Py_BEGIN_ALLOW_THREADS
|
---|
5973 | pid = wait(&status);
|
---|
5974 | Py_END_ALLOW_THREADS
|
---|
5975 | if (pid == -1)
|
---|
5976 | return posix_error();
|
---|
5977 |
|
---|
5978 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
|
---|
5979 | }
|
---|
5980 | #endif
|
---|
5981 |
|
---|
5982 |
|
---|
5983 | PyDoc_STRVAR(posix_lstat__doc__,
|
---|
5984 | "lstat(path) -> stat result\n\n\
|
---|
5985 | Like stat(path), but do not follow symbolic links.");
|
---|
5986 |
|
---|
5987 | static PyObject *
|
---|
5988 | posix_lstat(PyObject *self, PyObject *args)
|
---|
5989 | {
|
---|
5990 | #ifdef HAVE_LSTAT
|
---|
5991 | return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL);
|
---|
5992 | #else /* !HAVE_LSTAT */
|
---|
5993 | #ifdef MS_WINDOWS
|
---|
5994 | return posix_do_stat(self, args, "et:lstat", STAT, "U:lstat", win32_wstat);
|
---|
5995 | #else
|
---|
5996 | return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL);
|
---|
5997 | #endif
|
---|
5998 | #endif /* !HAVE_LSTAT */
|
---|
5999 | }
|
---|
6000 |
|
---|
6001 |
|
---|
6002 | #ifdef HAVE_READLINK
|
---|
6003 | PyDoc_STRVAR(posix_readlink__doc__,
|
---|
6004 | "readlink(path) -> path\n\n\
|
---|
6005 | Return a string representing the path to which the symbolic link points.");
|
---|
6006 |
|
---|
6007 | static PyObject *
|
---|
6008 | posix_readlink(PyObject *self, PyObject *args)
|
---|
6009 | {
|
---|
6010 | PyObject* v;
|
---|
6011 | char buf[MAXPATHLEN];
|
---|
6012 | char *path;
|
---|
6013 | int n;
|
---|
6014 | #ifdef Py_USING_UNICODE
|
---|
6015 | int arg_is_unicode = 0;
|
---|
6016 | #endif
|
---|
6017 |
|
---|
6018 | if (!PyArg_ParseTuple(args, "et:readlink",
|
---|
6019 | Py_FileSystemDefaultEncoding, &path))
|
---|
6020 | return NULL;
|
---|
6021 | #ifdef Py_USING_UNICODE
|
---|
6022 | v = PySequence_GetItem(args, 0);
|
---|
6023 | if (v == NULL) {
|
---|
6024 | PyMem_Free(path);
|
---|
6025 | return NULL;
|
---|
6026 | }
|
---|
6027 |
|
---|
6028 | if (PyUnicode_Check(v)) {
|
---|
6029 | arg_is_unicode = 1;
|
---|
6030 | }
|
---|
6031 | Py_DECREF(v);
|
---|
6032 | #endif
|
---|
6033 |
|
---|
6034 | Py_BEGIN_ALLOW_THREADS
|
---|
6035 | n = readlink(path, buf, (int) sizeof buf);
|
---|
6036 | Py_END_ALLOW_THREADS
|
---|
6037 | if (n < 0)
|
---|
6038 | return posix_error_with_allocated_filename(path);
|
---|
6039 |
|
---|
6040 | PyMem_Free(path);
|
---|
6041 | v = PyString_FromStringAndSize(buf, n);
|
---|
6042 | #ifdef Py_USING_UNICODE
|
---|
6043 | if (arg_is_unicode) {
|
---|
6044 | PyObject *w;
|
---|
6045 |
|
---|
6046 | w = PyUnicode_FromEncodedObject(v,
|
---|
6047 | Py_FileSystemDefaultEncoding,
|
---|
6048 | "strict");
|
---|
6049 | if (w != NULL) {
|
---|
6050 | Py_DECREF(v);
|
---|
6051 | v = w;
|
---|
6052 | }
|
---|
6053 | else {
|
---|
6054 | /* fall back to the original byte string, as
|
---|
6055 | discussed in patch #683592 */
|
---|
6056 | PyErr_Clear();
|
---|
6057 | }
|
---|
6058 | }
|
---|
6059 | #endif
|
---|
6060 | return v;
|
---|
6061 | }
|
---|
6062 | #endif /* HAVE_READLINK */
|
---|
6063 |
|
---|
6064 |
|
---|
6065 | #ifdef HAVE_SYMLINK
|
---|
6066 | PyDoc_STRVAR(posix_symlink__doc__,
|
---|
6067 | "symlink(src, dst)\n\n\
|
---|
6068 | Create a symbolic link pointing to src named dst.");
|
---|
6069 |
|
---|
6070 | static PyObject *
|
---|
6071 | posix_symlink(PyObject *self, PyObject *args)
|
---|
6072 | {
|
---|
6073 | return posix_2str(args, "etet:symlink", symlink);
|
---|
6074 | }
|
---|
6075 | #endif /* HAVE_SYMLINK */
|
---|
6076 |
|
---|
6077 |
|
---|
6078 | #ifdef HAVE_TIMES
|
---|
6079 | #if defined(PYCC_VACPP) && defined(PYOS_OS2)
|
---|
6080 | static long
|
---|
6081 | system_uptime(void)
|
---|
6082 | {
|
---|
6083 | ULONG value = 0;
|
---|
6084 |
|
---|
6085 | Py_BEGIN_ALLOW_THREADS
|
---|
6086 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
|
---|
6087 | Py_END_ALLOW_THREADS
|
---|
6088 |
|
---|
6089 | return value;
|
---|
6090 | }
|
---|
6091 |
|
---|
6092 | static PyObject *
|
---|
6093 | posix_times(PyObject *self, PyObject *noargs)
|
---|
6094 | {
|
---|
6095 | /* Currently Only Uptime is Provided -- Others Later */
|
---|
6096 | return Py_BuildValue("ddddd",
|
---|
6097 | (double)0 /* t.tms_utime / HZ */,
|
---|
6098 | (double)0 /* t.tms_stime / HZ */,
|
---|
6099 | (double)0 /* t.tms_cutime / HZ */,
|
---|
6100 | (double)0 /* t.tms_cstime / HZ */,
|
---|
6101 | (double)system_uptime() / 1000);
|
---|
6102 | }
|
---|
6103 | #else /* not OS2 */
|
---|
6104 | #define NEED_TICKS_PER_SECOND
|
---|
6105 | static long ticks_per_second = -1;
|
---|
6106 | static PyObject *
|
---|
6107 | posix_times(PyObject *self, PyObject *noargs)
|
---|
6108 | {
|
---|
6109 | struct tms t;
|
---|
6110 | clock_t c;
|
---|
6111 | errno = 0;
|
---|
6112 | c = times(&t);
|
---|
6113 | if (c == (clock_t) -1)
|
---|
6114 | return posix_error();
|
---|
6115 | return Py_BuildValue("ddddd",
|
---|
6116 | (double)t.tms_utime / ticks_per_second,
|
---|
6117 | (double)t.tms_stime / ticks_per_second,
|
---|
6118 | (double)t.tms_cutime / ticks_per_second,
|
---|
6119 | (double)t.tms_cstime / ticks_per_second,
|
---|
6120 | (double)c / ticks_per_second);
|
---|
6121 | }
|
---|
6122 | #endif /* not OS2 */
|
---|
6123 | #endif /* HAVE_TIMES */
|
---|
6124 |
|
---|
6125 |
|
---|
6126 | #ifdef MS_WINDOWS
|
---|
6127 | #define HAVE_TIMES /* so the method table will pick it up */
|
---|
6128 | static PyObject *
|
---|
6129 | posix_times(PyObject *self, PyObject *noargs)
|
---|
6130 | {
|
---|
6131 | FILETIME create, exit, kernel, user;
|
---|
6132 | HANDLE hProc;
|
---|
6133 | hProc = GetCurrentProcess();
|
---|
6134 | GetProcessTimes(hProc, &create, &exit, &kernel, &user);
|
---|
6135 | /* The fields of a FILETIME structure are the hi and lo part
|
---|
6136 | of a 64-bit value expressed in 100 nanosecond units.
|
---|
6137 | 1e7 is one second in such units; 1e-7 the inverse.
|
---|
6138 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
|
---|
6139 | */
|
---|
6140 | return Py_BuildValue(
|
---|
6141 | "ddddd",
|
---|
6142 | (double)(user.dwHighDateTime*429.4967296 +
|
---|
6143 | user.dwLowDateTime*1e-7),
|
---|
6144 | (double)(kernel.dwHighDateTime*429.4967296 +
|
---|
6145 | kernel.dwLowDateTime*1e-7),
|
---|
6146 | (double)0,
|
---|
6147 | (double)0,
|
---|
6148 | (double)0);
|
---|
6149 | }
|
---|
6150 | #endif /* MS_WINDOWS */
|
---|
6151 |
|
---|
6152 | #ifdef HAVE_TIMES
|
---|
6153 | PyDoc_STRVAR(posix_times__doc__,
|
---|
6154 | "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
|
---|
6155 | Return a tuple of floating point numbers indicating process times.");
|
---|
6156 | #endif
|
---|
6157 |
|
---|
6158 |
|
---|
6159 | #ifdef HAVE_GETSID
|
---|
6160 | PyDoc_STRVAR(posix_getsid__doc__,
|
---|
6161 | "getsid(pid) -> sid\n\n\
|
---|
6162 | Call the system call getsid().");
|
---|
6163 |
|
---|
6164 | static PyObject *
|
---|
6165 | posix_getsid(PyObject *self, PyObject *args)
|
---|
6166 | {
|
---|
6167 | pid_t pid;
|
---|
6168 | int sid;
|
---|
6169 | if (!PyArg_ParseTuple(args, PARSE_PID ":getsid", &pid))
|
---|
6170 | return NULL;
|
---|
6171 | sid = getsid(pid);
|
---|
6172 | if (sid < 0)
|
---|
6173 | return posix_error();
|
---|
6174 | return PyInt_FromLong((long)sid);
|
---|
6175 | }
|
---|
6176 | #endif /* HAVE_GETSID */
|
---|
6177 |
|
---|
6178 |
|
---|
6179 | #ifdef HAVE_SETSID
|
---|
6180 | PyDoc_STRVAR(posix_setsid__doc__,
|
---|
6181 | "setsid()\n\n\
|
---|
6182 | Call the system call setsid().");
|
---|
6183 |
|
---|
6184 | static PyObject *
|
---|
6185 | posix_setsid(PyObject *self, PyObject *noargs)
|
---|
6186 | {
|
---|
6187 | if (setsid() < 0)
|
---|
6188 | return posix_error();
|
---|
6189 | Py_INCREF(Py_None);
|
---|
6190 | return Py_None;
|
---|
6191 | }
|
---|
6192 | #endif /* HAVE_SETSID */
|
---|
6193 |
|
---|
6194 | #ifdef HAVE_SETPGID
|
---|
6195 | PyDoc_STRVAR(posix_setpgid__doc__,
|
---|
6196 | "setpgid(pid, pgrp)\n\n\
|
---|
6197 | Call the system call setpgid().");
|
---|
6198 |
|
---|
6199 | static PyObject *
|
---|
6200 | posix_setpgid(PyObject *self, PyObject *args)
|
---|
6201 | {
|
---|
6202 | pid_t pid;
|
---|
6203 | int pgrp;
|
---|
6204 | if (!PyArg_ParseTuple(args, PARSE_PID "i:setpgid", &pid, &pgrp))
|
---|
6205 | return NULL;
|
---|
6206 | if (setpgid(pid, pgrp) < 0)
|
---|
6207 | return posix_error();
|
---|
6208 | Py_INCREF(Py_None);
|
---|
6209 | return Py_None;
|
---|
6210 | }
|
---|
6211 | #endif /* HAVE_SETPGID */
|
---|
6212 |
|
---|
6213 |
|
---|
6214 | #ifdef HAVE_TCGETPGRP
|
---|
6215 | PyDoc_STRVAR(posix_tcgetpgrp__doc__,
|
---|
6216 | "tcgetpgrp(fd) -> pgid\n\n\
|
---|
6217 | Return the process group associated with the terminal given by a fd.");
|
---|
6218 |
|
---|
6219 | static PyObject *
|
---|
6220 | posix_tcgetpgrp(PyObject *self, PyObject *args)
|
---|
6221 | {
|
---|
6222 | int fd;
|
---|
6223 | pid_t pgid;
|
---|
6224 | if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
|
---|
6225 | return NULL;
|
---|
6226 | pgid = tcgetpgrp(fd);
|
---|
6227 | if (pgid < 0)
|
---|
6228 | return posix_error();
|
---|
6229 | return PyLong_FromPid(pgid);
|
---|
6230 | }
|
---|
6231 | #endif /* HAVE_TCGETPGRP */
|
---|
6232 |
|
---|
6233 |
|
---|
6234 | #ifdef HAVE_TCSETPGRP
|
---|
6235 | PyDoc_STRVAR(posix_tcsetpgrp__doc__,
|
---|
6236 | "tcsetpgrp(fd, pgid)\n\n\
|
---|
6237 | Set the process group associated with the terminal given by a fd.");
|
---|
6238 |
|
---|
6239 | static PyObject *
|
---|
6240 | posix_tcsetpgrp(PyObject *self, PyObject *args)
|
---|
6241 | {
|
---|
6242 | int fd;
|
---|
6243 | pid_t pgid;
|
---|
6244 | if (!PyArg_ParseTuple(args, "i" PARSE_PID ":tcsetpgrp", &fd, &pgid))
|
---|
6245 | return NULL;
|
---|
6246 | if (tcsetpgrp(fd, pgid) < 0)
|
---|
6247 | return posix_error();
|
---|
6248 | Py_INCREF(Py_None);
|
---|
6249 | return Py_None;
|
---|
6250 | }
|
---|
6251 | #endif /* HAVE_TCSETPGRP */
|
---|
6252 |
|
---|
6253 | /* Functions acting on file descriptors */
|
---|
6254 |
|
---|
6255 | PyDoc_STRVAR(posix_open__doc__,
|
---|
6256 | "open(filename, flag [, mode=0777]) -> fd\n\n\
|
---|
6257 | Open a file (for low level IO).");
|
---|
6258 |
|
---|
6259 | static PyObject *
|
---|
6260 | posix_open(PyObject *self, PyObject *args)
|
---|
6261 | {
|
---|
6262 | char *file = NULL;
|
---|
6263 | int flag;
|
---|
6264 | int mode = 0777;
|
---|
6265 | int fd;
|
---|
6266 |
|
---|
6267 | #ifdef MS_WINDOWS
|
---|
6268 | if (unicode_file_names()) {
|
---|
6269 | PyUnicodeObject *po;
|
---|
6270 | if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
|
---|
6271 | Py_BEGIN_ALLOW_THREADS
|
---|
6272 | /* PyUnicode_AS_UNICODE OK without thread
|
---|
6273 | lock as it is a simple dereference. */
|
---|
6274 | fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
|
---|
6275 | Py_END_ALLOW_THREADS
|
---|
6276 | if (fd < 0)
|
---|
6277 | return posix_error();
|
---|
6278 | return PyInt_FromLong((long)fd);
|
---|
6279 | }
|
---|
6280 | /* Drop the argument parsing error as narrow strings
|
---|
6281 | are also valid. */
|
---|
6282 | PyErr_Clear();
|
---|
6283 | }
|
---|
6284 | #endif
|
---|
6285 |
|
---|
6286 | if (!PyArg_ParseTuple(args, "eti|i",
|
---|
6287 | Py_FileSystemDefaultEncoding, &file,
|
---|
6288 | &flag, &mode))
|
---|
6289 | return NULL;
|
---|
6290 |
|
---|
6291 | Py_BEGIN_ALLOW_THREADS
|
---|
6292 | fd = open(file, flag, mode);
|
---|
6293 | Py_END_ALLOW_THREADS
|
---|
6294 | if (fd < 0)
|
---|
6295 | return posix_error_with_allocated_filename(file);
|
---|
6296 | PyMem_Free(file);
|
---|
6297 | return PyInt_FromLong((long)fd);
|
---|
6298 | }
|
---|
6299 |
|
---|
6300 |
|
---|
6301 | PyDoc_STRVAR(posix_close__doc__,
|
---|
6302 | "close(fd)\n\n\
|
---|
6303 | Close a file descriptor (for low level IO).");
|
---|
6304 |
|
---|
6305 | static PyObject *
|
---|
6306 | posix_close(PyObject *self, PyObject *args)
|
---|
6307 | {
|
---|
6308 | int fd, res;
|
---|
6309 | if (!PyArg_ParseTuple(args, "i:close", &fd))
|
---|
6310 | return NULL;
|
---|
6311 | Py_BEGIN_ALLOW_THREADS
|
---|
6312 | res = close(fd);
|
---|
6313 | Py_END_ALLOW_THREADS
|
---|
6314 | if (res < 0)
|
---|
6315 | return posix_error();
|
---|
6316 | Py_INCREF(Py_None);
|
---|
6317 | return Py_None;
|
---|
6318 | }
|
---|
6319 |
|
---|
6320 |
|
---|
6321 | PyDoc_STRVAR(posix_closerange__doc__,
|
---|
6322 | "closerange(fd_low, fd_high)\n\n\
|
---|
6323 | Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
|
---|
6324 |
|
---|
6325 | static PyObject *
|
---|
6326 | posix_closerange(PyObject *self, PyObject *args)
|
---|
6327 | {
|
---|
6328 | int fd_from, fd_to, i;
|
---|
6329 | if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
|
---|
6330 | return NULL;
|
---|
6331 | Py_BEGIN_ALLOW_THREADS
|
---|
6332 | for (i = fd_from; i < fd_to; i++)
|
---|
6333 | close(i);
|
---|
6334 | Py_END_ALLOW_THREADS
|
---|
6335 | Py_RETURN_NONE;
|
---|
6336 | }
|
---|
6337 |
|
---|
6338 |
|
---|
6339 | PyDoc_STRVAR(posix_dup__doc__,
|
---|
6340 | "dup(fd) -> fd2\n\n\
|
---|
6341 | Return a duplicate of a file descriptor.");
|
---|
6342 |
|
---|
6343 | static PyObject *
|
---|
6344 | posix_dup(PyObject *self, PyObject *args)
|
---|
6345 | {
|
---|
6346 | int fd;
|
---|
6347 | if (!PyArg_ParseTuple(args, "i:dup", &fd))
|
---|
6348 | return NULL;
|
---|
6349 | Py_BEGIN_ALLOW_THREADS
|
---|
6350 | fd = dup(fd);
|
---|
6351 | Py_END_ALLOW_THREADS
|
---|
6352 | if (fd < 0)
|
---|
6353 | return posix_error();
|
---|
6354 | return PyInt_FromLong((long)fd);
|
---|
6355 | }
|
---|
6356 |
|
---|
6357 |
|
---|
6358 | PyDoc_STRVAR(posix_dup2__doc__,
|
---|
6359 | "dup2(old_fd, new_fd)\n\n\
|
---|
6360 | Duplicate file descriptor.");
|
---|
6361 |
|
---|
6362 | static PyObject *
|
---|
6363 | posix_dup2(PyObject *self, PyObject *args)
|
---|
6364 | {
|
---|
6365 | int fd, fd2, res;
|
---|
6366 | if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
|
---|
6367 | return NULL;
|
---|
6368 | Py_BEGIN_ALLOW_THREADS
|
---|
6369 | res = dup2(fd, fd2);
|
---|
6370 | Py_END_ALLOW_THREADS
|
---|
6371 | if (res < 0)
|
---|
6372 | return posix_error();
|
---|
6373 | Py_INCREF(Py_None);
|
---|
6374 | return Py_None;
|
---|
6375 | }
|
---|
6376 |
|
---|
6377 |
|
---|
6378 | PyDoc_STRVAR(posix_lseek__doc__,
|
---|
6379 | "lseek(fd, pos, how) -> newpos\n\n\
|
---|
6380 | Set the current position of a file descriptor.");
|
---|
6381 |
|
---|
6382 | static PyObject *
|
---|
6383 | posix_lseek(PyObject *self, PyObject *args)
|
---|
6384 | {
|
---|
6385 | int fd, how;
|
---|
6386 | #if defined(MS_WIN64) || defined(MS_WINDOWS)
|
---|
6387 | PY_LONG_LONG pos, res;
|
---|
6388 | #else
|
---|
6389 | off_t pos, res;
|
---|
6390 | #endif
|
---|
6391 | PyObject *posobj;
|
---|
6392 | if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
|
---|
6393 | return NULL;
|
---|
6394 | #ifdef SEEK_SET
|
---|
6395 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
|
---|
6396 | switch (how) {
|
---|
6397 | case 0: how = SEEK_SET; break;
|
---|
6398 | case 1: how = SEEK_CUR; break;
|
---|
6399 | case 2: how = SEEK_END; break;
|
---|
6400 | }
|
---|
6401 | #endif /* SEEK_END */
|
---|
6402 |
|
---|
6403 | #if !defined(HAVE_LARGEFILE_SUPPORT)
|
---|
6404 | pos = PyInt_AsLong(posobj);
|
---|
6405 | #else
|
---|
6406 | pos = PyLong_Check(posobj) ?
|
---|
6407 | PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
|
---|
6408 | #endif
|
---|
6409 | if (PyErr_Occurred())
|
---|
6410 | return NULL;
|
---|
6411 |
|
---|
6412 | Py_BEGIN_ALLOW_THREADS
|
---|
6413 | #if defined(MS_WIN64) || defined(MS_WINDOWS)
|
---|
6414 | res = _lseeki64(fd, pos, how);
|
---|
6415 | #else
|
---|
6416 | res = lseek(fd, pos, how);
|
---|
6417 | #endif
|
---|
6418 | Py_END_ALLOW_THREADS
|
---|
6419 | if (res < 0)
|
---|
6420 | return posix_error();
|
---|
6421 |
|
---|
6422 | #if !defined(HAVE_LARGEFILE_SUPPORT)
|
---|
6423 | return PyInt_FromLong(res);
|
---|
6424 | #else
|
---|
6425 | return PyLong_FromLongLong(res);
|
---|
6426 | #endif
|
---|
6427 | }
|
---|
6428 |
|
---|
6429 |
|
---|
6430 | PyDoc_STRVAR(posix_read__doc__,
|
---|
6431 | "read(fd, buffersize) -> string\n\n\
|
---|
6432 | Read a file descriptor.");
|
---|
6433 |
|
---|
6434 | static PyObject *
|
---|
6435 | posix_read(PyObject *self, PyObject *args)
|
---|
6436 | {
|
---|
6437 | int fd, size, n;
|
---|
6438 | PyObject *buffer;
|
---|
6439 | if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
|
---|
6440 | return NULL;
|
---|
6441 | if (size < 0) {
|
---|
6442 | errno = EINVAL;
|
---|
6443 | return posix_error();
|
---|
6444 | }
|
---|
6445 | buffer = PyString_FromStringAndSize((char *)NULL, size);
|
---|
6446 | if (buffer == NULL)
|
---|
6447 | return NULL;
|
---|
6448 | Py_BEGIN_ALLOW_THREADS
|
---|
6449 | n = read(fd, PyString_AsString(buffer), size);
|
---|
6450 | Py_END_ALLOW_THREADS
|
---|
6451 | if (n < 0) {
|
---|
6452 | Py_DECREF(buffer);
|
---|
6453 | return posix_error();
|
---|
6454 | }
|
---|
6455 | if (n != size)
|
---|
6456 | _PyString_Resize(&buffer, n);
|
---|
6457 | return buffer;
|
---|
6458 | }
|
---|
6459 |
|
---|
6460 |
|
---|
6461 | PyDoc_STRVAR(posix_write__doc__,
|
---|
6462 | "write(fd, string) -> byteswritten\n\n\
|
---|
6463 | Write a string to a file descriptor.");
|
---|
6464 |
|
---|
6465 | static PyObject *
|
---|
6466 | posix_write(PyObject *self, PyObject *args)
|
---|
6467 | {
|
---|
6468 | Py_buffer pbuf;
|
---|
6469 | int fd;
|
---|
6470 | Py_ssize_t size;
|
---|
6471 |
|
---|
6472 | if (!PyArg_ParseTuple(args, "is*:write", &fd, &pbuf))
|
---|
6473 | return NULL;
|
---|
6474 | Py_BEGIN_ALLOW_THREADS
|
---|
6475 | size = write(fd, pbuf.buf, (size_t)pbuf.len);
|
---|
6476 | Py_END_ALLOW_THREADS
|
---|
6477 | PyBuffer_Release(&pbuf);
|
---|
6478 | if (size < 0)
|
---|
6479 | return posix_error();
|
---|
6480 | return PyInt_FromSsize_t(size);
|
---|
6481 | }
|
---|
6482 |
|
---|
6483 |
|
---|
6484 | PyDoc_STRVAR(posix_fstat__doc__,
|
---|
6485 | "fstat(fd) -> stat result\n\n\
|
---|
6486 | Like stat(), but for an open file descriptor.");
|
---|
6487 |
|
---|
6488 | static PyObject *
|
---|
6489 | posix_fstat(PyObject *self, PyObject *args)
|
---|
6490 | {
|
---|
6491 | int fd;
|
---|
6492 | STRUCT_STAT st;
|
---|
6493 | int res;
|
---|
6494 | if (!PyArg_ParseTuple(args, "i:fstat", &fd))
|
---|
6495 | return NULL;
|
---|
6496 | #ifdef __VMS
|
---|
6497 | /* on OpenVMS we must ensure that all bytes are written to the file */
|
---|
6498 | fsync(fd);
|
---|
6499 | #endif
|
---|
6500 | Py_BEGIN_ALLOW_THREADS
|
---|
6501 | res = FSTAT(fd, &st);
|
---|
6502 | Py_END_ALLOW_THREADS
|
---|
6503 | if (res != 0) {
|
---|
6504 | #ifdef MS_WINDOWS
|
---|
6505 | return win32_error("fstat", NULL);
|
---|
6506 | #else
|
---|
6507 | return posix_error();
|
---|
6508 | #endif
|
---|
6509 | }
|
---|
6510 |
|
---|
6511 | return _pystat_fromstructstat(&st);
|
---|
6512 | }
|
---|
6513 |
|
---|
6514 |
|
---|
6515 | PyDoc_STRVAR(posix_fdopen__doc__,
|
---|
6516 | "fdopen(fd [, mode='r' [, bufsize]]) -> file_object\n\n\
|
---|
6517 | Return an open file object connected to a file descriptor.");
|
---|
6518 |
|
---|
6519 | static PyObject *
|
---|
6520 | posix_fdopen(PyObject *self, PyObject *args)
|
---|
6521 | {
|
---|
6522 | int fd;
|
---|
6523 | char *orgmode = "r";
|
---|
6524 | int bufsize = -1;
|
---|
6525 | FILE *fp;
|
---|
6526 | PyObject *f;
|
---|
6527 | char *mode;
|
---|
6528 | if (!PyArg_ParseTuple(args, "i|si", &fd, &orgmode, &bufsize))
|
---|
6529 | return NULL;
|
---|
6530 |
|
---|
6531 | /* Sanitize mode. See fileobject.c */
|
---|
6532 | mode = PyMem_MALLOC(strlen(orgmode)+3);
|
---|
6533 | if (!mode) {
|
---|
6534 | PyErr_NoMemory();
|
---|
6535 | return NULL;
|
---|
6536 | }
|
---|
6537 | strcpy(mode, orgmode);
|
---|
6538 | if (_PyFile_SanitizeMode(mode)) {
|
---|
6539 | PyMem_FREE(mode);
|
---|
6540 | return NULL;
|
---|
6541 | }
|
---|
6542 | Py_BEGIN_ALLOW_THREADS
|
---|
6543 | #if !defined(MS_WINDOWS) && defined(HAVE_FCNTL_H)
|
---|
6544 | if (mode[0] == 'a') {
|
---|
6545 | /* try to make sure the O_APPEND flag is set */
|
---|
6546 | int flags;
|
---|
6547 | flags = fcntl(fd, F_GETFL);
|
---|
6548 | if (flags != -1)
|
---|
6549 | fcntl(fd, F_SETFL, flags | O_APPEND);
|
---|
6550 | fp = fdopen(fd, mode);
|
---|
6551 | if (fp == NULL && flags != -1)
|
---|
6552 | /* restore old mode if fdopen failed */
|
---|
6553 | fcntl(fd, F_SETFL, flags);
|
---|
6554 | } else {
|
---|
6555 | fp = fdopen(fd, mode);
|
---|
6556 | }
|
---|
6557 | #else
|
---|
6558 | fp = fdopen(fd, mode);
|
---|
6559 | #endif
|
---|
6560 | Py_END_ALLOW_THREADS
|
---|
6561 | PyMem_FREE(mode);
|
---|
6562 | if (fp == NULL)
|
---|
6563 | return posix_error();
|
---|
6564 | f = PyFile_FromFile(fp, "<fdopen>", orgmode, fclose);
|
---|
6565 | if (f != NULL)
|
---|
6566 | PyFile_SetBufSize(f, bufsize);
|
---|
6567 | return f;
|
---|
6568 | }
|
---|
6569 |
|
---|
6570 | PyDoc_STRVAR(posix_isatty__doc__,
|
---|
6571 | "isatty(fd) -> bool\n\n\
|
---|
6572 | Return True if the file descriptor 'fd' is an open file descriptor\n\
|
---|
6573 | connected to the slave end of a terminal.");
|
---|
6574 |
|
---|
6575 | static PyObject *
|
---|
6576 | posix_isatty(PyObject *self, PyObject *args)
|
---|
6577 | {
|
---|
6578 | int fd;
|
---|
6579 | if (!PyArg_ParseTuple(args, "i:isatty", &fd))
|
---|
6580 | return NULL;
|
---|
6581 | return PyBool_FromLong(isatty(fd));
|
---|
6582 | }
|
---|
6583 |
|
---|
6584 | #ifdef HAVE_PIPE
|
---|
6585 | PyDoc_STRVAR(posix_pipe__doc__,
|
---|
6586 | "pipe() -> (read_end, write_end)\n\n\
|
---|
6587 | Create a pipe.");
|
---|
6588 |
|
---|
6589 | static PyObject *
|
---|
6590 | posix_pipe(PyObject *self, PyObject *noargs)
|
---|
6591 | {
|
---|
6592 | #if defined(PYOS_OS2)
|
---|
6593 | HFILE read, write;
|
---|
6594 | APIRET rc;
|
---|
6595 |
|
---|
6596 | Py_BEGIN_ALLOW_THREADS
|
---|
6597 | rc = DosCreatePipe( &read, &write, 4096);
|
---|
6598 | Py_END_ALLOW_THREADS
|
---|
6599 | if (rc != NO_ERROR)
|
---|
6600 | return os2_error(rc);
|
---|
6601 |
|
---|
6602 | return Py_BuildValue("(ii)", read, write);
|
---|
6603 | #else
|
---|
6604 | #if !defined(MS_WINDOWS)
|
---|
6605 | int fds[2];
|
---|
6606 | int res;
|
---|
6607 | Py_BEGIN_ALLOW_THREADS
|
---|
6608 | res = pipe(fds);
|
---|
6609 | Py_END_ALLOW_THREADS
|
---|
6610 | if (res != 0)
|
---|
6611 | return posix_error();
|
---|
6612 | return Py_BuildValue("(ii)", fds[0], fds[1]);
|
---|
6613 | #else /* MS_WINDOWS */
|
---|
6614 | HANDLE read, write;
|
---|
6615 | int read_fd, write_fd;
|
---|
6616 | BOOL ok;
|
---|
6617 | Py_BEGIN_ALLOW_THREADS
|
---|
6618 | ok = CreatePipe(&read, &write, NULL, 0);
|
---|
6619 | Py_END_ALLOW_THREADS
|
---|
6620 | if (!ok)
|
---|
6621 | return win32_error("CreatePipe", NULL);
|
---|
6622 | read_fd = _open_osfhandle((Py_intptr_t)read, 0);
|
---|
6623 | write_fd = _open_osfhandle((Py_intptr_t)write, 1);
|
---|
6624 | return Py_BuildValue("(ii)", read_fd, write_fd);
|
---|
6625 | #endif /* MS_WINDOWS */
|
---|
6626 | #endif
|
---|
6627 | }
|
---|
6628 | #endif /* HAVE_PIPE */
|
---|
6629 |
|
---|
6630 |
|
---|
6631 | #ifdef HAVE_MKFIFO
|
---|
6632 | PyDoc_STRVAR(posix_mkfifo__doc__,
|
---|
6633 | "mkfifo(filename [, mode=0666])\n\n\
|
---|
6634 | Create a FIFO (a POSIX named pipe).");
|
---|
6635 |
|
---|
6636 | static PyObject *
|
---|
6637 | posix_mkfifo(PyObject *self, PyObject *args)
|
---|
6638 | {
|
---|
6639 | char *filename;
|
---|
6640 | int mode = 0666;
|
---|
6641 | int res;
|
---|
6642 | if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode))
|
---|
6643 | return NULL;
|
---|
6644 | Py_BEGIN_ALLOW_THREADS
|
---|
6645 | res = mkfifo(filename, mode);
|
---|
6646 | Py_END_ALLOW_THREADS
|
---|
6647 | if (res < 0)
|
---|
6648 | return posix_error();
|
---|
6649 | Py_INCREF(Py_None);
|
---|
6650 | return Py_None;
|
---|
6651 | }
|
---|
6652 | #endif
|
---|
6653 |
|
---|
6654 |
|
---|
6655 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
|
---|
6656 | PyDoc_STRVAR(posix_mknod__doc__,
|
---|
6657 | "mknod(filename [, mode=0600, device])\n\n\
|
---|
6658 | Create a filesystem node (file, device special file or named pipe)\n\
|
---|
6659 | named filename. mode specifies both the permissions to use and the\n\
|
---|
6660 | type of node to be created, being combined (bitwise OR) with one of\n\
|
---|
6661 | S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
|
---|
6662 | device defines the newly created device special file (probably using\n\
|
---|
6663 | os.makedev()), otherwise it is ignored.");
|
---|
6664 |
|
---|
6665 |
|
---|
6666 | static PyObject *
|
---|
6667 | posix_mknod(PyObject *self, PyObject *args)
|
---|
6668 | {
|
---|
6669 | char *filename;
|
---|
6670 | int mode = 0600;
|
---|
6671 | int device = 0;
|
---|
6672 | int res;
|
---|
6673 | if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device))
|
---|
6674 | return NULL;
|
---|
6675 | Py_BEGIN_ALLOW_THREADS
|
---|
6676 | res = mknod(filename, mode, device);
|
---|
6677 | Py_END_ALLOW_THREADS
|
---|
6678 | if (res < 0)
|
---|
6679 | return posix_error();
|
---|
6680 | Py_INCREF(Py_None);
|
---|
6681 | return Py_None;
|
---|
6682 | }
|
---|
6683 | #endif
|
---|
6684 |
|
---|
6685 | #ifdef HAVE_DEVICE_MACROS
|
---|
6686 | PyDoc_STRVAR(posix_major__doc__,
|
---|
6687 | "major(device) -> major number\n\
|
---|
6688 | Extracts a device major number from a raw device number.");
|
---|
6689 |
|
---|
6690 | static PyObject *
|
---|
6691 | posix_major(PyObject *self, PyObject *args)
|
---|
6692 | {
|
---|
6693 | int device;
|
---|
6694 | if (!PyArg_ParseTuple(args, "i:major", &device))
|
---|
6695 | return NULL;
|
---|
6696 | return PyInt_FromLong((long)major(device));
|
---|
6697 | }
|
---|
6698 |
|
---|
6699 | PyDoc_STRVAR(posix_minor__doc__,
|
---|
6700 | "minor(device) -> minor number\n\
|
---|
6701 | Extracts a device minor number from a raw device number.");
|
---|
6702 |
|
---|
6703 | static PyObject *
|
---|
6704 | posix_minor(PyObject *self, PyObject *args)
|
---|
6705 | {
|
---|
6706 | int device;
|
---|
6707 | if (!PyArg_ParseTuple(args, "i:minor", &device))
|
---|
6708 | return NULL;
|
---|
6709 | return PyInt_FromLong((long)minor(device));
|
---|
6710 | }
|
---|
6711 |
|
---|
6712 | PyDoc_STRVAR(posix_makedev__doc__,
|
---|
6713 | "makedev(major, minor) -> device number\n\
|
---|
6714 | Composes a raw device number from the major and minor device numbers.");
|
---|
6715 |
|
---|
6716 | static PyObject *
|
---|
6717 | posix_makedev(PyObject *self, PyObject *args)
|
---|
6718 | {
|
---|
6719 | int major, minor;
|
---|
6720 | if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
|
---|
6721 | return NULL;
|
---|
6722 | return PyInt_FromLong((long)makedev(major, minor));
|
---|
6723 | }
|
---|
6724 | #endif /* device macros */
|
---|
6725 |
|
---|
6726 |
|
---|
6727 | #ifdef HAVE_FTRUNCATE
|
---|
6728 | PyDoc_STRVAR(posix_ftruncate__doc__,
|
---|
6729 | "ftruncate(fd, length)\n\n\
|
---|
6730 | Truncate a file to a specified length.");
|
---|
6731 |
|
---|
6732 | static PyObject *
|
---|
6733 | posix_ftruncate(PyObject *self, PyObject *args)
|
---|
6734 | {
|
---|
6735 | int fd;
|
---|
6736 | off_t length;
|
---|
6737 | int res;
|
---|
6738 | PyObject *lenobj;
|
---|
6739 |
|
---|
6740 | if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
|
---|
6741 | return NULL;
|
---|
6742 |
|
---|
6743 | #if !defined(HAVE_LARGEFILE_SUPPORT)
|
---|
6744 | length = PyInt_AsLong(lenobj);
|
---|
6745 | #else
|
---|
6746 | length = PyLong_Check(lenobj) ?
|
---|
6747 | PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj);
|
---|
6748 | #endif
|
---|
6749 | if (PyErr_Occurred())
|
---|
6750 | return NULL;
|
---|
6751 |
|
---|
6752 | Py_BEGIN_ALLOW_THREADS
|
---|
6753 | res = ftruncate(fd, length);
|
---|
6754 | Py_END_ALLOW_THREADS
|
---|
6755 | if (res < 0) {
|
---|
6756 | PyErr_SetFromErrno(PyExc_IOError);
|
---|
6757 | return NULL;
|
---|
6758 | }
|
---|
6759 | Py_INCREF(Py_None);
|
---|
6760 | return Py_None;
|
---|
6761 | }
|
---|
6762 | #endif
|
---|
6763 |
|
---|
6764 | #ifdef HAVE_PUTENV
|
---|
6765 | PyDoc_STRVAR(posix_putenv__doc__,
|
---|
6766 | "putenv(key, value)\n\n\
|
---|
6767 | Change or add an environment variable.");
|
---|
6768 |
|
---|
6769 | /* Save putenv() parameters as values here, so we can collect them when they
|
---|
6770 | * get re-set with another call for the same key. */
|
---|
6771 | static PyObject *posix_putenv_garbage;
|
---|
6772 |
|
---|
6773 | static PyObject *
|
---|
6774 | posix_putenv(PyObject *self, PyObject *args)
|
---|
6775 | {
|
---|
6776 | char *s1, *s2;
|
---|
6777 | char *newenv;
|
---|
6778 | PyObject *newstr;
|
---|
6779 | size_t len;
|
---|
6780 |
|
---|
6781 | if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2))
|
---|
6782 | return NULL;
|
---|
6783 |
|
---|
6784 | #if defined(PYOS_OS2)
|
---|
6785 | if (stricmp(s1, "BEGINLIBPATH") == 0) {
|
---|
6786 | APIRET rc;
|
---|
6787 |
|
---|
6788 | rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
|
---|
6789 | if (rc != NO_ERROR)
|
---|
6790 | return os2_error(rc);
|
---|
6791 |
|
---|
6792 | } else if (stricmp(s1, "ENDLIBPATH") == 0) {
|
---|
6793 | APIRET rc;
|
---|
6794 |
|
---|
6795 | rc = DosSetExtLIBPATH(s2, END_LIBPATH);
|
---|
6796 | if (rc != NO_ERROR)
|
---|
6797 | return os2_error(rc);
|
---|
6798 | } else {
|
---|
6799 | #endif
|
---|
6800 |
|
---|
6801 | /* XXX This can leak memory -- not easy to fix :-( */
|
---|
6802 | len = strlen(s1) + strlen(s2) + 2;
|
---|
6803 | /* len includes space for a trailing \0; the size arg to
|
---|
6804 | PyString_FromStringAndSize does not count that */
|
---|
6805 | newstr = PyString_FromStringAndSize(NULL, (int)len - 1);
|
---|
6806 | if (newstr == NULL)
|
---|
6807 | return PyErr_NoMemory();
|
---|
6808 | newenv = PyString_AS_STRING(newstr);
|
---|
6809 | PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
|
---|
6810 | if (putenv(newenv)) {
|
---|
6811 | Py_DECREF(newstr);
|
---|
6812 | posix_error();
|
---|
6813 | return NULL;
|
---|
6814 | }
|
---|
6815 | /* Install the first arg and newstr in posix_putenv_garbage;
|
---|
6816 | * this will cause previous value to be collected. This has to
|
---|
6817 | * happen after the real putenv() call because the old value
|
---|
6818 | * was still accessible until then. */
|
---|
6819 | if (PyDict_SetItem(posix_putenv_garbage,
|
---|
6820 | PyTuple_GET_ITEM(args, 0), newstr)) {
|
---|
6821 | /* really not much we can do; just leak */
|
---|
6822 | PyErr_Clear();
|
---|
6823 | }
|
---|
6824 | else {
|
---|
6825 | Py_DECREF(newstr);
|
---|
6826 | }
|
---|
6827 |
|
---|
6828 | #if defined(PYOS_OS2)
|
---|
6829 | }
|
---|
6830 | #endif
|
---|
6831 | Py_INCREF(Py_None);
|
---|
6832 | return Py_None;
|
---|
6833 | }
|
---|
6834 | #endif /* putenv */
|
---|
6835 |
|
---|
6836 | #ifdef HAVE_UNSETENV
|
---|
6837 | PyDoc_STRVAR(posix_unsetenv__doc__,
|
---|
6838 | "unsetenv(key)\n\n\
|
---|
6839 | Delete an environment variable.");
|
---|
6840 |
|
---|
6841 | static PyObject *
|
---|
6842 | posix_unsetenv(PyObject *self, PyObject *args)
|
---|
6843 | {
|
---|
6844 | char *s1;
|
---|
6845 |
|
---|
6846 | if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
|
---|
6847 | return NULL;
|
---|
6848 |
|
---|
6849 | unsetenv(s1);
|
---|
6850 |
|
---|
6851 | /* Remove the key from posix_putenv_garbage;
|
---|
6852 | * this will cause it to be collected. This has to
|
---|
6853 | * happen after the real unsetenv() call because the
|
---|
6854 | * old value was still accessible until then.
|
---|
6855 | */
|
---|
6856 | if (PyDict_DelItem(posix_putenv_garbage,
|
---|
6857 | PyTuple_GET_ITEM(args, 0))) {
|
---|
6858 | /* really not much we can do; just leak */
|
---|
6859 | PyErr_Clear();
|
---|
6860 | }
|
---|
6861 |
|
---|
6862 | Py_INCREF(Py_None);
|
---|
6863 | return Py_None;
|
---|
6864 | }
|
---|
6865 | #endif /* unsetenv */
|
---|
6866 |
|
---|
6867 | PyDoc_STRVAR(posix_strerror__doc__,
|
---|
6868 | "strerror(code) -> string\n\n\
|
---|
6869 | Translate an error code to a message string.");
|
---|
6870 |
|
---|
6871 | static PyObject *
|
---|
6872 | posix_strerror(PyObject *self, PyObject *args)
|
---|
6873 | {
|
---|
6874 | int code;
|
---|
6875 | char *message;
|
---|
6876 | if (!PyArg_ParseTuple(args, "i:strerror", &code))
|
---|
6877 | return NULL;
|
---|
6878 | message = strerror(code);
|
---|
6879 | if (message == NULL) {
|
---|
6880 | PyErr_SetString(PyExc_ValueError,
|
---|
6881 | "strerror() argument out of range");
|
---|
6882 | return NULL;
|
---|
6883 | }
|
---|
6884 | return PyString_FromString(message);
|
---|
6885 | }
|
---|
6886 |
|
---|
6887 |
|
---|
6888 | #ifdef HAVE_SYS_WAIT_H
|
---|
6889 |
|
---|
6890 | #ifdef WCOREDUMP
|
---|
6891 | PyDoc_STRVAR(posix_WCOREDUMP__doc__,
|
---|
6892 | "WCOREDUMP(status) -> bool\n\n\
|
---|
6893 | Return True if the process returning 'status' was dumped to a core file.");
|
---|
6894 |
|
---|
6895 | static PyObject *
|
---|
6896 | posix_WCOREDUMP(PyObject *self, PyObject *args)
|
---|
6897 | {
|
---|
6898 | WAIT_TYPE status;
|
---|
6899 | WAIT_STATUS_INT(status) = 0;
|
---|
6900 |
|
---|
6901 | if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
|
---|
6902 | return NULL;
|
---|
6903 |
|
---|
6904 | return PyBool_FromLong(WCOREDUMP(status));
|
---|
6905 | }
|
---|
6906 | #endif /* WCOREDUMP */
|
---|
6907 |
|
---|
6908 | #ifdef WIFCONTINUED
|
---|
6909 | PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
|
---|
6910 | "WIFCONTINUED(status) -> bool\n\n\
|
---|
6911 | Return True if the process returning 'status' was continued from a\n\
|
---|
6912 | job control stop.");
|
---|
6913 |
|
---|
6914 | static PyObject *
|
---|
6915 | posix_WIFCONTINUED(PyObject *self, PyObject *args)
|
---|
6916 | {
|
---|
6917 | WAIT_TYPE status;
|
---|
6918 | WAIT_STATUS_INT(status) = 0;
|
---|
6919 |
|
---|
6920 | if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
|
---|
6921 | return NULL;
|
---|
6922 |
|
---|
6923 | return PyBool_FromLong(WIFCONTINUED(status));
|
---|
6924 | }
|
---|
6925 | #endif /* WIFCONTINUED */
|
---|
6926 |
|
---|
6927 | #ifdef WIFSTOPPED
|
---|
6928 | PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
|
---|
6929 | "WIFSTOPPED(status) -> bool\n\n\
|
---|
6930 | Return True if the process returning 'status' was stopped.");
|
---|
6931 |
|
---|
6932 | static PyObject *
|
---|
6933 | posix_WIFSTOPPED(PyObject *self, PyObject *args)
|
---|
6934 | {
|
---|
6935 | WAIT_TYPE status;
|
---|
6936 | WAIT_STATUS_INT(status) = 0;
|
---|
6937 |
|
---|
6938 | if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
|
---|
6939 | return NULL;
|
---|
6940 |
|
---|
6941 | return PyBool_FromLong(WIFSTOPPED(status));
|
---|
6942 | }
|
---|
6943 | #endif /* WIFSTOPPED */
|
---|
6944 |
|
---|
6945 | #ifdef WIFSIGNALED
|
---|
6946 | PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
|
---|
6947 | "WIFSIGNALED(status) -> bool\n\n\
|
---|
6948 | Return True if the process returning 'status' was terminated by a signal.");
|
---|
6949 |
|
---|
6950 | static PyObject *
|
---|
6951 | posix_WIFSIGNALED(PyObject *self, PyObject *args)
|
---|
6952 | {
|
---|
6953 | WAIT_TYPE status;
|
---|
6954 | WAIT_STATUS_INT(status) = 0;
|
---|
6955 |
|
---|
6956 | if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
|
---|
6957 | return NULL;
|
---|
6958 |
|
---|
6959 | return PyBool_FromLong(WIFSIGNALED(status));
|
---|
6960 | }
|
---|
6961 | #endif /* WIFSIGNALED */
|
---|
6962 |
|
---|
6963 | #ifdef WIFEXITED
|
---|
6964 | PyDoc_STRVAR(posix_WIFEXITED__doc__,
|
---|
6965 | "WIFEXITED(status) -> bool\n\n\
|
---|
6966 | Return true if the process returning 'status' exited using the exit()\n\
|
---|
6967 | system call.");
|
---|
6968 |
|
---|
6969 | static PyObject *
|
---|
6970 | posix_WIFEXITED(PyObject *self, PyObject *args)
|
---|
6971 | {
|
---|
6972 | WAIT_TYPE status;
|
---|
6973 | WAIT_STATUS_INT(status) = 0;
|
---|
6974 |
|
---|
6975 | if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
|
---|
6976 | return NULL;
|
---|
6977 |
|
---|
6978 | return PyBool_FromLong(WIFEXITED(status));
|
---|
6979 | }
|
---|
6980 | #endif /* WIFEXITED */
|
---|
6981 |
|
---|
6982 | #ifdef WEXITSTATUS
|
---|
6983 | PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
|
---|
6984 | "WEXITSTATUS(status) -> integer\n\n\
|
---|
6985 | Return the process return code from 'status'.");
|
---|
6986 |
|
---|
6987 | static PyObject *
|
---|
6988 | posix_WEXITSTATUS(PyObject *self, PyObject *args)
|
---|
6989 | {
|
---|
6990 | WAIT_TYPE status;
|
---|
6991 | WAIT_STATUS_INT(status) = 0;
|
---|
6992 |
|
---|
6993 | if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
|
---|
6994 | return NULL;
|
---|
6995 |
|
---|
6996 | return Py_BuildValue("i", WEXITSTATUS(status));
|
---|
6997 | }
|
---|
6998 | #endif /* WEXITSTATUS */
|
---|
6999 |
|
---|
7000 | #ifdef WTERMSIG
|
---|
7001 | PyDoc_STRVAR(posix_WTERMSIG__doc__,
|
---|
7002 | "WTERMSIG(status) -> integer\n\n\
|
---|
7003 | Return the signal that terminated the process that provided the 'status'\n\
|
---|
7004 | value.");
|
---|
7005 |
|
---|
7006 | static PyObject *
|
---|
7007 | posix_WTERMSIG(PyObject *self, PyObject *args)
|
---|
7008 | {
|
---|
7009 | WAIT_TYPE status;
|
---|
7010 | WAIT_STATUS_INT(status) = 0;
|
---|
7011 |
|
---|
7012 | if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
|
---|
7013 | return NULL;
|
---|
7014 |
|
---|
7015 | return Py_BuildValue("i", WTERMSIG(status));
|
---|
7016 | }
|
---|
7017 | #endif /* WTERMSIG */
|
---|
7018 |
|
---|
7019 | #ifdef WSTOPSIG
|
---|
7020 | PyDoc_STRVAR(posix_WSTOPSIG__doc__,
|
---|
7021 | "WSTOPSIG(status) -> integer\n\n\
|
---|
7022 | Return the signal that stopped the process that provided\n\
|
---|
7023 | the 'status' value.");
|
---|
7024 |
|
---|
7025 | static PyObject *
|
---|
7026 | posix_WSTOPSIG(PyObject *self, PyObject *args)
|
---|
7027 | {
|
---|
7028 | WAIT_TYPE status;
|
---|
7029 | WAIT_STATUS_INT(status) = 0;
|
---|
7030 |
|
---|
7031 | if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
|
---|
7032 | return NULL;
|
---|
7033 |
|
---|
7034 | return Py_BuildValue("i", WSTOPSIG(status));
|
---|
7035 | }
|
---|
7036 | #endif /* WSTOPSIG */
|
---|
7037 |
|
---|
7038 | #endif /* HAVE_SYS_WAIT_H */
|
---|
7039 |
|
---|
7040 |
|
---|
7041 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
|
---|
7042 | #ifdef _SCO_DS
|
---|
7043 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
|
---|
7044 | needed definitions in sys/statvfs.h */
|
---|
7045 | #define _SVID3
|
---|
7046 | #endif
|
---|
7047 | #include <sys/statvfs.h>
|
---|
7048 |
|
---|
7049 | static PyObject*
|
---|
7050 | _pystatvfs_fromstructstatvfs(struct statvfs st) {
|
---|
7051 | PyObject *v = PyStructSequence_New(&StatVFSResultType);
|
---|
7052 | if (v == NULL)
|
---|
7053 | return NULL;
|
---|
7054 |
|
---|
7055 | #if !defined(HAVE_LARGEFILE_SUPPORT)
|
---|
7056 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
|
---|
7057 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
|
---|
7058 | PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks));
|
---|
7059 | PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree));
|
---|
7060 | PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail));
|
---|
7061 | PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files));
|
---|
7062 | PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree));
|
---|
7063 | PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail));
|
---|
7064 | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
|
---|
7065 | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
|
---|
7066 | #else
|
---|
7067 | PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize));
|
---|
7068 | PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize));
|
---|
7069 | PyStructSequence_SET_ITEM(v, 2,
|
---|
7070 | PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
|
---|
7071 | PyStructSequence_SET_ITEM(v, 3,
|
---|
7072 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
|
---|
7073 | PyStructSequence_SET_ITEM(v, 4,
|
---|
7074 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
|
---|
7075 | PyStructSequence_SET_ITEM(v, 5,
|
---|
7076 | PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
|
---|
7077 | PyStructSequence_SET_ITEM(v, 6,
|
---|
7078 | PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
|
---|
7079 | PyStructSequence_SET_ITEM(v, 7,
|
---|
7080 | PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
|
---|
7081 | PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag));
|
---|
7082 | PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax));
|
---|
7083 | #endif
|
---|
7084 |
|
---|
7085 | return v;
|
---|
7086 | }
|
---|
7087 |
|
---|
7088 | PyDoc_STRVAR(posix_fstatvfs__doc__,
|
---|
7089 | "fstatvfs(fd) -> statvfs result\n\n\
|
---|
7090 | Perform an fstatvfs system call on the given fd.");
|
---|
7091 |
|
---|
7092 | static PyObject *
|
---|
7093 | posix_fstatvfs(PyObject *self, PyObject *args)
|
---|
7094 | {
|
---|
7095 | int fd, res;
|
---|
7096 | struct statvfs st;
|
---|
7097 |
|
---|
7098 | if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
|
---|
7099 | return NULL;
|
---|
7100 | Py_BEGIN_ALLOW_THREADS
|
---|
7101 | res = fstatvfs(fd, &st);
|
---|
7102 | Py_END_ALLOW_THREADS
|
---|
7103 | if (res != 0)
|
---|
7104 | return posix_error();
|
---|
7105 |
|
---|
7106 | return _pystatvfs_fromstructstatvfs(st);
|
---|
7107 | }
|
---|
7108 | #endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
|
---|
7109 |
|
---|
7110 |
|
---|
7111 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
|
---|
7112 | #include <sys/statvfs.h>
|
---|
7113 |
|
---|
7114 | PyDoc_STRVAR(posix_statvfs__doc__,
|
---|
7115 | "statvfs(path) -> statvfs result\n\n\
|
---|
7116 | Perform a statvfs system call on the given path.");
|
---|
7117 |
|
---|
7118 | static PyObject *
|
---|
7119 | posix_statvfs(PyObject *self, PyObject *args)
|
---|
7120 | {
|
---|
7121 | char *path;
|
---|
7122 | int res;
|
---|
7123 | struct statvfs st;
|
---|
7124 | if (!PyArg_ParseTuple(args, "s:statvfs", &path))
|
---|
7125 | return NULL;
|
---|
7126 | Py_BEGIN_ALLOW_THREADS
|
---|
7127 | res = statvfs(path, &st);
|
---|
7128 | Py_END_ALLOW_THREADS
|
---|
7129 | if (res != 0)
|
---|
7130 | return posix_error_with_filename(path);
|
---|
7131 |
|
---|
7132 | return _pystatvfs_fromstructstatvfs(st);
|
---|
7133 | }
|
---|
7134 | #endif /* HAVE_STATVFS */
|
---|
7135 |
|
---|
7136 |
|
---|
7137 | #ifdef HAVE_TEMPNAM
|
---|
7138 | PyDoc_STRVAR(posix_tempnam__doc__,
|
---|
7139 | "tempnam([dir[, prefix]]) -> string\n\n\
|
---|
7140 | Return a unique name for a temporary file.\n\
|
---|
7141 | The directory and a prefix may be specified as strings; they may be omitted\n\
|
---|
7142 | or None if not needed.");
|
---|
7143 |
|
---|
7144 | static PyObject *
|
---|
7145 | posix_tempnam(PyObject *self, PyObject *args)
|
---|
7146 | {
|
---|
7147 | PyObject *result = NULL;
|
---|
7148 | char *dir = NULL;
|
---|
7149 | char *pfx = NULL;
|
---|
7150 | char *name;
|
---|
7151 |
|
---|
7152 | if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx))
|
---|
7153 | return NULL;
|
---|
7154 |
|
---|
7155 | if (PyErr_Warn(PyExc_RuntimeWarning,
|
---|
7156 | "tempnam is a potential security risk to your program") < 0)
|
---|
7157 | return NULL;
|
---|
7158 |
|
---|
7159 | #ifdef MS_WINDOWS
|
---|
7160 | name = _tempnam(dir, pfx);
|
---|
7161 | #else
|
---|
7162 | name = tempnam(dir, pfx);
|
---|
7163 | #endif
|
---|
7164 | if (name == NULL)
|
---|
7165 | return PyErr_NoMemory();
|
---|
7166 | result = PyString_FromString(name);
|
---|
7167 | free(name);
|
---|
7168 | return result;
|
---|
7169 | }
|
---|
7170 | #endif
|
---|
7171 |
|
---|
7172 |
|
---|
7173 | #ifdef HAVE_TMPFILE
|
---|
7174 | PyDoc_STRVAR(posix_tmpfile__doc__,
|
---|
7175 | "tmpfile() -> file object\n\n\
|
---|
7176 | Create a temporary file with no directory entries.");
|
---|
7177 |
|
---|
7178 | static PyObject *
|
---|
7179 | posix_tmpfile(PyObject *self, PyObject *noargs)
|
---|
7180 | {
|
---|
7181 | FILE *fp;
|
---|
7182 |
|
---|
7183 | fp = tmpfile();
|
---|
7184 | if (fp == NULL)
|
---|
7185 | return posix_error();
|
---|
7186 | return PyFile_FromFile(fp, "<tmpfile>", "w+b", fclose);
|
---|
7187 | }
|
---|
7188 | #endif
|
---|
7189 |
|
---|
7190 |
|
---|
7191 | #ifdef HAVE_TMPNAM
|
---|
7192 | PyDoc_STRVAR(posix_tmpnam__doc__,
|
---|
7193 | "tmpnam() -> string\n\n\
|
---|
7194 | Return a unique name for a temporary file.");
|
---|
7195 |
|
---|
7196 | static PyObject *
|
---|
7197 | posix_tmpnam(PyObject *self, PyObject *noargs)
|
---|
7198 | {
|
---|
7199 | char buffer[L_tmpnam];
|
---|
7200 | char *name;
|
---|
7201 |
|
---|
7202 | if (PyErr_Warn(PyExc_RuntimeWarning,
|
---|
7203 | "tmpnam is a potential security risk to your program") < 0)
|
---|
7204 | return NULL;
|
---|
7205 |
|
---|
7206 | #ifdef USE_TMPNAM_R
|
---|
7207 | name = tmpnam_r(buffer);
|
---|
7208 | #else
|
---|
7209 | name = tmpnam(buffer);
|
---|
7210 | #endif
|
---|
7211 | if (name == NULL) {
|
---|
7212 | PyObject *err = Py_BuildValue("is", 0,
|
---|
7213 | #ifdef USE_TMPNAM_R
|
---|
7214 | "unexpected NULL from tmpnam_r"
|
---|
7215 | #else
|
---|
7216 | "unexpected NULL from tmpnam"
|
---|
7217 | #endif
|
---|
7218 | );
|
---|
7219 | PyErr_SetObject(PyExc_OSError, err);
|
---|
7220 | Py_XDECREF(err);
|
---|
7221 | return NULL;
|
---|
7222 | }
|
---|
7223 | return PyString_FromString(buffer);
|
---|
7224 | }
|
---|
7225 | #endif
|
---|
7226 |
|
---|
7227 |
|
---|
7228 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf().
|
---|
7229 | * It maps strings representing configuration variable names to
|
---|
7230 | * integer values, allowing those functions to be called with the
|
---|
7231 | * magic names instead of polluting the module's namespace with tons of
|
---|
7232 | * rarely-used constants. There are three separate tables that use
|
---|
7233 | * these definitions.
|
---|
7234 | *
|
---|
7235 | * This code is always included, even if none of the interfaces that
|
---|
7236 | * need it are included. The #if hackery needed to avoid it would be
|
---|
7237 | * sufficiently pervasive that it's not worth the loss of readability.
|
---|
7238 | */
|
---|
7239 | struct constdef {
|
---|
7240 | char *name;
|
---|
7241 | long value;
|
---|
7242 | };
|
---|
7243 |
|
---|
7244 | static int
|
---|
7245 | conv_confname(PyObject *arg, int *valuep, struct constdef *table,
|
---|
7246 | size_t tablesize)
|
---|
7247 | {
|
---|
7248 | if (PyInt_Check(arg)) {
|
---|
7249 | *valuep = PyInt_AS_LONG(arg);
|
---|
7250 | return 1;
|
---|
7251 | }
|
---|
7252 | if (PyString_Check(arg)) {
|
---|
7253 | /* look up the value in the table using a binary search */
|
---|
7254 | size_t lo = 0;
|
---|
7255 | size_t mid;
|
---|
7256 | size_t hi = tablesize;
|
---|
7257 | int cmp;
|
---|
7258 | char *confname = PyString_AS_STRING(arg);
|
---|
7259 | while (lo < hi) {
|
---|
7260 | mid = (lo + hi) / 2;
|
---|
7261 | cmp = strcmp(confname, table[mid].name);
|
---|
7262 | if (cmp < 0)
|
---|
7263 | hi = mid;
|
---|
7264 | else if (cmp > 0)
|
---|
7265 | lo = mid + 1;
|
---|
7266 | else {
|
---|
7267 | *valuep = table[mid].value;
|
---|
7268 | return 1;
|
---|
7269 | }
|
---|
7270 | }
|
---|
7271 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
|
---|
7272 | }
|
---|
7273 | else
|
---|
7274 | PyErr_SetString(PyExc_TypeError,
|
---|
7275 | "configuration names must be strings or integers");
|
---|
7276 | return 0;
|
---|
7277 | }
|
---|
7278 |
|
---|
7279 |
|
---|
7280 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
|
---|
7281 | static struct constdef posix_constants_pathconf[] = {
|
---|
7282 | #ifdef _PC_ABI_AIO_XFER_MAX
|
---|
7283 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
|
---|
7284 | #endif
|
---|
7285 | #ifdef _PC_ABI_ASYNC_IO
|
---|
7286 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
|
---|
7287 | #endif
|
---|
7288 | #ifdef _PC_ASYNC_IO
|
---|
7289 | {"PC_ASYNC_IO", _PC_ASYNC_IO},
|
---|
7290 | #endif
|
---|
7291 | #ifdef _PC_CHOWN_RESTRICTED
|
---|
7292 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
|
---|
7293 | #endif
|
---|
7294 | #ifdef _PC_FILESIZEBITS
|
---|
7295 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
|
---|
7296 | #endif
|
---|
7297 | #ifdef _PC_LAST
|
---|
7298 | {"PC_LAST", _PC_LAST},
|
---|
7299 | #endif
|
---|
7300 | #ifdef _PC_LINK_MAX
|
---|
7301 | {"PC_LINK_MAX", _PC_LINK_MAX},
|
---|
7302 | #endif
|
---|
7303 | #ifdef _PC_MAX_CANON
|
---|
7304 | {"PC_MAX_CANON", _PC_MAX_CANON},
|
---|
7305 | #endif
|
---|
7306 | #ifdef _PC_MAX_INPUT
|
---|
7307 | {"PC_MAX_INPUT", _PC_MAX_INPUT},
|
---|
7308 | #endif
|
---|
7309 | #ifdef _PC_NAME_MAX
|
---|
7310 | {"PC_NAME_MAX", _PC_NAME_MAX},
|
---|
7311 | #endif
|
---|
7312 | #ifdef _PC_NO_TRUNC
|
---|
7313 | {"PC_NO_TRUNC", _PC_NO_TRUNC},
|
---|
7314 | #endif
|
---|
7315 | #ifdef _PC_PATH_MAX
|
---|
7316 | {"PC_PATH_MAX", _PC_PATH_MAX},
|
---|
7317 | #endif
|
---|
7318 | #ifdef _PC_PIPE_BUF
|
---|
7319 | {"PC_PIPE_BUF", _PC_PIPE_BUF},
|
---|
7320 | #endif
|
---|
7321 | #ifdef _PC_PRIO_IO
|
---|
7322 | {"PC_PRIO_IO", _PC_PRIO_IO},
|
---|
7323 | #endif
|
---|
7324 | #ifdef _PC_SOCK_MAXBUF
|
---|
7325 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
|
---|
7326 | #endif
|
---|
7327 | #ifdef _PC_SYNC_IO
|
---|
7328 | {"PC_SYNC_IO", _PC_SYNC_IO},
|
---|
7329 | #endif
|
---|
7330 | #ifdef _PC_VDISABLE
|
---|
7331 | {"PC_VDISABLE", _PC_VDISABLE},
|
---|
7332 | #endif
|
---|
7333 | };
|
---|
7334 |
|
---|
7335 | static int
|
---|
7336 | conv_path_confname(PyObject *arg, int *valuep)
|
---|
7337 | {
|
---|
7338 | return conv_confname(arg, valuep, posix_constants_pathconf,
|
---|
7339 | sizeof(posix_constants_pathconf)
|
---|
7340 | / sizeof(struct constdef));
|
---|
7341 | }
|
---|
7342 | #endif
|
---|
7343 |
|
---|
7344 | #ifdef HAVE_FPATHCONF
|
---|
7345 | PyDoc_STRVAR(posix_fpathconf__doc__,
|
---|
7346 | "fpathconf(fd, name) -> integer\n\n\
|
---|
7347 | Return the configuration limit name for the file descriptor fd.\n\
|
---|
7348 | If there is no limit, return -1.");
|
---|
7349 |
|
---|
7350 | static PyObject *
|
---|
7351 | posix_fpathconf(PyObject *self, PyObject *args)
|
---|
7352 | {
|
---|
7353 | PyObject *result = NULL;
|
---|
7354 | int name, fd;
|
---|
7355 |
|
---|
7356 | if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
|
---|
7357 | conv_path_confname, &name)) {
|
---|
7358 | long limit;
|
---|
7359 |
|
---|
7360 | errno = 0;
|
---|
7361 | limit = fpathconf(fd, name);
|
---|
7362 | if (limit == -1 && errno != 0)
|
---|
7363 | posix_error();
|
---|
7364 | else
|
---|
7365 | result = PyInt_FromLong(limit);
|
---|
7366 | }
|
---|
7367 | return result;
|
---|
7368 | }
|
---|
7369 | #endif
|
---|
7370 |
|
---|
7371 |
|
---|
7372 | #ifdef HAVE_PATHCONF
|
---|
7373 | PyDoc_STRVAR(posix_pathconf__doc__,
|
---|
7374 | "pathconf(path, name) -> integer\n\n\
|
---|
7375 | Return the configuration limit name for the file or directory path.\n\
|
---|
7376 | If there is no limit, return -1.");
|
---|
7377 |
|
---|
7378 | static PyObject *
|
---|
7379 | posix_pathconf(PyObject *self, PyObject *args)
|
---|
7380 | {
|
---|
7381 | PyObject *result = NULL;
|
---|
7382 | int name;
|
---|
7383 | char *path;
|
---|
7384 |
|
---|
7385 | if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
|
---|
7386 | conv_path_confname, &name)) {
|
---|
7387 | long limit;
|
---|
7388 |
|
---|
7389 | errno = 0;
|
---|
7390 | limit = pathconf(path, name);
|
---|
7391 | if (limit == -1 && errno != 0) {
|
---|
7392 | if (errno == EINVAL)
|
---|
7393 | /* could be a path or name problem */
|
---|
7394 | posix_error();
|
---|
7395 | else
|
---|
7396 | posix_error_with_filename(path);
|
---|
7397 | }
|
---|
7398 | else
|
---|
7399 | result = PyInt_FromLong(limit);
|
---|
7400 | }
|
---|
7401 | return result;
|
---|
7402 | }
|
---|
7403 | #endif
|
---|
7404 |
|
---|
7405 | #ifdef HAVE_CONFSTR
|
---|
7406 | static struct constdef posix_constants_confstr[] = {
|
---|
7407 | #ifdef _CS_ARCHITECTURE
|
---|
7408 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
|
---|
7409 | #endif
|
---|
7410 | #ifdef _CS_HOSTNAME
|
---|
7411 | {"CS_HOSTNAME", _CS_HOSTNAME},
|
---|
7412 | #endif
|
---|
7413 | #ifdef _CS_HW_PROVIDER
|
---|
7414 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
|
---|
7415 | #endif
|
---|
7416 | #ifdef _CS_HW_SERIAL
|
---|
7417 | {"CS_HW_SERIAL", _CS_HW_SERIAL},
|
---|
7418 | #endif
|
---|
7419 | #ifdef _CS_INITTAB_NAME
|
---|
7420 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
|
---|
7421 | #endif
|
---|
7422 | #ifdef _CS_LFS64_CFLAGS
|
---|
7423 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
|
---|
7424 | #endif
|
---|
7425 | #ifdef _CS_LFS64_LDFLAGS
|
---|
7426 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
|
---|
7427 | #endif
|
---|
7428 | #ifdef _CS_LFS64_LIBS
|
---|
7429 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
|
---|
7430 | #endif
|
---|
7431 | #ifdef _CS_LFS64_LINTFLAGS
|
---|
7432 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
|
---|
7433 | #endif
|
---|
7434 | #ifdef _CS_LFS_CFLAGS
|
---|
7435 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
|
---|
7436 | #endif
|
---|
7437 | #ifdef _CS_LFS_LDFLAGS
|
---|
7438 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
|
---|
7439 | #endif
|
---|
7440 | #ifdef _CS_LFS_LIBS
|
---|
7441 | {"CS_LFS_LIBS", _CS_LFS_LIBS},
|
---|
7442 | #endif
|
---|
7443 | #ifdef _CS_LFS_LINTFLAGS
|
---|
7444 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
|
---|
7445 | #endif
|
---|
7446 | #ifdef _CS_MACHINE
|
---|
7447 | {"CS_MACHINE", _CS_MACHINE},
|
---|
7448 | #endif
|
---|
7449 | #ifdef _CS_PATH
|
---|
7450 | {"CS_PATH", _CS_PATH},
|
---|
7451 | #endif
|
---|
7452 | #ifdef _CS_RELEASE
|
---|
7453 | {"CS_RELEASE", _CS_RELEASE},
|
---|
7454 | #endif
|
---|
7455 | #ifdef _CS_SRPC_DOMAIN
|
---|
7456 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
|
---|
7457 | #endif
|
---|
7458 | #ifdef _CS_SYSNAME
|
---|
7459 | {"CS_SYSNAME", _CS_SYSNAME},
|
---|
7460 | #endif
|
---|
7461 | #ifdef _CS_VERSION
|
---|
7462 | {"CS_VERSION", _CS_VERSION},
|
---|
7463 | #endif
|
---|
7464 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
|
---|
7465 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
|
---|
7466 | #endif
|
---|
7467 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
|
---|
7468 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
|
---|
7469 | #endif
|
---|
7470 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS
|
---|
7471 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
|
---|
7472 | #endif
|
---|
7473 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
|
---|
7474 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
|
---|
7475 | #endif
|
---|
7476 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
|
---|
7477 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
|
---|
7478 | #endif
|
---|
7479 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
|
---|
7480 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
|
---|
7481 | #endif
|
---|
7482 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
|
---|
7483 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
|
---|
7484 | #endif
|
---|
7485 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
|
---|
7486 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
|
---|
7487 | #endif
|
---|
7488 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS
|
---|
7489 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
|
---|
7490 | #endif
|
---|
7491 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
|
---|
7492 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
|
---|
7493 | #endif
|
---|
7494 | #ifdef _CS_XBS5_LP64_OFF64_LIBS
|
---|
7495 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
|
---|
7496 | #endif
|
---|
7497 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
|
---|
7498 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
|
---|
7499 | #endif
|
---|
7500 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
|
---|
7501 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
|
---|
7502 | #endif
|
---|
7503 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
|
---|
7504 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
|
---|
7505 | #endif
|
---|
7506 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
|
---|
7507 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
|
---|
7508 | #endif
|
---|
7509 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
|
---|
7510 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
|
---|
7511 | #endif
|
---|
7512 | #ifdef _MIPS_CS_AVAIL_PROCESSORS
|
---|
7513 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
|
---|
7514 | #endif
|
---|
7515 | #ifdef _MIPS_CS_BASE
|
---|
7516 | {"MIPS_CS_BASE", _MIPS_CS_BASE},
|
---|
7517 | #endif
|
---|
7518 | #ifdef _MIPS_CS_HOSTID
|
---|
7519 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
|
---|
7520 | #endif
|
---|
7521 | #ifdef _MIPS_CS_HW_NAME
|
---|
7522 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
|
---|
7523 | #endif
|
---|
7524 | #ifdef _MIPS_CS_NUM_PROCESSORS
|
---|
7525 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
|
---|
7526 | #endif
|
---|
7527 | #ifdef _MIPS_CS_OSREL_MAJ
|
---|
7528 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
|
---|
7529 | #endif
|
---|
7530 | #ifdef _MIPS_CS_OSREL_MIN
|
---|
7531 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
|
---|
7532 | #endif
|
---|
7533 | #ifdef _MIPS_CS_OSREL_PATCH
|
---|
7534 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
|
---|
7535 | #endif
|
---|
7536 | #ifdef _MIPS_CS_OS_NAME
|
---|
7537 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
|
---|
7538 | #endif
|
---|
7539 | #ifdef _MIPS_CS_OS_PROVIDER
|
---|
7540 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
|
---|
7541 | #endif
|
---|
7542 | #ifdef _MIPS_CS_PROCESSORS
|
---|
7543 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
|
---|
7544 | #endif
|
---|
7545 | #ifdef _MIPS_CS_SERIAL
|
---|
7546 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
|
---|
7547 | #endif
|
---|
7548 | #ifdef _MIPS_CS_VENDOR
|
---|
7549 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
|
---|
7550 | #endif
|
---|
7551 | };
|
---|
7552 |
|
---|
7553 | static int
|
---|
7554 | conv_confstr_confname(PyObject *arg, int *valuep)
|
---|
7555 | {
|
---|
7556 | return conv_confname(arg, valuep, posix_constants_confstr,
|
---|
7557 | sizeof(posix_constants_confstr)
|
---|
7558 | / sizeof(struct constdef));
|
---|
7559 | }
|
---|
7560 |
|
---|
7561 | PyDoc_STRVAR(posix_confstr__doc__,
|
---|
7562 | "confstr(name) -> string\n\n\
|
---|
7563 | Return a string-valued system configuration variable.");
|
---|
7564 |
|
---|
7565 | static PyObject *
|
---|
7566 | posix_confstr(PyObject *self, PyObject *args)
|
---|
7567 | {
|
---|
7568 | PyObject *result = NULL;
|
---|
7569 | int name;
|
---|
7570 | char buffer[256];
|
---|
7571 |
|
---|
7572 | if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
|
---|
7573 | int len;
|
---|
7574 |
|
---|
7575 | errno = 0;
|
---|
7576 | len = confstr(name, buffer, sizeof(buffer));
|
---|
7577 | if (len == 0) {
|
---|
7578 | if (errno) {
|
---|
7579 | posix_error();
|
---|
7580 | }
|
---|
7581 | else {
|
---|
7582 | result = Py_None;
|
---|
7583 | Py_INCREF(Py_None);
|
---|
7584 | }
|
---|
7585 | }
|
---|
7586 | else {
|
---|
7587 | if ((unsigned int)len >= sizeof(buffer)) {
|
---|
7588 | result = PyString_FromStringAndSize(NULL, len-1);
|
---|
7589 | if (result != NULL)
|
---|
7590 | confstr(name, PyString_AS_STRING(result), len);
|
---|
7591 | }
|
---|
7592 | else
|
---|
7593 | result = PyString_FromStringAndSize(buffer, len-1);
|
---|
7594 | }
|
---|
7595 | }
|
---|
7596 | return result;
|
---|
7597 | }
|
---|
7598 | #endif
|
---|
7599 |
|
---|
7600 |
|
---|
7601 | #ifdef HAVE_SYSCONF
|
---|
7602 | static struct constdef posix_constants_sysconf[] = {
|
---|
7603 | #ifdef _SC_2_CHAR_TERM
|
---|
7604 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
|
---|
7605 | #endif
|
---|
7606 | #ifdef _SC_2_C_BIND
|
---|
7607 | {"SC_2_C_BIND", _SC_2_C_BIND},
|
---|
7608 | #endif
|
---|
7609 | #ifdef _SC_2_C_DEV
|
---|
7610 | {"SC_2_C_DEV", _SC_2_C_DEV},
|
---|
7611 | #endif
|
---|
7612 | #ifdef _SC_2_C_VERSION
|
---|
7613 | {"SC_2_C_VERSION", _SC_2_C_VERSION},
|
---|
7614 | #endif
|
---|
7615 | #ifdef _SC_2_FORT_DEV
|
---|
7616 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
|
---|
7617 | #endif
|
---|
7618 | #ifdef _SC_2_FORT_RUN
|
---|
7619 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
|
---|
7620 | #endif
|
---|
7621 | #ifdef _SC_2_LOCALEDEF
|
---|
7622 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
|
---|
7623 | #endif
|
---|
7624 | #ifdef _SC_2_SW_DEV
|
---|
7625 | {"SC_2_SW_DEV", _SC_2_SW_DEV},
|
---|
7626 | #endif
|
---|
7627 | #ifdef _SC_2_UPE
|
---|
7628 | {"SC_2_UPE", _SC_2_UPE},
|
---|
7629 | #endif
|
---|
7630 | #ifdef _SC_2_VERSION
|
---|
7631 | {"SC_2_VERSION", _SC_2_VERSION},
|
---|
7632 | #endif
|
---|
7633 | #ifdef _SC_ABI_ASYNCHRONOUS_IO
|
---|
7634 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
|
---|
7635 | #endif
|
---|
7636 | #ifdef _SC_ACL
|
---|
7637 | {"SC_ACL", _SC_ACL},
|
---|
7638 | #endif
|
---|
7639 | #ifdef _SC_AIO_LISTIO_MAX
|
---|
7640 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
|
---|
7641 | #endif
|
---|
7642 | #ifdef _SC_AIO_MAX
|
---|
7643 | {"SC_AIO_MAX", _SC_AIO_MAX},
|
---|
7644 | #endif
|
---|
7645 | #ifdef _SC_AIO_PRIO_DELTA_MAX
|
---|
7646 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
|
---|
7647 | #endif
|
---|
7648 | #ifdef _SC_ARG_MAX
|
---|
7649 | {"SC_ARG_MAX", _SC_ARG_MAX},
|
---|
7650 | #endif
|
---|
7651 | #ifdef _SC_ASYNCHRONOUS_IO
|
---|
7652 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
|
---|
7653 | #endif
|
---|
7654 | #ifdef _SC_ATEXIT_MAX
|
---|
7655 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
|
---|
7656 | #endif
|
---|
7657 | #ifdef _SC_AUDIT
|
---|
7658 | {"SC_AUDIT", _SC_AUDIT},
|
---|
7659 | #endif
|
---|
7660 | #ifdef _SC_AVPHYS_PAGES
|
---|
7661 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
|
---|
7662 | #endif
|
---|
7663 | #ifdef _SC_BC_BASE_MAX
|
---|
7664 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
|
---|
7665 | #endif
|
---|
7666 | #ifdef _SC_BC_DIM_MAX
|
---|
7667 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
|
---|
7668 | #endif
|
---|
7669 | #ifdef _SC_BC_SCALE_MAX
|
---|
7670 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
|
---|
7671 | #endif
|
---|
7672 | #ifdef _SC_BC_STRING_MAX
|
---|
7673 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
|
---|
7674 | #endif
|
---|
7675 | #ifdef _SC_CAP
|
---|
7676 | {"SC_CAP", _SC_CAP},
|
---|
7677 | #endif
|
---|
7678 | #ifdef _SC_CHARCLASS_NAME_MAX
|
---|
7679 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
|
---|
7680 | #endif
|
---|
7681 | #ifdef _SC_CHAR_BIT
|
---|
7682 | {"SC_CHAR_BIT", _SC_CHAR_BIT},
|
---|
7683 | #endif
|
---|
7684 | #ifdef _SC_CHAR_MAX
|
---|
7685 | {"SC_CHAR_MAX", _SC_CHAR_MAX},
|
---|
7686 | #endif
|
---|
7687 | #ifdef _SC_CHAR_MIN
|
---|
7688 | {"SC_CHAR_MIN", _SC_CHAR_MIN},
|
---|
7689 | #endif
|
---|
7690 | #ifdef _SC_CHILD_MAX
|
---|
7691 | {"SC_CHILD_MAX", _SC_CHILD_MAX},
|
---|
7692 | #endif
|
---|
7693 | #ifdef _SC_CLK_TCK
|
---|
7694 | {"SC_CLK_TCK", _SC_CLK_TCK},
|
---|
7695 | #endif
|
---|
7696 | #ifdef _SC_COHER_BLKSZ
|
---|
7697 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
|
---|
7698 | #endif
|
---|
7699 | #ifdef _SC_COLL_WEIGHTS_MAX
|
---|
7700 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
|
---|
7701 | #endif
|
---|
7702 | #ifdef _SC_DCACHE_ASSOC
|
---|
7703 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
|
---|
7704 | #endif
|
---|
7705 | #ifdef _SC_DCACHE_BLKSZ
|
---|
7706 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
|
---|
7707 | #endif
|
---|
7708 | #ifdef _SC_DCACHE_LINESZ
|
---|
7709 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
|
---|
7710 | #endif
|
---|
7711 | #ifdef _SC_DCACHE_SZ
|
---|
7712 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
|
---|
7713 | #endif
|
---|
7714 | #ifdef _SC_DCACHE_TBLKSZ
|
---|
7715 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
|
---|
7716 | #endif
|
---|
7717 | #ifdef _SC_DELAYTIMER_MAX
|
---|
7718 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
|
---|
7719 | #endif
|
---|
7720 | #ifdef _SC_EQUIV_CLASS_MAX
|
---|
7721 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
|
---|
7722 | #endif
|
---|
7723 | #ifdef _SC_EXPR_NEST_MAX
|
---|
7724 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
|
---|
7725 | #endif
|
---|
7726 | #ifdef _SC_FSYNC
|
---|
7727 | {"SC_FSYNC", _SC_FSYNC},
|
---|
7728 | #endif
|
---|
7729 | #ifdef _SC_GETGR_R_SIZE_MAX
|
---|
7730 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
|
---|
7731 | #endif
|
---|
7732 | #ifdef _SC_GETPW_R_SIZE_MAX
|
---|
7733 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
|
---|
7734 | #endif
|
---|
7735 | #ifdef _SC_ICACHE_ASSOC
|
---|
7736 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
|
---|
7737 | #endif
|
---|
7738 | #ifdef _SC_ICACHE_BLKSZ
|
---|
7739 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
|
---|
7740 | #endif
|
---|
7741 | #ifdef _SC_ICACHE_LINESZ
|
---|
7742 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
|
---|
7743 | #endif
|
---|
7744 | #ifdef _SC_ICACHE_SZ
|
---|
7745 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
|
---|
7746 | #endif
|
---|
7747 | #ifdef _SC_INF
|
---|
7748 | {"SC_INF", _SC_INF},
|
---|
7749 | #endif
|
---|
7750 | #ifdef _SC_INT_MAX
|
---|
7751 | {"SC_INT_MAX", _SC_INT_MAX},
|
---|
7752 | #endif
|
---|
7753 | #ifdef _SC_INT_MIN
|
---|
7754 | {"SC_INT_MIN", _SC_INT_MIN},
|
---|
7755 | #endif
|
---|
7756 | #ifdef _SC_IOV_MAX
|
---|
7757 | {"SC_IOV_MAX", _SC_IOV_MAX},
|
---|
7758 | #endif
|
---|
7759 | #ifdef _SC_IP_SECOPTS
|
---|
7760 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
|
---|
7761 | #endif
|
---|
7762 | #ifdef _SC_JOB_CONTROL
|
---|
7763 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
|
---|
7764 | #endif
|
---|
7765 | #ifdef _SC_KERN_POINTERS
|
---|
7766 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
|
---|
7767 | #endif
|
---|
7768 | #ifdef _SC_KERN_SIM
|
---|
7769 | {"SC_KERN_SIM", _SC_KERN_SIM},
|
---|
7770 | #endif
|
---|
7771 | #ifdef _SC_LINE_MAX
|
---|
7772 | {"SC_LINE_MAX", _SC_LINE_MAX},
|
---|
7773 | #endif
|
---|
7774 | #ifdef _SC_LOGIN_NAME_MAX
|
---|
7775 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
|
---|
7776 | #endif
|
---|
7777 | #ifdef _SC_LOGNAME_MAX
|
---|
7778 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
|
---|
7779 | #endif
|
---|
7780 | #ifdef _SC_LONG_BIT
|
---|
7781 | {"SC_LONG_BIT", _SC_LONG_BIT},
|
---|
7782 | #endif
|
---|
7783 | #ifdef _SC_MAC
|
---|
7784 | {"SC_MAC", _SC_MAC},
|
---|
7785 | #endif
|
---|
7786 | #ifdef _SC_MAPPED_FILES
|
---|
7787 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
|
---|
7788 | #endif
|
---|
7789 | #ifdef _SC_MAXPID
|
---|
7790 | {"SC_MAXPID", _SC_MAXPID},
|
---|
7791 | #endif
|
---|
7792 | #ifdef _SC_MB_LEN_MAX
|
---|
7793 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
|
---|
7794 | #endif
|
---|
7795 | #ifdef _SC_MEMLOCK
|
---|
7796 | {"SC_MEMLOCK", _SC_MEMLOCK},
|
---|
7797 | #endif
|
---|
7798 | #ifdef _SC_MEMLOCK_RANGE
|
---|
7799 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
|
---|
7800 | #endif
|
---|
7801 | #ifdef _SC_MEMORY_PROTECTION
|
---|
7802 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
|
---|
7803 | #endif
|
---|
7804 | #ifdef _SC_MESSAGE_PASSING
|
---|
7805 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
|
---|
7806 | #endif
|
---|
7807 | #ifdef _SC_MMAP_FIXED_ALIGNMENT
|
---|
7808 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
|
---|
7809 | #endif
|
---|
7810 | #ifdef _SC_MQ_OPEN_MAX
|
---|
7811 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
|
---|
7812 | #endif
|
---|
7813 | #ifdef _SC_MQ_PRIO_MAX
|
---|
7814 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
|
---|
7815 | #endif
|
---|
7816 | #ifdef _SC_NACLS_MAX
|
---|
7817 | {"SC_NACLS_MAX", _SC_NACLS_MAX},
|
---|
7818 | #endif
|
---|
7819 | #ifdef _SC_NGROUPS_MAX
|
---|
7820 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
|
---|
7821 | #endif
|
---|
7822 | #ifdef _SC_NL_ARGMAX
|
---|
7823 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
|
---|
7824 | #endif
|
---|
7825 | #ifdef _SC_NL_LANGMAX
|
---|
7826 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
|
---|
7827 | #endif
|
---|
7828 | #ifdef _SC_NL_MSGMAX
|
---|
7829 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
|
---|
7830 | #endif
|
---|
7831 | #ifdef _SC_NL_NMAX
|
---|
7832 | {"SC_NL_NMAX", _SC_NL_NMAX},
|
---|
7833 | #endif
|
---|
7834 | #ifdef _SC_NL_SETMAX
|
---|
7835 | {"SC_NL_SETMAX", _SC_NL_SETMAX},
|
---|
7836 | #endif
|
---|
7837 | #ifdef _SC_NL_TEXTMAX
|
---|
7838 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
|
---|
7839 | #endif
|
---|
7840 | #ifdef _SC_NPROCESSORS_CONF
|
---|
7841 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
|
---|
7842 | #endif
|
---|
7843 | #ifdef _SC_NPROCESSORS_ONLN
|
---|
7844 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
|
---|
7845 | #endif
|
---|
7846 | #ifdef _SC_NPROC_CONF
|
---|
7847 | {"SC_NPROC_CONF", _SC_NPROC_CONF},
|
---|
7848 | #endif
|
---|
7849 | #ifdef _SC_NPROC_ONLN
|
---|
7850 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
|
---|
7851 | #endif
|
---|
7852 | #ifdef _SC_NZERO
|
---|
7853 | {"SC_NZERO", _SC_NZERO},
|
---|
7854 | #endif
|
---|
7855 | #ifdef _SC_OPEN_MAX
|
---|
7856 | {"SC_OPEN_MAX", _SC_OPEN_MAX},
|
---|
7857 | #endif
|
---|
7858 | #ifdef _SC_PAGESIZE
|
---|
7859 | {"SC_PAGESIZE", _SC_PAGESIZE},
|
---|
7860 | #endif
|
---|
7861 | #ifdef _SC_PAGE_SIZE
|
---|
7862 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
|
---|
7863 | #endif
|
---|
7864 | #ifdef _SC_PASS_MAX
|
---|
7865 | {"SC_PASS_MAX", _SC_PASS_MAX},
|
---|
7866 | #endif
|
---|
7867 | #ifdef _SC_PHYS_PAGES
|
---|
7868 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
|
---|
7869 | #endif
|
---|
7870 | #ifdef _SC_PII
|
---|
7871 | {"SC_PII", _SC_PII},
|
---|
7872 | #endif
|
---|
7873 | #ifdef _SC_PII_INTERNET
|
---|
7874 | {"SC_PII_INTERNET", _SC_PII_INTERNET},
|
---|
7875 | #endif
|
---|
7876 | #ifdef _SC_PII_INTERNET_DGRAM
|
---|
7877 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
|
---|
7878 | #endif
|
---|
7879 | #ifdef _SC_PII_INTERNET_STREAM
|
---|
7880 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
|
---|
7881 | #endif
|
---|
7882 | #ifdef _SC_PII_OSI
|
---|
7883 | {"SC_PII_OSI", _SC_PII_OSI},
|
---|
7884 | #endif
|
---|
7885 | #ifdef _SC_PII_OSI_CLTS
|
---|
7886 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
|
---|
7887 | #endif
|
---|
7888 | #ifdef _SC_PII_OSI_COTS
|
---|
7889 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
|
---|
7890 | #endif
|
---|
7891 | #ifdef _SC_PII_OSI_M
|
---|
7892 | {"SC_PII_OSI_M", _SC_PII_OSI_M},
|
---|
7893 | #endif
|
---|
7894 | #ifdef _SC_PII_SOCKET
|
---|
7895 | {"SC_PII_SOCKET", _SC_PII_SOCKET},
|
---|
7896 | #endif
|
---|
7897 | #ifdef _SC_PII_XTI
|
---|
7898 | {"SC_PII_XTI", _SC_PII_XTI},
|
---|
7899 | #endif
|
---|
7900 | #ifdef _SC_POLL
|
---|
7901 | {"SC_POLL", _SC_POLL},
|
---|
7902 | #endif
|
---|
7903 | #ifdef _SC_PRIORITIZED_IO
|
---|
7904 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
|
---|
7905 | #endif
|
---|
7906 | #ifdef _SC_PRIORITY_SCHEDULING
|
---|
7907 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
|
---|
7908 | #endif
|
---|
7909 | #ifdef _SC_REALTIME_SIGNALS
|
---|
7910 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
|
---|
7911 | #endif
|
---|
7912 | #ifdef _SC_RE_DUP_MAX
|
---|
7913 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
|
---|
7914 | #endif
|
---|
7915 | #ifdef _SC_RTSIG_MAX
|
---|
7916 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
|
---|
7917 | #endif
|
---|
7918 | #ifdef _SC_SAVED_IDS
|
---|
7919 | {"SC_SAVED_IDS", _SC_SAVED_IDS},
|
---|
7920 | #endif
|
---|
7921 | #ifdef _SC_SCHAR_MAX
|
---|
7922 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
|
---|
7923 | #endif
|
---|
7924 | #ifdef _SC_SCHAR_MIN
|
---|
7925 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
|
---|
7926 | #endif
|
---|
7927 | #ifdef _SC_SELECT
|
---|
7928 | {"SC_SELECT", _SC_SELECT},
|
---|
7929 | #endif
|
---|
7930 | #ifdef _SC_SEMAPHORES
|
---|
7931 | {"SC_SEMAPHORES", _SC_SEMAPHORES},
|
---|
7932 | #endif
|
---|
7933 | #ifdef _SC_SEM_NSEMS_MAX
|
---|
7934 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
|
---|
7935 | #endif
|
---|
7936 | #ifdef _SC_SEM_VALUE_MAX
|
---|
7937 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
|
---|
7938 | #endif
|
---|
7939 | #ifdef _SC_SHARED_MEMORY_OBJECTS
|
---|
7940 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
|
---|
7941 | #endif
|
---|
7942 | #ifdef _SC_SHRT_MAX
|
---|
7943 | {"SC_SHRT_MAX", _SC_SHRT_MAX},
|
---|
7944 | #endif
|
---|
7945 | #ifdef _SC_SHRT_MIN
|
---|
7946 | {"SC_SHRT_MIN", _SC_SHRT_MIN},
|
---|
7947 | #endif
|
---|
7948 | #ifdef _SC_SIGQUEUE_MAX
|
---|
7949 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
|
---|
7950 | #endif
|
---|
7951 | #ifdef _SC_SIGRT_MAX
|
---|
7952 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
|
---|
7953 | #endif
|
---|
7954 | #ifdef _SC_SIGRT_MIN
|
---|
7955 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
|
---|
7956 | #endif
|
---|
7957 | #ifdef _SC_SOFTPOWER
|
---|
7958 | {"SC_SOFTPOWER", _SC_SOFTPOWER},
|
---|
7959 | #endif
|
---|
7960 | #ifdef _SC_SPLIT_CACHE
|
---|
7961 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
|
---|
7962 | #endif
|
---|
7963 | #ifdef _SC_SSIZE_MAX
|
---|
7964 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
|
---|
7965 | #endif
|
---|
7966 | #ifdef _SC_STACK_PROT
|
---|
7967 | {"SC_STACK_PROT", _SC_STACK_PROT},
|
---|
7968 | #endif
|
---|
7969 | #ifdef _SC_STREAM_MAX
|
---|
7970 | {"SC_STREAM_MAX", _SC_STREAM_MAX},
|
---|
7971 | #endif
|
---|
7972 | #ifdef _SC_SYNCHRONIZED_IO
|
---|
7973 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
|
---|
7974 | #endif
|
---|
7975 | #ifdef _SC_THREADS
|
---|
7976 | {"SC_THREADS", _SC_THREADS},
|
---|
7977 | #endif
|
---|
7978 | #ifdef _SC_THREAD_ATTR_STACKADDR
|
---|
7979 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
|
---|
7980 | #endif
|
---|
7981 | #ifdef _SC_THREAD_ATTR_STACKSIZE
|
---|
7982 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
|
---|
7983 | #endif
|
---|
7984 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
|
---|
7985 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
|
---|
7986 | #endif
|
---|
7987 | #ifdef _SC_THREAD_KEYS_MAX
|
---|
7988 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
|
---|
7989 | #endif
|
---|
7990 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING
|
---|
7991 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
|
---|
7992 | #endif
|
---|
7993 | #ifdef _SC_THREAD_PRIO_INHERIT
|
---|
7994 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
|
---|
7995 | #endif
|
---|
7996 | #ifdef _SC_THREAD_PRIO_PROTECT
|
---|
7997 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
|
---|
7998 | #endif
|
---|
7999 | #ifdef _SC_THREAD_PROCESS_SHARED
|
---|
8000 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
|
---|
8001 | #endif
|
---|
8002 | #ifdef _SC_THREAD_SAFE_FUNCTIONS
|
---|
8003 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
|
---|
8004 | #endif
|
---|
8005 | #ifdef _SC_THREAD_STACK_MIN
|
---|
8006 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
|
---|
8007 | #endif
|
---|
8008 | #ifdef _SC_THREAD_THREADS_MAX
|
---|
8009 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
|
---|
8010 | #endif
|
---|
8011 | #ifdef _SC_TIMERS
|
---|
8012 | {"SC_TIMERS", _SC_TIMERS},
|
---|
8013 | #endif
|
---|
8014 | #ifdef _SC_TIMER_MAX
|
---|
8015 | {"SC_TIMER_MAX", _SC_TIMER_MAX},
|
---|
8016 | #endif
|
---|
8017 | #ifdef _SC_TTY_NAME_MAX
|
---|
8018 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
|
---|
8019 | #endif
|
---|
8020 | #ifdef _SC_TZNAME_MAX
|
---|
8021 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
|
---|
8022 | #endif
|
---|
8023 | #ifdef _SC_T_IOV_MAX
|
---|
8024 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
|
---|
8025 | #endif
|
---|
8026 | #ifdef _SC_UCHAR_MAX
|
---|
8027 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
|
---|
8028 | #endif
|
---|
8029 | #ifdef _SC_UINT_MAX
|
---|
8030 | {"SC_UINT_MAX", _SC_UINT_MAX},
|
---|
8031 | #endif
|
---|
8032 | #ifdef _SC_UIO_MAXIOV
|
---|
8033 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
|
---|
8034 | #endif
|
---|
8035 | #ifdef _SC_ULONG_MAX
|
---|
8036 | {"SC_ULONG_MAX", _SC_ULONG_MAX},
|
---|
8037 | #endif
|
---|
8038 | #ifdef _SC_USHRT_MAX
|
---|
8039 | {"SC_USHRT_MAX", _SC_USHRT_MAX},
|
---|
8040 | #endif
|
---|
8041 | #ifdef _SC_VERSION
|
---|
8042 | {"SC_VERSION", _SC_VERSION},
|
---|
8043 | #endif
|
---|
8044 | #ifdef _SC_WORD_BIT
|
---|
8045 | {"SC_WORD_BIT", _SC_WORD_BIT},
|
---|
8046 | #endif
|
---|
8047 | #ifdef _SC_XBS5_ILP32_OFF32
|
---|
8048 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
|
---|
8049 | #endif
|
---|
8050 | #ifdef _SC_XBS5_ILP32_OFFBIG
|
---|
8051 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
|
---|
8052 | #endif
|
---|
8053 | #ifdef _SC_XBS5_LP64_OFF64
|
---|
8054 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
|
---|
8055 | #endif
|
---|
8056 | #ifdef _SC_XBS5_LPBIG_OFFBIG
|
---|
8057 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
|
---|
8058 | #endif
|
---|
8059 | #ifdef _SC_XOPEN_CRYPT
|
---|
8060 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
|
---|
8061 | #endif
|
---|
8062 | #ifdef _SC_XOPEN_ENH_I18N
|
---|
8063 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
|
---|
8064 | #endif
|
---|
8065 | #ifdef _SC_XOPEN_LEGACY
|
---|
8066 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
|
---|
8067 | #endif
|
---|
8068 | #ifdef _SC_XOPEN_REALTIME
|
---|
8069 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
|
---|
8070 | #endif
|
---|
8071 | #ifdef _SC_XOPEN_REALTIME_THREADS
|
---|
8072 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
|
---|
8073 | #endif
|
---|
8074 | #ifdef _SC_XOPEN_SHM
|
---|
8075 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
|
---|
8076 | #endif
|
---|
8077 | #ifdef _SC_XOPEN_UNIX
|
---|
8078 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
|
---|
8079 | #endif
|
---|
8080 | #ifdef _SC_XOPEN_VERSION
|
---|
8081 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
|
---|
8082 | #endif
|
---|
8083 | #ifdef _SC_XOPEN_XCU_VERSION
|
---|
8084 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
|
---|
8085 | #endif
|
---|
8086 | #ifdef _SC_XOPEN_XPG2
|
---|
8087 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
|
---|
8088 | #endif
|
---|
8089 | #ifdef _SC_XOPEN_XPG3
|
---|
8090 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
|
---|
8091 | #endif
|
---|
8092 | #ifdef _SC_XOPEN_XPG4
|
---|
8093 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
|
---|
8094 | #endif
|
---|
8095 | };
|
---|
8096 |
|
---|
8097 | static int
|
---|
8098 | conv_sysconf_confname(PyObject *arg, int *valuep)
|
---|
8099 | {
|
---|
8100 | return conv_confname(arg, valuep, posix_constants_sysconf,
|
---|
8101 | sizeof(posix_constants_sysconf)
|
---|
8102 | / sizeof(struct constdef));
|
---|
8103 | }
|
---|
8104 |
|
---|
8105 | PyDoc_STRVAR(posix_sysconf__doc__,
|
---|
8106 | "sysconf(name) -> integer\n\n\
|
---|
8107 | Return an integer-valued system configuration variable.");
|
---|
8108 |
|
---|
8109 | static PyObject *
|
---|
8110 | posix_sysconf(PyObject *self, PyObject *args)
|
---|
8111 | {
|
---|
8112 | PyObject *result = NULL;
|
---|
8113 | int name;
|
---|
8114 |
|
---|
8115 | if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
|
---|
8116 | int value;
|
---|
8117 |
|
---|
8118 | errno = 0;
|
---|
8119 | value = sysconf(name);
|
---|
8120 | if (value == -1 && errno != 0)
|
---|
8121 | posix_error();
|
---|
8122 | else
|
---|
8123 | result = PyInt_FromLong(value);
|
---|
8124 | }
|
---|
8125 | return result;
|
---|
8126 | }
|
---|
8127 | #endif
|
---|
8128 |
|
---|
8129 |
|
---|
8130 | /* This code is used to ensure that the tables of configuration value names
|
---|
8131 | * are in sorted order as required by conv_confname(), and also to build the
|
---|
8132 | * the exported dictionaries that are used to publish information about the
|
---|
8133 | * names available on the host platform.
|
---|
8134 | *
|
---|
8135 | * Sorting the table at runtime ensures that the table is properly ordered
|
---|
8136 | * when used, even for platforms we're not able to test on. It also makes
|
---|
8137 | * it easier to add additional entries to the tables.
|
---|
8138 | */
|
---|
8139 |
|
---|
8140 | static int
|
---|
8141 | cmp_constdefs(const void *v1, const void *v2)
|
---|
8142 | {
|
---|
8143 | const struct constdef *c1 =
|
---|
8144 | (const struct constdef *) v1;
|
---|
8145 | const struct constdef *c2 =
|
---|
8146 | (const struct constdef *) v2;
|
---|
8147 |
|
---|
8148 | return strcmp(c1->name, c2->name);
|
---|
8149 | }
|
---|
8150 |
|
---|
8151 | static int
|
---|
8152 | setup_confname_table(struct constdef *table, size_t tablesize,
|
---|
8153 | char *tablename, PyObject *module)
|
---|
8154 | {
|
---|
8155 | PyObject *d = NULL;
|
---|
8156 | size_t i;
|
---|
8157 |
|
---|
8158 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
|
---|
8159 | d = PyDict_New();
|
---|
8160 | if (d == NULL)
|
---|
8161 | return -1;
|
---|
8162 |
|
---|
8163 | for (i=0; i < tablesize; ++i) {
|
---|
8164 | PyObject *o = PyInt_FromLong(table[i].value);
|
---|
8165 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
|
---|
8166 | Py_XDECREF(o);
|
---|
8167 | Py_DECREF(d);
|
---|
8168 | return -1;
|
---|
8169 | }
|
---|
8170 | Py_DECREF(o);
|
---|
8171 | }
|
---|
8172 | return PyModule_AddObject(module, tablename, d);
|
---|
8173 | }
|
---|
8174 |
|
---|
8175 | /* Return -1 on failure, 0 on success. */
|
---|
8176 | static int
|
---|
8177 | setup_confname_tables(PyObject *module)
|
---|
8178 | {
|
---|
8179 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
|
---|
8180 | if (setup_confname_table(posix_constants_pathconf,
|
---|
8181 | sizeof(posix_constants_pathconf)
|
---|
8182 | / sizeof(struct constdef),
|
---|
8183 | "pathconf_names", module))
|
---|
8184 | return -1;
|
---|
8185 | #endif
|
---|
8186 | #ifdef HAVE_CONFSTR
|
---|
8187 | if (setup_confname_table(posix_constants_confstr,
|
---|
8188 | sizeof(posix_constants_confstr)
|
---|
8189 | / sizeof(struct constdef),
|
---|
8190 | "confstr_names", module))
|
---|
8191 | return -1;
|
---|
8192 | #endif
|
---|
8193 | #ifdef HAVE_SYSCONF
|
---|
8194 | if (setup_confname_table(posix_constants_sysconf,
|
---|
8195 | sizeof(posix_constants_sysconf)
|
---|
8196 | / sizeof(struct constdef),
|
---|
8197 | "sysconf_names", module))
|
---|
8198 | return -1;
|
---|
8199 | #endif
|
---|
8200 | return 0;
|
---|
8201 | }
|
---|
8202 |
|
---|
8203 |
|
---|
8204 | PyDoc_STRVAR(posix_abort__doc__,
|
---|
8205 | "abort() -> does not return!\n\n\
|
---|
8206 | Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
|
---|
8207 | in the hardest way possible on the hosting operating system.");
|
---|
8208 |
|
---|
8209 | static PyObject *
|
---|
8210 | posix_abort(PyObject *self, PyObject *noargs)
|
---|
8211 | {
|
---|
8212 | abort();
|
---|
8213 | /*NOTREACHED*/
|
---|
8214 | Py_FatalError("abort() called from Python code didn't abort!");
|
---|
8215 | return NULL;
|
---|
8216 | }
|
---|
8217 |
|
---|
8218 | #ifdef MS_WINDOWS
|
---|
8219 | PyDoc_STRVAR(win32_startfile__doc__,
|
---|
8220 | "startfile(filepath [, operation]) - Start a file with its associated\n\
|
---|
8221 | application.\n\
|
---|
8222 | \n\
|
---|
8223 | When \"operation\" is not specified or \"open\", this acts like\n\
|
---|
8224 | double-clicking the file in Explorer, or giving the file name as an\n\
|
---|
8225 | argument to the DOS \"start\" command: the file is opened with whatever\n\
|
---|
8226 | application (if any) its extension is associated.\n\
|
---|
8227 | When another \"operation\" is given, it specifies what should be done with\n\
|
---|
8228 | the file. A typical operation is \"print\".\n\
|
---|
8229 | \n\
|
---|
8230 | startfile returns as soon as the associated application is launched.\n\
|
---|
8231 | There is no option to wait for the application to close, and no way\n\
|
---|
8232 | to retrieve the application's exit status.\n\
|
---|
8233 | \n\
|
---|
8234 | The filepath is relative to the current directory. If you want to use\n\
|
---|
8235 | an absolute path, make sure the first character is not a slash (\"/\");\n\
|
---|
8236 | the underlying Win32 ShellExecute function doesn't work if it is.");
|
---|
8237 |
|
---|
8238 | static PyObject *
|
---|
8239 | win32_startfile(PyObject *self, PyObject *args)
|
---|
8240 | {
|
---|
8241 | char *filepath;
|
---|
8242 | char *operation = NULL;
|
---|
8243 | HINSTANCE rc;
|
---|
8244 | #ifdef Py_WIN_WIDE_FILENAMES
|
---|
8245 | if (unicode_file_names()) {
|
---|
8246 | PyObject *unipath, *woperation = NULL;
|
---|
8247 | if (!PyArg_ParseTuple(args, "U|s:startfile",
|
---|
8248 | &unipath, &operation)) {
|
---|
8249 | PyErr_Clear();
|
---|
8250 | goto normal;
|
---|
8251 | }
|
---|
8252 |
|
---|
8253 |
|
---|
8254 | if (operation) {
|
---|
8255 | woperation = PyUnicode_DecodeASCII(operation,
|
---|
8256 | strlen(operation), NULL);
|
---|
8257 | if (!woperation) {
|
---|
8258 | PyErr_Clear();
|
---|
8259 | operation = NULL;
|
---|
8260 | goto normal;
|
---|
8261 | }
|
---|
8262 | }
|
---|
8263 |
|
---|
8264 | Py_BEGIN_ALLOW_THREADS
|
---|
8265 | rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
|
---|
8266 | PyUnicode_AS_UNICODE(unipath),
|
---|
8267 | NULL, NULL, SW_SHOWNORMAL);
|
---|
8268 | Py_END_ALLOW_THREADS
|
---|
8269 |
|
---|
8270 | Py_XDECREF(woperation);
|
---|
8271 | if (rc <= (HINSTANCE)32) {
|
---|
8272 | PyObject *errval = win32_error_unicode("startfile",
|
---|
8273 | PyUnicode_AS_UNICODE(unipath));
|
---|
8274 | return errval;
|
---|
8275 | }
|
---|
8276 | Py_INCREF(Py_None);
|
---|
8277 | return Py_None;
|
---|
8278 | }
|
---|
8279 | #endif
|
---|
8280 |
|
---|
8281 | normal:
|
---|
8282 | if (!PyArg_ParseTuple(args, "et|s:startfile",
|
---|
8283 | Py_FileSystemDefaultEncoding, &filepath,
|
---|
8284 | &operation))
|
---|
8285 | return NULL;
|
---|
8286 | Py_BEGIN_ALLOW_THREADS
|
---|
8287 | rc = ShellExecute((HWND)0, operation, filepath,
|
---|
8288 | NULL, NULL, SW_SHOWNORMAL);
|
---|
8289 | Py_END_ALLOW_THREADS
|
---|
8290 | if (rc <= (HINSTANCE)32) {
|
---|
8291 | PyObject *errval = win32_error("startfile", filepath);
|
---|
8292 | PyMem_Free(filepath);
|
---|
8293 | return errval;
|
---|
8294 | }
|
---|
8295 | PyMem_Free(filepath);
|
---|
8296 | Py_INCREF(Py_None);
|
---|
8297 | return Py_None;
|
---|
8298 | }
|
---|
8299 | #endif
|
---|
8300 |
|
---|
8301 | #ifdef HAVE_GETLOADAVG
|
---|
8302 | PyDoc_STRVAR(posix_getloadavg__doc__,
|
---|
8303 | "getloadavg() -> (float, float, float)\n\n\
|
---|
8304 | Return the number of processes in the system run queue averaged over\n\
|
---|
8305 | the last 1, 5, and 15 minutes or raises OSError if the load average\n\
|
---|
8306 | was unobtainable");
|
---|
8307 |
|
---|
8308 | static PyObject *
|
---|
8309 | posix_getloadavg(PyObject *self, PyObject *noargs)
|
---|
8310 | {
|
---|
8311 | double loadavg[3];
|
---|
8312 | if (getloadavg(loadavg, 3)!=3) {
|
---|
8313 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
|
---|
8314 | return NULL;
|
---|
8315 | } else
|
---|
8316 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
|
---|
8317 | }
|
---|
8318 | #endif
|
---|
8319 |
|
---|
8320 | #ifdef MS_WINDOWS
|
---|
8321 |
|
---|
8322 | PyDoc_STRVAR(win32_urandom__doc__,
|
---|
8323 | "urandom(n) -> str\n\n\
|
---|
8324 | Return a string of n random bytes suitable for cryptographic use.");
|
---|
8325 |
|
---|
8326 | typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
|
---|
8327 | LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
|
---|
8328 | DWORD dwFlags );
|
---|
8329 | typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
|
---|
8330 | BYTE *pbBuffer );
|
---|
8331 |
|
---|
8332 | static CRYPTGENRANDOM pCryptGenRandom = NULL;
|
---|
8333 | /* This handle is never explicitly released. Instead, the operating
|
---|
8334 | system will release it when the process terminates. */
|
---|
8335 | static HCRYPTPROV hCryptProv = 0;
|
---|
8336 |
|
---|
8337 | static PyObject*
|
---|
8338 | win32_urandom(PyObject *self, PyObject *args)
|
---|
8339 | {
|
---|
8340 | int howMany;
|
---|
8341 | PyObject* result;
|
---|
8342 |
|
---|
8343 | /* Read arguments */
|
---|
8344 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
|
---|
8345 | return NULL;
|
---|
8346 | if (howMany < 0)
|
---|
8347 | return PyErr_Format(PyExc_ValueError,
|
---|
8348 | "negative argument not allowed");
|
---|
8349 |
|
---|
8350 | if (hCryptProv == 0) {
|
---|
8351 | HINSTANCE hAdvAPI32 = NULL;
|
---|
8352 | CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
|
---|
8353 |
|
---|
8354 | /* Obtain handle to the DLL containing CryptoAPI
|
---|
8355 | This should not fail */
|
---|
8356 | hAdvAPI32 = GetModuleHandle("advapi32.dll");
|
---|
8357 | if(hAdvAPI32 == NULL)
|
---|
8358 | return win32_error("GetModuleHandle", NULL);
|
---|
8359 |
|
---|
8360 | /* Obtain pointers to the CryptoAPI functions
|
---|
8361 | This will fail on some early versions of Win95 */
|
---|
8362 | pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
|
---|
8363 | hAdvAPI32,
|
---|
8364 | "CryptAcquireContextA");
|
---|
8365 | if (pCryptAcquireContext == NULL)
|
---|
8366 | return PyErr_Format(PyExc_NotImplementedError,
|
---|
8367 | "CryptAcquireContextA not found");
|
---|
8368 |
|
---|
8369 | pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
|
---|
8370 | hAdvAPI32, "CryptGenRandom");
|
---|
8371 | if (pCryptGenRandom == NULL)
|
---|
8372 | return PyErr_Format(PyExc_NotImplementedError,
|
---|
8373 | "CryptGenRandom not found");
|
---|
8374 |
|
---|
8375 | /* Acquire context */
|
---|
8376 | if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
|
---|
8377 | PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
|
---|
8378 | return win32_error("CryptAcquireContext", NULL);
|
---|
8379 | }
|
---|
8380 |
|
---|
8381 | /* Allocate bytes */
|
---|
8382 | result = PyString_FromStringAndSize(NULL, howMany);
|
---|
8383 | if (result != NULL) {
|
---|
8384 | /* Get random data */
|
---|
8385 | memset(PyString_AS_STRING(result), 0, howMany); /* zero seed */
|
---|
8386 | if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
|
---|
8387 | PyString_AS_STRING(result))) {
|
---|
8388 | Py_DECREF(result);
|
---|
8389 | return win32_error("CryptGenRandom", NULL);
|
---|
8390 | }
|
---|
8391 | }
|
---|
8392 | return result;
|
---|
8393 | }
|
---|
8394 | #endif
|
---|
8395 |
|
---|
8396 | #ifdef __VMS
|
---|
8397 | /* Use openssl random routine */
|
---|
8398 | #include <openssl/rand.h>
|
---|
8399 | PyDoc_STRVAR(vms_urandom__doc__,
|
---|
8400 | "urandom(n) -> str\n\n\
|
---|
8401 | Return a string of n random bytes suitable for cryptographic use.");
|
---|
8402 |
|
---|
8403 | static PyObject*
|
---|
8404 | vms_urandom(PyObject *self, PyObject *args)
|
---|
8405 | {
|
---|
8406 | int howMany;
|
---|
8407 | PyObject* result;
|
---|
8408 |
|
---|
8409 | /* Read arguments */
|
---|
8410 | if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
|
---|
8411 | return NULL;
|
---|
8412 | if (howMany < 0)
|
---|
8413 | return PyErr_Format(PyExc_ValueError,
|
---|
8414 | "negative argument not allowed");
|
---|
8415 |
|
---|
8416 | /* Allocate bytes */
|
---|
8417 | result = PyString_FromStringAndSize(NULL, howMany);
|
---|
8418 | if (result != NULL) {
|
---|
8419 | /* Get random data */
|
---|
8420 | if (RAND_pseudo_bytes((unsigned char*)
|
---|
8421 | PyString_AS_STRING(result),
|
---|
8422 | howMany) < 0) {
|
---|
8423 | Py_DECREF(result);
|
---|
8424 | return PyErr_Format(PyExc_ValueError,
|
---|
8425 | "RAND_pseudo_bytes");
|
---|
8426 | }
|
---|
8427 | }
|
---|
8428 | return result;
|
---|
8429 | }
|
---|
8430 | #endif
|
---|
8431 |
|
---|
8432 | static PyMethodDef posix_methods[] = {
|
---|
8433 | {"access", posix_access, METH_VARARGS, posix_access__doc__},
|
---|
8434 | #ifdef HAVE_TTYNAME
|
---|
8435 | {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
|
---|
8436 | #endif
|
---|
8437 | {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
|
---|
8438 | #ifdef HAVE_CHFLAGS
|
---|
8439 | {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
|
---|
8440 | #endif /* HAVE_CHFLAGS */
|
---|
8441 | {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
|
---|
8442 | #ifdef HAVE_FCHMOD
|
---|
8443 | {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
|
---|
8444 | #endif /* HAVE_FCHMOD */
|
---|
8445 | #ifdef HAVE_CHOWN
|
---|
8446 | {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
|
---|
8447 | #endif /* HAVE_CHOWN */
|
---|
8448 | #ifdef HAVE_LCHMOD
|
---|
8449 | {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
|
---|
8450 | #endif /* HAVE_LCHMOD */
|
---|
8451 | #ifdef HAVE_FCHOWN
|
---|
8452 | {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
|
---|
8453 | #endif /* HAVE_FCHOWN */
|
---|
8454 | #ifdef HAVE_LCHFLAGS
|
---|
8455 | {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
|
---|
8456 | #endif /* HAVE_LCHFLAGS */
|
---|
8457 | #ifdef HAVE_LCHOWN
|
---|
8458 | {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
|
---|
8459 | #endif /* HAVE_LCHOWN */
|
---|
8460 | #ifdef HAVE_CHROOT
|
---|
8461 | {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
|
---|
8462 | #endif
|
---|
8463 | #ifdef HAVE_CTERMID
|
---|
8464 | {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
|
---|
8465 | #endif
|
---|
8466 | #ifdef HAVE_GETCWD
|
---|
8467 | {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__},
|
---|
8468 | #ifdef Py_USING_UNICODE
|
---|
8469 | {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__},
|
---|
8470 | #endif
|
---|
8471 | #endif
|
---|
8472 | #ifdef HAVE_LINK
|
---|
8473 | {"link", posix_link, METH_VARARGS, posix_link__doc__},
|
---|
8474 | #endif /* HAVE_LINK */
|
---|
8475 | {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
|
---|
8476 | {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
|
---|
8477 | {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
|
---|
8478 | #ifdef HAVE_NICE
|
---|
8479 | {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
|
---|
8480 | #endif /* HAVE_NICE */
|
---|
8481 | #ifdef HAVE_READLINK
|
---|
8482 | {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
|
---|
8483 | #endif /* HAVE_READLINK */
|
---|
8484 | {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
|
---|
8485 | {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
|
---|
8486 | {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
|
---|
8487 | {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
|
---|
8488 | #ifdef HAVE_SYMLINK
|
---|
8489 | {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
|
---|
8490 | #endif /* HAVE_SYMLINK */
|
---|
8491 | #ifdef HAVE_SYSTEM
|
---|
8492 | {"system", posix_system, METH_VARARGS, posix_system__doc__},
|
---|
8493 | #endif
|
---|
8494 | {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
|
---|
8495 | #ifdef HAVE_UNAME
|
---|
8496 | {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
|
---|
8497 | #endif /* HAVE_UNAME */
|
---|
8498 | {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
|
---|
8499 | {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
|
---|
8500 | {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
|
---|
8501 | #ifdef HAVE_TIMES
|
---|
8502 | {"times", posix_times, METH_NOARGS, posix_times__doc__},
|
---|
8503 | #endif /* HAVE_TIMES */
|
---|
8504 | {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
|
---|
8505 | #ifdef HAVE_EXECV
|
---|
8506 | {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
|
---|
8507 | {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
|
---|
8508 | #endif /* HAVE_EXECV */
|
---|
8509 | #ifdef HAVE_SPAWNV
|
---|
8510 | {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
|
---|
8511 | {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
|
---|
8512 | #if defined(PYOS_OS2)
|
---|
8513 | {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
|
---|
8514 | {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
|
---|
8515 | #endif /* PYOS_OS2 */
|
---|
8516 | #endif /* HAVE_SPAWNV */
|
---|
8517 | #ifdef HAVE_FORK1
|
---|
8518 | {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
|
---|
8519 | #endif /* HAVE_FORK1 */
|
---|
8520 | #ifdef HAVE_FORK
|
---|
8521 | {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
|
---|
8522 | #endif /* HAVE_FORK */
|
---|
8523 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
|
---|
8524 | {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
|
---|
8525 | #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
|
---|
8526 | #ifdef HAVE_FORKPTY
|
---|
8527 | {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
|
---|
8528 | #endif /* HAVE_FORKPTY */
|
---|
8529 | #ifdef HAVE_GETEGID
|
---|
8530 | {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
|
---|
8531 | #endif /* HAVE_GETEGID */
|
---|
8532 | #ifdef HAVE_GETEUID
|
---|
8533 | {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
|
---|
8534 | #endif /* HAVE_GETEUID */
|
---|
8535 | #ifdef HAVE_GETGID
|
---|
8536 | {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
|
---|
8537 | #endif /* HAVE_GETGID */
|
---|
8538 | #ifdef HAVE_GETGROUPS
|
---|
8539 | {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
|
---|
8540 | #endif
|
---|
8541 | {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
|
---|
8542 | #ifdef HAVE_GETPGRP
|
---|
8543 | {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
|
---|
8544 | #endif /* HAVE_GETPGRP */
|
---|
8545 | #ifdef HAVE_GETPPID
|
---|
8546 | {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
|
---|
8547 | #endif /* HAVE_GETPPID */
|
---|
8548 | #ifdef HAVE_GETUID
|
---|
8549 | {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
|
---|
8550 | #endif /* HAVE_GETUID */
|
---|
8551 | #ifdef HAVE_GETLOGIN
|
---|
8552 | {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
|
---|
8553 | #endif
|
---|
8554 | #ifdef HAVE_KILL
|
---|
8555 | {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
|
---|
8556 | #endif /* HAVE_KILL */
|
---|
8557 | #ifdef HAVE_KILLPG
|
---|
8558 | {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
|
---|
8559 | #endif /* HAVE_KILLPG */
|
---|
8560 | #ifdef HAVE_PLOCK
|
---|
8561 | {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
|
---|
8562 | #endif /* HAVE_PLOCK */
|
---|
8563 | #ifdef HAVE_POPEN
|
---|
8564 | {"popen", posix_popen, METH_VARARGS, posix_popen__doc__},
|
---|
8565 | #ifdef MS_WINDOWS
|
---|
8566 | {"popen2", win32_popen2, METH_VARARGS},
|
---|
8567 | {"popen3", win32_popen3, METH_VARARGS},
|
---|
8568 | {"popen4", win32_popen4, METH_VARARGS},
|
---|
8569 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
|
---|
8570 | #else
|
---|
8571 | #if defined(PYOS_OS2) && defined(PYCC_GCC)
|
---|
8572 | {"popen2", os2emx_popen2, METH_VARARGS},
|
---|
8573 | {"popen3", os2emx_popen3, METH_VARARGS},
|
---|
8574 | {"popen4", os2emx_popen4, METH_VARARGS},
|
---|
8575 | #endif
|
---|
8576 | #endif
|
---|
8577 | #endif /* HAVE_POPEN */
|
---|
8578 | #ifdef HAVE_SETUID
|
---|
8579 | {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
|
---|
8580 | #endif /* HAVE_SETUID */
|
---|
8581 | #ifdef HAVE_SETEUID
|
---|
8582 | {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
|
---|
8583 | #endif /* HAVE_SETEUID */
|
---|
8584 | #ifdef HAVE_SETEGID
|
---|
8585 | {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
|
---|
8586 | #endif /* HAVE_SETEGID */
|
---|
8587 | #ifdef HAVE_SETREUID
|
---|
8588 | {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
|
---|
8589 | #endif /* HAVE_SETREUID */
|
---|
8590 | #ifdef HAVE_SETREGID
|
---|
8591 | {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
|
---|
8592 | #endif /* HAVE_SETREGID */
|
---|
8593 | #ifdef HAVE_SETGID
|
---|
8594 | {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
|
---|
8595 | #endif /* HAVE_SETGID */
|
---|
8596 | #ifdef HAVE_SETGROUPS
|
---|
8597 | {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
|
---|
8598 | #endif /* HAVE_SETGROUPS */
|
---|
8599 | #ifdef HAVE_GETPGID
|
---|
8600 | {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
|
---|
8601 | #endif /* HAVE_GETPGID */
|
---|
8602 | #ifdef HAVE_SETPGRP
|
---|
8603 | {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
|
---|
8604 | #endif /* HAVE_SETPGRP */
|
---|
8605 | #ifdef HAVE_WAIT
|
---|
8606 | {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
|
---|
8607 | #endif /* HAVE_WAIT */
|
---|
8608 | #ifdef HAVE_WAIT3
|
---|
8609 | {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
|
---|
8610 | #endif /* HAVE_WAIT3 */
|
---|
8611 | #ifdef HAVE_WAIT4
|
---|
8612 | {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
|
---|
8613 | #endif /* HAVE_WAIT4 */
|
---|
8614 | #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
|
---|
8615 | {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
|
---|
8616 | #endif /* HAVE_WAITPID */
|
---|
8617 | #ifdef HAVE_GETSID
|
---|
8618 | {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
|
---|
8619 | #endif /* HAVE_GETSID */
|
---|
8620 | #ifdef HAVE_SETSID
|
---|
8621 | {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
|
---|
8622 | #endif /* HAVE_SETSID */
|
---|
8623 | #ifdef HAVE_SETPGID
|
---|
8624 | {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
|
---|
8625 | #endif /* HAVE_SETPGID */
|
---|
8626 | #ifdef HAVE_TCGETPGRP
|
---|
8627 | {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
|
---|
8628 | #endif /* HAVE_TCGETPGRP */
|
---|
8629 | #ifdef HAVE_TCSETPGRP
|
---|
8630 | {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
|
---|
8631 | #endif /* HAVE_TCSETPGRP */
|
---|
8632 | {"open", posix_open, METH_VARARGS, posix_open__doc__},
|
---|
8633 | {"close", posix_close, METH_VARARGS, posix_close__doc__},
|
---|
8634 | {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
|
---|
8635 | {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
|
---|
8636 | {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
|
---|
8637 | {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
|
---|
8638 | {"read", posix_read, METH_VARARGS, posix_read__doc__},
|
---|
8639 | {"write", posix_write, METH_VARARGS, posix_write__doc__},
|
---|
8640 | {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
|
---|
8641 | {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__},
|
---|
8642 | {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
|
---|
8643 | #ifdef HAVE_PIPE
|
---|
8644 | {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
|
---|
8645 | #endif
|
---|
8646 | #ifdef HAVE_MKFIFO
|
---|
8647 | {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
|
---|
8648 | #endif
|
---|
8649 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
|
---|
8650 | {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
|
---|
8651 | #endif
|
---|
8652 | #ifdef HAVE_DEVICE_MACROS
|
---|
8653 | {"major", posix_major, METH_VARARGS, posix_major__doc__},
|
---|
8654 | {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
|
---|
8655 | {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
|
---|
8656 | #endif
|
---|
8657 | #ifdef HAVE_FTRUNCATE
|
---|
8658 | {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
|
---|
8659 | #endif
|
---|
8660 | #ifdef HAVE_PUTENV
|
---|
8661 | {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
|
---|
8662 | #endif
|
---|
8663 | #ifdef HAVE_UNSETENV
|
---|
8664 | {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
|
---|
8665 | #endif
|
---|
8666 | {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
|
---|
8667 | #ifdef HAVE_FCHDIR
|
---|
8668 | {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
|
---|
8669 | #endif
|
---|
8670 | #ifdef HAVE_FSYNC
|
---|
8671 | {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
|
---|
8672 | #endif
|
---|
8673 | #ifdef HAVE_FDATASYNC
|
---|
8674 | {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
|
---|
8675 | #endif
|
---|
8676 | #ifdef HAVE_SYS_WAIT_H
|
---|
8677 | #ifdef WCOREDUMP
|
---|
8678 | {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
|
---|
8679 | #endif /* WCOREDUMP */
|
---|
8680 | #ifdef WIFCONTINUED
|
---|
8681 | {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
|
---|
8682 | #endif /* WIFCONTINUED */
|
---|
8683 | #ifdef WIFSTOPPED
|
---|
8684 | {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
|
---|
8685 | #endif /* WIFSTOPPED */
|
---|
8686 | #ifdef WIFSIGNALED
|
---|
8687 | {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
|
---|
8688 | #endif /* WIFSIGNALED */
|
---|
8689 | #ifdef WIFEXITED
|
---|
8690 | {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
|
---|
8691 | #endif /* WIFEXITED */
|
---|
8692 | #ifdef WEXITSTATUS
|
---|
8693 | {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
|
---|
8694 | #endif /* WEXITSTATUS */
|
---|
8695 | #ifdef WTERMSIG
|
---|
8696 | {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
|
---|
8697 | #endif /* WTERMSIG */
|
---|
8698 | #ifdef WSTOPSIG
|
---|
8699 | {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
|
---|
8700 | #endif /* WSTOPSIG */
|
---|
8701 | #endif /* HAVE_SYS_WAIT_H */
|
---|
8702 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
|
---|
8703 | {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
|
---|
8704 | #endif
|
---|
8705 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
|
---|
8706 | {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
|
---|
8707 | #endif
|
---|
8708 | #ifdef HAVE_TMPFILE
|
---|
8709 | {"tmpfile", posix_tmpfile, METH_NOARGS, posix_tmpfile__doc__},
|
---|
8710 | #endif
|
---|
8711 | #ifdef HAVE_TEMPNAM
|
---|
8712 | {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__},
|
---|
8713 | #endif
|
---|
8714 | #ifdef HAVE_TMPNAM
|
---|
8715 | {"tmpnam", posix_tmpnam, METH_NOARGS, posix_tmpnam__doc__},
|
---|
8716 | #endif
|
---|
8717 | #ifdef HAVE_CONFSTR
|
---|
8718 | {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
|
---|
8719 | #endif
|
---|
8720 | #ifdef HAVE_SYSCONF
|
---|
8721 | {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
|
---|
8722 | #endif
|
---|
8723 | #ifdef HAVE_FPATHCONF
|
---|
8724 | {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
|
---|
8725 | #endif
|
---|
8726 | #ifdef HAVE_PATHCONF
|
---|
8727 | {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
|
---|
8728 | #endif
|
---|
8729 | {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
|
---|
8730 | #ifdef MS_WINDOWS
|
---|
8731 | {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
|
---|
8732 | #endif
|
---|
8733 | #ifdef HAVE_GETLOADAVG
|
---|
8734 | {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
|
---|
8735 | #endif
|
---|
8736 | #ifdef MS_WINDOWS
|
---|
8737 | {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
|
---|
8738 | #endif
|
---|
8739 | #ifdef __VMS
|
---|
8740 | {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
|
---|
8741 | #endif
|
---|
8742 | {NULL, NULL} /* Sentinel */
|
---|
8743 | };
|
---|
8744 |
|
---|
8745 |
|
---|
8746 | static int
|
---|
8747 | ins(PyObject *module, char *symbol, long value)
|
---|
8748 | {
|
---|
8749 | return PyModule_AddIntConstant(module, symbol, value);
|
---|
8750 | }
|
---|
8751 |
|
---|
8752 | #if defined(PYOS_OS2)
|
---|
8753 | /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
|
---|
8754 | static int insertvalues(PyObject *module)
|
---|
8755 | {
|
---|
8756 | APIRET rc;
|
---|
8757 | ULONG values[QSV_MAX+1];
|
---|
8758 | PyObject *v;
|
---|
8759 | char *ver, tmp[50];
|
---|
8760 |
|
---|
8761 | Py_BEGIN_ALLOW_THREADS
|
---|
8762 | rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
|
---|
8763 | Py_END_ALLOW_THREADS
|
---|
8764 |
|
---|
8765 | if (rc != NO_ERROR) {
|
---|
8766 | os2_error(rc);
|
---|
8767 | return -1;
|
---|
8768 | }
|
---|
8769 |
|
---|
8770 | if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
|
---|
8771 | if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
|
---|
8772 | if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
|
---|
8773 | if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
|
---|
8774 | if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
|
---|
8775 | if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
|
---|
8776 | if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
|
---|
8777 |
|
---|
8778 | switch (values[QSV_VERSION_MINOR]) {
|
---|
8779 | case 0: ver = "2.00"; break;
|
---|
8780 | case 10: ver = "2.10"; break;
|
---|
8781 | case 11: ver = "2.11"; break;
|
---|
8782 | case 30: ver = "3.00"; break;
|
---|
8783 | case 40: ver = "4.00"; break;
|
---|
8784 | case 50: ver = "5.00"; break;
|
---|
8785 | default:
|
---|
8786 | PyOS_snprintf(tmp, sizeof(tmp),
|
---|
8787 | "%d-%d", values[QSV_VERSION_MAJOR],
|
---|
8788 | values[QSV_VERSION_MINOR]);
|
---|
8789 | ver = &tmp[0];
|
---|
8790 | }
|
---|
8791 |
|
---|
8792 | /* Add Indicator of the Version of the Operating System */
|
---|
8793 | if (PyModule_AddStringConstant(module, "version", tmp) < 0)
|
---|
8794 | return -1;
|
---|
8795 |
|
---|
8796 | /* Add Indicator of Which Drive was Used to Boot the System */
|
---|
8797 | tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
|
---|
8798 | tmp[1] = ':';
|
---|
8799 | tmp[2] = '\0';
|
---|
8800 |
|
---|
8801 | return PyModule_AddStringConstant(module, "bootdrive", tmp);
|
---|
8802 | }
|
---|
8803 | #endif
|
---|
8804 |
|
---|
8805 | static int
|
---|
8806 | all_ins(PyObject *d)
|
---|
8807 | {
|
---|
8808 | #ifdef F_OK
|
---|
8809 | if (ins(d, "F_OK", (long)F_OK)) return -1;
|
---|
8810 | #endif
|
---|
8811 | #ifdef R_OK
|
---|
8812 | if (ins(d, "R_OK", (long)R_OK)) return -1;
|
---|
8813 | #endif
|
---|
8814 | #ifdef W_OK
|
---|
8815 | if (ins(d, "W_OK", (long)W_OK)) return -1;
|
---|
8816 | #endif
|
---|
8817 | #ifdef X_OK
|
---|
8818 | if (ins(d, "X_OK", (long)X_OK)) return -1;
|
---|
8819 | #endif
|
---|
8820 | #ifdef NGROUPS_MAX
|
---|
8821 | if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
|
---|
8822 | #endif
|
---|
8823 | #ifdef TMP_MAX
|
---|
8824 | if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
|
---|
8825 | #endif
|
---|
8826 | #ifdef WCONTINUED
|
---|
8827 | if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
|
---|
8828 | #endif
|
---|
8829 | #ifdef WNOHANG
|
---|
8830 | if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
|
---|
8831 | #endif
|
---|
8832 | #ifdef WUNTRACED
|
---|
8833 | if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
|
---|
8834 | #endif
|
---|
8835 | #ifdef O_RDONLY
|
---|
8836 | if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
|
---|
8837 | #endif
|
---|
8838 | #ifdef O_WRONLY
|
---|
8839 | if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
|
---|
8840 | #endif
|
---|
8841 | #ifdef O_RDWR
|
---|
8842 | if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
|
---|
8843 | #endif
|
---|
8844 | #ifdef O_NDELAY
|
---|
8845 | if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
|
---|
8846 | #endif
|
---|
8847 | #ifdef O_NONBLOCK
|
---|
8848 | if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
|
---|
8849 | #endif
|
---|
8850 | #ifdef O_APPEND
|
---|
8851 | if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
|
---|
8852 | #endif
|
---|
8853 | #ifdef O_DSYNC
|
---|
8854 | if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
|
---|
8855 | #endif
|
---|
8856 | #ifdef O_RSYNC
|
---|
8857 | if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
|
---|
8858 | #endif
|
---|
8859 | #ifdef O_SYNC
|
---|
8860 | if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
|
---|
8861 | #endif
|
---|
8862 | #ifdef O_NOCTTY
|
---|
8863 | if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
|
---|
8864 | #endif
|
---|
8865 | #ifdef O_CREAT
|
---|
8866 | if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
|
---|
8867 | #endif
|
---|
8868 | #ifdef O_EXCL
|
---|
8869 | if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
|
---|
8870 | #endif
|
---|
8871 | #ifdef O_TRUNC
|
---|
8872 | if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
|
---|
8873 | #endif
|
---|
8874 | #ifdef O_BINARY
|
---|
8875 | if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
|
---|
8876 | #endif
|
---|
8877 | #ifdef O_TEXT
|
---|
8878 | if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
|
---|
8879 | #endif
|
---|
8880 | #ifdef O_LARGEFILE
|
---|
8881 | if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
|
---|
8882 | #endif
|
---|
8883 | #ifdef O_SHLOCK
|
---|
8884 | if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
|
---|
8885 | #endif
|
---|
8886 | #ifdef O_EXLOCK
|
---|
8887 | if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
|
---|
8888 | #endif
|
---|
8889 |
|
---|
8890 | /* MS Windows */
|
---|
8891 | #ifdef O_NOINHERIT
|
---|
8892 | /* Don't inherit in child processes. */
|
---|
8893 | if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
|
---|
8894 | #endif
|
---|
8895 | #ifdef _O_SHORT_LIVED
|
---|
8896 | /* Optimize for short life (keep in memory). */
|
---|
8897 | /* MS forgot to define this one with a non-underscore form too. */
|
---|
8898 | if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
|
---|
8899 | #endif
|
---|
8900 | #ifdef O_TEMPORARY
|
---|
8901 | /* Automatically delete when last handle is closed. */
|
---|
8902 | if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
|
---|
8903 | #endif
|
---|
8904 | #ifdef O_RANDOM
|
---|
8905 | /* Optimize for random access. */
|
---|
8906 | if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
|
---|
8907 | #endif
|
---|
8908 | #ifdef O_SEQUENTIAL
|
---|
8909 | /* Optimize for sequential access. */
|
---|
8910 | if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
|
---|
8911 | #endif
|
---|
8912 |
|
---|
8913 | /* GNU extensions. */
|
---|
8914 | #ifdef O_ASYNC
|
---|
8915 | /* Send a SIGIO signal whenever input or output
|
---|
8916 | becomes available on file descriptor */
|
---|
8917 | if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
|
---|
8918 | #endif
|
---|
8919 | #ifdef O_DIRECT
|
---|
8920 | /* Direct disk access. */
|
---|
8921 | if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
|
---|
8922 | #endif
|
---|
8923 | #ifdef O_DIRECTORY
|
---|
8924 | /* Must be a directory. */
|
---|
8925 | if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
|
---|
8926 | #endif
|
---|
8927 | #ifdef O_NOFOLLOW
|
---|
8928 | /* Do not follow links. */
|
---|
8929 | if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
|
---|
8930 | #endif
|
---|
8931 | #ifdef O_NOATIME
|
---|
8932 | /* Do not update the access time. */
|
---|
8933 | if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
|
---|
8934 | #endif
|
---|
8935 |
|
---|
8936 | /* These come from sysexits.h */
|
---|
8937 | #ifdef EX_OK
|
---|
8938 | if (ins(d, "EX_OK", (long)EX_OK)) return -1;
|
---|
8939 | #endif /* EX_OK */
|
---|
8940 | #ifdef EX_USAGE
|
---|
8941 | if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
|
---|
8942 | #endif /* EX_USAGE */
|
---|
8943 | #ifdef EX_DATAERR
|
---|
8944 | if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
|
---|
8945 | #endif /* EX_DATAERR */
|
---|
8946 | #ifdef EX_NOINPUT
|
---|
8947 | if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
|
---|
8948 | #endif /* EX_NOINPUT */
|
---|
8949 | #ifdef EX_NOUSER
|
---|
8950 | if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
|
---|
8951 | #endif /* EX_NOUSER */
|
---|
8952 | #ifdef EX_NOHOST
|
---|
8953 | if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
|
---|
8954 | #endif /* EX_NOHOST */
|
---|
8955 | #ifdef EX_UNAVAILABLE
|
---|
8956 | if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
|
---|
8957 | #endif /* EX_UNAVAILABLE */
|
---|
8958 | #ifdef EX_SOFTWARE
|
---|
8959 | if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
|
---|
8960 | #endif /* EX_SOFTWARE */
|
---|
8961 | #ifdef EX_OSERR
|
---|
8962 | if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
|
---|
8963 | #endif /* EX_OSERR */
|
---|
8964 | #ifdef EX_OSFILE
|
---|
8965 | if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
|
---|
8966 | #endif /* EX_OSFILE */
|
---|
8967 | #ifdef EX_CANTCREAT
|
---|
8968 | if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
|
---|
8969 | #endif /* EX_CANTCREAT */
|
---|
8970 | #ifdef EX_IOERR
|
---|
8971 | if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
|
---|
8972 | #endif /* EX_IOERR */
|
---|
8973 | #ifdef EX_TEMPFAIL
|
---|
8974 | if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
|
---|
8975 | #endif /* EX_TEMPFAIL */
|
---|
8976 | #ifdef EX_PROTOCOL
|
---|
8977 | if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
|
---|
8978 | #endif /* EX_PROTOCOL */
|
---|
8979 | #ifdef EX_NOPERM
|
---|
8980 | if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
|
---|
8981 | #endif /* EX_NOPERM */
|
---|
8982 | #ifdef EX_CONFIG
|
---|
8983 | if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
|
---|
8984 | #endif /* EX_CONFIG */
|
---|
8985 | #ifdef EX_NOTFOUND
|
---|
8986 | if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
|
---|
8987 | #endif /* EX_NOTFOUND */
|
---|
8988 |
|
---|
8989 | #ifdef HAVE_SPAWNV
|
---|
8990 | #if defined(PYOS_OS2) && defined(PYCC_GCC)
|
---|
8991 | if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
|
---|
8992 | if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
|
---|
8993 | if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
|
---|
8994 | if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
|
---|
8995 | if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
|
---|
8996 | if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
|
---|
8997 | if (ins(d, "P_PM", (long)P_PM)) return -1;
|
---|
8998 | if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
|
---|
8999 | if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
|
---|
9000 | if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
|
---|
9001 | if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
|
---|
9002 | if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
|
---|
9003 | if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
|
---|
9004 | if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
|
---|
9005 | if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
|
---|
9006 | if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
|
---|
9007 | if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
|
---|
9008 | if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
|
---|
9009 | if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
|
---|
9010 | if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
|
---|
9011 | #else
|
---|
9012 | if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
|
---|
9013 | if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
|
---|
9014 | if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
|
---|
9015 | if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
|
---|
9016 | if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
|
---|
9017 | #endif
|
---|
9018 | #endif
|
---|
9019 |
|
---|
9020 | #if defined(PYOS_OS2)
|
---|
9021 | if (insertvalues(d)) return -1;
|
---|
9022 | #endif
|
---|
9023 | return 0;
|
---|
9024 | }
|
---|
9025 |
|
---|
9026 |
|
---|
9027 | #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
|
---|
9028 | #define INITFUNC initnt
|
---|
9029 | #define MODNAME "nt"
|
---|
9030 |
|
---|
9031 | #elif defined(PYOS_OS2)
|
---|
9032 | #define INITFUNC initos2
|
---|
9033 | #define MODNAME "os2"
|
---|
9034 |
|
---|
9035 | #else
|
---|
9036 | #define INITFUNC initposix
|
---|
9037 | #define MODNAME "posix"
|
---|
9038 | #endif
|
---|
9039 |
|
---|
9040 | PyMODINIT_FUNC
|
---|
9041 | INITFUNC(void)
|
---|
9042 | {
|
---|
9043 | PyObject *m, *v;
|
---|
9044 |
|
---|
9045 | m = Py_InitModule3(MODNAME,
|
---|
9046 | posix_methods,
|
---|
9047 | posix__doc__);
|
---|
9048 | if (m == NULL)
|
---|
9049 | return;
|
---|
9050 |
|
---|
9051 | /* Initialize environ dictionary */
|
---|
9052 | v = convertenviron();
|
---|
9053 | Py_XINCREF(v);
|
---|
9054 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
|
---|
9055 | return;
|
---|
9056 | Py_DECREF(v);
|
---|
9057 |
|
---|
9058 | if (all_ins(m))
|
---|
9059 | return;
|
---|
9060 |
|
---|
9061 | if (setup_confname_tables(m))
|
---|
9062 | return;
|
---|
9063 |
|
---|
9064 | Py_INCREF(PyExc_OSError);
|
---|
9065 | PyModule_AddObject(m, "error", PyExc_OSError);
|
---|
9066 |
|
---|
9067 | #ifdef HAVE_PUTENV
|
---|
9068 | if (posix_putenv_garbage == NULL)
|
---|
9069 | posix_putenv_garbage = PyDict_New();
|
---|
9070 | #endif
|
---|
9071 |
|
---|
9072 | if (!initialized) {
|
---|
9073 | stat_result_desc.name = MODNAME ".stat_result";
|
---|
9074 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
|
---|
9075 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
|
---|
9076 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
|
---|
9077 | PyStructSequence_InitType(&StatResultType, &stat_result_desc);
|
---|
9078 | structseq_new = StatResultType.tp_new;
|
---|
9079 | StatResultType.tp_new = statresult_new;
|
---|
9080 |
|
---|
9081 | statvfs_result_desc.name = MODNAME ".statvfs_result";
|
---|
9082 | PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
|
---|
9083 | #ifdef NEED_TICKS_PER_SECOND
|
---|
9084 | # if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
|
---|
9085 | ticks_per_second = sysconf(_SC_CLK_TCK);
|
---|
9086 | # elif defined(HZ)
|
---|
9087 | ticks_per_second = HZ;
|
---|
9088 | # else
|
---|
9089 | ticks_per_second = 60; /* magic fallback value; may be bogus */
|
---|
9090 | # endif
|
---|
9091 | #endif
|
---|
9092 | }
|
---|
9093 | Py_INCREF((PyObject*) &StatResultType);
|
---|
9094 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
|
---|
9095 | Py_INCREF((PyObject*) &StatVFSResultType);
|
---|
9096 | PyModule_AddObject(m, "statvfs_result",
|
---|
9097 | (PyObject*) &StatVFSResultType);
|
---|
9098 | initialized = 1;
|
---|
9099 |
|
---|
9100 | #ifdef __APPLE__
|
---|
9101 | /*
|
---|
9102 | * Step 2 of weak-linking support on Mac OS X.
|
---|
9103 | *
|
---|
9104 | * The code below removes functions that are not available on the
|
---|
9105 | * currently active platform.
|
---|
9106 | *
|
---|
9107 | * This block allow one to use a python binary that was build on
|
---|
9108 | * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
|
---|
9109 | * OSX 10.4.
|
---|
9110 | */
|
---|
9111 | #ifdef HAVE_FSTATVFS
|
---|
9112 | if (fstatvfs == NULL) {
|
---|
9113 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
|
---|
9114 | return;
|
---|
9115 | }
|
---|
9116 | }
|
---|
9117 | #endif /* HAVE_FSTATVFS */
|
---|
9118 |
|
---|
9119 | #ifdef HAVE_STATVFS
|
---|
9120 | if (statvfs == NULL) {
|
---|
9121 | if (PyObject_DelAttrString(m, "statvfs") == -1) {
|
---|
9122 | return;
|
---|
9123 | }
|
---|
9124 | }
|
---|
9125 | #endif /* HAVE_STATVFS */
|
---|
9126 |
|
---|
9127 | # ifdef HAVE_LCHOWN
|
---|
9128 | if (lchown == NULL) {
|
---|
9129 | if (PyObject_DelAttrString(m, "lchown") == -1) {
|
---|
9130 | return;
|
---|
9131 | }
|
---|
9132 | }
|
---|
9133 | #endif /* HAVE_LCHOWN */
|
---|
9134 |
|
---|
9135 |
|
---|
9136 | #endif /* __APPLE__ */
|
---|
9137 |
|
---|
9138 | }
|
---|
9139 |
|
---|
9140 | #ifdef __cplusplus
|
---|
9141 | }
|
---|
9142 | #endif
|
---|
9143 |
|
---|
9144 |
|
---|