2 /* POSIX module implementation */
4 /* This file is also used for Windows NT/MS-Win and OS/2. In that case the
5 module actually calls itself 'nt' or 'os2', not 'posix', and a few
6 functions are either unimplemented or implemented differently. The source
7 assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent
8 of the compiler used. Different compilers define their own feature
9 test macro, e.g. '__BORLANDC__' or '_MSC_VER'. For OS/2, the compiler
10 independent macro PYOS_OS2 should be defined. On OS/2 the default
11 compiler is assumed to be IBM's VisualAge C++ (VACPP). PYCC_GCC is used
12 as the compiler specific macro for the EMX port of gcc to OS/2. */
14 /* See also ../Dos/dosmodule.c */
18 * Step 1 of support for weak-linking a number of symbols existing on
19 * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block
20 * at the end of this file for more information.
24 # pragma weak fstatvfs
26 #endif /* __APPLE__ */
28 #define PY_SSIZE_T_CLEAN
31 #include "structseq.h"
35 #endif /* defined(__VMS) */
41 PyDoc_STRVAR(posix__doc__
,
42 "This module provides access to operating system functionality that is\n\
43 standardized by the C Standard and the POSIX standard (a thinly\n\
44 disguised Unix interface). Refer to the library manual and\n\
45 corresponding Unix manual entries for more information on calls.");
47 #ifndef Py_USING_UNICODE
48 /* This is used in signatures of functions. */
49 #define Py_UNICODE void
54 #define INCL_DOSERRORS
55 #define INCL_DOSPROCESS
67 #ifdef HAVE_SYS_TYPES_H
68 #include <sys/types.h>
69 #endif /* HAVE_SYS_TYPES_H */
71 #ifdef HAVE_SYS_STAT_H
73 #endif /* HAVE_SYS_STAT_H */
75 #ifdef HAVE_SYS_WAIT_H
76 #include <sys/wait.h> /* For WNOHANG */
85 #endif /* HAVE_FCNTL_H */
91 #ifdef HAVE_SYSEXITS_H
93 #endif /* HAVE_SYSEXITS_H */
95 #ifdef HAVE_SYS_LOADAVG_H
96 #include <sys/loadavg.h>
99 /* Various compilers have only certain posix functions */
100 /* XXX Gosh I wish these were all moved into pyconfig.h */
101 #if defined(PYCC_VACPP) && defined(PYOS_OS2)
104 #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
105 #define HAVE_GETCWD 1
106 #define HAVE_OPENDIR 1
107 #define HAVE_SYSTEM 1
114 #ifdef __BORLANDC__ /* Borland compiler */
116 #define HAVE_GETCWD 1
117 #define HAVE_OPENDIR 1
120 #define HAVE_SYSTEM 1
123 #ifdef _MSC_VER /* Microsoft compiler */
124 #define HAVE_GETCWD 1
125 #define HAVE_SPAWNV 1
129 #define HAVE_SYSTEM 1
132 #define fsync _commit
134 #if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS)
135 /* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */
136 #else /* all other compilers */
137 /* Unix functions that the configure script doesn't check for */
140 #if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
143 #define HAVE_GETCWD 1
144 #define HAVE_GETEGID 1
145 #define HAVE_GETEUID 1
146 #define HAVE_GETGID 1
147 #define HAVE_GETPPID 1
148 #define HAVE_GETUID 1
150 #define HAVE_OPENDIR 1
155 #define HAVE_SYSTEM 1
157 #define HAVE_TTYNAME 1
158 #endif /* PYOS_OS2 && PYCC_GCC && __VMS */
159 #endif /* _MSC_VER */
160 #endif /* __BORLANDC__ */
161 #endif /* ! __WATCOMC__ || __QNX__ */
162 #endif /* ! __IBMC__ */
166 #if defined(__sgi)&&_COMPILER_VERSION>=700
167 /* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
169 extern char *ctermid_r(char *);
172 #ifndef HAVE_UNISTD_H
173 #if defined(PYCC_VACPP)
174 extern int mkdir(char *);
176 #if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
177 extern int mkdir(const char *);
179 extern int mkdir(const char *, mode_t
);
182 #if defined(__IBMC__) || defined(__IBMCPP__)
183 extern int chdir(char *);
184 extern int rmdir(char *);
186 extern int chdir(const char *);
187 extern int rmdir(const char *);
190 extern int chmod(const char *, int);
192 extern int chmod(const char *, mode_t
);
195 extern int fchmod(int, mode_t);
198 extern int lchmod(const char *, mode_t);
200 extern int chown(const char *, uid_t
, gid_t
);
201 extern char *getcwd(char *, int);
202 extern char *strerror(int);
203 extern int link(const char *, const char *);
204 extern int rename(const char *, const char *);
205 extern int stat(const char *, struct stat
*);
206 extern int unlink(const char *);
207 extern int pclose(FILE *);
209 extern int symlink(const char *, const char *);
210 #endif /* HAVE_SYMLINK */
212 extern int lstat(const char *, struct stat
*);
213 #endif /* HAVE_LSTAT */
214 #endif /* !HAVE_UNISTD_H */
216 #endif /* !_MSC_VER */
220 #endif /* HAVE_UTIME_H */
222 #ifdef HAVE_SYS_UTIME_H
223 #include <sys/utime.h>
224 #define HAVE_UTIME_H /* pretend we do for the rest of this file */
225 #endif /* HAVE_SYS_UTIME_H */
227 #ifdef HAVE_SYS_TIMES_H
228 #include <sys/times.h>
229 #endif /* HAVE_SYS_TIMES_H */
231 #ifdef HAVE_SYS_PARAM_H
232 #include <sys/param.h>
233 #endif /* HAVE_SYS_PARAM_H */
235 #ifdef HAVE_SYS_UTSNAME_H
236 #include <sys/utsname.h>
237 #endif /* HAVE_SYS_UTSNAME_H */
241 #define NAMLEN(dirent) strlen((dirent)->d_name)
243 #if defined(__WATCOMC__) && !defined(__QNX__)
245 #define NAMLEN(dirent) strlen((dirent)->d_name)
247 #define dirent direct
248 #define NAMLEN(dirent) (dirent)->d_namlen
250 #ifdef HAVE_SYS_NDIR_H
251 #include <sys/ndir.h>
253 #ifdef HAVE_SYS_DIR_H
268 #ifdef HAVE_PROCESS_H
273 #include <shellapi.h> /* for ShellExecute() */
275 #define pclose _pclose
276 #endif /* _MSC_VER */
278 #if defined(PYCC_VACPP) && defined(PYOS_OS2)
283 #if defined(PATH_MAX) && PATH_MAX > 1024
284 #define MAXPATHLEN PATH_MAX
286 #define MAXPATHLEN 1024
288 #endif /* MAXPATHLEN */
291 /* Emulate some macros on systems that have a union instead of macros */
294 #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
298 #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
302 #define WTERMSIG(u_wait) ((u_wait).w_termsig)
305 #define WAIT_TYPE union wait
306 #define WAIT_STATUS_INT(s) (s.w_status)
308 #else /* !UNION_WAIT */
309 #define WAIT_TYPE int
310 #define WAIT_STATUS_INT(s) (s)
311 #endif /* UNION_WAIT */
313 /* Don't use the "_r" form if we don't need it (also, won't have a
314 prototype for it, at least on Solaris -- maybe others as well?). */
315 #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
316 #define USE_CTERMID_R
319 #if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD)
323 /* choose the appropriate stat and fstat functions and return structs */
325 #if defined(MS_WIN64) || defined(MS_WINDOWS)
326 # define STAT win32_stat
327 # define FSTAT win32_fstat
328 # define STRUCT_STAT struct win32_stat
332 # define STRUCT_STAT struct stat
335 #if defined(MAJOR_IN_MKDEV)
336 #include <sys/mkdev.h>
338 #if defined(MAJOR_IN_SYSMACROS)
339 #include <sys/sysmacros.h>
341 #if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
342 #include <sys/mkdev.h>
346 /* Return a dictionary corresponding to the POSIX environment table */
347 #ifdef WITH_NEXT_FRAMEWORK
348 /* On Darwin/MacOSX a shared library or framework has no access to
349 ** environ directly, we must obtain it with _NSGetEnviron().
351 #include <crt_externs.h>
352 static char **environ
;
353 #elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
354 extern char **environ
;
355 #endif /* !_MSC_VER */
365 #ifdef WITH_NEXT_FRAMEWORK
367 environ
= *_NSGetEnviron();
371 /* This part ignores errors */
372 for (e
= environ
; *e
!= NULL
; e
++) {
375 char *p
= strchr(*e
, '=');
378 k
= PyString_FromStringAndSize(*e
, (int)(p
-*e
));
383 v
= PyString_FromString(p
+1);
389 if (PyDict_GetItem(d
, k
) == NULL
) {
390 if (PyDict_SetItem(d
, k
, v
) != 0)
396 #if defined(PYOS_OS2)
399 char buffer
[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
401 rc
= DosQueryExtLIBPATH(buffer
, BEGIN_LIBPATH
);
402 if (rc
== NO_ERROR
) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
403 PyObject
*v
= PyString_FromString(buffer
);
404 PyDict_SetItemString(d
, "BEGINLIBPATH", v
);
407 rc
= DosQueryExtLIBPATH(buffer
, END_LIBPATH
);
408 if (rc
== NO_ERROR
) { /* (not a typo, envname is NOT 'END_LIBPATH') */
409 PyObject
*v
= PyString_FromString(buffer
);
410 PyDict_SetItemString(d
, "ENDLIBPATH", v
);
419 /* Set a POSIX-specific error from errno, and return NULL */
424 return PyErr_SetFromErrno(PyExc_OSError
);
427 posix_error_with_filename(char* name
)
429 return PyErr_SetFromErrnoWithFilename(PyExc_OSError
, name
);
432 #ifdef Py_WIN_WIDE_FILENAMES
434 posix_error_with_unicode_filename(Py_UNICODE
* name
)
436 return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError
, name
);
438 #endif /* Py_WIN_WIDE_FILENAMES */
442 posix_error_with_allocated_filename(char* name
)
444 PyObject
*rc
= PyErr_SetFromErrnoWithFilename(PyExc_OSError
, name
);
451 win32_error(char* function
, char* filename
)
453 /* XXX We should pass the function name along in the future.
454 (_winreg.c also wants to pass the function name.)
455 This would however require an additional param to the
456 Windows error object, which is non-trivial.
458 errno
= GetLastError();
460 return PyErr_SetFromWindowsErrWithFilename(errno
, filename
);
462 return PyErr_SetFromWindowsErr(errno
);
465 #ifdef Py_WIN_WIDE_FILENAMES
467 win32_error_unicode(char* function
, Py_UNICODE
* filename
)
469 /* XXX - see win32_error for comments on 'function' */
470 errno
= GetLastError();
472 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno
, filename
);
474 return PyErr_SetFromWindowsErr(errno
);
477 static PyObject
*_PyUnicode_FromFileSystemEncodedObject(register PyObject
*obj
)
482 convert_to_unicode(PyObject
**param
)
484 if (PyUnicode_CheckExact(*param
))
486 else if (PyUnicode_Check(*param
))
487 /* For a Unicode subtype that's not a Unicode object,
488 return a true Unicode object with the same data. */
489 *param
= PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param
),
490 PyUnicode_GET_SIZE(*param
));
492 *param
= PyUnicode_FromEncodedObject(*param
,
493 Py_FileSystemDefaultEncoding
,
495 return (*param
) != NULL
;
498 #endif /* Py_WIN_WIDE_FILENAMES */
502 #if defined(PYOS_OS2)
503 /**********************************************************************
504 * Helper Function to Trim and Format OS/2 Messages
505 **********************************************************************/
507 os2_formatmsg(char *msgbuf
, int msglen
, char *reason
)
509 msgbuf
[msglen
] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
511 if (strlen(msgbuf
) > 0) { /* If Non-Empty Msg, Trim CRLF */
512 char *lastc
= &msgbuf
[ strlen(msgbuf
)-1 ];
514 while (lastc
> msgbuf
&& isspace(Py_CHARMASK(*lastc
)))
515 *lastc
-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
518 /* Add Optional Reason Text */
520 strcat(msgbuf
, " : ");
521 strcat(msgbuf
, reason
);
525 /**********************************************************************
526 * Decode an OS/2 Operating System Error Code
528 * A convenience function to lookup an OS/2 error code and return a
529 * text message we can use to raise a Python exception.
532 * The messages for errors returned from the OS/2 kernel reside in
533 * the file OSO001.MSG in the \OS2 directory hierarchy.
535 **********************************************************************/
537 os2_strerror(char *msgbuf
, int msgbuflen
, int errorcode
, char *reason
)
542 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
543 Py_BEGIN_ALLOW_THREADS
544 rc
= DosGetMessage(NULL
, 0, msgbuf
, msgbuflen
,
545 errorcode
, "oso001.msg", &msglen
);
549 os2_formatmsg(msgbuf
, msglen
, reason
);
551 PyOS_snprintf(msgbuf
, msgbuflen
,
552 "unknown OS error #%d", errorcode
);
557 /* Set an OS/2-specific error and return NULL. OS/2 kernel
558 errors are not in a global variable e.g. 'errno' nor are
559 they congruent with posix error numbers. */
561 static PyObject
* os2_error(int code
)
566 os2_strerror(text
, sizeof(text
), code
, "");
568 v
= Py_BuildValue("(is)", code
, text
);
570 PyErr_SetObject(PyExc_OSError
, v
);
573 return NULL
; /* Signal to Python that an Exception is Pending */
578 /* POSIX generic methods */
581 posix_fildes(PyObject
*fdobj
, int (*func
)(int))
585 fd
= PyObject_AsFileDescriptor(fdobj
);
588 Py_BEGIN_ALLOW_THREADS
592 return posix_error();
597 #ifdef Py_WIN_WIDE_FILENAMES
599 unicode_file_names(void)
601 static int canusewide
= -1;
602 if (canusewide
== -1) {
603 /* As per doc for ::GetVersion(), this is the correct test for
604 the Windows NT family. */
605 canusewide
= (GetVersion() < 0x80000000) ? 1 : 0;
612 posix_1str(PyObject
*args
, char *format
, int (*func
)(const char*))
616 if (!PyArg_ParseTuple(args
, format
,
617 Py_FileSystemDefaultEncoding
, &path1
))
619 Py_BEGIN_ALLOW_THREADS
620 res
= (*func
)(path1
);
623 return posix_error_with_allocated_filename(path1
);
630 posix_2str(PyObject
*args
,
632 int (*func
)(const char *, const char *))
634 char *path1
= NULL
, *path2
= NULL
;
636 if (!PyArg_ParseTuple(args
, format
,
637 Py_FileSystemDefaultEncoding
, &path1
,
638 Py_FileSystemDefaultEncoding
, &path2
))
640 Py_BEGIN_ALLOW_THREADS
641 res
= (*func
)(path1
, path2
);
646 /* XXX how to report both path1 and path2??? */
647 return posix_error();
652 #ifdef Py_WIN_WIDE_FILENAMES
654 win32_1str(PyObject
* args
, char* func
,
655 char* format
, BOOL (__stdcall
*funcA
)(LPCSTR
),
656 char* wformat
, BOOL (__stdcall
*funcW
)(LPWSTR
))
661 if (unicode_file_names()) {
662 if (!PyArg_ParseTuple(args
, wformat
, &uni
))
665 Py_BEGIN_ALLOW_THREADS
666 result
= funcW(PyUnicode_AsUnicode(uni
));
669 return win32_error_unicode(func
, PyUnicode_AsUnicode(uni
));
674 if (!PyArg_ParseTuple(args
, format
, &ansi
))
676 Py_BEGIN_ALLOW_THREADS
677 result
= funcA(ansi
);
680 return win32_error(func
, ansi
);
686 /* This is a reimplementation of the C library's chdir function,
687 but one that produces Win32 errors instead of DOS error codes.
688 chdir is essentially a wrapper around SetCurrentDirectory; however,
689 it also needs to set "magic" environment variables indicating
690 the per-drive current directory, which are of the form =<drive>: */
692 win32_chdir(LPCSTR path
)
694 char new_path
[MAX_PATH
+1];
698 if(!SetCurrentDirectoryA(path
))
700 result
= GetCurrentDirectoryA(MAX_PATH
+1, new_path
);
703 /* In the ANSI API, there should not be any paths longer
705 assert(result
<= MAX_PATH
+1);
706 if (strncmp(new_path
, "\\\\", 2) == 0 ||
707 strncmp(new_path
, "//", 2) == 0)
708 /* UNC path, nothing to do. */
710 env
[1] = new_path
[0];
711 return SetEnvironmentVariableA(env
, new_path
);
714 /* The Unicode version differs from the ANSI version
715 since the current directory might exceed MAX_PATH characters */
717 win32_wchdir(LPCWSTR path
)
719 wchar_t _new_path
[MAX_PATH
+1], *new_path
= _new_path
;
721 wchar_t env
[4] = L
"=x:";
723 if(!SetCurrentDirectoryW(path
))
725 result
= GetCurrentDirectoryW(MAX_PATH
+1, new_path
);
728 if (result
> MAX_PATH
+1) {
729 new_path
= malloc(result
);
731 SetLastError(ERROR_OUTOFMEMORY
);
735 if (wcsncmp(new_path
, L
"\\\\", 2) == 0 ||
736 wcsncmp(new_path
, L
"//", 2) == 0)
737 /* UNC path, nothing to do. */
739 env
[1] = new_path
[0];
740 result
= SetEnvironmentVariableW(env
, new_path
);
741 if (new_path
!= _new_path
)
748 /* The CRT of Windows has a number of flaws wrt. its stat() implementation:
749 - time stamps are restricted to second resolution
750 - file modification times suffer from forth-and-back conversions between
752 Therefore, we implement our own stat, based on the Win32 API directly.
754 #define HAVE_STAT_NSEC 1
759 unsigned short st_mode
;
773 static __int64 secs_between_epochs
= 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
776 FILE_TIME_to_time_t_nsec(FILETIME
*in_ptr
, int *time_out
, int* nsec_out
)
778 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
779 /* Cannot simply cast and dereference in_ptr,
780 since it might not be aligned properly */
782 memcpy(&in
, in_ptr
, sizeof(in
));
783 *nsec_out
= (int)(in
% 10000000) * 100; /* FILETIME is in units of 100 nsec. */
784 /* XXX Win32 supports time stamps past 2038; we currently don't */
785 *time_out
= Py_SAFE_DOWNCAST((in
/ 10000000) - secs_between_epochs
, __int64
, int);
789 time_t_to_FILE_TIME(int time_in
, int nsec_in
, FILETIME
*out_ptr
)
793 out
= time_in
+ secs_between_epochs
;
794 out
= out
* 10000000 + nsec_in
/ 100;
795 memcpy(out_ptr
, &out
, sizeof(out
));
798 /* Below, we *know* that ugo+r is 0444 */
800 #error Unsupported C library
803 attributes_to_mode(DWORD attr
)
806 if (attr
& FILE_ATTRIBUTE_DIRECTORY
)
807 m
|= _S_IFDIR
| 0111; /* IFEXEC for user,group,other */
810 if (attr
& FILE_ATTRIBUTE_READONLY
)
818 attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA
*info
, struct win32_stat
*result
)
820 memset(result
, 0, sizeof(*result
));
821 result
->st_mode
= attributes_to_mode(info
->dwFileAttributes
);
822 result
->st_size
= (((__int64
)info
->nFileSizeHigh
)<<32) + info
->nFileSizeLow
;
823 FILE_TIME_to_time_t_nsec(&info
->ftCreationTime
, &result
->st_ctime
, &result
->st_ctime_nsec
);
824 FILE_TIME_to_time_t_nsec(&info
->ftLastWriteTime
, &result
->st_mtime
, &result
->st_mtime_nsec
);
825 FILE_TIME_to_time_t_nsec(&info
->ftLastAccessTime
, &result
->st_atime
, &result
->st_atime_nsec
);
830 /* Emulate GetFileAttributesEx[AW] on Windows 95 */
831 static int checked
= 0;
832 static BOOL (CALLBACK
*gfaxa
)(LPCSTR
, GET_FILEEX_INFO_LEVELS
, LPVOID
);
833 static BOOL (CALLBACK
*gfaxw
)(LPCWSTR
, GET_FILEEX_INFO_LEVELS
, LPVOID
);
841 hKernel32
= GetModuleHandle("KERNEL32");
842 *(FARPROC
*)&gfaxa
= GetProcAddress(hKernel32
, "GetFileAttributesExA");
843 *(FARPROC
*)&gfaxw
= GetProcAddress(hKernel32
, "GetFileAttributesExW");
847 attributes_from_dir(LPCSTR pszFile
, LPWIN32_FILE_ATTRIBUTE_DATA pfad
)
850 WIN32_FIND_DATAA FileData
;
851 hFindFile
= FindFirstFileA(pszFile
, &FileData
);
852 if (hFindFile
== INVALID_HANDLE_VALUE
)
854 FindClose(hFindFile
);
855 pfad
->dwFileAttributes
= FileData
.dwFileAttributes
;
856 pfad
->ftCreationTime
= FileData
.ftCreationTime
;
857 pfad
->ftLastAccessTime
= FileData
.ftLastAccessTime
;
858 pfad
->ftLastWriteTime
= FileData
.ftLastWriteTime
;
859 pfad
->nFileSizeHigh
= FileData
.nFileSizeHigh
;
860 pfad
->nFileSizeLow
= FileData
.nFileSizeLow
;
865 attributes_from_dir_w(LPCWSTR pszFile
, LPWIN32_FILE_ATTRIBUTE_DATA pfad
)
868 WIN32_FIND_DATAW FileData
;
869 hFindFile
= FindFirstFileW(pszFile
, &FileData
);
870 if (hFindFile
== INVALID_HANDLE_VALUE
)
872 FindClose(hFindFile
);
873 pfad
->dwFileAttributes
= FileData
.dwFileAttributes
;
874 pfad
->ftCreationTime
= FileData
.ftCreationTime
;
875 pfad
->ftLastAccessTime
= FileData
.ftLastAccessTime
;
876 pfad
->ftLastWriteTime
= FileData
.ftLastWriteTime
;
877 pfad
->nFileSizeHigh
= FileData
.nFileSizeHigh
;
878 pfad
->nFileSizeLow
= FileData
.nFileSizeLow
;
883 Py_GetFileAttributesExA(LPCSTR pszFile
,
884 GET_FILEEX_INFO_LEVELS level
,
888 LPWIN32_FILE_ATTRIBUTE_DATA pfad
= pv
;
889 /* First try to use the system's implementation, if that is
890 available and either succeeds to gives an error other than
891 that it isn't implemented. */
894 result
= gfaxa(pszFile
, level
, pv
);
895 if (result
|| GetLastError() != ERROR_CALL_NOT_IMPLEMENTED
)
898 /* It's either not present, or not implemented.
899 Emulate using FindFirstFile. */
900 if (level
!= GetFileExInfoStandard
) {
901 SetLastError(ERROR_INVALID_PARAMETER
);
904 /* Use GetFileAttributes to validate that the file name
905 does not contain wildcards (which FindFirstFile would
907 if (GetFileAttributesA(pszFile
) == 0xFFFFFFFF)
909 return attributes_from_dir(pszFile
, pfad
);
913 Py_GetFileAttributesExW(LPCWSTR pszFile
,
914 GET_FILEEX_INFO_LEVELS level
,
918 LPWIN32_FILE_ATTRIBUTE_DATA pfad
= pv
;
919 /* First try to use the system's implementation, if that is
920 available and either succeeds to gives an error other than
921 that it isn't implemented. */
924 result
= gfaxw(pszFile
, level
, pv
);
925 if (result
|| GetLastError() != ERROR_CALL_NOT_IMPLEMENTED
)
928 /* It's either not present, or not implemented.
929 Emulate using FindFirstFile. */
930 if (level
!= GetFileExInfoStandard
) {
931 SetLastError(ERROR_INVALID_PARAMETER
);
934 /* Use GetFileAttributes to validate that the file name
935 does not contain wildcards (which FindFirstFile would
937 if (GetFileAttributesW(pszFile
) == 0xFFFFFFFF)
939 return attributes_from_dir_w(pszFile
, pfad
);
943 win32_stat(const char* path
, struct win32_stat
*result
)
945 WIN32_FILE_ATTRIBUTE_DATA info
;
948 /* XXX not supported on Win95 and NT 3.x */
949 if (!Py_GetFileAttributesExA(path
, GetFileExInfoStandard
, &info
)) {
950 if (GetLastError() != ERROR_SHARING_VIOLATION
) {
951 /* Protocol violation: we explicitly clear errno, instead of
952 setting it to a POSIX error. Callers should use GetLastError. */
956 /* Could not get attributes on open file. Fall back to
957 reading the directory. */
958 if (!attributes_from_dir(path
, &info
)) {
959 /* Very strange. This should not fail now */
965 code
= attribute_data_to_stat(&info
, result
);
968 /* Set S_IFEXEC if it is an .exe, .bat, ... */
969 dot
= strrchr(path
, '.');
971 if (stricmp(dot
, ".bat") == 0 ||
972 stricmp(dot
, ".cmd") == 0 ||
973 stricmp(dot
, ".exe") == 0 ||
974 stricmp(dot
, ".com") == 0)
975 result
->st_mode
|= 0111;
981 win32_wstat(const wchar_t* path
, struct win32_stat
*result
)
985 WIN32_FILE_ATTRIBUTE_DATA info
;
986 /* XXX not supported on Win95 and NT 3.x */
987 if (!Py_GetFileAttributesExW(path
, GetFileExInfoStandard
, &info
)) {
988 if (GetLastError() != ERROR_SHARING_VIOLATION
) {
989 /* Protocol violation: we explicitly clear errno, instead of
990 setting it to a POSIX error. Callers should use GetLastError. */
994 /* Could not get attributes on open file. Fall back to
995 reading the directory. */
996 if (!attributes_from_dir_w(path
, &info
)) {
997 /* Very strange. This should not fail now */
1003 code
= attribute_data_to_stat(&info
, result
);
1006 /* Set IFEXEC if it is an .exe, .bat, ... */
1007 dot
= wcsrchr(path
, '.');
1009 if (_wcsicmp(dot
, L
".bat") == 0 ||
1010 _wcsicmp(dot
, L
".cmd") == 0 ||
1011 _wcsicmp(dot
, L
".exe") == 0 ||
1012 _wcsicmp(dot
, L
".com") == 0)
1013 result
->st_mode
|= 0111;
1019 win32_fstat(int file_number
, struct win32_stat
*result
)
1021 BY_HANDLE_FILE_INFORMATION info
;
1025 h
= (HANDLE
)_get_osfhandle(file_number
);
1027 /* Protocol violation: we explicitly clear errno, instead of
1028 setting it to a POSIX error. Callers should use GetLastError. */
1031 if (h
== INVALID_HANDLE_VALUE
) {
1032 /* This is really a C library error (invalid file handle).
1033 We set the Win32 error to the closes one matching. */
1034 SetLastError(ERROR_INVALID_HANDLE
);
1037 memset(result
, 0, sizeof(*result
));
1039 type
= GetFileType(h
);
1040 if (type
== FILE_TYPE_UNKNOWN
) {
1041 DWORD error
= GetLastError();
1045 /* else: valid but unknown file */
1048 if (type
!= FILE_TYPE_DISK
) {
1049 if (type
== FILE_TYPE_CHAR
)
1050 result
->st_mode
= _S_IFCHR
;
1051 else if (type
== FILE_TYPE_PIPE
)
1052 result
->st_mode
= _S_IFIFO
;
1056 if (!GetFileInformationByHandle(h
, &info
)) {
1060 /* similar to stat() */
1061 result
->st_mode
= attributes_to_mode(info
.dwFileAttributes
);
1062 result
->st_size
= (((__int64
)info
.nFileSizeHigh
)<<32) + info
.nFileSizeLow
;
1063 FILE_TIME_to_time_t_nsec(&info
.ftCreationTime
, &result
->st_ctime
, &result
->st_ctime_nsec
);
1064 FILE_TIME_to_time_t_nsec(&info
.ftLastWriteTime
, &result
->st_mtime
, &result
->st_mtime_nsec
);
1065 FILE_TIME_to_time_t_nsec(&info
.ftLastAccessTime
, &result
->st_atime
, &result
->st_atime_nsec
);
1066 /* specific to fstat() */
1067 result
->st_nlink
= info
.nNumberOfLinks
;
1068 result
->st_ino
= (((__int64
)info
.nFileIndexHigh
)<<32) + info
.nFileIndexLow
;
1072 #endif /* MS_WINDOWS */
1074 PyDoc_STRVAR(stat_result__doc__
,
1075 "stat_result: Result from stat or lstat.\n\n\
1076 This object may be accessed either as a tuple of\n\
1077 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
1078 or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1080 Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1081 or st_flags, they are available as attributes only.\n\
1083 See os.stat for more information.");
1085 static PyStructSequence_Field stat_result_fields
[] = {
1086 {"st_mode", "protection bits"},
1087 {"st_ino", "inode"},
1088 {"st_dev", "device"},
1089 {"st_nlink", "number of hard links"},
1090 {"st_uid", "user ID of owner"},
1091 {"st_gid", "group ID of owner"},
1092 {"st_size", "total size, in bytes"},
1093 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1094 {NULL
, "integer time of last access"},
1095 {NULL
, "integer time of last modification"},
1096 {NULL
, "integer time of last change"},
1097 {"st_atime", "time of last access"},
1098 {"st_mtime", "time of last modification"},
1099 {"st_ctime", "time of last change"},
1100 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1101 {"st_blksize", "blocksize for filesystem I/O"},
1103 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
1104 {"st_blocks", "number of blocks allocated"},
1106 #ifdef HAVE_STRUCT_STAT_ST_RDEV
1107 {"st_rdev", "device type (if inode device)"},
1109 #ifdef HAVE_STRUCT_STAT_ST_FLAGS
1110 {"st_flags", "user defined flags for file"},
1112 #ifdef HAVE_STRUCT_STAT_ST_GEN
1113 {"st_gen", "generation number"},
1115 #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1116 {"st_birthtime", "time of creation"},
1121 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1122 #define ST_BLKSIZE_IDX 13
1124 #define ST_BLKSIZE_IDX 12
1127 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
1128 #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1130 #define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1133 #ifdef HAVE_STRUCT_STAT_ST_RDEV
1134 #define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1136 #define ST_RDEV_IDX ST_BLOCKS_IDX
1139 #ifdef HAVE_STRUCT_STAT_ST_FLAGS
1140 #define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1142 #define ST_FLAGS_IDX ST_RDEV_IDX
1145 #ifdef HAVE_STRUCT_STAT_ST_GEN
1146 #define ST_GEN_IDX (ST_FLAGS_IDX+1)
1148 #define ST_GEN_IDX ST_FLAGS_IDX
1151 #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1152 #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1154 #define ST_BIRTHTIME_IDX ST_GEN_IDX
1157 static PyStructSequence_Desc stat_result_desc
= {
1158 "stat_result", /* name */
1159 stat_result__doc__
, /* doc */
1164 PyDoc_STRVAR(statvfs_result__doc__
,
1165 "statvfs_result: Result from statvfs or fstatvfs.\n\n\
1166 This object may be accessed either as a tuple of\n\
1167 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
1168 or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
1170 See os.statvfs for more information.");
1172 static PyStructSequence_Field statvfs_result_fields
[] = {
1186 static PyStructSequence_Desc statvfs_result_desc
= {
1187 "statvfs_result", /* name */
1188 statvfs_result__doc__
, /* doc */
1189 statvfs_result_fields
,
1193 static int initialized
;
1194 static PyTypeObject StatResultType
;
1195 static PyTypeObject StatVFSResultType
;
1196 static newfunc structseq_new
;
1199 statresult_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
1201 PyStructSequence
*result
;
1204 result
= (PyStructSequence
*)structseq_new(type
, args
, kwds
);
1207 /* If we have been initialized from a tuple,
1208 st_?time might be set to None. Initialize it
1209 from the int slots. */
1210 for (i
= 7; i
<= 9; i
++) {
1211 if (result
->ob_item
[i
+3] == Py_None
) {
1213 Py_INCREF(result
->ob_item
[i
]);
1214 result
->ob_item
[i
+3] = result
->ob_item
[i
];
1217 return (PyObject
*)result
;
1222 /* If true, st_?time is float. */
1223 static int _stat_float_times
= 1;
1225 PyDoc_STRVAR(stat_float_times__doc__
,
1226 "stat_float_times([newval]) -> oldval\n\n\
1227 Determine whether os.[lf]stat represents time stamps as float objects.\n\
1228 If newval is True, future calls to stat() return floats, if it is False,\n\
1229 future calls return ints. \n\
1230 If newval is omitted, return the current setting.\n");
1233 stat_float_times(PyObject
* self
, PyObject
*args
)
1236 if (!PyArg_ParseTuple(args
, "|i:stat_float_times", &newval
))
1239 /* Return old value */
1240 return PyBool_FromLong(_stat_float_times
);
1241 _stat_float_times
= newval
;
1247 fill_time(PyObject
*v
, int index
, time_t sec
, unsigned long nsec
)
1249 PyObject
*fval
,*ival
;
1250 #if SIZEOF_TIME_T > SIZEOF_LONG
1251 ival
= PyLong_FromLongLong((PY_LONG_LONG
)sec
);
1253 ival
= PyInt_FromLong((long)sec
);
1257 if (_stat_float_times
) {
1258 fval
= PyFloat_FromDouble(sec
+ 1e-9*nsec
);
1263 PyStructSequence_SET_ITEM(v
, index
, ival
);
1264 PyStructSequence_SET_ITEM(v
, index
+3, fval
);
1267 /* pack a system stat C structure into the Python stat tuple
1268 (used by posix_stat() and posix_fstat()) */
1270 _pystat_fromstructstat(STRUCT_STAT
*st
)
1272 unsigned long ansec
, mnsec
, cnsec
;
1273 PyObject
*v
= PyStructSequence_New(&StatResultType
);
1277 PyStructSequence_SET_ITEM(v
, 0, PyInt_FromLong((long)st
->st_mode
));
1278 #ifdef HAVE_LARGEFILE_SUPPORT
1279 PyStructSequence_SET_ITEM(v
, 1,
1280 PyLong_FromLongLong((PY_LONG_LONG
)st
->st_ino
));
1282 PyStructSequence_SET_ITEM(v
, 1, PyInt_FromLong((long)st
->st_ino
));
1284 #if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
1285 PyStructSequence_SET_ITEM(v
, 2,
1286 PyLong_FromLongLong((PY_LONG_LONG
)st
->st_dev
));
1288 PyStructSequence_SET_ITEM(v
, 2, PyInt_FromLong((long)st
->st_dev
));
1290 PyStructSequence_SET_ITEM(v
, 3, PyInt_FromLong((long)st
->st_nlink
));
1291 PyStructSequence_SET_ITEM(v
, 4, PyInt_FromLong((long)st
->st_uid
));
1292 PyStructSequence_SET_ITEM(v
, 5, PyInt_FromLong((long)st
->st_gid
));
1293 #ifdef HAVE_LARGEFILE_SUPPORT
1294 PyStructSequence_SET_ITEM(v
, 6,
1295 PyLong_FromLongLong((PY_LONG_LONG
)st
->st_size
));
1297 PyStructSequence_SET_ITEM(v
, 6, PyInt_FromLong(st
->st_size
));
1300 #if defined(HAVE_STAT_TV_NSEC)
1301 ansec
= st
->st_atim
.tv_nsec
;
1302 mnsec
= st
->st_mtim
.tv_nsec
;
1303 cnsec
= st
->st_ctim
.tv_nsec
;
1304 #elif defined(HAVE_STAT_TV_NSEC2)
1305 ansec
= st
->st_atimespec
.tv_nsec
;
1306 mnsec
= st
->st_mtimespec
.tv_nsec
;
1307 cnsec
= st
->st_ctimespec
.tv_nsec
;
1308 #elif defined(HAVE_STAT_NSEC)
1309 ansec
= st
->st_atime_nsec
;
1310 mnsec
= st
->st_mtime_nsec
;
1311 cnsec
= st
->st_ctime_nsec
;
1313 ansec
= mnsec
= cnsec
= 0;
1315 fill_time(v
, 7, st
->st_atime
, ansec
);
1316 fill_time(v
, 8, st
->st_mtime
, mnsec
);
1317 fill_time(v
, 9, st
->st_ctime
, cnsec
);
1319 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1320 PyStructSequence_SET_ITEM(v
, ST_BLKSIZE_IDX
,
1321 PyInt_FromLong((long)st
->st_blksize
));
1323 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
1324 PyStructSequence_SET_ITEM(v
, ST_BLOCKS_IDX
,
1325 PyInt_FromLong((long)st
->st_blocks
));
1327 #ifdef HAVE_STRUCT_STAT_ST_RDEV
1328 PyStructSequence_SET_ITEM(v
, ST_RDEV_IDX
,
1329 PyInt_FromLong((long)st
->st_rdev
));
1331 #ifdef HAVE_STRUCT_STAT_ST_GEN
1332 PyStructSequence_SET_ITEM(v
, ST_GEN_IDX
,
1333 PyInt_FromLong((long)st
->st_gen
));
1335 #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1338 unsigned long bsec
,bnsec
;
1339 bsec
= (long)st
->st_birthtime
;
1340 #ifdef HAVE_STAT_TV_NSEC2
1341 bnsec
= st
->st_birthtimespec
.tv_nsec
;
1345 if (_stat_float_times
) {
1346 val
= PyFloat_FromDouble(bsec
+ 1e-9*bnsec
);
1348 val
= PyInt_FromLong((long)bsec
);
1350 PyStructSequence_SET_ITEM(v
, ST_BIRTHTIME_IDX
,
1354 #ifdef HAVE_STRUCT_STAT_ST_FLAGS
1355 PyStructSequence_SET_ITEM(v
, ST_FLAGS_IDX
,
1356 PyInt_FromLong((long)st
->st_flags
));
1359 if (PyErr_Occurred()) {
1369 /* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\,
1370 where / can be used in place of \ and the trailing slash is optional.
1371 Both SERVER and SHARE must have at least one character.
1374 #define ISSLASHA(c) ((c) == '\\' || (c) == '/')
1375 #define ISSLASHW(c) ((c) == L'\\' || (c) == L'/')
1377 #define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))
1381 IsUNCRootA(char *path
, int pathlen
)
1383 #define ISSLASH ISSLASHA
1387 if (pathlen
< 5 || !ISSLASH(path
[0]) || !ISSLASH(path
[1]))
1388 /* minimum UNCRoot is \\x\y */
1390 for (i
= 2; i
< pathlen
; i
++)
1391 if (ISSLASH(path
[i
])) break;
1392 if (i
== 2 || i
== pathlen
)
1393 /* do not allow \\\SHARE or \\SERVER */
1396 for (i
= share
; i
< pathlen
; i
++)
1397 if (ISSLASH(path
[i
])) break;
1398 return (i
!= share
&& (i
== pathlen
|| i
== pathlen
-1));
1403 #ifdef Py_WIN_WIDE_FILENAMES
1405 IsUNCRootW(Py_UNICODE
*path
, int pathlen
)
1407 #define ISSLASH ISSLASHW
1411 if (pathlen
< 5 || !ISSLASH(path
[0]) || !ISSLASH(path
[1]))
1412 /* minimum UNCRoot is \\x\y */
1414 for (i
= 2; i
< pathlen
; i
++)
1415 if (ISSLASH(path
[i
])) break;
1416 if (i
== 2 || i
== pathlen
)
1417 /* do not allow \\\SHARE or \\SERVER */
1420 for (i
= share
; i
< pathlen
; i
++)
1421 if (ISSLASH(path
[i
])) break;
1422 return (i
!= share
&& (i
== pathlen
|| i
== pathlen
-1));
1426 #endif /* Py_WIN_WIDE_FILENAMES */
1427 #endif /* MS_WINDOWS */
1430 posix_do_stat(PyObject
*self
, PyObject
*args
,
1433 int (*statfunc
)(const char *, STRUCT_STAT
*, ...),
1435 int (*statfunc
)(const char *, STRUCT_STAT
*),
1438 int (*wstatfunc
)(const Py_UNICODE
*, STRUCT_STAT
*))
1441 char *path
= NULL
; /* pass this to stat; do not free() it */
1442 char *pathfree
= NULL
; /* this memory must be free'd */
1446 #ifdef Py_WIN_WIDE_FILENAMES
1447 /* If on wide-character-capable OS see if argument
1448 is Unicode and if so use wide API. */
1449 if (unicode_file_names()) {
1450 PyUnicodeObject
*po
;
1451 if (PyArg_ParseTuple(args
, wformat
, &po
)) {
1452 Py_UNICODE
*wpath
= PyUnicode_AS_UNICODE(po
);
1454 Py_BEGIN_ALLOW_THREADS
1455 /* PyUnicode_AS_UNICODE result OK without
1456 thread lock as it is a simple dereference. */
1457 res
= wstatfunc(wpath
, &st
);
1458 Py_END_ALLOW_THREADS
1461 return win32_error_unicode("stat", wpath
);
1462 return _pystat_fromstructstat(&st
);
1464 /* Drop the argument parsing error as narrow strings
1470 if (!PyArg_ParseTuple(args
, format
,
1471 Py_FileSystemDefaultEncoding
, &path
))
1475 Py_BEGIN_ALLOW_THREADS
1476 res
= (*statfunc
)(path
, &st
);
1477 Py_END_ALLOW_THREADS
1481 result
= win32_error("stat", pathfree
);
1483 result
= posix_error_with_filename(pathfree
);
1487 result
= _pystat_fromstructstat(&st
);
1489 PyMem_Free(pathfree
);
1495 PyDoc_STRVAR(posix_access__doc__
,
1496 "access(path, mode) -> True if granted, False otherwise\n\n\
1497 Use the real uid/gid to test for access to a path. Note that most\n\
1498 operations will use the effective uid/gid, therefore this routine can\n\
1499 be used in a suid/sgid environment to test if the invoking user has the\n\
1500 specified access to the path. The mode argument can be F_OK to test\n\
1501 existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
1504 posix_access(PyObject
*self
, PyObject
*args
)
1509 #ifdef Py_WIN_WIDE_FILENAMES
1511 if (unicode_file_names()) {
1512 PyUnicodeObject
*po
;
1513 if (PyArg_ParseTuple(args
, "Ui:access", &po
, &mode
)) {
1514 Py_BEGIN_ALLOW_THREADS
1515 /* PyUnicode_AS_UNICODE OK without thread lock as
1516 it is a simple dereference. */
1517 attr
= GetFileAttributesW(PyUnicode_AS_UNICODE(po
));
1518 Py_END_ALLOW_THREADS
1521 /* Drop the argument parsing error as narrow strings
1525 if (!PyArg_ParseTuple(args
, "eti:access",
1526 Py_FileSystemDefaultEncoding
, &path
, &mode
))
1528 Py_BEGIN_ALLOW_THREADS
1529 attr
= GetFileAttributesA(path
);
1530 Py_END_ALLOW_THREADS
1533 if (attr
== 0xFFFFFFFF)
1534 /* File does not exist, or cannot read attributes */
1535 return PyBool_FromLong(0);
1536 /* Access is possible if either write access wasn't requested, or
1537 the file isn't read-only, or if it's a directory, as there are
1538 no read-only directories on Windows. */
1539 return PyBool_FromLong(!(mode
& 2)
1540 || !(attr
& FILE_ATTRIBUTE_READONLY
)
1541 || (attr
& FILE_ATTRIBUTE_DIRECTORY
));
1544 if (!PyArg_ParseTuple(args
, "eti:access",
1545 Py_FileSystemDefaultEncoding
, &path
, &mode
))
1547 Py_BEGIN_ALLOW_THREADS
1548 res
= access(path
, mode
);
1549 Py_END_ALLOW_THREADS
1551 return PyBool_FromLong(res
== 0);
1569 PyDoc_STRVAR(posix_ttyname__doc__
,
1570 "ttyname(fd) -> string\n\n\
1571 Return the name of the terminal device connected to 'fd'.");
1574 posix_ttyname(PyObject
*self
, PyObject
*args
)
1579 if (!PyArg_ParseTuple(args
, "i:ttyname", &id
))
1583 /* file descriptor 0 only, the default input device (stdin) */
1594 return posix_error();
1595 return PyString_FromString(ret
);
1600 PyDoc_STRVAR(posix_ctermid__doc__
,
1601 "ctermid() -> string\n\n\
1602 Return the name of the controlling terminal for this process.");
1605 posix_ctermid(PyObject
*self
, PyObject
*noargs
)
1608 char buffer
[L_ctermid
];
1610 #ifdef USE_CTERMID_R
1611 ret
= ctermid_r(buffer
);
1613 ret
= ctermid(buffer
);
1616 return posix_error();
1617 return PyString_FromString(buffer
);
1621 PyDoc_STRVAR(posix_chdir__doc__
,
1623 Change the current working directory to the specified path.");
1626 posix_chdir(PyObject
*self
, PyObject
*args
)
1629 return win32_1str(args
, "chdir", "s:chdir", win32_chdir
, "U:chdir", win32_wchdir
);
1630 #elif defined(PYOS_OS2) && defined(PYCC_GCC)
1631 return posix_1str(args
, "et:chdir", _chdir2
);
1632 #elif defined(__VMS)
1633 return posix_1str(args
, "et:chdir", (int (*)(const char *))chdir
);
1635 return posix_1str(args
, "et:chdir", chdir
);
1640 PyDoc_STRVAR(posix_fchdir__doc__
,
1641 "fchdir(fildes)\n\n\
1642 Change to the directory of the given file descriptor. fildes must be\n\
1643 opened on a directory, not a file.");
1646 posix_fchdir(PyObject
*self
, PyObject
*fdobj
)
1648 return posix_fildes(fdobj
, fchdir
);
1650 #endif /* HAVE_FCHDIR */
1653 PyDoc_STRVAR(posix_chmod__doc__
,
1654 "chmod(path, mode)\n\n\
1655 Change the access permissions of a file.");
1658 posix_chmod(PyObject
*self
, PyObject
*args
)
1663 #ifdef Py_WIN_WIDE_FILENAMES
1665 if (unicode_file_names()) {
1666 PyUnicodeObject
*po
;
1667 if (PyArg_ParseTuple(args
, "Ui|:chmod", &po
, &i
)) {
1668 Py_BEGIN_ALLOW_THREADS
1669 attr
= GetFileAttributesW(PyUnicode_AS_UNICODE(po
));
1670 if (attr
!= 0xFFFFFFFF) {
1672 attr
&= ~FILE_ATTRIBUTE_READONLY
;
1674 attr
|= FILE_ATTRIBUTE_READONLY
;
1675 res
= SetFileAttributesW(PyUnicode_AS_UNICODE(po
), attr
);
1679 Py_END_ALLOW_THREADS
1681 return win32_error_unicode("chmod",
1682 PyUnicode_AS_UNICODE(po
));
1686 /* Drop the argument parsing error as narrow strings
1690 if (!PyArg_ParseTuple(args
, "eti:chmod", Py_FileSystemDefaultEncoding
,
1693 Py_BEGIN_ALLOW_THREADS
1694 attr
= GetFileAttributesA(path
);
1695 if (attr
!= 0xFFFFFFFF) {
1697 attr
&= ~FILE_ATTRIBUTE_READONLY
;
1699 attr
|= FILE_ATTRIBUTE_READONLY
;
1700 res
= SetFileAttributesA(path
, attr
);
1704 Py_END_ALLOW_THREADS
1706 win32_error("chmod", path
);
1713 #else /* Py_WIN_WIDE_FILENAMES */
1714 if (!PyArg_ParseTuple(args
, "eti:chmod", Py_FileSystemDefaultEncoding
,
1717 Py_BEGIN_ALLOW_THREADS
1718 res
= chmod(path
, i
);
1719 Py_END_ALLOW_THREADS
1721 return posix_error_with_allocated_filename(path
);
1729 PyDoc_STRVAR(posix_fchmod__doc__
,
1730 "fchmod(fd, mode)\n\n\
1731 Change the access permissions of the file given by file\n\
1735 posix_fchmod(PyObject
*self
, PyObject
*args
)
1738 if (!PyArg_ParseTuple(args
, "ii:fchmod", &fd
, &mode
))
1740 Py_BEGIN_ALLOW_THREADS
1741 res
= fchmod(fd
, mode
);
1742 Py_END_ALLOW_THREADS
1744 return posix_error();
1747 #endif /* HAVE_FCHMOD */
1750 PyDoc_STRVAR(posix_lchmod__doc__
,
1751 "lchmod(path, mode)\n\n\
1752 Change the access permissions of a file. If path is a symlink, this\n\
1753 affects the link itself rather than the target.");
1756 posix_lchmod(PyObject
*self
, PyObject
*args
)
1761 if (!PyArg_ParseTuple(args
, "eti:lchmod", Py_FileSystemDefaultEncoding
,
1764 Py_BEGIN_ALLOW_THREADS
1765 res
= lchmod(path
, i
);
1766 Py_END_ALLOW_THREADS
1768 return posix_error_with_allocated_filename(path
);
1772 #endif /* HAVE_LCHMOD */
1776 PyDoc_STRVAR(posix_chflags__doc__
,
1777 "chflags(path, flags)\n\n\
1781 posix_chflags(PyObject
*self
, PyObject
*args
)
1784 unsigned long flags
;
1786 if (!PyArg_ParseTuple(args
, "etk:chflags",
1787 Py_FileSystemDefaultEncoding
, &path
, &flags
))
1789 Py_BEGIN_ALLOW_THREADS
1790 res
= chflags(path
, flags
);
1791 Py_END_ALLOW_THREADS
1793 return posix_error_with_allocated_filename(path
);
1798 #endif /* HAVE_CHFLAGS */
1800 #ifdef HAVE_LCHFLAGS
1801 PyDoc_STRVAR(posix_lchflags__doc__
,
1802 "lchflags(path, flags)\n\n\
1804 This function will not follow symbolic links.");
1807 posix_lchflags(PyObject
*self
, PyObject
*args
)
1810 unsigned long flags
;
1812 if (!PyArg_ParseTuple(args
, "etk:lchflags",
1813 Py_FileSystemDefaultEncoding
, &path
, &flags
))
1815 Py_BEGIN_ALLOW_THREADS
1816 res
= lchflags(path
, flags
);
1817 Py_END_ALLOW_THREADS
1819 return posix_error_with_allocated_filename(path
);
1824 #endif /* HAVE_LCHFLAGS */
1827 PyDoc_STRVAR(posix_chroot__doc__
,
1829 Change root directory to path.");
1832 posix_chroot(PyObject
*self
, PyObject
*args
)
1834 return posix_1str(args
, "et:chroot", chroot
);
1839 PyDoc_STRVAR(posix_fsync__doc__
,
1841 force write of file with filedescriptor to disk.");
1844 posix_fsync(PyObject
*self
, PyObject
*fdobj
)
1846 return posix_fildes(fdobj
, fsync
);
1848 #endif /* HAVE_FSYNC */
1850 #ifdef HAVE_FDATASYNC
1853 extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
1856 PyDoc_STRVAR(posix_fdatasync__doc__
,
1857 "fdatasync(fildes)\n\n\
1858 force write of file with filedescriptor to disk.\n\
1859 does not force update of metadata.");
1862 posix_fdatasync(PyObject
*self
, PyObject
*fdobj
)
1864 return posix_fildes(fdobj
, fdatasync
);
1866 #endif /* HAVE_FDATASYNC */
1870 PyDoc_STRVAR(posix_chown__doc__
,
1871 "chown(path, uid, gid)\n\n\
1872 Change the owner and group id of path to the numeric uid and gid.");
1875 posix_chown(PyObject
*self
, PyObject
*args
)
1880 if (!PyArg_ParseTuple(args
, "etll:chown",
1881 Py_FileSystemDefaultEncoding
, &path
,
1884 Py_BEGIN_ALLOW_THREADS
1885 res
= chown(path
, (uid_t
) uid
, (gid_t
) gid
);
1886 Py_END_ALLOW_THREADS
1888 return posix_error_with_allocated_filename(path
);
1893 #endif /* HAVE_CHOWN */
1896 PyDoc_STRVAR(posix_fchown__doc__
,
1897 "fchown(fd, uid, gid)\n\n\
1898 Change the owner and group id of the file given by file descriptor\n\
1899 fd to the numeric uid and gid.");
1902 posix_fchown(PyObject
*self
, PyObject
*args
)
1906 if (!PyArg_ParseTuple(args
, "iii:chown", &fd
, &uid
, &gid
))
1908 Py_BEGIN_ALLOW_THREADS
1909 res
= fchown(fd
, (uid_t
) uid
, (gid_t
) gid
);
1910 Py_END_ALLOW_THREADS
1912 return posix_error();
1915 #endif /* HAVE_FCHOWN */
1918 PyDoc_STRVAR(posix_lchown__doc__
,
1919 "lchown(path, uid, gid)\n\n\
1920 Change the owner and group id of path to the numeric uid and gid.\n\
1921 This function will not follow symbolic links.");
1924 posix_lchown(PyObject
*self
, PyObject
*args
)
1929 if (!PyArg_ParseTuple(args
, "etii:lchown",
1930 Py_FileSystemDefaultEncoding
, &path
,
1933 Py_BEGIN_ALLOW_THREADS
1934 res
= lchown(path
, (uid_t
) uid
, (gid_t
) gid
);
1935 Py_END_ALLOW_THREADS
1937 return posix_error_with_allocated_filename(path
);
1942 #endif /* HAVE_LCHOWN */
1946 PyDoc_STRVAR(posix_getcwd__doc__
,
1947 "getcwd() -> path\n\n\
1948 Return a string representing the current working directory.");
1951 posix_getcwd(PyObject
*self
, PyObject
*noargs
)
1953 int bufsize_incr
= 1024;
1955 char *tmpbuf
= NULL
;
1957 PyObject
*dynamic_return
;
1959 Py_BEGIN_ALLOW_THREADS
1961 bufsize
= bufsize
+ bufsize_incr
;
1962 tmpbuf
= malloc(bufsize
);
1963 if (tmpbuf
== NULL
) {
1966 #if defined(PYOS_OS2) && defined(PYCC_GCC)
1967 res
= _getcwd2(tmpbuf
, bufsize
);
1969 res
= getcwd(tmpbuf
, bufsize
);
1975 } while ((res
== NULL
) && (errno
== ERANGE
));
1976 Py_END_ALLOW_THREADS
1979 return posix_error();
1981 dynamic_return
= PyString_FromString(tmpbuf
);
1984 return dynamic_return
;
1987 #ifdef Py_USING_UNICODE
1988 PyDoc_STRVAR(posix_getcwdu__doc__
,
1989 "getcwdu() -> path\n\n\
1990 Return a unicode string representing the current working directory.");
1993 posix_getcwdu(PyObject
*self
, PyObject
*noargs
)
1998 #ifdef Py_WIN_WIDE_FILENAMES
2000 if (unicode_file_names()) {
2002 wchar_t *wbuf2
= wbuf
;
2004 Py_BEGIN_ALLOW_THREADS
2005 len
= GetCurrentDirectoryW(sizeof wbuf
/ sizeof wbuf
[0], wbuf
);
2006 /* If the buffer is large enough, len does not include the
2007 terminating \0. If the buffer is too small, len includes
2008 the space needed for the terminator. */
2009 if (len
>= sizeof wbuf
/ sizeof wbuf
[0]) {
2010 wbuf2
= malloc(len
* sizeof(wchar_t));
2012 len
= GetCurrentDirectoryW(len
, wbuf2
);
2014 Py_END_ALLOW_THREADS
2020 if (wbuf2
!= wbuf
) free(wbuf2
);
2021 return win32_error("getcwdu", NULL
);
2023 resobj
= PyUnicode_FromWideChar(wbuf2
, len
);
2024 if (wbuf2
!= wbuf
) free(wbuf2
);
2029 Py_BEGIN_ALLOW_THREADS
2030 #if defined(PYOS_OS2) && defined(PYCC_GCC)
2031 res
= _getcwd2(buf
, sizeof buf
);
2033 res
= getcwd(buf
, sizeof buf
);
2035 Py_END_ALLOW_THREADS
2037 return posix_error();
2038 return PyUnicode_Decode(buf
, strlen(buf
), Py_FileSystemDefaultEncoding
,"strict");
2045 PyDoc_STRVAR(posix_link__doc__
,
2046 "link(src, dst)\n\n\
2047 Create a hard link to a file.");
2050 posix_link(PyObject
*self
, PyObject
*args
)
2052 return posix_2str(args
, "etet:link", link
);
2054 #endif /* HAVE_LINK */
2057 PyDoc_STRVAR(posix_listdir__doc__
,
2058 "listdir(path) -> list_of_strings\n\n\
2059 Return a list containing the names of the entries in the directory.\n\
2061 path: path of directory to list\n\
2063 The list is in arbitrary order. It does not include the special\n\
2064 entries '.' and '..' even if they are present in the directory.");
2067 posix_listdir(PyObject
*self
, PyObject
*args
)
2069 /* XXX Should redo this putting the (now four) versions of opendir
2070 in separate files instead of having them all here... */
2071 #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
2076 WIN32_FIND_DATA FileData
;
2077 char namebuf
[MAX_PATH
+5]; /* Overallocate for \\*.*\0 */
2078 char *bufptr
= namebuf
;
2079 Py_ssize_t len
= sizeof(namebuf
)-5; /* only claim to have space for MAX_PATH */
2081 #ifdef Py_WIN_WIDE_FILENAMES
2082 /* If on wide-character-capable OS see if argument
2083 is Unicode and if so use wide API. */
2084 if (unicode_file_names()) {
2086 if (PyArg_ParseTuple(args
, "U:listdir", &po
)) {
2087 WIN32_FIND_DATAW wFileData
;
2088 Py_UNICODE
*wnamebuf
;
2090 /* Overallocate for \\*.*\0 */
2091 len
= PyUnicode_GET_SIZE(po
);
2092 wnamebuf
= malloc((len
+ 5) * sizeof(wchar_t));
2097 wcscpy(wnamebuf
, PyUnicode_AS_UNICODE(po
));
2098 wch
= len
> 0 ? wnamebuf
[len
-1] : '\0';
2099 if (wch
!= L
'/' && wch
!= L
'\\' && wch
!= L
':')
2100 wnamebuf
[len
++] = L
'\\';
2101 wcscpy(wnamebuf
+ len
, L
"*.*");
2102 if ((d
= PyList_New(0)) == NULL
) {
2106 hFindFile
= FindFirstFileW(wnamebuf
, &wFileData
);
2107 if (hFindFile
== INVALID_HANDLE_VALUE
) {
2108 int error
= GetLastError();
2109 if (error
== ERROR_FILE_NOT_FOUND
) {
2114 win32_error_unicode("FindFirstFileW", wnamebuf
);
2119 /* Skip over . and .. */
2120 if (wcscmp(wFileData
.cFileName
, L
".") != 0 &&
2121 wcscmp(wFileData
.cFileName
, L
"..") != 0) {
2122 v
= PyUnicode_FromUnicode(wFileData
.cFileName
, wcslen(wFileData
.cFileName
));
2128 if (PyList_Append(d
, v
) != 0) {
2136 Py_BEGIN_ALLOW_THREADS
2137 result
= FindNextFileW(hFindFile
, &wFileData
);
2138 Py_END_ALLOW_THREADS
2139 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2140 it got to the end of the directory. */
2141 if (!result
&& GetLastError() != ERROR_NO_MORE_FILES
) {
2143 win32_error_unicode("FindNextFileW", wnamebuf
);
2144 FindClose(hFindFile
);
2148 } while (result
== TRUE
);
2150 if (FindClose(hFindFile
) == FALSE
) {
2152 win32_error_unicode("FindClose", wnamebuf
);
2159 /* Drop the argument parsing error as narrow strings
2165 if (!PyArg_ParseTuple(args
, "et#:listdir",
2166 Py_FileSystemDefaultEncoding
, &bufptr
, &len
))
2169 char ch
= namebuf
[len
-1];
2170 if (ch
!= SEP
&& ch
!= ALTSEP
&& ch
!= ':')
2171 namebuf
[len
++] = '/';
2173 strcpy(namebuf
+ len
, "*.*");
2175 if ((d
= PyList_New(0)) == NULL
)
2178 hFindFile
= FindFirstFile(namebuf
, &FileData
);
2179 if (hFindFile
== INVALID_HANDLE_VALUE
) {
2180 int error
= GetLastError();
2181 if (error
== ERROR_FILE_NOT_FOUND
)
2184 return win32_error("FindFirstFile", namebuf
);
2187 /* Skip over . and .. */
2188 if (strcmp(FileData
.cFileName
, ".") != 0 &&
2189 strcmp(FileData
.cFileName
, "..") != 0) {
2190 v
= PyString_FromString(FileData
.cFileName
);
2196 if (PyList_Append(d
, v
) != 0) {
2204 Py_BEGIN_ALLOW_THREADS
2205 result
= FindNextFile(hFindFile
, &FileData
);
2206 Py_END_ALLOW_THREADS
2207 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2208 it got to the end of the directory. */
2209 if (!result
&& GetLastError() != ERROR_NO_MORE_FILES
) {
2211 win32_error("FindNextFile", namebuf
);
2212 FindClose(hFindFile
);
2215 } while (result
== TRUE
);
2217 if (FindClose(hFindFile
) == FALSE
) {
2219 return win32_error("FindClose", namebuf
);
2224 #elif defined(PYOS_OS2)
2227 #define MAX_PATH CCHMAXPATH
2232 char namebuf
[MAX_PATH
+5];
2238 if (!PyArg_ParseTuple(args
, "t#:listdir", &name
, &len
))
2240 if (len
>= MAX_PATH
) {
2241 PyErr_SetString(PyExc_ValueError
, "path too long");
2244 strcpy(namebuf
, name
);
2245 for (pt
= namebuf
; *pt
; pt
++)
2248 if (namebuf
[len
-1] != SEP
)
2249 namebuf
[len
++] = SEP
;
2250 strcpy(namebuf
+ len
, "*.*");
2252 if ((d
= PyList_New(0)) == NULL
)
2255 rc
= DosFindFirst(namebuf
, /* Wildcard Pattern to Match */
2256 &hdir
, /* Handle to Use While Search Directory */
2257 FILE_READONLY
| FILE_HIDDEN
| FILE_SYSTEM
| FILE_DIRECTORY
,
2258 &ep
, sizeof(ep
), /* Structure to Receive Directory Entry */
2259 &srchcnt
, /* Max and Actual Count of Entries Per Iteration */
2260 FIL_STANDARD
); /* Format of Entry (EAs or Not) */
2262 if (rc
!= NO_ERROR
) {
2264 return posix_error_with_filename(name
);
2267 if (srchcnt
> 0) { /* If Directory is NOT Totally Empty, */
2269 if (ep
.achName
[0] == '.'
2270 && (ep
.achName
[1] == '\0' || (ep
.achName
[1] == '.' && ep
.achName
[2] == '\0')))
2271 continue; /* Skip Over "." and ".." Names */
2273 strcpy(namebuf
, ep
.achName
);
2275 /* Leave Case of Name Alone -- In Native Form */
2276 /* (Removed Forced Lowercasing Code) */
2278 v
= PyString_FromString(namebuf
);
2284 if (PyList_Append(d
, v
) != 0) {
2291 } while (DosFindNext(hdir
, &ep
, sizeof(ep
), &srchcnt
) == NO_ERROR
&& srchcnt
> 0);
2301 int arg_is_unicode
= 1;
2304 if (!PyArg_ParseTuple(args
, "U:listdir", &v
)) {
2308 if (!PyArg_ParseTuple(args
, "et:listdir", Py_FileSystemDefaultEncoding
, &name
))
2310 if ((dirp
= opendir(name
)) == NULL
) {
2311 return posix_error_with_allocated_filename(name
);
2313 if ((d
= PyList_New(0)) == NULL
) {
2320 Py_BEGIN_ALLOW_THREADS
2322 Py_END_ALLOW_THREADS
2329 return posix_error_with_allocated_filename(name
);
2332 if (ep
->d_name
[0] == '.' &&
2334 (ep
->d_name
[1] == '.' && NAMLEN(ep
) == 2)))
2336 v
= PyString_FromStringAndSize(ep
->d_name
, NAMLEN(ep
));
2342 #ifdef Py_USING_UNICODE
2343 if (arg_is_unicode
) {
2346 w
= PyUnicode_FromEncodedObject(v
,
2347 Py_FileSystemDefaultEncoding
,
2354 /* fall back to the original byte string, as
2355 discussed in patch #683592 */
2360 if (PyList_Append(d
, v
) != 0) {
2373 #endif /* which OS */
2374 } /* end of posix_listdir */
2377 /* A helper function for abspath on win32 */
2379 posix__getfullpathname(PyObject
*self
, PyObject
*args
)
2381 /* assume encoded strings wont more than double no of chars */
2382 char inbuf
[MAX_PATH
*2];
2383 char *inbufp
= inbuf
;
2384 Py_ssize_t insize
= sizeof(inbuf
);
2385 char outbuf
[MAX_PATH
*2];
2387 #ifdef Py_WIN_WIDE_FILENAMES
2388 if (unicode_file_names()) {
2389 PyUnicodeObject
*po
;
2390 if (PyArg_ParseTuple(args
, "U|:_getfullpathname", &po
)) {
2391 Py_UNICODE woutbuf
[MAX_PATH
*2];
2393 if (!GetFullPathNameW(PyUnicode_AS_UNICODE(po
),
2394 sizeof(woutbuf
)/sizeof(woutbuf
[0]),
2396 return win32_error("GetFullPathName", "");
2397 return PyUnicode_FromUnicode(woutbuf
, wcslen(woutbuf
));
2399 /* Drop the argument parsing error as narrow strings
2404 if (!PyArg_ParseTuple (args
, "et#:_getfullpathname",
2405 Py_FileSystemDefaultEncoding
, &inbufp
,
2408 if (!GetFullPathName(inbuf
, sizeof(outbuf
)/sizeof(outbuf
[0]),
2410 return win32_error("GetFullPathName", inbuf
);
2411 if (PyUnicode_Check(PyTuple_GetItem(args
, 0))) {
2412 return PyUnicode_Decode(outbuf
, strlen(outbuf
),
2413 Py_FileSystemDefaultEncoding
, NULL
);
2415 return PyString_FromString(outbuf
);
2416 } /* end of posix__getfullpathname */
2417 #endif /* MS_WINDOWS */
2419 PyDoc_STRVAR(posix_mkdir__doc__
,
2420 "mkdir(path [, mode=0777])\n\n\
2421 Create a directory.");
2424 posix_mkdir(PyObject
*self
, PyObject
*args
)
2430 #ifdef Py_WIN_WIDE_FILENAMES
2431 if (unicode_file_names()) {
2432 PyUnicodeObject
*po
;
2433 if (PyArg_ParseTuple(args
, "U|i:mkdir", &po
, &mode
)) {
2434 Py_BEGIN_ALLOW_THREADS
2435 /* PyUnicode_AS_UNICODE OK without thread lock as
2436 it is a simple dereference. */
2437 res
= CreateDirectoryW(PyUnicode_AS_UNICODE(po
), NULL
);
2438 Py_END_ALLOW_THREADS
2440 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po
));
2444 /* Drop the argument parsing error as narrow strings
2448 if (!PyArg_ParseTuple(args
, "et|i:mkdir",
2449 Py_FileSystemDefaultEncoding
, &path
, &mode
))
2451 Py_BEGIN_ALLOW_THREADS
2452 /* PyUnicode_AS_UNICODE OK without thread lock as
2453 it is a simple dereference. */
2454 res
= CreateDirectoryA(path
, NULL
);
2455 Py_END_ALLOW_THREADS
2457 win32_error("mkdir", path
);
2466 if (!PyArg_ParseTuple(args
, "et|i:mkdir",
2467 Py_FileSystemDefaultEncoding
, &path
, &mode
))
2469 Py_BEGIN_ALLOW_THREADS
2470 #if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
2473 res
= mkdir(path
, mode
);
2475 Py_END_ALLOW_THREADS
2477 return posix_error_with_allocated_filename(path
);
2485 /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2486 #if defined(HAVE_SYS_RESOURCE_H)
2487 #include <sys/resource.h>
2492 PyDoc_STRVAR(posix_nice__doc__
,
2493 "nice(inc) -> new_priority\n\n\
2494 Decrease the priority of process by inc and return the new priority.");
2497 posix_nice(PyObject
*self
, PyObject
*args
)
2499 int increment
, value
;
2501 if (!PyArg_ParseTuple(args
, "i:nice", &increment
))
2504 /* There are two flavours of 'nice': one that returns the new
2505 priority (as required by almost all standards out there) and the
2506 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
2507 the use of getpriority() to get the new priority.
2509 If we are of the nice family that returns the new priority, we
2510 need to clear errno before the call, and check if errno is filled
2511 before calling posix_error() on a returnvalue of -1, because the
2512 -1 may be the actual new priority! */
2515 value
= nice(increment
);
2516 #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
2518 value
= getpriority(PRIO_PROCESS
, 0);
2520 if (value
== -1 && errno
!= 0)
2521 /* either nice() or getpriority() returned an error */
2522 return posix_error();
2523 return PyInt_FromLong((long) value
);
2525 #endif /* HAVE_NICE */
2527 PyDoc_STRVAR(posix_rename__doc__
,
2528 "rename(old, new)\n\n\
2529 Rename a file or directory.");
2532 posix_rename(PyObject
*self
, PyObject
*args
)
2538 if (unicode_file_names()) {
2539 if (!PyArg_ParseTuple(args
, "OO:rename", &o1
, &o2
))
2541 if (!convert_to_unicode(&o1
))
2543 if (!convert_to_unicode(&o2
)) {
2547 Py_BEGIN_ALLOW_THREADS
2548 result
= MoveFileW(PyUnicode_AsUnicode(o1
),
2549 PyUnicode_AsUnicode(o2
));
2550 Py_END_ALLOW_THREADS
2554 return win32_error("rename", NULL
);
2560 if (!PyArg_ParseTuple(args
, "ss:rename", &p1
, &p2
))
2562 Py_BEGIN_ALLOW_THREADS
2563 result
= MoveFileA(p1
, p2
);
2564 Py_END_ALLOW_THREADS
2566 return win32_error("rename", NULL
);
2570 return posix_2str(args
, "etet:rename", rename
);
2575 PyDoc_STRVAR(posix_rmdir__doc__
,
2577 Remove a directory.");
2580 posix_rmdir(PyObject
*self
, PyObject
*args
)
2583 return win32_1str(args
, "rmdir", "s:rmdir", RemoveDirectoryA
, "U:rmdir", RemoveDirectoryW
);
2585 return posix_1str(args
, "et:rmdir", rmdir
);
2590 PyDoc_STRVAR(posix_stat__doc__
,
2591 "stat(path) -> stat result\n\n\
2592 Perform a stat system call on the given path.");
2595 posix_stat(PyObject
*self
, PyObject
*args
)
2598 return posix_do_stat(self
, args
, "et:stat", STAT
, "U:stat", win32_wstat
);
2600 return posix_do_stat(self
, args
, "et:stat", STAT
, NULL
, NULL
);
2606 PyDoc_STRVAR(posix_system__doc__
,
2607 "system(command) -> exit_status\n\n\
2608 Execute the command (a string) in a subshell.");
2611 posix_system(PyObject
*self
, PyObject
*args
)
2615 if (!PyArg_ParseTuple(args
, "s:system", &command
))
2617 Py_BEGIN_ALLOW_THREADS
2618 sts
= system(command
);
2619 Py_END_ALLOW_THREADS
2620 return PyInt_FromLong(sts
);
2625 PyDoc_STRVAR(posix_umask__doc__
,
2626 "umask(new_mask) -> old_mask\n\n\
2627 Set the current numeric umask and return the previous umask.");
2630 posix_umask(PyObject
*self
, PyObject
*args
)
2633 if (!PyArg_ParseTuple(args
, "i:umask", &i
))
2637 return posix_error();
2638 return PyInt_FromLong((long)i
);
2642 PyDoc_STRVAR(posix_unlink__doc__
,
2644 Remove a file (same as remove(path)).");
2646 PyDoc_STRVAR(posix_remove__doc__
,
2648 Remove a file (same as unlink(path)).");
2651 posix_unlink(PyObject
*self
, PyObject
*args
)
2654 return win32_1str(args
, "remove", "s:remove", DeleteFileA
, "U:remove", DeleteFileW
);
2656 return posix_1str(args
, "et:remove", unlink
);
2662 PyDoc_STRVAR(posix_uname__doc__
,
2663 "uname() -> (sysname, nodename, release, version, machine)\n\n\
2664 Return a tuple identifying the current operating system.");
2667 posix_uname(PyObject
*self
, PyObject
*noargs
)
2672 Py_BEGIN_ALLOW_THREADS
2674 Py_END_ALLOW_THREADS
2676 return posix_error();
2677 return Py_BuildValue("(sssss)",
2684 #endif /* HAVE_UNAME */
2687 extract_time(PyObject
*t
, long* sec
, long* usec
)
2690 if (PyFloat_Check(t
)) {
2691 double tval
= PyFloat_AsDouble(t
);
2692 PyObject
*intobj
= Py_TYPE(t
)->tp_as_number
->nb_int(t
);
2695 intval
= PyInt_AsLong(intobj
);
2697 if (intval
== -1 && PyErr_Occurred())
2700 *usec
= (long)((tval
- intval
) * 1e6
); /* can't exceed 1000000 */
2702 /* If rounding gave us a negative number,
2707 intval
= PyInt_AsLong(t
);
2708 if (intval
== -1 && PyErr_Occurred())
2715 PyDoc_STRVAR(posix_utime__doc__
,
2716 "utime(path, (atime, mtime))\n\
2717 utime(path, None)\n\n\
2718 Set the access and modified time of the file to the given values. If the\n\
2719 second form is used, set the access and modified times to the current time.");
2722 posix_utime(PyObject
*self
, PyObject
*args
)
2724 #ifdef Py_WIN_WIDE_FILENAMES
2726 PyUnicodeObject
*obwpath
;
2727 wchar_t *wpath
= NULL
;
2730 long atimesec
, mtimesec
, ausec
, musec
;
2731 FILETIME atime
, mtime
;
2732 PyObject
*result
= NULL
;
2734 if (unicode_file_names()) {
2735 if (PyArg_ParseTuple(args
, "UO|:utime", &obwpath
, &arg
)) {
2736 wpath
= PyUnicode_AS_UNICODE(obwpath
);
2737 Py_BEGIN_ALLOW_THREADS
2738 hFile
= CreateFileW(wpath
, FILE_WRITE_ATTRIBUTES
, 0,
2739 NULL
, OPEN_EXISTING
,
2740 FILE_FLAG_BACKUP_SEMANTICS
, NULL
);
2741 Py_END_ALLOW_THREADS
2742 if (hFile
== INVALID_HANDLE_VALUE
)
2743 return win32_error_unicode("utime", wpath
);
2745 /* Drop the argument parsing error as narrow strings
2750 if (!PyArg_ParseTuple(args
, "etO:utime",
2751 Py_FileSystemDefaultEncoding
, &apath
, &arg
))
2753 Py_BEGIN_ALLOW_THREADS
2754 hFile
= CreateFileA(apath
, FILE_WRITE_ATTRIBUTES
, 0,
2755 NULL
, OPEN_EXISTING
,
2756 FILE_FLAG_BACKUP_SEMANTICS
, NULL
);
2757 Py_END_ALLOW_THREADS
2758 if (hFile
== INVALID_HANDLE_VALUE
) {
2759 win32_error("utime", apath
);
2766 if (arg
== Py_None
) {
2768 GetSystemTime(&now
);
2769 if (!SystemTimeToFileTime(&now
, &mtime
) ||
2770 !SystemTimeToFileTime(&now
, &atime
)) {
2771 win32_error("utime", NULL
);
2775 else if (!PyTuple_Check(arg
) || PyTuple_Size(arg
) != 2) {
2776 PyErr_SetString(PyExc_TypeError
,
2777 "utime() arg 2 must be a tuple (atime, mtime)");
2781 if (extract_time(PyTuple_GET_ITEM(arg
, 0),
2782 &atimesec
, &ausec
) == -1)
2784 time_t_to_FILE_TIME(atimesec
, 1000*ausec
, &atime
);
2785 if (extract_time(PyTuple_GET_ITEM(arg
, 1),
2786 &mtimesec
, &musec
) == -1)
2788 time_t_to_FILE_TIME(mtimesec
, 1000*musec
, &mtime
);
2790 if (!SetFileTime(hFile
, NULL
, &atime
, &mtime
)) {
2791 /* Avoid putting the file name into the error here,
2792 as that may confuse the user into believing that
2793 something is wrong with the file, when it also
2794 could be the time stamp that gives a problem. */
2795 win32_error("utime", NULL
);
2802 #else /* Py_WIN_WIDE_FILENAMES */
2805 long atime
, mtime
, ausec
, musec
;
2809 #if defined(HAVE_UTIMES)
2810 struct timeval buf
[2];
2811 #define ATIME buf[0].tv_sec
2812 #define MTIME buf[1].tv_sec
2813 #elif defined(HAVE_UTIME_H)
2814 /* XXX should define struct utimbuf instead, above */
2816 #define ATIME buf.actime
2817 #define MTIME buf.modtime
2818 #define UTIME_ARG &buf
2819 #else /* HAVE_UTIMES */
2821 #define ATIME buf[0]
2822 #define MTIME buf[1]
2823 #define UTIME_ARG buf
2824 #endif /* HAVE_UTIMES */
2827 if (!PyArg_ParseTuple(args
, "etO:utime",
2828 Py_FileSystemDefaultEncoding
, &path
, &arg
))
2830 if (arg
== Py_None
) {
2831 /* optional time values not given */
2832 Py_BEGIN_ALLOW_THREADS
2833 res
= utime(path
, NULL
);
2834 Py_END_ALLOW_THREADS
2836 else if (!PyTuple_Check(arg
) || PyTuple_Size(arg
) != 2) {
2837 PyErr_SetString(PyExc_TypeError
,
2838 "utime() arg 2 must be a tuple (atime, mtime)");
2843 if (extract_time(PyTuple_GET_ITEM(arg
, 0),
2844 &atime
, &ausec
) == -1) {
2848 if (extract_time(PyTuple_GET_ITEM(arg
, 1),
2849 &mtime
, &musec
) == -1) {
2856 buf
[0].tv_usec
= ausec
;
2857 buf
[1].tv_usec
= musec
;
2858 Py_BEGIN_ALLOW_THREADS
2859 res
= utimes(path
, buf
);
2860 Py_END_ALLOW_THREADS
2862 Py_BEGIN_ALLOW_THREADS
2863 res
= utime(path
, UTIME_ARG
);
2864 Py_END_ALLOW_THREADS
2865 #endif /* HAVE_UTIMES */
2868 return posix_error_with_allocated_filename(path
);
2876 #endif /* Py_WIN_WIDE_FILENAMES */
2880 /* Process operations */
2882 PyDoc_STRVAR(posix__exit__doc__
,
2884 Exit to the system with specified status, without normal exit processing.");
2887 posix__exit(PyObject
*self
, PyObject
*args
)
2890 if (!PyArg_ParseTuple(args
, "i:_exit", &sts
))
2893 return NULL
; /* Make gcc -Wall happy */
2896 #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
2898 free_string_array(char **array
, Py_ssize_t count
)
2901 for (i
= 0; i
< count
; i
++)
2902 PyMem_Free(array
[i
]);
2909 PyDoc_STRVAR(posix_execv__doc__
,
2910 "execv(path, args)\n\n\
2911 Execute an executable path with arguments, replacing current process.\n\
2913 path: path of executable file\n\
2914 args: tuple or list of strings");
2917 posix_execv(PyObject
*self
, PyObject
*args
)
2923 PyObject
*(*getitem
)(PyObject
*, Py_ssize_t
);
2925 /* execv has two arguments: (path, argv), where
2926 argv is a list or tuple of strings. */
2928 if (!PyArg_ParseTuple(args
, "etO:execv",
2929 Py_FileSystemDefaultEncoding
,
2932 if (PyList_Check(argv
)) {
2933 argc
= PyList_Size(argv
);
2934 getitem
= PyList_GetItem
;
2936 else if (PyTuple_Check(argv
)) {
2937 argc
= PyTuple_Size(argv
);
2938 getitem
= PyTuple_GetItem
;
2941 PyErr_SetString(PyExc_TypeError
, "execv() arg 2 must be a tuple or list");
2946 argvlist
= PyMem_NEW(char *, argc
+1);
2947 if (argvlist
== NULL
) {
2949 return PyErr_NoMemory();
2951 for (i
= 0; i
< argc
; i
++) {
2952 if (!PyArg_Parse((*getitem
)(argv
, i
), "et",
2953 Py_FileSystemDefaultEncoding
,
2955 free_string_array(argvlist
, i
);
2956 PyErr_SetString(PyExc_TypeError
,
2957 "execv() arg 2 must contain only strings");
2963 argvlist
[argc
] = NULL
;
2965 execv(path
, argvlist
);
2967 /* If we get here it's definitely an error */
2969 free_string_array(argvlist
, argc
);
2971 return posix_error();
2975 PyDoc_STRVAR(posix_execve__doc__
,
2976 "execve(path, args, env)\n\n\
2977 Execute a path with arguments and environment, replacing current process.\n\
2979 path: path of executable file\n\
2980 args: tuple or list of arguments\n\
2981 env: dictionary of strings mapping to strings");
2984 posix_execve(PyObject
*self
, PyObject
*args
)
2987 PyObject
*argv
, *env
;
2990 PyObject
*key
, *val
, *keys
=NULL
, *vals
=NULL
;
2991 Py_ssize_t i
, pos
, argc
, envc
;
2992 PyObject
*(*getitem
)(PyObject
*, Py_ssize_t
);
2993 Py_ssize_t lastarg
= 0;
2995 /* execve has three arguments: (path, argv, env), where
2996 argv is a list or tuple of strings and env is a dictionary
2997 like posix.environ. */
2999 if (!PyArg_ParseTuple(args
, "etOO:execve",
3000 Py_FileSystemDefaultEncoding
,
3001 &path
, &argv
, &env
))
3003 if (PyList_Check(argv
)) {
3004 argc
= PyList_Size(argv
);
3005 getitem
= PyList_GetItem
;
3007 else if (PyTuple_Check(argv
)) {
3008 argc
= PyTuple_Size(argv
);
3009 getitem
= PyTuple_GetItem
;
3012 PyErr_SetString(PyExc_TypeError
,
3013 "execve() arg 2 must be a tuple or list");
3016 if (!PyMapping_Check(env
)) {
3017 PyErr_SetString(PyExc_TypeError
,
3018 "execve() arg 3 must be a mapping object");
3022 argvlist
= PyMem_NEW(char *, argc
+1);
3023 if (argvlist
== NULL
) {
3027 for (i
= 0; i
< argc
; i
++) {
3028 if (!PyArg_Parse((*getitem
)(argv
, i
),
3029 "et;execve() arg 2 must contain only strings",
3030 Py_FileSystemDefaultEncoding
,
3038 argvlist
[argc
] = NULL
;
3040 i
= PyMapping_Size(env
);
3043 envlist
= PyMem_NEW(char *, i
+ 1);
3044 if (envlist
== NULL
) {
3049 keys
= PyMapping_Keys(env
);
3050 vals
= PyMapping_Values(env
);
3053 if (!PyList_Check(keys
) || !PyList_Check(vals
)) {
3054 PyErr_SetString(PyExc_TypeError
,
3055 "execve(): env.keys() or env.values() is not a list");
3059 for (pos
= 0; pos
< i
; pos
++) {
3063 key
= PyList_GetItem(keys
, pos
);
3064 val
= PyList_GetItem(vals
, pos
);
3070 "s;execve() arg 3 contains a non-string key",
3074 "s;execve() arg 3 contains a non-string value",
3080 #if defined(PYOS_OS2)
3081 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3082 if (stricmp(k
, "BEGINLIBPATH") != 0 && stricmp(k
, "ENDLIBPATH") != 0) {
3084 len
= PyString_Size(key
) + PyString_Size(val
) + 2;
3085 p
= PyMem_NEW(char, len
);
3090 PyOS_snprintf(p
, len
, "%s=%s", k
, v
);
3091 envlist
[envc
++] = p
;
3092 #if defined(PYOS_OS2)
3098 execve(path
, argvlist
, envlist
);
3100 /* If we get here it's definitely an error */
3102 (void) posix_error();
3106 PyMem_DEL(envlist
[envc
]);
3109 free_string_array(argvlist
, lastarg
);
3116 #endif /* HAVE_EXECV */
3120 PyDoc_STRVAR(posix_spawnv__doc__
,
3121 "spawnv(mode, path, args)\n\n\
3122 Execute the program 'path' in a new process.\n\
3124 mode: mode of process creation\n\
3125 path: path of executable file\n\
3126 args: tuple or list of strings");
3129 posix_spawnv(PyObject
*self
, PyObject
*args
)
3136 Py_intptr_t spawnval
;
3137 PyObject
*(*getitem
)(PyObject
*, Py_ssize_t
);
3139 /* spawnv has three arguments: (mode, path, argv), where
3140 argv is a list or tuple of strings. */
3142 if (!PyArg_ParseTuple(args
, "ietO:spawnv", &mode
,
3143 Py_FileSystemDefaultEncoding
,
3146 if (PyList_Check(argv
)) {
3147 argc
= PyList_Size(argv
);
3148 getitem
= PyList_GetItem
;
3150 else if (PyTuple_Check(argv
)) {
3151 argc
= PyTuple_Size(argv
);
3152 getitem
= PyTuple_GetItem
;
3155 PyErr_SetString(PyExc_TypeError
,
3156 "spawnv() arg 2 must be a tuple or list");
3161 argvlist
= PyMem_NEW(char *, argc
+1);
3162 if (argvlist
== NULL
) {
3164 return PyErr_NoMemory();
3166 for (i
= 0; i
< argc
; i
++) {
3167 if (!PyArg_Parse((*getitem
)(argv
, i
), "et",
3168 Py_FileSystemDefaultEncoding
,
3170 free_string_array(argvlist
, i
);
3173 "spawnv() arg 2 must contain only strings");
3178 argvlist
[argc
] = NULL
;
3180 #if defined(PYOS_OS2) && defined(PYCC_GCC)
3181 Py_BEGIN_ALLOW_THREADS
3182 spawnval
= spawnv(mode
, path
, argvlist
);
3183 Py_END_ALLOW_THREADS
3185 if (mode
== _OLD_P_OVERLAY
)
3188 Py_BEGIN_ALLOW_THREADS
3189 spawnval
= _spawnv(mode
, path
, argvlist
);
3190 Py_END_ALLOW_THREADS
3193 free_string_array(argvlist
, argc
);
3197 return posix_error();
3199 #if SIZEOF_LONG == SIZEOF_VOID_P
3200 return Py_BuildValue("l", (long) spawnval
);
3202 return Py_BuildValue("L", (PY_LONG_LONG
) spawnval
);
3207 PyDoc_STRVAR(posix_spawnve__doc__
,
3208 "spawnve(mode, path, args, env)\n\n\
3209 Execute the program 'path' in a new process.\n\
3211 mode: mode of process creation\n\
3212 path: path of executable file\n\
3213 args: tuple or list of arguments\n\
3214 env: dictionary of strings mapping to strings");
3217 posix_spawnve(PyObject
*self
, PyObject
*args
)
3220 PyObject
*argv
, *env
;
3223 PyObject
*key
, *val
, *keys
=NULL
, *vals
=NULL
, *res
=NULL
;
3224 int mode
, pos
, envc
;
3226 Py_intptr_t spawnval
;
3227 PyObject
*(*getitem
)(PyObject
*, Py_ssize_t
);
3228 Py_ssize_t lastarg
= 0;
3230 /* spawnve has four arguments: (mode, path, argv, env), where
3231 argv is a list or tuple of strings and env is a dictionary
3232 like posix.environ. */
3234 if (!PyArg_ParseTuple(args
, "ietOO:spawnve", &mode
,
3235 Py_FileSystemDefaultEncoding
,
3236 &path
, &argv
, &env
))
3238 if (PyList_Check(argv
)) {
3239 argc
= PyList_Size(argv
);
3240 getitem
= PyList_GetItem
;
3242 else if (PyTuple_Check(argv
)) {
3243 argc
= PyTuple_Size(argv
);
3244 getitem
= PyTuple_GetItem
;
3247 PyErr_SetString(PyExc_TypeError
,
3248 "spawnve() arg 2 must be a tuple or list");
3251 if (!PyMapping_Check(env
)) {
3252 PyErr_SetString(PyExc_TypeError
,
3253 "spawnve() arg 3 must be a mapping object");
3257 argvlist
= PyMem_NEW(char *, argc
+1);
3258 if (argvlist
== NULL
) {
3262 for (i
= 0; i
< argc
; i
++) {
3263 if (!PyArg_Parse((*getitem
)(argv
, i
),
3264 "et;spawnve() arg 2 must contain only strings",
3265 Py_FileSystemDefaultEncoding
,
3273 argvlist
[argc
] = NULL
;
3275 i
= PyMapping_Size(env
);
3278 envlist
= PyMem_NEW(char *, i
+ 1);
3279 if (envlist
== NULL
) {
3284 keys
= PyMapping_Keys(env
);
3285 vals
= PyMapping_Values(env
);
3288 if (!PyList_Check(keys
) || !PyList_Check(vals
)) {
3289 PyErr_SetString(PyExc_TypeError
,
3290 "spawnve(): env.keys() or env.values() is not a list");
3294 for (pos
= 0; pos
< i
; pos
++) {
3298 key
= PyList_GetItem(keys
, pos
);
3299 val
= PyList_GetItem(vals
, pos
);
3305 "s;spawnve() arg 3 contains a non-string key",
3309 "s;spawnve() arg 3 contains a non-string value",
3314 len
= PyString_Size(key
) + PyString_Size(val
) + 2;
3315 p
= PyMem_NEW(char, len
);
3320 PyOS_snprintf(p
, len
, "%s=%s", k
, v
);
3321 envlist
[envc
++] = p
;
3325 #if defined(PYOS_OS2) && defined(PYCC_GCC)
3326 Py_BEGIN_ALLOW_THREADS
3327 spawnval
= spawnve(mode
, path
, argvlist
, envlist
);
3328 Py_END_ALLOW_THREADS
3330 if (mode
== _OLD_P_OVERLAY
)
3333 Py_BEGIN_ALLOW_THREADS
3334 spawnval
= _spawnve(mode
, path
, argvlist
, envlist
);
3335 Py_END_ALLOW_THREADS
3339 (void) posix_error();
3341 #if SIZEOF_LONG == SIZEOF_VOID_P
3342 res
= Py_BuildValue("l", (long) spawnval
);
3344 res
= Py_BuildValue("L", (PY_LONG_LONG
) spawnval
);
3349 PyMem_DEL(envlist
[envc
]);
3352 free_string_array(argvlist
, lastarg
);
3360 /* OS/2 supports spawnvp & spawnvpe natively */
3361 #if defined(PYOS_OS2)
3362 PyDoc_STRVAR(posix_spawnvp__doc__
,
3363 "spawnvp(mode, file, args)\n\n\
3364 Execute the program 'file' in a new process, using the environment\n\
3365 search path to find the file.\n\
3367 mode: mode of process creation\n\
3368 file: executable file name\n\
3369 args: tuple or list of strings");
3372 posix_spawnvp(PyObject
*self
, PyObject
*args
)
3378 Py_intptr_t spawnval
;
3379 PyObject
*(*getitem
)(PyObject
*, Py_ssize_t
);
3381 /* spawnvp has three arguments: (mode, path, argv), where
3382 argv is a list or tuple of strings. */
3384 if (!PyArg_ParseTuple(args
, "ietO:spawnvp", &mode
,
3385 Py_FileSystemDefaultEncoding
,
3388 if (PyList_Check(argv
)) {
3389 argc
= PyList_Size(argv
);
3390 getitem
= PyList_GetItem
;
3392 else if (PyTuple_Check(argv
)) {
3393 argc
= PyTuple_Size(argv
);
3394 getitem
= PyTuple_GetItem
;
3397 PyErr_SetString(PyExc_TypeError
,
3398 "spawnvp() arg 2 must be a tuple or list");
3403 argvlist
= PyMem_NEW(char *, argc
+1);
3404 if (argvlist
== NULL
) {
3406 return PyErr_NoMemory();
3408 for (i
= 0; i
< argc
; i
++) {
3409 if (!PyArg_Parse((*getitem
)(argv
, i
), "et",
3410 Py_FileSystemDefaultEncoding
,
3412 free_string_array(argvlist
, i
);
3415 "spawnvp() arg 2 must contain only strings");
3420 argvlist
[argc
] = NULL
;
3422 Py_BEGIN_ALLOW_THREADS
3423 #if defined(PYCC_GCC)
3424 spawnval
= spawnvp(mode
, path
, argvlist
);
3426 spawnval
= _spawnvp(mode
, path
, argvlist
);
3428 Py_END_ALLOW_THREADS
3430 free_string_array(argvlist
, argc
);
3434 return posix_error();
3436 return Py_BuildValue("l", (long) spawnval
);
3440 PyDoc_STRVAR(posix_spawnvpe__doc__
,
3441 "spawnvpe(mode, file, args, env)\n\n\
3442 Execute the program 'file' in a new process, using the environment\n\
3443 search path to find the file.\n\
3445 mode: mode of process creation\n\
3446 file: executable file name\n\
3447 args: tuple or list of arguments\n\
3448 env: dictionary of strings mapping to strings");
3451 posix_spawnvpe(PyObject
*self
, PyObject
*args
)
3454 PyObject
*argv
, *env
;
3457 PyObject
*key
, *val
, *keys
=NULL
, *vals
=NULL
, *res
=NULL
;
3458 int mode
, i
, pos
, argc
, envc
;
3459 Py_intptr_t spawnval
;
3460 PyObject
*(*getitem
)(PyObject
*, Py_ssize_t
);
3463 /* spawnvpe has four arguments: (mode, path, argv, env), where
3464 argv is a list or tuple of strings and env is a dictionary
3465 like posix.environ. */
3467 if (!PyArg_ParseTuple(args
, "ietOO:spawnvpe", &mode
,
3468 Py_FileSystemDefaultEncoding
,
3469 &path
, &argv
, &env
))
3471 if (PyList_Check(argv
)) {
3472 argc
= PyList_Size(argv
);
3473 getitem
= PyList_GetItem
;
3475 else if (PyTuple_Check(argv
)) {
3476 argc
= PyTuple_Size(argv
);
3477 getitem
= PyTuple_GetItem
;
3480 PyErr_SetString(PyExc_TypeError
,
3481 "spawnvpe() arg 2 must be a tuple or list");
3484 if (!PyMapping_Check(env
)) {
3485 PyErr_SetString(PyExc_TypeError
,
3486 "spawnvpe() arg 3 must be a mapping object");
3490 argvlist
= PyMem_NEW(char *, argc
+1);
3491 if (argvlist
== NULL
) {
3495 for (i
= 0; i
< argc
; i
++) {
3496 if (!PyArg_Parse((*getitem
)(argv
, i
),
3497 "et;spawnvpe() arg 2 must contain only strings",
3498 Py_FileSystemDefaultEncoding
,
3506 argvlist
[argc
] = NULL
;
3508 i
= PyMapping_Size(env
);
3511 envlist
= PyMem_NEW(char *, i
+ 1);
3512 if (envlist
== NULL
) {
3517 keys
= PyMapping_Keys(env
);
3518 vals
= PyMapping_Values(env
);
3521 if (!PyList_Check(keys
) || !PyList_Check(vals
)) {
3522 PyErr_SetString(PyExc_TypeError
,
3523 "spawnvpe(): env.keys() or env.values() is not a list");
3527 for (pos
= 0; pos
< i
; pos
++) {
3531 key
= PyList_GetItem(keys
, pos
);
3532 val
= PyList_GetItem(vals
, pos
);
3538 "s;spawnvpe() arg 3 contains a non-string key",
3542 "s;spawnvpe() arg 3 contains a non-string value",
3547 len
= PyString_Size(key
) + PyString_Size(val
) + 2;
3548 p
= PyMem_NEW(char, len
);
3553 PyOS_snprintf(p
, len
, "%s=%s", k
, v
);
3554 envlist
[envc
++] = p
;
3558 Py_BEGIN_ALLOW_THREADS
3559 #if defined(PYCC_GCC)
3560 spawnval
= spawnvpe(mode
, path
, argvlist
, envlist
);
3562 spawnval
= _spawnvpe(mode
, path
, argvlist
, envlist
);
3564 Py_END_ALLOW_THREADS
3567 (void) posix_error();
3569 res
= Py_BuildValue("l", (long) spawnval
);
3573 PyMem_DEL(envlist
[envc
]);
3576 free_string_array(argvlist
, lastarg
);
3583 #endif /* PYOS_OS2 */
3584 #endif /* HAVE_SPAWNV */
3588 PyDoc_STRVAR(posix_fork1__doc__
,
3589 "fork1() -> pid\n\n\
3590 Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
3592 Return 0 to child process and PID of child to parent process.");
3595 posix_fork1(PyObject
*self
, PyObject
*noargs
)
3597 pid_t pid
= fork1();
3599 return posix_error();
3602 return PyInt_FromLong(pid
);
3608 PyDoc_STRVAR(posix_fork__doc__
,
3610 Fork a child process.\n\
3611 Return 0 to child process and PID of child to parent process.");
3614 posix_fork(PyObject
*self
, PyObject
*noargs
)
3618 return posix_error();
3621 return PyInt_FromLong(pid
);
3625 /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
3626 /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
3627 #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
3628 #define DEV_PTY_FILE "/dev/ptc"
3629 #define HAVE_DEV_PTMX
3631 #define DEV_PTY_FILE "/dev/ptmx"
3634 #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
3638 #ifdef HAVE_LIBUTIL_H
3639 #include <libutil.h>
3640 #endif /* HAVE_LIBUTIL_H */
3641 #endif /* HAVE_PTY_H */
3642 #ifdef HAVE_STROPTS_H
3643 #include <stropts.h>
3645 #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
3647 #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
3648 PyDoc_STRVAR(posix_openpty__doc__
,
3649 "openpty() -> (master_fd, slave_fd)\n\n\
3650 Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
3653 posix_openpty(PyObject
*self
, PyObject
*noargs
)
3655 int master_fd
, slave_fd
;
3656 #ifndef HAVE_OPENPTY
3659 #if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
3660 PyOS_sighandler_t sig_saved
;
3662 extern char *ptsname(int fildes
);
3667 if (openpty(&master_fd
, &slave_fd
, NULL
, NULL
, NULL
) != 0)
3668 return posix_error();
3669 #elif defined(HAVE__GETPTY)
3670 slave_name
= _getpty(&master_fd
, O_RDWR
, 0666, 0);
3671 if (slave_name
== NULL
)
3672 return posix_error();
3674 slave_fd
= open(slave_name
, O_RDWR
);
3676 return posix_error();
3678 master_fd
= open(DEV_PTY_FILE
, O_RDWR
| O_NOCTTY
); /* open master */
3680 return posix_error();
3681 sig_saved
= PyOS_setsig(SIGCHLD
, SIG_DFL
);
3682 /* change permission of slave */
3683 if (grantpt(master_fd
) < 0) {
3684 PyOS_setsig(SIGCHLD
, sig_saved
);
3685 return posix_error();
3688 if (unlockpt(master_fd
) < 0) {
3689 PyOS_setsig(SIGCHLD
, sig_saved
);
3690 return posix_error();
3692 PyOS_setsig(SIGCHLD
, sig_saved
);
3693 slave_name
= ptsname(master_fd
); /* get name of slave */
3694 if (slave_name
== NULL
)
3695 return posix_error();
3696 slave_fd
= open(slave_name
, O_RDWR
| O_NOCTTY
); /* open slave */
3698 return posix_error();
3699 #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
3700 ioctl(slave_fd
, I_PUSH
, "ptem"); /* push ptem */
3701 ioctl(slave_fd
, I_PUSH
, "ldterm"); /* push ldterm */
3703 ioctl(slave_fd
, I_PUSH
, "ttcompat"); /* push ttcompat */
3705 #endif /* HAVE_CYGWIN */
3706 #endif /* HAVE_OPENPTY */
3708 return Py_BuildValue("(ii)", master_fd
, slave_fd
);
3711 #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
3714 PyDoc_STRVAR(posix_forkpty__doc__
,
3715 "forkpty() -> (pid, master_fd)\n\n\
3716 Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
3717 Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
3718 To both, return fd of newly opened pseudo-terminal.\n");
3721 posix_forkpty(PyObject
*self
, PyObject
*noargs
)
3726 pid
= forkpty(&master_fd
, NULL
, NULL
, NULL
);
3728 return posix_error();
3731 return Py_BuildValue("(li)", pid
, master_fd
);
3736 PyDoc_STRVAR(posix_getegid__doc__
,
3737 "getegid() -> egid\n\n\
3738 Return the current process's effective group id.");
3741 posix_getegid(PyObject
*self
, PyObject
*noargs
)
3743 return PyInt_FromLong((long)getegid());
3749 PyDoc_STRVAR(posix_geteuid__doc__
,
3750 "geteuid() -> euid\n\n\
3751 Return the current process's effective user id.");
3754 posix_geteuid(PyObject
*self
, PyObject
*noargs
)
3756 return PyInt_FromLong((long)geteuid());
3762 PyDoc_STRVAR(posix_getgid__doc__
,
3763 "getgid() -> gid\n\n\
3764 Return the current process's group id.");
3767 posix_getgid(PyObject
*self
, PyObject
*noargs
)
3769 return PyInt_FromLong((long)getgid());
3774 PyDoc_STRVAR(posix_getpid__doc__
,
3775 "getpid() -> pid\n\n\
3776 Return the current process id");
3779 posix_getpid(PyObject
*self
, PyObject
*noargs
)
3781 return PyInt_FromLong((long)getpid());
3785 #ifdef HAVE_GETGROUPS
3786 PyDoc_STRVAR(posix_getgroups__doc__
,
3787 "getgroups() -> list of group IDs\n\n\
3788 Return list of supplemental group IDs for the process.");
3791 posix_getgroups(PyObject
*self
, PyObject
*noargs
)
3793 PyObject
*result
= NULL
;
3796 #define MAX_GROUPS NGROUPS_MAX
3798 /* defined to be 16 on Solaris7, so this should be a small number */
3799 #define MAX_GROUPS 64
3801 gid_t grouplist
[MAX_GROUPS
];
3804 n
= getgroups(MAX_GROUPS
, grouplist
);
3808 result
= PyList_New(n
);
3809 if (result
!= NULL
) {
3811 for (i
= 0; i
< n
; ++i
) {
3812 PyObject
*o
= PyInt_FromLong((long)grouplist
[i
]);
3818 PyList_SET_ITEM(result
, i
, o
);
3828 PyDoc_STRVAR(posix_getpgid__doc__
,
3829 "getpgid(pid) -> pgid\n\n\
3830 Call the system call getpgid().");
3833 posix_getpgid(PyObject
*self
, PyObject
*args
)
3836 if (!PyArg_ParseTuple(args
, "i:getpgid", &pid
))
3838 pgid
= getpgid(pid
);
3840 return posix_error();
3841 return PyInt_FromLong((long)pgid
);
3843 #endif /* HAVE_GETPGID */
3847 PyDoc_STRVAR(posix_getpgrp__doc__
,
3848 "getpgrp() -> pgrp\n\n\
3849 Return the current process group id.");
3852 posix_getpgrp(PyObject
*self
, PyObject
*noargs
)
3854 #ifdef GETPGRP_HAVE_ARG
3855 return PyInt_FromLong((long)getpgrp(0));
3856 #else /* GETPGRP_HAVE_ARG */
3857 return PyInt_FromLong((long)getpgrp());
3858 #endif /* GETPGRP_HAVE_ARG */
3860 #endif /* HAVE_GETPGRP */
3864 PyDoc_STRVAR(posix_setpgrp__doc__
,
3866 Make this process a session leader.");
3869 posix_setpgrp(PyObject
*self
, PyObject
*noargs
)
3871 #ifdef SETPGRP_HAVE_ARG
3872 if (setpgrp(0, 0) < 0)
3873 #else /* SETPGRP_HAVE_ARG */
3875 #endif /* SETPGRP_HAVE_ARG */
3876 return posix_error();
3881 #endif /* HAVE_SETPGRP */
3884 PyDoc_STRVAR(posix_getppid__doc__
,
3885 "getppid() -> ppid\n\n\
3886 Return the parent's process id.");
3889 posix_getppid(PyObject
*self
, PyObject
*noargs
)
3891 return PyInt_FromLong(getppid());
3896 #ifdef HAVE_GETLOGIN
3897 PyDoc_STRVAR(posix_getlogin__doc__
,
3898 "getlogin() -> string\n\n\
3899 Return the actual login name.");
3902 posix_getlogin(PyObject
*self
, PyObject
*noargs
)
3904 PyObject
*result
= NULL
;
3906 int old_errno
= errno
;
3914 PyErr_SetString(PyExc_OSError
,
3915 "unable to determine login name");
3918 result
= PyString_FromString(name
);
3926 PyDoc_STRVAR(posix_getuid__doc__
,
3927 "getuid() -> uid\n\n\
3928 Return the current process's user id.");
3931 posix_getuid(PyObject
*self
, PyObject
*noargs
)
3933 return PyInt_FromLong((long)getuid());
3939 PyDoc_STRVAR(posix_kill__doc__
,
3940 "kill(pid, sig)\n\n\
3941 Kill a process with a signal.");
3944 posix_kill(PyObject
*self
, PyObject
*args
)
3948 if (!PyArg_ParseTuple(args
, "ii:kill", &pid
, &sig
))
3950 #if defined(PYOS_OS2) && !defined(PYCC_GCC)
3951 if (sig
== XCPT_SIGNAL_INTR
|| sig
== XCPT_SIGNAL_BREAK
) {
3953 if ((rc
= DosSendSignalException(pid
, sig
)) != NO_ERROR
)
3954 return os2_error(rc
);
3956 } else if (sig
== XCPT_SIGNAL_KILLPROC
) {
3958 if ((rc
= DosKillProcess(DKP_PROCESS
, pid
)) != NO_ERROR
)
3959 return os2_error(rc
);
3962 return NULL
; /* Unrecognized Signal Requested */
3964 if (kill(pid
, sig
) == -1)
3965 return posix_error();
3973 PyDoc_STRVAR(posix_killpg__doc__
,
3974 "killpg(pgid, sig)\n\n\
3975 Kill a process group with a signal.");
3978 posix_killpg(PyObject
*self
, PyObject
*args
)
3981 if (!PyArg_ParseTuple(args
, "ii:killpg", &pgid
, &sig
))
3983 if (killpg(pgid
, sig
) == -1)
3984 return posix_error();
3992 #ifdef HAVE_SYS_LOCK_H
3993 #include <sys/lock.h>
3996 PyDoc_STRVAR(posix_plock__doc__
,
3998 Lock program segments into memory.");
4001 posix_plock(PyObject
*self
, PyObject
*args
)
4004 if (!PyArg_ParseTuple(args
, "i:plock", &op
))
4006 if (plock(op
) == -1)
4007 return posix_error();
4015 PyDoc_STRVAR(posix_popen__doc__
,
4016 "popen(command [, mode='r' [, bufsize]]) -> pipe\n\n\
4017 Open a pipe to/from a command returning a file object.");
4019 #if defined(PYOS_OS2)
4020 #if defined(PYCC_VACPP)
4022 async_system(const char *command
)
4024 char errormsg
[256], args
[1024];
4028 char *shell
= getenv("COMSPEC");
4032 /* avoid overflowing the argument buffer */
4033 if (strlen(shell
) + 3 + strlen(command
) >= 1024)
4034 return ERROR_NOT_ENOUGH_MEMORY
4037 strcat(args
, shell
);
4038 strcat(args
, "/c ");
4039 strcat(args
, command
);
4041 /* execute asynchronously, inheriting the environment */
4042 rc
= DosExecPgm(errormsg
,
4053 popen(const char *command
, const char *mode
, int pipesize
, int *err
)
4059 /* mode determines which of stdin or stdout is reconnected to
4060 * the pipe to the child
4062 if (strchr(mode
, 'r') != NULL
) {
4063 tgt_fd
= 1; /* stdout */
4064 } else if (strchr(mode
, 'w')) {
4065 tgt_fd
= 0; /* stdin */
4067 *err
= ERROR_INVALID_ACCESS
;
4071 /* setup the pipe */
4072 if ((rc
= DosCreatePipe(&pipeh
[0], &pipeh
[1], pipesize
)) != NO_ERROR
) {
4077 /* prevent other threads accessing stdio */
4080 /* reconnect stdio and execute child */
4083 if (dup2(pipeh
[tgtfd
], tgtfd
) == 0) {
4084 DosClose(pipeh
[tgtfd
]);
4085 rc
= async_system(command
);
4092 /* allow other threads access to stdio */
4095 /* if execution of child was successful return file stream */
4097 return fdopen(pipeh
[1 - tgtfd
], mode
);
4099 DosClose(pipeh
[1 - tgtfd
]);
4106 posix_popen(PyObject
*self
, PyObject
*args
)
4110 int err
, bufsize
= -1;
4113 if (!PyArg_ParseTuple(args
, "s|si:popen", &name
, &mode
, &bufsize
))
4115 Py_BEGIN_ALLOW_THREADS
4116 fp
= popen(name
, mode
, (bufsize
> 0) ? bufsize
: 4096, &err
);
4117 Py_END_ALLOW_THREADS
4119 return os2_error(err
);
4121 f
= PyFile_FromFile(fp
, name
, mode
, fclose
);
4123 PyFile_SetBufSize(f
, bufsize
);
4127 #elif defined(PYCC_GCC)
4129 /* standard posix version of popen() support */
4131 posix_popen(PyObject
*self
, PyObject
*args
)
4138 if (!PyArg_ParseTuple(args
, "s|si:popen", &name
, &mode
, &bufsize
))
4140 Py_BEGIN_ALLOW_THREADS
4141 fp
= popen(name
, mode
);
4142 Py_END_ALLOW_THREADS
4144 return posix_error();
4145 f
= PyFile_FromFile(fp
, name
, mode
, pclose
);
4147 PyFile_SetBufSize(f
, bufsize
);
4151 /* fork() under OS/2 has lots'o'warts
4152 * EMX supports pipe() and spawn*() so we can synthesize popen[234]()
4153 * most of this code is a ripoff of the win32 code, but using the
4154 * capabilities of EMX's C library routines
4157 /* These tell _PyPopen() whether to return 1, 2, or 3 file objects. */
4163 static PyObject
*_PyPopen(char *, int, int, int);
4164 static int _PyPclose(FILE *file
);
4167 * Internal dictionary mapping popen* file pointers to process handles,
4168 * for use when retrieving the process exit code. See _PyPclose() below
4169 * for more information on this dictionary's use.
4171 static PyObject
*_PyPopenProcs
= NULL
;
4173 /* os2emx version of popen2()
4175 * The result of this function is a pipe (file) connected to the
4176 * process's stdin, and a pipe connected to the process's stdout.
4180 os2emx_popen2(PyObject
*self
, PyObject
*args
)
4188 if (!PyArg_ParseTuple(args
, "s|si:popen2", &cmdstring
, &mode
, &bufsize
))
4193 else if (*mode
!= 'b') {
4194 PyErr_SetString(PyExc_ValueError
, "mode must be 't' or 'b'");
4199 f
= _PyPopen(cmdstring
, tm
, POPEN_2
, bufsize
);
4205 * Variation on os2emx.popen2
4207 * The result of this function is 3 pipes - the process's stdin,
4212 os2emx_popen3(PyObject
*self
, PyObject
*args
)
4220 if (!PyArg_ParseTuple(args
, "s|si:popen3", &cmdstring
, &mode
, &bufsize
))
4225 else if (*mode
!= 'b') {
4226 PyErr_SetString(PyExc_ValueError
, "mode must be 't' or 'b'");
4231 f
= _PyPopen(cmdstring
, tm
, POPEN_3
, bufsize
);
4237 * Variation on os2emx.popen2
4239 * The result of this function is 2 pipes - the processes stdin,
4240 * and stdout+stderr combined as a single pipe.
4244 os2emx_popen4(PyObject
*self
, PyObject
*args
)
4252 if (!PyArg_ParseTuple(args
, "s|si:popen4", &cmdstring
, &mode
, &bufsize
))
4257 else if (*mode
!= 'b') {
4258 PyErr_SetString(PyExc_ValueError
, "mode must be 't' or 'b'");
4263 f
= _PyPopen(cmdstring
, tm
, POPEN_4
, bufsize
);
4268 /* a couple of structures for convenient handling of multiple
4269 * file handles and pipes
4283 /* The following code is derived from the win32 code */
4286 _PyPopen(char *cmdstring
, int mode
, int n
, int bufsize
)
4288 struct file_ref stdio
[3];
4289 struct pipe_ref p_fd
[3];
4291 int file_count
, i
, pipe_err
;
4293 char *shell
, *sh_name
, *opt
, *rd_mode
, *wr_mode
;
4294 PyObject
*f
, *p_f
[3];
4296 /* file modes for subsequent fdopen's on pipe handles */
4308 /* prepare shell references */
4309 if ((shell
= getenv("EMXSHELL")) == NULL
)
4310 if ((shell
= getenv("COMSPEC")) == NULL
)
4313 return posix_error();
4316 sh_name
= _getname(shell
);
4317 if (stricmp(sh_name
, "cmd.exe") == 0 || stricmp(sh_name
, "4os2.exe") == 0)
4322 /* save current stdio fds + their flags, and set not inheritable */
4324 while (pipe_err
>= 0 && i
< 3)
4326 pipe_err
= stdio
[i
].handle
= dup(i
);
4327 stdio
[i
].flags
= fcntl(i
, F_GETFD
, 0);
4328 fcntl(stdio
[i
].handle
, F_SETFD
, stdio
[i
].flags
| FD_CLOEXEC
);
4333 /* didn't get them all saved - clean up and bail out */
4334 int saved_err
= errno
;
4337 close(stdio
[i
].handle
);
4340 return posix_error();
4343 /* create pipe ends */
4348 while ((pipe_err
== 0) && (i
< file_count
))
4349 pipe_err
= pipe((int *)&p_fd
[i
++]);
4352 /* didn't get them all made - clean up and bail out */
4359 return posix_error();
4362 /* change the actual standard IO streams over temporarily,
4363 * making the retained pipe ends non-inheritable
4368 if (dup2(p_fd
[0].rd
, 0) == 0)
4371 i
= fcntl(p_fd
[0].wr
, F_GETFD
, 0);
4372 fcntl(p_fd
[0].wr
, F_SETFD
, i
| FD_CLOEXEC
);
4373 if ((p_s
[0] = fdopen(p_fd
[0].wr
, wr_mode
)) == NULL
)
4387 if (dup2(p_fd
[1].wr
, 1) == 1)
4390 i
= fcntl(p_fd
[1].rd
, F_GETFD
, 0);
4391 fcntl(p_fd
[1].rd
, F_SETFD
, i
| FD_CLOEXEC
);
4392 if ((p_s
[1] = fdopen(p_fd
[1].rd
, rd_mode
)) == NULL
)
4404 /* - stderr, as required */
4410 if (dup2(p_fd
[2].wr
, 2) == 2)
4413 i
= fcntl(p_fd
[2].rd
, F_GETFD
, 0);
4414 fcntl(p_fd
[2].rd
, F_SETFD
, i
| FD_CLOEXEC
);
4415 if ((p_s
[2] = fdopen(p_fd
[2].rd
, rd_mode
)) == NULL
)
4430 if (dup2(1, 2) != 2)
4438 /* spawn the child process */
4441 pipe_pid
= spawnlp(P_NOWAIT
, shell
, shell
, opt
, cmdstring
, (char *)0);
4448 /* save the PID into the FILE structure
4449 * NOTE: this implementation doesn't actually
4450 * take advantage of this, but do it for
4451 * completeness - AIM Apr01
4453 for (i
= 0; i
< file_count
; i
++)
4454 p_s
[i
]->_pid
= pipe_pid
;
4458 /* reset standard IO to normal */
4459 for (i
= 0; i
< 3; i
++)
4461 dup2(stdio
[i
].handle
, i
);
4462 fcntl(i
, F_SETFD
, stdio
[i
].flags
);
4463 close(stdio
[i
].handle
);
4466 /* if any remnant problems, clean up and bail out */
4469 for (i
= 0; i
< 3; i
++)
4475 return posix_error_with_filename(cmdstring
);
4478 /* build tuple of file objects to return */
4479 if ((p_f
[0] = PyFile_FromFile(p_s
[0], cmdstring
, wr_mode
, _PyPclose
)) != NULL
)
4480 PyFile_SetBufSize(p_f
[0], bufsize
);
4481 if ((p_f
[1] = PyFile_FromFile(p_s
[1], cmdstring
, rd_mode
, _PyPclose
)) != NULL
)
4482 PyFile_SetBufSize(p_f
[1], bufsize
);
4485 if ((p_f
[2] = PyFile_FromFile(p_s
[2], cmdstring
, rd_mode
, _PyPclose
)) != NULL
)
4486 PyFile_SetBufSize(p_f
[0], bufsize
);
4487 f
= PyTuple_Pack(3, p_f
[0], p_f
[1], p_f
[2]);
4490 f
= PyTuple_Pack(2, p_f
[0], p_f
[1]);
4493 * Insert the files we've created into the process dictionary
4494 * all referencing the list with the process handle and the
4495 * initial number of files (see description below in _PyPclose).
4496 * Since if _PyPclose later tried to wait on a process when all
4497 * handles weren't closed, it could create a deadlock with the
4498 * child, we spend some energy here to try to ensure that we
4499 * either insert all file handles into the dictionary or none
4500 * at all. It's a little clumsy with the various popen modes
4501 * and variable number of files involved.
4505 _PyPopenProcs
= PyDict_New();
4510 PyObject
*procObj
, *pidObj
, *intObj
, *fileObj
[3];
4513 fileObj
[0] = fileObj
[1] = fileObj
[2] = NULL
;
4514 ins_rc
[0] = ins_rc
[1] = ins_rc
[2] = 0;
4516 procObj
= PyList_New(2);
4517 pidObj
= PyInt_FromLong((long) pipe_pid
);
4518 intObj
= PyInt_FromLong((long) file_count
);
4520 if (procObj
&& pidObj
&& intObj
)
4522 PyList_SetItem(procObj
, 0, pidObj
);
4523 PyList_SetItem(procObj
, 1, intObj
);
4525 fileObj
[0] = PyLong_FromVoidPtr(p_s
[0]);
4528 ins_rc
[0] = PyDict_SetItem(_PyPopenProcs
,
4532 fileObj
[1] = PyLong_FromVoidPtr(p_s
[1]);
4535 ins_rc
[1] = PyDict_SetItem(_PyPopenProcs
,
4539 if (file_count
>= 3)
4541 fileObj
[2] = PyLong_FromVoidPtr(p_s
[2]);
4544 ins_rc
[2] = PyDict_SetItem(_PyPopenProcs
,
4550 if (ins_rc
[0] < 0 || !fileObj
[0] ||
4551 ins_rc
[1] < 0 || (file_count
> 1 && !fileObj
[1]) ||
4552 ins_rc
[2] < 0 || (file_count
> 2 && !fileObj
[2]))
4554 /* Something failed - remove any dictionary
4555 * entries that did make it.
4557 if (!ins_rc
[0] && fileObj
[0])
4559 PyDict_DelItem(_PyPopenProcs
,
4562 if (!ins_rc
[1] && fileObj
[1])
4564 PyDict_DelItem(_PyPopenProcs
,
4567 if (!ins_rc
[2] && fileObj
[2])
4569 PyDict_DelItem(_PyPopenProcs
,
4576 * Clean up our localized references for the dictionary keys
4577 * and value since PyDict_SetItem will Py_INCREF any copies
4578 * that got placed in the dictionary.
4580 Py_XDECREF(procObj
);
4581 Py_XDECREF(fileObj
[0]);
4582 Py_XDECREF(fileObj
[1]);
4583 Py_XDECREF(fileObj
[2]);
4586 /* Child is launched. */
4591 * Wrapper for fclose() to use for popen* files, so we can retrieve the
4592 * exit code for the child process and return as a result of the close.
4594 * This function uses the _PyPopenProcs dictionary in order to map the
4595 * input file pointer to information about the process that was
4596 * originally created by the popen* call that created the file pointer.
4597 * The dictionary uses the file pointer as a key (with one entry
4598 * inserted for each file returned by the original popen* call) and a
4599 * single list object as the value for all files from a single call.
4600 * The list object contains the Win32 process handle at [0], and a file
4601 * count at [1], which is initialized to the total number of file
4602 * handles using that list.
4604 * This function closes whichever handle it is passed, and decrements
4605 * the file count in the dictionary for the process handle pointed to
4606 * by this file. On the last close (when the file count reaches zero),
4607 * this function will wait for the child process and then return its
4608 * exit code as the result of the close() operation. This permits the
4609 * files to be closed in any order - it is always the close() of the
4610 * final handle that will return the exit code.
4612 * NOTE: This function is currently called with the GIL released.
4613 * hence we use the GILState API to manage our state.
4616 static int _PyPclose(FILE *file
)
4621 PyObject
*procObj
, *pidObj
, *intObj
, *fileObj
;
4624 PyGILState_STATE state
;
4627 /* Close the file handle first, to ensure it can't block the
4628 * child from exiting if it's the last handle.
4630 result
= fclose(file
);
4633 state
= PyGILState_Ensure();
4637 if ((fileObj
= PyLong_FromVoidPtr(file
)) != NULL
&&
4638 (procObj
= PyDict_GetItem(_PyPopenProcs
,
4639 fileObj
)) != NULL
&&
4640 (pidObj
= PyList_GetItem(procObj
,0)) != NULL
&&
4641 (intObj
= PyList_GetItem(procObj
,1)) != NULL
)
4643 pipe_pid
= (int) PyInt_AsLong(pidObj
);
4644 file_count
= (int) PyInt_AsLong(intObj
);
4648 /* Still other files referencing process */
4650 PyList_SetItem(procObj
,1,
4651 PyInt_FromLong((long) file_count
));
4655 /* Last file for this process */
4656 if (result
!= EOF
&&
4657 waitpid(pipe_pid
, &exit_code
, 0) == pipe_pid
)
4659 /* extract exit status */
4660 if (WIFEXITED(exit_code
))
4662 result
= WEXITSTATUS(exit_code
);
4672 /* Indicate failure - this will cause the file object
4673 * to raise an I/O error and translate the last
4674 * error code from errno. We do have a problem with
4675 * last errors that overlap the normal errno table,
4676 * but that's a consistent problem with the file object.
4682 /* Remove this file pointer from dictionary */
4683 PyDict_DelItem(_PyPopenProcs
, fileObj
);
4685 if (PyDict_Size(_PyPopenProcs
) == 0)
4687 Py_DECREF(_PyPopenProcs
);
4688 _PyPopenProcs
= NULL
;
4691 } /* if object retrieval ok */
4693 Py_XDECREF(fileObj
);
4694 } /* if _PyPopenProcs */
4697 PyGILState_Release(state
);
4702 #endif /* PYCC_??? */
4704 #elif defined(MS_WINDOWS)
4707 * Portable 'popen' replacement for Win32.
4709 * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks
4710 * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com>
4711 * Return code handling by David Bolen <db3l@fitlinxx.com>.
4718 /* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */
4724 static PyObject
*_PyPopen(char *, int, int);
4725 static int _PyPclose(FILE *file
);
4728 * Internal dictionary mapping popen* file pointers to process handles,
4729 * for use when retrieving the process exit code. See _PyPclose() below
4730 * for more information on this dictionary's use.
4732 static PyObject
*_PyPopenProcs
= NULL
;
4735 /* popen that works from a GUI.
4737 * The result of this function is a pipe (file) connected to the
4738 * processes stdin or stdout, depending on the requested mode.
4742 posix_popen(PyObject
*self
, PyObject
*args
)
4750 if (!PyArg_ParseTuple(args
, "s|si:popen", &cmdstring
, &mode
, &bufsize
))
4755 else if (*mode
!= 'w') {
4756 PyErr_SetString(PyExc_ValueError
, "popen() arg 2 must be 'r' or 'w'");
4761 if (bufsize
!= -1) {
4762 PyErr_SetString(PyExc_ValueError
, "popen() arg 3 must be -1");
4766 if (*(mode
+1) == 't')
4767 f
= _PyPopen(cmdstring
, tm
| _O_TEXT
, POPEN_1
);
4768 else if (*(mode
+1) == 'b')
4769 f
= _PyPopen(cmdstring
, tm
| _O_BINARY
, POPEN_1
);
4771 f
= _PyPopen(cmdstring
, tm
| _O_TEXT
, POPEN_1
);
4776 /* Variation on win32pipe.popen
4778 * The result of this function is a pipe (file) connected to the
4779 * process's stdin, and a pipe connected to the process's stdout.
4783 win32_popen2(PyObject
*self
, PyObject
*args
)
4791 if (!PyArg_ParseTuple(args
, "s|si:popen2", &cmdstring
, &mode
, &bufsize
))
4796 else if (*mode
!= 'b') {
4797 PyErr_SetString(PyExc_ValueError
, "popen2() arg 2 must be 't' or 'b'");
4802 if (bufsize
!= -1) {
4803 PyErr_SetString(PyExc_ValueError
, "popen2() arg 3 must be -1");
4807 f
= _PyPopen(cmdstring
, tm
, POPEN_2
);
4813 * Variation on <om win32pipe.popen>
4815 * The result of this function is 3 pipes - the process's stdin,
4820 win32_popen3(PyObject
*self
, PyObject
*args
)
4828 if (!PyArg_ParseTuple(args
, "s|si:popen3", &cmdstring
, &mode
, &bufsize
))
4833 else if (*mode
!= 'b') {
4834 PyErr_SetString(PyExc_ValueError
, "popen3() arg 2 must be 't' or 'b'");
4839 if (bufsize
!= -1) {
4840 PyErr_SetString(PyExc_ValueError
, "popen3() arg 3 must be -1");
4844 f
= _PyPopen(cmdstring
, tm
, POPEN_3
);
4850 * Variation on win32pipe.popen
4852 * The result of this function is 2 pipes - the processes stdin,
4853 * and stdout+stderr combined as a single pipe.
4857 win32_popen4(PyObject
*self
, PyObject
*args
)
4865 if (!PyArg_ParseTuple(args
, "s|si:popen4", &cmdstring
, &mode
, &bufsize
))
4870 else if (*mode
!= 'b') {
4871 PyErr_SetString(PyExc_ValueError
, "popen4() arg 2 must be 't' or 'b'");
4876 if (bufsize
!= -1) {
4877 PyErr_SetString(PyExc_ValueError
, "popen4() arg 3 must be -1");
4881 f
= _PyPopen(cmdstring
, tm
, POPEN_4
);
4887 _PyPopenCreateProcess(char *cmdstring
,
4893 PROCESS_INFORMATION piProcInfo
;
4894 STARTUPINFO siStartInfo
;
4895 DWORD dwProcessFlags
= 0; /* no NEW_CONSOLE by default for Ctrl+C handling */
4896 char *s1
,*s2
, *s3
= " /c ";
4897 const char *szConsoleSpawn
= "w9xpopen.exe";
4901 if (i
= GetEnvironmentVariable("COMSPEC",NULL
,0)) {
4904 s1
= (char *)alloca(i
);
4905 if (!(x
= GetEnvironmentVariable("COMSPEC", s1
, i
)))
4906 /* x < i, so x fits into an integer */
4909 /* Explicitly check if we are using COMMAND.COM. If we are
4910 * then use the w9xpopen hack.
4913 while (comshell
>= s1
&& *comshell
!= '\\')
4917 if (GetVersion() < 0x80000000 &&
4918 _stricmp(comshell
, "command.com") != 0) {
4919 /* NT/2000 and not using command.com. */
4920 x
= i
+ strlen(s3
) + strlen(cmdstring
) + 1;
4921 s2
= (char *)alloca(x
);
4923 PyOS_snprintf(s2
, x
, "%s%s%s", s1
, s3
, cmdstring
);
4927 * Oh gag, we're on Win9x or using COMMAND.COM. Use
4928 * the workaround listed in KB: Q150956
4930 char modulepath
[_MAX_PATH
];
4931 struct stat statinfo
;
4932 GetModuleFileName(NULL
, modulepath
, sizeof(modulepath
));
4933 for (x
= i
= 0; modulepath
[i
]; i
++)
4934 if (modulepath
[i
] == SEP
)
4936 modulepath
[x
] = '\0';
4937 /* Create the full-name to w9xpopen, so we can test it exists */
4940 (sizeof(modulepath
)/sizeof(modulepath
[0]))
4941 -strlen(modulepath
));
4942 if (stat(modulepath
, &statinfo
) != 0) {
4943 size_t mplen
= sizeof(modulepath
)/sizeof(modulepath
[0]);
4944 /* Eeek - file-not-found - possibly an embedding
4945 situation - see if we can locate it in sys.prefix
4950 modulepath
[mplen
-1] = '\0';
4951 if (modulepath
[strlen(modulepath
)-1] != '\\')
4952 strcat(modulepath
, "\\");
4955 mplen
-strlen(modulepath
));
4956 /* No where else to look - raise an easily identifiable
4957 error, rather than leaving Windows to report
4958 "file not found" - as the user is probably blissfully
4959 unaware this shim EXE is used, and it will confuse them.
4960 (well, it confused me for a while ;-)
4962 if (stat(modulepath
, &statinfo
) != 0) {
4963 PyErr_Format(PyExc_RuntimeError
,
4964 "Can not locate '%s' which is needed "
4965 "for popen to work with your shell "
4971 x
= i
+ strlen(s3
) + strlen(cmdstring
) + 1 +
4972 strlen(modulepath
) +
4973 strlen(szConsoleSpawn
) + 1;
4975 s2
= (char *)alloca(x
);
4977 /* To maintain correct argument passing semantics,
4978 we pass the command-line as it stands, and allow
4979 quoting to be applied. w9xpopen.exe will then
4980 use its argv vector, and re-quote the necessary
4981 args for the ultimate child process.
4990 /* Not passing CREATE_NEW_CONSOLE has been known to
4991 cause random failures on win9x. Specifically a
4993 "Your program accessed mem currently in use at xxx"
4994 and a hopeful warning about the stability of your
4996 Cost is Ctrl+C wont kill children, but anyone
4997 who cares can have a go!
4999 dwProcessFlags
|= CREATE_NEW_CONSOLE
;
5003 /* Could be an else here to try cmd.exe / command.com in the path
5004 Now we'll just error out.. */
5006 PyErr_SetString(PyExc_RuntimeError
,
5007 "Cannot locate a COMSPEC environment variable to "
5008 "use as the shell");
5012 ZeroMemory(&siStartInfo
, sizeof(STARTUPINFO
));
5013 siStartInfo
.cb
= sizeof(STARTUPINFO
);
5014 siStartInfo
.dwFlags
= STARTF_USESTDHANDLES
| STARTF_USESHOWWINDOW
;
5015 siStartInfo
.hStdInput
= hStdin
;
5016 siStartInfo
.hStdOutput
= hStdout
;
5017 siStartInfo
.hStdError
= hStderr
;
5018 siStartInfo
.wShowWindow
= SW_HIDE
;
5020 if (CreateProcess(NULL
,
5030 /* Close the handles now so anyone waiting is woken. */
5031 CloseHandle(piProcInfo
.hThread
);
5033 /* Return process handle */
5034 *hProcess
= piProcInfo
.hProcess
;
5037 win32_error("CreateProcess", s2
);
5041 /* The following code is based off of KB: Q190351 */
5044 _PyPopen(char *cmdstring
, int mode
, int n
)
5046 HANDLE hChildStdinRd
, hChildStdinWr
, hChildStdoutRd
, hChildStdoutWr
,
5047 hChildStderrRd
, hChildStderrWr
, hChildStdinWrDup
, hChildStdoutRdDup
,
5048 hChildStderrRdDup
, hProcess
; /* hChildStdoutWrDup; */
5050 SECURITY_ATTRIBUTES saAttr
;
5057 saAttr
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
5058 saAttr
.bInheritHandle
= TRUE
;
5059 saAttr
.lpSecurityDescriptor
= NULL
;
5061 if (!CreatePipe(&hChildStdinRd
, &hChildStdinWr
, &saAttr
, 0))
5062 return win32_error("CreatePipe", NULL
);
5064 /* Create new output read handle and the input write handle. Set
5065 * the inheritance properties to FALSE. Otherwise, the child inherits
5066 * these handles; resulting in non-closeable handles to the pipes
5068 fSuccess
= DuplicateHandle(GetCurrentProcess(), hChildStdinWr
,
5069 GetCurrentProcess(), &hChildStdinWrDup
, 0,
5071 DUPLICATE_SAME_ACCESS
);
5073 return win32_error("DuplicateHandle", NULL
);
5075 /* Close the inheritable version of ChildStdin
5076 that we're using. */
5077 CloseHandle(hChildStdinWr
);
5079 if (!CreatePipe(&hChildStdoutRd
, &hChildStdoutWr
, &saAttr
, 0))
5080 return win32_error("CreatePipe", NULL
);
5082 fSuccess
= DuplicateHandle(GetCurrentProcess(), hChildStdoutRd
,
5083 GetCurrentProcess(), &hChildStdoutRdDup
, 0,
5084 FALSE
, DUPLICATE_SAME_ACCESS
);
5086 return win32_error("DuplicateHandle", NULL
);
5088 /* Close the inheritable version of ChildStdout
5089 that we're using. */
5090 CloseHandle(hChildStdoutRd
);
5093 if (!CreatePipe(&hChildStderrRd
, &hChildStderrWr
, &saAttr
, 0))
5094 return win32_error("CreatePipe", NULL
);
5095 fSuccess
= DuplicateHandle(GetCurrentProcess(),
5097 GetCurrentProcess(),
5098 &hChildStderrRdDup
, 0,
5099 FALSE
, DUPLICATE_SAME_ACCESS
);
5101 return win32_error("DuplicateHandle", NULL
);
5102 /* Close the inheritable version of ChildStdErr that we're using. */
5103 CloseHandle(hChildStderrRd
);
5108 switch (mode
& (_O_RDONLY
| _O_TEXT
| _O_BINARY
| _O_WRONLY
)) {
5109 case _O_WRONLY
| _O_TEXT
:
5110 /* Case for writing to child Stdin in text mode. */
5111 fd1
= _open_osfhandle((Py_intptr_t
)hChildStdinWrDup
, mode
);
5112 f1
= _fdopen(fd1
, "w");
5113 f
= PyFile_FromFile(f1
, cmdstring
, "w", _PyPclose
);
5114 PyFile_SetBufSize(f
, 0);
5115 /* We don't care about these pipes anymore, so close them. */
5116 CloseHandle(hChildStdoutRdDup
);
5117 CloseHandle(hChildStderrRdDup
);
5120 case _O_RDONLY
| _O_TEXT
:
5121 /* Case for reading from child Stdout in text mode. */
5122 fd1
= _open_osfhandle((Py_intptr_t
)hChildStdoutRdDup
, mode
);
5123 f1
= _fdopen(fd1
, "r");
5124 f
= PyFile_FromFile(f1
, cmdstring
, "r", _PyPclose
);
5125 PyFile_SetBufSize(f
, 0);
5126 /* We don't care about these pipes anymore, so close them. */
5127 CloseHandle(hChildStdinWrDup
);
5128 CloseHandle(hChildStderrRdDup
);
5131 case _O_RDONLY
| _O_BINARY
:
5132 /* Case for readinig from child Stdout in binary mode. */
5133 fd1
= _open_osfhandle((Py_intptr_t
)hChildStdoutRdDup
, mode
);
5134 f1
= _fdopen(fd1
, "rb");
5135 f
= PyFile_FromFile(f1
, cmdstring
, "rb", _PyPclose
);
5136 PyFile_SetBufSize(f
, 0);
5137 /* We don't care about these pipes anymore, so close them. */
5138 CloseHandle(hChildStdinWrDup
);
5139 CloseHandle(hChildStderrRdDup
);
5142 case _O_WRONLY
| _O_BINARY
:
5143 /* Case for writing to child Stdin in binary mode. */
5144 fd1
= _open_osfhandle((Py_intptr_t
)hChildStdinWrDup
, mode
);
5145 f1
= _fdopen(fd1
, "wb");
5146 f
= PyFile_FromFile(f1
, cmdstring
, "wb", _PyPclose
);
5147 PyFile_SetBufSize(f
, 0);
5148 /* We don't care about these pipes anymore, so close them. */
5149 CloseHandle(hChildStdoutRdDup
);
5150 CloseHandle(hChildStderrRdDup
);
5162 if (mode
& _O_TEXT
) {
5170 fd1
= _open_osfhandle((Py_intptr_t
)hChildStdinWrDup
, mode
);
5171 f1
= _fdopen(fd1
, m2
);
5172 fd2
= _open_osfhandle((Py_intptr_t
)hChildStdoutRdDup
, mode
);
5173 f2
= _fdopen(fd2
, m1
);
5174 p1
= PyFile_FromFile(f1
, cmdstring
, m2
, _PyPclose
);
5175 PyFile_SetBufSize(p1
, 0);
5176 p2
= PyFile_FromFile(f2
, cmdstring
, m1
, _PyPclose
);
5177 PyFile_SetBufSize(p2
, 0);
5180 CloseHandle(hChildStderrRdDup
);
5182 f
= PyTuple_Pack(2,p1
,p2
);
5192 PyObject
*p1
, *p2
, *p3
;
5194 if (mode
& _O_TEXT
) {
5202 fd1
= _open_osfhandle((Py_intptr_t
)hChildStdinWrDup
, mode
);
5203 f1
= _fdopen(fd1
, m2
);
5204 fd2
= _open_osfhandle((Py_intptr_t
)hChildStdoutRdDup
, mode
);
5205 f2
= _fdopen(fd2
, m1
);
5206 fd3
= _open_osfhandle((Py_intptr_t
)hChildStderrRdDup
, mode
);
5207 f3
= _fdopen(fd3
, m1
);
5208 p1
= PyFile_FromFile(f1
, cmdstring
, m2
, _PyPclose
);
5209 p2
= PyFile_FromFile(f2
, cmdstring
, m1
, _PyPclose
);
5210 p3
= PyFile_FromFile(f3
, cmdstring
, m1
, _PyPclose
);
5211 PyFile_SetBufSize(p1
, 0);
5212 PyFile_SetBufSize(p2
, 0);
5213 PyFile_SetBufSize(p3
, 0);
5214 f
= PyTuple_Pack(3,p1
,p2
,p3
);
5224 if (!_PyPopenCreateProcess(cmdstring
,
5232 if (!_PyPopenCreateProcess(cmdstring
,
5241 * Insert the files we've created into the process dictionary
5242 * all referencing the list with the process handle and the
5243 * initial number of files (see description below in _PyPclose).
5244 * Since if _PyPclose later tried to wait on a process when all
5245 * handles weren't closed, it could create a deadlock with the
5246 * child, we spend some energy here to try to ensure that we
5247 * either insert all file handles into the dictionary or none
5248 * at all. It's a little clumsy with the various popen modes
5249 * and variable number of files involved.
5251 if (!_PyPopenProcs
) {
5252 _PyPopenProcs
= PyDict_New();
5255 if (_PyPopenProcs
) {
5256 PyObject
*procObj
, *hProcessObj
, *intObj
, *fileObj
[3];
5259 fileObj
[0] = fileObj
[1] = fileObj
[2] = NULL
;
5260 ins_rc
[0] = ins_rc
[1] = ins_rc
[2] = 0;
5262 procObj
= PyList_New(2);
5263 hProcessObj
= PyLong_FromVoidPtr(hProcess
);
5264 intObj
= PyInt_FromLong(file_count
);
5266 if (procObj
&& hProcessObj
&& intObj
) {
5267 PyList_SetItem(procObj
,0,hProcessObj
);
5268 PyList_SetItem(procObj
,1,intObj
);
5270 fileObj
[0] = PyLong_FromVoidPtr(f1
);
5272 ins_rc
[0] = PyDict_SetItem(_PyPopenProcs
,
5276 if (file_count
>= 2) {
5277 fileObj
[1] = PyLong_FromVoidPtr(f2
);
5279 ins_rc
[1] = PyDict_SetItem(_PyPopenProcs
,
5284 if (file_count
>= 3) {
5285 fileObj
[2] = PyLong_FromVoidPtr(f3
);
5287 ins_rc
[2] = PyDict_SetItem(_PyPopenProcs
,
5293 if (ins_rc
[0] < 0 || !fileObj
[0] ||
5294 ins_rc
[1] < 0 || (file_count
> 1 && !fileObj
[1]) ||
5295 ins_rc
[2] < 0 || (file_count
> 2 && !fileObj
[2])) {
5296 /* Something failed - remove any dictionary
5297 * entries that did make it.
5299 if (!ins_rc
[0] && fileObj
[0]) {
5300 PyDict_DelItem(_PyPopenProcs
,
5303 if (!ins_rc
[1] && fileObj
[1]) {
5304 PyDict_DelItem(_PyPopenProcs
,
5307 if (!ins_rc
[2] && fileObj
[2]) {
5308 PyDict_DelItem(_PyPopenProcs
,
5315 * Clean up our localized references for the dictionary keys
5316 * and value since PyDict_SetItem will Py_INCREF any copies
5317 * that got placed in the dictionary.
5319 Py_XDECREF(procObj
);
5320 Py_XDECREF(fileObj
[0]);
5321 Py_XDECREF(fileObj
[1]);
5322 Py_XDECREF(fileObj
[2]);
5325 /* Child is launched. Close the parents copy of those pipe
5326 * handles that only the child should have open. You need to
5327 * make sure that no handles to the write end of the output pipe
5328 * are maintained in this process or else the pipe will not close
5329 * when the child process exits and the ReadFile will hang. */
5331 if (!CloseHandle(hChildStdinRd
))
5332 return win32_error("CloseHandle", NULL
);
5334 if (!CloseHandle(hChildStdoutWr
))
5335 return win32_error("CloseHandle", NULL
);
5337 if ((n
!= 4) && (!CloseHandle(hChildStderrWr
)))
5338 return win32_error("CloseHandle", NULL
);
5344 * Wrapper for fclose() to use for popen* files, so we can retrieve the
5345 * exit code for the child process and return as a result of the close.
5347 * This function uses the _PyPopenProcs dictionary in order to map the
5348 * input file pointer to information about the process that was
5349 * originally created by the popen* call that created the file pointer.
5350 * The dictionary uses the file pointer as a key (with one entry
5351 * inserted for each file returned by the original popen* call) and a
5352 * single list object as the value for all files from a single call.
5353 * The list object contains the Win32 process handle at [0], and a file
5354 * count at [1], which is initialized to the total number of file
5355 * handles using that list.
5357 * This function closes whichever handle it is passed, and decrements
5358 * the file count in the dictionary for the process handle pointed to
5359 * by this file. On the last close (when the file count reaches zero),
5360 * this function will wait for the child process and then return its
5361 * exit code as the result of the close() operation. This permits the
5362 * files to be closed in any order - it is always the close() of the
5363 * final handle that will return the exit code.
5365 * NOTE: This function is currently called with the GIL released.
5366 * hence we use the GILState API to manage our state.
5369 static int _PyPclose(FILE *file
)
5374 PyObject
*procObj
, *hProcessObj
, *intObj
, *fileObj
;
5377 PyGILState_STATE state
;
5380 /* Close the file handle first, to ensure it can't block the
5381 * child from exiting if it's the last handle.
5383 result
= fclose(file
);
5385 state
= PyGILState_Ensure();
5387 if (_PyPopenProcs
) {
5388 if ((fileObj
= PyLong_FromVoidPtr(file
)) != NULL
&&
5389 (procObj
= PyDict_GetItem(_PyPopenProcs
,
5390 fileObj
)) != NULL
&&
5391 (hProcessObj
= PyList_GetItem(procObj
,0)) != NULL
&&
5392 (intObj
= PyList_GetItem(procObj
,1)) != NULL
) {
5394 hProcess
= PyLong_AsVoidPtr(hProcessObj
);
5395 file_count
= PyInt_AsLong(intObj
);
5397 if (file_count
> 1) {
5398 /* Still other files referencing process */
5400 PyList_SetItem(procObj
,1,
5401 PyInt_FromLong(file_count
));
5403 /* Last file for this process */
5404 if (result
!= EOF
&&
5405 WaitForSingleObject(hProcess
, INFINITE
) != WAIT_FAILED
&&
5406 GetExitCodeProcess(hProcess
, &exit_code
)) {
5407 /* Possible truncation here in 16-bit environments, but
5408 * real exit codes are just the lower byte in any event.
5412 /* Indicate failure - this will cause the file object
5413 * to raise an I/O error and translate the last Win32
5414 * error code from errno. We do have a problem with
5415 * last errors that overlap the normal errno table,
5416 * but that's a consistent problem with the file object.
5418 if (result
!= EOF
) {
5419 /* If the error wasn't from the fclose(), then
5420 * set errno for the file object error handling.
5422 errno
= GetLastError();
5427 /* Free up the native handle at this point */
5428 CloseHandle(hProcess
);
5431 /* Remove this file pointer from dictionary */
5432 PyDict_DelItem(_PyPopenProcs
, fileObj
);
5434 if (PyDict_Size(_PyPopenProcs
) == 0) {
5435 Py_DECREF(_PyPopenProcs
);
5436 _PyPopenProcs
= NULL
;
5439 } /* if object retrieval ok */
5441 Py_XDECREF(fileObj
);
5442 } /* if _PyPopenProcs */
5445 PyGILState_Release(state
);
5450 #else /* which OS? */
5452 posix_popen(PyObject
*self
, PyObject
*args
)
5459 if (!PyArg_ParseTuple(args
, "s|si:popen", &name
, &mode
, &bufsize
))
5461 /* Strip mode of binary or text modifiers */
5462 if (strcmp(mode
, "rb") == 0 || strcmp(mode
, "rt") == 0)
5464 else if (strcmp(mode
, "wb") == 0 || strcmp(mode
, "wt") == 0)
5466 Py_BEGIN_ALLOW_THREADS
5467 fp
= popen(name
, mode
);
5468 Py_END_ALLOW_THREADS
5470 return posix_error();
5471 f
= PyFile_FromFile(fp
, name
, mode
, pclose
);
5473 PyFile_SetBufSize(f
, bufsize
);
5477 #endif /* PYOS_??? */
5478 #endif /* HAVE_POPEN */
5482 PyDoc_STRVAR(posix_setuid__doc__
,
5484 Set the current process's user id.");
5487 posix_setuid(PyObject
*self
, PyObject
*args
)
5490 if (!PyArg_ParseTuple(args
, "i:setuid", &uid
))
5492 if (setuid(uid
) < 0)
5493 return posix_error();
5497 #endif /* HAVE_SETUID */
5501 PyDoc_STRVAR(posix_seteuid__doc__
,
5503 Set the current process's effective user id.");
5506 posix_seteuid (PyObject
*self
, PyObject
*args
)
5509 if (!PyArg_ParseTuple(args
, "i", &euid
)) {
5511 } else if (seteuid(euid
) < 0) {
5512 return posix_error();
5518 #endif /* HAVE_SETEUID */
5521 PyDoc_STRVAR(posix_setegid__doc__
,
5523 Set the current process's effective group id.");
5526 posix_setegid (PyObject
*self
, PyObject
*args
)
5529 if (!PyArg_ParseTuple(args
, "i", &egid
)) {
5531 } else if (setegid(egid
) < 0) {
5532 return posix_error();
5538 #endif /* HAVE_SETEGID */
5540 #ifdef HAVE_SETREUID
5541 PyDoc_STRVAR(posix_setreuid__doc__
,
5542 "setreuid(ruid, euid)\n\n\
5543 Set the current process's real and effective user ids.");
5546 posix_setreuid (PyObject
*self
, PyObject
*args
)
5549 if (!PyArg_ParseTuple(args
, "ii", &ruid
, &euid
)) {
5551 } else if (setreuid(ruid
, euid
) < 0) {
5552 return posix_error();
5558 #endif /* HAVE_SETREUID */
5560 #ifdef HAVE_SETREGID
5561 PyDoc_STRVAR(posix_setregid__doc__
,
5562 "setregid(rgid, egid)\n\n\
5563 Set the current process's real and effective group ids.");
5566 posix_setregid (PyObject
*self
, PyObject
*args
)
5569 if (!PyArg_ParseTuple(args
, "ii", &rgid
, &egid
)) {
5571 } else if (setregid(rgid
, egid
) < 0) {
5572 return posix_error();
5578 #endif /* HAVE_SETREGID */
5581 PyDoc_STRVAR(posix_setgid__doc__
,
5583 Set the current process's group id.");
5586 posix_setgid(PyObject
*self
, PyObject
*args
)
5589 if (!PyArg_ParseTuple(args
, "i:setgid", &gid
))
5591 if (setgid(gid
) < 0)
5592 return posix_error();
5596 #endif /* HAVE_SETGID */
5598 #ifdef HAVE_SETGROUPS
5599 PyDoc_STRVAR(posix_setgroups__doc__
,
5600 "setgroups(list)\n\n\
5601 Set the groups of the current process to list.");
5604 posix_setgroups(PyObject
*self
, PyObject
*groups
)
5607 gid_t grouplist
[MAX_GROUPS
];
5609 if (!PySequence_Check(groups
)) {
5610 PyErr_SetString(PyExc_TypeError
, "setgroups argument must be a sequence");
5613 len
= PySequence_Size(groups
);
5614 if (len
> MAX_GROUPS
) {
5615 PyErr_SetString(PyExc_ValueError
, "too many groups");
5618 for(i
= 0; i
< len
; i
++) {
5620 elem
= PySequence_GetItem(groups
, i
);
5623 if (!PyInt_Check(elem
)) {
5624 if (!PyLong_Check(elem
)) {
5625 PyErr_SetString(PyExc_TypeError
,
5626 "groups must be integers");
5630 unsigned long x
= PyLong_AsUnsignedLong(elem
);
5631 if (PyErr_Occurred()) {
5632 PyErr_SetString(PyExc_TypeError
,
5633 "group id too big");
5638 /* read back the value to see if it fitted in gid_t */
5639 if (grouplist
[i
] != x
) {
5640 PyErr_SetString(PyExc_TypeError
,
5641 "group id too big");
5647 long x
= PyInt_AsLong(elem
);
5649 if (grouplist
[i
] != x
) {
5650 PyErr_SetString(PyExc_TypeError
,
5651 "group id too big");
5659 if (setgroups(len
, grouplist
) < 0)
5660 return posix_error();
5664 #endif /* HAVE_SETGROUPS */
5666 #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
5668 wait_helper(pid_t pid
, int status
, struct rusage
*ru
)
5671 static PyObject
*struct_rusage
;
5674 return posix_error();
5676 if (struct_rusage
== NULL
) {
5677 PyObject
*m
= PyImport_ImportModuleNoBlock("resource");
5680 struct_rusage
= PyObject_GetAttrString(m
, "struct_rusage");
5682 if (struct_rusage
== NULL
)
5686 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
5687 result
= PyStructSequence_New((PyTypeObject
*) struct_rusage
);
5692 #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
5695 PyStructSequence_SET_ITEM(result
, 0,
5696 PyFloat_FromDouble(doubletime(ru
->ru_utime
)));
5697 PyStructSequence_SET_ITEM(result
, 1,
5698 PyFloat_FromDouble(doubletime(ru
->ru_stime
)));
5699 #define SET_INT(result, index, value)\
5700 PyStructSequence_SET_ITEM(result, index, PyInt_FromLong(value))
5701 SET_INT(result
, 2, ru
->ru_maxrss
);
5702 SET_INT(result
, 3, ru
->ru_ixrss
);
5703 SET_INT(result
, 4, ru
->ru_idrss
);
5704 SET_INT(result
, 5, ru
->ru_isrss
);
5705 SET_INT(result
, 6, ru
->ru_minflt
);
5706 SET_INT(result
, 7, ru
->ru_majflt
);
5707 SET_INT(result
, 8, ru
->ru_nswap
);
5708 SET_INT(result
, 9, ru
->ru_inblock
);
5709 SET_INT(result
, 10, ru
->ru_oublock
);
5710 SET_INT(result
, 11, ru
->ru_msgsnd
);
5711 SET_INT(result
, 12, ru
->ru_msgrcv
);
5712 SET_INT(result
, 13, ru
->ru_nsignals
);
5713 SET_INT(result
, 14, ru
->ru_nvcsw
);
5714 SET_INT(result
, 15, ru
->ru_nivcsw
);
5717 if (PyErr_Occurred()) {
5722 return Py_BuildValue("iiN", pid
, status
, result
);
5724 #endif /* HAVE_WAIT3 || HAVE_WAIT4 */
5727 PyDoc_STRVAR(posix_wait3__doc__
,
5728 "wait3(options) -> (pid, status, rusage)\n\n\
5729 Wait for completion of a child process.");
5732 posix_wait3(PyObject
*self
, PyObject
*args
)
5738 WAIT_STATUS_INT(status
) = 0;
5740 if (!PyArg_ParseTuple(args
, "i:wait3", &options
))
5743 Py_BEGIN_ALLOW_THREADS
5744 pid
= wait3(&status
, options
, &ru
);
5745 Py_END_ALLOW_THREADS
5747 return wait_helper(pid
, WAIT_STATUS_INT(status
), &ru
);
5749 #endif /* HAVE_WAIT3 */
5752 PyDoc_STRVAR(posix_wait4__doc__
,
5753 "wait4(pid, options) -> (pid, status, rusage)\n\n\
5754 Wait for completion of a given child process.");
5757 posix_wait4(PyObject
*self
, PyObject
*args
)
5763 WAIT_STATUS_INT(status
) = 0;
5765 if (!PyArg_ParseTuple(args
, "ii:wait4", &pid
, &options
))
5768 Py_BEGIN_ALLOW_THREADS
5769 pid
= wait4(pid
, &status
, options
, &ru
);
5770 Py_END_ALLOW_THREADS
5772 return wait_helper(pid
, WAIT_STATUS_INT(status
), &ru
);
5774 #endif /* HAVE_WAIT4 */
5777 PyDoc_STRVAR(posix_waitpid__doc__
,
5778 "waitpid(pid, options) -> (pid, status)\n\n\
5779 Wait for completion of a given child process.");
5782 posix_waitpid(PyObject
*self
, PyObject
*args
)
5787 WAIT_STATUS_INT(status
) = 0;
5789 if (!PyArg_ParseTuple(args
, "ii:waitpid", &pid
, &options
))
5791 Py_BEGIN_ALLOW_THREADS
5792 pid
= waitpid(pid
, &status
, options
);
5793 Py_END_ALLOW_THREADS
5795 return posix_error();
5797 return Py_BuildValue("ii", pid
, WAIT_STATUS_INT(status
));
5800 #elif defined(HAVE_CWAIT)
5802 /* MS C has a variant of waitpid() that's usable for most purposes. */
5803 PyDoc_STRVAR(posix_waitpid__doc__
,
5804 "waitpid(pid, options) -> (pid, status << 8)\n\n"
5805 "Wait for completion of a given process. options is ignored on Windows.");
5808 posix_waitpid(PyObject
*self
, PyObject
*args
)
5811 int status
, options
;
5813 if (!PyArg_ParseTuple(args
, "ii:waitpid", &pid
, &options
))
5815 Py_BEGIN_ALLOW_THREADS
5816 pid
= _cwait(&status
, pid
, options
);
5817 Py_END_ALLOW_THREADS
5819 return posix_error();
5821 /* shift the status left a byte so this is more like the POSIX waitpid */
5822 return Py_BuildValue("ii", pid
, status
<< 8);
5824 #endif /* HAVE_WAITPID || HAVE_CWAIT */
5827 PyDoc_STRVAR(posix_wait__doc__
,
5828 "wait() -> (pid, status)\n\n\
5829 Wait for completion of a child process.");
5832 posix_wait(PyObject
*self
, PyObject
*noargs
)
5836 WAIT_STATUS_INT(status
) = 0;
5838 Py_BEGIN_ALLOW_THREADS
5839 pid
= wait(&status
);
5840 Py_END_ALLOW_THREADS
5842 return posix_error();
5844 return Py_BuildValue("ii", pid
, WAIT_STATUS_INT(status
));
5849 PyDoc_STRVAR(posix_lstat__doc__
,
5850 "lstat(path) -> stat result\n\n\
5851 Like stat(path), but do not follow symbolic links.");
5854 posix_lstat(PyObject
*self
, PyObject
*args
)
5857 return posix_do_stat(self
, args
, "et:lstat", lstat
, NULL
, NULL
);
5858 #else /* !HAVE_LSTAT */
5860 return posix_do_stat(self
, args
, "et:lstat", STAT
, "U:lstat", win32_wstat
);
5862 return posix_do_stat(self
, args
, "et:lstat", STAT
, NULL
, NULL
);
5864 #endif /* !HAVE_LSTAT */
5868 #ifdef HAVE_READLINK
5869 PyDoc_STRVAR(posix_readlink__doc__
,
5870 "readlink(path) -> path\n\n\
5871 Return a string representing the path to which the symbolic link points.");
5874 posix_readlink(PyObject
*self
, PyObject
*args
)
5877 char buf
[MAXPATHLEN
];
5880 #ifdef Py_USING_UNICODE
5881 int arg_is_unicode
= 0;
5884 if (!PyArg_ParseTuple(args
, "et:readlink",
5885 Py_FileSystemDefaultEncoding
, &path
))
5887 #ifdef Py_USING_UNICODE
5888 v
= PySequence_GetItem(args
, 0);
5894 if (PyUnicode_Check(v
)) {
5900 Py_BEGIN_ALLOW_THREADS
5901 n
= readlink(path
, buf
, (int) sizeof buf
);
5902 Py_END_ALLOW_THREADS
5904 return posix_error_with_allocated_filename(path
);
5907 v
= PyString_FromStringAndSize(buf
, n
);
5908 #ifdef Py_USING_UNICODE
5909 if (arg_is_unicode
) {
5912 w
= PyUnicode_FromEncodedObject(v
,
5913 Py_FileSystemDefaultEncoding
,
5920 /* fall back to the original byte string, as
5921 discussed in patch #683592 */
5928 #endif /* HAVE_READLINK */
5932 PyDoc_STRVAR(posix_symlink__doc__
,
5933 "symlink(src, dst)\n\n\
5934 Create a symbolic link pointing to src named dst.");
5937 posix_symlink(PyObject
*self
, PyObject
*args
)
5939 return posix_2str(args
, "etet:symlink", symlink
);
5941 #endif /* HAVE_SYMLINK */
5946 #define HZ 60 /* Universal constant :-) */
5949 #if defined(PYCC_VACPP) && defined(PYOS_OS2)
5955 Py_BEGIN_ALLOW_THREADS
5956 DosQuerySysInfo(QSV_MS_COUNT
, QSV_MS_COUNT
, &value
, sizeof(value
));
5957 Py_END_ALLOW_THREADS
5963 posix_times(PyObject
*self
, PyObject
*noargs
)
5965 /* Currently Only Uptime is Provided -- Others Later */
5966 return Py_BuildValue("ddddd",
5967 (double)0 /* t.tms_utime / HZ */,
5968 (double)0 /* t.tms_stime / HZ */,
5969 (double)0 /* t.tms_cutime / HZ */,
5970 (double)0 /* t.tms_cstime / HZ */,
5971 (double)system_uptime() / 1000);
5975 posix_times(PyObject
*self
, PyObject
*noargs
)
5981 if (c
== (clock_t) -1)
5982 return posix_error();
5983 return Py_BuildValue("ddddd",
5984 (double)t
.tms_utime
/ HZ
,
5985 (double)t
.tms_stime
/ HZ
,
5986 (double)t
.tms_cutime
/ HZ
,
5987 (double)t
.tms_cstime
/ HZ
,
5990 #endif /* not OS2 */
5991 #endif /* HAVE_TIMES */
5995 #define HAVE_TIMES /* so the method table will pick it up */
5997 posix_times(PyObject
*self
, PyObject
*noargs
)
5999 FILETIME create
, exit
, kernel
, user
;
6001 hProc
= GetCurrentProcess();
6002 GetProcessTimes(hProc
, &create
, &exit
, &kernel
, &user
);
6003 /* The fields of a FILETIME structure are the hi and lo part
6004 of a 64-bit value expressed in 100 nanosecond units.
6005 1e7 is one second in such units; 1e-7 the inverse.
6006 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
6008 return Py_BuildValue(
6010 (double)(user
.dwHighDateTime
*429.4967296 +
6011 user
.dwLowDateTime
*1e-7),
6012 (double)(kernel
.dwHighDateTime
*429.4967296 +
6013 kernel
.dwLowDateTime
*1e-7),
6018 #endif /* MS_WINDOWS */
6021 PyDoc_STRVAR(posix_times__doc__
,
6022 "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
6023 Return a tuple of floating point numbers indicating process times.");
6028 PyDoc_STRVAR(posix_getsid__doc__
,
6029 "getsid(pid) -> sid\n\n\
6030 Call the system call getsid().");
6033 posix_getsid(PyObject
*self
, PyObject
*args
)
6037 if (!PyArg_ParseTuple(args
, "i:getsid", &pid
))
6041 return posix_error();
6042 return PyInt_FromLong((long)sid
);
6044 #endif /* HAVE_GETSID */
6048 PyDoc_STRVAR(posix_setsid__doc__
,
6050 Call the system call setsid().");
6053 posix_setsid(PyObject
*self
, PyObject
*noargs
)
6056 return posix_error();
6060 #endif /* HAVE_SETSID */
6063 PyDoc_STRVAR(posix_setpgid__doc__
,
6064 "setpgid(pid, pgrp)\n\n\
6065 Call the system call setpgid().");
6068 posix_setpgid(PyObject
*self
, PyObject
*args
)
6072 if (!PyArg_ParseTuple(args
, "ii:setpgid", &pid
, &pgrp
))
6074 if (setpgid(pid
, pgrp
) < 0)
6075 return posix_error();
6079 #endif /* HAVE_SETPGID */
6082 #ifdef HAVE_TCGETPGRP
6083 PyDoc_STRVAR(posix_tcgetpgrp__doc__
,
6084 "tcgetpgrp(fd) -> pgid\n\n\
6085 Return the process group associated with the terminal given by a fd.");
6088 posix_tcgetpgrp(PyObject
*self
, PyObject
*args
)
6092 if (!PyArg_ParseTuple(args
, "i:tcgetpgrp", &fd
))
6094 pgid
= tcgetpgrp(fd
);
6096 return posix_error();
6097 return PyInt_FromLong((long)pgid
);
6099 #endif /* HAVE_TCGETPGRP */
6102 #ifdef HAVE_TCSETPGRP
6103 PyDoc_STRVAR(posix_tcsetpgrp__doc__
,
6104 "tcsetpgrp(fd, pgid)\n\n\
6105 Set the process group associated with the terminal given by a fd.");
6108 posix_tcsetpgrp(PyObject
*self
, PyObject
*args
)
6111 if (!PyArg_ParseTuple(args
, "ii:tcsetpgrp", &fd
, &pgid
))
6113 if (tcsetpgrp(fd
, pgid
) < 0)
6114 return posix_error();
6118 #endif /* HAVE_TCSETPGRP */
6120 /* Functions acting on file descriptors */
6122 PyDoc_STRVAR(posix_open__doc__
,
6123 "open(filename, flag [, mode=0777]) -> fd\n\n\
6124 Open a file (for low level IO).");
6127 posix_open(PyObject
*self
, PyObject
*args
)
6135 if (unicode_file_names()) {
6136 PyUnicodeObject
*po
;
6137 if (PyArg_ParseTuple(args
, "Ui|i:mkdir", &po
, &flag
, &mode
)) {
6138 Py_BEGIN_ALLOW_THREADS
6139 /* PyUnicode_AS_UNICODE OK without thread
6140 lock as it is a simple dereference. */
6141 fd
= _wopen(PyUnicode_AS_UNICODE(po
), flag
, mode
);
6142 Py_END_ALLOW_THREADS
6144 return posix_error();
6145 return PyInt_FromLong((long)fd
);
6147 /* Drop the argument parsing error as narrow strings
6153 if (!PyArg_ParseTuple(args
, "eti|i",
6154 Py_FileSystemDefaultEncoding
, &file
,
6158 Py_BEGIN_ALLOW_THREADS
6159 fd
= open(file
, flag
, mode
);
6160 Py_END_ALLOW_THREADS
6162 return posix_error_with_allocated_filename(file
);
6164 return PyInt_FromLong((long)fd
);
6168 PyDoc_STRVAR(posix_close__doc__
,
6170 Close a file descriptor (for low level IO).");
6173 posix_close(PyObject
*self
, PyObject
*args
)
6176 if (!PyArg_ParseTuple(args
, "i:close", &fd
))
6178 Py_BEGIN_ALLOW_THREADS
6180 Py_END_ALLOW_THREADS
6182 return posix_error();
6188 PyDoc_STRVAR(posix_closerange__doc__
,
6189 "closerange(fd_low, fd_high)\n\n\
6190 Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
6193 posix_closerange(PyObject
*self
, PyObject
*args
)
6195 int fd_from
, fd_to
, i
;
6196 if (!PyArg_ParseTuple(args
, "ii:closerange", &fd_from
, &fd_to
))
6198 Py_BEGIN_ALLOW_THREADS
6199 for (i
= fd_from
; i
< fd_to
; i
++)
6201 Py_END_ALLOW_THREADS
6206 PyDoc_STRVAR(posix_dup__doc__
,
6207 "dup(fd) -> fd2\n\n\
6208 Return a duplicate of a file descriptor.");
6211 posix_dup(PyObject
*self
, PyObject
*args
)
6214 if (!PyArg_ParseTuple(args
, "i:dup", &fd
))
6216 Py_BEGIN_ALLOW_THREADS
6218 Py_END_ALLOW_THREADS
6220 return posix_error();
6221 return PyInt_FromLong((long)fd
);
6225 PyDoc_STRVAR(posix_dup2__doc__
,
6226 "dup2(old_fd, new_fd)\n\n\
6227 Duplicate file descriptor.");
6230 posix_dup2(PyObject
*self
, PyObject
*args
)
6233 if (!PyArg_ParseTuple(args
, "ii:dup2", &fd
, &fd2
))
6235 Py_BEGIN_ALLOW_THREADS
6236 res
= dup2(fd
, fd2
);
6237 Py_END_ALLOW_THREADS
6239 return posix_error();
6245 PyDoc_STRVAR(posix_lseek__doc__
,
6246 "lseek(fd, pos, how) -> newpos\n\n\
6247 Set the current position of a file descriptor.");
6250 posix_lseek(PyObject
*self
, PyObject
*args
)
6253 #if defined(MS_WIN64) || defined(MS_WINDOWS)
6254 PY_LONG_LONG pos
, res
;
6259 if (!PyArg_ParseTuple(args
, "iOi:lseek", &fd
, &posobj
, &how
))
6262 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
6264 case 0: how
= SEEK_SET
; break;
6265 case 1: how
= SEEK_CUR
; break;
6266 case 2: how
= SEEK_END
; break;
6268 #endif /* SEEK_END */
6270 #if !defined(HAVE_LARGEFILE_SUPPORT)
6271 pos
= PyInt_AsLong(posobj
);
6273 pos
= PyLong_Check(posobj
) ?
6274 PyLong_AsLongLong(posobj
) : PyInt_AsLong(posobj
);
6276 if (PyErr_Occurred())
6279 Py_BEGIN_ALLOW_THREADS
6280 #if defined(MS_WIN64) || defined(MS_WINDOWS)
6281 res
= _lseeki64(fd
, pos
, how
);
6283 res
= lseek(fd
, pos
, how
);
6285 Py_END_ALLOW_THREADS
6287 return posix_error();
6289 #if !defined(HAVE_LARGEFILE_SUPPORT)
6290 return PyInt_FromLong(res
);
6292 return PyLong_FromLongLong(res
);
6297 PyDoc_STRVAR(posix_read__doc__
,
6298 "read(fd, buffersize) -> string\n\n\
6299 Read a file descriptor.");
6302 posix_read(PyObject
*self
, PyObject
*args
)
6306 if (!PyArg_ParseTuple(args
, "ii:read", &fd
, &size
))
6310 return posix_error();
6312 buffer
= PyString_FromStringAndSize((char *)NULL
, size
);
6315 Py_BEGIN_ALLOW_THREADS
6316 n
= read(fd
, PyString_AsString(buffer
), size
);
6317 Py_END_ALLOW_THREADS
6320 return posix_error();
6323 _PyString_Resize(&buffer
, n
);
6328 PyDoc_STRVAR(posix_write__doc__
,
6329 "write(fd, string) -> byteswritten\n\n\
6330 Write a string to a file descriptor.");
6333 posix_write(PyObject
*self
, PyObject
*args
)
6339 if (!PyArg_ParseTuple(args
, "is*:write", &fd
, &pbuf
))
6341 Py_BEGIN_ALLOW_THREADS
6342 size
= write(fd
, pbuf
.buf
, (size_t)pbuf
.len
);
6343 Py_END_ALLOW_THREADS
6344 PyBuffer_Release(&pbuf
);
6346 return posix_error();
6347 return PyInt_FromSsize_t(size
);
6351 PyDoc_STRVAR(posix_fstat__doc__
,
6352 "fstat(fd) -> stat result\n\n\
6353 Like stat(), but for an open file descriptor.");
6356 posix_fstat(PyObject
*self
, PyObject
*args
)
6361 if (!PyArg_ParseTuple(args
, "i:fstat", &fd
))
6364 /* on OpenVMS we must ensure that all bytes are written to the file */
6367 Py_BEGIN_ALLOW_THREADS
6368 res
= FSTAT(fd
, &st
);
6369 Py_END_ALLOW_THREADS
6372 return win32_error("fstat", NULL
);
6374 return posix_error();
6378 return _pystat_fromstructstat(&st
);
6382 PyDoc_STRVAR(posix_fdopen__doc__
,
6383 "fdopen(fd [, mode='r' [, bufsize]]) -> file_object\n\n\
6384 Return an open file object connected to a file descriptor.");
6387 posix_fdopen(PyObject
*self
, PyObject
*args
)
6390 char *orgmode
= "r";
6395 if (!PyArg_ParseTuple(args
, "i|si", &fd
, &orgmode
, &bufsize
))
6398 /* Sanitize mode. See fileobject.c */
6399 mode
= PyMem_MALLOC(strlen(orgmode
)+3);
6404 strcpy(mode
, orgmode
);
6405 if (_PyFile_SanitizeMode(mode
)) {
6409 Py_BEGIN_ALLOW_THREADS
6410 #if !defined(MS_WINDOWS) && defined(HAVE_FCNTL_H)
6411 if (mode
[0] == 'a') {
6412 /* try to make sure the O_APPEND flag is set */
6414 flags
= fcntl(fd
, F_GETFL
);
6416 fcntl(fd
, F_SETFL
, flags
| O_APPEND
);
6417 fp
= fdopen(fd
, mode
);
6418 if (fp
== NULL
&& flags
!= -1)
6419 /* restore old mode if fdopen failed */
6420 fcntl(fd
, F_SETFL
, flags
);
6422 fp
= fdopen(fd
, mode
);
6425 fp
= fdopen(fd
, mode
);
6427 Py_END_ALLOW_THREADS
6430 return posix_error();
6431 f
= PyFile_FromFile(fp
, "<fdopen>", orgmode
, fclose
);
6433 PyFile_SetBufSize(f
, bufsize
);
6437 PyDoc_STRVAR(posix_isatty__doc__
,
6438 "isatty(fd) -> bool\n\n\
6439 Return True if the file descriptor 'fd' is an open file descriptor\n\
6440 connected to the slave end of a terminal.");
6443 posix_isatty(PyObject
*self
, PyObject
*args
)
6446 if (!PyArg_ParseTuple(args
, "i:isatty", &fd
))
6448 return PyBool_FromLong(isatty(fd
));
6452 PyDoc_STRVAR(posix_pipe__doc__
,
6453 "pipe() -> (read_end, write_end)\n\n\
6457 posix_pipe(PyObject
*self
, PyObject
*noargs
)
6459 #if defined(PYOS_OS2)
6463 Py_BEGIN_ALLOW_THREADS
6464 rc
= DosCreatePipe( &read
, &write
, 4096);
6465 Py_END_ALLOW_THREADS
6467 return os2_error(rc
);
6469 return Py_BuildValue("(ii)", read
, write
);
6471 #if !defined(MS_WINDOWS)
6474 Py_BEGIN_ALLOW_THREADS
6476 Py_END_ALLOW_THREADS
6478 return posix_error();
6479 return Py_BuildValue("(ii)", fds
[0], fds
[1]);
6480 #else /* MS_WINDOWS */
6482 int read_fd
, write_fd
;
6484 Py_BEGIN_ALLOW_THREADS
6485 ok
= CreatePipe(&read
, &write
, NULL
, 0);
6486 Py_END_ALLOW_THREADS
6488 return win32_error("CreatePipe", NULL
);
6489 read_fd
= _open_osfhandle((Py_intptr_t
)read
, 0);
6490 write_fd
= _open_osfhandle((Py_intptr_t
)write
, 1);
6491 return Py_BuildValue("(ii)", read_fd
, write_fd
);
6492 #endif /* MS_WINDOWS */
6495 #endif /* HAVE_PIPE */
6499 PyDoc_STRVAR(posix_mkfifo__doc__
,
6500 "mkfifo(filename [, mode=0666])\n\n\
6501 Create a FIFO (a POSIX named pipe).");
6504 posix_mkfifo(PyObject
*self
, PyObject
*args
)
6509 if (!PyArg_ParseTuple(args
, "s|i:mkfifo", &filename
, &mode
))
6511 Py_BEGIN_ALLOW_THREADS
6512 res
= mkfifo(filename
, mode
);
6513 Py_END_ALLOW_THREADS
6515 return posix_error();
6522 #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
6523 PyDoc_STRVAR(posix_mknod__doc__
,
6524 "mknod(filename [, mode=0600, device])\n\n\
6525 Create a filesystem node (file, device special file or named pipe)\n\
6526 named filename. mode specifies both the permissions to use and the\n\
6527 type of node to be created, being combined (bitwise OR) with one of\n\
6528 S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
6529 device defines the newly created device special file (probably using\n\
6530 os.makedev()), otherwise it is ignored.");
6534 posix_mknod(PyObject
*self
, PyObject
*args
)
6540 if (!PyArg_ParseTuple(args
, "s|ii:mknod", &filename
, &mode
, &device
))
6542 Py_BEGIN_ALLOW_THREADS
6543 res
= mknod(filename
, mode
, device
);
6544 Py_END_ALLOW_THREADS
6546 return posix_error();
6552 #ifdef HAVE_DEVICE_MACROS
6553 PyDoc_STRVAR(posix_major__doc__
,
6554 "major(device) -> major number\n\
6555 Extracts a device major number from a raw device number.");
6558 posix_major(PyObject
*self
, PyObject
*args
)
6561 if (!PyArg_ParseTuple(args
, "i:major", &device
))
6563 return PyInt_FromLong((long)major(device
));
6566 PyDoc_STRVAR(posix_minor__doc__
,
6567 "minor(device) -> minor number\n\
6568 Extracts a device minor number from a raw device number.");
6571 posix_minor(PyObject
*self
, PyObject
*args
)
6574 if (!PyArg_ParseTuple(args
, "i:minor", &device
))
6576 return PyInt_FromLong((long)minor(device
));
6579 PyDoc_STRVAR(posix_makedev__doc__
,
6580 "makedev(major, minor) -> device number\n\
6581 Composes a raw device number from the major and minor device numbers.");
6584 posix_makedev(PyObject
*self
, PyObject
*args
)
6587 if (!PyArg_ParseTuple(args
, "ii:makedev", &major
, &minor
))
6589 return PyInt_FromLong((long)makedev(major
, minor
));
6591 #endif /* device macros */
6594 #ifdef HAVE_FTRUNCATE
6595 PyDoc_STRVAR(posix_ftruncate__doc__
,
6596 "ftruncate(fd, length)\n\n\
6597 Truncate a file to a specified length.");
6600 posix_ftruncate(PyObject
*self
, PyObject
*args
)
6607 if (!PyArg_ParseTuple(args
, "iO:ftruncate", &fd
, &lenobj
))
6610 #if !defined(HAVE_LARGEFILE_SUPPORT)
6611 length
= PyInt_AsLong(lenobj
);
6613 length
= PyLong_Check(lenobj
) ?
6614 PyLong_AsLongLong(lenobj
) : PyInt_AsLong(lenobj
);
6616 if (PyErr_Occurred())
6619 Py_BEGIN_ALLOW_THREADS
6620 res
= ftruncate(fd
, length
);
6621 Py_END_ALLOW_THREADS
6623 PyErr_SetFromErrno(PyExc_IOError
);
6632 PyDoc_STRVAR(posix_putenv__doc__
,
6633 "putenv(key, value)\n\n\
6634 Change or add an environment variable.");
6636 /* Save putenv() parameters as values here, so we can collect them when they
6637 * get re-set with another call for the same key. */
6638 static PyObject
*posix_putenv_garbage
;
6641 posix_putenv(PyObject
*self
, PyObject
*args
)
6648 if (!PyArg_ParseTuple(args
, "ss:putenv", &s1
, &s2
))
6651 #if defined(PYOS_OS2)
6652 if (stricmp(s1
, "BEGINLIBPATH") == 0) {
6655 rc
= DosSetExtLIBPATH(s2
, BEGIN_LIBPATH
);
6657 return os2_error(rc
);
6659 } else if (stricmp(s1
, "ENDLIBPATH") == 0) {
6662 rc
= DosSetExtLIBPATH(s2
, END_LIBPATH
);
6664 return os2_error(rc
);
6668 /* XXX This can leak memory -- not easy to fix :-( */
6669 len
= strlen(s1
) + strlen(s2
) + 2;
6670 /* len includes space for a trailing \0; the size arg to
6671 PyString_FromStringAndSize does not count that */
6672 newstr
= PyString_FromStringAndSize(NULL
, (int)len
- 1);
6674 return PyErr_NoMemory();
6675 newenv
= PyString_AS_STRING(newstr
);
6676 PyOS_snprintf(newenv
, len
, "%s=%s", s1
, s2
);
6677 if (putenv(newenv
)) {
6682 /* Install the first arg and newstr in posix_putenv_garbage;
6683 * this will cause previous value to be collected. This has to
6684 * happen after the real putenv() call because the old value
6685 * was still accessible until then. */
6686 if (PyDict_SetItem(posix_putenv_garbage
,
6687 PyTuple_GET_ITEM(args
, 0), newstr
)) {
6688 /* really not much we can do; just leak */
6695 #if defined(PYOS_OS2)
6703 #ifdef HAVE_UNSETENV
6704 PyDoc_STRVAR(posix_unsetenv__doc__
,
6706 Delete an environment variable.");
6709 posix_unsetenv(PyObject
*self
, PyObject
*args
)
6713 if (!PyArg_ParseTuple(args
, "s:unsetenv", &s1
))
6718 /* Remove the key from posix_putenv_garbage;
6719 * this will cause it to be collected. This has to
6720 * happen after the real unsetenv() call because the
6721 * old value was still accessible until then.
6723 if (PyDict_DelItem(posix_putenv_garbage
,
6724 PyTuple_GET_ITEM(args
, 0))) {
6725 /* really not much we can do; just leak */
6732 #endif /* unsetenv */
6734 PyDoc_STRVAR(posix_strerror__doc__
,
6735 "strerror(code) -> string\n\n\
6736 Translate an error code to a message string.");
6739 posix_strerror(PyObject
*self
, PyObject
*args
)
6743 if (!PyArg_ParseTuple(args
, "i:strerror", &code
))
6745 message
= strerror(code
);
6746 if (message
== NULL
) {
6747 PyErr_SetString(PyExc_ValueError
,
6748 "strerror() argument out of range");
6751 return PyString_FromString(message
);
6755 #ifdef HAVE_SYS_WAIT_H
6758 PyDoc_STRVAR(posix_WCOREDUMP__doc__
,
6759 "WCOREDUMP(status) -> bool\n\n\
6760 Return True if the process returning 'status' was dumped to a core file.");
6763 posix_WCOREDUMP(PyObject
*self
, PyObject
*args
)
6766 WAIT_STATUS_INT(status
) = 0;
6768 if (!PyArg_ParseTuple(args
, "i:WCOREDUMP", &WAIT_STATUS_INT(status
)))
6771 return PyBool_FromLong(WCOREDUMP(status
));
6773 #endif /* WCOREDUMP */
6776 PyDoc_STRVAR(posix_WIFCONTINUED__doc__
,
6777 "WIFCONTINUED(status) -> bool\n\n\
6778 Return True if the process returning 'status' was continued from a\n\
6779 job control stop.");
6782 posix_WIFCONTINUED(PyObject
*self
, PyObject
*args
)
6785 WAIT_STATUS_INT(status
) = 0;
6787 if (!PyArg_ParseTuple(args
, "i:WCONTINUED", &WAIT_STATUS_INT(status
)))
6790 return PyBool_FromLong(WIFCONTINUED(status
));
6792 #endif /* WIFCONTINUED */
6795 PyDoc_STRVAR(posix_WIFSTOPPED__doc__
,
6796 "WIFSTOPPED(status) -> bool\n\n\
6797 Return True if the process returning 'status' was stopped.");
6800 posix_WIFSTOPPED(PyObject
*self
, PyObject
*args
)
6803 WAIT_STATUS_INT(status
) = 0;
6805 if (!PyArg_ParseTuple(args
, "i:WIFSTOPPED", &WAIT_STATUS_INT(status
)))
6808 return PyBool_FromLong(WIFSTOPPED(status
));
6810 #endif /* WIFSTOPPED */
6813 PyDoc_STRVAR(posix_WIFSIGNALED__doc__
,
6814 "WIFSIGNALED(status) -> bool\n\n\
6815 Return True if the process returning 'status' was terminated by a signal.");
6818 posix_WIFSIGNALED(PyObject
*self
, PyObject
*args
)
6821 WAIT_STATUS_INT(status
) = 0;
6823 if (!PyArg_ParseTuple(args
, "i:WIFSIGNALED", &WAIT_STATUS_INT(status
)))
6826 return PyBool_FromLong(WIFSIGNALED(status
));
6828 #endif /* WIFSIGNALED */
6831 PyDoc_STRVAR(posix_WIFEXITED__doc__
,
6832 "WIFEXITED(status) -> bool\n\n\
6833 Return true if the process returning 'status' exited using the exit()\n\
6837 posix_WIFEXITED(PyObject
*self
, PyObject
*args
)
6840 WAIT_STATUS_INT(status
) = 0;
6842 if (!PyArg_ParseTuple(args
, "i:WIFEXITED", &WAIT_STATUS_INT(status
)))
6845 return PyBool_FromLong(WIFEXITED(status
));
6847 #endif /* WIFEXITED */
6850 PyDoc_STRVAR(posix_WEXITSTATUS__doc__
,
6851 "WEXITSTATUS(status) -> integer\n\n\
6852 Return the process return code from 'status'.");
6855 posix_WEXITSTATUS(PyObject
*self
, PyObject
*args
)
6858 WAIT_STATUS_INT(status
) = 0;
6860 if (!PyArg_ParseTuple(args
, "i:WEXITSTATUS", &WAIT_STATUS_INT(status
)))
6863 return Py_BuildValue("i", WEXITSTATUS(status
));
6865 #endif /* WEXITSTATUS */
6868 PyDoc_STRVAR(posix_WTERMSIG__doc__
,
6869 "WTERMSIG(status) -> integer\n\n\
6870 Return the signal that terminated the process that provided the 'status'\n\
6874 posix_WTERMSIG(PyObject
*self
, PyObject
*args
)
6877 WAIT_STATUS_INT(status
) = 0;
6879 if (!PyArg_ParseTuple(args
, "i:WTERMSIG", &WAIT_STATUS_INT(status
)))
6882 return Py_BuildValue("i", WTERMSIG(status
));
6884 #endif /* WTERMSIG */
6887 PyDoc_STRVAR(posix_WSTOPSIG__doc__
,
6888 "WSTOPSIG(status) -> integer\n\n\
6889 Return the signal that stopped the process that provided\n\
6890 the 'status' value.");
6893 posix_WSTOPSIG(PyObject
*self
, PyObject
*args
)
6896 WAIT_STATUS_INT(status
) = 0;
6898 if (!PyArg_ParseTuple(args
, "i:WSTOPSIG", &WAIT_STATUS_INT(status
)))
6901 return Py_BuildValue("i", WSTOPSIG(status
));
6903 #endif /* WSTOPSIG */
6905 #endif /* HAVE_SYS_WAIT_H */
6908 #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
6910 /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
6911 needed definitions in sys/statvfs.h */
6914 #include <sys/statvfs.h>
6917 _pystatvfs_fromstructstatvfs(struct statvfs st
) {
6918 PyObject
*v
= PyStructSequence_New(&StatVFSResultType
);
6922 #if !defined(HAVE_LARGEFILE_SUPPORT)
6923 PyStructSequence_SET_ITEM(v
, 0, PyInt_FromLong((long) st
.f_bsize
));
6924 PyStructSequence_SET_ITEM(v
, 1, PyInt_FromLong((long) st
.f_frsize
));
6925 PyStructSequence_SET_ITEM(v
, 2, PyInt_FromLong((long) st
.f_blocks
));
6926 PyStructSequence_SET_ITEM(v
, 3, PyInt_FromLong((long) st
.f_bfree
));
6927 PyStructSequence_SET_ITEM(v
, 4, PyInt_FromLong((long) st
.f_bavail
));
6928 PyStructSequence_SET_ITEM(v
, 5, PyInt_FromLong((long) st
.f_files
));
6929 PyStructSequence_SET_ITEM(v
, 6, PyInt_FromLong((long) st
.f_ffree
));
6930 PyStructSequence_SET_ITEM(v
, 7, PyInt_FromLong((long) st
.f_favail
));
6931 PyStructSequence_SET_ITEM(v
, 8, PyInt_FromLong((long) st
.f_flag
));
6932 PyStructSequence_SET_ITEM(v
, 9, PyInt_FromLong((long) st
.f_namemax
));
6934 PyStructSequence_SET_ITEM(v
, 0, PyInt_FromLong((long) st
.f_bsize
));
6935 PyStructSequence_SET_ITEM(v
, 1, PyInt_FromLong((long) st
.f_frsize
));
6936 PyStructSequence_SET_ITEM(v
, 2,
6937 PyLong_FromLongLong((PY_LONG_LONG
) st
.f_blocks
));
6938 PyStructSequence_SET_ITEM(v
, 3,
6939 PyLong_FromLongLong((PY_LONG_LONG
) st
.f_bfree
));
6940 PyStructSequence_SET_ITEM(v
, 4,
6941 PyLong_FromLongLong((PY_LONG_LONG
) st
.f_bavail
));
6942 PyStructSequence_SET_ITEM(v
, 5,
6943 PyLong_FromLongLong((PY_LONG_LONG
) st
.f_files
));
6944 PyStructSequence_SET_ITEM(v
, 6,
6945 PyLong_FromLongLong((PY_LONG_LONG
) st
.f_ffree
));
6946 PyStructSequence_SET_ITEM(v
, 7,
6947 PyLong_FromLongLong((PY_LONG_LONG
) st
.f_favail
));
6948 PyStructSequence_SET_ITEM(v
, 8, PyInt_FromLong((long) st
.f_flag
));
6949 PyStructSequence_SET_ITEM(v
, 9, PyInt_FromLong((long) st
.f_namemax
));
6955 PyDoc_STRVAR(posix_fstatvfs__doc__
,
6956 "fstatvfs(fd) -> statvfs result\n\n\
6957 Perform an fstatvfs system call on the given fd.");
6960 posix_fstatvfs(PyObject
*self
, PyObject
*args
)
6965 if (!PyArg_ParseTuple(args
, "i:fstatvfs", &fd
))
6967 Py_BEGIN_ALLOW_THREADS
6968 res
= fstatvfs(fd
, &st
);
6969 Py_END_ALLOW_THREADS
6971 return posix_error();
6973 return _pystatvfs_fromstructstatvfs(st
);
6975 #endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
6978 #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
6979 #include <sys/statvfs.h>
6981 PyDoc_STRVAR(posix_statvfs__doc__
,
6982 "statvfs(path) -> statvfs result\n\n\
6983 Perform a statvfs system call on the given path.");
6986 posix_statvfs(PyObject
*self
, PyObject
*args
)
6991 if (!PyArg_ParseTuple(args
, "s:statvfs", &path
))
6993 Py_BEGIN_ALLOW_THREADS
6994 res
= statvfs(path
, &st
);
6995 Py_END_ALLOW_THREADS
6997 return posix_error_with_filename(path
);
6999 return _pystatvfs_fromstructstatvfs(st
);
7001 #endif /* HAVE_STATVFS */
7005 PyDoc_STRVAR(posix_tempnam__doc__
,
7006 "tempnam([dir[, prefix]]) -> string\n\n\
7007 Return a unique name for a temporary file.\n\
7008 The directory and a prefix may be specified as strings; they may be omitted\n\
7009 or None if not needed.");
7012 posix_tempnam(PyObject
*self
, PyObject
*args
)
7014 PyObject
*result
= NULL
;
7019 if (!PyArg_ParseTuple(args
, "|zz:tempnam", &dir
, &pfx
))
7022 if (PyErr_Warn(PyExc_RuntimeWarning
,
7023 "tempnam is a potential security risk to your program") < 0)
7027 name
= _tempnam(dir
, pfx
);
7029 name
= tempnam(dir
, pfx
);
7032 return PyErr_NoMemory();
7033 result
= PyString_FromString(name
);
7041 PyDoc_STRVAR(posix_tmpfile__doc__
,
7042 "tmpfile() -> file object\n\n\
7043 Create a temporary file with no directory entries.");
7046 posix_tmpfile(PyObject
*self
, PyObject
*noargs
)
7052 return posix_error();
7053 return PyFile_FromFile(fp
, "<tmpfile>", "w+b", fclose
);
7059 PyDoc_STRVAR(posix_tmpnam__doc__
,
7060 "tmpnam() -> string\n\n\
7061 Return a unique name for a temporary file.");
7064 posix_tmpnam(PyObject
*self
, PyObject
*noargs
)
7066 char buffer
[L_tmpnam
];
7069 if (PyErr_Warn(PyExc_RuntimeWarning
,
7070 "tmpnam is a potential security risk to your program") < 0)
7074 name
= tmpnam_r(buffer
);
7076 name
= tmpnam(buffer
);
7079 PyObject
*err
= Py_BuildValue("is", 0,
7081 "unexpected NULL from tmpnam_r"
7083 "unexpected NULL from tmpnam"
7086 PyErr_SetObject(PyExc_OSError
, err
);
7090 return PyString_FromString(buffer
);
7095 /* This is used for fpathconf(), pathconf(), confstr() and sysconf().
7096 * It maps strings representing configuration variable names to
7097 * integer values, allowing those functions to be called with the
7098 * magic names instead of polluting the module's namespace with tons of
7099 * rarely-used constants. There are three separate tables that use
7100 * these definitions.
7102 * This code is always included, even if none of the interfaces that
7103 * need it are included. The #if hackery needed to avoid it would be
7104 * sufficiently pervasive that it's not worth the loss of readability.
7112 conv_confname(PyObject
*arg
, int *valuep
, struct constdef
*table
,
7115 if (PyInt_Check(arg
)) {
7116 *valuep
= PyInt_AS_LONG(arg
);
7119 if (PyString_Check(arg
)) {
7120 /* look up the value in the table using a binary search */
7123 size_t hi
= tablesize
;
7125 char *confname
= PyString_AS_STRING(arg
);
7127 mid
= (lo
+ hi
) / 2;
7128 cmp
= strcmp(confname
, table
[mid
].name
);
7134 *valuep
= table
[mid
].value
;
7138 PyErr_SetString(PyExc_ValueError
, "unrecognized configuration name");
7141 PyErr_SetString(PyExc_TypeError
,
7142 "configuration names must be strings or integers");
7147 #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
7148 static struct constdef posix_constants_pathconf
[] = {
7149 #ifdef _PC_ABI_AIO_XFER_MAX
7150 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX
},
7152 #ifdef _PC_ABI_ASYNC_IO
7153 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO
},
7156 {"PC_ASYNC_IO", _PC_ASYNC_IO
},
7158 #ifdef _PC_CHOWN_RESTRICTED
7159 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED
},
7161 #ifdef _PC_FILESIZEBITS
7162 {"PC_FILESIZEBITS", _PC_FILESIZEBITS
},
7165 {"PC_LAST", _PC_LAST
},
7168 {"PC_LINK_MAX", _PC_LINK_MAX
},
7170 #ifdef _PC_MAX_CANON
7171 {"PC_MAX_CANON", _PC_MAX_CANON
},
7173 #ifdef _PC_MAX_INPUT
7174 {"PC_MAX_INPUT", _PC_MAX_INPUT
},
7177 {"PC_NAME_MAX", _PC_NAME_MAX
},
7180 {"PC_NO_TRUNC", _PC_NO_TRUNC
},
7183 {"PC_PATH_MAX", _PC_PATH_MAX
},
7186 {"PC_PIPE_BUF", _PC_PIPE_BUF
},
7189 {"PC_PRIO_IO", _PC_PRIO_IO
},
7191 #ifdef _PC_SOCK_MAXBUF
7192 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF
},
7195 {"PC_SYNC_IO", _PC_SYNC_IO
},
7198 {"PC_VDISABLE", _PC_VDISABLE
},
7203 conv_path_confname(PyObject
*arg
, int *valuep
)
7205 return conv_confname(arg
, valuep
, posix_constants_pathconf
,
7206 sizeof(posix_constants_pathconf
)
7207 / sizeof(struct constdef
));
7211 #ifdef HAVE_FPATHCONF
7212 PyDoc_STRVAR(posix_fpathconf__doc__
,
7213 "fpathconf(fd, name) -> integer\n\n\
7214 Return the configuration limit name for the file descriptor fd.\n\
7215 If there is no limit, return -1.");
7218 posix_fpathconf(PyObject
*self
, PyObject
*args
)
7220 PyObject
*result
= NULL
;
7223 if (PyArg_ParseTuple(args
, "iO&:fpathconf", &fd
,
7224 conv_path_confname
, &name
)) {
7228 limit
= fpathconf(fd
, name
);
7229 if (limit
== -1 && errno
!= 0)
7232 result
= PyInt_FromLong(limit
);
7239 #ifdef HAVE_PATHCONF
7240 PyDoc_STRVAR(posix_pathconf__doc__
,
7241 "pathconf(path, name) -> integer\n\n\
7242 Return the configuration limit name for the file or directory path.\n\
7243 If there is no limit, return -1.");
7246 posix_pathconf(PyObject
*self
, PyObject
*args
)
7248 PyObject
*result
= NULL
;
7252 if (PyArg_ParseTuple(args
, "sO&:pathconf", &path
,
7253 conv_path_confname
, &name
)) {
7257 limit
= pathconf(path
, name
);
7258 if (limit
== -1 && errno
!= 0) {
7259 if (errno
== EINVAL
)
7260 /* could be a path or name problem */
7263 posix_error_with_filename(path
);
7266 result
= PyInt_FromLong(limit
);
7273 static struct constdef posix_constants_confstr
[] = {
7274 #ifdef _CS_ARCHITECTURE
7275 {"CS_ARCHITECTURE", _CS_ARCHITECTURE
},
7278 {"CS_HOSTNAME", _CS_HOSTNAME
},
7280 #ifdef _CS_HW_PROVIDER
7281 {"CS_HW_PROVIDER", _CS_HW_PROVIDER
},
7283 #ifdef _CS_HW_SERIAL
7284 {"CS_HW_SERIAL", _CS_HW_SERIAL
},
7286 #ifdef _CS_INITTAB_NAME
7287 {"CS_INITTAB_NAME", _CS_INITTAB_NAME
},
7289 #ifdef _CS_LFS64_CFLAGS
7290 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS
},
7292 #ifdef _CS_LFS64_LDFLAGS
7293 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS
},
7295 #ifdef _CS_LFS64_LIBS
7296 {"CS_LFS64_LIBS", _CS_LFS64_LIBS
},
7298 #ifdef _CS_LFS64_LINTFLAGS
7299 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS
},
7301 #ifdef _CS_LFS_CFLAGS
7302 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS
},
7304 #ifdef _CS_LFS_LDFLAGS
7305 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS
},
7308 {"CS_LFS_LIBS", _CS_LFS_LIBS
},
7310 #ifdef _CS_LFS_LINTFLAGS
7311 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS
},
7314 {"CS_MACHINE", _CS_MACHINE
},
7317 {"CS_PATH", _CS_PATH
},
7320 {"CS_RELEASE", _CS_RELEASE
},
7322 #ifdef _CS_SRPC_DOMAIN
7323 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN
},
7326 {"CS_SYSNAME", _CS_SYSNAME
},
7329 {"CS_VERSION", _CS_VERSION
},
7331 #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
7332 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS
},
7334 #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
7335 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS
},
7337 #ifdef _CS_XBS5_ILP32_OFF32_LIBS
7338 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS
},
7340 #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
7341 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS
},
7343 #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
7344 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS
},
7346 #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
7347 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS
},
7349 #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
7350 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS
},
7352 #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
7353 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
},
7355 #ifdef _CS_XBS5_LP64_OFF64_CFLAGS
7356 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS
},
7358 #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
7359 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS
},
7361 #ifdef _CS_XBS5_LP64_OFF64_LIBS
7362 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS
},
7364 #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
7365 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS
},
7367 #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
7368 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS
},
7370 #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
7371 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
},
7373 #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
7374 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS
},
7376 #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
7377 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
},
7379 #ifdef _MIPS_CS_AVAIL_PROCESSORS
7380 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS
},
7382 #ifdef _MIPS_CS_BASE
7383 {"MIPS_CS_BASE", _MIPS_CS_BASE
},
7385 #ifdef _MIPS_CS_HOSTID
7386 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID
},
7388 #ifdef _MIPS_CS_HW_NAME
7389 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME
},
7391 #ifdef _MIPS_CS_NUM_PROCESSORS
7392 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS
},
7394 #ifdef _MIPS_CS_OSREL_MAJ
7395 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ
},
7397 #ifdef _MIPS_CS_OSREL_MIN
7398 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN
},
7400 #ifdef _MIPS_CS_OSREL_PATCH
7401 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH
},
7403 #ifdef _MIPS_CS_OS_NAME
7404 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME
},
7406 #ifdef _MIPS_CS_OS_PROVIDER
7407 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER
},
7409 #ifdef _MIPS_CS_PROCESSORS
7410 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS
},
7412 #ifdef _MIPS_CS_SERIAL
7413 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL
},
7415 #ifdef _MIPS_CS_VENDOR
7416 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR
},
7421 conv_confstr_confname(PyObject
*arg
, int *valuep
)
7423 return conv_confname(arg
, valuep
, posix_constants_confstr
,
7424 sizeof(posix_constants_confstr
)
7425 / sizeof(struct constdef
));
7428 PyDoc_STRVAR(posix_confstr__doc__
,
7429 "confstr(name) -> string\n\n\
7430 Return a string-valued system configuration variable.");
7433 posix_confstr(PyObject
*self
, PyObject
*args
)
7435 PyObject
*result
= NULL
;
7439 if (PyArg_ParseTuple(args
, "O&:confstr", conv_confstr_confname
, &name
)) {
7443 len
= confstr(name
, buffer
, sizeof(buffer
));
7454 if ((unsigned int)len
>= sizeof(buffer
)) {
7455 result
= PyString_FromStringAndSize(NULL
, len
-1);
7457 confstr(name
, PyString_AS_STRING(result
), len
);
7460 result
= PyString_FromStringAndSize(buffer
, len
-1);
7469 static struct constdef posix_constants_sysconf
[] = {
7470 #ifdef _SC_2_CHAR_TERM
7471 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM
},
7474 {"SC_2_C_BIND", _SC_2_C_BIND
},
7477 {"SC_2_C_DEV", _SC_2_C_DEV
},
7479 #ifdef _SC_2_C_VERSION
7480 {"SC_2_C_VERSION", _SC_2_C_VERSION
},
7482 #ifdef _SC_2_FORT_DEV
7483 {"SC_2_FORT_DEV", _SC_2_FORT_DEV
},
7485 #ifdef _SC_2_FORT_RUN
7486 {"SC_2_FORT_RUN", _SC_2_FORT_RUN
},
7488 #ifdef _SC_2_LOCALEDEF
7489 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF
},
7492 {"SC_2_SW_DEV", _SC_2_SW_DEV
},
7495 {"SC_2_UPE", _SC_2_UPE
},
7497 #ifdef _SC_2_VERSION
7498 {"SC_2_VERSION", _SC_2_VERSION
},
7500 #ifdef _SC_ABI_ASYNCHRONOUS_IO
7501 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO
},
7504 {"SC_ACL", _SC_ACL
},
7506 #ifdef _SC_AIO_LISTIO_MAX
7507 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX
},
7510 {"SC_AIO_MAX", _SC_AIO_MAX
},
7512 #ifdef _SC_AIO_PRIO_DELTA_MAX
7513 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX
},
7516 {"SC_ARG_MAX", _SC_ARG_MAX
},
7518 #ifdef _SC_ASYNCHRONOUS_IO
7519 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO
},
7521 #ifdef _SC_ATEXIT_MAX
7522 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX
},
7525 {"SC_AUDIT", _SC_AUDIT
},
7527 #ifdef _SC_AVPHYS_PAGES
7528 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES
},
7530 #ifdef _SC_BC_BASE_MAX
7531 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX
},
7533 #ifdef _SC_BC_DIM_MAX
7534 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX
},
7536 #ifdef _SC_BC_SCALE_MAX
7537 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX
},
7539 #ifdef _SC_BC_STRING_MAX
7540 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX
},
7543 {"SC_CAP", _SC_CAP
},
7545 #ifdef _SC_CHARCLASS_NAME_MAX
7546 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX
},
7549 {"SC_CHAR_BIT", _SC_CHAR_BIT
},
7552 {"SC_CHAR_MAX", _SC_CHAR_MAX
},
7555 {"SC_CHAR_MIN", _SC_CHAR_MIN
},
7557 #ifdef _SC_CHILD_MAX
7558 {"SC_CHILD_MAX", _SC_CHILD_MAX
},
7561 {"SC_CLK_TCK", _SC_CLK_TCK
},
7563 #ifdef _SC_COHER_BLKSZ
7564 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ
},
7566 #ifdef _SC_COLL_WEIGHTS_MAX
7567 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX
},
7569 #ifdef _SC_DCACHE_ASSOC
7570 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC
},
7572 #ifdef _SC_DCACHE_BLKSZ
7573 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ
},
7575 #ifdef _SC_DCACHE_LINESZ
7576 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ
},
7578 #ifdef _SC_DCACHE_SZ
7579 {"SC_DCACHE_SZ", _SC_DCACHE_SZ
},
7581 #ifdef _SC_DCACHE_TBLKSZ
7582 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ
},
7584 #ifdef _SC_DELAYTIMER_MAX
7585 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX
},
7587 #ifdef _SC_EQUIV_CLASS_MAX
7588 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX
},
7590 #ifdef _SC_EXPR_NEST_MAX
7591 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX
},
7594 {"SC_FSYNC", _SC_FSYNC
},
7596 #ifdef _SC_GETGR_R_SIZE_MAX
7597 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX
},
7599 #ifdef _SC_GETPW_R_SIZE_MAX
7600 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX
},
7602 #ifdef _SC_ICACHE_ASSOC
7603 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC
},
7605 #ifdef _SC_ICACHE_BLKSZ
7606 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ
},
7608 #ifdef _SC_ICACHE_LINESZ
7609 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ
},
7611 #ifdef _SC_ICACHE_SZ
7612 {"SC_ICACHE_SZ", _SC_ICACHE_SZ
},
7615 {"SC_INF", _SC_INF
},
7618 {"SC_INT_MAX", _SC_INT_MAX
},
7621 {"SC_INT_MIN", _SC_INT_MIN
},
7624 {"SC_IOV_MAX", _SC_IOV_MAX
},
7626 #ifdef _SC_IP_SECOPTS
7627 {"SC_IP_SECOPTS", _SC_IP_SECOPTS
},
7629 #ifdef _SC_JOB_CONTROL
7630 {"SC_JOB_CONTROL", _SC_JOB_CONTROL
},
7632 #ifdef _SC_KERN_POINTERS
7633 {"SC_KERN_POINTERS", _SC_KERN_POINTERS
},
7636 {"SC_KERN_SIM", _SC_KERN_SIM
},
7639 {"SC_LINE_MAX", _SC_LINE_MAX
},
7641 #ifdef _SC_LOGIN_NAME_MAX
7642 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX
},
7644 #ifdef _SC_LOGNAME_MAX
7645 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX
},
7648 {"SC_LONG_BIT", _SC_LONG_BIT
},
7651 {"SC_MAC", _SC_MAC
},
7653 #ifdef _SC_MAPPED_FILES
7654 {"SC_MAPPED_FILES", _SC_MAPPED_FILES
},
7657 {"SC_MAXPID", _SC_MAXPID
},
7659 #ifdef _SC_MB_LEN_MAX
7660 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX
},
7663 {"SC_MEMLOCK", _SC_MEMLOCK
},
7665 #ifdef _SC_MEMLOCK_RANGE
7666 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE
},
7668 #ifdef _SC_MEMORY_PROTECTION
7669 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION
},
7671 #ifdef _SC_MESSAGE_PASSING
7672 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING
},
7674 #ifdef _SC_MMAP_FIXED_ALIGNMENT
7675 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT
},
7677 #ifdef _SC_MQ_OPEN_MAX
7678 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX
},
7680 #ifdef _SC_MQ_PRIO_MAX
7681 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX
},
7683 #ifdef _SC_NACLS_MAX
7684 {"SC_NACLS_MAX", _SC_NACLS_MAX
},
7686 #ifdef _SC_NGROUPS_MAX
7687 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX
},
7689 #ifdef _SC_NL_ARGMAX
7690 {"SC_NL_ARGMAX", _SC_NL_ARGMAX
},
7692 #ifdef _SC_NL_LANGMAX
7693 {"SC_NL_LANGMAX", _SC_NL_LANGMAX
},
7695 #ifdef _SC_NL_MSGMAX
7696 {"SC_NL_MSGMAX", _SC_NL_MSGMAX
},
7699 {"SC_NL_NMAX", _SC_NL_NMAX
},
7701 #ifdef _SC_NL_SETMAX
7702 {"SC_NL_SETMAX", _SC_NL_SETMAX
},
7704 #ifdef _SC_NL_TEXTMAX
7705 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX
},
7707 #ifdef _SC_NPROCESSORS_CONF
7708 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF
},
7710 #ifdef _SC_NPROCESSORS_ONLN
7711 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN
},
7713 #ifdef _SC_NPROC_CONF
7714 {"SC_NPROC_CONF", _SC_NPROC_CONF
},
7716 #ifdef _SC_NPROC_ONLN
7717 {"SC_NPROC_ONLN", _SC_NPROC_ONLN
},
7720 {"SC_NZERO", _SC_NZERO
},
7723 {"SC_OPEN_MAX", _SC_OPEN_MAX
},
7726 {"SC_PAGESIZE", _SC_PAGESIZE
},
7728 #ifdef _SC_PAGE_SIZE
7729 {"SC_PAGE_SIZE", _SC_PAGE_SIZE
},
7732 {"SC_PASS_MAX", _SC_PASS_MAX
},
7734 #ifdef _SC_PHYS_PAGES
7735 {"SC_PHYS_PAGES", _SC_PHYS_PAGES
},
7738 {"SC_PII", _SC_PII
},
7740 #ifdef _SC_PII_INTERNET
7741 {"SC_PII_INTERNET", _SC_PII_INTERNET
},
7743 #ifdef _SC_PII_INTERNET_DGRAM
7744 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM
},
7746 #ifdef _SC_PII_INTERNET_STREAM
7747 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM
},
7750 {"SC_PII_OSI", _SC_PII_OSI
},
7752 #ifdef _SC_PII_OSI_CLTS
7753 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS
},
7755 #ifdef _SC_PII_OSI_COTS
7756 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS
},
7758 #ifdef _SC_PII_OSI_M
7759 {"SC_PII_OSI_M", _SC_PII_OSI_M
},
7761 #ifdef _SC_PII_SOCKET
7762 {"SC_PII_SOCKET", _SC_PII_SOCKET
},
7765 {"SC_PII_XTI", _SC_PII_XTI
},
7768 {"SC_POLL", _SC_POLL
},
7770 #ifdef _SC_PRIORITIZED_IO
7771 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO
},
7773 #ifdef _SC_PRIORITY_SCHEDULING
7774 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING
},
7776 #ifdef _SC_REALTIME_SIGNALS
7777 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS
},
7779 #ifdef _SC_RE_DUP_MAX
7780 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX
},
7782 #ifdef _SC_RTSIG_MAX
7783 {"SC_RTSIG_MAX", _SC_RTSIG_MAX
},
7785 #ifdef _SC_SAVED_IDS
7786 {"SC_SAVED_IDS", _SC_SAVED_IDS
},
7788 #ifdef _SC_SCHAR_MAX
7789 {"SC_SCHAR_MAX", _SC_SCHAR_MAX
},
7791 #ifdef _SC_SCHAR_MIN
7792 {"SC_SCHAR_MIN", _SC_SCHAR_MIN
},
7795 {"SC_SELECT", _SC_SELECT
},
7797 #ifdef _SC_SEMAPHORES
7798 {"SC_SEMAPHORES", _SC_SEMAPHORES
},
7800 #ifdef _SC_SEM_NSEMS_MAX
7801 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX
},
7803 #ifdef _SC_SEM_VALUE_MAX
7804 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX
},
7806 #ifdef _SC_SHARED_MEMORY_OBJECTS
7807 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS
},
7810 {"SC_SHRT_MAX", _SC_SHRT_MAX
},
7813 {"SC_SHRT_MIN", _SC_SHRT_MIN
},
7815 #ifdef _SC_SIGQUEUE_MAX
7816 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX
},
7818 #ifdef _SC_SIGRT_MAX
7819 {"SC_SIGRT_MAX", _SC_SIGRT_MAX
},
7821 #ifdef _SC_SIGRT_MIN
7822 {"SC_SIGRT_MIN", _SC_SIGRT_MIN
},
7824 #ifdef _SC_SOFTPOWER
7825 {"SC_SOFTPOWER", _SC_SOFTPOWER
},
7827 #ifdef _SC_SPLIT_CACHE
7828 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE
},
7830 #ifdef _SC_SSIZE_MAX
7831 {"SC_SSIZE_MAX", _SC_SSIZE_MAX
},
7833 #ifdef _SC_STACK_PROT
7834 {"SC_STACK_PROT", _SC_STACK_PROT
},
7836 #ifdef _SC_STREAM_MAX
7837 {"SC_STREAM_MAX", _SC_STREAM_MAX
},
7839 #ifdef _SC_SYNCHRONIZED_IO
7840 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO
},
7843 {"SC_THREADS", _SC_THREADS
},
7845 #ifdef _SC_THREAD_ATTR_STACKADDR
7846 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR
},
7848 #ifdef _SC_THREAD_ATTR_STACKSIZE
7849 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE
},
7851 #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
7852 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS
},
7854 #ifdef _SC_THREAD_KEYS_MAX
7855 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX
},
7857 #ifdef _SC_THREAD_PRIORITY_SCHEDULING
7858 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING
},
7860 #ifdef _SC_THREAD_PRIO_INHERIT
7861 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT
},
7863 #ifdef _SC_THREAD_PRIO_PROTECT
7864 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT
},
7866 #ifdef _SC_THREAD_PROCESS_SHARED
7867 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED
},
7869 #ifdef _SC_THREAD_SAFE_FUNCTIONS
7870 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS
},
7872 #ifdef _SC_THREAD_STACK_MIN
7873 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN
},
7875 #ifdef _SC_THREAD_THREADS_MAX
7876 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX
},
7879 {"SC_TIMERS", _SC_TIMERS
},
7881 #ifdef _SC_TIMER_MAX
7882 {"SC_TIMER_MAX", _SC_TIMER_MAX
},
7884 #ifdef _SC_TTY_NAME_MAX
7885 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX
},
7887 #ifdef _SC_TZNAME_MAX
7888 {"SC_TZNAME_MAX", _SC_TZNAME_MAX
},
7890 #ifdef _SC_T_IOV_MAX
7891 {"SC_T_IOV_MAX", _SC_T_IOV_MAX
},
7893 #ifdef _SC_UCHAR_MAX
7894 {"SC_UCHAR_MAX", _SC_UCHAR_MAX
},
7897 {"SC_UINT_MAX", _SC_UINT_MAX
},
7899 #ifdef _SC_UIO_MAXIOV
7900 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV
},
7902 #ifdef _SC_ULONG_MAX
7903 {"SC_ULONG_MAX", _SC_ULONG_MAX
},
7905 #ifdef _SC_USHRT_MAX
7906 {"SC_USHRT_MAX", _SC_USHRT_MAX
},
7909 {"SC_VERSION", _SC_VERSION
},
7912 {"SC_WORD_BIT", _SC_WORD_BIT
},
7914 #ifdef _SC_XBS5_ILP32_OFF32
7915 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32
},
7917 #ifdef _SC_XBS5_ILP32_OFFBIG
7918 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG
},
7920 #ifdef _SC_XBS5_LP64_OFF64
7921 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64
},
7923 #ifdef _SC_XBS5_LPBIG_OFFBIG
7924 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG
},
7926 #ifdef _SC_XOPEN_CRYPT
7927 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT
},
7929 #ifdef _SC_XOPEN_ENH_I18N
7930 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N
},
7932 #ifdef _SC_XOPEN_LEGACY
7933 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY
},
7935 #ifdef _SC_XOPEN_REALTIME
7936 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME
},
7938 #ifdef _SC_XOPEN_REALTIME_THREADS
7939 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS
},
7941 #ifdef _SC_XOPEN_SHM
7942 {"SC_XOPEN_SHM", _SC_XOPEN_SHM
},
7944 #ifdef _SC_XOPEN_UNIX
7945 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX
},
7947 #ifdef _SC_XOPEN_VERSION
7948 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION
},
7950 #ifdef _SC_XOPEN_XCU_VERSION
7951 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION
},
7953 #ifdef _SC_XOPEN_XPG2
7954 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2
},
7956 #ifdef _SC_XOPEN_XPG3
7957 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3
},
7959 #ifdef _SC_XOPEN_XPG4
7960 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4
},
7965 conv_sysconf_confname(PyObject
*arg
, int *valuep
)
7967 return conv_confname(arg
, valuep
, posix_constants_sysconf
,
7968 sizeof(posix_constants_sysconf
)
7969 / sizeof(struct constdef
));
7972 PyDoc_STRVAR(posix_sysconf__doc__
,
7973 "sysconf(name) -> integer\n\n\
7974 Return an integer-valued system configuration variable.");
7977 posix_sysconf(PyObject
*self
, PyObject
*args
)
7979 PyObject
*result
= NULL
;
7982 if (PyArg_ParseTuple(args
, "O&:sysconf", conv_sysconf_confname
, &name
)) {
7986 value
= sysconf(name
);
7987 if (value
== -1 && errno
!= 0)
7990 result
= PyInt_FromLong(value
);
7997 /* This code is used to ensure that the tables of configuration value names
7998 * are in sorted order as required by conv_confname(), and also to build the
7999 * the exported dictionaries that are used to publish information about the
8000 * names available on the host platform.
8002 * Sorting the table at runtime ensures that the table is properly ordered
8003 * when used, even for platforms we're not able to test on. It also makes
8004 * it easier to add additional entries to the tables.
8008 cmp_constdefs(const void *v1
, const void *v2
)
8010 const struct constdef
*c1
=
8011 (const struct constdef
*) v1
;
8012 const struct constdef
*c2
=
8013 (const struct constdef
*) v2
;
8015 return strcmp(c1
->name
, c2
->name
);
8019 setup_confname_table(struct constdef
*table
, size_t tablesize
,
8020 char *tablename
, PyObject
*module
)
8025 qsort(table
, tablesize
, sizeof(struct constdef
), cmp_constdefs
);
8030 for (i
=0; i
< tablesize
; ++i
) {
8031 PyObject
*o
= PyInt_FromLong(table
[i
].value
);
8032 if (o
== NULL
|| PyDict_SetItemString(d
, table
[i
].name
, o
) == -1) {
8039 return PyModule_AddObject(module
, tablename
, d
);
8042 /* Return -1 on failure, 0 on success. */
8044 setup_confname_tables(PyObject
*module
)
8046 #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
8047 if (setup_confname_table(posix_constants_pathconf
,
8048 sizeof(posix_constants_pathconf
)
8049 / sizeof(struct constdef
),
8050 "pathconf_names", module
))
8054 if (setup_confname_table(posix_constants_confstr
,
8055 sizeof(posix_constants_confstr
)
8056 / sizeof(struct constdef
),
8057 "confstr_names", module
))
8061 if (setup_confname_table(posix_constants_sysconf
,
8062 sizeof(posix_constants_sysconf
)
8063 / sizeof(struct constdef
),
8064 "sysconf_names", module
))
8071 PyDoc_STRVAR(posix_abort__doc__
,
8072 "abort() -> does not return!\n\n\
8073 Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
8074 in the hardest way possible on the hosting operating system.");
8077 posix_abort(PyObject
*self
, PyObject
*noargs
)
8081 Py_FatalError("abort() called from Python code didn't abort!");
8086 PyDoc_STRVAR(win32_startfile__doc__
,
8087 "startfile(filepath [, operation]) - Start a file with its associated\n\
8090 When \"operation\" is not specified or \"open\", this acts like\n\
8091 double-clicking the file in Explorer, or giving the file name as an\n\
8092 argument to the DOS \"start\" command: the file is opened with whatever\n\
8093 application (if any) its extension is associated.\n\
8094 When another \"operation\" is given, it specifies what should be done with\n\
8095 the file. A typical operation is \"print\".\n\
8097 startfile returns as soon as the associated application is launched.\n\
8098 There is no option to wait for the application to close, and no way\n\
8099 to retrieve the application's exit status.\n\
8101 The filepath is relative to the current directory. If you want to use\n\
8102 an absolute path, make sure the first character is not a slash (\"/\");\n\
8103 the underlying Win32 ShellExecute function doesn't work if it is.");
8106 win32_startfile(PyObject
*self
, PyObject
*args
)
8109 char *operation
= NULL
;
8111 #ifdef Py_WIN_WIDE_FILENAMES
8112 if (unicode_file_names()) {
8113 PyObject
*unipath
, *woperation
= NULL
;
8114 if (!PyArg_ParseTuple(args
, "U|s:startfile",
8115 &unipath
, &operation
)) {
8122 woperation
= PyUnicode_DecodeASCII(operation
,
8123 strlen(operation
), NULL
);
8131 Py_BEGIN_ALLOW_THREADS
8132 rc
= ShellExecuteW((HWND
)0, woperation
? PyUnicode_AS_UNICODE(woperation
) : 0,
8133 PyUnicode_AS_UNICODE(unipath
),
8134 NULL
, NULL
, SW_SHOWNORMAL
);
8135 Py_END_ALLOW_THREADS
8137 Py_XDECREF(woperation
);
8138 if (rc
<= (HINSTANCE
)32) {
8139 PyObject
*errval
= win32_error_unicode("startfile",
8140 PyUnicode_AS_UNICODE(unipath
));
8149 if (!PyArg_ParseTuple(args
, "et|s:startfile",
8150 Py_FileSystemDefaultEncoding
, &filepath
,
8153 Py_BEGIN_ALLOW_THREADS
8154 rc
= ShellExecute((HWND
)0, operation
, filepath
,
8155 NULL
, NULL
, SW_SHOWNORMAL
);
8156 Py_END_ALLOW_THREADS
8157 if (rc
<= (HINSTANCE
)32) {
8158 PyObject
*errval
= win32_error("startfile", filepath
);
8159 PyMem_Free(filepath
);
8162 PyMem_Free(filepath
);
8168 #ifdef HAVE_GETLOADAVG
8169 PyDoc_STRVAR(posix_getloadavg__doc__
,
8170 "getloadavg() -> (float, float, float)\n\n\
8171 Return the number of processes in the system run queue averaged over\n\
8172 the last 1, 5, and 15 minutes or raises OSError if the load average\n\
8176 posix_getloadavg(PyObject
*self
, PyObject
*noargs
)
8179 if (getloadavg(loadavg
, 3)!=3) {
8180 PyErr_SetString(PyExc_OSError
, "Load averages are unobtainable");
8183 return Py_BuildValue("ddd", loadavg
[0], loadavg
[1], loadavg
[2]);
8189 PyDoc_STRVAR(win32_urandom__doc__
,
8190 "urandom(n) -> str\n\n\
8191 Return a string of n random bytes suitable for cryptographic use.");
8193 typedef BOOL (WINAPI
*CRYPTACQUIRECONTEXTA
)(HCRYPTPROV
*phProv
,\
8194 LPCSTR pszContainer
, LPCSTR pszProvider
, DWORD dwProvType
,\
8196 typedef BOOL (WINAPI
*CRYPTGENRANDOM
)(HCRYPTPROV hProv
, DWORD dwLen
,\
8199 static CRYPTGENRANDOM pCryptGenRandom
= NULL
;
8200 /* This handle is never explicitly released. Instead, the operating
8201 system will release it when the process terminates. */
8202 static HCRYPTPROV hCryptProv
= 0;
8205 win32_urandom(PyObject
*self
, PyObject
*args
)
8210 /* Read arguments */
8211 if (! PyArg_ParseTuple(args
, "i:urandom", &howMany
))
8214 return PyErr_Format(PyExc_ValueError
,
8215 "negative argument not allowed");
8217 if (hCryptProv
== 0) {
8218 HINSTANCE hAdvAPI32
= NULL
;
8219 CRYPTACQUIRECONTEXTA pCryptAcquireContext
= NULL
;
8221 /* Obtain handle to the DLL containing CryptoAPI
8222 This should not fail */
8223 hAdvAPI32
= GetModuleHandle("advapi32.dll");
8224 if(hAdvAPI32
== NULL
)
8225 return win32_error("GetModuleHandle", NULL
);
8227 /* Obtain pointers to the CryptoAPI functions
8228 This will fail on some early versions of Win95 */
8229 pCryptAcquireContext
= (CRYPTACQUIRECONTEXTA
)GetProcAddress(
8231 "CryptAcquireContextA");
8232 if (pCryptAcquireContext
== NULL
)
8233 return PyErr_Format(PyExc_NotImplementedError
,
8234 "CryptAcquireContextA not found");
8236 pCryptGenRandom
= (CRYPTGENRANDOM
)GetProcAddress(
8237 hAdvAPI32
, "CryptGenRandom");
8238 if (pCryptGenRandom
== NULL
)
8239 return PyErr_Format(PyExc_NotImplementedError
,
8240 "CryptGenRandom not found");
8242 /* Acquire context */
8243 if (! pCryptAcquireContext(&hCryptProv
, NULL
, NULL
,
8244 PROV_RSA_FULL
, CRYPT_VERIFYCONTEXT
))
8245 return win32_error("CryptAcquireContext", NULL
);
8248 /* Allocate bytes */
8249 result
= PyString_FromStringAndSize(NULL
, howMany
);
8250 if (result
!= NULL
) {
8251 /* Get random data */
8252 memset(PyString_AS_STRING(result
), 0, howMany
); /* zero seed */
8253 if (! pCryptGenRandom(hCryptProv
, howMany
, (unsigned char*)
8254 PyString_AS_STRING(result
))) {
8256 return win32_error("CryptGenRandom", NULL
);
8264 /* Use openssl random routine */
8265 #include <openssl/rand.h>
8266 PyDoc_STRVAR(vms_urandom__doc__
,
8267 "urandom(n) -> str\n\n\
8268 Return a string of n random bytes suitable for cryptographic use.");
8271 vms_urandom(PyObject
*self
, PyObject
*args
)
8276 /* Read arguments */
8277 if (! PyArg_ParseTuple(args
, "i:urandom", &howMany
))
8280 return PyErr_Format(PyExc_ValueError
,
8281 "negative argument not allowed");
8283 /* Allocate bytes */
8284 result
= PyString_FromStringAndSize(NULL
, howMany
);
8285 if (result
!= NULL
) {
8286 /* Get random data */
8287 if (RAND_pseudo_bytes((unsigned char*)
8288 PyString_AS_STRING(result
),
8291 return PyErr_Format(PyExc_ValueError
,
8292 "RAND_pseudo_bytes");
8299 static PyMethodDef posix_methods
[] = {
8300 {"access", posix_access
, METH_VARARGS
, posix_access__doc__
},
8302 {"ttyname", posix_ttyname
, METH_VARARGS
, posix_ttyname__doc__
},
8304 {"chdir", posix_chdir
, METH_VARARGS
, posix_chdir__doc__
},
8306 {"chflags", posix_chflags
, METH_VARARGS
, posix_chflags__doc__
},
8307 #endif /* HAVE_CHFLAGS */
8308 {"chmod", posix_chmod
, METH_VARARGS
, posix_chmod__doc__
},
8310 {"fchmod", posix_fchmod
, METH_VARARGS
, posix_fchmod__doc__
},
8311 #endif /* HAVE_FCHMOD */
8313 {"chown", posix_chown
, METH_VARARGS
, posix_chown__doc__
},
8314 #endif /* HAVE_CHOWN */
8316 {"lchmod", posix_lchmod
, METH_VARARGS
, posix_lchmod__doc__
},
8317 #endif /* HAVE_LCHMOD */
8319 {"fchown", posix_fchown
, METH_VARARGS
, posix_fchown__doc__
},
8320 #endif /* HAVE_FCHOWN */
8321 #ifdef HAVE_LCHFLAGS
8322 {"lchflags", posix_lchflags
, METH_VARARGS
, posix_lchflags__doc__
},
8323 #endif /* HAVE_LCHFLAGS */
8325 {"lchown", posix_lchown
, METH_VARARGS
, posix_lchown__doc__
},
8326 #endif /* HAVE_LCHOWN */
8328 {"chroot", posix_chroot
, METH_VARARGS
, posix_chroot__doc__
},
8331 {"ctermid", posix_ctermid
, METH_NOARGS
, posix_ctermid__doc__
},
8334 {"getcwd", posix_getcwd
, METH_NOARGS
, posix_getcwd__doc__
},
8335 #ifdef Py_USING_UNICODE
8336 {"getcwdu", posix_getcwdu
, METH_NOARGS
, posix_getcwdu__doc__
},
8340 {"link", posix_link
, METH_VARARGS
, posix_link__doc__
},
8341 #endif /* HAVE_LINK */
8342 {"listdir", posix_listdir
, METH_VARARGS
, posix_listdir__doc__
},
8343 {"lstat", posix_lstat
, METH_VARARGS
, posix_lstat__doc__
},
8344 {"mkdir", posix_mkdir
, METH_VARARGS
, posix_mkdir__doc__
},
8346 {"nice", posix_nice
, METH_VARARGS
, posix_nice__doc__
},
8347 #endif /* HAVE_NICE */
8348 #ifdef HAVE_READLINK
8349 {"readlink", posix_readlink
, METH_VARARGS
, posix_readlink__doc__
},
8350 #endif /* HAVE_READLINK */
8351 {"rename", posix_rename
, METH_VARARGS
, posix_rename__doc__
},
8352 {"rmdir", posix_rmdir
, METH_VARARGS
, posix_rmdir__doc__
},
8353 {"stat", posix_stat
, METH_VARARGS
, posix_stat__doc__
},
8354 {"stat_float_times", stat_float_times
, METH_VARARGS
, stat_float_times__doc__
},
8356 {"symlink", posix_symlink
, METH_VARARGS
, posix_symlink__doc__
},
8357 #endif /* HAVE_SYMLINK */
8359 {"system", posix_system
, METH_VARARGS
, posix_system__doc__
},
8361 {"umask", posix_umask
, METH_VARARGS
, posix_umask__doc__
},
8363 {"uname", posix_uname
, METH_NOARGS
, posix_uname__doc__
},
8364 #endif /* HAVE_UNAME */
8365 {"unlink", posix_unlink
, METH_VARARGS
, posix_unlink__doc__
},
8366 {"remove", posix_unlink
, METH_VARARGS
, posix_remove__doc__
},
8367 {"utime", posix_utime
, METH_VARARGS
, posix_utime__doc__
},
8369 {"times", posix_times
, METH_NOARGS
, posix_times__doc__
},
8370 #endif /* HAVE_TIMES */
8371 {"_exit", posix__exit
, METH_VARARGS
, posix__exit__doc__
},
8373 {"execv", posix_execv
, METH_VARARGS
, posix_execv__doc__
},
8374 {"execve", posix_execve
, METH_VARARGS
, posix_execve__doc__
},
8375 #endif /* HAVE_EXECV */
8377 {"spawnv", posix_spawnv
, METH_VARARGS
, posix_spawnv__doc__
},
8378 {"spawnve", posix_spawnve
, METH_VARARGS
, posix_spawnve__doc__
},
8379 #if defined(PYOS_OS2)
8380 {"spawnvp", posix_spawnvp
, METH_VARARGS
, posix_spawnvp__doc__
},
8381 {"spawnvpe", posix_spawnvpe
, METH_VARARGS
, posix_spawnvpe__doc__
},
8382 #endif /* PYOS_OS2 */
8383 #endif /* HAVE_SPAWNV */
8385 {"fork1", posix_fork1
, METH_NOARGS
, posix_fork1__doc__
},
8386 #endif /* HAVE_FORK1 */
8388 {"fork", posix_fork
, METH_NOARGS
, posix_fork__doc__
},
8389 #endif /* HAVE_FORK */
8390 #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
8391 {"openpty", posix_openpty
, METH_NOARGS
, posix_openpty__doc__
},
8392 #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
8394 {"forkpty", posix_forkpty
, METH_NOARGS
, posix_forkpty__doc__
},
8395 #endif /* HAVE_FORKPTY */
8397 {"getegid", posix_getegid
, METH_NOARGS
, posix_getegid__doc__
},
8398 #endif /* HAVE_GETEGID */
8400 {"geteuid", posix_geteuid
, METH_NOARGS
, posix_geteuid__doc__
},
8401 #endif /* HAVE_GETEUID */
8403 {"getgid", posix_getgid
, METH_NOARGS
, posix_getgid__doc__
},
8404 #endif /* HAVE_GETGID */
8405 #ifdef HAVE_GETGROUPS
8406 {"getgroups", posix_getgroups
, METH_NOARGS
, posix_getgroups__doc__
},
8408 {"getpid", posix_getpid
, METH_NOARGS
, posix_getpid__doc__
},
8410 {"getpgrp", posix_getpgrp
, METH_NOARGS
, posix_getpgrp__doc__
},
8411 #endif /* HAVE_GETPGRP */
8413 {"getppid", posix_getppid
, METH_NOARGS
, posix_getppid__doc__
},
8414 #endif /* HAVE_GETPPID */
8416 {"getuid", posix_getuid
, METH_NOARGS
, posix_getuid__doc__
},
8417 #endif /* HAVE_GETUID */
8418 #ifdef HAVE_GETLOGIN
8419 {"getlogin", posix_getlogin
, METH_NOARGS
, posix_getlogin__doc__
},
8422 {"kill", posix_kill
, METH_VARARGS
, posix_kill__doc__
},
8423 #endif /* HAVE_KILL */
8425 {"killpg", posix_killpg
, METH_VARARGS
, posix_killpg__doc__
},
8426 #endif /* HAVE_KILLPG */
8428 {"plock", posix_plock
, METH_VARARGS
, posix_plock__doc__
},
8429 #endif /* HAVE_PLOCK */
8431 {"popen", posix_popen
, METH_VARARGS
, posix_popen__doc__
},
8433 {"popen2", win32_popen2
, METH_VARARGS
},
8434 {"popen3", win32_popen3
, METH_VARARGS
},
8435 {"popen4", win32_popen4
, METH_VARARGS
},
8436 {"startfile", win32_startfile
, METH_VARARGS
, win32_startfile__doc__
},
8438 #if defined(PYOS_OS2) && defined(PYCC_GCC)
8439 {"popen2", os2emx_popen2
, METH_VARARGS
},
8440 {"popen3", os2emx_popen3
, METH_VARARGS
},
8441 {"popen4", os2emx_popen4
, METH_VARARGS
},
8444 #endif /* HAVE_POPEN */
8446 {"setuid", posix_setuid
, METH_VARARGS
, posix_setuid__doc__
},
8447 #endif /* HAVE_SETUID */
8449 {"seteuid", posix_seteuid
, METH_VARARGS
, posix_seteuid__doc__
},
8450 #endif /* HAVE_SETEUID */
8452 {"setegid", posix_setegid
, METH_VARARGS
, posix_setegid__doc__
},
8453 #endif /* HAVE_SETEGID */
8454 #ifdef HAVE_SETREUID
8455 {"setreuid", posix_setreuid
, METH_VARARGS
, posix_setreuid__doc__
},
8456 #endif /* HAVE_SETREUID */
8457 #ifdef HAVE_SETREGID
8458 {"setregid", posix_setregid
, METH_VARARGS
, posix_setregid__doc__
},
8459 #endif /* HAVE_SETREGID */
8461 {"setgid", posix_setgid
, METH_VARARGS
, posix_setgid__doc__
},
8462 #endif /* HAVE_SETGID */
8463 #ifdef HAVE_SETGROUPS
8464 {"setgroups", posix_setgroups
, METH_O
, posix_setgroups__doc__
},
8465 #endif /* HAVE_SETGROUPS */
8467 {"getpgid", posix_getpgid
, METH_VARARGS
, posix_getpgid__doc__
},
8468 #endif /* HAVE_GETPGID */
8470 {"setpgrp", posix_setpgrp
, METH_NOARGS
, posix_setpgrp__doc__
},
8471 #endif /* HAVE_SETPGRP */
8473 {"wait", posix_wait
, METH_NOARGS
, posix_wait__doc__
},
8474 #endif /* HAVE_WAIT */
8476 {"wait3", posix_wait3
, METH_VARARGS
, posix_wait3__doc__
},
8477 #endif /* HAVE_WAIT3 */
8479 {"wait4", posix_wait4
, METH_VARARGS
, posix_wait4__doc__
},
8480 #endif /* HAVE_WAIT4 */
8481 #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
8482 {"waitpid", posix_waitpid
, METH_VARARGS
, posix_waitpid__doc__
},
8483 #endif /* HAVE_WAITPID */
8485 {"getsid", posix_getsid
, METH_VARARGS
, posix_getsid__doc__
},
8486 #endif /* HAVE_GETSID */
8488 {"setsid", posix_setsid
, METH_NOARGS
, posix_setsid__doc__
},
8489 #endif /* HAVE_SETSID */
8491 {"setpgid", posix_setpgid
, METH_VARARGS
, posix_setpgid__doc__
},
8492 #endif /* HAVE_SETPGID */
8493 #ifdef HAVE_TCGETPGRP
8494 {"tcgetpgrp", posix_tcgetpgrp
, METH_VARARGS
, posix_tcgetpgrp__doc__
},
8495 #endif /* HAVE_TCGETPGRP */
8496 #ifdef HAVE_TCSETPGRP
8497 {"tcsetpgrp", posix_tcsetpgrp
, METH_VARARGS
, posix_tcsetpgrp__doc__
},
8498 #endif /* HAVE_TCSETPGRP */
8499 {"open", posix_open
, METH_VARARGS
, posix_open__doc__
},
8500 {"close", posix_close
, METH_VARARGS
, posix_close__doc__
},
8501 {"closerange", posix_closerange
, METH_VARARGS
, posix_closerange__doc__
},
8502 {"dup", posix_dup
, METH_VARARGS
, posix_dup__doc__
},
8503 {"dup2", posix_dup2
, METH_VARARGS
, posix_dup2__doc__
},
8504 {"lseek", posix_lseek
, METH_VARARGS
, posix_lseek__doc__
},
8505 {"read", posix_read
, METH_VARARGS
, posix_read__doc__
},
8506 {"write", posix_write
, METH_VARARGS
, posix_write__doc__
},
8507 {"fstat", posix_fstat
, METH_VARARGS
, posix_fstat__doc__
},
8508 {"fdopen", posix_fdopen
, METH_VARARGS
, posix_fdopen__doc__
},
8509 {"isatty", posix_isatty
, METH_VARARGS
, posix_isatty__doc__
},
8511 {"pipe", posix_pipe
, METH_NOARGS
, posix_pipe__doc__
},
8514 {"mkfifo", posix_mkfifo
, METH_VARARGS
, posix_mkfifo__doc__
},
8516 #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
8517 {"mknod", posix_mknod
, METH_VARARGS
, posix_mknod__doc__
},
8519 #ifdef HAVE_DEVICE_MACROS
8520 {"major", posix_major
, METH_VARARGS
, posix_major__doc__
},
8521 {"minor", posix_minor
, METH_VARARGS
, posix_minor__doc__
},
8522 {"makedev", posix_makedev
, METH_VARARGS
, posix_makedev__doc__
},
8524 #ifdef HAVE_FTRUNCATE
8525 {"ftruncate", posix_ftruncate
, METH_VARARGS
, posix_ftruncate__doc__
},
8528 {"putenv", posix_putenv
, METH_VARARGS
, posix_putenv__doc__
},
8530 #ifdef HAVE_UNSETENV
8531 {"unsetenv", posix_unsetenv
, METH_VARARGS
, posix_unsetenv__doc__
},
8533 {"strerror", posix_strerror
, METH_VARARGS
, posix_strerror__doc__
},
8535 {"fchdir", posix_fchdir
, METH_O
, posix_fchdir__doc__
},
8538 {"fsync", posix_fsync
, METH_O
, posix_fsync__doc__
},
8540 #ifdef HAVE_FDATASYNC
8541 {"fdatasync", posix_fdatasync
, METH_O
, posix_fdatasync__doc__
},
8543 #ifdef HAVE_SYS_WAIT_H
8545 {"WCOREDUMP", posix_WCOREDUMP
, METH_VARARGS
, posix_WCOREDUMP__doc__
},
8546 #endif /* WCOREDUMP */
8548 {"WIFCONTINUED",posix_WIFCONTINUED
, METH_VARARGS
, posix_WIFCONTINUED__doc__
},
8549 #endif /* WIFCONTINUED */
8551 {"WIFSTOPPED", posix_WIFSTOPPED
, METH_VARARGS
, posix_WIFSTOPPED__doc__
},
8552 #endif /* WIFSTOPPED */
8554 {"WIFSIGNALED", posix_WIFSIGNALED
, METH_VARARGS
, posix_WIFSIGNALED__doc__
},
8555 #endif /* WIFSIGNALED */
8557 {"WIFEXITED", posix_WIFEXITED
, METH_VARARGS
, posix_WIFEXITED__doc__
},
8558 #endif /* WIFEXITED */
8560 {"WEXITSTATUS", posix_WEXITSTATUS
, METH_VARARGS
, posix_WEXITSTATUS__doc__
},
8561 #endif /* WEXITSTATUS */
8563 {"WTERMSIG", posix_WTERMSIG
, METH_VARARGS
, posix_WTERMSIG__doc__
},
8564 #endif /* WTERMSIG */
8566 {"WSTOPSIG", posix_WSTOPSIG
, METH_VARARGS
, posix_WSTOPSIG__doc__
},
8567 #endif /* WSTOPSIG */
8568 #endif /* HAVE_SYS_WAIT_H */
8569 #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
8570 {"fstatvfs", posix_fstatvfs
, METH_VARARGS
, posix_fstatvfs__doc__
},
8572 #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
8573 {"statvfs", posix_statvfs
, METH_VARARGS
, posix_statvfs__doc__
},
8576 {"tmpfile", posix_tmpfile
, METH_NOARGS
, posix_tmpfile__doc__
},
8579 {"tempnam", posix_tempnam
, METH_VARARGS
, posix_tempnam__doc__
},
8582 {"tmpnam", posix_tmpnam
, METH_NOARGS
, posix_tmpnam__doc__
},
8585 {"confstr", posix_confstr
, METH_VARARGS
, posix_confstr__doc__
},
8588 {"sysconf", posix_sysconf
, METH_VARARGS
, posix_sysconf__doc__
},
8590 #ifdef HAVE_FPATHCONF
8591 {"fpathconf", posix_fpathconf
, METH_VARARGS
, posix_fpathconf__doc__
},
8593 #ifdef HAVE_PATHCONF
8594 {"pathconf", posix_pathconf
, METH_VARARGS
, posix_pathconf__doc__
},
8596 {"abort", posix_abort
, METH_NOARGS
, posix_abort__doc__
},
8598 {"_getfullpathname", posix__getfullpathname
, METH_VARARGS
, NULL
},
8600 #ifdef HAVE_GETLOADAVG
8601 {"getloadavg", posix_getloadavg
, METH_NOARGS
, posix_getloadavg__doc__
},
8604 {"urandom", win32_urandom
, METH_VARARGS
, win32_urandom__doc__
},
8607 {"urandom", vms_urandom
, METH_VARARGS
, vms_urandom__doc__
},
8609 {NULL
, NULL
} /* Sentinel */
8614 ins(PyObject
*module
, char *symbol
, long value
)
8616 return PyModule_AddIntConstant(module
, symbol
, value
);
8619 #if defined(PYOS_OS2)
8620 /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
8621 static int insertvalues(PyObject
*module
)
8624 ULONG values
[QSV_MAX
+1];
8628 Py_BEGIN_ALLOW_THREADS
8629 rc
= DosQuerySysInfo(1L, QSV_MAX
, &values
[1], sizeof(ULONG
) * QSV_MAX
);
8630 Py_END_ALLOW_THREADS
8632 if (rc
!= NO_ERROR
) {
8637 if (ins(module
, "meminstalled", values
[QSV_TOTPHYSMEM
])) return -1;
8638 if (ins(module
, "memkernel", values
[QSV_TOTRESMEM
])) return -1;
8639 if (ins(module
, "memvirtual", values
[QSV_TOTAVAILMEM
])) return -1;
8640 if (ins(module
, "maxpathlen", values
[QSV_MAX_PATH_LENGTH
])) return -1;
8641 if (ins(module
, "maxnamelen", values
[QSV_MAX_COMP_LENGTH
])) return -1;
8642 if (ins(module
, "revision", values
[QSV_VERSION_REVISION
])) return -1;
8643 if (ins(module
, "timeslice", values
[QSV_MIN_SLICE
])) return -1;
8645 switch (values
[QSV_VERSION_MINOR
]) {
8646 case 0: ver
= "2.00"; break;
8647 case 10: ver
= "2.10"; break;
8648 case 11: ver
= "2.11"; break;
8649 case 30: ver
= "3.00"; break;
8650 case 40: ver
= "4.00"; break;
8651 case 50: ver
= "5.00"; break;
8653 PyOS_snprintf(tmp
, sizeof(tmp
),
8654 "%d-%d", values
[QSV_VERSION_MAJOR
],
8655 values
[QSV_VERSION_MINOR
]);
8659 /* Add Indicator of the Version of the Operating System */
8660 if (PyModule_AddStringConstant(module
, "version", tmp
) < 0)
8663 /* Add Indicator of Which Drive was Used to Boot the System */
8664 tmp
[0] = 'A' + values
[QSV_BOOT_DRIVE
] - 1;
8668 return PyModule_AddStringConstant(module
, "bootdrive", tmp
);
8673 all_ins(PyObject
*d
)
8676 if (ins(d
, "F_OK", (long)F_OK
)) return -1;
8679 if (ins(d
, "R_OK", (long)R_OK
)) return -1;
8682 if (ins(d
, "W_OK", (long)W_OK
)) return -1;
8685 if (ins(d
, "X_OK", (long)X_OK
)) return -1;
8688 if (ins(d
, "NGROUPS_MAX", (long)NGROUPS_MAX
)) return -1;
8691 if (ins(d
, "TMP_MAX", (long)TMP_MAX
)) return -1;
8694 if (ins(d
, "WCONTINUED", (long)WCONTINUED
)) return -1;
8697 if (ins(d
, "WNOHANG", (long)WNOHANG
)) return -1;
8700 if (ins(d
, "WUNTRACED", (long)WUNTRACED
)) return -1;
8703 if (ins(d
, "O_RDONLY", (long)O_RDONLY
)) return -1;
8706 if (ins(d
, "O_WRONLY", (long)O_WRONLY
)) return -1;
8709 if (ins(d
, "O_RDWR", (long)O_RDWR
)) return -1;
8712 if (ins(d
, "O_NDELAY", (long)O_NDELAY
)) return -1;
8715 if (ins(d
, "O_NONBLOCK", (long)O_NONBLOCK
)) return -1;
8718 if (ins(d
, "O_APPEND", (long)O_APPEND
)) return -1;
8721 if (ins(d
, "O_DSYNC", (long)O_DSYNC
)) return -1;
8724 if (ins(d
, "O_RSYNC", (long)O_RSYNC
)) return -1;
8727 if (ins(d
, "O_SYNC", (long)O_SYNC
)) return -1;
8730 if (ins(d
, "O_NOCTTY", (long)O_NOCTTY
)) return -1;
8733 if (ins(d
, "O_CREAT", (long)O_CREAT
)) return -1;
8736 if (ins(d
, "O_EXCL", (long)O_EXCL
)) return -1;
8739 if (ins(d
, "O_TRUNC", (long)O_TRUNC
)) return -1;
8742 if (ins(d
, "O_BINARY", (long)O_BINARY
)) return -1;
8745 if (ins(d
, "O_TEXT", (long)O_TEXT
)) return -1;
8748 if (ins(d
, "O_LARGEFILE", (long)O_LARGEFILE
)) return -1;
8751 if (ins(d
, "O_SHLOCK", (long)O_SHLOCK
)) return -1;
8754 if (ins(d
, "O_EXLOCK", (long)O_EXLOCK
)) return -1;
8759 /* Don't inherit in child processes. */
8760 if (ins(d
, "O_NOINHERIT", (long)O_NOINHERIT
)) return -1;
8762 #ifdef _O_SHORT_LIVED
8763 /* Optimize for short life (keep in memory). */
8764 /* MS forgot to define this one with a non-underscore form too. */
8765 if (ins(d
, "O_SHORT_LIVED", (long)_O_SHORT_LIVED
)) return -1;
8768 /* Automatically delete when last handle is closed. */
8769 if (ins(d
, "O_TEMPORARY", (long)O_TEMPORARY
)) return -1;
8772 /* Optimize for random access. */
8773 if (ins(d
, "O_RANDOM", (long)O_RANDOM
)) return -1;
8776 /* Optimize for sequential access. */
8777 if (ins(d
, "O_SEQUENTIAL", (long)O_SEQUENTIAL
)) return -1;
8780 /* GNU extensions. */
8782 /* Send a SIGIO signal whenever input or output
8783 becomes available on file descriptor */
8784 if (ins(d
, "O_ASYNC", (long)O_ASYNC
)) return -1;
8787 /* Direct disk access. */
8788 if (ins(d
, "O_DIRECT", (long)O_DIRECT
)) return -1;
8791 /* Must be a directory. */
8792 if (ins(d
, "O_DIRECTORY", (long)O_DIRECTORY
)) return -1;
8795 /* Do not follow links. */
8796 if (ins(d
, "O_NOFOLLOW", (long)O_NOFOLLOW
)) return -1;
8799 /* Do not update the access time. */
8800 if (ins(d
, "O_NOATIME", (long)O_NOATIME
)) return -1;
8803 /* These come from sysexits.h */
8805 if (ins(d
, "EX_OK", (long)EX_OK
)) return -1;
8808 if (ins(d
, "EX_USAGE", (long)EX_USAGE
)) return -1;
8809 #endif /* EX_USAGE */
8811 if (ins(d
, "EX_DATAERR", (long)EX_DATAERR
)) return -1;
8812 #endif /* EX_DATAERR */
8814 if (ins(d
, "EX_NOINPUT", (long)EX_NOINPUT
)) return -1;
8815 #endif /* EX_NOINPUT */
8817 if (ins(d
, "EX_NOUSER", (long)EX_NOUSER
)) return -1;
8818 #endif /* EX_NOUSER */
8820 if (ins(d
, "EX_NOHOST", (long)EX_NOHOST
)) return -1;
8821 #endif /* EX_NOHOST */
8822 #ifdef EX_UNAVAILABLE
8823 if (ins(d
, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE
)) return -1;
8824 #endif /* EX_UNAVAILABLE */
8826 if (ins(d
, "EX_SOFTWARE", (long)EX_SOFTWARE
)) return -1;
8827 #endif /* EX_SOFTWARE */
8829 if (ins(d
, "EX_OSERR", (long)EX_OSERR
)) return -1;
8830 #endif /* EX_OSERR */
8832 if (ins(d
, "EX_OSFILE", (long)EX_OSFILE
)) return -1;
8833 #endif /* EX_OSFILE */
8835 if (ins(d
, "EX_CANTCREAT", (long)EX_CANTCREAT
)) return -1;
8836 #endif /* EX_CANTCREAT */
8838 if (ins(d
, "EX_IOERR", (long)EX_IOERR
)) return -1;
8839 #endif /* EX_IOERR */
8841 if (ins(d
, "EX_TEMPFAIL", (long)EX_TEMPFAIL
)) return -1;
8842 #endif /* EX_TEMPFAIL */
8844 if (ins(d
, "EX_PROTOCOL", (long)EX_PROTOCOL
)) return -1;
8845 #endif /* EX_PROTOCOL */
8847 if (ins(d
, "EX_NOPERM", (long)EX_NOPERM
)) return -1;
8848 #endif /* EX_NOPERM */
8850 if (ins(d
, "EX_CONFIG", (long)EX_CONFIG
)) return -1;
8851 #endif /* EX_CONFIG */
8853 if (ins(d
, "EX_NOTFOUND", (long)EX_NOTFOUND
)) return -1;
8854 #endif /* EX_NOTFOUND */
8857 #if defined(PYOS_OS2) && defined(PYCC_GCC)
8858 if (ins(d
, "P_WAIT", (long)P_WAIT
)) return -1;
8859 if (ins(d
, "P_NOWAIT", (long)P_NOWAIT
)) return -1;
8860 if (ins(d
, "P_OVERLAY", (long)P_OVERLAY
)) return -1;
8861 if (ins(d
, "P_DEBUG", (long)P_DEBUG
)) return -1;
8862 if (ins(d
, "P_SESSION", (long)P_SESSION
)) return -1;
8863 if (ins(d
, "P_DETACH", (long)P_DETACH
)) return -1;
8864 if (ins(d
, "P_PM", (long)P_PM
)) return -1;
8865 if (ins(d
, "P_DEFAULT", (long)P_DEFAULT
)) return -1;
8866 if (ins(d
, "P_MINIMIZE", (long)P_MINIMIZE
)) return -1;
8867 if (ins(d
, "P_MAXIMIZE", (long)P_MAXIMIZE
)) return -1;
8868 if (ins(d
, "P_FULLSCREEN", (long)P_FULLSCREEN
)) return -1;
8869 if (ins(d
, "P_WINDOWED", (long)P_WINDOWED
)) return -1;
8870 if (ins(d
, "P_FOREGROUND", (long)P_FOREGROUND
)) return -1;
8871 if (ins(d
, "P_BACKGROUND", (long)P_BACKGROUND
)) return -1;
8872 if (ins(d
, "P_NOCLOSE", (long)P_NOCLOSE
)) return -1;
8873 if (ins(d
, "P_NOSESSION", (long)P_NOSESSION
)) return -1;
8874 if (ins(d
, "P_QUOTE", (long)P_QUOTE
)) return -1;
8875 if (ins(d
, "P_TILDE", (long)P_TILDE
)) return -1;
8876 if (ins(d
, "P_UNRELATED", (long)P_UNRELATED
)) return -1;
8877 if (ins(d
, "P_DEBUGDESC", (long)P_DEBUGDESC
)) return -1;
8879 if (ins(d
, "P_WAIT", (long)_P_WAIT
)) return -1;
8880 if (ins(d
, "P_NOWAIT", (long)_P_NOWAIT
)) return -1;
8881 if (ins(d
, "P_OVERLAY", (long)_OLD_P_OVERLAY
)) return -1;
8882 if (ins(d
, "P_NOWAITO", (long)_P_NOWAITO
)) return -1;
8883 if (ins(d
, "P_DETACH", (long)_P_DETACH
)) return -1;
8887 #if defined(PYOS_OS2)
8888 if (insertvalues(d
)) return -1;
8894 #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
8895 #define INITFUNC initnt
8896 #define MODNAME "nt"
8898 #elif defined(PYOS_OS2)
8899 #define INITFUNC initos2
8900 #define MODNAME "os2"
8903 #define INITFUNC initposix
8904 #define MODNAME "posix"
8912 m
= Py_InitModule3(MODNAME
,
8918 /* Initialize environ dictionary */
8919 v
= convertenviron();
8921 if (v
== NULL
|| PyModule_AddObject(m
, "environ", v
) != 0)
8928 if (setup_confname_tables(m
))
8931 Py_INCREF(PyExc_OSError
);
8932 PyModule_AddObject(m
, "error", PyExc_OSError
);
8935 if (posix_putenv_garbage
== NULL
)
8936 posix_putenv_garbage
= PyDict_New();
8940 stat_result_desc
.name
= MODNAME
".stat_result";
8941 stat_result_desc
.fields
[7].name
= PyStructSequence_UnnamedField
;
8942 stat_result_desc
.fields
[8].name
= PyStructSequence_UnnamedField
;
8943 stat_result_desc
.fields
[9].name
= PyStructSequence_UnnamedField
;
8944 PyStructSequence_InitType(&StatResultType
, &stat_result_desc
);
8945 structseq_new
= StatResultType
.tp_new
;
8946 StatResultType
.tp_new
= statresult_new
;
8948 statvfs_result_desc
.name
= MODNAME
".statvfs_result";
8949 PyStructSequence_InitType(&StatVFSResultType
, &statvfs_result_desc
);
8951 Py_INCREF((PyObject
*) &StatResultType
);
8952 PyModule_AddObject(m
, "stat_result", (PyObject
*) &StatResultType
);
8953 Py_INCREF((PyObject
*) &StatVFSResultType
);
8954 PyModule_AddObject(m
, "statvfs_result",
8955 (PyObject
*) &StatVFSResultType
);
8960 * Step 2 of weak-linking support on Mac OS X.
8962 * The code below removes functions that are not available on the
8963 * currently active platform.
8965 * This block allow one to use a python binary that was build on
8966 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
8969 #ifdef HAVE_FSTATVFS
8970 if (fstatvfs
== NULL
) {
8971 if (PyObject_DelAttrString(m
, "fstatvfs") == -1) {
8975 #endif /* HAVE_FSTATVFS */
8978 if (statvfs
== NULL
) {
8979 if (PyObject_DelAttrString(m
, "statvfs") == -1) {
8983 #endif /* HAVE_STATVFS */
8986 if (lchown
== NULL
) {
8987 if (PyObject_DelAttrString(m
, "lchown") == -1) {
8991 #endif /* HAVE_LCHOWN */
8994 #endif /* __APPLE__ */