2 OS-specific module implementation for EDK II and UEFI.
3 Derived from posixmodule.c in Python 2.7.2.
5 Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials are licensed and made available under
7 the terms and conditions of the BSD License that accompanies this distribution.
8 The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14 #define PY_SSIZE_T_CLEAN
17 #include "structseq.h"
22 #include <sys/syslimits.h>
28 PyDoc_STRVAR(posix__doc__
,
29 "This module provides access to operating system functionality that is\n\
30 standardized by the C Standard and the POSIX standard (a thinly\n\
31 disguised Unix interface). Refer to the library manual and\n\
32 corresponding Unix manual entries for more information on calls.");
34 #ifndef Py_USING_UNICODE
35 /* This is used in signatures of functions. */
36 #define Py_UNICODE void
39 #ifdef HAVE_SYS_TYPES_H
40 #include <sys/types.h>
41 #endif /* HAVE_SYS_TYPES_H */
43 #ifdef HAVE_SYS_STAT_H
45 #endif /* HAVE_SYS_STAT_H */
47 #ifdef HAVE_SYS_WAIT_H
48 #include <sys/wait.h> /* For WNOHANG */
57 #endif /* HAVE_FCNTL_H */
63 #ifdef HAVE_SYSEXITS_H
65 #endif /* HAVE_SYSEXITS_H */
67 #ifdef HAVE_SYS_LOADAVG_H
68 #include <sys/loadavg.h>
73 #endif /* HAVE_UTIME_H */
75 #ifdef HAVE_SYS_UTIME_H
76 #include <sys/utime.h>
77 #define HAVE_UTIME_H /* pretend we do for the rest of this file */
78 #endif /* HAVE_SYS_UTIME_H */
80 #ifdef HAVE_SYS_TIMES_H
81 #include <sys/times.h>
82 #endif /* HAVE_SYS_TIMES_H */
84 #ifdef HAVE_SYS_PARAM_H
85 #include <sys/param.h>
86 #endif /* HAVE_SYS_PARAM_H */
88 #ifdef HAVE_SYS_UTSNAME_H
89 #include <sys/utsname.h>
90 #endif /* HAVE_SYS_UTSNAME_H */
94 #define NAMLEN(dirent) wcslen((dirent)->FileName)
97 #define NAMLEN(dirent) (dirent)->d_namlen
98 #ifdef HAVE_SYS_NDIR_H
101 #ifdef HAVE_SYS_DIR_H
110 #if defined(PATH_MAX) && PATH_MAX > 1024
111 #define MAXPATHLEN PATH_MAX
113 #define MAXPATHLEN 1024
115 #endif /* MAXPATHLEN */
117 #define WAIT_TYPE int
118 #define WAIT_STATUS_INT(s) (s)
120 /* Issue #1983: pid_t can be longer than a C long on some systems */
121 #if !defined(SIZEOF_PID_T) || SIZEOF_PID_T == SIZEOF_INT
122 #define PARSE_PID "i"
123 #define PyLong_FromPid PyInt_FromLong
124 #define PyLong_AsPid PyInt_AsLong
125 #elif SIZEOF_PID_T == SIZEOF_LONG
126 #define PARSE_PID "l"
127 #define PyLong_FromPid PyInt_FromLong
128 #define PyLong_AsPid PyInt_AsLong
129 #elif defined(SIZEOF_LONG_LONG) && SIZEOF_PID_T == SIZEOF_LONG_LONG
130 #define PARSE_PID "L"
131 #define PyLong_FromPid PyLong_FromLongLong
132 #define PyLong_AsPid PyInt_AsLongLong
134 #error "sizeof(pid_t) is neither sizeof(int), sizeof(long) or sizeof(long long)"
135 #endif /* SIZEOF_PID_T */
137 /* Don't use the "_r" form if we don't need it (also, won't have a
138 prototype for it, at least on Solaris -- maybe others as well?). */
139 #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
140 #define USE_CTERMID_R
143 #if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD)
147 /* choose the appropriate stat and fstat functions and return structs */
153 #define STRUCT_STAT struct stat
155 /* dummy version. _PyVerify_fd() is already defined in fileobject.h */
156 #define _PyVerify_fd_dup2(A, B) (1)
159 /* Return a dictionary corresponding to the POSIX environment table */
160 extern char **environ
;
172 /* This part ignores errors */
173 for (e
= environ
; *e
!= NULL
; e
++) {
176 char *p
= strchr(*e
, '=');
179 k
= PyString_FromStringAndSize(*e
, (int)(p
-*e
));
184 v
= PyString_FromString(p
+1);
190 if (PyDict_GetItem(d
, k
) == NULL
) {
191 if (PyDict_SetItem(d
, k
, v
) != 0)
199 #endif /* UEFI_ENV */
201 /* Set a POSIX-specific error from errno, and return NULL */
206 return PyErr_SetFromErrno(PyExc_OSError
);
209 posix_error_with_filename(char* name
)
211 return PyErr_SetFromErrnoWithFilename(PyExc_OSError
, name
);
216 posix_error_with_allocated_filename(char* name
)
218 PyObject
*rc
= PyErr_SetFromErrnoWithFilename(PyExc_OSError
, name
);
223 /* POSIX generic methods */
226 posix_fildes(PyObject
*fdobj
, int (*func
)(int))
230 fd
= PyObject_AsFileDescriptor(fdobj
);
233 if (!_PyVerify_fd(fd
))
234 return posix_error();
235 Py_BEGIN_ALLOW_THREADS
239 return posix_error();
245 posix_1str(PyObject
*args
, char *format
, int (*func
)(const char*))
249 if (!PyArg_ParseTuple(args
, format
,
250 Py_FileSystemDefaultEncoding
, &path1
))
252 Py_BEGIN_ALLOW_THREADS
253 res
= (*func
)(path1
);
256 return posix_error_with_allocated_filename(path1
);
263 posix_2str(PyObject
*args
,
265 int (*func
)(const char *, const char *))
267 char *path1
= NULL
, *path2
= NULL
;
269 if (!PyArg_ParseTuple(args
, format
,
270 Py_FileSystemDefaultEncoding
, &path1
,
271 Py_FileSystemDefaultEncoding
, &path2
))
273 Py_BEGIN_ALLOW_THREADS
274 res
= (*func
)(path1
, path2
);
279 /* XXX how to report both path1 and path2??? */
280 return posix_error();
285 PyDoc_STRVAR(stat_result__doc__
,
286 "stat_result: Result from stat or lstat.\n\n\
287 This object may be accessed either as a tuple of\n\
288 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
289 or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
291 Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
292 or st_flags, they are available as attributes only.\n\
294 See os.stat for more information.");
296 static PyStructSequence_Field stat_result_fields
[] = {
297 {"st_mode", "protection bits"},
298 //{"st_ino", "inode"},
299 //{"st_dev", "device"},
300 //{"st_nlink", "number of hard links"},
301 //{"st_uid", "user ID of owner"},
302 //{"st_gid", "group ID of owner"},
303 {"st_size", "total size, in bytes"},
304 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
305 {NULL
, "integer time of last access"},
306 {NULL
, "integer time of last modification"},
307 {NULL
, "integer time of last change"},
308 {"st_atime", "time of last access"},
309 {"st_mtime", "time of last modification"},
310 {"st_ctime", "time of last change"},
311 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
312 {"st_blksize", "blocksize for filesystem I/O"},
314 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
315 {"st_blocks", "number of blocks allocated"},
317 #ifdef HAVE_STRUCT_STAT_ST_RDEV
318 {"st_rdev", "device type (if inode device)"},
320 #ifdef HAVE_STRUCT_STAT_ST_FLAGS
321 {"st_flags", "user defined flags for file"},
323 #ifdef HAVE_STRUCT_STAT_ST_GEN
324 {"st_gen", "generation number"},
326 #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
327 {"st_birthtime", "time of creation"},
332 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
333 #define ST_BLKSIZE_IDX 8
335 #define ST_BLKSIZE_IDX 12
338 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
339 #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
341 #define ST_BLOCKS_IDX ST_BLKSIZE_IDX
344 #ifdef HAVE_STRUCT_STAT_ST_RDEV
345 #define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
347 #define ST_RDEV_IDX ST_BLOCKS_IDX
350 #ifdef HAVE_STRUCT_STAT_ST_FLAGS
351 #define ST_FLAGS_IDX (ST_RDEV_IDX+1)
353 #define ST_FLAGS_IDX ST_RDEV_IDX
356 #ifdef HAVE_STRUCT_STAT_ST_GEN
357 #define ST_GEN_IDX (ST_FLAGS_IDX+1)
359 #define ST_GEN_IDX ST_FLAGS_IDX
362 #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
363 #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
365 #define ST_BIRTHTIME_IDX ST_GEN_IDX
368 static PyStructSequence_Desc stat_result_desc
= {
369 "stat_result", /* name */
370 stat_result__doc__
, /* doc */
375 #ifndef UEFI_ENV /* Not in UEFI */
376 PyDoc_STRVAR(statvfs_result__doc__
,
377 "statvfs_result: Result from statvfs or fstatvfs.\n\n\
378 This object may be accessed either as a tuple of\n\
379 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
380 or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
382 See os.statvfs for more information.");
384 static PyStructSequence_Field statvfs_result_fields
[] = {
398 static PyStructSequence_Desc statvfs_result_desc
= {
399 "statvfs_result", /* name */
400 statvfs_result__doc__
, /* doc */
401 statvfs_result_fields
,
406 static int initialized
;
407 static PyTypeObject StatResultType
;
408 static PyTypeObject StatVFSResultType
;
409 static newfunc structseq_new
;
412 statresult_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
414 PyStructSequence
*result
;
417 result
= (PyStructSequence
*)structseq_new(type
, args
, kwds
);
420 /* If we have been initialized from a tuple,
421 st_?time might be set to None. Initialize it
422 from the int slots. */
423 for (i
= 7; i
<= 9; i
++) {
424 if (result
->ob_item
[i
+3] == Py_None
) {
426 Py_INCREF(result
->ob_item
[i
]);
427 result
->ob_item
[i
+3] = result
->ob_item
[i
];
430 return (PyObject
*)result
;
435 /* If true, st_?time is float. */
436 #if defined(UEFI_ENV)
437 static int _stat_float_times
= 0;
439 static int _stat_float_times
= 1;
442 PyDoc_STRVAR(stat_float_times__doc__
,
443 "stat_float_times([newval]) -> oldval\n\n\
444 Determine whether os.[lf]stat represents time stamps as float objects.\n\
445 If newval is True, future calls to stat() return floats, if it is False,\n\
446 future calls return ints. \n\
447 If newval is omitted, return the current setting.\n");
450 stat_float_times(PyObject
* self
, PyObject
*args
)
454 if (!PyArg_ParseTuple(args
, "|i:stat_float_times", &newval
))
457 /* Return old value */
458 return PyBool_FromLong(_stat_float_times
);
459 _stat_float_times
= newval
;
465 fill_time(PyObject
*v
, int index
, time_t sec
, unsigned long nsec
)
467 PyObject
*fval
,*ival
;
468 #if SIZEOF_TIME_T > SIZEOF_LONG
469 ival
= PyLong_FromLongLong((PY_LONG_LONG
)sec
);
471 ival
= PyInt_FromLong((long)sec
);
475 if (_stat_float_times
) {
476 fval
= PyFloat_FromDouble(sec
+ 1e-9*nsec
);
481 PyStructSequence_SET_ITEM(v
, index
, ival
);
482 PyStructSequence_SET_ITEM(v
, index
+3, fval
);
485 /* pack a system stat C structure into the Python stat tuple
486 (used by posix_stat() and posix_fstat()) */
488 _pystat_fromstructstat(STRUCT_STAT
*st
)
490 unsigned long ansec
, mnsec
, cnsec
;
491 PyObject
*v
= PyStructSequence_New(&StatResultType
);
495 PyStructSequence_SET_ITEM(v
, 0, PyInt_FromLong((long)st
->st_mode
));
496 PyStructSequence_SET_ITEM(v
, 1,
497 PyLong_FromLongLong((PY_LONG_LONG
)st
->st_size
));
499 ansec
= mnsec
= cnsec
= 0;
500 /* The index used by fill_time is the index of the integer time.
501 fill_time will add 3 to the index to get the floating time index.
503 fill_time(v
, 2, st
->st_atime
, ansec
);
504 fill_time(v
, 3, st
->st_mtime
, mnsec
);
505 fill_time(v
, 4, st
->st_mtime
, cnsec
);
507 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
508 PyStructSequence_SET_ITEM(v
, ST_BLKSIZE_IDX
,
509 PyInt_FromLong((long)st
->st_blksize
));
511 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
512 PyStructSequence_SET_ITEM(v
, ST_BLOCKS_IDX
,
513 PyInt_FromLong((long)st
->st_blocks
));
515 #ifdef HAVE_STRUCT_STAT_ST_RDEV
516 PyStructSequence_SET_ITEM(v
, ST_RDEV_IDX
,
517 PyInt_FromLong((long)st
->st_rdev
));
519 #ifdef HAVE_STRUCT_STAT_ST_GEN
520 PyStructSequence_SET_ITEM(v
, ST_GEN_IDX
,
521 PyInt_FromLong((long)st
->st_gen
));
523 #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
526 unsigned long bsec
,bnsec
;
527 bsec
= (long)st
->st_birthtime
;
528 #ifdef HAVE_STAT_TV_NSEC2
529 bnsec
= st
->st_birthtimespec
.tv_nsec
;
533 if (_stat_float_times
) {
534 val
= PyFloat_FromDouble(bsec
+ 1e-9*bnsec
);
536 val
= PyInt_FromLong((long)bsec
);
538 PyStructSequence_SET_ITEM(v
, ST_BIRTHTIME_IDX
,
542 #ifdef HAVE_STRUCT_STAT_ST_FLAGS
543 PyStructSequence_SET_ITEM(v
, ST_FLAGS_IDX
,
544 PyInt_FromLong((long)st
->st_flags
));
547 if (PyErr_Occurred()) {
556 posix_do_stat(PyObject
*self
, PyObject
*args
,
558 int (*statfunc
)(const char *, STRUCT_STAT
*),
560 int (*wstatfunc
)(const Py_UNICODE
*, STRUCT_STAT
*))
563 char *path
= NULL
; /* pass this to stat; do not free() it */
564 char *pathfree
= NULL
; /* this memory must be free'd */
568 if (!PyArg_ParseTuple(args
, format
,
569 Py_FileSystemDefaultEncoding
, &path
))
573 Py_BEGIN_ALLOW_THREADS
574 res
= (*statfunc
)(path
, &st
);
578 result
= posix_error_with_filename(pathfree
);
581 result
= _pystat_fromstructstat(&st
);
583 PyMem_Free(pathfree
);
589 PyDoc_STRVAR(posix_access__doc__
,
590 "access(path, mode) -> True if granted, False otherwise\n\n\
591 Use the real uid/gid to test for access to a path. Note that most\n\
592 operations will use the effective uid/gid, therefore this routine can\n\
593 be used in a suid/sgid environment to test if the invoking user has the\n\
594 specified access to the path. The mode argument can be F_OK to test\n\
595 existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
598 posix_access(PyObject
*self
, PyObject
*args
)
604 if (!PyArg_ParseTuple(args
, "eti:access",
605 Py_FileSystemDefaultEncoding
, &path
, &mode
))
607 Py_BEGIN_ALLOW_THREADS
608 res
= access(path
, mode
);
611 return PyBool_FromLong(res
== 0);
627 PyDoc_STRVAR(posix_chdir__doc__
,
629 Change the current working directory to the specified path.");
632 posix_chdir(PyObject
*self
, PyObject
*args
)
634 return posix_1str(args
, "et:chdir", chdir
);
637 PyDoc_STRVAR(posix_chmod__doc__
,
638 "chmod(path, mode)\n\n\
639 Change the access permissions of a file.");
642 posix_chmod(PyObject
*self
, PyObject
*args
)
647 if (!PyArg_ParseTuple(args
, "eti:chmod", Py_FileSystemDefaultEncoding
,
650 Py_BEGIN_ALLOW_THREADS
651 res
= chmod(path
, i
);
654 return posix_error_with_allocated_filename(path
);
661 PyDoc_STRVAR(posix_fchmod__doc__
,
662 "fchmod(fd, mode)\n\n\
663 Change the access permissions of the file given by file\n\
667 posix_fchmod(PyObject
*self
, PyObject
*args
)
670 if (!PyArg_ParseTuple(args
, "ii:fchmod", &fd
, &mode
))
672 Py_BEGIN_ALLOW_THREADS
673 res
= fchmod(fd
, mode
);
676 return posix_error();
679 #endif /* HAVE_FCHMOD */
682 PyDoc_STRVAR(posix_lchmod__doc__
,
683 "lchmod(path, mode)\n\n\
684 Change the access permissions of a file. If path is a symlink, this\n\
685 affects the link itself rather than the target.");
688 posix_lchmod(PyObject
*self
, PyObject
*args
)
693 if (!PyArg_ParseTuple(args
, "eti:lchmod", Py_FileSystemDefaultEncoding
,
696 Py_BEGIN_ALLOW_THREADS
697 res
= lchmod(path
, i
);
700 return posix_error_with_allocated_filename(path
);
704 #endif /* HAVE_LCHMOD */
708 PyDoc_STRVAR(posix_chflags__doc__
,
709 "chflags(path, flags)\n\n\
713 posix_chflags(PyObject
*self
, PyObject
*args
)
718 if (!PyArg_ParseTuple(args
, "etk:chflags",
719 Py_FileSystemDefaultEncoding
, &path
, &flags
))
721 Py_BEGIN_ALLOW_THREADS
722 res
= chflags(path
, flags
);
725 return posix_error_with_allocated_filename(path
);
730 #endif /* HAVE_CHFLAGS */
733 PyDoc_STRVAR(posix_lchflags__doc__
,
734 "lchflags(path, flags)\n\n\
736 This function will not follow symbolic links.");
739 posix_lchflags(PyObject
*self
, PyObject
*args
)
744 if (!PyArg_ParseTuple(args
, "etk:lchflags",
745 Py_FileSystemDefaultEncoding
, &path
, &flags
))
747 Py_BEGIN_ALLOW_THREADS
748 res
= lchflags(path
, flags
);
751 return posix_error_with_allocated_filename(path
);
756 #endif /* HAVE_LCHFLAGS */
759 PyDoc_STRVAR(posix_chroot__doc__
,
761 Change root directory to path.");
764 posix_chroot(PyObject
*self
, PyObject
*args
)
766 return posix_1str(args
, "et:chroot", chroot
);
771 PyDoc_STRVAR(posix_fsync__doc__
,
773 force write of file with filedescriptor to disk.");
776 posix_fsync(PyObject
*self
, PyObject
*fdobj
)
778 return posix_fildes(fdobj
, fsync
);
780 #endif /* HAVE_FSYNC */
782 #ifdef HAVE_FDATASYNC
785 extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
788 PyDoc_STRVAR(posix_fdatasync__doc__
,
789 "fdatasync(fildes)\n\n\
790 force write of file with filedescriptor to disk.\n\
791 does not force update of metadata.");
794 posix_fdatasync(PyObject
*self
, PyObject
*fdobj
)
796 return posix_fildes(fdobj
, fdatasync
);
798 #endif /* HAVE_FDATASYNC */
802 PyDoc_STRVAR(posix_chown__doc__
,
803 "chown(path, uid, gid)\n\n\
804 Change the owner and group id of path to the numeric uid and gid.");
807 posix_chown(PyObject
*self
, PyObject
*args
)
812 if (!PyArg_ParseTuple(args
, "etll:chown",
813 Py_FileSystemDefaultEncoding
, &path
,
816 Py_BEGIN_ALLOW_THREADS
817 res
= chown(path
, (uid_t
) uid
, (gid_t
) gid
);
820 return posix_error_with_allocated_filename(path
);
825 #endif /* HAVE_CHOWN */
828 PyDoc_STRVAR(posix_fchown__doc__
,
829 "fchown(fd, uid, gid)\n\n\
830 Change the owner and group id of the file given by file descriptor\n\
831 fd to the numeric uid and gid.");
834 posix_fchown(PyObject
*self
, PyObject
*args
)
839 if (!PyArg_ParseTuple(args
, "ill:chown", &fd
, &uid
, &gid
))
841 Py_BEGIN_ALLOW_THREADS
842 res
= fchown(fd
, (uid_t
) uid
, (gid_t
) gid
);
845 return posix_error();
848 #endif /* HAVE_FCHOWN */
851 PyDoc_STRVAR(posix_lchown__doc__
,
852 "lchown(path, uid, gid)\n\n\
853 Change the owner and group id of path to the numeric uid and gid.\n\
854 This function will not follow symbolic links.");
857 posix_lchown(PyObject
*self
, PyObject
*args
)
862 if (!PyArg_ParseTuple(args
, "etll:lchown",
863 Py_FileSystemDefaultEncoding
, &path
,
866 Py_BEGIN_ALLOW_THREADS
867 res
= lchown(path
, (uid_t
) uid
, (gid_t
) gid
);
870 return posix_error_with_allocated_filename(path
);
875 #endif /* HAVE_LCHOWN */
879 PyDoc_STRVAR(posix_getcwd__doc__
,
880 "getcwd() -> path\n\n\
881 Return a string representing the current working directory.");
884 posix_getcwd(PyObject
*self
, PyObject
*noargs
)
886 int bufsize_incr
= 1024;
890 PyObject
*dynamic_return
;
892 Py_BEGIN_ALLOW_THREADS
894 bufsize
= bufsize
+ bufsize_incr
;
895 tmpbuf
= malloc(bufsize
);
896 if (tmpbuf
== NULL
) {
899 res
= getcwd(tmpbuf
, bufsize
);
903 } while ((res
== NULL
) && (errno
== ERANGE
));
907 return posix_error();
909 dynamic_return
= PyString_FromString(tmpbuf
);
912 return dynamic_return
;
915 #ifdef Py_USING_UNICODE
916 PyDoc_STRVAR(posix_getcwdu__doc__
,
917 "getcwdu() -> path\n\n\
918 Return a unicode string representing the current working directory.");
921 posix_getcwdu(PyObject
*self
, PyObject
*noargs
)
926 Py_BEGIN_ALLOW_THREADS
927 res
= getcwd(buf
, sizeof buf
);
930 return posix_error();
931 return PyUnicode_Decode(buf
, strlen(buf
), Py_FileSystemDefaultEncoding
,"strict");
933 #endif /* Py_USING_UNICODE */
934 #endif /* HAVE_GETCWD */
937 PyDoc_STRVAR(posix_listdir__doc__
,
938 "listdir(path) -> list_of_strings\n\n\
939 Return a list containing the names of the entries in the directory.\n\
941 path: path of directory to list\n\
943 The list is in arbitrary order. It does not include the special\n\
944 entries '.' and '..' even if they are present in the directory.");
947 posix_listdir(PyObject
*self
, PyObject
*args
)
949 /* XXX Should redo this putting the (now four) versions of opendir
950 in separate files instead of having them all here... */
957 int arg_is_unicode
= 1;
960 if (!PyArg_ParseTuple(args
, "U:listdir", &v
)) {
964 if (!PyArg_ParseTuple(args
, "et:listdir", Py_FileSystemDefaultEncoding
, &name
))
966 Py_BEGIN_ALLOW_THREADS
967 dirp
= opendir(name
);
970 return posix_error_with_allocated_filename(name
);
972 if ((d
= PyList_New(0)) == NULL
) {
973 Py_BEGIN_ALLOW_THREADS
979 if((MBname
= malloc(NAME_MAX
)) == NULL
) {
980 Py_BEGIN_ALLOW_THREADS
989 Py_BEGIN_ALLOW_THREADS
993 if ((errno
== 0) || (errno
== EISDIR
)) {
996 Py_BEGIN_ALLOW_THREADS
1000 return posix_error_with_allocated_filename(name
);
1003 if (ep
->FileName
[0] == L
'.' &&
1005 (ep
->FileName
[1] == L
'.' && NAMLEN(ep
) == 2)))
1007 if(wcstombs(MBname
, ep
->FileName
, NAME_MAX
) == -1) {
1009 Py_BEGIN_ALLOW_THREADS
1011 Py_END_ALLOW_THREADS
1016 v
= PyString_FromStringAndSize(MBname
, strlen(MBname
));
1022 #ifdef Py_USING_UNICODE
1023 if (arg_is_unicode
) {
1026 w
= PyUnicode_FromEncodedObject(v
,
1027 Py_FileSystemDefaultEncoding
,
1034 /* fall back to the original byte string, as
1035 discussed in patch #683592 */
1040 if (PyList_Append(d
, v
) != 0) {
1048 Py_BEGIN_ALLOW_THREADS
1050 Py_END_ALLOW_THREADS
1052 if(MBname
!= NULL
) {
1058 } /* end of posix_listdir */
1061 /* A helper function for abspath on win32 */
1063 posix__getfullpathname(PyObject
*self
, PyObject
*args
)
1065 /* assume encoded strings won't more than double no of chars */
1066 char inbuf
[MAX_PATH
*2];
1067 char *inbufp
= inbuf
;
1068 Py_ssize_t insize
= sizeof(inbuf
);
1069 char outbuf
[MAX_PATH
*2];
1072 PyUnicodeObject
*po
;
1073 if (PyArg_ParseTuple(args
, "U|:_getfullpathname", &po
)) {
1074 Py_UNICODE
*wpath
= PyUnicode_AS_UNICODE(po
);
1075 Py_UNICODE woutbuf
[MAX_PATH
*2], *woutbufp
= woutbuf
;
1079 result
= GetFullPathNameW(wpath
,
1080 sizeof(woutbuf
)/sizeof(woutbuf
[0]),
1082 if (result
> sizeof(woutbuf
)/sizeof(woutbuf
[0])) {
1083 woutbufp
= malloc(result
* sizeof(Py_UNICODE
));
1085 return PyErr_NoMemory();
1086 result
= GetFullPathNameW(wpath
, result
, woutbufp
, &wtemp
);
1089 v
= PyUnicode_FromUnicode(woutbufp
, wcslen(woutbufp
));
1091 v
= win32_error_unicode("GetFullPathNameW", wpath
);
1092 if (woutbufp
!= woutbuf
)
1096 /* Drop the argument parsing error as narrow strings
1100 if (!PyArg_ParseTuple (args
, "et#:_getfullpathname",
1101 Py_FileSystemDefaultEncoding
, &inbufp
,
1104 if (!GetFullPathName(inbuf
, sizeof(outbuf
)/sizeof(outbuf
[0]),
1106 return win32_error("GetFullPathName", inbuf
);
1107 if (PyUnicode_Check(PyTuple_GetItem(args
, 0))) {
1108 return PyUnicode_Decode(outbuf
, strlen(outbuf
),
1109 Py_FileSystemDefaultEncoding
, NULL
);
1111 return PyString_FromString(outbuf
);
1112 } /* end of posix__getfullpathname */
1113 #endif /* MS_WINDOWS */
1115 PyDoc_STRVAR(posix_mkdir__doc__
,
1116 "mkdir(path [, mode=0777])\n\n\
1117 Create a directory.");
1120 posix_mkdir(PyObject
*self
, PyObject
*args
)
1126 if (!PyArg_ParseTuple(args
, "et|i:mkdir",
1127 Py_FileSystemDefaultEncoding
, &path
, &mode
))
1129 Py_BEGIN_ALLOW_THREADS
1130 res
= mkdir(path
, mode
);
1131 Py_END_ALLOW_THREADS
1133 return posix_error_with_allocated_filename(path
);
1140 /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
1141 #if defined(HAVE_SYS_RESOURCE_H)
1142 #include <sys/resource.h>
1147 PyDoc_STRVAR(posix_nice__doc__
,
1148 "nice(inc) -> new_priority\n\n\
1149 Decrease the priority of process by inc and return the new priority.");
1152 posix_nice(PyObject
*self
, PyObject
*args
)
1154 int increment
, value
;
1156 if (!PyArg_ParseTuple(args
, "i:nice", &increment
))
1159 /* There are two flavours of 'nice': one that returns the new
1160 priority (as required by almost all standards out there) and the
1161 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
1162 the use of getpriority() to get the new priority.
1164 If we are of the nice family that returns the new priority, we
1165 need to clear errno before the call, and check if errno is filled
1166 before calling posix_error() on a returnvalue of -1, because the
1167 -1 may be the actual new priority! */
1170 value
= nice(increment
);
1171 #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
1173 value
= getpriority(PRIO_PROCESS
, 0);
1175 if (value
== -1 && errno
!= 0)
1176 /* either nice() or getpriority() returned an error */
1177 return posix_error();
1178 return PyInt_FromLong((long) value
);
1180 #endif /* HAVE_NICE */
1182 PyDoc_STRVAR(posix_rename__doc__
,
1183 "rename(old, new)\n\n\
1184 Rename a file or directory.");
1187 posix_rename(PyObject
*self
, PyObject
*args
)
1189 return posix_2str(args
, "etet:rename", rename
);
1193 PyDoc_STRVAR(posix_rmdir__doc__
,
1195 Remove a directory.");
1198 posix_rmdir(PyObject
*self
, PyObject
*args
)
1200 return posix_1str(args
, "et:rmdir", rmdir
);
1204 PyDoc_STRVAR(posix_stat__doc__
,
1205 "stat(path) -> stat result\n\n\
1206 Perform a stat system call on the given path.");
1209 posix_stat(PyObject
*self
, PyObject
*args
)
1211 return posix_do_stat(self
, args
, "et:stat", STAT
, NULL
, NULL
);
1216 PyDoc_STRVAR(posix_system__doc__
,
1217 "system(command) -> exit_status\n\n\
1218 Execute the command (a string) in a subshell.");
1221 posix_system(PyObject
*self
, PyObject
*args
)
1225 if (!PyArg_ParseTuple(args
, "s:system", &command
))
1227 Py_BEGIN_ALLOW_THREADS
1228 sts
= system(command
);
1229 Py_END_ALLOW_THREADS
1230 return PyInt_FromLong(sts
);
1235 PyDoc_STRVAR(posix_umask__doc__
,
1236 "umask(new_mask) -> old_mask\n\n\
1237 Set the current numeric umask and return the previous umask.");
1240 posix_umask(PyObject
*self
, PyObject
*args
)
1243 if (!PyArg_ParseTuple(args
, "i:umask", &i
))
1247 return posix_error();
1248 return PyInt_FromLong((long)i
);
1252 PyDoc_STRVAR(posix_unlink__doc__
,
1254 Remove a file (same as remove(path)).");
1256 PyDoc_STRVAR(posix_remove__doc__
,
1258 Remove a file (same as unlink(path)).");
1261 posix_unlink(PyObject
*self
, PyObject
*args
)
1263 return posix_1str(args
, "et:remove", unlink
);
1268 extract_time(PyObject
*t
, time_t* sec
, long* usec
)
1271 if (PyFloat_Check(t
)) {
1272 double tval
= PyFloat_AsDouble(t
);
1273 PyObject
*intobj
= PyNumber_Long(t
);
1276 #if SIZEOF_TIME_T > SIZEOF_LONG
1277 intval
= PyInt_AsUnsignedLongLongMask(intobj
);
1279 intval
= PyInt_AsLong(intobj
);
1282 if (intval
== -1 && PyErr_Occurred())
1285 *usec
= (long)((tval
- intval
) * 1e6
); /* can't exceed 1000000 */
1287 /* If rounding gave us a negative number,
1292 #if SIZEOF_TIME_T > SIZEOF_LONG
1293 intval
= PyInt_AsUnsignedLongLongMask(t
);
1295 intval
= PyInt_AsLong(t
);
1297 if (intval
== -1 && PyErr_Occurred())
1304 PyDoc_STRVAR(posix_utime__doc__
,
1305 "utime(path, (atime, mtime))\n\
1306 utime(path, None)\n\n\
1307 Set the access and modified time of the file to the given values. If the\n\
1308 second form is used, set the access and modified times to the current time.");
1311 posix_utime(PyObject
*self
, PyObject
*args
)
1314 time_t atime
, mtime
;
1319 #if defined(HAVE_UTIMES)
1320 struct timeval buf
[2];
1321 #define ATIME buf[0].tv_sec
1322 #define MTIME buf[1].tv_sec
1323 #elif defined(HAVE_UTIME_H)
1324 /* XXX should define struct utimbuf instead, above */
1326 #define ATIME buf.actime
1327 #define MTIME buf.modtime
1328 #define UTIME_ARG &buf
1329 #else /* HAVE_UTIMES */
1331 #define ATIME buf[0]
1332 #define MTIME buf[1]
1333 #define UTIME_ARG buf
1334 #endif /* HAVE_UTIMES */
1337 if (!PyArg_ParseTuple(args
, "etO:utime",
1338 Py_FileSystemDefaultEncoding
, &path
, &arg
))
1340 if (arg
== Py_None
) {
1341 /* optional time values not given */
1342 Py_BEGIN_ALLOW_THREADS
1343 res
= utime(path
, NULL
);
1344 Py_END_ALLOW_THREADS
1346 else if (!PyTuple_Check(arg
) || PyTuple_Size(arg
) != 2) {
1347 PyErr_SetString(PyExc_TypeError
,
1348 "utime() arg 2 must be a tuple (atime, mtime)");
1353 if (extract_time(PyTuple_GET_ITEM(arg
, 0),
1354 &atime
, &ausec
) == -1) {
1358 if (extract_time(PyTuple_GET_ITEM(arg
, 1),
1359 &mtime
, &musec
) == -1) {
1366 buf
[0].tv_usec
= ausec
;
1367 buf
[1].tv_usec
= musec
;
1368 Py_BEGIN_ALLOW_THREADS
1369 res
= utimes(path
, buf
);
1370 Py_END_ALLOW_THREADS
1372 Py_BEGIN_ALLOW_THREADS
1373 res
= utime(path
, UTIME_ARG
);
1374 Py_END_ALLOW_THREADS
1375 #endif /* HAVE_UTIMES */
1378 return posix_error_with_allocated_filename(path
);
1389 /* Process operations */
1391 PyDoc_STRVAR(posix__exit__doc__
,
1393 Exit to the system with specified status, without normal exit processing.");
1396 posix__exit(PyObject
*self
, PyObject
*args
)
1399 if (!PyArg_ParseTuple(args
, "i:_exit", &sts
))
1402 return NULL
; /* Make gcc -Wall happy */
1405 #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
1407 free_string_array(char **array
, Py_ssize_t count
)
1410 for (i
= 0; i
< count
; i
++)
1411 PyMem_Free(array
[i
]);
1418 PyDoc_STRVAR(posix_execv__doc__
,
1419 "execv(path, args)\n\n\
1420 Execute an executable path with arguments, replacing current process.\n\
1422 path: path of executable file\n\
1423 args: tuple or list of strings");
1426 posix_execv(PyObject
*self
, PyObject
*args
)
1432 PyObject
*(*getitem
)(PyObject
*, Py_ssize_t
);
1434 /* execv has two arguments: (path, argv), where
1435 argv is a list or tuple of strings. */
1437 if (!PyArg_ParseTuple(args
, "etO:execv",
1438 Py_FileSystemDefaultEncoding
,
1441 if (PyList_Check(argv
)) {
1442 argc
= PyList_Size(argv
);
1443 getitem
= PyList_GetItem
;
1445 else if (PyTuple_Check(argv
)) {
1446 argc
= PyTuple_Size(argv
);
1447 getitem
= PyTuple_GetItem
;
1450 PyErr_SetString(PyExc_TypeError
, "execv() arg 2 must be a tuple or list");
1455 PyErr_SetString(PyExc_ValueError
, "execv() arg 2 must not be empty");
1460 argvlist
= PyMem_NEW(char *, argc
+1);
1461 if (argvlist
== NULL
) {
1463 return PyErr_NoMemory();
1465 for (i
= 0; i
< argc
; i
++) {
1466 if (!PyArg_Parse((*getitem
)(argv
, i
), "et",
1467 Py_FileSystemDefaultEncoding
,
1469 free_string_array(argvlist
, i
);
1470 PyErr_SetString(PyExc_TypeError
,
1471 "execv() arg 2 must contain only strings");
1477 argvlist
[argc
] = NULL
;
1479 execv(path
, argvlist
);
1481 /* If we get here it's definitely an error */
1483 free_string_array(argvlist
, argc
);
1485 return posix_error();
1489 PyDoc_STRVAR(posix_execve__doc__
,
1490 "execve(path, args, env)\n\n\
1491 Execute a path with arguments and environment, replacing current process.\n\
1493 path: path of executable file\n\
1494 args: tuple or list of arguments\n\
1495 env: dictionary of strings mapping to strings");
1498 posix_execve(PyObject
*self
, PyObject
*args
)
1501 PyObject
*argv
, *env
;
1504 PyObject
*key
, *val
, *keys
=NULL
, *vals
=NULL
;
1505 Py_ssize_t i
, pos
, argc
, envc
;
1506 PyObject
*(*getitem
)(PyObject
*, Py_ssize_t
);
1507 Py_ssize_t lastarg
= 0;
1509 /* execve has three arguments: (path, argv, env), where
1510 argv is a list or tuple of strings and env is a dictionary
1511 like posix.environ. */
1513 if (!PyArg_ParseTuple(args
, "etOO:execve",
1514 Py_FileSystemDefaultEncoding
,
1515 &path
, &argv
, &env
))
1517 if (PyList_Check(argv
)) {
1518 argc
= PyList_Size(argv
);
1519 getitem
= PyList_GetItem
;
1521 else if (PyTuple_Check(argv
)) {
1522 argc
= PyTuple_Size(argv
);
1523 getitem
= PyTuple_GetItem
;
1526 PyErr_SetString(PyExc_TypeError
,
1527 "execve() arg 2 must be a tuple or list");
1530 if (!PyMapping_Check(env
)) {
1531 PyErr_SetString(PyExc_TypeError
,
1532 "execve() arg 3 must be a mapping object");
1536 argvlist
= PyMem_NEW(char *, argc
+1);
1537 if (argvlist
== NULL
) {
1541 for (i
= 0; i
< argc
; i
++) {
1542 if (!PyArg_Parse((*getitem
)(argv
, i
),
1543 "et;execve() arg 2 must contain only strings",
1544 Py_FileSystemDefaultEncoding
,
1552 argvlist
[argc
] = NULL
;
1554 i
= PyMapping_Size(env
);
1557 envlist
= PyMem_NEW(char *, i
+ 1);
1558 if (envlist
== NULL
) {
1563 keys
= PyMapping_Keys(env
);
1564 vals
= PyMapping_Values(env
);
1567 if (!PyList_Check(keys
) || !PyList_Check(vals
)) {
1568 PyErr_SetString(PyExc_TypeError
,
1569 "execve(): env.keys() or env.values() is not a list");
1573 for (pos
= 0; pos
< i
; pos
++) {
1577 key
= PyList_GetItem(keys
, pos
);
1578 val
= PyList_GetItem(vals
, pos
);
1584 "s;execve() arg 3 contains a non-string key",
1588 "s;execve() arg 3 contains a non-string value",
1594 #if defined(PYOS_OS2)
1595 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
1596 if (stricmp(k
, "BEGINLIBPATH") != 0 && stricmp(k
, "ENDLIBPATH") != 0) {
1598 len
= PyString_Size(key
) + PyString_Size(val
) + 2;
1599 p
= PyMem_NEW(char, len
);
1604 PyOS_snprintf(p
, len
, "%s=%s", k
, v
);
1605 envlist
[envc
++] = p
;
1606 #if defined(PYOS_OS2)
1612 execve(path
, argvlist
, envlist
);
1614 /* If we get here it's definitely an error */
1616 (void) posix_error();
1620 PyMem_DEL(envlist
[envc
]);
1623 free_string_array(argvlist
, lastarg
);
1630 #endif /* HAVE_EXECV */
1634 PyDoc_STRVAR(posix_spawnv__doc__
,
1635 "spawnv(mode, path, args)\n\n\
1636 Execute the program 'path' in a new process.\n\
1638 mode: mode of process creation\n\
1639 path: path of executable file\n\
1640 args: tuple or list of strings");
1643 posix_spawnv(PyObject
*self
, PyObject
*args
)
1650 Py_intptr_t spawnval
;
1651 PyObject
*(*getitem
)(PyObject
*, Py_ssize_t
);
1653 /* spawnv has three arguments: (mode, path, argv), where
1654 argv is a list or tuple of strings. */
1656 if (!PyArg_ParseTuple(args
, "ietO:spawnv", &mode
,
1657 Py_FileSystemDefaultEncoding
,
1660 if (PyList_Check(argv
)) {
1661 argc
= PyList_Size(argv
);
1662 getitem
= PyList_GetItem
;
1664 else if (PyTuple_Check(argv
)) {
1665 argc
= PyTuple_Size(argv
);
1666 getitem
= PyTuple_GetItem
;
1669 PyErr_SetString(PyExc_TypeError
,
1670 "spawnv() arg 2 must be a tuple or list");
1675 argvlist
= PyMem_NEW(char *, argc
+1);
1676 if (argvlist
== NULL
) {
1678 return PyErr_NoMemory();
1680 for (i
= 0; i
< argc
; i
++) {
1681 if (!PyArg_Parse((*getitem
)(argv
, i
), "et",
1682 Py_FileSystemDefaultEncoding
,
1684 free_string_array(argvlist
, i
);
1687 "spawnv() arg 2 must contain only strings");
1692 argvlist
[argc
] = NULL
;
1694 #if defined(PYOS_OS2) && defined(PYCC_GCC)
1695 Py_BEGIN_ALLOW_THREADS
1696 spawnval
= spawnv(mode
, path
, argvlist
);
1697 Py_END_ALLOW_THREADS
1699 if (mode
== _OLD_P_OVERLAY
)
1702 Py_BEGIN_ALLOW_THREADS
1703 spawnval
= _spawnv(mode
, path
, argvlist
);
1704 Py_END_ALLOW_THREADS
1707 free_string_array(argvlist
, argc
);
1711 return posix_error();
1713 #if SIZEOF_LONG == SIZEOF_VOID_P
1714 return Py_BuildValue("l", (long) spawnval
);
1716 return Py_BuildValue("L", (PY_LONG_LONG
) spawnval
);
1721 PyDoc_STRVAR(posix_spawnve__doc__
,
1722 "spawnve(mode, path, args, env)\n\n\
1723 Execute the program 'path' in a new process.\n\
1725 mode: mode of process creation\n\
1726 path: path of executable file\n\
1727 args: tuple or list of arguments\n\
1728 env: dictionary of strings mapping to strings");
1731 posix_spawnve(PyObject
*self
, PyObject
*args
)
1734 PyObject
*argv
, *env
;
1737 PyObject
*key
, *val
, *keys
=NULL
, *vals
=NULL
, *res
=NULL
;
1738 int mode
, pos
, envc
;
1740 Py_intptr_t spawnval
;
1741 PyObject
*(*getitem
)(PyObject
*, Py_ssize_t
);
1742 Py_ssize_t lastarg
= 0;
1744 /* spawnve has four arguments: (mode, path, argv, env), where
1745 argv is a list or tuple of strings and env is a dictionary
1746 like posix.environ. */
1748 if (!PyArg_ParseTuple(args
, "ietOO:spawnve", &mode
,
1749 Py_FileSystemDefaultEncoding
,
1750 &path
, &argv
, &env
))
1752 if (PyList_Check(argv
)) {
1753 argc
= PyList_Size(argv
);
1754 getitem
= PyList_GetItem
;
1756 else if (PyTuple_Check(argv
)) {
1757 argc
= PyTuple_Size(argv
);
1758 getitem
= PyTuple_GetItem
;
1761 PyErr_SetString(PyExc_TypeError
,
1762 "spawnve() arg 2 must be a tuple or list");
1765 if (!PyMapping_Check(env
)) {
1766 PyErr_SetString(PyExc_TypeError
,
1767 "spawnve() arg 3 must be a mapping object");
1771 argvlist
= PyMem_NEW(char *, argc
+1);
1772 if (argvlist
== NULL
) {
1776 for (i
= 0; i
< argc
; i
++) {
1777 if (!PyArg_Parse((*getitem
)(argv
, i
),
1778 "et;spawnve() arg 2 must contain only strings",
1779 Py_FileSystemDefaultEncoding
,
1787 argvlist
[argc
] = NULL
;
1789 i
= PyMapping_Size(env
);
1792 envlist
= PyMem_NEW(char *, i
+ 1);
1793 if (envlist
== NULL
) {
1798 keys
= PyMapping_Keys(env
);
1799 vals
= PyMapping_Values(env
);
1802 if (!PyList_Check(keys
) || !PyList_Check(vals
)) {
1803 PyErr_SetString(PyExc_TypeError
,
1804 "spawnve(): env.keys() or env.values() is not a list");
1808 for (pos
= 0; pos
< i
; pos
++) {
1812 key
= PyList_GetItem(keys
, pos
);
1813 val
= PyList_GetItem(vals
, pos
);
1819 "s;spawnve() arg 3 contains a non-string key",
1823 "s;spawnve() arg 3 contains a non-string value",
1828 len
= PyString_Size(key
) + PyString_Size(val
) + 2;
1829 p
= PyMem_NEW(char, len
);
1834 PyOS_snprintf(p
, len
, "%s=%s", k
, v
);
1835 envlist
[envc
++] = p
;
1839 #if defined(PYOS_OS2) && defined(PYCC_GCC)
1840 Py_BEGIN_ALLOW_THREADS
1841 spawnval
= spawnve(mode
, path
, argvlist
, envlist
);
1842 Py_END_ALLOW_THREADS
1844 if (mode
== _OLD_P_OVERLAY
)
1847 Py_BEGIN_ALLOW_THREADS
1848 spawnval
= _spawnve(mode
, path
, argvlist
, envlist
);
1849 Py_END_ALLOW_THREADS
1853 (void) posix_error();
1855 #if SIZEOF_LONG == SIZEOF_VOID_P
1856 res
= Py_BuildValue("l", (long) spawnval
);
1858 res
= Py_BuildValue("L", (PY_LONG_LONG
) spawnval
);
1863 PyMem_DEL(envlist
[envc
]);
1866 free_string_array(argvlist
, lastarg
);
1874 /* OS/2 supports spawnvp & spawnvpe natively */
1875 #if defined(PYOS_OS2)
1876 PyDoc_STRVAR(posix_spawnvp__doc__
,
1877 "spawnvp(mode, file, args)\n\n\
1878 Execute the program 'file' in a new process, using the environment\n\
1879 search path to find the file.\n\
1881 mode: mode of process creation\n\
1882 file: executable file name\n\
1883 args: tuple or list of strings");
1886 posix_spawnvp(PyObject
*self
, PyObject
*args
)
1892 Py_intptr_t spawnval
;
1893 PyObject
*(*getitem
)(PyObject
*, Py_ssize_t
);
1895 /* spawnvp has three arguments: (mode, path, argv), where
1896 argv is a list or tuple of strings. */
1898 if (!PyArg_ParseTuple(args
, "ietO:spawnvp", &mode
,
1899 Py_FileSystemDefaultEncoding
,
1902 if (PyList_Check(argv
)) {
1903 argc
= PyList_Size(argv
);
1904 getitem
= PyList_GetItem
;
1906 else if (PyTuple_Check(argv
)) {
1907 argc
= PyTuple_Size(argv
);
1908 getitem
= PyTuple_GetItem
;
1911 PyErr_SetString(PyExc_TypeError
,
1912 "spawnvp() arg 2 must be a tuple or list");
1917 argvlist
= PyMem_NEW(char *, argc
+1);
1918 if (argvlist
== NULL
) {
1920 return PyErr_NoMemory();
1922 for (i
= 0; i
< argc
; i
++) {
1923 if (!PyArg_Parse((*getitem
)(argv
, i
), "et",
1924 Py_FileSystemDefaultEncoding
,
1926 free_string_array(argvlist
, i
);
1929 "spawnvp() arg 2 must contain only strings");
1934 argvlist
[argc
] = NULL
;
1936 Py_BEGIN_ALLOW_THREADS
1937 #if defined(PYCC_GCC)
1938 spawnval
= spawnvp(mode
, path
, argvlist
);
1940 spawnval
= _spawnvp(mode
, path
, argvlist
);
1942 Py_END_ALLOW_THREADS
1944 free_string_array(argvlist
, argc
);
1948 return posix_error();
1950 return Py_BuildValue("l", (long) spawnval
);
1954 PyDoc_STRVAR(posix_spawnvpe__doc__
,
1955 "spawnvpe(mode, file, args, env)\n\n\
1956 Execute the program 'file' in a new process, using the environment\n\
1957 search path to find the file.\n\
1959 mode: mode of process creation\n\
1960 file: executable file name\n\
1961 args: tuple or list of arguments\n\
1962 env: dictionary of strings mapping to strings");
1965 posix_spawnvpe(PyObject
*self
, PyObject
*args
)
1968 PyObject
*argv
, *env
;
1971 PyObject
*key
, *val
, *keys
=NULL
, *vals
=NULL
, *res
=NULL
;
1972 int mode
, i
, pos
, argc
, envc
;
1973 Py_intptr_t spawnval
;
1974 PyObject
*(*getitem
)(PyObject
*, Py_ssize_t
);
1977 /* spawnvpe has four arguments: (mode, path, argv, env), where
1978 argv is a list or tuple of strings and env is a dictionary
1979 like posix.environ. */
1981 if (!PyArg_ParseTuple(args
, "ietOO:spawnvpe", &mode
,
1982 Py_FileSystemDefaultEncoding
,
1983 &path
, &argv
, &env
))
1985 if (PyList_Check(argv
)) {
1986 argc
= PyList_Size(argv
);
1987 getitem
= PyList_GetItem
;
1989 else if (PyTuple_Check(argv
)) {
1990 argc
= PyTuple_Size(argv
);
1991 getitem
= PyTuple_GetItem
;
1994 PyErr_SetString(PyExc_TypeError
,
1995 "spawnvpe() arg 2 must be a tuple or list");
1998 if (!PyMapping_Check(env
)) {
1999 PyErr_SetString(PyExc_TypeError
,
2000 "spawnvpe() arg 3 must be a mapping object");
2004 argvlist
= PyMem_NEW(char *, argc
+1);
2005 if (argvlist
== NULL
) {
2009 for (i
= 0; i
< argc
; i
++) {
2010 if (!PyArg_Parse((*getitem
)(argv
, i
),
2011 "et;spawnvpe() arg 2 must contain only strings",
2012 Py_FileSystemDefaultEncoding
,
2020 argvlist
[argc
] = NULL
;
2022 i
= PyMapping_Size(env
);
2025 envlist
= PyMem_NEW(char *, i
+ 1);
2026 if (envlist
== NULL
) {
2031 keys
= PyMapping_Keys(env
);
2032 vals
= PyMapping_Values(env
);
2035 if (!PyList_Check(keys
) || !PyList_Check(vals
)) {
2036 PyErr_SetString(PyExc_TypeError
,
2037 "spawnvpe(): env.keys() or env.values() is not a list");
2041 for (pos
= 0; pos
< i
; pos
++) {
2045 key
= PyList_GetItem(keys
, pos
);
2046 val
= PyList_GetItem(vals
, pos
);
2052 "s;spawnvpe() arg 3 contains a non-string key",
2056 "s;spawnvpe() arg 3 contains a non-string value",
2061 len
= PyString_Size(key
) + PyString_Size(val
) + 2;
2062 p
= PyMem_NEW(char, len
);
2067 PyOS_snprintf(p
, len
, "%s=%s", k
, v
);
2068 envlist
[envc
++] = p
;
2072 Py_BEGIN_ALLOW_THREADS
2073 #if defined(PYCC_GCC)
2074 spawnval
= spawnvpe(mode
, path
, argvlist
, envlist
);
2076 spawnval
= _spawnvpe(mode
, path
, argvlist
, envlist
);
2078 Py_END_ALLOW_THREADS
2081 (void) posix_error();
2083 res
= Py_BuildValue("l", (long) spawnval
);
2087 PyMem_DEL(envlist
[envc
]);
2090 free_string_array(argvlist
, lastarg
);
2097 #endif /* PYOS_OS2 */
2098 #endif /* HAVE_SPAWNV */
2102 PyDoc_STRVAR(posix_fork1__doc__
,
2103 "fork1() -> pid\n\n\
2104 Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
2106 Return 0 to child process and PID of child to parent process.");
2109 posix_fork1(PyObject
*self
, PyObject
*noargs
)
2113 _PyImport_AcquireLock();
2116 /* child: this clobbers and resets the import lock. */
2119 /* parent: release the import lock. */
2120 result
= _PyImport_ReleaseLock();
2123 return posix_error();
2125 /* Don't clobber the OSError if the fork failed. */
2126 PyErr_SetString(PyExc_RuntimeError
,
2127 "not holding the import lock");
2130 return PyLong_FromPid(pid
);
2136 PyDoc_STRVAR(posix_fork__doc__
,
2138 Fork a child process.\n\
2139 Return 0 to child process and PID of child to parent process.");
2142 posix_fork(PyObject
*self
, PyObject
*noargs
)
2146 _PyImport_AcquireLock();
2149 /* child: this clobbers and resets the import lock. */
2152 /* parent: release the import lock. */
2153 result
= _PyImport_ReleaseLock();
2156 return posix_error();
2158 /* Don't clobber the OSError if the fork failed. */
2159 PyErr_SetString(PyExc_RuntimeError
,
2160 "not holding the import lock");
2163 return PyLong_FromPid(pid
);
2167 /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
2168 /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
2169 #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
2170 #define DEV_PTY_FILE "/dev/ptc"
2171 #define HAVE_DEV_PTMX
2173 #define DEV_PTY_FILE "/dev/ptmx"
2176 #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
2180 #ifdef HAVE_LIBUTIL_H
2181 #include <libutil.h>
2185 #endif /* HAVE_UTIL_H */
2186 #endif /* HAVE_LIBUTIL_H */
2187 #endif /* HAVE_PTY_H */
2188 #ifdef HAVE_STROPTS_H
2189 #include <stropts.h>
2191 #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
2193 #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
2194 PyDoc_STRVAR(posix_openpty__doc__
,
2195 "openpty() -> (master_fd, slave_fd)\n\n\
2196 Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
2199 posix_openpty(PyObject
*self
, PyObject
*noargs
)
2201 int master_fd
, slave_fd
;
2202 #ifndef HAVE_OPENPTY
2205 #if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
2206 PyOS_sighandler_t sig_saved
;
2208 extern char *ptsname(int fildes
);
2213 if (openpty(&master_fd
, &slave_fd
, NULL
, NULL
, NULL
) != 0)
2214 return posix_error();
2215 #elif defined(HAVE__GETPTY)
2216 slave_name
= _getpty(&master_fd
, O_RDWR
, 0666, 0);
2217 if (slave_name
== NULL
)
2218 return posix_error();
2220 slave_fd
= open(slave_name
, O_RDWR
);
2222 return posix_error();
2224 master_fd
= open(DEV_PTY_FILE
, O_RDWR
| O_NOCTTY
); /* open master */
2226 return posix_error();
2227 sig_saved
= PyOS_setsig(SIGCHLD
, SIG_DFL
);
2228 /* change permission of slave */
2229 if (grantpt(master_fd
) < 0) {
2230 PyOS_setsig(SIGCHLD
, sig_saved
);
2231 return posix_error();
2234 if (unlockpt(master_fd
) < 0) {
2235 PyOS_setsig(SIGCHLD
, sig_saved
);
2236 return posix_error();
2238 PyOS_setsig(SIGCHLD
, sig_saved
);
2239 slave_name
= ptsname(master_fd
); /* get name of slave */
2240 if (slave_name
== NULL
)
2241 return posix_error();
2242 slave_fd
= open(slave_name
, O_RDWR
| O_NOCTTY
); /* open slave */
2244 return posix_error();
2245 #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
2246 ioctl(slave_fd
, I_PUSH
, "ptem"); /* push ptem */
2247 ioctl(slave_fd
, I_PUSH
, "ldterm"); /* push ldterm */
2249 ioctl(slave_fd
, I_PUSH
, "ttcompat"); /* push ttcompat */
2251 #endif /* HAVE_CYGWIN */
2252 #endif /* HAVE_OPENPTY */
2254 return Py_BuildValue("(ii)", master_fd
, slave_fd
);
2257 #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
2260 PyDoc_STRVAR(posix_forkpty__doc__
,
2261 "forkpty() -> (pid, master_fd)\n\n\
2262 Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
2263 Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
2264 To both, return fd of newly opened pseudo-terminal.\n");
2267 posix_forkpty(PyObject
*self
, PyObject
*noargs
)
2269 int master_fd
= -1, result
= 0;
2272 _PyImport_AcquireLock();
2273 pid
= forkpty(&master_fd
, NULL
, NULL
, NULL
);
2275 /* child: this clobbers and resets the import lock. */
2278 /* parent: release the import lock. */
2279 result
= _PyImport_ReleaseLock();
2282 return posix_error();
2284 /* Don't clobber the OSError if the fork failed. */
2285 PyErr_SetString(PyExc_RuntimeError
,
2286 "not holding the import lock");
2289 return Py_BuildValue("(Ni)", PyLong_FromPid(pid
), master_fd
);
2294 PyDoc_STRVAR(posix_getegid__doc__
,
2295 "getegid() -> egid\n\n\
2296 Return the current process's effective group id.");
2299 posix_getegid(PyObject
*self
, PyObject
*noargs
)
2301 return PyInt_FromLong((long)getegid());
2307 PyDoc_STRVAR(posix_geteuid__doc__
,
2308 "geteuid() -> euid\n\n\
2309 Return the current process's effective user id.");
2312 posix_geteuid(PyObject
*self
, PyObject
*noargs
)
2314 return PyInt_FromLong((long)geteuid());
2320 PyDoc_STRVAR(posix_getgid__doc__
,
2321 "getgid() -> gid\n\n\
2322 Return the current process's group id.");
2325 posix_getgid(PyObject
*self
, PyObject
*noargs
)
2327 return PyInt_FromLong((long)getgid());
2332 PyDoc_STRVAR(posix_getpid__doc__
,
2333 "getpid() -> pid\n\n\
2334 Return the current process id");
2337 posix_getpid(PyObject
*self
, PyObject
*noargs
)
2339 return PyLong_FromPid(getpid());
2343 #ifdef HAVE_GETGROUPS
2344 PyDoc_STRVAR(posix_getgroups__doc__
,
2345 "getgroups() -> list of group IDs\n\n\
2346 Return list of supplemental group IDs for the process.");
2349 posix_getgroups(PyObject
*self
, PyObject
*noargs
)
2351 PyObject
*result
= NULL
;
2354 #define MAX_GROUPS NGROUPS_MAX
2356 /* defined to be 16 on Solaris7, so this should be a small number */
2357 #define MAX_GROUPS 64
2359 gid_t grouplist
[MAX_GROUPS
];
2361 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
2362 * This is a helper variable to store the intermediate result when
2365 * To keep the code readable the OSX behaviour is unconditional,
2366 * according to the POSIX spec this should be safe on all unix-y
2369 gid_t
* alt_grouplist
= grouplist
;
2372 n
= getgroups(MAX_GROUPS
, grouplist
);
2374 if (errno
== EINVAL
) {
2375 n
= getgroups(0, NULL
);
2377 return posix_error();
2380 /* Avoid malloc(0) */
2381 alt_grouplist
= grouplist
;
2383 alt_grouplist
= PyMem_Malloc(n
* sizeof(gid_t
));
2384 if (alt_grouplist
== NULL
) {
2386 return posix_error();
2388 n
= getgroups(n
, alt_grouplist
);
2390 PyMem_Free(alt_grouplist
);
2391 return posix_error();
2395 return posix_error();
2398 result
= PyList_New(n
);
2399 if (result
!= NULL
) {
2401 for (i
= 0; i
< n
; ++i
) {
2402 PyObject
*o
= PyInt_FromLong((long)alt_grouplist
[i
]);
2408 PyList_SET_ITEM(result
, i
, o
);
2412 if (alt_grouplist
!= grouplist
) {
2413 PyMem_Free(alt_grouplist
);
2420 #ifdef HAVE_INITGROUPS
2421 PyDoc_STRVAR(posix_initgroups__doc__
,
2422 "initgroups(username, gid) -> None\n\n\
2423 Call the system initgroups() to initialize the group access list with all of\n\
2424 the groups of which the specified username is a member, plus the specified\n\
2428 posix_initgroups(PyObject
*self
, PyObject
*args
)
2433 if (!PyArg_ParseTuple(args
, "sl:initgroups", &username
, &gid
))
2436 if (initgroups(username
, (gid_t
) gid
) == -1)
2437 return PyErr_SetFromErrno(PyExc_OSError
);
2445 PyDoc_STRVAR(posix_getpgid__doc__
,
2446 "getpgid(pid) -> pgid\n\n\
2447 Call the system call getpgid().");
2450 posix_getpgid(PyObject
*self
, PyObject
*args
)
2453 if (!PyArg_ParseTuple(args
, PARSE_PID
":getpgid", &pid
))
2455 pgid
= getpgid(pid
);
2457 return posix_error();
2458 return PyLong_FromPid(pgid
);
2460 #endif /* HAVE_GETPGID */
2464 PyDoc_STRVAR(posix_getpgrp__doc__
,
2465 "getpgrp() -> pgrp\n\n\
2466 Return the current process group id.");
2469 posix_getpgrp(PyObject
*self
, PyObject
*noargs
)
2471 #ifdef GETPGRP_HAVE_ARG
2472 return PyLong_FromPid(getpgrp(0));
2473 #else /* GETPGRP_HAVE_ARG */
2474 return PyLong_FromPid(getpgrp());
2475 #endif /* GETPGRP_HAVE_ARG */
2477 #endif /* HAVE_GETPGRP */
2481 PyDoc_STRVAR(posix_setpgrp__doc__
,
2483 Make this process the process group leader.");
2486 posix_setpgrp(PyObject
*self
, PyObject
*noargs
)
2488 #ifdef SETPGRP_HAVE_ARG
2489 if (setpgrp(0, 0) < 0)
2490 #else /* SETPGRP_HAVE_ARG */
2492 #endif /* SETPGRP_HAVE_ARG */
2493 return posix_error();
2498 #endif /* HAVE_SETPGRP */
2501 PyDoc_STRVAR(posix_getppid__doc__
,
2502 "getppid() -> ppid\n\n\
2503 Return the parent's process id.");
2506 posix_getppid(PyObject
*self
, PyObject
*noargs
)
2508 return PyLong_FromPid(getppid());
2513 #ifdef HAVE_GETLOGIN
2514 PyDoc_STRVAR(posix_getlogin__doc__
,
2515 "getlogin() -> string\n\n\
2516 Return the actual login name.");
2519 posix_getlogin(PyObject
*self
, PyObject
*noargs
)
2521 PyObject
*result
= NULL
;
2523 int old_errno
= errno
;
2531 PyErr_SetString(PyExc_OSError
,
2532 "unable to determine login name");
2535 result
= PyString_FromString(name
);
2542 PyDoc_STRVAR(posix_getuid__doc__
,
2543 "getuid() -> uid\n\n\
2544 Return the current process's user id.");
2547 posix_getuid(PyObject
*self
, PyObject
*noargs
)
2549 return PyInt_FromLong((long)getuid());
2554 PyDoc_STRVAR(posix_kill__doc__
,
2555 "kill(pid, sig)\n\n\
2556 Kill a process with a signal.");
2559 posix_kill(PyObject
*self
, PyObject
*args
)
2563 if (!PyArg_ParseTuple(args
, PARSE_PID
"i:kill", &pid
, &sig
))
2565 #if defined(PYOS_OS2) && !defined(PYCC_GCC)
2566 if (sig
== XCPT_SIGNAL_INTR
|| sig
== XCPT_SIGNAL_BREAK
) {
2568 if ((rc
= DosSendSignalException(pid
, sig
)) != NO_ERROR
)
2569 return os2_error(rc
);
2571 } else if (sig
== XCPT_SIGNAL_KILLPROC
) {
2573 if ((rc
= DosKillProcess(DKP_PROCESS
, pid
)) != NO_ERROR
)
2574 return os2_error(rc
);
2577 return NULL
; /* Unrecognized Signal Requested */
2579 if (kill(pid
, sig
) == -1)
2580 return posix_error();
2588 PyDoc_STRVAR(posix_killpg__doc__
,
2589 "killpg(pgid, sig)\n\n\
2590 Kill a process group with a signal.");
2593 posix_killpg(PyObject
*self
, PyObject
*args
)
2597 /* XXX some man pages make the `pgid` parameter an int, others
2598 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
2599 take the same type. Moreover, pid_t is always at least as wide as
2600 int (else compilation of this module fails), which is safe. */
2601 if (!PyArg_ParseTuple(args
, PARSE_PID
"i:killpg", &pgid
, &sig
))
2603 if (killpg(pgid
, sig
) == -1)
2604 return posix_error();
2612 #ifdef HAVE_SYS_LOCK_H
2613 #include <sys/lock.h>
2616 PyDoc_STRVAR(posix_plock__doc__
,
2618 Lock program segments into memory.");
2621 posix_plock(PyObject
*self
, PyObject
*args
)
2624 if (!PyArg_ParseTuple(args
, "i:plock", &op
))
2626 if (plock(op
) == -1)
2627 return posix_error();
2635 PyDoc_STRVAR(posix_popen__doc__
,
2636 "popen(command [, mode='r' [, bufsize]]) -> pipe\n\n\
2637 Open a pipe to/from a command returning a file object.");
2639 #if defined(PYOS_OS2)
2640 #if defined(PYCC_VACPP)
2642 async_system(const char *command
)
2644 char errormsg
[256], args
[1024];
2648 char *shell
= getenv("COMSPEC");
2652 /* avoid overflowing the argument buffer */
2653 if (strlen(shell
) + 3 + strlen(command
) >= 1024)
2654 return ERROR_NOT_ENOUGH_MEMORY
2657 strcat(args
, shell
);
2658 strcat(args
, "/c ");
2659 strcat(args
, command
);
2661 /* execute asynchronously, inheriting the environment */
2662 rc
= DosExecPgm(errormsg
,
2673 popen(const char *command
, const char *mode
, int pipesize
, int *err
)
2679 /* mode determines which of stdin or stdout is reconnected to
2680 * the pipe to the child
2682 if (strchr(mode
, 'r') != NULL
) {
2683 tgt_fd
= 1; /* stdout */
2684 } else if (strchr(mode
, 'w')) {
2685 tgt_fd
= 0; /* stdin */
2687 *err
= ERROR_INVALID_ACCESS
;
2691 /* setup the pipe */
2692 if ((rc
= DosCreatePipe(&pipeh
[0], &pipeh
[1], pipesize
)) != NO_ERROR
) {
2697 /* prevent other threads accessing stdio */
2700 /* reconnect stdio and execute child */
2703 if (dup2(pipeh
[tgtfd
], tgtfd
) == 0) {
2704 DosClose(pipeh
[tgtfd
]);
2705 rc
= async_system(command
);
2712 /* allow other threads access to stdio */
2715 /* if execution of child was successful return file stream */
2717 return fdopen(pipeh
[1 - tgtfd
], mode
);
2719 DosClose(pipeh
[1 - tgtfd
]);
2726 posix_popen(PyObject
*self
, PyObject
*args
)
2730 int err
, bufsize
= -1;
2733 if (!PyArg_ParseTuple(args
, "s|si:popen", &name
, &mode
, &bufsize
))
2735 Py_BEGIN_ALLOW_THREADS
2736 fp
= popen(name
, mode
, (bufsize
> 0) ? bufsize
: 4096, &err
);
2737 Py_END_ALLOW_THREADS
2739 return os2_error(err
);
2741 f
= PyFile_FromFile(fp
, name
, mode
, fclose
);
2743 PyFile_SetBufSize(f
, bufsize
);
2747 #elif defined(PYCC_GCC)
2749 /* standard posix version of popen() support */
2751 posix_popen(PyObject
*self
, PyObject
*args
)
2758 if (!PyArg_ParseTuple(args
, "s|si:popen", &name
, &mode
, &bufsize
))
2760 Py_BEGIN_ALLOW_THREADS
2761 fp
= popen(name
, mode
);
2762 Py_END_ALLOW_THREADS
2764 return posix_error();
2765 f
= PyFile_FromFile(fp
, name
, mode
, pclose
);
2767 PyFile_SetBufSize(f
, bufsize
);
2771 /* fork() under OS/2 has lots'o'warts
2772 * EMX supports pipe() and spawn*() so we can synthesize popen[234]()
2773 * most of this code is a ripoff of the win32 code, but using the
2774 * capabilities of EMX's C library routines
2777 /* These tell _PyPopen() whether to return 1, 2, or 3 file objects. */
2783 static PyObject
*_PyPopen(char *, int, int, int);
2784 static int _PyPclose(FILE *file
);
2787 * Internal dictionary mapping popen* file pointers to process handles,
2788 * for use when retrieving the process exit code. See _PyPclose() below
2789 * for more information on this dictionary's use.
2791 static PyObject
*_PyPopenProcs
= NULL
;
2793 /* os2emx version of popen2()
2795 * The result of this function is a pipe (file) connected to the
2796 * process's stdin, and a pipe connected to the process's stdout.
2800 os2emx_popen2(PyObject
*self
, PyObject
*args
)
2808 if (!PyArg_ParseTuple(args
, "s|si:popen2", &cmdstring
, &mode
, &bufsize
))
2813 else if (*mode
!= 'b') {
2814 PyErr_SetString(PyExc_ValueError
, "mode must be 't' or 'b'");
2819 f
= _PyPopen(cmdstring
, tm
, POPEN_2
, bufsize
);
2825 * Variation on os2emx.popen2
2827 * The result of this function is 3 pipes - the process's stdin,
2832 os2emx_popen3(PyObject
*self
, PyObject
*args
)
2840 if (!PyArg_ParseTuple(args
, "s|si:popen3", &cmdstring
, &mode
, &bufsize
))
2845 else if (*mode
!= 'b') {
2846 PyErr_SetString(PyExc_ValueError
, "mode must be 't' or 'b'");
2851 f
= _PyPopen(cmdstring
, tm
, POPEN_3
, bufsize
);
2857 * Variation on os2emx.popen2
2859 * The result of this function is 2 pipes - the processes stdin,
2860 * and stdout+stderr combined as a single pipe.
2864 os2emx_popen4(PyObject
*self
, PyObject
*args
)
2872 if (!PyArg_ParseTuple(args
, "s|si:popen4", &cmdstring
, &mode
, &bufsize
))
2877 else if (*mode
!= 'b') {
2878 PyErr_SetString(PyExc_ValueError
, "mode must be 't' or 'b'");
2883 f
= _PyPopen(cmdstring
, tm
, POPEN_4
, bufsize
);
2888 /* a couple of structures for convenient handling of multiple
2889 * file handles and pipes
2903 /* The following code is derived from the win32 code */
2906 _PyPopen(char *cmdstring
, int mode
, int n
, int bufsize
)
2908 struct file_ref stdio
[3];
2909 struct pipe_ref p_fd
[3];
2911 int file_count
, i
, pipe_err
;
2913 char *shell
, *sh_name
, *opt
, *rd_mode
, *wr_mode
;
2914 PyObject
*f
, *p_f
[3];
2916 /* file modes for subsequent fdopen's on pipe handles */
2928 /* prepare shell references */
2929 if ((shell
= getenv("EMXSHELL")) == NULL
)
2930 if ((shell
= getenv("COMSPEC")) == NULL
)
2933 return posix_error();
2936 sh_name
= _getname(shell
);
2937 if (stricmp(sh_name
, "cmd.exe") == 0 || stricmp(sh_name
, "4os2.exe") == 0)
2942 /* save current stdio fds + their flags, and set not inheritable */
2944 while (pipe_err
>= 0 && i
< 3)
2946 pipe_err
= stdio
[i
].handle
= dup(i
);
2947 stdio
[i
].flags
= fcntl(i
, F_GETFD
, 0);
2948 fcntl(stdio
[i
].handle
, F_SETFD
, stdio
[i
].flags
| FD_CLOEXEC
);
2953 /* didn't get them all saved - clean up and bail out */
2954 int saved_err
= errno
;
2957 close(stdio
[i
].handle
);
2960 return posix_error();
2963 /* create pipe ends */
2968 while ((pipe_err
== 0) && (i
< file_count
))
2969 pipe_err
= pipe((int *)&p_fd
[i
++]);
2972 /* didn't get them all made - clean up and bail out */
2979 return posix_error();
2982 /* change the actual standard IO streams over temporarily,
2983 * making the retained pipe ends non-inheritable
2988 if (dup2(p_fd
[0].rd
, 0) == 0)
2991 i
= fcntl(p_fd
[0].wr
, F_GETFD
, 0);
2992 fcntl(p_fd
[0].wr
, F_SETFD
, i
| FD_CLOEXEC
);
2993 if ((p_s
[0] = fdopen(p_fd
[0].wr
, wr_mode
)) == NULL
)
3007 if (dup2(p_fd
[1].wr
, 1) == 1)
3010 i
= fcntl(p_fd
[1].rd
, F_GETFD
, 0);
3011 fcntl(p_fd
[1].rd
, F_SETFD
, i
| FD_CLOEXEC
);
3012 if ((p_s
[1] = fdopen(p_fd
[1].rd
, rd_mode
)) == NULL
)
3024 /* - stderr, as required */
3030 if (dup2(p_fd
[2].wr
, 2) == 2)
3033 i
= fcntl(p_fd
[2].rd
, F_GETFD
, 0);
3034 fcntl(p_fd
[2].rd
, F_SETFD
, i
| FD_CLOEXEC
);
3035 if ((p_s
[2] = fdopen(p_fd
[2].rd
, rd_mode
)) == NULL
)
3050 if (dup2(1, 2) != 2)
3058 /* spawn the child process */
3061 pipe_pid
= spawnlp(P_NOWAIT
, shell
, shell
, opt
, cmdstring
, (char *)0);
3068 /* save the PID into the FILE structure
3069 * NOTE: this implementation doesn't actually
3070 * take advantage of this, but do it for
3071 * completeness - AIM Apr01
3073 for (i
= 0; i
< file_count
; i
++)
3074 p_s
[i
]->_pid
= pipe_pid
;
3078 /* reset standard IO to normal */
3079 for (i
= 0; i
< 3; i
++)
3081 dup2(stdio
[i
].handle
, i
);
3082 fcntl(i
, F_SETFD
, stdio
[i
].flags
);
3083 close(stdio
[i
].handle
);
3086 /* if any remnant problems, clean up and bail out */
3089 for (i
= 0; i
< 3; i
++)
3095 return posix_error_with_filename(cmdstring
);
3098 /* build tuple of file objects to return */
3099 if ((p_f
[0] = PyFile_FromFile(p_s
[0], cmdstring
, wr_mode
, _PyPclose
)) != NULL
)
3100 PyFile_SetBufSize(p_f
[0], bufsize
);
3101 if ((p_f
[1] = PyFile_FromFile(p_s
[1], cmdstring
, rd_mode
, _PyPclose
)) != NULL
)
3102 PyFile_SetBufSize(p_f
[1], bufsize
);
3105 if ((p_f
[2] = PyFile_FromFile(p_s
[2], cmdstring
, rd_mode
, _PyPclose
)) != NULL
)
3106 PyFile_SetBufSize(p_f
[0], bufsize
);
3107 f
= PyTuple_Pack(3, p_f
[0], p_f
[1], p_f
[2]);
3110 f
= PyTuple_Pack(2, p_f
[0], p_f
[1]);
3113 * Insert the files we've created into the process dictionary
3114 * all referencing the list with the process handle and the
3115 * initial number of files (see description below in _PyPclose).
3116 * Since if _PyPclose later tried to wait on a process when all
3117 * handles weren't closed, it could create a deadlock with the
3118 * child, we spend some energy here to try to ensure that we
3119 * either insert all file handles into the dictionary or none
3120 * at all. It's a little clumsy with the various popen modes
3121 * and variable number of files involved.
3125 _PyPopenProcs
= PyDict_New();
3130 PyObject
*procObj
, *pidObj
, *intObj
, *fileObj
[3];
3133 fileObj
[0] = fileObj
[1] = fileObj
[2] = NULL
;
3134 ins_rc
[0] = ins_rc
[1] = ins_rc
[2] = 0;
3136 procObj
= PyList_New(2);
3137 pidObj
= PyLong_FromPid(pipe_pid
);
3138 intObj
= PyInt_FromLong((long) file_count
);
3140 if (procObj
&& pidObj
&& intObj
)
3142 PyList_SetItem(procObj
, 0, pidObj
);
3143 PyList_SetItem(procObj
, 1, intObj
);
3145 fileObj
[0] = PyLong_FromVoidPtr(p_s
[0]);
3148 ins_rc
[0] = PyDict_SetItem(_PyPopenProcs
,
3152 fileObj
[1] = PyLong_FromVoidPtr(p_s
[1]);
3155 ins_rc
[1] = PyDict_SetItem(_PyPopenProcs
,
3159 if (file_count
>= 3)
3161 fileObj
[2] = PyLong_FromVoidPtr(p_s
[2]);
3164 ins_rc
[2] = PyDict_SetItem(_PyPopenProcs
,
3170 if (ins_rc
[0] < 0 || !fileObj
[0] ||
3171 ins_rc
[1] < 0 || (file_count
> 1 && !fileObj
[1]) ||
3172 ins_rc
[2] < 0 || (file_count
> 2 && !fileObj
[2]))
3174 /* Something failed - remove any dictionary
3175 * entries that did make it.
3177 if (!ins_rc
[0] && fileObj
[0])
3179 PyDict_DelItem(_PyPopenProcs
,
3182 if (!ins_rc
[1] && fileObj
[1])
3184 PyDict_DelItem(_PyPopenProcs
,
3187 if (!ins_rc
[2] && fileObj
[2])
3189 PyDict_DelItem(_PyPopenProcs
,
3196 * Clean up our localized references for the dictionary keys
3197 * and value since PyDict_SetItem will Py_INCREF any copies
3198 * that got placed in the dictionary.
3200 Py_XDECREF(procObj
);
3201 Py_XDECREF(fileObj
[0]);
3202 Py_XDECREF(fileObj
[1]);
3203 Py_XDECREF(fileObj
[2]);
3206 /* Child is launched. */
3211 * Wrapper for fclose() to use for popen* files, so we can retrieve the
3212 * exit code for the child process and return as a result of the close.
3214 * This function uses the _PyPopenProcs dictionary in order to map the
3215 * input file pointer to information about the process that was
3216 * originally created by the popen* call that created the file pointer.
3217 * The dictionary uses the file pointer as a key (with one entry
3218 * inserted for each file returned by the original popen* call) and a
3219 * single list object as the value for all files from a single call.
3220 * The list object contains the Win32 process handle at [0], and a file
3221 * count at [1], which is initialized to the total number of file
3222 * handles using that list.
3224 * This function closes whichever handle it is passed, and decrements
3225 * the file count in the dictionary for the process handle pointed to
3226 * by this file. On the last close (when the file count reaches zero),
3227 * this function will wait for the child process and then return its
3228 * exit code as the result of the close() operation. This permits the
3229 * files to be closed in any order - it is always the close() of the
3230 * final handle that will return the exit code.
3232 * NOTE: This function is currently called with the GIL released.
3233 * hence we use the GILState API to manage our state.
3236 static int _PyPclose(FILE *file
)
3241 PyObject
*procObj
, *pidObj
, *intObj
, *fileObj
;
3244 PyGILState_STATE state
;
3247 /* Close the file handle first, to ensure it can't block the
3248 * child from exiting if it's the last handle.
3250 result
= fclose(file
);
3253 state
= PyGILState_Ensure();
3257 if ((fileObj
= PyLong_FromVoidPtr(file
)) != NULL
&&
3258 (procObj
= PyDict_GetItem(_PyPopenProcs
,
3259 fileObj
)) != NULL
&&
3260 (pidObj
= PyList_GetItem(procObj
,0)) != NULL
&&
3261 (intObj
= PyList_GetItem(procObj
,1)) != NULL
)
3263 pipe_pid
= (pid_t
) PyLong_AsPid(pidObj
);
3264 file_count
= (int) PyInt_AsLong(intObj
);
3268 /* Still other files referencing process */
3270 PyList_SetItem(procObj
,1,
3271 PyInt_FromLong((long) file_count
));
3275 /* Last file for this process */
3276 if (result
!= EOF
&&
3277 waitpid(pipe_pid
, &exit_code
, 0) == pipe_pid
)
3279 /* extract exit status */
3280 if (WIFEXITED(exit_code
))
3282 result
= WEXITSTATUS(exit_code
);
3292 /* Indicate failure - this will cause the file object
3293 * to raise an I/O error and translate the last
3294 * error code from errno. We do have a problem with
3295 * last errors that overlap the normal errno table,
3296 * but that's a consistent problem with the file object.
3302 /* Remove this file pointer from dictionary */
3303 PyDict_DelItem(_PyPopenProcs
, fileObj
);
3305 if (PyDict_Size(_PyPopenProcs
) == 0)
3307 Py_DECREF(_PyPopenProcs
);
3308 _PyPopenProcs
= NULL
;
3311 } /* if object retrieval ok */
3313 Py_XDECREF(fileObj
);
3314 } /* if _PyPopenProcs */
3317 PyGILState_Release(state
);
3322 #endif /* PYCC_??? */
3324 #elif defined(MS_WINDOWS)
3327 * Portable 'popen' replacement for Win32.
3329 * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks
3330 * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com>
3331 * Return code handling by David Bolen <db3l@fitlinxx.com>.
3338 /* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */
3344 static PyObject
*_PyPopen(char *, int, int);
3345 static int _PyPclose(FILE *file
);
3348 * Internal dictionary mapping popen* file pointers to process handles,
3349 * for use when retrieving the process exit code. See _PyPclose() below
3350 * for more information on this dictionary's use.
3352 static PyObject
*_PyPopenProcs
= NULL
;
3355 /* popen that works from a GUI.
3357 * The result of this function is a pipe (file) connected to the
3358 * processes stdin or stdout, depending on the requested mode.
3362 posix_popen(PyObject
*self
, PyObject
*args
)
3370 if (!PyArg_ParseTuple(args
, "s|si:popen", &cmdstring
, &mode
, &bufsize
))
3375 else if (*mode
!= 'w') {
3376 PyErr_SetString(PyExc_ValueError
, "popen() arg 2 must be 'r' or 'w'");
3381 if (bufsize
!= -1) {
3382 PyErr_SetString(PyExc_ValueError
, "popen() arg 3 must be -1");
3386 if (*(mode
+1) == 't')
3387 f
= _PyPopen(cmdstring
, tm
| _O_TEXT
, POPEN_1
);
3388 else if (*(mode
+1) == 'b')
3389 f
= _PyPopen(cmdstring
, tm
| _O_BINARY
, POPEN_1
);
3391 f
= _PyPopen(cmdstring
, tm
| _O_TEXT
, POPEN_1
);
3396 /* Variation on win32pipe.popen
3398 * The result of this function is a pipe (file) connected to the
3399 * process's stdin, and a pipe connected to the process's stdout.
3403 win32_popen2(PyObject
*self
, PyObject
*args
)
3411 if (!PyArg_ParseTuple(args
, "s|si:popen2", &cmdstring
, &mode
, &bufsize
))
3416 else if (*mode
!= 'b') {
3417 PyErr_SetString(PyExc_ValueError
, "popen2() arg 2 must be 't' or 'b'");
3422 if (bufsize
!= -1) {
3423 PyErr_SetString(PyExc_ValueError
, "popen2() arg 3 must be -1");
3427 f
= _PyPopen(cmdstring
, tm
, POPEN_2
);
3433 * Variation on <om win32pipe.popen>
3435 * The result of this function is 3 pipes - the process's stdin,
3440 win32_popen3(PyObject
*self
, PyObject
*args
)
3448 if (!PyArg_ParseTuple(args
, "s|si:popen3", &cmdstring
, &mode
, &bufsize
))
3453 else if (*mode
!= 'b') {
3454 PyErr_SetString(PyExc_ValueError
, "popen3() arg 2 must be 't' or 'b'");
3459 if (bufsize
!= -1) {
3460 PyErr_SetString(PyExc_ValueError
, "popen3() arg 3 must be -1");
3464 f
= _PyPopen(cmdstring
, tm
, POPEN_3
);
3470 * Variation on win32pipe.popen
3472 * The result of this function is 2 pipes - the processes stdin,
3473 * and stdout+stderr combined as a single pipe.
3477 win32_popen4(PyObject
*self
, PyObject
*args
)
3485 if (!PyArg_ParseTuple(args
, "s|si:popen4", &cmdstring
, &mode
, &bufsize
))
3490 else if (*mode
!= 'b') {
3491 PyErr_SetString(PyExc_ValueError
, "popen4() arg 2 must be 't' or 'b'");
3496 if (bufsize
!= -1) {
3497 PyErr_SetString(PyExc_ValueError
, "popen4() arg 3 must be -1");
3501 f
= _PyPopen(cmdstring
, tm
, POPEN_4
);
3507 _PyPopenCreateProcess(char *cmdstring
,
3513 PROCESS_INFORMATION piProcInfo
;
3514 STARTUPINFO siStartInfo
;
3515 DWORD dwProcessFlags
= 0; /* no NEW_CONSOLE by default for Ctrl+C handling */
3516 char *s1
,*s2
, *s3
= " /c ";
3517 const char *szConsoleSpawn
= "w9xpopen.exe";
3521 if (i
= GetEnvironmentVariable("COMSPEC",NULL
,0)) {
3524 s1
= (char *)alloca(i
);
3525 if (!(x
= GetEnvironmentVariable("COMSPEC", s1
, i
)))
3526 /* x < i, so x fits into an integer */
3529 /* Explicitly check if we are using COMMAND.COM. If we are
3530 * then use the w9xpopen hack.
3533 while (comshell
>= s1
&& *comshell
!= '\\')
3537 if (GetVersion() < 0x80000000 &&
3538 _stricmp(comshell
, "command.com") != 0) {
3539 /* NT/2000 and not using command.com. */
3540 x
= i
+ strlen(s3
) + strlen(cmdstring
) + 1;
3541 s2
= (char *)alloca(x
);
3543 PyOS_snprintf(s2
, x
, "%s%s%s", s1
, s3
, cmdstring
);
3547 * Oh gag, we're on Win9x or using COMMAND.COM. Use
3548 * the workaround listed in KB: Q150956
3550 char modulepath
[_MAX_PATH
];
3551 struct stat statinfo
;
3552 GetModuleFileName(NULL
, modulepath
, sizeof(modulepath
));
3553 for (x
= i
= 0; modulepath
[i
]; i
++)
3554 if (modulepath
[i
] == SEP
)
3556 modulepath
[x
] = '\0';
3557 /* Create the full-name to w9xpopen, so we can test it exists */
3560 (sizeof(modulepath
)/sizeof(modulepath
[0]))
3561 -strlen(modulepath
));
3562 if (stat(modulepath
, &statinfo
) != 0) {
3563 size_t mplen
= sizeof(modulepath
)/sizeof(modulepath
[0]);
3564 /* Eeek - file-not-found - possibly an embedding
3565 situation - see if we can locate it in sys.prefix
3570 modulepath
[mplen
-1] = '\0';
3571 if (modulepath
[strlen(modulepath
)-1] != '\\')
3572 strcat(modulepath
, "\\");
3575 mplen
-strlen(modulepath
));
3576 /* No where else to look - raise an easily identifiable
3577 error, rather than leaving Windows to report
3578 "file not found" - as the user is probably blissfully
3579 unaware this shim EXE is used, and it will confuse them.
3580 (well, it confused me for a while ;-)
3582 if (stat(modulepath
, &statinfo
) != 0) {
3583 PyErr_Format(PyExc_RuntimeError
,
3584 "Can not locate '%s' which is needed "
3585 "for popen to work with your shell "
3591 x
= i
+ strlen(s3
) + strlen(cmdstring
) + 1 +
3592 strlen(modulepath
) +
3593 strlen(szConsoleSpawn
) + 1;
3595 s2
= (char *)alloca(x
);
3597 /* To maintain correct argument passing semantics,
3598 we pass the command-line as it stands, and allow
3599 quoting to be applied. w9xpopen.exe will then
3600 use its argv vector, and re-quote the necessary
3601 args for the ultimate child process.
3610 /* Not passing CREATE_NEW_CONSOLE has been known to
3611 cause random failures on win9x. Specifically a
3613 "Your program accessed mem currently in use at xxx"
3614 and a hopeful warning about the stability of your
3616 Cost is Ctrl+C won't kill children, but anyone
3617 who cares can have a go!
3619 dwProcessFlags
|= CREATE_NEW_CONSOLE
;
3623 /* Could be an else here to try cmd.exe / command.com in the path
3624 Now we'll just error out.. */
3626 PyErr_SetString(PyExc_RuntimeError
,
3627 "Cannot locate a COMSPEC environment variable to "
3628 "use as the shell");
3632 ZeroMemory(&siStartInfo
, sizeof(STARTUPINFO
));
3633 siStartInfo
.cb
= sizeof(STARTUPINFO
);
3634 siStartInfo
.dwFlags
= STARTF_USESTDHANDLES
| STARTF_USESHOWWINDOW
;
3635 siStartInfo
.hStdInput
= hStdin
;
3636 siStartInfo
.hStdOutput
= hStdout
;
3637 siStartInfo
.hStdError
= hStderr
;
3638 siStartInfo
.wShowWindow
= SW_HIDE
;
3640 if (CreateProcess(NULL
,
3650 /* Close the handles now so anyone waiting is woken. */
3651 CloseHandle(piProcInfo
.hThread
);
3653 /* Return process handle */
3654 *hProcess
= piProcInfo
.hProcess
;
3657 win32_error("CreateProcess", s2
);
3661 /* The following code is based off of KB: Q190351 */
3664 _PyPopen(char *cmdstring
, int mode
, int n
)
3666 HANDLE hChildStdinRd
, hChildStdinWr
, hChildStdoutRd
, hChildStdoutWr
,
3667 hChildStderrRd
, hChildStderrWr
, hChildStdinWrDup
, hChildStdoutRdDup
,
3668 hChildStderrRdDup
, hProcess
; /* hChildStdoutWrDup; */
3670 SECURITY_ATTRIBUTES saAttr
;
3677 saAttr
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
3678 saAttr
.bInheritHandle
= TRUE
;
3679 saAttr
.lpSecurityDescriptor
= NULL
;
3681 if (!CreatePipe(&hChildStdinRd
, &hChildStdinWr
, &saAttr
, 0))
3682 return win32_error("CreatePipe", NULL
);
3684 /* Create new output read handle and the input write handle. Set
3685 * the inheritance properties to FALSE. Otherwise, the child inherits
3686 * these handles; resulting in non-closeable handles to the pipes
3688 fSuccess
= DuplicateHandle(GetCurrentProcess(), hChildStdinWr
,
3689 GetCurrentProcess(), &hChildStdinWrDup
, 0,
3691 DUPLICATE_SAME_ACCESS
);
3693 return win32_error("DuplicateHandle", NULL
);
3695 /* Close the inheritable version of ChildStdin
3696 that we're using. */
3697 CloseHandle(hChildStdinWr
);
3699 if (!CreatePipe(&hChildStdoutRd
, &hChildStdoutWr
, &saAttr
, 0))
3700 return win32_error("CreatePipe", NULL
);
3702 fSuccess
= DuplicateHandle(GetCurrentProcess(), hChildStdoutRd
,
3703 GetCurrentProcess(), &hChildStdoutRdDup
, 0,
3704 FALSE
, DUPLICATE_SAME_ACCESS
);
3706 return win32_error("DuplicateHandle", NULL
);
3708 /* Close the inheritable version of ChildStdout
3709 that we're using. */
3710 CloseHandle(hChildStdoutRd
);
3713 if (!CreatePipe(&hChildStderrRd
, &hChildStderrWr
, &saAttr
, 0))
3714 return win32_error("CreatePipe", NULL
);
3715 fSuccess
= DuplicateHandle(GetCurrentProcess(),
3717 GetCurrentProcess(),
3718 &hChildStderrRdDup
, 0,
3719 FALSE
, DUPLICATE_SAME_ACCESS
);
3721 return win32_error("DuplicateHandle", NULL
);
3722 /* Close the inheritable version of ChildStdErr that we're using. */
3723 CloseHandle(hChildStderrRd
);
3728 switch (mode
& (_O_RDONLY
| _O_TEXT
| _O_BINARY
| _O_WRONLY
)) {
3729 case _O_WRONLY
| _O_TEXT
:
3730 /* Case for writing to child Stdin in text mode. */
3731 fd1
= _open_osfhandle((Py_intptr_t
)hChildStdinWrDup
, mode
);
3732 f1
= _fdopen(fd1
, "w");
3733 f
= PyFile_FromFile(f1
, cmdstring
, "w", _PyPclose
);
3734 PyFile_SetBufSize(f
, 0);
3735 /* We don't care about these pipes anymore, so close them. */
3736 CloseHandle(hChildStdoutRdDup
);
3737 CloseHandle(hChildStderrRdDup
);
3740 case _O_RDONLY
| _O_TEXT
:
3741 /* Case for reading from child Stdout in text mode. */
3742 fd1
= _open_osfhandle((Py_intptr_t
)hChildStdoutRdDup
, mode
);
3743 f1
= _fdopen(fd1
, "r");
3744 f
= PyFile_FromFile(f1
, cmdstring
, "r", _PyPclose
);
3745 PyFile_SetBufSize(f
, 0);
3746 /* We don't care about these pipes anymore, so close them. */
3747 CloseHandle(hChildStdinWrDup
);
3748 CloseHandle(hChildStderrRdDup
);
3751 case _O_RDONLY
| _O_BINARY
:
3752 /* Case for readinig from child Stdout in binary mode. */
3753 fd1
= _open_osfhandle((Py_intptr_t
)hChildStdoutRdDup
, mode
);
3754 f1
= _fdopen(fd1
, "rb");
3755 f
= PyFile_FromFile(f1
, cmdstring
, "rb", _PyPclose
);
3756 PyFile_SetBufSize(f
, 0);
3757 /* We don't care about these pipes anymore, so close them. */
3758 CloseHandle(hChildStdinWrDup
);
3759 CloseHandle(hChildStderrRdDup
);
3762 case _O_WRONLY
| _O_BINARY
:
3763 /* Case for writing to child Stdin in binary mode. */
3764 fd1
= _open_osfhandle((Py_intptr_t
)hChildStdinWrDup
, mode
);
3765 f1
= _fdopen(fd1
, "wb");
3766 f
= PyFile_FromFile(f1
, cmdstring
, "wb", _PyPclose
);
3767 PyFile_SetBufSize(f
, 0);
3768 /* We don't care about these pipes anymore, so close them. */
3769 CloseHandle(hChildStdoutRdDup
);
3770 CloseHandle(hChildStderrRdDup
);
3782 if (mode
& _O_TEXT
) {
3790 fd1
= _open_osfhandle((Py_intptr_t
)hChildStdinWrDup
, mode
);
3791 f1
= _fdopen(fd1
, m2
);
3792 fd2
= _open_osfhandle((Py_intptr_t
)hChildStdoutRdDup
, mode
);
3793 f2
= _fdopen(fd2
, m1
);
3794 p1
= PyFile_FromFile(f1
, cmdstring
, m2
, _PyPclose
);
3795 PyFile_SetBufSize(p1
, 0);
3796 p2
= PyFile_FromFile(f2
, cmdstring
, m1
, _PyPclose
);
3797 PyFile_SetBufSize(p2
, 0);
3800 CloseHandle(hChildStderrRdDup
);
3802 f
= PyTuple_Pack(2,p1
,p2
);
3812 PyObject
*p1
, *p2
, *p3
;
3814 if (mode
& _O_TEXT
) {
3822 fd1
= _open_osfhandle((Py_intptr_t
)hChildStdinWrDup
, mode
);
3823 f1
= _fdopen(fd1
, m2
);
3824 fd2
= _open_osfhandle((Py_intptr_t
)hChildStdoutRdDup
, mode
);
3825 f2
= _fdopen(fd2
, m1
);
3826 fd3
= _open_osfhandle((Py_intptr_t
)hChildStderrRdDup
, mode
);
3827 f3
= _fdopen(fd3
, m1
);
3828 p1
= PyFile_FromFile(f1
, cmdstring
, m2
, _PyPclose
);
3829 p2
= PyFile_FromFile(f2
, cmdstring
, m1
, _PyPclose
);
3830 p3
= PyFile_FromFile(f3
, cmdstring
, m1
, _PyPclose
);
3831 PyFile_SetBufSize(p1
, 0);
3832 PyFile_SetBufSize(p2
, 0);
3833 PyFile_SetBufSize(p3
, 0);
3834 f
= PyTuple_Pack(3,p1
,p2
,p3
);
3844 if (!_PyPopenCreateProcess(cmdstring
,
3852 if (!_PyPopenCreateProcess(cmdstring
,
3861 * Insert the files we've created into the process dictionary
3862 * all referencing the list with the process handle and the
3863 * initial number of files (see description below in _PyPclose).
3864 * Since if _PyPclose later tried to wait on a process when all
3865 * handles weren't closed, it could create a deadlock with the
3866 * child, we spend some energy here to try to ensure that we
3867 * either insert all file handles into the dictionary or none
3868 * at all. It's a little clumsy with the various popen modes
3869 * and variable number of files involved.
3871 if (!_PyPopenProcs
) {
3872 _PyPopenProcs
= PyDict_New();
3875 if (_PyPopenProcs
) {
3876 PyObject
*procObj
, *hProcessObj
, *intObj
, *fileObj
[3];
3879 fileObj
[0] = fileObj
[1] = fileObj
[2] = NULL
;
3880 ins_rc
[0] = ins_rc
[1] = ins_rc
[2] = 0;
3882 procObj
= PyList_New(2);
3883 hProcessObj
= PyLong_FromVoidPtr(hProcess
);
3884 intObj
= PyInt_FromLong(file_count
);
3886 if (procObj
&& hProcessObj
&& intObj
) {
3887 PyList_SetItem(procObj
,0,hProcessObj
);
3888 PyList_SetItem(procObj
,1,intObj
);
3890 fileObj
[0] = PyLong_FromVoidPtr(f1
);
3892 ins_rc
[0] = PyDict_SetItem(_PyPopenProcs
,
3896 if (file_count
>= 2) {
3897 fileObj
[1] = PyLong_FromVoidPtr(f2
);
3899 ins_rc
[1] = PyDict_SetItem(_PyPopenProcs
,
3904 if (file_count
>= 3) {
3905 fileObj
[2] = PyLong_FromVoidPtr(f3
);
3907 ins_rc
[2] = PyDict_SetItem(_PyPopenProcs
,
3913 if (ins_rc
[0] < 0 || !fileObj
[0] ||
3914 ins_rc
[1] < 0 || (file_count
> 1 && !fileObj
[1]) ||
3915 ins_rc
[2] < 0 || (file_count
> 2 && !fileObj
[2])) {
3916 /* Something failed - remove any dictionary
3917 * entries that did make it.
3919 if (!ins_rc
[0] && fileObj
[0]) {
3920 PyDict_DelItem(_PyPopenProcs
,
3923 if (!ins_rc
[1] && fileObj
[1]) {
3924 PyDict_DelItem(_PyPopenProcs
,
3927 if (!ins_rc
[2] && fileObj
[2]) {
3928 PyDict_DelItem(_PyPopenProcs
,
3935 * Clean up our localized references for the dictionary keys
3936 * and value since PyDict_SetItem will Py_INCREF any copies
3937 * that got placed in the dictionary.
3939 Py_XDECREF(procObj
);
3940 Py_XDECREF(fileObj
[0]);
3941 Py_XDECREF(fileObj
[1]);
3942 Py_XDECREF(fileObj
[2]);
3945 /* Child is launched. Close the parents copy of those pipe
3946 * handles that only the child should have open. You need to
3947 * make sure that no handles to the write end of the output pipe
3948 * are maintained in this process or else the pipe will not close
3949 * when the child process exits and the ReadFile will hang. */
3951 if (!CloseHandle(hChildStdinRd
))
3952 return win32_error("CloseHandle", NULL
);
3954 if (!CloseHandle(hChildStdoutWr
))
3955 return win32_error("CloseHandle", NULL
);
3957 if ((n
!= 4) && (!CloseHandle(hChildStderrWr
)))
3958 return win32_error("CloseHandle", NULL
);
3964 * Wrapper for fclose() to use for popen* files, so we can retrieve the
3965 * exit code for the child process and return as a result of the close.
3967 * This function uses the _PyPopenProcs dictionary in order to map the
3968 * input file pointer to information about the process that was
3969 * originally created by the popen* call that created the file pointer.
3970 * The dictionary uses the file pointer as a key (with one entry
3971 * inserted for each file returned by the original popen* call) and a
3972 * single list object as the value for all files from a single call.
3973 * The list object contains the Win32 process handle at [0], and a file
3974 * count at [1], which is initialized to the total number of file
3975 * handles using that list.
3977 * This function closes whichever handle it is passed, and decrements
3978 * the file count in the dictionary for the process handle pointed to
3979 * by this file. On the last close (when the file count reaches zero),
3980 * this function will wait for the child process and then return its
3981 * exit code as the result of the close() operation. This permits the
3982 * files to be closed in any order - it is always the close() of the
3983 * final handle that will return the exit code.
3985 * NOTE: This function is currently called with the GIL released.
3986 * hence we use the GILState API to manage our state.
3989 static int _PyPclose(FILE *file
)
3994 PyObject
*procObj
, *hProcessObj
, *intObj
, *fileObj
;
3997 PyGILState_STATE state
;
4000 /* Close the file handle first, to ensure it can't block the
4001 * child from exiting if it's the last handle.
4003 result
= fclose(file
);
4005 state
= PyGILState_Ensure();
4007 if (_PyPopenProcs
) {
4008 if ((fileObj
= PyLong_FromVoidPtr(file
)) != NULL
&&
4009 (procObj
= PyDict_GetItem(_PyPopenProcs
,
4010 fileObj
)) != NULL
&&
4011 (hProcessObj
= PyList_GetItem(procObj
,0)) != NULL
&&
4012 (intObj
= PyList_GetItem(procObj
,1)) != NULL
) {
4014 hProcess
= PyLong_AsVoidPtr(hProcessObj
);
4015 file_count
= PyInt_AsLong(intObj
);
4017 if (file_count
> 1) {
4018 /* Still other files referencing process */
4020 PyList_SetItem(procObj
,1,
4021 PyInt_FromLong(file_count
));
4023 /* Last file for this process */
4024 if (result
!= EOF
&&
4025 WaitForSingleObject(hProcess
, INFINITE
) != WAIT_FAILED
&&
4026 GetExitCodeProcess(hProcess
, &exit_code
)) {
4027 /* Possible truncation here in 16-bit environments, but
4028 * real exit codes are just the lower byte in any event.
4032 /* Indicate failure - this will cause the file object
4033 * to raise an I/O error and translate the last Win32
4034 * error code from errno. We do have a problem with
4035 * last errors that overlap the normal errno table,
4036 * but that's a consistent problem with the file object.
4038 if (result
!= EOF
) {
4039 /* If the error wasn't from the fclose(), then
4040 * set errno for the file object error handling.
4042 errno
= GetLastError();
4047 /* Free up the native handle at this point */
4048 CloseHandle(hProcess
);
4051 /* Remove this file pointer from dictionary */
4052 PyDict_DelItem(_PyPopenProcs
, fileObj
);
4054 if (PyDict_Size(_PyPopenProcs
) == 0) {
4055 Py_DECREF(_PyPopenProcs
);
4056 _PyPopenProcs
= NULL
;
4059 } /* if object retrieval ok */
4061 Py_XDECREF(fileObj
);
4062 } /* if _PyPopenProcs */
4065 PyGILState_Release(state
);
4070 #else /* which OS? */
4072 posix_popen(PyObject
*self
, PyObject
*args
)
4079 if (!PyArg_ParseTuple(args
, "s|si:popen", &name
, &mode
, &bufsize
))
4081 /* Strip mode of binary or text modifiers */
4082 if (strcmp(mode
, "rb") == 0 || strcmp(mode
, "rt") == 0)
4084 else if (strcmp(mode
, "wb") == 0 || strcmp(mode
, "wt") == 0)
4086 Py_BEGIN_ALLOW_THREADS
4087 fp
= popen(name
, mode
);
4088 Py_END_ALLOW_THREADS
4090 return posix_error();
4091 f
= PyFile_FromFile(fp
, name
, mode
, pclose
);
4093 PyFile_SetBufSize(f
, bufsize
);
4097 #endif /* PYOS_??? */
4098 #endif /* HAVE_POPEN */
4102 PyDoc_STRVAR(posix_setuid__doc__
,
4104 Set the current process's user id.");
4107 posix_setuid(PyObject
*self
, PyObject
*args
)
4111 if (!PyArg_ParseTuple(args
, "l:setuid", &uid_arg
))
4114 if (uid
!= uid_arg
) {
4115 PyErr_SetString(PyExc_OverflowError
, "user id too big");
4118 if (setuid(uid
) < 0)
4119 return posix_error();
4123 #endif /* HAVE_SETUID */
4127 PyDoc_STRVAR(posix_seteuid__doc__
,
4129 Set the current process's effective user id.");
4132 posix_seteuid (PyObject
*self
, PyObject
*args
)
4136 if (!PyArg_ParseTuple(args
, "l", &euid_arg
))
4139 if (euid
!= euid_arg
) {
4140 PyErr_SetString(PyExc_OverflowError
, "user id too big");
4143 if (seteuid(euid
) < 0) {
4144 return posix_error();
4150 #endif /* HAVE_SETEUID */
4153 PyDoc_STRVAR(posix_setegid__doc__
,
4155 Set the current process's effective group id.");
4158 posix_setegid (PyObject
*self
, PyObject
*args
)
4162 if (!PyArg_ParseTuple(args
, "l", &egid_arg
))
4165 if (egid
!= egid_arg
) {
4166 PyErr_SetString(PyExc_OverflowError
, "group id too big");
4169 if (setegid(egid
) < 0) {
4170 return posix_error();
4176 #endif /* HAVE_SETEGID */
4178 #ifdef HAVE_SETREUID
4179 PyDoc_STRVAR(posix_setreuid__doc__
,
4180 "setreuid(ruid, euid)\n\n\
4181 Set the current process's real and effective user ids.");
4184 posix_setreuid (PyObject
*self
, PyObject
*args
)
4186 long ruid_arg
, euid_arg
;
4188 if (!PyArg_ParseTuple(args
, "ll", &ruid_arg
, &euid_arg
))
4191 ruid
= (uid_t
)-1; /* let the compiler choose how -1 fits */
4193 ruid
= ruid_arg
; /* otherwise, assign from our long */
4198 if ((euid_arg
!= -1 && euid
!= euid_arg
) ||
4199 (ruid_arg
!= -1 && ruid
!= ruid_arg
)) {
4200 PyErr_SetString(PyExc_OverflowError
, "user id too big");
4203 if (setreuid(ruid
, euid
) < 0) {
4204 return posix_error();
4210 #endif /* HAVE_SETREUID */
4212 #ifdef HAVE_SETREGID
4213 PyDoc_STRVAR(posix_setregid__doc__
,
4214 "setregid(rgid, egid)\n\n\
4215 Set the current process's real and effective group ids.");
4218 posix_setregid (PyObject
*self
, PyObject
*args
)
4220 long rgid_arg
, egid_arg
;
4222 if (!PyArg_ParseTuple(args
, "ll", &rgid_arg
, &egid_arg
))
4225 rgid
= (gid_t
)-1; /* let the compiler choose how -1 fits */
4227 rgid
= rgid_arg
; /* otherwise, assign from our long */
4232 if ((egid_arg
!= -1 && egid
!= egid_arg
) ||
4233 (rgid_arg
!= -1 && rgid
!= rgid_arg
)) {
4234 PyErr_SetString(PyExc_OverflowError
, "group id too big");
4237 if (setregid(rgid
, egid
) < 0) {
4238 return posix_error();
4244 #endif /* HAVE_SETREGID */
4247 PyDoc_STRVAR(posix_setgid__doc__
,
4249 Set the current process's group id.");
4252 posix_setgid(PyObject
*self
, PyObject
*args
)
4256 if (!PyArg_ParseTuple(args
, "l:setgid", &gid_arg
))
4259 if (gid
!= gid_arg
) {
4260 PyErr_SetString(PyExc_OverflowError
, "group id too big");
4263 if (setgid(gid
) < 0)
4264 return posix_error();
4268 #endif /* HAVE_SETGID */
4270 #ifdef HAVE_SETGROUPS
4271 PyDoc_STRVAR(posix_setgroups__doc__
,
4272 "setgroups(list)\n\n\
4273 Set the groups of the current process to list.");
4276 posix_setgroups(PyObject
*self
, PyObject
*groups
)
4279 gid_t grouplist
[MAX_GROUPS
];
4281 if (!PySequence_Check(groups
)) {
4282 PyErr_SetString(PyExc_TypeError
, "setgroups argument must be a sequence");
4285 len
= PySequence_Size(groups
);
4286 if (len
> MAX_GROUPS
) {
4287 PyErr_SetString(PyExc_ValueError
, "too many groups");
4290 for(i
= 0; i
< len
; i
++) {
4292 elem
= PySequence_GetItem(groups
, i
);
4295 if (!PyInt_Check(elem
)) {
4296 if (!PyLong_Check(elem
)) {
4297 PyErr_SetString(PyExc_TypeError
,
4298 "groups must be integers");
4302 unsigned long x
= PyLong_AsUnsignedLong(elem
);
4303 if (PyErr_Occurred()) {
4304 PyErr_SetString(PyExc_TypeError
,
4305 "group id too big");
4310 /* read back to see if it fits in gid_t */
4311 if (grouplist
[i
] != x
) {
4312 PyErr_SetString(PyExc_TypeError
,
4313 "group id too big");
4319 long x
= PyInt_AsLong(elem
);
4321 if (grouplist
[i
] != x
) {
4322 PyErr_SetString(PyExc_TypeError
,
4323 "group id too big");
4331 if (setgroups(len
, grouplist
) < 0)
4332 return posix_error();
4336 #endif /* HAVE_SETGROUPS */
4338 #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
4340 wait_helper(pid_t pid
, int status
, struct rusage
*ru
)
4343 static PyObject
*struct_rusage
;
4346 return posix_error();
4348 if (struct_rusage
== NULL
) {
4349 PyObject
*m
= PyImport_ImportModuleNoBlock("resource");
4352 struct_rusage
= PyObject_GetAttrString(m
, "struct_rusage");
4354 if (struct_rusage
== NULL
)
4358 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
4359 result
= PyStructSequence_New((PyTypeObject
*) struct_rusage
);
4364 #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
4367 PyStructSequence_SET_ITEM(result
, 0,
4368 PyFloat_FromDouble(doubletime(ru
->ru_utime
)));
4369 PyStructSequence_SET_ITEM(result
, 1,
4370 PyFloat_FromDouble(doubletime(ru
->ru_stime
)));
4371 #define SET_INT(result, index, value)\
4372 PyStructSequence_SET_ITEM(result, index, PyInt_FromLong(value))
4373 SET_INT(result
, 2, ru
->ru_maxrss
);
4374 SET_INT(result
, 3, ru
->ru_ixrss
);
4375 SET_INT(result
, 4, ru
->ru_idrss
);
4376 SET_INT(result
, 5, ru
->ru_isrss
);
4377 SET_INT(result
, 6, ru
->ru_minflt
);
4378 SET_INT(result
, 7, ru
->ru_majflt
);
4379 SET_INT(result
, 8, ru
->ru_nswap
);
4380 SET_INT(result
, 9, ru
->ru_inblock
);
4381 SET_INT(result
, 10, ru
->ru_oublock
);
4382 SET_INT(result
, 11, ru
->ru_msgsnd
);
4383 SET_INT(result
, 12, ru
->ru_msgrcv
);
4384 SET_INT(result
, 13, ru
->ru_nsignals
);
4385 SET_INT(result
, 14, ru
->ru_nvcsw
);
4386 SET_INT(result
, 15, ru
->ru_nivcsw
);
4389 if (PyErr_Occurred()) {
4394 return Py_BuildValue("NiN", PyLong_FromPid(pid
), status
, result
);
4396 #endif /* HAVE_WAIT3 || HAVE_WAIT4 */
4399 PyDoc_STRVAR(posix_wait3__doc__
,
4400 "wait3(options) -> (pid, status, rusage)\n\n\
4401 Wait for completion of a child process.");
4404 posix_wait3(PyObject
*self
, PyObject
*args
)
4410 WAIT_STATUS_INT(status
) = 0;
4412 if (!PyArg_ParseTuple(args
, "i:wait3", &options
))
4415 Py_BEGIN_ALLOW_THREADS
4416 pid
= wait3(&status
, options
, &ru
);
4417 Py_END_ALLOW_THREADS
4419 return wait_helper(pid
, WAIT_STATUS_INT(status
), &ru
);
4421 #endif /* HAVE_WAIT3 */
4424 PyDoc_STRVAR(posix_wait4__doc__
,
4425 "wait4(pid, options) -> (pid, status, rusage)\n\n\
4426 Wait for completion of a given child process.");
4429 posix_wait4(PyObject
*self
, PyObject
*args
)
4435 WAIT_STATUS_INT(status
) = 0;
4437 if (!PyArg_ParseTuple(args
, PARSE_PID
"i:wait4", &pid
, &options
))
4440 Py_BEGIN_ALLOW_THREADS
4441 pid
= wait4(pid
, &status
, options
, &ru
);
4442 Py_END_ALLOW_THREADS
4444 return wait_helper(pid
, WAIT_STATUS_INT(status
), &ru
);
4446 #endif /* HAVE_WAIT4 */
4449 PyDoc_STRVAR(posix_waitpid__doc__
,
4450 "waitpid(pid, options) -> (pid, status)\n\n\
4451 Wait for completion of a given child process.");
4454 posix_waitpid(PyObject
*self
, PyObject
*args
)
4459 WAIT_STATUS_INT(status
) = 0;
4461 if (!PyArg_ParseTuple(args
, PARSE_PID
"i:waitpid", &pid
, &options
))
4463 Py_BEGIN_ALLOW_THREADS
4464 pid
= waitpid(pid
, &status
, options
);
4465 Py_END_ALLOW_THREADS
4467 return posix_error();
4469 return Py_BuildValue("Ni", PyLong_FromPid(pid
), WAIT_STATUS_INT(status
));
4472 #elif defined(HAVE_CWAIT)
4474 /* MS C has a variant of waitpid() that's usable for most purposes. */
4475 PyDoc_STRVAR(posix_waitpid__doc__
,
4476 "waitpid(pid, options) -> (pid, status << 8)\n\n"
4477 "Wait for completion of a given process. options is ignored on Windows.");
4480 posix_waitpid(PyObject
*self
, PyObject
*args
)
4483 int status
, options
;
4485 if (!PyArg_ParseTuple(args
, PARSE_PID
"i:waitpid", &pid
, &options
))
4487 Py_BEGIN_ALLOW_THREADS
4488 pid
= _cwait(&status
, pid
, options
);
4489 Py_END_ALLOW_THREADS
4491 return posix_error();
4493 /* shift the status left a byte so this is more like the POSIX waitpid */
4494 return Py_BuildValue("Ni", PyLong_FromPid(pid
), status
<< 8);
4496 #endif /* HAVE_WAITPID || HAVE_CWAIT */
4499 PyDoc_STRVAR(posix_wait__doc__
,
4500 "wait() -> (pid, status)\n\n\
4501 Wait for completion of a child process.");
4504 posix_wait(PyObject
*self
, PyObject
*noargs
)
4508 WAIT_STATUS_INT(status
) = 0;
4510 Py_BEGIN_ALLOW_THREADS
4511 pid
= wait(&status
);
4512 Py_END_ALLOW_THREADS
4514 return posix_error();
4516 return Py_BuildValue("Ni", PyLong_FromPid(pid
), WAIT_STATUS_INT(status
));
4521 PyDoc_STRVAR(posix_lstat__doc__
,
4522 "lstat(path) -> stat result\n\n\
4523 Like stat(path), but do not follow symbolic links.");
4526 posix_lstat(PyObject
*self
, PyObject
*args
)
4529 return posix_do_stat(self
, args
, "et:lstat", lstat
, NULL
, NULL
);
4530 #else /* !HAVE_LSTAT */
4531 return posix_do_stat(self
, args
, "et:lstat", STAT
, NULL
, NULL
);
4532 #endif /* !HAVE_LSTAT */
4536 #ifdef HAVE_READLINK
4537 PyDoc_STRVAR(posix_readlink__doc__
,
4538 "readlink(path) -> path\n\n\
4539 Return a string representing the path to which the symbolic link points.");
4542 posix_readlink(PyObject
*self
, PyObject
*args
)
4545 char buf
[MAXPATHLEN
];
4548 #ifdef Py_USING_UNICODE
4549 int arg_is_unicode
= 0;
4552 if (!PyArg_ParseTuple(args
, "et:readlink",
4553 Py_FileSystemDefaultEncoding
, &path
))
4555 #ifdef Py_USING_UNICODE
4556 v
= PySequence_GetItem(args
, 0);
4562 if (PyUnicode_Check(v
)) {
4568 Py_BEGIN_ALLOW_THREADS
4569 n
= readlink(path
, buf
, (int) sizeof buf
);
4570 Py_END_ALLOW_THREADS
4572 return posix_error_with_allocated_filename(path
);
4575 v
= PyString_FromStringAndSize(buf
, n
);
4576 #ifdef Py_USING_UNICODE
4577 if (arg_is_unicode
) {
4580 w
= PyUnicode_FromEncodedObject(v
,
4581 Py_FileSystemDefaultEncoding
,
4588 /* fall back to the original byte string, as
4589 discussed in patch #683592 */
4596 #endif /* HAVE_READLINK */
4600 PyDoc_STRVAR(posix_symlink__doc__
,
4601 "symlink(src, dst)\n\n\
4602 Create a symbolic link pointing to src named dst.");
4605 posix_symlink(PyObject
*self
, PyObject
*args
)
4607 return posix_2str(args
, "etet:symlink", symlink
);
4609 #endif /* HAVE_SYMLINK */
4613 #if defined(PYCC_VACPP) && defined(PYOS_OS2)
4619 Py_BEGIN_ALLOW_THREADS
4620 DosQuerySysInfo(QSV_MS_COUNT
, QSV_MS_COUNT
, &value
, sizeof(value
));
4621 Py_END_ALLOW_THREADS
4627 posix_times(PyObject
*self
, PyObject
*noargs
)
4629 /* Currently Only Uptime is Provided -- Others Later */
4630 return Py_BuildValue("ddddd",
4631 (double)0 /* t.tms_utime / HZ */,
4632 (double)0 /* t.tms_stime / HZ */,
4633 (double)0 /* t.tms_cutime / HZ */,
4634 (double)0 /* t.tms_cstime / HZ */,
4635 (double)system_uptime() / 1000);
4638 #define NEED_TICKS_PER_SECOND
4639 static long ticks_per_second
= -1;
4641 posix_times(PyObject
*self
, PyObject
*noargs
)
4647 if (c
== (clock_t) -1)
4648 return posix_error();
4649 return Py_BuildValue("ddddd",
4650 (double)t
.tms_utime
/ ticks_per_second
,
4651 (double)t
.tms_stime
/ ticks_per_second
,
4652 (double)t
.tms_cutime
/ ticks_per_second
,
4653 (double)t
.tms_cstime
/ ticks_per_second
,
4654 (double)c
/ ticks_per_second
);
4656 #endif /* not OS2 */
4657 #endif /* HAVE_TIMES */
4661 PyDoc_STRVAR(posix_times__doc__
,
4662 "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
4663 Return a tuple of floating point numbers indicating process times.");
4668 PyDoc_STRVAR(posix_getsid__doc__
,
4669 "getsid(pid) -> sid\n\n\
4670 Call the system call getsid().");
4673 posix_getsid(PyObject
*self
, PyObject
*args
)
4677 if (!PyArg_ParseTuple(args
, PARSE_PID
":getsid", &pid
))
4681 return posix_error();
4682 return PyInt_FromLong((long)sid
);
4684 #endif /* HAVE_GETSID */
4688 PyDoc_STRVAR(posix_setsid__doc__
,
4690 Call the system call setsid().");
4693 posix_setsid(PyObject
*self
, PyObject
*noargs
)
4696 return posix_error();
4700 #endif /* HAVE_SETSID */
4703 PyDoc_STRVAR(posix_setpgid__doc__
,
4704 "setpgid(pid, pgrp)\n\n\
4705 Call the system call setpgid().");
4708 posix_setpgid(PyObject
*self
, PyObject
*args
)
4712 if (!PyArg_ParseTuple(args
, PARSE_PID
"i:setpgid", &pid
, &pgrp
))
4714 if (setpgid(pid
, pgrp
) < 0)
4715 return posix_error();
4719 #endif /* HAVE_SETPGID */
4722 #ifdef HAVE_TCGETPGRP
4723 PyDoc_STRVAR(posix_tcgetpgrp__doc__
,
4724 "tcgetpgrp(fd) -> pgid\n\n\
4725 Return the process group associated with the terminal given by a fd.");
4728 posix_tcgetpgrp(PyObject
*self
, PyObject
*args
)
4732 if (!PyArg_ParseTuple(args
, "i:tcgetpgrp", &fd
))
4734 pgid
= tcgetpgrp(fd
);
4736 return posix_error();
4737 return PyLong_FromPid(pgid
);
4739 #endif /* HAVE_TCGETPGRP */
4742 #ifdef HAVE_TCSETPGRP
4743 PyDoc_STRVAR(posix_tcsetpgrp__doc__
,
4744 "tcsetpgrp(fd, pgid)\n\n\
4745 Set the process group associated with the terminal given by a fd.");
4748 posix_tcsetpgrp(PyObject
*self
, PyObject
*args
)
4752 if (!PyArg_ParseTuple(args
, "i" PARSE_PID
":tcsetpgrp", &fd
, &pgid
))
4754 if (tcsetpgrp(fd
, pgid
) < 0)
4755 return posix_error();
4759 #endif /* HAVE_TCSETPGRP */
4761 /* Functions acting on file descriptors */
4763 PyDoc_STRVAR(posix_open__doc__
,
4764 "open(filename, flag [, mode=0777]) -> fd\n\n\
4765 Open a file (for low level IO).");
4768 posix_open(PyObject
*self
, PyObject
*args
)
4775 if (!PyArg_ParseTuple(args
, "eti|i",
4776 Py_FileSystemDefaultEncoding
, &file
,
4780 Py_BEGIN_ALLOW_THREADS
4781 fd
= open(file
, flag
, mode
);
4782 Py_END_ALLOW_THREADS
4784 return posix_error_with_allocated_filename(file
);
4786 return PyInt_FromLong((long)fd
);
4790 PyDoc_STRVAR(posix_close__doc__
,
4792 Close a file descriptor (for low level IO).");
4795 posix_close(PyObject
*self
, PyObject
*args
)
4798 if (!PyArg_ParseTuple(args
, "i:close", &fd
))
4800 if (!_PyVerify_fd(fd
))
4801 return posix_error();
4802 Py_BEGIN_ALLOW_THREADS
4804 Py_END_ALLOW_THREADS
4806 return posix_error();
4812 PyDoc_STRVAR(posix_closerange__doc__
,
4813 "closerange(fd_low, fd_high)\n\n\
4814 Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
4817 posix_closerange(PyObject
*self
, PyObject
*args
)
4819 int fd_from
, fd_to
, i
;
4820 if (!PyArg_ParseTuple(args
, "ii:closerange", &fd_from
, &fd_to
))
4822 Py_BEGIN_ALLOW_THREADS
4823 for (i
= fd_from
; i
< fd_to
; i
++)
4824 if (_PyVerify_fd(i
))
4826 Py_END_ALLOW_THREADS
4831 PyDoc_STRVAR(posix_dup__doc__
,
4832 "dup(fd) -> fd2\n\n\
4833 Return a duplicate of a file descriptor.");
4836 posix_dup(PyObject
*self
, PyObject
*args
)
4839 if (!PyArg_ParseTuple(args
, "i:dup", &fd
))
4841 if (!_PyVerify_fd(fd
))
4842 return posix_error();
4843 Py_BEGIN_ALLOW_THREADS
4845 Py_END_ALLOW_THREADS
4847 return posix_error();
4848 return PyInt_FromLong((long)fd
);
4852 PyDoc_STRVAR(posix_dup2__doc__
,
4853 "dup2(old_fd, new_fd)\n\n\
4854 Duplicate file descriptor.");
4857 posix_dup2(PyObject
*self
, PyObject
*args
)
4860 if (!PyArg_ParseTuple(args
, "ii:dup2", &fd
, &fd2
))
4862 if (!_PyVerify_fd_dup2(fd
, fd2
))
4863 return posix_error();
4864 Py_BEGIN_ALLOW_THREADS
4865 res
= dup2(fd
, fd2
);
4866 Py_END_ALLOW_THREADS
4868 return posix_error();
4874 PyDoc_STRVAR(posix_lseek__doc__
,
4875 "lseek(fd, pos, how) -> newpos\n\n\
4876 Set the current position of a file descriptor.");
4879 posix_lseek(PyObject
*self
, PyObject
*args
)
4884 if (!PyArg_ParseTuple(args
, "iOi:lseek", &fd
, &posobj
, &how
))
4887 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
4889 case 0: how
= SEEK_SET
; break;
4890 case 1: how
= SEEK_CUR
; break;
4891 case 2: how
= SEEK_END
; break;
4893 #endif /* SEEK_END */
4895 #if !defined(HAVE_LARGEFILE_SUPPORT)
4896 pos
= PyInt_AsLong(posobj
);
4898 pos
= PyLong_Check(posobj
) ?
4899 PyLong_AsLongLong(posobj
) : PyInt_AsLong(posobj
);
4901 if (PyErr_Occurred())
4904 if (!_PyVerify_fd(fd
))
4905 return posix_error();
4906 Py_BEGIN_ALLOW_THREADS
4907 res
= lseek(fd
, pos
, how
);
4908 Py_END_ALLOW_THREADS
4910 return posix_error();
4912 #if !defined(HAVE_LARGEFILE_SUPPORT)
4913 return PyInt_FromLong(res
);
4915 return PyLong_FromLongLong(res
);
4920 PyDoc_STRVAR(posix_read__doc__
,
4921 "read(fd, buffersize) -> string\n\n\
4922 Read a file descriptor.");
4925 posix_read(PyObject
*self
, PyObject
*args
)
4929 if (!PyArg_ParseTuple(args
, "ii:read", &fd
, &size
))
4933 return posix_error();
4935 buffer
= PyString_FromStringAndSize((char *)NULL
, size
);
4938 if (!_PyVerify_fd(fd
)) {
4940 return posix_error();
4942 Py_BEGIN_ALLOW_THREADS
4943 n
= read(fd
, PyString_AsString(buffer
), size
);
4944 Py_END_ALLOW_THREADS
4947 return posix_error();
4950 _PyString_Resize(&buffer
, n
);
4955 PyDoc_STRVAR(posix_write__doc__
,
4956 "write(fd, string) -> byteswritten\n\n\
4957 Write a string to a file descriptor.");
4960 posix_write(PyObject
*self
, PyObject
*args
)
4966 if (!PyArg_ParseTuple(args
, "is*:write", &fd
, &pbuf
))
4968 if (!_PyVerify_fd(fd
)) {
4969 PyBuffer_Release(&pbuf
);
4970 return posix_error();
4972 Py_BEGIN_ALLOW_THREADS
4973 size
= write(fd
, pbuf
.buf
, (size_t)pbuf
.len
);
4974 Py_END_ALLOW_THREADS
4975 PyBuffer_Release(&pbuf
);
4977 return posix_error();
4978 return PyInt_FromSsize_t(size
);
4982 PyDoc_STRVAR(posix_fstat__doc__
,
4983 "fstat(fd) -> stat result\n\n\
4984 Like stat(), but for an open file descriptor.");
4987 posix_fstat(PyObject
*self
, PyObject
*args
)
4992 if (!PyArg_ParseTuple(args
, "i:fstat", &fd
))
4994 if (!_PyVerify_fd(fd
))
4995 return posix_error();
4996 Py_BEGIN_ALLOW_THREADS
4997 res
= FSTAT(fd
, &st
);
4998 Py_END_ALLOW_THREADS
5000 return posix_error();
5003 return _pystat_fromstructstat(&st
);
5007 PyDoc_STRVAR(posix_fdopen__doc__
,
5008 "fdopen(fd [, mode='r' [, bufsize]]) -> file_object\n\n\
5009 Return an open file object connected to a file descriptor.");
5012 posix_fdopen(PyObject
*self
, PyObject
*args
)
5015 char *orgmode
= "r";
5020 if (!PyArg_ParseTuple(args
, "i|si", &fd
, &orgmode
, &bufsize
))
5023 /* Sanitize mode. See fileobject.c */
5024 mode
= PyMem_MALLOC(strlen(orgmode
)+3);
5029 strcpy(mode
, orgmode
);
5030 if (_PyFile_SanitizeMode(mode
)) {
5034 if (!_PyVerify_fd(fd
))
5035 return posix_error();
5036 Py_BEGIN_ALLOW_THREADS
5037 #if !defined(MS_WINDOWS) && defined(HAVE_FCNTL_H)
5038 if (mode
[0] == 'a') {
5039 /* try to make sure the O_APPEND flag is set */
5041 flags
= fcntl(fd
, F_GETFL
);
5043 fcntl(fd
, F_SETFL
, flags
| O_APPEND
);
5044 fp
= fdopen(fd
, mode
);
5045 if (fp
== NULL
&& flags
!= -1)
5046 /* restore old mode if fdopen failed */
5047 fcntl(fd
, F_SETFL
, flags
);
5049 fp
= fdopen(fd
, mode
);
5052 fp
= fdopen(fd
, mode
);
5054 Py_END_ALLOW_THREADS
5057 return posix_error();
5058 f
= PyFile_FromFile(fp
, "<fdopen>", orgmode
, fclose
);
5060 PyFile_SetBufSize(f
, bufsize
);
5064 PyDoc_STRVAR(posix_isatty__doc__
,
5065 "isatty(fd) -> bool\n\n\
5066 Return True if the file descriptor 'fd' is an open file descriptor\n\
5067 connected to the slave end of a terminal.");
5070 posix_isatty(PyObject
*self
, PyObject
*args
)
5073 if (!PyArg_ParseTuple(args
, "i:isatty", &fd
))
5075 if (!_PyVerify_fd(fd
))
5076 return PyBool_FromLong(0);
5077 return PyBool_FromLong(isatty(fd
));
5081 PyDoc_STRVAR(posix_pipe__doc__
,
5082 "pipe() -> (read_end, write_end)\n\n\
5086 posix_pipe(PyObject
*self
, PyObject
*noargs
)
5088 #if defined(PYOS_OS2)
5092 Py_BEGIN_ALLOW_THREADS
5093 rc
= DosCreatePipe( &read
, &write
, 4096);
5094 Py_END_ALLOW_THREADS
5096 return os2_error(rc
);
5098 return Py_BuildValue("(ii)", read
, write
);
5100 #if !defined(MS_WINDOWS)
5103 Py_BEGIN_ALLOW_THREADS
5105 Py_END_ALLOW_THREADS
5107 return posix_error();
5108 return Py_BuildValue("(ii)", fds
[0], fds
[1]);
5109 #else /* MS_WINDOWS */
5111 int read_fd
, write_fd
;
5113 Py_BEGIN_ALLOW_THREADS
5114 ok
= CreatePipe(&read
, &write
, NULL
, 0);
5115 Py_END_ALLOW_THREADS
5117 return win32_error("CreatePipe", NULL
);
5118 read_fd
= _open_osfhandle((Py_intptr_t
)read
, 0);
5119 write_fd
= _open_osfhandle((Py_intptr_t
)write
, 1);
5120 return Py_BuildValue("(ii)", read_fd
, write_fd
);
5121 #endif /* MS_WINDOWS */
5124 #endif /* HAVE_PIPE */
5128 PyDoc_STRVAR(posix_mkfifo__doc__
,
5129 "mkfifo(filename [, mode=0666])\n\n\
5130 Create a FIFO (a POSIX named pipe).");
5133 posix_mkfifo(PyObject
*self
, PyObject
*args
)
5138 if (!PyArg_ParseTuple(args
, "s|i:mkfifo", &filename
, &mode
))
5140 Py_BEGIN_ALLOW_THREADS
5141 res
= mkfifo(filename
, mode
);
5142 Py_END_ALLOW_THREADS
5144 return posix_error();
5151 #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
5152 PyDoc_STRVAR(posix_mknod__doc__
,
5153 "mknod(filename [, mode=0600, device])\n\n\
5154 Create a filesystem node (file, device special file or named pipe)\n\
5155 named filename. mode specifies both the permissions to use and the\n\
5156 type of node to be created, being combined (bitwise OR) with one of\n\
5157 S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
5158 device defines the newly created device special file (probably using\n\
5159 os.makedev()), otherwise it is ignored.");
5163 posix_mknod(PyObject
*self
, PyObject
*args
)
5169 if (!PyArg_ParseTuple(args
, "s|ii:mknod", &filename
, &mode
, &device
))
5171 Py_BEGIN_ALLOW_THREADS
5172 res
= mknod(filename
, mode
, device
);
5173 Py_END_ALLOW_THREADS
5175 return posix_error();
5181 #ifdef HAVE_DEVICE_MACROS
5182 PyDoc_STRVAR(posix_major__doc__
,
5183 "major(device) -> major number\n\
5184 Extracts a device major number from a raw device number.");
5187 posix_major(PyObject
*self
, PyObject
*args
)
5190 if (!PyArg_ParseTuple(args
, "i:major", &device
))
5192 return PyInt_FromLong((long)major(device
));
5195 PyDoc_STRVAR(posix_minor__doc__
,
5196 "minor(device) -> minor number\n\
5197 Extracts a device minor number from a raw device number.");
5200 posix_minor(PyObject
*self
, PyObject
*args
)
5203 if (!PyArg_ParseTuple(args
, "i:minor", &device
))
5205 return PyInt_FromLong((long)minor(device
));
5208 PyDoc_STRVAR(posix_makedev__doc__
,
5209 "makedev(major, minor) -> device number\n\
5210 Composes a raw device number from the major and minor device numbers.");
5213 posix_makedev(PyObject
*self
, PyObject
*args
)
5216 if (!PyArg_ParseTuple(args
, "ii:makedev", &major
, &minor
))
5218 return PyInt_FromLong((long)makedev(major
, minor
));
5220 #endif /* device macros */
5223 #ifdef HAVE_FTRUNCATE
5224 PyDoc_STRVAR(posix_ftruncate__doc__
,
5225 "ftruncate(fd, length)\n\n\
5226 Truncate a file to a specified length.");
5229 posix_ftruncate(PyObject
*self
, PyObject
*args
)
5236 if (!PyArg_ParseTuple(args
, "iO:ftruncate", &fd
, &lenobj
))
5239 #if !defined(HAVE_LARGEFILE_SUPPORT)
5240 length
= PyInt_AsLong(lenobj
);
5242 length
= PyLong_Check(lenobj
) ?
5243 PyLong_AsLongLong(lenobj
) : PyInt_AsLong(lenobj
);
5245 if (PyErr_Occurred())
5248 Py_BEGIN_ALLOW_THREADS
5249 res
= ftruncate(fd
, length
);
5250 Py_END_ALLOW_THREADS
5252 return posix_error();
5259 PyDoc_STRVAR(posix_putenv__doc__
,
5260 "putenv(key, value)\n\n\
5261 Change or add an environment variable.");
5263 /* Save putenv() parameters as values here, so we can collect them when they
5264 * get re-set with another call for the same key. */
5265 static PyObject
*posix_putenv_garbage
;
5268 posix_putenv(PyObject
*self
, PyObject
*args
)
5275 if (!PyArg_ParseTuple(args
, "ss:putenv", &s1
, &s2
))
5278 #if defined(PYOS_OS2)
5279 if (stricmp(s1
, "BEGINLIBPATH") == 0) {
5282 rc
= DosSetExtLIBPATH(s2
, BEGIN_LIBPATH
);
5284 return os2_error(rc
);
5286 } else if (stricmp(s1
, "ENDLIBPATH") == 0) {
5289 rc
= DosSetExtLIBPATH(s2
, END_LIBPATH
);
5291 return os2_error(rc
);
5295 /* XXX This can leak memory -- not easy to fix :-( */
5296 len
= strlen(s1
) + strlen(s2
) + 2;
5297 /* len includes space for a trailing \0; the size arg to
5298 PyString_FromStringAndSize does not count that */
5299 newstr
= PyString_FromStringAndSize(NULL
, (int)len
- 1);
5301 return PyErr_NoMemory();
5302 newenv
= PyString_AS_STRING(newstr
);
5303 PyOS_snprintf(newenv
, len
, "%s=%s", s1
, s2
);
5304 if (putenv(newenv
)) {
5309 /* Install the first arg and newstr in posix_putenv_garbage;
5310 * this will cause previous value to be collected. This has to
5311 * happen after the real putenv() call because the old value
5312 * was still accessible until then. */
5313 if (PyDict_SetItem(posix_putenv_garbage
,
5314 PyTuple_GET_ITEM(args
, 0), newstr
)) {
5315 /* really not much we can do; just leak */
5322 #if defined(PYOS_OS2)
5330 #ifdef HAVE_UNSETENV
5331 PyDoc_STRVAR(posix_unsetenv__doc__
,
5333 Delete an environment variable.");
5336 posix_unsetenv(PyObject
*self
, PyObject
*args
)
5340 if (!PyArg_ParseTuple(args
, "s:unsetenv", &s1
))
5345 /* Remove the key from posix_putenv_garbage;
5346 * this will cause it to be collected. This has to
5347 * happen after the real unsetenv() call because the
5348 * old value was still accessible until then.
5350 if (PyDict_DelItem(posix_putenv_garbage
,
5351 PyTuple_GET_ITEM(args
, 0))) {
5352 /* really not much we can do; just leak */
5359 #endif /* unsetenv */
5361 PyDoc_STRVAR(posix_strerror__doc__
,
5362 "strerror(code) -> string\n\n\
5363 Translate an error code to a message string.");
5366 posix_strerror(PyObject
*self
, PyObject
*args
)
5370 if (!PyArg_ParseTuple(args
, "i:strerror", &code
))
5372 message
= strerror(code
);
5373 if (message
== NULL
) {
5374 PyErr_SetString(PyExc_ValueError
,
5375 "strerror() argument out of range");
5378 return PyString_FromString(message
);
5382 #ifdef HAVE_SYS_WAIT_H
5385 PyDoc_STRVAR(posix_WCOREDUMP__doc__
,
5386 "WCOREDUMP(status) -> bool\n\n\
5387 Return True if the process returning 'status' was dumped to a core file.");
5390 posix_WCOREDUMP(PyObject
*self
, PyObject
*args
)
5393 WAIT_STATUS_INT(status
) = 0;
5395 if (!PyArg_ParseTuple(args
, "i:WCOREDUMP", &WAIT_STATUS_INT(status
)))
5398 return PyBool_FromLong(WCOREDUMP(status
));
5400 #endif /* WCOREDUMP */
5403 PyDoc_STRVAR(posix_WIFCONTINUED__doc__
,
5404 "WIFCONTINUED(status) -> bool\n\n\
5405 Return True if the process returning 'status' was continued from a\n\
5406 job control stop.");
5409 posix_WIFCONTINUED(PyObject
*self
, PyObject
*args
)
5412 WAIT_STATUS_INT(status
) = 0;
5414 if (!PyArg_ParseTuple(args
, "i:WCONTINUED", &WAIT_STATUS_INT(status
)))
5417 return PyBool_FromLong(WIFCONTINUED(status
));
5419 #endif /* WIFCONTINUED */
5422 PyDoc_STRVAR(posix_WIFSTOPPED__doc__
,
5423 "WIFSTOPPED(status) -> bool\n\n\
5424 Return True if the process returning 'status' was stopped.");
5427 posix_WIFSTOPPED(PyObject
*self
, PyObject
*args
)
5430 WAIT_STATUS_INT(status
) = 0;
5432 if (!PyArg_ParseTuple(args
, "i:WIFSTOPPED", &WAIT_STATUS_INT(status
)))
5435 return PyBool_FromLong(WIFSTOPPED(status
));
5437 #endif /* WIFSTOPPED */
5440 PyDoc_STRVAR(posix_WIFSIGNALED__doc__
,
5441 "WIFSIGNALED(status) -> bool\n\n\
5442 Return True if the process returning 'status' was terminated by a signal.");
5445 posix_WIFSIGNALED(PyObject
*self
, PyObject
*args
)
5448 WAIT_STATUS_INT(status
) = 0;
5450 if (!PyArg_ParseTuple(args
, "i:WIFSIGNALED", &WAIT_STATUS_INT(status
)))
5453 return PyBool_FromLong(WIFSIGNALED(status
));
5455 #endif /* WIFSIGNALED */
5458 PyDoc_STRVAR(posix_WIFEXITED__doc__
,
5459 "WIFEXITED(status) -> bool\n\n\
5460 Return true if the process returning 'status' exited using the exit()\n\
5464 posix_WIFEXITED(PyObject
*self
, PyObject
*args
)
5467 WAIT_STATUS_INT(status
) = 0;
5469 if (!PyArg_ParseTuple(args
, "i:WIFEXITED", &WAIT_STATUS_INT(status
)))
5472 return PyBool_FromLong(WIFEXITED(status
));
5474 #endif /* WIFEXITED */
5477 PyDoc_STRVAR(posix_WEXITSTATUS__doc__
,
5478 "WEXITSTATUS(status) -> integer\n\n\
5479 Return the process return code from 'status'.");
5482 posix_WEXITSTATUS(PyObject
*self
, PyObject
*args
)
5485 WAIT_STATUS_INT(status
) = 0;
5487 if (!PyArg_ParseTuple(args
, "i:WEXITSTATUS", &WAIT_STATUS_INT(status
)))
5490 return Py_BuildValue("i", WEXITSTATUS(status
));
5492 #endif /* WEXITSTATUS */
5495 PyDoc_STRVAR(posix_WTERMSIG__doc__
,
5496 "WTERMSIG(status) -> integer\n\n\
5497 Return the signal that terminated the process that provided the 'status'\n\
5501 posix_WTERMSIG(PyObject
*self
, PyObject
*args
)
5504 WAIT_STATUS_INT(status
) = 0;
5506 if (!PyArg_ParseTuple(args
, "i:WTERMSIG", &WAIT_STATUS_INT(status
)))
5509 return Py_BuildValue("i", WTERMSIG(status
));
5511 #endif /* WTERMSIG */
5514 PyDoc_STRVAR(posix_WSTOPSIG__doc__
,
5515 "WSTOPSIG(status) -> integer\n\n\
5516 Return the signal that stopped the process that provided\n\
5517 the 'status' value.");
5520 posix_WSTOPSIG(PyObject
*self
, PyObject
*args
)
5523 WAIT_STATUS_INT(status
) = 0;
5525 if (!PyArg_ParseTuple(args
, "i:WSTOPSIG", &WAIT_STATUS_INT(status
)))
5528 return Py_BuildValue("i", WSTOPSIG(status
));
5530 #endif /* WSTOPSIG */
5532 #endif /* HAVE_SYS_WAIT_H */
5535 #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
5537 /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
5538 needed definitions in sys/statvfs.h */
5541 #include <sys/statvfs.h>
5544 _pystatvfs_fromstructstatvfs(struct statvfs st
) {
5545 PyObject
*v
= PyStructSequence_New(&StatVFSResultType
);
5549 #if !defined(HAVE_LARGEFILE_SUPPORT)
5550 PyStructSequence_SET_ITEM(v
, 0, PyInt_FromLong((long) st
.f_bsize
));
5551 PyStructSequence_SET_ITEM(v
, 1, PyInt_FromLong((long) st
.f_frsize
));
5552 PyStructSequence_SET_ITEM(v
, 2, PyInt_FromLong((long) st
.f_blocks
));
5553 PyStructSequence_SET_ITEM(v
, 3, PyInt_FromLong((long) st
.f_bfree
));
5554 PyStructSequence_SET_ITEM(v
, 4, PyInt_FromLong((long) st
.f_bavail
));
5555 PyStructSequence_SET_ITEM(v
, 5, PyInt_FromLong((long) st
.f_files
));
5556 PyStructSequence_SET_ITEM(v
, 6, PyInt_FromLong((long) st
.f_ffree
));
5557 PyStructSequence_SET_ITEM(v
, 7, PyInt_FromLong((long) st
.f_favail
));
5558 PyStructSequence_SET_ITEM(v
, 8, PyInt_FromLong((long) st
.f_flag
));
5559 PyStructSequence_SET_ITEM(v
, 9, PyInt_FromLong((long) st
.f_namemax
));
5561 PyStructSequence_SET_ITEM(v
, 0, PyInt_FromLong((long) st
.f_bsize
));
5562 PyStructSequence_SET_ITEM(v
, 1, PyInt_FromLong((long) st
.f_frsize
));
5563 PyStructSequence_SET_ITEM(v
, 2,
5564 PyLong_FromLongLong((PY_LONG_LONG
) st
.f_blocks
));
5565 PyStructSequence_SET_ITEM(v
, 3,
5566 PyLong_FromLongLong((PY_LONG_LONG
) st
.f_bfree
));
5567 PyStructSequence_SET_ITEM(v
, 4,
5568 PyLong_FromLongLong((PY_LONG_LONG
) st
.f_bavail
));
5569 PyStructSequence_SET_ITEM(v
, 5,
5570 PyLong_FromLongLong((PY_LONG_LONG
) st
.f_files
));
5571 PyStructSequence_SET_ITEM(v
, 6,
5572 PyLong_FromLongLong((PY_LONG_LONG
) st
.f_ffree
));
5573 PyStructSequence_SET_ITEM(v
, 7,
5574 PyLong_FromLongLong((PY_LONG_LONG
) st
.f_favail
));
5575 PyStructSequence_SET_ITEM(v
, 8, PyInt_FromLong((long) st
.f_flag
));
5576 PyStructSequence_SET_ITEM(v
, 9, PyInt_FromLong((long) st
.f_namemax
));
5582 PyDoc_STRVAR(posix_fstatvfs__doc__
,
5583 "fstatvfs(fd) -> statvfs result\n\n\
5584 Perform an fstatvfs system call on the given fd.");
5587 posix_fstatvfs(PyObject
*self
, PyObject
*args
)
5592 if (!PyArg_ParseTuple(args
, "i:fstatvfs", &fd
))
5594 Py_BEGIN_ALLOW_THREADS
5595 res
= fstatvfs(fd
, &st
);
5596 Py_END_ALLOW_THREADS
5598 return posix_error();
5600 return _pystatvfs_fromstructstatvfs(st
);
5602 #endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
5605 #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
5606 #include <sys/statvfs.h>
5608 PyDoc_STRVAR(posix_statvfs__doc__
,
5609 "statvfs(path) -> statvfs result\n\n\
5610 Perform a statvfs system call on the given path.");
5613 posix_statvfs(PyObject
*self
, PyObject
*args
)
5618 if (!PyArg_ParseTuple(args
, "s:statvfs", &path
))
5620 Py_BEGIN_ALLOW_THREADS
5621 res
= statvfs(path
, &st
);
5622 Py_END_ALLOW_THREADS
5624 return posix_error_with_filename(path
);
5626 return _pystatvfs_fromstructstatvfs(st
);
5628 #endif /* HAVE_STATVFS */
5632 PyDoc_STRVAR(posix_tempnam__doc__
,
5633 "tempnam([dir[, prefix]]) -> string\n\n\
5634 Return a unique name for a temporary file.\n\
5635 The directory and a prefix may be specified as strings; they may be omitted\n\
5636 or None if not needed.");
5639 posix_tempnam(PyObject
*self
, PyObject
*args
)
5641 PyObject
*result
= NULL
;
5646 if (!PyArg_ParseTuple(args
, "|zz:tempnam", &dir
, &pfx
))
5649 if (PyErr_Warn(PyExc_RuntimeWarning
,
5650 "tempnam is a potential security risk to your program") < 0)
5653 if (PyErr_WarnPy3k("tempnam has been removed in 3.x; "
5654 "use the tempfile module", 1) < 0)
5657 name
= tempnam(dir
, pfx
);
5659 return PyErr_NoMemory();
5660 result
= PyString_FromString(name
);
5668 PyDoc_STRVAR(posix_tmpfile__doc__
,
5669 "tmpfile() -> file object\n\n\
5670 Create a temporary file with no directory entries.");
5673 posix_tmpfile(PyObject
*self
, PyObject
*noargs
)
5677 if (PyErr_WarnPy3k("tmpfile has been removed in 3.x; "
5678 "use the tempfile module", 1) < 0)
5683 return posix_error();
5684 return PyFile_FromFile(fp
, "<tmpfile>", "w+b", fclose
);
5690 PyDoc_STRVAR(posix_tmpnam__doc__
,
5691 "tmpnam() -> string\n\n\
5692 Return a unique name for a temporary file.");
5695 posix_tmpnam(PyObject
*self
, PyObject
*noargs
)
5697 char buffer
[L_tmpnam
];
5700 if (PyErr_Warn(PyExc_RuntimeWarning
,
5701 "tmpnam is a potential security risk to your program") < 0)
5704 if (PyErr_WarnPy3k("tmpnam has been removed in 3.x; "
5705 "use the tempfile module", 1) < 0)
5709 name
= tmpnam_r(buffer
);
5711 name
= tmpnam(buffer
);
5714 PyObject
*err
= Py_BuildValue("is", 0,
5716 "unexpected NULL from tmpnam_r"
5718 "unexpected NULL from tmpnam"
5721 PyErr_SetObject(PyExc_OSError
, err
);
5725 return PyString_FromString(buffer
);
5730 /* This is used for fpathconf(), pathconf(), confstr() and sysconf().
5731 * It maps strings representing configuration variable names to
5732 * integer values, allowing those functions to be called with the
5733 * magic names instead of polluting the module's namespace with tons of
5734 * rarely-used constants. There are three separate tables that use
5735 * these definitions.
5737 * This code is always included, even if none of the interfaces that
5738 * need it are included. The #if hackery needed to avoid it would be
5739 * sufficiently pervasive that it's not worth the loss of readability.
5747 conv_confname(PyObject
*arg
, int *valuep
, struct constdef
*table
,
5750 if (PyInt_Check(arg
)) {
5751 *valuep
= PyInt_AS_LONG(arg
);
5754 if (PyString_Check(arg
)) {
5755 /* look up the value in the table using a binary search */
5758 size_t hi
= tablesize
;
5760 char *confname
= PyString_AS_STRING(arg
);
5762 mid
= (lo
+ hi
) / 2;
5763 cmp
= strcmp(confname
, table
[mid
].name
);
5769 *valuep
= table
[mid
].value
;
5773 PyErr_SetString(PyExc_ValueError
, "unrecognized configuration name");
5776 PyErr_SetString(PyExc_TypeError
,
5777 "configuration names must be strings or integers");
5782 #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
5783 static struct constdef posix_constants_pathconf
[] = {
5784 #ifdef _PC_ABI_AIO_XFER_MAX
5785 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX
},
5787 #ifdef _PC_ABI_ASYNC_IO
5788 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO
},
5791 {"PC_ASYNC_IO", _PC_ASYNC_IO
},
5793 #ifdef _PC_CHOWN_RESTRICTED
5794 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED
},
5796 #ifdef _PC_FILESIZEBITS
5797 {"PC_FILESIZEBITS", _PC_FILESIZEBITS
},
5800 {"PC_LAST", _PC_LAST
},
5803 {"PC_LINK_MAX", _PC_LINK_MAX
},
5805 #ifdef _PC_MAX_CANON
5806 {"PC_MAX_CANON", _PC_MAX_CANON
},
5808 #ifdef _PC_MAX_INPUT
5809 {"PC_MAX_INPUT", _PC_MAX_INPUT
},
5812 {"PC_NAME_MAX", _PC_NAME_MAX
},
5815 {"PC_NO_TRUNC", _PC_NO_TRUNC
},
5818 {"PC_PATH_MAX", _PC_PATH_MAX
},
5821 {"PC_PIPE_BUF", _PC_PIPE_BUF
},
5824 {"PC_PRIO_IO", _PC_PRIO_IO
},
5826 #ifdef _PC_SOCK_MAXBUF
5827 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF
},
5830 {"PC_SYNC_IO", _PC_SYNC_IO
},
5833 {"PC_VDISABLE", _PC_VDISABLE
},
5838 conv_path_confname(PyObject
*arg
, int *valuep
)
5840 return conv_confname(arg
, valuep
, posix_constants_pathconf
,
5841 sizeof(posix_constants_pathconf
)
5842 / sizeof(struct constdef
));
5846 #ifdef HAVE_FPATHCONF
5847 PyDoc_STRVAR(posix_fpathconf__doc__
,
5848 "fpathconf(fd, name) -> integer\n\n\
5849 Return the configuration limit name for the file descriptor fd.\n\
5850 If there is no limit, return -1.");
5853 posix_fpathconf(PyObject
*self
, PyObject
*args
)
5855 PyObject
*result
= NULL
;
5858 if (PyArg_ParseTuple(args
, "iO&:fpathconf", &fd
,
5859 conv_path_confname
, &name
)) {
5863 limit
= fpathconf(fd
, name
);
5864 if (limit
== -1 && errno
!= 0)
5867 result
= PyInt_FromLong(limit
);
5874 #ifdef HAVE_PATHCONF
5875 PyDoc_STRVAR(posix_pathconf__doc__
,
5876 "pathconf(path, name) -> integer\n\n\
5877 Return the configuration limit name for the file or directory path.\n\
5878 If there is no limit, return -1.");
5881 posix_pathconf(PyObject
*self
, PyObject
*args
)
5883 PyObject
*result
= NULL
;
5887 if (PyArg_ParseTuple(args
, "sO&:pathconf", &path
,
5888 conv_path_confname
, &name
)) {
5892 limit
= pathconf(path
, name
);
5893 if (limit
== -1 && errno
!= 0) {
5894 if (errno
== EINVAL
)
5895 /* could be a path or name problem */
5898 posix_error_with_filename(path
);
5901 result
= PyInt_FromLong(limit
);
5908 static struct constdef posix_constants_confstr
[] = {
5909 #ifdef _CS_ARCHITECTURE
5910 {"CS_ARCHITECTURE", _CS_ARCHITECTURE
},
5913 {"CS_HOSTNAME", _CS_HOSTNAME
},
5915 #ifdef _CS_HW_PROVIDER
5916 {"CS_HW_PROVIDER", _CS_HW_PROVIDER
},
5918 #ifdef _CS_HW_SERIAL
5919 {"CS_HW_SERIAL", _CS_HW_SERIAL
},
5921 #ifdef _CS_INITTAB_NAME
5922 {"CS_INITTAB_NAME", _CS_INITTAB_NAME
},
5924 #ifdef _CS_LFS64_CFLAGS
5925 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS
},
5927 #ifdef _CS_LFS64_LDFLAGS
5928 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS
},
5930 #ifdef _CS_LFS64_LIBS
5931 {"CS_LFS64_LIBS", _CS_LFS64_LIBS
},
5933 #ifdef _CS_LFS64_LINTFLAGS
5934 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS
},
5936 #ifdef _CS_LFS_CFLAGS
5937 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS
},
5939 #ifdef _CS_LFS_LDFLAGS
5940 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS
},
5943 {"CS_LFS_LIBS", _CS_LFS_LIBS
},
5945 #ifdef _CS_LFS_LINTFLAGS
5946 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS
},
5949 {"CS_MACHINE", _CS_MACHINE
},
5952 {"CS_PATH", _CS_PATH
},
5955 {"CS_RELEASE", _CS_RELEASE
},
5957 #ifdef _CS_SRPC_DOMAIN
5958 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN
},
5961 {"CS_SYSNAME", _CS_SYSNAME
},
5964 {"CS_VERSION", _CS_VERSION
},
5966 #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
5967 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS
},
5969 #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
5970 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS
},
5972 #ifdef _CS_XBS5_ILP32_OFF32_LIBS
5973 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS
},
5975 #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
5976 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS
},
5978 #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
5979 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS
},
5981 #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
5982 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS
},
5984 #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
5985 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS
},
5987 #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
5988 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
},
5990 #ifdef _CS_XBS5_LP64_OFF64_CFLAGS
5991 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS
},
5993 #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
5994 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS
},
5996 #ifdef _CS_XBS5_LP64_OFF64_LIBS
5997 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS
},
5999 #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
6000 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS
},
6002 #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
6003 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS
},
6005 #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
6006 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
},
6008 #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
6009 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS
},
6011 #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
6012 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
},
6014 #ifdef _MIPS_CS_AVAIL_PROCESSORS
6015 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS
},
6017 #ifdef _MIPS_CS_BASE
6018 {"MIPS_CS_BASE", _MIPS_CS_BASE
},
6020 #ifdef _MIPS_CS_HOSTID
6021 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID
},
6023 #ifdef _MIPS_CS_HW_NAME
6024 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME
},
6026 #ifdef _MIPS_CS_NUM_PROCESSORS
6027 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS
},
6029 #ifdef _MIPS_CS_OSREL_MAJ
6030 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ
},
6032 #ifdef _MIPS_CS_OSREL_MIN
6033 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN
},
6035 #ifdef _MIPS_CS_OSREL_PATCH
6036 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH
},
6038 #ifdef _MIPS_CS_OS_NAME
6039 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME
},
6041 #ifdef _MIPS_CS_OS_PROVIDER
6042 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER
},
6044 #ifdef _MIPS_CS_PROCESSORS
6045 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS
},
6047 #ifdef _MIPS_CS_SERIAL
6048 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL
},
6050 #ifdef _MIPS_CS_VENDOR
6051 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR
},
6056 conv_confstr_confname(PyObject
*arg
, int *valuep
)
6058 return conv_confname(arg
, valuep
, posix_constants_confstr
,
6059 sizeof(posix_constants_confstr
)
6060 / sizeof(struct constdef
));
6063 PyDoc_STRVAR(posix_confstr__doc__
,
6064 "confstr(name) -> string\n\n\
6065 Return a string-valued system configuration variable.");
6068 posix_confstr(PyObject
*self
, PyObject
*args
)
6070 PyObject
*result
= NULL
;
6074 if (PyArg_ParseTuple(args
, "O&:confstr", conv_confstr_confname
, &name
)) {
6078 len
= confstr(name
, buffer
, sizeof(buffer
));
6089 if ((unsigned int)len
>= sizeof(buffer
)) {
6090 result
= PyString_FromStringAndSize(NULL
, len
-1);
6092 confstr(name
, PyString_AS_STRING(result
), len
);
6095 result
= PyString_FromStringAndSize(buffer
, len
-1);
6104 static struct constdef posix_constants_sysconf
[] = {
6105 #ifdef _SC_2_CHAR_TERM
6106 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM
},
6109 {"SC_2_C_BIND", _SC_2_C_BIND
},
6112 {"SC_2_C_DEV", _SC_2_C_DEV
},
6114 #ifdef _SC_2_C_VERSION
6115 {"SC_2_C_VERSION", _SC_2_C_VERSION
},
6117 #ifdef _SC_2_FORT_DEV
6118 {"SC_2_FORT_DEV", _SC_2_FORT_DEV
},
6120 #ifdef _SC_2_FORT_RUN
6121 {"SC_2_FORT_RUN", _SC_2_FORT_RUN
},
6123 #ifdef _SC_2_LOCALEDEF
6124 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF
},
6127 {"SC_2_SW_DEV", _SC_2_SW_DEV
},
6130 {"SC_2_UPE", _SC_2_UPE
},
6132 #ifdef _SC_2_VERSION
6133 {"SC_2_VERSION", _SC_2_VERSION
},
6135 #ifdef _SC_ABI_ASYNCHRONOUS_IO
6136 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO
},
6139 {"SC_ACL", _SC_ACL
},
6141 #ifdef _SC_AIO_LISTIO_MAX
6142 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX
},
6145 {"SC_AIO_MAX", _SC_AIO_MAX
},
6147 #ifdef _SC_AIO_PRIO_DELTA_MAX
6148 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX
},
6151 {"SC_ARG_MAX", _SC_ARG_MAX
},
6153 #ifdef _SC_ASYNCHRONOUS_IO
6154 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO
},
6156 #ifdef _SC_ATEXIT_MAX
6157 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX
},
6160 {"SC_AUDIT", _SC_AUDIT
},
6162 #ifdef _SC_AVPHYS_PAGES
6163 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES
},
6165 #ifdef _SC_BC_BASE_MAX
6166 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX
},
6168 #ifdef _SC_BC_DIM_MAX
6169 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX
},
6171 #ifdef _SC_BC_SCALE_MAX
6172 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX
},
6174 #ifdef _SC_BC_STRING_MAX
6175 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX
},
6178 {"SC_CAP", _SC_CAP
},
6180 #ifdef _SC_CHARCLASS_NAME_MAX
6181 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX
},
6184 {"SC_CHAR_BIT", _SC_CHAR_BIT
},
6187 {"SC_CHAR_MAX", _SC_CHAR_MAX
},
6190 {"SC_CHAR_MIN", _SC_CHAR_MIN
},
6192 #ifdef _SC_CHILD_MAX
6193 {"SC_CHILD_MAX", _SC_CHILD_MAX
},
6196 {"SC_CLK_TCK", _SC_CLK_TCK
},
6198 #ifdef _SC_COHER_BLKSZ
6199 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ
},
6201 #ifdef _SC_COLL_WEIGHTS_MAX
6202 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX
},
6204 #ifdef _SC_DCACHE_ASSOC
6205 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC
},
6207 #ifdef _SC_DCACHE_BLKSZ
6208 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ
},
6210 #ifdef _SC_DCACHE_LINESZ
6211 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ
},
6213 #ifdef _SC_DCACHE_SZ
6214 {"SC_DCACHE_SZ", _SC_DCACHE_SZ
},
6216 #ifdef _SC_DCACHE_TBLKSZ
6217 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ
},
6219 #ifdef _SC_DELAYTIMER_MAX
6220 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX
},
6222 #ifdef _SC_EQUIV_CLASS_MAX
6223 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX
},
6225 #ifdef _SC_EXPR_NEST_MAX
6226 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX
},
6229 {"SC_FSYNC", _SC_FSYNC
},
6231 #ifdef _SC_GETGR_R_SIZE_MAX
6232 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX
},
6234 #ifdef _SC_GETPW_R_SIZE_MAX
6235 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX
},
6237 #ifdef _SC_ICACHE_ASSOC
6238 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC
},
6240 #ifdef _SC_ICACHE_BLKSZ
6241 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ
},
6243 #ifdef _SC_ICACHE_LINESZ
6244 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ
},
6246 #ifdef _SC_ICACHE_SZ
6247 {"SC_ICACHE_SZ", _SC_ICACHE_SZ
},
6250 {"SC_INF", _SC_INF
},
6253 {"SC_INT_MAX", _SC_INT_MAX
},
6256 {"SC_INT_MIN", _SC_INT_MIN
},
6259 {"SC_IOV_MAX", _SC_IOV_MAX
},
6261 #ifdef _SC_IP_SECOPTS
6262 {"SC_IP_SECOPTS", _SC_IP_SECOPTS
},
6264 #ifdef _SC_JOB_CONTROL
6265 {"SC_JOB_CONTROL", _SC_JOB_CONTROL
},
6267 #ifdef _SC_KERN_POINTERS
6268 {"SC_KERN_POINTERS", _SC_KERN_POINTERS
},
6271 {"SC_KERN_SIM", _SC_KERN_SIM
},
6274 {"SC_LINE_MAX", _SC_LINE_MAX
},
6276 #ifdef _SC_LOGIN_NAME_MAX
6277 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX
},
6279 #ifdef _SC_LOGNAME_MAX
6280 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX
},
6283 {"SC_LONG_BIT", _SC_LONG_BIT
},
6286 {"SC_MAC", _SC_MAC
},
6288 #ifdef _SC_MAPPED_FILES
6289 {"SC_MAPPED_FILES", _SC_MAPPED_FILES
},
6292 {"SC_MAXPID", _SC_MAXPID
},
6294 #ifdef _SC_MB_LEN_MAX
6295 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX
},
6298 {"SC_MEMLOCK", _SC_MEMLOCK
},
6300 #ifdef _SC_MEMLOCK_RANGE
6301 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE
},
6303 #ifdef _SC_MEMORY_PROTECTION
6304 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION
},
6306 #ifdef _SC_MESSAGE_PASSING
6307 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING
},
6309 #ifdef _SC_MMAP_FIXED_ALIGNMENT
6310 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT
},
6312 #ifdef _SC_MQ_OPEN_MAX
6313 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX
},
6315 #ifdef _SC_MQ_PRIO_MAX
6316 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX
},
6318 #ifdef _SC_NACLS_MAX
6319 {"SC_NACLS_MAX", _SC_NACLS_MAX
},
6321 #ifdef _SC_NGROUPS_MAX
6322 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX
},
6324 #ifdef _SC_NL_ARGMAX
6325 {"SC_NL_ARGMAX", _SC_NL_ARGMAX
},
6327 #ifdef _SC_NL_LANGMAX
6328 {"SC_NL_LANGMAX", _SC_NL_LANGMAX
},
6330 #ifdef _SC_NL_MSGMAX
6331 {"SC_NL_MSGMAX", _SC_NL_MSGMAX
},
6334 {"SC_NL_NMAX", _SC_NL_NMAX
},
6336 #ifdef _SC_NL_SETMAX
6337 {"SC_NL_SETMAX", _SC_NL_SETMAX
},
6339 #ifdef _SC_NL_TEXTMAX
6340 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX
},
6342 #ifdef _SC_NPROCESSORS_CONF
6343 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF
},
6345 #ifdef _SC_NPROCESSORS_ONLN
6346 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN
},
6348 #ifdef _SC_NPROC_CONF
6349 {"SC_NPROC_CONF", _SC_NPROC_CONF
},
6351 #ifdef _SC_NPROC_ONLN
6352 {"SC_NPROC_ONLN", _SC_NPROC_ONLN
},
6355 {"SC_NZERO", _SC_NZERO
},
6358 {"SC_OPEN_MAX", _SC_OPEN_MAX
},
6361 {"SC_PAGESIZE", _SC_PAGESIZE
},
6363 #ifdef _SC_PAGE_SIZE
6364 {"SC_PAGE_SIZE", _SC_PAGE_SIZE
},
6367 {"SC_PASS_MAX", _SC_PASS_MAX
},
6369 #ifdef _SC_PHYS_PAGES
6370 {"SC_PHYS_PAGES", _SC_PHYS_PAGES
},
6373 {"SC_PII", _SC_PII
},
6375 #ifdef _SC_PII_INTERNET
6376 {"SC_PII_INTERNET", _SC_PII_INTERNET
},
6378 #ifdef _SC_PII_INTERNET_DGRAM
6379 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM
},
6381 #ifdef _SC_PII_INTERNET_STREAM
6382 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM
},
6385 {"SC_PII_OSI", _SC_PII_OSI
},
6387 #ifdef _SC_PII_OSI_CLTS
6388 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS
},
6390 #ifdef _SC_PII_OSI_COTS
6391 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS
},
6393 #ifdef _SC_PII_OSI_M
6394 {"SC_PII_OSI_M", _SC_PII_OSI_M
},
6396 #ifdef _SC_PII_SOCKET
6397 {"SC_PII_SOCKET", _SC_PII_SOCKET
},
6400 {"SC_PII_XTI", _SC_PII_XTI
},
6403 {"SC_POLL", _SC_POLL
},
6405 #ifdef _SC_PRIORITIZED_IO
6406 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO
},
6408 #ifdef _SC_PRIORITY_SCHEDULING
6409 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING
},
6411 #ifdef _SC_REALTIME_SIGNALS
6412 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS
},
6414 #ifdef _SC_RE_DUP_MAX
6415 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX
},
6417 #ifdef _SC_RTSIG_MAX
6418 {"SC_RTSIG_MAX", _SC_RTSIG_MAX
},
6420 #ifdef _SC_SAVED_IDS
6421 {"SC_SAVED_IDS", _SC_SAVED_IDS
},
6423 #ifdef _SC_SCHAR_MAX
6424 {"SC_SCHAR_MAX", _SC_SCHAR_MAX
},
6426 #ifdef _SC_SCHAR_MIN
6427 {"SC_SCHAR_MIN", _SC_SCHAR_MIN
},
6430 {"SC_SELECT", _SC_SELECT
},
6432 #ifdef _SC_SEMAPHORES
6433 {"SC_SEMAPHORES", _SC_SEMAPHORES
},
6435 #ifdef _SC_SEM_NSEMS_MAX
6436 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX
},
6438 #ifdef _SC_SEM_VALUE_MAX
6439 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX
},
6441 #ifdef _SC_SHARED_MEMORY_OBJECTS
6442 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS
},
6445 {"SC_SHRT_MAX", _SC_SHRT_MAX
},
6448 {"SC_SHRT_MIN", _SC_SHRT_MIN
},
6450 #ifdef _SC_SIGQUEUE_MAX
6451 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX
},
6453 #ifdef _SC_SIGRT_MAX
6454 {"SC_SIGRT_MAX", _SC_SIGRT_MAX
},
6456 #ifdef _SC_SIGRT_MIN
6457 {"SC_SIGRT_MIN", _SC_SIGRT_MIN
},
6459 #ifdef _SC_SOFTPOWER
6460 {"SC_SOFTPOWER", _SC_SOFTPOWER
},
6462 #ifdef _SC_SPLIT_CACHE
6463 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE
},
6465 #ifdef _SC_SSIZE_MAX
6466 {"SC_SSIZE_MAX", _SC_SSIZE_MAX
},
6468 #ifdef _SC_STACK_PROT
6469 {"SC_STACK_PROT", _SC_STACK_PROT
},
6471 #ifdef _SC_STREAM_MAX
6472 {"SC_STREAM_MAX", _SC_STREAM_MAX
},
6474 #ifdef _SC_SYNCHRONIZED_IO
6475 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO
},
6478 {"SC_THREADS", _SC_THREADS
},
6480 #ifdef _SC_THREAD_ATTR_STACKADDR
6481 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR
},
6483 #ifdef _SC_THREAD_ATTR_STACKSIZE
6484 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE
},
6486 #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
6487 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS
},
6489 #ifdef _SC_THREAD_KEYS_MAX
6490 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX
},
6492 #ifdef _SC_THREAD_PRIORITY_SCHEDULING
6493 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING
},
6495 #ifdef _SC_THREAD_PRIO_INHERIT
6496 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT
},
6498 #ifdef _SC_THREAD_PRIO_PROTECT
6499 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT
},
6501 #ifdef _SC_THREAD_PROCESS_SHARED
6502 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED
},
6504 #ifdef _SC_THREAD_SAFE_FUNCTIONS
6505 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS
},
6507 #ifdef _SC_THREAD_STACK_MIN
6508 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN
},
6510 #ifdef _SC_THREAD_THREADS_MAX
6511 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX
},
6514 {"SC_TIMERS", _SC_TIMERS
},
6516 #ifdef _SC_TIMER_MAX
6517 {"SC_TIMER_MAX", _SC_TIMER_MAX
},
6519 #ifdef _SC_TTY_NAME_MAX
6520 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX
},
6522 #ifdef _SC_TZNAME_MAX
6523 {"SC_TZNAME_MAX", _SC_TZNAME_MAX
},
6525 #ifdef _SC_T_IOV_MAX
6526 {"SC_T_IOV_MAX", _SC_T_IOV_MAX
},
6528 #ifdef _SC_UCHAR_MAX
6529 {"SC_UCHAR_MAX", _SC_UCHAR_MAX
},
6532 {"SC_UINT_MAX", _SC_UINT_MAX
},
6534 #ifdef _SC_UIO_MAXIOV
6535 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV
},
6537 #ifdef _SC_ULONG_MAX
6538 {"SC_ULONG_MAX", _SC_ULONG_MAX
},
6540 #ifdef _SC_USHRT_MAX
6541 {"SC_USHRT_MAX", _SC_USHRT_MAX
},
6544 {"SC_VERSION", _SC_VERSION
},
6547 {"SC_WORD_BIT", _SC_WORD_BIT
},
6549 #ifdef _SC_XBS5_ILP32_OFF32
6550 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32
},
6552 #ifdef _SC_XBS5_ILP32_OFFBIG
6553 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG
},
6555 #ifdef _SC_XBS5_LP64_OFF64
6556 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64
},
6558 #ifdef _SC_XBS5_LPBIG_OFFBIG
6559 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG
},
6561 #ifdef _SC_XOPEN_CRYPT
6562 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT
},
6564 #ifdef _SC_XOPEN_ENH_I18N
6565 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N
},
6567 #ifdef _SC_XOPEN_LEGACY
6568 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY
},
6570 #ifdef _SC_XOPEN_REALTIME
6571 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME
},
6573 #ifdef _SC_XOPEN_REALTIME_THREADS
6574 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS
},
6576 #ifdef _SC_XOPEN_SHM
6577 {"SC_XOPEN_SHM", _SC_XOPEN_SHM
},
6579 #ifdef _SC_XOPEN_UNIX
6580 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX
},
6582 #ifdef _SC_XOPEN_VERSION
6583 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION
},
6585 #ifdef _SC_XOPEN_XCU_VERSION
6586 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION
},
6588 #ifdef _SC_XOPEN_XPG2
6589 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2
},
6591 #ifdef _SC_XOPEN_XPG3
6592 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3
},
6594 #ifdef _SC_XOPEN_XPG4
6595 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4
},
6600 conv_sysconf_confname(PyObject
*arg
, int *valuep
)
6602 return conv_confname(arg
, valuep
, posix_constants_sysconf
,
6603 sizeof(posix_constants_sysconf
)
6604 / sizeof(struct constdef
));
6607 PyDoc_STRVAR(posix_sysconf__doc__
,
6608 "sysconf(name) -> integer\n\n\
6609 Return an integer-valued system configuration variable.");
6612 posix_sysconf(PyObject
*self
, PyObject
*args
)
6614 PyObject
*result
= NULL
;
6617 if (PyArg_ParseTuple(args
, "O&:sysconf", conv_sysconf_confname
, &name
)) {
6621 value
= sysconf(name
);
6622 if (value
== -1 && errno
!= 0)
6625 result
= PyInt_FromLong(value
);
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
);
6677 /* Return -1 on failure, 0 on success. */
6679 setup_confname_tables(PyObject
*module
)
6681 #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
6682 if (setup_confname_table(posix_constants_pathconf
,
6683 sizeof(posix_constants_pathconf
)
6684 / sizeof(struct constdef
),
6685 "pathconf_names", module
))
6689 if (setup_confname_table(posix_constants_confstr
,
6690 sizeof(posix_constants_confstr
)
6691 / sizeof(struct constdef
),
6692 "confstr_names", module
))
6696 if (setup_confname_table(posix_constants_sysconf
,
6697 sizeof(posix_constants_sysconf
)
6698 / sizeof(struct constdef
),
6699 "sysconf_names", module
))
6706 PyDoc_STRVAR(posix_abort__doc__
,
6707 "abort() -> does not return!\n\n\
6708 Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
6709 in the hardest way possible on the hosting operating system.");
6712 posix_abort(PyObject
*self
, PyObject
*noargs
)
6716 Py_FatalError("abort() called from Python code didn't abort!");
6720 #ifdef HAVE_SETRESUID
6721 PyDoc_STRVAR(posix_setresuid__doc__
,
6722 "setresuid(ruid, euid, suid)\n\n\
6723 Set the current process's real, effective, and saved user ids.");
6726 posix_setresuid (PyObject
*self
, PyObject
*args
)
6728 /* We assume uid_t is no larger than a long. */
6729 long ruid
, euid
, suid
;
6730 if (!PyArg_ParseTuple(args
, "lll", &ruid
, &euid
, &suid
))
6732 if (setresuid(ruid
, euid
, suid
) < 0)
6733 return posix_error();
6738 #ifdef HAVE_SETRESGID
6739 PyDoc_STRVAR(posix_setresgid__doc__
,
6740 "setresgid(rgid, egid, sgid)\n\n\
6741 Set the current process's real, effective, and saved group ids.");
6744 posix_setresgid (PyObject
*self
, PyObject
*args
)
6746 /* We assume uid_t is no larger than a long. */
6747 long rgid
, egid
, sgid
;
6748 if (!PyArg_ParseTuple(args
, "lll", &rgid
, &egid
, &sgid
))
6750 if (setresgid(rgid
, egid
, sgid
) < 0)
6751 return posix_error();
6756 #ifdef HAVE_GETRESUID
6757 PyDoc_STRVAR(posix_getresuid__doc__
,
6758 "getresuid() -> (ruid, euid, suid)\n\n\
6759 Get tuple of the current process's real, effective, and saved user ids.");
6762 posix_getresuid (PyObject
*self
, PyObject
*noargs
)
6764 uid_t ruid
, euid
, suid
;
6765 long l_ruid
, l_euid
, l_suid
;
6766 if (getresuid(&ruid
, &euid
, &suid
) < 0)
6767 return posix_error();
6768 /* Force the values into long's as we don't know the size of uid_t. */
6772 return Py_BuildValue("(lll)", l_ruid
, l_euid
, l_suid
);
6776 #ifdef HAVE_GETRESGID
6777 PyDoc_STRVAR(posix_getresgid__doc__
,
6778 "getresgid() -> (rgid, egid, sgid)\n\n\
6779 Get tuple of the current process's real, effective, and saved group ids.");
6782 posix_getresgid (PyObject
*self
, PyObject
*noargs
)
6784 uid_t rgid
, egid
, sgid
;
6785 long l_rgid
, l_egid
, l_sgid
;
6786 if (getresgid(&rgid
, &egid
, &sgid
) < 0)
6787 return posix_error();
6788 /* Force the values into long's as we don't know the size of uid_t. */
6792 return Py_BuildValue("(lll)", l_rgid
, l_egid
, l_sgid
);
6796 static PyMethodDef posix_methods
[] = {
6797 {"access", posix_access
, METH_VARARGS
, posix_access__doc__
},
6799 {"ttyname", posix_ttyname
, METH_VARARGS
, posix_ttyname__doc__
},
6801 {"chdir", posix_chdir
, METH_VARARGS
, posix_chdir__doc__
},
6803 {"chflags", posix_chflags
, METH_VARARGS
, posix_chflags__doc__
},
6804 #endif /* HAVE_CHFLAGS */
6805 {"chmod", posix_chmod
, METH_VARARGS
, posix_chmod__doc__
},
6807 {"fchmod", posix_fchmod
, METH_VARARGS
, posix_fchmod__doc__
},
6808 #endif /* HAVE_FCHMOD */
6810 {"chown", posix_chown
, METH_VARARGS
, posix_chown__doc__
},
6811 #endif /* HAVE_CHOWN */
6813 {"lchmod", posix_lchmod
, METH_VARARGS
, posix_lchmod__doc__
},
6814 #endif /* HAVE_LCHMOD */
6816 {"fchown", posix_fchown
, METH_VARARGS
, posix_fchown__doc__
},
6817 #endif /* HAVE_FCHOWN */
6818 #ifdef HAVE_LCHFLAGS
6819 {"lchflags", posix_lchflags
, METH_VARARGS
, posix_lchflags__doc__
},
6820 #endif /* HAVE_LCHFLAGS */
6822 {"lchown", posix_lchown
, METH_VARARGS
, posix_lchown__doc__
},
6823 #endif /* HAVE_LCHOWN */
6825 {"chroot", posix_chroot
, METH_VARARGS
, posix_chroot__doc__
},
6828 {"ctermid", posix_ctermid
, METH_NOARGS
, posix_ctermid__doc__
},
6831 {"getcwd", posix_getcwd
, METH_NOARGS
, posix_getcwd__doc__
},
6832 #ifdef Py_USING_UNICODE
6833 {"getcwdu", posix_getcwdu
, METH_NOARGS
, posix_getcwdu__doc__
},
6837 {"link", posix_link
, METH_VARARGS
, posix_link__doc__
},
6838 #endif /* HAVE_LINK */
6839 {"listdir", posix_listdir
, METH_VARARGS
, posix_listdir__doc__
},
6840 {"lstat", posix_lstat
, METH_VARARGS
, posix_lstat__doc__
},
6841 {"mkdir", posix_mkdir
, METH_VARARGS
, posix_mkdir__doc__
},
6843 {"nice", posix_nice
, METH_VARARGS
, posix_nice__doc__
},
6844 #endif /* HAVE_NICE */
6845 #ifdef HAVE_READLINK
6846 {"readlink", posix_readlink
, METH_VARARGS
, posix_readlink__doc__
},
6847 #endif /* HAVE_READLINK */
6848 {"rename", posix_rename
, METH_VARARGS
, posix_rename__doc__
},
6849 {"rmdir", posix_rmdir
, METH_VARARGS
, posix_rmdir__doc__
},
6850 {"stat", posix_stat
, METH_VARARGS
, posix_stat__doc__
},
6851 //{"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
6853 {"symlink", posix_symlink
, METH_VARARGS
, posix_symlink__doc__
},
6854 #endif /* HAVE_SYMLINK */
6856 {"system", posix_system
, METH_VARARGS
, posix_system__doc__
},
6858 {"umask", posix_umask
, METH_VARARGS
, posix_umask__doc__
},
6860 {"uname", posix_uname
, METH_NOARGS
, posix_uname__doc__
},
6861 #endif /* HAVE_UNAME */
6862 {"unlink", posix_unlink
, METH_VARARGS
, posix_unlink__doc__
},
6863 {"remove", posix_unlink
, METH_VARARGS
, posix_remove__doc__
},
6864 {"utime", posix_utime
, METH_VARARGS
, posix_utime__doc__
},
6866 {"times", posix_times
, METH_NOARGS
, posix_times__doc__
},
6867 #endif /* HAVE_TIMES */
6868 {"_exit", posix__exit
, METH_VARARGS
, posix__exit__doc__
},
6870 {"execv", posix_execv
, METH_VARARGS
, posix_execv__doc__
},
6871 {"execve", posix_execve
, METH_VARARGS
, posix_execve__doc__
},
6872 #endif /* HAVE_EXECV */
6874 {"spawnv", posix_spawnv
, METH_VARARGS
, posix_spawnv__doc__
},
6875 {"spawnve", posix_spawnve
, METH_VARARGS
, posix_spawnve__doc__
},
6876 #if defined(PYOS_OS2)
6877 {"spawnvp", posix_spawnvp
, METH_VARARGS
, posix_spawnvp__doc__
},
6878 {"spawnvpe", posix_spawnvpe
, METH_VARARGS
, posix_spawnvpe__doc__
},
6879 #endif /* PYOS_OS2 */
6880 #endif /* HAVE_SPAWNV */
6882 {"fork1", posix_fork1
, METH_NOARGS
, posix_fork1__doc__
},
6883 #endif /* HAVE_FORK1 */
6885 {"fork", posix_fork
, METH_NOARGS
, posix_fork__doc__
},
6886 #endif /* HAVE_FORK */
6887 #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
6888 {"openpty", posix_openpty
, METH_NOARGS
, posix_openpty__doc__
},
6889 #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
6891 {"forkpty", posix_forkpty
, METH_NOARGS
, posix_forkpty__doc__
},
6892 #endif /* HAVE_FORKPTY */
6894 {"getegid", posix_getegid
, METH_NOARGS
, posix_getegid__doc__
},
6895 #endif /* HAVE_GETEGID */
6897 {"geteuid", posix_geteuid
, METH_NOARGS
, posix_geteuid__doc__
},
6898 #endif /* HAVE_GETEUID */
6900 {"getgid", posix_getgid
, METH_NOARGS
, posix_getgid__doc__
},
6901 #endif /* HAVE_GETGID */
6902 #ifdef HAVE_GETGROUPS
6903 {"getgroups", posix_getgroups
, METH_NOARGS
, posix_getgroups__doc__
},
6905 {"getpid", posix_getpid
, METH_NOARGS
, posix_getpid__doc__
},
6907 {"getpgrp", posix_getpgrp
, METH_NOARGS
, posix_getpgrp__doc__
},
6908 #endif /* HAVE_GETPGRP */
6910 {"getppid", posix_getppid
, METH_NOARGS
, posix_getppid__doc__
},
6911 #endif /* HAVE_GETPPID */
6913 {"getuid", posix_getuid
, METH_NOARGS
, posix_getuid__doc__
},
6914 #endif /* HAVE_GETUID */
6915 #ifdef HAVE_GETLOGIN
6916 {"getlogin", posix_getlogin
, METH_NOARGS
, posix_getlogin__doc__
},
6919 {"kill", posix_kill
, METH_VARARGS
, posix_kill__doc__
},
6920 #endif /* HAVE_KILL */
6922 {"killpg", posix_killpg
, METH_VARARGS
, posix_killpg__doc__
},
6923 #endif /* HAVE_KILLPG */
6925 {"plock", posix_plock
, METH_VARARGS
, posix_plock__doc__
},
6926 #endif /* HAVE_PLOCK */
6928 {"popen", posix_popen
, METH_VARARGS
, posix_popen__doc__
},
6930 {"popen2", win32_popen2
, METH_VARARGS
},
6931 {"popen3", win32_popen3
, METH_VARARGS
},
6932 {"popen4", win32_popen4
, METH_VARARGS
},
6933 {"startfile", win32_startfile
, METH_VARARGS
, win32_startfile__doc__
},
6934 {"kill", win32_kill
, METH_VARARGS
, win32_kill__doc__
},
6936 #if defined(PYOS_OS2) && defined(PYCC_GCC)
6937 {"popen2", os2emx_popen2
, METH_VARARGS
},
6938 {"popen3", os2emx_popen3
, METH_VARARGS
},
6939 {"popen4", os2emx_popen4
, METH_VARARGS
},
6942 #endif /* HAVE_POPEN */
6944 {"setuid", posix_setuid
, METH_VARARGS
, posix_setuid__doc__
},
6945 #endif /* HAVE_SETUID */
6947 {"seteuid", posix_seteuid
, METH_VARARGS
, posix_seteuid__doc__
},
6948 #endif /* HAVE_SETEUID */
6950 {"setegid", posix_setegid
, METH_VARARGS
, posix_setegid__doc__
},
6951 #endif /* HAVE_SETEGID */
6952 #ifdef HAVE_SETREUID
6953 {"setreuid", posix_setreuid
, METH_VARARGS
, posix_setreuid__doc__
},
6954 #endif /* HAVE_SETREUID */
6955 #ifdef HAVE_SETREGID
6956 {"setregid", posix_setregid
, METH_VARARGS
, posix_setregid__doc__
},
6957 #endif /* HAVE_SETREGID */
6959 {"setgid", posix_setgid
, METH_VARARGS
, posix_setgid__doc__
},
6960 #endif /* HAVE_SETGID */
6961 #ifdef HAVE_SETGROUPS
6962 {"setgroups", posix_setgroups
, METH_O
, posix_setgroups__doc__
},
6963 #endif /* HAVE_SETGROUPS */
6964 #ifdef HAVE_INITGROUPS
6965 {"initgroups", posix_initgroups
, METH_VARARGS
, posix_initgroups__doc__
},
6966 #endif /* HAVE_INITGROUPS */
6968 {"getpgid", posix_getpgid
, METH_VARARGS
, posix_getpgid__doc__
},
6969 #endif /* HAVE_GETPGID */
6971 {"setpgrp", posix_setpgrp
, METH_NOARGS
, posix_setpgrp__doc__
},
6972 #endif /* HAVE_SETPGRP */
6974 {"wait", posix_wait
, METH_NOARGS
, posix_wait__doc__
},
6975 #endif /* HAVE_WAIT */
6977 {"wait3", posix_wait3
, METH_VARARGS
, posix_wait3__doc__
},
6978 #endif /* HAVE_WAIT3 */
6980 {"wait4", posix_wait4
, METH_VARARGS
, posix_wait4__doc__
},
6981 #endif /* HAVE_WAIT4 */
6982 #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
6983 {"waitpid", posix_waitpid
, METH_VARARGS
, posix_waitpid__doc__
},
6984 #endif /* HAVE_WAITPID */
6986 {"getsid", posix_getsid
, METH_VARARGS
, posix_getsid__doc__
},
6987 #endif /* HAVE_GETSID */
6989 {"setsid", posix_setsid
, METH_NOARGS
, posix_setsid__doc__
},
6990 #endif /* HAVE_SETSID */
6992 {"setpgid", posix_setpgid
, METH_VARARGS
, posix_setpgid__doc__
},
6993 #endif /* HAVE_SETPGID */
6994 #ifdef HAVE_TCGETPGRP
6995 {"tcgetpgrp", posix_tcgetpgrp
, METH_VARARGS
, posix_tcgetpgrp__doc__
},
6996 #endif /* HAVE_TCGETPGRP */
6997 #ifdef HAVE_TCSETPGRP
6998 {"tcsetpgrp", posix_tcsetpgrp
, METH_VARARGS
, posix_tcsetpgrp__doc__
},
6999 #endif /* HAVE_TCSETPGRP */
7000 {"open", posix_open
, METH_VARARGS
, posix_open__doc__
},
7001 {"close", posix_close
, METH_VARARGS
, posix_close__doc__
},
7002 {"closerange", posix_closerange
, METH_VARARGS
, posix_closerange__doc__
},
7003 {"dup", posix_dup
, METH_VARARGS
, posix_dup__doc__
},
7004 {"dup2", posix_dup2
, METH_VARARGS
, posix_dup2__doc__
},
7005 {"lseek", posix_lseek
, METH_VARARGS
, posix_lseek__doc__
},
7006 {"read", posix_read
, METH_VARARGS
, posix_read__doc__
},
7007 {"write", posix_write
, METH_VARARGS
, posix_write__doc__
},
7008 {"fstat", posix_fstat
, METH_VARARGS
, posix_fstat__doc__
},
7009 {"fdopen", posix_fdopen
, METH_VARARGS
, posix_fdopen__doc__
},
7010 {"isatty", posix_isatty
, METH_VARARGS
, posix_isatty__doc__
},
7012 {"pipe", posix_pipe
, METH_NOARGS
, posix_pipe__doc__
},
7015 {"mkfifo", posix_mkfifo
, METH_VARARGS
, posix_mkfifo__doc__
},
7017 #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
7018 {"mknod", posix_mknod
, METH_VARARGS
, posix_mknod__doc__
},
7020 #ifdef HAVE_DEVICE_MACROS
7021 {"major", posix_major
, METH_VARARGS
, posix_major__doc__
},
7022 {"minor", posix_minor
, METH_VARARGS
, posix_minor__doc__
},
7023 {"makedev", posix_makedev
, METH_VARARGS
, posix_makedev__doc__
},
7025 #ifdef HAVE_FTRUNCATE
7026 {"ftruncate", posix_ftruncate
, METH_VARARGS
, posix_ftruncate__doc__
},
7029 {"putenv", posix_putenv
, METH_VARARGS
, posix_putenv__doc__
},
7031 #ifdef HAVE_UNSETENV
7032 {"unsetenv", posix_unsetenv
, METH_VARARGS
, posix_unsetenv__doc__
},
7034 {"strerror", posix_strerror
, METH_VARARGS
, posix_strerror__doc__
},
7036 {"fchdir", posix_fchdir
, METH_O
, posix_fchdir__doc__
},
7039 {"fsync", posix_fsync
, METH_O
, posix_fsync__doc__
},
7041 #ifdef HAVE_FDATASYNC
7042 {"fdatasync", posix_fdatasync
, METH_O
, posix_fdatasync__doc__
},
7044 #ifdef HAVE_SYS_WAIT_H
7046 {"WCOREDUMP", posix_WCOREDUMP
, METH_VARARGS
, posix_WCOREDUMP__doc__
},
7047 #endif /* WCOREDUMP */
7049 {"WIFCONTINUED",posix_WIFCONTINUED
, METH_VARARGS
, posix_WIFCONTINUED__doc__
},
7050 #endif /* WIFCONTINUED */
7052 {"WIFSTOPPED", posix_WIFSTOPPED
, METH_VARARGS
, posix_WIFSTOPPED__doc__
},
7053 #endif /* WIFSTOPPED */
7055 {"WIFSIGNALED", posix_WIFSIGNALED
, METH_VARARGS
, posix_WIFSIGNALED__doc__
},
7056 #endif /* WIFSIGNALED */
7058 {"WIFEXITED", posix_WIFEXITED
, METH_VARARGS
, posix_WIFEXITED__doc__
},
7059 #endif /* WIFEXITED */
7061 {"WEXITSTATUS", posix_WEXITSTATUS
, METH_VARARGS
, posix_WEXITSTATUS__doc__
},
7062 #endif /* WEXITSTATUS */
7064 {"WTERMSIG", posix_WTERMSIG
, METH_VARARGS
, posix_WTERMSIG__doc__
},
7065 #endif /* WTERMSIG */
7067 {"WSTOPSIG", posix_WSTOPSIG
, METH_VARARGS
, posix_WSTOPSIG__doc__
},
7068 #endif /* WSTOPSIG */
7069 #endif /* HAVE_SYS_WAIT_H */
7070 #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
7071 {"fstatvfs", posix_fstatvfs
, METH_VARARGS
, posix_fstatvfs__doc__
},
7073 #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
7074 {"statvfs", posix_statvfs
, METH_VARARGS
, posix_statvfs__doc__
},
7077 {"tmpfile", posix_tmpfile
, METH_NOARGS
, posix_tmpfile__doc__
},
7080 {"tempnam", posix_tempnam
, METH_VARARGS
, posix_tempnam__doc__
},
7083 {"tmpnam", posix_tmpnam
, METH_NOARGS
, posix_tmpnam__doc__
},
7086 {"confstr", posix_confstr
, METH_VARARGS
, posix_confstr__doc__
},
7089 {"sysconf", posix_sysconf
, METH_VARARGS
, posix_sysconf__doc__
},
7091 #ifdef HAVE_FPATHCONF
7092 {"fpathconf", posix_fpathconf
, METH_VARARGS
, posix_fpathconf__doc__
},
7094 #ifdef HAVE_PATHCONF
7095 {"pathconf", posix_pathconf
, METH_VARARGS
, posix_pathconf__doc__
},
7097 {"abort", posix_abort
, METH_NOARGS
, posix_abort__doc__
},
7098 #ifdef HAVE_SETRESUID
7099 {"setresuid", posix_setresuid
, METH_VARARGS
, posix_setresuid__doc__
},
7101 #ifdef HAVE_SETRESGID
7102 {"setresgid", posix_setresgid
, METH_VARARGS
, posix_setresgid__doc__
},
7104 #ifdef HAVE_GETRESUID
7105 {"getresuid", posix_getresuid
, METH_NOARGS
, posix_getresuid__doc__
},
7107 #ifdef HAVE_GETRESGID
7108 {"getresgid", posix_getresgid
, METH_NOARGS
, posix_getresgid__doc__
},
7111 {NULL
, NULL
} /* Sentinel */
7116 ins(PyObject
*module
, char *symbol
, long value
)
7118 return PyModule_AddIntConstant(module
, symbol
, value
);
7122 all_ins(PyObject
*d
)
7125 if (ins(d
, "F_OK", (long)F_OK
)) return -1;
7128 if (ins(d
, "R_OK", (long)R_OK
)) return -1;
7131 if (ins(d
, "W_OK", (long)W_OK
)) return -1;
7134 if (ins(d
, "X_OK", (long)X_OK
)) return -1;
7137 if (ins(d
, "NGROUPS_MAX", (long)NGROUPS_MAX
)) return -1;
7140 if (ins(d
, "TMP_MAX", (long)TMP_MAX
)) return -1;
7143 if (ins(d
, "WCONTINUED", (long)WCONTINUED
)) return -1;
7146 if (ins(d
, "WNOHANG", (long)WNOHANG
)) return -1;
7149 if (ins(d
, "WUNTRACED", (long)WUNTRACED
)) return -1;
7152 if (ins(d
, "O_RDONLY", (long)O_RDONLY
)) return -1;
7155 if (ins(d
, "O_WRONLY", (long)O_WRONLY
)) return -1;
7158 if (ins(d
, "O_RDWR", (long)O_RDWR
)) return -1;
7161 if (ins(d
, "O_NDELAY", (long)O_NDELAY
)) return -1;
7164 if (ins(d
, "O_NONBLOCK", (long)O_NONBLOCK
)) return -1;
7167 if (ins(d
, "O_APPEND", (long)O_APPEND
)) return -1;
7170 if (ins(d
, "O_DSYNC", (long)O_DSYNC
)) return -1;
7173 if (ins(d
, "O_RSYNC", (long)O_RSYNC
)) return -1;
7176 if (ins(d
, "O_SYNC", (long)O_SYNC
)) return -1;
7179 if (ins(d
, "O_NOCTTY", (long)O_NOCTTY
)) return -1;
7182 if (ins(d
, "O_CREAT", (long)O_CREAT
)) return -1;
7185 if (ins(d
, "O_EXCL", (long)O_EXCL
)) return -1;
7188 if (ins(d
, "O_TRUNC", (long)O_TRUNC
)) return -1;
7191 if (ins(d
, "O_BINARY", (long)O_BINARY
)) return -1;
7194 if (ins(d
, "O_TEXT", (long)O_TEXT
)) return -1;
7197 if (ins(d
, "O_LARGEFILE", (long)O_LARGEFILE
)) return -1;
7200 if (ins(d
, "O_SHLOCK", (long)O_SHLOCK
)) return -1;
7203 if (ins(d
, "O_EXLOCK", (long)O_EXLOCK
)) return -1;
7208 /* Don't inherit in child processes. */
7209 if (ins(d
, "O_NOINHERIT", (long)O_NOINHERIT
)) return -1;
7211 #ifdef _O_SHORT_LIVED
7212 /* Optimize for short life (keep in memory). */
7213 /* MS forgot to define this one with a non-underscore form too. */
7214 if (ins(d
, "O_SHORT_LIVED", (long)_O_SHORT_LIVED
)) return -1;
7217 /* Automatically delete when last handle is closed. */
7218 if (ins(d
, "O_TEMPORARY", (long)O_TEMPORARY
)) return -1;
7221 /* Optimize for random access. */
7222 if (ins(d
, "O_RANDOM", (long)O_RANDOM
)) return -1;
7225 /* Optimize for sequential access. */
7226 if (ins(d
, "O_SEQUENTIAL", (long)O_SEQUENTIAL
)) return -1;
7229 /* GNU extensions. */
7231 /* Send a SIGIO signal whenever input or output
7232 becomes available on file descriptor */
7233 if (ins(d
, "O_ASYNC", (long)O_ASYNC
)) return -1;
7236 /* Direct disk access. */
7237 if (ins(d
, "O_DIRECT", (long)O_DIRECT
)) return -1;
7240 /* Must be a directory. */
7241 if (ins(d
, "O_DIRECTORY", (long)O_DIRECTORY
)) return -1;
7244 /* Do not follow links. */
7245 if (ins(d
, "O_NOFOLLOW", (long)O_NOFOLLOW
)) return -1;
7248 /* Do not update the access time. */
7249 if (ins(d
, "O_NOATIME", (long)O_NOATIME
)) return -1;
7252 /* These come from sysexits.h */
7254 if (ins(d
, "EX_OK", (long)EX_OK
)) return -1;
7257 if (ins(d
, "EX_USAGE", (long)EX_USAGE
)) return -1;
7258 #endif /* EX_USAGE */
7260 if (ins(d
, "EX_DATAERR", (long)EX_DATAERR
)) return -1;
7261 #endif /* EX_DATAERR */
7263 if (ins(d
, "EX_NOINPUT", (long)EX_NOINPUT
)) return -1;
7264 #endif /* EX_NOINPUT */
7266 if (ins(d
, "EX_NOUSER", (long)EX_NOUSER
)) return -1;
7267 #endif /* EX_NOUSER */
7269 if (ins(d
, "EX_NOHOST", (long)EX_NOHOST
)) return -1;
7270 #endif /* EX_NOHOST */
7271 #ifdef EX_UNAVAILABLE
7272 if (ins(d
, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE
)) return -1;
7273 #endif /* EX_UNAVAILABLE */
7275 if (ins(d
, "EX_SOFTWARE", (long)EX_SOFTWARE
)) return -1;
7276 #endif /* EX_SOFTWARE */
7278 if (ins(d
, "EX_OSERR", (long)EX_OSERR
)) return -1;
7279 #endif /* EX_OSERR */
7281 if (ins(d
, "EX_OSFILE", (long)EX_OSFILE
)) return -1;
7282 #endif /* EX_OSFILE */
7284 if (ins(d
, "EX_CANTCREAT", (long)EX_CANTCREAT
)) return -1;
7285 #endif /* EX_CANTCREAT */
7287 if (ins(d
, "EX_IOERR", (long)EX_IOERR
)) return -1;
7288 #endif /* EX_IOERR */
7290 if (ins(d
, "EX_TEMPFAIL", (long)EX_TEMPFAIL
)) return -1;
7291 #endif /* EX_TEMPFAIL */
7293 if (ins(d
, "EX_PROTOCOL", (long)EX_PROTOCOL
)) return -1;
7294 #endif /* EX_PROTOCOL */
7296 if (ins(d
, "EX_NOPERM", (long)EX_NOPERM
)) return -1;
7297 #endif /* EX_NOPERM */
7299 if (ins(d
, "EX_CONFIG", (long)EX_CONFIG
)) return -1;
7300 #endif /* EX_CONFIG */
7302 if (ins(d
, "EX_NOTFOUND", (long)EX_NOTFOUND
)) return -1;
7303 #endif /* EX_NOTFOUND */
7306 #if defined(PYOS_OS2) && defined(PYCC_GCC)
7307 if (ins(d
, "P_WAIT", (long)P_WAIT
)) return -1;
7308 if (ins(d
, "P_NOWAIT", (long)P_NOWAIT
)) return -1;
7309 if (ins(d
, "P_OVERLAY", (long)P_OVERLAY
)) return -1;
7310 if (ins(d
, "P_DEBUG", (long)P_DEBUG
)) return -1;
7311 if (ins(d
, "P_SESSION", (long)P_SESSION
)) return -1;
7312 if (ins(d
, "P_DETACH", (long)P_DETACH
)) return -1;
7313 if (ins(d
, "P_PM", (long)P_PM
)) return -1;
7314 if (ins(d
, "P_DEFAULT", (long)P_DEFAULT
)) return -1;
7315 if (ins(d
, "P_MINIMIZE", (long)P_MINIMIZE
)) return -1;
7316 if (ins(d
, "P_MAXIMIZE", (long)P_MAXIMIZE
)) return -1;
7317 if (ins(d
, "P_FULLSCREEN", (long)P_FULLSCREEN
)) return -1;
7318 if (ins(d
, "P_WINDOWED", (long)P_WINDOWED
)) return -1;
7319 if (ins(d
, "P_FOREGROUND", (long)P_FOREGROUND
)) return -1;
7320 if (ins(d
, "P_BACKGROUND", (long)P_BACKGROUND
)) return -1;
7321 if (ins(d
, "P_NOCLOSE", (long)P_NOCLOSE
)) return -1;
7322 if (ins(d
, "P_NOSESSION", (long)P_NOSESSION
)) return -1;
7323 if (ins(d
, "P_QUOTE", (long)P_QUOTE
)) return -1;
7324 if (ins(d
, "P_TILDE", (long)P_TILDE
)) return -1;
7325 if (ins(d
, "P_UNRELATED", (long)P_UNRELATED
)) return -1;
7326 if (ins(d
, "P_DEBUGDESC", (long)P_DEBUGDESC
)) return -1;
7328 if (ins(d
, "P_WAIT", (long)_P_WAIT
)) return -1;
7329 if (ins(d
, "P_NOWAIT", (long)_P_NOWAIT
)) return -1;
7330 if (ins(d
, "P_OVERLAY", (long)_OLD_P_OVERLAY
)) return -1;
7331 if (ins(d
, "P_NOWAITO", (long)_P_NOWAITO
)) return -1;
7332 if (ins(d
, "P_DETACH", (long)_P_DETACH
)) return -1;
7338 #define INITFUNC initedk2
7339 #define MODNAME "edk2"
7346 m
= Py_InitModule3(MODNAME
,
7353 /* Initialize environ dictionary */
7354 v
= convertenviron();
7356 if (v
== NULL
|| PyModule_AddObject(m
, "environ", v
) != 0)
7359 #endif /* UEFI_ENV */
7364 if (setup_confname_tables(m
))
7367 Py_INCREF(PyExc_OSError
);
7368 PyModule_AddObject(m
, "error", PyExc_OSError
);
7371 if (posix_putenv_garbage
== NULL
)
7372 posix_putenv_garbage
= PyDict_New();
7376 stat_result_desc
.name
= MODNAME
".stat_result";
7377 stat_result_desc
.fields
[2].name
= PyStructSequence_UnnamedField
;
7378 stat_result_desc
.fields
[3].name
= PyStructSequence_UnnamedField
;
7379 stat_result_desc
.fields
[4].name
= PyStructSequence_UnnamedField
;
7380 PyStructSequence_InitType(&StatResultType
, &stat_result_desc
);
7381 structseq_new
= StatResultType
.tp_new
;
7382 StatResultType
.tp_new
= statresult_new
;
7384 //statvfs_result_desc.name = MODNAME ".statvfs_result";
7385 //PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
7386 #ifdef NEED_TICKS_PER_SECOND
7387 # if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
7388 ticks_per_second
= sysconf(_SC_CLK_TCK
);
7390 ticks_per_second
= HZ
;
7392 ticks_per_second
= 60; /* magic fallback value; may be bogus */
7396 Py_INCREF((PyObject
*) &StatResultType
);
7397 PyModule_AddObject(m
, "stat_result", (PyObject
*) &StatResultType
);
7398 //Py_INCREF((PyObject*) &StatVFSResultType);
7399 //PyModule_AddObject(m, "statvfs_result",
7400 // (PyObject*) &StatVFSResultType);