2 OS-specific module implementation for EDK II and UEFI.
3 Derived from posixmodule.c in Python 2.7.2.
5 Copyright (c) 2011 - 2012, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials are licensed and made available under
7 the terms and conditions of the BSD License that accompanies this distribution.
8 The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14 #define PY_SSIZE_T_CLEAN
17 #include "structseq.h"
22 #include <sys/syslimits.h>
28 PyDoc_STRVAR(edk2__doc__
,
29 "This module provides access to UEFI firmware functionality that is\n\
30 standardized by the C Standard and the POSIX standard (a thinly\n\
31 disguised Unix interface). Refer to the library manual and\n\
32 corresponding UEFI Specification entries for more information on calls.");
34 #ifndef Py_USING_UNICODE
35 /* This is used in signatures of functions. */
36 #define Py_UNICODE void
39 #ifdef HAVE_SYS_TYPES_H
40 #include <sys/types.h>
41 #endif /* HAVE_SYS_TYPES_H */
43 #ifdef HAVE_SYS_STAT_H
45 #endif /* HAVE_SYS_STAT_H */
47 #ifdef HAVE_SYS_WAIT_H
48 #include <sys/wait.h> /* For WNOHANG */
57 #endif /* HAVE_FCNTL_H */
63 #ifdef HAVE_SYSEXITS_H
65 #endif /* HAVE_SYSEXITS_H */
67 #ifdef HAVE_SYS_LOADAVG_H
68 #include <sys/loadavg.h>
73 #endif /* HAVE_UTIME_H */
75 #ifdef HAVE_SYS_UTIME_H
76 #include <sys/utime.h>
77 #define HAVE_UTIME_H /* pretend we do for the rest of this file */
78 #endif /* HAVE_SYS_UTIME_H */
80 #ifdef HAVE_SYS_TIMES_H
81 #include <sys/times.h>
82 #endif /* HAVE_SYS_TIMES_H */
84 #ifdef HAVE_SYS_PARAM_H
85 #include <sys/param.h>
86 #endif /* HAVE_SYS_PARAM_H */
88 #ifdef HAVE_SYS_UTSNAME_H
89 #include <sys/utsname.h>
90 #endif /* HAVE_SYS_UTSNAME_H */
94 #define NAMLEN(dirent) wcslen((dirent)->FileName)
97 #define NAMLEN(dirent) (dirent)->d_namlen
98 #ifdef HAVE_SYS_NDIR_H
101 #ifdef HAVE_SYS_DIR_H
110 #if defined(PATH_MAX) && PATH_MAX > 1024
111 #define MAXPATHLEN PATH_MAX
113 #define MAXPATHLEN 1024
115 #endif /* MAXPATHLEN */
117 #define WAIT_TYPE int
118 #define WAIT_STATUS_INT(s) (s)
120 /* Issue #1983: pid_t can be longer than a C long on some systems */
121 #if !defined(SIZEOF_PID_T) || SIZEOF_PID_T == SIZEOF_INT
122 #define PARSE_PID "i"
123 #define PyLong_FromPid PyInt_FromLong
124 #define PyLong_AsPid PyInt_AsLong
125 #elif SIZEOF_PID_T == SIZEOF_LONG
126 #define PARSE_PID "l"
127 #define PyLong_FromPid PyInt_FromLong
128 #define PyLong_AsPid PyInt_AsLong
129 #elif defined(SIZEOF_LONG_LONG) && SIZEOF_PID_T == SIZEOF_LONG_LONG
130 #define PARSE_PID "L"
131 #define PyLong_FromPid PyLong_FromLongLong
132 #define PyLong_AsPid PyInt_AsLongLong
134 #error "sizeof(pid_t) is neither sizeof(int), sizeof(long) or sizeof(long long)"
135 #endif /* SIZEOF_PID_T */
137 /* Don't use the "_r" form if we don't need it (also, won't have a
138 prototype for it, at least on Solaris -- maybe others as well?). */
139 #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
140 #define USE_CTERMID_R
143 #if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD)
147 /* choose the appropriate stat and fstat functions and return structs */
153 #define STRUCT_STAT struct stat
155 /* dummy version. _PyVerify_fd() is already defined in fileobject.h */
156 #define _PyVerify_fd_dup2(A, B) (1)
158 #ifndef UEFI_C_SOURCE
159 /* Return a dictionary corresponding to the POSIX environment table */
160 extern char **environ
;
172 /* This part ignores errors */
173 for (e
= environ
; *e
!= NULL
; e
++) {
176 char *p
= strchr(*e
, '=');
179 k
= PyString_FromStringAndSize(*e
, (int)(p
-*e
));
184 v
= PyString_FromString(p
+1);
190 if (PyDict_GetItem(d
, k
) == NULL
) {
191 if (PyDict_SetItem(d
, k
, v
) != 0)
199 #endif /* UEFI_C_SOURCE */
201 /* Set a POSIX-specific error from errno, and return NULL */
206 return PyErr_SetFromErrno(PyExc_OSError
);
209 posix_error_with_filename(char* name
)
211 return PyErr_SetFromErrnoWithFilename(PyExc_OSError
, name
);
216 posix_error_with_allocated_filename(char* name
)
218 PyObject
*rc
= PyErr_SetFromErrnoWithFilename(PyExc_OSError
, name
);
223 /* POSIX generic methods */
225 #ifndef UEFI_C_SOURCE
227 posix_fildes(PyObject
*fdobj
, int (*func
)(int))
231 fd
= PyObject_AsFileDescriptor(fdobj
);
234 if (!_PyVerify_fd(fd
))
235 return posix_error();
236 Py_BEGIN_ALLOW_THREADS
240 return posix_error();
244 #endif /* UEFI_C_SOURCE */
247 posix_1str(PyObject
*args
, char *format
, int (*func
)(const char*))
251 if (!PyArg_ParseTuple(args
, format
,
252 Py_FileSystemDefaultEncoding
, &path1
))
254 Py_BEGIN_ALLOW_THREADS
255 res
= (*func
)(path1
);
258 return posix_error_with_allocated_filename(path1
);
265 posix_2str(PyObject
*args
,
267 int (*func
)(const char *, const char *))
269 char *path1
= NULL
, *path2
= NULL
;
271 if (!PyArg_ParseTuple(args
, format
,
272 Py_FileSystemDefaultEncoding
, &path1
,
273 Py_FileSystemDefaultEncoding
, &path2
))
275 Py_BEGIN_ALLOW_THREADS
276 res
= (*func
)(path1
, path2
);
281 /* XXX how to report both path1 and path2??? */
282 return posix_error();
287 PyDoc_STRVAR(stat_result__doc__
,
288 "stat_result: Result from stat or lstat.\n\n\
289 This object may be accessed either as a tuple of\n\
290 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
291 or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
293 Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
294 or st_flags, they are available as attributes only.\n\
296 See os.stat for more information.");
298 static PyStructSequence_Field stat_result_fields
[] = {
299 {"st_mode", "protection bits"},
300 //{"st_ino", "inode"},
301 //{"st_dev", "device"},
302 //{"st_nlink", "number of hard links"},
303 //{"st_uid", "user ID of owner"},
304 //{"st_gid", "group ID of owner"},
305 {"st_size", "total size, in bytes"},
306 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
307 {NULL
, "integer time of last access"},
308 {NULL
, "integer time of last modification"},
309 {NULL
, "integer time of last change"},
310 {"st_atime", "time of last access"},
311 {"st_mtime", "time of last modification"},
312 {"st_ctime", "time of last change"},
313 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
314 {"st_blksize", "blocksize for filesystem I/O"},
316 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
317 {"st_blocks", "number of blocks allocated"},
319 #ifdef HAVE_STRUCT_STAT_ST_RDEV
320 {"st_rdev", "device type (if inode device)"},
322 #ifdef HAVE_STRUCT_STAT_ST_FLAGS
323 {"st_flags", "user defined flags for file"},
325 #ifdef HAVE_STRUCT_STAT_ST_GEN
326 {"st_gen", "generation number"},
328 #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
329 {"st_birthtime", "time of creation"},
334 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
335 #define ST_BLKSIZE_IDX 8
337 #define ST_BLKSIZE_IDX 12
340 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
341 #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
343 #define ST_BLOCKS_IDX ST_BLKSIZE_IDX
346 #ifdef HAVE_STRUCT_STAT_ST_RDEV
347 #define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
349 #define ST_RDEV_IDX ST_BLOCKS_IDX
352 #ifdef HAVE_STRUCT_STAT_ST_FLAGS
353 #define ST_FLAGS_IDX (ST_RDEV_IDX+1)
355 #define ST_FLAGS_IDX ST_RDEV_IDX
358 #ifdef HAVE_STRUCT_STAT_ST_GEN
359 #define ST_GEN_IDX (ST_FLAGS_IDX+1)
361 #define ST_GEN_IDX ST_FLAGS_IDX
364 #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
365 #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
367 #define ST_BIRTHTIME_IDX ST_GEN_IDX
370 static PyStructSequence_Desc stat_result_desc
= {
371 "stat_result", /* name */
372 stat_result__doc__
, /* doc */
377 #ifndef UEFI_C_SOURCE /* Not in UEFI */
378 PyDoc_STRVAR(statvfs_result__doc__
,
379 "statvfs_result: Result from statvfs or fstatvfs.\n\n\
380 This object may be accessed either as a tuple of\n\
381 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
382 or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
384 See os.statvfs for more information.");
386 static PyStructSequence_Field statvfs_result_fields
[] = {
400 static PyStructSequence_Desc statvfs_result_desc
= {
401 "statvfs_result", /* name */
402 statvfs_result__doc__
, /* doc */
403 statvfs_result_fields
,
407 static PyTypeObject StatVFSResultType
;
410 static int initialized
;
411 static PyTypeObject StatResultType
;
412 static newfunc structseq_new
;
415 statresult_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
417 PyStructSequence
*result
;
420 result
= (PyStructSequence
*)structseq_new(type
, args
, kwds
);
423 /* If we have been initialized from a tuple,
424 st_?time might be set to None. Initialize it
425 from the int slots. */
426 for (i
= 7; i
<= 9; i
++) {
427 if (result
->ob_item
[i
+3] == Py_None
) {
429 Py_INCREF(result
->ob_item
[i
]);
430 result
->ob_item
[i
+3] = result
->ob_item
[i
];
433 return (PyObject
*)result
;
438 /* If true, st_?time is float. */
439 #if defined(UEFI_C_SOURCE)
440 static int _stat_float_times
= 0;
442 static int _stat_float_times
= 1;
444 PyDoc_STRVAR(stat_float_times__doc__
,
445 "stat_float_times([newval]) -> oldval\n\n\
446 Determine whether os.[lf]stat represents time stamps as float objects.\n\
447 If newval is True, future calls to stat() return floats, if it is False,\n\
448 future calls return ints. \n\
449 If newval is omitted, return the current setting.\n");
452 stat_float_times(PyObject
* self
, PyObject
*args
)
456 if (!PyArg_ParseTuple(args
, "|i:stat_float_times", &newval
))
459 /* Return old value */
460 return PyBool_FromLong(_stat_float_times
);
461 _stat_float_times
= newval
;
465 #endif /* UEFI_C_SOURCE */
468 fill_time(PyObject
*v
, int index
, time_t sec
, unsigned long nsec
)
470 PyObject
*fval
,*ival
;
471 #if SIZEOF_TIME_T > SIZEOF_LONG
472 ival
= PyLong_FromLongLong((PY_LONG_LONG
)sec
);
474 ival
= PyInt_FromLong((long)sec
);
478 if (_stat_float_times
) {
479 fval
= PyFloat_FromDouble(sec
+ 1e-9*nsec
);
484 PyStructSequence_SET_ITEM(v
, index
, ival
);
485 PyStructSequence_SET_ITEM(v
, index
+3, fval
);
488 /* pack a system stat C structure into the Python stat tuple
489 (used by posix_stat() and posix_fstat()) */
491 _pystat_fromstructstat(STRUCT_STAT
*st
)
493 unsigned long ansec
, mnsec
, cnsec
;
494 PyObject
*v
= PyStructSequence_New(&StatResultType
);
498 PyStructSequence_SET_ITEM(v
, 0, PyInt_FromLong((long)st
->st_mode
));
499 PyStructSequence_SET_ITEM(v
, 1,
500 PyLong_FromLongLong((PY_LONG_LONG
)st
->st_size
));
502 ansec
= mnsec
= cnsec
= 0;
503 /* The index used by fill_time is the index of the integer time.
504 fill_time will add 3 to the index to get the floating time index.
506 fill_time(v
, 2, st
->st_atime
, ansec
);
507 fill_time(v
, 3, st
->st_mtime
, mnsec
);
508 fill_time(v
, 4, st
->st_mtime
, cnsec
);
510 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
511 PyStructSequence_SET_ITEM(v
, ST_BLKSIZE_IDX
,
512 PyInt_FromLong((long)st
->st_blksize
));
514 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
515 PyStructSequence_SET_ITEM(v
, ST_BLOCKS_IDX
,
516 PyInt_FromLong((long)st
->st_blocks
));
518 #ifdef HAVE_STRUCT_STAT_ST_RDEV
519 PyStructSequence_SET_ITEM(v
, ST_RDEV_IDX
,
520 PyInt_FromLong((long)st
->st_rdev
));
522 #ifdef HAVE_STRUCT_STAT_ST_GEN
523 PyStructSequence_SET_ITEM(v
, ST_GEN_IDX
,
524 PyInt_FromLong((long)st
->st_gen
));
526 #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
529 unsigned long bsec
,bnsec
;
530 bsec
= (long)st
->st_birthtime
;
531 #ifdef HAVE_STAT_TV_NSEC2
532 bnsec
= st
->st_birthtimespec
.tv_nsec
;
536 if (_stat_float_times
) {
537 val
= PyFloat_FromDouble(bsec
+ 1e-9*bnsec
);
539 val
= PyInt_FromLong((long)bsec
);
541 PyStructSequence_SET_ITEM(v
, ST_BIRTHTIME_IDX
,
545 #ifdef HAVE_STRUCT_STAT_ST_FLAGS
546 PyStructSequence_SET_ITEM(v
, ST_FLAGS_IDX
,
547 PyInt_FromLong((long)st
->st_flags
));
550 if (PyErr_Occurred()) {
559 posix_do_stat(PyObject
*self
, PyObject
*args
,
561 int (*statfunc
)(const char *, STRUCT_STAT
*),
563 int (*wstatfunc
)(const Py_UNICODE
*, STRUCT_STAT
*))
566 char *path
= NULL
; /* pass this to stat; do not free() it */
567 char *pathfree
= NULL
; /* this memory must be free'd */
571 if (!PyArg_ParseTuple(args
, format
,
572 Py_FileSystemDefaultEncoding
, &path
))
576 Py_BEGIN_ALLOW_THREADS
577 res
= (*statfunc
)(path
, &st
);
581 result
= posix_error_with_filename(pathfree
);
584 result
= _pystat_fromstructstat(&st
);
586 PyMem_Free(pathfree
);
592 PyDoc_STRVAR(posix_access__doc__
,
593 "access(path, mode) -> True if granted, False otherwise\n\n\
594 Use the real uid/gid to test for access to a path. Note that most\n\
595 operations will use the effective uid/gid, therefore this routine can\n\
596 be used in a suid/sgid environment to test if the invoking user has the\n\
597 specified access to the path. The mode argument can be F_OK to test\n\
598 existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
601 posix_access(PyObject
*self
, PyObject
*args
)
607 if (!PyArg_ParseTuple(args
, "eti:access",
608 Py_FileSystemDefaultEncoding
, &path
, &mode
))
610 Py_BEGIN_ALLOW_THREADS
611 res
= access(path
, mode
);
614 return PyBool_FromLong(res
== 0);
630 PyDoc_STRVAR(posix_chdir__doc__
,
632 Change the current working directory to the specified path.");
635 posix_chdir(PyObject
*self
, PyObject
*args
)
637 return posix_1str(args
, "et:chdir", chdir
);
640 PyDoc_STRVAR(posix_chmod__doc__
,
641 "chmod(path, mode)\n\n\
642 Change the access permissions of a file.");
645 posix_chmod(PyObject
*self
, PyObject
*args
)
650 if (!PyArg_ParseTuple(args
, "eti:chmod", Py_FileSystemDefaultEncoding
,
653 Py_BEGIN_ALLOW_THREADS
654 res
= chmod(path
, i
);
657 return posix_error_with_allocated_filename(path
);
664 PyDoc_STRVAR(posix_fchmod__doc__
,
665 "fchmod(fd, mode)\n\n\
666 Change the access permissions of the file given by file\n\
670 posix_fchmod(PyObject
*self
, PyObject
*args
)
673 if (!PyArg_ParseTuple(args
, "ii:fchmod", &fd
, &mode
))
675 Py_BEGIN_ALLOW_THREADS
676 res
= fchmod(fd
, mode
);
679 return posix_error();
682 #endif /* HAVE_FCHMOD */
685 PyDoc_STRVAR(posix_lchmod__doc__
,
686 "lchmod(path, mode)\n\n\
687 Change the access permissions of a file. If path is a symlink, this\n\
688 affects the link itself rather than the target.");
691 posix_lchmod(PyObject
*self
, PyObject
*args
)
696 if (!PyArg_ParseTuple(args
, "eti:lchmod", Py_FileSystemDefaultEncoding
,
699 Py_BEGIN_ALLOW_THREADS
700 res
= lchmod(path
, i
);
703 return posix_error_with_allocated_filename(path
);
707 #endif /* HAVE_LCHMOD */
711 PyDoc_STRVAR(posix_chflags__doc__
,
712 "chflags(path, flags)\n\n\
716 posix_chflags(PyObject
*self
, PyObject
*args
)
721 if (!PyArg_ParseTuple(args
, "etk:chflags",
722 Py_FileSystemDefaultEncoding
, &path
, &flags
))
724 Py_BEGIN_ALLOW_THREADS
725 res
= chflags(path
, flags
);
728 return posix_error_with_allocated_filename(path
);
733 #endif /* HAVE_CHFLAGS */
736 PyDoc_STRVAR(posix_lchflags__doc__
,
737 "lchflags(path, flags)\n\n\
739 This function will not follow symbolic links.");
742 posix_lchflags(PyObject
*self
, PyObject
*args
)
747 if (!PyArg_ParseTuple(args
, "etk:lchflags",
748 Py_FileSystemDefaultEncoding
, &path
, &flags
))
750 Py_BEGIN_ALLOW_THREADS
751 res
= lchflags(path
, flags
);
754 return posix_error_with_allocated_filename(path
);
759 #endif /* HAVE_LCHFLAGS */
762 PyDoc_STRVAR(posix_chroot__doc__
,
764 Change root directory to path.");
767 posix_chroot(PyObject
*self
, PyObject
*args
)
769 return posix_1str(args
, "et:chroot", chroot
);
774 PyDoc_STRVAR(posix_fsync__doc__
,
776 force write of file with filedescriptor to disk.");
779 posix_fsync(PyObject
*self
, PyObject
*fdobj
)
781 return posix_fildes(fdobj
, fsync
);
783 #endif /* HAVE_FSYNC */
785 #ifdef HAVE_FDATASYNC
788 extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
791 PyDoc_STRVAR(posix_fdatasync__doc__
,
792 "fdatasync(fildes)\n\n\
793 force write of file with filedescriptor to disk.\n\
794 does not force update of metadata.");
797 posix_fdatasync(PyObject
*self
, PyObject
*fdobj
)
799 return posix_fildes(fdobj
, fdatasync
);
801 #endif /* HAVE_FDATASYNC */
805 PyDoc_STRVAR(posix_chown__doc__
,
806 "chown(path, uid, gid)\n\n\
807 Change the owner and group id of path to the numeric uid and gid.");
810 posix_chown(PyObject
*self
, PyObject
*args
)
815 if (!PyArg_ParseTuple(args
, "etll:chown",
816 Py_FileSystemDefaultEncoding
, &path
,
819 Py_BEGIN_ALLOW_THREADS
820 res
= chown(path
, (uid_t
) uid
, (gid_t
) gid
);
823 return posix_error_with_allocated_filename(path
);
828 #endif /* HAVE_CHOWN */
831 PyDoc_STRVAR(posix_fchown__doc__
,
832 "fchown(fd, uid, gid)\n\n\
833 Change the owner and group id of the file given by file descriptor\n\
834 fd to the numeric uid and gid.");
837 posix_fchown(PyObject
*self
, PyObject
*args
)
842 if (!PyArg_ParseTuple(args
, "ill:chown", &fd
, &uid
, &gid
))
844 Py_BEGIN_ALLOW_THREADS
845 res
= fchown(fd
, (uid_t
) uid
, (gid_t
) gid
);
848 return posix_error();
851 #endif /* HAVE_FCHOWN */
854 PyDoc_STRVAR(posix_lchown__doc__
,
855 "lchown(path, uid, gid)\n\n\
856 Change the owner and group id of path to the numeric uid and gid.\n\
857 This function will not follow symbolic links.");
860 posix_lchown(PyObject
*self
, PyObject
*args
)
865 if (!PyArg_ParseTuple(args
, "etll:lchown",
866 Py_FileSystemDefaultEncoding
, &path
,
869 Py_BEGIN_ALLOW_THREADS
870 res
= lchown(path
, (uid_t
) uid
, (gid_t
) gid
);
873 return posix_error_with_allocated_filename(path
);
878 #endif /* HAVE_LCHOWN */
882 PyDoc_STRVAR(posix_getcwd__doc__
,
883 "getcwd() -> path\n\n\
884 Return a string representing the current working directory.");
887 posix_getcwd(PyObject
*self
, PyObject
*noargs
)
889 int bufsize_incr
= 1024;
893 PyObject
*dynamic_return
;
895 Py_BEGIN_ALLOW_THREADS
897 bufsize
= bufsize
+ bufsize_incr
;
898 tmpbuf
= malloc(bufsize
);
899 if (tmpbuf
== NULL
) {
902 res
= getcwd(tmpbuf
, bufsize
);
906 } while ((res
== NULL
) && (errno
== ERANGE
));
910 return posix_error();
912 dynamic_return
= PyString_FromString(tmpbuf
);
915 return dynamic_return
;
918 #ifdef Py_USING_UNICODE
919 PyDoc_STRVAR(posix_getcwdu__doc__
,
920 "getcwdu() -> path\n\n\
921 Return a unicode string representing the current working directory.");
924 posix_getcwdu(PyObject
*self
, PyObject
*noargs
)
929 Py_BEGIN_ALLOW_THREADS
930 res
= getcwd(buf
, sizeof buf
);
933 return posix_error();
934 return PyUnicode_Decode(buf
, strlen(buf
), Py_FileSystemDefaultEncoding
,"strict");
936 #endif /* Py_USING_UNICODE */
937 #endif /* HAVE_GETCWD */
940 PyDoc_STRVAR(posix_listdir__doc__
,
941 "listdir(path) -> list_of_strings\n\n\
942 Return a list containing the names of the entries in the directory.\n\
944 path: path of directory to list\n\
946 The list is in arbitrary order. It does not include the special\n\
947 entries '.' and '..' even if they are present in the directory.");
950 posix_listdir(PyObject
*self
, PyObject
*args
)
952 /* XXX Should redo this putting the (now four) versions of opendir
953 in separate files instead of having them all here... */
960 int arg_is_unicode
= 1;
963 if (!PyArg_ParseTuple(args
, "U:listdir", &v
)) {
967 if (!PyArg_ParseTuple(args
, "et:listdir", Py_FileSystemDefaultEncoding
, &name
))
969 Py_BEGIN_ALLOW_THREADS
970 dirp
= opendir(name
);
973 return posix_error_with_allocated_filename(name
);
975 if ((d
= PyList_New(0)) == NULL
) {
976 Py_BEGIN_ALLOW_THREADS
982 if((MBname
= malloc(NAME_MAX
)) == NULL
) {
983 Py_BEGIN_ALLOW_THREADS
992 Py_BEGIN_ALLOW_THREADS
996 if ((errno
== 0) || (errno
== EISDIR
)) {
999 Py_BEGIN_ALLOW_THREADS
1001 Py_END_ALLOW_THREADS
1003 return posix_error_with_allocated_filename(name
);
1006 if (ep
->FileName
[0] == L
'.' &&
1008 (ep
->FileName
[1] == L
'.' && NAMLEN(ep
) == 2)))
1010 if(wcstombs(MBname
, ep
->FileName
, NAME_MAX
) == -1) {
1012 Py_BEGIN_ALLOW_THREADS
1014 Py_END_ALLOW_THREADS
1019 v
= PyString_FromStringAndSize(MBname
, strlen(MBname
));
1025 #ifdef Py_USING_UNICODE
1026 if (arg_is_unicode
) {
1029 w
= PyUnicode_FromEncodedObject(v
,
1030 Py_FileSystemDefaultEncoding
,
1037 /* fall back to the original byte string, as
1038 discussed in patch #683592 */
1043 if (PyList_Append(d
, v
) != 0) {
1051 Py_BEGIN_ALLOW_THREADS
1053 Py_END_ALLOW_THREADS
1055 if(MBname
!= NULL
) {
1061 } /* end of posix_listdir */
1064 /* A helper function for abspath on win32 */
1066 posix__getfullpathname(PyObject
*self
, PyObject
*args
)
1068 /* assume encoded strings won't more than double no of chars */
1069 char inbuf
[MAX_PATH
*2];
1070 char *inbufp
= inbuf
;
1071 Py_ssize_t insize
= sizeof(inbuf
);
1072 char outbuf
[MAX_PATH
*2];
1075 PyUnicodeObject
*po
;
1076 if (PyArg_ParseTuple(args
, "U|:_getfullpathname", &po
)) {
1077 Py_UNICODE
*wpath
= PyUnicode_AS_UNICODE(po
);
1078 Py_UNICODE woutbuf
[MAX_PATH
*2], *woutbufp
= woutbuf
;
1082 result
= GetFullPathNameW(wpath
,
1083 sizeof(woutbuf
)/sizeof(woutbuf
[0]),
1085 if (result
> sizeof(woutbuf
)/sizeof(woutbuf
[0])) {
1086 woutbufp
= malloc(result
* sizeof(Py_UNICODE
));
1088 return PyErr_NoMemory();
1089 result
= GetFullPathNameW(wpath
, result
, woutbufp
, &wtemp
);
1092 v
= PyUnicode_FromUnicode(woutbufp
, wcslen(woutbufp
));
1094 v
= win32_error_unicode("GetFullPathNameW", wpath
);
1095 if (woutbufp
!= woutbuf
)
1099 /* Drop the argument parsing error as narrow strings
1103 if (!PyArg_ParseTuple (args
, "et#:_getfullpathname",
1104 Py_FileSystemDefaultEncoding
, &inbufp
,
1107 if (!GetFullPathName(inbuf
, sizeof(outbuf
)/sizeof(outbuf
[0]),
1109 return win32_error("GetFullPathName", inbuf
);
1110 if (PyUnicode_Check(PyTuple_GetItem(args
, 0))) {
1111 return PyUnicode_Decode(outbuf
, strlen(outbuf
),
1112 Py_FileSystemDefaultEncoding
, NULL
);
1114 return PyString_FromString(outbuf
);
1115 } /* end of posix__getfullpathname */
1116 #endif /* MS_WINDOWS */
1118 PyDoc_STRVAR(posix_mkdir__doc__
,
1119 "mkdir(path [, mode=0777])\n\n\
1120 Create a directory.");
1123 posix_mkdir(PyObject
*self
, PyObject
*args
)
1129 if (!PyArg_ParseTuple(args
, "et|i:mkdir",
1130 Py_FileSystemDefaultEncoding
, &path
, &mode
))
1132 Py_BEGIN_ALLOW_THREADS
1133 res
= mkdir(path
, mode
);
1134 Py_END_ALLOW_THREADS
1136 return posix_error_with_allocated_filename(path
);
1143 /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
1144 #if defined(HAVE_SYS_RESOURCE_H)
1145 #include <sys/resource.h>
1150 PyDoc_STRVAR(posix_nice__doc__
,
1151 "nice(inc) -> new_priority\n\n\
1152 Decrease the priority of process by inc and return the new priority.");
1155 posix_nice(PyObject
*self
, PyObject
*args
)
1157 int increment
, value
;
1159 if (!PyArg_ParseTuple(args
, "i:nice", &increment
))
1162 /* There are two flavours of 'nice': one that returns the new
1163 priority (as required by almost all standards out there) and the
1164 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
1165 the use of getpriority() to get the new priority.
1167 If we are of the nice family that returns the new priority, we
1168 need to clear errno before the call, and check if errno is filled
1169 before calling posix_error() on a returnvalue of -1, because the
1170 -1 may be the actual new priority! */
1173 value
= nice(increment
);
1174 #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
1176 value
= getpriority(PRIO_PROCESS
, 0);
1178 if (value
== -1 && errno
!= 0)
1179 /* either nice() or getpriority() returned an error */
1180 return posix_error();
1181 return PyInt_FromLong((long) value
);
1183 #endif /* HAVE_NICE */
1185 PyDoc_STRVAR(posix_rename__doc__
,
1186 "rename(old, new)\n\n\
1187 Rename a file or directory.");
1190 posix_rename(PyObject
*self
, PyObject
*args
)
1192 return posix_2str(args
, "etet:rename", rename
);
1196 PyDoc_STRVAR(posix_rmdir__doc__
,
1198 Remove a directory.");
1201 posix_rmdir(PyObject
*self
, PyObject
*args
)
1203 return posix_1str(args
, "et:rmdir", rmdir
);
1207 PyDoc_STRVAR(posix_stat__doc__
,
1208 "stat(path) -> stat result\n\n\
1209 Perform a stat system call on the given path.");
1212 posix_stat(PyObject
*self
, PyObject
*args
)
1214 return posix_do_stat(self
, args
, "et:stat", STAT
, NULL
, NULL
);
1219 PyDoc_STRVAR(posix_system__doc__
,
1220 "system(command) -> exit_status\n\n\
1221 Execute the command (a string) in a subshell.");
1224 posix_system(PyObject
*self
, PyObject
*args
)
1228 if (!PyArg_ParseTuple(args
, "s:system", &command
))
1230 Py_BEGIN_ALLOW_THREADS
1231 sts
= system(command
);
1232 Py_END_ALLOW_THREADS
1233 return PyInt_FromLong(sts
);
1238 PyDoc_STRVAR(posix_umask__doc__
,
1239 "umask(new_mask) -> old_mask\n\n\
1240 Set the current numeric umask and return the previous umask.");
1243 posix_umask(PyObject
*self
, PyObject
*args
)
1246 if (!PyArg_ParseTuple(args
, "i:umask", &i
))
1250 return posix_error();
1251 return PyInt_FromLong((long)i
);
1255 PyDoc_STRVAR(posix_unlink__doc__
,
1257 Remove a file (same as remove(path)).");
1259 PyDoc_STRVAR(posix_remove__doc__
,
1261 Remove a file (same as unlink(path)).");
1264 posix_unlink(PyObject
*self
, PyObject
*args
)
1266 return posix_1str(args
, "et:remove", unlink
);
1271 extract_time(PyObject
*t
, time_t* sec
, long* usec
)
1274 if (PyFloat_Check(t
)) {
1275 double tval
= PyFloat_AsDouble(t
);
1276 PyObject
*intobj
= PyNumber_Long(t
);
1279 #if SIZEOF_TIME_T > SIZEOF_LONG
1280 intval
= PyInt_AsUnsignedLongLongMask(intobj
);
1282 intval
= PyInt_AsLong(intobj
);
1285 if (intval
== -1 && PyErr_Occurred())
1288 *usec
= (long)((tval
- intval
) * 1e6
); /* can't exceed 1000000 */
1290 /* If rounding gave us a negative number,
1295 #if SIZEOF_TIME_T > SIZEOF_LONG
1296 intval
= PyInt_AsUnsignedLongLongMask(t
);
1298 intval
= PyInt_AsLong(t
);
1300 if (intval
== -1 && PyErr_Occurred())
1307 PyDoc_STRVAR(posix_utime__doc__
,
1308 "utime(path, (atime, mtime))\n\
1309 utime(path, None)\n\n\
1310 Set the access and modified time of the file to the given values. If the\n\
1311 second form is used, set the access and modified times to the current time.");
1314 posix_utime(PyObject
*self
, PyObject
*args
)
1317 time_t atime
, mtime
;
1322 #if defined(HAVE_UTIMES)
1323 struct timeval buf
[2];
1324 #define ATIME buf[0].tv_sec
1325 #define MTIME buf[1].tv_sec
1326 #elif defined(HAVE_UTIME_H)
1327 /* XXX should define struct utimbuf instead, above */
1329 #define ATIME buf.actime
1330 #define MTIME buf.modtime
1331 #define UTIME_ARG &buf
1332 #else /* HAVE_UTIMES */
1334 #define ATIME buf[0]
1335 #define MTIME buf[1]
1336 #define UTIME_ARG buf
1337 #endif /* HAVE_UTIMES */
1340 if (!PyArg_ParseTuple(args
, "etO:utime",
1341 Py_FileSystemDefaultEncoding
, &path
, &arg
))
1343 if (arg
== Py_None
) {
1344 /* optional time values not given */
1345 Py_BEGIN_ALLOW_THREADS
1346 res
= utime(path
, NULL
);
1347 Py_END_ALLOW_THREADS
1349 else if (!PyTuple_Check(arg
) || PyTuple_Size(arg
) != 2) {
1350 PyErr_SetString(PyExc_TypeError
,
1351 "utime() arg 2 must be a tuple (atime, mtime)");
1356 if (extract_time(PyTuple_GET_ITEM(arg
, 0),
1357 &atime
, &ausec
) == -1) {
1361 if (extract_time(PyTuple_GET_ITEM(arg
, 1),
1362 &mtime
, &musec
) == -1) {
1369 buf
[0].tv_usec
= ausec
;
1370 buf
[1].tv_usec
= musec
;
1371 Py_BEGIN_ALLOW_THREADS
1372 res
= utimes(path
, buf
);
1373 Py_END_ALLOW_THREADS
1375 Py_BEGIN_ALLOW_THREADS
1376 res
= utime(path
, UTIME_ARG
);
1377 Py_END_ALLOW_THREADS
1378 #endif /* HAVE_UTIMES */
1381 return posix_error_with_allocated_filename(path
);
1392 /* Process operations */
1394 PyDoc_STRVAR(posix__exit__doc__
,
1396 Exit to the system with specified status, without normal exit processing.");
1399 posix__exit(PyObject
*self
, PyObject
*args
)
1402 if (!PyArg_ParseTuple(args
, "i:_exit", &sts
))
1405 return NULL
; /* Make gcc -Wall happy */
1408 #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
1410 free_string_array(char **array
, Py_ssize_t count
)
1413 for (i
= 0; i
< count
; i
++)
1414 PyMem_Free(array
[i
]);
1421 PyDoc_STRVAR(posix_execv__doc__
,
1422 "execv(path, args)\n\n\
1423 Execute an executable path with arguments, replacing current process.\n\
1425 path: path of executable file\n\
1426 args: tuple or list of strings");
1429 posix_execv(PyObject
*self
, PyObject
*args
)
1435 PyObject
*(*getitem
)(PyObject
*, Py_ssize_t
);
1437 /* execv has two arguments: (path, argv), where
1438 argv is a list or tuple of strings. */
1440 if (!PyArg_ParseTuple(args
, "etO:execv",
1441 Py_FileSystemDefaultEncoding
,
1444 if (PyList_Check(argv
)) {
1445 argc
= PyList_Size(argv
);
1446 getitem
= PyList_GetItem
;
1448 else if (PyTuple_Check(argv
)) {
1449 argc
= PyTuple_Size(argv
);
1450 getitem
= PyTuple_GetItem
;
1453 PyErr_SetString(PyExc_TypeError
, "execv() arg 2 must be a tuple or list");
1458 PyErr_SetString(PyExc_ValueError
, "execv() arg 2 must not be empty");
1463 argvlist
= PyMem_NEW(char *, argc
+1);
1464 if (argvlist
== NULL
) {
1466 return PyErr_NoMemory();
1468 for (i
= 0; i
< argc
; i
++) {
1469 if (!PyArg_Parse((*getitem
)(argv
, i
), "et",
1470 Py_FileSystemDefaultEncoding
,
1472 free_string_array(argvlist
, i
);
1473 PyErr_SetString(PyExc_TypeError
,
1474 "execv() arg 2 must contain only strings");
1480 argvlist
[argc
] = NULL
;
1482 execv(path
, argvlist
);
1484 /* If we get here it's definitely an error */
1486 free_string_array(argvlist
, argc
);
1488 return posix_error();
1492 PyDoc_STRVAR(posix_execve__doc__
,
1493 "execve(path, args, env)\n\n\
1494 Execute a path with arguments and environment, replacing current process.\n\
1496 path: path of executable file\n\
1497 args: tuple or list of arguments\n\
1498 env: dictionary of strings mapping to strings");
1501 posix_execve(PyObject
*self
, PyObject
*args
)
1504 PyObject
*argv
, *env
;
1507 PyObject
*key
, *val
, *keys
=NULL
, *vals
=NULL
;
1508 Py_ssize_t i
, pos
, argc
, envc
;
1509 PyObject
*(*getitem
)(PyObject
*, Py_ssize_t
);
1510 Py_ssize_t lastarg
= 0;
1512 /* execve has three arguments: (path, argv, env), where
1513 argv is a list or tuple of strings and env is a dictionary
1514 like posix.environ. */
1516 if (!PyArg_ParseTuple(args
, "etOO:execve",
1517 Py_FileSystemDefaultEncoding
,
1518 &path
, &argv
, &env
))
1520 if (PyList_Check(argv
)) {
1521 argc
= PyList_Size(argv
);
1522 getitem
= PyList_GetItem
;
1524 else if (PyTuple_Check(argv
)) {
1525 argc
= PyTuple_Size(argv
);
1526 getitem
= PyTuple_GetItem
;
1529 PyErr_SetString(PyExc_TypeError
,
1530 "execve() arg 2 must be a tuple or list");
1533 if (!PyMapping_Check(env
)) {
1534 PyErr_SetString(PyExc_TypeError
,
1535 "execve() arg 3 must be a mapping object");
1539 argvlist
= PyMem_NEW(char *, argc
+1);
1540 if (argvlist
== NULL
) {
1544 for (i
= 0; i
< argc
; i
++) {
1545 if (!PyArg_Parse((*getitem
)(argv
, i
),
1546 "et;execve() arg 2 must contain only strings",
1547 Py_FileSystemDefaultEncoding
,
1555 argvlist
[argc
] = NULL
;
1557 i
= PyMapping_Size(env
);
1560 envlist
= PyMem_NEW(char *, i
+ 1);
1561 if (envlist
== NULL
) {
1566 keys
= PyMapping_Keys(env
);
1567 vals
= PyMapping_Values(env
);
1570 if (!PyList_Check(keys
) || !PyList_Check(vals
)) {
1571 PyErr_SetString(PyExc_TypeError
,
1572 "execve(): env.keys() or env.values() is not a list");
1576 for (pos
= 0; pos
< i
; pos
++) {
1580 key
= PyList_GetItem(keys
, pos
);
1581 val
= PyList_GetItem(vals
, pos
);
1587 "s;execve() arg 3 contains a non-string key",
1591 "s;execve() arg 3 contains a non-string value",
1597 #if defined(PYOS_OS2)
1598 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
1599 if (stricmp(k
, "BEGINLIBPATH") != 0 && stricmp(k
, "ENDLIBPATH") != 0) {
1601 len
= PyString_Size(key
) + PyString_Size(val
) + 2;
1602 p
= PyMem_NEW(char, len
);
1607 PyOS_snprintf(p
, len
, "%s=%s", k
, v
);
1608 envlist
[envc
++] = p
;
1609 #if defined(PYOS_OS2)
1615 execve(path
, argvlist
, envlist
);
1617 /* If we get here it's definitely an error */
1619 (void) posix_error();
1623 PyMem_DEL(envlist
[envc
]);
1626 free_string_array(argvlist
, lastarg
);
1633 #endif /* HAVE_EXECV */
1637 PyDoc_STRVAR(posix_spawnv__doc__
,
1638 "spawnv(mode, path, args)\n\n\
1639 Execute the program 'path' in a new process.\n\
1641 mode: mode of process creation\n\
1642 path: path of executable file\n\
1643 args: tuple or list of strings");
1646 posix_spawnv(PyObject
*self
, PyObject
*args
)
1653 Py_intptr_t spawnval
;
1654 PyObject
*(*getitem
)(PyObject
*, Py_ssize_t
);
1656 /* spawnv has three arguments: (mode, path, argv), where
1657 argv is a list or tuple of strings. */
1659 if (!PyArg_ParseTuple(args
, "ietO:spawnv", &mode
,
1660 Py_FileSystemDefaultEncoding
,
1663 if (PyList_Check(argv
)) {
1664 argc
= PyList_Size(argv
);
1665 getitem
= PyList_GetItem
;
1667 else if (PyTuple_Check(argv
)) {
1668 argc
= PyTuple_Size(argv
);
1669 getitem
= PyTuple_GetItem
;
1672 PyErr_SetString(PyExc_TypeError
,
1673 "spawnv() arg 2 must be a tuple or list");
1678 argvlist
= PyMem_NEW(char *, argc
+1);
1679 if (argvlist
== NULL
) {
1681 return PyErr_NoMemory();
1683 for (i
= 0; i
< argc
; i
++) {
1684 if (!PyArg_Parse((*getitem
)(argv
, i
), "et",
1685 Py_FileSystemDefaultEncoding
,
1687 free_string_array(argvlist
, i
);
1690 "spawnv() arg 2 must contain only strings");
1695 argvlist
[argc
] = NULL
;
1697 #if defined(PYOS_OS2) && defined(PYCC_GCC)
1698 Py_BEGIN_ALLOW_THREADS
1699 spawnval
= spawnv(mode
, path
, argvlist
);
1700 Py_END_ALLOW_THREADS
1702 if (mode
== _OLD_P_OVERLAY
)
1705 Py_BEGIN_ALLOW_THREADS
1706 spawnval
= _spawnv(mode
, path
, argvlist
);
1707 Py_END_ALLOW_THREADS
1710 free_string_array(argvlist
, argc
);
1714 return posix_error();
1716 #if SIZEOF_LONG == SIZEOF_VOID_P
1717 return Py_BuildValue("l", (long) spawnval
);
1719 return Py_BuildValue("L", (PY_LONG_LONG
) spawnval
);
1724 PyDoc_STRVAR(posix_spawnve__doc__
,
1725 "spawnve(mode, path, args, env)\n\n\
1726 Execute the program 'path' in a new process.\n\
1728 mode: mode of process creation\n\
1729 path: path of executable file\n\
1730 args: tuple or list of arguments\n\
1731 env: dictionary of strings mapping to strings");
1734 posix_spawnve(PyObject
*self
, PyObject
*args
)
1737 PyObject
*argv
, *env
;
1740 PyObject
*key
, *val
, *keys
=NULL
, *vals
=NULL
, *res
=NULL
;
1741 int mode
, pos
, envc
;
1743 Py_intptr_t spawnval
;
1744 PyObject
*(*getitem
)(PyObject
*, Py_ssize_t
);
1745 Py_ssize_t lastarg
= 0;
1747 /* spawnve has four arguments: (mode, path, argv, env), where
1748 argv is a list or tuple of strings and env is a dictionary
1749 like posix.environ. */
1751 if (!PyArg_ParseTuple(args
, "ietOO:spawnve", &mode
,
1752 Py_FileSystemDefaultEncoding
,
1753 &path
, &argv
, &env
))
1755 if (PyList_Check(argv
)) {
1756 argc
= PyList_Size(argv
);
1757 getitem
= PyList_GetItem
;
1759 else if (PyTuple_Check(argv
)) {
1760 argc
= PyTuple_Size(argv
);
1761 getitem
= PyTuple_GetItem
;
1764 PyErr_SetString(PyExc_TypeError
,
1765 "spawnve() arg 2 must be a tuple or list");
1768 if (!PyMapping_Check(env
)) {
1769 PyErr_SetString(PyExc_TypeError
,
1770 "spawnve() arg 3 must be a mapping object");
1774 argvlist
= PyMem_NEW(char *, argc
+1);
1775 if (argvlist
== NULL
) {
1779 for (i
= 0; i
< argc
; i
++) {
1780 if (!PyArg_Parse((*getitem
)(argv
, i
),
1781 "et;spawnve() arg 2 must contain only strings",
1782 Py_FileSystemDefaultEncoding
,
1790 argvlist
[argc
] = NULL
;
1792 i
= PyMapping_Size(env
);
1795 envlist
= PyMem_NEW(char *, i
+ 1);
1796 if (envlist
== NULL
) {
1801 keys
= PyMapping_Keys(env
);
1802 vals
= PyMapping_Values(env
);
1805 if (!PyList_Check(keys
) || !PyList_Check(vals
)) {
1806 PyErr_SetString(PyExc_TypeError
,
1807 "spawnve(): env.keys() or env.values() is not a list");
1811 for (pos
= 0; pos
< i
; pos
++) {
1815 key
= PyList_GetItem(keys
, pos
);
1816 val
= PyList_GetItem(vals
, pos
);
1822 "s;spawnve() arg 3 contains a non-string key",
1826 "s;spawnve() arg 3 contains a non-string value",
1831 len
= PyString_Size(key
) + PyString_Size(val
) + 2;
1832 p
= PyMem_NEW(char, len
);
1837 PyOS_snprintf(p
, len
, "%s=%s", k
, v
);
1838 envlist
[envc
++] = p
;
1842 #if defined(PYOS_OS2) && defined(PYCC_GCC)
1843 Py_BEGIN_ALLOW_THREADS
1844 spawnval
= spawnve(mode
, path
, argvlist
, envlist
);
1845 Py_END_ALLOW_THREADS
1847 if (mode
== _OLD_P_OVERLAY
)
1850 Py_BEGIN_ALLOW_THREADS
1851 spawnval
= _spawnve(mode
, path
, argvlist
, envlist
);
1852 Py_END_ALLOW_THREADS
1856 (void) posix_error();
1858 #if SIZEOF_LONG == SIZEOF_VOID_P
1859 res
= Py_BuildValue("l", (long) spawnval
);
1861 res
= Py_BuildValue("L", (PY_LONG_LONG
) spawnval
);
1866 PyMem_DEL(envlist
[envc
]);
1869 free_string_array(argvlist
, lastarg
);
1877 /* OS/2 supports spawnvp & spawnvpe natively */
1878 #if defined(PYOS_OS2)
1879 PyDoc_STRVAR(posix_spawnvp__doc__
,
1880 "spawnvp(mode, file, args)\n\n\
1881 Execute the program 'file' in a new process, using the environment\n\
1882 search path to find the file.\n\
1884 mode: mode of process creation\n\
1885 file: executable file name\n\
1886 args: tuple or list of strings");
1889 posix_spawnvp(PyObject
*self
, PyObject
*args
)
1895 Py_intptr_t spawnval
;
1896 PyObject
*(*getitem
)(PyObject
*, Py_ssize_t
);
1898 /* spawnvp has three arguments: (mode, path, argv), where
1899 argv is a list or tuple of strings. */
1901 if (!PyArg_ParseTuple(args
, "ietO:spawnvp", &mode
,
1902 Py_FileSystemDefaultEncoding
,
1905 if (PyList_Check(argv
)) {
1906 argc
= PyList_Size(argv
);
1907 getitem
= PyList_GetItem
;
1909 else if (PyTuple_Check(argv
)) {
1910 argc
= PyTuple_Size(argv
);
1911 getitem
= PyTuple_GetItem
;
1914 PyErr_SetString(PyExc_TypeError
,
1915 "spawnvp() arg 2 must be a tuple or list");
1920 argvlist
= PyMem_NEW(char *, argc
+1);
1921 if (argvlist
== NULL
) {
1923 return PyErr_NoMemory();
1925 for (i
= 0; i
< argc
; i
++) {
1926 if (!PyArg_Parse((*getitem
)(argv
, i
), "et",
1927 Py_FileSystemDefaultEncoding
,
1929 free_string_array(argvlist
, i
);
1932 "spawnvp() arg 2 must contain only strings");
1937 argvlist
[argc
] = NULL
;
1939 Py_BEGIN_ALLOW_THREADS
1940 #if defined(PYCC_GCC)
1941 spawnval
= spawnvp(mode
, path
, argvlist
);
1943 spawnval
= _spawnvp(mode
, path
, argvlist
);
1945 Py_END_ALLOW_THREADS
1947 free_string_array(argvlist
, argc
);
1951 return posix_error();
1953 return Py_BuildValue("l", (long) spawnval
);
1957 PyDoc_STRVAR(posix_spawnvpe__doc__
,
1958 "spawnvpe(mode, file, args, env)\n\n\
1959 Execute the program 'file' in a new process, using the environment\n\
1960 search path to find the file.\n\
1962 mode: mode of process creation\n\
1963 file: executable file name\n\
1964 args: tuple or list of arguments\n\
1965 env: dictionary of strings mapping to strings");
1968 posix_spawnvpe(PyObject
*self
, PyObject
*args
)
1971 PyObject
*argv
, *env
;
1974 PyObject
*key
, *val
, *keys
=NULL
, *vals
=NULL
, *res
=NULL
;
1975 int mode
, i
, pos
, argc
, envc
;
1976 Py_intptr_t spawnval
;
1977 PyObject
*(*getitem
)(PyObject
*, Py_ssize_t
);
1980 /* spawnvpe has four arguments: (mode, path, argv, env), where
1981 argv is a list or tuple of strings and env is a dictionary
1982 like posix.environ. */
1984 if (!PyArg_ParseTuple(args
, "ietOO:spawnvpe", &mode
,
1985 Py_FileSystemDefaultEncoding
,
1986 &path
, &argv
, &env
))
1988 if (PyList_Check(argv
)) {
1989 argc
= PyList_Size(argv
);
1990 getitem
= PyList_GetItem
;
1992 else if (PyTuple_Check(argv
)) {
1993 argc
= PyTuple_Size(argv
);
1994 getitem
= PyTuple_GetItem
;
1997 PyErr_SetString(PyExc_TypeError
,
1998 "spawnvpe() arg 2 must be a tuple or list");
2001 if (!PyMapping_Check(env
)) {
2002 PyErr_SetString(PyExc_TypeError
,
2003 "spawnvpe() arg 3 must be a mapping object");
2007 argvlist
= PyMem_NEW(char *, argc
+1);
2008 if (argvlist
== NULL
) {
2012 for (i
= 0; i
< argc
; i
++) {
2013 if (!PyArg_Parse((*getitem
)(argv
, i
),
2014 "et;spawnvpe() arg 2 must contain only strings",
2015 Py_FileSystemDefaultEncoding
,
2023 argvlist
[argc
] = NULL
;
2025 i
= PyMapping_Size(env
);
2028 envlist
= PyMem_NEW(char *, i
+ 1);
2029 if (envlist
== NULL
) {
2034 keys
= PyMapping_Keys(env
);
2035 vals
= PyMapping_Values(env
);
2038 if (!PyList_Check(keys
) || !PyList_Check(vals
)) {
2039 PyErr_SetString(PyExc_TypeError
,
2040 "spawnvpe(): env.keys() or env.values() is not a list");
2044 for (pos
= 0; pos
< i
; pos
++) {
2048 key
= PyList_GetItem(keys
, pos
);
2049 val
= PyList_GetItem(vals
, pos
);
2055 "s;spawnvpe() arg 3 contains a non-string key",
2059 "s;spawnvpe() arg 3 contains a non-string value",
2064 len
= PyString_Size(key
) + PyString_Size(val
) + 2;
2065 p
= PyMem_NEW(char, len
);
2070 PyOS_snprintf(p
, len
, "%s=%s", k
, v
);
2071 envlist
[envc
++] = p
;
2075 Py_BEGIN_ALLOW_THREADS
2076 #if defined(PYCC_GCC)
2077 spawnval
= spawnvpe(mode
, path
, argvlist
, envlist
);
2079 spawnval
= _spawnvpe(mode
, path
, argvlist
, envlist
);
2081 Py_END_ALLOW_THREADS
2084 (void) posix_error();
2086 res
= Py_BuildValue("l", (long) spawnval
);
2090 PyMem_DEL(envlist
[envc
]);
2093 free_string_array(argvlist
, lastarg
);
2100 #endif /* PYOS_OS2 */
2101 #endif /* HAVE_SPAWNV */
2105 PyDoc_STRVAR(posix_fork1__doc__
,
2106 "fork1() -> pid\n\n\
2107 Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
2109 Return 0 to child process and PID of child to parent process.");
2112 posix_fork1(PyObject
*self
, PyObject
*noargs
)
2116 _PyImport_AcquireLock();
2119 /* child: this clobbers and resets the import lock. */
2122 /* parent: release the import lock. */
2123 result
= _PyImport_ReleaseLock();
2126 return posix_error();
2128 /* Don't clobber the OSError if the fork failed. */
2129 PyErr_SetString(PyExc_RuntimeError
,
2130 "not holding the import lock");
2133 return PyLong_FromPid(pid
);
2139 PyDoc_STRVAR(posix_fork__doc__
,
2141 Fork a child process.\n\
2142 Return 0 to child process and PID of child to parent process.");
2145 posix_fork(PyObject
*self
, PyObject
*noargs
)
2149 _PyImport_AcquireLock();
2152 /* child: this clobbers and resets the import lock. */
2155 /* parent: release the import lock. */
2156 result
= _PyImport_ReleaseLock();
2159 return posix_error();
2161 /* Don't clobber the OSError if the fork failed. */
2162 PyErr_SetString(PyExc_RuntimeError
,
2163 "not holding the import lock");
2166 return PyLong_FromPid(pid
);
2170 /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
2171 /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
2172 #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
2173 #define DEV_PTY_FILE "/dev/ptc"
2174 #define HAVE_DEV_PTMX
2176 #define DEV_PTY_FILE "/dev/ptmx"
2179 #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
2183 #ifdef HAVE_LIBUTIL_H
2184 #include <libutil.h>
2188 #endif /* HAVE_UTIL_H */
2189 #endif /* HAVE_LIBUTIL_H */
2190 #endif /* HAVE_PTY_H */
2191 #ifdef HAVE_STROPTS_H
2192 #include <stropts.h>
2194 #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
2196 #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
2197 PyDoc_STRVAR(posix_openpty__doc__
,
2198 "openpty() -> (master_fd, slave_fd)\n\n\
2199 Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
2202 posix_openpty(PyObject
*self
, PyObject
*noargs
)
2204 int master_fd
, slave_fd
;
2205 #ifndef HAVE_OPENPTY
2208 #if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
2209 PyOS_sighandler_t sig_saved
;
2211 extern char *ptsname(int fildes
);
2216 if (openpty(&master_fd
, &slave_fd
, NULL
, NULL
, NULL
) != 0)
2217 return posix_error();
2218 #elif defined(HAVE__GETPTY)
2219 slave_name
= _getpty(&master_fd
, O_RDWR
, 0666, 0);
2220 if (slave_name
== NULL
)
2221 return posix_error();
2223 slave_fd
= open(slave_name
, O_RDWR
);
2225 return posix_error();
2227 master_fd
= open(DEV_PTY_FILE
, O_RDWR
| O_NOCTTY
); /* open master */
2229 return posix_error();
2230 sig_saved
= PyOS_setsig(SIGCHLD
, SIG_DFL
);
2231 /* change permission of slave */
2232 if (grantpt(master_fd
) < 0) {
2233 PyOS_setsig(SIGCHLD
, sig_saved
);
2234 return posix_error();
2237 if (unlockpt(master_fd
) < 0) {
2238 PyOS_setsig(SIGCHLD
, sig_saved
);
2239 return posix_error();
2241 PyOS_setsig(SIGCHLD
, sig_saved
);
2242 slave_name
= ptsname(master_fd
); /* get name of slave */
2243 if (slave_name
== NULL
)
2244 return posix_error();
2245 slave_fd
= open(slave_name
, O_RDWR
| O_NOCTTY
); /* open slave */
2247 return posix_error();
2248 #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
2249 ioctl(slave_fd
, I_PUSH
, "ptem"); /* push ptem */
2250 ioctl(slave_fd
, I_PUSH
, "ldterm"); /* push ldterm */
2252 ioctl(slave_fd
, I_PUSH
, "ttcompat"); /* push ttcompat */
2254 #endif /* HAVE_CYGWIN */
2255 #endif /* HAVE_OPENPTY */
2257 return Py_BuildValue("(ii)", master_fd
, slave_fd
);
2260 #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
2263 PyDoc_STRVAR(posix_forkpty__doc__
,
2264 "forkpty() -> (pid, master_fd)\n\n\
2265 Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
2266 Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
2267 To both, return fd of newly opened pseudo-terminal.\n");
2270 posix_forkpty(PyObject
*self
, PyObject
*noargs
)
2272 int master_fd
= -1, result
= 0;
2275 _PyImport_AcquireLock();
2276 pid
= forkpty(&master_fd
, NULL
, NULL
, NULL
);
2278 /* child: this clobbers and resets the import lock. */
2281 /* parent: release the import lock. */
2282 result
= _PyImport_ReleaseLock();
2285 return posix_error();
2287 /* Don't clobber the OSError if the fork failed. */
2288 PyErr_SetString(PyExc_RuntimeError
,
2289 "not holding the import lock");
2292 return Py_BuildValue("(Ni)", PyLong_FromPid(pid
), master_fd
);
2297 PyDoc_STRVAR(posix_getegid__doc__
,
2298 "getegid() -> egid\n\n\
2299 Return the current process's effective group id.");
2302 posix_getegid(PyObject
*self
, PyObject
*noargs
)
2304 return PyInt_FromLong((long)getegid());
2310 PyDoc_STRVAR(posix_geteuid__doc__
,
2311 "geteuid() -> euid\n\n\
2312 Return the current process's effective user id.");
2315 posix_geteuid(PyObject
*self
, PyObject
*noargs
)
2317 return PyInt_FromLong((long)geteuid());
2323 PyDoc_STRVAR(posix_getgid__doc__
,
2324 "getgid() -> gid\n\n\
2325 Return the current process's group id.");
2328 posix_getgid(PyObject
*self
, PyObject
*noargs
)
2330 return PyInt_FromLong((long)getgid());
2335 PyDoc_STRVAR(posix_getpid__doc__
,
2336 "getpid() -> pid\n\n\
2337 Return the current process id");
2340 posix_getpid(PyObject
*self
, PyObject
*noargs
)
2342 return PyLong_FromPid(getpid());
2346 #ifdef HAVE_GETGROUPS
2347 PyDoc_STRVAR(posix_getgroups__doc__
,
2348 "getgroups() -> list of group IDs\n\n\
2349 Return list of supplemental group IDs for the process.");
2352 posix_getgroups(PyObject
*self
, PyObject
*noargs
)
2354 PyObject
*result
= NULL
;
2357 #define MAX_GROUPS NGROUPS_MAX
2359 /* defined to be 16 on Solaris7, so this should be a small number */
2360 #define MAX_GROUPS 64
2362 gid_t grouplist
[MAX_GROUPS
];
2364 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
2365 * This is a helper variable to store the intermediate result when
2368 * To keep the code readable the OSX behaviour is unconditional,
2369 * according to the POSIX spec this should be safe on all unix-y
2372 gid_t
* alt_grouplist
= grouplist
;
2375 n
= getgroups(MAX_GROUPS
, grouplist
);
2377 if (errno
== EINVAL
) {
2378 n
= getgroups(0, NULL
);
2380 return posix_error();
2383 /* Avoid malloc(0) */
2384 alt_grouplist
= grouplist
;
2386 alt_grouplist
= PyMem_Malloc(n
* sizeof(gid_t
));
2387 if (alt_grouplist
== NULL
) {
2389 return posix_error();
2391 n
= getgroups(n
, alt_grouplist
);
2393 PyMem_Free(alt_grouplist
);
2394 return posix_error();
2398 return posix_error();
2401 result
= PyList_New(n
);
2402 if (result
!= NULL
) {
2404 for (i
= 0; i
< n
; ++i
) {
2405 PyObject
*o
= PyInt_FromLong((long)alt_grouplist
[i
]);
2411 PyList_SET_ITEM(result
, i
, o
);
2415 if (alt_grouplist
!= grouplist
) {
2416 PyMem_Free(alt_grouplist
);
2423 #ifdef HAVE_INITGROUPS
2424 PyDoc_STRVAR(posix_initgroups__doc__
,
2425 "initgroups(username, gid) -> None\n\n\
2426 Call the system initgroups() to initialize the group access list with all of\n\
2427 the groups of which the specified username is a member, plus the specified\n\
2431 posix_initgroups(PyObject
*self
, PyObject
*args
)
2436 if (!PyArg_ParseTuple(args
, "sl:initgroups", &username
, &gid
))
2439 if (initgroups(username
, (gid_t
) gid
) == -1)
2440 return PyErr_SetFromErrno(PyExc_OSError
);
2448 PyDoc_STRVAR(posix_getpgid__doc__
,
2449 "getpgid(pid) -> pgid\n\n\
2450 Call the system call getpgid().");
2453 posix_getpgid(PyObject
*self
, PyObject
*args
)
2456 if (!PyArg_ParseTuple(args
, PARSE_PID
":getpgid", &pid
))
2458 pgid
= getpgid(pid
);
2460 return posix_error();
2461 return PyLong_FromPid(pgid
);
2463 #endif /* HAVE_GETPGID */
2467 PyDoc_STRVAR(posix_getpgrp__doc__
,
2468 "getpgrp() -> pgrp\n\n\
2469 Return the current process group id.");
2472 posix_getpgrp(PyObject
*self
, PyObject
*noargs
)
2474 #ifdef GETPGRP_HAVE_ARG
2475 return PyLong_FromPid(getpgrp(0));
2476 #else /* GETPGRP_HAVE_ARG */
2477 return PyLong_FromPid(getpgrp());
2478 #endif /* GETPGRP_HAVE_ARG */
2480 #endif /* HAVE_GETPGRP */
2484 PyDoc_STRVAR(posix_setpgrp__doc__
,
2486 Make this process the process group leader.");
2489 posix_setpgrp(PyObject
*self
, PyObject
*noargs
)
2491 #ifdef SETPGRP_HAVE_ARG
2492 if (setpgrp(0, 0) < 0)
2493 #else /* SETPGRP_HAVE_ARG */
2495 #endif /* SETPGRP_HAVE_ARG */
2496 return posix_error();
2501 #endif /* HAVE_SETPGRP */
2504 PyDoc_STRVAR(posix_getppid__doc__
,
2505 "getppid() -> ppid\n\n\
2506 Return the parent's process id.");
2509 posix_getppid(PyObject
*self
, PyObject
*noargs
)
2511 return PyLong_FromPid(getppid());
2516 #ifdef HAVE_GETLOGIN
2517 PyDoc_STRVAR(posix_getlogin__doc__
,
2518 "getlogin() -> string\n\n\
2519 Return the actual login name.");
2522 posix_getlogin(PyObject
*self
, PyObject
*noargs
)
2524 PyObject
*result
= NULL
;
2526 int old_errno
= errno
;
2534 PyErr_SetString(PyExc_OSError
,
2535 "unable to determine login name");
2538 result
= PyString_FromString(name
);
2545 #ifndef UEFI_C_SOURCE
2546 PyDoc_STRVAR(posix_getuid__doc__
,
2547 "getuid() -> uid\n\n\
2548 Return the current process's user id.");
2551 posix_getuid(PyObject
*self
, PyObject
*noargs
)
2553 return PyInt_FromLong((long)getuid());
2555 #endif /* UEFI_C_SOURCE */
2558 PyDoc_STRVAR(posix_kill__doc__
,
2559 "kill(pid, sig)\n\n\
2560 Kill a process with a signal.");
2563 posix_kill(PyObject
*self
, PyObject
*args
)
2567 if (!PyArg_ParseTuple(args
, PARSE_PID
"i:kill", &pid
, &sig
))
2569 #if defined(PYOS_OS2) && !defined(PYCC_GCC)
2570 if (sig
== XCPT_SIGNAL_INTR
|| sig
== XCPT_SIGNAL_BREAK
) {
2572 if ((rc
= DosSendSignalException(pid
, sig
)) != NO_ERROR
)
2573 return os2_error(rc
);
2575 } else if (sig
== XCPT_SIGNAL_KILLPROC
) {
2577 if ((rc
= DosKillProcess(DKP_PROCESS
, pid
)) != NO_ERROR
)
2578 return os2_error(rc
);
2581 return NULL
; /* Unrecognized Signal Requested */
2583 if (kill(pid
, sig
) == -1)
2584 return posix_error();
2592 PyDoc_STRVAR(posix_killpg__doc__
,
2593 "killpg(pgid, sig)\n\n\
2594 Kill a process group with a signal.");
2597 posix_killpg(PyObject
*self
, PyObject
*args
)
2601 /* XXX some man pages make the `pgid` parameter an int, others
2602 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
2603 take the same type. Moreover, pid_t is always at least as wide as
2604 int (else compilation of this module fails), which is safe. */
2605 if (!PyArg_ParseTuple(args
, PARSE_PID
"i:killpg", &pgid
, &sig
))
2607 if (killpg(pgid
, sig
) == -1)
2608 return posix_error();
2616 #ifdef HAVE_SYS_LOCK_H
2617 #include <sys/lock.h>
2620 PyDoc_STRVAR(posix_plock__doc__
,
2622 Lock program segments into memory.");
2625 posix_plock(PyObject
*self
, PyObject
*args
)
2628 if (!PyArg_ParseTuple(args
, "i:plock", &op
))
2630 if (plock(op
) == -1)
2631 return posix_error();
2639 PyDoc_STRVAR(posix_popen__doc__
,
2640 "popen(command [, mode='r' [, bufsize]]) -> pipe\n\n\
2641 Open a pipe to/from a command returning a file object.");
2643 #if defined(PYOS_OS2)
2644 #if defined(PYCC_VACPP)
2646 async_system(const char *command
)
2648 char errormsg
[256], args
[1024];
2652 char *shell
= getenv("COMSPEC");
2656 /* avoid overflowing the argument buffer */
2657 if (strlen(shell
) + 3 + strlen(command
) >= 1024)
2658 return ERROR_NOT_ENOUGH_MEMORY
2661 strcat(args
, shell
);
2662 strcat(args
, "/c ");
2663 strcat(args
, command
);
2665 /* execute asynchronously, inheriting the environment */
2666 rc
= DosExecPgm(errormsg
,
2677 popen(const char *command
, const char *mode
, int pipesize
, int *err
)
2683 /* mode determines which of stdin or stdout is reconnected to
2684 * the pipe to the child
2686 if (strchr(mode
, 'r') != NULL
) {
2687 tgt_fd
= 1; /* stdout */
2688 } else if (strchr(mode
, 'w')) {
2689 tgt_fd
= 0; /* stdin */
2691 *err
= ERROR_INVALID_ACCESS
;
2695 /* setup the pipe */
2696 if ((rc
= DosCreatePipe(&pipeh
[0], &pipeh
[1], pipesize
)) != NO_ERROR
) {
2701 /* prevent other threads accessing stdio */
2704 /* reconnect stdio and execute child */
2707 if (dup2(pipeh
[tgtfd
], tgtfd
) == 0) {
2708 DosClose(pipeh
[tgtfd
]);
2709 rc
= async_system(command
);
2716 /* allow other threads access to stdio */
2719 /* if execution of child was successful return file stream */
2721 return fdopen(pipeh
[1 - tgtfd
], mode
);
2723 DosClose(pipeh
[1 - tgtfd
]);
2730 posix_popen(PyObject
*self
, PyObject
*args
)
2734 int err
, bufsize
= -1;
2737 if (!PyArg_ParseTuple(args
, "s|si:popen", &name
, &mode
, &bufsize
))
2739 Py_BEGIN_ALLOW_THREADS
2740 fp
= popen(name
, mode
, (bufsize
> 0) ? bufsize
: 4096, &err
);
2741 Py_END_ALLOW_THREADS
2743 return os2_error(err
);
2745 f
= PyFile_FromFile(fp
, name
, mode
, fclose
);
2747 PyFile_SetBufSize(f
, bufsize
);
2751 #elif defined(PYCC_GCC)
2753 /* standard posix version of popen() support */
2755 posix_popen(PyObject
*self
, PyObject
*args
)
2762 if (!PyArg_ParseTuple(args
, "s|si:popen", &name
, &mode
, &bufsize
))
2764 Py_BEGIN_ALLOW_THREADS
2765 fp
= popen(name
, mode
);
2766 Py_END_ALLOW_THREADS
2768 return posix_error();
2769 f
= PyFile_FromFile(fp
, name
, mode
, pclose
);
2771 PyFile_SetBufSize(f
, bufsize
);
2775 /* fork() under OS/2 has lots'o'warts
2776 * EMX supports pipe() and spawn*() so we can synthesize popen[234]()
2777 * most of this code is a ripoff of the win32 code, but using the
2778 * capabilities of EMX's C library routines
2781 /* These tell _PyPopen() whether to return 1, 2, or 3 file objects. */
2787 static PyObject
*_PyPopen(char *, int, int, int);
2788 static int _PyPclose(FILE *file
);
2791 * Internal dictionary mapping popen* file pointers to process handles,
2792 * for use when retrieving the process exit code. See _PyPclose() below
2793 * for more information on this dictionary's use.
2795 static PyObject
*_PyPopenProcs
= NULL
;
2797 /* os2emx version of popen2()
2799 * The result of this function is a pipe (file) connected to the
2800 * process's stdin, and a pipe connected to the process's stdout.
2804 os2emx_popen2(PyObject
*self
, PyObject
*args
)
2812 if (!PyArg_ParseTuple(args
, "s|si:popen2", &cmdstring
, &mode
, &bufsize
))
2817 else if (*mode
!= 'b') {
2818 PyErr_SetString(PyExc_ValueError
, "mode must be 't' or 'b'");
2823 f
= _PyPopen(cmdstring
, tm
, POPEN_2
, bufsize
);
2829 * Variation on os2emx.popen2
2831 * The result of this function is 3 pipes - the process's stdin,
2836 os2emx_popen3(PyObject
*self
, PyObject
*args
)
2844 if (!PyArg_ParseTuple(args
, "s|si:popen3", &cmdstring
, &mode
, &bufsize
))
2849 else if (*mode
!= 'b') {
2850 PyErr_SetString(PyExc_ValueError
, "mode must be 't' or 'b'");
2855 f
= _PyPopen(cmdstring
, tm
, POPEN_3
, bufsize
);
2861 * Variation on os2emx.popen2
2863 * The result of this function is 2 pipes - the processes stdin,
2864 * and stdout+stderr combined as a single pipe.
2868 os2emx_popen4(PyObject
*self
, PyObject
*args
)
2876 if (!PyArg_ParseTuple(args
, "s|si:popen4", &cmdstring
, &mode
, &bufsize
))
2881 else if (*mode
!= 'b') {
2882 PyErr_SetString(PyExc_ValueError
, "mode must be 't' or 'b'");
2887 f
= _PyPopen(cmdstring
, tm
, POPEN_4
, bufsize
);
2892 /* a couple of structures for convenient handling of multiple
2893 * file handles and pipes
2907 /* The following code is derived from the win32 code */
2910 _PyPopen(char *cmdstring
, int mode
, int n
, int bufsize
)
2912 struct file_ref stdio
[3];
2913 struct pipe_ref p_fd
[3];
2915 int file_count
, i
, pipe_err
;
2917 char *shell
, *sh_name
, *opt
, *rd_mode
, *wr_mode
;
2918 PyObject
*f
, *p_f
[3];
2920 /* file modes for subsequent fdopen's on pipe handles */
2932 /* prepare shell references */
2933 if ((shell
= getenv("EMXSHELL")) == NULL
)
2934 if ((shell
= getenv("COMSPEC")) == NULL
)
2937 return posix_error();
2940 sh_name
= _getname(shell
);
2941 if (stricmp(sh_name
, "cmd.exe") == 0 || stricmp(sh_name
, "4os2.exe") == 0)
2946 /* save current stdio fds + their flags, and set not inheritable */
2948 while (pipe_err
>= 0 && i
< 3)
2950 pipe_err
= stdio
[i
].handle
= dup(i
);
2951 stdio
[i
].flags
= fcntl(i
, F_GETFD
, 0);
2952 fcntl(stdio
[i
].handle
, F_SETFD
, stdio
[i
].flags
| FD_CLOEXEC
);
2957 /* didn't get them all saved - clean up and bail out */
2958 int saved_err
= errno
;
2961 close(stdio
[i
].handle
);
2964 return posix_error();
2967 /* create pipe ends */
2972 while ((pipe_err
== 0) && (i
< file_count
))
2973 pipe_err
= pipe((int *)&p_fd
[i
++]);
2976 /* didn't get them all made - clean up and bail out */
2983 return posix_error();
2986 /* change the actual standard IO streams over temporarily,
2987 * making the retained pipe ends non-inheritable
2992 if (dup2(p_fd
[0].rd
, 0) == 0)
2995 i
= fcntl(p_fd
[0].wr
, F_GETFD
, 0);
2996 fcntl(p_fd
[0].wr
, F_SETFD
, i
| FD_CLOEXEC
);
2997 if ((p_s
[0] = fdopen(p_fd
[0].wr
, wr_mode
)) == NULL
)
3011 if (dup2(p_fd
[1].wr
, 1) == 1)
3014 i
= fcntl(p_fd
[1].rd
, F_GETFD
, 0);
3015 fcntl(p_fd
[1].rd
, F_SETFD
, i
| FD_CLOEXEC
);
3016 if ((p_s
[1] = fdopen(p_fd
[1].rd
, rd_mode
)) == NULL
)
3028 /* - stderr, as required */
3034 if (dup2(p_fd
[2].wr
, 2) == 2)
3037 i
= fcntl(p_fd
[2].rd
, F_GETFD
, 0);
3038 fcntl(p_fd
[2].rd
, F_SETFD
, i
| FD_CLOEXEC
);
3039 if ((p_s
[2] = fdopen(p_fd
[2].rd
, rd_mode
)) == NULL
)
3054 if (dup2(1, 2) != 2)
3062 /* spawn the child process */
3065 pipe_pid
= spawnlp(P_NOWAIT
, shell
, shell
, opt
, cmdstring
, (char *)0);
3072 /* save the PID into the FILE structure
3073 * NOTE: this implementation doesn't actually
3074 * take advantage of this, but do it for
3075 * completeness - AIM Apr01
3077 for (i
= 0; i
< file_count
; i
++)
3078 p_s
[i
]->_pid
= pipe_pid
;
3082 /* reset standard IO to normal */
3083 for (i
= 0; i
< 3; i
++)
3085 dup2(stdio
[i
].handle
, i
);
3086 fcntl(i
, F_SETFD
, stdio
[i
].flags
);
3087 close(stdio
[i
].handle
);
3090 /* if any remnant problems, clean up and bail out */
3093 for (i
= 0; i
< 3; i
++)
3099 return posix_error_with_filename(cmdstring
);
3102 /* build tuple of file objects to return */
3103 if ((p_f
[0] = PyFile_FromFile(p_s
[0], cmdstring
, wr_mode
, _PyPclose
)) != NULL
)
3104 PyFile_SetBufSize(p_f
[0], bufsize
);
3105 if ((p_f
[1] = PyFile_FromFile(p_s
[1], cmdstring
, rd_mode
, _PyPclose
)) != NULL
)
3106 PyFile_SetBufSize(p_f
[1], bufsize
);
3109 if ((p_f
[2] = PyFile_FromFile(p_s
[2], cmdstring
, rd_mode
, _PyPclose
)) != NULL
)
3110 PyFile_SetBufSize(p_f
[0], bufsize
);
3111 f
= PyTuple_Pack(3, p_f
[0], p_f
[1], p_f
[2]);
3114 f
= PyTuple_Pack(2, p_f
[0], p_f
[1]);
3117 * Insert the files we've created into the process dictionary
3118 * all referencing the list with the process handle and the
3119 * initial number of files (see description below in _PyPclose).
3120 * Since if _PyPclose later tried to wait on a process when all
3121 * handles weren't closed, it could create a deadlock with the
3122 * child, we spend some energy here to try to ensure that we
3123 * either insert all file handles into the dictionary or none
3124 * at all. It's a little clumsy with the various popen modes
3125 * and variable number of files involved.
3129 _PyPopenProcs
= PyDict_New();
3134 PyObject
*procObj
, *pidObj
, *intObj
, *fileObj
[3];
3137 fileObj
[0] = fileObj
[1] = fileObj
[2] = NULL
;
3138 ins_rc
[0] = ins_rc
[1] = ins_rc
[2] = 0;
3140 procObj
= PyList_New(2);
3141 pidObj
= PyLong_FromPid(pipe_pid
);
3142 intObj
= PyInt_FromLong((long) file_count
);
3144 if (procObj
&& pidObj
&& intObj
)
3146 PyList_SetItem(procObj
, 0, pidObj
);
3147 PyList_SetItem(procObj
, 1, intObj
);
3149 fileObj
[0] = PyLong_FromVoidPtr(p_s
[0]);
3152 ins_rc
[0] = PyDict_SetItem(_PyPopenProcs
,
3156 fileObj
[1] = PyLong_FromVoidPtr(p_s
[1]);
3159 ins_rc
[1] = PyDict_SetItem(_PyPopenProcs
,
3163 if (file_count
>= 3)
3165 fileObj
[2] = PyLong_FromVoidPtr(p_s
[2]);
3168 ins_rc
[2] = PyDict_SetItem(_PyPopenProcs
,
3174 if (ins_rc
[0] < 0 || !fileObj
[0] ||
3175 ins_rc
[1] < 0 || (file_count
> 1 && !fileObj
[1]) ||
3176 ins_rc
[2] < 0 || (file_count
> 2 && !fileObj
[2]))
3178 /* Something failed - remove any dictionary
3179 * entries that did make it.
3181 if (!ins_rc
[0] && fileObj
[0])
3183 PyDict_DelItem(_PyPopenProcs
,
3186 if (!ins_rc
[1] && fileObj
[1])
3188 PyDict_DelItem(_PyPopenProcs
,
3191 if (!ins_rc
[2] && fileObj
[2])
3193 PyDict_DelItem(_PyPopenProcs
,
3200 * Clean up our localized references for the dictionary keys
3201 * and value since PyDict_SetItem will Py_INCREF any copies
3202 * that got placed in the dictionary.
3204 Py_XDECREF(procObj
);
3205 Py_XDECREF(fileObj
[0]);
3206 Py_XDECREF(fileObj
[1]);
3207 Py_XDECREF(fileObj
[2]);
3210 /* Child is launched. */
3215 * Wrapper for fclose() to use for popen* files, so we can retrieve the
3216 * exit code for the child process and return as a result of the close.
3218 * This function uses the _PyPopenProcs dictionary in order to map the
3219 * input file pointer to information about the process that was
3220 * originally created by the popen* call that created the file pointer.
3221 * The dictionary uses the file pointer as a key (with one entry
3222 * inserted for each file returned by the original popen* call) and a
3223 * single list object as the value for all files from a single call.
3224 * The list object contains the Win32 process handle at [0], and a file
3225 * count at [1], which is initialized to the total number of file
3226 * handles using that list.
3228 * This function closes whichever handle it is passed, and decrements
3229 * the file count in the dictionary for the process handle pointed to
3230 * by this file. On the last close (when the file count reaches zero),
3231 * this function will wait for the child process and then return its
3232 * exit code as the result of the close() operation. This permits the
3233 * files to be closed in any order - it is always the close() of the
3234 * final handle that will return the exit code.
3236 * NOTE: This function is currently called with the GIL released.
3237 * hence we use the GILState API to manage our state.
3240 static int _PyPclose(FILE *file
)
3245 PyObject
*procObj
, *pidObj
, *intObj
, *fileObj
;
3248 PyGILState_STATE state
;
3251 /* Close the file handle first, to ensure it can't block the
3252 * child from exiting if it's the last handle.
3254 result
= fclose(file
);
3257 state
= PyGILState_Ensure();
3261 if ((fileObj
= PyLong_FromVoidPtr(file
)) != NULL
&&
3262 (procObj
= PyDict_GetItem(_PyPopenProcs
,
3263 fileObj
)) != NULL
&&
3264 (pidObj
= PyList_GetItem(procObj
,0)) != NULL
&&
3265 (intObj
= PyList_GetItem(procObj
,1)) != NULL
)
3267 pipe_pid
= (pid_t
) PyLong_AsPid(pidObj
);
3268 file_count
= (int) PyInt_AsLong(intObj
);
3272 /* Still other files referencing process */
3274 PyList_SetItem(procObj
,1,
3275 PyInt_FromLong((long) file_count
));
3279 /* Last file for this process */
3280 if (result
!= EOF
&&
3281 waitpid(pipe_pid
, &exit_code
, 0) == pipe_pid
)
3283 /* extract exit status */
3284 if (WIFEXITED(exit_code
))
3286 result
= WEXITSTATUS(exit_code
);
3296 /* Indicate failure - this will cause the file object
3297 * to raise an I/O error and translate the last
3298 * error code from errno. We do have a problem with
3299 * last errors that overlap the normal errno table,
3300 * but that's a consistent problem with the file object.
3306 /* Remove this file pointer from dictionary */
3307 PyDict_DelItem(_PyPopenProcs
, fileObj
);
3309 if (PyDict_Size(_PyPopenProcs
) == 0)
3311 Py_DECREF(_PyPopenProcs
);
3312 _PyPopenProcs
= NULL
;
3315 } /* if object retrieval ok */
3317 Py_XDECREF(fileObj
);
3318 } /* if _PyPopenProcs */
3321 PyGILState_Release(state
);
3326 #endif /* PYCC_??? */
3328 #elif defined(MS_WINDOWS)
3331 * Portable 'popen' replacement for Win32.
3333 * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks
3334 * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com>
3335 * Return code handling by David Bolen <db3l@fitlinxx.com>.
3342 /* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */
3348 static PyObject
*_PyPopen(char *, int, int);
3349 static int _PyPclose(FILE *file
);
3352 * Internal dictionary mapping popen* file pointers to process handles,
3353 * for use when retrieving the process exit code. See _PyPclose() below
3354 * for more information on this dictionary's use.
3356 static PyObject
*_PyPopenProcs
= NULL
;
3359 /* popen that works from a GUI.
3361 * The result of this function is a pipe (file) connected to the
3362 * processes stdin or stdout, depending on the requested mode.
3366 posix_popen(PyObject
*self
, PyObject
*args
)
3374 if (!PyArg_ParseTuple(args
, "s|si:popen", &cmdstring
, &mode
, &bufsize
))
3379 else if (*mode
!= 'w') {
3380 PyErr_SetString(PyExc_ValueError
, "popen() arg 2 must be 'r' or 'w'");
3385 if (bufsize
!= -1) {
3386 PyErr_SetString(PyExc_ValueError
, "popen() arg 3 must be -1");
3390 if (*(mode
+1) == 't')
3391 f
= _PyPopen(cmdstring
, tm
| _O_TEXT
, POPEN_1
);
3392 else if (*(mode
+1) == 'b')
3393 f
= _PyPopen(cmdstring
, tm
| _O_BINARY
, POPEN_1
);
3395 f
= _PyPopen(cmdstring
, tm
| _O_TEXT
, POPEN_1
);
3400 /* Variation on win32pipe.popen
3402 * The result of this function is a pipe (file) connected to the
3403 * process's stdin, and a pipe connected to the process's stdout.
3407 win32_popen2(PyObject
*self
, PyObject
*args
)
3415 if (!PyArg_ParseTuple(args
, "s|si:popen2", &cmdstring
, &mode
, &bufsize
))
3420 else if (*mode
!= 'b') {
3421 PyErr_SetString(PyExc_ValueError
, "popen2() arg 2 must be 't' or 'b'");
3426 if (bufsize
!= -1) {
3427 PyErr_SetString(PyExc_ValueError
, "popen2() arg 3 must be -1");
3431 f
= _PyPopen(cmdstring
, tm
, POPEN_2
);
3437 * Variation on <om win32pipe.popen>
3439 * The result of this function is 3 pipes - the process's stdin,
3444 win32_popen3(PyObject
*self
, PyObject
*args
)
3452 if (!PyArg_ParseTuple(args
, "s|si:popen3", &cmdstring
, &mode
, &bufsize
))
3457 else if (*mode
!= 'b') {
3458 PyErr_SetString(PyExc_ValueError
, "popen3() arg 2 must be 't' or 'b'");
3463 if (bufsize
!= -1) {
3464 PyErr_SetString(PyExc_ValueError
, "popen3() arg 3 must be -1");
3468 f
= _PyPopen(cmdstring
, tm
, POPEN_3
);
3474 * Variation on win32pipe.popen
3476 * The result of this function is 2 pipes - the processes stdin,
3477 * and stdout+stderr combined as a single pipe.
3481 win32_popen4(PyObject
*self
, PyObject
*args
)
3489 if (!PyArg_ParseTuple(args
, "s|si:popen4", &cmdstring
, &mode
, &bufsize
))
3494 else if (*mode
!= 'b') {
3495 PyErr_SetString(PyExc_ValueError
, "popen4() arg 2 must be 't' or 'b'");
3500 if (bufsize
!= -1) {
3501 PyErr_SetString(PyExc_ValueError
, "popen4() arg 3 must be -1");
3505 f
= _PyPopen(cmdstring
, tm
, POPEN_4
);
3511 _PyPopenCreateProcess(char *cmdstring
,
3517 PROCESS_INFORMATION piProcInfo
;
3518 STARTUPINFO siStartInfo
;
3519 DWORD dwProcessFlags
= 0; /* no NEW_CONSOLE by default for Ctrl+C handling */
3520 char *s1
,*s2
, *s3
= " /c ";
3521 const char *szConsoleSpawn
= "w9xpopen.exe";
3525 if (i
= GetEnvironmentVariable("COMSPEC",NULL
,0)) {
3528 s1
= (char *)alloca(i
);
3529 if (!(x
= GetEnvironmentVariable("COMSPEC", s1
, i
)))
3530 /* x < i, so x fits into an integer */
3533 /* Explicitly check if we are using COMMAND.COM. If we are
3534 * then use the w9xpopen hack.
3537 while (comshell
>= s1
&& *comshell
!= '\\')
3541 if (GetVersion() < 0x80000000 &&
3542 _stricmp(comshell
, "command.com") != 0) {
3543 /* NT/2000 and not using command.com. */
3544 x
= i
+ strlen(s3
) + strlen(cmdstring
) + 1;
3545 s2
= (char *)alloca(x
);
3547 PyOS_snprintf(s2
, x
, "%s%s%s", s1
, s3
, cmdstring
);
3551 * Oh gag, we're on Win9x or using COMMAND.COM. Use
3552 * the workaround listed in KB: Q150956
3554 char modulepath
[_MAX_PATH
];
3555 struct stat statinfo
;
3556 GetModuleFileName(NULL
, modulepath
, sizeof(modulepath
));
3557 for (x
= i
= 0; modulepath
[i
]; i
++)
3558 if (modulepath
[i
] == SEP
)
3560 modulepath
[x
] = '\0';
3561 /* Create the full-name to w9xpopen, so we can test it exists */
3564 (sizeof(modulepath
)/sizeof(modulepath
[0]))
3565 -strlen(modulepath
));
3566 if (stat(modulepath
, &statinfo
) != 0) {
3567 size_t mplen
= sizeof(modulepath
)/sizeof(modulepath
[0]);
3568 /* Eeek - file-not-found - possibly an embedding
3569 situation - see if we can locate it in sys.prefix
3574 modulepath
[mplen
-1] = '\0';
3575 if (modulepath
[strlen(modulepath
)-1] != '\\')
3576 strcat(modulepath
, "\\");
3579 mplen
-strlen(modulepath
));
3580 /* No where else to look - raise an easily identifiable
3581 error, rather than leaving Windows to report
3582 "file not found" - as the user is probably blissfully
3583 unaware this shim EXE is used, and it will confuse them.
3584 (well, it confused me for a while ;-)
3586 if (stat(modulepath
, &statinfo
) != 0) {
3587 PyErr_Format(PyExc_RuntimeError
,
3588 "Can not locate '%s' which is needed "
3589 "for popen to work with your shell "
3595 x
= i
+ strlen(s3
) + strlen(cmdstring
) + 1 +
3596 strlen(modulepath
) +
3597 strlen(szConsoleSpawn
) + 1;
3599 s2
= (char *)alloca(x
);
3601 /* To maintain correct argument passing semantics,
3602 we pass the command-line as it stands, and allow
3603 quoting to be applied. w9xpopen.exe will then
3604 use its argv vector, and re-quote the necessary
3605 args for the ultimate child process.
3614 /* Not passing CREATE_NEW_CONSOLE has been known to
3615 cause random failures on win9x. Specifically a
3617 "Your program accessed mem currently in use at xxx"
3618 and a hopeful warning about the stability of your
3620 Cost is Ctrl+C won't kill children, but anyone
3621 who cares can have a go!
3623 dwProcessFlags
|= CREATE_NEW_CONSOLE
;
3627 /* Could be an else here to try cmd.exe / command.com in the path
3628 Now we'll just error out.. */
3630 PyErr_SetString(PyExc_RuntimeError
,
3631 "Cannot locate a COMSPEC environment variable to "
3632 "use as the shell");
3636 ZeroMemory(&siStartInfo
, sizeof(STARTUPINFO
));
3637 siStartInfo
.cb
= sizeof(STARTUPINFO
);
3638 siStartInfo
.dwFlags
= STARTF_USESTDHANDLES
| STARTF_USESHOWWINDOW
;
3639 siStartInfo
.hStdInput
= hStdin
;
3640 siStartInfo
.hStdOutput
= hStdout
;
3641 siStartInfo
.hStdError
= hStderr
;
3642 siStartInfo
.wShowWindow
= SW_HIDE
;
3644 if (CreateProcess(NULL
,
3654 /* Close the handles now so anyone waiting is woken. */
3655 CloseHandle(piProcInfo
.hThread
);
3657 /* Return process handle */
3658 *hProcess
= piProcInfo
.hProcess
;
3661 win32_error("CreateProcess", s2
);
3665 /* The following code is based off of KB: Q190351 */
3668 _PyPopen(char *cmdstring
, int mode
, int n
)
3670 HANDLE hChildStdinRd
, hChildStdinWr
, hChildStdoutRd
, hChildStdoutWr
,
3671 hChildStderrRd
, hChildStderrWr
, hChildStdinWrDup
, hChildStdoutRdDup
,
3672 hChildStderrRdDup
, hProcess
; /* hChildStdoutWrDup; */
3674 SECURITY_ATTRIBUTES saAttr
;
3681 saAttr
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
3682 saAttr
.bInheritHandle
= TRUE
;
3683 saAttr
.lpSecurityDescriptor
= NULL
;
3685 if (!CreatePipe(&hChildStdinRd
, &hChildStdinWr
, &saAttr
, 0))
3686 return win32_error("CreatePipe", NULL
);
3688 /* Create new output read handle and the input write handle. Set
3689 * the inheritance properties to FALSE. Otherwise, the child inherits
3690 * these handles; resulting in non-closeable handles to the pipes
3692 fSuccess
= DuplicateHandle(GetCurrentProcess(), hChildStdinWr
,
3693 GetCurrentProcess(), &hChildStdinWrDup
, 0,
3695 DUPLICATE_SAME_ACCESS
);
3697 return win32_error("DuplicateHandle", NULL
);
3699 /* Close the inheritable version of ChildStdin
3700 that we're using. */
3701 CloseHandle(hChildStdinWr
);
3703 if (!CreatePipe(&hChildStdoutRd
, &hChildStdoutWr
, &saAttr
, 0))
3704 return win32_error("CreatePipe", NULL
);
3706 fSuccess
= DuplicateHandle(GetCurrentProcess(), hChildStdoutRd
,
3707 GetCurrentProcess(), &hChildStdoutRdDup
, 0,
3708 FALSE
, DUPLICATE_SAME_ACCESS
);
3710 return win32_error("DuplicateHandle", NULL
);
3712 /* Close the inheritable version of ChildStdout
3713 that we're using. */
3714 CloseHandle(hChildStdoutRd
);
3717 if (!CreatePipe(&hChildStderrRd
, &hChildStderrWr
, &saAttr
, 0))
3718 return win32_error("CreatePipe", NULL
);
3719 fSuccess
= DuplicateHandle(GetCurrentProcess(),
3721 GetCurrentProcess(),
3722 &hChildStderrRdDup
, 0,
3723 FALSE
, DUPLICATE_SAME_ACCESS
);
3725 return win32_error("DuplicateHandle", NULL
);
3726 /* Close the inheritable version of ChildStdErr that we're using. */
3727 CloseHandle(hChildStderrRd
);
3732 switch (mode
& (_O_RDONLY
| _O_TEXT
| _O_BINARY
| _O_WRONLY
)) {
3733 case _O_WRONLY
| _O_TEXT
:
3734 /* Case for writing to child Stdin in text mode. */
3735 fd1
= _open_osfhandle((Py_intptr_t
)hChildStdinWrDup
, mode
);
3736 f1
= _fdopen(fd1
, "w");
3737 f
= PyFile_FromFile(f1
, cmdstring
, "w", _PyPclose
);
3738 PyFile_SetBufSize(f
, 0);
3739 /* We don't care about these pipes anymore, so close them. */
3740 CloseHandle(hChildStdoutRdDup
);
3741 CloseHandle(hChildStderrRdDup
);
3744 case _O_RDONLY
| _O_TEXT
:
3745 /* Case for reading from child Stdout in text mode. */
3746 fd1
= _open_osfhandle((Py_intptr_t
)hChildStdoutRdDup
, mode
);
3747 f1
= _fdopen(fd1
, "r");
3748 f
= PyFile_FromFile(f1
, cmdstring
, "r", _PyPclose
);
3749 PyFile_SetBufSize(f
, 0);
3750 /* We don't care about these pipes anymore, so close them. */
3751 CloseHandle(hChildStdinWrDup
);
3752 CloseHandle(hChildStderrRdDup
);
3755 case _O_RDONLY
| _O_BINARY
:
3756 /* Case for readinig from child Stdout in binary mode. */
3757 fd1
= _open_osfhandle((Py_intptr_t
)hChildStdoutRdDup
, mode
);
3758 f1
= _fdopen(fd1
, "rb");
3759 f
= PyFile_FromFile(f1
, cmdstring
, "rb", _PyPclose
);
3760 PyFile_SetBufSize(f
, 0);
3761 /* We don't care about these pipes anymore, so close them. */
3762 CloseHandle(hChildStdinWrDup
);
3763 CloseHandle(hChildStderrRdDup
);
3766 case _O_WRONLY
| _O_BINARY
:
3767 /* Case for writing to child Stdin in binary mode. */
3768 fd1
= _open_osfhandle((Py_intptr_t
)hChildStdinWrDup
, mode
);
3769 f1
= _fdopen(fd1
, "wb");
3770 f
= PyFile_FromFile(f1
, cmdstring
, "wb", _PyPclose
);
3771 PyFile_SetBufSize(f
, 0);
3772 /* We don't care about these pipes anymore, so close them. */
3773 CloseHandle(hChildStdoutRdDup
);
3774 CloseHandle(hChildStderrRdDup
);
3786 if (mode
& _O_TEXT
) {
3794 fd1
= _open_osfhandle((Py_intptr_t
)hChildStdinWrDup
, mode
);
3795 f1
= _fdopen(fd1
, m2
);
3796 fd2
= _open_osfhandle((Py_intptr_t
)hChildStdoutRdDup
, mode
);
3797 f2
= _fdopen(fd2
, m1
);
3798 p1
= PyFile_FromFile(f1
, cmdstring
, m2
, _PyPclose
);
3799 PyFile_SetBufSize(p1
, 0);
3800 p2
= PyFile_FromFile(f2
, cmdstring
, m1
, _PyPclose
);
3801 PyFile_SetBufSize(p2
, 0);
3804 CloseHandle(hChildStderrRdDup
);
3806 f
= PyTuple_Pack(2,p1
,p2
);
3816 PyObject
*p1
, *p2
, *p3
;
3818 if (mode
& _O_TEXT
) {
3826 fd1
= _open_osfhandle((Py_intptr_t
)hChildStdinWrDup
, mode
);
3827 f1
= _fdopen(fd1
, m2
);
3828 fd2
= _open_osfhandle((Py_intptr_t
)hChildStdoutRdDup
, mode
);
3829 f2
= _fdopen(fd2
, m1
);
3830 fd3
= _open_osfhandle((Py_intptr_t
)hChildStderrRdDup
, mode
);
3831 f3
= _fdopen(fd3
, m1
);
3832 p1
= PyFile_FromFile(f1
, cmdstring
, m2
, _PyPclose
);
3833 p2
= PyFile_FromFile(f2
, cmdstring
, m1
, _PyPclose
);
3834 p3
= PyFile_FromFile(f3
, cmdstring
, m1
, _PyPclose
);
3835 PyFile_SetBufSize(p1
, 0);
3836 PyFile_SetBufSize(p2
, 0);
3837 PyFile_SetBufSize(p3
, 0);
3838 f
= PyTuple_Pack(3,p1
,p2
,p3
);
3848 if (!_PyPopenCreateProcess(cmdstring
,
3856 if (!_PyPopenCreateProcess(cmdstring
,
3865 * Insert the files we've created into the process dictionary
3866 * all referencing the list with the process handle and the
3867 * initial number of files (see description below in _PyPclose).
3868 * Since if _PyPclose later tried to wait on a process when all
3869 * handles weren't closed, it could create a deadlock with the
3870 * child, we spend some energy here to try to ensure that we
3871 * either insert all file handles into the dictionary or none
3872 * at all. It's a little clumsy with the various popen modes
3873 * and variable number of files involved.
3875 if (!_PyPopenProcs
) {
3876 _PyPopenProcs
= PyDict_New();
3879 if (_PyPopenProcs
) {
3880 PyObject
*procObj
, *hProcessObj
, *intObj
, *fileObj
[3];
3883 fileObj
[0] = fileObj
[1] = fileObj
[2] = NULL
;
3884 ins_rc
[0] = ins_rc
[1] = ins_rc
[2] = 0;
3886 procObj
= PyList_New(2);
3887 hProcessObj
= PyLong_FromVoidPtr(hProcess
);
3888 intObj
= PyInt_FromLong(file_count
);
3890 if (procObj
&& hProcessObj
&& intObj
) {
3891 PyList_SetItem(procObj
,0,hProcessObj
);
3892 PyList_SetItem(procObj
,1,intObj
);
3894 fileObj
[0] = PyLong_FromVoidPtr(f1
);
3896 ins_rc
[0] = PyDict_SetItem(_PyPopenProcs
,
3900 if (file_count
>= 2) {
3901 fileObj
[1] = PyLong_FromVoidPtr(f2
);
3903 ins_rc
[1] = PyDict_SetItem(_PyPopenProcs
,
3908 if (file_count
>= 3) {
3909 fileObj
[2] = PyLong_FromVoidPtr(f3
);
3911 ins_rc
[2] = PyDict_SetItem(_PyPopenProcs
,
3917 if (ins_rc
[0] < 0 || !fileObj
[0] ||
3918 ins_rc
[1] < 0 || (file_count
> 1 && !fileObj
[1]) ||
3919 ins_rc
[2] < 0 || (file_count
> 2 && !fileObj
[2])) {
3920 /* Something failed - remove any dictionary
3921 * entries that did make it.
3923 if (!ins_rc
[0] && fileObj
[0]) {
3924 PyDict_DelItem(_PyPopenProcs
,
3927 if (!ins_rc
[1] && fileObj
[1]) {
3928 PyDict_DelItem(_PyPopenProcs
,
3931 if (!ins_rc
[2] && fileObj
[2]) {
3932 PyDict_DelItem(_PyPopenProcs
,
3939 * Clean up our localized references for the dictionary keys
3940 * and value since PyDict_SetItem will Py_INCREF any copies
3941 * that got placed in the dictionary.
3943 Py_XDECREF(procObj
);
3944 Py_XDECREF(fileObj
[0]);
3945 Py_XDECREF(fileObj
[1]);
3946 Py_XDECREF(fileObj
[2]);
3949 /* Child is launched. Close the parents copy of those pipe
3950 * handles that only the child should have open. You need to
3951 * make sure that no handles to the write end of the output pipe
3952 * are maintained in this process or else the pipe will not close
3953 * when the child process exits and the ReadFile will hang. */
3955 if (!CloseHandle(hChildStdinRd
))
3956 return win32_error("CloseHandle", NULL
);
3958 if (!CloseHandle(hChildStdoutWr
))
3959 return win32_error("CloseHandle", NULL
);
3961 if ((n
!= 4) && (!CloseHandle(hChildStderrWr
)))
3962 return win32_error("CloseHandle", NULL
);
3968 * Wrapper for fclose() to use for popen* files, so we can retrieve the
3969 * exit code for the child process and return as a result of the close.
3971 * This function uses the _PyPopenProcs dictionary in order to map the
3972 * input file pointer to information about the process that was
3973 * originally created by the popen* call that created the file pointer.
3974 * The dictionary uses the file pointer as a key (with one entry
3975 * inserted for each file returned by the original popen* call) and a
3976 * single list object as the value for all files from a single call.
3977 * The list object contains the Win32 process handle at [0], and a file
3978 * count at [1], which is initialized to the total number of file
3979 * handles using that list.
3981 * This function closes whichever handle it is passed, and decrements
3982 * the file count in the dictionary for the process handle pointed to
3983 * by this file. On the last close (when the file count reaches zero),
3984 * this function will wait for the child process and then return its
3985 * exit code as the result of the close() operation. This permits the
3986 * files to be closed in any order - it is always the close() of the
3987 * final handle that will return the exit code.
3989 * NOTE: This function is currently called with the GIL released.
3990 * hence we use the GILState API to manage our state.
3993 static int _PyPclose(FILE *file
)
3998 PyObject
*procObj
, *hProcessObj
, *intObj
, *fileObj
;
4001 PyGILState_STATE state
;
4004 /* Close the file handle first, to ensure it can't block the
4005 * child from exiting if it's the last handle.
4007 result
= fclose(file
);
4009 state
= PyGILState_Ensure();
4011 if (_PyPopenProcs
) {
4012 if ((fileObj
= PyLong_FromVoidPtr(file
)) != NULL
&&
4013 (procObj
= PyDict_GetItem(_PyPopenProcs
,
4014 fileObj
)) != NULL
&&
4015 (hProcessObj
= PyList_GetItem(procObj
,0)) != NULL
&&
4016 (intObj
= PyList_GetItem(procObj
,1)) != NULL
) {
4018 hProcess
= PyLong_AsVoidPtr(hProcessObj
);
4019 file_count
= PyInt_AsLong(intObj
);
4021 if (file_count
> 1) {
4022 /* Still other files referencing process */
4024 PyList_SetItem(procObj
,1,
4025 PyInt_FromLong(file_count
));
4027 /* Last file for this process */
4028 if (result
!= EOF
&&
4029 WaitForSingleObject(hProcess
, INFINITE
) != WAIT_FAILED
&&
4030 GetExitCodeProcess(hProcess
, &exit_code
)) {
4031 /* Possible truncation here in 16-bit environments, but
4032 * real exit codes are just the lower byte in any event.
4036 /* Indicate failure - this will cause the file object
4037 * to raise an I/O error and translate the last Win32
4038 * error code from errno. We do have a problem with
4039 * last errors that overlap the normal errno table,
4040 * but that's a consistent problem with the file object.
4042 if (result
!= EOF
) {
4043 /* If the error wasn't from the fclose(), then
4044 * set errno for the file object error handling.
4046 errno
= GetLastError();
4051 /* Free up the native handle at this point */
4052 CloseHandle(hProcess
);
4055 /* Remove this file pointer from dictionary */
4056 PyDict_DelItem(_PyPopenProcs
, fileObj
);
4058 if (PyDict_Size(_PyPopenProcs
) == 0) {
4059 Py_DECREF(_PyPopenProcs
);
4060 _PyPopenProcs
= NULL
;
4063 } /* if object retrieval ok */
4065 Py_XDECREF(fileObj
);
4066 } /* if _PyPopenProcs */
4069 PyGILState_Release(state
);
4074 #else /* which OS? */
4076 posix_popen(PyObject
*self
, PyObject
*args
)
4083 if (!PyArg_ParseTuple(args
, "s|si:popen", &name
, &mode
, &bufsize
))
4085 /* Strip mode of binary or text modifiers */
4086 if (strcmp(mode
, "rb") == 0 || strcmp(mode
, "rt") == 0)
4088 else if (strcmp(mode
, "wb") == 0 || strcmp(mode
, "wt") == 0)
4090 Py_BEGIN_ALLOW_THREADS
4091 fp
= popen(name
, mode
);
4092 Py_END_ALLOW_THREADS
4094 return posix_error();
4095 f
= PyFile_FromFile(fp
, name
, mode
, pclose
);
4097 PyFile_SetBufSize(f
, bufsize
);
4101 #endif /* PYOS_??? */
4102 #endif /* HAVE_POPEN */
4106 PyDoc_STRVAR(posix_setuid__doc__
,
4108 Set the current process's user id.");
4111 posix_setuid(PyObject
*self
, PyObject
*args
)
4115 if (!PyArg_ParseTuple(args
, "l:setuid", &uid_arg
))
4118 if (uid
!= uid_arg
) {
4119 PyErr_SetString(PyExc_OverflowError
, "user id too big");
4122 if (setuid(uid
) < 0)
4123 return posix_error();
4127 #endif /* HAVE_SETUID */
4131 PyDoc_STRVAR(posix_seteuid__doc__
,
4133 Set the current process's effective user id.");
4136 posix_seteuid (PyObject
*self
, PyObject
*args
)
4140 if (!PyArg_ParseTuple(args
, "l", &euid_arg
))
4143 if (euid
!= euid_arg
) {
4144 PyErr_SetString(PyExc_OverflowError
, "user id too big");
4147 if (seteuid(euid
) < 0) {
4148 return posix_error();
4154 #endif /* HAVE_SETEUID */
4157 PyDoc_STRVAR(posix_setegid__doc__
,
4159 Set the current process's effective group id.");
4162 posix_setegid (PyObject
*self
, PyObject
*args
)
4166 if (!PyArg_ParseTuple(args
, "l", &egid_arg
))
4169 if (egid
!= egid_arg
) {
4170 PyErr_SetString(PyExc_OverflowError
, "group id too big");
4173 if (setegid(egid
) < 0) {
4174 return posix_error();
4180 #endif /* HAVE_SETEGID */
4182 #ifdef HAVE_SETREUID
4183 PyDoc_STRVAR(posix_setreuid__doc__
,
4184 "setreuid(ruid, euid)\n\n\
4185 Set the current process's real and effective user ids.");
4188 posix_setreuid (PyObject
*self
, PyObject
*args
)
4190 long ruid_arg
, euid_arg
;
4192 if (!PyArg_ParseTuple(args
, "ll", &ruid_arg
, &euid_arg
))
4195 ruid
= (uid_t
)-1; /* let the compiler choose how -1 fits */
4197 ruid
= ruid_arg
; /* otherwise, assign from our long */
4202 if ((euid_arg
!= -1 && euid
!= euid_arg
) ||
4203 (ruid_arg
!= -1 && ruid
!= ruid_arg
)) {
4204 PyErr_SetString(PyExc_OverflowError
, "user id too big");
4207 if (setreuid(ruid
, euid
) < 0) {
4208 return posix_error();
4214 #endif /* HAVE_SETREUID */
4216 #ifdef HAVE_SETREGID
4217 PyDoc_STRVAR(posix_setregid__doc__
,
4218 "setregid(rgid, egid)\n\n\
4219 Set the current process's real and effective group ids.");
4222 posix_setregid (PyObject
*self
, PyObject
*args
)
4224 long rgid_arg
, egid_arg
;
4226 if (!PyArg_ParseTuple(args
, "ll", &rgid_arg
, &egid_arg
))
4229 rgid
= (gid_t
)-1; /* let the compiler choose how -1 fits */
4231 rgid
= rgid_arg
; /* otherwise, assign from our long */
4236 if ((egid_arg
!= -1 && egid
!= egid_arg
) ||
4237 (rgid_arg
!= -1 && rgid
!= rgid_arg
)) {
4238 PyErr_SetString(PyExc_OverflowError
, "group id too big");
4241 if (setregid(rgid
, egid
) < 0) {
4242 return posix_error();
4248 #endif /* HAVE_SETREGID */
4251 PyDoc_STRVAR(posix_setgid__doc__
,
4253 Set the current process's group id.");
4256 posix_setgid(PyObject
*self
, PyObject
*args
)
4260 if (!PyArg_ParseTuple(args
, "l:setgid", &gid_arg
))
4263 if (gid
!= gid_arg
) {
4264 PyErr_SetString(PyExc_OverflowError
, "group id too big");
4267 if (setgid(gid
) < 0)
4268 return posix_error();
4272 #endif /* HAVE_SETGID */
4274 #ifdef HAVE_SETGROUPS
4275 PyDoc_STRVAR(posix_setgroups__doc__
,
4276 "setgroups(list)\n\n\
4277 Set the groups of the current process to list.");
4280 posix_setgroups(PyObject
*self
, PyObject
*groups
)
4283 gid_t grouplist
[MAX_GROUPS
];
4285 if (!PySequence_Check(groups
)) {
4286 PyErr_SetString(PyExc_TypeError
, "setgroups argument must be a sequence");
4289 len
= PySequence_Size(groups
);
4290 if (len
> MAX_GROUPS
) {
4291 PyErr_SetString(PyExc_ValueError
, "too many groups");
4294 for(i
= 0; i
< len
; i
++) {
4296 elem
= PySequence_GetItem(groups
, i
);
4299 if (!PyInt_Check(elem
)) {
4300 if (!PyLong_Check(elem
)) {
4301 PyErr_SetString(PyExc_TypeError
,
4302 "groups must be integers");
4306 unsigned long x
= PyLong_AsUnsignedLong(elem
);
4307 if (PyErr_Occurred()) {
4308 PyErr_SetString(PyExc_TypeError
,
4309 "group id too big");
4314 /* read back to see if it fits in gid_t */
4315 if (grouplist
[i
] != x
) {
4316 PyErr_SetString(PyExc_TypeError
,
4317 "group id too big");
4323 long x
= PyInt_AsLong(elem
);
4325 if (grouplist
[i
] != x
) {
4326 PyErr_SetString(PyExc_TypeError
,
4327 "group id too big");
4335 if (setgroups(len
, grouplist
) < 0)
4336 return posix_error();
4340 #endif /* HAVE_SETGROUPS */
4342 #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
4344 wait_helper(pid_t pid
, int status
, struct rusage
*ru
)
4347 static PyObject
*struct_rusage
;
4350 return posix_error();
4352 if (struct_rusage
== NULL
) {
4353 PyObject
*m
= PyImport_ImportModuleNoBlock("resource");
4356 struct_rusage
= PyObject_GetAttrString(m
, "struct_rusage");
4358 if (struct_rusage
== NULL
)
4362 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
4363 result
= PyStructSequence_New((PyTypeObject
*) struct_rusage
);
4368 #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
4371 PyStructSequence_SET_ITEM(result
, 0,
4372 PyFloat_FromDouble(doubletime(ru
->ru_utime
)));
4373 PyStructSequence_SET_ITEM(result
, 1,
4374 PyFloat_FromDouble(doubletime(ru
->ru_stime
)));
4375 #define SET_INT(result, index, value)\
4376 PyStructSequence_SET_ITEM(result, index, PyInt_FromLong(value))
4377 SET_INT(result
, 2, ru
->ru_maxrss
);
4378 SET_INT(result
, 3, ru
->ru_ixrss
);
4379 SET_INT(result
, 4, ru
->ru_idrss
);
4380 SET_INT(result
, 5, ru
->ru_isrss
);
4381 SET_INT(result
, 6, ru
->ru_minflt
);
4382 SET_INT(result
, 7, ru
->ru_majflt
);
4383 SET_INT(result
, 8, ru
->ru_nswap
);
4384 SET_INT(result
, 9, ru
->ru_inblock
);
4385 SET_INT(result
, 10, ru
->ru_oublock
);
4386 SET_INT(result
, 11, ru
->ru_msgsnd
);
4387 SET_INT(result
, 12, ru
->ru_msgrcv
);
4388 SET_INT(result
, 13, ru
->ru_nsignals
);
4389 SET_INT(result
, 14, ru
->ru_nvcsw
);
4390 SET_INT(result
, 15, ru
->ru_nivcsw
);
4393 if (PyErr_Occurred()) {
4398 return Py_BuildValue("NiN", PyLong_FromPid(pid
), status
, result
);
4400 #endif /* HAVE_WAIT3 || HAVE_WAIT4 */
4403 PyDoc_STRVAR(posix_wait3__doc__
,
4404 "wait3(options) -> (pid, status, rusage)\n\n\
4405 Wait for completion of a child process.");
4408 posix_wait3(PyObject
*self
, PyObject
*args
)
4414 WAIT_STATUS_INT(status
) = 0;
4416 if (!PyArg_ParseTuple(args
, "i:wait3", &options
))
4419 Py_BEGIN_ALLOW_THREADS
4420 pid
= wait3(&status
, options
, &ru
);
4421 Py_END_ALLOW_THREADS
4423 return wait_helper(pid
, WAIT_STATUS_INT(status
), &ru
);
4425 #endif /* HAVE_WAIT3 */
4428 PyDoc_STRVAR(posix_wait4__doc__
,
4429 "wait4(pid, options) -> (pid, status, rusage)\n\n\
4430 Wait for completion of a given child process.");
4433 posix_wait4(PyObject
*self
, PyObject
*args
)
4439 WAIT_STATUS_INT(status
) = 0;
4441 if (!PyArg_ParseTuple(args
, PARSE_PID
"i:wait4", &pid
, &options
))
4444 Py_BEGIN_ALLOW_THREADS
4445 pid
= wait4(pid
, &status
, options
, &ru
);
4446 Py_END_ALLOW_THREADS
4448 return wait_helper(pid
, WAIT_STATUS_INT(status
), &ru
);
4450 #endif /* HAVE_WAIT4 */
4453 PyDoc_STRVAR(posix_waitpid__doc__
,
4454 "waitpid(pid, options) -> (pid, status)\n\n\
4455 Wait for completion of a given child process.");
4458 posix_waitpid(PyObject
*self
, PyObject
*args
)
4463 WAIT_STATUS_INT(status
) = 0;
4465 if (!PyArg_ParseTuple(args
, PARSE_PID
"i:waitpid", &pid
, &options
))
4467 Py_BEGIN_ALLOW_THREADS
4468 pid
= waitpid(pid
, &status
, options
);
4469 Py_END_ALLOW_THREADS
4471 return posix_error();
4473 return Py_BuildValue("Ni", PyLong_FromPid(pid
), WAIT_STATUS_INT(status
));
4476 #elif defined(HAVE_CWAIT)
4478 /* MS C has a variant of waitpid() that's usable for most purposes. */
4479 PyDoc_STRVAR(posix_waitpid__doc__
,
4480 "waitpid(pid, options) -> (pid, status << 8)\n\n"
4481 "Wait for completion of a given process. options is ignored on Windows.");
4484 posix_waitpid(PyObject
*self
, PyObject
*args
)
4487 int status
, options
;
4489 if (!PyArg_ParseTuple(args
, PARSE_PID
"i:waitpid", &pid
, &options
))
4491 Py_BEGIN_ALLOW_THREADS
4492 pid
= _cwait(&status
, pid
, options
);
4493 Py_END_ALLOW_THREADS
4495 return posix_error();
4497 /* shift the status left a byte so this is more like the POSIX waitpid */
4498 return Py_BuildValue("Ni", PyLong_FromPid(pid
), status
<< 8);
4500 #endif /* HAVE_WAITPID || HAVE_CWAIT */
4503 PyDoc_STRVAR(posix_wait__doc__
,
4504 "wait() -> (pid, status)\n\n\
4505 Wait for completion of a child process.");
4508 posix_wait(PyObject
*self
, PyObject
*noargs
)
4512 WAIT_STATUS_INT(status
) = 0;
4514 Py_BEGIN_ALLOW_THREADS
4515 pid
= wait(&status
);
4516 Py_END_ALLOW_THREADS
4518 return posix_error();
4520 return Py_BuildValue("Ni", PyLong_FromPid(pid
), WAIT_STATUS_INT(status
));
4525 PyDoc_STRVAR(posix_lstat__doc__
,
4526 "lstat(path) -> stat result\n\n\
4527 Like stat(path), but do not follow symbolic links.");
4530 posix_lstat(PyObject
*self
, PyObject
*args
)
4533 return posix_do_stat(self
, args
, "et:lstat", lstat
, NULL
, NULL
);
4534 #else /* !HAVE_LSTAT */
4535 return posix_do_stat(self
, args
, "et:lstat", STAT
, NULL
, NULL
);
4536 #endif /* !HAVE_LSTAT */
4540 #ifdef HAVE_READLINK
4541 PyDoc_STRVAR(posix_readlink__doc__
,
4542 "readlink(path) -> path\n\n\
4543 Return a string representing the path to which the symbolic link points.");
4546 posix_readlink(PyObject
*self
, PyObject
*args
)
4549 char buf
[MAXPATHLEN
];
4552 #ifdef Py_USING_UNICODE
4553 int arg_is_unicode
= 0;
4556 if (!PyArg_ParseTuple(args
, "et:readlink",
4557 Py_FileSystemDefaultEncoding
, &path
))
4559 #ifdef Py_USING_UNICODE
4560 v
= PySequence_GetItem(args
, 0);
4566 if (PyUnicode_Check(v
)) {
4572 Py_BEGIN_ALLOW_THREADS
4573 n
= readlink(path
, buf
, (int) sizeof buf
);
4574 Py_END_ALLOW_THREADS
4576 return posix_error_with_allocated_filename(path
);
4579 v
= PyString_FromStringAndSize(buf
, n
);
4580 #ifdef Py_USING_UNICODE
4581 if (arg_is_unicode
) {
4584 w
= PyUnicode_FromEncodedObject(v
,
4585 Py_FileSystemDefaultEncoding
,
4592 /* fall back to the original byte string, as
4593 discussed in patch #683592 */
4600 #endif /* HAVE_READLINK */
4604 PyDoc_STRVAR(posix_symlink__doc__
,
4605 "symlink(src, dst)\n\n\
4606 Create a symbolic link pointing to src named dst.");
4609 posix_symlink(PyObject
*self
, PyObject
*args
)
4611 return posix_2str(args
, "etet:symlink", symlink
);
4613 #endif /* HAVE_SYMLINK */
4617 #if defined(PYCC_VACPP) && defined(PYOS_OS2)
4623 Py_BEGIN_ALLOW_THREADS
4624 DosQuerySysInfo(QSV_MS_COUNT
, QSV_MS_COUNT
, &value
, sizeof(value
));
4625 Py_END_ALLOW_THREADS
4631 posix_times(PyObject
*self
, PyObject
*noargs
)
4633 /* Currently Only Uptime is Provided -- Others Later */
4634 return Py_BuildValue("ddddd",
4635 (double)0 /* t.tms_utime / HZ */,
4636 (double)0 /* t.tms_stime / HZ */,
4637 (double)0 /* t.tms_cutime / HZ */,
4638 (double)0 /* t.tms_cstime / HZ */,
4639 (double)system_uptime() / 1000);
4642 #define NEED_TICKS_PER_SECOND
4643 static long ticks_per_second
= -1;
4645 posix_times(PyObject
*self
, PyObject
*noargs
)
4651 if (c
== (clock_t) -1)
4652 return posix_error();
4653 return Py_BuildValue("ddddd",
4654 (double)t
.tms_utime
/ ticks_per_second
,
4655 (double)t
.tms_stime
/ ticks_per_second
,
4656 (double)t
.tms_cutime
/ ticks_per_second
,
4657 (double)t
.tms_cstime
/ ticks_per_second
,
4658 (double)c
/ ticks_per_second
);
4660 #endif /* not OS2 */
4661 #endif /* HAVE_TIMES */
4665 PyDoc_STRVAR(posix_times__doc__
,
4666 "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
4667 Return a tuple of floating point numbers indicating process times.");
4672 PyDoc_STRVAR(posix_getsid__doc__
,
4673 "getsid(pid) -> sid\n\n\
4674 Call the system call getsid().");
4677 posix_getsid(PyObject
*self
, PyObject
*args
)
4681 if (!PyArg_ParseTuple(args
, PARSE_PID
":getsid", &pid
))
4685 return posix_error();
4686 return PyInt_FromLong((long)sid
);
4688 #endif /* HAVE_GETSID */
4692 PyDoc_STRVAR(posix_setsid__doc__
,
4694 Call the system call setsid().");
4697 posix_setsid(PyObject
*self
, PyObject
*noargs
)
4700 return posix_error();
4704 #endif /* HAVE_SETSID */
4707 PyDoc_STRVAR(posix_setpgid__doc__
,
4708 "setpgid(pid, pgrp)\n\n\
4709 Call the system call setpgid().");
4712 posix_setpgid(PyObject
*self
, PyObject
*args
)
4716 if (!PyArg_ParseTuple(args
, PARSE_PID
"i:setpgid", &pid
, &pgrp
))
4718 if (setpgid(pid
, pgrp
) < 0)
4719 return posix_error();
4723 #endif /* HAVE_SETPGID */
4726 #ifdef HAVE_TCGETPGRP
4727 PyDoc_STRVAR(posix_tcgetpgrp__doc__
,
4728 "tcgetpgrp(fd) -> pgid\n\n\
4729 Return the process group associated with the terminal given by a fd.");
4732 posix_tcgetpgrp(PyObject
*self
, PyObject
*args
)
4736 if (!PyArg_ParseTuple(args
, "i:tcgetpgrp", &fd
))
4738 pgid
= tcgetpgrp(fd
);
4740 return posix_error();
4741 return PyLong_FromPid(pgid
);
4743 #endif /* HAVE_TCGETPGRP */
4746 #ifdef HAVE_TCSETPGRP
4747 PyDoc_STRVAR(posix_tcsetpgrp__doc__
,
4748 "tcsetpgrp(fd, pgid)\n\n\
4749 Set the process group associated with the terminal given by a fd.");
4752 posix_tcsetpgrp(PyObject
*self
, PyObject
*args
)
4756 if (!PyArg_ParseTuple(args
, "i" PARSE_PID
":tcsetpgrp", &fd
, &pgid
))
4758 if (tcsetpgrp(fd
, pgid
) < 0)
4759 return posix_error();
4763 #endif /* HAVE_TCSETPGRP */
4765 /* Functions acting on file descriptors */
4767 PyDoc_STRVAR(posix_open__doc__
,
4768 "open(filename, flag [, mode=0777]) -> fd\n\n\
4769 Open a file (for low level IO).");
4772 posix_open(PyObject
*self
, PyObject
*args
)
4779 if (!PyArg_ParseTuple(args
, "eti|i",
4780 Py_FileSystemDefaultEncoding
, &file
,
4784 Py_BEGIN_ALLOW_THREADS
4785 fd
= open(file
, flag
, mode
);
4786 Py_END_ALLOW_THREADS
4788 return posix_error_with_allocated_filename(file
);
4790 return PyInt_FromLong((long)fd
);
4794 PyDoc_STRVAR(posix_close__doc__
,
4796 Close a file descriptor (for low level IO).");
4799 posix_close(PyObject
*self
, PyObject
*args
)
4802 if (!PyArg_ParseTuple(args
, "i:close", &fd
))
4804 if (!_PyVerify_fd(fd
))
4805 return posix_error();
4806 Py_BEGIN_ALLOW_THREADS
4808 Py_END_ALLOW_THREADS
4810 return posix_error();
4816 PyDoc_STRVAR(posix_closerange__doc__
,
4817 "closerange(fd_low, fd_high)\n\n\
4818 Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
4821 posix_closerange(PyObject
*self
, PyObject
*args
)
4823 int fd_from
, fd_to
, i
;
4824 if (!PyArg_ParseTuple(args
, "ii:closerange", &fd_from
, &fd_to
))
4826 Py_BEGIN_ALLOW_THREADS
4827 for (i
= fd_from
; i
< fd_to
; i
++)
4828 if (_PyVerify_fd(i
))
4830 Py_END_ALLOW_THREADS
4835 PyDoc_STRVAR(posix_dup__doc__
,
4836 "dup(fd) -> fd2\n\n\
4837 Return a duplicate of a file descriptor.");
4840 posix_dup(PyObject
*self
, PyObject
*args
)
4843 if (!PyArg_ParseTuple(args
, "i:dup", &fd
))
4845 if (!_PyVerify_fd(fd
))
4846 return posix_error();
4847 Py_BEGIN_ALLOW_THREADS
4849 Py_END_ALLOW_THREADS
4851 return posix_error();
4852 return PyInt_FromLong((long)fd
);
4856 PyDoc_STRVAR(posix_dup2__doc__
,
4857 "dup2(old_fd, new_fd)\n\n\
4858 Duplicate file descriptor.");
4861 posix_dup2(PyObject
*self
, PyObject
*args
)
4864 if (!PyArg_ParseTuple(args
, "ii:dup2", &fd
, &fd2
))
4866 if (!_PyVerify_fd_dup2(fd
, fd2
))
4867 return posix_error();
4868 Py_BEGIN_ALLOW_THREADS
4869 res
= dup2(fd
, fd2
);
4870 Py_END_ALLOW_THREADS
4872 return posix_error();
4878 PyDoc_STRVAR(posix_lseek__doc__
,
4879 "lseek(fd, pos, how) -> newpos\n\n\
4880 Set the current position of a file descriptor.");
4883 posix_lseek(PyObject
*self
, PyObject
*args
)
4888 if (!PyArg_ParseTuple(args
, "iOi:lseek", &fd
, &posobj
, &how
))
4891 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
4893 case 0: how
= SEEK_SET
; break;
4894 case 1: how
= SEEK_CUR
; break;
4895 case 2: how
= SEEK_END
; break;
4897 #endif /* SEEK_END */
4899 #if !defined(HAVE_LARGEFILE_SUPPORT)
4900 pos
= PyInt_AsLong(posobj
);
4902 pos
= PyLong_Check(posobj
) ?
4903 PyLong_AsLongLong(posobj
) : PyInt_AsLong(posobj
);
4905 if (PyErr_Occurred())
4908 if (!_PyVerify_fd(fd
))
4909 return posix_error();
4910 Py_BEGIN_ALLOW_THREADS
4911 res
= lseek(fd
, pos
, how
);
4912 Py_END_ALLOW_THREADS
4914 return posix_error();
4916 #if !defined(HAVE_LARGEFILE_SUPPORT)
4917 return PyInt_FromLong(res
);
4919 return PyLong_FromLongLong(res
);
4924 PyDoc_STRVAR(posix_read__doc__
,
4925 "read(fd, buffersize) -> string\n\n\
4926 Read a file descriptor.");
4929 posix_read(PyObject
*self
, PyObject
*args
)
4933 if (!PyArg_ParseTuple(args
, "ii:read", &fd
, &size
))
4937 return posix_error();
4939 buffer
= PyString_FromStringAndSize((char *)NULL
, size
);
4942 if (!_PyVerify_fd(fd
)) {
4944 return posix_error();
4946 Py_BEGIN_ALLOW_THREADS
4947 n
= read(fd
, PyString_AsString(buffer
), size
);
4948 Py_END_ALLOW_THREADS
4951 return posix_error();
4954 _PyString_Resize(&buffer
, n
);
4959 PyDoc_STRVAR(posix_write__doc__
,
4960 "write(fd, string) -> byteswritten\n\n\
4961 Write a string to a file descriptor.");
4964 posix_write(PyObject
*self
, PyObject
*args
)
4970 if (!PyArg_ParseTuple(args
, "is*:write", &fd
, &pbuf
))
4972 if (!_PyVerify_fd(fd
)) {
4973 PyBuffer_Release(&pbuf
);
4974 return posix_error();
4976 Py_BEGIN_ALLOW_THREADS
4977 size
= write(fd
, pbuf
.buf
, (size_t)pbuf
.len
);
4978 Py_END_ALLOW_THREADS
4979 PyBuffer_Release(&pbuf
);
4981 return posix_error();
4982 return PyInt_FromSsize_t(size
);
4986 PyDoc_STRVAR(posix_fstat__doc__
,
4987 "fstat(fd) -> stat result\n\n\
4988 Like stat(), but for an open file descriptor.");
4991 posix_fstat(PyObject
*self
, PyObject
*args
)
4996 if (!PyArg_ParseTuple(args
, "i:fstat", &fd
))
4998 if (!_PyVerify_fd(fd
))
4999 return posix_error();
5000 Py_BEGIN_ALLOW_THREADS
5001 res
= FSTAT(fd
, &st
);
5002 Py_END_ALLOW_THREADS
5004 return posix_error();
5007 return _pystat_fromstructstat(&st
);
5011 PyDoc_STRVAR(posix_fdopen__doc__
,
5012 "fdopen(fd [, mode='r' [, bufsize]]) -> file_object\n\n\
5013 Return an open file object connected to a file descriptor.");
5016 posix_fdopen(PyObject
*self
, PyObject
*args
)
5019 char *orgmode
= "r";
5024 if (!PyArg_ParseTuple(args
, "i|si", &fd
, &orgmode
, &bufsize
))
5027 /* Sanitize mode. See fileobject.c */
5028 mode
= PyMem_MALLOC(strlen(orgmode
)+3);
5033 strcpy(mode
, orgmode
);
5034 if (_PyFile_SanitizeMode(mode
)) {
5038 if (!_PyVerify_fd(fd
))
5039 return posix_error();
5040 Py_BEGIN_ALLOW_THREADS
5041 #if !defined(MS_WINDOWS) && defined(HAVE_FCNTL_H)
5042 if (mode
[0] == 'a') {
5043 /* try to make sure the O_APPEND flag is set */
5045 flags
= fcntl(fd
, F_GETFL
);
5047 fcntl(fd
, F_SETFL
, flags
| O_APPEND
);
5048 fp
= fdopen(fd
, mode
);
5049 if (fp
== NULL
&& flags
!= -1)
5050 /* restore old mode if fdopen failed */
5051 fcntl(fd
, F_SETFL
, flags
);
5053 fp
= fdopen(fd
, mode
);
5056 fp
= fdopen(fd
, mode
);
5058 Py_END_ALLOW_THREADS
5061 return posix_error();
5062 f
= PyFile_FromFile(fp
, "<fdopen>", orgmode
, fclose
);
5064 PyFile_SetBufSize(f
, bufsize
);
5068 PyDoc_STRVAR(posix_isatty__doc__
,
5069 "isatty(fd) -> bool\n\n\
5070 Return True if the file descriptor 'fd' is an open file descriptor\n\
5071 connected to the slave end of a terminal.");
5074 posix_isatty(PyObject
*self
, PyObject
*args
)
5077 if (!PyArg_ParseTuple(args
, "i:isatty", &fd
))
5079 if (!_PyVerify_fd(fd
))
5080 return PyBool_FromLong(0);
5081 return PyBool_FromLong(isatty(fd
));
5085 PyDoc_STRVAR(posix_pipe__doc__
,
5086 "pipe() -> (read_end, write_end)\n\n\
5090 posix_pipe(PyObject
*self
, PyObject
*noargs
)
5092 #if defined(PYOS_OS2)
5096 Py_BEGIN_ALLOW_THREADS
5097 rc
= DosCreatePipe( &read
, &write
, 4096);
5098 Py_END_ALLOW_THREADS
5100 return os2_error(rc
);
5102 return Py_BuildValue("(ii)", read
, write
);
5104 #if !defined(MS_WINDOWS)
5107 Py_BEGIN_ALLOW_THREADS
5109 Py_END_ALLOW_THREADS
5111 return posix_error();
5112 return Py_BuildValue("(ii)", fds
[0], fds
[1]);
5113 #else /* MS_WINDOWS */
5115 int read_fd
, write_fd
;
5117 Py_BEGIN_ALLOW_THREADS
5118 ok
= CreatePipe(&read
, &write
, NULL
, 0);
5119 Py_END_ALLOW_THREADS
5121 return win32_error("CreatePipe", NULL
);
5122 read_fd
= _open_osfhandle((Py_intptr_t
)read
, 0);
5123 write_fd
= _open_osfhandle((Py_intptr_t
)write
, 1);
5124 return Py_BuildValue("(ii)", read_fd
, write_fd
);
5125 #endif /* MS_WINDOWS */
5128 #endif /* HAVE_PIPE */
5132 PyDoc_STRVAR(posix_mkfifo__doc__
,
5133 "mkfifo(filename [, mode=0666])\n\n\
5134 Create a FIFO (a POSIX named pipe).");
5137 posix_mkfifo(PyObject
*self
, PyObject
*args
)
5142 if (!PyArg_ParseTuple(args
, "s|i:mkfifo", &filename
, &mode
))
5144 Py_BEGIN_ALLOW_THREADS
5145 res
= mkfifo(filename
, mode
);
5146 Py_END_ALLOW_THREADS
5148 return posix_error();
5155 #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
5156 PyDoc_STRVAR(posix_mknod__doc__
,
5157 "mknod(filename [, mode=0600, device])\n\n\
5158 Create a filesystem node (file, device special file or named pipe)\n\
5159 named filename. mode specifies both the permissions to use and the\n\
5160 type of node to be created, being combined (bitwise OR) with one of\n\
5161 S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
5162 device defines the newly created device special file (probably using\n\
5163 os.makedev()), otherwise it is ignored.");
5167 posix_mknod(PyObject
*self
, PyObject
*args
)
5173 if (!PyArg_ParseTuple(args
, "s|ii:mknod", &filename
, &mode
, &device
))
5175 Py_BEGIN_ALLOW_THREADS
5176 res
= mknod(filename
, mode
, device
);
5177 Py_END_ALLOW_THREADS
5179 return posix_error();
5185 #ifdef HAVE_DEVICE_MACROS
5186 PyDoc_STRVAR(posix_major__doc__
,
5187 "major(device) -> major number\n\
5188 Extracts a device major number from a raw device number.");
5191 posix_major(PyObject
*self
, PyObject
*args
)
5194 if (!PyArg_ParseTuple(args
, "i:major", &device
))
5196 return PyInt_FromLong((long)major(device
));
5199 PyDoc_STRVAR(posix_minor__doc__
,
5200 "minor(device) -> minor number\n\
5201 Extracts a device minor number from a raw device number.");
5204 posix_minor(PyObject
*self
, PyObject
*args
)
5207 if (!PyArg_ParseTuple(args
, "i:minor", &device
))
5209 return PyInt_FromLong((long)minor(device
));
5212 PyDoc_STRVAR(posix_makedev__doc__
,
5213 "makedev(major, minor) -> device number\n\
5214 Composes a raw device number from the major and minor device numbers.");
5217 posix_makedev(PyObject
*self
, PyObject
*args
)
5220 if (!PyArg_ParseTuple(args
, "ii:makedev", &major
, &minor
))
5222 return PyInt_FromLong((long)makedev(major
, minor
));
5224 #endif /* device macros */
5227 #ifdef HAVE_FTRUNCATE
5228 PyDoc_STRVAR(posix_ftruncate__doc__
,
5229 "ftruncate(fd, length)\n\n\
5230 Truncate a file to a specified length.");
5233 posix_ftruncate(PyObject
*self
, PyObject
*args
)
5240 if (!PyArg_ParseTuple(args
, "iO:ftruncate", &fd
, &lenobj
))
5243 #if !defined(HAVE_LARGEFILE_SUPPORT)
5244 length
= PyInt_AsLong(lenobj
);
5246 length
= PyLong_Check(lenobj
) ?
5247 PyLong_AsLongLong(lenobj
) : PyInt_AsLong(lenobj
);
5249 if (PyErr_Occurred())
5252 Py_BEGIN_ALLOW_THREADS
5253 res
= ftruncate(fd
, length
);
5254 Py_END_ALLOW_THREADS
5256 return posix_error();
5263 PyDoc_STRVAR(posix_putenv__doc__
,
5264 "putenv(key, value)\n\n\
5265 Change or add an environment variable.");
5267 /* Save putenv() parameters as values here, so we can collect them when they
5268 * get re-set with another call for the same key. */
5269 static PyObject
*posix_putenv_garbage
;
5272 posix_putenv(PyObject
*self
, PyObject
*args
)
5279 if (!PyArg_ParseTuple(args
, "ss:putenv", &s1
, &s2
))
5282 #if defined(PYOS_OS2)
5283 if (stricmp(s1
, "BEGINLIBPATH") == 0) {
5286 rc
= DosSetExtLIBPATH(s2
, BEGIN_LIBPATH
);
5288 return os2_error(rc
);
5290 } else if (stricmp(s1
, "ENDLIBPATH") == 0) {
5293 rc
= DosSetExtLIBPATH(s2
, END_LIBPATH
);
5295 return os2_error(rc
);
5299 /* XXX This can leak memory -- not easy to fix :-( */
5300 len
= strlen(s1
) + strlen(s2
) + 2;
5301 /* len includes space for a trailing \0; the size arg to
5302 PyString_FromStringAndSize does not count that */
5303 newstr
= PyString_FromStringAndSize(NULL
, (int)len
- 1);
5305 return PyErr_NoMemory();
5306 newenv
= PyString_AS_STRING(newstr
);
5307 PyOS_snprintf(newenv
, len
, "%s=%s", s1
, s2
);
5308 if (putenv(newenv
)) {
5313 /* Install the first arg and newstr in posix_putenv_garbage;
5314 * this will cause previous value to be collected. This has to
5315 * happen after the real putenv() call because the old value
5316 * was still accessible until then. */
5317 if (PyDict_SetItem(posix_putenv_garbage
,
5318 PyTuple_GET_ITEM(args
, 0), newstr
)) {
5319 /* really not much we can do; just leak */
5326 #if defined(PYOS_OS2)
5334 #ifdef HAVE_UNSETENV
5335 PyDoc_STRVAR(posix_unsetenv__doc__
,
5337 Delete an environment variable.");
5340 posix_unsetenv(PyObject
*self
, PyObject
*args
)
5344 if (!PyArg_ParseTuple(args
, "s:unsetenv", &s1
))
5349 /* Remove the key from posix_putenv_garbage;
5350 * this will cause it to be collected. This has to
5351 * happen after the real unsetenv() call because the
5352 * old value was still accessible until then.
5354 if (PyDict_DelItem(posix_putenv_garbage
,
5355 PyTuple_GET_ITEM(args
, 0))) {
5356 /* really not much we can do; just leak */
5363 #endif /* unsetenv */
5365 PyDoc_STRVAR(posix_strerror__doc__
,
5366 "strerror(code) -> string\n\n\
5367 Translate an error code to a message string.");
5370 posix_strerror(PyObject
*self
, PyObject
*args
)
5374 if (!PyArg_ParseTuple(args
, "i:strerror", &code
))
5376 message
= strerror(code
);
5377 if (message
== NULL
) {
5378 PyErr_SetString(PyExc_ValueError
,
5379 "strerror() argument out of range");
5382 return PyString_FromString(message
);
5386 #ifdef HAVE_SYS_WAIT_H
5389 PyDoc_STRVAR(posix_WCOREDUMP__doc__
,
5390 "WCOREDUMP(status) -> bool\n\n\
5391 Return True if the process returning 'status' was dumped to a core file.");
5394 posix_WCOREDUMP(PyObject
*self
, PyObject
*args
)
5397 WAIT_STATUS_INT(status
) = 0;
5399 if (!PyArg_ParseTuple(args
, "i:WCOREDUMP", &WAIT_STATUS_INT(status
)))
5402 return PyBool_FromLong(WCOREDUMP(status
));
5404 #endif /* WCOREDUMP */
5407 PyDoc_STRVAR(posix_WIFCONTINUED__doc__
,
5408 "WIFCONTINUED(status) -> bool\n\n\
5409 Return True if the process returning 'status' was continued from a\n\
5410 job control stop.");
5413 posix_WIFCONTINUED(PyObject
*self
, PyObject
*args
)
5416 WAIT_STATUS_INT(status
) = 0;
5418 if (!PyArg_ParseTuple(args
, "i:WCONTINUED", &WAIT_STATUS_INT(status
)))
5421 return PyBool_FromLong(WIFCONTINUED(status
));
5423 #endif /* WIFCONTINUED */
5426 PyDoc_STRVAR(posix_WIFSTOPPED__doc__
,
5427 "WIFSTOPPED(status) -> bool\n\n\
5428 Return True if the process returning 'status' was stopped.");
5431 posix_WIFSTOPPED(PyObject
*self
, PyObject
*args
)
5434 WAIT_STATUS_INT(status
) = 0;
5436 if (!PyArg_ParseTuple(args
, "i:WIFSTOPPED", &WAIT_STATUS_INT(status
)))
5439 return PyBool_FromLong(WIFSTOPPED(status
));
5441 #endif /* WIFSTOPPED */
5444 PyDoc_STRVAR(posix_WIFSIGNALED__doc__
,
5445 "WIFSIGNALED(status) -> bool\n\n\
5446 Return True if the process returning 'status' was terminated by a signal.");
5449 posix_WIFSIGNALED(PyObject
*self
, PyObject
*args
)
5452 WAIT_STATUS_INT(status
) = 0;
5454 if (!PyArg_ParseTuple(args
, "i:WIFSIGNALED", &WAIT_STATUS_INT(status
)))
5457 return PyBool_FromLong(WIFSIGNALED(status
));
5459 #endif /* WIFSIGNALED */
5462 PyDoc_STRVAR(posix_WIFEXITED__doc__
,
5463 "WIFEXITED(status) -> bool\n\n\
5464 Return true if the process returning 'status' exited using the exit()\n\
5468 posix_WIFEXITED(PyObject
*self
, PyObject
*args
)
5471 WAIT_STATUS_INT(status
) = 0;
5473 if (!PyArg_ParseTuple(args
, "i:WIFEXITED", &WAIT_STATUS_INT(status
)))
5476 return PyBool_FromLong(WIFEXITED(status
));
5478 #endif /* WIFEXITED */
5481 PyDoc_STRVAR(posix_WEXITSTATUS__doc__
,
5482 "WEXITSTATUS(status) -> integer\n\n\
5483 Return the process return code from 'status'.");
5486 posix_WEXITSTATUS(PyObject
*self
, PyObject
*args
)
5489 WAIT_STATUS_INT(status
) = 0;
5491 if (!PyArg_ParseTuple(args
, "i:WEXITSTATUS", &WAIT_STATUS_INT(status
)))
5494 return Py_BuildValue("i", WEXITSTATUS(status
));
5496 #endif /* WEXITSTATUS */
5499 PyDoc_STRVAR(posix_WTERMSIG__doc__
,
5500 "WTERMSIG(status) -> integer\n\n\
5501 Return the signal that terminated the process that provided the 'status'\n\
5505 posix_WTERMSIG(PyObject
*self
, PyObject
*args
)
5508 WAIT_STATUS_INT(status
) = 0;
5510 if (!PyArg_ParseTuple(args
, "i:WTERMSIG", &WAIT_STATUS_INT(status
)))
5513 return Py_BuildValue("i", WTERMSIG(status
));
5515 #endif /* WTERMSIG */
5518 PyDoc_STRVAR(posix_WSTOPSIG__doc__
,
5519 "WSTOPSIG(status) -> integer\n\n\
5520 Return the signal that stopped the process that provided\n\
5521 the 'status' value.");
5524 posix_WSTOPSIG(PyObject
*self
, PyObject
*args
)
5527 WAIT_STATUS_INT(status
) = 0;
5529 if (!PyArg_ParseTuple(args
, "i:WSTOPSIG", &WAIT_STATUS_INT(status
)))
5532 return Py_BuildValue("i", WSTOPSIG(status
));
5534 #endif /* WSTOPSIG */
5536 #endif /* HAVE_SYS_WAIT_H */
5539 #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
5541 /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
5542 needed definitions in sys/statvfs.h */
5545 #include <sys/statvfs.h>
5548 _pystatvfs_fromstructstatvfs(struct statvfs st
) {
5549 PyObject
*v
= PyStructSequence_New(&StatVFSResultType
);
5553 #if !defined(HAVE_LARGEFILE_SUPPORT)
5554 PyStructSequence_SET_ITEM(v
, 0, PyInt_FromLong((long) st
.f_bsize
));
5555 PyStructSequence_SET_ITEM(v
, 1, PyInt_FromLong((long) st
.f_frsize
));
5556 PyStructSequence_SET_ITEM(v
, 2, PyInt_FromLong((long) st
.f_blocks
));
5557 PyStructSequence_SET_ITEM(v
, 3, PyInt_FromLong((long) st
.f_bfree
));
5558 PyStructSequence_SET_ITEM(v
, 4, PyInt_FromLong((long) st
.f_bavail
));
5559 PyStructSequence_SET_ITEM(v
, 5, PyInt_FromLong((long) st
.f_files
));
5560 PyStructSequence_SET_ITEM(v
, 6, PyInt_FromLong((long) st
.f_ffree
));
5561 PyStructSequence_SET_ITEM(v
, 7, PyInt_FromLong((long) st
.f_favail
));
5562 PyStructSequence_SET_ITEM(v
, 8, PyInt_FromLong((long) st
.f_flag
));
5563 PyStructSequence_SET_ITEM(v
, 9, PyInt_FromLong((long) st
.f_namemax
));
5565 PyStructSequence_SET_ITEM(v
, 0, PyInt_FromLong((long) st
.f_bsize
));
5566 PyStructSequence_SET_ITEM(v
, 1, PyInt_FromLong((long) st
.f_frsize
));
5567 PyStructSequence_SET_ITEM(v
, 2,
5568 PyLong_FromLongLong((PY_LONG_LONG
) st
.f_blocks
));
5569 PyStructSequence_SET_ITEM(v
, 3,
5570 PyLong_FromLongLong((PY_LONG_LONG
) st
.f_bfree
));
5571 PyStructSequence_SET_ITEM(v
, 4,
5572 PyLong_FromLongLong((PY_LONG_LONG
) st
.f_bavail
));
5573 PyStructSequence_SET_ITEM(v
, 5,
5574 PyLong_FromLongLong((PY_LONG_LONG
) st
.f_files
));
5575 PyStructSequence_SET_ITEM(v
, 6,
5576 PyLong_FromLongLong((PY_LONG_LONG
) st
.f_ffree
));
5577 PyStructSequence_SET_ITEM(v
, 7,
5578 PyLong_FromLongLong((PY_LONG_LONG
) st
.f_favail
));
5579 PyStructSequence_SET_ITEM(v
, 8, PyInt_FromLong((long) st
.f_flag
));
5580 PyStructSequence_SET_ITEM(v
, 9, PyInt_FromLong((long) st
.f_namemax
));
5586 PyDoc_STRVAR(posix_fstatvfs__doc__
,
5587 "fstatvfs(fd) -> statvfs result\n\n\
5588 Perform an fstatvfs system call on the given fd.");
5591 posix_fstatvfs(PyObject
*self
, PyObject
*args
)
5596 if (!PyArg_ParseTuple(args
, "i:fstatvfs", &fd
))
5598 Py_BEGIN_ALLOW_THREADS
5599 res
= fstatvfs(fd
, &st
);
5600 Py_END_ALLOW_THREADS
5602 return posix_error();
5604 return _pystatvfs_fromstructstatvfs(st
);
5606 #endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
5609 #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
5610 #include <sys/statvfs.h>
5612 PyDoc_STRVAR(posix_statvfs__doc__
,
5613 "statvfs(path) -> statvfs result\n\n\
5614 Perform a statvfs system call on the given path.");
5617 posix_statvfs(PyObject
*self
, PyObject
*args
)
5622 if (!PyArg_ParseTuple(args
, "s:statvfs", &path
))
5624 Py_BEGIN_ALLOW_THREADS
5625 res
= statvfs(path
, &st
);
5626 Py_END_ALLOW_THREADS
5628 return posix_error_with_filename(path
);
5630 return _pystatvfs_fromstructstatvfs(st
);
5632 #endif /* HAVE_STATVFS */
5636 PyDoc_STRVAR(posix_tempnam__doc__
,
5637 "tempnam([dir[, prefix]]) -> string\n\n\
5638 Return a unique name for a temporary file.\n\
5639 The directory and a prefix may be specified as strings; they may be omitted\n\
5640 or None if not needed.");
5643 posix_tempnam(PyObject
*self
, PyObject
*args
)
5645 PyObject
*result
= NULL
;
5650 if (!PyArg_ParseTuple(args
, "|zz:tempnam", &dir
, &pfx
))
5653 if (PyErr_Warn(PyExc_RuntimeWarning
,
5654 "tempnam is a potential security risk to your program") < 0)
5657 if (PyErr_WarnPy3k("tempnam has been removed in 3.x; "
5658 "use the tempfile module", 1) < 0)
5661 name
= tempnam(dir
, pfx
);
5663 return PyErr_NoMemory();
5664 result
= PyString_FromString(name
);
5672 PyDoc_STRVAR(posix_tmpfile__doc__
,
5673 "tmpfile() -> file object\n\n\
5674 Create a temporary file with no directory entries.");
5677 posix_tmpfile(PyObject
*self
, PyObject
*noargs
)
5681 if (PyErr_WarnPy3k("tmpfile has been removed in 3.x; "
5682 "use the tempfile module", 1) < 0)
5687 return posix_error();
5688 return PyFile_FromFile(fp
, "<tmpfile>", "w+b", fclose
);
5694 PyDoc_STRVAR(posix_tmpnam__doc__
,
5695 "tmpnam() -> string\n\n\
5696 Return a unique name for a temporary file.");
5699 posix_tmpnam(PyObject
*self
, PyObject
*noargs
)
5701 char buffer
[L_tmpnam
];
5704 if (PyErr_Warn(PyExc_RuntimeWarning
,
5705 "tmpnam is a potential security risk to your program") < 0)
5708 if (PyErr_WarnPy3k("tmpnam has been removed in 3.x; "
5709 "use the tempfile module", 1) < 0)
5713 name
= tmpnam_r(buffer
);
5715 name
= tmpnam(buffer
);
5718 PyObject
*err
= Py_BuildValue("is", 0,
5720 "unexpected NULL from tmpnam_r"
5722 "unexpected NULL from tmpnam"
5725 PyErr_SetObject(PyExc_OSError
, err
);
5729 return PyString_FromString(buffer
);
5734 /* This is used for fpathconf(), pathconf(), confstr() and sysconf().
5735 * It maps strings representing configuration variable names to
5736 * integer values, allowing those functions to be called with the
5737 * magic names instead of polluting the module's namespace with tons of
5738 * rarely-used constants. There are three separate tables that use
5739 * these definitions.
5741 * This code is always included, even if none of the interfaces that
5742 * need it are included. The #if hackery needed to avoid it would be
5743 * sufficiently pervasive that it's not worth the loss of readability.
5750 #ifndef UEFI_C_SOURCE
5752 conv_confname(PyObject
*arg
, int *valuep
, struct constdef
*table
,
5755 if (PyInt_Check(arg
)) {
5756 *valuep
= PyInt_AS_LONG(arg
);
5759 if (PyString_Check(arg
)) {
5760 /* look up the value in the table using a binary search */
5763 size_t hi
= tablesize
;
5765 char *confname
= PyString_AS_STRING(arg
);
5767 mid
= (lo
+ hi
) / 2;
5768 cmp
= strcmp(confname
, table
[mid
].name
);
5774 *valuep
= table
[mid
].value
;
5778 PyErr_SetString(PyExc_ValueError
, "unrecognized configuration name");
5781 PyErr_SetString(PyExc_TypeError
,
5782 "configuration names must be strings or integers");
5785 #endif /* UEFI_C_SOURCE */
5787 #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
5788 static struct constdef posix_constants_pathconf
[] = {
5789 #ifdef _PC_ABI_AIO_XFER_MAX
5790 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX
},
5792 #ifdef _PC_ABI_ASYNC_IO
5793 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO
},
5796 {"PC_ASYNC_IO", _PC_ASYNC_IO
},
5798 #ifdef _PC_CHOWN_RESTRICTED
5799 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED
},
5801 #ifdef _PC_FILESIZEBITS
5802 {"PC_FILESIZEBITS", _PC_FILESIZEBITS
},
5805 {"PC_LAST", _PC_LAST
},
5808 {"PC_LINK_MAX", _PC_LINK_MAX
},
5810 #ifdef _PC_MAX_CANON
5811 {"PC_MAX_CANON", _PC_MAX_CANON
},
5813 #ifdef _PC_MAX_INPUT
5814 {"PC_MAX_INPUT", _PC_MAX_INPUT
},
5817 {"PC_NAME_MAX", _PC_NAME_MAX
},
5820 {"PC_NO_TRUNC", _PC_NO_TRUNC
},
5823 {"PC_PATH_MAX", _PC_PATH_MAX
},
5826 {"PC_PIPE_BUF", _PC_PIPE_BUF
},
5829 {"PC_PRIO_IO", _PC_PRIO_IO
},
5831 #ifdef _PC_SOCK_MAXBUF
5832 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF
},
5835 {"PC_SYNC_IO", _PC_SYNC_IO
},
5838 {"PC_VDISABLE", _PC_VDISABLE
},
5843 conv_path_confname(PyObject
*arg
, int *valuep
)
5845 return conv_confname(arg
, valuep
, posix_constants_pathconf
,
5846 sizeof(posix_constants_pathconf
)
5847 / sizeof(struct constdef
));
5851 #ifdef HAVE_FPATHCONF
5852 PyDoc_STRVAR(posix_fpathconf__doc__
,
5853 "fpathconf(fd, name) -> integer\n\n\
5854 Return the configuration limit name for the file descriptor fd.\n\
5855 If there is no limit, return -1.");
5858 posix_fpathconf(PyObject
*self
, PyObject
*args
)
5860 PyObject
*result
= NULL
;
5863 if (PyArg_ParseTuple(args
, "iO&:fpathconf", &fd
,
5864 conv_path_confname
, &name
)) {
5868 limit
= fpathconf(fd
, name
);
5869 if (limit
== -1 && errno
!= 0)
5872 result
= PyInt_FromLong(limit
);
5879 #ifdef HAVE_PATHCONF
5880 PyDoc_STRVAR(posix_pathconf__doc__
,
5881 "pathconf(path, name) -> integer\n\n\
5882 Return the configuration limit name for the file or directory path.\n\
5883 If there is no limit, return -1.");
5886 posix_pathconf(PyObject
*self
, PyObject
*args
)
5888 PyObject
*result
= NULL
;
5892 if (PyArg_ParseTuple(args
, "sO&:pathconf", &path
,
5893 conv_path_confname
, &name
)) {
5897 limit
= pathconf(path
, name
);
5898 if (limit
== -1 && errno
!= 0) {
5899 if (errno
== EINVAL
)
5900 /* could be a path or name problem */
5903 posix_error_with_filename(path
);
5906 result
= PyInt_FromLong(limit
);
5913 static struct constdef posix_constants_confstr
[] = {
5914 #ifdef _CS_ARCHITECTURE
5915 {"CS_ARCHITECTURE", _CS_ARCHITECTURE
},
5918 {"CS_HOSTNAME", _CS_HOSTNAME
},
5920 #ifdef _CS_HW_PROVIDER
5921 {"CS_HW_PROVIDER", _CS_HW_PROVIDER
},
5923 #ifdef _CS_HW_SERIAL
5924 {"CS_HW_SERIAL", _CS_HW_SERIAL
},
5926 #ifdef _CS_INITTAB_NAME
5927 {"CS_INITTAB_NAME", _CS_INITTAB_NAME
},
5929 #ifdef _CS_LFS64_CFLAGS
5930 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS
},
5932 #ifdef _CS_LFS64_LDFLAGS
5933 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS
},
5935 #ifdef _CS_LFS64_LIBS
5936 {"CS_LFS64_LIBS", _CS_LFS64_LIBS
},
5938 #ifdef _CS_LFS64_LINTFLAGS
5939 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS
},
5941 #ifdef _CS_LFS_CFLAGS
5942 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS
},
5944 #ifdef _CS_LFS_LDFLAGS
5945 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS
},
5948 {"CS_LFS_LIBS", _CS_LFS_LIBS
},
5950 #ifdef _CS_LFS_LINTFLAGS
5951 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS
},
5954 {"CS_MACHINE", _CS_MACHINE
},
5957 {"CS_PATH", _CS_PATH
},
5960 {"CS_RELEASE", _CS_RELEASE
},
5962 #ifdef _CS_SRPC_DOMAIN
5963 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN
},
5966 {"CS_SYSNAME", _CS_SYSNAME
},
5969 {"CS_VERSION", _CS_VERSION
},
5971 #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
5972 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS
},
5974 #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
5975 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS
},
5977 #ifdef _CS_XBS5_ILP32_OFF32_LIBS
5978 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS
},
5980 #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
5981 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS
},
5983 #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
5984 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS
},
5986 #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
5987 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS
},
5989 #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
5990 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS
},
5992 #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
5993 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
},
5995 #ifdef _CS_XBS5_LP64_OFF64_CFLAGS
5996 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS
},
5998 #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
5999 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS
},
6001 #ifdef _CS_XBS5_LP64_OFF64_LIBS
6002 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS
},
6004 #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
6005 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS
},
6007 #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
6008 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS
},
6010 #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
6011 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
},
6013 #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
6014 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS
},
6016 #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
6017 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
},
6019 #ifdef _MIPS_CS_AVAIL_PROCESSORS
6020 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS
},
6022 #ifdef _MIPS_CS_BASE
6023 {"MIPS_CS_BASE", _MIPS_CS_BASE
},
6025 #ifdef _MIPS_CS_HOSTID
6026 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID
},
6028 #ifdef _MIPS_CS_HW_NAME
6029 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME
},
6031 #ifdef _MIPS_CS_NUM_PROCESSORS
6032 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS
},
6034 #ifdef _MIPS_CS_OSREL_MAJ
6035 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ
},
6037 #ifdef _MIPS_CS_OSREL_MIN
6038 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN
},
6040 #ifdef _MIPS_CS_OSREL_PATCH
6041 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH
},
6043 #ifdef _MIPS_CS_OS_NAME
6044 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME
},
6046 #ifdef _MIPS_CS_OS_PROVIDER
6047 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER
},
6049 #ifdef _MIPS_CS_PROCESSORS
6050 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS
},
6052 #ifdef _MIPS_CS_SERIAL
6053 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL
},
6055 #ifdef _MIPS_CS_VENDOR
6056 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR
},
6061 conv_confstr_confname(PyObject
*arg
, int *valuep
)
6063 return conv_confname(arg
, valuep
, posix_constants_confstr
,
6064 sizeof(posix_constants_confstr
)
6065 / sizeof(struct constdef
));
6068 PyDoc_STRVAR(posix_confstr__doc__
,
6069 "confstr(name) -> string\n\n\
6070 Return a string-valued system configuration variable.");
6073 posix_confstr(PyObject
*self
, PyObject
*args
)
6075 PyObject
*result
= NULL
;
6079 if (PyArg_ParseTuple(args
, "O&:confstr", conv_confstr_confname
, &name
)) {
6083 len
= confstr(name
, buffer
, sizeof(buffer
));
6094 if ((unsigned int)len
>= sizeof(buffer
)) {
6095 result
= PyString_FromStringAndSize(NULL
, len
-1);
6097 confstr(name
, PyString_AS_STRING(result
), len
);
6100 result
= PyString_FromStringAndSize(buffer
, len
-1);
6109 static struct constdef posix_constants_sysconf
[] = {
6110 #ifdef _SC_2_CHAR_TERM
6111 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM
},
6114 {"SC_2_C_BIND", _SC_2_C_BIND
},
6117 {"SC_2_C_DEV", _SC_2_C_DEV
},
6119 #ifdef _SC_2_C_VERSION
6120 {"SC_2_C_VERSION", _SC_2_C_VERSION
},
6122 #ifdef _SC_2_FORT_DEV
6123 {"SC_2_FORT_DEV", _SC_2_FORT_DEV
},
6125 #ifdef _SC_2_FORT_RUN
6126 {"SC_2_FORT_RUN", _SC_2_FORT_RUN
},
6128 #ifdef _SC_2_LOCALEDEF
6129 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF
},
6132 {"SC_2_SW_DEV", _SC_2_SW_DEV
},
6135 {"SC_2_UPE", _SC_2_UPE
},
6137 #ifdef _SC_2_VERSION
6138 {"SC_2_VERSION", _SC_2_VERSION
},
6140 #ifdef _SC_ABI_ASYNCHRONOUS_IO
6141 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO
},
6144 {"SC_ACL", _SC_ACL
},
6146 #ifdef _SC_AIO_LISTIO_MAX
6147 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX
},
6150 {"SC_AIO_MAX", _SC_AIO_MAX
},
6152 #ifdef _SC_AIO_PRIO_DELTA_MAX
6153 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX
},
6156 {"SC_ARG_MAX", _SC_ARG_MAX
},
6158 #ifdef _SC_ASYNCHRONOUS_IO
6159 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO
},
6161 #ifdef _SC_ATEXIT_MAX
6162 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX
},
6165 {"SC_AUDIT", _SC_AUDIT
},
6167 #ifdef _SC_AVPHYS_PAGES
6168 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES
},
6170 #ifdef _SC_BC_BASE_MAX
6171 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX
},
6173 #ifdef _SC_BC_DIM_MAX
6174 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX
},
6176 #ifdef _SC_BC_SCALE_MAX
6177 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX
},
6179 #ifdef _SC_BC_STRING_MAX
6180 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX
},
6183 {"SC_CAP", _SC_CAP
},
6185 #ifdef _SC_CHARCLASS_NAME_MAX
6186 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX
},
6189 {"SC_CHAR_BIT", _SC_CHAR_BIT
},
6192 {"SC_CHAR_MAX", _SC_CHAR_MAX
},
6195 {"SC_CHAR_MIN", _SC_CHAR_MIN
},
6197 #ifdef _SC_CHILD_MAX
6198 {"SC_CHILD_MAX", _SC_CHILD_MAX
},
6201 {"SC_CLK_TCK", _SC_CLK_TCK
},
6203 #ifdef _SC_COHER_BLKSZ
6204 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ
},
6206 #ifdef _SC_COLL_WEIGHTS_MAX
6207 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX
},
6209 #ifdef _SC_DCACHE_ASSOC
6210 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC
},
6212 #ifdef _SC_DCACHE_BLKSZ
6213 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ
},
6215 #ifdef _SC_DCACHE_LINESZ
6216 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ
},
6218 #ifdef _SC_DCACHE_SZ
6219 {"SC_DCACHE_SZ", _SC_DCACHE_SZ
},
6221 #ifdef _SC_DCACHE_TBLKSZ
6222 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ
},
6224 #ifdef _SC_DELAYTIMER_MAX
6225 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX
},
6227 #ifdef _SC_EQUIV_CLASS_MAX
6228 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX
},
6230 #ifdef _SC_EXPR_NEST_MAX
6231 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX
},
6234 {"SC_FSYNC", _SC_FSYNC
},
6236 #ifdef _SC_GETGR_R_SIZE_MAX
6237 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX
},
6239 #ifdef _SC_GETPW_R_SIZE_MAX
6240 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX
},
6242 #ifdef _SC_ICACHE_ASSOC
6243 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC
},
6245 #ifdef _SC_ICACHE_BLKSZ
6246 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ
},
6248 #ifdef _SC_ICACHE_LINESZ
6249 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ
},
6251 #ifdef _SC_ICACHE_SZ
6252 {"SC_ICACHE_SZ", _SC_ICACHE_SZ
},
6255 {"SC_INF", _SC_INF
},
6258 {"SC_INT_MAX", _SC_INT_MAX
},
6261 {"SC_INT_MIN", _SC_INT_MIN
},
6264 {"SC_IOV_MAX", _SC_IOV_MAX
},
6266 #ifdef _SC_IP_SECOPTS
6267 {"SC_IP_SECOPTS", _SC_IP_SECOPTS
},
6269 #ifdef _SC_JOB_CONTROL
6270 {"SC_JOB_CONTROL", _SC_JOB_CONTROL
},
6272 #ifdef _SC_KERN_POINTERS
6273 {"SC_KERN_POINTERS", _SC_KERN_POINTERS
},
6276 {"SC_KERN_SIM", _SC_KERN_SIM
},
6279 {"SC_LINE_MAX", _SC_LINE_MAX
},
6281 #ifdef _SC_LOGIN_NAME_MAX
6282 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX
},
6284 #ifdef _SC_LOGNAME_MAX
6285 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX
},
6288 {"SC_LONG_BIT", _SC_LONG_BIT
},
6291 {"SC_MAC", _SC_MAC
},
6293 #ifdef _SC_MAPPED_FILES
6294 {"SC_MAPPED_FILES", _SC_MAPPED_FILES
},
6297 {"SC_MAXPID", _SC_MAXPID
},
6299 #ifdef _SC_MB_LEN_MAX
6300 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX
},
6303 {"SC_MEMLOCK", _SC_MEMLOCK
},
6305 #ifdef _SC_MEMLOCK_RANGE
6306 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE
},
6308 #ifdef _SC_MEMORY_PROTECTION
6309 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION
},
6311 #ifdef _SC_MESSAGE_PASSING
6312 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING
},
6314 #ifdef _SC_MMAP_FIXED_ALIGNMENT
6315 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT
},
6317 #ifdef _SC_MQ_OPEN_MAX
6318 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX
},
6320 #ifdef _SC_MQ_PRIO_MAX
6321 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX
},
6323 #ifdef _SC_NACLS_MAX
6324 {"SC_NACLS_MAX", _SC_NACLS_MAX
},
6326 #ifdef _SC_NGROUPS_MAX
6327 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX
},
6329 #ifdef _SC_NL_ARGMAX
6330 {"SC_NL_ARGMAX", _SC_NL_ARGMAX
},
6332 #ifdef _SC_NL_LANGMAX
6333 {"SC_NL_LANGMAX", _SC_NL_LANGMAX
},
6335 #ifdef _SC_NL_MSGMAX
6336 {"SC_NL_MSGMAX", _SC_NL_MSGMAX
},
6339 {"SC_NL_NMAX", _SC_NL_NMAX
},
6341 #ifdef _SC_NL_SETMAX
6342 {"SC_NL_SETMAX", _SC_NL_SETMAX
},
6344 #ifdef _SC_NL_TEXTMAX
6345 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX
},
6347 #ifdef _SC_NPROCESSORS_CONF
6348 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF
},
6350 #ifdef _SC_NPROCESSORS_ONLN
6351 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN
},
6353 #ifdef _SC_NPROC_CONF
6354 {"SC_NPROC_CONF", _SC_NPROC_CONF
},
6356 #ifdef _SC_NPROC_ONLN
6357 {"SC_NPROC_ONLN", _SC_NPROC_ONLN
},
6360 {"SC_NZERO", _SC_NZERO
},
6363 {"SC_OPEN_MAX", _SC_OPEN_MAX
},
6366 {"SC_PAGESIZE", _SC_PAGESIZE
},
6368 #ifdef _SC_PAGE_SIZE
6369 {"SC_PAGE_SIZE", _SC_PAGE_SIZE
},
6372 {"SC_PASS_MAX", _SC_PASS_MAX
},
6374 #ifdef _SC_PHYS_PAGES
6375 {"SC_PHYS_PAGES", _SC_PHYS_PAGES
},
6378 {"SC_PII", _SC_PII
},
6380 #ifdef _SC_PII_INTERNET
6381 {"SC_PII_INTERNET", _SC_PII_INTERNET
},
6383 #ifdef _SC_PII_INTERNET_DGRAM
6384 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM
},
6386 #ifdef _SC_PII_INTERNET_STREAM
6387 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM
},
6390 {"SC_PII_OSI", _SC_PII_OSI
},
6392 #ifdef _SC_PII_OSI_CLTS
6393 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS
},
6395 #ifdef _SC_PII_OSI_COTS
6396 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS
},
6398 #ifdef _SC_PII_OSI_M
6399 {"SC_PII_OSI_M", _SC_PII_OSI_M
},
6401 #ifdef _SC_PII_SOCKET
6402 {"SC_PII_SOCKET", _SC_PII_SOCKET
},
6405 {"SC_PII_XTI", _SC_PII_XTI
},
6408 {"SC_POLL", _SC_POLL
},
6410 #ifdef _SC_PRIORITIZED_IO
6411 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO
},
6413 #ifdef _SC_PRIORITY_SCHEDULING
6414 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING
},
6416 #ifdef _SC_REALTIME_SIGNALS
6417 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS
},
6419 #ifdef _SC_RE_DUP_MAX
6420 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX
},
6422 #ifdef _SC_RTSIG_MAX
6423 {"SC_RTSIG_MAX", _SC_RTSIG_MAX
},
6425 #ifdef _SC_SAVED_IDS
6426 {"SC_SAVED_IDS", _SC_SAVED_IDS
},
6428 #ifdef _SC_SCHAR_MAX
6429 {"SC_SCHAR_MAX", _SC_SCHAR_MAX
},
6431 #ifdef _SC_SCHAR_MIN
6432 {"SC_SCHAR_MIN", _SC_SCHAR_MIN
},
6435 {"SC_SELECT", _SC_SELECT
},
6437 #ifdef _SC_SEMAPHORES
6438 {"SC_SEMAPHORES", _SC_SEMAPHORES
},
6440 #ifdef _SC_SEM_NSEMS_MAX
6441 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX
},
6443 #ifdef _SC_SEM_VALUE_MAX
6444 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX
},
6446 #ifdef _SC_SHARED_MEMORY_OBJECTS
6447 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS
},
6450 {"SC_SHRT_MAX", _SC_SHRT_MAX
},
6453 {"SC_SHRT_MIN", _SC_SHRT_MIN
},
6455 #ifdef _SC_SIGQUEUE_MAX
6456 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX
},
6458 #ifdef _SC_SIGRT_MAX
6459 {"SC_SIGRT_MAX", _SC_SIGRT_MAX
},
6461 #ifdef _SC_SIGRT_MIN
6462 {"SC_SIGRT_MIN", _SC_SIGRT_MIN
},
6464 #ifdef _SC_SOFTPOWER
6465 {"SC_SOFTPOWER", _SC_SOFTPOWER
},
6467 #ifdef _SC_SPLIT_CACHE
6468 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE
},
6470 #ifdef _SC_SSIZE_MAX
6471 {"SC_SSIZE_MAX", _SC_SSIZE_MAX
},
6473 #ifdef _SC_STACK_PROT
6474 {"SC_STACK_PROT", _SC_STACK_PROT
},
6476 #ifdef _SC_STREAM_MAX
6477 {"SC_STREAM_MAX", _SC_STREAM_MAX
},
6479 #ifdef _SC_SYNCHRONIZED_IO
6480 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO
},
6483 {"SC_THREADS", _SC_THREADS
},
6485 #ifdef _SC_THREAD_ATTR_STACKADDR
6486 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR
},
6488 #ifdef _SC_THREAD_ATTR_STACKSIZE
6489 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE
},
6491 #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
6492 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS
},
6494 #ifdef _SC_THREAD_KEYS_MAX
6495 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX
},
6497 #ifdef _SC_THREAD_PRIORITY_SCHEDULING
6498 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING
},
6500 #ifdef _SC_THREAD_PRIO_INHERIT
6501 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT
},
6503 #ifdef _SC_THREAD_PRIO_PROTECT
6504 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT
},
6506 #ifdef _SC_THREAD_PROCESS_SHARED
6507 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED
},
6509 #ifdef _SC_THREAD_SAFE_FUNCTIONS
6510 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS
},
6512 #ifdef _SC_THREAD_STACK_MIN
6513 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN
},
6515 #ifdef _SC_THREAD_THREADS_MAX
6516 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX
},
6519 {"SC_TIMERS", _SC_TIMERS
},
6521 #ifdef _SC_TIMER_MAX
6522 {"SC_TIMER_MAX", _SC_TIMER_MAX
},
6524 #ifdef _SC_TTY_NAME_MAX
6525 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX
},
6527 #ifdef _SC_TZNAME_MAX
6528 {"SC_TZNAME_MAX", _SC_TZNAME_MAX
},
6530 #ifdef _SC_T_IOV_MAX
6531 {"SC_T_IOV_MAX", _SC_T_IOV_MAX
},
6533 #ifdef _SC_UCHAR_MAX
6534 {"SC_UCHAR_MAX", _SC_UCHAR_MAX
},
6537 {"SC_UINT_MAX", _SC_UINT_MAX
},
6539 #ifdef _SC_UIO_MAXIOV
6540 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV
},
6542 #ifdef _SC_ULONG_MAX
6543 {"SC_ULONG_MAX", _SC_ULONG_MAX
},
6545 #ifdef _SC_USHRT_MAX
6546 {"SC_USHRT_MAX", _SC_USHRT_MAX
},
6549 {"SC_VERSION", _SC_VERSION
},
6552 {"SC_WORD_BIT", _SC_WORD_BIT
},
6554 #ifdef _SC_XBS5_ILP32_OFF32
6555 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32
},
6557 #ifdef _SC_XBS5_ILP32_OFFBIG
6558 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG
},
6560 #ifdef _SC_XBS5_LP64_OFF64
6561 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64
},
6563 #ifdef _SC_XBS5_LPBIG_OFFBIG
6564 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG
},
6566 #ifdef _SC_XOPEN_CRYPT
6567 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT
},
6569 #ifdef _SC_XOPEN_ENH_I18N
6570 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N
},
6572 #ifdef _SC_XOPEN_LEGACY
6573 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY
},
6575 #ifdef _SC_XOPEN_REALTIME
6576 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME
},
6578 #ifdef _SC_XOPEN_REALTIME_THREADS
6579 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS
},
6581 #ifdef _SC_XOPEN_SHM
6582 {"SC_XOPEN_SHM", _SC_XOPEN_SHM
},
6584 #ifdef _SC_XOPEN_UNIX
6585 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX
},
6587 #ifdef _SC_XOPEN_VERSION
6588 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION
},
6590 #ifdef _SC_XOPEN_XCU_VERSION
6591 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION
},
6593 #ifdef _SC_XOPEN_XPG2
6594 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2
},
6596 #ifdef _SC_XOPEN_XPG3
6597 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3
},
6599 #ifdef _SC_XOPEN_XPG4
6600 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4
},
6605 conv_sysconf_confname(PyObject
*arg
, int *valuep
)
6607 return conv_confname(arg
, valuep
, posix_constants_sysconf
,
6608 sizeof(posix_constants_sysconf
)
6609 / sizeof(struct constdef
));
6612 PyDoc_STRVAR(posix_sysconf__doc__
,
6613 "sysconf(name) -> integer\n\n\
6614 Return an integer-valued system configuration variable.");
6617 posix_sysconf(PyObject
*self
, PyObject
*args
)
6619 PyObject
*result
= NULL
;
6622 if (PyArg_ParseTuple(args
, "O&:sysconf", conv_sysconf_confname
, &name
)) {
6626 value
= sysconf(name
);
6627 if (value
== -1 && errno
!= 0)
6630 result
= PyInt_FromLong(value
);
6637 #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) || defined(HAVE_CONFSTR) || defined(HAVE_SYSCONF)
6638 /* This code is used to ensure that the tables of configuration value names
6639 * are in sorted order as required by conv_confname(), and also to build the
6640 * the exported dictionaries that are used to publish information about the
6641 * names available on the host platform.
6643 * Sorting the table at runtime ensures that the table is properly ordered
6644 * when used, even for platforms we're not able to test on. It also makes
6645 * it easier to add additional entries to the tables.
6649 cmp_constdefs(const void *v1
, const void *v2
)
6651 const struct constdef
*c1
=
6652 (const struct constdef
*) v1
;
6653 const struct constdef
*c2
=
6654 (const struct constdef
*) v2
;
6656 return strcmp(c1
->name
, c2
->name
);
6660 setup_confname_table(struct constdef
*table
, size_t tablesize
,
6661 char *tablename
, PyObject
*module
)
6666 qsort(table
, tablesize
, sizeof(struct constdef
), cmp_constdefs
);
6671 for (i
=0; i
< tablesize
; ++i
) {
6672 PyObject
*o
= PyInt_FromLong(table
[i
].value
);
6673 if (o
== NULL
|| PyDict_SetItemString(d
, table
[i
].name
, o
) == -1) {
6680 return PyModule_AddObject(module
, tablename
, d
);
6682 #endif /* HAVE_FPATHCONF || HAVE_PATHCONF || HAVE_CONFSTR || HAVE_SYSCONF */
6684 /* Return -1 on failure, 0 on success. */
6686 setup_confname_tables(PyObject
*module
)
6688 #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
6689 if (setup_confname_table(posix_constants_pathconf
,
6690 sizeof(posix_constants_pathconf
)
6691 / sizeof(struct constdef
),
6692 "pathconf_names", module
))
6696 if (setup_confname_table(posix_constants_confstr
,
6697 sizeof(posix_constants_confstr
)
6698 / sizeof(struct constdef
),
6699 "confstr_names", module
))
6703 if (setup_confname_table(posix_constants_sysconf
,
6704 sizeof(posix_constants_sysconf
)
6705 / sizeof(struct constdef
),
6706 "sysconf_names", module
))
6713 PyDoc_STRVAR(posix_abort__doc__
,
6714 "abort() -> does not return!\n\n\
6715 Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
6716 in the hardest way possible on the hosting operating system.");
6719 posix_abort(PyObject
*self
, PyObject
*noargs
)
6723 Py_FatalError("abort() called from Python code didn't abort!");
6727 #ifdef HAVE_SETRESUID
6728 PyDoc_STRVAR(posix_setresuid__doc__
,
6729 "setresuid(ruid, euid, suid)\n\n\
6730 Set the current process's real, effective, and saved user ids.");
6733 posix_setresuid (PyObject
*self
, PyObject
*args
)
6735 /* We assume uid_t is no larger than a long. */
6736 long ruid
, euid
, suid
;
6737 if (!PyArg_ParseTuple(args
, "lll", &ruid
, &euid
, &suid
))
6739 if (setresuid(ruid
, euid
, suid
) < 0)
6740 return posix_error();
6745 #ifdef HAVE_SETRESGID
6746 PyDoc_STRVAR(posix_setresgid__doc__
,
6747 "setresgid(rgid, egid, sgid)\n\n\
6748 Set the current process's real, effective, and saved group ids.");
6751 posix_setresgid (PyObject
*self
, PyObject
*args
)
6753 /* We assume uid_t is no larger than a long. */
6754 long rgid
, egid
, sgid
;
6755 if (!PyArg_ParseTuple(args
, "lll", &rgid
, &egid
, &sgid
))
6757 if (setresgid(rgid
, egid
, sgid
) < 0)
6758 return posix_error();
6763 #ifdef HAVE_GETRESUID
6764 PyDoc_STRVAR(posix_getresuid__doc__
,
6765 "getresuid() -> (ruid, euid, suid)\n\n\
6766 Get tuple of the current process's real, effective, and saved user ids.");
6769 posix_getresuid (PyObject
*self
, PyObject
*noargs
)
6771 uid_t ruid
, euid
, suid
;
6772 long l_ruid
, l_euid
, l_suid
;
6773 if (getresuid(&ruid
, &euid
, &suid
) < 0)
6774 return posix_error();
6775 /* Force the values into long's as we don't know the size of uid_t. */
6779 return Py_BuildValue("(lll)", l_ruid
, l_euid
, l_suid
);
6783 #ifdef HAVE_GETRESGID
6784 PyDoc_STRVAR(posix_getresgid__doc__
,
6785 "getresgid() -> (rgid, egid, sgid)\n\n\
6786 Get tuple of the current process's real, effective, and saved group ids.");
6789 posix_getresgid (PyObject
*self
, PyObject
*noargs
)
6791 uid_t rgid
, egid
, sgid
;
6792 long l_rgid
, l_egid
, l_sgid
;
6793 if (getresgid(&rgid
, &egid
, &sgid
) < 0)
6794 return posix_error();
6795 /* Force the values into long's as we don't know the size of uid_t. */
6799 return Py_BuildValue("(lll)", l_rgid
, l_egid
, l_sgid
);
6803 static PyMethodDef posix_methods
[] = {
6804 {"access", posix_access
, METH_VARARGS
, posix_access__doc__
},
6806 {"ttyname", posix_ttyname
, METH_VARARGS
, posix_ttyname__doc__
},
6808 {"chdir", posix_chdir
, METH_VARARGS
, posix_chdir__doc__
},
6810 {"chflags", posix_chflags
, METH_VARARGS
, posix_chflags__doc__
},
6811 #endif /* HAVE_CHFLAGS */
6812 {"chmod", posix_chmod
, METH_VARARGS
, posix_chmod__doc__
},
6814 {"fchmod", posix_fchmod
, METH_VARARGS
, posix_fchmod__doc__
},
6815 #endif /* HAVE_FCHMOD */
6817 {"chown", posix_chown
, METH_VARARGS
, posix_chown__doc__
},
6818 #endif /* HAVE_CHOWN */
6820 {"lchmod", posix_lchmod
, METH_VARARGS
, posix_lchmod__doc__
},
6821 #endif /* HAVE_LCHMOD */
6823 {"fchown", posix_fchown
, METH_VARARGS
, posix_fchown__doc__
},
6824 #endif /* HAVE_FCHOWN */
6825 #ifdef HAVE_LCHFLAGS
6826 {"lchflags", posix_lchflags
, METH_VARARGS
, posix_lchflags__doc__
},
6827 #endif /* HAVE_LCHFLAGS */
6829 {"lchown", posix_lchown
, METH_VARARGS
, posix_lchown__doc__
},
6830 #endif /* HAVE_LCHOWN */
6832 {"chroot", posix_chroot
, METH_VARARGS
, posix_chroot__doc__
},
6835 {"ctermid", posix_ctermid
, METH_NOARGS
, posix_ctermid__doc__
},
6838 {"getcwd", posix_getcwd
, METH_NOARGS
, posix_getcwd__doc__
},
6839 #ifdef Py_USING_UNICODE
6840 {"getcwdu", posix_getcwdu
, METH_NOARGS
, posix_getcwdu__doc__
},
6844 {"link", posix_link
, METH_VARARGS
, posix_link__doc__
},
6845 #endif /* HAVE_LINK */
6846 {"listdir", posix_listdir
, METH_VARARGS
, posix_listdir__doc__
},
6847 {"lstat", posix_lstat
, METH_VARARGS
, posix_lstat__doc__
},
6848 {"mkdir", posix_mkdir
, METH_VARARGS
, posix_mkdir__doc__
},
6850 {"nice", posix_nice
, METH_VARARGS
, posix_nice__doc__
},
6851 #endif /* HAVE_NICE */
6852 #ifdef HAVE_READLINK
6853 {"readlink", posix_readlink
, METH_VARARGS
, posix_readlink__doc__
},
6854 #endif /* HAVE_READLINK */
6855 {"rename", posix_rename
, METH_VARARGS
, posix_rename__doc__
},
6856 {"rmdir", posix_rmdir
, METH_VARARGS
, posix_rmdir__doc__
},
6857 {"stat", posix_stat
, METH_VARARGS
, posix_stat__doc__
},
6858 //{"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
6860 {"symlink", posix_symlink
, METH_VARARGS
, posix_symlink__doc__
},
6861 #endif /* HAVE_SYMLINK */
6863 {"system", posix_system
, METH_VARARGS
, posix_system__doc__
},
6865 {"umask", posix_umask
, METH_VARARGS
, posix_umask__doc__
},
6867 {"uname", posix_uname
, METH_NOARGS
, posix_uname__doc__
},
6868 #endif /* HAVE_UNAME */
6869 {"unlink", posix_unlink
, METH_VARARGS
, posix_unlink__doc__
},
6870 {"remove", posix_unlink
, METH_VARARGS
, posix_remove__doc__
},
6871 {"utime", posix_utime
, METH_VARARGS
, posix_utime__doc__
},
6873 {"times", posix_times
, METH_NOARGS
, posix_times__doc__
},
6874 #endif /* HAVE_TIMES */
6875 {"_exit", posix__exit
, METH_VARARGS
, posix__exit__doc__
},
6877 {"execv", posix_execv
, METH_VARARGS
, posix_execv__doc__
},
6878 {"execve", posix_execve
, METH_VARARGS
, posix_execve__doc__
},
6879 #endif /* HAVE_EXECV */
6881 {"spawnv", posix_spawnv
, METH_VARARGS
, posix_spawnv__doc__
},
6882 {"spawnve", posix_spawnve
, METH_VARARGS
, posix_spawnve__doc__
},
6883 #if defined(PYOS_OS2)
6884 {"spawnvp", posix_spawnvp
, METH_VARARGS
, posix_spawnvp__doc__
},
6885 {"spawnvpe", posix_spawnvpe
, METH_VARARGS
, posix_spawnvpe__doc__
},
6886 #endif /* PYOS_OS2 */
6887 #endif /* HAVE_SPAWNV */
6889 {"fork1", posix_fork1
, METH_NOARGS
, posix_fork1__doc__
},
6890 #endif /* HAVE_FORK1 */
6892 {"fork", posix_fork
, METH_NOARGS
, posix_fork__doc__
},
6893 #endif /* HAVE_FORK */
6894 #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
6895 {"openpty", posix_openpty
, METH_NOARGS
, posix_openpty__doc__
},
6896 #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
6898 {"forkpty", posix_forkpty
, METH_NOARGS
, posix_forkpty__doc__
},
6899 #endif /* HAVE_FORKPTY */
6901 {"getegid", posix_getegid
, METH_NOARGS
, posix_getegid__doc__
},
6902 #endif /* HAVE_GETEGID */
6904 {"geteuid", posix_geteuid
, METH_NOARGS
, posix_geteuid__doc__
},
6905 #endif /* HAVE_GETEUID */
6907 {"getgid", posix_getgid
, METH_NOARGS
, posix_getgid__doc__
},
6908 #endif /* HAVE_GETGID */
6909 #ifdef HAVE_GETGROUPS
6910 {"getgroups", posix_getgroups
, METH_NOARGS
, posix_getgroups__doc__
},
6912 {"getpid", posix_getpid
, METH_NOARGS
, posix_getpid__doc__
},
6914 {"getpgrp", posix_getpgrp
, METH_NOARGS
, posix_getpgrp__doc__
},
6915 #endif /* HAVE_GETPGRP */
6917 {"getppid", posix_getppid
, METH_NOARGS
, posix_getppid__doc__
},
6918 #endif /* HAVE_GETPPID */
6920 {"getuid", posix_getuid
, METH_NOARGS
, posix_getuid__doc__
},
6921 #endif /* HAVE_GETUID */
6922 #ifdef HAVE_GETLOGIN
6923 {"getlogin", posix_getlogin
, METH_NOARGS
, posix_getlogin__doc__
},
6926 {"kill", posix_kill
, METH_VARARGS
, posix_kill__doc__
},
6927 #endif /* HAVE_KILL */
6929 {"killpg", posix_killpg
, METH_VARARGS
, posix_killpg__doc__
},
6930 #endif /* HAVE_KILLPG */
6932 {"plock", posix_plock
, METH_VARARGS
, posix_plock__doc__
},
6933 #endif /* HAVE_PLOCK */
6935 {"popen", posix_popen
, METH_VARARGS
, posix_popen__doc__
},
6937 {"popen2", win32_popen2
, METH_VARARGS
},
6938 {"popen3", win32_popen3
, METH_VARARGS
},
6939 {"popen4", win32_popen4
, METH_VARARGS
},
6940 {"startfile", win32_startfile
, METH_VARARGS
, win32_startfile__doc__
},
6941 {"kill", win32_kill
, METH_VARARGS
, win32_kill__doc__
},
6943 #if defined(PYOS_OS2) && defined(PYCC_GCC)
6944 {"popen2", os2emx_popen2
, METH_VARARGS
},
6945 {"popen3", os2emx_popen3
, METH_VARARGS
},
6946 {"popen4", os2emx_popen4
, METH_VARARGS
},
6949 #endif /* HAVE_POPEN */
6951 {"setuid", posix_setuid
, METH_VARARGS
, posix_setuid__doc__
},
6952 #endif /* HAVE_SETUID */
6954 {"seteuid", posix_seteuid
, METH_VARARGS
, posix_seteuid__doc__
},
6955 #endif /* HAVE_SETEUID */
6957 {"setegid", posix_setegid
, METH_VARARGS
, posix_setegid__doc__
},
6958 #endif /* HAVE_SETEGID */
6959 #ifdef HAVE_SETREUID
6960 {"setreuid", posix_setreuid
, METH_VARARGS
, posix_setreuid__doc__
},
6961 #endif /* HAVE_SETREUID */
6962 #ifdef HAVE_SETREGID
6963 {"setregid", posix_setregid
, METH_VARARGS
, posix_setregid__doc__
},
6964 #endif /* HAVE_SETREGID */
6966 {"setgid", posix_setgid
, METH_VARARGS
, posix_setgid__doc__
},
6967 #endif /* HAVE_SETGID */
6968 #ifdef HAVE_SETGROUPS
6969 {"setgroups", posix_setgroups
, METH_O
, posix_setgroups__doc__
},
6970 #endif /* HAVE_SETGROUPS */
6971 #ifdef HAVE_INITGROUPS
6972 {"initgroups", posix_initgroups
, METH_VARARGS
, posix_initgroups__doc__
},
6973 #endif /* HAVE_INITGROUPS */
6975 {"getpgid", posix_getpgid
, METH_VARARGS
, posix_getpgid__doc__
},
6976 #endif /* HAVE_GETPGID */
6978 {"setpgrp", posix_setpgrp
, METH_NOARGS
, posix_setpgrp__doc__
},
6979 #endif /* HAVE_SETPGRP */
6981 {"wait", posix_wait
, METH_NOARGS
, posix_wait__doc__
},
6982 #endif /* HAVE_WAIT */
6984 {"wait3", posix_wait3
, METH_VARARGS
, posix_wait3__doc__
},
6985 #endif /* HAVE_WAIT3 */
6987 {"wait4", posix_wait4
, METH_VARARGS
, posix_wait4__doc__
},
6988 #endif /* HAVE_WAIT4 */
6989 #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
6990 {"waitpid", posix_waitpid
, METH_VARARGS
, posix_waitpid__doc__
},
6991 #endif /* HAVE_WAITPID */
6993 {"getsid", posix_getsid
, METH_VARARGS
, posix_getsid__doc__
},
6994 #endif /* HAVE_GETSID */
6996 {"setsid", posix_setsid
, METH_NOARGS
, posix_setsid__doc__
},
6997 #endif /* HAVE_SETSID */
6999 {"setpgid", posix_setpgid
, METH_VARARGS
, posix_setpgid__doc__
},
7000 #endif /* HAVE_SETPGID */
7001 #ifdef HAVE_TCGETPGRP
7002 {"tcgetpgrp", posix_tcgetpgrp
, METH_VARARGS
, posix_tcgetpgrp__doc__
},
7003 #endif /* HAVE_TCGETPGRP */
7004 #ifdef HAVE_TCSETPGRP
7005 {"tcsetpgrp", posix_tcsetpgrp
, METH_VARARGS
, posix_tcsetpgrp__doc__
},
7006 #endif /* HAVE_TCSETPGRP */
7007 {"open", posix_open
, METH_VARARGS
, posix_open__doc__
},
7008 {"close", posix_close
, METH_VARARGS
, posix_close__doc__
},
7009 {"closerange", posix_closerange
, METH_VARARGS
, posix_closerange__doc__
},
7010 {"dup", posix_dup
, METH_VARARGS
, posix_dup__doc__
},
7011 {"dup2", posix_dup2
, METH_VARARGS
, posix_dup2__doc__
},
7012 {"lseek", posix_lseek
, METH_VARARGS
, posix_lseek__doc__
},
7013 {"read", posix_read
, METH_VARARGS
, posix_read__doc__
},
7014 {"write", posix_write
, METH_VARARGS
, posix_write__doc__
},
7015 {"fstat", posix_fstat
, METH_VARARGS
, posix_fstat__doc__
},
7016 {"fdopen", posix_fdopen
, METH_VARARGS
, posix_fdopen__doc__
},
7017 {"isatty", posix_isatty
, METH_VARARGS
, posix_isatty__doc__
},
7019 {"pipe", posix_pipe
, METH_NOARGS
, posix_pipe__doc__
},
7022 {"mkfifo", posix_mkfifo
, METH_VARARGS
, posix_mkfifo__doc__
},
7024 #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
7025 {"mknod", posix_mknod
, METH_VARARGS
, posix_mknod__doc__
},
7027 #ifdef HAVE_DEVICE_MACROS
7028 {"major", posix_major
, METH_VARARGS
, posix_major__doc__
},
7029 {"minor", posix_minor
, METH_VARARGS
, posix_minor__doc__
},
7030 {"makedev", posix_makedev
, METH_VARARGS
, posix_makedev__doc__
},
7032 #ifdef HAVE_FTRUNCATE
7033 {"ftruncate", posix_ftruncate
, METH_VARARGS
, posix_ftruncate__doc__
},
7036 {"putenv", posix_putenv
, METH_VARARGS
, posix_putenv__doc__
},
7038 #ifdef HAVE_UNSETENV
7039 {"unsetenv", posix_unsetenv
, METH_VARARGS
, posix_unsetenv__doc__
},
7041 {"strerror", posix_strerror
, METH_VARARGS
, posix_strerror__doc__
},
7043 {"fchdir", posix_fchdir
, METH_O
, posix_fchdir__doc__
},
7046 {"fsync", posix_fsync
, METH_O
, posix_fsync__doc__
},
7048 #ifdef HAVE_FDATASYNC
7049 {"fdatasync", posix_fdatasync
, METH_O
, posix_fdatasync__doc__
},
7051 #ifdef HAVE_SYS_WAIT_H
7053 {"WCOREDUMP", posix_WCOREDUMP
, METH_VARARGS
, posix_WCOREDUMP__doc__
},
7054 #endif /* WCOREDUMP */
7056 {"WIFCONTINUED",posix_WIFCONTINUED
, METH_VARARGS
, posix_WIFCONTINUED__doc__
},
7057 #endif /* WIFCONTINUED */
7059 {"WIFSTOPPED", posix_WIFSTOPPED
, METH_VARARGS
, posix_WIFSTOPPED__doc__
},
7060 #endif /* WIFSTOPPED */
7062 {"WIFSIGNALED", posix_WIFSIGNALED
, METH_VARARGS
, posix_WIFSIGNALED__doc__
},
7063 #endif /* WIFSIGNALED */
7065 {"WIFEXITED", posix_WIFEXITED
, METH_VARARGS
, posix_WIFEXITED__doc__
},
7066 #endif /* WIFEXITED */
7068 {"WEXITSTATUS", posix_WEXITSTATUS
, METH_VARARGS
, posix_WEXITSTATUS__doc__
},
7069 #endif /* WEXITSTATUS */
7071 {"WTERMSIG", posix_WTERMSIG
, METH_VARARGS
, posix_WTERMSIG__doc__
},
7072 #endif /* WTERMSIG */
7074 {"WSTOPSIG", posix_WSTOPSIG
, METH_VARARGS
, posix_WSTOPSIG__doc__
},
7075 #endif /* WSTOPSIG */
7076 #endif /* HAVE_SYS_WAIT_H */
7077 #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
7078 {"fstatvfs", posix_fstatvfs
, METH_VARARGS
, posix_fstatvfs__doc__
},
7080 #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
7081 {"statvfs", posix_statvfs
, METH_VARARGS
, posix_statvfs__doc__
},
7084 {"tmpfile", posix_tmpfile
, METH_NOARGS
, posix_tmpfile__doc__
},
7087 {"tempnam", posix_tempnam
, METH_VARARGS
, posix_tempnam__doc__
},
7090 {"tmpnam", posix_tmpnam
, METH_NOARGS
, posix_tmpnam__doc__
},
7093 {"confstr", posix_confstr
, METH_VARARGS
, posix_confstr__doc__
},
7096 {"sysconf", posix_sysconf
, METH_VARARGS
, posix_sysconf__doc__
},
7098 #ifdef HAVE_FPATHCONF
7099 {"fpathconf", posix_fpathconf
, METH_VARARGS
, posix_fpathconf__doc__
},
7101 #ifdef HAVE_PATHCONF
7102 {"pathconf", posix_pathconf
, METH_VARARGS
, posix_pathconf__doc__
},
7104 {"abort", posix_abort
, METH_NOARGS
, posix_abort__doc__
},
7105 #ifdef HAVE_SETRESUID
7106 {"setresuid", posix_setresuid
, METH_VARARGS
, posix_setresuid__doc__
},
7108 #ifdef HAVE_SETRESGID
7109 {"setresgid", posix_setresgid
, METH_VARARGS
, posix_setresgid__doc__
},
7111 #ifdef HAVE_GETRESUID
7112 {"getresuid", posix_getresuid
, METH_NOARGS
, posix_getresuid__doc__
},
7114 #ifdef HAVE_GETRESGID
7115 {"getresgid", posix_getresgid
, METH_NOARGS
, posix_getresgid__doc__
},
7118 {NULL
, NULL
} /* Sentinel */
7123 ins(PyObject
*module
, char *symbol
, long value
)
7125 return PyModule_AddIntConstant(module
, symbol
, value
);
7129 all_ins(PyObject
*d
)
7132 if (ins(d
, "F_OK", (long)F_OK
)) return -1;
7135 if (ins(d
, "R_OK", (long)R_OK
)) return -1;
7138 if (ins(d
, "W_OK", (long)W_OK
)) return -1;
7141 if (ins(d
, "X_OK", (long)X_OK
)) return -1;
7144 if (ins(d
, "NGROUPS_MAX", (long)NGROUPS_MAX
)) return -1;
7147 if (ins(d
, "TMP_MAX", (long)TMP_MAX
)) return -1;
7150 if (ins(d
, "WCONTINUED", (long)WCONTINUED
)) return -1;
7153 if (ins(d
, "WNOHANG", (long)WNOHANG
)) return -1;
7156 if (ins(d
, "WUNTRACED", (long)WUNTRACED
)) return -1;
7159 if (ins(d
, "O_RDONLY", (long)O_RDONLY
)) return -1;
7162 if (ins(d
, "O_WRONLY", (long)O_WRONLY
)) return -1;
7165 if (ins(d
, "O_RDWR", (long)O_RDWR
)) return -1;
7168 if (ins(d
, "O_NDELAY", (long)O_NDELAY
)) return -1;
7171 if (ins(d
, "O_NONBLOCK", (long)O_NONBLOCK
)) return -1;
7174 if (ins(d
, "O_APPEND", (long)O_APPEND
)) return -1;
7177 if (ins(d
, "O_DSYNC", (long)O_DSYNC
)) return -1;
7180 if (ins(d
, "O_RSYNC", (long)O_RSYNC
)) return -1;
7183 if (ins(d
, "O_SYNC", (long)O_SYNC
)) return -1;
7186 if (ins(d
, "O_NOCTTY", (long)O_NOCTTY
)) return -1;
7189 if (ins(d
, "O_CREAT", (long)O_CREAT
)) return -1;
7192 if (ins(d
, "O_EXCL", (long)O_EXCL
)) return -1;
7195 if (ins(d
, "O_TRUNC", (long)O_TRUNC
)) return -1;
7198 if (ins(d
, "O_BINARY", (long)O_BINARY
)) return -1;
7201 if (ins(d
, "O_TEXT", (long)O_TEXT
)) return -1;
7204 if (ins(d
, "O_LARGEFILE", (long)O_LARGEFILE
)) return -1;
7207 if (ins(d
, "O_SHLOCK", (long)O_SHLOCK
)) return -1;
7210 if (ins(d
, "O_EXLOCK", (long)O_EXLOCK
)) return -1;
7215 /* Don't inherit in child processes. */
7216 if (ins(d
, "O_NOINHERIT", (long)O_NOINHERIT
)) return -1;
7218 #ifdef _O_SHORT_LIVED
7219 /* Optimize for short life (keep in memory). */
7220 /* MS forgot to define this one with a non-underscore form too. */
7221 if (ins(d
, "O_SHORT_LIVED", (long)_O_SHORT_LIVED
)) return -1;
7224 /* Automatically delete when last handle is closed. */
7225 if (ins(d
, "O_TEMPORARY", (long)O_TEMPORARY
)) return -1;
7228 /* Optimize for random access. */
7229 if (ins(d
, "O_RANDOM", (long)O_RANDOM
)) return -1;
7232 /* Optimize for sequential access. */
7233 if (ins(d
, "O_SEQUENTIAL", (long)O_SEQUENTIAL
)) return -1;
7236 /* GNU extensions. */
7238 /* Send a SIGIO signal whenever input or output
7239 becomes available on file descriptor */
7240 if (ins(d
, "O_ASYNC", (long)O_ASYNC
)) return -1;
7243 /* Direct disk access. */
7244 if (ins(d
, "O_DIRECT", (long)O_DIRECT
)) return -1;
7247 /* Must be a directory. */
7248 if (ins(d
, "O_DIRECTORY", (long)O_DIRECTORY
)) return -1;
7251 /* Do not follow links. */
7252 if (ins(d
, "O_NOFOLLOW", (long)O_NOFOLLOW
)) return -1;
7255 /* Do not update the access time. */
7256 if (ins(d
, "O_NOATIME", (long)O_NOATIME
)) return -1;
7259 /* These come from sysexits.h */
7261 if (ins(d
, "EX_OK", (long)EX_OK
)) return -1;
7264 if (ins(d
, "EX_USAGE", (long)EX_USAGE
)) return -1;
7265 #endif /* EX_USAGE */
7267 if (ins(d
, "EX_DATAERR", (long)EX_DATAERR
)) return -1;
7268 #endif /* EX_DATAERR */
7270 if (ins(d
, "EX_NOINPUT", (long)EX_NOINPUT
)) return -1;
7271 #endif /* EX_NOINPUT */
7273 if (ins(d
, "EX_NOUSER", (long)EX_NOUSER
)) return -1;
7274 #endif /* EX_NOUSER */
7276 if (ins(d
, "EX_NOHOST", (long)EX_NOHOST
)) return -1;
7277 #endif /* EX_NOHOST */
7278 #ifdef EX_UNAVAILABLE
7279 if (ins(d
, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE
)) return -1;
7280 #endif /* EX_UNAVAILABLE */
7282 if (ins(d
, "EX_SOFTWARE", (long)EX_SOFTWARE
)) return -1;
7283 #endif /* EX_SOFTWARE */
7285 if (ins(d
, "EX_OSERR", (long)EX_OSERR
)) return -1;
7286 #endif /* EX_OSERR */
7288 if (ins(d
, "EX_OSFILE", (long)EX_OSFILE
)) return -1;
7289 #endif /* EX_OSFILE */
7291 if (ins(d
, "EX_CANTCREAT", (long)EX_CANTCREAT
)) return -1;
7292 #endif /* EX_CANTCREAT */
7294 if (ins(d
, "EX_IOERR", (long)EX_IOERR
)) return -1;
7295 #endif /* EX_IOERR */
7297 if (ins(d
, "EX_TEMPFAIL", (long)EX_TEMPFAIL
)) return -1;
7298 #endif /* EX_TEMPFAIL */
7300 if (ins(d
, "EX_PROTOCOL", (long)EX_PROTOCOL
)) return -1;
7301 #endif /* EX_PROTOCOL */
7303 if (ins(d
, "EX_NOPERM", (long)EX_NOPERM
)) return -1;
7304 #endif /* EX_NOPERM */
7306 if (ins(d
, "EX_CONFIG", (long)EX_CONFIG
)) return -1;
7307 #endif /* EX_CONFIG */
7309 if (ins(d
, "EX_NOTFOUND", (long)EX_NOTFOUND
)) return -1;
7310 #endif /* EX_NOTFOUND */
7313 #if defined(PYOS_OS2) && defined(PYCC_GCC)
7314 if (ins(d
, "P_WAIT", (long)P_WAIT
)) return -1;
7315 if (ins(d
, "P_NOWAIT", (long)P_NOWAIT
)) return -1;
7316 if (ins(d
, "P_OVERLAY", (long)P_OVERLAY
)) return -1;
7317 if (ins(d
, "P_DEBUG", (long)P_DEBUG
)) return -1;
7318 if (ins(d
, "P_SESSION", (long)P_SESSION
)) return -1;
7319 if (ins(d
, "P_DETACH", (long)P_DETACH
)) return -1;
7320 if (ins(d
, "P_PM", (long)P_PM
)) return -1;
7321 if (ins(d
, "P_DEFAULT", (long)P_DEFAULT
)) return -1;
7322 if (ins(d
, "P_MINIMIZE", (long)P_MINIMIZE
)) return -1;
7323 if (ins(d
, "P_MAXIMIZE", (long)P_MAXIMIZE
)) return -1;
7324 if (ins(d
, "P_FULLSCREEN", (long)P_FULLSCREEN
)) return -1;
7325 if (ins(d
, "P_WINDOWED", (long)P_WINDOWED
)) return -1;
7326 if (ins(d
, "P_FOREGROUND", (long)P_FOREGROUND
)) return -1;
7327 if (ins(d
, "P_BACKGROUND", (long)P_BACKGROUND
)) return -1;
7328 if (ins(d
, "P_NOCLOSE", (long)P_NOCLOSE
)) return -1;
7329 if (ins(d
, "P_NOSESSION", (long)P_NOSESSION
)) return -1;
7330 if (ins(d
, "P_QUOTE", (long)P_QUOTE
)) return -1;
7331 if (ins(d
, "P_TILDE", (long)P_TILDE
)) return -1;
7332 if (ins(d
, "P_UNRELATED", (long)P_UNRELATED
)) return -1;
7333 if (ins(d
, "P_DEBUGDESC", (long)P_DEBUGDESC
)) return -1;
7335 if (ins(d
, "P_WAIT", (long)_P_WAIT
)) return -1;
7336 if (ins(d
, "P_NOWAIT", (long)_P_NOWAIT
)) return -1;
7337 if (ins(d
, "P_OVERLAY", (long)_OLD_P_OVERLAY
)) return -1;
7338 if (ins(d
, "P_NOWAITO", (long)_P_NOWAITO
)) return -1;
7339 if (ins(d
, "P_DETACH", (long)_P_DETACH
)) return -1;
7345 #define INITFUNC initedk2
7346 #define MODNAME "edk2"
7353 #ifndef UEFI_C_SOURCE
7357 m
= Py_InitModule3(MODNAME
,
7363 #ifndef UEFI_C_SOURCE
7364 /* Initialize environ dictionary */
7365 v
= convertenviron();
7367 if (v
== NULL
|| PyModule_AddObject(m
, "environ", v
) != 0)
7370 #endif /* UEFI_C_SOURCE */
7375 if (setup_confname_tables(m
))
7378 Py_INCREF(PyExc_OSError
);
7379 PyModule_AddObject(m
, "error", PyExc_OSError
);
7382 if (posix_putenv_garbage
== NULL
)
7383 posix_putenv_garbage
= PyDict_New();
7387 stat_result_desc
.name
= MODNAME
".stat_result";
7388 stat_result_desc
.fields
[2].name
= PyStructSequence_UnnamedField
;
7389 stat_result_desc
.fields
[3].name
= PyStructSequence_UnnamedField
;
7390 stat_result_desc
.fields
[4].name
= PyStructSequence_UnnamedField
;
7391 PyStructSequence_InitType(&StatResultType
, &stat_result_desc
);
7392 structseq_new
= StatResultType
.tp_new
;
7393 StatResultType
.tp_new
= statresult_new
;
7395 //statvfs_result_desc.name = MODNAME ".statvfs_result";
7396 //PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
7397 #ifdef NEED_TICKS_PER_SECOND
7398 # if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
7399 ticks_per_second
= sysconf(_SC_CLK_TCK
);
7401 ticks_per_second
= HZ
;
7403 ticks_per_second
= 60; /* magic fallback value; may be bogus */
7407 Py_INCREF((PyObject
*) &StatResultType
);
7408 PyModule_AddObject(m
, "stat_result", (PyObject
*) &StatResultType
);
7409 //Py_INCREF((PyObject*) &StatVFSResultType);
7410 //PyModule_AddObject(m, "statvfs_result",
7411 // (PyObject*) &StatVFSResultType);