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 SPDX-License-Identifier: BSD-2-Clause-Patent
8 #define PY_SSIZE_T_CLEAN
11 #include "structseq.h"
16 #include <sys/syslimits.h>
22 PyDoc_STRVAR(edk2__doc__
,
23 "This module provides access to UEFI firmware functionality that is\n\
24 standardized by the C Standard and the POSIX standard (a thinly\n\
25 disguised Unix interface). Refer to the library manual and\n\
26 corresponding UEFI Specification entries for more information on calls.");
28 #ifndef Py_USING_UNICODE
29 /* This is used in signatures of functions. */
30 #define Py_UNICODE void
33 #ifdef HAVE_SYS_TYPES_H
34 #include <sys/types.h>
35 #endif /* HAVE_SYS_TYPES_H */
37 #ifdef HAVE_SYS_STAT_H
39 #endif /* HAVE_SYS_STAT_H */
41 #ifdef HAVE_SYS_WAIT_H
42 #include <sys/wait.h> /* For WNOHANG */
51 #endif /* HAVE_FCNTL_H */
57 #ifdef HAVE_SYSEXITS_H
59 #endif /* HAVE_SYSEXITS_H */
61 #ifdef HAVE_SYS_LOADAVG_H
62 #include <sys/loadavg.h>
67 #endif /* HAVE_UTIME_H */
69 #ifdef HAVE_SYS_UTIME_H
70 #include <sys/utime.h>
71 #define HAVE_UTIME_H /* pretend we do for the rest of this file */
72 #endif /* HAVE_SYS_UTIME_H */
74 #ifdef HAVE_SYS_TIMES_H
75 #include <sys/times.h>
76 #endif /* HAVE_SYS_TIMES_H */
78 #ifdef HAVE_SYS_PARAM_H
79 #include <sys/param.h>
80 #endif /* HAVE_SYS_PARAM_H */
82 #ifdef HAVE_SYS_UTSNAME_H
83 #include <sys/utsname.h>
84 #endif /* HAVE_SYS_UTSNAME_H */
88 #define NAMLEN(dirent) wcslen((dirent)->FileName)
91 #define NAMLEN(dirent) (dirent)->d_namlen
92 #ifdef HAVE_SYS_NDIR_H
104 #if defined(PATH_MAX) && PATH_MAX > 1024
105 #define MAXPATHLEN PATH_MAX
107 #define MAXPATHLEN 1024
109 #endif /* MAXPATHLEN */
111 #define WAIT_TYPE int
112 #define WAIT_STATUS_INT(s) (s)
114 /* Issue #1983: pid_t can be longer than a C long on some systems */
115 #if !defined(SIZEOF_PID_T) || SIZEOF_PID_T == SIZEOF_INT
116 #define PARSE_PID "i"
117 #define PyLong_FromPid PyInt_FromLong
118 #define PyLong_AsPid PyInt_AsLong
119 #elif SIZEOF_PID_T == SIZEOF_LONG
120 #define PARSE_PID "l"
121 #define PyLong_FromPid PyInt_FromLong
122 #define PyLong_AsPid PyInt_AsLong
123 #elif defined(SIZEOF_LONG_LONG) && SIZEOF_PID_T == SIZEOF_LONG_LONG
124 #define PARSE_PID "L"
125 #define PyLong_FromPid PyLong_FromLongLong
126 #define PyLong_AsPid PyInt_AsLongLong
128 #error "sizeof(pid_t) is neither sizeof(int), sizeof(long) or sizeof(long long)"
129 #endif /* SIZEOF_PID_T */
131 /* Don't use the "_r" form if we don't need it (also, won't have a
132 prototype for it, at least on Solaris -- maybe others as well?). */
133 #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
134 #define USE_CTERMID_R
137 #if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD)
141 /* choose the appropriate stat and fstat functions and return structs */
147 #define STRUCT_STAT struct stat
149 /* dummy version. _PyVerify_fd() is already defined in fileobject.h */
150 #define _PyVerify_fd_dup2(A, B) (1)
152 #ifndef UEFI_C_SOURCE
153 /* Return a dictionary corresponding to the POSIX environment table */
154 extern char **environ
;
166 /* This part ignores errors */
167 for (e
= environ
; *e
!= NULL
; e
++) {
170 char *p
= strchr(*e
, '=');
173 k
= PyString_FromStringAndSize(*e
, (int)(p
-*e
));
178 v
= PyString_FromString(p
+1);
184 if (PyDict_GetItem(d
, k
) == NULL
) {
185 if (PyDict_SetItem(d
, k
, v
) != 0)
193 #endif /* UEFI_C_SOURCE */
195 /* Set a POSIX-specific error from errno, and return NULL */
200 return PyErr_SetFromErrno(PyExc_OSError
);
203 posix_error_with_filename(char* name
)
205 return PyErr_SetFromErrnoWithFilename(PyExc_OSError
, name
);
210 posix_error_with_allocated_filename(char* name
)
212 PyObject
*rc
= PyErr_SetFromErrnoWithFilename(PyExc_OSError
, name
);
217 /* POSIX generic methods */
219 #ifndef UEFI_C_SOURCE
221 posix_fildes(PyObject
*fdobj
, int (*func
)(int))
225 fd
= PyObject_AsFileDescriptor(fdobj
);
228 if (!_PyVerify_fd(fd
))
229 return posix_error();
230 Py_BEGIN_ALLOW_THREADS
234 return posix_error();
238 #endif /* UEFI_C_SOURCE */
241 posix_1str(PyObject
*args
, char *format
, int (*func
)(const char*))
245 if (!PyArg_ParseTuple(args
, format
,
246 Py_FileSystemDefaultEncoding
, &path1
))
248 Py_BEGIN_ALLOW_THREADS
249 res
= (*func
)(path1
);
252 return posix_error_with_allocated_filename(path1
);
259 posix_2str(PyObject
*args
,
261 int (*func
)(const char *, const char *))
263 char *path1
= NULL
, *path2
= NULL
;
265 if (!PyArg_ParseTuple(args
, format
,
266 Py_FileSystemDefaultEncoding
, &path1
,
267 Py_FileSystemDefaultEncoding
, &path2
))
269 Py_BEGIN_ALLOW_THREADS
270 res
= (*func
)(path1
, path2
);
275 /* XXX how to report both path1 and path2??? */
276 return posix_error();
281 PyDoc_STRVAR(stat_result__doc__
,
282 "stat_result: Result from stat or lstat.\n\n\
283 This object may be accessed either as a tuple of\n\
284 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
285 or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
287 Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
288 or st_flags, they are available as attributes only.\n\
290 See os.stat for more information.");
292 static PyStructSequence_Field stat_result_fields
[] = {
293 {"st_mode", "protection bits"},
294 //{"st_ino", "inode"},
295 //{"st_dev", "device"},
296 //{"st_nlink", "number of hard links"},
297 //{"st_uid", "user ID of owner"},
298 //{"st_gid", "group ID of owner"},
299 {"st_size", "total size, in bytes"},
300 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
301 {NULL
, "integer time of last access"},
302 {NULL
, "integer time of last modification"},
303 {NULL
, "integer time of last change"},
304 {"st_atime", "time of last access"},
305 {"st_mtime", "time of last modification"},
306 {"st_ctime", "time of last change"},
307 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
308 {"st_blksize", "blocksize for filesystem I/O"},
310 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
311 {"st_blocks", "number of blocks allocated"},
313 #ifdef HAVE_STRUCT_STAT_ST_RDEV
314 {"st_rdev", "device type (if inode device)"},
316 #ifdef HAVE_STRUCT_STAT_ST_FLAGS
317 {"st_flags", "user defined flags for file"},
319 #ifdef HAVE_STRUCT_STAT_ST_GEN
320 {"st_gen", "generation number"},
322 #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
323 {"st_birthtime", "time of creation"},
328 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
329 #define ST_BLKSIZE_IDX 8
331 #define ST_BLKSIZE_IDX 12
334 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
335 #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
337 #define ST_BLOCKS_IDX ST_BLKSIZE_IDX
340 #ifdef HAVE_STRUCT_STAT_ST_RDEV
341 #define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
343 #define ST_RDEV_IDX ST_BLOCKS_IDX
346 #ifdef HAVE_STRUCT_STAT_ST_FLAGS
347 #define ST_FLAGS_IDX (ST_RDEV_IDX+1)
349 #define ST_FLAGS_IDX ST_RDEV_IDX
352 #ifdef HAVE_STRUCT_STAT_ST_GEN
353 #define ST_GEN_IDX (ST_FLAGS_IDX+1)
355 #define ST_GEN_IDX ST_FLAGS_IDX
358 #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
359 #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
361 #define ST_BIRTHTIME_IDX ST_GEN_IDX
364 static PyStructSequence_Desc stat_result_desc
= {
365 "stat_result", /* name */
366 stat_result__doc__
, /* doc */
371 #ifndef UEFI_C_SOURCE /* Not in UEFI */
372 PyDoc_STRVAR(statvfs_result__doc__
,
373 "statvfs_result: Result from statvfs or fstatvfs.\n\n\
374 This object may be accessed either as a tuple of\n\
375 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
376 or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
378 See os.statvfs for more information.");
380 static PyStructSequence_Field statvfs_result_fields
[] = {
394 static PyStructSequence_Desc statvfs_result_desc
= {
395 "statvfs_result", /* name */
396 statvfs_result__doc__
, /* doc */
397 statvfs_result_fields
,
401 static PyTypeObject StatVFSResultType
;
404 static int initialized
;
405 static PyTypeObject StatResultType
;
406 static newfunc structseq_new
;
409 statresult_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
411 PyStructSequence
*result
;
414 result
= (PyStructSequence
*)structseq_new(type
, args
, kwds
);
417 /* If we have been initialized from a tuple,
418 st_?time might be set to None. Initialize it
419 from the int slots. */
420 for (i
= 7; i
<= 9; i
++) {
421 if (result
->ob_item
[i
+3] == Py_None
) {
423 Py_INCREF(result
->ob_item
[i
]);
424 result
->ob_item
[i
+3] = result
->ob_item
[i
];
427 return (PyObject
*)result
;
432 /* If true, st_?time is float. */
433 #if defined(UEFI_C_SOURCE)
434 static int _stat_float_times
= 0;
436 static int _stat_float_times
= 1;
438 PyDoc_STRVAR(stat_float_times__doc__
,
439 "stat_float_times([newval]) -> oldval\n\n\
440 Determine whether os.[lf]stat represents time stamps as float objects.\n\
441 If newval is True, future calls to stat() return floats, if it is False,\n\
442 future calls return ints. \n\
443 If newval is omitted, return the current setting.\n");
446 stat_float_times(PyObject
* self
, PyObject
*args
)
450 if (!PyArg_ParseTuple(args
, "|i:stat_float_times", &newval
))
453 /* Return old value */
454 return PyBool_FromLong(_stat_float_times
);
455 _stat_float_times
= newval
;
459 #endif /* UEFI_C_SOURCE */
462 fill_time(PyObject
*v
, int index
, time_t sec
, unsigned long nsec
)
464 PyObject
*fval
,*ival
;
465 #if SIZEOF_TIME_T > SIZEOF_LONG
466 ival
= PyLong_FromLongLong((PY_LONG_LONG
)sec
);
468 ival
= PyInt_FromLong((long)sec
);
472 if (_stat_float_times
) {
473 fval
= PyFloat_FromDouble(sec
+ 1e-9*nsec
);
478 PyStructSequence_SET_ITEM(v
, index
, ival
);
479 PyStructSequence_SET_ITEM(v
, index
+3, fval
);
482 /* pack a system stat C structure into the Python stat tuple
483 (used by posix_stat() and posix_fstat()) */
485 _pystat_fromstructstat(STRUCT_STAT
*st
)
487 unsigned long ansec
, mnsec
, cnsec
;
488 PyObject
*v
= PyStructSequence_New(&StatResultType
);
492 PyStructSequence_SET_ITEM(v
, 0, PyInt_FromLong((long)st
->st_mode
));
493 PyStructSequence_SET_ITEM(v
, 1,
494 PyLong_FromLongLong((PY_LONG_LONG
)st
->st_size
));
496 ansec
= mnsec
= cnsec
= 0;
497 /* The index used by fill_time is the index of the integer time.
498 fill_time will add 3 to the index to get the floating time index.
500 fill_time(v
, 2, st
->st_atime
, ansec
);
501 fill_time(v
, 3, st
->st_mtime
, mnsec
);
502 fill_time(v
, 4, st
->st_mtime
, cnsec
);
504 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
505 PyStructSequence_SET_ITEM(v
, ST_BLKSIZE_IDX
,
506 PyInt_FromLong((long)st
->st_blksize
));
508 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
509 PyStructSequence_SET_ITEM(v
, ST_BLOCKS_IDX
,
510 PyInt_FromLong((long)st
->st_blocks
));
512 #ifdef HAVE_STRUCT_STAT_ST_RDEV
513 PyStructSequence_SET_ITEM(v
, ST_RDEV_IDX
,
514 PyInt_FromLong((long)st
->st_rdev
));
516 #ifdef HAVE_STRUCT_STAT_ST_GEN
517 PyStructSequence_SET_ITEM(v
, ST_GEN_IDX
,
518 PyInt_FromLong((long)st
->st_gen
));
520 #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
523 unsigned long bsec
,bnsec
;
524 bsec
= (long)st
->st_birthtime
;
525 #ifdef HAVE_STAT_TV_NSEC2
526 bnsec
= st
->st_birthtimespec
.tv_nsec
;
530 if (_stat_float_times
) {
531 val
= PyFloat_FromDouble(bsec
+ 1e-9*bnsec
);
533 val
= PyInt_FromLong((long)bsec
);
535 PyStructSequence_SET_ITEM(v
, ST_BIRTHTIME_IDX
,
539 #ifdef HAVE_STRUCT_STAT_ST_FLAGS
540 PyStructSequence_SET_ITEM(v
, ST_FLAGS_IDX
,
541 PyInt_FromLong((long)st
->st_flags
));
544 if (PyErr_Occurred()) {
553 posix_do_stat(PyObject
*self
, PyObject
*args
,
555 int (*statfunc
)(const char *, STRUCT_STAT
*),
557 int (*wstatfunc
)(const Py_UNICODE
*, STRUCT_STAT
*))
560 char *path
= NULL
; /* pass this to stat; do not free() it */
561 char *pathfree
= NULL
; /* this memory must be free'd */
565 if (!PyArg_ParseTuple(args
, format
,
566 Py_FileSystemDefaultEncoding
, &path
))
570 Py_BEGIN_ALLOW_THREADS
571 res
= (*statfunc
)(path
, &st
);
575 result
= posix_error_with_filename(pathfree
);
578 result
= _pystat_fromstructstat(&st
);
580 PyMem_Free(pathfree
);
586 PyDoc_STRVAR(posix_access__doc__
,
587 "access(path, mode) -> True if granted, False otherwise\n\n\
588 Use the real uid/gid to test for access to a path. Note that most\n\
589 operations will use the effective uid/gid, therefore this routine can\n\
590 be used in a suid/sgid environment to test if the invoking user has the\n\
591 specified access to the path. The mode argument can be F_OK to test\n\
592 existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
595 posix_access(PyObject
*self
, PyObject
*args
)
601 if (!PyArg_ParseTuple(args
, "eti:access",
602 Py_FileSystemDefaultEncoding
, &path
, &mode
))
604 Py_BEGIN_ALLOW_THREADS
605 res
= access(path
, mode
);
608 return PyBool_FromLong(res
== 0);
624 PyDoc_STRVAR(posix_chdir__doc__
,
626 Change the current working directory to the specified path.");
629 posix_chdir(PyObject
*self
, PyObject
*args
)
631 return posix_1str(args
, "et:chdir", chdir
);
634 PyDoc_STRVAR(posix_chmod__doc__
,
635 "chmod(path, mode)\n\n\
636 Change the access permissions of a file.");
639 posix_chmod(PyObject
*self
, PyObject
*args
)
644 if (!PyArg_ParseTuple(args
, "eti:chmod", Py_FileSystemDefaultEncoding
,
647 Py_BEGIN_ALLOW_THREADS
648 res
= chmod(path
, i
);
651 return posix_error_with_allocated_filename(path
);
658 PyDoc_STRVAR(posix_fchmod__doc__
,
659 "fchmod(fd, mode)\n\n\
660 Change the access permissions of the file given by file\n\
664 posix_fchmod(PyObject
*self
, PyObject
*args
)
667 if (!PyArg_ParseTuple(args
, "ii:fchmod", &fd
, &mode
))
669 Py_BEGIN_ALLOW_THREADS
670 res
= fchmod(fd
, mode
);
673 return posix_error();
676 #endif /* HAVE_FCHMOD */
679 PyDoc_STRVAR(posix_lchmod__doc__
,
680 "lchmod(path, mode)\n\n\
681 Change the access permissions of a file. If path is a symlink, this\n\
682 affects the link itself rather than the target.");
685 posix_lchmod(PyObject
*self
, PyObject
*args
)
690 if (!PyArg_ParseTuple(args
, "eti:lchmod", Py_FileSystemDefaultEncoding
,
693 Py_BEGIN_ALLOW_THREADS
694 res
= lchmod(path
, i
);
697 return posix_error_with_allocated_filename(path
);
701 #endif /* HAVE_LCHMOD */
705 PyDoc_STRVAR(posix_chflags__doc__
,
706 "chflags(path, flags)\n\n\
710 posix_chflags(PyObject
*self
, PyObject
*args
)
715 if (!PyArg_ParseTuple(args
, "etk:chflags",
716 Py_FileSystemDefaultEncoding
, &path
, &flags
))
718 Py_BEGIN_ALLOW_THREADS
719 res
= chflags(path
, flags
);
722 return posix_error_with_allocated_filename(path
);
727 #endif /* HAVE_CHFLAGS */
730 PyDoc_STRVAR(posix_lchflags__doc__
,
731 "lchflags(path, flags)\n\n\
733 This function will not follow symbolic links.");
736 posix_lchflags(PyObject
*self
, PyObject
*args
)
741 if (!PyArg_ParseTuple(args
, "etk:lchflags",
742 Py_FileSystemDefaultEncoding
, &path
, &flags
))
744 Py_BEGIN_ALLOW_THREADS
745 res
= lchflags(path
, flags
);
748 return posix_error_with_allocated_filename(path
);
753 #endif /* HAVE_LCHFLAGS */
756 PyDoc_STRVAR(posix_chroot__doc__
,
758 Change root directory to path.");
761 posix_chroot(PyObject
*self
, PyObject
*args
)
763 return posix_1str(args
, "et:chroot", chroot
);
768 PyDoc_STRVAR(posix_fsync__doc__
,
770 force write of file with filedescriptor to disk.");
773 posix_fsync(PyObject
*self
, PyObject
*fdobj
)
775 return posix_fildes(fdobj
, fsync
);
777 #endif /* HAVE_FSYNC */
779 #ifdef HAVE_FDATASYNC
782 extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
785 PyDoc_STRVAR(posix_fdatasync__doc__
,
786 "fdatasync(fildes)\n\n\
787 force write of file with filedescriptor to disk.\n\
788 does not force update of metadata.");
791 posix_fdatasync(PyObject
*self
, PyObject
*fdobj
)
793 return posix_fildes(fdobj
, fdatasync
);
795 #endif /* HAVE_FDATASYNC */
799 PyDoc_STRVAR(posix_chown__doc__
,
800 "chown(path, uid, gid)\n\n\
801 Change the owner and group id of path to the numeric uid and gid.");
804 posix_chown(PyObject
*self
, PyObject
*args
)
809 if (!PyArg_ParseTuple(args
, "etll:chown",
810 Py_FileSystemDefaultEncoding
, &path
,
813 Py_BEGIN_ALLOW_THREADS
814 res
= chown(path
, (uid_t
) uid
, (gid_t
) gid
);
817 return posix_error_with_allocated_filename(path
);
822 #endif /* HAVE_CHOWN */
825 PyDoc_STRVAR(posix_fchown__doc__
,
826 "fchown(fd, uid, gid)\n\n\
827 Change the owner and group id of the file given by file descriptor\n\
828 fd to the numeric uid and gid.");
831 posix_fchown(PyObject
*self
, PyObject
*args
)
836 if (!PyArg_ParseTuple(args
, "ill:chown", &fd
, &uid
, &gid
))
838 Py_BEGIN_ALLOW_THREADS
839 res
= fchown(fd
, (uid_t
) uid
, (gid_t
) gid
);
842 return posix_error();
845 #endif /* HAVE_FCHOWN */
848 PyDoc_STRVAR(posix_lchown__doc__
,
849 "lchown(path, uid, gid)\n\n\
850 Change the owner and group id of path to the numeric uid and gid.\n\
851 This function will not follow symbolic links.");
854 posix_lchown(PyObject
*self
, PyObject
*args
)
859 if (!PyArg_ParseTuple(args
, "etll:lchown",
860 Py_FileSystemDefaultEncoding
, &path
,
863 Py_BEGIN_ALLOW_THREADS
864 res
= lchown(path
, (uid_t
) uid
, (gid_t
) gid
);
867 return posix_error_with_allocated_filename(path
);
872 #endif /* HAVE_LCHOWN */
876 PyDoc_STRVAR(posix_getcwd__doc__
,
877 "getcwd() -> path\n\n\
878 Return a string representing the current working directory.");
881 posix_getcwd(PyObject
*self
, PyObject
*noargs
)
883 int bufsize_incr
= 1024;
887 PyObject
*dynamic_return
;
889 Py_BEGIN_ALLOW_THREADS
891 bufsize
= bufsize
+ bufsize_incr
;
892 tmpbuf
= malloc(bufsize
);
893 if (tmpbuf
== NULL
) {
896 res
= getcwd(tmpbuf
, bufsize
);
900 } while ((res
== NULL
) && (errno
== ERANGE
));
904 return posix_error();
906 dynamic_return
= PyString_FromString(tmpbuf
);
909 return dynamic_return
;
912 #ifdef Py_USING_UNICODE
913 PyDoc_STRVAR(posix_getcwdu__doc__
,
914 "getcwdu() -> path\n\n\
915 Return a unicode string representing the current working directory.");
918 posix_getcwdu(PyObject
*self
, PyObject
*noargs
)
923 Py_BEGIN_ALLOW_THREADS
924 res
= getcwd(buf
, sizeof buf
);
927 return posix_error();
928 return PyUnicode_Decode(buf
, strlen(buf
), Py_FileSystemDefaultEncoding
,"strict");
930 #endif /* Py_USING_UNICODE */
931 #endif /* HAVE_GETCWD */
934 PyDoc_STRVAR(posix_listdir__doc__
,
935 "listdir(path) -> list_of_strings\n\n\
936 Return a list containing the names of the entries in the directory.\n\
938 path: path of directory to list\n\
940 The list is in arbitrary order. It does not include the special\n\
941 entries '.' and '..' even if they are present in the directory.");
944 posix_listdir(PyObject
*self
, PyObject
*args
)
946 /* XXX Should redo this putting the (now four) versions of opendir
947 in separate files instead of having them all here... */
954 int arg_is_unicode
= 1;
957 if (!PyArg_ParseTuple(args
, "U:listdir", &v
)) {
961 if (!PyArg_ParseTuple(args
, "et:listdir", Py_FileSystemDefaultEncoding
, &name
))
963 Py_BEGIN_ALLOW_THREADS
964 dirp
= opendir(name
);
967 return posix_error_with_allocated_filename(name
);
969 if ((d
= PyList_New(0)) == NULL
) {
970 Py_BEGIN_ALLOW_THREADS
976 if((MBname
= malloc(NAME_MAX
)) == NULL
) {
977 Py_BEGIN_ALLOW_THREADS
986 Py_BEGIN_ALLOW_THREADS
990 if ((errno
== 0) || (errno
== EISDIR
)) {
993 Py_BEGIN_ALLOW_THREADS
997 return posix_error_with_allocated_filename(name
);
1000 if (ep
->FileName
[0] == L
'.' &&
1002 (ep
->FileName
[1] == L
'.' && NAMLEN(ep
) == 2)))
1004 if(wcstombs(MBname
, ep
->FileName
, NAME_MAX
) == -1) {
1006 Py_BEGIN_ALLOW_THREADS
1008 Py_END_ALLOW_THREADS
1013 v
= PyString_FromStringAndSize(MBname
, strlen(MBname
));
1019 #ifdef Py_USING_UNICODE
1020 if (arg_is_unicode
) {
1023 w
= PyUnicode_FromEncodedObject(v
,
1024 Py_FileSystemDefaultEncoding
,
1031 /* fall back to the original byte string, as
1032 discussed in patch #683592 */
1037 if (PyList_Append(d
, v
) != 0) {
1045 Py_BEGIN_ALLOW_THREADS
1047 Py_END_ALLOW_THREADS
1049 if(MBname
!= NULL
) {
1055 } /* end of posix_listdir */
1058 /* A helper function for abspath on win32 */
1060 posix__getfullpathname(PyObject
*self
, PyObject
*args
)
1062 /* assume encoded strings won't more than double no of chars */
1063 char inbuf
[MAX_PATH
*2];
1064 char *inbufp
= inbuf
;
1065 Py_ssize_t insize
= sizeof(inbuf
);
1066 char outbuf
[MAX_PATH
*2];
1069 PyUnicodeObject
*po
;
1070 if (PyArg_ParseTuple(args
, "U|:_getfullpathname", &po
)) {
1071 Py_UNICODE
*wpath
= PyUnicode_AS_UNICODE(po
);
1072 Py_UNICODE woutbuf
[MAX_PATH
*2], *woutbufp
= woutbuf
;
1076 result
= GetFullPathNameW(wpath
,
1077 sizeof(woutbuf
)/sizeof(woutbuf
[0]),
1079 if (result
> sizeof(woutbuf
)/sizeof(woutbuf
[0])) {
1080 woutbufp
= malloc(result
* sizeof(Py_UNICODE
));
1082 return PyErr_NoMemory();
1083 result
= GetFullPathNameW(wpath
, result
, woutbufp
, &wtemp
);
1086 v
= PyUnicode_FromUnicode(woutbufp
, wcslen(woutbufp
));
1088 v
= win32_error_unicode("GetFullPathNameW", wpath
);
1089 if (woutbufp
!= woutbuf
)
1093 /* Drop the argument parsing error as narrow strings
1097 if (!PyArg_ParseTuple (args
, "et#:_getfullpathname",
1098 Py_FileSystemDefaultEncoding
, &inbufp
,
1101 if (!GetFullPathName(inbuf
, sizeof(outbuf
)/sizeof(outbuf
[0]),
1103 return win32_error("GetFullPathName", inbuf
);
1104 if (PyUnicode_Check(PyTuple_GetItem(args
, 0))) {
1105 return PyUnicode_Decode(outbuf
, strlen(outbuf
),
1106 Py_FileSystemDefaultEncoding
, NULL
);
1108 return PyString_FromString(outbuf
);
1109 } /* end of posix__getfullpathname */
1110 #endif /* MS_WINDOWS */
1112 PyDoc_STRVAR(posix_mkdir__doc__
,
1113 "mkdir(path [, mode=0777])\n\n\
1114 Create a directory.");
1117 posix_mkdir(PyObject
*self
, PyObject
*args
)
1123 if (!PyArg_ParseTuple(args
, "et|i:mkdir",
1124 Py_FileSystemDefaultEncoding
, &path
, &mode
))
1126 Py_BEGIN_ALLOW_THREADS
1127 res
= mkdir(path
, mode
);
1128 Py_END_ALLOW_THREADS
1130 return posix_error_with_allocated_filename(path
);
1137 /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
1138 #if defined(HAVE_SYS_RESOURCE_H)
1139 #include <sys/resource.h>
1144 PyDoc_STRVAR(posix_nice__doc__
,
1145 "nice(inc) -> new_priority\n\n\
1146 Decrease the priority of process by inc and return the new priority.");
1149 posix_nice(PyObject
*self
, PyObject
*args
)
1151 int increment
, value
;
1153 if (!PyArg_ParseTuple(args
, "i:nice", &increment
))
1156 /* There are two flavours of 'nice': one that returns the new
1157 priority (as required by almost all standards out there) and the
1158 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
1159 the use of getpriority() to get the new priority.
1161 If we are of the nice family that returns the new priority, we
1162 need to clear errno before the call, and check if errno is filled
1163 before calling posix_error() on a returnvalue of -1, because the
1164 -1 may be the actual new priority! */
1167 value
= nice(increment
);
1168 #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
1170 value
= getpriority(PRIO_PROCESS
, 0);
1172 if (value
== -1 && errno
!= 0)
1173 /* either nice() or getpriority() returned an error */
1174 return posix_error();
1175 return PyInt_FromLong((long) value
);
1177 #endif /* HAVE_NICE */
1179 PyDoc_STRVAR(posix_rename__doc__
,
1180 "rename(old, new)\n\n\
1181 Rename a file or directory.");
1184 posix_rename(PyObject
*self
, PyObject
*args
)
1186 return posix_2str(args
, "etet:rename", rename
);
1190 PyDoc_STRVAR(posix_rmdir__doc__
,
1192 Remove a directory.");
1195 posix_rmdir(PyObject
*self
, PyObject
*args
)
1197 return posix_1str(args
, "et:rmdir", rmdir
);
1201 PyDoc_STRVAR(posix_stat__doc__
,
1202 "stat(path) -> stat result\n\n\
1203 Perform a stat system call on the given path.");
1206 posix_stat(PyObject
*self
, PyObject
*args
)
1208 return posix_do_stat(self
, args
, "et:stat", STAT
, NULL
, NULL
);
1213 PyDoc_STRVAR(posix_system__doc__
,
1214 "system(command) -> exit_status\n\n\
1215 Execute the command (a string) in a subshell.");
1218 posix_system(PyObject
*self
, PyObject
*args
)
1222 if (!PyArg_ParseTuple(args
, "s:system", &command
))
1224 Py_BEGIN_ALLOW_THREADS
1225 sts
= system(command
);
1226 Py_END_ALLOW_THREADS
1227 return PyInt_FromLong(sts
);
1232 PyDoc_STRVAR(posix_umask__doc__
,
1233 "umask(new_mask) -> old_mask\n\n\
1234 Set the current numeric umask and return the previous umask.");
1237 posix_umask(PyObject
*self
, PyObject
*args
)
1240 if (!PyArg_ParseTuple(args
, "i:umask", &i
))
1244 return posix_error();
1245 return PyInt_FromLong((long)i
);
1249 PyDoc_STRVAR(posix_unlink__doc__
,
1251 Remove a file (same as remove(path)).");
1253 PyDoc_STRVAR(posix_remove__doc__
,
1255 Remove a file (same as unlink(path)).");
1258 posix_unlink(PyObject
*self
, PyObject
*args
)
1260 return posix_1str(args
, "et:remove", unlink
);
1265 extract_time(PyObject
*t
, time_t* sec
, long* usec
)
1268 if (PyFloat_Check(t
)) {
1269 double tval
= PyFloat_AsDouble(t
);
1270 PyObject
*intobj
= PyNumber_Long(t
);
1273 #if SIZEOF_TIME_T > SIZEOF_LONG
1274 intval
= PyInt_AsUnsignedLongLongMask(intobj
);
1276 intval
= PyInt_AsLong(intobj
);
1279 if (intval
== -1 && PyErr_Occurred())
1282 *usec
= (long)((tval
- intval
) * 1e6
); /* can't exceed 1000000 */
1284 /* If rounding gave us a negative number,
1289 #if SIZEOF_TIME_T > SIZEOF_LONG
1290 intval
= PyInt_AsUnsignedLongLongMask(t
);
1292 intval
= PyInt_AsLong(t
);
1294 if (intval
== -1 && PyErr_Occurred())
1301 PyDoc_STRVAR(posix_utime__doc__
,
1302 "utime(path, (atime, mtime))\n\
1303 utime(path, None)\n\n\
1304 Set the access and modified time of the file to the given values. If the\n\
1305 second form is used, set the access and modified times to the current time.");
1308 posix_utime(PyObject
*self
, PyObject
*args
)
1311 time_t atime
, mtime
;
1316 #if defined(HAVE_UTIMES)
1317 struct timeval buf
[2];
1318 #define ATIME buf[0].tv_sec
1319 #define MTIME buf[1].tv_sec
1320 #elif defined(HAVE_UTIME_H)
1321 /* XXX should define struct utimbuf instead, above */
1323 #define ATIME buf.actime
1324 #define MTIME buf.modtime
1325 #define UTIME_ARG &buf
1326 #else /* HAVE_UTIMES */
1328 #define ATIME buf[0]
1329 #define MTIME buf[1]
1330 #define UTIME_ARG buf
1331 #endif /* HAVE_UTIMES */
1334 if (!PyArg_ParseTuple(args
, "etO:utime",
1335 Py_FileSystemDefaultEncoding
, &path
, &arg
))
1337 if (arg
== Py_None
) {
1338 /* optional time values not given */
1339 Py_BEGIN_ALLOW_THREADS
1340 res
= utime(path
, NULL
);
1341 Py_END_ALLOW_THREADS
1343 else if (!PyTuple_Check(arg
) || PyTuple_Size(arg
) != 2) {
1344 PyErr_SetString(PyExc_TypeError
,
1345 "utime() arg 2 must be a tuple (atime, mtime)");
1350 if (extract_time(PyTuple_GET_ITEM(arg
, 0),
1351 &atime
, &ausec
) == -1) {
1355 if (extract_time(PyTuple_GET_ITEM(arg
, 1),
1356 &mtime
, &musec
) == -1) {
1363 buf
[0].tv_usec
= ausec
;
1364 buf
[1].tv_usec
= musec
;
1365 Py_BEGIN_ALLOW_THREADS
1366 res
= utimes(path
, buf
);
1367 Py_END_ALLOW_THREADS
1369 Py_BEGIN_ALLOW_THREADS
1370 res
= utime(path
, UTIME_ARG
);
1371 Py_END_ALLOW_THREADS
1372 #endif /* HAVE_UTIMES */
1375 return posix_error_with_allocated_filename(path
);
1386 /* Process operations */
1388 PyDoc_STRVAR(posix__exit__doc__
,
1390 Exit to the system with specified status, without normal exit processing.");
1393 posix__exit(PyObject
*self
, PyObject
*args
)
1396 if (!PyArg_ParseTuple(args
, "i:_exit", &sts
))
1399 return NULL
; /* Make gcc -Wall happy */
1402 #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
1404 free_string_array(char **array
, Py_ssize_t count
)
1407 for (i
= 0; i
< count
; i
++)
1408 PyMem_Free(array
[i
]);
1415 PyDoc_STRVAR(posix_execv__doc__
,
1416 "execv(path, args)\n\n\
1417 Execute an executable path with arguments, replacing current process.\n\
1419 path: path of executable file\n\
1420 args: tuple or list of strings");
1423 posix_execv(PyObject
*self
, PyObject
*args
)
1429 PyObject
*(*getitem
)(PyObject
*, Py_ssize_t
);
1431 /* execv has two arguments: (path, argv), where
1432 argv is a list or tuple of strings. */
1434 if (!PyArg_ParseTuple(args
, "etO:execv",
1435 Py_FileSystemDefaultEncoding
,
1438 if (PyList_Check(argv
)) {
1439 argc
= PyList_Size(argv
);
1440 getitem
= PyList_GetItem
;
1442 else if (PyTuple_Check(argv
)) {
1443 argc
= PyTuple_Size(argv
);
1444 getitem
= PyTuple_GetItem
;
1447 PyErr_SetString(PyExc_TypeError
, "execv() arg 2 must be a tuple or list");
1452 PyErr_SetString(PyExc_ValueError
, "execv() arg 2 must not be empty");
1457 argvlist
= PyMem_NEW(char *, argc
+1);
1458 if (argvlist
== NULL
) {
1460 return PyErr_NoMemory();
1462 for (i
= 0; i
< argc
; i
++) {
1463 if (!PyArg_Parse((*getitem
)(argv
, i
), "et",
1464 Py_FileSystemDefaultEncoding
,
1466 free_string_array(argvlist
, i
);
1467 PyErr_SetString(PyExc_TypeError
,
1468 "execv() arg 2 must contain only strings");
1474 argvlist
[argc
] = NULL
;
1476 execv(path
, argvlist
);
1478 /* If we get here it's definitely an error */
1480 free_string_array(argvlist
, argc
);
1482 return posix_error();
1486 PyDoc_STRVAR(posix_execve__doc__
,
1487 "execve(path, args, env)\n\n\
1488 Execute a path with arguments and environment, replacing current process.\n\
1490 path: path of executable file\n\
1491 args: tuple or list of arguments\n\
1492 env: dictionary of strings mapping to strings");
1495 posix_execve(PyObject
*self
, PyObject
*args
)
1498 PyObject
*argv
, *env
;
1501 PyObject
*key
, *val
, *keys
=NULL
, *vals
=NULL
;
1502 Py_ssize_t i
, pos
, argc
, envc
;
1503 PyObject
*(*getitem
)(PyObject
*, Py_ssize_t
);
1504 Py_ssize_t lastarg
= 0;
1506 /* execve has three arguments: (path, argv, env), where
1507 argv is a list or tuple of strings and env is a dictionary
1508 like posix.environ. */
1510 if (!PyArg_ParseTuple(args
, "etOO:execve",
1511 Py_FileSystemDefaultEncoding
,
1512 &path
, &argv
, &env
))
1514 if (PyList_Check(argv
)) {
1515 argc
= PyList_Size(argv
);
1516 getitem
= PyList_GetItem
;
1518 else if (PyTuple_Check(argv
)) {
1519 argc
= PyTuple_Size(argv
);
1520 getitem
= PyTuple_GetItem
;
1523 PyErr_SetString(PyExc_TypeError
,
1524 "execve() arg 2 must be a tuple or list");
1527 if (!PyMapping_Check(env
)) {
1528 PyErr_SetString(PyExc_TypeError
,
1529 "execve() arg 3 must be a mapping object");
1533 argvlist
= PyMem_NEW(char *, argc
+1);
1534 if (argvlist
== NULL
) {
1538 for (i
= 0; i
< argc
; i
++) {
1539 if (!PyArg_Parse((*getitem
)(argv
, i
),
1540 "et;execve() arg 2 must contain only strings",
1541 Py_FileSystemDefaultEncoding
,
1549 argvlist
[argc
] = NULL
;
1551 i
= PyMapping_Size(env
);
1554 envlist
= PyMem_NEW(char *, i
+ 1);
1555 if (envlist
== NULL
) {
1560 keys
= PyMapping_Keys(env
);
1561 vals
= PyMapping_Values(env
);
1564 if (!PyList_Check(keys
) || !PyList_Check(vals
)) {
1565 PyErr_SetString(PyExc_TypeError
,
1566 "execve(): env.keys() or env.values() is not a list");
1570 for (pos
= 0; pos
< i
; pos
++) {
1574 key
= PyList_GetItem(keys
, pos
);
1575 val
= PyList_GetItem(vals
, pos
);
1581 "s;execve() arg 3 contains a non-string key",
1585 "s;execve() arg 3 contains a non-string value",
1591 #if defined(PYOS_OS2)
1592 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
1593 if (stricmp(k
, "BEGINLIBPATH") != 0 && stricmp(k
, "ENDLIBPATH") != 0) {
1595 len
= PyString_Size(key
) + PyString_Size(val
) + 2;
1596 p
= PyMem_NEW(char, len
);
1601 PyOS_snprintf(p
, len
, "%s=%s", k
, v
);
1602 envlist
[envc
++] = p
;
1603 #if defined(PYOS_OS2)
1609 execve(path
, argvlist
, envlist
);
1611 /* If we get here it's definitely an error */
1613 (void) posix_error();
1617 PyMem_DEL(envlist
[envc
]);
1620 free_string_array(argvlist
, lastarg
);
1627 #endif /* HAVE_EXECV */
1631 PyDoc_STRVAR(posix_spawnv__doc__
,
1632 "spawnv(mode, path, args)\n\n\
1633 Execute the program 'path' in a new process.\n\
1635 mode: mode of process creation\n\
1636 path: path of executable file\n\
1637 args: tuple or list of strings");
1640 posix_spawnv(PyObject
*self
, PyObject
*args
)
1647 Py_intptr_t spawnval
;
1648 PyObject
*(*getitem
)(PyObject
*, Py_ssize_t
);
1650 /* spawnv has three arguments: (mode, path, argv), where
1651 argv is a list or tuple of strings. */
1653 if (!PyArg_ParseTuple(args
, "ietO:spawnv", &mode
,
1654 Py_FileSystemDefaultEncoding
,
1657 if (PyList_Check(argv
)) {
1658 argc
= PyList_Size(argv
);
1659 getitem
= PyList_GetItem
;
1661 else if (PyTuple_Check(argv
)) {
1662 argc
= PyTuple_Size(argv
);
1663 getitem
= PyTuple_GetItem
;
1666 PyErr_SetString(PyExc_TypeError
,
1667 "spawnv() arg 2 must be a tuple or list");
1672 argvlist
= PyMem_NEW(char *, argc
+1);
1673 if (argvlist
== NULL
) {
1675 return PyErr_NoMemory();
1677 for (i
= 0; i
< argc
; i
++) {
1678 if (!PyArg_Parse((*getitem
)(argv
, i
), "et",
1679 Py_FileSystemDefaultEncoding
,
1681 free_string_array(argvlist
, i
);
1684 "spawnv() arg 2 must contain only strings");
1689 argvlist
[argc
] = NULL
;
1691 #if defined(PYOS_OS2) && defined(PYCC_GCC)
1692 Py_BEGIN_ALLOW_THREADS
1693 spawnval
= spawnv(mode
, path
, argvlist
);
1694 Py_END_ALLOW_THREADS
1696 if (mode
== _OLD_P_OVERLAY
)
1699 Py_BEGIN_ALLOW_THREADS
1700 spawnval
= _spawnv(mode
, path
, argvlist
);
1701 Py_END_ALLOW_THREADS
1704 free_string_array(argvlist
, argc
);
1708 return posix_error();
1710 #if SIZEOF_LONG == SIZEOF_VOID_P
1711 return Py_BuildValue("l", (long) spawnval
);
1713 return Py_BuildValue("L", (PY_LONG_LONG
) spawnval
);
1718 PyDoc_STRVAR(posix_spawnve__doc__
,
1719 "spawnve(mode, path, args, env)\n\n\
1720 Execute the program 'path' in a new process.\n\
1722 mode: mode of process creation\n\
1723 path: path of executable file\n\
1724 args: tuple or list of arguments\n\
1725 env: dictionary of strings mapping to strings");
1728 posix_spawnve(PyObject
*self
, PyObject
*args
)
1731 PyObject
*argv
, *env
;
1734 PyObject
*key
, *val
, *keys
=NULL
, *vals
=NULL
, *res
=NULL
;
1735 int mode
, pos
, envc
;
1737 Py_intptr_t spawnval
;
1738 PyObject
*(*getitem
)(PyObject
*, Py_ssize_t
);
1739 Py_ssize_t lastarg
= 0;
1741 /* spawnve has four arguments: (mode, path, argv, env), where
1742 argv is a list or tuple of strings and env is a dictionary
1743 like posix.environ. */
1745 if (!PyArg_ParseTuple(args
, "ietOO:spawnve", &mode
,
1746 Py_FileSystemDefaultEncoding
,
1747 &path
, &argv
, &env
))
1749 if (PyList_Check(argv
)) {
1750 argc
= PyList_Size(argv
);
1751 getitem
= PyList_GetItem
;
1753 else if (PyTuple_Check(argv
)) {
1754 argc
= PyTuple_Size(argv
);
1755 getitem
= PyTuple_GetItem
;
1758 PyErr_SetString(PyExc_TypeError
,
1759 "spawnve() arg 2 must be a tuple or list");
1762 if (!PyMapping_Check(env
)) {
1763 PyErr_SetString(PyExc_TypeError
,
1764 "spawnve() arg 3 must be a mapping object");
1768 argvlist
= PyMem_NEW(char *, argc
+1);
1769 if (argvlist
== NULL
) {
1773 for (i
= 0; i
< argc
; i
++) {
1774 if (!PyArg_Parse((*getitem
)(argv
, i
),
1775 "et;spawnve() arg 2 must contain only strings",
1776 Py_FileSystemDefaultEncoding
,
1784 argvlist
[argc
] = NULL
;
1786 i
= PyMapping_Size(env
);
1789 envlist
= PyMem_NEW(char *, i
+ 1);
1790 if (envlist
== NULL
) {
1795 keys
= PyMapping_Keys(env
);
1796 vals
= PyMapping_Values(env
);
1799 if (!PyList_Check(keys
) || !PyList_Check(vals
)) {
1800 PyErr_SetString(PyExc_TypeError
,
1801 "spawnve(): env.keys() or env.values() is not a list");
1805 for (pos
= 0; pos
< i
; pos
++) {
1809 key
= PyList_GetItem(keys
, pos
);
1810 val
= PyList_GetItem(vals
, pos
);
1816 "s;spawnve() arg 3 contains a non-string key",
1820 "s;spawnve() arg 3 contains a non-string value",
1825 len
= PyString_Size(key
) + PyString_Size(val
) + 2;
1826 p
= PyMem_NEW(char, len
);
1831 PyOS_snprintf(p
, len
, "%s=%s", k
, v
);
1832 envlist
[envc
++] = p
;
1836 #if defined(PYOS_OS2) && defined(PYCC_GCC)
1837 Py_BEGIN_ALLOW_THREADS
1838 spawnval
= spawnve(mode
, path
, argvlist
, envlist
);
1839 Py_END_ALLOW_THREADS
1841 if (mode
== _OLD_P_OVERLAY
)
1844 Py_BEGIN_ALLOW_THREADS
1845 spawnval
= _spawnve(mode
, path
, argvlist
, envlist
);
1846 Py_END_ALLOW_THREADS
1850 (void) posix_error();
1852 #if SIZEOF_LONG == SIZEOF_VOID_P
1853 res
= Py_BuildValue("l", (long) spawnval
);
1855 res
= Py_BuildValue("L", (PY_LONG_LONG
) spawnval
);
1860 PyMem_DEL(envlist
[envc
]);
1863 free_string_array(argvlist
, lastarg
);
1871 /* OS/2 supports spawnvp & spawnvpe natively */
1872 #if defined(PYOS_OS2)
1873 PyDoc_STRVAR(posix_spawnvp__doc__
,
1874 "spawnvp(mode, file, args)\n\n\
1875 Execute the program 'file' in a new process, using the environment\n\
1876 search path to find the file.\n\
1878 mode: mode of process creation\n\
1879 file: executable file name\n\
1880 args: tuple or list of strings");
1883 posix_spawnvp(PyObject
*self
, PyObject
*args
)
1889 Py_intptr_t spawnval
;
1890 PyObject
*(*getitem
)(PyObject
*, Py_ssize_t
);
1892 /* spawnvp has three arguments: (mode, path, argv), where
1893 argv is a list or tuple of strings. */
1895 if (!PyArg_ParseTuple(args
, "ietO:spawnvp", &mode
,
1896 Py_FileSystemDefaultEncoding
,
1899 if (PyList_Check(argv
)) {
1900 argc
= PyList_Size(argv
);
1901 getitem
= PyList_GetItem
;
1903 else if (PyTuple_Check(argv
)) {
1904 argc
= PyTuple_Size(argv
);
1905 getitem
= PyTuple_GetItem
;
1908 PyErr_SetString(PyExc_TypeError
,
1909 "spawnvp() arg 2 must be a tuple or list");
1914 argvlist
= PyMem_NEW(char *, argc
+1);
1915 if (argvlist
== NULL
) {
1917 return PyErr_NoMemory();
1919 for (i
= 0; i
< argc
; i
++) {
1920 if (!PyArg_Parse((*getitem
)(argv
, i
), "et",
1921 Py_FileSystemDefaultEncoding
,
1923 free_string_array(argvlist
, i
);
1926 "spawnvp() arg 2 must contain only strings");
1931 argvlist
[argc
] = NULL
;
1933 Py_BEGIN_ALLOW_THREADS
1934 #if defined(PYCC_GCC)
1935 spawnval
= spawnvp(mode
, path
, argvlist
);
1937 spawnval
= _spawnvp(mode
, path
, argvlist
);
1939 Py_END_ALLOW_THREADS
1941 free_string_array(argvlist
, argc
);
1945 return posix_error();
1947 return Py_BuildValue("l", (long) spawnval
);
1951 PyDoc_STRVAR(posix_spawnvpe__doc__
,
1952 "spawnvpe(mode, file, args, env)\n\n\
1953 Execute the program 'file' in a new process, using the environment\n\
1954 search path to find the file.\n\
1956 mode: mode of process creation\n\
1957 file: executable file name\n\
1958 args: tuple or list of arguments\n\
1959 env: dictionary of strings mapping to strings");
1962 posix_spawnvpe(PyObject
*self
, PyObject
*args
)
1965 PyObject
*argv
, *env
;
1968 PyObject
*key
, *val
, *keys
=NULL
, *vals
=NULL
, *res
=NULL
;
1969 int mode
, i
, pos
, argc
, envc
;
1970 Py_intptr_t spawnval
;
1971 PyObject
*(*getitem
)(PyObject
*, Py_ssize_t
);
1974 /* spawnvpe has four arguments: (mode, path, argv, env), where
1975 argv is a list or tuple of strings and env is a dictionary
1976 like posix.environ. */
1978 if (!PyArg_ParseTuple(args
, "ietOO:spawnvpe", &mode
,
1979 Py_FileSystemDefaultEncoding
,
1980 &path
, &argv
, &env
))
1982 if (PyList_Check(argv
)) {
1983 argc
= PyList_Size(argv
);
1984 getitem
= PyList_GetItem
;
1986 else if (PyTuple_Check(argv
)) {
1987 argc
= PyTuple_Size(argv
);
1988 getitem
= PyTuple_GetItem
;
1991 PyErr_SetString(PyExc_TypeError
,
1992 "spawnvpe() arg 2 must be a tuple or list");
1995 if (!PyMapping_Check(env
)) {
1996 PyErr_SetString(PyExc_TypeError
,
1997 "spawnvpe() arg 3 must be a mapping object");
2001 argvlist
= PyMem_NEW(char *, argc
+1);
2002 if (argvlist
== NULL
) {
2006 for (i
= 0; i
< argc
; i
++) {
2007 if (!PyArg_Parse((*getitem
)(argv
, i
),
2008 "et;spawnvpe() arg 2 must contain only strings",
2009 Py_FileSystemDefaultEncoding
,
2017 argvlist
[argc
] = NULL
;
2019 i
= PyMapping_Size(env
);
2022 envlist
= PyMem_NEW(char *, i
+ 1);
2023 if (envlist
== NULL
) {
2028 keys
= PyMapping_Keys(env
);
2029 vals
= PyMapping_Values(env
);
2032 if (!PyList_Check(keys
) || !PyList_Check(vals
)) {
2033 PyErr_SetString(PyExc_TypeError
,
2034 "spawnvpe(): env.keys() or env.values() is not a list");
2038 for (pos
= 0; pos
< i
; pos
++) {
2042 key
= PyList_GetItem(keys
, pos
);
2043 val
= PyList_GetItem(vals
, pos
);
2049 "s;spawnvpe() arg 3 contains a non-string key",
2053 "s;spawnvpe() arg 3 contains a non-string value",
2058 len
= PyString_Size(key
) + PyString_Size(val
) + 2;
2059 p
= PyMem_NEW(char, len
);
2064 PyOS_snprintf(p
, len
, "%s=%s", k
, v
);
2065 envlist
[envc
++] = p
;
2069 Py_BEGIN_ALLOW_THREADS
2070 #if defined(PYCC_GCC)
2071 spawnval
= spawnvpe(mode
, path
, argvlist
, envlist
);
2073 spawnval
= _spawnvpe(mode
, path
, argvlist
, envlist
);
2075 Py_END_ALLOW_THREADS
2078 (void) posix_error();
2080 res
= Py_BuildValue("l", (long) spawnval
);
2084 PyMem_DEL(envlist
[envc
]);
2087 free_string_array(argvlist
, lastarg
);
2094 #endif /* PYOS_OS2 */
2095 #endif /* HAVE_SPAWNV */
2099 PyDoc_STRVAR(posix_fork1__doc__
,
2100 "fork1() -> pid\n\n\
2101 Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
2103 Return 0 to child process and PID of child to parent process.");
2106 posix_fork1(PyObject
*self
, PyObject
*noargs
)
2110 _PyImport_AcquireLock();
2113 /* child: this clobbers and resets the import lock. */
2116 /* parent: release the import lock. */
2117 result
= _PyImport_ReleaseLock();
2120 return posix_error();
2122 /* Don't clobber the OSError if the fork failed. */
2123 PyErr_SetString(PyExc_RuntimeError
,
2124 "not holding the import lock");
2127 return PyLong_FromPid(pid
);
2133 PyDoc_STRVAR(posix_fork__doc__
,
2135 Fork a child process.\n\
2136 Return 0 to child process and PID of child to parent process.");
2139 posix_fork(PyObject
*self
, PyObject
*noargs
)
2143 _PyImport_AcquireLock();
2146 /* child: this clobbers and resets the import lock. */
2149 /* parent: release the import lock. */
2150 result
= _PyImport_ReleaseLock();
2153 return posix_error();
2155 /* Don't clobber the OSError if the fork failed. */
2156 PyErr_SetString(PyExc_RuntimeError
,
2157 "not holding the import lock");
2160 return PyLong_FromPid(pid
);
2164 /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
2165 /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
2166 #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
2167 #define DEV_PTY_FILE "/dev/ptc"
2168 #define HAVE_DEV_PTMX
2170 #define DEV_PTY_FILE "/dev/ptmx"
2173 #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
2177 #ifdef HAVE_LIBUTIL_H
2178 #include <libutil.h>
2182 #endif /* HAVE_UTIL_H */
2183 #endif /* HAVE_LIBUTIL_H */
2184 #endif /* HAVE_PTY_H */
2185 #ifdef HAVE_STROPTS_H
2186 #include <stropts.h>
2188 #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
2190 #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
2191 PyDoc_STRVAR(posix_openpty__doc__
,
2192 "openpty() -> (master_fd, slave_fd)\n\n\
2193 Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
2196 posix_openpty(PyObject
*self
, PyObject
*noargs
)
2198 int master_fd
, slave_fd
;
2199 #ifndef HAVE_OPENPTY
2202 #if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
2203 PyOS_sighandler_t sig_saved
;
2205 extern char *ptsname(int fildes
);
2210 if (openpty(&master_fd
, &slave_fd
, NULL
, NULL
, NULL
) != 0)
2211 return posix_error();
2212 #elif defined(HAVE__GETPTY)
2213 slave_name
= _getpty(&master_fd
, O_RDWR
, 0666, 0);
2214 if (slave_name
== NULL
)
2215 return posix_error();
2217 slave_fd
= open(slave_name
, O_RDWR
);
2219 return posix_error();
2221 master_fd
= open(DEV_PTY_FILE
, O_RDWR
| O_NOCTTY
); /* open master */
2223 return posix_error();
2224 sig_saved
= PyOS_setsig(SIGCHLD
, SIG_DFL
);
2225 /* change permission of slave */
2226 if (grantpt(master_fd
) < 0) {
2227 PyOS_setsig(SIGCHLD
, sig_saved
);
2228 return posix_error();
2231 if (unlockpt(master_fd
) < 0) {
2232 PyOS_setsig(SIGCHLD
, sig_saved
);
2233 return posix_error();
2235 PyOS_setsig(SIGCHLD
, sig_saved
);
2236 slave_name
= ptsname(master_fd
); /* get name of slave */
2237 if (slave_name
== NULL
)
2238 return posix_error();
2239 slave_fd
= open(slave_name
, O_RDWR
| O_NOCTTY
); /* open slave */
2241 return posix_error();
2242 #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
2243 ioctl(slave_fd
, I_PUSH
, "ptem"); /* push ptem */
2244 ioctl(slave_fd
, I_PUSH
, "ldterm"); /* push ldterm */
2246 ioctl(slave_fd
, I_PUSH
, "ttcompat"); /* push ttcompat */
2248 #endif /* HAVE_CYGWIN */
2249 #endif /* HAVE_OPENPTY */
2251 return Py_BuildValue("(ii)", master_fd
, slave_fd
);
2254 #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
2257 PyDoc_STRVAR(posix_forkpty__doc__
,
2258 "forkpty() -> (pid, master_fd)\n\n\
2259 Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
2260 Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
2261 To both, return fd of newly opened pseudo-terminal.\n");
2264 posix_forkpty(PyObject
*self
, PyObject
*noargs
)
2266 int master_fd
= -1, result
= 0;
2269 _PyImport_AcquireLock();
2270 pid
= forkpty(&master_fd
, NULL
, NULL
, NULL
);
2272 /* child: this clobbers and resets the import lock. */
2275 /* parent: release the import lock. */
2276 result
= _PyImport_ReleaseLock();
2279 return posix_error();
2281 /* Don't clobber the OSError if the fork failed. */
2282 PyErr_SetString(PyExc_RuntimeError
,
2283 "not holding the import lock");
2286 return Py_BuildValue("(Ni)", PyLong_FromPid(pid
), master_fd
);
2291 PyDoc_STRVAR(posix_getegid__doc__
,
2292 "getegid() -> egid\n\n\
2293 Return the current process's effective group id.");
2296 posix_getegid(PyObject
*self
, PyObject
*noargs
)
2298 return PyInt_FromLong((long)getegid());
2304 PyDoc_STRVAR(posix_geteuid__doc__
,
2305 "geteuid() -> euid\n\n\
2306 Return the current process's effective user id.");
2309 posix_geteuid(PyObject
*self
, PyObject
*noargs
)
2311 return PyInt_FromLong((long)geteuid());
2317 PyDoc_STRVAR(posix_getgid__doc__
,
2318 "getgid() -> gid\n\n\
2319 Return the current process's group id.");
2322 posix_getgid(PyObject
*self
, PyObject
*noargs
)
2324 return PyInt_FromLong((long)getgid());
2329 PyDoc_STRVAR(posix_getpid__doc__
,
2330 "getpid() -> pid\n\n\
2331 Return the current process id");
2334 posix_getpid(PyObject
*self
, PyObject
*noargs
)
2336 return PyLong_FromPid(getpid());
2340 #ifdef HAVE_GETGROUPS
2341 PyDoc_STRVAR(posix_getgroups__doc__
,
2342 "getgroups() -> list of group IDs\n\n\
2343 Return list of supplemental group IDs for the process.");
2346 posix_getgroups(PyObject
*self
, PyObject
*noargs
)
2348 PyObject
*result
= NULL
;
2351 #define MAX_GROUPS NGROUPS_MAX
2353 /* defined to be 16 on Solaris7, so this should be a small number */
2354 #define MAX_GROUPS 64
2356 gid_t grouplist
[MAX_GROUPS
];
2358 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
2359 * This is a helper variable to store the intermediate result when
2362 * To keep the code readable the OSX behaviour is unconditional,
2363 * according to the POSIX spec this should be safe on all unix-y
2366 gid_t
* alt_grouplist
= grouplist
;
2369 n
= getgroups(MAX_GROUPS
, grouplist
);
2371 if (errno
== EINVAL
) {
2372 n
= getgroups(0, NULL
);
2374 return posix_error();
2377 /* Avoid malloc(0) */
2378 alt_grouplist
= grouplist
;
2380 alt_grouplist
= PyMem_Malloc(n
* sizeof(gid_t
));
2381 if (alt_grouplist
== NULL
) {
2383 return posix_error();
2385 n
= getgroups(n
, alt_grouplist
);
2387 PyMem_Free(alt_grouplist
);
2388 return posix_error();
2392 return posix_error();
2395 result
= PyList_New(n
);
2396 if (result
!= NULL
) {
2398 for (i
= 0; i
< n
; ++i
) {
2399 PyObject
*o
= PyInt_FromLong((long)alt_grouplist
[i
]);
2405 PyList_SET_ITEM(result
, i
, o
);
2409 if (alt_grouplist
!= grouplist
) {
2410 PyMem_Free(alt_grouplist
);
2417 #ifdef HAVE_INITGROUPS
2418 PyDoc_STRVAR(posix_initgroups__doc__
,
2419 "initgroups(username, gid) -> None\n\n\
2420 Call the system initgroups() to initialize the group access list with all of\n\
2421 the groups of which the specified username is a member, plus the specified\n\
2425 posix_initgroups(PyObject
*self
, PyObject
*args
)
2430 if (!PyArg_ParseTuple(args
, "sl:initgroups", &username
, &gid
))
2433 if (initgroups(username
, (gid_t
) gid
) == -1)
2434 return PyErr_SetFromErrno(PyExc_OSError
);
2442 PyDoc_STRVAR(posix_getpgid__doc__
,
2443 "getpgid(pid) -> pgid\n\n\
2444 Call the system call getpgid().");
2447 posix_getpgid(PyObject
*self
, PyObject
*args
)
2450 if (!PyArg_ParseTuple(args
, PARSE_PID
":getpgid", &pid
))
2452 pgid
= getpgid(pid
);
2454 return posix_error();
2455 return PyLong_FromPid(pgid
);
2457 #endif /* HAVE_GETPGID */
2461 PyDoc_STRVAR(posix_getpgrp__doc__
,
2462 "getpgrp() -> pgrp\n\n\
2463 Return the current process group id.");
2466 posix_getpgrp(PyObject
*self
, PyObject
*noargs
)
2468 #ifdef GETPGRP_HAVE_ARG
2469 return PyLong_FromPid(getpgrp(0));
2470 #else /* GETPGRP_HAVE_ARG */
2471 return PyLong_FromPid(getpgrp());
2472 #endif /* GETPGRP_HAVE_ARG */
2474 #endif /* HAVE_GETPGRP */
2478 PyDoc_STRVAR(posix_setpgrp__doc__
,
2480 Make this process the process group leader.");
2483 posix_setpgrp(PyObject
*self
, PyObject
*noargs
)
2485 #ifdef SETPGRP_HAVE_ARG
2486 if (setpgrp(0, 0) < 0)
2487 #else /* SETPGRP_HAVE_ARG */
2489 #endif /* SETPGRP_HAVE_ARG */
2490 return posix_error();
2495 #endif /* HAVE_SETPGRP */
2498 PyDoc_STRVAR(posix_getppid__doc__
,
2499 "getppid() -> ppid\n\n\
2500 Return the parent's process id.");
2503 posix_getppid(PyObject
*self
, PyObject
*noargs
)
2505 return PyLong_FromPid(getppid());
2510 #ifdef HAVE_GETLOGIN
2511 PyDoc_STRVAR(posix_getlogin__doc__
,
2512 "getlogin() -> string\n\n\
2513 Return the actual login name.");
2516 posix_getlogin(PyObject
*self
, PyObject
*noargs
)
2518 PyObject
*result
= NULL
;
2520 int old_errno
= errno
;
2528 PyErr_SetString(PyExc_OSError
,
2529 "unable to determine login name");
2532 result
= PyString_FromString(name
);
2539 #ifndef UEFI_C_SOURCE
2540 PyDoc_STRVAR(posix_getuid__doc__
,
2541 "getuid() -> uid\n\n\
2542 Return the current process's user id.");
2545 posix_getuid(PyObject
*self
, PyObject
*noargs
)
2547 return PyInt_FromLong((long)getuid());
2549 #endif /* UEFI_C_SOURCE */
2552 PyDoc_STRVAR(posix_kill__doc__
,
2553 "kill(pid, sig)\n\n\
2554 Kill a process with a signal.");
2557 posix_kill(PyObject
*self
, PyObject
*args
)
2561 if (!PyArg_ParseTuple(args
, PARSE_PID
"i:kill", &pid
, &sig
))
2563 #if defined(PYOS_OS2) && !defined(PYCC_GCC)
2564 if (sig
== XCPT_SIGNAL_INTR
|| sig
== XCPT_SIGNAL_BREAK
) {
2566 if ((rc
= DosSendSignalException(pid
, sig
)) != NO_ERROR
)
2567 return os2_error(rc
);
2569 } else if (sig
== XCPT_SIGNAL_KILLPROC
) {
2571 if ((rc
= DosKillProcess(DKP_PROCESS
, pid
)) != NO_ERROR
)
2572 return os2_error(rc
);
2575 return NULL
; /* Unrecognized Signal Requested */
2577 if (kill(pid
, sig
) == -1)
2578 return posix_error();
2586 PyDoc_STRVAR(posix_killpg__doc__
,
2587 "killpg(pgid, sig)\n\n\
2588 Kill a process group with a signal.");
2591 posix_killpg(PyObject
*self
, PyObject
*args
)
2595 /* XXX some man pages make the `pgid` parameter an int, others
2596 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
2597 take the same type. Moreover, pid_t is always at least as wide as
2598 int (else compilation of this module fails), which is safe. */
2599 if (!PyArg_ParseTuple(args
, PARSE_PID
"i:killpg", &pgid
, &sig
))
2601 if (killpg(pgid
, sig
) == -1)
2602 return posix_error();
2610 #ifdef HAVE_SYS_LOCK_H
2611 #include <sys/lock.h>
2614 PyDoc_STRVAR(posix_plock__doc__
,
2616 Lock program segments into memory.");
2619 posix_plock(PyObject
*self
, PyObject
*args
)
2622 if (!PyArg_ParseTuple(args
, "i:plock", &op
))
2624 if (plock(op
) == -1)
2625 return posix_error();
2633 PyDoc_STRVAR(posix_popen__doc__
,
2634 "popen(command [, mode='r' [, bufsize]]) -> pipe\n\n\
2635 Open a pipe to/from a command returning a file object.");
2637 #if defined(PYOS_OS2)
2638 #if defined(PYCC_VACPP)
2640 async_system(const char *command
)
2642 char errormsg
[256], args
[1024];
2646 char *shell
= getenv("COMSPEC");
2650 /* avoid overflowing the argument buffer */
2651 if (strlen(shell
) + 3 + strlen(command
) >= 1024)
2652 return ERROR_NOT_ENOUGH_MEMORY
2655 strcat(args
, shell
);
2656 strcat(args
, "/c ");
2657 strcat(args
, command
);
2659 /* execute asynchronously, inheriting the environment */
2660 rc
= DosExecPgm(errormsg
,
2671 popen(const char *command
, const char *mode
, int pipesize
, int *err
)
2677 /* mode determines which of stdin or stdout is reconnected to
2678 * the pipe to the child
2680 if (strchr(mode
, 'r') != NULL
) {
2681 tgt_fd
= 1; /* stdout */
2682 } else if (strchr(mode
, 'w')) {
2683 tgt_fd
= 0; /* stdin */
2685 *err
= ERROR_INVALID_ACCESS
;
2689 /* setup the pipe */
2690 if ((rc
= DosCreatePipe(&pipeh
[0], &pipeh
[1], pipesize
)) != NO_ERROR
) {
2695 /* prevent other threads accessing stdio */
2698 /* reconnect stdio and execute child */
2701 if (dup2(pipeh
[tgtfd
], tgtfd
) == 0) {
2702 DosClose(pipeh
[tgtfd
]);
2703 rc
= async_system(command
);
2710 /* allow other threads access to stdio */
2713 /* if execution of child was successful return file stream */
2715 return fdopen(pipeh
[1 - tgtfd
], mode
);
2717 DosClose(pipeh
[1 - tgtfd
]);
2724 posix_popen(PyObject
*self
, PyObject
*args
)
2728 int err
, bufsize
= -1;
2731 if (!PyArg_ParseTuple(args
, "s|si:popen", &name
, &mode
, &bufsize
))
2733 Py_BEGIN_ALLOW_THREADS
2734 fp
= popen(name
, mode
, (bufsize
> 0) ? bufsize
: 4096, &err
);
2735 Py_END_ALLOW_THREADS
2737 return os2_error(err
);
2739 f
= PyFile_FromFile(fp
, name
, mode
, fclose
);
2741 PyFile_SetBufSize(f
, bufsize
);
2745 #elif defined(PYCC_GCC)
2747 /* standard posix version of popen() support */
2749 posix_popen(PyObject
*self
, PyObject
*args
)
2756 if (!PyArg_ParseTuple(args
, "s|si:popen", &name
, &mode
, &bufsize
))
2758 Py_BEGIN_ALLOW_THREADS
2759 fp
= popen(name
, mode
);
2760 Py_END_ALLOW_THREADS
2762 return posix_error();
2763 f
= PyFile_FromFile(fp
, name
, mode
, pclose
);
2765 PyFile_SetBufSize(f
, bufsize
);
2769 /* fork() under OS/2 has lots'o'warts
2770 * EMX supports pipe() and spawn*() so we can synthesize popen[234]()
2771 * most of this code is a ripoff of the win32 code, but using the
2772 * capabilities of EMX's C library routines
2775 /* These tell _PyPopen() whether to return 1, 2, or 3 file objects. */
2781 static PyObject
*_PyPopen(char *, int, int, int);
2782 static int _PyPclose(FILE *file
);
2785 * Internal dictionary mapping popen* file pointers to process handles,
2786 * for use when retrieving the process exit code. See _PyPclose() below
2787 * for more information on this dictionary's use.
2789 static PyObject
*_PyPopenProcs
= NULL
;
2791 /* os2emx version of popen2()
2793 * The result of this function is a pipe (file) connected to the
2794 * process's stdin, and a pipe connected to the process's stdout.
2798 os2emx_popen2(PyObject
*self
, PyObject
*args
)
2806 if (!PyArg_ParseTuple(args
, "s|si:popen2", &cmdstring
, &mode
, &bufsize
))
2811 else if (*mode
!= 'b') {
2812 PyErr_SetString(PyExc_ValueError
, "mode must be 't' or 'b'");
2817 f
= _PyPopen(cmdstring
, tm
, POPEN_2
, bufsize
);
2823 * Variation on os2emx.popen2
2825 * The result of this function is 3 pipes - the process's stdin,
2830 os2emx_popen3(PyObject
*self
, PyObject
*args
)
2838 if (!PyArg_ParseTuple(args
, "s|si:popen3", &cmdstring
, &mode
, &bufsize
))
2843 else if (*mode
!= 'b') {
2844 PyErr_SetString(PyExc_ValueError
, "mode must be 't' or 'b'");
2849 f
= _PyPopen(cmdstring
, tm
, POPEN_3
, bufsize
);
2855 * Variation on os2emx.popen2
2857 * The result of this function is 2 pipes - the processes stdin,
2858 * and stdout+stderr combined as a single pipe.
2862 os2emx_popen4(PyObject
*self
, PyObject
*args
)
2870 if (!PyArg_ParseTuple(args
, "s|si:popen4", &cmdstring
, &mode
, &bufsize
))
2875 else if (*mode
!= 'b') {
2876 PyErr_SetString(PyExc_ValueError
, "mode must be 't' or 'b'");
2881 f
= _PyPopen(cmdstring
, tm
, POPEN_4
, bufsize
);
2886 /* a couple of structures for convenient handling of multiple
2887 * file handles and pipes
2901 /* The following code is derived from the win32 code */
2904 _PyPopen(char *cmdstring
, int mode
, int n
, int bufsize
)
2906 struct file_ref stdio
[3];
2907 struct pipe_ref p_fd
[3];
2909 int file_count
, i
, pipe_err
;
2911 char *shell
, *sh_name
, *opt
, *rd_mode
, *wr_mode
;
2912 PyObject
*f
, *p_f
[3];
2914 /* file modes for subsequent fdopen's on pipe handles */
2926 /* prepare shell references */
2927 if ((shell
= getenv("EMXSHELL")) == NULL
)
2928 if ((shell
= getenv("COMSPEC")) == NULL
)
2931 return posix_error();
2934 sh_name
= _getname(shell
);
2935 if (stricmp(sh_name
, "cmd.exe") == 0 || stricmp(sh_name
, "4os2.exe") == 0)
2940 /* save current stdio fds + their flags, and set not inheritable */
2942 while (pipe_err
>= 0 && i
< 3)
2944 pipe_err
= stdio
[i
].handle
= dup(i
);
2945 stdio
[i
].flags
= fcntl(i
, F_GETFD
, 0);
2946 fcntl(stdio
[i
].handle
, F_SETFD
, stdio
[i
].flags
| FD_CLOEXEC
);
2951 /* didn't get them all saved - clean up and bail out */
2952 int saved_err
= errno
;
2955 close(stdio
[i
].handle
);
2958 return posix_error();
2961 /* create pipe ends */
2966 while ((pipe_err
== 0) && (i
< file_count
))
2967 pipe_err
= pipe((int *)&p_fd
[i
++]);
2970 /* didn't get them all made - clean up and bail out */
2977 return posix_error();
2980 /* change the actual standard IO streams over temporarily,
2981 * making the retained pipe ends non-inheritable
2986 if (dup2(p_fd
[0].rd
, 0) == 0)
2989 i
= fcntl(p_fd
[0].wr
, F_GETFD
, 0);
2990 fcntl(p_fd
[0].wr
, F_SETFD
, i
| FD_CLOEXEC
);
2991 if ((p_s
[0] = fdopen(p_fd
[0].wr
, wr_mode
)) == NULL
)
3005 if (dup2(p_fd
[1].wr
, 1) == 1)
3008 i
= fcntl(p_fd
[1].rd
, F_GETFD
, 0);
3009 fcntl(p_fd
[1].rd
, F_SETFD
, i
| FD_CLOEXEC
);
3010 if ((p_s
[1] = fdopen(p_fd
[1].rd
, rd_mode
)) == NULL
)
3022 /* - stderr, as required */
3028 if (dup2(p_fd
[2].wr
, 2) == 2)
3031 i
= fcntl(p_fd
[2].rd
, F_GETFD
, 0);
3032 fcntl(p_fd
[2].rd
, F_SETFD
, i
| FD_CLOEXEC
);
3033 if ((p_s
[2] = fdopen(p_fd
[2].rd
, rd_mode
)) == NULL
)
3048 if (dup2(1, 2) != 2)
3056 /* spawn the child process */
3059 pipe_pid
= spawnlp(P_NOWAIT
, shell
, shell
, opt
, cmdstring
, (char *)0);
3066 /* save the PID into the FILE structure
3067 * NOTE: this implementation doesn't actually
3068 * take advantage of this, but do it for
3069 * completeness - AIM Apr01
3071 for (i
= 0; i
< file_count
; i
++)
3072 p_s
[i
]->_pid
= pipe_pid
;
3076 /* reset standard IO to normal */
3077 for (i
= 0; i
< 3; i
++)
3079 dup2(stdio
[i
].handle
, i
);
3080 fcntl(i
, F_SETFD
, stdio
[i
].flags
);
3081 close(stdio
[i
].handle
);
3084 /* if any remnant problems, clean up and bail out */
3087 for (i
= 0; i
< 3; i
++)
3093 return posix_error_with_filename(cmdstring
);
3096 /* build tuple of file objects to return */
3097 if ((p_f
[0] = PyFile_FromFile(p_s
[0], cmdstring
, wr_mode
, _PyPclose
)) != NULL
)
3098 PyFile_SetBufSize(p_f
[0], bufsize
);
3099 if ((p_f
[1] = PyFile_FromFile(p_s
[1], cmdstring
, rd_mode
, _PyPclose
)) != NULL
)
3100 PyFile_SetBufSize(p_f
[1], bufsize
);
3103 if ((p_f
[2] = PyFile_FromFile(p_s
[2], cmdstring
, rd_mode
, _PyPclose
)) != NULL
)
3104 PyFile_SetBufSize(p_f
[0], bufsize
);
3105 f
= PyTuple_Pack(3, p_f
[0], p_f
[1], p_f
[2]);
3108 f
= PyTuple_Pack(2, p_f
[0], p_f
[1]);
3111 * Insert the files we've created into the process dictionary
3112 * all referencing the list with the process handle and the
3113 * initial number of files (see description below in _PyPclose).
3114 * Since if _PyPclose later tried to wait on a process when all
3115 * handles weren't closed, it could create a deadlock with the
3116 * child, we spend some energy here to try to ensure that we
3117 * either insert all file handles into the dictionary or none
3118 * at all. It's a little clumsy with the various popen modes
3119 * and variable number of files involved.
3123 _PyPopenProcs
= PyDict_New();
3128 PyObject
*procObj
, *pidObj
, *intObj
, *fileObj
[3];
3131 fileObj
[0] = fileObj
[1] = fileObj
[2] = NULL
;
3132 ins_rc
[0] = ins_rc
[1] = ins_rc
[2] = 0;
3134 procObj
= PyList_New(2);
3135 pidObj
= PyLong_FromPid(pipe_pid
);
3136 intObj
= PyInt_FromLong((long) file_count
);
3138 if (procObj
&& pidObj
&& intObj
)
3140 PyList_SetItem(procObj
, 0, pidObj
);
3141 PyList_SetItem(procObj
, 1, intObj
);
3143 fileObj
[0] = PyLong_FromVoidPtr(p_s
[0]);
3146 ins_rc
[0] = PyDict_SetItem(_PyPopenProcs
,
3150 fileObj
[1] = PyLong_FromVoidPtr(p_s
[1]);
3153 ins_rc
[1] = PyDict_SetItem(_PyPopenProcs
,
3157 if (file_count
>= 3)
3159 fileObj
[2] = PyLong_FromVoidPtr(p_s
[2]);
3162 ins_rc
[2] = PyDict_SetItem(_PyPopenProcs
,
3168 if (ins_rc
[0] < 0 || !fileObj
[0] ||
3169 ins_rc
[1] < 0 || (file_count
> 1 && !fileObj
[1]) ||
3170 ins_rc
[2] < 0 || (file_count
> 2 && !fileObj
[2]))
3172 /* Something failed - remove any dictionary
3173 * entries that did make it.
3175 if (!ins_rc
[0] && fileObj
[0])
3177 PyDict_DelItem(_PyPopenProcs
,
3180 if (!ins_rc
[1] && fileObj
[1])
3182 PyDict_DelItem(_PyPopenProcs
,
3185 if (!ins_rc
[2] && fileObj
[2])
3187 PyDict_DelItem(_PyPopenProcs
,
3194 * Clean up our localized references for the dictionary keys
3195 * and value since PyDict_SetItem will Py_INCREF any copies
3196 * that got placed in the dictionary.
3198 Py_XDECREF(procObj
);
3199 Py_XDECREF(fileObj
[0]);
3200 Py_XDECREF(fileObj
[1]);
3201 Py_XDECREF(fileObj
[2]);
3204 /* Child is launched. */
3209 * Wrapper for fclose() to use for popen* files, so we can retrieve the
3210 * exit code for the child process and return as a result of the close.
3212 * This function uses the _PyPopenProcs dictionary in order to map the
3213 * input file pointer to information about the process that was
3214 * originally created by the popen* call that created the file pointer.
3215 * The dictionary uses the file pointer as a key (with one entry
3216 * inserted for each file returned by the original popen* call) and a
3217 * single list object as the value for all files from a single call.
3218 * The list object contains the Win32 process handle at [0], and a file
3219 * count at [1], which is initialized to the total number of file
3220 * handles using that list.
3222 * This function closes whichever handle it is passed, and decrements
3223 * the file count in the dictionary for the process handle pointed to
3224 * by this file. On the last close (when the file count reaches zero),
3225 * this function will wait for the child process and then return its
3226 * exit code as the result of the close() operation. This permits the
3227 * files to be closed in any order - it is always the close() of the
3228 * final handle that will return the exit code.
3230 * NOTE: This function is currently called with the GIL released.
3231 * hence we use the GILState API to manage our state.
3234 static int _PyPclose(FILE *file
)
3239 PyObject
*procObj
, *pidObj
, *intObj
, *fileObj
;
3242 PyGILState_STATE state
;
3245 /* Close the file handle first, to ensure it can't block the
3246 * child from exiting if it's the last handle.
3248 result
= fclose(file
);
3251 state
= PyGILState_Ensure();
3255 if ((fileObj
= PyLong_FromVoidPtr(file
)) != NULL
&&
3256 (procObj
= PyDict_GetItem(_PyPopenProcs
,
3257 fileObj
)) != NULL
&&
3258 (pidObj
= PyList_GetItem(procObj
,0)) != NULL
&&
3259 (intObj
= PyList_GetItem(procObj
,1)) != NULL
)
3261 pipe_pid
= (pid_t
) PyLong_AsPid(pidObj
);
3262 file_count
= (int) PyInt_AsLong(intObj
);
3266 /* Still other files referencing process */
3268 PyList_SetItem(procObj
,1,
3269 PyInt_FromLong((long) file_count
));
3273 /* Last file for this process */
3274 if (result
!= EOF
&&
3275 waitpid(pipe_pid
, &exit_code
, 0) == pipe_pid
)
3277 /* extract exit status */
3278 if (WIFEXITED(exit_code
))
3280 result
= WEXITSTATUS(exit_code
);
3290 /* Indicate failure - this will cause the file object
3291 * to raise an I/O error and translate the last
3292 * error code from errno. We do have a problem with
3293 * last errors that overlap the normal errno table,
3294 * but that's a consistent problem with the file object.
3300 /* Remove this file pointer from dictionary */
3301 PyDict_DelItem(_PyPopenProcs
, fileObj
);
3303 if (PyDict_Size(_PyPopenProcs
) == 0)
3305 Py_DECREF(_PyPopenProcs
);
3306 _PyPopenProcs
= NULL
;
3309 } /* if object retrieval ok */
3311 Py_XDECREF(fileObj
);
3312 } /* if _PyPopenProcs */
3315 PyGILState_Release(state
);
3320 #endif /* PYCC_??? */
3322 #elif defined(MS_WINDOWS)
3325 * Portable 'popen' replacement for Win32.
3327 * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks
3328 * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com>
3329 * Return code handling by David Bolen <db3l@fitlinxx.com>.
3336 /* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */
3342 static PyObject
*_PyPopen(char *, int, int);
3343 static int _PyPclose(FILE *file
);
3346 * Internal dictionary mapping popen* file pointers to process handles,
3347 * for use when retrieving the process exit code. See _PyPclose() below
3348 * for more information on this dictionary's use.
3350 static PyObject
*_PyPopenProcs
= NULL
;
3353 /* popen that works from a GUI.
3355 * The result of this function is a pipe (file) connected to the
3356 * processes stdin or stdout, depending on the requested mode.
3360 posix_popen(PyObject
*self
, PyObject
*args
)
3368 if (!PyArg_ParseTuple(args
, "s|si:popen", &cmdstring
, &mode
, &bufsize
))
3373 else if (*mode
!= 'w') {
3374 PyErr_SetString(PyExc_ValueError
, "popen() arg 2 must be 'r' or 'w'");
3379 if (bufsize
!= -1) {
3380 PyErr_SetString(PyExc_ValueError
, "popen() arg 3 must be -1");
3384 if (*(mode
+1) == 't')
3385 f
= _PyPopen(cmdstring
, tm
| _O_TEXT
, POPEN_1
);
3386 else if (*(mode
+1) == 'b')
3387 f
= _PyPopen(cmdstring
, tm
| _O_BINARY
, POPEN_1
);
3389 f
= _PyPopen(cmdstring
, tm
| _O_TEXT
, POPEN_1
);
3394 /* Variation on win32pipe.popen
3396 * The result of this function is a pipe (file) connected to the
3397 * process's stdin, and a pipe connected to the process's stdout.
3401 win32_popen2(PyObject
*self
, PyObject
*args
)
3409 if (!PyArg_ParseTuple(args
, "s|si:popen2", &cmdstring
, &mode
, &bufsize
))
3414 else if (*mode
!= 'b') {
3415 PyErr_SetString(PyExc_ValueError
, "popen2() arg 2 must be 't' or 'b'");
3420 if (bufsize
!= -1) {
3421 PyErr_SetString(PyExc_ValueError
, "popen2() arg 3 must be -1");
3425 f
= _PyPopen(cmdstring
, tm
, POPEN_2
);
3431 * Variation on <om win32pipe.popen>
3433 * The result of this function is 3 pipes - the process's stdin,
3438 win32_popen3(PyObject
*self
, PyObject
*args
)
3446 if (!PyArg_ParseTuple(args
, "s|si:popen3", &cmdstring
, &mode
, &bufsize
))
3451 else if (*mode
!= 'b') {
3452 PyErr_SetString(PyExc_ValueError
, "popen3() arg 2 must be 't' or 'b'");
3457 if (bufsize
!= -1) {
3458 PyErr_SetString(PyExc_ValueError
, "popen3() arg 3 must be -1");
3462 f
= _PyPopen(cmdstring
, tm
, POPEN_3
);
3468 * Variation on win32pipe.popen
3470 * The result of this function is 2 pipes - the processes stdin,
3471 * and stdout+stderr combined as a single pipe.
3475 win32_popen4(PyObject
*self
, PyObject
*args
)
3483 if (!PyArg_ParseTuple(args
, "s|si:popen4", &cmdstring
, &mode
, &bufsize
))
3488 else if (*mode
!= 'b') {
3489 PyErr_SetString(PyExc_ValueError
, "popen4() arg 2 must be 't' or 'b'");
3494 if (bufsize
!= -1) {
3495 PyErr_SetString(PyExc_ValueError
, "popen4() arg 3 must be -1");
3499 f
= _PyPopen(cmdstring
, tm
, POPEN_4
);
3505 _PyPopenCreateProcess(char *cmdstring
,
3511 PROCESS_INFORMATION piProcInfo
;
3512 STARTUPINFO siStartInfo
;
3513 DWORD dwProcessFlags
= 0; /* no NEW_CONSOLE by default for Ctrl+C handling */
3514 char *s1
,*s2
, *s3
= " /c ";
3515 const char *szConsoleSpawn
= "w9xpopen.exe";
3519 if (i
= GetEnvironmentVariable("COMSPEC",NULL
,0)) {
3522 s1
= (char *)alloca(i
);
3523 if (!(x
= GetEnvironmentVariable("COMSPEC", s1
, i
)))
3524 /* x < i, so x fits into an integer */
3527 /* Explicitly check if we are using COMMAND.COM. If we are
3528 * then use the w9xpopen hack.
3531 while (comshell
>= s1
&& *comshell
!= '\\')
3535 if (GetVersion() < 0x80000000 &&
3536 _stricmp(comshell
, "command.com") != 0) {
3537 /* NT/2000 and not using command.com. */
3538 x
= i
+ strlen(s3
) + strlen(cmdstring
) + 1;
3539 s2
= (char *)alloca(x
);
3541 PyOS_snprintf(s2
, x
, "%s%s%s", s1
, s3
, cmdstring
);
3545 * Oh gag, we're on Win9x or using COMMAND.COM. Use
3546 * the workaround listed in KB: Q150956
3548 char modulepath
[_MAX_PATH
];
3549 struct stat statinfo
;
3550 GetModuleFileName(NULL
, modulepath
, sizeof(modulepath
));
3551 for (x
= i
= 0; modulepath
[i
]; i
++)
3552 if (modulepath
[i
] == SEP
)
3554 modulepath
[x
] = '\0';
3555 /* Create the full-name to w9xpopen, so we can test it exists */
3558 (sizeof(modulepath
)/sizeof(modulepath
[0]))
3559 -strlen(modulepath
));
3560 if (stat(modulepath
, &statinfo
) != 0) {
3561 size_t mplen
= sizeof(modulepath
)/sizeof(modulepath
[0]);
3562 /* Eeek - file-not-found - possibly an embedding
3563 situation - see if we can locate it in sys.prefix
3568 modulepath
[mplen
-1] = '\0';
3569 if (modulepath
[strlen(modulepath
)-1] != '\\')
3570 strcat(modulepath
, "\\");
3573 mplen
-strlen(modulepath
));
3574 /* No where else to look - raise an easily identifiable
3575 error, rather than leaving Windows to report
3576 "file not found" - as the user is probably blissfully
3577 unaware this shim EXE is used, and it will confuse them.
3578 (well, it confused me for a while ;-)
3580 if (stat(modulepath
, &statinfo
) != 0) {
3581 PyErr_Format(PyExc_RuntimeError
,
3582 "Can not locate '%s' which is needed "
3583 "for popen to work with your shell "
3589 x
= i
+ strlen(s3
) + strlen(cmdstring
) + 1 +
3590 strlen(modulepath
) +
3591 strlen(szConsoleSpawn
) + 1;
3593 s2
= (char *)alloca(x
);
3595 /* To maintain correct argument passing semantics,
3596 we pass the command-line as it stands, and allow
3597 quoting to be applied. w9xpopen.exe will then
3598 use its argv vector, and re-quote the necessary
3599 args for the ultimate child process.
3608 /* Not passing CREATE_NEW_CONSOLE has been known to
3609 cause random failures on win9x. Specifically a
3611 "Your program accessed mem currently in use at xxx"
3612 and a hopeful warning about the stability of your
3614 Cost is Ctrl+C won't kill children, but anyone
3615 who cares can have a go!
3617 dwProcessFlags
|= CREATE_NEW_CONSOLE
;
3621 /* Could be an else here to try cmd.exe / command.com in the path
3622 Now we'll just error out.. */
3624 PyErr_SetString(PyExc_RuntimeError
,
3625 "Cannot locate a COMSPEC environment variable to "
3626 "use as the shell");
3630 ZeroMemory(&siStartInfo
, sizeof(STARTUPINFO
));
3631 siStartInfo
.cb
= sizeof(STARTUPINFO
);
3632 siStartInfo
.dwFlags
= STARTF_USESTDHANDLES
| STARTF_USESHOWWINDOW
;
3633 siStartInfo
.hStdInput
= hStdin
;
3634 siStartInfo
.hStdOutput
= hStdout
;
3635 siStartInfo
.hStdError
= hStderr
;
3636 siStartInfo
.wShowWindow
= SW_HIDE
;
3638 if (CreateProcess(NULL
,
3648 /* Close the handles now so anyone waiting is woken. */
3649 CloseHandle(piProcInfo
.hThread
);
3651 /* Return process handle */
3652 *hProcess
= piProcInfo
.hProcess
;
3655 win32_error("CreateProcess", s2
);
3659 /* The following code is based off of KB: Q190351 */
3662 _PyPopen(char *cmdstring
, int mode
, int n
)
3664 HANDLE hChildStdinRd
, hChildStdinWr
, hChildStdoutRd
, hChildStdoutWr
,
3665 hChildStderrRd
, hChildStderrWr
, hChildStdinWrDup
, hChildStdoutRdDup
,
3666 hChildStderrRdDup
, hProcess
; /* hChildStdoutWrDup; */
3668 SECURITY_ATTRIBUTES saAttr
;
3675 saAttr
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
3676 saAttr
.bInheritHandle
= TRUE
;
3677 saAttr
.lpSecurityDescriptor
= NULL
;
3679 if (!CreatePipe(&hChildStdinRd
, &hChildStdinWr
, &saAttr
, 0))
3680 return win32_error("CreatePipe", NULL
);
3682 /* Create new output read handle and the input write handle. Set
3683 * the inheritance properties to FALSE. Otherwise, the child inherits
3684 * these handles; resulting in non-closeable handles to the pipes
3686 fSuccess
= DuplicateHandle(GetCurrentProcess(), hChildStdinWr
,
3687 GetCurrentProcess(), &hChildStdinWrDup
, 0,
3689 DUPLICATE_SAME_ACCESS
);
3691 return win32_error("DuplicateHandle", NULL
);
3693 /* Close the inheritable version of ChildStdin
3694 that we're using. */
3695 CloseHandle(hChildStdinWr
);
3697 if (!CreatePipe(&hChildStdoutRd
, &hChildStdoutWr
, &saAttr
, 0))
3698 return win32_error("CreatePipe", NULL
);
3700 fSuccess
= DuplicateHandle(GetCurrentProcess(), hChildStdoutRd
,
3701 GetCurrentProcess(), &hChildStdoutRdDup
, 0,
3702 FALSE
, DUPLICATE_SAME_ACCESS
);
3704 return win32_error("DuplicateHandle", NULL
);
3706 /* Close the inheritable version of ChildStdout
3707 that we're using. */
3708 CloseHandle(hChildStdoutRd
);
3711 if (!CreatePipe(&hChildStderrRd
, &hChildStderrWr
, &saAttr
, 0))
3712 return win32_error("CreatePipe", NULL
);
3713 fSuccess
= DuplicateHandle(GetCurrentProcess(),
3715 GetCurrentProcess(),
3716 &hChildStderrRdDup
, 0,
3717 FALSE
, DUPLICATE_SAME_ACCESS
);
3719 return win32_error("DuplicateHandle", NULL
);
3720 /* Close the inheritable version of ChildStdErr that we're using. */
3721 CloseHandle(hChildStderrRd
);
3726 switch (mode
& (_O_RDONLY
| _O_TEXT
| _O_BINARY
| _O_WRONLY
)) {
3727 case _O_WRONLY
| _O_TEXT
:
3728 /* Case for writing to child Stdin in text mode. */
3729 fd1
= _open_osfhandle((Py_intptr_t
)hChildStdinWrDup
, mode
);
3730 f1
= _fdopen(fd1
, "w");
3731 f
= PyFile_FromFile(f1
, cmdstring
, "w", _PyPclose
);
3732 PyFile_SetBufSize(f
, 0);
3733 /* We don't care about these pipes anymore, so close them. */
3734 CloseHandle(hChildStdoutRdDup
);
3735 CloseHandle(hChildStderrRdDup
);
3738 case _O_RDONLY
| _O_TEXT
:
3739 /* Case for reading from child Stdout in text mode. */
3740 fd1
= _open_osfhandle((Py_intptr_t
)hChildStdoutRdDup
, mode
);
3741 f1
= _fdopen(fd1
, "r");
3742 f
= PyFile_FromFile(f1
, cmdstring
, "r", _PyPclose
);
3743 PyFile_SetBufSize(f
, 0);
3744 /* We don't care about these pipes anymore, so close them. */
3745 CloseHandle(hChildStdinWrDup
);
3746 CloseHandle(hChildStderrRdDup
);
3749 case _O_RDONLY
| _O_BINARY
:
3750 /* Case for readinig from child Stdout in binary mode. */
3751 fd1
= _open_osfhandle((Py_intptr_t
)hChildStdoutRdDup
, mode
);
3752 f1
= _fdopen(fd1
, "rb");
3753 f
= PyFile_FromFile(f1
, cmdstring
, "rb", _PyPclose
);
3754 PyFile_SetBufSize(f
, 0);
3755 /* We don't care about these pipes anymore, so close them. */
3756 CloseHandle(hChildStdinWrDup
);
3757 CloseHandle(hChildStderrRdDup
);
3760 case _O_WRONLY
| _O_BINARY
:
3761 /* Case for writing to child Stdin in binary mode. */
3762 fd1
= _open_osfhandle((Py_intptr_t
)hChildStdinWrDup
, mode
);
3763 f1
= _fdopen(fd1
, "wb");
3764 f
= PyFile_FromFile(f1
, cmdstring
, "wb", _PyPclose
);
3765 PyFile_SetBufSize(f
, 0);
3766 /* We don't care about these pipes anymore, so close them. */
3767 CloseHandle(hChildStdoutRdDup
);
3768 CloseHandle(hChildStderrRdDup
);
3780 if (mode
& _O_TEXT
) {
3788 fd1
= _open_osfhandle((Py_intptr_t
)hChildStdinWrDup
, mode
);
3789 f1
= _fdopen(fd1
, m2
);
3790 fd2
= _open_osfhandle((Py_intptr_t
)hChildStdoutRdDup
, mode
);
3791 f2
= _fdopen(fd2
, m1
);
3792 p1
= PyFile_FromFile(f1
, cmdstring
, m2
, _PyPclose
);
3793 PyFile_SetBufSize(p1
, 0);
3794 p2
= PyFile_FromFile(f2
, cmdstring
, m1
, _PyPclose
);
3795 PyFile_SetBufSize(p2
, 0);
3798 CloseHandle(hChildStderrRdDup
);
3800 f
= PyTuple_Pack(2,p1
,p2
);
3810 PyObject
*p1
, *p2
, *p3
;
3812 if (mode
& _O_TEXT
) {
3820 fd1
= _open_osfhandle((Py_intptr_t
)hChildStdinWrDup
, mode
);
3821 f1
= _fdopen(fd1
, m2
);
3822 fd2
= _open_osfhandle((Py_intptr_t
)hChildStdoutRdDup
, mode
);
3823 f2
= _fdopen(fd2
, m1
);
3824 fd3
= _open_osfhandle((Py_intptr_t
)hChildStderrRdDup
, mode
);
3825 f3
= _fdopen(fd3
, m1
);
3826 p1
= PyFile_FromFile(f1
, cmdstring
, m2
, _PyPclose
);
3827 p2
= PyFile_FromFile(f2
, cmdstring
, m1
, _PyPclose
);
3828 p3
= PyFile_FromFile(f3
, cmdstring
, m1
, _PyPclose
);
3829 PyFile_SetBufSize(p1
, 0);
3830 PyFile_SetBufSize(p2
, 0);
3831 PyFile_SetBufSize(p3
, 0);
3832 f
= PyTuple_Pack(3,p1
,p2
,p3
);
3842 if (!_PyPopenCreateProcess(cmdstring
,
3850 if (!_PyPopenCreateProcess(cmdstring
,
3859 * Insert the files we've created into the process dictionary
3860 * all referencing the list with the process handle and the
3861 * initial number of files (see description below in _PyPclose).
3862 * Since if _PyPclose later tried to wait on a process when all
3863 * handles weren't closed, it could create a deadlock with the
3864 * child, we spend some energy here to try to ensure that we
3865 * either insert all file handles into the dictionary or none
3866 * at all. It's a little clumsy with the various popen modes
3867 * and variable number of files involved.
3869 if (!_PyPopenProcs
) {
3870 _PyPopenProcs
= PyDict_New();
3873 if (_PyPopenProcs
) {
3874 PyObject
*procObj
, *hProcessObj
, *intObj
, *fileObj
[3];
3877 fileObj
[0] = fileObj
[1] = fileObj
[2] = NULL
;
3878 ins_rc
[0] = ins_rc
[1] = ins_rc
[2] = 0;
3880 procObj
= PyList_New(2);
3881 hProcessObj
= PyLong_FromVoidPtr(hProcess
);
3882 intObj
= PyInt_FromLong(file_count
);
3884 if (procObj
&& hProcessObj
&& intObj
) {
3885 PyList_SetItem(procObj
,0,hProcessObj
);
3886 PyList_SetItem(procObj
,1,intObj
);
3888 fileObj
[0] = PyLong_FromVoidPtr(f1
);
3890 ins_rc
[0] = PyDict_SetItem(_PyPopenProcs
,
3894 if (file_count
>= 2) {
3895 fileObj
[1] = PyLong_FromVoidPtr(f2
);
3897 ins_rc
[1] = PyDict_SetItem(_PyPopenProcs
,
3902 if (file_count
>= 3) {
3903 fileObj
[2] = PyLong_FromVoidPtr(f3
);
3905 ins_rc
[2] = PyDict_SetItem(_PyPopenProcs
,
3911 if (ins_rc
[0] < 0 || !fileObj
[0] ||
3912 ins_rc
[1] < 0 || (file_count
> 1 && !fileObj
[1]) ||
3913 ins_rc
[2] < 0 || (file_count
> 2 && !fileObj
[2])) {
3914 /* Something failed - remove any dictionary
3915 * entries that did make it.
3917 if (!ins_rc
[0] && fileObj
[0]) {
3918 PyDict_DelItem(_PyPopenProcs
,
3921 if (!ins_rc
[1] && fileObj
[1]) {
3922 PyDict_DelItem(_PyPopenProcs
,
3925 if (!ins_rc
[2] && fileObj
[2]) {
3926 PyDict_DelItem(_PyPopenProcs
,
3933 * Clean up our localized references for the dictionary keys
3934 * and value since PyDict_SetItem will Py_INCREF any copies
3935 * that got placed in the dictionary.
3937 Py_XDECREF(procObj
);
3938 Py_XDECREF(fileObj
[0]);
3939 Py_XDECREF(fileObj
[1]);
3940 Py_XDECREF(fileObj
[2]);
3943 /* Child is launched. Close the parents copy of those pipe
3944 * handles that only the child should have open. You need to
3945 * make sure that no handles to the write end of the output pipe
3946 * are maintained in this process or else the pipe will not close
3947 * when the child process exits and the ReadFile will hang. */
3949 if (!CloseHandle(hChildStdinRd
))
3950 return win32_error("CloseHandle", NULL
);
3952 if (!CloseHandle(hChildStdoutWr
))
3953 return win32_error("CloseHandle", NULL
);
3955 if ((n
!= 4) && (!CloseHandle(hChildStderrWr
)))
3956 return win32_error("CloseHandle", NULL
);
3962 * Wrapper for fclose() to use for popen* files, so we can retrieve the
3963 * exit code for the child process and return as a result of the close.
3965 * This function uses the _PyPopenProcs dictionary in order to map the
3966 * input file pointer to information about the process that was
3967 * originally created by the popen* call that created the file pointer.
3968 * The dictionary uses the file pointer as a key (with one entry
3969 * inserted for each file returned by the original popen* call) and a
3970 * single list object as the value for all files from a single call.
3971 * The list object contains the Win32 process handle at [0], and a file
3972 * count at [1], which is initialized to the total number of file
3973 * handles using that list.
3975 * This function closes whichever handle it is passed, and decrements
3976 * the file count in the dictionary for the process handle pointed to
3977 * by this file. On the last close (when the file count reaches zero),
3978 * this function will wait for the child process and then return its
3979 * exit code as the result of the close() operation. This permits the
3980 * files to be closed in any order - it is always the close() of the
3981 * final handle that will return the exit code.
3983 * NOTE: This function is currently called with the GIL released.
3984 * hence we use the GILState API to manage our state.
3987 static int _PyPclose(FILE *file
)
3992 PyObject
*procObj
, *hProcessObj
, *intObj
, *fileObj
;
3995 PyGILState_STATE state
;
3998 /* Close the file handle first, to ensure it can't block the
3999 * child from exiting if it's the last handle.
4001 result
= fclose(file
);
4003 state
= PyGILState_Ensure();
4005 if (_PyPopenProcs
) {
4006 if ((fileObj
= PyLong_FromVoidPtr(file
)) != NULL
&&
4007 (procObj
= PyDict_GetItem(_PyPopenProcs
,
4008 fileObj
)) != NULL
&&
4009 (hProcessObj
= PyList_GetItem(procObj
,0)) != NULL
&&
4010 (intObj
= PyList_GetItem(procObj
,1)) != NULL
) {
4012 hProcess
= PyLong_AsVoidPtr(hProcessObj
);
4013 file_count
= PyInt_AsLong(intObj
);
4015 if (file_count
> 1) {
4016 /* Still other files referencing process */
4018 PyList_SetItem(procObj
,1,
4019 PyInt_FromLong(file_count
));
4021 /* Last file for this process */
4022 if (result
!= EOF
&&
4023 WaitForSingleObject(hProcess
, INFINITE
) != WAIT_FAILED
&&
4024 GetExitCodeProcess(hProcess
, &exit_code
)) {
4025 /* Possible truncation here in 16-bit environments, but
4026 * real exit codes are just the lower byte in any event.
4030 /* Indicate failure - this will cause the file object
4031 * to raise an I/O error and translate the last Win32
4032 * error code from errno. We do have a problem with
4033 * last errors that overlap the normal errno table,
4034 * but that's a consistent problem with the file object.
4036 if (result
!= EOF
) {
4037 /* If the error wasn't from the fclose(), then
4038 * set errno for the file object error handling.
4040 errno
= GetLastError();
4045 /* Free up the native handle at this point */
4046 CloseHandle(hProcess
);
4049 /* Remove this file pointer from dictionary */
4050 PyDict_DelItem(_PyPopenProcs
, fileObj
);
4052 if (PyDict_Size(_PyPopenProcs
) == 0) {
4053 Py_DECREF(_PyPopenProcs
);
4054 _PyPopenProcs
= NULL
;
4057 } /* if object retrieval ok */
4059 Py_XDECREF(fileObj
);
4060 } /* if _PyPopenProcs */
4063 PyGILState_Release(state
);
4068 #else /* which OS? */
4070 posix_popen(PyObject
*self
, PyObject
*args
)
4077 if (!PyArg_ParseTuple(args
, "s|si:popen", &name
, &mode
, &bufsize
))
4079 /* Strip mode of binary or text modifiers */
4080 if (strcmp(mode
, "rb") == 0 || strcmp(mode
, "rt") == 0)
4082 else if (strcmp(mode
, "wb") == 0 || strcmp(mode
, "wt") == 0)
4084 Py_BEGIN_ALLOW_THREADS
4085 fp
= popen(name
, mode
);
4086 Py_END_ALLOW_THREADS
4088 return posix_error();
4089 f
= PyFile_FromFile(fp
, name
, mode
, pclose
);
4091 PyFile_SetBufSize(f
, bufsize
);
4095 #endif /* PYOS_??? */
4096 #endif /* HAVE_POPEN */
4100 PyDoc_STRVAR(posix_setuid__doc__
,
4102 Set the current process's user id.");
4105 posix_setuid(PyObject
*self
, PyObject
*args
)
4109 if (!PyArg_ParseTuple(args
, "l:setuid", &uid_arg
))
4112 if (uid
!= uid_arg
) {
4113 PyErr_SetString(PyExc_OverflowError
, "user id too big");
4116 if (setuid(uid
) < 0)
4117 return posix_error();
4121 #endif /* HAVE_SETUID */
4125 PyDoc_STRVAR(posix_seteuid__doc__
,
4127 Set the current process's effective user id.");
4130 posix_seteuid (PyObject
*self
, PyObject
*args
)
4134 if (!PyArg_ParseTuple(args
, "l", &euid_arg
))
4137 if (euid
!= euid_arg
) {
4138 PyErr_SetString(PyExc_OverflowError
, "user id too big");
4141 if (seteuid(euid
) < 0) {
4142 return posix_error();
4148 #endif /* HAVE_SETEUID */
4151 PyDoc_STRVAR(posix_setegid__doc__
,
4153 Set the current process's effective group id.");
4156 posix_setegid (PyObject
*self
, PyObject
*args
)
4160 if (!PyArg_ParseTuple(args
, "l", &egid_arg
))
4163 if (egid
!= egid_arg
) {
4164 PyErr_SetString(PyExc_OverflowError
, "group id too big");
4167 if (setegid(egid
) < 0) {
4168 return posix_error();
4174 #endif /* HAVE_SETEGID */
4176 #ifdef HAVE_SETREUID
4177 PyDoc_STRVAR(posix_setreuid__doc__
,
4178 "setreuid(ruid, euid)\n\n\
4179 Set the current process's real and effective user ids.");
4182 posix_setreuid (PyObject
*self
, PyObject
*args
)
4184 long ruid_arg
, euid_arg
;
4186 if (!PyArg_ParseTuple(args
, "ll", &ruid_arg
, &euid_arg
))
4189 ruid
= (uid_t
)-1; /* let the compiler choose how -1 fits */
4191 ruid
= ruid_arg
; /* otherwise, assign from our long */
4196 if ((euid_arg
!= -1 && euid
!= euid_arg
) ||
4197 (ruid_arg
!= -1 && ruid
!= ruid_arg
)) {
4198 PyErr_SetString(PyExc_OverflowError
, "user id too big");
4201 if (setreuid(ruid
, euid
) < 0) {
4202 return posix_error();
4208 #endif /* HAVE_SETREUID */
4210 #ifdef HAVE_SETREGID
4211 PyDoc_STRVAR(posix_setregid__doc__
,
4212 "setregid(rgid, egid)\n\n\
4213 Set the current process's real and effective group ids.");
4216 posix_setregid (PyObject
*self
, PyObject
*args
)
4218 long rgid_arg
, egid_arg
;
4220 if (!PyArg_ParseTuple(args
, "ll", &rgid_arg
, &egid_arg
))
4223 rgid
= (gid_t
)-1; /* let the compiler choose how -1 fits */
4225 rgid
= rgid_arg
; /* otherwise, assign from our long */
4230 if ((egid_arg
!= -1 && egid
!= egid_arg
) ||
4231 (rgid_arg
!= -1 && rgid
!= rgid_arg
)) {
4232 PyErr_SetString(PyExc_OverflowError
, "group id too big");
4235 if (setregid(rgid
, egid
) < 0) {
4236 return posix_error();
4242 #endif /* HAVE_SETREGID */
4245 PyDoc_STRVAR(posix_setgid__doc__
,
4247 Set the current process's group id.");
4250 posix_setgid(PyObject
*self
, PyObject
*args
)
4254 if (!PyArg_ParseTuple(args
, "l:setgid", &gid_arg
))
4257 if (gid
!= gid_arg
) {
4258 PyErr_SetString(PyExc_OverflowError
, "group id too big");
4261 if (setgid(gid
) < 0)
4262 return posix_error();
4266 #endif /* HAVE_SETGID */
4268 #ifdef HAVE_SETGROUPS
4269 PyDoc_STRVAR(posix_setgroups__doc__
,
4270 "setgroups(list)\n\n\
4271 Set the groups of the current process to list.");
4274 posix_setgroups(PyObject
*self
, PyObject
*groups
)
4277 gid_t grouplist
[MAX_GROUPS
];
4279 if (!PySequence_Check(groups
)) {
4280 PyErr_SetString(PyExc_TypeError
, "setgroups argument must be a sequence");
4283 len
= PySequence_Size(groups
);
4284 if (len
> MAX_GROUPS
) {
4285 PyErr_SetString(PyExc_ValueError
, "too many groups");
4288 for(i
= 0; i
< len
; i
++) {
4290 elem
= PySequence_GetItem(groups
, i
);
4293 if (!PyInt_Check(elem
)) {
4294 if (!PyLong_Check(elem
)) {
4295 PyErr_SetString(PyExc_TypeError
,
4296 "groups must be integers");
4300 unsigned long x
= PyLong_AsUnsignedLong(elem
);
4301 if (PyErr_Occurred()) {
4302 PyErr_SetString(PyExc_TypeError
,
4303 "group id too big");
4308 /* read back to see if it fits in gid_t */
4309 if (grouplist
[i
] != x
) {
4310 PyErr_SetString(PyExc_TypeError
,
4311 "group id too big");
4317 long x
= PyInt_AsLong(elem
);
4319 if (grouplist
[i
] != x
) {
4320 PyErr_SetString(PyExc_TypeError
,
4321 "group id too big");
4329 if (setgroups(len
, grouplist
) < 0)
4330 return posix_error();
4334 #endif /* HAVE_SETGROUPS */
4336 #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
4338 wait_helper(pid_t pid
, int status
, struct rusage
*ru
)
4341 static PyObject
*struct_rusage
;
4344 return posix_error();
4346 if (struct_rusage
== NULL
) {
4347 PyObject
*m
= PyImport_ImportModuleNoBlock("resource");
4350 struct_rusage
= PyObject_GetAttrString(m
, "struct_rusage");
4352 if (struct_rusage
== NULL
)
4356 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
4357 result
= PyStructSequence_New((PyTypeObject
*) struct_rusage
);
4362 #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
4365 PyStructSequence_SET_ITEM(result
, 0,
4366 PyFloat_FromDouble(doubletime(ru
->ru_utime
)));
4367 PyStructSequence_SET_ITEM(result
, 1,
4368 PyFloat_FromDouble(doubletime(ru
->ru_stime
)));
4369 #define SET_INT(result, index, value)\
4370 PyStructSequence_SET_ITEM(result, index, PyInt_FromLong(value))
4371 SET_INT(result
, 2, ru
->ru_maxrss
);
4372 SET_INT(result
, 3, ru
->ru_ixrss
);
4373 SET_INT(result
, 4, ru
->ru_idrss
);
4374 SET_INT(result
, 5, ru
->ru_isrss
);
4375 SET_INT(result
, 6, ru
->ru_minflt
);
4376 SET_INT(result
, 7, ru
->ru_majflt
);
4377 SET_INT(result
, 8, ru
->ru_nswap
);
4378 SET_INT(result
, 9, ru
->ru_inblock
);
4379 SET_INT(result
, 10, ru
->ru_oublock
);
4380 SET_INT(result
, 11, ru
->ru_msgsnd
);
4381 SET_INT(result
, 12, ru
->ru_msgrcv
);
4382 SET_INT(result
, 13, ru
->ru_nsignals
);
4383 SET_INT(result
, 14, ru
->ru_nvcsw
);
4384 SET_INT(result
, 15, ru
->ru_nivcsw
);
4387 if (PyErr_Occurred()) {
4392 return Py_BuildValue("NiN", PyLong_FromPid(pid
), status
, result
);
4394 #endif /* HAVE_WAIT3 || HAVE_WAIT4 */
4397 PyDoc_STRVAR(posix_wait3__doc__
,
4398 "wait3(options) -> (pid, status, rusage)\n\n\
4399 Wait for completion of a child process.");
4402 posix_wait3(PyObject
*self
, PyObject
*args
)
4408 WAIT_STATUS_INT(status
) = 0;
4410 if (!PyArg_ParseTuple(args
, "i:wait3", &options
))
4413 Py_BEGIN_ALLOW_THREADS
4414 pid
= wait3(&status
, options
, &ru
);
4415 Py_END_ALLOW_THREADS
4417 return wait_helper(pid
, WAIT_STATUS_INT(status
), &ru
);
4419 #endif /* HAVE_WAIT3 */
4422 PyDoc_STRVAR(posix_wait4__doc__
,
4423 "wait4(pid, options) -> (pid, status, rusage)\n\n\
4424 Wait for completion of a given child process.");
4427 posix_wait4(PyObject
*self
, PyObject
*args
)
4433 WAIT_STATUS_INT(status
) = 0;
4435 if (!PyArg_ParseTuple(args
, PARSE_PID
"i:wait4", &pid
, &options
))
4438 Py_BEGIN_ALLOW_THREADS
4439 pid
= wait4(pid
, &status
, options
, &ru
);
4440 Py_END_ALLOW_THREADS
4442 return wait_helper(pid
, WAIT_STATUS_INT(status
), &ru
);
4444 #endif /* HAVE_WAIT4 */
4447 PyDoc_STRVAR(posix_waitpid__doc__
,
4448 "waitpid(pid, options) -> (pid, status)\n\n\
4449 Wait for completion of a given child process.");
4452 posix_waitpid(PyObject
*self
, PyObject
*args
)
4457 WAIT_STATUS_INT(status
) = 0;
4459 if (!PyArg_ParseTuple(args
, PARSE_PID
"i:waitpid", &pid
, &options
))
4461 Py_BEGIN_ALLOW_THREADS
4462 pid
= waitpid(pid
, &status
, options
);
4463 Py_END_ALLOW_THREADS
4465 return posix_error();
4467 return Py_BuildValue("Ni", PyLong_FromPid(pid
), WAIT_STATUS_INT(status
));
4470 #elif defined(HAVE_CWAIT)
4472 /* MS C has a variant of waitpid() that's usable for most purposes. */
4473 PyDoc_STRVAR(posix_waitpid__doc__
,
4474 "waitpid(pid, options) -> (pid, status << 8)\n\n"
4475 "Wait for completion of a given process. options is ignored on Windows.");
4478 posix_waitpid(PyObject
*self
, PyObject
*args
)
4481 int status
, options
;
4483 if (!PyArg_ParseTuple(args
, PARSE_PID
"i:waitpid", &pid
, &options
))
4485 Py_BEGIN_ALLOW_THREADS
4486 pid
= _cwait(&status
, pid
, options
);
4487 Py_END_ALLOW_THREADS
4489 return posix_error();
4491 /* shift the status left a byte so this is more like the POSIX waitpid */
4492 return Py_BuildValue("Ni", PyLong_FromPid(pid
), status
<< 8);
4494 #endif /* HAVE_WAITPID || HAVE_CWAIT */
4497 PyDoc_STRVAR(posix_wait__doc__
,
4498 "wait() -> (pid, status)\n\n\
4499 Wait for completion of a child process.");
4502 posix_wait(PyObject
*self
, PyObject
*noargs
)
4506 WAIT_STATUS_INT(status
) = 0;
4508 Py_BEGIN_ALLOW_THREADS
4509 pid
= wait(&status
);
4510 Py_END_ALLOW_THREADS
4512 return posix_error();
4514 return Py_BuildValue("Ni", PyLong_FromPid(pid
), WAIT_STATUS_INT(status
));
4519 PyDoc_STRVAR(posix_lstat__doc__
,
4520 "lstat(path) -> stat result\n\n\
4521 Like stat(path), but do not follow symbolic links.");
4524 posix_lstat(PyObject
*self
, PyObject
*args
)
4527 return posix_do_stat(self
, args
, "et:lstat", lstat
, NULL
, NULL
);
4528 #else /* !HAVE_LSTAT */
4529 return posix_do_stat(self
, args
, "et:lstat", STAT
, NULL
, NULL
);
4530 #endif /* !HAVE_LSTAT */
4534 #ifdef HAVE_READLINK
4535 PyDoc_STRVAR(posix_readlink__doc__
,
4536 "readlink(path) -> path\n\n\
4537 Return a string representing the path to which the symbolic link points.");
4540 posix_readlink(PyObject
*self
, PyObject
*args
)
4543 char buf
[MAXPATHLEN
];
4546 #ifdef Py_USING_UNICODE
4547 int arg_is_unicode
= 0;
4550 if (!PyArg_ParseTuple(args
, "et:readlink",
4551 Py_FileSystemDefaultEncoding
, &path
))
4553 #ifdef Py_USING_UNICODE
4554 v
= PySequence_GetItem(args
, 0);
4560 if (PyUnicode_Check(v
)) {
4566 Py_BEGIN_ALLOW_THREADS
4567 n
= readlink(path
, buf
, (int) sizeof buf
);
4568 Py_END_ALLOW_THREADS
4570 return posix_error_with_allocated_filename(path
);
4573 v
= PyString_FromStringAndSize(buf
, n
);
4574 #ifdef Py_USING_UNICODE
4575 if (arg_is_unicode
) {
4578 w
= PyUnicode_FromEncodedObject(v
,
4579 Py_FileSystemDefaultEncoding
,
4586 /* fall back to the original byte string, as
4587 discussed in patch #683592 */
4594 #endif /* HAVE_READLINK */
4598 PyDoc_STRVAR(posix_symlink__doc__
,
4599 "symlink(src, dst)\n\n\
4600 Create a symbolic link pointing to src named dst.");
4603 posix_symlink(PyObject
*self
, PyObject
*args
)
4605 return posix_2str(args
, "etet:symlink", symlink
);
4607 #endif /* HAVE_SYMLINK */
4611 #if defined(PYCC_VACPP) && defined(PYOS_OS2)
4617 Py_BEGIN_ALLOW_THREADS
4618 DosQuerySysInfo(QSV_MS_COUNT
, QSV_MS_COUNT
, &value
, sizeof(value
));
4619 Py_END_ALLOW_THREADS
4625 posix_times(PyObject
*self
, PyObject
*noargs
)
4627 /* Currently Only Uptime is Provided -- Others Later */
4628 return Py_BuildValue("ddddd",
4629 (double)0 /* t.tms_utime / HZ */,
4630 (double)0 /* t.tms_stime / HZ */,
4631 (double)0 /* t.tms_cutime / HZ */,
4632 (double)0 /* t.tms_cstime / HZ */,
4633 (double)system_uptime() / 1000);
4636 #define NEED_TICKS_PER_SECOND
4637 static long ticks_per_second
= -1;
4639 posix_times(PyObject
*self
, PyObject
*noargs
)
4645 if (c
== (clock_t) -1)
4646 return posix_error();
4647 return Py_BuildValue("ddddd",
4648 (double)t
.tms_utime
/ ticks_per_second
,
4649 (double)t
.tms_stime
/ ticks_per_second
,
4650 (double)t
.tms_cutime
/ ticks_per_second
,
4651 (double)t
.tms_cstime
/ ticks_per_second
,
4652 (double)c
/ ticks_per_second
);
4654 #endif /* not OS2 */
4655 #endif /* HAVE_TIMES */
4659 PyDoc_STRVAR(posix_times__doc__
,
4660 "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
4661 Return a tuple of floating point numbers indicating process times.");
4666 PyDoc_STRVAR(posix_getsid__doc__
,
4667 "getsid(pid) -> sid\n\n\
4668 Call the system call getsid().");
4671 posix_getsid(PyObject
*self
, PyObject
*args
)
4675 if (!PyArg_ParseTuple(args
, PARSE_PID
":getsid", &pid
))
4679 return posix_error();
4680 return PyInt_FromLong((long)sid
);
4682 #endif /* HAVE_GETSID */
4686 PyDoc_STRVAR(posix_setsid__doc__
,
4688 Call the system call setsid().");
4691 posix_setsid(PyObject
*self
, PyObject
*noargs
)
4694 return posix_error();
4698 #endif /* HAVE_SETSID */
4701 PyDoc_STRVAR(posix_setpgid__doc__
,
4702 "setpgid(pid, pgrp)\n\n\
4703 Call the system call setpgid().");
4706 posix_setpgid(PyObject
*self
, PyObject
*args
)
4710 if (!PyArg_ParseTuple(args
, PARSE_PID
"i:setpgid", &pid
, &pgrp
))
4712 if (setpgid(pid
, pgrp
) < 0)
4713 return posix_error();
4717 #endif /* HAVE_SETPGID */
4720 #ifdef HAVE_TCGETPGRP
4721 PyDoc_STRVAR(posix_tcgetpgrp__doc__
,
4722 "tcgetpgrp(fd) -> pgid\n\n\
4723 Return the process group associated with the terminal given by a fd.");
4726 posix_tcgetpgrp(PyObject
*self
, PyObject
*args
)
4730 if (!PyArg_ParseTuple(args
, "i:tcgetpgrp", &fd
))
4732 pgid
= tcgetpgrp(fd
);
4734 return posix_error();
4735 return PyLong_FromPid(pgid
);
4737 #endif /* HAVE_TCGETPGRP */
4740 #ifdef HAVE_TCSETPGRP
4741 PyDoc_STRVAR(posix_tcsetpgrp__doc__
,
4742 "tcsetpgrp(fd, pgid)\n\n\
4743 Set the process group associated with the terminal given by a fd.");
4746 posix_tcsetpgrp(PyObject
*self
, PyObject
*args
)
4750 if (!PyArg_ParseTuple(args
, "i" PARSE_PID
":tcsetpgrp", &fd
, &pgid
))
4752 if (tcsetpgrp(fd
, pgid
) < 0)
4753 return posix_error();
4757 #endif /* HAVE_TCSETPGRP */
4759 /* Functions acting on file descriptors */
4761 PyDoc_STRVAR(posix_open__doc__
,
4762 "open(filename, flag [, mode=0777]) -> fd\n\n\
4763 Open a file (for low level IO).");
4766 posix_open(PyObject
*self
, PyObject
*args
)
4773 if (!PyArg_ParseTuple(args
, "eti|i",
4774 Py_FileSystemDefaultEncoding
, &file
,
4778 Py_BEGIN_ALLOW_THREADS
4779 fd
= open(file
, flag
, mode
);
4780 Py_END_ALLOW_THREADS
4782 return posix_error_with_allocated_filename(file
);
4784 return PyInt_FromLong((long)fd
);
4788 PyDoc_STRVAR(posix_close__doc__
,
4790 Close a file descriptor (for low level IO).");
4793 posix_close(PyObject
*self
, PyObject
*args
)
4796 if (!PyArg_ParseTuple(args
, "i:close", &fd
))
4798 if (!_PyVerify_fd(fd
))
4799 return posix_error();
4800 Py_BEGIN_ALLOW_THREADS
4802 Py_END_ALLOW_THREADS
4804 return posix_error();
4810 PyDoc_STRVAR(posix_closerange__doc__
,
4811 "closerange(fd_low, fd_high)\n\n\
4812 Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
4815 posix_closerange(PyObject
*self
, PyObject
*args
)
4817 int fd_from
, fd_to
, i
;
4818 if (!PyArg_ParseTuple(args
, "ii:closerange", &fd_from
, &fd_to
))
4820 Py_BEGIN_ALLOW_THREADS
4821 for (i
= fd_from
; i
< fd_to
; i
++)
4822 if (_PyVerify_fd(i
))
4824 Py_END_ALLOW_THREADS
4829 PyDoc_STRVAR(posix_dup__doc__
,
4830 "dup(fd) -> fd2\n\n\
4831 Return a duplicate of a file descriptor.");
4834 posix_dup(PyObject
*self
, PyObject
*args
)
4837 if (!PyArg_ParseTuple(args
, "i:dup", &fd
))
4839 if (!_PyVerify_fd(fd
))
4840 return posix_error();
4841 Py_BEGIN_ALLOW_THREADS
4843 Py_END_ALLOW_THREADS
4845 return posix_error();
4846 return PyInt_FromLong((long)fd
);
4850 PyDoc_STRVAR(posix_dup2__doc__
,
4851 "dup2(old_fd, new_fd)\n\n\
4852 Duplicate file descriptor.");
4855 posix_dup2(PyObject
*self
, PyObject
*args
)
4858 if (!PyArg_ParseTuple(args
, "ii:dup2", &fd
, &fd2
))
4860 if (!_PyVerify_fd_dup2(fd
, fd2
))
4861 return posix_error();
4862 Py_BEGIN_ALLOW_THREADS
4863 res
= dup2(fd
, fd2
);
4864 Py_END_ALLOW_THREADS
4866 return posix_error();
4872 PyDoc_STRVAR(posix_lseek__doc__
,
4873 "lseek(fd, pos, how) -> newpos\n\n\
4874 Set the current position of a file descriptor.");
4877 posix_lseek(PyObject
*self
, PyObject
*args
)
4882 if (!PyArg_ParseTuple(args
, "iOi:lseek", &fd
, &posobj
, &how
))
4885 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
4887 case 0: how
= SEEK_SET
; break;
4888 case 1: how
= SEEK_CUR
; break;
4889 case 2: how
= SEEK_END
; break;
4891 #endif /* SEEK_END */
4893 #if !defined(HAVE_LARGEFILE_SUPPORT)
4894 pos
= PyInt_AsLong(posobj
);
4896 pos
= PyLong_Check(posobj
) ?
4897 PyLong_AsLongLong(posobj
) : PyInt_AsLong(posobj
);
4899 if (PyErr_Occurred())
4902 if (!_PyVerify_fd(fd
))
4903 return posix_error();
4904 Py_BEGIN_ALLOW_THREADS
4905 res
= lseek(fd
, pos
, how
);
4906 Py_END_ALLOW_THREADS
4908 return posix_error();
4910 #if !defined(HAVE_LARGEFILE_SUPPORT)
4911 return PyInt_FromLong(res
);
4913 return PyLong_FromLongLong(res
);
4918 PyDoc_STRVAR(posix_read__doc__
,
4919 "read(fd, buffersize) -> string\n\n\
4920 Read a file descriptor.");
4923 posix_read(PyObject
*self
, PyObject
*args
)
4927 if (!PyArg_ParseTuple(args
, "ii:read", &fd
, &size
))
4931 return posix_error();
4933 buffer
= PyString_FromStringAndSize((char *)NULL
, size
);
4936 if (!_PyVerify_fd(fd
)) {
4938 return posix_error();
4940 Py_BEGIN_ALLOW_THREADS
4941 n
= read(fd
, PyString_AsString(buffer
), size
);
4942 Py_END_ALLOW_THREADS
4945 return posix_error();
4948 _PyString_Resize(&buffer
, n
);
4953 PyDoc_STRVAR(posix_write__doc__
,
4954 "write(fd, string) -> byteswritten\n\n\
4955 Write a string to a file descriptor.");
4958 posix_write(PyObject
*self
, PyObject
*args
)
4964 if (!PyArg_ParseTuple(args
, "is*:write", &fd
, &pbuf
))
4966 if (!_PyVerify_fd(fd
)) {
4967 PyBuffer_Release(&pbuf
);
4968 return posix_error();
4970 Py_BEGIN_ALLOW_THREADS
4971 size
= write(fd
, pbuf
.buf
, (size_t)pbuf
.len
);
4972 Py_END_ALLOW_THREADS
4973 PyBuffer_Release(&pbuf
);
4975 return posix_error();
4976 return PyInt_FromSsize_t(size
);
4980 PyDoc_STRVAR(posix_fstat__doc__
,
4981 "fstat(fd) -> stat result\n\n\
4982 Like stat(), but for an open file descriptor.");
4985 posix_fstat(PyObject
*self
, PyObject
*args
)
4990 if (!PyArg_ParseTuple(args
, "i:fstat", &fd
))
4992 if (!_PyVerify_fd(fd
))
4993 return posix_error();
4994 Py_BEGIN_ALLOW_THREADS
4995 res
= FSTAT(fd
, &st
);
4996 Py_END_ALLOW_THREADS
4998 return posix_error();
5001 return _pystat_fromstructstat(&st
);
5005 PyDoc_STRVAR(posix_fdopen__doc__
,
5006 "fdopen(fd [, mode='r' [, bufsize]]) -> file_object\n\n\
5007 Return an open file object connected to a file descriptor.");
5010 posix_fdopen(PyObject
*self
, PyObject
*args
)
5013 char *orgmode
= "r";
5018 if (!PyArg_ParseTuple(args
, "i|si", &fd
, &orgmode
, &bufsize
))
5021 /* Sanitize mode. See fileobject.c */
5022 mode
= PyMem_MALLOC(strlen(orgmode
)+3);
5027 strcpy(mode
, orgmode
);
5028 if (_PyFile_SanitizeMode(mode
)) {
5032 if (!_PyVerify_fd(fd
))
5033 return posix_error();
5034 Py_BEGIN_ALLOW_THREADS
5035 #if !defined(MS_WINDOWS) && defined(HAVE_FCNTL_H)
5036 if (mode
[0] == 'a') {
5037 /* try to make sure the O_APPEND flag is set */
5039 flags
= fcntl(fd
, F_GETFL
);
5041 fcntl(fd
, F_SETFL
, flags
| O_APPEND
);
5042 fp
= fdopen(fd
, mode
);
5043 if (fp
== NULL
&& flags
!= -1)
5044 /* restore old mode if fdopen failed */
5045 fcntl(fd
, F_SETFL
, flags
);
5047 fp
= fdopen(fd
, mode
);
5050 fp
= fdopen(fd
, mode
);
5052 Py_END_ALLOW_THREADS
5055 return posix_error();
5056 f
= PyFile_FromFile(fp
, "<fdopen>", orgmode
, fclose
);
5058 PyFile_SetBufSize(f
, bufsize
);
5062 PyDoc_STRVAR(posix_isatty__doc__
,
5063 "isatty(fd) -> bool\n\n\
5064 Return True if the file descriptor 'fd' is an open file descriptor\n\
5065 connected to the slave end of a terminal.");
5068 posix_isatty(PyObject
*self
, PyObject
*args
)
5071 if (!PyArg_ParseTuple(args
, "i:isatty", &fd
))
5073 if (!_PyVerify_fd(fd
))
5074 return PyBool_FromLong(0);
5075 return PyBool_FromLong(isatty(fd
));
5079 PyDoc_STRVAR(posix_pipe__doc__
,
5080 "pipe() -> (read_end, write_end)\n\n\
5084 posix_pipe(PyObject
*self
, PyObject
*noargs
)
5086 #if defined(PYOS_OS2)
5090 Py_BEGIN_ALLOW_THREADS
5091 rc
= DosCreatePipe( &read
, &write
, 4096);
5092 Py_END_ALLOW_THREADS
5094 return os2_error(rc
);
5096 return Py_BuildValue("(ii)", read
, write
);
5098 #if !defined(MS_WINDOWS)
5101 Py_BEGIN_ALLOW_THREADS
5103 Py_END_ALLOW_THREADS
5105 return posix_error();
5106 return Py_BuildValue("(ii)", fds
[0], fds
[1]);
5107 #else /* MS_WINDOWS */
5109 int read_fd
, write_fd
;
5111 Py_BEGIN_ALLOW_THREADS
5112 ok
= CreatePipe(&read
, &write
, NULL
, 0);
5113 Py_END_ALLOW_THREADS
5115 return win32_error("CreatePipe", NULL
);
5116 read_fd
= _open_osfhandle((Py_intptr_t
)read
, 0);
5117 write_fd
= _open_osfhandle((Py_intptr_t
)write
, 1);
5118 return Py_BuildValue("(ii)", read_fd
, write_fd
);
5119 #endif /* MS_WINDOWS */
5122 #endif /* HAVE_PIPE */
5126 PyDoc_STRVAR(posix_mkfifo__doc__
,
5127 "mkfifo(filename [, mode=0666])\n\n\
5128 Create a FIFO (a POSIX named pipe).");
5131 posix_mkfifo(PyObject
*self
, PyObject
*args
)
5136 if (!PyArg_ParseTuple(args
, "s|i:mkfifo", &filename
, &mode
))
5138 Py_BEGIN_ALLOW_THREADS
5139 res
= mkfifo(filename
, mode
);
5140 Py_END_ALLOW_THREADS
5142 return posix_error();
5149 #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
5150 PyDoc_STRVAR(posix_mknod__doc__
,
5151 "mknod(filename [, mode=0600, device])\n\n\
5152 Create a filesystem node (file, device special file or named pipe)\n\
5153 named filename. mode specifies both the permissions to use and the\n\
5154 type of node to be created, being combined (bitwise OR) with one of\n\
5155 S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
5156 device defines the newly created device special file (probably using\n\
5157 os.makedev()), otherwise it is ignored.");
5161 posix_mknod(PyObject
*self
, PyObject
*args
)
5167 if (!PyArg_ParseTuple(args
, "s|ii:mknod", &filename
, &mode
, &device
))
5169 Py_BEGIN_ALLOW_THREADS
5170 res
= mknod(filename
, mode
, device
);
5171 Py_END_ALLOW_THREADS
5173 return posix_error();
5179 #ifdef HAVE_DEVICE_MACROS
5180 PyDoc_STRVAR(posix_major__doc__
,
5181 "major(device) -> major number\n\
5182 Extracts a device major number from a raw device number.");
5185 posix_major(PyObject
*self
, PyObject
*args
)
5188 if (!PyArg_ParseTuple(args
, "i:major", &device
))
5190 return PyInt_FromLong((long)major(device
));
5193 PyDoc_STRVAR(posix_minor__doc__
,
5194 "minor(device) -> minor number\n\
5195 Extracts a device minor number from a raw device number.");
5198 posix_minor(PyObject
*self
, PyObject
*args
)
5201 if (!PyArg_ParseTuple(args
, "i:minor", &device
))
5203 return PyInt_FromLong((long)minor(device
));
5206 PyDoc_STRVAR(posix_makedev__doc__
,
5207 "makedev(major, minor) -> device number\n\
5208 Composes a raw device number from the major and minor device numbers.");
5211 posix_makedev(PyObject
*self
, PyObject
*args
)
5214 if (!PyArg_ParseTuple(args
, "ii:makedev", &major
, &minor
))
5216 return PyInt_FromLong((long)makedev(major
, minor
));
5218 #endif /* device macros */
5221 #ifdef HAVE_FTRUNCATE
5222 PyDoc_STRVAR(posix_ftruncate__doc__
,
5223 "ftruncate(fd, length)\n\n\
5224 Truncate a file to a specified length.");
5227 posix_ftruncate(PyObject
*self
, PyObject
*args
)
5234 if (!PyArg_ParseTuple(args
, "iO:ftruncate", &fd
, &lenobj
))
5237 #if !defined(HAVE_LARGEFILE_SUPPORT)
5238 length
= PyInt_AsLong(lenobj
);
5240 length
= PyLong_Check(lenobj
) ?
5241 PyLong_AsLongLong(lenobj
) : PyInt_AsLong(lenobj
);
5243 if (PyErr_Occurred())
5246 Py_BEGIN_ALLOW_THREADS
5247 res
= ftruncate(fd
, length
);
5248 Py_END_ALLOW_THREADS
5250 return posix_error();
5257 PyDoc_STRVAR(posix_putenv__doc__
,
5258 "putenv(key, value)\n\n\
5259 Change or add an environment variable.");
5261 /* Save putenv() parameters as values here, so we can collect them when they
5262 * get re-set with another call for the same key. */
5263 static PyObject
*posix_putenv_garbage
;
5266 posix_putenv(PyObject
*self
, PyObject
*args
)
5273 if (!PyArg_ParseTuple(args
, "ss:putenv", &s1
, &s2
))
5276 #if defined(PYOS_OS2)
5277 if (stricmp(s1
, "BEGINLIBPATH") == 0) {
5280 rc
= DosSetExtLIBPATH(s2
, BEGIN_LIBPATH
);
5282 return os2_error(rc
);
5284 } else if (stricmp(s1
, "ENDLIBPATH") == 0) {
5287 rc
= DosSetExtLIBPATH(s2
, END_LIBPATH
);
5289 return os2_error(rc
);
5293 /* XXX This can leak memory -- not easy to fix :-( */
5294 len
= strlen(s1
) + strlen(s2
) + 2;
5295 /* len includes space for a trailing \0; the size arg to
5296 PyString_FromStringAndSize does not count that */
5297 newstr
= PyString_FromStringAndSize(NULL
, (int)len
- 1);
5299 return PyErr_NoMemory();
5300 newenv
= PyString_AS_STRING(newstr
);
5301 PyOS_snprintf(newenv
, len
, "%s=%s", s1
, s2
);
5302 if (putenv(newenv
)) {
5307 /* Install the first arg and newstr in posix_putenv_garbage;
5308 * this will cause previous value to be collected. This has to
5309 * happen after the real putenv() call because the old value
5310 * was still accessible until then. */
5311 if (PyDict_SetItem(posix_putenv_garbage
,
5312 PyTuple_GET_ITEM(args
, 0), newstr
)) {
5313 /* really not much we can do; just leak */
5320 #if defined(PYOS_OS2)
5328 #ifdef HAVE_UNSETENV
5329 PyDoc_STRVAR(posix_unsetenv__doc__
,
5331 Delete an environment variable.");
5334 posix_unsetenv(PyObject
*self
, PyObject
*args
)
5338 if (!PyArg_ParseTuple(args
, "s:unsetenv", &s1
))
5343 /* Remove the key from posix_putenv_garbage;
5344 * this will cause it to be collected. This has to
5345 * happen after the real unsetenv() call because the
5346 * old value was still accessible until then.
5348 if (PyDict_DelItem(posix_putenv_garbage
,
5349 PyTuple_GET_ITEM(args
, 0))) {
5350 /* really not much we can do; just leak */
5357 #endif /* unsetenv */
5359 PyDoc_STRVAR(posix_strerror__doc__
,
5360 "strerror(code) -> string\n\n\
5361 Translate an error code to a message string.");
5364 posix_strerror(PyObject
*self
, PyObject
*args
)
5368 if (!PyArg_ParseTuple(args
, "i:strerror", &code
))
5370 message
= strerror(code
);
5371 if (message
== NULL
) {
5372 PyErr_SetString(PyExc_ValueError
,
5373 "strerror() argument out of range");
5376 return PyString_FromString(message
);
5380 #ifdef HAVE_SYS_WAIT_H
5383 PyDoc_STRVAR(posix_WCOREDUMP__doc__
,
5384 "WCOREDUMP(status) -> bool\n\n\
5385 Return True if the process returning 'status' was dumped to a core file.");
5388 posix_WCOREDUMP(PyObject
*self
, PyObject
*args
)
5391 WAIT_STATUS_INT(status
) = 0;
5393 if (!PyArg_ParseTuple(args
, "i:WCOREDUMP", &WAIT_STATUS_INT(status
)))
5396 return PyBool_FromLong(WCOREDUMP(status
));
5398 #endif /* WCOREDUMP */
5401 PyDoc_STRVAR(posix_WIFCONTINUED__doc__
,
5402 "WIFCONTINUED(status) -> bool\n\n\
5403 Return True if the process returning 'status' was continued from a\n\
5404 job control stop.");
5407 posix_WIFCONTINUED(PyObject
*self
, PyObject
*args
)
5410 WAIT_STATUS_INT(status
) = 0;
5412 if (!PyArg_ParseTuple(args
, "i:WCONTINUED", &WAIT_STATUS_INT(status
)))
5415 return PyBool_FromLong(WIFCONTINUED(status
));
5417 #endif /* WIFCONTINUED */
5420 PyDoc_STRVAR(posix_WIFSTOPPED__doc__
,
5421 "WIFSTOPPED(status) -> bool\n\n\
5422 Return True if the process returning 'status' was stopped.");
5425 posix_WIFSTOPPED(PyObject
*self
, PyObject
*args
)
5428 WAIT_STATUS_INT(status
) = 0;
5430 if (!PyArg_ParseTuple(args
, "i:WIFSTOPPED", &WAIT_STATUS_INT(status
)))
5433 return PyBool_FromLong(WIFSTOPPED(status
));
5435 #endif /* WIFSTOPPED */
5438 PyDoc_STRVAR(posix_WIFSIGNALED__doc__
,
5439 "WIFSIGNALED(status) -> bool\n\n\
5440 Return True if the process returning 'status' was terminated by a signal.");
5443 posix_WIFSIGNALED(PyObject
*self
, PyObject
*args
)
5446 WAIT_STATUS_INT(status
) = 0;
5448 if (!PyArg_ParseTuple(args
, "i:WIFSIGNALED", &WAIT_STATUS_INT(status
)))
5451 return PyBool_FromLong(WIFSIGNALED(status
));
5453 #endif /* WIFSIGNALED */
5456 PyDoc_STRVAR(posix_WIFEXITED__doc__
,
5457 "WIFEXITED(status) -> bool\n\n\
5458 Return true if the process returning 'status' exited using the exit()\n\
5462 posix_WIFEXITED(PyObject
*self
, PyObject
*args
)
5465 WAIT_STATUS_INT(status
) = 0;
5467 if (!PyArg_ParseTuple(args
, "i:WIFEXITED", &WAIT_STATUS_INT(status
)))
5470 return PyBool_FromLong(WIFEXITED(status
));
5472 #endif /* WIFEXITED */
5475 PyDoc_STRVAR(posix_WEXITSTATUS__doc__
,
5476 "WEXITSTATUS(status) -> integer\n\n\
5477 Return the process return code from 'status'.");
5480 posix_WEXITSTATUS(PyObject
*self
, PyObject
*args
)
5483 WAIT_STATUS_INT(status
) = 0;
5485 if (!PyArg_ParseTuple(args
, "i:WEXITSTATUS", &WAIT_STATUS_INT(status
)))
5488 return Py_BuildValue("i", WEXITSTATUS(status
));
5490 #endif /* WEXITSTATUS */
5493 PyDoc_STRVAR(posix_WTERMSIG__doc__
,
5494 "WTERMSIG(status) -> integer\n\n\
5495 Return the signal that terminated the process that provided the 'status'\n\
5499 posix_WTERMSIG(PyObject
*self
, PyObject
*args
)
5502 WAIT_STATUS_INT(status
) = 0;
5504 if (!PyArg_ParseTuple(args
, "i:WTERMSIG", &WAIT_STATUS_INT(status
)))
5507 return Py_BuildValue("i", WTERMSIG(status
));
5509 #endif /* WTERMSIG */
5512 PyDoc_STRVAR(posix_WSTOPSIG__doc__
,
5513 "WSTOPSIG(status) -> integer\n\n\
5514 Return the signal that stopped the process that provided\n\
5515 the 'status' value.");
5518 posix_WSTOPSIG(PyObject
*self
, PyObject
*args
)
5521 WAIT_STATUS_INT(status
) = 0;
5523 if (!PyArg_ParseTuple(args
, "i:WSTOPSIG", &WAIT_STATUS_INT(status
)))
5526 return Py_BuildValue("i", WSTOPSIG(status
));
5528 #endif /* WSTOPSIG */
5530 #endif /* HAVE_SYS_WAIT_H */
5533 #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
5535 /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
5536 needed definitions in sys/statvfs.h */
5539 #include <sys/statvfs.h>
5542 _pystatvfs_fromstructstatvfs(struct statvfs st
) {
5543 PyObject
*v
= PyStructSequence_New(&StatVFSResultType
);
5547 #if !defined(HAVE_LARGEFILE_SUPPORT)
5548 PyStructSequence_SET_ITEM(v
, 0, PyInt_FromLong((long) st
.f_bsize
));
5549 PyStructSequence_SET_ITEM(v
, 1, PyInt_FromLong((long) st
.f_frsize
));
5550 PyStructSequence_SET_ITEM(v
, 2, PyInt_FromLong((long) st
.f_blocks
));
5551 PyStructSequence_SET_ITEM(v
, 3, PyInt_FromLong((long) st
.f_bfree
));
5552 PyStructSequence_SET_ITEM(v
, 4, PyInt_FromLong((long) st
.f_bavail
));
5553 PyStructSequence_SET_ITEM(v
, 5, PyInt_FromLong((long) st
.f_files
));
5554 PyStructSequence_SET_ITEM(v
, 6, PyInt_FromLong((long) st
.f_ffree
));
5555 PyStructSequence_SET_ITEM(v
, 7, PyInt_FromLong((long) st
.f_favail
));
5556 PyStructSequence_SET_ITEM(v
, 8, PyInt_FromLong((long) st
.f_flag
));
5557 PyStructSequence_SET_ITEM(v
, 9, PyInt_FromLong((long) st
.f_namemax
));
5559 PyStructSequence_SET_ITEM(v
, 0, PyInt_FromLong((long) st
.f_bsize
));
5560 PyStructSequence_SET_ITEM(v
, 1, PyInt_FromLong((long) st
.f_frsize
));
5561 PyStructSequence_SET_ITEM(v
, 2,
5562 PyLong_FromLongLong((PY_LONG_LONG
) st
.f_blocks
));
5563 PyStructSequence_SET_ITEM(v
, 3,
5564 PyLong_FromLongLong((PY_LONG_LONG
) st
.f_bfree
));
5565 PyStructSequence_SET_ITEM(v
, 4,
5566 PyLong_FromLongLong((PY_LONG_LONG
) st
.f_bavail
));
5567 PyStructSequence_SET_ITEM(v
, 5,
5568 PyLong_FromLongLong((PY_LONG_LONG
) st
.f_files
));
5569 PyStructSequence_SET_ITEM(v
, 6,
5570 PyLong_FromLongLong((PY_LONG_LONG
) st
.f_ffree
));
5571 PyStructSequence_SET_ITEM(v
, 7,
5572 PyLong_FromLongLong((PY_LONG_LONG
) st
.f_favail
));
5573 PyStructSequence_SET_ITEM(v
, 8, PyInt_FromLong((long) st
.f_flag
));
5574 PyStructSequence_SET_ITEM(v
, 9, PyInt_FromLong((long) st
.f_namemax
));
5580 PyDoc_STRVAR(posix_fstatvfs__doc__
,
5581 "fstatvfs(fd) -> statvfs result\n\n\
5582 Perform an fstatvfs system call on the given fd.");
5585 posix_fstatvfs(PyObject
*self
, PyObject
*args
)
5590 if (!PyArg_ParseTuple(args
, "i:fstatvfs", &fd
))
5592 Py_BEGIN_ALLOW_THREADS
5593 res
= fstatvfs(fd
, &st
);
5594 Py_END_ALLOW_THREADS
5596 return posix_error();
5598 return _pystatvfs_fromstructstatvfs(st
);
5600 #endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
5603 #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
5604 #include <sys/statvfs.h>
5606 PyDoc_STRVAR(posix_statvfs__doc__
,
5607 "statvfs(path) -> statvfs result\n\n\
5608 Perform a statvfs system call on the given path.");
5611 posix_statvfs(PyObject
*self
, PyObject
*args
)
5616 if (!PyArg_ParseTuple(args
, "s:statvfs", &path
))
5618 Py_BEGIN_ALLOW_THREADS
5619 res
= statvfs(path
, &st
);
5620 Py_END_ALLOW_THREADS
5622 return posix_error_with_filename(path
);
5624 return _pystatvfs_fromstructstatvfs(st
);
5626 #endif /* HAVE_STATVFS */
5630 PyDoc_STRVAR(posix_tempnam__doc__
,
5631 "tempnam([dir[, prefix]]) -> string\n\n\
5632 Return a unique name for a temporary file.\n\
5633 The directory and a prefix may be specified as strings; they may be omitted\n\
5634 or None if not needed.");
5637 posix_tempnam(PyObject
*self
, PyObject
*args
)
5639 PyObject
*result
= NULL
;
5644 if (!PyArg_ParseTuple(args
, "|zz:tempnam", &dir
, &pfx
))
5647 if (PyErr_Warn(PyExc_RuntimeWarning
,
5648 "tempnam is a potential security risk to your program") < 0)
5651 if (PyErr_WarnPy3k("tempnam has been removed in 3.x; "
5652 "use the tempfile module", 1) < 0)
5655 name
= tempnam(dir
, pfx
);
5657 return PyErr_NoMemory();
5658 result
= PyString_FromString(name
);
5666 PyDoc_STRVAR(posix_tmpfile__doc__
,
5667 "tmpfile() -> file object\n\n\
5668 Create a temporary file with no directory entries.");
5671 posix_tmpfile(PyObject
*self
, PyObject
*noargs
)
5675 if (PyErr_WarnPy3k("tmpfile has been removed in 3.x; "
5676 "use the tempfile module", 1) < 0)
5681 return posix_error();
5682 return PyFile_FromFile(fp
, "<tmpfile>", "w+b", fclose
);
5688 PyDoc_STRVAR(posix_tmpnam__doc__
,
5689 "tmpnam() -> string\n\n\
5690 Return a unique name for a temporary file.");
5693 posix_tmpnam(PyObject
*self
, PyObject
*noargs
)
5695 char buffer
[L_tmpnam
];
5698 if (PyErr_Warn(PyExc_RuntimeWarning
,
5699 "tmpnam is a potential security risk to your program") < 0)
5702 if (PyErr_WarnPy3k("tmpnam has been removed in 3.x; "
5703 "use the tempfile module", 1) < 0)
5707 name
= tmpnam_r(buffer
);
5709 name
= tmpnam(buffer
);
5712 PyObject
*err
= Py_BuildValue("is", 0,
5714 "unexpected NULL from tmpnam_r"
5716 "unexpected NULL from tmpnam"
5719 PyErr_SetObject(PyExc_OSError
, err
);
5723 return PyString_FromString(buffer
);
5728 /* This is used for fpathconf(), pathconf(), confstr() and sysconf().
5729 * It maps strings representing configuration variable names to
5730 * integer values, allowing those functions to be called with the
5731 * magic names instead of polluting the module's namespace with tons of
5732 * rarely-used constants. There are three separate tables that use
5733 * these definitions.
5735 * This code is always included, even if none of the interfaces that
5736 * need it are included. The #if hackery needed to avoid it would be
5737 * sufficiently pervasive that it's not worth the loss of readability.
5744 #ifndef UEFI_C_SOURCE
5746 conv_confname(PyObject
*arg
, int *valuep
, struct constdef
*table
,
5749 if (PyInt_Check(arg
)) {
5750 *valuep
= PyInt_AS_LONG(arg
);
5753 if (PyString_Check(arg
)) {
5754 /* look up the value in the table using a binary search */
5757 size_t hi
= tablesize
;
5759 char *confname
= PyString_AS_STRING(arg
);
5761 mid
= (lo
+ hi
) / 2;
5762 cmp
= strcmp(confname
, table
[mid
].name
);
5768 *valuep
= table
[mid
].value
;
5772 PyErr_SetString(PyExc_ValueError
, "unrecognized configuration name");
5775 PyErr_SetString(PyExc_TypeError
,
5776 "configuration names must be strings or integers");
5779 #endif /* UEFI_C_SOURCE */
5781 #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
5782 static struct constdef posix_constants_pathconf
[] = {
5783 #ifdef _PC_ABI_AIO_XFER_MAX
5784 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX
},
5786 #ifdef _PC_ABI_ASYNC_IO
5787 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO
},
5790 {"PC_ASYNC_IO", _PC_ASYNC_IO
},
5792 #ifdef _PC_CHOWN_RESTRICTED
5793 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED
},
5795 #ifdef _PC_FILESIZEBITS
5796 {"PC_FILESIZEBITS", _PC_FILESIZEBITS
},
5799 {"PC_LAST", _PC_LAST
},
5802 {"PC_LINK_MAX", _PC_LINK_MAX
},
5804 #ifdef _PC_MAX_CANON
5805 {"PC_MAX_CANON", _PC_MAX_CANON
},
5807 #ifdef _PC_MAX_INPUT
5808 {"PC_MAX_INPUT", _PC_MAX_INPUT
},
5811 {"PC_NAME_MAX", _PC_NAME_MAX
},
5814 {"PC_NO_TRUNC", _PC_NO_TRUNC
},
5817 {"PC_PATH_MAX", _PC_PATH_MAX
},
5820 {"PC_PIPE_BUF", _PC_PIPE_BUF
},
5823 {"PC_PRIO_IO", _PC_PRIO_IO
},
5825 #ifdef _PC_SOCK_MAXBUF
5826 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF
},
5829 {"PC_SYNC_IO", _PC_SYNC_IO
},
5832 {"PC_VDISABLE", _PC_VDISABLE
},
5837 conv_path_confname(PyObject
*arg
, int *valuep
)
5839 return conv_confname(arg
, valuep
, posix_constants_pathconf
,
5840 sizeof(posix_constants_pathconf
)
5841 / sizeof(struct constdef
));
5845 #ifdef HAVE_FPATHCONF
5846 PyDoc_STRVAR(posix_fpathconf__doc__
,
5847 "fpathconf(fd, name) -> integer\n\n\
5848 Return the configuration limit name for the file descriptor fd.\n\
5849 If there is no limit, return -1.");
5852 posix_fpathconf(PyObject
*self
, PyObject
*args
)
5854 PyObject
*result
= NULL
;
5857 if (PyArg_ParseTuple(args
, "iO&:fpathconf", &fd
,
5858 conv_path_confname
, &name
)) {
5862 limit
= fpathconf(fd
, name
);
5863 if (limit
== -1 && errno
!= 0)
5866 result
= PyInt_FromLong(limit
);
5873 #ifdef HAVE_PATHCONF
5874 PyDoc_STRVAR(posix_pathconf__doc__
,
5875 "pathconf(path, name) -> integer\n\n\
5876 Return the configuration limit name for the file or directory path.\n\
5877 If there is no limit, return -1.");
5880 posix_pathconf(PyObject
*self
, PyObject
*args
)
5882 PyObject
*result
= NULL
;
5886 if (PyArg_ParseTuple(args
, "sO&:pathconf", &path
,
5887 conv_path_confname
, &name
)) {
5891 limit
= pathconf(path
, name
);
5892 if (limit
== -1 && errno
!= 0) {
5893 if (errno
== EINVAL
)
5894 /* could be a path or name problem */
5897 posix_error_with_filename(path
);
5900 result
= PyInt_FromLong(limit
);
5907 static struct constdef posix_constants_confstr
[] = {
5908 #ifdef _CS_ARCHITECTURE
5909 {"CS_ARCHITECTURE", _CS_ARCHITECTURE
},
5912 {"CS_HOSTNAME", _CS_HOSTNAME
},
5914 #ifdef _CS_HW_PROVIDER
5915 {"CS_HW_PROVIDER", _CS_HW_PROVIDER
},
5917 #ifdef _CS_HW_SERIAL
5918 {"CS_HW_SERIAL", _CS_HW_SERIAL
},
5920 #ifdef _CS_INITTAB_NAME
5921 {"CS_INITTAB_NAME", _CS_INITTAB_NAME
},
5923 #ifdef _CS_LFS64_CFLAGS
5924 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS
},
5926 #ifdef _CS_LFS64_LDFLAGS
5927 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS
},
5929 #ifdef _CS_LFS64_LIBS
5930 {"CS_LFS64_LIBS", _CS_LFS64_LIBS
},
5932 #ifdef _CS_LFS64_LINTFLAGS
5933 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS
},
5935 #ifdef _CS_LFS_CFLAGS
5936 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS
},
5938 #ifdef _CS_LFS_LDFLAGS
5939 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS
},
5942 {"CS_LFS_LIBS", _CS_LFS_LIBS
},
5944 #ifdef _CS_LFS_LINTFLAGS
5945 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS
},
5948 {"CS_MACHINE", _CS_MACHINE
},
5951 {"CS_PATH", _CS_PATH
},
5954 {"CS_RELEASE", _CS_RELEASE
},
5956 #ifdef _CS_SRPC_DOMAIN
5957 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN
},
5960 {"CS_SYSNAME", _CS_SYSNAME
},
5963 {"CS_VERSION", _CS_VERSION
},
5965 #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
5966 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS
},
5968 #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
5969 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS
},
5971 #ifdef _CS_XBS5_ILP32_OFF32_LIBS
5972 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS
},
5974 #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
5975 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS
},
5977 #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
5978 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS
},
5980 #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
5981 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS
},
5983 #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
5984 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS
},
5986 #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
5987 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
},
5989 #ifdef _CS_XBS5_LP64_OFF64_CFLAGS
5990 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS
},
5992 #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
5993 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS
},
5995 #ifdef _CS_XBS5_LP64_OFF64_LIBS
5996 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS
},
5998 #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
5999 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS
},
6001 #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
6002 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS
},
6004 #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
6005 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
},
6007 #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
6008 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS
},
6010 #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
6011 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
},
6013 #ifdef _MIPS_CS_AVAIL_PROCESSORS
6014 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS
},
6016 #ifdef _MIPS_CS_BASE
6017 {"MIPS_CS_BASE", _MIPS_CS_BASE
},
6019 #ifdef _MIPS_CS_HOSTID
6020 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID
},
6022 #ifdef _MIPS_CS_HW_NAME
6023 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME
},
6025 #ifdef _MIPS_CS_NUM_PROCESSORS
6026 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS
},
6028 #ifdef _MIPS_CS_OSREL_MAJ
6029 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ
},
6031 #ifdef _MIPS_CS_OSREL_MIN
6032 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN
},
6034 #ifdef _MIPS_CS_OSREL_PATCH
6035 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH
},
6037 #ifdef _MIPS_CS_OS_NAME
6038 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME
},
6040 #ifdef _MIPS_CS_OS_PROVIDER
6041 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER
},
6043 #ifdef _MIPS_CS_PROCESSORS
6044 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS
},
6046 #ifdef _MIPS_CS_SERIAL
6047 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL
},
6049 #ifdef _MIPS_CS_VENDOR
6050 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR
},
6055 conv_confstr_confname(PyObject
*arg
, int *valuep
)
6057 return conv_confname(arg
, valuep
, posix_constants_confstr
,
6058 sizeof(posix_constants_confstr
)
6059 / sizeof(struct constdef
));
6062 PyDoc_STRVAR(posix_confstr__doc__
,
6063 "confstr(name) -> string\n\n\
6064 Return a string-valued system configuration variable.");
6067 posix_confstr(PyObject
*self
, PyObject
*args
)
6069 PyObject
*result
= NULL
;
6073 if (PyArg_ParseTuple(args
, "O&:confstr", conv_confstr_confname
, &name
)) {
6077 len
= confstr(name
, buffer
, sizeof(buffer
));
6088 if ((unsigned int)len
>= sizeof(buffer
)) {
6089 result
= PyString_FromStringAndSize(NULL
, len
-1);
6091 confstr(name
, PyString_AS_STRING(result
), len
);
6094 result
= PyString_FromStringAndSize(buffer
, len
-1);
6103 static struct constdef posix_constants_sysconf
[] = {
6104 #ifdef _SC_2_CHAR_TERM
6105 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM
},
6108 {"SC_2_C_BIND", _SC_2_C_BIND
},
6111 {"SC_2_C_DEV", _SC_2_C_DEV
},
6113 #ifdef _SC_2_C_VERSION
6114 {"SC_2_C_VERSION", _SC_2_C_VERSION
},
6116 #ifdef _SC_2_FORT_DEV
6117 {"SC_2_FORT_DEV", _SC_2_FORT_DEV
},
6119 #ifdef _SC_2_FORT_RUN
6120 {"SC_2_FORT_RUN", _SC_2_FORT_RUN
},
6122 #ifdef _SC_2_LOCALEDEF
6123 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF
},
6126 {"SC_2_SW_DEV", _SC_2_SW_DEV
},
6129 {"SC_2_UPE", _SC_2_UPE
},
6131 #ifdef _SC_2_VERSION
6132 {"SC_2_VERSION", _SC_2_VERSION
},
6134 #ifdef _SC_ABI_ASYNCHRONOUS_IO
6135 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO
},
6138 {"SC_ACL", _SC_ACL
},
6140 #ifdef _SC_AIO_LISTIO_MAX
6141 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX
},
6144 {"SC_AIO_MAX", _SC_AIO_MAX
},
6146 #ifdef _SC_AIO_PRIO_DELTA_MAX
6147 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX
},
6150 {"SC_ARG_MAX", _SC_ARG_MAX
},
6152 #ifdef _SC_ASYNCHRONOUS_IO
6153 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO
},
6155 #ifdef _SC_ATEXIT_MAX
6156 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX
},
6159 {"SC_AUDIT", _SC_AUDIT
},
6161 #ifdef _SC_AVPHYS_PAGES
6162 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES
},
6164 #ifdef _SC_BC_BASE_MAX
6165 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX
},
6167 #ifdef _SC_BC_DIM_MAX
6168 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX
},
6170 #ifdef _SC_BC_SCALE_MAX
6171 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX
},
6173 #ifdef _SC_BC_STRING_MAX
6174 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX
},
6177 {"SC_CAP", _SC_CAP
},
6179 #ifdef _SC_CHARCLASS_NAME_MAX
6180 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX
},
6183 {"SC_CHAR_BIT", _SC_CHAR_BIT
},
6186 {"SC_CHAR_MAX", _SC_CHAR_MAX
},
6189 {"SC_CHAR_MIN", _SC_CHAR_MIN
},
6191 #ifdef _SC_CHILD_MAX
6192 {"SC_CHILD_MAX", _SC_CHILD_MAX
},
6195 {"SC_CLK_TCK", _SC_CLK_TCK
},
6197 #ifdef _SC_COHER_BLKSZ
6198 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ
},
6200 #ifdef _SC_COLL_WEIGHTS_MAX
6201 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX
},
6203 #ifdef _SC_DCACHE_ASSOC
6204 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC
},
6206 #ifdef _SC_DCACHE_BLKSZ
6207 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ
},
6209 #ifdef _SC_DCACHE_LINESZ
6210 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ
},
6212 #ifdef _SC_DCACHE_SZ
6213 {"SC_DCACHE_SZ", _SC_DCACHE_SZ
},
6215 #ifdef _SC_DCACHE_TBLKSZ
6216 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ
},
6218 #ifdef _SC_DELAYTIMER_MAX
6219 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX
},
6221 #ifdef _SC_EQUIV_CLASS_MAX
6222 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX
},
6224 #ifdef _SC_EXPR_NEST_MAX
6225 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX
},
6228 {"SC_FSYNC", _SC_FSYNC
},
6230 #ifdef _SC_GETGR_R_SIZE_MAX
6231 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX
},
6233 #ifdef _SC_GETPW_R_SIZE_MAX
6234 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX
},
6236 #ifdef _SC_ICACHE_ASSOC
6237 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC
},
6239 #ifdef _SC_ICACHE_BLKSZ
6240 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ
},
6242 #ifdef _SC_ICACHE_LINESZ
6243 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ
},
6245 #ifdef _SC_ICACHE_SZ
6246 {"SC_ICACHE_SZ", _SC_ICACHE_SZ
},
6249 {"SC_INF", _SC_INF
},
6252 {"SC_INT_MAX", _SC_INT_MAX
},
6255 {"SC_INT_MIN", _SC_INT_MIN
},
6258 {"SC_IOV_MAX", _SC_IOV_MAX
},
6260 #ifdef _SC_IP_SECOPTS
6261 {"SC_IP_SECOPTS", _SC_IP_SECOPTS
},
6263 #ifdef _SC_JOB_CONTROL
6264 {"SC_JOB_CONTROL", _SC_JOB_CONTROL
},
6266 #ifdef _SC_KERN_POINTERS
6267 {"SC_KERN_POINTERS", _SC_KERN_POINTERS
},
6270 {"SC_KERN_SIM", _SC_KERN_SIM
},
6273 {"SC_LINE_MAX", _SC_LINE_MAX
},
6275 #ifdef _SC_LOGIN_NAME_MAX
6276 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX
},
6278 #ifdef _SC_LOGNAME_MAX
6279 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX
},
6282 {"SC_LONG_BIT", _SC_LONG_BIT
},
6285 {"SC_MAC", _SC_MAC
},
6287 #ifdef _SC_MAPPED_FILES
6288 {"SC_MAPPED_FILES", _SC_MAPPED_FILES
},
6291 {"SC_MAXPID", _SC_MAXPID
},
6293 #ifdef _SC_MB_LEN_MAX
6294 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX
},
6297 {"SC_MEMLOCK", _SC_MEMLOCK
},
6299 #ifdef _SC_MEMLOCK_RANGE
6300 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE
},
6302 #ifdef _SC_MEMORY_PROTECTION
6303 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION
},
6305 #ifdef _SC_MESSAGE_PASSING
6306 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING
},
6308 #ifdef _SC_MMAP_FIXED_ALIGNMENT
6309 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT
},
6311 #ifdef _SC_MQ_OPEN_MAX
6312 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX
},
6314 #ifdef _SC_MQ_PRIO_MAX
6315 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX
},
6317 #ifdef _SC_NACLS_MAX
6318 {"SC_NACLS_MAX", _SC_NACLS_MAX
},
6320 #ifdef _SC_NGROUPS_MAX
6321 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX
},
6323 #ifdef _SC_NL_ARGMAX
6324 {"SC_NL_ARGMAX", _SC_NL_ARGMAX
},
6326 #ifdef _SC_NL_LANGMAX
6327 {"SC_NL_LANGMAX", _SC_NL_LANGMAX
},
6329 #ifdef _SC_NL_MSGMAX
6330 {"SC_NL_MSGMAX", _SC_NL_MSGMAX
},
6333 {"SC_NL_NMAX", _SC_NL_NMAX
},
6335 #ifdef _SC_NL_SETMAX
6336 {"SC_NL_SETMAX", _SC_NL_SETMAX
},
6338 #ifdef _SC_NL_TEXTMAX
6339 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX
},
6341 #ifdef _SC_NPROCESSORS_CONF
6342 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF
},
6344 #ifdef _SC_NPROCESSORS_ONLN
6345 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN
},
6347 #ifdef _SC_NPROC_CONF
6348 {"SC_NPROC_CONF", _SC_NPROC_CONF
},
6350 #ifdef _SC_NPROC_ONLN
6351 {"SC_NPROC_ONLN", _SC_NPROC_ONLN
},
6354 {"SC_NZERO", _SC_NZERO
},
6357 {"SC_OPEN_MAX", _SC_OPEN_MAX
},
6360 {"SC_PAGESIZE", _SC_PAGESIZE
},
6362 #ifdef _SC_PAGE_SIZE
6363 {"SC_PAGE_SIZE", _SC_PAGE_SIZE
},
6366 {"SC_PASS_MAX", _SC_PASS_MAX
},
6368 #ifdef _SC_PHYS_PAGES
6369 {"SC_PHYS_PAGES", _SC_PHYS_PAGES
},
6372 {"SC_PII", _SC_PII
},
6374 #ifdef _SC_PII_INTERNET
6375 {"SC_PII_INTERNET", _SC_PII_INTERNET
},
6377 #ifdef _SC_PII_INTERNET_DGRAM
6378 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM
},
6380 #ifdef _SC_PII_INTERNET_STREAM
6381 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM
},
6384 {"SC_PII_OSI", _SC_PII_OSI
},
6386 #ifdef _SC_PII_OSI_CLTS
6387 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS
},
6389 #ifdef _SC_PII_OSI_COTS
6390 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS
},
6392 #ifdef _SC_PII_OSI_M
6393 {"SC_PII_OSI_M", _SC_PII_OSI_M
},
6395 #ifdef _SC_PII_SOCKET
6396 {"SC_PII_SOCKET", _SC_PII_SOCKET
},
6399 {"SC_PII_XTI", _SC_PII_XTI
},
6402 {"SC_POLL", _SC_POLL
},
6404 #ifdef _SC_PRIORITIZED_IO
6405 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO
},
6407 #ifdef _SC_PRIORITY_SCHEDULING
6408 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING
},
6410 #ifdef _SC_REALTIME_SIGNALS
6411 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS
},
6413 #ifdef _SC_RE_DUP_MAX
6414 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX
},
6416 #ifdef _SC_RTSIG_MAX
6417 {"SC_RTSIG_MAX", _SC_RTSIG_MAX
},
6419 #ifdef _SC_SAVED_IDS
6420 {"SC_SAVED_IDS", _SC_SAVED_IDS
},
6422 #ifdef _SC_SCHAR_MAX
6423 {"SC_SCHAR_MAX", _SC_SCHAR_MAX
},
6425 #ifdef _SC_SCHAR_MIN
6426 {"SC_SCHAR_MIN", _SC_SCHAR_MIN
},
6429 {"SC_SELECT", _SC_SELECT
},
6431 #ifdef _SC_SEMAPHORES
6432 {"SC_SEMAPHORES", _SC_SEMAPHORES
},
6434 #ifdef _SC_SEM_NSEMS_MAX
6435 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX
},
6437 #ifdef _SC_SEM_VALUE_MAX
6438 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX
},
6440 #ifdef _SC_SHARED_MEMORY_OBJECTS
6441 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS
},
6444 {"SC_SHRT_MAX", _SC_SHRT_MAX
},
6447 {"SC_SHRT_MIN", _SC_SHRT_MIN
},
6449 #ifdef _SC_SIGQUEUE_MAX
6450 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX
},
6452 #ifdef _SC_SIGRT_MAX
6453 {"SC_SIGRT_MAX", _SC_SIGRT_MAX
},
6455 #ifdef _SC_SIGRT_MIN
6456 {"SC_SIGRT_MIN", _SC_SIGRT_MIN
},
6458 #ifdef _SC_SOFTPOWER
6459 {"SC_SOFTPOWER", _SC_SOFTPOWER
},
6461 #ifdef _SC_SPLIT_CACHE
6462 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE
},
6464 #ifdef _SC_SSIZE_MAX
6465 {"SC_SSIZE_MAX", _SC_SSIZE_MAX
},
6467 #ifdef _SC_STACK_PROT
6468 {"SC_STACK_PROT", _SC_STACK_PROT
},
6470 #ifdef _SC_STREAM_MAX
6471 {"SC_STREAM_MAX", _SC_STREAM_MAX
},
6473 #ifdef _SC_SYNCHRONIZED_IO
6474 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO
},
6477 {"SC_THREADS", _SC_THREADS
},
6479 #ifdef _SC_THREAD_ATTR_STACKADDR
6480 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR
},
6482 #ifdef _SC_THREAD_ATTR_STACKSIZE
6483 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE
},
6485 #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
6486 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS
},
6488 #ifdef _SC_THREAD_KEYS_MAX
6489 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX
},
6491 #ifdef _SC_THREAD_PRIORITY_SCHEDULING
6492 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING
},
6494 #ifdef _SC_THREAD_PRIO_INHERIT
6495 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT
},
6497 #ifdef _SC_THREAD_PRIO_PROTECT
6498 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT
},
6500 #ifdef _SC_THREAD_PROCESS_SHARED
6501 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED
},
6503 #ifdef _SC_THREAD_SAFE_FUNCTIONS
6504 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS
},
6506 #ifdef _SC_THREAD_STACK_MIN
6507 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN
},
6509 #ifdef _SC_THREAD_THREADS_MAX
6510 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX
},
6513 {"SC_TIMERS", _SC_TIMERS
},
6515 #ifdef _SC_TIMER_MAX
6516 {"SC_TIMER_MAX", _SC_TIMER_MAX
},
6518 #ifdef _SC_TTY_NAME_MAX
6519 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX
},
6521 #ifdef _SC_TZNAME_MAX
6522 {"SC_TZNAME_MAX", _SC_TZNAME_MAX
},
6524 #ifdef _SC_T_IOV_MAX
6525 {"SC_T_IOV_MAX", _SC_T_IOV_MAX
},
6527 #ifdef _SC_UCHAR_MAX
6528 {"SC_UCHAR_MAX", _SC_UCHAR_MAX
},
6531 {"SC_UINT_MAX", _SC_UINT_MAX
},
6533 #ifdef _SC_UIO_MAXIOV
6534 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV
},
6536 #ifdef _SC_ULONG_MAX
6537 {"SC_ULONG_MAX", _SC_ULONG_MAX
},
6539 #ifdef _SC_USHRT_MAX
6540 {"SC_USHRT_MAX", _SC_USHRT_MAX
},
6543 {"SC_VERSION", _SC_VERSION
},
6546 {"SC_WORD_BIT", _SC_WORD_BIT
},
6548 #ifdef _SC_XBS5_ILP32_OFF32
6549 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32
},
6551 #ifdef _SC_XBS5_ILP32_OFFBIG
6552 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG
},
6554 #ifdef _SC_XBS5_LP64_OFF64
6555 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64
},
6557 #ifdef _SC_XBS5_LPBIG_OFFBIG
6558 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG
},
6560 #ifdef _SC_XOPEN_CRYPT
6561 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT
},
6563 #ifdef _SC_XOPEN_ENH_I18N
6564 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N
},
6566 #ifdef _SC_XOPEN_LEGACY
6567 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY
},
6569 #ifdef _SC_XOPEN_REALTIME
6570 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME
},
6572 #ifdef _SC_XOPEN_REALTIME_THREADS
6573 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS
},
6575 #ifdef _SC_XOPEN_SHM
6576 {"SC_XOPEN_SHM", _SC_XOPEN_SHM
},
6578 #ifdef _SC_XOPEN_UNIX
6579 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX
},
6581 #ifdef _SC_XOPEN_VERSION
6582 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION
},
6584 #ifdef _SC_XOPEN_XCU_VERSION
6585 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION
},
6587 #ifdef _SC_XOPEN_XPG2
6588 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2
},
6590 #ifdef _SC_XOPEN_XPG3
6591 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3
},
6593 #ifdef _SC_XOPEN_XPG4
6594 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4
},
6599 conv_sysconf_confname(PyObject
*arg
, int *valuep
)
6601 return conv_confname(arg
, valuep
, posix_constants_sysconf
,
6602 sizeof(posix_constants_sysconf
)
6603 / sizeof(struct constdef
));
6606 PyDoc_STRVAR(posix_sysconf__doc__
,
6607 "sysconf(name) -> integer\n\n\
6608 Return an integer-valued system configuration variable.");
6611 posix_sysconf(PyObject
*self
, PyObject
*args
)
6613 PyObject
*result
= NULL
;
6616 if (PyArg_ParseTuple(args
, "O&:sysconf", conv_sysconf_confname
, &name
)) {
6620 value
= sysconf(name
);
6621 if (value
== -1 && errno
!= 0)
6624 result
= PyInt_FromLong(value
);
6631 #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) || defined(HAVE_CONFSTR) || defined(HAVE_SYSCONF)
6632 /* This code is used to ensure that the tables of configuration value names
6633 * are in sorted order as required by conv_confname(), and also to build the
6634 * the exported dictionaries that are used to publish information about the
6635 * names available on the host platform.
6637 * Sorting the table at runtime ensures that the table is properly ordered
6638 * when used, even for platforms we're not able to test on. It also makes
6639 * it easier to add additional entries to the tables.
6643 cmp_constdefs(const void *v1
, const void *v2
)
6645 const struct constdef
*c1
=
6646 (const struct constdef
*) v1
;
6647 const struct constdef
*c2
=
6648 (const struct constdef
*) v2
;
6650 return strcmp(c1
->name
, c2
->name
);
6654 setup_confname_table(struct constdef
*table
, size_t tablesize
,
6655 char *tablename
, PyObject
*module
)
6660 qsort(table
, tablesize
, sizeof(struct constdef
), cmp_constdefs
);
6665 for (i
=0; i
< tablesize
; ++i
) {
6666 PyObject
*o
= PyInt_FromLong(table
[i
].value
);
6667 if (o
== NULL
|| PyDict_SetItemString(d
, table
[i
].name
, o
) == -1) {
6674 return PyModule_AddObject(module
, tablename
, d
);
6676 #endif /* HAVE_FPATHCONF || HAVE_PATHCONF || HAVE_CONFSTR || HAVE_SYSCONF */
6678 /* Return -1 on failure, 0 on success. */
6680 setup_confname_tables(PyObject
*module
)
6682 #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
6683 if (setup_confname_table(posix_constants_pathconf
,
6684 sizeof(posix_constants_pathconf
)
6685 / sizeof(struct constdef
),
6686 "pathconf_names", module
))
6690 if (setup_confname_table(posix_constants_confstr
,
6691 sizeof(posix_constants_confstr
)
6692 / sizeof(struct constdef
),
6693 "confstr_names", module
))
6697 if (setup_confname_table(posix_constants_sysconf
,
6698 sizeof(posix_constants_sysconf
)
6699 / sizeof(struct constdef
),
6700 "sysconf_names", module
))
6707 PyDoc_STRVAR(posix_abort__doc__
,
6708 "abort() -> does not return!\n\n\
6709 Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
6710 in the hardest way possible on the hosting operating system.");
6713 posix_abort(PyObject
*self
, PyObject
*noargs
)
6717 Py_FatalError("abort() called from Python code didn't abort!");
6721 #ifdef HAVE_SETRESUID
6722 PyDoc_STRVAR(posix_setresuid__doc__
,
6723 "setresuid(ruid, euid, suid)\n\n\
6724 Set the current process's real, effective, and saved user ids.");
6727 posix_setresuid (PyObject
*self
, PyObject
*args
)
6729 /* We assume uid_t is no larger than a long. */
6730 long ruid
, euid
, suid
;
6731 if (!PyArg_ParseTuple(args
, "lll", &ruid
, &euid
, &suid
))
6733 if (setresuid(ruid
, euid
, suid
) < 0)
6734 return posix_error();
6739 #ifdef HAVE_SETRESGID
6740 PyDoc_STRVAR(posix_setresgid__doc__
,
6741 "setresgid(rgid, egid, sgid)\n\n\
6742 Set the current process's real, effective, and saved group ids.");
6745 posix_setresgid (PyObject
*self
, PyObject
*args
)
6747 /* We assume uid_t is no larger than a long. */
6748 long rgid
, egid
, sgid
;
6749 if (!PyArg_ParseTuple(args
, "lll", &rgid
, &egid
, &sgid
))
6751 if (setresgid(rgid
, egid
, sgid
) < 0)
6752 return posix_error();
6757 #ifdef HAVE_GETRESUID
6758 PyDoc_STRVAR(posix_getresuid__doc__
,
6759 "getresuid() -> (ruid, euid, suid)\n\n\
6760 Get tuple of the current process's real, effective, and saved user ids.");
6763 posix_getresuid (PyObject
*self
, PyObject
*noargs
)
6765 uid_t ruid
, euid
, suid
;
6766 long l_ruid
, l_euid
, l_suid
;
6767 if (getresuid(&ruid
, &euid
, &suid
) < 0)
6768 return posix_error();
6769 /* Force the values into long's as we don't know the size of uid_t. */
6773 return Py_BuildValue("(lll)", l_ruid
, l_euid
, l_suid
);
6777 #ifdef HAVE_GETRESGID
6778 PyDoc_STRVAR(posix_getresgid__doc__
,
6779 "getresgid() -> (rgid, egid, sgid)\n\n\
6780 Get tuple of the current process's real, effective, and saved group ids.");
6783 posix_getresgid (PyObject
*self
, PyObject
*noargs
)
6785 uid_t rgid
, egid
, sgid
;
6786 long l_rgid
, l_egid
, l_sgid
;
6787 if (getresgid(&rgid
, &egid
, &sgid
) < 0)
6788 return posix_error();
6789 /* Force the values into long's as we don't know the size of uid_t. */
6793 return Py_BuildValue("(lll)", l_rgid
, l_egid
, l_sgid
);
6797 static PyMethodDef posix_methods
[] = {
6798 {"access", posix_access
, METH_VARARGS
, posix_access__doc__
},
6800 {"ttyname", posix_ttyname
, METH_VARARGS
, posix_ttyname__doc__
},
6802 {"chdir", posix_chdir
, METH_VARARGS
, posix_chdir__doc__
},
6804 {"chflags", posix_chflags
, METH_VARARGS
, posix_chflags__doc__
},
6805 #endif /* HAVE_CHFLAGS */
6806 {"chmod", posix_chmod
, METH_VARARGS
, posix_chmod__doc__
},
6808 {"fchmod", posix_fchmod
, METH_VARARGS
, posix_fchmod__doc__
},
6809 #endif /* HAVE_FCHMOD */
6811 {"chown", posix_chown
, METH_VARARGS
, posix_chown__doc__
},
6812 #endif /* HAVE_CHOWN */
6814 {"lchmod", posix_lchmod
, METH_VARARGS
, posix_lchmod__doc__
},
6815 #endif /* HAVE_LCHMOD */
6817 {"fchown", posix_fchown
, METH_VARARGS
, posix_fchown__doc__
},
6818 #endif /* HAVE_FCHOWN */
6819 #ifdef HAVE_LCHFLAGS
6820 {"lchflags", posix_lchflags
, METH_VARARGS
, posix_lchflags__doc__
},
6821 #endif /* HAVE_LCHFLAGS */
6823 {"lchown", posix_lchown
, METH_VARARGS
, posix_lchown__doc__
},
6824 #endif /* HAVE_LCHOWN */
6826 {"chroot", posix_chroot
, METH_VARARGS
, posix_chroot__doc__
},
6829 {"ctermid", posix_ctermid
, METH_NOARGS
, posix_ctermid__doc__
},
6832 {"getcwd", posix_getcwd
, METH_NOARGS
, posix_getcwd__doc__
},
6833 #ifdef Py_USING_UNICODE
6834 {"getcwdu", posix_getcwdu
, METH_NOARGS
, posix_getcwdu__doc__
},
6838 {"link", posix_link
, METH_VARARGS
, posix_link__doc__
},
6839 #endif /* HAVE_LINK */
6840 {"listdir", posix_listdir
, METH_VARARGS
, posix_listdir__doc__
},
6841 {"lstat", posix_lstat
, METH_VARARGS
, posix_lstat__doc__
},
6842 {"mkdir", posix_mkdir
, METH_VARARGS
, posix_mkdir__doc__
},
6844 {"nice", posix_nice
, METH_VARARGS
, posix_nice__doc__
},
6845 #endif /* HAVE_NICE */
6846 #ifdef HAVE_READLINK
6847 {"readlink", posix_readlink
, METH_VARARGS
, posix_readlink__doc__
},
6848 #endif /* HAVE_READLINK */
6849 {"rename", posix_rename
, METH_VARARGS
, posix_rename__doc__
},
6850 {"rmdir", posix_rmdir
, METH_VARARGS
, posix_rmdir__doc__
},
6851 {"stat", posix_stat
, METH_VARARGS
, posix_stat__doc__
},
6852 //{"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
6854 {"symlink", posix_symlink
, METH_VARARGS
, posix_symlink__doc__
},
6855 #endif /* HAVE_SYMLINK */
6857 {"system", posix_system
, METH_VARARGS
, posix_system__doc__
},
6859 {"umask", posix_umask
, METH_VARARGS
, posix_umask__doc__
},
6861 {"uname", posix_uname
, METH_NOARGS
, posix_uname__doc__
},
6862 #endif /* HAVE_UNAME */
6863 {"unlink", posix_unlink
, METH_VARARGS
, posix_unlink__doc__
},
6864 {"remove", posix_unlink
, METH_VARARGS
, posix_remove__doc__
},
6865 {"utime", posix_utime
, METH_VARARGS
, posix_utime__doc__
},
6867 {"times", posix_times
, METH_NOARGS
, posix_times__doc__
},
6868 #endif /* HAVE_TIMES */
6869 {"_exit", posix__exit
, METH_VARARGS
, posix__exit__doc__
},
6871 {"execv", posix_execv
, METH_VARARGS
, posix_execv__doc__
},
6872 {"execve", posix_execve
, METH_VARARGS
, posix_execve__doc__
},
6873 #endif /* HAVE_EXECV */
6875 {"spawnv", posix_spawnv
, METH_VARARGS
, posix_spawnv__doc__
},
6876 {"spawnve", posix_spawnve
, METH_VARARGS
, posix_spawnve__doc__
},
6877 #if defined(PYOS_OS2)
6878 {"spawnvp", posix_spawnvp
, METH_VARARGS
, posix_spawnvp__doc__
},
6879 {"spawnvpe", posix_spawnvpe
, METH_VARARGS
, posix_spawnvpe__doc__
},
6880 #endif /* PYOS_OS2 */
6881 #endif /* HAVE_SPAWNV */
6883 {"fork1", posix_fork1
, METH_NOARGS
, posix_fork1__doc__
},
6884 #endif /* HAVE_FORK1 */
6886 {"fork", posix_fork
, METH_NOARGS
, posix_fork__doc__
},
6887 #endif /* HAVE_FORK */
6888 #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
6889 {"openpty", posix_openpty
, METH_NOARGS
, posix_openpty__doc__
},
6890 #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
6892 {"forkpty", posix_forkpty
, METH_NOARGS
, posix_forkpty__doc__
},
6893 #endif /* HAVE_FORKPTY */
6895 {"getegid", posix_getegid
, METH_NOARGS
, posix_getegid__doc__
},
6896 #endif /* HAVE_GETEGID */
6898 {"geteuid", posix_geteuid
, METH_NOARGS
, posix_geteuid__doc__
},
6899 #endif /* HAVE_GETEUID */
6901 {"getgid", posix_getgid
, METH_NOARGS
, posix_getgid__doc__
},
6902 #endif /* HAVE_GETGID */
6903 #ifdef HAVE_GETGROUPS
6904 {"getgroups", posix_getgroups
, METH_NOARGS
, posix_getgroups__doc__
},
6906 {"getpid", posix_getpid
, METH_NOARGS
, posix_getpid__doc__
},
6908 {"getpgrp", posix_getpgrp
, METH_NOARGS
, posix_getpgrp__doc__
},
6909 #endif /* HAVE_GETPGRP */
6911 {"getppid", posix_getppid
, METH_NOARGS
, posix_getppid__doc__
},
6912 #endif /* HAVE_GETPPID */
6914 {"getuid", posix_getuid
, METH_NOARGS
, posix_getuid__doc__
},
6915 #endif /* HAVE_GETUID */
6916 #ifdef HAVE_GETLOGIN
6917 {"getlogin", posix_getlogin
, METH_NOARGS
, posix_getlogin__doc__
},
6920 {"kill", posix_kill
, METH_VARARGS
, posix_kill__doc__
},
6921 #endif /* HAVE_KILL */
6923 {"killpg", posix_killpg
, METH_VARARGS
, posix_killpg__doc__
},
6924 #endif /* HAVE_KILLPG */
6926 {"plock", posix_plock
, METH_VARARGS
, posix_plock__doc__
},
6927 #endif /* HAVE_PLOCK */
6929 {"popen", posix_popen
, METH_VARARGS
, posix_popen__doc__
},
6931 {"popen2", win32_popen2
, METH_VARARGS
},
6932 {"popen3", win32_popen3
, METH_VARARGS
},
6933 {"popen4", win32_popen4
, METH_VARARGS
},
6934 {"startfile", win32_startfile
, METH_VARARGS
, win32_startfile__doc__
},
6935 {"kill", win32_kill
, METH_VARARGS
, win32_kill__doc__
},
6937 #if defined(PYOS_OS2) && defined(PYCC_GCC)
6938 {"popen2", os2emx_popen2
, METH_VARARGS
},
6939 {"popen3", os2emx_popen3
, METH_VARARGS
},
6940 {"popen4", os2emx_popen4
, METH_VARARGS
},
6943 #endif /* HAVE_POPEN */
6945 {"setuid", posix_setuid
, METH_VARARGS
, posix_setuid__doc__
},
6946 #endif /* HAVE_SETUID */
6948 {"seteuid", posix_seteuid
, METH_VARARGS
, posix_seteuid__doc__
},
6949 #endif /* HAVE_SETEUID */
6951 {"setegid", posix_setegid
, METH_VARARGS
, posix_setegid__doc__
},
6952 #endif /* HAVE_SETEGID */
6953 #ifdef HAVE_SETREUID
6954 {"setreuid", posix_setreuid
, METH_VARARGS
, posix_setreuid__doc__
},
6955 #endif /* HAVE_SETREUID */
6956 #ifdef HAVE_SETREGID
6957 {"setregid", posix_setregid
, METH_VARARGS
, posix_setregid__doc__
},
6958 #endif /* HAVE_SETREGID */
6960 {"setgid", posix_setgid
, METH_VARARGS
, posix_setgid__doc__
},
6961 #endif /* HAVE_SETGID */
6962 #ifdef HAVE_SETGROUPS
6963 {"setgroups", posix_setgroups
, METH_O
, posix_setgroups__doc__
},
6964 #endif /* HAVE_SETGROUPS */
6965 #ifdef HAVE_INITGROUPS
6966 {"initgroups", posix_initgroups
, METH_VARARGS
, posix_initgroups__doc__
},
6967 #endif /* HAVE_INITGROUPS */
6969 {"getpgid", posix_getpgid
, METH_VARARGS
, posix_getpgid__doc__
},
6970 #endif /* HAVE_GETPGID */
6972 {"setpgrp", posix_setpgrp
, METH_NOARGS
, posix_setpgrp__doc__
},
6973 #endif /* HAVE_SETPGRP */
6975 {"wait", posix_wait
, METH_NOARGS
, posix_wait__doc__
},
6976 #endif /* HAVE_WAIT */
6978 {"wait3", posix_wait3
, METH_VARARGS
, posix_wait3__doc__
},
6979 #endif /* HAVE_WAIT3 */
6981 {"wait4", posix_wait4
, METH_VARARGS
, posix_wait4__doc__
},
6982 #endif /* HAVE_WAIT4 */
6983 #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
6984 {"waitpid", posix_waitpid
, METH_VARARGS
, posix_waitpid__doc__
},
6985 #endif /* HAVE_WAITPID */
6987 {"getsid", posix_getsid
, METH_VARARGS
, posix_getsid__doc__
},
6988 #endif /* HAVE_GETSID */
6990 {"setsid", posix_setsid
, METH_NOARGS
, posix_setsid__doc__
},
6991 #endif /* HAVE_SETSID */
6993 {"setpgid", posix_setpgid
, METH_VARARGS
, posix_setpgid__doc__
},
6994 #endif /* HAVE_SETPGID */
6995 #ifdef HAVE_TCGETPGRP
6996 {"tcgetpgrp", posix_tcgetpgrp
, METH_VARARGS
, posix_tcgetpgrp__doc__
},
6997 #endif /* HAVE_TCGETPGRP */
6998 #ifdef HAVE_TCSETPGRP
6999 {"tcsetpgrp", posix_tcsetpgrp
, METH_VARARGS
, posix_tcsetpgrp__doc__
},
7000 #endif /* HAVE_TCSETPGRP */
7001 {"open", posix_open
, METH_VARARGS
, posix_open__doc__
},
7002 {"close", posix_close
, METH_VARARGS
, posix_close__doc__
},
7003 {"closerange", posix_closerange
, METH_VARARGS
, posix_closerange__doc__
},
7004 {"dup", posix_dup
, METH_VARARGS
, posix_dup__doc__
},
7005 {"dup2", posix_dup2
, METH_VARARGS
, posix_dup2__doc__
},
7006 {"lseek", posix_lseek
, METH_VARARGS
, posix_lseek__doc__
},
7007 {"read", posix_read
, METH_VARARGS
, posix_read__doc__
},
7008 {"write", posix_write
, METH_VARARGS
, posix_write__doc__
},
7009 {"fstat", posix_fstat
, METH_VARARGS
, posix_fstat__doc__
},
7010 {"fdopen", posix_fdopen
, METH_VARARGS
, posix_fdopen__doc__
},
7011 {"isatty", posix_isatty
, METH_VARARGS
, posix_isatty__doc__
},
7013 {"pipe", posix_pipe
, METH_NOARGS
, posix_pipe__doc__
},
7016 {"mkfifo", posix_mkfifo
, METH_VARARGS
, posix_mkfifo__doc__
},
7018 #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
7019 {"mknod", posix_mknod
, METH_VARARGS
, posix_mknod__doc__
},
7021 #ifdef HAVE_DEVICE_MACROS
7022 {"major", posix_major
, METH_VARARGS
, posix_major__doc__
},
7023 {"minor", posix_minor
, METH_VARARGS
, posix_minor__doc__
},
7024 {"makedev", posix_makedev
, METH_VARARGS
, posix_makedev__doc__
},
7026 #ifdef HAVE_FTRUNCATE
7027 {"ftruncate", posix_ftruncate
, METH_VARARGS
, posix_ftruncate__doc__
},
7030 {"putenv", posix_putenv
, METH_VARARGS
, posix_putenv__doc__
},
7032 #ifdef HAVE_UNSETENV
7033 {"unsetenv", posix_unsetenv
, METH_VARARGS
, posix_unsetenv__doc__
},
7035 {"strerror", posix_strerror
, METH_VARARGS
, posix_strerror__doc__
},
7037 {"fchdir", posix_fchdir
, METH_O
, posix_fchdir__doc__
},
7040 {"fsync", posix_fsync
, METH_O
, posix_fsync__doc__
},
7042 #ifdef HAVE_FDATASYNC
7043 {"fdatasync", posix_fdatasync
, METH_O
, posix_fdatasync__doc__
},
7045 #ifdef HAVE_SYS_WAIT_H
7047 {"WCOREDUMP", posix_WCOREDUMP
, METH_VARARGS
, posix_WCOREDUMP__doc__
},
7048 #endif /* WCOREDUMP */
7050 {"WIFCONTINUED",posix_WIFCONTINUED
, METH_VARARGS
, posix_WIFCONTINUED__doc__
},
7051 #endif /* WIFCONTINUED */
7053 {"WIFSTOPPED", posix_WIFSTOPPED
, METH_VARARGS
, posix_WIFSTOPPED__doc__
},
7054 #endif /* WIFSTOPPED */
7056 {"WIFSIGNALED", posix_WIFSIGNALED
, METH_VARARGS
, posix_WIFSIGNALED__doc__
},
7057 #endif /* WIFSIGNALED */
7059 {"WIFEXITED", posix_WIFEXITED
, METH_VARARGS
, posix_WIFEXITED__doc__
},
7060 #endif /* WIFEXITED */
7062 {"WEXITSTATUS", posix_WEXITSTATUS
, METH_VARARGS
, posix_WEXITSTATUS__doc__
},
7063 #endif /* WEXITSTATUS */
7065 {"WTERMSIG", posix_WTERMSIG
, METH_VARARGS
, posix_WTERMSIG__doc__
},
7066 #endif /* WTERMSIG */
7068 {"WSTOPSIG", posix_WSTOPSIG
, METH_VARARGS
, posix_WSTOPSIG__doc__
},
7069 #endif /* WSTOPSIG */
7070 #endif /* HAVE_SYS_WAIT_H */
7071 #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
7072 {"fstatvfs", posix_fstatvfs
, METH_VARARGS
, posix_fstatvfs__doc__
},
7074 #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
7075 {"statvfs", posix_statvfs
, METH_VARARGS
, posix_statvfs__doc__
},
7078 {"tmpfile", posix_tmpfile
, METH_NOARGS
, posix_tmpfile__doc__
},
7081 {"tempnam", posix_tempnam
, METH_VARARGS
, posix_tempnam__doc__
},
7084 {"tmpnam", posix_tmpnam
, METH_NOARGS
, posix_tmpnam__doc__
},
7087 {"confstr", posix_confstr
, METH_VARARGS
, posix_confstr__doc__
},
7090 {"sysconf", posix_sysconf
, METH_VARARGS
, posix_sysconf__doc__
},
7092 #ifdef HAVE_FPATHCONF
7093 {"fpathconf", posix_fpathconf
, METH_VARARGS
, posix_fpathconf__doc__
},
7095 #ifdef HAVE_PATHCONF
7096 {"pathconf", posix_pathconf
, METH_VARARGS
, posix_pathconf__doc__
},
7098 {"abort", posix_abort
, METH_NOARGS
, posix_abort__doc__
},
7099 #ifdef HAVE_SETRESUID
7100 {"setresuid", posix_setresuid
, METH_VARARGS
, posix_setresuid__doc__
},
7102 #ifdef HAVE_SETRESGID
7103 {"setresgid", posix_setresgid
, METH_VARARGS
, posix_setresgid__doc__
},
7105 #ifdef HAVE_GETRESUID
7106 {"getresuid", posix_getresuid
, METH_NOARGS
, posix_getresuid__doc__
},
7108 #ifdef HAVE_GETRESGID
7109 {"getresgid", posix_getresgid
, METH_NOARGS
, posix_getresgid__doc__
},
7112 {NULL
, NULL
} /* Sentinel */
7117 ins(PyObject
*module
, char *symbol
, long value
)
7119 return PyModule_AddIntConstant(module
, symbol
, value
);
7123 all_ins(PyObject
*d
)
7126 if (ins(d
, "F_OK", (long)F_OK
)) return -1;
7129 if (ins(d
, "R_OK", (long)R_OK
)) return -1;
7132 if (ins(d
, "W_OK", (long)W_OK
)) return -1;
7135 if (ins(d
, "X_OK", (long)X_OK
)) return -1;
7138 if (ins(d
, "NGROUPS_MAX", (long)NGROUPS_MAX
)) return -1;
7141 if (ins(d
, "TMP_MAX", (long)TMP_MAX
)) return -1;
7144 if (ins(d
, "WCONTINUED", (long)WCONTINUED
)) return -1;
7147 if (ins(d
, "WNOHANG", (long)WNOHANG
)) return -1;
7150 if (ins(d
, "WUNTRACED", (long)WUNTRACED
)) return -1;
7153 if (ins(d
, "O_RDONLY", (long)O_RDONLY
)) return -1;
7156 if (ins(d
, "O_WRONLY", (long)O_WRONLY
)) return -1;
7159 if (ins(d
, "O_RDWR", (long)O_RDWR
)) return -1;
7162 if (ins(d
, "O_NDELAY", (long)O_NDELAY
)) return -1;
7165 if (ins(d
, "O_NONBLOCK", (long)O_NONBLOCK
)) return -1;
7168 if (ins(d
, "O_APPEND", (long)O_APPEND
)) return -1;
7171 if (ins(d
, "O_DSYNC", (long)O_DSYNC
)) return -1;
7174 if (ins(d
, "O_RSYNC", (long)O_RSYNC
)) return -1;
7177 if (ins(d
, "O_SYNC", (long)O_SYNC
)) return -1;
7180 if (ins(d
, "O_NOCTTY", (long)O_NOCTTY
)) return -1;
7183 if (ins(d
, "O_CREAT", (long)O_CREAT
)) return -1;
7186 if (ins(d
, "O_EXCL", (long)O_EXCL
)) return -1;
7189 if (ins(d
, "O_TRUNC", (long)O_TRUNC
)) return -1;
7192 if (ins(d
, "O_BINARY", (long)O_BINARY
)) return -1;
7195 if (ins(d
, "O_TEXT", (long)O_TEXT
)) return -1;
7198 if (ins(d
, "O_LARGEFILE", (long)O_LARGEFILE
)) return -1;
7201 if (ins(d
, "O_SHLOCK", (long)O_SHLOCK
)) return -1;
7204 if (ins(d
, "O_EXLOCK", (long)O_EXLOCK
)) return -1;
7209 /* Don't inherit in child processes. */
7210 if (ins(d
, "O_NOINHERIT", (long)O_NOINHERIT
)) return -1;
7212 #ifdef _O_SHORT_LIVED
7213 /* Optimize for short life (keep in memory). */
7214 /* MS forgot to define this one with a non-underscore form too. */
7215 if (ins(d
, "O_SHORT_LIVED", (long)_O_SHORT_LIVED
)) return -1;
7218 /* Automatically delete when last handle is closed. */
7219 if (ins(d
, "O_TEMPORARY", (long)O_TEMPORARY
)) return -1;
7222 /* Optimize for random access. */
7223 if (ins(d
, "O_RANDOM", (long)O_RANDOM
)) return -1;
7226 /* Optimize for sequential access. */
7227 if (ins(d
, "O_SEQUENTIAL", (long)O_SEQUENTIAL
)) return -1;
7230 /* GNU extensions. */
7232 /* Send a SIGIO signal whenever input or output
7233 becomes available on file descriptor */
7234 if (ins(d
, "O_ASYNC", (long)O_ASYNC
)) return -1;
7237 /* Direct disk access. */
7238 if (ins(d
, "O_DIRECT", (long)O_DIRECT
)) return -1;
7241 /* Must be a directory. */
7242 if (ins(d
, "O_DIRECTORY", (long)O_DIRECTORY
)) return -1;
7245 /* Do not follow links. */
7246 if (ins(d
, "O_NOFOLLOW", (long)O_NOFOLLOW
)) return -1;
7249 /* Do not update the access time. */
7250 if (ins(d
, "O_NOATIME", (long)O_NOATIME
)) return -1;
7253 /* These come from sysexits.h */
7255 if (ins(d
, "EX_OK", (long)EX_OK
)) return -1;
7258 if (ins(d
, "EX_USAGE", (long)EX_USAGE
)) return -1;
7259 #endif /* EX_USAGE */
7261 if (ins(d
, "EX_DATAERR", (long)EX_DATAERR
)) return -1;
7262 #endif /* EX_DATAERR */
7264 if (ins(d
, "EX_NOINPUT", (long)EX_NOINPUT
)) return -1;
7265 #endif /* EX_NOINPUT */
7267 if (ins(d
, "EX_NOUSER", (long)EX_NOUSER
)) return -1;
7268 #endif /* EX_NOUSER */
7270 if (ins(d
, "EX_NOHOST", (long)EX_NOHOST
)) return -1;
7271 #endif /* EX_NOHOST */
7272 #ifdef EX_UNAVAILABLE
7273 if (ins(d
, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE
)) return -1;
7274 #endif /* EX_UNAVAILABLE */
7276 if (ins(d
, "EX_SOFTWARE", (long)EX_SOFTWARE
)) return -1;
7277 #endif /* EX_SOFTWARE */
7279 if (ins(d
, "EX_OSERR", (long)EX_OSERR
)) return -1;
7280 #endif /* EX_OSERR */
7282 if (ins(d
, "EX_OSFILE", (long)EX_OSFILE
)) return -1;
7283 #endif /* EX_OSFILE */
7285 if (ins(d
, "EX_CANTCREAT", (long)EX_CANTCREAT
)) return -1;
7286 #endif /* EX_CANTCREAT */
7288 if (ins(d
, "EX_IOERR", (long)EX_IOERR
)) return -1;
7289 #endif /* EX_IOERR */
7291 if (ins(d
, "EX_TEMPFAIL", (long)EX_TEMPFAIL
)) return -1;
7292 #endif /* EX_TEMPFAIL */
7294 if (ins(d
, "EX_PROTOCOL", (long)EX_PROTOCOL
)) return -1;
7295 #endif /* EX_PROTOCOL */
7297 if (ins(d
, "EX_NOPERM", (long)EX_NOPERM
)) return -1;
7298 #endif /* EX_NOPERM */
7300 if (ins(d
, "EX_CONFIG", (long)EX_CONFIG
)) return -1;
7301 #endif /* EX_CONFIG */
7303 if (ins(d
, "EX_NOTFOUND", (long)EX_NOTFOUND
)) return -1;
7304 #endif /* EX_NOTFOUND */
7307 #if defined(PYOS_OS2) && defined(PYCC_GCC)
7308 if (ins(d
, "P_WAIT", (long)P_WAIT
)) return -1;
7309 if (ins(d
, "P_NOWAIT", (long)P_NOWAIT
)) return -1;
7310 if (ins(d
, "P_OVERLAY", (long)P_OVERLAY
)) return -1;
7311 if (ins(d
, "P_DEBUG", (long)P_DEBUG
)) return -1;
7312 if (ins(d
, "P_SESSION", (long)P_SESSION
)) return -1;
7313 if (ins(d
, "P_DETACH", (long)P_DETACH
)) return -1;
7314 if (ins(d
, "P_PM", (long)P_PM
)) return -1;
7315 if (ins(d
, "P_DEFAULT", (long)P_DEFAULT
)) return -1;
7316 if (ins(d
, "P_MINIMIZE", (long)P_MINIMIZE
)) return -1;
7317 if (ins(d
, "P_MAXIMIZE", (long)P_MAXIMIZE
)) return -1;
7318 if (ins(d
, "P_FULLSCREEN", (long)P_FULLSCREEN
)) return -1;
7319 if (ins(d
, "P_WINDOWED", (long)P_WINDOWED
)) return -1;
7320 if (ins(d
, "P_FOREGROUND", (long)P_FOREGROUND
)) return -1;
7321 if (ins(d
, "P_BACKGROUND", (long)P_BACKGROUND
)) return -1;
7322 if (ins(d
, "P_NOCLOSE", (long)P_NOCLOSE
)) return -1;
7323 if (ins(d
, "P_NOSESSION", (long)P_NOSESSION
)) return -1;
7324 if (ins(d
, "P_QUOTE", (long)P_QUOTE
)) return -1;
7325 if (ins(d
, "P_TILDE", (long)P_TILDE
)) return -1;
7326 if (ins(d
, "P_UNRELATED", (long)P_UNRELATED
)) return -1;
7327 if (ins(d
, "P_DEBUGDESC", (long)P_DEBUGDESC
)) return -1;
7329 if (ins(d
, "P_WAIT", (long)_P_WAIT
)) return -1;
7330 if (ins(d
, "P_NOWAIT", (long)_P_NOWAIT
)) return -1;
7331 if (ins(d
, "P_OVERLAY", (long)_OLD_P_OVERLAY
)) return -1;
7332 if (ins(d
, "P_NOWAITO", (long)_P_NOWAITO
)) return -1;
7333 if (ins(d
, "P_DETACH", (long)_P_DETACH
)) return -1;
7339 #define INITFUNC initedk2
7340 #define MODNAME "edk2"
7347 #ifndef UEFI_C_SOURCE
7351 m
= Py_InitModule3(MODNAME
,
7357 #ifndef UEFI_C_SOURCE
7358 /* Initialize environ dictionary */
7359 v
= convertenviron();
7361 if (v
== NULL
|| PyModule_AddObject(m
, "environ", v
) != 0)
7364 #endif /* UEFI_C_SOURCE */
7369 if (setup_confname_tables(m
))
7372 Py_INCREF(PyExc_OSError
);
7373 PyModule_AddObject(m
, "error", PyExc_OSError
);
7376 if (posix_putenv_garbage
== NULL
)
7377 posix_putenv_garbage
= PyDict_New();
7381 stat_result_desc
.name
= MODNAME
".stat_result";
7382 stat_result_desc
.fields
[2].name
= PyStructSequence_UnnamedField
;
7383 stat_result_desc
.fields
[3].name
= PyStructSequence_UnnamedField
;
7384 stat_result_desc
.fields
[4].name
= PyStructSequence_UnnamedField
;
7385 PyStructSequence_InitType(&StatResultType
, &stat_result_desc
);
7386 structseq_new
= StatResultType
.tp_new
;
7387 StatResultType
.tp_new
= statresult_new
;
7389 //statvfs_result_desc.name = MODNAME ".statvfs_result";
7390 //PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
7391 #ifdef NEED_TICKS_PER_SECOND
7392 # if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
7393 ticks_per_second
= sysconf(_SC_CLK_TCK
);
7395 ticks_per_second
= HZ
;
7397 ticks_per_second
= 60; /* magic fallback value; may be bogus */
7401 Py_INCREF((PyObject
*) &StatResultType
);
7402 PyModule_AddObject(m
, "stat_result", (PyObject
*) &StatResultType
);
7403 //Py_INCREF((PyObject*) &StatVFSResultType);
7404 //PyModule_AddObject(m, "statvfs_result",
7405 // (PyObject*) &StatVFSResultType);