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
274 #include <shellapi.h> /* for ShellExecute() */
276 #define pclose _pclose
277 #endif /* _MSC_VER */
279 #if defined(PYCC_VACPP) && defined(PYOS_OS2)
284 #if defined(PATH_MAX) && PATH_MAX > 1024
285 #define MAXPATHLEN PATH_MAX
287 #define MAXPATHLEN 1024
289 #endif /* MAXPATHLEN */
292 /* Emulate some macros on systems that have a union instead of macros */
295 #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
299 #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
303 #define WTERMSIG(u_wait) ((u_wait).w_termsig)
306 #define WAIT_TYPE union wait
307 #define WAIT_STATUS_INT(s) (s.w_status)
309 #else /* !UNION_WAIT */
310 #define WAIT_TYPE int
311 #define WAIT_STATUS_INT(s) (s)
312 #endif /* UNION_WAIT */
314 /* Don't use the "_r" form if we don't need it (also, won't have a
315 prototype for it, at least on Solaris -- maybe others as well?). */
316 #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
317 #define USE_CTERMID_R
320 #if defined(HAVE_TMPNAM_R) && defined(WITH_THREAD)
324 /* choose the appropriate stat and fstat functions and return structs */
326 #if defined(MS_WIN64) || defined(MS_WINDOWS)
327 # define STAT win32_stat
328 # define FSTAT win32_fstat
329 # define STRUCT_STAT struct win32_stat
333 # define STRUCT_STAT struct stat
336 #if defined(MAJOR_IN_MKDEV)
337 #include <sys/mkdev.h>
339 #if defined(MAJOR_IN_SYSMACROS)
340 #include <sys/sysmacros.h>
342 #if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
343 #include <sys/mkdev.h>
347 #if defined _MSC_VER && _MSC_VER >= 1400
348 /* Microsoft CRT in VS2005 and higher will verify that a filehandle is
349 * valid and throw an assertion if it isn't.
350 * Normally, an invalid fd is likely to be a C program error and therefore
351 * an assertion can be useful, but it does contradict the POSIX standard
352 * which for write(2) states:
353 * "Otherwise, -1 shall be returned and errno set to indicate the error."
354 * "[EBADF] The fildes argument is not a valid file descriptor open for
356 * Furthermore, python allows the user to enter any old integer
357 * as a fd and should merely raise a python exception on error.
358 * The Microsoft CRT doesn't provide an official way to check for the
359 * validity of a file descriptor, but we can emulate its internal behaviour
360 * by using the exported __pinfo data member and knowledge of the
361 * internal structures involved.
362 * The structures below must be updated for each version of visual studio
363 * according to the file internal.h in the CRT source, until MS comes
364 * up with a less hacky way to do this.
365 * (all of this is to avoid globally modifying the CRT behaviour using
366 * _set_invalid_parameter_handler() and _CrtSetReportMode())
368 /* The actual size of the structure is determined at runtime.
369 * Only the first items must be present.
376 extern __declspec(dllimport
) char * __pioinfo
[];
378 #define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
379 #define IOINFO_ARRAYS 64
380 #define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS)
382 #define _NO_CONSOLE_FILENO (intptr_t)-2
384 /* This function emulates what the windows CRT does to validate file handles */
388 const int i1
= fd
>> IOINFO_L2E
;
389 const int i2
= fd
& ((1 << IOINFO_L2E
) - 1);
391 static int sizeof_ioinfo
= 0;
393 /* Determine the actual size of the ioinfo structure,
394 * as used by the CRT loaded in memory
396 if (sizeof_ioinfo
== 0 && __pioinfo
[0] != NULL
) {
397 sizeof_ioinfo
= _msize(__pioinfo
[0]) / IOINFO_ARRAY_ELTS
;
399 if (sizeof_ioinfo
== 0) {
400 /* This should not happen... */
404 /* See that it isn't a special CLEAR fileno */
405 if (fd
!= _NO_CONSOLE_FILENO
) {
406 /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead
407 * we check pointer validity and other info
409 if (0 <= i1
&& i1
< IOINFO_ARRAYS
&& __pioinfo
[i1
] != NULL
) {
410 /* finally, check that the file is open */
411 my_ioinfo
* info
= (my_ioinfo
*)(__pioinfo
[i1
] + i2
* sizeof_ioinfo
);
412 if (info
->osfile
& FOPEN
) {
422 /* the special case of checking dup2. The target fd must be in a sensible range */
424 _PyVerify_fd_dup2(int fd1
, int fd2
)
426 if (!_PyVerify_fd(fd1
))
428 if (fd2
== _NO_CONSOLE_FILENO
)
430 if ((unsigned)fd2
< _NHANDLE_
)
436 /* dummy version. _PyVerify_fd() is already defined in fileobject.h */
437 #define _PyVerify_fd_dup2(A, B) (1)
440 /* Return a dictionary corresponding to the POSIX environment table */
441 #ifdef WITH_NEXT_FRAMEWORK
442 /* On Darwin/MacOSX a shared library or framework has no access to
443 ** environ directly, we must obtain it with _NSGetEnviron().
445 #include <crt_externs.h>
446 static char **environ
;
447 #elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
448 extern char **environ
;
449 #endif /* !_MSC_VER */
459 #ifdef WITH_NEXT_FRAMEWORK
461 environ
= *_NSGetEnviron();
465 /* This part ignores errors */
466 for (e
= environ
; *e
!= NULL
; e
++) {
469 char *p
= strchr(*e
, '=');
472 k
= PyString_FromStringAndSize(*e
, (int)(p
-*e
));
477 v
= PyString_FromString(p
+1);
483 if (PyDict_GetItem(d
, k
) == NULL
) {
484 if (PyDict_SetItem(d
, k
, v
) != 0)
490 #if defined(PYOS_OS2)
493 char buffer
[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
495 rc
= DosQueryExtLIBPATH(buffer
, BEGIN_LIBPATH
);
496 if (rc
== NO_ERROR
) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
497 PyObject
*v
= PyString_FromString(buffer
);
498 PyDict_SetItemString(d
, "BEGINLIBPATH", v
);
501 rc
= DosQueryExtLIBPATH(buffer
, END_LIBPATH
);
502 if (rc
== NO_ERROR
) { /* (not a typo, envname is NOT 'END_LIBPATH') */
503 PyObject
*v
= PyString_FromString(buffer
);
504 PyDict_SetItemString(d
, "ENDLIBPATH", v
);
513 /* Set a POSIX-specific error from errno, and return NULL */
518 return PyErr_SetFromErrno(PyExc_OSError
);
521 posix_error_with_filename(char* name
)
523 return PyErr_SetFromErrnoWithFilename(PyExc_OSError
, name
);
526 #ifdef Py_WIN_WIDE_FILENAMES
528 posix_error_with_unicode_filename(Py_UNICODE
* name
)
530 return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError
, name
);
532 #endif /* Py_WIN_WIDE_FILENAMES */
536 posix_error_with_allocated_filename(char* name
)
538 PyObject
*rc
= PyErr_SetFromErrnoWithFilename(PyExc_OSError
, name
);
545 win32_error(char* function
, char* filename
)
547 /* XXX We should pass the function name along in the future.
548 (_winreg.c also wants to pass the function name.)
549 This would however require an additional param to the
550 Windows error object, which is non-trivial.
552 errno
= GetLastError();
554 return PyErr_SetFromWindowsErrWithFilename(errno
, filename
);
556 return PyErr_SetFromWindowsErr(errno
);
559 #ifdef Py_WIN_WIDE_FILENAMES
561 win32_error_unicode(char* function
, Py_UNICODE
* filename
)
563 /* XXX - see win32_error for comments on 'function' */
564 errno
= GetLastError();
566 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno
, filename
);
568 return PyErr_SetFromWindowsErr(errno
);
572 convert_to_unicode(PyObject
**param
)
574 if (PyUnicode_CheckExact(*param
))
576 else if (PyUnicode_Check(*param
))
577 /* For a Unicode subtype that's not a Unicode object,
578 return a true Unicode object with the same data. */
579 *param
= PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param
),
580 PyUnicode_GET_SIZE(*param
));
582 *param
= PyUnicode_FromEncodedObject(*param
,
583 Py_FileSystemDefaultEncoding
,
585 return (*param
) != NULL
;
588 #endif /* Py_WIN_WIDE_FILENAMES */
592 #if defined(PYOS_OS2)
593 /**********************************************************************
594 * Helper Function to Trim and Format OS/2 Messages
595 **********************************************************************/
597 os2_formatmsg(char *msgbuf
, int msglen
, char *reason
)
599 msgbuf
[msglen
] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
601 if (strlen(msgbuf
) > 0) { /* If Non-Empty Msg, Trim CRLF */
602 char *lastc
= &msgbuf
[ strlen(msgbuf
)-1 ];
604 while (lastc
> msgbuf
&& isspace(Py_CHARMASK(*lastc
)))
605 *lastc
-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
608 /* Add Optional Reason Text */
610 strcat(msgbuf
, " : ");
611 strcat(msgbuf
, reason
);
615 /**********************************************************************
616 * Decode an OS/2 Operating System Error Code
618 * A convenience function to lookup an OS/2 error code and return a
619 * text message we can use to raise a Python exception.
622 * The messages for errors returned from the OS/2 kernel reside in
623 * the file OSO001.MSG in the \OS2 directory hierarchy.
625 **********************************************************************/
627 os2_strerror(char *msgbuf
, int msgbuflen
, int errorcode
, char *reason
)
632 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
633 Py_BEGIN_ALLOW_THREADS
634 rc
= DosGetMessage(NULL
, 0, msgbuf
, msgbuflen
,
635 errorcode
, "oso001.msg", &msglen
);
639 os2_formatmsg(msgbuf
, msglen
, reason
);
641 PyOS_snprintf(msgbuf
, msgbuflen
,
642 "unknown OS error #%d", errorcode
);
647 /* Set an OS/2-specific error and return NULL. OS/2 kernel
648 errors are not in a global variable e.g. 'errno' nor are
649 they congruent with posix error numbers. */
651 static PyObject
* os2_error(int code
)
656 os2_strerror(text
, sizeof(text
), code
, "");
658 v
= Py_BuildValue("(is)", code
, text
);
660 PyErr_SetObject(PyExc_OSError
, v
);
663 return NULL
; /* Signal to Python that an Exception is Pending */
668 /* POSIX generic methods */
671 posix_fildes(PyObject
*fdobj
, int (*func
)(int))
675 fd
= PyObject_AsFileDescriptor(fdobj
);
678 if (!_PyVerify_fd(fd
))
679 return posix_error();
680 Py_BEGIN_ALLOW_THREADS
684 return posix_error();
689 #ifdef Py_WIN_WIDE_FILENAMES
691 unicode_file_names(void)
693 static int canusewide
= -1;
694 if (canusewide
== -1) {
695 /* As per doc for ::GetVersion(), this is the correct test for
696 the Windows NT family. */
697 canusewide
= (GetVersion() < 0x80000000) ? 1 : 0;
704 posix_1str(PyObject
*args
, char *format
, int (*func
)(const char*))
708 if (!PyArg_ParseTuple(args
, format
,
709 Py_FileSystemDefaultEncoding
, &path1
))
711 Py_BEGIN_ALLOW_THREADS
712 res
= (*func
)(path1
);
715 return posix_error_with_allocated_filename(path1
);
722 posix_2str(PyObject
*args
,
724 int (*func
)(const char *, const char *))
726 char *path1
= NULL
, *path2
= NULL
;
728 if (!PyArg_ParseTuple(args
, format
,
729 Py_FileSystemDefaultEncoding
, &path1
,
730 Py_FileSystemDefaultEncoding
, &path2
))
732 Py_BEGIN_ALLOW_THREADS
733 res
= (*func
)(path1
, path2
);
738 /* XXX how to report both path1 and path2??? */
739 return posix_error();
744 #ifdef Py_WIN_WIDE_FILENAMES
746 win32_1str(PyObject
* args
, char* func
,
747 char* format
, BOOL (__stdcall
*funcA
)(LPCSTR
),
748 char* wformat
, BOOL (__stdcall
*funcW
)(LPWSTR
))
753 if (unicode_file_names()) {
754 if (!PyArg_ParseTuple(args
, wformat
, &uni
))
757 Py_BEGIN_ALLOW_THREADS
758 result
= funcW(PyUnicode_AsUnicode(uni
));
761 return win32_error_unicode(func
, PyUnicode_AsUnicode(uni
));
766 if (!PyArg_ParseTuple(args
, format
, &ansi
))
768 Py_BEGIN_ALLOW_THREADS
769 result
= funcA(ansi
);
772 return win32_error(func
, ansi
);
778 /* This is a reimplementation of the C library's chdir function,
779 but one that produces Win32 errors instead of DOS error codes.
780 chdir is essentially a wrapper around SetCurrentDirectory; however,
781 it also needs to set "magic" environment variables indicating
782 the per-drive current directory, which are of the form =<drive>: */
783 static BOOL __stdcall
784 win32_chdir(LPCSTR path
)
786 char new_path
[MAX_PATH
+1];
790 if(!SetCurrentDirectoryA(path
))
792 result
= GetCurrentDirectoryA(MAX_PATH
+1, new_path
);
795 /* In the ANSI API, there should not be any paths longer
797 assert(result
<= MAX_PATH
+1);
798 if (strncmp(new_path
, "\\\\", 2) == 0 ||
799 strncmp(new_path
, "//", 2) == 0)
800 /* UNC path, nothing to do. */
802 env
[1] = new_path
[0];
803 return SetEnvironmentVariableA(env
, new_path
);
806 /* The Unicode version differs from the ANSI version
807 since the current directory might exceed MAX_PATH characters */
808 static BOOL __stdcall
809 win32_wchdir(LPCWSTR path
)
811 wchar_t _new_path
[MAX_PATH
+1], *new_path
= _new_path
;
813 wchar_t env
[4] = L
"=x:";
815 if(!SetCurrentDirectoryW(path
))
817 result
= GetCurrentDirectoryW(MAX_PATH
+1, new_path
);
820 if (result
> MAX_PATH
+1) {
821 new_path
= malloc(result
* sizeof(wchar_t));
823 SetLastError(ERROR_OUTOFMEMORY
);
826 result
= GetCurrentDirectoryW(result
, new_path
);
832 if (wcsncmp(new_path
, L
"\\\\", 2) == 0 ||
833 wcsncmp(new_path
, L
"//", 2) == 0)
834 /* UNC path, nothing to do. */
836 env
[1] = new_path
[0];
837 result
= SetEnvironmentVariableW(env
, new_path
);
838 if (new_path
!= _new_path
)
845 /* The CRT of Windows has a number of flaws wrt. its stat() implementation:
846 - time stamps are restricted to second resolution
847 - file modification times suffer from forth-and-back conversions between
849 Therefore, we implement our own stat, based on the Win32 API directly.
851 #define HAVE_STAT_NSEC 1
856 unsigned short st_mode
;
870 static __int64 secs_between_epochs
= 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
873 FILE_TIME_to_time_t_nsec(FILETIME
*in_ptr
, int *time_out
, int* nsec_out
)
875 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
876 /* Cannot simply cast and dereference in_ptr,
877 since it might not be aligned properly */
879 memcpy(&in
, in_ptr
, sizeof(in
));
880 *nsec_out
= (int)(in
% 10000000) * 100; /* FILETIME is in units of 100 nsec. */
881 /* XXX Win32 supports time stamps past 2038; we currently don't */
882 *time_out
= Py_SAFE_DOWNCAST((in
/ 10000000) - secs_between_epochs
, __int64
, int);
886 time_t_to_FILE_TIME(int time_in
, int nsec_in
, FILETIME
*out_ptr
)
890 out
= time_in
+ secs_between_epochs
;
891 out
= out
* 10000000 + nsec_in
/ 100;
892 memcpy(out_ptr
, &out
, sizeof(out
));
895 /* Below, we *know* that ugo+r is 0444 */
897 #error Unsupported C library
900 attributes_to_mode(DWORD attr
)
903 if (attr
& FILE_ATTRIBUTE_DIRECTORY
)
904 m
|= _S_IFDIR
| 0111; /* IFEXEC for user,group,other */
907 if (attr
& FILE_ATTRIBUTE_READONLY
)
915 attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA
*info
, struct win32_stat
*result
)
917 memset(result
, 0, sizeof(*result
));
918 result
->st_mode
= attributes_to_mode(info
->dwFileAttributes
);
919 result
->st_size
= (((__int64
)info
->nFileSizeHigh
)<<32) + info
->nFileSizeLow
;
920 FILE_TIME_to_time_t_nsec(&info
->ftCreationTime
, &result
->st_ctime
, &result
->st_ctime_nsec
);
921 FILE_TIME_to_time_t_nsec(&info
->ftLastWriteTime
, &result
->st_mtime
, &result
->st_mtime_nsec
);
922 FILE_TIME_to_time_t_nsec(&info
->ftLastAccessTime
, &result
->st_atime
, &result
->st_atime_nsec
);
927 /* Emulate GetFileAttributesEx[AW] on Windows 95 */
928 static int checked
= 0;
929 static BOOL (CALLBACK
*gfaxa
)(LPCSTR
, GET_FILEEX_INFO_LEVELS
, LPVOID
);
930 static BOOL (CALLBACK
*gfaxw
)(LPCWSTR
, GET_FILEEX_INFO_LEVELS
, LPVOID
);
938 hKernel32
= GetModuleHandle("KERNEL32");
939 *(FARPROC
*)&gfaxa
= GetProcAddress(hKernel32
, "GetFileAttributesExA");
940 *(FARPROC
*)&gfaxw
= GetProcAddress(hKernel32
, "GetFileAttributesExW");
944 attributes_from_dir(LPCSTR pszFile
, LPWIN32_FILE_ATTRIBUTE_DATA pfad
)
947 WIN32_FIND_DATAA FileData
;
948 hFindFile
= FindFirstFileA(pszFile
, &FileData
);
949 if (hFindFile
== INVALID_HANDLE_VALUE
)
951 FindClose(hFindFile
);
952 pfad
->dwFileAttributes
= FileData
.dwFileAttributes
;
953 pfad
->ftCreationTime
= FileData
.ftCreationTime
;
954 pfad
->ftLastAccessTime
= FileData
.ftLastAccessTime
;
955 pfad
->ftLastWriteTime
= FileData
.ftLastWriteTime
;
956 pfad
->nFileSizeHigh
= FileData
.nFileSizeHigh
;
957 pfad
->nFileSizeLow
= FileData
.nFileSizeLow
;
962 attributes_from_dir_w(LPCWSTR pszFile
, LPWIN32_FILE_ATTRIBUTE_DATA pfad
)
965 WIN32_FIND_DATAW FileData
;
966 hFindFile
= FindFirstFileW(pszFile
, &FileData
);
967 if (hFindFile
== INVALID_HANDLE_VALUE
)
969 FindClose(hFindFile
);
970 pfad
->dwFileAttributes
= FileData
.dwFileAttributes
;
971 pfad
->ftCreationTime
= FileData
.ftCreationTime
;
972 pfad
->ftLastAccessTime
= FileData
.ftLastAccessTime
;
973 pfad
->ftLastWriteTime
= FileData
.ftLastWriteTime
;
974 pfad
->nFileSizeHigh
= FileData
.nFileSizeHigh
;
975 pfad
->nFileSizeLow
= FileData
.nFileSizeLow
;
980 Py_GetFileAttributesExA(LPCSTR pszFile
,
981 GET_FILEEX_INFO_LEVELS level
,
985 LPWIN32_FILE_ATTRIBUTE_DATA pfad
= pv
;
986 /* First try to use the system's implementation, if that is
987 available and either succeeds to gives an error other than
988 that it isn't implemented. */
991 result
= gfaxa(pszFile
, level
, pv
);
992 if (result
|| GetLastError() != ERROR_CALL_NOT_IMPLEMENTED
)
995 /* It's either not present, or not implemented.
996 Emulate using FindFirstFile. */
997 if (level
!= GetFileExInfoStandard
) {
998 SetLastError(ERROR_INVALID_PARAMETER
);
1001 /* Use GetFileAttributes to validate that the file name
1002 does not contain wildcards (which FindFirstFile would
1004 if (GetFileAttributesA(pszFile
) == 0xFFFFFFFF)
1006 return attributes_from_dir(pszFile
, pfad
);
1010 Py_GetFileAttributesExW(LPCWSTR pszFile
,
1011 GET_FILEEX_INFO_LEVELS level
,
1015 LPWIN32_FILE_ATTRIBUTE_DATA pfad
= pv
;
1016 /* First try to use the system's implementation, if that is
1017 available and either succeeds to gives an error other than
1018 that it isn't implemented. */
1021 result
= gfaxw(pszFile
, level
, pv
);
1022 if (result
|| GetLastError() != ERROR_CALL_NOT_IMPLEMENTED
)
1025 /* It's either not present, or not implemented.
1026 Emulate using FindFirstFile. */
1027 if (level
!= GetFileExInfoStandard
) {
1028 SetLastError(ERROR_INVALID_PARAMETER
);
1031 /* Use GetFileAttributes to validate that the file name
1032 does not contain wildcards (which FindFirstFile would
1034 if (GetFileAttributesW(pszFile
) == 0xFFFFFFFF)
1036 return attributes_from_dir_w(pszFile
, pfad
);
1040 win32_stat(const char* path
, struct win32_stat
*result
)
1042 WIN32_FILE_ATTRIBUTE_DATA info
;
1045 /* XXX not supported on Win95 and NT 3.x */
1046 if (!Py_GetFileAttributesExA(path
, GetFileExInfoStandard
, &info
)) {
1047 if (GetLastError() != ERROR_SHARING_VIOLATION
) {
1048 /* Protocol violation: we explicitly clear errno, instead of
1049 setting it to a POSIX error. Callers should use GetLastError. */
1053 /* Could not get attributes on open file. Fall back to
1054 reading the directory. */
1055 if (!attributes_from_dir(path
, &info
)) {
1056 /* Very strange. This should not fail now */
1062 code
= attribute_data_to_stat(&info
, result
);
1065 /* Set S_IFEXEC if it is an .exe, .bat, ... */
1066 dot
= strrchr(path
, '.');
1068 if (stricmp(dot
, ".bat") == 0 ||
1069 stricmp(dot
, ".cmd") == 0 ||
1070 stricmp(dot
, ".exe") == 0 ||
1071 stricmp(dot
, ".com") == 0)
1072 result
->st_mode
|= 0111;
1078 win32_wstat(const wchar_t* path
, struct win32_stat
*result
)
1082 WIN32_FILE_ATTRIBUTE_DATA info
;
1083 /* XXX not supported on Win95 and NT 3.x */
1084 if (!Py_GetFileAttributesExW(path
, GetFileExInfoStandard
, &info
)) {
1085 if (GetLastError() != ERROR_SHARING_VIOLATION
) {
1086 /* Protocol violation: we explicitly clear errno, instead of
1087 setting it to a POSIX error. Callers should use GetLastError. */
1091 /* Could not get attributes on open file. Fall back to
1092 reading the directory. */
1093 if (!attributes_from_dir_w(path
, &info
)) {
1094 /* Very strange. This should not fail now */
1100 code
= attribute_data_to_stat(&info
, result
);
1103 /* Set IFEXEC if it is an .exe, .bat, ... */
1104 dot
= wcsrchr(path
, '.');
1106 if (_wcsicmp(dot
, L
".bat") == 0 ||
1107 _wcsicmp(dot
, L
".cmd") == 0 ||
1108 _wcsicmp(dot
, L
".exe") == 0 ||
1109 _wcsicmp(dot
, L
".com") == 0)
1110 result
->st_mode
|= 0111;
1116 win32_fstat(int file_number
, struct win32_stat
*result
)
1118 BY_HANDLE_FILE_INFORMATION info
;
1122 h
= (HANDLE
)_get_osfhandle(file_number
);
1124 /* Protocol violation: we explicitly clear errno, instead of
1125 setting it to a POSIX error. Callers should use GetLastError. */
1128 if (h
== INVALID_HANDLE_VALUE
) {
1129 /* This is really a C library error (invalid file handle).
1130 We set the Win32 error to the closes one matching. */
1131 SetLastError(ERROR_INVALID_HANDLE
);
1134 memset(result
, 0, sizeof(*result
));
1136 type
= GetFileType(h
);
1137 if (type
== FILE_TYPE_UNKNOWN
) {
1138 DWORD error
= GetLastError();
1142 /* else: valid but unknown file */
1145 if (type
!= FILE_TYPE_DISK
) {
1146 if (type
== FILE_TYPE_CHAR
)
1147 result
->st_mode
= _S_IFCHR
;
1148 else if (type
== FILE_TYPE_PIPE
)
1149 result
->st_mode
= _S_IFIFO
;
1153 if (!GetFileInformationByHandle(h
, &info
)) {
1157 /* similar to stat() */
1158 result
->st_mode
= attributes_to_mode(info
.dwFileAttributes
);
1159 result
->st_size
= (((__int64
)info
.nFileSizeHigh
)<<32) + info
.nFileSizeLow
;
1160 FILE_TIME_to_time_t_nsec(&info
.ftCreationTime
, &result
->st_ctime
, &result
->st_ctime_nsec
);
1161 FILE_TIME_to_time_t_nsec(&info
.ftLastWriteTime
, &result
->st_mtime
, &result
->st_mtime_nsec
);
1162 FILE_TIME_to_time_t_nsec(&info
.ftLastAccessTime
, &result
->st_atime
, &result
->st_atime_nsec
);
1163 /* specific to fstat() */
1164 result
->st_nlink
= info
.nNumberOfLinks
;
1165 result
->st_ino
= (((__int64
)info
.nFileIndexHigh
)<<32) + info
.nFileIndexLow
;
1169 #endif /* MS_WINDOWS */
1171 PyDoc_STRVAR(stat_result__doc__
,
1172 "stat_result: Result from stat or lstat.\n\n\
1173 This object may be accessed either as a tuple of\n\
1174 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
1175 or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1177 Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1178 or st_flags, they are available as attributes only.\n\
1180 See os.stat for more information.");
1182 static PyStructSequence_Field stat_result_fields
[] = {
1183 {"st_mode", "protection bits"},
1184 {"st_ino", "inode"},
1185 {"st_dev", "device"},
1186 {"st_nlink", "number of hard links"},
1187 {"st_uid", "user ID of owner"},
1188 {"st_gid", "group ID of owner"},
1189 {"st_size", "total size, in bytes"},
1190 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1191 {NULL
, "integer time of last access"},
1192 {NULL
, "integer time of last modification"},
1193 {NULL
, "integer time of last change"},
1194 {"st_atime", "time of last access"},
1195 {"st_mtime", "time of last modification"},
1196 {"st_ctime", "time of last change"},
1197 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1198 {"st_blksize", "blocksize for filesystem I/O"},
1200 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
1201 {"st_blocks", "number of blocks allocated"},
1203 #ifdef HAVE_STRUCT_STAT_ST_RDEV
1204 {"st_rdev", "device type (if inode device)"},
1206 #ifdef HAVE_STRUCT_STAT_ST_FLAGS
1207 {"st_flags", "user defined flags for file"},
1209 #ifdef HAVE_STRUCT_STAT_ST_GEN
1210 {"st_gen", "generation number"},
1212 #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1213 {"st_birthtime", "time of creation"},
1218 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1219 #define ST_BLKSIZE_IDX 13
1221 #define ST_BLKSIZE_IDX 12
1224 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
1225 #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1227 #define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1230 #ifdef HAVE_STRUCT_STAT_ST_RDEV
1231 #define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1233 #define ST_RDEV_IDX ST_BLOCKS_IDX
1236 #ifdef HAVE_STRUCT_STAT_ST_FLAGS
1237 #define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1239 #define ST_FLAGS_IDX ST_RDEV_IDX
1242 #ifdef HAVE_STRUCT_STAT_ST_GEN
1243 #define ST_GEN_IDX (ST_FLAGS_IDX+1)
1245 #define ST_GEN_IDX ST_FLAGS_IDX
1248 #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1249 #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1251 #define ST_BIRTHTIME_IDX ST_GEN_IDX
1254 static PyStructSequence_Desc stat_result_desc
= {
1255 "stat_result", /* name */
1256 stat_result__doc__
, /* doc */
1261 PyDoc_STRVAR(statvfs_result__doc__
,
1262 "statvfs_result: Result from statvfs or fstatvfs.\n\n\
1263 This object may be accessed either as a tuple of\n\
1264 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
1265 or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
1267 See os.statvfs for more information.");
1269 static PyStructSequence_Field statvfs_result_fields
[] = {
1283 static PyStructSequence_Desc statvfs_result_desc
= {
1284 "statvfs_result", /* name */
1285 statvfs_result__doc__
, /* doc */
1286 statvfs_result_fields
,
1290 static int initialized
;
1291 static PyTypeObject StatResultType
;
1292 static PyTypeObject StatVFSResultType
;
1293 static newfunc structseq_new
;
1296 statresult_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
1298 PyStructSequence
*result
;
1301 result
= (PyStructSequence
*)structseq_new(type
, args
, kwds
);
1304 /* If we have been initialized from a tuple,
1305 st_?time might be set to None. Initialize it
1306 from the int slots. */
1307 for (i
= 7; i
<= 9; i
++) {
1308 if (result
->ob_item
[i
+3] == Py_None
) {
1310 Py_INCREF(result
->ob_item
[i
]);
1311 result
->ob_item
[i
+3] = result
->ob_item
[i
];
1314 return (PyObject
*)result
;
1319 /* If true, st_?time is float. */
1320 static int _stat_float_times
= 1;
1322 PyDoc_STRVAR(stat_float_times__doc__
,
1323 "stat_float_times([newval]) -> oldval\n\n\
1324 Determine whether os.[lf]stat represents time stamps as float objects.\n\
1325 If newval is True, future calls to stat() return floats, if it is False,\n\
1326 future calls return ints. \n\
1327 If newval is omitted, return the current setting.\n");
1330 stat_float_times(PyObject
* self
, PyObject
*args
)
1333 if (!PyArg_ParseTuple(args
, "|i:stat_float_times", &newval
))
1336 /* Return old value */
1337 return PyBool_FromLong(_stat_float_times
);
1338 _stat_float_times
= newval
;
1344 fill_time(PyObject
*v
, int index
, time_t sec
, unsigned long nsec
)
1346 PyObject
*fval
,*ival
;
1347 #if SIZEOF_TIME_T > SIZEOF_LONG
1348 ival
= PyLong_FromLongLong((PY_LONG_LONG
)sec
);
1350 ival
= PyInt_FromLong((long)sec
);
1354 if (_stat_float_times
) {
1355 fval
= PyFloat_FromDouble(sec
+ 1e-9*nsec
);
1360 PyStructSequence_SET_ITEM(v
, index
, ival
);
1361 PyStructSequence_SET_ITEM(v
, index
+3, fval
);
1364 /* pack a system stat C structure into the Python stat tuple
1365 (used by posix_stat() and posix_fstat()) */
1367 _pystat_fromstructstat(STRUCT_STAT
*st
)
1369 unsigned long ansec
, mnsec
, cnsec
;
1370 PyObject
*v
= PyStructSequence_New(&StatResultType
);
1374 PyStructSequence_SET_ITEM(v
, 0, PyInt_FromLong((long)st
->st_mode
));
1375 #ifdef HAVE_LARGEFILE_SUPPORT
1376 PyStructSequence_SET_ITEM(v
, 1,
1377 PyLong_FromLongLong((PY_LONG_LONG
)st
->st_ino
));
1379 PyStructSequence_SET_ITEM(v
, 1, PyInt_FromLong((long)st
->st_ino
));
1381 #if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
1382 PyStructSequence_SET_ITEM(v
, 2,
1383 PyLong_FromLongLong((PY_LONG_LONG
)st
->st_dev
));
1385 PyStructSequence_SET_ITEM(v
, 2, PyInt_FromLong((long)st
->st_dev
));
1387 PyStructSequence_SET_ITEM(v
, 3, PyInt_FromLong((long)st
->st_nlink
));
1388 PyStructSequence_SET_ITEM(v
, 4, PyInt_FromLong((long)st
->st_uid
));
1389 PyStructSequence_SET_ITEM(v
, 5, PyInt_FromLong((long)st
->st_gid
));
1390 #ifdef HAVE_LARGEFILE_SUPPORT
1391 PyStructSequence_SET_ITEM(v
, 6,
1392 PyLong_FromLongLong((PY_LONG_LONG
)st
->st_size
));
1394 PyStructSequence_SET_ITEM(v
, 6, PyInt_FromLong(st
->st_size
));
1397 #if defined(HAVE_STAT_TV_NSEC)
1398 ansec
= st
->st_atim
.tv_nsec
;
1399 mnsec
= st
->st_mtim
.tv_nsec
;
1400 cnsec
= st
->st_ctim
.tv_nsec
;
1401 #elif defined(HAVE_STAT_TV_NSEC2)
1402 ansec
= st
->st_atimespec
.tv_nsec
;
1403 mnsec
= st
->st_mtimespec
.tv_nsec
;
1404 cnsec
= st
->st_ctimespec
.tv_nsec
;
1405 #elif defined(HAVE_STAT_NSEC)
1406 ansec
= st
->st_atime_nsec
;
1407 mnsec
= st
->st_mtime_nsec
;
1408 cnsec
= st
->st_ctime_nsec
;
1410 ansec
= mnsec
= cnsec
= 0;
1412 fill_time(v
, 7, st
->st_atime
, ansec
);
1413 fill_time(v
, 8, st
->st_mtime
, mnsec
);
1414 fill_time(v
, 9, st
->st_ctime
, cnsec
);
1416 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1417 PyStructSequence_SET_ITEM(v
, ST_BLKSIZE_IDX
,
1418 PyInt_FromLong((long)st
->st_blksize
));
1420 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
1421 PyStructSequence_SET_ITEM(v
, ST_BLOCKS_IDX
,
1422 PyInt_FromLong((long)st
->st_blocks
));
1424 #ifdef HAVE_STRUCT_STAT_ST_RDEV
1425 PyStructSequence_SET_ITEM(v
, ST_RDEV_IDX
,
1426 PyInt_FromLong((long)st
->st_rdev
));
1428 #ifdef HAVE_STRUCT_STAT_ST_GEN
1429 PyStructSequence_SET_ITEM(v
, ST_GEN_IDX
,
1430 PyInt_FromLong((long)st
->st_gen
));
1432 #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1435 unsigned long bsec
,bnsec
;
1436 bsec
= (long)st
->st_birthtime
;
1437 #ifdef HAVE_STAT_TV_NSEC2
1438 bnsec
= st
->st_birthtimespec
.tv_nsec
;
1442 if (_stat_float_times
) {
1443 val
= PyFloat_FromDouble(bsec
+ 1e-9*bnsec
);
1445 val
= PyInt_FromLong((long)bsec
);
1447 PyStructSequence_SET_ITEM(v
, ST_BIRTHTIME_IDX
,
1451 #ifdef HAVE_STRUCT_STAT_ST_FLAGS
1452 PyStructSequence_SET_ITEM(v
, ST_FLAGS_IDX
,
1453 PyInt_FromLong((long)st
->st_flags
));
1456 if (PyErr_Occurred()) {
1466 /* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\,
1467 where / can be used in place of \ and the trailing slash is optional.
1468 Both SERVER and SHARE must have at least one character.
1471 #define ISSLASHA(c) ((c) == '\\' || (c) == '/')
1472 #define ISSLASHW(c) ((c) == L'\\' || (c) == L'/')
1474 #define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))
1478 IsUNCRootA(char *path
, int pathlen
)
1480 #define ISSLASH ISSLASHA
1484 if (pathlen
< 5 || !ISSLASH(path
[0]) || !ISSLASH(path
[1]))
1485 /* minimum UNCRoot is \\x\y */
1487 for (i
= 2; i
< pathlen
; i
++)
1488 if (ISSLASH(path
[i
])) break;
1489 if (i
== 2 || i
== pathlen
)
1490 /* do not allow \\\SHARE or \\SERVER */
1493 for (i
= share
; i
< pathlen
; i
++)
1494 if (ISSLASH(path
[i
])) break;
1495 return (i
!= share
&& (i
== pathlen
|| i
== pathlen
-1));
1500 #ifdef Py_WIN_WIDE_FILENAMES
1502 IsUNCRootW(Py_UNICODE
*path
, int pathlen
)
1504 #define ISSLASH ISSLASHW
1508 if (pathlen
< 5 || !ISSLASH(path
[0]) || !ISSLASH(path
[1]))
1509 /* minimum UNCRoot is \\x\y */
1511 for (i
= 2; i
< pathlen
; i
++)
1512 if (ISSLASH(path
[i
])) break;
1513 if (i
== 2 || i
== pathlen
)
1514 /* do not allow \\\SHARE or \\SERVER */
1517 for (i
= share
; i
< pathlen
; i
++)
1518 if (ISSLASH(path
[i
])) break;
1519 return (i
!= share
&& (i
== pathlen
|| i
== pathlen
-1));
1523 #endif /* Py_WIN_WIDE_FILENAMES */
1524 #endif /* MS_WINDOWS */
1527 posix_do_stat(PyObject
*self
, PyObject
*args
,
1530 int (*statfunc
)(const char *, STRUCT_STAT
*, ...),
1532 int (*statfunc
)(const char *, STRUCT_STAT
*),
1535 int (*wstatfunc
)(const Py_UNICODE
*, STRUCT_STAT
*))
1538 char *path
= NULL
; /* pass this to stat; do not free() it */
1539 char *pathfree
= NULL
; /* this memory must be free'd */
1543 #ifdef Py_WIN_WIDE_FILENAMES
1544 /* If on wide-character-capable OS see if argument
1545 is Unicode and if so use wide API. */
1546 if (unicode_file_names()) {
1547 PyUnicodeObject
*po
;
1548 if (PyArg_ParseTuple(args
, wformat
, &po
)) {
1549 Py_UNICODE
*wpath
= PyUnicode_AS_UNICODE(po
);
1551 Py_BEGIN_ALLOW_THREADS
1552 /* PyUnicode_AS_UNICODE result OK without
1553 thread lock as it is a simple dereference. */
1554 res
= wstatfunc(wpath
, &st
);
1555 Py_END_ALLOW_THREADS
1558 return win32_error_unicode("stat", wpath
);
1559 return _pystat_fromstructstat(&st
);
1561 /* Drop the argument parsing error as narrow strings
1567 if (!PyArg_ParseTuple(args
, format
,
1568 Py_FileSystemDefaultEncoding
, &path
))
1572 Py_BEGIN_ALLOW_THREADS
1573 res
= (*statfunc
)(path
, &st
);
1574 Py_END_ALLOW_THREADS
1578 result
= win32_error("stat", pathfree
);
1580 result
= posix_error_with_filename(pathfree
);
1584 result
= _pystat_fromstructstat(&st
);
1586 PyMem_Free(pathfree
);
1592 PyDoc_STRVAR(posix_access__doc__
,
1593 "access(path, mode) -> True if granted, False otherwise\n\n\
1594 Use the real uid/gid to test for access to a path. Note that most\n\
1595 operations will use the effective uid/gid, therefore this routine can\n\
1596 be used in a suid/sgid environment to test if the invoking user has the\n\
1597 specified access to the path. The mode argument can be F_OK to test\n\
1598 existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
1601 posix_access(PyObject
*self
, PyObject
*args
)
1606 #ifdef Py_WIN_WIDE_FILENAMES
1608 if (unicode_file_names()) {
1609 PyUnicodeObject
*po
;
1610 if (PyArg_ParseTuple(args
, "Ui:access", &po
, &mode
)) {
1611 Py_BEGIN_ALLOW_THREADS
1612 /* PyUnicode_AS_UNICODE OK without thread lock as
1613 it is a simple dereference. */
1614 attr
= GetFileAttributesW(PyUnicode_AS_UNICODE(po
));
1615 Py_END_ALLOW_THREADS
1618 /* Drop the argument parsing error as narrow strings
1622 if (!PyArg_ParseTuple(args
, "eti:access",
1623 Py_FileSystemDefaultEncoding
, &path
, &mode
))
1625 Py_BEGIN_ALLOW_THREADS
1626 attr
= GetFileAttributesA(path
);
1627 Py_END_ALLOW_THREADS
1630 if (attr
== 0xFFFFFFFF)
1631 /* File does not exist, or cannot read attributes */
1632 return PyBool_FromLong(0);
1633 /* Access is possible if either write access wasn't requested, or
1634 the file isn't read-only, or if it's a directory, as there are
1635 no read-only directories on Windows. */
1636 return PyBool_FromLong(!(mode
& 2)
1637 || !(attr
& FILE_ATTRIBUTE_READONLY
)
1638 || (attr
& FILE_ATTRIBUTE_DIRECTORY
));
1641 if (!PyArg_ParseTuple(args
, "eti:access",
1642 Py_FileSystemDefaultEncoding
, &path
, &mode
))
1644 Py_BEGIN_ALLOW_THREADS
1645 res
= access(path
, mode
);
1646 Py_END_ALLOW_THREADS
1648 return PyBool_FromLong(res
== 0);
1666 PyDoc_STRVAR(posix_ttyname__doc__
,
1667 "ttyname(fd) -> string\n\n\
1668 Return the name of the terminal device connected to 'fd'.");
1671 posix_ttyname(PyObject
*self
, PyObject
*args
)
1676 if (!PyArg_ParseTuple(args
, "i:ttyname", &id
))
1680 /* file descriptor 0 only, the default input device (stdin) */
1691 return posix_error();
1692 return PyString_FromString(ret
);
1697 PyDoc_STRVAR(posix_ctermid__doc__
,
1698 "ctermid() -> string\n\n\
1699 Return the name of the controlling terminal for this process.");
1702 posix_ctermid(PyObject
*self
, PyObject
*noargs
)
1705 char buffer
[L_ctermid
];
1707 #ifdef USE_CTERMID_R
1708 ret
= ctermid_r(buffer
);
1710 ret
= ctermid(buffer
);
1713 return posix_error();
1714 return PyString_FromString(buffer
);
1718 PyDoc_STRVAR(posix_chdir__doc__
,
1720 Change the current working directory to the specified path.");
1723 posix_chdir(PyObject
*self
, PyObject
*args
)
1726 return win32_1str(args
, "chdir", "s:chdir", win32_chdir
, "U:chdir", win32_wchdir
);
1727 #elif defined(PYOS_OS2) && defined(PYCC_GCC)
1728 return posix_1str(args
, "et:chdir", _chdir2
);
1729 #elif defined(__VMS)
1730 return posix_1str(args
, "et:chdir", (int (*)(const char *))chdir
);
1732 return posix_1str(args
, "et:chdir", chdir
);
1737 PyDoc_STRVAR(posix_fchdir__doc__
,
1738 "fchdir(fildes)\n\n\
1739 Change to the directory of the given file descriptor. fildes must be\n\
1740 opened on a directory, not a file.");
1743 posix_fchdir(PyObject
*self
, PyObject
*fdobj
)
1745 return posix_fildes(fdobj
, fchdir
);
1747 #endif /* HAVE_FCHDIR */
1750 PyDoc_STRVAR(posix_chmod__doc__
,
1751 "chmod(path, mode)\n\n\
1752 Change the access permissions of a file.");
1755 posix_chmod(PyObject
*self
, PyObject
*args
)
1760 #ifdef Py_WIN_WIDE_FILENAMES
1762 if (unicode_file_names()) {
1763 PyUnicodeObject
*po
;
1764 if (PyArg_ParseTuple(args
, "Ui|:chmod", &po
, &i
)) {
1765 Py_BEGIN_ALLOW_THREADS
1766 attr
= GetFileAttributesW(PyUnicode_AS_UNICODE(po
));
1767 if (attr
!= 0xFFFFFFFF) {
1769 attr
&= ~FILE_ATTRIBUTE_READONLY
;
1771 attr
|= FILE_ATTRIBUTE_READONLY
;
1772 res
= SetFileAttributesW(PyUnicode_AS_UNICODE(po
), attr
);
1776 Py_END_ALLOW_THREADS
1778 return win32_error_unicode("chmod",
1779 PyUnicode_AS_UNICODE(po
));
1783 /* Drop the argument parsing error as narrow strings
1787 if (!PyArg_ParseTuple(args
, "eti:chmod", Py_FileSystemDefaultEncoding
,
1790 Py_BEGIN_ALLOW_THREADS
1791 attr
= GetFileAttributesA(path
);
1792 if (attr
!= 0xFFFFFFFF) {
1794 attr
&= ~FILE_ATTRIBUTE_READONLY
;
1796 attr
|= FILE_ATTRIBUTE_READONLY
;
1797 res
= SetFileAttributesA(path
, attr
);
1801 Py_END_ALLOW_THREADS
1803 win32_error("chmod", path
);
1810 #else /* Py_WIN_WIDE_FILENAMES */
1811 if (!PyArg_ParseTuple(args
, "eti:chmod", Py_FileSystemDefaultEncoding
,
1814 Py_BEGIN_ALLOW_THREADS
1815 res
= chmod(path
, i
);
1816 Py_END_ALLOW_THREADS
1818 return posix_error_with_allocated_filename(path
);
1826 PyDoc_STRVAR(posix_fchmod__doc__
,
1827 "fchmod(fd, mode)\n\n\
1828 Change the access permissions of the file given by file\n\
1832 posix_fchmod(PyObject
*self
, PyObject
*args
)
1835 if (!PyArg_ParseTuple(args
, "ii:fchmod", &fd
, &mode
))
1837 Py_BEGIN_ALLOW_THREADS
1838 res
= fchmod(fd
, mode
);
1839 Py_END_ALLOW_THREADS
1841 return posix_error();
1844 #endif /* HAVE_FCHMOD */
1847 PyDoc_STRVAR(posix_lchmod__doc__
,
1848 "lchmod(path, mode)\n\n\
1849 Change the access permissions of a file. If path is a symlink, this\n\
1850 affects the link itself rather than the target.");
1853 posix_lchmod(PyObject
*self
, PyObject
*args
)
1858 if (!PyArg_ParseTuple(args
, "eti:lchmod", Py_FileSystemDefaultEncoding
,
1861 Py_BEGIN_ALLOW_THREADS
1862 res
= lchmod(path
, i
);
1863 Py_END_ALLOW_THREADS
1865 return posix_error_with_allocated_filename(path
);
1869 #endif /* HAVE_LCHMOD */
1873 PyDoc_STRVAR(posix_chflags__doc__
,
1874 "chflags(path, flags)\n\n\
1878 posix_chflags(PyObject
*self
, PyObject
*args
)
1881 unsigned long flags
;
1883 if (!PyArg_ParseTuple(args
, "etk:chflags",
1884 Py_FileSystemDefaultEncoding
, &path
, &flags
))
1886 Py_BEGIN_ALLOW_THREADS
1887 res
= chflags(path
, flags
);
1888 Py_END_ALLOW_THREADS
1890 return posix_error_with_allocated_filename(path
);
1895 #endif /* HAVE_CHFLAGS */
1897 #ifdef HAVE_LCHFLAGS
1898 PyDoc_STRVAR(posix_lchflags__doc__
,
1899 "lchflags(path, flags)\n\n\
1901 This function will not follow symbolic links.");
1904 posix_lchflags(PyObject
*self
, PyObject
*args
)
1907 unsigned long flags
;
1909 if (!PyArg_ParseTuple(args
, "etk:lchflags",
1910 Py_FileSystemDefaultEncoding
, &path
, &flags
))
1912 Py_BEGIN_ALLOW_THREADS
1913 res
= lchflags(path
, flags
);
1914 Py_END_ALLOW_THREADS
1916 return posix_error_with_allocated_filename(path
);
1921 #endif /* HAVE_LCHFLAGS */
1924 PyDoc_STRVAR(posix_chroot__doc__
,
1926 Change root directory to path.");
1929 posix_chroot(PyObject
*self
, PyObject
*args
)
1931 return posix_1str(args
, "et:chroot", chroot
);
1936 PyDoc_STRVAR(posix_fsync__doc__
,
1938 force write of file with filedescriptor to disk.");
1941 posix_fsync(PyObject
*self
, PyObject
*fdobj
)
1943 return posix_fildes(fdobj
, fsync
);
1945 #endif /* HAVE_FSYNC */
1947 #ifdef HAVE_FDATASYNC
1950 extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
1953 PyDoc_STRVAR(posix_fdatasync__doc__
,
1954 "fdatasync(fildes)\n\n\
1955 force write of file with filedescriptor to disk.\n\
1956 does not force update of metadata.");
1959 posix_fdatasync(PyObject
*self
, PyObject
*fdobj
)
1961 return posix_fildes(fdobj
, fdatasync
);
1963 #endif /* HAVE_FDATASYNC */
1967 PyDoc_STRVAR(posix_chown__doc__
,
1968 "chown(path, uid, gid)\n\n\
1969 Change the owner and group id of path to the numeric uid and gid.");
1972 posix_chown(PyObject
*self
, PyObject
*args
)
1977 if (!PyArg_ParseTuple(args
, "etll:chown",
1978 Py_FileSystemDefaultEncoding
, &path
,
1981 Py_BEGIN_ALLOW_THREADS
1982 res
= chown(path
, (uid_t
) uid
, (gid_t
) gid
);
1983 Py_END_ALLOW_THREADS
1985 return posix_error_with_allocated_filename(path
);
1990 #endif /* HAVE_CHOWN */
1993 PyDoc_STRVAR(posix_fchown__doc__
,
1994 "fchown(fd, uid, gid)\n\n\
1995 Change the owner and group id of the file given by file descriptor\n\
1996 fd to the numeric uid and gid.");
1999 posix_fchown(PyObject
*self
, PyObject
*args
)
2003 if (!PyArg_ParseTuple(args
, "iii:chown", &fd
, &uid
, &gid
))
2005 Py_BEGIN_ALLOW_THREADS
2006 res
= fchown(fd
, (uid_t
) uid
, (gid_t
) gid
);
2007 Py_END_ALLOW_THREADS
2009 return posix_error();
2012 #endif /* HAVE_FCHOWN */
2015 PyDoc_STRVAR(posix_lchown__doc__
,
2016 "lchown(path, uid, gid)\n\n\
2017 Change the owner and group id of path to the numeric uid and gid.\n\
2018 This function will not follow symbolic links.");
2021 posix_lchown(PyObject
*self
, PyObject
*args
)
2026 if (!PyArg_ParseTuple(args
, "etii:lchown",
2027 Py_FileSystemDefaultEncoding
, &path
,
2030 Py_BEGIN_ALLOW_THREADS
2031 res
= lchown(path
, (uid_t
) uid
, (gid_t
) gid
);
2032 Py_END_ALLOW_THREADS
2034 return posix_error_with_allocated_filename(path
);
2039 #endif /* HAVE_LCHOWN */
2043 PyDoc_STRVAR(posix_getcwd__doc__
,
2044 "getcwd() -> path\n\n\
2045 Return a string representing the current working directory.");
2048 posix_getcwd(PyObject
*self
, PyObject
*noargs
)
2050 int bufsize_incr
= 1024;
2052 char *tmpbuf
= NULL
;
2054 PyObject
*dynamic_return
;
2056 Py_BEGIN_ALLOW_THREADS
2058 bufsize
= bufsize
+ bufsize_incr
;
2059 tmpbuf
= malloc(bufsize
);
2060 if (tmpbuf
== NULL
) {
2063 #if defined(PYOS_OS2) && defined(PYCC_GCC)
2064 res
= _getcwd2(tmpbuf
, bufsize
);
2066 res
= getcwd(tmpbuf
, bufsize
);
2072 } while ((res
== NULL
) && (errno
== ERANGE
));
2073 Py_END_ALLOW_THREADS
2076 return posix_error();
2078 dynamic_return
= PyString_FromString(tmpbuf
);
2081 return dynamic_return
;
2084 #ifdef Py_USING_UNICODE
2085 PyDoc_STRVAR(posix_getcwdu__doc__
,
2086 "getcwdu() -> path\n\n\
2087 Return a unicode string representing the current working directory.");
2090 posix_getcwdu(PyObject
*self
, PyObject
*noargs
)
2095 #ifdef Py_WIN_WIDE_FILENAMES
2097 if (unicode_file_names()) {
2099 wchar_t *wbuf2
= wbuf
;
2101 Py_BEGIN_ALLOW_THREADS
2102 len
= GetCurrentDirectoryW(sizeof wbuf
/ sizeof wbuf
[0], wbuf
);
2103 /* If the buffer is large enough, len does not include the
2104 terminating \0. If the buffer is too small, len includes
2105 the space needed for the terminator. */
2106 if (len
>= sizeof wbuf
/ sizeof wbuf
[0]) {
2107 wbuf2
= malloc(len
* sizeof(wchar_t));
2109 len
= GetCurrentDirectoryW(len
, wbuf2
);
2111 Py_END_ALLOW_THREADS
2117 if (wbuf2
!= wbuf
) free(wbuf2
);
2118 return win32_error("getcwdu", NULL
);
2120 resobj
= PyUnicode_FromWideChar(wbuf2
, len
);
2121 if (wbuf2
!= wbuf
) free(wbuf2
);
2126 Py_BEGIN_ALLOW_THREADS
2127 #if defined(PYOS_OS2) && defined(PYCC_GCC)
2128 res
= _getcwd2(buf
, sizeof buf
);
2130 res
= getcwd(buf
, sizeof buf
);
2132 Py_END_ALLOW_THREADS
2134 return posix_error();
2135 return PyUnicode_Decode(buf
, strlen(buf
), Py_FileSystemDefaultEncoding
,"strict");
2142 PyDoc_STRVAR(posix_link__doc__
,
2143 "link(src, dst)\n\n\
2144 Create a hard link to a file.");
2147 posix_link(PyObject
*self
, PyObject
*args
)
2149 return posix_2str(args
, "etet:link", link
);
2151 #endif /* HAVE_LINK */
2154 PyDoc_STRVAR(posix_listdir__doc__
,
2155 "listdir(path) -> list_of_strings\n\n\
2156 Return a list containing the names of the entries in the directory.\n\
2158 path: path of directory to list\n\
2160 The list is in arbitrary order. It does not include the special\n\
2161 entries '.' and '..' even if they are present in the directory.");
2164 posix_listdir(PyObject
*self
, PyObject
*args
)
2166 /* XXX Should redo this putting the (now four) versions of opendir
2167 in separate files instead of having them all here... */
2168 #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
2173 WIN32_FIND_DATA FileData
;
2174 char namebuf
[MAX_PATH
+5]; /* Overallocate for \\*.*\0 */
2175 char *bufptr
= namebuf
;
2176 Py_ssize_t len
= sizeof(namebuf
)-5; /* only claim to have space for MAX_PATH */
2178 #ifdef Py_WIN_WIDE_FILENAMES
2179 /* If on wide-character-capable OS see if argument
2180 is Unicode and if so use wide API. */
2181 if (unicode_file_names()) {
2183 if (PyArg_ParseTuple(args
, "U:listdir", &po
)) {
2184 WIN32_FIND_DATAW wFileData
;
2185 Py_UNICODE
*wnamebuf
;
2187 /* Overallocate for \\*.*\0 */
2188 len
= PyUnicode_GET_SIZE(po
);
2189 wnamebuf
= malloc((len
+ 5) * sizeof(wchar_t));
2194 wcscpy(wnamebuf
, PyUnicode_AS_UNICODE(po
));
2195 wch
= len
> 0 ? wnamebuf
[len
-1] : '\0';
2196 if (wch
!= L
'/' && wch
!= L
'\\' && wch
!= L
':')
2197 wnamebuf
[len
++] = L
'\\';
2198 wcscpy(wnamebuf
+ len
, L
"*.*");
2199 if ((d
= PyList_New(0)) == NULL
) {
2203 hFindFile
= FindFirstFileW(wnamebuf
, &wFileData
);
2204 if (hFindFile
== INVALID_HANDLE_VALUE
) {
2205 int error
= GetLastError();
2206 if (error
== ERROR_FILE_NOT_FOUND
) {
2211 win32_error_unicode("FindFirstFileW", wnamebuf
);
2216 /* Skip over . and .. */
2217 if (wcscmp(wFileData
.cFileName
, L
".") != 0 &&
2218 wcscmp(wFileData
.cFileName
, L
"..") != 0) {
2219 v
= PyUnicode_FromUnicode(wFileData
.cFileName
, wcslen(wFileData
.cFileName
));
2225 if (PyList_Append(d
, v
) != 0) {
2233 Py_BEGIN_ALLOW_THREADS
2234 result
= FindNextFileW(hFindFile
, &wFileData
);
2235 Py_END_ALLOW_THREADS
2236 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2237 it got to the end of the directory. */
2238 if (!result
&& GetLastError() != ERROR_NO_MORE_FILES
) {
2240 win32_error_unicode("FindNextFileW", wnamebuf
);
2241 FindClose(hFindFile
);
2245 } while (result
== TRUE
);
2247 if (FindClose(hFindFile
) == FALSE
) {
2249 win32_error_unicode("FindClose", wnamebuf
);
2256 /* Drop the argument parsing error as narrow strings
2262 if (!PyArg_ParseTuple(args
, "et#:listdir",
2263 Py_FileSystemDefaultEncoding
, &bufptr
, &len
))
2266 char ch
= namebuf
[len
-1];
2267 if (ch
!= SEP
&& ch
!= ALTSEP
&& ch
!= ':')
2268 namebuf
[len
++] = '/';
2270 strcpy(namebuf
+ len
, "*.*");
2272 if ((d
= PyList_New(0)) == NULL
)
2275 hFindFile
= FindFirstFile(namebuf
, &FileData
);
2276 if (hFindFile
== INVALID_HANDLE_VALUE
) {
2277 int error
= GetLastError();
2278 if (error
== ERROR_FILE_NOT_FOUND
)
2281 return win32_error("FindFirstFile", namebuf
);
2284 /* Skip over . and .. */
2285 if (strcmp(FileData
.cFileName
, ".") != 0 &&
2286 strcmp(FileData
.cFileName
, "..") != 0) {
2287 v
= PyString_FromString(FileData
.cFileName
);
2293 if (PyList_Append(d
, v
) != 0) {
2301 Py_BEGIN_ALLOW_THREADS
2302 result
= FindNextFile(hFindFile
, &FileData
);
2303 Py_END_ALLOW_THREADS
2304 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2305 it got to the end of the directory. */
2306 if (!result
&& GetLastError() != ERROR_NO_MORE_FILES
) {
2308 win32_error("FindNextFile", namebuf
);
2309 FindClose(hFindFile
);
2312 } while (result
== TRUE
);
2314 if (FindClose(hFindFile
) == FALSE
) {
2316 return win32_error("FindClose", namebuf
);
2321 #elif defined(PYOS_OS2)
2324 #define MAX_PATH CCHMAXPATH
2329 char namebuf
[MAX_PATH
+5];
2335 if (!PyArg_ParseTuple(args
, "t#:listdir", &name
, &len
))
2337 if (len
>= MAX_PATH
) {
2338 PyErr_SetString(PyExc_ValueError
, "path too long");
2341 strcpy(namebuf
, name
);
2342 for (pt
= namebuf
; *pt
; pt
++)
2345 if (namebuf
[len
-1] != SEP
)
2346 namebuf
[len
++] = SEP
;
2347 strcpy(namebuf
+ len
, "*.*");
2349 if ((d
= PyList_New(0)) == NULL
)
2352 rc
= DosFindFirst(namebuf
, /* Wildcard Pattern to Match */
2353 &hdir
, /* Handle to Use While Search Directory */
2354 FILE_READONLY
| FILE_HIDDEN
| FILE_SYSTEM
| FILE_DIRECTORY
,
2355 &ep
, sizeof(ep
), /* Structure to Receive Directory Entry */
2356 &srchcnt
, /* Max and Actual Count of Entries Per Iteration */
2357 FIL_STANDARD
); /* Format of Entry (EAs or Not) */
2359 if (rc
!= NO_ERROR
) {
2361 return posix_error_with_filename(name
);
2364 if (srchcnt
> 0) { /* If Directory is NOT Totally Empty, */
2366 if (ep
.achName
[0] == '.'
2367 && (ep
.achName
[1] == '\0' || (ep
.achName
[1] == '.' && ep
.achName
[2] == '\0')))
2368 continue; /* Skip Over "." and ".." Names */
2370 strcpy(namebuf
, ep
.achName
);
2372 /* Leave Case of Name Alone -- In Native Form */
2373 /* (Removed Forced Lowercasing Code) */
2375 v
= PyString_FromString(namebuf
);
2381 if (PyList_Append(d
, v
) != 0) {
2388 } while (DosFindNext(hdir
, &ep
, sizeof(ep
), &srchcnt
) == NO_ERROR
&& srchcnt
> 0);
2398 int arg_is_unicode
= 1;
2401 if (!PyArg_ParseTuple(args
, "U:listdir", &v
)) {
2405 if (!PyArg_ParseTuple(args
, "et:listdir", Py_FileSystemDefaultEncoding
, &name
))
2407 if ((dirp
= opendir(name
)) == NULL
) {
2408 return posix_error_with_allocated_filename(name
);
2410 if ((d
= PyList_New(0)) == NULL
) {
2417 Py_BEGIN_ALLOW_THREADS
2419 Py_END_ALLOW_THREADS
2426 return posix_error_with_allocated_filename(name
);
2429 if (ep
->d_name
[0] == '.' &&
2431 (ep
->d_name
[1] == '.' && NAMLEN(ep
) == 2)))
2433 v
= PyString_FromStringAndSize(ep
->d_name
, NAMLEN(ep
));
2439 #ifdef Py_USING_UNICODE
2440 if (arg_is_unicode
) {
2443 w
= PyUnicode_FromEncodedObject(v
,
2444 Py_FileSystemDefaultEncoding
,
2451 /* fall back to the original byte string, as
2452 discussed in patch #683592 */
2457 if (PyList_Append(d
, v
) != 0) {
2470 #endif /* which OS */
2471 } /* end of posix_listdir */
2474 /* A helper function for abspath on win32 */
2476 posix__getfullpathname(PyObject
*self
, PyObject
*args
)
2478 /* assume encoded strings won't more than double no of chars */
2479 char inbuf
[MAX_PATH
*2];
2480 char *inbufp
= inbuf
;
2481 Py_ssize_t insize
= sizeof(inbuf
);
2482 char outbuf
[MAX_PATH
*2];
2484 #ifdef Py_WIN_WIDE_FILENAMES
2485 if (unicode_file_names()) {
2486 PyUnicodeObject
*po
;
2487 if (PyArg_ParseTuple(args
, "U|:_getfullpathname", &po
)) {
2488 Py_UNICODE
*wpath
= PyUnicode_AS_UNICODE(po
);
2489 Py_UNICODE woutbuf
[MAX_PATH
*2], *woutbufp
= woutbuf
;
2493 result
= GetFullPathNameW(wpath
,
2494 sizeof(woutbuf
)/sizeof(woutbuf
[0]),
2496 if (result
> sizeof(woutbuf
)/sizeof(woutbuf
[0])) {
2497 woutbufp
= malloc(result
* sizeof(Py_UNICODE
));
2499 return PyErr_NoMemory();
2500 result
= GetFullPathNameW(wpath
, result
, woutbufp
, &wtemp
);
2503 v
= PyUnicode_FromUnicode(woutbufp
, wcslen(woutbufp
));
2505 v
= win32_error_unicode("GetFullPathNameW", wpath
);
2506 if (woutbufp
!= woutbuf
)
2510 /* Drop the argument parsing error as narrow strings
2515 if (!PyArg_ParseTuple (args
, "et#:_getfullpathname",
2516 Py_FileSystemDefaultEncoding
, &inbufp
,
2519 if (!GetFullPathName(inbuf
, sizeof(outbuf
)/sizeof(outbuf
[0]),
2521 return win32_error("GetFullPathName", inbuf
);
2522 if (PyUnicode_Check(PyTuple_GetItem(args
, 0))) {
2523 return PyUnicode_Decode(outbuf
, strlen(outbuf
),
2524 Py_FileSystemDefaultEncoding
, NULL
);
2526 return PyString_FromString(outbuf
);
2527 } /* end of posix__getfullpathname */
2528 #endif /* MS_WINDOWS */
2530 PyDoc_STRVAR(posix_mkdir__doc__
,
2531 "mkdir(path [, mode=0777])\n\n\
2532 Create a directory.");
2535 posix_mkdir(PyObject
*self
, PyObject
*args
)
2541 #ifdef Py_WIN_WIDE_FILENAMES
2542 if (unicode_file_names()) {
2543 PyUnicodeObject
*po
;
2544 if (PyArg_ParseTuple(args
, "U|i:mkdir", &po
, &mode
)) {
2545 Py_BEGIN_ALLOW_THREADS
2546 /* PyUnicode_AS_UNICODE OK without thread lock as
2547 it is a simple dereference. */
2548 res
= CreateDirectoryW(PyUnicode_AS_UNICODE(po
), NULL
);
2549 Py_END_ALLOW_THREADS
2551 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po
));
2555 /* Drop the argument parsing error as narrow strings
2559 if (!PyArg_ParseTuple(args
, "et|i:mkdir",
2560 Py_FileSystemDefaultEncoding
, &path
, &mode
))
2562 Py_BEGIN_ALLOW_THREADS
2563 /* PyUnicode_AS_UNICODE OK without thread lock as
2564 it is a simple dereference. */
2565 res
= CreateDirectoryA(path
, NULL
);
2566 Py_END_ALLOW_THREADS
2568 win32_error("mkdir", path
);
2577 if (!PyArg_ParseTuple(args
, "et|i:mkdir",
2578 Py_FileSystemDefaultEncoding
, &path
, &mode
))
2580 Py_BEGIN_ALLOW_THREADS
2581 #if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
2584 res
= mkdir(path
, mode
);
2586 Py_END_ALLOW_THREADS
2588 return posix_error_with_allocated_filename(path
);
2596 /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2597 #if defined(HAVE_SYS_RESOURCE_H)
2598 #include <sys/resource.h>
2603 PyDoc_STRVAR(posix_nice__doc__
,
2604 "nice(inc) -> new_priority\n\n\
2605 Decrease the priority of process by inc and return the new priority.");
2608 posix_nice(PyObject
*self
, PyObject
*args
)
2610 int increment
, value
;
2612 if (!PyArg_ParseTuple(args
, "i:nice", &increment
))
2615 /* There are two flavours of 'nice': one that returns the new
2616 priority (as required by almost all standards out there) and the
2617 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
2618 the use of getpriority() to get the new priority.
2620 If we are of the nice family that returns the new priority, we
2621 need to clear errno before the call, and check if errno is filled
2622 before calling posix_error() on a returnvalue of -1, because the
2623 -1 may be the actual new priority! */
2626 value
= nice(increment
);
2627 #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
2629 value
= getpriority(PRIO_PROCESS
, 0);
2631 if (value
== -1 && errno
!= 0)
2632 /* either nice() or getpriority() returned an error */
2633 return posix_error();
2634 return PyInt_FromLong((long) value
);
2636 #endif /* HAVE_NICE */
2638 PyDoc_STRVAR(posix_rename__doc__
,
2639 "rename(old, new)\n\n\
2640 Rename a file or directory.");
2643 posix_rename(PyObject
*self
, PyObject
*args
)
2649 if (unicode_file_names()) {
2650 if (!PyArg_ParseTuple(args
, "OO:rename", &o1
, &o2
))
2652 if (!convert_to_unicode(&o1
))
2654 if (!convert_to_unicode(&o2
)) {
2658 Py_BEGIN_ALLOW_THREADS
2659 result
= MoveFileW(PyUnicode_AsUnicode(o1
),
2660 PyUnicode_AsUnicode(o2
));
2661 Py_END_ALLOW_THREADS
2665 return win32_error("rename", NULL
);
2671 if (!PyArg_ParseTuple(args
, "ss:rename", &p1
, &p2
))
2673 Py_BEGIN_ALLOW_THREADS
2674 result
= MoveFileA(p1
, p2
);
2675 Py_END_ALLOW_THREADS
2677 return win32_error("rename", NULL
);
2681 return posix_2str(args
, "etet:rename", rename
);
2686 PyDoc_STRVAR(posix_rmdir__doc__
,
2688 Remove a directory.");
2691 posix_rmdir(PyObject
*self
, PyObject
*args
)
2694 return win32_1str(args
, "rmdir", "s:rmdir", RemoveDirectoryA
, "U:rmdir", RemoveDirectoryW
);
2696 return posix_1str(args
, "et:rmdir", rmdir
);
2701 PyDoc_STRVAR(posix_stat__doc__
,
2702 "stat(path) -> stat result\n\n\
2703 Perform a stat system call on the given path.");
2706 posix_stat(PyObject
*self
, PyObject
*args
)
2709 return posix_do_stat(self
, args
, "et:stat", STAT
, "U:stat", win32_wstat
);
2711 return posix_do_stat(self
, args
, "et:stat", STAT
, NULL
, NULL
);
2717 PyDoc_STRVAR(posix_system__doc__
,
2718 "system(command) -> exit_status\n\n\
2719 Execute the command (a string) in a subshell.");
2722 posix_system(PyObject
*self
, PyObject
*args
)
2726 if (!PyArg_ParseTuple(args
, "s:system", &command
))
2728 Py_BEGIN_ALLOW_THREADS
2729 sts
= system(command
);
2730 Py_END_ALLOW_THREADS
2731 return PyInt_FromLong(sts
);
2736 PyDoc_STRVAR(posix_umask__doc__
,
2737 "umask(new_mask) -> old_mask\n\n\
2738 Set the current numeric umask and return the previous umask.");
2741 posix_umask(PyObject
*self
, PyObject
*args
)
2744 if (!PyArg_ParseTuple(args
, "i:umask", &i
))
2748 return posix_error();
2749 return PyInt_FromLong((long)i
);
2753 PyDoc_STRVAR(posix_unlink__doc__
,
2755 Remove a file (same as remove(path)).");
2757 PyDoc_STRVAR(posix_remove__doc__
,
2759 Remove a file (same as unlink(path)).");
2762 posix_unlink(PyObject
*self
, PyObject
*args
)
2765 return win32_1str(args
, "remove", "s:remove", DeleteFileA
, "U:remove", DeleteFileW
);
2767 return posix_1str(args
, "et:remove", unlink
);
2773 PyDoc_STRVAR(posix_uname__doc__
,
2774 "uname() -> (sysname, nodename, release, version, machine)\n\n\
2775 Return a tuple identifying the current operating system.");
2778 posix_uname(PyObject
*self
, PyObject
*noargs
)
2783 Py_BEGIN_ALLOW_THREADS
2785 Py_END_ALLOW_THREADS
2787 return posix_error();
2788 return Py_BuildValue("(sssss)",
2795 #endif /* HAVE_UNAME */
2798 extract_time(PyObject
*t
, long* sec
, long* usec
)
2801 if (PyFloat_Check(t
)) {
2802 double tval
= PyFloat_AsDouble(t
);
2803 PyObject
*intobj
= Py_TYPE(t
)->tp_as_number
->nb_int(t
);
2806 intval
= PyInt_AsLong(intobj
);
2808 if (intval
== -1 && PyErr_Occurred())
2811 *usec
= (long)((tval
- intval
) * 1e6
); /* can't exceed 1000000 */
2813 /* If rounding gave us a negative number,
2818 intval
= PyInt_AsLong(t
);
2819 if (intval
== -1 && PyErr_Occurred())
2826 PyDoc_STRVAR(posix_utime__doc__
,
2827 "utime(path, (atime, mtime))\n\
2828 utime(path, None)\n\n\
2829 Set the access and modified time of the file to the given values. If the\n\
2830 second form is used, set the access and modified times to the current time.");
2833 posix_utime(PyObject
*self
, PyObject
*args
)
2835 #ifdef Py_WIN_WIDE_FILENAMES
2837 PyUnicodeObject
*obwpath
;
2838 wchar_t *wpath
= NULL
;
2841 long atimesec
, mtimesec
, ausec
, musec
;
2842 FILETIME atime
, mtime
;
2843 PyObject
*result
= NULL
;
2845 if (unicode_file_names()) {
2846 if (PyArg_ParseTuple(args
, "UO|:utime", &obwpath
, &arg
)) {
2847 wpath
= PyUnicode_AS_UNICODE(obwpath
);
2848 Py_BEGIN_ALLOW_THREADS
2849 hFile
= CreateFileW(wpath
, FILE_WRITE_ATTRIBUTES
, 0,
2850 NULL
, OPEN_EXISTING
,
2851 FILE_FLAG_BACKUP_SEMANTICS
, NULL
);
2852 Py_END_ALLOW_THREADS
2853 if (hFile
== INVALID_HANDLE_VALUE
)
2854 return win32_error_unicode("utime", wpath
);
2856 /* Drop the argument parsing error as narrow strings
2861 if (!PyArg_ParseTuple(args
, "etO:utime",
2862 Py_FileSystemDefaultEncoding
, &apath
, &arg
))
2864 Py_BEGIN_ALLOW_THREADS
2865 hFile
= CreateFileA(apath
, FILE_WRITE_ATTRIBUTES
, 0,
2866 NULL
, OPEN_EXISTING
,
2867 FILE_FLAG_BACKUP_SEMANTICS
, NULL
);
2868 Py_END_ALLOW_THREADS
2869 if (hFile
== INVALID_HANDLE_VALUE
) {
2870 win32_error("utime", apath
);
2877 if (arg
== Py_None
) {
2879 GetSystemTime(&now
);
2880 if (!SystemTimeToFileTime(&now
, &mtime
) ||
2881 !SystemTimeToFileTime(&now
, &atime
)) {
2882 win32_error("utime", NULL
);
2886 else if (!PyTuple_Check(arg
) || PyTuple_Size(arg
) != 2) {
2887 PyErr_SetString(PyExc_TypeError
,
2888 "utime() arg 2 must be a tuple (atime, mtime)");
2892 if (extract_time(PyTuple_GET_ITEM(arg
, 0),
2893 &atimesec
, &ausec
) == -1)
2895 time_t_to_FILE_TIME(atimesec
, 1000*ausec
, &atime
);
2896 if (extract_time(PyTuple_GET_ITEM(arg
, 1),
2897 &mtimesec
, &musec
) == -1)
2899 time_t_to_FILE_TIME(mtimesec
, 1000*musec
, &mtime
);
2901 if (!SetFileTime(hFile
, NULL
, &atime
, &mtime
)) {
2902 /* Avoid putting the file name into the error here,
2903 as that may confuse the user into believing that
2904 something is wrong with the file, when it also
2905 could be the time stamp that gives a problem. */
2906 win32_error("utime", NULL
);
2913 #else /* Py_WIN_WIDE_FILENAMES */
2916 long atime
, mtime
, ausec
, musec
;
2920 #if defined(HAVE_UTIMES)
2921 struct timeval buf
[2];
2922 #define ATIME buf[0].tv_sec
2923 #define MTIME buf[1].tv_sec
2924 #elif defined(HAVE_UTIME_H)
2925 /* XXX should define struct utimbuf instead, above */
2927 #define ATIME buf.actime
2928 #define MTIME buf.modtime
2929 #define UTIME_ARG &buf
2930 #else /* HAVE_UTIMES */
2932 #define ATIME buf[0]
2933 #define MTIME buf[1]
2934 #define UTIME_ARG buf
2935 #endif /* HAVE_UTIMES */
2938 if (!PyArg_ParseTuple(args
, "etO:utime",
2939 Py_FileSystemDefaultEncoding
, &path
, &arg
))
2941 if (arg
== Py_None
) {
2942 /* optional time values not given */
2943 Py_BEGIN_ALLOW_THREADS
2944 res
= utime(path
, NULL
);
2945 Py_END_ALLOW_THREADS
2947 else if (!PyTuple_Check(arg
) || PyTuple_Size(arg
) != 2) {
2948 PyErr_SetString(PyExc_TypeError
,
2949 "utime() arg 2 must be a tuple (atime, mtime)");
2954 if (extract_time(PyTuple_GET_ITEM(arg
, 0),
2955 &atime
, &ausec
) == -1) {
2959 if (extract_time(PyTuple_GET_ITEM(arg
, 1),
2960 &mtime
, &musec
) == -1) {
2967 buf
[0].tv_usec
= ausec
;
2968 buf
[1].tv_usec
= musec
;
2969 Py_BEGIN_ALLOW_THREADS
2970 res
= utimes(path
, buf
);
2971 Py_END_ALLOW_THREADS
2973 Py_BEGIN_ALLOW_THREADS
2974 res
= utime(path
, UTIME_ARG
);
2975 Py_END_ALLOW_THREADS
2976 #endif /* HAVE_UTIMES */
2979 return posix_error_with_allocated_filename(path
);
2987 #endif /* Py_WIN_WIDE_FILENAMES */
2991 /* Process operations */
2993 PyDoc_STRVAR(posix__exit__doc__
,
2995 Exit to the system with specified status, without normal exit processing.");
2998 posix__exit(PyObject
*self
, PyObject
*args
)
3001 if (!PyArg_ParseTuple(args
, "i:_exit", &sts
))
3004 return NULL
; /* Make gcc -Wall happy */
3007 #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
3009 free_string_array(char **array
, Py_ssize_t count
)
3012 for (i
= 0; i
< count
; i
++)
3013 PyMem_Free(array
[i
]);
3020 PyDoc_STRVAR(posix_execv__doc__
,
3021 "execv(path, args)\n\n\
3022 Execute an executable path with arguments, replacing current process.\n\
3024 path: path of executable file\n\
3025 args: tuple or list of strings");
3028 posix_execv(PyObject
*self
, PyObject
*args
)
3034 PyObject
*(*getitem
)(PyObject
*, Py_ssize_t
);
3036 /* execv has two arguments: (path, argv), where
3037 argv is a list or tuple of strings. */
3039 if (!PyArg_ParseTuple(args
, "etO:execv",
3040 Py_FileSystemDefaultEncoding
,
3043 if (PyList_Check(argv
)) {
3044 argc
= PyList_Size(argv
);
3045 getitem
= PyList_GetItem
;
3047 else if (PyTuple_Check(argv
)) {
3048 argc
= PyTuple_Size(argv
);
3049 getitem
= PyTuple_GetItem
;
3052 PyErr_SetString(PyExc_TypeError
, "execv() arg 2 must be a tuple or list");
3057 argvlist
= PyMem_NEW(char *, argc
+1);
3058 if (argvlist
== NULL
) {
3060 return PyErr_NoMemory();
3062 for (i
= 0; i
< argc
; i
++) {
3063 if (!PyArg_Parse((*getitem
)(argv
, i
), "et",
3064 Py_FileSystemDefaultEncoding
,
3066 free_string_array(argvlist
, i
);
3067 PyErr_SetString(PyExc_TypeError
,
3068 "execv() arg 2 must contain only strings");
3074 argvlist
[argc
] = NULL
;
3076 execv(path
, argvlist
);
3078 /* If we get here it's definitely an error */
3080 free_string_array(argvlist
, argc
);
3082 return posix_error();
3086 PyDoc_STRVAR(posix_execve__doc__
,
3087 "execve(path, args, env)\n\n\
3088 Execute a path with arguments and environment, replacing current process.\n\
3090 path: path of executable file\n\
3091 args: tuple or list of arguments\n\
3092 env: dictionary of strings mapping to strings");
3095 posix_execve(PyObject
*self
, PyObject
*args
)
3098 PyObject
*argv
, *env
;
3101 PyObject
*key
, *val
, *keys
=NULL
, *vals
=NULL
;
3102 Py_ssize_t i
, pos
, argc
, envc
;
3103 PyObject
*(*getitem
)(PyObject
*, Py_ssize_t
);
3104 Py_ssize_t lastarg
= 0;
3106 /* execve has three arguments: (path, argv, env), where
3107 argv is a list or tuple of strings and env is a dictionary
3108 like posix.environ. */
3110 if (!PyArg_ParseTuple(args
, "etOO:execve",
3111 Py_FileSystemDefaultEncoding
,
3112 &path
, &argv
, &env
))
3114 if (PyList_Check(argv
)) {
3115 argc
= PyList_Size(argv
);
3116 getitem
= PyList_GetItem
;
3118 else if (PyTuple_Check(argv
)) {
3119 argc
= PyTuple_Size(argv
);
3120 getitem
= PyTuple_GetItem
;
3123 PyErr_SetString(PyExc_TypeError
,
3124 "execve() arg 2 must be a tuple or list");
3127 if (!PyMapping_Check(env
)) {
3128 PyErr_SetString(PyExc_TypeError
,
3129 "execve() arg 3 must be a mapping object");
3133 argvlist
= PyMem_NEW(char *, argc
+1);
3134 if (argvlist
== NULL
) {
3138 for (i
= 0; i
< argc
; i
++) {
3139 if (!PyArg_Parse((*getitem
)(argv
, i
),
3140 "et;execve() arg 2 must contain only strings",
3141 Py_FileSystemDefaultEncoding
,
3149 argvlist
[argc
] = NULL
;
3151 i
= PyMapping_Size(env
);
3154 envlist
= PyMem_NEW(char *, i
+ 1);
3155 if (envlist
== NULL
) {
3160 keys
= PyMapping_Keys(env
);
3161 vals
= PyMapping_Values(env
);
3164 if (!PyList_Check(keys
) || !PyList_Check(vals
)) {
3165 PyErr_SetString(PyExc_TypeError
,
3166 "execve(): env.keys() or env.values() is not a list");
3170 for (pos
= 0; pos
< i
; pos
++) {
3174 key
= PyList_GetItem(keys
, pos
);
3175 val
= PyList_GetItem(vals
, pos
);
3181 "s;execve() arg 3 contains a non-string key",
3185 "s;execve() arg 3 contains a non-string value",
3191 #if defined(PYOS_OS2)
3192 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3193 if (stricmp(k
, "BEGINLIBPATH") != 0 && stricmp(k
, "ENDLIBPATH") != 0) {
3195 len
= PyString_Size(key
) + PyString_Size(val
) + 2;
3196 p
= PyMem_NEW(char, len
);
3201 PyOS_snprintf(p
, len
, "%s=%s", k
, v
);
3202 envlist
[envc
++] = p
;
3203 #if defined(PYOS_OS2)
3209 execve(path
, argvlist
, envlist
);
3211 /* If we get here it's definitely an error */
3213 (void) posix_error();
3217 PyMem_DEL(envlist
[envc
]);
3220 free_string_array(argvlist
, lastarg
);
3227 #endif /* HAVE_EXECV */
3231 PyDoc_STRVAR(posix_spawnv__doc__
,
3232 "spawnv(mode, path, args)\n\n\
3233 Execute the program 'path' in a new process.\n\
3235 mode: mode of process creation\n\
3236 path: path of executable file\n\
3237 args: tuple or list of strings");
3240 posix_spawnv(PyObject
*self
, PyObject
*args
)
3247 Py_intptr_t spawnval
;
3248 PyObject
*(*getitem
)(PyObject
*, Py_ssize_t
);
3250 /* spawnv has three arguments: (mode, path, argv), where
3251 argv is a list or tuple of strings. */
3253 if (!PyArg_ParseTuple(args
, "ietO:spawnv", &mode
,
3254 Py_FileSystemDefaultEncoding
,
3257 if (PyList_Check(argv
)) {
3258 argc
= PyList_Size(argv
);
3259 getitem
= PyList_GetItem
;
3261 else if (PyTuple_Check(argv
)) {
3262 argc
= PyTuple_Size(argv
);
3263 getitem
= PyTuple_GetItem
;
3266 PyErr_SetString(PyExc_TypeError
,
3267 "spawnv() arg 2 must be a tuple or list");
3272 argvlist
= PyMem_NEW(char *, argc
+1);
3273 if (argvlist
== NULL
) {
3275 return PyErr_NoMemory();
3277 for (i
= 0; i
< argc
; i
++) {
3278 if (!PyArg_Parse((*getitem
)(argv
, i
), "et",
3279 Py_FileSystemDefaultEncoding
,
3281 free_string_array(argvlist
, i
);
3284 "spawnv() arg 2 must contain only strings");
3289 argvlist
[argc
] = NULL
;
3291 #if defined(PYOS_OS2) && defined(PYCC_GCC)
3292 Py_BEGIN_ALLOW_THREADS
3293 spawnval
= spawnv(mode
, path
, argvlist
);
3294 Py_END_ALLOW_THREADS
3296 if (mode
== _OLD_P_OVERLAY
)
3299 Py_BEGIN_ALLOW_THREADS
3300 spawnval
= _spawnv(mode
, path
, argvlist
);
3301 Py_END_ALLOW_THREADS
3304 free_string_array(argvlist
, argc
);
3308 return posix_error();
3310 #if SIZEOF_LONG == SIZEOF_VOID_P
3311 return Py_BuildValue("l", (long) spawnval
);
3313 return Py_BuildValue("L", (PY_LONG_LONG
) spawnval
);
3318 PyDoc_STRVAR(posix_spawnve__doc__
,
3319 "spawnve(mode, path, args, env)\n\n\
3320 Execute the program 'path' in a new process.\n\
3322 mode: mode of process creation\n\
3323 path: path of executable file\n\
3324 args: tuple or list of arguments\n\
3325 env: dictionary of strings mapping to strings");
3328 posix_spawnve(PyObject
*self
, PyObject
*args
)
3331 PyObject
*argv
, *env
;
3334 PyObject
*key
, *val
, *keys
=NULL
, *vals
=NULL
, *res
=NULL
;
3335 int mode
, pos
, envc
;
3337 Py_intptr_t spawnval
;
3338 PyObject
*(*getitem
)(PyObject
*, Py_ssize_t
);
3339 Py_ssize_t lastarg
= 0;
3341 /* spawnve has four arguments: (mode, path, argv, env), where
3342 argv is a list or tuple of strings and env is a dictionary
3343 like posix.environ. */
3345 if (!PyArg_ParseTuple(args
, "ietOO:spawnve", &mode
,
3346 Py_FileSystemDefaultEncoding
,
3347 &path
, &argv
, &env
))
3349 if (PyList_Check(argv
)) {
3350 argc
= PyList_Size(argv
);
3351 getitem
= PyList_GetItem
;
3353 else if (PyTuple_Check(argv
)) {
3354 argc
= PyTuple_Size(argv
);
3355 getitem
= PyTuple_GetItem
;
3358 PyErr_SetString(PyExc_TypeError
,
3359 "spawnve() arg 2 must be a tuple or list");
3362 if (!PyMapping_Check(env
)) {
3363 PyErr_SetString(PyExc_TypeError
,
3364 "spawnve() arg 3 must be a mapping object");
3368 argvlist
= PyMem_NEW(char *, argc
+1);
3369 if (argvlist
== NULL
) {
3373 for (i
= 0; i
< argc
; i
++) {
3374 if (!PyArg_Parse((*getitem
)(argv
, i
),
3375 "et;spawnve() arg 2 must contain only strings",
3376 Py_FileSystemDefaultEncoding
,
3384 argvlist
[argc
] = NULL
;
3386 i
= PyMapping_Size(env
);
3389 envlist
= PyMem_NEW(char *, i
+ 1);
3390 if (envlist
== NULL
) {
3395 keys
= PyMapping_Keys(env
);
3396 vals
= PyMapping_Values(env
);
3399 if (!PyList_Check(keys
) || !PyList_Check(vals
)) {
3400 PyErr_SetString(PyExc_TypeError
,
3401 "spawnve(): env.keys() or env.values() is not a list");
3405 for (pos
= 0; pos
< i
; pos
++) {
3409 key
= PyList_GetItem(keys
, pos
);
3410 val
= PyList_GetItem(vals
, pos
);
3416 "s;spawnve() arg 3 contains a non-string key",
3420 "s;spawnve() arg 3 contains a non-string value",
3425 len
= PyString_Size(key
) + PyString_Size(val
) + 2;
3426 p
= PyMem_NEW(char, len
);
3431 PyOS_snprintf(p
, len
, "%s=%s", k
, v
);
3432 envlist
[envc
++] = p
;
3436 #if defined(PYOS_OS2) && defined(PYCC_GCC)
3437 Py_BEGIN_ALLOW_THREADS
3438 spawnval
= spawnve(mode
, path
, argvlist
, envlist
);
3439 Py_END_ALLOW_THREADS
3441 if (mode
== _OLD_P_OVERLAY
)
3444 Py_BEGIN_ALLOW_THREADS
3445 spawnval
= _spawnve(mode
, path
, argvlist
, envlist
);
3446 Py_END_ALLOW_THREADS
3450 (void) posix_error();
3452 #if SIZEOF_LONG == SIZEOF_VOID_P
3453 res
= Py_BuildValue("l", (long) spawnval
);
3455 res
= Py_BuildValue("L", (PY_LONG_LONG
) spawnval
);
3460 PyMem_DEL(envlist
[envc
]);
3463 free_string_array(argvlist
, lastarg
);
3471 /* OS/2 supports spawnvp & spawnvpe natively */
3472 #if defined(PYOS_OS2)
3473 PyDoc_STRVAR(posix_spawnvp__doc__
,
3474 "spawnvp(mode, file, args)\n\n\
3475 Execute the program 'file' in a new process, using the environment\n\
3476 search path to find the file.\n\
3478 mode: mode of process creation\n\
3479 file: executable file name\n\
3480 args: tuple or list of strings");
3483 posix_spawnvp(PyObject
*self
, PyObject
*args
)
3489 Py_intptr_t spawnval
;
3490 PyObject
*(*getitem
)(PyObject
*, Py_ssize_t
);
3492 /* spawnvp has three arguments: (mode, path, argv), where
3493 argv is a list or tuple of strings. */
3495 if (!PyArg_ParseTuple(args
, "ietO:spawnvp", &mode
,
3496 Py_FileSystemDefaultEncoding
,
3499 if (PyList_Check(argv
)) {
3500 argc
= PyList_Size(argv
);
3501 getitem
= PyList_GetItem
;
3503 else if (PyTuple_Check(argv
)) {
3504 argc
= PyTuple_Size(argv
);
3505 getitem
= PyTuple_GetItem
;
3508 PyErr_SetString(PyExc_TypeError
,
3509 "spawnvp() arg 2 must be a tuple or list");
3514 argvlist
= PyMem_NEW(char *, argc
+1);
3515 if (argvlist
== NULL
) {
3517 return PyErr_NoMemory();
3519 for (i
= 0; i
< argc
; i
++) {
3520 if (!PyArg_Parse((*getitem
)(argv
, i
), "et",
3521 Py_FileSystemDefaultEncoding
,
3523 free_string_array(argvlist
, i
);
3526 "spawnvp() arg 2 must contain only strings");
3531 argvlist
[argc
] = NULL
;
3533 Py_BEGIN_ALLOW_THREADS
3534 #if defined(PYCC_GCC)
3535 spawnval
= spawnvp(mode
, path
, argvlist
);
3537 spawnval
= _spawnvp(mode
, path
, argvlist
);
3539 Py_END_ALLOW_THREADS
3541 free_string_array(argvlist
, argc
);
3545 return posix_error();
3547 return Py_BuildValue("l", (long) spawnval
);
3551 PyDoc_STRVAR(posix_spawnvpe__doc__
,
3552 "spawnvpe(mode, file, args, env)\n\n\
3553 Execute the program 'file' in a new process, using the environment\n\
3554 search path to find the file.\n\
3556 mode: mode of process creation\n\
3557 file: executable file name\n\
3558 args: tuple or list of arguments\n\
3559 env: dictionary of strings mapping to strings");
3562 posix_spawnvpe(PyObject
*self
, PyObject
*args
)
3565 PyObject
*argv
, *env
;
3568 PyObject
*key
, *val
, *keys
=NULL
, *vals
=NULL
, *res
=NULL
;
3569 int mode
, i
, pos
, argc
, envc
;
3570 Py_intptr_t spawnval
;
3571 PyObject
*(*getitem
)(PyObject
*, Py_ssize_t
);
3574 /* spawnvpe has four arguments: (mode, path, argv, env), where
3575 argv is a list or tuple of strings and env is a dictionary
3576 like posix.environ. */
3578 if (!PyArg_ParseTuple(args
, "ietOO:spawnvpe", &mode
,
3579 Py_FileSystemDefaultEncoding
,
3580 &path
, &argv
, &env
))
3582 if (PyList_Check(argv
)) {
3583 argc
= PyList_Size(argv
);
3584 getitem
= PyList_GetItem
;
3586 else if (PyTuple_Check(argv
)) {
3587 argc
= PyTuple_Size(argv
);
3588 getitem
= PyTuple_GetItem
;
3591 PyErr_SetString(PyExc_TypeError
,
3592 "spawnvpe() arg 2 must be a tuple or list");
3595 if (!PyMapping_Check(env
)) {
3596 PyErr_SetString(PyExc_TypeError
,
3597 "spawnvpe() arg 3 must be a mapping object");
3601 argvlist
= PyMem_NEW(char *, argc
+1);
3602 if (argvlist
== NULL
) {
3606 for (i
= 0; i
< argc
; i
++) {
3607 if (!PyArg_Parse((*getitem
)(argv
, i
),
3608 "et;spawnvpe() arg 2 must contain only strings",
3609 Py_FileSystemDefaultEncoding
,
3617 argvlist
[argc
] = NULL
;
3619 i
= PyMapping_Size(env
);
3622 envlist
= PyMem_NEW(char *, i
+ 1);
3623 if (envlist
== NULL
) {
3628 keys
= PyMapping_Keys(env
);
3629 vals
= PyMapping_Values(env
);
3632 if (!PyList_Check(keys
) || !PyList_Check(vals
)) {
3633 PyErr_SetString(PyExc_TypeError
,
3634 "spawnvpe(): env.keys() or env.values() is not a list");
3638 for (pos
= 0; pos
< i
; pos
++) {
3642 key
= PyList_GetItem(keys
, pos
);
3643 val
= PyList_GetItem(vals
, pos
);
3649 "s;spawnvpe() arg 3 contains a non-string key",
3653 "s;spawnvpe() arg 3 contains a non-string value",
3658 len
= PyString_Size(key
) + PyString_Size(val
) + 2;
3659 p
= PyMem_NEW(char, len
);
3664 PyOS_snprintf(p
, len
, "%s=%s", k
, v
);
3665 envlist
[envc
++] = p
;
3669 Py_BEGIN_ALLOW_THREADS
3670 #if defined(PYCC_GCC)
3671 spawnval
= spawnvpe(mode
, path
, argvlist
, envlist
);
3673 spawnval
= _spawnvpe(mode
, path
, argvlist
, envlist
);
3675 Py_END_ALLOW_THREADS
3678 (void) posix_error();
3680 res
= Py_BuildValue("l", (long) spawnval
);
3684 PyMem_DEL(envlist
[envc
]);
3687 free_string_array(argvlist
, lastarg
);
3694 #endif /* PYOS_OS2 */
3695 #endif /* HAVE_SPAWNV */
3699 PyDoc_STRVAR(posix_fork1__doc__
,
3700 "fork1() -> pid\n\n\
3701 Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
3703 Return 0 to child process and PID of child to parent process.");
3706 posix_fork1(PyObject
*self
, PyObject
*noargs
)
3708 pid_t pid
= fork1();
3710 return posix_error();
3713 return PyInt_FromLong(pid
);
3719 PyDoc_STRVAR(posix_fork__doc__
,
3721 Fork a child process.\n\
3722 Return 0 to child process and PID of child to parent process.");
3725 posix_fork(PyObject
*self
, PyObject
*noargs
)
3729 return posix_error();
3732 return PyInt_FromLong(pid
);
3736 /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
3737 /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
3738 #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
3739 #define DEV_PTY_FILE "/dev/ptc"
3740 #define HAVE_DEV_PTMX
3742 #define DEV_PTY_FILE "/dev/ptmx"
3745 #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
3749 #ifdef HAVE_LIBUTIL_H
3750 #include <libutil.h>
3751 #endif /* HAVE_LIBUTIL_H */
3752 #endif /* HAVE_PTY_H */
3753 #ifdef HAVE_STROPTS_H
3754 #include <stropts.h>
3756 #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
3758 #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
3759 PyDoc_STRVAR(posix_openpty__doc__
,
3760 "openpty() -> (master_fd, slave_fd)\n\n\
3761 Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
3764 posix_openpty(PyObject
*self
, PyObject
*noargs
)
3766 int master_fd
, slave_fd
;
3767 #ifndef HAVE_OPENPTY
3770 #if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
3771 PyOS_sighandler_t sig_saved
;
3773 extern char *ptsname(int fildes
);
3778 if (openpty(&master_fd
, &slave_fd
, NULL
, NULL
, NULL
) != 0)
3779 return posix_error();
3780 #elif defined(HAVE__GETPTY)
3781 slave_name
= _getpty(&master_fd
, O_RDWR
, 0666, 0);
3782 if (slave_name
== NULL
)
3783 return posix_error();
3785 slave_fd
= open(slave_name
, O_RDWR
);
3787 return posix_error();
3789 master_fd
= open(DEV_PTY_FILE
, O_RDWR
| O_NOCTTY
); /* open master */
3791 return posix_error();
3792 sig_saved
= PyOS_setsig(SIGCHLD
, SIG_DFL
);
3793 /* change permission of slave */
3794 if (grantpt(master_fd
) < 0) {
3795 PyOS_setsig(SIGCHLD
, sig_saved
);
3796 return posix_error();
3799 if (unlockpt(master_fd
) < 0) {
3800 PyOS_setsig(SIGCHLD
, sig_saved
);
3801 return posix_error();
3803 PyOS_setsig(SIGCHLD
, sig_saved
);
3804 slave_name
= ptsname(master_fd
); /* get name of slave */
3805 if (slave_name
== NULL
)
3806 return posix_error();
3807 slave_fd
= open(slave_name
, O_RDWR
| O_NOCTTY
); /* open slave */
3809 return posix_error();
3810 #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
3811 ioctl(slave_fd
, I_PUSH
, "ptem"); /* push ptem */
3812 ioctl(slave_fd
, I_PUSH
, "ldterm"); /* push ldterm */
3814 ioctl(slave_fd
, I_PUSH
, "ttcompat"); /* push ttcompat */
3816 #endif /* HAVE_CYGWIN */
3817 #endif /* HAVE_OPENPTY */
3819 return Py_BuildValue("(ii)", master_fd
, slave_fd
);
3822 #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
3825 PyDoc_STRVAR(posix_forkpty__doc__
,
3826 "forkpty() -> (pid, master_fd)\n\n\
3827 Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
3828 Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
3829 To both, return fd of newly opened pseudo-terminal.\n");
3832 posix_forkpty(PyObject
*self
, PyObject
*noargs
)
3837 pid
= forkpty(&master_fd
, NULL
, NULL
, NULL
);
3839 return posix_error();
3842 return Py_BuildValue("(li)", pid
, master_fd
);
3847 PyDoc_STRVAR(posix_getegid__doc__
,
3848 "getegid() -> egid\n\n\
3849 Return the current process's effective group id.");
3852 posix_getegid(PyObject
*self
, PyObject
*noargs
)
3854 return PyInt_FromLong((long)getegid());
3860 PyDoc_STRVAR(posix_geteuid__doc__
,
3861 "geteuid() -> euid\n\n\
3862 Return the current process's effective user id.");
3865 posix_geteuid(PyObject
*self
, PyObject
*noargs
)
3867 return PyInt_FromLong((long)geteuid());
3873 PyDoc_STRVAR(posix_getgid__doc__
,
3874 "getgid() -> gid\n\n\
3875 Return the current process's group id.");
3878 posix_getgid(PyObject
*self
, PyObject
*noargs
)
3880 return PyInt_FromLong((long)getgid());
3885 PyDoc_STRVAR(posix_getpid__doc__
,
3886 "getpid() -> pid\n\n\
3887 Return the current process id");
3890 posix_getpid(PyObject
*self
, PyObject
*noargs
)
3892 return PyInt_FromLong((long)getpid());
3896 #ifdef HAVE_GETGROUPS
3897 PyDoc_STRVAR(posix_getgroups__doc__
,
3898 "getgroups() -> list of group IDs\n\n\
3899 Return list of supplemental group IDs for the process.");
3902 posix_getgroups(PyObject
*self
, PyObject
*noargs
)
3904 PyObject
*result
= NULL
;
3907 #define MAX_GROUPS NGROUPS_MAX
3909 /* defined to be 16 on Solaris7, so this should be a small number */
3910 #define MAX_GROUPS 64
3912 gid_t grouplist
[MAX_GROUPS
];
3915 n
= getgroups(MAX_GROUPS
, grouplist
);
3919 result
= PyList_New(n
);
3920 if (result
!= NULL
) {
3922 for (i
= 0; i
< n
; ++i
) {
3923 PyObject
*o
= PyInt_FromLong((long)grouplist
[i
]);
3929 PyList_SET_ITEM(result
, i
, o
);
3939 PyDoc_STRVAR(posix_getpgid__doc__
,
3940 "getpgid(pid) -> pgid\n\n\
3941 Call the system call getpgid().");
3944 posix_getpgid(PyObject
*self
, PyObject
*args
)
3947 if (!PyArg_ParseTuple(args
, "i:getpgid", &pid
))
3949 pgid
= getpgid(pid
);
3951 return posix_error();
3952 return PyInt_FromLong((long)pgid
);
3954 #endif /* HAVE_GETPGID */
3958 PyDoc_STRVAR(posix_getpgrp__doc__
,
3959 "getpgrp() -> pgrp\n\n\
3960 Return the current process group id.");
3963 posix_getpgrp(PyObject
*self
, PyObject
*noargs
)
3965 #ifdef GETPGRP_HAVE_ARG
3966 return PyInt_FromLong((long)getpgrp(0));
3967 #else /* GETPGRP_HAVE_ARG */
3968 return PyInt_FromLong((long)getpgrp());
3969 #endif /* GETPGRP_HAVE_ARG */
3971 #endif /* HAVE_GETPGRP */
3975 PyDoc_STRVAR(posix_setpgrp__doc__
,
3977 Make this process a session leader.");
3980 posix_setpgrp(PyObject
*self
, PyObject
*noargs
)
3982 #ifdef SETPGRP_HAVE_ARG
3983 if (setpgrp(0, 0) < 0)
3984 #else /* SETPGRP_HAVE_ARG */
3986 #endif /* SETPGRP_HAVE_ARG */
3987 return posix_error();
3992 #endif /* HAVE_SETPGRP */
3995 PyDoc_STRVAR(posix_getppid__doc__
,
3996 "getppid() -> ppid\n\n\
3997 Return the parent's process id.");
4000 posix_getppid(PyObject
*self
, PyObject
*noargs
)
4002 return PyInt_FromLong(getppid());
4007 #ifdef HAVE_GETLOGIN
4008 PyDoc_STRVAR(posix_getlogin__doc__
,
4009 "getlogin() -> string\n\n\
4010 Return the actual login name.");
4013 posix_getlogin(PyObject
*self
, PyObject
*noargs
)
4015 PyObject
*result
= NULL
;
4017 int old_errno
= errno
;
4025 PyErr_SetString(PyExc_OSError
,
4026 "unable to determine login name");
4029 result
= PyString_FromString(name
);
4037 PyDoc_STRVAR(posix_getuid__doc__
,
4038 "getuid() -> uid\n\n\
4039 Return the current process's user id.");
4042 posix_getuid(PyObject
*self
, PyObject
*noargs
)
4044 return PyInt_FromLong((long)getuid());
4050 PyDoc_STRVAR(posix_kill__doc__
,
4051 "kill(pid, sig)\n\n\
4052 Kill a process with a signal.");
4055 posix_kill(PyObject
*self
, PyObject
*args
)
4059 if (!PyArg_ParseTuple(args
, "ii:kill", &pid
, &sig
))
4061 #if defined(PYOS_OS2) && !defined(PYCC_GCC)
4062 if (sig
== XCPT_SIGNAL_INTR
|| sig
== XCPT_SIGNAL_BREAK
) {
4064 if ((rc
= DosSendSignalException(pid
, sig
)) != NO_ERROR
)
4065 return os2_error(rc
);
4067 } else if (sig
== XCPT_SIGNAL_KILLPROC
) {
4069 if ((rc
= DosKillProcess(DKP_PROCESS
, pid
)) != NO_ERROR
)
4070 return os2_error(rc
);
4073 return NULL
; /* Unrecognized Signal Requested */
4075 if (kill(pid
, sig
) == -1)
4076 return posix_error();
4084 PyDoc_STRVAR(posix_killpg__doc__
,
4085 "killpg(pgid, sig)\n\n\
4086 Kill a process group with a signal.");
4089 posix_killpg(PyObject
*self
, PyObject
*args
)
4092 if (!PyArg_ParseTuple(args
, "ii:killpg", &pgid
, &sig
))
4094 if (killpg(pgid
, sig
) == -1)
4095 return posix_error();
4103 #ifdef HAVE_SYS_LOCK_H
4104 #include <sys/lock.h>
4107 PyDoc_STRVAR(posix_plock__doc__
,
4109 Lock program segments into memory.");
4112 posix_plock(PyObject
*self
, PyObject
*args
)
4115 if (!PyArg_ParseTuple(args
, "i:plock", &op
))
4117 if (plock(op
) == -1)
4118 return posix_error();
4126 PyDoc_STRVAR(posix_popen__doc__
,
4127 "popen(command [, mode='r' [, bufsize]]) -> pipe\n\n\
4128 Open a pipe to/from a command returning a file object.");
4130 #if defined(PYOS_OS2)
4131 #if defined(PYCC_VACPP)
4133 async_system(const char *command
)
4135 char errormsg
[256], args
[1024];
4139 char *shell
= getenv("COMSPEC");
4143 /* avoid overflowing the argument buffer */
4144 if (strlen(shell
) + 3 + strlen(command
) >= 1024)
4145 return ERROR_NOT_ENOUGH_MEMORY
4148 strcat(args
, shell
);
4149 strcat(args
, "/c ");
4150 strcat(args
, command
);
4152 /* execute asynchronously, inheriting the environment */
4153 rc
= DosExecPgm(errormsg
,
4164 popen(const char *command
, const char *mode
, int pipesize
, int *err
)
4170 /* mode determines which of stdin or stdout is reconnected to
4171 * the pipe to the child
4173 if (strchr(mode
, 'r') != NULL
) {
4174 tgt_fd
= 1; /* stdout */
4175 } else if (strchr(mode
, 'w')) {
4176 tgt_fd
= 0; /* stdin */
4178 *err
= ERROR_INVALID_ACCESS
;
4182 /* setup the pipe */
4183 if ((rc
= DosCreatePipe(&pipeh
[0], &pipeh
[1], pipesize
)) != NO_ERROR
) {
4188 /* prevent other threads accessing stdio */
4191 /* reconnect stdio and execute child */
4194 if (dup2(pipeh
[tgtfd
], tgtfd
) == 0) {
4195 DosClose(pipeh
[tgtfd
]);
4196 rc
= async_system(command
);
4203 /* allow other threads access to stdio */
4206 /* if execution of child was successful return file stream */
4208 return fdopen(pipeh
[1 - tgtfd
], mode
);
4210 DosClose(pipeh
[1 - tgtfd
]);
4217 posix_popen(PyObject
*self
, PyObject
*args
)
4221 int err
, bufsize
= -1;
4224 if (!PyArg_ParseTuple(args
, "s|si:popen", &name
, &mode
, &bufsize
))
4226 Py_BEGIN_ALLOW_THREADS
4227 fp
= popen(name
, mode
, (bufsize
> 0) ? bufsize
: 4096, &err
);
4228 Py_END_ALLOW_THREADS
4230 return os2_error(err
);
4232 f
= PyFile_FromFile(fp
, name
, mode
, fclose
);
4234 PyFile_SetBufSize(f
, bufsize
);
4238 #elif defined(PYCC_GCC)
4240 /* standard posix version of popen() support */
4242 posix_popen(PyObject
*self
, PyObject
*args
)
4249 if (!PyArg_ParseTuple(args
, "s|si:popen", &name
, &mode
, &bufsize
))
4251 Py_BEGIN_ALLOW_THREADS
4252 fp
= popen(name
, mode
);
4253 Py_END_ALLOW_THREADS
4255 return posix_error();
4256 f
= PyFile_FromFile(fp
, name
, mode
, pclose
);
4258 PyFile_SetBufSize(f
, bufsize
);
4262 /* fork() under OS/2 has lots'o'warts
4263 * EMX supports pipe() and spawn*() so we can synthesize popen[234]()
4264 * most of this code is a ripoff of the win32 code, but using the
4265 * capabilities of EMX's C library routines
4268 /* These tell _PyPopen() whether to return 1, 2, or 3 file objects. */
4274 static PyObject
*_PyPopen(char *, int, int, int);
4275 static int _PyPclose(FILE *file
);
4278 * Internal dictionary mapping popen* file pointers to process handles,
4279 * for use when retrieving the process exit code. See _PyPclose() below
4280 * for more information on this dictionary's use.
4282 static PyObject
*_PyPopenProcs
= NULL
;
4284 /* os2emx version of popen2()
4286 * The result of this function is a pipe (file) connected to the
4287 * process's stdin, and a pipe connected to the process's stdout.
4291 os2emx_popen2(PyObject
*self
, PyObject
*args
)
4299 if (!PyArg_ParseTuple(args
, "s|si:popen2", &cmdstring
, &mode
, &bufsize
))
4304 else if (*mode
!= 'b') {
4305 PyErr_SetString(PyExc_ValueError
, "mode must be 't' or 'b'");
4310 f
= _PyPopen(cmdstring
, tm
, POPEN_2
, bufsize
);
4316 * Variation on os2emx.popen2
4318 * The result of this function is 3 pipes - the process's stdin,
4323 os2emx_popen3(PyObject
*self
, PyObject
*args
)
4331 if (!PyArg_ParseTuple(args
, "s|si:popen3", &cmdstring
, &mode
, &bufsize
))
4336 else if (*mode
!= 'b') {
4337 PyErr_SetString(PyExc_ValueError
, "mode must be 't' or 'b'");
4342 f
= _PyPopen(cmdstring
, tm
, POPEN_3
, bufsize
);
4348 * Variation on os2emx.popen2
4350 * The result of this function is 2 pipes - the processes stdin,
4351 * and stdout+stderr combined as a single pipe.
4355 os2emx_popen4(PyObject
*self
, PyObject
*args
)
4363 if (!PyArg_ParseTuple(args
, "s|si:popen4", &cmdstring
, &mode
, &bufsize
))
4368 else if (*mode
!= 'b') {
4369 PyErr_SetString(PyExc_ValueError
, "mode must be 't' or 'b'");
4374 f
= _PyPopen(cmdstring
, tm
, POPEN_4
, bufsize
);
4379 /* a couple of structures for convenient handling of multiple
4380 * file handles and pipes
4394 /* The following code is derived from the win32 code */
4397 _PyPopen(char *cmdstring
, int mode
, int n
, int bufsize
)
4399 struct file_ref stdio
[3];
4400 struct pipe_ref p_fd
[3];
4402 int file_count
, i
, pipe_err
;
4404 char *shell
, *sh_name
, *opt
, *rd_mode
, *wr_mode
;
4405 PyObject
*f
, *p_f
[3];
4407 /* file modes for subsequent fdopen's on pipe handles */
4419 /* prepare shell references */
4420 if ((shell
= getenv("EMXSHELL")) == NULL
)
4421 if ((shell
= getenv("COMSPEC")) == NULL
)
4424 return posix_error();
4427 sh_name
= _getname(shell
);
4428 if (stricmp(sh_name
, "cmd.exe") == 0 || stricmp(sh_name
, "4os2.exe") == 0)
4433 /* save current stdio fds + their flags, and set not inheritable */
4435 while (pipe_err
>= 0 && i
< 3)
4437 pipe_err
= stdio
[i
].handle
= dup(i
);
4438 stdio
[i
].flags
= fcntl(i
, F_GETFD
, 0);
4439 fcntl(stdio
[i
].handle
, F_SETFD
, stdio
[i
].flags
| FD_CLOEXEC
);
4444 /* didn't get them all saved - clean up and bail out */
4445 int saved_err
= errno
;
4448 close(stdio
[i
].handle
);
4451 return posix_error();
4454 /* create pipe ends */
4459 while ((pipe_err
== 0) && (i
< file_count
))
4460 pipe_err
= pipe((int *)&p_fd
[i
++]);
4463 /* didn't get them all made - clean up and bail out */
4470 return posix_error();
4473 /* change the actual standard IO streams over temporarily,
4474 * making the retained pipe ends non-inheritable
4479 if (dup2(p_fd
[0].rd
, 0) == 0)
4482 i
= fcntl(p_fd
[0].wr
, F_GETFD
, 0);
4483 fcntl(p_fd
[0].wr
, F_SETFD
, i
| FD_CLOEXEC
);
4484 if ((p_s
[0] = fdopen(p_fd
[0].wr
, wr_mode
)) == NULL
)
4498 if (dup2(p_fd
[1].wr
, 1) == 1)
4501 i
= fcntl(p_fd
[1].rd
, F_GETFD
, 0);
4502 fcntl(p_fd
[1].rd
, F_SETFD
, i
| FD_CLOEXEC
);
4503 if ((p_s
[1] = fdopen(p_fd
[1].rd
, rd_mode
)) == NULL
)
4515 /* - stderr, as required */
4521 if (dup2(p_fd
[2].wr
, 2) == 2)
4524 i
= fcntl(p_fd
[2].rd
, F_GETFD
, 0);
4525 fcntl(p_fd
[2].rd
, F_SETFD
, i
| FD_CLOEXEC
);
4526 if ((p_s
[2] = fdopen(p_fd
[2].rd
, rd_mode
)) == NULL
)
4541 if (dup2(1, 2) != 2)
4549 /* spawn the child process */
4552 pipe_pid
= spawnlp(P_NOWAIT
, shell
, shell
, opt
, cmdstring
, (char *)0);
4559 /* save the PID into the FILE structure
4560 * NOTE: this implementation doesn't actually
4561 * take advantage of this, but do it for
4562 * completeness - AIM Apr01
4564 for (i
= 0; i
< file_count
; i
++)
4565 p_s
[i
]->_pid
= pipe_pid
;
4569 /* reset standard IO to normal */
4570 for (i
= 0; i
< 3; i
++)
4572 dup2(stdio
[i
].handle
, i
);
4573 fcntl(i
, F_SETFD
, stdio
[i
].flags
);
4574 close(stdio
[i
].handle
);
4577 /* if any remnant problems, clean up and bail out */
4580 for (i
= 0; i
< 3; i
++)
4586 return posix_error_with_filename(cmdstring
);
4589 /* build tuple of file objects to return */
4590 if ((p_f
[0] = PyFile_FromFile(p_s
[0], cmdstring
, wr_mode
, _PyPclose
)) != NULL
)
4591 PyFile_SetBufSize(p_f
[0], bufsize
);
4592 if ((p_f
[1] = PyFile_FromFile(p_s
[1], cmdstring
, rd_mode
, _PyPclose
)) != NULL
)
4593 PyFile_SetBufSize(p_f
[1], bufsize
);
4596 if ((p_f
[2] = PyFile_FromFile(p_s
[2], cmdstring
, rd_mode
, _PyPclose
)) != NULL
)
4597 PyFile_SetBufSize(p_f
[0], bufsize
);
4598 f
= PyTuple_Pack(3, p_f
[0], p_f
[1], p_f
[2]);
4601 f
= PyTuple_Pack(2, p_f
[0], p_f
[1]);
4604 * Insert the files we've created into the process dictionary
4605 * all referencing the list with the process handle and the
4606 * initial number of files (see description below in _PyPclose).
4607 * Since if _PyPclose later tried to wait on a process when all
4608 * handles weren't closed, it could create a deadlock with the
4609 * child, we spend some energy here to try to ensure that we
4610 * either insert all file handles into the dictionary or none
4611 * at all. It's a little clumsy with the various popen modes
4612 * and variable number of files involved.
4616 _PyPopenProcs
= PyDict_New();
4621 PyObject
*procObj
, *pidObj
, *intObj
, *fileObj
[3];
4624 fileObj
[0] = fileObj
[1] = fileObj
[2] = NULL
;
4625 ins_rc
[0] = ins_rc
[1] = ins_rc
[2] = 0;
4627 procObj
= PyList_New(2);
4628 pidObj
= PyInt_FromLong((long) pipe_pid
);
4629 intObj
= PyInt_FromLong((long) file_count
);
4631 if (procObj
&& pidObj
&& intObj
)
4633 PyList_SetItem(procObj
, 0, pidObj
);
4634 PyList_SetItem(procObj
, 1, intObj
);
4636 fileObj
[0] = PyLong_FromVoidPtr(p_s
[0]);
4639 ins_rc
[0] = PyDict_SetItem(_PyPopenProcs
,
4643 fileObj
[1] = PyLong_FromVoidPtr(p_s
[1]);
4646 ins_rc
[1] = PyDict_SetItem(_PyPopenProcs
,
4650 if (file_count
>= 3)
4652 fileObj
[2] = PyLong_FromVoidPtr(p_s
[2]);
4655 ins_rc
[2] = PyDict_SetItem(_PyPopenProcs
,
4661 if (ins_rc
[0] < 0 || !fileObj
[0] ||
4662 ins_rc
[1] < 0 || (file_count
> 1 && !fileObj
[1]) ||
4663 ins_rc
[2] < 0 || (file_count
> 2 && !fileObj
[2]))
4665 /* Something failed - remove any dictionary
4666 * entries that did make it.
4668 if (!ins_rc
[0] && fileObj
[0])
4670 PyDict_DelItem(_PyPopenProcs
,
4673 if (!ins_rc
[1] && fileObj
[1])
4675 PyDict_DelItem(_PyPopenProcs
,
4678 if (!ins_rc
[2] && fileObj
[2])
4680 PyDict_DelItem(_PyPopenProcs
,
4687 * Clean up our localized references for the dictionary keys
4688 * and value since PyDict_SetItem will Py_INCREF any copies
4689 * that got placed in the dictionary.
4691 Py_XDECREF(procObj
);
4692 Py_XDECREF(fileObj
[0]);
4693 Py_XDECREF(fileObj
[1]);
4694 Py_XDECREF(fileObj
[2]);
4697 /* Child is launched. */
4702 * Wrapper for fclose() to use for popen* files, so we can retrieve the
4703 * exit code for the child process and return as a result of the close.
4705 * This function uses the _PyPopenProcs dictionary in order to map the
4706 * input file pointer to information about the process that was
4707 * originally created by the popen* call that created the file pointer.
4708 * The dictionary uses the file pointer as a key (with one entry
4709 * inserted for each file returned by the original popen* call) and a
4710 * single list object as the value for all files from a single call.
4711 * The list object contains the Win32 process handle at [0], and a file
4712 * count at [1], which is initialized to the total number of file
4713 * handles using that list.
4715 * This function closes whichever handle it is passed, and decrements
4716 * the file count in the dictionary for the process handle pointed to
4717 * by this file. On the last close (when the file count reaches zero),
4718 * this function will wait for the child process and then return its
4719 * exit code as the result of the close() operation. This permits the
4720 * files to be closed in any order - it is always the close() of the
4721 * final handle that will return the exit code.
4723 * NOTE: This function is currently called with the GIL released.
4724 * hence we use the GILState API to manage our state.
4727 static int _PyPclose(FILE *file
)
4732 PyObject
*procObj
, *pidObj
, *intObj
, *fileObj
;
4735 PyGILState_STATE state
;
4738 /* Close the file handle first, to ensure it can't block the
4739 * child from exiting if it's the last handle.
4741 result
= fclose(file
);
4744 state
= PyGILState_Ensure();
4748 if ((fileObj
= PyLong_FromVoidPtr(file
)) != NULL
&&
4749 (procObj
= PyDict_GetItem(_PyPopenProcs
,
4750 fileObj
)) != NULL
&&
4751 (pidObj
= PyList_GetItem(procObj
,0)) != NULL
&&
4752 (intObj
= PyList_GetItem(procObj
,1)) != NULL
)
4754 pipe_pid
= (int) PyInt_AsLong(pidObj
);
4755 file_count
= (int) PyInt_AsLong(intObj
);
4759 /* Still other files referencing process */
4761 PyList_SetItem(procObj
,1,
4762 PyInt_FromLong((long) file_count
));
4766 /* Last file for this process */
4767 if (result
!= EOF
&&
4768 waitpid(pipe_pid
, &exit_code
, 0) == pipe_pid
)
4770 /* extract exit status */
4771 if (WIFEXITED(exit_code
))
4773 result
= WEXITSTATUS(exit_code
);
4783 /* Indicate failure - this will cause the file object
4784 * to raise an I/O error and translate the last
4785 * error code from errno. We do have a problem with
4786 * last errors that overlap the normal errno table,
4787 * but that's a consistent problem with the file object.
4793 /* Remove this file pointer from dictionary */
4794 PyDict_DelItem(_PyPopenProcs
, fileObj
);
4796 if (PyDict_Size(_PyPopenProcs
) == 0)
4798 Py_DECREF(_PyPopenProcs
);
4799 _PyPopenProcs
= NULL
;
4802 } /* if object retrieval ok */
4804 Py_XDECREF(fileObj
);
4805 } /* if _PyPopenProcs */
4808 PyGILState_Release(state
);
4813 #endif /* PYCC_??? */
4815 #elif defined(MS_WINDOWS)
4818 * Portable 'popen' replacement for Win32.
4820 * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks
4821 * and 2.0 integration by Fredrik Lundh <fredrik@pythonware.com>
4822 * Return code handling by David Bolen <db3l@fitlinxx.com>.
4829 /* These tell _PyPopen() wether to return 1, 2, or 3 file objects. */
4835 static PyObject
*_PyPopen(char *, int, int);
4836 static int _PyPclose(FILE *file
);
4839 * Internal dictionary mapping popen* file pointers to process handles,
4840 * for use when retrieving the process exit code. See _PyPclose() below
4841 * for more information on this dictionary's use.
4843 static PyObject
*_PyPopenProcs
= NULL
;
4846 /* popen that works from a GUI.
4848 * The result of this function is a pipe (file) connected to the
4849 * processes stdin or stdout, depending on the requested mode.
4853 posix_popen(PyObject
*self
, PyObject
*args
)
4861 if (!PyArg_ParseTuple(args
, "s|si:popen", &cmdstring
, &mode
, &bufsize
))
4866 else if (*mode
!= 'w') {
4867 PyErr_SetString(PyExc_ValueError
, "popen() arg 2 must be 'r' or 'w'");
4872 if (bufsize
!= -1) {
4873 PyErr_SetString(PyExc_ValueError
, "popen() arg 3 must be -1");
4877 if (*(mode
+1) == 't')
4878 f
= _PyPopen(cmdstring
, tm
| _O_TEXT
, POPEN_1
);
4879 else if (*(mode
+1) == 'b')
4880 f
= _PyPopen(cmdstring
, tm
| _O_BINARY
, POPEN_1
);
4882 f
= _PyPopen(cmdstring
, tm
| _O_TEXT
, POPEN_1
);
4887 /* Variation on win32pipe.popen
4889 * The result of this function is a pipe (file) connected to the
4890 * process's stdin, and a pipe connected to the process's stdout.
4894 win32_popen2(PyObject
*self
, PyObject
*args
)
4902 if (!PyArg_ParseTuple(args
, "s|si:popen2", &cmdstring
, &mode
, &bufsize
))
4907 else if (*mode
!= 'b') {
4908 PyErr_SetString(PyExc_ValueError
, "popen2() arg 2 must be 't' or 'b'");
4913 if (bufsize
!= -1) {
4914 PyErr_SetString(PyExc_ValueError
, "popen2() arg 3 must be -1");
4918 f
= _PyPopen(cmdstring
, tm
, POPEN_2
);
4924 * Variation on <om win32pipe.popen>
4926 * The result of this function is 3 pipes - the process's stdin,
4931 win32_popen3(PyObject
*self
, PyObject
*args
)
4939 if (!PyArg_ParseTuple(args
, "s|si:popen3", &cmdstring
, &mode
, &bufsize
))
4944 else if (*mode
!= 'b') {
4945 PyErr_SetString(PyExc_ValueError
, "popen3() arg 2 must be 't' or 'b'");
4950 if (bufsize
!= -1) {
4951 PyErr_SetString(PyExc_ValueError
, "popen3() arg 3 must be -1");
4955 f
= _PyPopen(cmdstring
, tm
, POPEN_3
);
4961 * Variation on win32pipe.popen
4963 * The result of this function is 2 pipes - the processes stdin,
4964 * and stdout+stderr combined as a single pipe.
4968 win32_popen4(PyObject
*self
, PyObject
*args
)
4976 if (!PyArg_ParseTuple(args
, "s|si:popen4", &cmdstring
, &mode
, &bufsize
))
4981 else if (*mode
!= 'b') {
4982 PyErr_SetString(PyExc_ValueError
, "popen4() arg 2 must be 't' or 'b'");
4987 if (bufsize
!= -1) {
4988 PyErr_SetString(PyExc_ValueError
, "popen4() arg 3 must be -1");
4992 f
= _PyPopen(cmdstring
, tm
, POPEN_4
);
4998 _PyPopenCreateProcess(char *cmdstring
,
5004 PROCESS_INFORMATION piProcInfo
;
5005 STARTUPINFO siStartInfo
;
5006 DWORD dwProcessFlags
= 0; /* no NEW_CONSOLE by default for Ctrl+C handling */
5007 char *s1
,*s2
, *s3
= " /c ";
5008 const char *szConsoleSpawn
= "w9xpopen.exe";
5012 if (i
= GetEnvironmentVariable("COMSPEC",NULL
,0)) {
5015 s1
= (char *)alloca(i
);
5016 if (!(x
= GetEnvironmentVariable("COMSPEC", s1
, i
)))
5017 /* x < i, so x fits into an integer */
5020 /* Explicitly check if we are using COMMAND.COM. If we are
5021 * then use the w9xpopen hack.
5024 while (comshell
>= s1
&& *comshell
!= '\\')
5028 if (GetVersion() < 0x80000000 &&
5029 _stricmp(comshell
, "command.com") != 0) {
5030 /* NT/2000 and not using command.com. */
5031 x
= i
+ strlen(s3
) + strlen(cmdstring
) + 1;
5032 s2
= (char *)alloca(x
);
5034 PyOS_snprintf(s2
, x
, "%s%s%s", s1
, s3
, cmdstring
);
5038 * Oh gag, we're on Win9x or using COMMAND.COM. Use
5039 * the workaround listed in KB: Q150956
5041 char modulepath
[_MAX_PATH
];
5042 struct stat statinfo
;
5043 GetModuleFileName(NULL
, modulepath
, sizeof(modulepath
));
5044 for (x
= i
= 0; modulepath
[i
]; i
++)
5045 if (modulepath
[i
] == SEP
)
5047 modulepath
[x
] = '\0';
5048 /* Create the full-name to w9xpopen, so we can test it exists */
5051 (sizeof(modulepath
)/sizeof(modulepath
[0]))
5052 -strlen(modulepath
));
5053 if (stat(modulepath
, &statinfo
) != 0) {
5054 size_t mplen
= sizeof(modulepath
)/sizeof(modulepath
[0]);
5055 /* Eeek - file-not-found - possibly an embedding
5056 situation - see if we can locate it in sys.prefix
5061 modulepath
[mplen
-1] = '\0';
5062 if (modulepath
[strlen(modulepath
)-1] != '\\')
5063 strcat(modulepath
, "\\");
5066 mplen
-strlen(modulepath
));
5067 /* No where else to look - raise an easily identifiable
5068 error, rather than leaving Windows to report
5069 "file not found" - as the user is probably blissfully
5070 unaware this shim EXE is used, and it will confuse them.
5071 (well, it confused me for a while ;-)
5073 if (stat(modulepath
, &statinfo
) != 0) {
5074 PyErr_Format(PyExc_RuntimeError
,
5075 "Can not locate '%s' which is needed "
5076 "for popen to work with your shell "
5082 x
= i
+ strlen(s3
) + strlen(cmdstring
) + 1 +
5083 strlen(modulepath
) +
5084 strlen(szConsoleSpawn
) + 1;
5086 s2
= (char *)alloca(x
);
5088 /* To maintain correct argument passing semantics,
5089 we pass the command-line as it stands, and allow
5090 quoting to be applied. w9xpopen.exe will then
5091 use its argv vector, and re-quote the necessary
5092 args for the ultimate child process.
5101 /* Not passing CREATE_NEW_CONSOLE has been known to
5102 cause random failures on win9x. Specifically a
5104 "Your program accessed mem currently in use at xxx"
5105 and a hopeful warning about the stability of your
5107 Cost is Ctrl+C won't kill children, but anyone
5108 who cares can have a go!
5110 dwProcessFlags
|= CREATE_NEW_CONSOLE
;
5114 /* Could be an else here to try cmd.exe / command.com in the path
5115 Now we'll just error out.. */
5117 PyErr_SetString(PyExc_RuntimeError
,
5118 "Cannot locate a COMSPEC environment variable to "
5119 "use as the shell");
5123 ZeroMemory(&siStartInfo
, sizeof(STARTUPINFO
));
5124 siStartInfo
.cb
= sizeof(STARTUPINFO
);
5125 siStartInfo
.dwFlags
= STARTF_USESTDHANDLES
| STARTF_USESHOWWINDOW
;
5126 siStartInfo
.hStdInput
= hStdin
;
5127 siStartInfo
.hStdOutput
= hStdout
;
5128 siStartInfo
.hStdError
= hStderr
;
5129 siStartInfo
.wShowWindow
= SW_HIDE
;
5131 if (CreateProcess(NULL
,
5141 /* Close the handles now so anyone waiting is woken. */
5142 CloseHandle(piProcInfo
.hThread
);
5144 /* Return process handle */
5145 *hProcess
= piProcInfo
.hProcess
;
5148 win32_error("CreateProcess", s2
);
5152 /* The following code is based off of KB: Q190351 */
5155 _PyPopen(char *cmdstring
, int mode
, int n
)
5157 HANDLE hChildStdinRd
, hChildStdinWr
, hChildStdoutRd
, hChildStdoutWr
,
5158 hChildStderrRd
, hChildStderrWr
, hChildStdinWrDup
, hChildStdoutRdDup
,
5159 hChildStderrRdDup
, hProcess
; /* hChildStdoutWrDup; */
5161 SECURITY_ATTRIBUTES saAttr
;
5168 saAttr
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
5169 saAttr
.bInheritHandle
= TRUE
;
5170 saAttr
.lpSecurityDescriptor
= NULL
;
5172 if (!CreatePipe(&hChildStdinRd
, &hChildStdinWr
, &saAttr
, 0))
5173 return win32_error("CreatePipe", NULL
);
5175 /* Create new output read handle and the input write handle. Set
5176 * the inheritance properties to FALSE. Otherwise, the child inherits
5177 * these handles; resulting in non-closeable handles to the pipes
5179 fSuccess
= DuplicateHandle(GetCurrentProcess(), hChildStdinWr
,
5180 GetCurrentProcess(), &hChildStdinWrDup
, 0,
5182 DUPLICATE_SAME_ACCESS
);
5184 return win32_error("DuplicateHandle", NULL
);
5186 /* Close the inheritable version of ChildStdin
5187 that we're using. */
5188 CloseHandle(hChildStdinWr
);
5190 if (!CreatePipe(&hChildStdoutRd
, &hChildStdoutWr
, &saAttr
, 0))
5191 return win32_error("CreatePipe", NULL
);
5193 fSuccess
= DuplicateHandle(GetCurrentProcess(), hChildStdoutRd
,
5194 GetCurrentProcess(), &hChildStdoutRdDup
, 0,
5195 FALSE
, DUPLICATE_SAME_ACCESS
);
5197 return win32_error("DuplicateHandle", NULL
);
5199 /* Close the inheritable version of ChildStdout
5200 that we're using. */
5201 CloseHandle(hChildStdoutRd
);
5204 if (!CreatePipe(&hChildStderrRd
, &hChildStderrWr
, &saAttr
, 0))
5205 return win32_error("CreatePipe", NULL
);
5206 fSuccess
= DuplicateHandle(GetCurrentProcess(),
5208 GetCurrentProcess(),
5209 &hChildStderrRdDup
, 0,
5210 FALSE
, DUPLICATE_SAME_ACCESS
);
5212 return win32_error("DuplicateHandle", NULL
);
5213 /* Close the inheritable version of ChildStdErr that we're using. */
5214 CloseHandle(hChildStderrRd
);
5219 switch (mode
& (_O_RDONLY
| _O_TEXT
| _O_BINARY
| _O_WRONLY
)) {
5220 case _O_WRONLY
| _O_TEXT
:
5221 /* Case for writing to child Stdin in text mode. */
5222 fd1
= _open_osfhandle((Py_intptr_t
)hChildStdinWrDup
, mode
);
5223 f1
= _fdopen(fd1
, "w");
5224 f
= PyFile_FromFile(f1
, cmdstring
, "w", _PyPclose
);
5225 PyFile_SetBufSize(f
, 0);
5226 /* We don't care about these pipes anymore, so close them. */
5227 CloseHandle(hChildStdoutRdDup
);
5228 CloseHandle(hChildStderrRdDup
);
5231 case _O_RDONLY
| _O_TEXT
:
5232 /* Case for reading from child Stdout in text mode. */
5233 fd1
= _open_osfhandle((Py_intptr_t
)hChildStdoutRdDup
, mode
);
5234 f1
= _fdopen(fd1
, "r");
5235 f
= PyFile_FromFile(f1
, cmdstring
, "r", _PyPclose
);
5236 PyFile_SetBufSize(f
, 0);
5237 /* We don't care about these pipes anymore, so close them. */
5238 CloseHandle(hChildStdinWrDup
);
5239 CloseHandle(hChildStderrRdDup
);
5242 case _O_RDONLY
| _O_BINARY
:
5243 /* Case for readinig from child Stdout in binary mode. */
5244 fd1
= _open_osfhandle((Py_intptr_t
)hChildStdoutRdDup
, mode
);
5245 f1
= _fdopen(fd1
, "rb");
5246 f
= PyFile_FromFile(f1
, cmdstring
, "rb", _PyPclose
);
5247 PyFile_SetBufSize(f
, 0);
5248 /* We don't care about these pipes anymore, so close them. */
5249 CloseHandle(hChildStdinWrDup
);
5250 CloseHandle(hChildStderrRdDup
);
5253 case _O_WRONLY
| _O_BINARY
:
5254 /* Case for writing to child Stdin in binary mode. */
5255 fd1
= _open_osfhandle((Py_intptr_t
)hChildStdinWrDup
, mode
);
5256 f1
= _fdopen(fd1
, "wb");
5257 f
= PyFile_FromFile(f1
, cmdstring
, "wb", _PyPclose
);
5258 PyFile_SetBufSize(f
, 0);
5259 /* We don't care about these pipes anymore, so close them. */
5260 CloseHandle(hChildStdoutRdDup
);
5261 CloseHandle(hChildStderrRdDup
);
5273 if (mode
& _O_TEXT
) {
5281 fd1
= _open_osfhandle((Py_intptr_t
)hChildStdinWrDup
, mode
);
5282 f1
= _fdopen(fd1
, m2
);
5283 fd2
= _open_osfhandle((Py_intptr_t
)hChildStdoutRdDup
, mode
);
5284 f2
= _fdopen(fd2
, m1
);
5285 p1
= PyFile_FromFile(f1
, cmdstring
, m2
, _PyPclose
);
5286 PyFile_SetBufSize(p1
, 0);
5287 p2
= PyFile_FromFile(f2
, cmdstring
, m1
, _PyPclose
);
5288 PyFile_SetBufSize(p2
, 0);
5291 CloseHandle(hChildStderrRdDup
);
5293 f
= PyTuple_Pack(2,p1
,p2
);
5303 PyObject
*p1
, *p2
, *p3
;
5305 if (mode
& _O_TEXT
) {
5313 fd1
= _open_osfhandle((Py_intptr_t
)hChildStdinWrDup
, mode
);
5314 f1
= _fdopen(fd1
, m2
);
5315 fd2
= _open_osfhandle((Py_intptr_t
)hChildStdoutRdDup
, mode
);
5316 f2
= _fdopen(fd2
, m1
);
5317 fd3
= _open_osfhandle((Py_intptr_t
)hChildStderrRdDup
, mode
);
5318 f3
= _fdopen(fd3
, m1
);
5319 p1
= PyFile_FromFile(f1
, cmdstring
, m2
, _PyPclose
);
5320 p2
= PyFile_FromFile(f2
, cmdstring
, m1
, _PyPclose
);
5321 p3
= PyFile_FromFile(f3
, cmdstring
, m1
, _PyPclose
);
5322 PyFile_SetBufSize(p1
, 0);
5323 PyFile_SetBufSize(p2
, 0);
5324 PyFile_SetBufSize(p3
, 0);
5325 f
= PyTuple_Pack(3,p1
,p2
,p3
);
5335 if (!_PyPopenCreateProcess(cmdstring
,
5343 if (!_PyPopenCreateProcess(cmdstring
,
5352 * Insert the files we've created into the process dictionary
5353 * all referencing the list with the process handle and the
5354 * initial number of files (see description below in _PyPclose).
5355 * Since if _PyPclose later tried to wait on a process when all
5356 * handles weren't closed, it could create a deadlock with the
5357 * child, we spend some energy here to try to ensure that we
5358 * either insert all file handles into the dictionary or none
5359 * at all. It's a little clumsy with the various popen modes
5360 * and variable number of files involved.
5362 if (!_PyPopenProcs
) {
5363 _PyPopenProcs
= PyDict_New();
5366 if (_PyPopenProcs
) {
5367 PyObject
*procObj
, *hProcessObj
, *intObj
, *fileObj
[3];
5370 fileObj
[0] = fileObj
[1] = fileObj
[2] = NULL
;
5371 ins_rc
[0] = ins_rc
[1] = ins_rc
[2] = 0;
5373 procObj
= PyList_New(2);
5374 hProcessObj
= PyLong_FromVoidPtr(hProcess
);
5375 intObj
= PyInt_FromLong(file_count
);
5377 if (procObj
&& hProcessObj
&& intObj
) {
5378 PyList_SetItem(procObj
,0,hProcessObj
);
5379 PyList_SetItem(procObj
,1,intObj
);
5381 fileObj
[0] = PyLong_FromVoidPtr(f1
);
5383 ins_rc
[0] = PyDict_SetItem(_PyPopenProcs
,
5387 if (file_count
>= 2) {
5388 fileObj
[1] = PyLong_FromVoidPtr(f2
);
5390 ins_rc
[1] = PyDict_SetItem(_PyPopenProcs
,
5395 if (file_count
>= 3) {
5396 fileObj
[2] = PyLong_FromVoidPtr(f3
);
5398 ins_rc
[2] = PyDict_SetItem(_PyPopenProcs
,
5404 if (ins_rc
[0] < 0 || !fileObj
[0] ||
5405 ins_rc
[1] < 0 || (file_count
> 1 && !fileObj
[1]) ||
5406 ins_rc
[2] < 0 || (file_count
> 2 && !fileObj
[2])) {
5407 /* Something failed - remove any dictionary
5408 * entries that did make it.
5410 if (!ins_rc
[0] && fileObj
[0]) {
5411 PyDict_DelItem(_PyPopenProcs
,
5414 if (!ins_rc
[1] && fileObj
[1]) {
5415 PyDict_DelItem(_PyPopenProcs
,
5418 if (!ins_rc
[2] && fileObj
[2]) {
5419 PyDict_DelItem(_PyPopenProcs
,
5426 * Clean up our localized references for the dictionary keys
5427 * and value since PyDict_SetItem will Py_INCREF any copies
5428 * that got placed in the dictionary.
5430 Py_XDECREF(procObj
);
5431 Py_XDECREF(fileObj
[0]);
5432 Py_XDECREF(fileObj
[1]);
5433 Py_XDECREF(fileObj
[2]);
5436 /* Child is launched. Close the parents copy of those pipe
5437 * handles that only the child should have open. You need to
5438 * make sure that no handles to the write end of the output pipe
5439 * are maintained in this process or else the pipe will not close
5440 * when the child process exits and the ReadFile will hang. */
5442 if (!CloseHandle(hChildStdinRd
))
5443 return win32_error("CloseHandle", NULL
);
5445 if (!CloseHandle(hChildStdoutWr
))
5446 return win32_error("CloseHandle", NULL
);
5448 if ((n
!= 4) && (!CloseHandle(hChildStderrWr
)))
5449 return win32_error("CloseHandle", NULL
);
5455 * Wrapper for fclose() to use for popen* files, so we can retrieve the
5456 * exit code for the child process and return as a result of the close.
5458 * This function uses the _PyPopenProcs dictionary in order to map the
5459 * input file pointer to information about the process that was
5460 * originally created by the popen* call that created the file pointer.
5461 * The dictionary uses the file pointer as a key (with one entry
5462 * inserted for each file returned by the original popen* call) and a
5463 * single list object as the value for all files from a single call.
5464 * The list object contains the Win32 process handle at [0], and a file
5465 * count at [1], which is initialized to the total number of file
5466 * handles using that list.
5468 * This function closes whichever handle it is passed, and decrements
5469 * the file count in the dictionary for the process handle pointed to
5470 * by this file. On the last close (when the file count reaches zero),
5471 * this function will wait for the child process and then return its
5472 * exit code as the result of the close() operation. This permits the
5473 * files to be closed in any order - it is always the close() of the
5474 * final handle that will return the exit code.
5476 * NOTE: This function is currently called with the GIL released.
5477 * hence we use the GILState API to manage our state.
5480 static int _PyPclose(FILE *file
)
5485 PyObject
*procObj
, *hProcessObj
, *intObj
, *fileObj
;
5488 PyGILState_STATE state
;
5491 /* Close the file handle first, to ensure it can't block the
5492 * child from exiting if it's the last handle.
5494 result
= fclose(file
);
5496 state
= PyGILState_Ensure();
5498 if (_PyPopenProcs
) {
5499 if ((fileObj
= PyLong_FromVoidPtr(file
)) != NULL
&&
5500 (procObj
= PyDict_GetItem(_PyPopenProcs
,
5501 fileObj
)) != NULL
&&
5502 (hProcessObj
= PyList_GetItem(procObj
,0)) != NULL
&&
5503 (intObj
= PyList_GetItem(procObj
,1)) != NULL
) {
5505 hProcess
= PyLong_AsVoidPtr(hProcessObj
);
5506 file_count
= PyInt_AsLong(intObj
);
5508 if (file_count
> 1) {
5509 /* Still other files referencing process */
5511 PyList_SetItem(procObj
,1,
5512 PyInt_FromLong(file_count
));
5514 /* Last file for this process */
5515 if (result
!= EOF
&&
5516 WaitForSingleObject(hProcess
, INFINITE
) != WAIT_FAILED
&&
5517 GetExitCodeProcess(hProcess
, &exit_code
)) {
5518 /* Possible truncation here in 16-bit environments, but
5519 * real exit codes are just the lower byte in any event.
5523 /* Indicate failure - this will cause the file object
5524 * to raise an I/O error and translate the last Win32
5525 * error code from errno. We do have a problem with
5526 * last errors that overlap the normal errno table,
5527 * but that's a consistent problem with the file object.
5529 if (result
!= EOF
) {
5530 /* If the error wasn't from the fclose(), then
5531 * set errno for the file object error handling.
5533 errno
= GetLastError();
5538 /* Free up the native handle at this point */
5539 CloseHandle(hProcess
);
5542 /* Remove this file pointer from dictionary */
5543 PyDict_DelItem(_PyPopenProcs
, fileObj
);
5545 if (PyDict_Size(_PyPopenProcs
) == 0) {
5546 Py_DECREF(_PyPopenProcs
);
5547 _PyPopenProcs
= NULL
;
5550 } /* if object retrieval ok */
5552 Py_XDECREF(fileObj
);
5553 } /* if _PyPopenProcs */
5556 PyGILState_Release(state
);
5561 #else /* which OS? */
5563 posix_popen(PyObject
*self
, PyObject
*args
)
5570 if (!PyArg_ParseTuple(args
, "s|si:popen", &name
, &mode
, &bufsize
))
5572 /* Strip mode of binary or text modifiers */
5573 if (strcmp(mode
, "rb") == 0 || strcmp(mode
, "rt") == 0)
5575 else if (strcmp(mode
, "wb") == 0 || strcmp(mode
, "wt") == 0)
5577 Py_BEGIN_ALLOW_THREADS
5578 fp
= popen(name
, mode
);
5579 Py_END_ALLOW_THREADS
5581 return posix_error();
5582 f
= PyFile_FromFile(fp
, name
, mode
, pclose
);
5584 PyFile_SetBufSize(f
, bufsize
);
5588 #endif /* PYOS_??? */
5589 #endif /* HAVE_POPEN */
5593 PyDoc_STRVAR(posix_setuid__doc__
,
5595 Set the current process's user id.");
5598 posix_setuid(PyObject
*self
, PyObject
*args
)
5602 if (!PyArg_ParseTuple(args
, "l:setuid", &uid_arg
))
5605 if (uid
!= uid_arg
) {
5606 PyErr_SetString(PyExc_OverflowError
, "user id too big");
5609 if (setuid(uid
) < 0)
5610 return posix_error();
5614 #endif /* HAVE_SETUID */
5618 PyDoc_STRVAR(posix_seteuid__doc__
,
5620 Set the current process's effective user id.");
5623 posix_seteuid (PyObject
*self
, PyObject
*args
)
5627 if (!PyArg_ParseTuple(args
, "l", &euid_arg
))
5630 if (euid
!= euid_arg
) {
5631 PyErr_SetString(PyExc_OverflowError
, "user id too big");
5634 if (seteuid(euid
) < 0) {
5635 return posix_error();
5641 #endif /* HAVE_SETEUID */
5644 PyDoc_STRVAR(posix_setegid__doc__
,
5646 Set the current process's effective group id.");
5649 posix_setegid (PyObject
*self
, PyObject
*args
)
5653 if (!PyArg_ParseTuple(args
, "l", &egid_arg
))
5656 if (egid
!= egid_arg
) {
5657 PyErr_SetString(PyExc_OverflowError
, "group id too big");
5660 if (setegid(egid
) < 0) {
5661 return posix_error();
5667 #endif /* HAVE_SETEGID */
5669 #ifdef HAVE_SETREUID
5670 PyDoc_STRVAR(posix_setreuid__doc__
,
5671 "setreuid(ruid, euid)\n\n\
5672 Set the current process's real and effective user ids.");
5675 posix_setreuid (PyObject
*self
, PyObject
*args
)
5677 long ruid_arg
, euid_arg
;
5679 if (!PyArg_ParseTuple(args
, "ll", &ruid_arg
, &euid_arg
))
5683 if (euid
!= euid_arg
|| ruid
!= ruid_arg
) {
5684 PyErr_SetString(PyExc_OverflowError
, "user id too big");
5687 if (setreuid(ruid
, euid
) < 0) {
5688 return posix_error();
5694 #endif /* HAVE_SETREUID */
5696 #ifdef HAVE_SETREGID
5697 PyDoc_STRVAR(posix_setregid__doc__
,
5698 "setregid(rgid, egid)\n\n\
5699 Set the current process's real and effective group ids.");
5702 posix_setregid (PyObject
*self
, PyObject
*args
)
5704 long rgid_arg
, egid_arg
;
5706 if (!PyArg_ParseTuple(args
, "ll", &rgid_arg
, &egid_arg
))
5710 if (egid
!= egid_arg
|| rgid
!= rgid_arg
) {
5711 PyErr_SetString(PyExc_OverflowError
, "group id too big");
5714 if (setregid(rgid
, egid
) < 0) {
5715 return posix_error();
5721 #endif /* HAVE_SETREGID */
5724 PyDoc_STRVAR(posix_setgid__doc__
,
5726 Set the current process's group id.");
5729 posix_setgid(PyObject
*self
, PyObject
*args
)
5733 if (!PyArg_ParseTuple(args
, "l:setgid", &gid_arg
))
5736 if (gid
!= gid_arg
) {
5737 PyErr_SetString(PyExc_OverflowError
, "group id too big");
5740 if (setgid(gid
) < 0)
5741 return posix_error();
5745 #endif /* HAVE_SETGID */
5747 #ifdef HAVE_SETGROUPS
5748 PyDoc_STRVAR(posix_setgroups__doc__
,
5749 "setgroups(list)\n\n\
5750 Set the groups of the current process to list.");
5753 posix_setgroups(PyObject
*self
, PyObject
*groups
)
5756 gid_t grouplist
[MAX_GROUPS
];
5758 if (!PySequence_Check(groups
)) {
5759 PyErr_SetString(PyExc_TypeError
, "setgroups argument must be a sequence");
5762 len
= PySequence_Size(groups
);
5763 if (len
> MAX_GROUPS
) {
5764 PyErr_SetString(PyExc_ValueError
, "too many groups");
5767 for(i
= 0; i
< len
; i
++) {
5769 elem
= PySequence_GetItem(groups
, i
);
5772 if (!PyInt_Check(elem
)) {
5773 if (!PyLong_Check(elem
)) {
5774 PyErr_SetString(PyExc_TypeError
,
5775 "groups must be integers");
5779 unsigned long x
= PyLong_AsUnsignedLong(elem
);
5780 if (PyErr_Occurred()) {
5781 PyErr_SetString(PyExc_TypeError
,
5782 "group id too big");
5787 /* read back to see if it fits in gid_t */
5788 if (grouplist
[i
] != x
) {
5789 PyErr_SetString(PyExc_TypeError
,
5790 "group id too big");
5796 long x
= PyInt_AsLong(elem
);
5798 if (grouplist
[i
] != x
) {
5799 PyErr_SetString(PyExc_TypeError
,
5800 "group id too big");
5808 if (setgroups(len
, grouplist
) < 0)
5809 return posix_error();
5813 #endif /* HAVE_SETGROUPS */
5815 #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
5817 wait_helper(pid_t pid
, int status
, struct rusage
*ru
)
5820 static PyObject
*struct_rusage
;
5823 return posix_error();
5825 if (struct_rusage
== NULL
) {
5826 PyObject
*m
= PyImport_ImportModuleNoBlock("resource");
5829 struct_rusage
= PyObject_GetAttrString(m
, "struct_rusage");
5831 if (struct_rusage
== NULL
)
5835 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
5836 result
= PyStructSequence_New((PyTypeObject
*) struct_rusage
);
5841 #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
5844 PyStructSequence_SET_ITEM(result
, 0,
5845 PyFloat_FromDouble(doubletime(ru
->ru_utime
)));
5846 PyStructSequence_SET_ITEM(result
, 1,
5847 PyFloat_FromDouble(doubletime(ru
->ru_stime
)));
5848 #define SET_INT(result, index, value)\
5849 PyStructSequence_SET_ITEM(result, index, PyInt_FromLong(value))
5850 SET_INT(result
, 2, ru
->ru_maxrss
);
5851 SET_INT(result
, 3, ru
->ru_ixrss
);
5852 SET_INT(result
, 4, ru
->ru_idrss
);
5853 SET_INT(result
, 5, ru
->ru_isrss
);
5854 SET_INT(result
, 6, ru
->ru_minflt
);
5855 SET_INT(result
, 7, ru
->ru_majflt
);
5856 SET_INT(result
, 8, ru
->ru_nswap
);
5857 SET_INT(result
, 9, ru
->ru_inblock
);
5858 SET_INT(result
, 10, ru
->ru_oublock
);
5859 SET_INT(result
, 11, ru
->ru_msgsnd
);
5860 SET_INT(result
, 12, ru
->ru_msgrcv
);
5861 SET_INT(result
, 13, ru
->ru_nsignals
);
5862 SET_INT(result
, 14, ru
->ru_nvcsw
);
5863 SET_INT(result
, 15, ru
->ru_nivcsw
);
5866 if (PyErr_Occurred()) {
5871 return Py_BuildValue("iiN", pid
, status
, result
);
5873 #endif /* HAVE_WAIT3 || HAVE_WAIT4 */
5876 PyDoc_STRVAR(posix_wait3__doc__
,
5877 "wait3(options) -> (pid, status, rusage)\n\n\
5878 Wait for completion of a child process.");
5881 posix_wait3(PyObject
*self
, PyObject
*args
)
5887 WAIT_STATUS_INT(status
) = 0;
5889 if (!PyArg_ParseTuple(args
, "i:wait3", &options
))
5892 Py_BEGIN_ALLOW_THREADS
5893 pid
= wait3(&status
, options
, &ru
);
5894 Py_END_ALLOW_THREADS
5896 return wait_helper(pid
, WAIT_STATUS_INT(status
), &ru
);
5898 #endif /* HAVE_WAIT3 */
5901 PyDoc_STRVAR(posix_wait4__doc__
,
5902 "wait4(pid, options) -> (pid, status, rusage)\n\n\
5903 Wait for completion of a given child process.");
5906 posix_wait4(PyObject
*self
, PyObject
*args
)
5912 WAIT_STATUS_INT(status
) = 0;
5914 if (!PyArg_ParseTuple(args
, "ii:wait4", &pid
, &options
))
5917 Py_BEGIN_ALLOW_THREADS
5918 pid
= wait4(pid
, &status
, options
, &ru
);
5919 Py_END_ALLOW_THREADS
5921 return wait_helper(pid
, WAIT_STATUS_INT(status
), &ru
);
5923 #endif /* HAVE_WAIT4 */
5926 PyDoc_STRVAR(posix_waitpid__doc__
,
5927 "waitpid(pid, options) -> (pid, status)\n\n\
5928 Wait for completion of a given child process.");
5931 posix_waitpid(PyObject
*self
, PyObject
*args
)
5936 WAIT_STATUS_INT(status
) = 0;
5938 if (!PyArg_ParseTuple(args
, "ii:waitpid", &pid
, &options
))
5940 Py_BEGIN_ALLOW_THREADS
5941 pid
= waitpid(pid
, &status
, options
);
5942 Py_END_ALLOW_THREADS
5944 return posix_error();
5946 return Py_BuildValue("ii", pid
, WAIT_STATUS_INT(status
));
5949 #elif defined(HAVE_CWAIT)
5951 /* MS C has a variant of waitpid() that's usable for most purposes. */
5952 PyDoc_STRVAR(posix_waitpid__doc__
,
5953 "waitpid(pid, options) -> (pid, status << 8)\n\n"
5954 "Wait for completion of a given process. options is ignored on Windows.");
5957 posix_waitpid(PyObject
*self
, PyObject
*args
)
5960 int status
, options
;
5962 if (!PyArg_ParseTuple(args
, "ii:waitpid", &pid
, &options
))
5964 Py_BEGIN_ALLOW_THREADS
5965 pid
= _cwait(&status
, pid
, options
);
5966 Py_END_ALLOW_THREADS
5968 return posix_error();
5970 /* shift the status left a byte so this is more like the POSIX waitpid */
5971 return Py_BuildValue("ii", pid
, status
<< 8);
5973 #endif /* HAVE_WAITPID || HAVE_CWAIT */
5976 PyDoc_STRVAR(posix_wait__doc__
,
5977 "wait() -> (pid, status)\n\n\
5978 Wait for completion of a child process.");
5981 posix_wait(PyObject
*self
, PyObject
*noargs
)
5985 WAIT_STATUS_INT(status
) = 0;
5987 Py_BEGIN_ALLOW_THREADS
5988 pid
= wait(&status
);
5989 Py_END_ALLOW_THREADS
5991 return posix_error();
5993 return Py_BuildValue("ii", pid
, WAIT_STATUS_INT(status
));
5998 PyDoc_STRVAR(posix_lstat__doc__
,
5999 "lstat(path) -> stat result\n\n\
6000 Like stat(path), but do not follow symbolic links.");
6003 posix_lstat(PyObject
*self
, PyObject
*args
)
6006 return posix_do_stat(self
, args
, "et:lstat", lstat
, NULL
, NULL
);
6007 #else /* !HAVE_LSTAT */
6009 return posix_do_stat(self
, args
, "et:lstat", STAT
, "U:lstat", win32_wstat
);
6011 return posix_do_stat(self
, args
, "et:lstat", STAT
, NULL
, NULL
);
6013 #endif /* !HAVE_LSTAT */
6017 #ifdef HAVE_READLINK
6018 PyDoc_STRVAR(posix_readlink__doc__
,
6019 "readlink(path) -> path\n\n\
6020 Return a string representing the path to which the symbolic link points.");
6023 posix_readlink(PyObject
*self
, PyObject
*args
)
6026 char buf
[MAXPATHLEN
];
6029 #ifdef Py_USING_UNICODE
6030 int arg_is_unicode
= 0;
6033 if (!PyArg_ParseTuple(args
, "et:readlink",
6034 Py_FileSystemDefaultEncoding
, &path
))
6036 #ifdef Py_USING_UNICODE
6037 v
= PySequence_GetItem(args
, 0);
6043 if (PyUnicode_Check(v
)) {
6049 Py_BEGIN_ALLOW_THREADS
6050 n
= readlink(path
, buf
, (int) sizeof buf
);
6051 Py_END_ALLOW_THREADS
6053 return posix_error_with_allocated_filename(path
);
6056 v
= PyString_FromStringAndSize(buf
, n
);
6057 #ifdef Py_USING_UNICODE
6058 if (arg_is_unicode
) {
6061 w
= PyUnicode_FromEncodedObject(v
,
6062 Py_FileSystemDefaultEncoding
,
6069 /* fall back to the original byte string, as
6070 discussed in patch #683592 */
6077 #endif /* HAVE_READLINK */
6081 PyDoc_STRVAR(posix_symlink__doc__
,
6082 "symlink(src, dst)\n\n\
6083 Create a symbolic link pointing to src named dst.");
6086 posix_symlink(PyObject
*self
, PyObject
*args
)
6088 return posix_2str(args
, "etet:symlink", symlink
);
6090 #endif /* HAVE_SYMLINK */
6094 #if defined(PYCC_VACPP) && defined(PYOS_OS2)
6100 Py_BEGIN_ALLOW_THREADS
6101 DosQuerySysInfo(QSV_MS_COUNT
, QSV_MS_COUNT
, &value
, sizeof(value
));
6102 Py_END_ALLOW_THREADS
6108 posix_times(PyObject
*self
, PyObject
*noargs
)
6110 /* Currently Only Uptime is Provided -- Others Later */
6111 return Py_BuildValue("ddddd",
6112 (double)0 /* t.tms_utime / HZ */,
6113 (double)0 /* t.tms_stime / HZ */,
6114 (double)0 /* t.tms_cutime / HZ */,
6115 (double)0 /* t.tms_cstime / HZ */,
6116 (double)system_uptime() / 1000);
6119 #define NEED_TICKS_PER_SECOND
6120 static long ticks_per_second
= -1;
6122 posix_times(PyObject
*self
, PyObject
*noargs
)
6128 if (c
== (clock_t) -1)
6129 return posix_error();
6130 return Py_BuildValue("ddddd",
6131 (double)t
.tms_utime
/ ticks_per_second
,
6132 (double)t
.tms_stime
/ ticks_per_second
,
6133 (double)t
.tms_cutime
/ ticks_per_second
,
6134 (double)t
.tms_cstime
/ ticks_per_second
,
6135 (double)c
/ ticks_per_second
);
6137 #endif /* not OS2 */
6138 #endif /* HAVE_TIMES */
6142 #define HAVE_TIMES /* so the method table will pick it up */
6144 posix_times(PyObject
*self
, PyObject
*noargs
)
6146 FILETIME create
, exit
, kernel
, user
;
6148 hProc
= GetCurrentProcess();
6149 GetProcessTimes(hProc
, &create
, &exit
, &kernel
, &user
);
6150 /* The fields of a FILETIME structure are the hi and lo part
6151 of a 64-bit value expressed in 100 nanosecond units.
6152 1e7 is one second in such units; 1e-7 the inverse.
6153 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
6155 return Py_BuildValue(
6157 (double)(user
.dwHighDateTime
*429.4967296 +
6158 user
.dwLowDateTime
*1e-7),
6159 (double)(kernel
.dwHighDateTime
*429.4967296 +
6160 kernel
.dwLowDateTime
*1e-7),
6165 #endif /* MS_WINDOWS */
6168 PyDoc_STRVAR(posix_times__doc__
,
6169 "times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
6170 Return a tuple of floating point numbers indicating process times.");
6175 PyDoc_STRVAR(posix_getsid__doc__
,
6176 "getsid(pid) -> sid\n\n\
6177 Call the system call getsid().");
6180 posix_getsid(PyObject
*self
, PyObject
*args
)
6184 if (!PyArg_ParseTuple(args
, "i:getsid", &pid
))
6188 return posix_error();
6189 return PyInt_FromLong((long)sid
);
6191 #endif /* HAVE_GETSID */
6195 PyDoc_STRVAR(posix_setsid__doc__
,
6197 Call the system call setsid().");
6200 posix_setsid(PyObject
*self
, PyObject
*noargs
)
6203 return posix_error();
6207 #endif /* HAVE_SETSID */
6210 PyDoc_STRVAR(posix_setpgid__doc__
,
6211 "setpgid(pid, pgrp)\n\n\
6212 Call the system call setpgid().");
6215 posix_setpgid(PyObject
*self
, PyObject
*args
)
6219 if (!PyArg_ParseTuple(args
, "ii:setpgid", &pid
, &pgrp
))
6221 if (setpgid(pid
, pgrp
) < 0)
6222 return posix_error();
6226 #endif /* HAVE_SETPGID */
6229 #ifdef HAVE_TCGETPGRP
6230 PyDoc_STRVAR(posix_tcgetpgrp__doc__
,
6231 "tcgetpgrp(fd) -> pgid\n\n\
6232 Return the process group associated with the terminal given by a fd.");
6235 posix_tcgetpgrp(PyObject
*self
, PyObject
*args
)
6239 if (!PyArg_ParseTuple(args
, "i:tcgetpgrp", &fd
))
6241 pgid
= tcgetpgrp(fd
);
6243 return posix_error();
6244 return PyInt_FromLong((long)pgid
);
6246 #endif /* HAVE_TCGETPGRP */
6249 #ifdef HAVE_TCSETPGRP
6250 PyDoc_STRVAR(posix_tcsetpgrp__doc__
,
6251 "tcsetpgrp(fd, pgid)\n\n\
6252 Set the process group associated with the terminal given by a fd.");
6255 posix_tcsetpgrp(PyObject
*self
, PyObject
*args
)
6258 if (!PyArg_ParseTuple(args
, "ii:tcsetpgrp", &fd
, &pgid
))
6260 if (tcsetpgrp(fd
, pgid
) < 0)
6261 return posix_error();
6265 #endif /* HAVE_TCSETPGRP */
6267 /* Functions acting on file descriptors */
6269 PyDoc_STRVAR(posix_open__doc__
,
6270 "open(filename, flag [, mode=0777]) -> fd\n\n\
6271 Open a file (for low level IO).");
6274 posix_open(PyObject
*self
, PyObject
*args
)
6282 if (unicode_file_names()) {
6283 PyUnicodeObject
*po
;
6284 if (PyArg_ParseTuple(args
, "Ui|i:mkdir", &po
, &flag
, &mode
)) {
6285 Py_BEGIN_ALLOW_THREADS
6286 /* PyUnicode_AS_UNICODE OK without thread
6287 lock as it is a simple dereference. */
6288 fd
= _wopen(PyUnicode_AS_UNICODE(po
), flag
, mode
);
6289 Py_END_ALLOW_THREADS
6291 return posix_error();
6292 return PyInt_FromLong((long)fd
);
6294 /* Drop the argument parsing error as narrow strings
6300 if (!PyArg_ParseTuple(args
, "eti|i",
6301 Py_FileSystemDefaultEncoding
, &file
,
6305 Py_BEGIN_ALLOW_THREADS
6306 fd
= open(file
, flag
, mode
);
6307 Py_END_ALLOW_THREADS
6309 return posix_error_with_allocated_filename(file
);
6311 return PyInt_FromLong((long)fd
);
6315 PyDoc_STRVAR(posix_close__doc__
,
6317 Close a file descriptor (for low level IO).");
6320 posix_close(PyObject
*self
, PyObject
*args
)
6323 if (!PyArg_ParseTuple(args
, "i:close", &fd
))
6325 if (!_PyVerify_fd(fd
))
6326 return posix_error();
6327 Py_BEGIN_ALLOW_THREADS
6329 Py_END_ALLOW_THREADS
6331 return posix_error();
6337 PyDoc_STRVAR(posix_closerange__doc__
,
6338 "closerange(fd_low, fd_high)\n\n\
6339 Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
6342 posix_closerange(PyObject
*self
, PyObject
*args
)
6344 int fd_from
, fd_to
, i
;
6345 if (!PyArg_ParseTuple(args
, "ii:closerange", &fd_from
, &fd_to
))
6347 Py_BEGIN_ALLOW_THREADS
6348 for (i
= fd_from
; i
< fd_to
; i
++)
6349 if (_PyVerify_fd(i
))
6351 Py_END_ALLOW_THREADS
6356 PyDoc_STRVAR(posix_dup__doc__
,
6357 "dup(fd) -> fd2\n\n\
6358 Return a duplicate of a file descriptor.");
6361 posix_dup(PyObject
*self
, PyObject
*args
)
6364 if (!PyArg_ParseTuple(args
, "i:dup", &fd
))
6366 if (!_PyVerify_fd(fd
))
6367 return posix_error();
6368 Py_BEGIN_ALLOW_THREADS
6370 Py_END_ALLOW_THREADS
6372 return posix_error();
6373 return PyInt_FromLong((long)fd
);
6377 PyDoc_STRVAR(posix_dup2__doc__
,
6378 "dup2(old_fd, new_fd)\n\n\
6379 Duplicate file descriptor.");
6382 posix_dup2(PyObject
*self
, PyObject
*args
)
6385 if (!PyArg_ParseTuple(args
, "ii:dup2", &fd
, &fd2
))
6387 if (!_PyVerify_fd_dup2(fd
, fd2
))
6388 return posix_error();
6389 Py_BEGIN_ALLOW_THREADS
6390 res
= dup2(fd
, fd2
);
6391 Py_END_ALLOW_THREADS
6393 return posix_error();
6399 PyDoc_STRVAR(posix_lseek__doc__
,
6400 "lseek(fd, pos, how) -> newpos\n\n\
6401 Set the current position of a file descriptor.");
6404 posix_lseek(PyObject
*self
, PyObject
*args
)
6407 #if defined(MS_WIN64) || defined(MS_WINDOWS)
6408 PY_LONG_LONG pos
, res
;
6413 if (!PyArg_ParseTuple(args
, "iOi:lseek", &fd
, &posobj
, &how
))
6416 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
6418 case 0: how
= SEEK_SET
; break;
6419 case 1: how
= SEEK_CUR
; break;
6420 case 2: how
= SEEK_END
; break;
6422 #endif /* SEEK_END */
6424 #if !defined(HAVE_LARGEFILE_SUPPORT)
6425 pos
= PyInt_AsLong(posobj
);
6427 pos
= PyLong_Check(posobj
) ?
6428 PyLong_AsLongLong(posobj
) : PyInt_AsLong(posobj
);
6430 if (PyErr_Occurred())
6433 if (!_PyVerify_fd(fd
))
6434 return posix_error();
6435 Py_BEGIN_ALLOW_THREADS
6436 #if defined(MS_WIN64) || defined(MS_WINDOWS)
6437 res
= _lseeki64(fd
, pos
, how
);
6439 res
= lseek(fd
, pos
, how
);
6441 Py_END_ALLOW_THREADS
6443 return posix_error();
6445 #if !defined(HAVE_LARGEFILE_SUPPORT)
6446 return PyInt_FromLong(res
);
6448 return PyLong_FromLongLong(res
);
6453 PyDoc_STRVAR(posix_read__doc__
,
6454 "read(fd, buffersize) -> string\n\n\
6455 Read a file descriptor.");
6458 posix_read(PyObject
*self
, PyObject
*args
)
6462 if (!PyArg_ParseTuple(args
, "ii:read", &fd
, &size
))
6466 return posix_error();
6468 buffer
= PyString_FromStringAndSize((char *)NULL
, size
);
6471 if (!_PyVerify_fd(fd
))
6472 return posix_error();
6473 Py_BEGIN_ALLOW_THREADS
6474 n
= read(fd
, PyString_AsString(buffer
), size
);
6475 Py_END_ALLOW_THREADS
6478 return posix_error();
6481 _PyString_Resize(&buffer
, n
);
6486 PyDoc_STRVAR(posix_write__doc__
,
6487 "write(fd, string) -> byteswritten\n\n\
6488 Write a string to a file descriptor.");
6491 posix_write(PyObject
*self
, PyObject
*args
)
6497 if (!PyArg_ParseTuple(args
, "is*:write", &fd
, &pbuf
))
6499 if (!_PyVerify_fd(fd
))
6500 return posix_error();
6501 Py_BEGIN_ALLOW_THREADS
6502 size
= write(fd
, pbuf
.buf
, (size_t)pbuf
.len
);
6503 Py_END_ALLOW_THREADS
6504 PyBuffer_Release(&pbuf
);
6506 return posix_error();
6507 return PyInt_FromSsize_t(size
);
6511 PyDoc_STRVAR(posix_fstat__doc__
,
6512 "fstat(fd) -> stat result\n\n\
6513 Like stat(), but for an open file descriptor.");
6516 posix_fstat(PyObject
*self
, PyObject
*args
)
6521 if (!PyArg_ParseTuple(args
, "i:fstat", &fd
))
6524 /* on OpenVMS we must ensure that all bytes are written to the file */
6527 if (!_PyVerify_fd(fd
))
6528 return posix_error();
6529 Py_BEGIN_ALLOW_THREADS
6530 res
= FSTAT(fd
, &st
);
6531 Py_END_ALLOW_THREADS
6534 return win32_error("fstat", NULL
);
6536 return posix_error();
6540 return _pystat_fromstructstat(&st
);
6544 PyDoc_STRVAR(posix_fdopen__doc__
,
6545 "fdopen(fd [, mode='r' [, bufsize]]) -> file_object\n\n\
6546 Return an open file object connected to a file descriptor.");
6549 posix_fdopen(PyObject
*self
, PyObject
*args
)
6552 char *orgmode
= "r";
6557 if (!PyArg_ParseTuple(args
, "i|si", &fd
, &orgmode
, &bufsize
))
6560 /* Sanitize mode. See fileobject.c */
6561 mode
= PyMem_MALLOC(strlen(orgmode
)+3);
6566 strcpy(mode
, orgmode
);
6567 if (_PyFile_SanitizeMode(mode
)) {
6571 if (!_PyVerify_fd(fd
))
6572 return posix_error();
6573 Py_BEGIN_ALLOW_THREADS
6574 #if !defined(MS_WINDOWS) && defined(HAVE_FCNTL_H)
6575 if (mode
[0] == 'a') {
6576 /* try to make sure the O_APPEND flag is set */
6578 flags
= fcntl(fd
, F_GETFL
);
6580 fcntl(fd
, F_SETFL
, flags
| O_APPEND
);
6581 fp
= fdopen(fd
, mode
);
6582 if (fp
== NULL
&& flags
!= -1)
6583 /* restore old mode if fdopen failed */
6584 fcntl(fd
, F_SETFL
, flags
);
6586 fp
= fdopen(fd
, mode
);
6589 fp
= fdopen(fd
, mode
);
6591 Py_END_ALLOW_THREADS
6594 return posix_error();
6595 f
= PyFile_FromFile(fp
, "<fdopen>", orgmode
, fclose
);
6597 PyFile_SetBufSize(f
, bufsize
);
6601 PyDoc_STRVAR(posix_isatty__doc__
,
6602 "isatty(fd) -> bool\n\n\
6603 Return True if the file descriptor 'fd' is an open file descriptor\n\
6604 connected to the slave end of a terminal.");
6607 posix_isatty(PyObject
*self
, PyObject
*args
)
6610 if (!PyArg_ParseTuple(args
, "i:isatty", &fd
))
6612 if (!_PyVerify_fd(fd
))
6613 return PyBool_FromLong(0);
6614 return PyBool_FromLong(isatty(fd
));
6618 PyDoc_STRVAR(posix_pipe__doc__
,
6619 "pipe() -> (read_end, write_end)\n\n\
6623 posix_pipe(PyObject
*self
, PyObject
*noargs
)
6625 #if defined(PYOS_OS2)
6629 Py_BEGIN_ALLOW_THREADS
6630 rc
= DosCreatePipe( &read
, &write
, 4096);
6631 Py_END_ALLOW_THREADS
6633 return os2_error(rc
);
6635 return Py_BuildValue("(ii)", read
, write
);
6637 #if !defined(MS_WINDOWS)
6640 Py_BEGIN_ALLOW_THREADS
6642 Py_END_ALLOW_THREADS
6644 return posix_error();
6645 return Py_BuildValue("(ii)", fds
[0], fds
[1]);
6646 #else /* MS_WINDOWS */
6648 int read_fd
, write_fd
;
6650 Py_BEGIN_ALLOW_THREADS
6651 ok
= CreatePipe(&read
, &write
, NULL
, 0);
6652 Py_END_ALLOW_THREADS
6654 return win32_error("CreatePipe", NULL
);
6655 read_fd
= _open_osfhandle((Py_intptr_t
)read
, 0);
6656 write_fd
= _open_osfhandle((Py_intptr_t
)write
, 1);
6657 return Py_BuildValue("(ii)", read_fd
, write_fd
);
6658 #endif /* MS_WINDOWS */
6661 #endif /* HAVE_PIPE */
6665 PyDoc_STRVAR(posix_mkfifo__doc__
,
6666 "mkfifo(filename [, mode=0666])\n\n\
6667 Create a FIFO (a POSIX named pipe).");
6670 posix_mkfifo(PyObject
*self
, PyObject
*args
)
6675 if (!PyArg_ParseTuple(args
, "s|i:mkfifo", &filename
, &mode
))
6677 Py_BEGIN_ALLOW_THREADS
6678 res
= mkfifo(filename
, mode
);
6679 Py_END_ALLOW_THREADS
6681 return posix_error();
6688 #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
6689 PyDoc_STRVAR(posix_mknod__doc__
,
6690 "mknod(filename [, mode=0600, device])\n\n\
6691 Create a filesystem node (file, device special file or named pipe)\n\
6692 named filename. mode specifies both the permissions to use and the\n\
6693 type of node to be created, being combined (bitwise OR) with one of\n\
6694 S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
6695 device defines the newly created device special file (probably using\n\
6696 os.makedev()), otherwise it is ignored.");
6700 posix_mknod(PyObject
*self
, PyObject
*args
)
6706 if (!PyArg_ParseTuple(args
, "s|ii:mknod", &filename
, &mode
, &device
))
6708 Py_BEGIN_ALLOW_THREADS
6709 res
= mknod(filename
, mode
, device
);
6710 Py_END_ALLOW_THREADS
6712 return posix_error();
6718 #ifdef HAVE_DEVICE_MACROS
6719 PyDoc_STRVAR(posix_major__doc__
,
6720 "major(device) -> major number\n\
6721 Extracts a device major number from a raw device number.");
6724 posix_major(PyObject
*self
, PyObject
*args
)
6727 if (!PyArg_ParseTuple(args
, "i:major", &device
))
6729 return PyInt_FromLong((long)major(device
));
6732 PyDoc_STRVAR(posix_minor__doc__
,
6733 "minor(device) -> minor number\n\
6734 Extracts a device minor number from a raw device number.");
6737 posix_minor(PyObject
*self
, PyObject
*args
)
6740 if (!PyArg_ParseTuple(args
, "i:minor", &device
))
6742 return PyInt_FromLong((long)minor(device
));
6745 PyDoc_STRVAR(posix_makedev__doc__
,
6746 "makedev(major, minor) -> device number\n\
6747 Composes a raw device number from the major and minor device numbers.");
6750 posix_makedev(PyObject
*self
, PyObject
*args
)
6753 if (!PyArg_ParseTuple(args
, "ii:makedev", &major
, &minor
))
6755 return PyInt_FromLong((long)makedev(major
, minor
));
6757 #endif /* device macros */
6760 #ifdef HAVE_FTRUNCATE
6761 PyDoc_STRVAR(posix_ftruncate__doc__
,
6762 "ftruncate(fd, length)\n\n\
6763 Truncate a file to a specified length.");
6766 posix_ftruncate(PyObject
*self
, PyObject
*args
)
6773 if (!PyArg_ParseTuple(args
, "iO:ftruncate", &fd
, &lenobj
))
6776 #if !defined(HAVE_LARGEFILE_SUPPORT)
6777 length
= PyInt_AsLong(lenobj
);
6779 length
= PyLong_Check(lenobj
) ?
6780 PyLong_AsLongLong(lenobj
) : PyInt_AsLong(lenobj
);
6782 if (PyErr_Occurred())
6785 Py_BEGIN_ALLOW_THREADS
6786 res
= ftruncate(fd
, length
);
6787 Py_END_ALLOW_THREADS
6789 return posix_error();
6796 PyDoc_STRVAR(posix_putenv__doc__
,
6797 "putenv(key, value)\n\n\
6798 Change or add an environment variable.");
6800 /* Save putenv() parameters as values here, so we can collect them when they
6801 * get re-set with another call for the same key. */
6802 static PyObject
*posix_putenv_garbage
;
6805 posix_putenv(PyObject
*self
, PyObject
*args
)
6812 if (!PyArg_ParseTuple(args
, "ss:putenv", &s1
, &s2
))
6815 #if defined(PYOS_OS2)
6816 if (stricmp(s1
, "BEGINLIBPATH") == 0) {
6819 rc
= DosSetExtLIBPATH(s2
, BEGIN_LIBPATH
);
6821 return os2_error(rc
);
6823 } else if (stricmp(s1
, "ENDLIBPATH") == 0) {
6826 rc
= DosSetExtLIBPATH(s2
, END_LIBPATH
);
6828 return os2_error(rc
);
6832 /* XXX This can leak memory -- not easy to fix :-( */
6833 len
= strlen(s1
) + strlen(s2
) + 2;
6834 /* len includes space for a trailing \0; the size arg to
6835 PyString_FromStringAndSize does not count that */
6836 newstr
= PyString_FromStringAndSize(NULL
, (int)len
- 1);
6838 return PyErr_NoMemory();
6839 newenv
= PyString_AS_STRING(newstr
);
6840 PyOS_snprintf(newenv
, len
, "%s=%s", s1
, s2
);
6841 if (putenv(newenv
)) {
6846 /* Install the first arg and newstr in posix_putenv_garbage;
6847 * this will cause previous value to be collected. This has to
6848 * happen after the real putenv() call because the old value
6849 * was still accessible until then. */
6850 if (PyDict_SetItem(posix_putenv_garbage
,
6851 PyTuple_GET_ITEM(args
, 0), newstr
)) {
6852 /* really not much we can do; just leak */
6859 #if defined(PYOS_OS2)
6867 #ifdef HAVE_UNSETENV
6868 PyDoc_STRVAR(posix_unsetenv__doc__
,
6870 Delete an environment variable.");
6873 posix_unsetenv(PyObject
*self
, PyObject
*args
)
6877 if (!PyArg_ParseTuple(args
, "s:unsetenv", &s1
))
6882 /* Remove the key from posix_putenv_garbage;
6883 * this will cause it to be collected. This has to
6884 * happen after the real unsetenv() call because the
6885 * old value was still accessible until then.
6887 if (PyDict_DelItem(posix_putenv_garbage
,
6888 PyTuple_GET_ITEM(args
, 0))) {
6889 /* really not much we can do; just leak */
6896 #endif /* unsetenv */
6898 PyDoc_STRVAR(posix_strerror__doc__
,
6899 "strerror(code) -> string\n\n\
6900 Translate an error code to a message string.");
6903 posix_strerror(PyObject
*self
, PyObject
*args
)
6907 if (!PyArg_ParseTuple(args
, "i:strerror", &code
))
6909 message
= strerror(code
);
6910 if (message
== NULL
) {
6911 PyErr_SetString(PyExc_ValueError
,
6912 "strerror() argument out of range");
6915 return PyString_FromString(message
);
6919 #ifdef HAVE_SYS_WAIT_H
6922 PyDoc_STRVAR(posix_WCOREDUMP__doc__
,
6923 "WCOREDUMP(status) -> bool\n\n\
6924 Return True if the process returning 'status' was dumped to a core file.");
6927 posix_WCOREDUMP(PyObject
*self
, PyObject
*args
)
6930 WAIT_STATUS_INT(status
) = 0;
6932 if (!PyArg_ParseTuple(args
, "i:WCOREDUMP", &WAIT_STATUS_INT(status
)))
6935 return PyBool_FromLong(WCOREDUMP(status
));
6937 #endif /* WCOREDUMP */
6940 PyDoc_STRVAR(posix_WIFCONTINUED__doc__
,
6941 "WIFCONTINUED(status) -> bool\n\n\
6942 Return True if the process returning 'status' was continued from a\n\
6943 job control stop.");
6946 posix_WIFCONTINUED(PyObject
*self
, PyObject
*args
)
6949 WAIT_STATUS_INT(status
) = 0;
6951 if (!PyArg_ParseTuple(args
, "i:WCONTINUED", &WAIT_STATUS_INT(status
)))
6954 return PyBool_FromLong(WIFCONTINUED(status
));
6956 #endif /* WIFCONTINUED */
6959 PyDoc_STRVAR(posix_WIFSTOPPED__doc__
,
6960 "WIFSTOPPED(status) -> bool\n\n\
6961 Return True if the process returning 'status' was stopped.");
6964 posix_WIFSTOPPED(PyObject
*self
, PyObject
*args
)
6967 WAIT_STATUS_INT(status
) = 0;
6969 if (!PyArg_ParseTuple(args
, "i:WIFSTOPPED", &WAIT_STATUS_INT(status
)))
6972 return PyBool_FromLong(WIFSTOPPED(status
));
6974 #endif /* WIFSTOPPED */
6977 PyDoc_STRVAR(posix_WIFSIGNALED__doc__
,
6978 "WIFSIGNALED(status) -> bool\n\n\
6979 Return True if the process returning 'status' was terminated by a signal.");
6982 posix_WIFSIGNALED(PyObject
*self
, PyObject
*args
)
6985 WAIT_STATUS_INT(status
) = 0;
6987 if (!PyArg_ParseTuple(args
, "i:WIFSIGNALED", &WAIT_STATUS_INT(status
)))
6990 return PyBool_FromLong(WIFSIGNALED(status
));
6992 #endif /* WIFSIGNALED */
6995 PyDoc_STRVAR(posix_WIFEXITED__doc__
,
6996 "WIFEXITED(status) -> bool\n\n\
6997 Return true if the process returning 'status' exited using the exit()\n\
7001 posix_WIFEXITED(PyObject
*self
, PyObject
*args
)
7004 WAIT_STATUS_INT(status
) = 0;
7006 if (!PyArg_ParseTuple(args
, "i:WIFEXITED", &WAIT_STATUS_INT(status
)))
7009 return PyBool_FromLong(WIFEXITED(status
));
7011 #endif /* WIFEXITED */
7014 PyDoc_STRVAR(posix_WEXITSTATUS__doc__
,
7015 "WEXITSTATUS(status) -> integer\n\n\
7016 Return the process return code from 'status'.");
7019 posix_WEXITSTATUS(PyObject
*self
, PyObject
*args
)
7022 WAIT_STATUS_INT(status
) = 0;
7024 if (!PyArg_ParseTuple(args
, "i:WEXITSTATUS", &WAIT_STATUS_INT(status
)))
7027 return Py_BuildValue("i", WEXITSTATUS(status
));
7029 #endif /* WEXITSTATUS */
7032 PyDoc_STRVAR(posix_WTERMSIG__doc__
,
7033 "WTERMSIG(status) -> integer\n\n\
7034 Return the signal that terminated the process that provided the 'status'\n\
7038 posix_WTERMSIG(PyObject
*self
, PyObject
*args
)
7041 WAIT_STATUS_INT(status
) = 0;
7043 if (!PyArg_ParseTuple(args
, "i:WTERMSIG", &WAIT_STATUS_INT(status
)))
7046 return Py_BuildValue("i", WTERMSIG(status
));
7048 #endif /* WTERMSIG */
7051 PyDoc_STRVAR(posix_WSTOPSIG__doc__
,
7052 "WSTOPSIG(status) -> integer\n\n\
7053 Return the signal that stopped the process that provided\n\
7054 the 'status' value.");
7057 posix_WSTOPSIG(PyObject
*self
, PyObject
*args
)
7060 WAIT_STATUS_INT(status
) = 0;
7062 if (!PyArg_ParseTuple(args
, "i:WSTOPSIG", &WAIT_STATUS_INT(status
)))
7065 return Py_BuildValue("i", WSTOPSIG(status
));
7067 #endif /* WSTOPSIG */
7069 #endif /* HAVE_SYS_WAIT_H */
7072 #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
7074 /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
7075 needed definitions in sys/statvfs.h */
7078 #include <sys/statvfs.h>
7081 _pystatvfs_fromstructstatvfs(struct statvfs st
) {
7082 PyObject
*v
= PyStructSequence_New(&StatVFSResultType
);
7086 #if !defined(HAVE_LARGEFILE_SUPPORT)
7087 PyStructSequence_SET_ITEM(v
, 0, PyInt_FromLong((long) st
.f_bsize
));
7088 PyStructSequence_SET_ITEM(v
, 1, PyInt_FromLong((long) st
.f_frsize
));
7089 PyStructSequence_SET_ITEM(v
, 2, PyInt_FromLong((long) st
.f_blocks
));
7090 PyStructSequence_SET_ITEM(v
, 3, PyInt_FromLong((long) st
.f_bfree
));
7091 PyStructSequence_SET_ITEM(v
, 4, PyInt_FromLong((long) st
.f_bavail
));
7092 PyStructSequence_SET_ITEM(v
, 5, PyInt_FromLong((long) st
.f_files
));
7093 PyStructSequence_SET_ITEM(v
, 6, PyInt_FromLong((long) st
.f_ffree
));
7094 PyStructSequence_SET_ITEM(v
, 7, PyInt_FromLong((long) st
.f_favail
));
7095 PyStructSequence_SET_ITEM(v
, 8, PyInt_FromLong((long) st
.f_flag
));
7096 PyStructSequence_SET_ITEM(v
, 9, PyInt_FromLong((long) st
.f_namemax
));
7098 PyStructSequence_SET_ITEM(v
, 0, PyInt_FromLong((long) st
.f_bsize
));
7099 PyStructSequence_SET_ITEM(v
, 1, PyInt_FromLong((long) st
.f_frsize
));
7100 PyStructSequence_SET_ITEM(v
, 2,
7101 PyLong_FromLongLong((PY_LONG_LONG
) st
.f_blocks
));
7102 PyStructSequence_SET_ITEM(v
, 3,
7103 PyLong_FromLongLong((PY_LONG_LONG
) st
.f_bfree
));
7104 PyStructSequence_SET_ITEM(v
, 4,
7105 PyLong_FromLongLong((PY_LONG_LONG
) st
.f_bavail
));
7106 PyStructSequence_SET_ITEM(v
, 5,
7107 PyLong_FromLongLong((PY_LONG_LONG
) st
.f_files
));
7108 PyStructSequence_SET_ITEM(v
, 6,
7109 PyLong_FromLongLong((PY_LONG_LONG
) st
.f_ffree
));
7110 PyStructSequence_SET_ITEM(v
, 7,
7111 PyLong_FromLongLong((PY_LONG_LONG
) st
.f_favail
));
7112 PyStructSequence_SET_ITEM(v
, 8, PyInt_FromLong((long) st
.f_flag
));
7113 PyStructSequence_SET_ITEM(v
, 9, PyInt_FromLong((long) st
.f_namemax
));
7119 PyDoc_STRVAR(posix_fstatvfs__doc__
,
7120 "fstatvfs(fd) -> statvfs result\n\n\
7121 Perform an fstatvfs system call on the given fd.");
7124 posix_fstatvfs(PyObject
*self
, PyObject
*args
)
7129 if (!PyArg_ParseTuple(args
, "i:fstatvfs", &fd
))
7131 Py_BEGIN_ALLOW_THREADS
7132 res
= fstatvfs(fd
, &st
);
7133 Py_END_ALLOW_THREADS
7135 return posix_error();
7137 return _pystatvfs_fromstructstatvfs(st
);
7139 #endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
7142 #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
7143 #include <sys/statvfs.h>
7145 PyDoc_STRVAR(posix_statvfs__doc__
,
7146 "statvfs(path) -> statvfs result\n\n\
7147 Perform a statvfs system call on the given path.");
7150 posix_statvfs(PyObject
*self
, PyObject
*args
)
7155 if (!PyArg_ParseTuple(args
, "s:statvfs", &path
))
7157 Py_BEGIN_ALLOW_THREADS
7158 res
= statvfs(path
, &st
);
7159 Py_END_ALLOW_THREADS
7161 return posix_error_with_filename(path
);
7163 return _pystatvfs_fromstructstatvfs(st
);
7165 #endif /* HAVE_STATVFS */
7169 PyDoc_STRVAR(posix_tempnam__doc__
,
7170 "tempnam([dir[, prefix]]) -> string\n\n\
7171 Return a unique name for a temporary file.\n\
7172 The directory and a prefix may be specified as strings; they may be omitted\n\
7173 or None if not needed.");
7176 posix_tempnam(PyObject
*self
, PyObject
*args
)
7178 PyObject
*result
= NULL
;
7183 if (!PyArg_ParseTuple(args
, "|zz:tempnam", &dir
, &pfx
))
7186 if (PyErr_Warn(PyExc_RuntimeWarning
,
7187 "tempnam is a potential security risk to your program") < 0)
7191 name
= _tempnam(dir
, pfx
);
7193 name
= tempnam(dir
, pfx
);
7196 return PyErr_NoMemory();
7197 result
= PyString_FromString(name
);
7205 PyDoc_STRVAR(posix_tmpfile__doc__
,
7206 "tmpfile() -> file object\n\n\
7207 Create a temporary file with no directory entries.");
7210 posix_tmpfile(PyObject
*self
, PyObject
*noargs
)
7216 return posix_error();
7217 return PyFile_FromFile(fp
, "<tmpfile>", "w+b", fclose
);
7223 PyDoc_STRVAR(posix_tmpnam__doc__
,
7224 "tmpnam() -> string\n\n\
7225 Return a unique name for a temporary file.");
7228 posix_tmpnam(PyObject
*self
, PyObject
*noargs
)
7230 char buffer
[L_tmpnam
];
7233 if (PyErr_Warn(PyExc_RuntimeWarning
,
7234 "tmpnam is a potential security risk to your program") < 0)
7238 name
= tmpnam_r(buffer
);
7240 name
= tmpnam(buffer
);
7243 PyObject
*err
= Py_BuildValue("is", 0,
7245 "unexpected NULL from tmpnam_r"
7247 "unexpected NULL from tmpnam"
7250 PyErr_SetObject(PyExc_OSError
, err
);
7254 return PyString_FromString(buffer
);
7259 /* This is used for fpathconf(), pathconf(), confstr() and sysconf().
7260 * It maps strings representing configuration variable names to
7261 * integer values, allowing those functions to be called with the
7262 * magic names instead of polluting the module's namespace with tons of
7263 * rarely-used constants. There are three separate tables that use
7264 * these definitions.
7266 * This code is always included, even if none of the interfaces that
7267 * need it are included. The #if hackery needed to avoid it would be
7268 * sufficiently pervasive that it's not worth the loss of readability.
7276 conv_confname(PyObject
*arg
, int *valuep
, struct constdef
*table
,
7279 if (PyInt_Check(arg
)) {
7280 *valuep
= PyInt_AS_LONG(arg
);
7283 if (PyString_Check(arg
)) {
7284 /* look up the value in the table using a binary search */
7287 size_t hi
= tablesize
;
7289 char *confname
= PyString_AS_STRING(arg
);
7291 mid
= (lo
+ hi
) / 2;
7292 cmp
= strcmp(confname
, table
[mid
].name
);
7298 *valuep
= table
[mid
].value
;
7302 PyErr_SetString(PyExc_ValueError
, "unrecognized configuration name");
7305 PyErr_SetString(PyExc_TypeError
,
7306 "configuration names must be strings or integers");
7311 #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
7312 static struct constdef posix_constants_pathconf
[] = {
7313 #ifdef _PC_ABI_AIO_XFER_MAX
7314 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX
},
7316 #ifdef _PC_ABI_ASYNC_IO
7317 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO
},
7320 {"PC_ASYNC_IO", _PC_ASYNC_IO
},
7322 #ifdef _PC_CHOWN_RESTRICTED
7323 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED
},
7325 #ifdef _PC_FILESIZEBITS
7326 {"PC_FILESIZEBITS", _PC_FILESIZEBITS
},
7329 {"PC_LAST", _PC_LAST
},
7332 {"PC_LINK_MAX", _PC_LINK_MAX
},
7334 #ifdef _PC_MAX_CANON
7335 {"PC_MAX_CANON", _PC_MAX_CANON
},
7337 #ifdef _PC_MAX_INPUT
7338 {"PC_MAX_INPUT", _PC_MAX_INPUT
},
7341 {"PC_NAME_MAX", _PC_NAME_MAX
},
7344 {"PC_NO_TRUNC", _PC_NO_TRUNC
},
7347 {"PC_PATH_MAX", _PC_PATH_MAX
},
7350 {"PC_PIPE_BUF", _PC_PIPE_BUF
},
7353 {"PC_PRIO_IO", _PC_PRIO_IO
},
7355 #ifdef _PC_SOCK_MAXBUF
7356 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF
},
7359 {"PC_SYNC_IO", _PC_SYNC_IO
},
7362 {"PC_VDISABLE", _PC_VDISABLE
},
7367 conv_path_confname(PyObject
*arg
, int *valuep
)
7369 return conv_confname(arg
, valuep
, posix_constants_pathconf
,
7370 sizeof(posix_constants_pathconf
)
7371 / sizeof(struct constdef
));
7375 #ifdef HAVE_FPATHCONF
7376 PyDoc_STRVAR(posix_fpathconf__doc__
,
7377 "fpathconf(fd, name) -> integer\n\n\
7378 Return the configuration limit name for the file descriptor fd.\n\
7379 If there is no limit, return -1.");
7382 posix_fpathconf(PyObject
*self
, PyObject
*args
)
7384 PyObject
*result
= NULL
;
7387 if (PyArg_ParseTuple(args
, "iO&:fpathconf", &fd
,
7388 conv_path_confname
, &name
)) {
7392 limit
= fpathconf(fd
, name
);
7393 if (limit
== -1 && errno
!= 0)
7396 result
= PyInt_FromLong(limit
);
7403 #ifdef HAVE_PATHCONF
7404 PyDoc_STRVAR(posix_pathconf__doc__
,
7405 "pathconf(path, name) -> integer\n\n\
7406 Return the configuration limit name for the file or directory path.\n\
7407 If there is no limit, return -1.");
7410 posix_pathconf(PyObject
*self
, PyObject
*args
)
7412 PyObject
*result
= NULL
;
7416 if (PyArg_ParseTuple(args
, "sO&:pathconf", &path
,
7417 conv_path_confname
, &name
)) {
7421 limit
= pathconf(path
, name
);
7422 if (limit
== -1 && errno
!= 0) {
7423 if (errno
== EINVAL
)
7424 /* could be a path or name problem */
7427 posix_error_with_filename(path
);
7430 result
= PyInt_FromLong(limit
);
7437 static struct constdef posix_constants_confstr
[] = {
7438 #ifdef _CS_ARCHITECTURE
7439 {"CS_ARCHITECTURE", _CS_ARCHITECTURE
},
7442 {"CS_HOSTNAME", _CS_HOSTNAME
},
7444 #ifdef _CS_HW_PROVIDER
7445 {"CS_HW_PROVIDER", _CS_HW_PROVIDER
},
7447 #ifdef _CS_HW_SERIAL
7448 {"CS_HW_SERIAL", _CS_HW_SERIAL
},
7450 #ifdef _CS_INITTAB_NAME
7451 {"CS_INITTAB_NAME", _CS_INITTAB_NAME
},
7453 #ifdef _CS_LFS64_CFLAGS
7454 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS
},
7456 #ifdef _CS_LFS64_LDFLAGS
7457 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS
},
7459 #ifdef _CS_LFS64_LIBS
7460 {"CS_LFS64_LIBS", _CS_LFS64_LIBS
},
7462 #ifdef _CS_LFS64_LINTFLAGS
7463 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS
},
7465 #ifdef _CS_LFS_CFLAGS
7466 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS
},
7468 #ifdef _CS_LFS_LDFLAGS
7469 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS
},
7472 {"CS_LFS_LIBS", _CS_LFS_LIBS
},
7474 #ifdef _CS_LFS_LINTFLAGS
7475 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS
},
7478 {"CS_MACHINE", _CS_MACHINE
},
7481 {"CS_PATH", _CS_PATH
},
7484 {"CS_RELEASE", _CS_RELEASE
},
7486 #ifdef _CS_SRPC_DOMAIN
7487 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN
},
7490 {"CS_SYSNAME", _CS_SYSNAME
},
7493 {"CS_VERSION", _CS_VERSION
},
7495 #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
7496 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS
},
7498 #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
7499 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS
},
7501 #ifdef _CS_XBS5_ILP32_OFF32_LIBS
7502 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS
},
7504 #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
7505 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS
},
7507 #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
7508 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS
},
7510 #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
7511 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS
},
7513 #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
7514 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS
},
7516 #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
7517 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
},
7519 #ifdef _CS_XBS5_LP64_OFF64_CFLAGS
7520 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS
},
7522 #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
7523 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS
},
7525 #ifdef _CS_XBS5_LP64_OFF64_LIBS
7526 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS
},
7528 #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
7529 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS
},
7531 #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
7532 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS
},
7534 #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
7535 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
},
7537 #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
7538 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS
},
7540 #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
7541 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
},
7543 #ifdef _MIPS_CS_AVAIL_PROCESSORS
7544 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS
},
7546 #ifdef _MIPS_CS_BASE
7547 {"MIPS_CS_BASE", _MIPS_CS_BASE
},
7549 #ifdef _MIPS_CS_HOSTID
7550 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID
},
7552 #ifdef _MIPS_CS_HW_NAME
7553 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME
},
7555 #ifdef _MIPS_CS_NUM_PROCESSORS
7556 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS
},
7558 #ifdef _MIPS_CS_OSREL_MAJ
7559 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ
},
7561 #ifdef _MIPS_CS_OSREL_MIN
7562 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN
},
7564 #ifdef _MIPS_CS_OSREL_PATCH
7565 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH
},
7567 #ifdef _MIPS_CS_OS_NAME
7568 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME
},
7570 #ifdef _MIPS_CS_OS_PROVIDER
7571 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER
},
7573 #ifdef _MIPS_CS_PROCESSORS
7574 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS
},
7576 #ifdef _MIPS_CS_SERIAL
7577 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL
},
7579 #ifdef _MIPS_CS_VENDOR
7580 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR
},
7585 conv_confstr_confname(PyObject
*arg
, int *valuep
)
7587 return conv_confname(arg
, valuep
, posix_constants_confstr
,
7588 sizeof(posix_constants_confstr
)
7589 / sizeof(struct constdef
));
7592 PyDoc_STRVAR(posix_confstr__doc__
,
7593 "confstr(name) -> string\n\n\
7594 Return a string-valued system configuration variable.");
7597 posix_confstr(PyObject
*self
, PyObject
*args
)
7599 PyObject
*result
= NULL
;
7603 if (PyArg_ParseTuple(args
, "O&:confstr", conv_confstr_confname
, &name
)) {
7607 len
= confstr(name
, buffer
, sizeof(buffer
));
7618 if ((unsigned int)len
>= sizeof(buffer
)) {
7619 result
= PyString_FromStringAndSize(NULL
, len
-1);
7621 confstr(name
, PyString_AS_STRING(result
), len
);
7624 result
= PyString_FromStringAndSize(buffer
, len
-1);
7633 static struct constdef posix_constants_sysconf
[] = {
7634 #ifdef _SC_2_CHAR_TERM
7635 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM
},
7638 {"SC_2_C_BIND", _SC_2_C_BIND
},
7641 {"SC_2_C_DEV", _SC_2_C_DEV
},
7643 #ifdef _SC_2_C_VERSION
7644 {"SC_2_C_VERSION", _SC_2_C_VERSION
},
7646 #ifdef _SC_2_FORT_DEV
7647 {"SC_2_FORT_DEV", _SC_2_FORT_DEV
},
7649 #ifdef _SC_2_FORT_RUN
7650 {"SC_2_FORT_RUN", _SC_2_FORT_RUN
},
7652 #ifdef _SC_2_LOCALEDEF
7653 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF
},
7656 {"SC_2_SW_DEV", _SC_2_SW_DEV
},
7659 {"SC_2_UPE", _SC_2_UPE
},
7661 #ifdef _SC_2_VERSION
7662 {"SC_2_VERSION", _SC_2_VERSION
},
7664 #ifdef _SC_ABI_ASYNCHRONOUS_IO
7665 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO
},
7668 {"SC_ACL", _SC_ACL
},
7670 #ifdef _SC_AIO_LISTIO_MAX
7671 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX
},
7674 {"SC_AIO_MAX", _SC_AIO_MAX
},
7676 #ifdef _SC_AIO_PRIO_DELTA_MAX
7677 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX
},
7680 {"SC_ARG_MAX", _SC_ARG_MAX
},
7682 #ifdef _SC_ASYNCHRONOUS_IO
7683 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO
},
7685 #ifdef _SC_ATEXIT_MAX
7686 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX
},
7689 {"SC_AUDIT", _SC_AUDIT
},
7691 #ifdef _SC_AVPHYS_PAGES
7692 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES
},
7694 #ifdef _SC_BC_BASE_MAX
7695 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX
},
7697 #ifdef _SC_BC_DIM_MAX
7698 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX
},
7700 #ifdef _SC_BC_SCALE_MAX
7701 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX
},
7703 #ifdef _SC_BC_STRING_MAX
7704 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX
},
7707 {"SC_CAP", _SC_CAP
},
7709 #ifdef _SC_CHARCLASS_NAME_MAX
7710 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX
},
7713 {"SC_CHAR_BIT", _SC_CHAR_BIT
},
7716 {"SC_CHAR_MAX", _SC_CHAR_MAX
},
7719 {"SC_CHAR_MIN", _SC_CHAR_MIN
},
7721 #ifdef _SC_CHILD_MAX
7722 {"SC_CHILD_MAX", _SC_CHILD_MAX
},
7725 {"SC_CLK_TCK", _SC_CLK_TCK
},
7727 #ifdef _SC_COHER_BLKSZ
7728 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ
},
7730 #ifdef _SC_COLL_WEIGHTS_MAX
7731 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX
},
7733 #ifdef _SC_DCACHE_ASSOC
7734 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC
},
7736 #ifdef _SC_DCACHE_BLKSZ
7737 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ
},
7739 #ifdef _SC_DCACHE_LINESZ
7740 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ
},
7742 #ifdef _SC_DCACHE_SZ
7743 {"SC_DCACHE_SZ", _SC_DCACHE_SZ
},
7745 #ifdef _SC_DCACHE_TBLKSZ
7746 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ
},
7748 #ifdef _SC_DELAYTIMER_MAX
7749 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX
},
7751 #ifdef _SC_EQUIV_CLASS_MAX
7752 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX
},
7754 #ifdef _SC_EXPR_NEST_MAX
7755 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX
},
7758 {"SC_FSYNC", _SC_FSYNC
},
7760 #ifdef _SC_GETGR_R_SIZE_MAX
7761 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX
},
7763 #ifdef _SC_GETPW_R_SIZE_MAX
7764 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX
},
7766 #ifdef _SC_ICACHE_ASSOC
7767 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC
},
7769 #ifdef _SC_ICACHE_BLKSZ
7770 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ
},
7772 #ifdef _SC_ICACHE_LINESZ
7773 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ
},
7775 #ifdef _SC_ICACHE_SZ
7776 {"SC_ICACHE_SZ", _SC_ICACHE_SZ
},
7779 {"SC_INF", _SC_INF
},
7782 {"SC_INT_MAX", _SC_INT_MAX
},
7785 {"SC_INT_MIN", _SC_INT_MIN
},
7788 {"SC_IOV_MAX", _SC_IOV_MAX
},
7790 #ifdef _SC_IP_SECOPTS
7791 {"SC_IP_SECOPTS", _SC_IP_SECOPTS
},
7793 #ifdef _SC_JOB_CONTROL
7794 {"SC_JOB_CONTROL", _SC_JOB_CONTROL
},
7796 #ifdef _SC_KERN_POINTERS
7797 {"SC_KERN_POINTERS", _SC_KERN_POINTERS
},
7800 {"SC_KERN_SIM", _SC_KERN_SIM
},
7803 {"SC_LINE_MAX", _SC_LINE_MAX
},
7805 #ifdef _SC_LOGIN_NAME_MAX
7806 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX
},
7808 #ifdef _SC_LOGNAME_MAX
7809 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX
},
7812 {"SC_LONG_BIT", _SC_LONG_BIT
},
7815 {"SC_MAC", _SC_MAC
},
7817 #ifdef _SC_MAPPED_FILES
7818 {"SC_MAPPED_FILES", _SC_MAPPED_FILES
},
7821 {"SC_MAXPID", _SC_MAXPID
},
7823 #ifdef _SC_MB_LEN_MAX
7824 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX
},
7827 {"SC_MEMLOCK", _SC_MEMLOCK
},
7829 #ifdef _SC_MEMLOCK_RANGE
7830 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE
},
7832 #ifdef _SC_MEMORY_PROTECTION
7833 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION
},
7835 #ifdef _SC_MESSAGE_PASSING
7836 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING
},
7838 #ifdef _SC_MMAP_FIXED_ALIGNMENT
7839 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT
},
7841 #ifdef _SC_MQ_OPEN_MAX
7842 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX
},
7844 #ifdef _SC_MQ_PRIO_MAX
7845 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX
},
7847 #ifdef _SC_NACLS_MAX
7848 {"SC_NACLS_MAX", _SC_NACLS_MAX
},
7850 #ifdef _SC_NGROUPS_MAX
7851 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX
},
7853 #ifdef _SC_NL_ARGMAX
7854 {"SC_NL_ARGMAX", _SC_NL_ARGMAX
},
7856 #ifdef _SC_NL_LANGMAX
7857 {"SC_NL_LANGMAX", _SC_NL_LANGMAX
},
7859 #ifdef _SC_NL_MSGMAX
7860 {"SC_NL_MSGMAX", _SC_NL_MSGMAX
},
7863 {"SC_NL_NMAX", _SC_NL_NMAX
},
7865 #ifdef _SC_NL_SETMAX
7866 {"SC_NL_SETMAX", _SC_NL_SETMAX
},
7868 #ifdef _SC_NL_TEXTMAX
7869 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX
},
7871 #ifdef _SC_NPROCESSORS_CONF
7872 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF
},
7874 #ifdef _SC_NPROCESSORS_ONLN
7875 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN
},
7877 #ifdef _SC_NPROC_CONF
7878 {"SC_NPROC_CONF", _SC_NPROC_CONF
},
7880 #ifdef _SC_NPROC_ONLN
7881 {"SC_NPROC_ONLN", _SC_NPROC_ONLN
},
7884 {"SC_NZERO", _SC_NZERO
},
7887 {"SC_OPEN_MAX", _SC_OPEN_MAX
},
7890 {"SC_PAGESIZE", _SC_PAGESIZE
},
7892 #ifdef _SC_PAGE_SIZE
7893 {"SC_PAGE_SIZE", _SC_PAGE_SIZE
},
7896 {"SC_PASS_MAX", _SC_PASS_MAX
},
7898 #ifdef _SC_PHYS_PAGES
7899 {"SC_PHYS_PAGES", _SC_PHYS_PAGES
},
7902 {"SC_PII", _SC_PII
},
7904 #ifdef _SC_PII_INTERNET
7905 {"SC_PII_INTERNET", _SC_PII_INTERNET
},
7907 #ifdef _SC_PII_INTERNET_DGRAM
7908 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM
},
7910 #ifdef _SC_PII_INTERNET_STREAM
7911 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM
},
7914 {"SC_PII_OSI", _SC_PII_OSI
},
7916 #ifdef _SC_PII_OSI_CLTS
7917 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS
},
7919 #ifdef _SC_PII_OSI_COTS
7920 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS
},
7922 #ifdef _SC_PII_OSI_M
7923 {"SC_PII_OSI_M", _SC_PII_OSI_M
},
7925 #ifdef _SC_PII_SOCKET
7926 {"SC_PII_SOCKET", _SC_PII_SOCKET
},
7929 {"SC_PII_XTI", _SC_PII_XTI
},
7932 {"SC_POLL", _SC_POLL
},
7934 #ifdef _SC_PRIORITIZED_IO
7935 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO
},
7937 #ifdef _SC_PRIORITY_SCHEDULING
7938 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING
},
7940 #ifdef _SC_REALTIME_SIGNALS
7941 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS
},
7943 #ifdef _SC_RE_DUP_MAX
7944 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX
},
7946 #ifdef _SC_RTSIG_MAX
7947 {"SC_RTSIG_MAX", _SC_RTSIG_MAX
},
7949 #ifdef _SC_SAVED_IDS
7950 {"SC_SAVED_IDS", _SC_SAVED_IDS
},
7952 #ifdef _SC_SCHAR_MAX
7953 {"SC_SCHAR_MAX", _SC_SCHAR_MAX
},
7955 #ifdef _SC_SCHAR_MIN
7956 {"SC_SCHAR_MIN", _SC_SCHAR_MIN
},
7959 {"SC_SELECT", _SC_SELECT
},
7961 #ifdef _SC_SEMAPHORES
7962 {"SC_SEMAPHORES", _SC_SEMAPHORES
},
7964 #ifdef _SC_SEM_NSEMS_MAX
7965 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX
},
7967 #ifdef _SC_SEM_VALUE_MAX
7968 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX
},
7970 #ifdef _SC_SHARED_MEMORY_OBJECTS
7971 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS
},
7974 {"SC_SHRT_MAX", _SC_SHRT_MAX
},
7977 {"SC_SHRT_MIN", _SC_SHRT_MIN
},
7979 #ifdef _SC_SIGQUEUE_MAX
7980 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX
},
7982 #ifdef _SC_SIGRT_MAX
7983 {"SC_SIGRT_MAX", _SC_SIGRT_MAX
},
7985 #ifdef _SC_SIGRT_MIN
7986 {"SC_SIGRT_MIN", _SC_SIGRT_MIN
},
7988 #ifdef _SC_SOFTPOWER
7989 {"SC_SOFTPOWER", _SC_SOFTPOWER
},
7991 #ifdef _SC_SPLIT_CACHE
7992 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE
},
7994 #ifdef _SC_SSIZE_MAX
7995 {"SC_SSIZE_MAX", _SC_SSIZE_MAX
},
7997 #ifdef _SC_STACK_PROT
7998 {"SC_STACK_PROT", _SC_STACK_PROT
},
8000 #ifdef _SC_STREAM_MAX
8001 {"SC_STREAM_MAX", _SC_STREAM_MAX
},
8003 #ifdef _SC_SYNCHRONIZED_IO
8004 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO
},
8007 {"SC_THREADS", _SC_THREADS
},
8009 #ifdef _SC_THREAD_ATTR_STACKADDR
8010 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR
},
8012 #ifdef _SC_THREAD_ATTR_STACKSIZE
8013 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE
},
8015 #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
8016 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS
},
8018 #ifdef _SC_THREAD_KEYS_MAX
8019 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX
},
8021 #ifdef _SC_THREAD_PRIORITY_SCHEDULING
8022 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING
},
8024 #ifdef _SC_THREAD_PRIO_INHERIT
8025 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT
},
8027 #ifdef _SC_THREAD_PRIO_PROTECT
8028 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT
},
8030 #ifdef _SC_THREAD_PROCESS_SHARED
8031 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED
},
8033 #ifdef _SC_THREAD_SAFE_FUNCTIONS
8034 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS
},
8036 #ifdef _SC_THREAD_STACK_MIN
8037 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN
},
8039 #ifdef _SC_THREAD_THREADS_MAX
8040 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX
},
8043 {"SC_TIMERS", _SC_TIMERS
},
8045 #ifdef _SC_TIMER_MAX
8046 {"SC_TIMER_MAX", _SC_TIMER_MAX
},
8048 #ifdef _SC_TTY_NAME_MAX
8049 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX
},
8051 #ifdef _SC_TZNAME_MAX
8052 {"SC_TZNAME_MAX", _SC_TZNAME_MAX
},
8054 #ifdef _SC_T_IOV_MAX
8055 {"SC_T_IOV_MAX", _SC_T_IOV_MAX
},
8057 #ifdef _SC_UCHAR_MAX
8058 {"SC_UCHAR_MAX", _SC_UCHAR_MAX
},
8061 {"SC_UINT_MAX", _SC_UINT_MAX
},
8063 #ifdef _SC_UIO_MAXIOV
8064 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV
},
8066 #ifdef _SC_ULONG_MAX
8067 {"SC_ULONG_MAX", _SC_ULONG_MAX
},
8069 #ifdef _SC_USHRT_MAX
8070 {"SC_USHRT_MAX", _SC_USHRT_MAX
},
8073 {"SC_VERSION", _SC_VERSION
},
8076 {"SC_WORD_BIT", _SC_WORD_BIT
},
8078 #ifdef _SC_XBS5_ILP32_OFF32
8079 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32
},
8081 #ifdef _SC_XBS5_ILP32_OFFBIG
8082 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG
},
8084 #ifdef _SC_XBS5_LP64_OFF64
8085 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64
},
8087 #ifdef _SC_XBS5_LPBIG_OFFBIG
8088 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG
},
8090 #ifdef _SC_XOPEN_CRYPT
8091 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT
},
8093 #ifdef _SC_XOPEN_ENH_I18N
8094 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N
},
8096 #ifdef _SC_XOPEN_LEGACY
8097 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY
},
8099 #ifdef _SC_XOPEN_REALTIME
8100 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME
},
8102 #ifdef _SC_XOPEN_REALTIME_THREADS
8103 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS
},
8105 #ifdef _SC_XOPEN_SHM
8106 {"SC_XOPEN_SHM", _SC_XOPEN_SHM
},
8108 #ifdef _SC_XOPEN_UNIX
8109 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX
},
8111 #ifdef _SC_XOPEN_VERSION
8112 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION
},
8114 #ifdef _SC_XOPEN_XCU_VERSION
8115 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION
},
8117 #ifdef _SC_XOPEN_XPG2
8118 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2
},
8120 #ifdef _SC_XOPEN_XPG3
8121 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3
},
8123 #ifdef _SC_XOPEN_XPG4
8124 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4
},
8129 conv_sysconf_confname(PyObject
*arg
, int *valuep
)
8131 return conv_confname(arg
, valuep
, posix_constants_sysconf
,
8132 sizeof(posix_constants_sysconf
)
8133 / sizeof(struct constdef
));
8136 PyDoc_STRVAR(posix_sysconf__doc__
,
8137 "sysconf(name) -> integer\n\n\
8138 Return an integer-valued system configuration variable.");
8141 posix_sysconf(PyObject
*self
, PyObject
*args
)
8143 PyObject
*result
= NULL
;
8146 if (PyArg_ParseTuple(args
, "O&:sysconf", conv_sysconf_confname
, &name
)) {
8150 value
= sysconf(name
);
8151 if (value
== -1 && errno
!= 0)
8154 result
= PyInt_FromLong(value
);
8161 /* This code is used to ensure that the tables of configuration value names
8162 * are in sorted order as required by conv_confname(), and also to build the
8163 * the exported dictionaries that are used to publish information about the
8164 * names available on the host platform.
8166 * Sorting the table at runtime ensures that the table is properly ordered
8167 * when used, even for platforms we're not able to test on. It also makes
8168 * it easier to add additional entries to the tables.
8172 cmp_constdefs(const void *v1
, const void *v2
)
8174 const struct constdef
*c1
=
8175 (const struct constdef
*) v1
;
8176 const struct constdef
*c2
=
8177 (const struct constdef
*) v2
;
8179 return strcmp(c1
->name
, c2
->name
);
8183 setup_confname_table(struct constdef
*table
, size_t tablesize
,
8184 char *tablename
, PyObject
*module
)
8189 qsort(table
, tablesize
, sizeof(struct constdef
), cmp_constdefs
);
8194 for (i
=0; i
< tablesize
; ++i
) {
8195 PyObject
*o
= PyInt_FromLong(table
[i
].value
);
8196 if (o
== NULL
|| PyDict_SetItemString(d
, table
[i
].name
, o
) == -1) {
8203 return PyModule_AddObject(module
, tablename
, d
);
8206 /* Return -1 on failure, 0 on success. */
8208 setup_confname_tables(PyObject
*module
)
8210 #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
8211 if (setup_confname_table(posix_constants_pathconf
,
8212 sizeof(posix_constants_pathconf
)
8213 / sizeof(struct constdef
),
8214 "pathconf_names", module
))
8218 if (setup_confname_table(posix_constants_confstr
,
8219 sizeof(posix_constants_confstr
)
8220 / sizeof(struct constdef
),
8221 "confstr_names", module
))
8225 if (setup_confname_table(posix_constants_sysconf
,
8226 sizeof(posix_constants_sysconf
)
8227 / sizeof(struct constdef
),
8228 "sysconf_names", module
))
8235 PyDoc_STRVAR(posix_abort__doc__
,
8236 "abort() -> does not return!\n\n\
8237 Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
8238 in the hardest way possible on the hosting operating system.");
8241 posix_abort(PyObject
*self
, PyObject
*noargs
)
8245 Py_FatalError("abort() called from Python code didn't abort!");
8250 PyDoc_STRVAR(win32_startfile__doc__
,
8251 "startfile(filepath [, operation]) - Start a file with its associated\n\
8254 When \"operation\" is not specified or \"open\", this acts like\n\
8255 double-clicking the file in Explorer, or giving the file name as an\n\
8256 argument to the DOS \"start\" command: the file is opened with whatever\n\
8257 application (if any) its extension is associated.\n\
8258 When another \"operation\" is given, it specifies what should be done with\n\
8259 the file. A typical operation is \"print\".\n\
8261 startfile returns as soon as the associated application is launched.\n\
8262 There is no option to wait for the application to close, and no way\n\
8263 to retrieve the application's exit status.\n\
8265 The filepath is relative to the current directory. If you want to use\n\
8266 an absolute path, make sure the first character is not a slash (\"/\");\n\
8267 the underlying Win32 ShellExecute function doesn't work if it is.");
8270 win32_startfile(PyObject
*self
, PyObject
*args
)
8273 char *operation
= NULL
;
8275 #ifdef Py_WIN_WIDE_FILENAMES
8276 if (unicode_file_names()) {
8277 PyObject
*unipath
, *woperation
= NULL
;
8278 if (!PyArg_ParseTuple(args
, "U|s:startfile",
8279 &unipath
, &operation
)) {
8286 woperation
= PyUnicode_DecodeASCII(operation
,
8287 strlen(operation
), NULL
);
8295 Py_BEGIN_ALLOW_THREADS
8296 rc
= ShellExecuteW((HWND
)0, woperation
? PyUnicode_AS_UNICODE(woperation
) : 0,
8297 PyUnicode_AS_UNICODE(unipath
),
8298 NULL
, NULL
, SW_SHOWNORMAL
);
8299 Py_END_ALLOW_THREADS
8301 Py_XDECREF(woperation
);
8302 if (rc
<= (HINSTANCE
)32) {
8303 PyObject
*errval
= win32_error_unicode("startfile",
8304 PyUnicode_AS_UNICODE(unipath
));
8313 if (!PyArg_ParseTuple(args
, "et|s:startfile",
8314 Py_FileSystemDefaultEncoding
, &filepath
,
8317 Py_BEGIN_ALLOW_THREADS
8318 rc
= ShellExecute((HWND
)0, operation
, filepath
,
8319 NULL
, NULL
, SW_SHOWNORMAL
);
8320 Py_END_ALLOW_THREADS
8321 if (rc
<= (HINSTANCE
)32) {
8322 PyObject
*errval
= win32_error("startfile", filepath
);
8323 PyMem_Free(filepath
);
8326 PyMem_Free(filepath
);
8332 #ifdef HAVE_GETLOADAVG
8333 PyDoc_STRVAR(posix_getloadavg__doc__
,
8334 "getloadavg() -> (float, float, float)\n\n\
8335 Return the number of processes in the system run queue averaged over\n\
8336 the last 1, 5, and 15 minutes or raises OSError if the load average\n\
8340 posix_getloadavg(PyObject
*self
, PyObject
*noargs
)
8343 if (getloadavg(loadavg
, 3)!=3) {
8344 PyErr_SetString(PyExc_OSError
, "Load averages are unobtainable");
8347 return Py_BuildValue("ddd", loadavg
[0], loadavg
[1], loadavg
[2]);
8353 PyDoc_STRVAR(win32_urandom__doc__
,
8354 "urandom(n) -> str\n\n\
8355 Return a string of n random bytes suitable for cryptographic use.");
8357 typedef BOOL (WINAPI
*CRYPTACQUIRECONTEXTA
)(HCRYPTPROV
*phProv
,\
8358 LPCSTR pszContainer
, LPCSTR pszProvider
, DWORD dwProvType
,\
8360 typedef BOOL (WINAPI
*CRYPTGENRANDOM
)(HCRYPTPROV hProv
, DWORD dwLen
,\
8363 static CRYPTGENRANDOM pCryptGenRandom
= NULL
;
8364 /* This handle is never explicitly released. Instead, the operating
8365 system will release it when the process terminates. */
8366 static HCRYPTPROV hCryptProv
= 0;
8369 win32_urandom(PyObject
*self
, PyObject
*args
)
8374 /* Read arguments */
8375 if (! PyArg_ParseTuple(args
, "i:urandom", &howMany
))
8378 return PyErr_Format(PyExc_ValueError
,
8379 "negative argument not allowed");
8381 if (hCryptProv
== 0) {
8382 HINSTANCE hAdvAPI32
= NULL
;
8383 CRYPTACQUIRECONTEXTA pCryptAcquireContext
= NULL
;
8385 /* Obtain handle to the DLL containing CryptoAPI
8386 This should not fail */
8387 hAdvAPI32
= GetModuleHandle("advapi32.dll");
8388 if(hAdvAPI32
== NULL
)
8389 return win32_error("GetModuleHandle", NULL
);
8391 /* Obtain pointers to the CryptoAPI functions
8392 This will fail on some early versions of Win95 */
8393 pCryptAcquireContext
= (CRYPTACQUIRECONTEXTA
)GetProcAddress(
8395 "CryptAcquireContextA");
8396 if (pCryptAcquireContext
== NULL
)
8397 return PyErr_Format(PyExc_NotImplementedError
,
8398 "CryptAcquireContextA not found");
8400 pCryptGenRandom
= (CRYPTGENRANDOM
)GetProcAddress(
8401 hAdvAPI32
, "CryptGenRandom");
8402 if (pCryptGenRandom
== NULL
)
8403 return PyErr_Format(PyExc_NotImplementedError
,
8404 "CryptGenRandom not found");
8406 /* Acquire context */
8407 if (! pCryptAcquireContext(&hCryptProv
, NULL
, NULL
,
8408 PROV_RSA_FULL
, CRYPT_VERIFYCONTEXT
))
8409 return win32_error("CryptAcquireContext", NULL
);
8412 /* Allocate bytes */
8413 result
= PyString_FromStringAndSize(NULL
, howMany
);
8414 if (result
!= NULL
) {
8415 /* Get random data */
8416 memset(PyString_AS_STRING(result
), 0, howMany
); /* zero seed */
8417 if (! pCryptGenRandom(hCryptProv
, howMany
, (unsigned char*)
8418 PyString_AS_STRING(result
))) {
8420 return win32_error("CryptGenRandom", NULL
);
8428 /* Use openssl random routine */
8429 #include <openssl/rand.h>
8430 PyDoc_STRVAR(vms_urandom__doc__
,
8431 "urandom(n) -> str\n\n\
8432 Return a string of n random bytes suitable for cryptographic use.");
8435 vms_urandom(PyObject
*self
, PyObject
*args
)
8440 /* Read arguments */
8441 if (! PyArg_ParseTuple(args
, "i:urandom", &howMany
))
8444 return PyErr_Format(PyExc_ValueError
,
8445 "negative argument not allowed");
8447 /* Allocate bytes */
8448 result
= PyString_FromStringAndSize(NULL
, howMany
);
8449 if (result
!= NULL
) {
8450 /* Get random data */
8451 if (RAND_pseudo_bytes((unsigned char*)
8452 PyString_AS_STRING(result
),
8455 return PyErr_Format(PyExc_ValueError
,
8456 "RAND_pseudo_bytes");
8463 static PyMethodDef posix_methods
[] = {
8464 {"access", posix_access
, METH_VARARGS
, posix_access__doc__
},
8466 {"ttyname", posix_ttyname
, METH_VARARGS
, posix_ttyname__doc__
},
8468 {"chdir", posix_chdir
, METH_VARARGS
, posix_chdir__doc__
},
8470 {"chflags", posix_chflags
, METH_VARARGS
, posix_chflags__doc__
},
8471 #endif /* HAVE_CHFLAGS */
8472 {"chmod", posix_chmod
, METH_VARARGS
, posix_chmod__doc__
},
8474 {"fchmod", posix_fchmod
, METH_VARARGS
, posix_fchmod__doc__
},
8475 #endif /* HAVE_FCHMOD */
8477 {"chown", posix_chown
, METH_VARARGS
, posix_chown__doc__
},
8478 #endif /* HAVE_CHOWN */
8480 {"lchmod", posix_lchmod
, METH_VARARGS
, posix_lchmod__doc__
},
8481 #endif /* HAVE_LCHMOD */
8483 {"fchown", posix_fchown
, METH_VARARGS
, posix_fchown__doc__
},
8484 #endif /* HAVE_FCHOWN */
8485 #ifdef HAVE_LCHFLAGS
8486 {"lchflags", posix_lchflags
, METH_VARARGS
, posix_lchflags__doc__
},
8487 #endif /* HAVE_LCHFLAGS */
8489 {"lchown", posix_lchown
, METH_VARARGS
, posix_lchown__doc__
},
8490 #endif /* HAVE_LCHOWN */
8492 {"chroot", posix_chroot
, METH_VARARGS
, posix_chroot__doc__
},
8495 {"ctermid", posix_ctermid
, METH_NOARGS
, posix_ctermid__doc__
},
8498 {"getcwd", posix_getcwd
, METH_NOARGS
, posix_getcwd__doc__
},
8499 #ifdef Py_USING_UNICODE
8500 {"getcwdu", posix_getcwdu
, METH_NOARGS
, posix_getcwdu__doc__
},
8504 {"link", posix_link
, METH_VARARGS
, posix_link__doc__
},
8505 #endif /* HAVE_LINK */
8506 {"listdir", posix_listdir
, METH_VARARGS
, posix_listdir__doc__
},
8507 {"lstat", posix_lstat
, METH_VARARGS
, posix_lstat__doc__
},
8508 {"mkdir", posix_mkdir
, METH_VARARGS
, posix_mkdir__doc__
},
8510 {"nice", posix_nice
, METH_VARARGS
, posix_nice__doc__
},
8511 #endif /* HAVE_NICE */
8512 #ifdef HAVE_READLINK
8513 {"readlink", posix_readlink
, METH_VARARGS
, posix_readlink__doc__
},
8514 #endif /* HAVE_READLINK */
8515 {"rename", posix_rename
, METH_VARARGS
, posix_rename__doc__
},
8516 {"rmdir", posix_rmdir
, METH_VARARGS
, posix_rmdir__doc__
},
8517 {"stat", posix_stat
, METH_VARARGS
, posix_stat__doc__
},
8518 {"stat_float_times", stat_float_times
, METH_VARARGS
, stat_float_times__doc__
},
8520 {"symlink", posix_symlink
, METH_VARARGS
, posix_symlink__doc__
},
8521 #endif /* HAVE_SYMLINK */
8523 {"system", posix_system
, METH_VARARGS
, posix_system__doc__
},
8525 {"umask", posix_umask
, METH_VARARGS
, posix_umask__doc__
},
8527 {"uname", posix_uname
, METH_NOARGS
, posix_uname__doc__
},
8528 #endif /* HAVE_UNAME */
8529 {"unlink", posix_unlink
, METH_VARARGS
, posix_unlink__doc__
},
8530 {"remove", posix_unlink
, METH_VARARGS
, posix_remove__doc__
},
8531 {"utime", posix_utime
, METH_VARARGS
, posix_utime__doc__
},
8533 {"times", posix_times
, METH_NOARGS
, posix_times__doc__
},
8534 #endif /* HAVE_TIMES */
8535 {"_exit", posix__exit
, METH_VARARGS
, posix__exit__doc__
},
8537 {"execv", posix_execv
, METH_VARARGS
, posix_execv__doc__
},
8538 {"execve", posix_execve
, METH_VARARGS
, posix_execve__doc__
},
8539 #endif /* HAVE_EXECV */
8541 {"spawnv", posix_spawnv
, METH_VARARGS
, posix_spawnv__doc__
},
8542 {"spawnve", posix_spawnve
, METH_VARARGS
, posix_spawnve__doc__
},
8543 #if defined(PYOS_OS2)
8544 {"spawnvp", posix_spawnvp
, METH_VARARGS
, posix_spawnvp__doc__
},
8545 {"spawnvpe", posix_spawnvpe
, METH_VARARGS
, posix_spawnvpe__doc__
},
8546 #endif /* PYOS_OS2 */
8547 #endif /* HAVE_SPAWNV */
8549 {"fork1", posix_fork1
, METH_NOARGS
, posix_fork1__doc__
},
8550 #endif /* HAVE_FORK1 */
8552 {"fork", posix_fork
, METH_NOARGS
, posix_fork__doc__
},
8553 #endif /* HAVE_FORK */
8554 #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
8555 {"openpty", posix_openpty
, METH_NOARGS
, posix_openpty__doc__
},
8556 #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
8558 {"forkpty", posix_forkpty
, METH_NOARGS
, posix_forkpty__doc__
},
8559 #endif /* HAVE_FORKPTY */
8561 {"getegid", posix_getegid
, METH_NOARGS
, posix_getegid__doc__
},
8562 #endif /* HAVE_GETEGID */
8564 {"geteuid", posix_geteuid
, METH_NOARGS
, posix_geteuid__doc__
},
8565 #endif /* HAVE_GETEUID */
8567 {"getgid", posix_getgid
, METH_NOARGS
, posix_getgid__doc__
},
8568 #endif /* HAVE_GETGID */
8569 #ifdef HAVE_GETGROUPS
8570 {"getgroups", posix_getgroups
, METH_NOARGS
, posix_getgroups__doc__
},
8572 {"getpid", posix_getpid
, METH_NOARGS
, posix_getpid__doc__
},
8574 {"getpgrp", posix_getpgrp
, METH_NOARGS
, posix_getpgrp__doc__
},
8575 #endif /* HAVE_GETPGRP */
8577 {"getppid", posix_getppid
, METH_NOARGS
, posix_getppid__doc__
},
8578 #endif /* HAVE_GETPPID */
8580 {"getuid", posix_getuid
, METH_NOARGS
, posix_getuid__doc__
},
8581 #endif /* HAVE_GETUID */
8582 #ifdef HAVE_GETLOGIN
8583 {"getlogin", posix_getlogin
, METH_NOARGS
, posix_getlogin__doc__
},
8586 {"kill", posix_kill
, METH_VARARGS
, posix_kill__doc__
},
8587 #endif /* HAVE_KILL */
8589 {"killpg", posix_killpg
, METH_VARARGS
, posix_killpg__doc__
},
8590 #endif /* HAVE_KILLPG */
8592 {"plock", posix_plock
, METH_VARARGS
, posix_plock__doc__
},
8593 #endif /* HAVE_PLOCK */
8595 {"popen", posix_popen
, METH_VARARGS
, posix_popen__doc__
},
8597 {"popen2", win32_popen2
, METH_VARARGS
},
8598 {"popen3", win32_popen3
, METH_VARARGS
},
8599 {"popen4", win32_popen4
, METH_VARARGS
},
8600 {"startfile", win32_startfile
, METH_VARARGS
, win32_startfile__doc__
},
8602 #if defined(PYOS_OS2) && defined(PYCC_GCC)
8603 {"popen2", os2emx_popen2
, METH_VARARGS
},
8604 {"popen3", os2emx_popen3
, METH_VARARGS
},
8605 {"popen4", os2emx_popen4
, METH_VARARGS
},
8608 #endif /* HAVE_POPEN */
8610 {"setuid", posix_setuid
, METH_VARARGS
, posix_setuid__doc__
},
8611 #endif /* HAVE_SETUID */
8613 {"seteuid", posix_seteuid
, METH_VARARGS
, posix_seteuid__doc__
},
8614 #endif /* HAVE_SETEUID */
8616 {"setegid", posix_setegid
, METH_VARARGS
, posix_setegid__doc__
},
8617 #endif /* HAVE_SETEGID */
8618 #ifdef HAVE_SETREUID
8619 {"setreuid", posix_setreuid
, METH_VARARGS
, posix_setreuid__doc__
},
8620 #endif /* HAVE_SETREUID */
8621 #ifdef HAVE_SETREGID
8622 {"setregid", posix_setregid
, METH_VARARGS
, posix_setregid__doc__
},
8623 #endif /* HAVE_SETREGID */
8625 {"setgid", posix_setgid
, METH_VARARGS
, posix_setgid__doc__
},
8626 #endif /* HAVE_SETGID */
8627 #ifdef HAVE_SETGROUPS
8628 {"setgroups", posix_setgroups
, METH_O
, posix_setgroups__doc__
},
8629 #endif /* HAVE_SETGROUPS */
8631 {"getpgid", posix_getpgid
, METH_VARARGS
, posix_getpgid__doc__
},
8632 #endif /* HAVE_GETPGID */
8634 {"setpgrp", posix_setpgrp
, METH_NOARGS
, posix_setpgrp__doc__
},
8635 #endif /* HAVE_SETPGRP */
8637 {"wait", posix_wait
, METH_NOARGS
, posix_wait__doc__
},
8638 #endif /* HAVE_WAIT */
8640 {"wait3", posix_wait3
, METH_VARARGS
, posix_wait3__doc__
},
8641 #endif /* HAVE_WAIT3 */
8643 {"wait4", posix_wait4
, METH_VARARGS
, posix_wait4__doc__
},
8644 #endif /* HAVE_WAIT4 */
8645 #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
8646 {"waitpid", posix_waitpid
, METH_VARARGS
, posix_waitpid__doc__
},
8647 #endif /* HAVE_WAITPID */
8649 {"getsid", posix_getsid
, METH_VARARGS
, posix_getsid__doc__
},
8650 #endif /* HAVE_GETSID */
8652 {"setsid", posix_setsid
, METH_NOARGS
, posix_setsid__doc__
},
8653 #endif /* HAVE_SETSID */
8655 {"setpgid", posix_setpgid
, METH_VARARGS
, posix_setpgid__doc__
},
8656 #endif /* HAVE_SETPGID */
8657 #ifdef HAVE_TCGETPGRP
8658 {"tcgetpgrp", posix_tcgetpgrp
, METH_VARARGS
, posix_tcgetpgrp__doc__
},
8659 #endif /* HAVE_TCGETPGRP */
8660 #ifdef HAVE_TCSETPGRP
8661 {"tcsetpgrp", posix_tcsetpgrp
, METH_VARARGS
, posix_tcsetpgrp__doc__
},
8662 #endif /* HAVE_TCSETPGRP */
8663 {"open", posix_open
, METH_VARARGS
, posix_open__doc__
},
8664 {"close", posix_close
, METH_VARARGS
, posix_close__doc__
},
8665 {"closerange", posix_closerange
, METH_VARARGS
, posix_closerange__doc__
},
8666 {"dup", posix_dup
, METH_VARARGS
, posix_dup__doc__
},
8667 {"dup2", posix_dup2
, METH_VARARGS
, posix_dup2__doc__
},
8668 {"lseek", posix_lseek
, METH_VARARGS
, posix_lseek__doc__
},
8669 {"read", posix_read
, METH_VARARGS
, posix_read__doc__
},
8670 {"write", posix_write
, METH_VARARGS
, posix_write__doc__
},
8671 {"fstat", posix_fstat
, METH_VARARGS
, posix_fstat__doc__
},
8672 {"fdopen", posix_fdopen
, METH_VARARGS
, posix_fdopen__doc__
},
8673 {"isatty", posix_isatty
, METH_VARARGS
, posix_isatty__doc__
},
8675 {"pipe", posix_pipe
, METH_NOARGS
, posix_pipe__doc__
},
8678 {"mkfifo", posix_mkfifo
, METH_VARARGS
, posix_mkfifo__doc__
},
8680 #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
8681 {"mknod", posix_mknod
, METH_VARARGS
, posix_mknod__doc__
},
8683 #ifdef HAVE_DEVICE_MACROS
8684 {"major", posix_major
, METH_VARARGS
, posix_major__doc__
},
8685 {"minor", posix_minor
, METH_VARARGS
, posix_minor__doc__
},
8686 {"makedev", posix_makedev
, METH_VARARGS
, posix_makedev__doc__
},
8688 #ifdef HAVE_FTRUNCATE
8689 {"ftruncate", posix_ftruncate
, METH_VARARGS
, posix_ftruncate__doc__
},
8692 {"putenv", posix_putenv
, METH_VARARGS
, posix_putenv__doc__
},
8694 #ifdef HAVE_UNSETENV
8695 {"unsetenv", posix_unsetenv
, METH_VARARGS
, posix_unsetenv__doc__
},
8697 {"strerror", posix_strerror
, METH_VARARGS
, posix_strerror__doc__
},
8699 {"fchdir", posix_fchdir
, METH_O
, posix_fchdir__doc__
},
8702 {"fsync", posix_fsync
, METH_O
, posix_fsync__doc__
},
8704 #ifdef HAVE_FDATASYNC
8705 {"fdatasync", posix_fdatasync
, METH_O
, posix_fdatasync__doc__
},
8707 #ifdef HAVE_SYS_WAIT_H
8709 {"WCOREDUMP", posix_WCOREDUMP
, METH_VARARGS
, posix_WCOREDUMP__doc__
},
8710 #endif /* WCOREDUMP */
8712 {"WIFCONTINUED",posix_WIFCONTINUED
, METH_VARARGS
, posix_WIFCONTINUED__doc__
},
8713 #endif /* WIFCONTINUED */
8715 {"WIFSTOPPED", posix_WIFSTOPPED
, METH_VARARGS
, posix_WIFSTOPPED__doc__
},
8716 #endif /* WIFSTOPPED */
8718 {"WIFSIGNALED", posix_WIFSIGNALED
, METH_VARARGS
, posix_WIFSIGNALED__doc__
},
8719 #endif /* WIFSIGNALED */
8721 {"WIFEXITED", posix_WIFEXITED
, METH_VARARGS
, posix_WIFEXITED__doc__
},
8722 #endif /* WIFEXITED */
8724 {"WEXITSTATUS", posix_WEXITSTATUS
, METH_VARARGS
, posix_WEXITSTATUS__doc__
},
8725 #endif /* WEXITSTATUS */
8727 {"WTERMSIG", posix_WTERMSIG
, METH_VARARGS
, posix_WTERMSIG__doc__
},
8728 #endif /* WTERMSIG */
8730 {"WSTOPSIG", posix_WSTOPSIG
, METH_VARARGS
, posix_WSTOPSIG__doc__
},
8731 #endif /* WSTOPSIG */
8732 #endif /* HAVE_SYS_WAIT_H */
8733 #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
8734 {"fstatvfs", posix_fstatvfs
, METH_VARARGS
, posix_fstatvfs__doc__
},
8736 #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
8737 {"statvfs", posix_statvfs
, METH_VARARGS
, posix_statvfs__doc__
},
8740 {"tmpfile", posix_tmpfile
, METH_NOARGS
, posix_tmpfile__doc__
},
8743 {"tempnam", posix_tempnam
, METH_VARARGS
, posix_tempnam__doc__
},
8746 {"tmpnam", posix_tmpnam
, METH_NOARGS
, posix_tmpnam__doc__
},
8749 {"confstr", posix_confstr
, METH_VARARGS
, posix_confstr__doc__
},
8752 {"sysconf", posix_sysconf
, METH_VARARGS
, posix_sysconf__doc__
},
8754 #ifdef HAVE_FPATHCONF
8755 {"fpathconf", posix_fpathconf
, METH_VARARGS
, posix_fpathconf__doc__
},
8757 #ifdef HAVE_PATHCONF
8758 {"pathconf", posix_pathconf
, METH_VARARGS
, posix_pathconf__doc__
},
8760 {"abort", posix_abort
, METH_NOARGS
, posix_abort__doc__
},
8762 {"_getfullpathname", posix__getfullpathname
, METH_VARARGS
, NULL
},
8764 #ifdef HAVE_GETLOADAVG
8765 {"getloadavg", posix_getloadavg
, METH_NOARGS
, posix_getloadavg__doc__
},
8768 {"urandom", win32_urandom
, METH_VARARGS
, win32_urandom__doc__
},
8771 {"urandom", vms_urandom
, METH_VARARGS
, vms_urandom__doc__
},
8773 {NULL
, NULL
} /* Sentinel */
8778 ins(PyObject
*module
, char *symbol
, long value
)
8780 return PyModule_AddIntConstant(module
, symbol
, value
);
8783 #if defined(PYOS_OS2)
8784 /* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
8785 static int insertvalues(PyObject
*module
)
8788 ULONG values
[QSV_MAX
+1];
8792 Py_BEGIN_ALLOW_THREADS
8793 rc
= DosQuerySysInfo(1L, QSV_MAX
, &values
[1], sizeof(ULONG
) * QSV_MAX
);
8794 Py_END_ALLOW_THREADS
8796 if (rc
!= NO_ERROR
) {
8801 if (ins(module
, "meminstalled", values
[QSV_TOTPHYSMEM
])) return -1;
8802 if (ins(module
, "memkernel", values
[QSV_TOTRESMEM
])) return -1;
8803 if (ins(module
, "memvirtual", values
[QSV_TOTAVAILMEM
])) return -1;
8804 if (ins(module
, "maxpathlen", values
[QSV_MAX_PATH_LENGTH
])) return -1;
8805 if (ins(module
, "maxnamelen", values
[QSV_MAX_COMP_LENGTH
])) return -1;
8806 if (ins(module
, "revision", values
[QSV_VERSION_REVISION
])) return -1;
8807 if (ins(module
, "timeslice", values
[QSV_MIN_SLICE
])) return -1;
8809 switch (values
[QSV_VERSION_MINOR
]) {
8810 case 0: ver
= "2.00"; break;
8811 case 10: ver
= "2.10"; break;
8812 case 11: ver
= "2.11"; break;
8813 case 30: ver
= "3.00"; break;
8814 case 40: ver
= "4.00"; break;
8815 case 50: ver
= "5.00"; break;
8817 PyOS_snprintf(tmp
, sizeof(tmp
),
8818 "%d-%d", values
[QSV_VERSION_MAJOR
],
8819 values
[QSV_VERSION_MINOR
]);
8823 /* Add Indicator of the Version of the Operating System */
8824 if (PyModule_AddStringConstant(module
, "version", tmp
) < 0)
8827 /* Add Indicator of Which Drive was Used to Boot the System */
8828 tmp
[0] = 'A' + values
[QSV_BOOT_DRIVE
] - 1;
8832 return PyModule_AddStringConstant(module
, "bootdrive", tmp
);
8837 all_ins(PyObject
*d
)
8840 if (ins(d
, "F_OK", (long)F_OK
)) return -1;
8843 if (ins(d
, "R_OK", (long)R_OK
)) return -1;
8846 if (ins(d
, "W_OK", (long)W_OK
)) return -1;
8849 if (ins(d
, "X_OK", (long)X_OK
)) return -1;
8852 if (ins(d
, "NGROUPS_MAX", (long)NGROUPS_MAX
)) return -1;
8855 if (ins(d
, "TMP_MAX", (long)TMP_MAX
)) return -1;
8858 if (ins(d
, "WCONTINUED", (long)WCONTINUED
)) return -1;
8861 if (ins(d
, "WNOHANG", (long)WNOHANG
)) return -1;
8864 if (ins(d
, "WUNTRACED", (long)WUNTRACED
)) return -1;
8867 if (ins(d
, "O_RDONLY", (long)O_RDONLY
)) return -1;
8870 if (ins(d
, "O_WRONLY", (long)O_WRONLY
)) return -1;
8873 if (ins(d
, "O_RDWR", (long)O_RDWR
)) return -1;
8876 if (ins(d
, "O_NDELAY", (long)O_NDELAY
)) return -1;
8879 if (ins(d
, "O_NONBLOCK", (long)O_NONBLOCK
)) return -1;
8882 if (ins(d
, "O_APPEND", (long)O_APPEND
)) return -1;
8885 if (ins(d
, "O_DSYNC", (long)O_DSYNC
)) return -1;
8888 if (ins(d
, "O_RSYNC", (long)O_RSYNC
)) return -1;
8891 if (ins(d
, "O_SYNC", (long)O_SYNC
)) return -1;
8894 if (ins(d
, "O_NOCTTY", (long)O_NOCTTY
)) return -1;
8897 if (ins(d
, "O_CREAT", (long)O_CREAT
)) return -1;
8900 if (ins(d
, "O_EXCL", (long)O_EXCL
)) return -1;
8903 if (ins(d
, "O_TRUNC", (long)O_TRUNC
)) return -1;
8906 if (ins(d
, "O_BINARY", (long)O_BINARY
)) return -1;
8909 if (ins(d
, "O_TEXT", (long)O_TEXT
)) return -1;
8912 if (ins(d
, "O_LARGEFILE", (long)O_LARGEFILE
)) return -1;
8915 if (ins(d
, "O_SHLOCK", (long)O_SHLOCK
)) return -1;
8918 if (ins(d
, "O_EXLOCK", (long)O_EXLOCK
)) return -1;
8923 /* Don't inherit in child processes. */
8924 if (ins(d
, "O_NOINHERIT", (long)O_NOINHERIT
)) return -1;
8926 #ifdef _O_SHORT_LIVED
8927 /* Optimize for short life (keep in memory). */
8928 /* MS forgot to define this one with a non-underscore form too. */
8929 if (ins(d
, "O_SHORT_LIVED", (long)_O_SHORT_LIVED
)) return -1;
8932 /* Automatically delete when last handle is closed. */
8933 if (ins(d
, "O_TEMPORARY", (long)O_TEMPORARY
)) return -1;
8936 /* Optimize for random access. */
8937 if (ins(d
, "O_RANDOM", (long)O_RANDOM
)) return -1;
8940 /* Optimize for sequential access. */
8941 if (ins(d
, "O_SEQUENTIAL", (long)O_SEQUENTIAL
)) return -1;
8944 /* GNU extensions. */
8946 /* Send a SIGIO signal whenever input or output
8947 becomes available on file descriptor */
8948 if (ins(d
, "O_ASYNC", (long)O_ASYNC
)) return -1;
8951 /* Direct disk access. */
8952 if (ins(d
, "O_DIRECT", (long)O_DIRECT
)) return -1;
8955 /* Must be a directory. */
8956 if (ins(d
, "O_DIRECTORY", (long)O_DIRECTORY
)) return -1;
8959 /* Do not follow links. */
8960 if (ins(d
, "O_NOFOLLOW", (long)O_NOFOLLOW
)) return -1;
8963 /* Do not update the access time. */
8964 if (ins(d
, "O_NOATIME", (long)O_NOATIME
)) return -1;
8967 /* These come from sysexits.h */
8969 if (ins(d
, "EX_OK", (long)EX_OK
)) return -1;
8972 if (ins(d
, "EX_USAGE", (long)EX_USAGE
)) return -1;
8973 #endif /* EX_USAGE */
8975 if (ins(d
, "EX_DATAERR", (long)EX_DATAERR
)) return -1;
8976 #endif /* EX_DATAERR */
8978 if (ins(d
, "EX_NOINPUT", (long)EX_NOINPUT
)) return -1;
8979 #endif /* EX_NOINPUT */
8981 if (ins(d
, "EX_NOUSER", (long)EX_NOUSER
)) return -1;
8982 #endif /* EX_NOUSER */
8984 if (ins(d
, "EX_NOHOST", (long)EX_NOHOST
)) return -1;
8985 #endif /* EX_NOHOST */
8986 #ifdef EX_UNAVAILABLE
8987 if (ins(d
, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE
)) return -1;
8988 #endif /* EX_UNAVAILABLE */
8990 if (ins(d
, "EX_SOFTWARE", (long)EX_SOFTWARE
)) return -1;
8991 #endif /* EX_SOFTWARE */
8993 if (ins(d
, "EX_OSERR", (long)EX_OSERR
)) return -1;
8994 #endif /* EX_OSERR */
8996 if (ins(d
, "EX_OSFILE", (long)EX_OSFILE
)) return -1;
8997 #endif /* EX_OSFILE */
8999 if (ins(d
, "EX_CANTCREAT", (long)EX_CANTCREAT
)) return -1;
9000 #endif /* EX_CANTCREAT */
9002 if (ins(d
, "EX_IOERR", (long)EX_IOERR
)) return -1;
9003 #endif /* EX_IOERR */
9005 if (ins(d
, "EX_TEMPFAIL", (long)EX_TEMPFAIL
)) return -1;
9006 #endif /* EX_TEMPFAIL */
9008 if (ins(d
, "EX_PROTOCOL", (long)EX_PROTOCOL
)) return -1;
9009 #endif /* EX_PROTOCOL */
9011 if (ins(d
, "EX_NOPERM", (long)EX_NOPERM
)) return -1;
9012 #endif /* EX_NOPERM */
9014 if (ins(d
, "EX_CONFIG", (long)EX_CONFIG
)) return -1;
9015 #endif /* EX_CONFIG */
9017 if (ins(d
, "EX_NOTFOUND", (long)EX_NOTFOUND
)) return -1;
9018 #endif /* EX_NOTFOUND */
9021 #if defined(PYOS_OS2) && defined(PYCC_GCC)
9022 if (ins(d
, "P_WAIT", (long)P_WAIT
)) return -1;
9023 if (ins(d
, "P_NOWAIT", (long)P_NOWAIT
)) return -1;
9024 if (ins(d
, "P_OVERLAY", (long)P_OVERLAY
)) return -1;
9025 if (ins(d
, "P_DEBUG", (long)P_DEBUG
)) return -1;
9026 if (ins(d
, "P_SESSION", (long)P_SESSION
)) return -1;
9027 if (ins(d
, "P_DETACH", (long)P_DETACH
)) return -1;
9028 if (ins(d
, "P_PM", (long)P_PM
)) return -1;
9029 if (ins(d
, "P_DEFAULT", (long)P_DEFAULT
)) return -1;
9030 if (ins(d
, "P_MINIMIZE", (long)P_MINIMIZE
)) return -1;
9031 if (ins(d
, "P_MAXIMIZE", (long)P_MAXIMIZE
)) return -1;
9032 if (ins(d
, "P_FULLSCREEN", (long)P_FULLSCREEN
)) return -1;
9033 if (ins(d
, "P_WINDOWED", (long)P_WINDOWED
)) return -1;
9034 if (ins(d
, "P_FOREGROUND", (long)P_FOREGROUND
)) return -1;
9035 if (ins(d
, "P_BACKGROUND", (long)P_BACKGROUND
)) return -1;
9036 if (ins(d
, "P_NOCLOSE", (long)P_NOCLOSE
)) return -1;
9037 if (ins(d
, "P_NOSESSION", (long)P_NOSESSION
)) return -1;
9038 if (ins(d
, "P_QUOTE", (long)P_QUOTE
)) return -1;
9039 if (ins(d
, "P_TILDE", (long)P_TILDE
)) return -1;
9040 if (ins(d
, "P_UNRELATED", (long)P_UNRELATED
)) return -1;
9041 if (ins(d
, "P_DEBUGDESC", (long)P_DEBUGDESC
)) return -1;
9043 if (ins(d
, "P_WAIT", (long)_P_WAIT
)) return -1;
9044 if (ins(d
, "P_NOWAIT", (long)_P_NOWAIT
)) return -1;
9045 if (ins(d
, "P_OVERLAY", (long)_OLD_P_OVERLAY
)) return -1;
9046 if (ins(d
, "P_NOWAITO", (long)_P_NOWAITO
)) return -1;
9047 if (ins(d
, "P_DETACH", (long)_P_DETACH
)) return -1;
9051 #if defined(PYOS_OS2)
9052 if (insertvalues(d
)) return -1;
9058 #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
9059 #define INITFUNC initnt
9060 #define MODNAME "nt"
9062 #elif defined(PYOS_OS2)
9063 #define INITFUNC initos2
9064 #define MODNAME "os2"
9067 #define INITFUNC initposix
9068 #define MODNAME "posix"
9076 m
= Py_InitModule3(MODNAME
,
9082 /* Initialize environ dictionary */
9083 v
= convertenviron();
9085 if (v
== NULL
|| PyModule_AddObject(m
, "environ", v
) != 0)
9092 if (setup_confname_tables(m
))
9095 Py_INCREF(PyExc_OSError
);
9096 PyModule_AddObject(m
, "error", PyExc_OSError
);
9099 if (posix_putenv_garbage
== NULL
)
9100 posix_putenv_garbage
= PyDict_New();
9104 stat_result_desc
.name
= MODNAME
".stat_result";
9105 stat_result_desc
.fields
[7].name
= PyStructSequence_UnnamedField
;
9106 stat_result_desc
.fields
[8].name
= PyStructSequence_UnnamedField
;
9107 stat_result_desc
.fields
[9].name
= PyStructSequence_UnnamedField
;
9108 PyStructSequence_InitType(&StatResultType
, &stat_result_desc
);
9109 structseq_new
= StatResultType
.tp_new
;
9110 StatResultType
.tp_new
= statresult_new
;
9112 statvfs_result_desc
.name
= MODNAME
".statvfs_result";
9113 PyStructSequence_InitType(&StatVFSResultType
, &statvfs_result_desc
);
9114 #ifdef NEED_TICKS_PER_SECOND
9115 # if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
9116 ticks_per_second
= sysconf(_SC_CLK_TCK
);
9118 ticks_per_second
= HZ
;
9120 ticks_per_second
= 60; /* magic fallback value; may be bogus */
9124 Py_INCREF((PyObject
*) &StatResultType
);
9125 PyModule_AddObject(m
, "stat_result", (PyObject
*) &StatResultType
);
9126 Py_INCREF((PyObject
*) &StatVFSResultType
);
9127 PyModule_AddObject(m
, "statvfs_result",
9128 (PyObject
*) &StatVFSResultType
);
9133 * Step 2 of weak-linking support on Mac OS X.
9135 * The code below removes functions that are not available on the
9136 * currently active platform.
9138 * This block allow one to use a python binary that was build on
9139 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
9142 #ifdef HAVE_FSTATVFS
9143 if (fstatvfs
== NULL
) {
9144 if (PyObject_DelAttrString(m
, "fstatvfs") == -1) {
9148 #endif /* HAVE_FSTATVFS */
9151 if (statvfs
== NULL
) {
9152 if (PyObject_DelAttrString(m
, "statvfs") == -1) {
9156 #endif /* HAVE_STATVFS */
9159 if (lchown
== NULL
) {
9160 if (PyObject_DelAttrString(m
, "lchown") == -1) {
9164 #endif /* HAVE_LCHOWN */
9167 #endif /* __APPLE__ */