Changeset 388 for python/vendor/current/Modules/posixmodule.c
- Timestamp:
- Mar 19, 2014, 11:11:30 AM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
python/vendor/current/Modules/posixmodule.c
r2 r388 12 12 as the compiler specific macro for the EMX port of gcc to OS/2. */ 13 13 14 /* See also ../Dos/dosmodule.c */15 16 14 #ifdef __APPLE__ 17 15 /* 18 * Step 1 of support for weak-linking a number of symbols existing on 16 * Step 1 of support for weak-linking a number of symbols existing on 19 17 * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block 20 18 * at the end of this file for more information. … … 30 28 #include "Python.h" 31 29 #include "structseq.h" 30 #ifndef MS_WINDOWS 31 #include "posixmodule.h" 32 #endif 32 33 33 34 #if defined(__VMS) … … 74 75 75 76 #ifdef HAVE_SYS_WAIT_H 76 #include <sys/wait.h> 77 #include <sys/wait.h> /* For WNOHANG */ 77 78 #endif 78 79 … … 102 103 #include <process.h> 103 104 #else 104 #if defined(__WATCOMC__) && !defined(__QNX__) 105 #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */ 105 106 #define HAVE_GETCWD 1 106 107 #define HAVE_OPENDIR 1 107 #define HAVE_SYSTEM 108 #define HAVE_SYSTEM 1 108 109 #if defined(__OS2__) 109 110 #define HAVE_EXECV 1 … … 112 113 #include <process.h> 113 114 #else 114 #ifdef __BORLANDC__ 115 #ifdef __BORLANDC__ /* Borland compiler */ 115 116 #define HAVE_EXECV 1 116 117 #define HAVE_GETCWD 1 … … 118 119 #define HAVE_PIPE 1 119 120 #define HAVE_POPEN 1 120 #define HAVE_SYSTEM 121 #define HAVE_SYSTEM 1 121 122 #define HAVE_WAIT 1 122 123 #else 123 #ifdef _MSC_VER 124 #ifdef _MSC_VER /* Microsoft compiler */ 124 125 #define HAVE_GETCWD 1 125 #define HAVE_SPAWNV 126 #define HAVE_SPAWNV 1 126 127 #define HAVE_EXECV 1 127 128 #define HAVE_PIPE 1 128 129 #define HAVE_POPEN 1 129 #define HAVE_SYSTEM 130 #define HAVE_CWAIT 131 #define HAVE_FSYNC 130 #define HAVE_SYSTEM 1 131 #define HAVE_CWAIT 1 132 #define HAVE_FSYNC 1 132 133 #define fsync _commit 133 134 #else 134 135 #if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS) 135 136 /* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */ 136 #else 137 #else /* all other compilers */ 137 138 /* Unix functions that the configure script doesn't check for */ 138 139 #define HAVE_EXECV 1 139 140 #define HAVE_FORK 1 140 #if defined(__USLC__) && defined(__SCO_VERSION__) 141 #if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */ 141 142 #define HAVE_FORK1 1 142 143 #endif … … 153 154 #define HAVE_POPEN 1 154 155 #endif 155 #define HAVE_SYSTEM 156 #define HAVE_SYSTEM 1 156 157 #define HAVE_WAIT 1 157 #define HAVE_TTYNAME 158 #define HAVE_TTYNAME 1 158 159 #endif /* PYOS_OS2 && PYCC_GCC && __VMS */ 159 160 #endif /* _MSC_VER */ … … 270 271 #endif 271 272 #include "osdefs.h" 273 #include <malloc.h> 272 274 #include <windows.h> 273 #include <shellapi.h> 274 #define popen 275 #define pclose 275 #include <shellapi.h> /* for ShellExecute() */ 276 #define popen _popen 277 #define pclose _pclose 276 278 #endif /* _MSC_VER */ 277 279 … … 338 340 #endif 339 341 340 /* choose the appropriate stat and fstat functions and return structs */341 #undef STAT342 #if defined(MS_WIN64) || defined(MS_WINDOWS)343 # define STAT win32_stat344 # define FSTAT win32_fstat345 # define STRUCT_STAT struct win32_stat346 #else347 # define STAT stat348 # define FSTAT fstat349 # define STRUCT_STAT struct stat350 #endif351 352 342 #if defined(MAJOR_IN_MKDEV) 353 343 #include <sys/mkdev.h> … … 361 351 #endif 362 352 353 354 #ifndef MS_WINDOWS 355 PyObject * 356 _PyInt_FromUid(uid_t uid) 357 { 358 if (uid <= LONG_MAX) 359 return PyInt_FromLong(uid); 360 return PyLong_FromUnsignedLong(uid); 361 } 362 363 PyObject * 364 _PyInt_FromGid(gid_t gid) 365 { 366 if (gid <= LONG_MAX) 367 return PyInt_FromLong(gid); 368 return PyLong_FromUnsignedLong(gid); 369 } 370 371 int 372 _Py_Uid_Converter(PyObject *obj, void *p) 373 { 374 int overflow; 375 long result; 376 if (PyFloat_Check(obj)) { 377 PyErr_SetString(PyExc_TypeError, 378 "integer argument expected, got float"); 379 return 0; 380 } 381 result = PyLong_AsLongAndOverflow(obj, &overflow); 382 if (overflow < 0) 383 goto OverflowDown; 384 if (!overflow && result == -1) { 385 /* error or -1 */ 386 if (PyErr_Occurred()) 387 return 0; 388 *(uid_t *)p = (uid_t)-1; 389 } 390 else { 391 /* unsigned uid_t */ 392 unsigned long uresult; 393 if (overflow > 0) { 394 uresult = PyLong_AsUnsignedLong(obj); 395 if (PyErr_Occurred()) { 396 if (PyErr_ExceptionMatches(PyExc_OverflowError)) 397 goto OverflowUp; 398 return 0; 399 } 400 } else { 401 if (result < 0) 402 goto OverflowDown; 403 uresult = result; 404 } 405 if (sizeof(uid_t) < sizeof(long) && 406 (unsigned long)(uid_t)uresult != uresult) 407 goto OverflowUp; 408 *(uid_t *)p = (uid_t)uresult; 409 } 410 return 1; 411 412 OverflowDown: 413 PyErr_SetString(PyExc_OverflowError, 414 "user id is less than minimum"); 415 return 0; 416 417 OverflowUp: 418 PyErr_SetString(PyExc_OverflowError, 419 "user id is greater than maximum"); 420 return 0; 421 } 422 423 int 424 _Py_Gid_Converter(PyObject *obj, void *p) 425 { 426 int overflow; 427 long result; 428 if (PyFloat_Check(obj)) { 429 PyErr_SetString(PyExc_TypeError, 430 "integer argument expected, got float"); 431 return 0; 432 } 433 result = PyLong_AsLongAndOverflow(obj, &overflow); 434 if (overflow < 0) 435 goto OverflowDown; 436 if (!overflow && result == -1) { 437 /* error or -1 */ 438 if (PyErr_Occurred()) 439 return 0; 440 *(gid_t *)p = (gid_t)-1; 441 } 442 else { 443 /* unsigned gid_t */ 444 unsigned long uresult; 445 if (overflow > 0) { 446 uresult = PyLong_AsUnsignedLong(obj); 447 if (PyErr_Occurred()) { 448 if (PyErr_ExceptionMatches(PyExc_OverflowError)) 449 goto OverflowUp; 450 return 0; 451 } 452 } else { 453 if (result < 0) 454 goto OverflowDown; 455 uresult = result; 456 } 457 if (sizeof(gid_t) < sizeof(long) && 458 (unsigned long)(gid_t)uresult != uresult) 459 goto OverflowUp; 460 *(gid_t *)p = (gid_t)uresult; 461 } 462 return 1; 463 464 OverflowDown: 465 PyErr_SetString(PyExc_OverflowError, 466 "group id is less than minimum"); 467 return 0; 468 469 OverflowUp: 470 PyErr_SetString(PyExc_OverflowError, 471 "group id is greater than maximum"); 472 return 0; 473 } 474 #endif /* MS_WINDOWS */ 475 476 477 #if defined _MSC_VER && _MSC_VER >= 1400 478 /* Microsoft CRT in VS2005 and higher will verify that a filehandle is 479 * valid and raise an assertion if it isn't. 480 * Normally, an invalid fd is likely to be a C program error and therefore 481 * an assertion can be useful, but it does contradict the POSIX standard 482 * which for write(2) states: 483 * "Otherwise, -1 shall be returned and errno set to indicate the error." 484 * "[EBADF] The fildes argument is not a valid file descriptor open for 485 * writing." 486 * Furthermore, python allows the user to enter any old integer 487 * as a fd and should merely raise a python exception on error. 488 * The Microsoft CRT doesn't provide an official way to check for the 489 * validity of a file descriptor, but we can emulate its internal behaviour 490 * by using the exported __pinfo data member and knowledge of the 491 * internal structures involved. 492 * The structures below must be updated for each version of visual studio 493 * according to the file internal.h in the CRT source, until MS comes 494 * up with a less hacky way to do this. 495 * (all of this is to avoid globally modifying the CRT behaviour using 496 * _set_invalid_parameter_handler() and _CrtSetReportMode()) 497 */ 498 /* The actual size of the structure is determined at runtime. 499 * Only the first items must be present. 500 */ 501 typedef struct { 502 intptr_t osfhnd; 503 char osfile; 504 } my_ioinfo; 505 506 extern __declspec(dllimport) char * __pioinfo[]; 507 #define IOINFO_L2E 5 508 #define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E) 509 #define IOINFO_ARRAYS 64 510 #define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS) 511 #define FOPEN 0x01 512 #define _NO_CONSOLE_FILENO (intptr_t)-2 513 514 /* This function emulates what the windows CRT does to validate file handles */ 515 int 516 _PyVerify_fd(int fd) 517 { 518 const int i1 = fd >> IOINFO_L2E; 519 const int i2 = fd & ((1 << IOINFO_L2E) - 1); 520 521 static int sizeof_ioinfo = 0; 522 523 /* Determine the actual size of the ioinfo structure, 524 * as used by the CRT loaded in memory 525 */ 526 if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) { 527 sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS; 528 } 529 if (sizeof_ioinfo == 0) { 530 /* This should not happen... */ 531 goto fail; 532 } 533 534 /* See that it isn't a special CLEAR fileno */ 535 if (fd != _NO_CONSOLE_FILENO) { 536 /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead 537 * we check pointer validity and other info 538 */ 539 if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) { 540 /* finally, check that the file is open */ 541 my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo); 542 if (info->osfile & FOPEN) { 543 return 1; 544 } 545 } 546 } 547 fail: 548 errno = EBADF; 549 return 0; 550 } 551 552 /* the special case of checking dup2. The target fd must be in a sensible range */ 553 static int 554 _PyVerify_fd_dup2(int fd1, int fd2) 555 { 556 if (!_PyVerify_fd(fd1)) 557 return 0; 558 if (fd2 == _NO_CONSOLE_FILENO) 559 return 0; 560 if ((unsigned)fd2 < _NHANDLE_) 561 return 1; 562 else 563 return 0; 564 } 565 #else 566 /* dummy version. _PyVerify_fd() is already defined in fileobject.h */ 567 #define _PyVerify_fd_dup2(A, B) (1) 568 #endif 569 363 570 /* Return a dictionary corresponding to the POSIX environment table */ 364 #if def WITH_NEXT_FRAMEWORK571 #if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED)) 365 572 /* On Darwin/MacOSX a shared library or framework has no access to 366 ** environ directly, we must obtain it with _NSGetEnviron(). 573 ** environ directly, we must obtain it with _NSGetEnviron(). See also 574 ** man environ(7). 367 575 */ 368 576 #include <crt_externs.h> … … 375 583 convertenviron(void) 376 584 { 377 PyObject *d; 378 char **e; 379 d = PyDict_New(); 380 if (d == NULL) 381 return NULL; 382 #ifdef WITH_NEXT_FRAMEWORK 383 if (environ == NULL) 384 environ = *_NSGetEnviron(); 385 #endif 386 if (environ == NULL) 387 return d; 388 /* This part ignores errors */ 389 for (e = environ; *e != NULL; e++) { 390 PyObject *k; 391 PyObject *v; 392 char *p = strchr(*e, '='); 393 if (p == NULL) 394 continue; 395 k = PyString_FromStringAndSize(*e, (int)(p-*e)); 396 if (k == NULL) { 397 PyErr_Clear(); 398 continue; 399 } 400 v = PyString_FromString(p+1); 401 if (v == NULL) { 402 PyErr_Clear(); 403 Py_DECREF(k); 404 continue; 405 } 406 if (PyDict_GetItem(d, k) == NULL) { 407 if (PyDict_SetItem(d, k, v) != 0) 408 PyErr_Clear(); 409 } 410 Py_DECREF(k); 411 Py_DECREF(v); 412 } 585 PyObject *d; 586 char **e; 413 587 #if defined(PYOS_OS2) 414 { 415 APIRET rc; 416 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */ 417 418 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH); 419 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */ 420 PyObject *v = PyString_FromString(buffer); 421 PyDict_SetItemString(d, "BEGINLIBPATH", v); 422 Py_DECREF(v); 588 APIRET rc; 589 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */ 590 #endif 591 d = PyDict_New(); 592 if (d == NULL) 593 return NULL; 594 #if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED)) 595 if (environ == NULL) 596 environ = *_NSGetEnviron(); 597 #endif 598 if (environ == NULL) 599 return d; 600 /* This part ignores errors */ 601 for (e = environ; *e != NULL; e++) { 602 PyObject *k; 603 PyObject *v; 604 char *p = strchr(*e, '='); 605 if (p == NULL) 606 continue; 607 k = PyString_FromStringAndSize(*e, (int)(p-*e)); 608 if (k == NULL) { 609 PyErr_Clear(); 610 continue; 423 611 } 424 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);425 if ( rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */426 Py Object *v = PyString_FromString(buffer);427 PyDict_SetItemString(d, "ENDLIBPATH", v);428 Py_DECREF(v);612 v = PyString_FromString(p+1); 613 if (v == NULL) { 614 PyErr_Clear(); 615 Py_DECREF(k); 616 continue; 429 617 } 430 } 431 #endif 432 return d; 618 if (PyDict_GetItem(d, k) == NULL) { 619 if (PyDict_SetItem(d, k, v) != 0) 620 PyErr_Clear(); 621 } 622 Py_DECREF(k); 623 Py_DECREF(v); 624 } 625 #if defined(PYOS_OS2) 626 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH); 627 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */ 628 PyObject *v = PyString_FromString(buffer); 629 PyDict_SetItemString(d, "BEGINLIBPATH", v); 630 Py_DECREF(v); 631 } 632 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH); 633 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */ 634 PyObject *v = PyString_FromString(buffer); 635 PyDict_SetItemString(d, "ENDLIBPATH", v); 636 Py_DECREF(v); 637 } 638 #endif 639 return d; 433 640 } 434 641 … … 439 646 posix_error(void) 440 647 { 441 648 return PyErr_SetFromErrno(PyExc_OSError); 442 649 } 443 650 static PyObject * 444 651 posix_error_with_filename(char* name) 445 652 { 446 447 } 448 449 #ifdef Py_WIN_WIDE_FILENAMES653 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); 654 } 655 656 #ifdef MS_WINDOWS 450 657 static PyObject * 451 658 posix_error_with_unicode_filename(Py_UNICODE* name) 452 659 { 453 454 } 455 #endif /* Py_WIN_WIDE_FILENAMES */660 return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name); 661 } 662 #endif /* MS_WINDOWS */ 456 663 457 664 … … 459 666 posix_error_with_allocated_filename(char* name) 460 667 { 461 462 463 668 PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); 669 PyMem_Free(name); 670 return rc; 464 671 } 465 672 … … 468 675 win32_error(char* function, char* filename) 469 676 { 470 /* XXX We should pass the function name along in the future. 471 (_winreg.c also wants to pass the function name.) 472 This would however require an additional param to the 473 Windows error object, which is non-trivial. 474 */ 475 errno = GetLastError(); 476 if (filename) 477 return PyErr_SetFromWindowsErrWithFilename(errno, filename); 478 else 479 return PyErr_SetFromWindowsErr(errno); 480 } 481 482 #ifdef Py_WIN_WIDE_FILENAMES 677 /* XXX We should pass the function name along in the future. 678 (_winreg.c also wants to pass the function name.) 679 This would however require an additional param to the 680 Windows error object, which is non-trivial. 681 */ 682 errno = GetLastError(); 683 if (filename) 684 return PyErr_SetFromWindowsErrWithFilename(errno, filename); 685 else 686 return PyErr_SetFromWindowsErr(errno); 687 } 688 483 689 static PyObject * 484 690 win32_error_unicode(char* function, Py_UNICODE* filename) 485 691 { 486 /* XXX - see win32_error for comments on 'function' */ 487 errno = GetLastError(); 488 if (filename) 489 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename); 490 else 491 return PyErr_SetFromWindowsErr(errno); 492 } 493 494 static PyObject *_PyUnicode_FromFileSystemEncodedObject(register PyObject *obj) 495 { 692 /* XXX - see win32_error for comments on 'function' */ 693 errno = GetLastError(); 694 if (filename) 695 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename); 696 else 697 return PyErr_SetFromWindowsErr(errno); 496 698 } 497 699 … … 499 701 convert_to_unicode(PyObject **param) 500 702 { 501 if (PyUnicode_CheckExact(*param)) 502 Py_INCREF(*param); 503 else if (PyUnicode_Check(*param)) 504 /* For a Unicode subtype that's not a Unicode object, 505 return a true Unicode object with the same data. */ 506 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param), 507 PyUnicode_GET_SIZE(*param)); 508 else 509 *param = PyUnicode_FromEncodedObject(*param, 510 Py_FileSystemDefaultEncoding, 511 "strict"); 512 return (*param) != NULL; 513 } 514 515 #endif /* Py_WIN_WIDE_FILENAMES */ 516 517 #endif 703 if (PyUnicode_CheckExact(*param)) 704 Py_INCREF(*param); 705 else if (PyUnicode_Check(*param)) 706 /* For a Unicode subtype that's not a Unicode object, 707 return a true Unicode object with the same data. */ 708 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param), 709 PyUnicode_GET_SIZE(*param)); 710 else 711 *param = PyUnicode_FromEncodedObject(*param, 712 Py_FileSystemDefaultEncoding, 713 "strict"); 714 return (*param) != NULL; 715 } 716 717 #endif /* MS_WINDOWS */ 518 718 519 719 #if defined(PYOS_OS2) … … 521 721 * Helper Function to Trim and Format OS/2 Messages 522 722 **********************************************************************/ 523 723 static void 524 724 os2_formatmsg(char *msgbuf, int msglen, char *reason) 525 725 { … … 551 751 * 552 752 **********************************************************************/ 553 753 static char * 554 754 os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason) 555 755 { … … 567 767 else 568 768 PyOS_snprintf(msgbuf, msgbuflen, 569 769 "unknown OS error #%d", errorcode); 570 770 571 771 return msgbuf; … … 576 776 they congruent with posix error numbers. */ 577 777 578 static PyObject * os2_error(int code) 778 static PyObject * 779 os2_error(int code) 579 780 { 580 781 char text[1024]; … … 598 799 posix_fildes(PyObject *fdobj, int (*func)(int)) 599 800 { 600 int fd; 601 int res; 602 fd = PyObject_AsFileDescriptor(fdobj); 603 if (fd < 0) 604 return NULL; 605 Py_BEGIN_ALLOW_THREADS 606 res = (*func)(fd); 607 Py_END_ALLOW_THREADS 608 if (res < 0) 609 return posix_error(); 610 Py_INCREF(Py_None); 611 return Py_None; 612 } 613 614 #ifdef Py_WIN_WIDE_FILENAMES 615 static int 616 unicode_file_names(void) 617 { 618 static int canusewide = -1; 619 if (canusewide == -1) { 620 /* As per doc for ::GetVersion(), this is the correct test for 621 the Windows NT family. */ 622 canusewide = (GetVersion() < 0x80000000) ? 1 : 0; 623 } 624 return canusewide; 625 } 626 #endif 801 int fd; 802 int res; 803 fd = PyObject_AsFileDescriptor(fdobj); 804 if (fd < 0) 805 return NULL; 806 if (!_PyVerify_fd(fd)) 807 return posix_error(); 808 Py_BEGIN_ALLOW_THREADS 809 res = (*func)(fd); 810 Py_END_ALLOW_THREADS 811 if (res < 0) 812 return posix_error(); 813 Py_INCREF(Py_None); 814 return Py_None; 815 } 627 816 628 817 static PyObject * 629 818 posix_1str(PyObject *args, char *format, int (*func)(const char*)) 630 819 { 631 632 633 634 635 636 637 638 639 640 641 642 643 820 char *path1 = NULL; 821 int res; 822 if (!PyArg_ParseTuple(args, format, 823 Py_FileSystemDefaultEncoding, &path1)) 824 return NULL; 825 Py_BEGIN_ALLOW_THREADS 826 res = (*func)(path1); 827 Py_END_ALLOW_THREADS 828 if (res < 0) 829 return posix_error_with_allocated_filename(path1); 830 PyMem_Free(path1); 831 Py_INCREF(Py_None); 832 return Py_None; 644 833 } 645 834 646 835 static PyObject * 647 836 posix_2str(PyObject *args, 648 649 650 { 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 } 668 669 #ifdef Py_WIN_WIDE_FILENAMES837 char *format, 838 int (*func)(const char *, const char *)) 839 { 840 char *path1 = NULL, *path2 = NULL; 841 int res; 842 if (!PyArg_ParseTuple(args, format, 843 Py_FileSystemDefaultEncoding, &path1, 844 Py_FileSystemDefaultEncoding, &path2)) 845 return NULL; 846 Py_BEGIN_ALLOW_THREADS 847 res = (*func)(path1, path2); 848 Py_END_ALLOW_THREADS 849 PyMem_Free(path1); 850 PyMem_Free(path2); 851 if (res != 0) 852 /* XXX how to report both path1 and path2??? */ 853 return posix_error(); 854 Py_INCREF(Py_None); 855 return Py_None; 856 } 857 858 #ifdef MS_WINDOWS 670 859 static PyObject* 671 win32_1str(PyObject* args, char* func, 672 char* format, BOOL (__stdcall *funcA)(LPCSTR), 673 char* wformat, BOOL (__stdcall *funcW)(LPWSTR)) 674 { 675 PyObject *uni; 676 char *ansi; 677 BOOL result; 678 if (unicode_file_names()) { 679 if (!PyArg_ParseTuple(args, wformat, &uni)) 680 PyErr_Clear(); 681 else { 682 Py_BEGIN_ALLOW_THREADS 683 result = funcW(PyUnicode_AsUnicode(uni)); 684 Py_END_ALLOW_THREADS 685 if (!result) 686 return win32_error_unicode(func, PyUnicode_AsUnicode(uni)); 687 Py_INCREF(Py_None); 688 return Py_None; 689 } 690 } 691 if (!PyArg_ParseTuple(args, format, &ansi)) 692 return NULL; 693 Py_BEGIN_ALLOW_THREADS 694 result = funcA(ansi); 695 Py_END_ALLOW_THREADS 696 if (!result) 697 return win32_error(func, ansi); 698 Py_INCREF(Py_None); 699 return Py_None; 860 win32_1str(PyObject* args, char* func, 861 char* format, BOOL (__stdcall *funcA)(LPCSTR), 862 char* wformat, BOOL (__stdcall *funcW)(LPWSTR)) 863 { 864 PyObject *uni; 865 char *ansi; 866 BOOL result; 867 868 if (!PyArg_ParseTuple(args, wformat, &uni)) 869 PyErr_Clear(); 870 else { 871 Py_BEGIN_ALLOW_THREADS 872 result = funcW(PyUnicode_AsUnicode(uni)); 873 Py_END_ALLOW_THREADS 874 if (!result) 875 return win32_error_unicode(func, PyUnicode_AsUnicode(uni)); 876 Py_INCREF(Py_None); 877 return Py_None; 878 } 879 if (!PyArg_ParseTuple(args, format, &ansi)) 880 return NULL; 881 Py_BEGIN_ALLOW_THREADS 882 result = funcA(ansi); 883 Py_END_ALLOW_THREADS 884 if (!result) 885 return win32_error(func, ansi); 886 Py_INCREF(Py_None); 887 return Py_None; 700 888 701 889 } … … 706 894 it also needs to set "magic" environment variables indicating 707 895 the per-drive current directory, which are of the form =<drive>: */ 708 BOOL __stdcall896 static BOOL __stdcall 709 897 win32_chdir(LPCSTR path) 710 898 { 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 899 char new_path[MAX_PATH+1]; 900 int result; 901 char env[4] = "=x:"; 902 903 if(!SetCurrentDirectoryA(path)) 904 return FALSE; 905 result = GetCurrentDirectoryA(MAX_PATH+1, new_path); 906 if (!result) 907 return FALSE; 908 /* In the ANSI API, there should not be any paths longer 909 than MAX_PATH. */ 910 assert(result <= MAX_PATH+1); 911 if (strncmp(new_path, "\\\\", 2) == 0 || 912 strncmp(new_path, "//", 2) == 0) 913 /* UNC path, nothing to do. */ 914 return TRUE; 915 env[1] = new_path[0]; 916 return SetEnvironmentVariableA(env, new_path); 729 917 } 730 918 731 919 /* The Unicode version differs from the ANSI version 732 920 since the current directory might exceed MAX_PATH characters */ 733 BOOL __stdcall921 static BOOL __stdcall 734 922 win32_wchdir(LPCWSTR path) 735 923 { 736 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path; 737 int result; 738 wchar_t env[4] = L"=x:"; 739 740 if(!SetCurrentDirectoryW(path)) 741 return FALSE; 742 result = GetCurrentDirectoryW(MAX_PATH+1, new_path); 743 if (!result) 744 return FALSE; 745 if (result > MAX_PATH+1) { 746 new_path = malloc(result * sizeof(wchar_t)); 747 if (!new_path) { 748 SetLastError(ERROR_OUTOFMEMORY); 749 return FALSE; 750 } 751 result = GetCurrentDirectoryW(result, new_path); 752 if (!result) { 753 free(new_path); 754 return FALSE; 755 } 756 } 757 if (wcsncmp(new_path, L"\\\\", 2) == 0 || 758 wcsncmp(new_path, L"//", 2) == 0) 759 /* UNC path, nothing to do. */ 760 return TRUE; 761 env[1] = new_path[0]; 762 result = SetEnvironmentVariableW(env, new_path); 763 if (new_path != _new_path) 764 free(new_path); 765 return result; 766 } 924 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path; 925 int result; 926 wchar_t env[4] = L"=x:"; 927 928 if(!SetCurrentDirectoryW(path)) 929 return FALSE; 930 result = GetCurrentDirectoryW(MAX_PATH+1, new_path); 931 if (!result) 932 return FALSE; 933 if (result > MAX_PATH+1) { 934 new_path = malloc(result * sizeof(wchar_t)); 935 if (!new_path) { 936 SetLastError(ERROR_OUTOFMEMORY); 937 return FALSE; 938 } 939 result = GetCurrentDirectoryW(result, new_path); 940 if (!result) { 941 free(new_path); 942 return FALSE; 943 } 944 } 945 if (wcsncmp(new_path, L"\\\\", 2) == 0 || 946 wcsncmp(new_path, L"//", 2) == 0) 947 /* UNC path, nothing to do. */ 948 return TRUE; 949 env[1] = new_path[0]; 950 result = SetEnvironmentVariableW(env, new_path); 951 if (new_path != _new_path) 952 free(new_path); 953 return result; 954 } 955 #endif 956 957 /* choose the appropriate stat and fstat functions and return structs */ 958 #undef STAT 959 #undef FSTAT 960 #undef STRUCT_STAT 961 #if defined(MS_WIN64) || defined(MS_WINDOWS) 962 # define STAT win32_stat 963 # define FSTAT win32_fstat 964 # define STRUCT_STAT struct win32_stat 965 #else 966 # define STAT stat 967 # define FSTAT fstat 968 # define STRUCT_STAT struct stat 767 969 #endif 768 970 … … 774 976 Therefore, we implement our own stat, based on the Win32 API directly. 775 977 */ 776 #define HAVE_STAT_NSEC 1 978 #define HAVE_STAT_NSEC 1 777 979 778 980 struct win32_stat{ … … 785 987 int st_rdev; 786 988 __int64 st_size; 787 int st_atime;989 time_t st_atime; 788 990 int st_atime_nsec; 789 int st_mtime;991 time_t st_mtime; 790 992 int st_mtime_nsec; 791 int st_ctime;993 time_t st_ctime; 792 994 int st_ctime_nsec; 793 995 }; … … 796 998 797 999 static void 798 FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out) 799 { 800 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */ 801 /* Cannot simply cast and dereference in_ptr, 802 since it might not be aligned properly */ 803 __int64 in; 804 memcpy(&in, in_ptr, sizeof(in)); 805 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */ 806 /* XXX Win32 supports time stamps past 2038; we currently don't */ 807 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int); 1000 FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, time_t *time_out, int* nsec_out) 1001 { 1002 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */ 1003 /* Cannot simply cast and dereference in_ptr, 1004 since it might not be aligned properly */ 1005 __int64 in; 1006 memcpy(&in, in_ptr, sizeof(in)); 1007 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */ 1008 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, time_t); 808 1009 } 809 1010 810 1011 static void 811 time_t_to_FILE_TIME( int time_in, int nsec_in, FILETIME *out_ptr)812 { 813 814 815 816 817 1012 time_t_to_FILE_TIME(time_t time_in, int nsec_in, FILETIME *out_ptr) 1013 { 1014 /* XXX endianness */ 1015 __int64 out; 1016 out = time_in + secs_between_epochs; 1017 out = out * 10000000 + nsec_in / 100; 1018 memcpy(out_ptr, &out, sizeof(out)); 818 1019 } 819 1020 … … 825 1026 attributes_to_mode(DWORD attr) 826 1027 { 827 828 829 830 831 832 833 834 835 836 1028 int m = 0; 1029 if (attr & FILE_ATTRIBUTE_DIRECTORY) 1030 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */ 1031 else 1032 m |= _S_IFREG; 1033 if (attr & FILE_ATTRIBUTE_READONLY) 1034 m |= 0444; 1035 else 1036 m |= 0666; 1037 return m; 837 1038 } 838 1039 … … 840 1041 attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result) 841 1042 { 842 memset(result, 0, sizeof(*result)); 843 result->st_mode = attributes_to_mode(info->dwFileAttributes); 844 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow; 845 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); 846 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); 847 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec); 848 849 return 0; 850 } 851 852 /* Emulate GetFileAttributesEx[AW] on Windows 95 */ 853 static int checked = 0; 854 static BOOL (CALLBACK *gfaxa)(LPCSTR, GET_FILEEX_INFO_LEVELS, LPVOID); 855 static BOOL (CALLBACK *gfaxw)(LPCWSTR, GET_FILEEX_INFO_LEVELS, LPVOID); 856 static void 857 check_gfax() 858 { 859 HINSTANCE hKernel32; 860 if (checked) 861 return; 862 checked = 1; 863 hKernel32 = GetModuleHandle("KERNEL32"); 864 *(FARPROC*)&gfaxa = GetProcAddress(hKernel32, "GetFileAttributesExA"); 865 *(FARPROC*)&gfaxw = GetProcAddress(hKernel32, "GetFileAttributesExW"); 1043 memset(result, 0, sizeof(*result)); 1044 result->st_mode = attributes_to_mode(info->dwFileAttributes); 1045 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow; 1046 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); 1047 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); 1048 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec); 1049 1050 return 0; 866 1051 } 867 1052 … … 869 1054 attributes_from_dir(LPCSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad) 870 1055 { 871 872 873 874 875 876 877 878 879 880 881 882 883 1056 HANDLE hFindFile; 1057 WIN32_FIND_DATAA FileData; 1058 hFindFile = FindFirstFileA(pszFile, &FileData); 1059 if (hFindFile == INVALID_HANDLE_VALUE) 1060 return FALSE; 1061 FindClose(hFindFile); 1062 pfad->dwFileAttributes = FileData.dwFileAttributes; 1063 pfad->ftCreationTime = FileData.ftCreationTime; 1064 pfad->ftLastAccessTime = FileData.ftLastAccessTime; 1065 pfad->ftLastWriteTime = FileData.ftLastWriteTime; 1066 pfad->nFileSizeHigh = FileData.nFileSizeHigh; 1067 pfad->nFileSizeLow = FileData.nFileSizeLow; 1068 return TRUE; 884 1069 } 885 1070 … … 887 1072 attributes_from_dir_w(LPCWSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad) 888 1073 { 889 HANDLE hFindFile; 890 WIN32_FIND_DATAW FileData; 891 hFindFile = FindFirstFileW(pszFile, &FileData); 892 if (hFindFile == INVALID_HANDLE_VALUE) 893 return FALSE; 894 FindClose(hFindFile); 895 pfad->dwFileAttributes = FileData.dwFileAttributes; 896 pfad->ftCreationTime = FileData.ftCreationTime; 897 pfad->ftLastAccessTime = FileData.ftLastAccessTime; 898 pfad->ftLastWriteTime = FileData.ftLastWriteTime; 899 pfad->nFileSizeHigh = FileData.nFileSizeHigh; 900 pfad->nFileSizeLow = FileData.nFileSizeLow; 901 return TRUE; 902 } 903 904 static BOOL WINAPI 905 Py_GetFileAttributesExA(LPCSTR pszFile, 906 GET_FILEEX_INFO_LEVELS level, 907 LPVOID pv) 908 { 909 BOOL result; 910 LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv; 911 /* First try to use the system's implementation, if that is 912 available and either succeeds to gives an error other than 913 that it isn't implemented. */ 914 check_gfax(); 915 if (gfaxa) { 916 result = gfaxa(pszFile, level, pv); 917 if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) 918 return result; 919 } 920 /* It's either not present, or not implemented. 921 Emulate using FindFirstFile. */ 922 if (level != GetFileExInfoStandard) { 923 SetLastError(ERROR_INVALID_PARAMETER); 924 return FALSE; 925 } 926 /* Use GetFileAttributes to validate that the file name 927 does not contain wildcards (which FindFirstFile would 928 accept). */ 929 if (GetFileAttributesA(pszFile) == 0xFFFFFFFF) 930 return FALSE; 931 return attributes_from_dir(pszFile, pfad); 932 } 933 934 static BOOL WINAPI 935 Py_GetFileAttributesExW(LPCWSTR pszFile, 936 GET_FILEEX_INFO_LEVELS level, 937 LPVOID pv) 938 { 939 BOOL result; 940 LPWIN32_FILE_ATTRIBUTE_DATA pfad = pv; 941 /* First try to use the system's implementation, if that is 942 available and either succeeds to gives an error other than 943 that it isn't implemented. */ 944 check_gfax(); 945 if (gfaxa) { 946 result = gfaxw(pszFile, level, pv); 947 if (result || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) 948 return result; 949 } 950 /* It's either not present, or not implemented. 951 Emulate using FindFirstFile. */ 952 if (level != GetFileExInfoStandard) { 953 SetLastError(ERROR_INVALID_PARAMETER); 954 return FALSE; 955 } 956 /* Use GetFileAttributes to validate that the file name 957 does not contain wildcards (which FindFirstFile would 958 accept). */ 959 if (GetFileAttributesW(pszFile) == 0xFFFFFFFF) 960 return FALSE; 961 return attributes_from_dir_w(pszFile, pfad); 962 } 963 964 static int 1074 HANDLE hFindFile; 1075 WIN32_FIND_DATAW FileData; 1076 hFindFile = FindFirstFileW(pszFile, &FileData); 1077 if (hFindFile == INVALID_HANDLE_VALUE) 1078 return FALSE; 1079 FindClose(hFindFile); 1080 pfad->dwFileAttributes = FileData.dwFileAttributes; 1081 pfad->ftCreationTime = FileData.ftCreationTime; 1082 pfad->ftLastAccessTime = FileData.ftLastAccessTime; 1083 pfad->ftLastWriteTime = FileData.ftLastWriteTime; 1084 pfad->nFileSizeHigh = FileData.nFileSizeHigh; 1085 pfad->nFileSizeLow = FileData.nFileSizeLow; 1086 return TRUE; 1087 } 1088 1089 static int 965 1090 win32_stat(const char* path, struct win32_stat *result) 966 1091 { 967 WIN32_FILE_ATTRIBUTE_DATA info; 968 int code; 969 char *dot; 970 /* XXX not supported on Win95 and NT 3.x */ 971 if (!Py_GetFileAttributesExA(path, GetFileExInfoStandard, &info)) { 972 if (GetLastError() != ERROR_SHARING_VIOLATION) { 973 /* Protocol violation: we explicitly clear errno, instead of 974 setting it to a POSIX error. Callers should use GetLastError. */ 975 errno = 0; 976 return -1; 977 } else { 978 /* Could not get attributes on open file. Fall back to 979 reading the directory. */ 980 if (!attributes_from_dir(path, &info)) { 981 /* Very strange. This should not fail now */ 982 errno = 0; 983 return -1; 984 } 985 } 986 } 987 code = attribute_data_to_stat(&info, result); 988 if (code != 0) 989 return code; 990 /* Set S_IFEXEC if it is an .exe, .bat, ... */ 991 dot = strrchr(path, '.'); 992 if (dot) { 993 if (stricmp(dot, ".bat") == 0 || 994 stricmp(dot, ".cmd") == 0 || 995 stricmp(dot, ".exe") == 0 || 996 stricmp(dot, ".com") == 0) 997 result->st_mode |= 0111; 998 } 999 return code; 1000 } 1001 1002 static int 1092 WIN32_FILE_ATTRIBUTE_DATA info; 1093 int code; 1094 char *dot; 1095 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &info)) { 1096 if (GetLastError() != ERROR_SHARING_VIOLATION) { 1097 /* Protocol violation: we explicitly clear errno, instead of 1098 setting it to a POSIX error. Callers should use GetLastError. */ 1099 errno = 0; 1100 return -1; 1101 } else { 1102 /* Could not get attributes on open file. Fall back to 1103 reading the directory. */ 1104 if (!attributes_from_dir(path, &info)) { 1105 /* Very strange. This should not fail now */ 1106 errno = 0; 1107 return -1; 1108 } 1109 } 1110 } 1111 code = attribute_data_to_stat(&info, result); 1112 if (code != 0) 1113 return code; 1114 /* Set S_IFEXEC if it is an .exe, .bat, ... */ 1115 dot = strrchr(path, '.'); 1116 if (dot) { 1117 if (stricmp(dot, ".bat") == 0 || 1118 stricmp(dot, ".cmd") == 0 || 1119 stricmp(dot, ".exe") == 0 || 1120 stricmp(dot, ".com") == 0) 1121 result->st_mode |= 0111; 1122 } 1123 return code; 1124 } 1125 1126 static int 1003 1127 win32_wstat(const wchar_t* path, struct win32_stat *result) 1004 1128 { 1005 int code; 1006 const wchar_t *dot; 1007 WIN32_FILE_ATTRIBUTE_DATA info; 1008 /* XXX not supported on Win95 and NT 3.x */ 1009 if (!Py_GetFileAttributesExW(path, GetFileExInfoStandard, &info)) { 1010 if (GetLastError() != ERROR_SHARING_VIOLATION) { 1011 /* Protocol violation: we explicitly clear errno, instead of 1012 setting it to a POSIX error. Callers should use GetLastError. */ 1013 errno = 0; 1014 return -1; 1015 } else { 1016 /* Could not get attributes on open file. Fall back to 1017 reading the directory. */ 1018 if (!attributes_from_dir_w(path, &info)) { 1019 /* Very strange. This should not fail now */ 1020 errno = 0; 1021 return -1; 1022 } 1023 } 1024 } 1025 code = attribute_data_to_stat(&info, result); 1026 if (code < 0) 1027 return code; 1028 /* Set IFEXEC if it is an .exe, .bat, ... */ 1029 dot = wcsrchr(path, '.'); 1030 if (dot) { 1031 if (_wcsicmp(dot, L".bat") == 0 || 1032 _wcsicmp(dot, L".cmd") == 0 || 1033 _wcsicmp(dot, L".exe") == 0 || 1034 _wcsicmp(dot, L".com") == 0) 1035 result->st_mode |= 0111; 1036 } 1037 return code; 1129 int code; 1130 const wchar_t *dot; 1131 WIN32_FILE_ATTRIBUTE_DATA info; 1132 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &info)) { 1133 if (GetLastError() != ERROR_SHARING_VIOLATION) { 1134 /* Protocol violation: we explicitly clear errno, instead of 1135 setting it to a POSIX error. Callers should use GetLastError. */ 1136 errno = 0; 1137 return -1; 1138 } else { 1139 /* Could not get attributes on open file. Fall back to 1140 reading the directory. */ 1141 if (!attributes_from_dir_w(path, &info)) { 1142 /* Very strange. This should not fail now */ 1143 errno = 0; 1144 return -1; 1145 } 1146 } 1147 } 1148 code = attribute_data_to_stat(&info, result); 1149 if (code < 0) 1150 return code; 1151 /* Set IFEXEC if it is an .exe, .bat, ... */ 1152 dot = wcsrchr(path, '.'); 1153 if (dot) { 1154 if (_wcsicmp(dot, L".bat") == 0 || 1155 _wcsicmp(dot, L".cmd") == 0 || 1156 _wcsicmp(dot, L".exe") == 0 || 1157 _wcsicmp(dot, L".com") == 0) 1158 result->st_mode |= 0111; 1159 } 1160 return code; 1038 1161 } 1039 1162 … … 1041 1164 win32_fstat(int file_number, struct win32_stat *result) 1042 1165 { 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1166 BY_HANDLE_FILE_INFORMATION info; 1167 HANDLE h; 1168 int type; 1169 1170 h = (HANDLE)_get_osfhandle(file_number); 1171 1172 /* Protocol violation: we explicitly clear errno, instead of 1173 setting it to a POSIX error. Callers should use GetLastError. */ 1174 errno = 0; 1175 1176 if (h == INVALID_HANDLE_VALUE) { 1177 /* This is really a C library error (invalid file handle). 1178 We set the Win32 error to the closes one matching. */ 1179 SetLastError(ERROR_INVALID_HANDLE); 1180 return -1; 1181 } 1182 memset(result, 0, sizeof(*result)); 1183 1184 type = GetFileType(h); 1185 if (type == FILE_TYPE_UNKNOWN) { 1186 DWORD error = GetLastError(); 1187 if (error != 0) { 1188 return -1; 1189 } 1190 /* else: valid but unknown file */ 1191 } 1192 1193 if (type != FILE_TYPE_DISK) { 1194 if (type == FILE_TYPE_CHAR) 1195 result->st_mode = _S_IFCHR; 1196 else if (type == FILE_TYPE_PIPE) 1197 result->st_mode = _S_IFIFO; 1198 return 0; 1199 } 1200 1201 if (!GetFileInformationByHandle(h, &info)) { 1202 return -1; 1203 } 1204 1205 /* similar to stat() */ 1206 result->st_mode = attributes_to_mode(info.dwFileAttributes); 1207 result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow; 1208 FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); 1209 FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); 1210 FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec); 1211 /* specific to fstat() */ 1212 result->st_nlink = info.nNumberOfLinks; 1213 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow; 1214 return 0; 1092 1215 } 1093 1216 … … 1106 1229 1107 1230 static PyStructSequence_Field stat_result_fields[] = { 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1231 {"st_mode", "protection bits"}, 1232 {"st_ino", "inode"}, 1233 {"st_dev", "device"}, 1234 {"st_nlink", "number of hard links"}, 1235 {"st_uid", "user ID of owner"}, 1236 {"st_gid", "group ID of owner"}, 1237 {"st_size", "total size, in bytes"}, 1238 /* The NULL is replaced with PyStructSequence_UnnamedField later. */ 1239 {NULL, "integer time of last access"}, 1240 {NULL, "integer time of last modification"}, 1241 {NULL, "integer time of last change"}, 1242 {"st_atime", "time of last access"}, 1243 {"st_mtime", "time of last modification"}, 1244 {"st_ctime", "time of last change"}, 1122 1245 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE 1123 1246 {"st_blksize", "blocksize for filesystem I/O"}, 1124 1247 #endif 1125 1248 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS 1126 1249 {"st_blocks", "number of blocks allocated"}, 1127 1250 #endif 1128 1251 #ifdef HAVE_STRUCT_STAT_ST_RDEV 1129 1252 {"st_rdev", "device type (if inode device)"}, 1130 1253 #endif 1131 1254 #ifdef HAVE_STRUCT_STAT_ST_FLAGS 1132 1255 {"st_flags", "user defined flags for file"}, 1133 1256 #endif 1134 1257 #ifdef HAVE_STRUCT_STAT_ST_GEN 1135 1258 {"st_gen", "generation number"}, 1136 1259 #endif 1137 1260 #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME 1138 1139 #endif 1140 1261 {"st_birthtime", "time of creation"}, 1262 #endif 1263 {0} 1141 1264 }; 1142 1265 … … 1178 1301 1179 1302 static PyStructSequence_Desc stat_result_desc = { 1180 1181 1182 1183 1303 "stat_result", /* name */ 1304 stat_result__doc__, /* doc */ 1305 stat_result_fields, 1306 10 1184 1307 }; 1185 1308 … … 1193 1316 1194 1317 static PyStructSequence_Field statvfs_result_fields[] = { 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1318 {"f_bsize", }, 1319 {"f_frsize", }, 1320 {"f_blocks", }, 1321 {"f_bfree", }, 1322 {"f_bavail", }, 1323 {"f_files", }, 1324 {"f_ffree", }, 1325 {"f_favail", }, 1326 {"f_flag", }, 1327 {"f_namemax",}, 1328 {0} 1206 1329 }; 1207 1330 1208 1331 static PyStructSequence_Desc statvfs_result_desc = { 1209 1210 1211 1212 1332 "statvfs_result", /* name */ 1333 statvfs_result__doc__, /* doc */ 1334 statvfs_result_fields, 1335 10 1213 1336 }; 1214 1337 … … 1221 1344 statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) 1222 1345 { 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1346 PyStructSequence *result; 1347 int i; 1348 1349 result = (PyStructSequence*)structseq_new(type, args, kwds); 1350 if (!result) 1351 return NULL; 1352 /* If we have been initialized from a tuple, 1353 st_?time might be set to None. Initialize it 1354 from the int slots. */ 1355 for (i = 7; i <= 9; i++) { 1356 if (result->ob_item[i+3] == Py_None) { 1357 Py_DECREF(Py_None); 1358 Py_INCREF(result->ob_item[i]); 1359 result->ob_item[i+3] = result->ob_item[i]; 1360 } 1361 } 1362 return (PyObject*)result; 1240 1363 } 1241 1364 … … 1255 1378 stat_float_times(PyObject* self, PyObject *args) 1256 1379 { 1257 1258 1259 1260 1261 1262 1263 1264 1265 1380 int newval = -1; 1381 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval)) 1382 return NULL; 1383 if (newval == -1) 1384 /* Return old value */ 1385 return PyBool_FromLong(_stat_float_times); 1386 _stat_float_times = newval; 1387 Py_INCREF(Py_None); 1388 return Py_None; 1266 1389 } 1267 1390 … … 1269 1392 fill_time(PyObject *v, int index, time_t sec, unsigned long nsec) 1270 1393 { 1271 1394 PyObject *fval,*ival; 1272 1395 #if SIZEOF_TIME_T > SIZEOF_LONG 1273 1396 ival = PyLong_FromLongLong((PY_LONG_LONG)sec); 1274 1397 #else 1275 1276 #endif 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1398 ival = PyInt_FromLong((long)sec); 1399 #endif 1400 if (!ival) 1401 return; 1402 if (_stat_float_times) { 1403 fval = PyFloat_FromDouble(sec + 1e-9*nsec); 1404 } else { 1405 fval = ival; 1406 Py_INCREF(fval); 1407 } 1408 PyStructSequence_SET_ITEM(v, index, ival); 1409 PyStructSequence_SET_ITEM(v, index+3, fval); 1287 1410 } 1288 1411 … … 1292 1415 _pystat_fromstructstat(STRUCT_STAT *st) 1293 1416 { 1294 1295 1296 1297 1298 1299 1417 unsigned long ansec, mnsec, cnsec; 1418 PyObject *v = PyStructSequence_New(&StatResultType); 1419 if (v == NULL) 1420 return NULL; 1421 1422 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st->st_mode)); 1300 1423 #ifdef HAVE_LARGEFILE_SUPPORT 1301 1302 1424 PyStructSequence_SET_ITEM(v, 1, 1425 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino)); 1303 1426 #else 1304 1427 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st->st_ino)); 1305 1428 #endif 1306 1429 #if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS) 1307 1308 1430 PyStructSequence_SET_ITEM(v, 2, 1431 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev)); 1309 1432 #else 1310 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st->st_dev)); 1311 #endif 1312 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st->st_nlink)); 1313 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st->st_uid)); 1314 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st->st_gid)); 1433 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st->st_dev)); 1434 #endif 1435 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st->st_nlink)); 1436 #if defined(MS_WINDOWS) 1437 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong(0)); 1438 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong(0)); 1439 #else 1440 PyStructSequence_SET_ITEM(v, 4, _PyInt_FromUid(st->st_uid)); 1441 PyStructSequence_SET_ITEM(v, 5, _PyInt_FromGid(st->st_gid)); 1442 #endif 1315 1443 #ifdef HAVE_LARGEFILE_SUPPORT 1316 1317 1444 PyStructSequence_SET_ITEM(v, 6, 1445 PyLong_FromLongLong((PY_LONG_LONG)st->st_size)); 1318 1446 #else 1319 1447 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong(st->st_size)); 1320 1448 #endif 1321 1449 1322 1450 #if defined(HAVE_STAT_TV_NSEC) 1323 1324 1325 1451 ansec = st->st_atim.tv_nsec; 1452 mnsec = st->st_mtim.tv_nsec; 1453 cnsec = st->st_ctim.tv_nsec; 1326 1454 #elif defined(HAVE_STAT_TV_NSEC2) 1327 1328 1329 1455 ansec = st->st_atimespec.tv_nsec; 1456 mnsec = st->st_mtimespec.tv_nsec; 1457 cnsec = st->st_ctimespec.tv_nsec; 1330 1458 #elif defined(HAVE_STAT_NSEC) 1331 1332 1333 1459 ansec = st->st_atime_nsec; 1460 mnsec = st->st_mtime_nsec; 1461 cnsec = st->st_ctime_nsec; 1334 1462 #else 1335 1336 #endif 1337 1338 1339 1463 ansec = mnsec = cnsec = 0; 1464 #endif 1465 fill_time(v, 7, st->st_atime, ansec); 1466 fill_time(v, 8, st->st_mtime, mnsec); 1467 fill_time(v, 9, st->st_ctime, cnsec); 1340 1468 1341 1469 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE 1342 1343 1470 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, 1471 PyInt_FromLong((long)st->st_blksize)); 1344 1472 #endif 1345 1473 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS 1346 1347 1474 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, 1475 PyInt_FromLong((long)st->st_blocks)); 1348 1476 #endif 1349 1477 #ifdef HAVE_STRUCT_STAT_ST_RDEV 1350 1351 1478 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, 1479 PyInt_FromLong((long)st->st_rdev)); 1352 1480 #endif 1353 1481 #ifdef HAVE_STRUCT_STAT_ST_GEN 1354 1355 1482 PyStructSequence_SET_ITEM(v, ST_GEN_IDX, 1483 PyInt_FromLong((long)st->st_gen)); 1356 1484 #endif 1357 1485 #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME 1358 1359 1360 1361 1486 { 1487 PyObject *val; 1488 unsigned long bsec,bnsec; 1489 bsec = (long)st->st_birthtime; 1362 1490 #ifdef HAVE_STAT_TV_NSEC2 1363 1491 bnsec = st->st_birthtimespec.tv_nsec; 1364 1492 #else 1365 1366 #endif 1367 1368 1369 1370 1371 1372 1373 1374 1493 bnsec = 0; 1494 #endif 1495 if (_stat_float_times) { 1496 val = PyFloat_FromDouble(bsec + 1e-9*bnsec); 1497 } else { 1498 val = PyInt_FromLong((long)bsec); 1499 } 1500 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, 1501 val); 1502 } 1375 1503 #endif 1376 1504 #ifdef HAVE_STRUCT_STAT_ST_FLAGS 1377 1378 1379 #endif 1380 1381 1382 1383 1384 1385 1386 1505 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, 1506 PyInt_FromLong((long)st->st_flags)); 1507 #endif 1508 1509 if (PyErr_Occurred()) { 1510 Py_DECREF(v); 1511 return NULL; 1512 } 1513 1514 return v; 1387 1515 } 1388 1516 … … 1403 1531 IsUNCRootA(char *path, int pathlen) 1404 1532 { 1405 #define ISSLASH ISSLASHA 1406 1407 int i, share; 1408 1409 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1])) 1410 /* minimum UNCRoot is \\x\y */ 1411 return FALSE; 1412 for (i = 2; i < pathlen ; i++) 1413 if (ISSLASH(path[i])) break; 1414 if (i == 2 || i == pathlen) 1415 /* do not allow \\\SHARE or \\SERVER */ 1416 return FALSE; 1417 share = i+1; 1418 for (i = share; i < pathlen; i++) 1419 if (ISSLASH(path[i])) break; 1420 return (i != share && (i == pathlen || i == pathlen-1)); 1421 1422 #undef ISSLASH 1423 } 1424 1425 #ifdef Py_WIN_WIDE_FILENAMES 1533 #define ISSLASH ISSLASHA 1534 1535 int i, share; 1536 1537 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1])) 1538 /* minimum UNCRoot is \\x\y */ 1539 return FALSE; 1540 for (i = 2; i < pathlen ; i++) 1541 if (ISSLASH(path[i])) break; 1542 if (i == 2 || i == pathlen) 1543 /* do not allow \\\SHARE or \\SERVER */ 1544 return FALSE; 1545 share = i+1; 1546 for (i = share; i < pathlen; i++) 1547 if (ISSLASH(path[i])) break; 1548 return (i != share && (i == pathlen || i == pathlen-1)); 1549 1550 #undef ISSLASH 1551 } 1552 1426 1553 static BOOL 1427 1554 IsUNCRootW(Py_UNICODE *path, int pathlen) 1428 1555 { 1429 #define ISSLASH ISSLASHW 1430 1431 int i, share; 1432 1433 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1])) 1434 /* minimum UNCRoot is \\x\y */ 1435 return FALSE; 1436 for (i = 2; i < pathlen ; i++) 1437 if (ISSLASH(path[i])) break; 1438 if (i == 2 || i == pathlen) 1439 /* do not allow \\\SHARE or \\SERVER */ 1440 return FALSE; 1441 share = i+1; 1442 for (i = share; i < pathlen; i++) 1443 if (ISSLASH(path[i])) break; 1444 return (i != share && (i == pathlen || i == pathlen-1)); 1445 1446 #undef ISSLASH 1447 } 1448 #endif /* Py_WIN_WIDE_FILENAMES */ 1556 #define ISSLASH ISSLASHW 1557 1558 int i, share; 1559 1560 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1])) 1561 /* minimum UNCRoot is \\x\y */ 1562 return FALSE; 1563 for (i = 2; i < pathlen ; i++) 1564 if (ISSLASH(path[i])) break; 1565 if (i == 2 || i == pathlen) 1566 /* do not allow \\\SHARE or \\SERVER */ 1567 return FALSE; 1568 share = i+1; 1569 for (i = share; i < pathlen; i++) 1570 if (ISSLASH(path[i])) break; 1571 return (i != share && (i == pathlen || i == pathlen-1)); 1572 1573 #undef ISSLASH 1574 } 1449 1575 #endif /* MS_WINDOWS */ 1450 1576 1451 1577 static PyObject * 1452 1578 posix_do_stat(PyObject *self, PyObject *args, 1453 1579 char *format, 1454 1580 #ifdef __VMS 1455 1581 int (*statfunc)(const char *, STRUCT_STAT *, ...), 1456 1582 #else 1457 int (*statfunc)(const char *, STRUCT_STAT *), 1458 #endif 1459 char *wformat, 1460 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *)) 1461 { 1462 STRUCT_STAT st; 1463 char *path = NULL; /* pass this to stat; do not free() it */ 1464 char *pathfree = NULL; /* this memory must be free'd */ 1465 int res; 1466 PyObject *result; 1467 1468 #ifdef Py_WIN_WIDE_FILENAMES 1469 /* If on wide-character-capable OS see if argument 1470 is Unicode and if so use wide API. */ 1471 if (unicode_file_names()) { 1472 PyUnicodeObject *po; 1473 if (PyArg_ParseTuple(args, wformat, &po)) { 1474 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po); 1475 1476 Py_BEGIN_ALLOW_THREADS 1477 /* PyUnicode_AS_UNICODE result OK without 1478 thread lock as it is a simple dereference. */ 1479 res = wstatfunc(wpath, &st); 1480 Py_END_ALLOW_THREADS 1481 1482 if (res != 0) 1483 return win32_error_unicode("stat", wpath); 1484 return _pystat_fromstructstat(&st); 1485 } 1486 /* Drop the argument parsing error as narrow strings 1487 are also valid. */ 1488 PyErr_Clear(); 1489 } 1490 #endif 1491 1492 if (!PyArg_ParseTuple(args, format, 1493 Py_FileSystemDefaultEncoding, &path)) 1494 return NULL; 1495 pathfree = path; 1496 1497 Py_BEGIN_ALLOW_THREADS 1498 res = (*statfunc)(path, &st); 1499 Py_END_ALLOW_THREADS 1500 1501 if (res != 0) { 1583 int (*statfunc)(const char *, STRUCT_STAT *), 1584 #endif 1585 char *wformat, 1586 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *)) 1587 { 1588 STRUCT_STAT st; 1589 char *path = NULL; /* pass this to stat; do not free() it */ 1590 char *pathfree = NULL; /* this memory must be free'd */ 1591 int res; 1592 PyObject *result; 1593 1502 1594 #ifdef MS_WINDOWS 1503 result = win32_error("stat", pathfree); 1595 PyUnicodeObject *po; 1596 if (PyArg_ParseTuple(args, wformat, &po)) { 1597 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po); 1598 1599 Py_BEGIN_ALLOW_THREADS 1600 /* PyUnicode_AS_UNICODE result OK without 1601 thread lock as it is a simple dereference. */ 1602 res = wstatfunc(wpath, &st); 1603 Py_END_ALLOW_THREADS 1604 1605 if (res != 0) 1606 return win32_error_unicode("stat", wpath); 1607 return _pystat_fromstructstat(&st); 1608 } 1609 /* Drop the argument parsing error as narrow strings 1610 are also valid. */ 1611 PyErr_Clear(); 1612 #endif 1613 1614 if (!PyArg_ParseTuple(args, format, 1615 Py_FileSystemDefaultEncoding, &path)) 1616 return NULL; 1617 pathfree = path; 1618 1619 Py_BEGIN_ALLOW_THREADS 1620 res = (*statfunc)(path, &st); 1621 Py_END_ALLOW_THREADS 1622 1623 if (res != 0) { 1624 #ifdef MS_WINDOWS 1625 result = win32_error("stat", pathfree); 1504 1626 #else 1505 1506 #endif 1507 } 1508 1509 1510 1511 1512 1627 result = posix_error_with_filename(pathfree); 1628 #endif 1629 } 1630 else 1631 result = _pystat_fromstructstat(&st); 1632 1633 PyMem_Free(pathfree); 1634 return result; 1513 1635 } 1514 1636 … … 1526 1648 posix_access(PyObject *self, PyObject *args) 1527 1649 { 1528 char *path; 1529 int mode; 1530 1531 #ifdef Py_WIN_WIDE_FILENAMES 1532 DWORD attr; 1533 if (unicode_file_names()) { 1534 PyUnicodeObject *po; 1535 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) { 1536 Py_BEGIN_ALLOW_THREADS 1537 /* PyUnicode_AS_UNICODE OK without thread lock as 1538 it is a simple dereference. */ 1539 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po)); 1540 Py_END_ALLOW_THREADS 1541 goto finish; 1542 } 1543 /* Drop the argument parsing error as narrow strings 1544 are also valid. */ 1545 PyErr_Clear(); 1546 } 1547 if (!PyArg_ParseTuple(args, "eti:access", 1548 Py_FileSystemDefaultEncoding, &path, &mode)) 1549 return 0; 1550 Py_BEGIN_ALLOW_THREADS 1551 attr = GetFileAttributesA(path); 1552 Py_END_ALLOW_THREADS 1553 PyMem_Free(path); 1650 char *path; 1651 int mode; 1652 1653 #ifdef MS_WINDOWS 1654 DWORD attr; 1655 PyUnicodeObject *po; 1656 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) { 1657 Py_BEGIN_ALLOW_THREADS 1658 /* PyUnicode_AS_UNICODE OK without thread lock as 1659 it is a simple dereference. */ 1660 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po)); 1661 Py_END_ALLOW_THREADS 1662 goto finish; 1663 } 1664 /* Drop the argument parsing error as narrow strings 1665 are also valid. */ 1666 PyErr_Clear(); 1667 if (!PyArg_ParseTuple(args, "eti:access", 1668 Py_FileSystemDefaultEncoding, &path, &mode)) 1669 return NULL; 1670 Py_BEGIN_ALLOW_THREADS 1671 attr = GetFileAttributesA(path); 1672 Py_END_ALLOW_THREADS 1673 PyMem_Free(path); 1554 1674 finish: 1555 1556 1557 1558 1559 1560 1561 return PyBool_FromLong(!(mode & 2) 1562 1563 1564 #else 1565 1566 if (!PyArg_ParseTuple(args, "eti:access", 1567 1568 1569 1570 1571 1572 1573 1574 #endif 1675 if (attr == 0xFFFFFFFF) 1676 /* File does not exist, or cannot read attributes */ 1677 return PyBool_FromLong(0); 1678 /* Access is possible if either write access wasn't requested, or 1679 the file isn't read-only, or if it's a directory, as there are 1680 no read-only directories on Windows. */ 1681 return PyBool_FromLong(!(mode & 2) 1682 || !(attr & FILE_ATTRIBUTE_READONLY) 1683 || (attr & FILE_ATTRIBUTE_DIRECTORY)); 1684 #else /* MS_WINDOWS */ 1685 int res; 1686 if (!PyArg_ParseTuple(args, "eti:access", 1687 Py_FileSystemDefaultEncoding, &path, &mode)) 1688 return NULL; 1689 Py_BEGIN_ALLOW_THREADS 1690 res = access(path, mode); 1691 Py_END_ALLOW_THREADS 1692 PyMem_Free(path); 1693 return PyBool_FromLong(res == 0); 1694 #endif /* MS_WINDOWS */ 1575 1695 } 1576 1696 … … 1596 1716 posix_ttyname(PyObject *self, PyObject *args) 1597 1717 { 1598 1599 1600 1601 1602 1718 int id; 1719 char *ret; 1720 1721 if (!PyArg_ParseTuple(args, "i:ttyname", &id)) 1722 return NULL; 1603 1723 1604 1724 #if defined(__VMS) 1605 1606 1607 1608 1609 1610 1611 1725 /* file descriptor 0 only, the default input device (stdin) */ 1726 if (id == 0) { 1727 ret = ttyname(); 1728 } 1729 else { 1730 ret = NULL; 1731 } 1612 1732 #else 1613 1614 #endif 1615 1616 1617 1733 ret = ttyname(id); 1734 #endif 1735 if (ret == NULL) 1736 return posix_error(); 1737 return PyString_FromString(ret); 1618 1738 } 1619 1739 #endif … … 1627 1747 posix_ctermid(PyObject *self, PyObject *noargs) 1628 1748 { 1629 1630 1749 char *ret; 1750 char buffer[L_ctermid]; 1631 1751 1632 1752 #ifdef USE_CTERMID_R 1633 1753 ret = ctermid_r(buffer); 1634 1754 #else 1635 1636 #endif 1637 1638 1639 1755 ret = ctermid(buffer); 1756 #endif 1757 if (ret == NULL) 1758 return posix_error(); 1759 return PyString_FromString(buffer); 1640 1760 } 1641 1761 #endif … … 1649 1769 { 1650 1770 #ifdef MS_WINDOWS 1651 1771 return win32_1str(args, "chdir", "s:chdir", win32_chdir, "U:chdir", win32_wchdir); 1652 1772 #elif defined(PYOS_OS2) && defined(PYCC_GCC) 1653 1773 return posix_1str(args, "et:chdir", _chdir2); 1654 1774 #elif defined(__VMS) 1655 1775 return posix_1str(args, "et:chdir", (int (*)(const char *))chdir); 1656 1776 #else 1657 1777 return posix_1str(args, "et:chdir", chdir); 1658 1778 #endif 1659 1779 } … … 1668 1788 posix_fchdir(PyObject *self, PyObject *fdobj) 1669 1789 { 1670 1790 return posix_fildes(fdobj, fchdir); 1671 1791 } 1672 1792 #endif /* HAVE_FCHDIR */ … … 1680 1800 posix_chmod(PyObject *self, PyObject *args) 1681 1801 { 1682 char *path = NULL; 1683 int i; 1684 int res; 1685 #ifdef Py_WIN_WIDE_FILENAMES 1686 DWORD attr; 1687 if (unicode_file_names()) { 1688 PyUnicodeObject *po; 1689 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) { 1690 Py_BEGIN_ALLOW_THREADS 1691 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po)); 1692 if (attr != 0xFFFFFFFF) { 1693 if (i & _S_IWRITE) 1694 attr &= ~FILE_ATTRIBUTE_READONLY; 1695 else 1696 attr |= FILE_ATTRIBUTE_READONLY; 1697 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr); 1698 } 1699 else 1700 res = 0; 1701 Py_END_ALLOW_THREADS 1702 if (!res) 1703 return win32_error_unicode("chmod", 1704 PyUnicode_AS_UNICODE(po)); 1705 Py_INCREF(Py_None); 1706 return Py_None; 1707 } 1708 /* Drop the argument parsing error as narrow strings 1709 are also valid. */ 1710 PyErr_Clear(); 1711 } 1712 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding, 1713 &path, &i)) 1714 return NULL; 1715 Py_BEGIN_ALLOW_THREADS 1716 attr = GetFileAttributesA(path); 1717 if (attr != 0xFFFFFFFF) { 1718 if (i & _S_IWRITE) 1719 attr &= ~FILE_ATTRIBUTE_READONLY; 1720 else 1721 attr |= FILE_ATTRIBUTE_READONLY; 1722 res = SetFileAttributesA(path, attr); 1723 } 1724 else 1725 res = 0; 1726 Py_END_ALLOW_THREADS 1727 if (!res) { 1728 win32_error("chmod", path); 1729 PyMem_Free(path); 1730 return NULL; 1731 } 1732 PyMem_Free(path); 1733 Py_INCREF(Py_None); 1734 return Py_None; 1735 #else /* Py_WIN_WIDE_FILENAMES */ 1736 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding, 1737 &path, &i)) 1738 return NULL; 1739 Py_BEGIN_ALLOW_THREADS 1740 res = chmod(path, i); 1741 Py_END_ALLOW_THREADS 1742 if (res < 0) 1743 return posix_error_with_allocated_filename(path); 1744 PyMem_Free(path); 1745 Py_INCREF(Py_None); 1746 return Py_None; 1802 char *path = NULL; 1803 int i; 1804 int res; 1805 #ifdef MS_WINDOWS 1806 DWORD attr; 1807 PyUnicodeObject *po; 1808 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) { 1809 Py_BEGIN_ALLOW_THREADS 1810 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po)); 1811 if (attr != 0xFFFFFFFF) { 1812 if (i & _S_IWRITE) 1813 attr &= ~FILE_ATTRIBUTE_READONLY; 1814 else 1815 attr |= FILE_ATTRIBUTE_READONLY; 1816 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr); 1817 } 1818 else 1819 res = 0; 1820 Py_END_ALLOW_THREADS 1821 if (!res) 1822 return win32_error_unicode("chmod", 1823 PyUnicode_AS_UNICODE(po)); 1824 Py_INCREF(Py_None); 1825 return Py_None; 1826 } 1827 /* Drop the argument parsing error as narrow strings 1828 are also valid. */ 1829 PyErr_Clear(); 1830 1831 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding, 1832 &path, &i)) 1833 return NULL; 1834 Py_BEGIN_ALLOW_THREADS 1835 attr = GetFileAttributesA(path); 1836 if (attr != 0xFFFFFFFF) { 1837 if (i & _S_IWRITE) 1838 attr &= ~FILE_ATTRIBUTE_READONLY; 1839 else 1840 attr |= FILE_ATTRIBUTE_READONLY; 1841 res = SetFileAttributesA(path, attr); 1842 } 1843 else 1844 res = 0; 1845 Py_END_ALLOW_THREADS 1846 if (!res) { 1847 win32_error("chmod", path); 1848 PyMem_Free(path); 1849 return NULL; 1850 } 1851 PyMem_Free(path); 1852 Py_INCREF(Py_None); 1853 return Py_None; 1854 #else /* MS_WINDOWS */ 1855 if (!PyArg_ParseTuple(args, "eti:chmod", Py_FileSystemDefaultEncoding, 1856 &path, &i)) 1857 return NULL; 1858 Py_BEGIN_ALLOW_THREADS 1859 res = chmod(path, i); 1860 Py_END_ALLOW_THREADS 1861 if (res < 0) 1862 return posix_error_with_allocated_filename(path); 1863 PyMem_Free(path); 1864 Py_INCREF(Py_None); 1865 return Py_None; 1747 1866 #endif 1748 1867 } … … 1757 1876 posix_fchmod(PyObject *self, PyObject *args) 1758 1877 { 1759 1760 1761 1762 1763 1764 1765 1766 1767 1878 int fd, mode, res; 1879 if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode)) 1880 return NULL; 1881 Py_BEGIN_ALLOW_THREADS 1882 res = fchmod(fd, mode); 1883 Py_END_ALLOW_THREADS 1884 if (res < 0) 1885 return posix_error(); 1886 Py_RETURN_NONE; 1768 1887 } 1769 1888 #endif /* HAVE_FCHMOD */ … … 1778 1897 posix_lchmod(PyObject *self, PyObject *args) 1779 1898 { 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1899 char *path = NULL; 1900 int i; 1901 int res; 1902 if (!PyArg_ParseTuple(args, "eti:lchmod", Py_FileSystemDefaultEncoding, 1903 &path, &i)) 1904 return NULL; 1905 Py_BEGIN_ALLOW_THREADS 1906 res = lchmod(path, i); 1907 Py_END_ALLOW_THREADS 1908 if (res < 0) 1909 return posix_error_with_allocated_filename(path); 1910 PyMem_Free(path); 1911 Py_RETURN_NONE; 1793 1912 } 1794 1913 #endif /* HAVE_LCHMOD */ … … 1803 1922 posix_chflags(PyObject *self, PyObject *args) 1804 1923 { 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1924 char *path; 1925 unsigned long flags; 1926 int res; 1927 if (!PyArg_ParseTuple(args, "etk:chflags", 1928 Py_FileSystemDefaultEncoding, &path, &flags)) 1929 return NULL; 1930 Py_BEGIN_ALLOW_THREADS 1931 res = chflags(path, flags); 1932 Py_END_ALLOW_THREADS 1933 if (res < 0) 1934 return posix_error_with_allocated_filename(path); 1935 PyMem_Free(path); 1936 Py_INCREF(Py_None); 1937 return Py_None; 1819 1938 } 1820 1939 #endif /* HAVE_CHFLAGS */ … … 1829 1948 posix_lchflags(PyObject *self, PyObject *args) 1830 1949 { 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1950 char *path; 1951 unsigned long flags; 1952 int res; 1953 if (!PyArg_ParseTuple(args, "etk:lchflags", 1954 Py_FileSystemDefaultEncoding, &path, &flags)) 1955 return NULL; 1956 Py_BEGIN_ALLOW_THREADS 1957 res = lchflags(path, flags); 1958 Py_END_ALLOW_THREADS 1959 if (res < 0) 1960 return posix_error_with_allocated_filename(path); 1961 PyMem_Free(path); 1962 Py_INCREF(Py_None); 1963 return Py_None; 1845 1964 } 1846 1965 #endif /* HAVE_LCHFLAGS */ … … 1854 1973 posix_chroot(PyObject *self, PyObject *args) 1855 1974 { 1856 1975 return posix_1str(args, "et:chroot", chroot); 1857 1976 } 1858 1977 #endif … … 1866 1985 posix_fsync(PyObject *self, PyObject *fdobj) 1867 1986 { 1868 1987 return posix_fildes(fdobj, fsync); 1869 1988 } 1870 1989 #endif /* HAVE_FSYNC */ … … 1884 2003 posix_fdatasync(PyObject *self, PyObject *fdobj) 1885 2004 { 1886 2005 return posix_fildes(fdobj, fdatasync); 1887 2006 } 1888 2007 #endif /* HAVE_FDATASYNC */ … … 1897 2016 posix_chown(PyObject *self, PyObject *args) 1898 2017 { 1899 char *path = NULL; 1900 long uid, gid; 1901 int res; 1902 if (!PyArg_ParseTuple(args, "etll:chown", 1903 Py_FileSystemDefaultEncoding, &path, 1904 &uid, &gid)) 1905 return NULL; 1906 Py_BEGIN_ALLOW_THREADS 1907 res = chown(path, (uid_t) uid, (gid_t) gid); 1908 Py_END_ALLOW_THREADS 1909 if (res < 0) 1910 return posix_error_with_allocated_filename(path); 1911 PyMem_Free(path); 1912 Py_INCREF(Py_None); 1913 return Py_None; 2018 char *path = NULL; 2019 uid_t uid; 2020 gid_t gid; 2021 int res; 2022 if (!PyArg_ParseTuple(args, "etO&O&:chown", 2023 Py_FileSystemDefaultEncoding, &path, 2024 _Py_Uid_Converter, &uid, 2025 _Py_Gid_Converter, &gid)) 2026 return NULL; 2027 Py_BEGIN_ALLOW_THREADS 2028 res = chown(path, uid, gid); 2029 Py_END_ALLOW_THREADS 2030 if (res < 0) 2031 return posix_error_with_allocated_filename(path); 2032 PyMem_Free(path); 2033 Py_INCREF(Py_None); 2034 return Py_None; 1914 2035 } 1915 2036 #endif /* HAVE_CHOWN */ … … 1924 2045 posix_fchown(PyObject *self, PyObject *args) 1925 2046 { 1926 int fd; 1927 long uid, gid; 1928 int res; 1929 if (!PyArg_ParseTuple(args, "ill:chown", &fd, &uid, &gid)) 1930 return NULL; 1931 Py_BEGIN_ALLOW_THREADS 1932 res = fchown(fd, (uid_t) uid, (gid_t) gid); 1933 Py_END_ALLOW_THREADS 1934 if (res < 0) 1935 return posix_error(); 1936 Py_RETURN_NONE; 2047 int fd; 2048 uid_t uid; 2049 gid_t gid; 2050 int res; 2051 if (!PyArg_ParseTuple(args, "iO&O&:fchown", &fd, 2052 _Py_Uid_Converter, &uid, 2053 _Py_Gid_Converter, &gid)) 2054 return NULL; 2055 Py_BEGIN_ALLOW_THREADS 2056 res = fchown(fd, uid, gid); 2057 Py_END_ALLOW_THREADS 2058 if (res < 0) 2059 return posix_error(); 2060 Py_RETURN_NONE; 1937 2061 } 1938 2062 #endif /* HAVE_FCHOWN */ … … 1947 2071 posix_lchown(PyObject *self, PyObject *args) 1948 2072 { 1949 char *path = NULL; 1950 long uid, gid; 1951 int res; 1952 if (!PyArg_ParseTuple(args, "etll:lchown", 1953 Py_FileSystemDefaultEncoding, &path, 1954 &uid, &gid)) 1955 return NULL; 1956 Py_BEGIN_ALLOW_THREADS 1957 res = lchown(path, (uid_t) uid, (gid_t) gid); 1958 Py_END_ALLOW_THREADS 1959 if (res < 0) 1960 return posix_error_with_allocated_filename(path); 1961 PyMem_Free(path); 1962 Py_INCREF(Py_None); 1963 return Py_None; 2073 char *path = NULL; 2074 uid_t uid; 2075 gid_t gid; 2076 int res; 2077 if (!PyArg_ParseTuple(args, "etO&O&:lchown", 2078 Py_FileSystemDefaultEncoding, &path, 2079 _Py_Uid_Converter, &uid, 2080 _Py_Gid_Converter, &gid)) 2081 return NULL; 2082 Py_BEGIN_ALLOW_THREADS 2083 res = lchown(path, uid, gid); 2084 Py_END_ALLOW_THREADS 2085 if (res < 0) 2086 return posix_error_with_allocated_filename(path); 2087 PyMem_Free(path); 2088 Py_INCREF(Py_None); 2089 return Py_None; 1964 2090 } 1965 2091 #endif /* HAVE_LCHOWN */ … … 1971 2097 Return a string representing the current working directory."); 1972 2098 2099 #if (defined(__sun) && defined(__SVR4)) || \ 2100 defined(__OpenBSD__) || \ 2101 defined(__NetBSD__) 2102 /* Issue 9185: getcwd() returns NULL/ERANGE indefinitely. */ 1973 2103 static PyObject * 1974 2104 posix_getcwd(PyObject *self, PyObject *noargs) 1975 2105 { 1976 int bufsize_incr = 1024; 1977 int bufsize = 0; 1978 char *tmpbuf = NULL; 1979 char *res = NULL; 1980 PyObject *dynamic_return; 1981 1982 Py_BEGIN_ALLOW_THREADS 1983 do { 1984 bufsize = bufsize + bufsize_incr; 1985 tmpbuf = malloc(bufsize); 1986 if (tmpbuf == NULL) { 1987 break; 1988 } 2106 char buf[PATH_MAX+2]; 2107 char *res; 2108 2109 Py_BEGIN_ALLOW_THREADS 2110 res = getcwd(buf, sizeof buf); 2111 Py_END_ALLOW_THREADS 2112 2113 if (res == NULL) 2114 return posix_error(); 2115 2116 return PyString_FromString(buf); 2117 } 2118 #else 2119 static PyObject * 2120 posix_getcwd(PyObject *self, PyObject *noargs) 2121 { 2122 int bufsize_incr = 1024; 2123 int bufsize = 0; 2124 char *tmpbuf = NULL; 2125 char *res = NULL; 2126 PyObject *dynamic_return; 2127 2128 Py_BEGIN_ALLOW_THREADS 2129 do { 2130 bufsize = bufsize + bufsize_incr; 2131 tmpbuf = malloc(bufsize); 2132 if (tmpbuf == NULL) { 2133 break; 2134 } 1989 2135 #if defined(PYOS_OS2) && defined(PYCC_GCC) 1990 2136 res = _getcwd2(tmpbuf, bufsize); 1991 2137 #else 1992 res = getcwd(tmpbuf, bufsize); 1993 #endif 1994 1995 if (res == NULL) { 1996 free(tmpbuf); 1997 } 1998 } while ((res == NULL) && (errno == ERANGE)); 1999 Py_END_ALLOW_THREADS 2000 2001 if (res == NULL) 2002 return posix_error(); 2003 2004 dynamic_return = PyString_FromString(tmpbuf); 2005 free(tmpbuf); 2006 2007 return dynamic_return; 2008 } 2138 res = getcwd(tmpbuf, bufsize); 2139 #endif 2140 2141 if (res == NULL) { 2142 free(tmpbuf); 2143 } 2144 } while ((res == NULL) && (errno == ERANGE)); 2145 Py_END_ALLOW_THREADS 2146 2147 if (res == NULL) 2148 return posix_error(); 2149 2150 dynamic_return = PyString_FromString(tmpbuf); 2151 free(tmpbuf); 2152 2153 return dynamic_return; 2154 } 2155 #endif /* getcwd() NULL/ERANGE workaround. */ 2009 2156 2010 2157 #ifdef Py_USING_UNICODE … … 2016 2163 posix_getcwdu(PyObject *self, PyObject *noargs) 2017 2164 { 2018 char buf[1026]; 2019 char *res; 2020 2021 #ifdef Py_WIN_WIDE_FILENAMES 2022 DWORD len; 2023 if (unicode_file_names()) { 2024 wchar_t wbuf[1026]; 2025 wchar_t *wbuf2 = wbuf; 2026 PyObject *resobj; 2027 Py_BEGIN_ALLOW_THREADS 2028 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf); 2029 /* If the buffer is large enough, len does not include the 2030 terminating \0. If the buffer is too small, len includes 2031 the space needed for the terminator. */ 2032 if (len >= sizeof wbuf/ sizeof wbuf[0]) { 2033 wbuf2 = malloc(len * sizeof(wchar_t)); 2034 if (wbuf2) 2035 len = GetCurrentDirectoryW(len, wbuf2); 2036 } 2037 Py_END_ALLOW_THREADS 2038 if (!wbuf2) { 2039 PyErr_NoMemory(); 2040 return NULL; 2041 } 2042 if (!len) { 2043 if (wbuf2 != wbuf) free(wbuf2); 2044 return win32_error("getcwdu", NULL); 2045 } 2046 resobj = PyUnicode_FromWideChar(wbuf2, len); 2047 if (wbuf2 != wbuf) free(wbuf2); 2048 return resobj; 2049 } 2050 #endif 2051 2052 Py_BEGIN_ALLOW_THREADS 2165 char buf[1026]; 2166 char *res; 2167 2168 #ifdef MS_WINDOWS 2169 DWORD len; 2170 wchar_t wbuf[1026]; 2171 wchar_t *wbuf2 = wbuf; 2172 PyObject *resobj; 2173 Py_BEGIN_ALLOW_THREADS 2174 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf); 2175 /* If the buffer is large enough, len does not include the 2176 terminating \0. If the buffer is too small, len includes 2177 the space needed for the terminator. */ 2178 if (len >= sizeof wbuf/ sizeof wbuf[0]) { 2179 wbuf2 = malloc(len * sizeof(wchar_t)); 2180 if (wbuf2) 2181 len = GetCurrentDirectoryW(len, wbuf2); 2182 } 2183 Py_END_ALLOW_THREADS 2184 if (!wbuf2) { 2185 PyErr_NoMemory(); 2186 return NULL; 2187 } 2188 if (!len) { 2189 if (wbuf2 != wbuf) free(wbuf2); 2190 return win32_error("getcwdu", NULL); 2191 } 2192 resobj = PyUnicode_FromWideChar(wbuf2, len); 2193 if (wbuf2 != wbuf) free(wbuf2); 2194 return resobj; 2195 #endif /* MS_WINDOWS */ 2196 2197 Py_BEGIN_ALLOW_THREADS 2053 2198 #if defined(PYOS_OS2) && defined(PYCC_GCC) 2054 2199 res = _getcwd2(buf, sizeof buf); 2055 2200 #else 2056 2057 #endif 2058 2059 2060 2061 2062 } 2063 #endif 2064 #endif 2201 res = getcwd(buf, sizeof buf); 2202 #endif 2203 Py_END_ALLOW_THREADS 2204 if (res == NULL) 2205 return posix_error(); 2206 return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"strict"); 2207 } 2208 #endif /* Py_USING_UNICODE */ 2209 #endif /* HAVE_GETCWD */ 2065 2210 2066 2211 … … 2073 2218 posix_link(PyObject *self, PyObject *args) 2074 2219 { 2075 2220 return posix_2str(args, "etet:link", link); 2076 2221 } 2077 2222 #endif /* HAVE_LINK */ … … 2082 2227 Return a list containing the names of the entries in the directory.\n\ 2083 2228 \n\ 2084 2229 path: path of directory to list\n\ 2085 2230 \n\ 2086 2231 The list is in arbitrary order. It does not include the special\n\ … … 2090 2235 posix_listdir(PyObject *self, PyObject *args) 2091 2236 { 2092 2093 2237 /* XXX Should redo this putting the (now four) versions of opendir 2238 in separate files instead of having them all here... */ 2094 2239 #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) 2095 2240 2096 PyObject *d, *v; 2097 HANDLE hFindFile; 2098 BOOL result; 2099 WIN32_FIND_DATA FileData; 2100 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */ 2101 char *bufptr = namebuf; 2102 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */ 2103 2104 #ifdef Py_WIN_WIDE_FILENAMES 2105 /* If on wide-character-capable OS see if argument 2106 is Unicode and if so use wide API. */ 2107 if (unicode_file_names()) { 2108 PyObject *po; 2109 if (PyArg_ParseTuple(args, "U:listdir", &po)) { 2110 WIN32_FIND_DATAW wFileData; 2111 Py_UNICODE *wnamebuf; 2112 Py_UNICODE wch; 2113 /* Overallocate for \\*.*\0 */ 2114 len = PyUnicode_GET_SIZE(po); 2115 wnamebuf = malloc((len + 5) * sizeof(wchar_t)); 2116 if (!wnamebuf) { 2117 PyErr_NoMemory(); 2118 return NULL; 2119 } 2120 wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po)); 2121 wch = len > 0 ? wnamebuf[len-1] : '\0'; 2122 if (wch != L'/' && wch != L'\\' && wch != L':') 2123 wnamebuf[len++] = L'\\'; 2124 wcscpy(wnamebuf + len, L"*.*"); 2125 if ((d = PyList_New(0)) == NULL) { 2126 free(wnamebuf); 2127 return NULL; 2128 } 2129 hFindFile = FindFirstFileW(wnamebuf, &wFileData); 2130 if (hFindFile == INVALID_HANDLE_VALUE) { 2131 int error = GetLastError(); 2132 if (error == ERROR_FILE_NOT_FOUND) { 2133 free(wnamebuf); 2134 return d; 2135 } 2136 Py_DECREF(d); 2137 win32_error_unicode("FindFirstFileW", wnamebuf); 2138 free(wnamebuf); 2139 return NULL; 2140 } 2141 do { 2142 /* Skip over . and .. */ 2143 if (wcscmp(wFileData.cFileName, L".") != 0 && 2144 wcscmp(wFileData.cFileName, L"..") != 0) { 2145 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName)); 2146 if (v == NULL) { 2147 Py_DECREF(d); 2148 d = NULL; 2149 break; 2150 } 2151 if (PyList_Append(d, v) != 0) { 2152 Py_DECREF(v); 2153 Py_DECREF(d); 2154 d = NULL; 2155 break; 2156 } 2157 Py_DECREF(v); 2158 } 2159 Py_BEGIN_ALLOW_THREADS 2160 result = FindNextFileW(hFindFile, &wFileData); 2161 Py_END_ALLOW_THREADS 2162 /* FindNextFile sets error to ERROR_NO_MORE_FILES if 2163 it got to the end of the directory. */ 2164 if (!result && GetLastError() != ERROR_NO_MORE_FILES) { 2165 Py_DECREF(d); 2166 win32_error_unicode("FindNextFileW", wnamebuf); 2167 FindClose(hFindFile); 2168 free(wnamebuf); 2169 return NULL; 2170 } 2171 } while (result == TRUE); 2172 2173 if (FindClose(hFindFile) == FALSE) { 2174 Py_DECREF(d); 2175 win32_error_unicode("FindClose", wnamebuf); 2176 free(wnamebuf); 2177 return NULL; 2178 } 2179 free(wnamebuf); 2180 return d; 2181 } 2182 /* Drop the argument parsing error as narrow strings 2183 are also valid. */ 2184 PyErr_Clear(); 2185 } 2186 #endif 2187 2188 if (!PyArg_ParseTuple(args, "et#:listdir", 2189 Py_FileSystemDefaultEncoding, &bufptr, &len)) 2190 return NULL; 2191 if (len > 0) { 2192 char ch = namebuf[len-1]; 2193 if (ch != SEP && ch != ALTSEP && ch != ':') 2194 namebuf[len++] = '/'; 2195 } 2196 strcpy(namebuf + len, "*.*"); 2197 2198 if ((d = PyList_New(0)) == NULL) 2199 return NULL; 2200 2201 hFindFile = FindFirstFile(namebuf, &FileData); 2202 if (hFindFile == INVALID_HANDLE_VALUE) { 2203 int error = GetLastError(); 2204 if (error == ERROR_FILE_NOT_FOUND) 2205 return d; 2206 Py_DECREF(d); 2207 return win32_error("FindFirstFile", namebuf); 2208 } 2209 do { 2210 /* Skip over . and .. */ 2211 if (strcmp(FileData.cFileName, ".") != 0 && 2212 strcmp(FileData.cFileName, "..") != 0) { 2213 v = PyString_FromString(FileData.cFileName); 2214 if (v == NULL) { 2215 Py_DECREF(d); 2216 d = NULL; 2217 break; 2218 } 2219 if (PyList_Append(d, v) != 0) { 2220 Py_DECREF(v); 2221 Py_DECREF(d); 2222 d = NULL; 2223 break; 2224 } 2225 Py_DECREF(v); 2226 } 2227 Py_BEGIN_ALLOW_THREADS 2228 result = FindNextFile(hFindFile, &FileData); 2229 Py_END_ALLOW_THREADS 2230 /* FindNextFile sets error to ERROR_NO_MORE_FILES if 2231 it got to the end of the directory. */ 2232 if (!result && GetLastError() != ERROR_NO_MORE_FILES) { 2233 Py_DECREF(d); 2234 win32_error("FindNextFile", namebuf); 2235 FindClose(hFindFile); 2236 return NULL; 2237 } 2238 } while (result == TRUE); 2239 2240 if (FindClose(hFindFile) == FALSE) { 2241 Py_DECREF(d); 2242 return win32_error("FindClose", namebuf); 2243 } 2244 2245 return d; 2241 PyObject *d, *v; 2242 HANDLE hFindFile; 2243 BOOL result; 2244 WIN32_FIND_DATA FileData; 2245 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */ 2246 char *bufptr = namebuf; 2247 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */ 2248 2249 PyObject *po; 2250 if (PyArg_ParseTuple(args, "U:listdir", &po)) { 2251 WIN32_FIND_DATAW wFileData; 2252 Py_UNICODE *wnamebuf; 2253 /* Overallocate for \\*.*\0 */ 2254 len = PyUnicode_GET_SIZE(po); 2255 wnamebuf = malloc((len + 5) * sizeof(wchar_t)); 2256 if (!wnamebuf) { 2257 PyErr_NoMemory(); 2258 return NULL; 2259 } 2260 wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po)); 2261 if (len > 0) { 2262 Py_UNICODE wch = wnamebuf[len-1]; 2263 if (wch != L'/' && wch != L'\\' && wch != L':') 2264 wnamebuf[len++] = L'\\'; 2265 wcscpy(wnamebuf + len, L"*.*"); 2266 } 2267 if ((d = PyList_New(0)) == NULL) { 2268 free(wnamebuf); 2269 return NULL; 2270 } 2271 Py_BEGIN_ALLOW_THREADS 2272 hFindFile = FindFirstFileW(wnamebuf, &wFileData); 2273 Py_END_ALLOW_THREADS 2274 if (hFindFile == INVALID_HANDLE_VALUE) { 2275 int error = GetLastError(); 2276 if (error == ERROR_FILE_NOT_FOUND) { 2277 free(wnamebuf); 2278 return d; 2279 } 2280 Py_DECREF(d); 2281 win32_error_unicode("FindFirstFileW", wnamebuf); 2282 free(wnamebuf); 2283 return NULL; 2284 } 2285 do { 2286 /* Skip over . and .. */ 2287 if (wcscmp(wFileData.cFileName, L".") != 0 && 2288 wcscmp(wFileData.cFileName, L"..") != 0) { 2289 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName)); 2290 if (v == NULL) { 2291 Py_DECREF(d); 2292 d = NULL; 2293 break; 2294 } 2295 if (PyList_Append(d, v) != 0) { 2296 Py_DECREF(v); 2297 Py_DECREF(d); 2298 d = NULL; 2299 break; 2300 } 2301 Py_DECREF(v); 2302 } 2303 Py_BEGIN_ALLOW_THREADS 2304 result = FindNextFileW(hFindFile, &wFileData); 2305 Py_END_ALLOW_THREADS 2306 /* FindNextFile sets error to ERROR_NO_MORE_FILES if 2307 it got to the end of the directory. */ 2308 if (!result && GetLastError() != ERROR_NO_MORE_FILES) { 2309 Py_DECREF(d); 2310 win32_error_unicode("FindNextFileW", wnamebuf); 2311 FindClose(hFindFile); 2312 free(wnamebuf); 2313 return NULL; 2314 } 2315 } while (result == TRUE); 2316 2317 if (FindClose(hFindFile) == FALSE) { 2318 Py_DECREF(d); 2319 win32_error_unicode("FindClose", wnamebuf); 2320 free(wnamebuf); 2321 return NULL; 2322 } 2323 free(wnamebuf); 2324 return d; 2325 } 2326 /* Drop the argument parsing error as narrow strings 2327 are also valid. */ 2328 PyErr_Clear(); 2329 2330 if (!PyArg_ParseTuple(args, "et#:listdir", 2331 Py_FileSystemDefaultEncoding, &bufptr, &len)) 2332 return NULL; 2333 if (len > 0) { 2334 char ch = namebuf[len-1]; 2335 if (ch != SEP && ch != ALTSEP && ch != ':') 2336 namebuf[len++] = '/'; 2337 strcpy(namebuf + len, "*.*"); 2338 } 2339 2340 if ((d = PyList_New(0)) == NULL) 2341 return NULL; 2342 2343 Py_BEGIN_ALLOW_THREADS 2344 hFindFile = FindFirstFile(namebuf, &FileData); 2345 Py_END_ALLOW_THREADS 2346 if (hFindFile == INVALID_HANDLE_VALUE) { 2347 int error = GetLastError(); 2348 if (error == ERROR_FILE_NOT_FOUND) 2349 return d; 2350 Py_DECREF(d); 2351 return win32_error("FindFirstFile", namebuf); 2352 } 2353 do { 2354 /* Skip over . and .. */ 2355 if (strcmp(FileData.cFileName, ".") != 0 && 2356 strcmp(FileData.cFileName, "..") != 0) { 2357 v = PyString_FromString(FileData.cFileName); 2358 if (v == NULL) { 2359 Py_DECREF(d); 2360 d = NULL; 2361 break; 2362 } 2363 if (PyList_Append(d, v) != 0) { 2364 Py_DECREF(v); 2365 Py_DECREF(d); 2366 d = NULL; 2367 break; 2368 } 2369 Py_DECREF(v); 2370 } 2371 Py_BEGIN_ALLOW_THREADS 2372 result = FindNextFile(hFindFile, &FileData); 2373 Py_END_ALLOW_THREADS 2374 /* FindNextFile sets error to ERROR_NO_MORE_FILES if 2375 it got to the end of the directory. */ 2376 if (!result && GetLastError() != ERROR_NO_MORE_FILES) { 2377 Py_DECREF(d); 2378 win32_error("FindNextFile", namebuf); 2379 FindClose(hFindFile); 2380 return NULL; 2381 } 2382 } while (result == TRUE); 2383 2384 if (FindClose(hFindFile) == FALSE) { 2385 Py_DECREF(d); 2386 return win32_error("FindClose", namebuf); 2387 } 2388 2389 return d; 2246 2390 2247 2391 #elif defined(PYOS_OS2) … … 2262 2406 return NULL; 2263 2407 if (len >= MAX_PATH) { 2264 2408 PyErr_SetString(PyExc_ValueError, "path too long"); 2265 2409 return NULL; 2266 2410 } … … 2273 2417 strcpy(namebuf + len, "*.*"); 2274 2418 2275 2419 if ((d = PyList_New(0)) == NULL) 2276 2420 return NULL; 2277 2421 … … 2318 2462 #else 2319 2463 2320 char *name = NULL; 2321 PyObject *d, *v; 2322 DIR *dirp; 2323 struct dirent *ep; 2324 int arg_is_unicode = 1; 2325 2326 errno = 0; 2327 if (!PyArg_ParseTuple(args, "U:listdir", &v)) { 2328 arg_is_unicode = 0; 2329 PyErr_Clear(); 2330 } 2331 if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name)) 2332 return NULL; 2333 if ((dirp = opendir(name)) == NULL) { 2334 return posix_error_with_allocated_filename(name); 2335 } 2336 if ((d = PyList_New(0)) == NULL) { 2337 closedir(dirp); 2338 PyMem_Free(name); 2339 return NULL; 2340 } 2341 for (;;) { 2342 errno = 0; 2343 Py_BEGIN_ALLOW_THREADS 2344 ep = readdir(dirp); 2345 Py_END_ALLOW_THREADS 2346 if (ep == NULL) { 2347 if (errno == 0) { 2348 break; 2349 } else { 2350 closedir(dirp); 2351 Py_DECREF(d); 2352 return posix_error_with_allocated_filename(name); 2353 } 2354 } 2355 if (ep->d_name[0] == '.' && 2356 (NAMLEN(ep) == 1 || 2357 (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) 2358 continue; 2359 v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep)); 2360 if (v == NULL) { 2361 Py_DECREF(d); 2362 d = NULL; 2363 break; 2364 } 2464 char *name = NULL; 2465 PyObject *d, *v; 2466 DIR *dirp; 2467 struct dirent *ep; 2468 int arg_is_unicode = 1; 2469 2470 errno = 0; 2471 if (!PyArg_ParseTuple(args, "U:listdir", &v)) { 2472 arg_is_unicode = 0; 2473 PyErr_Clear(); 2474 } 2475 if (!PyArg_ParseTuple(args, "et:listdir", Py_FileSystemDefaultEncoding, &name)) 2476 return NULL; 2477 Py_BEGIN_ALLOW_THREADS 2478 dirp = opendir(name); 2479 Py_END_ALLOW_THREADS 2480 if (dirp == NULL) { 2481 return posix_error_with_allocated_filename(name); 2482 } 2483 if ((d = PyList_New(0)) == NULL) { 2484 Py_BEGIN_ALLOW_THREADS 2485 closedir(dirp); 2486 Py_END_ALLOW_THREADS 2487 PyMem_Free(name); 2488 return NULL; 2489 } 2490 for (;;) { 2491 errno = 0; 2492 Py_BEGIN_ALLOW_THREADS 2493 ep = readdir(dirp); 2494 Py_END_ALLOW_THREADS 2495 if (ep == NULL) { 2496 if (errno == 0) { 2497 break; 2498 } else { 2499 Py_BEGIN_ALLOW_THREADS 2500 closedir(dirp); 2501 Py_END_ALLOW_THREADS 2502 Py_DECREF(d); 2503 return posix_error_with_allocated_filename(name); 2504 } 2505 } 2506 if (ep->d_name[0] == '.' && 2507 (NAMLEN(ep) == 1 || 2508 (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) 2509 continue; 2510 v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep)); 2511 if (v == NULL) { 2512 Py_DECREF(d); 2513 d = NULL; 2514 break; 2515 } 2365 2516 #ifdef Py_USING_UNICODE 2366 if (arg_is_unicode) { 2367 PyObject *w; 2368 2369 w = PyUnicode_FromEncodedObject(v, 2370 Py_FileSystemDefaultEncoding, 2371 "strict"); 2372 if (w != NULL) { 2373 Py_DECREF(v); 2374 v = w; 2375 } 2376 else { 2377 /* fall back to the original byte string, as 2378 discussed in patch #683592 */ 2379 PyErr_Clear(); 2380 } 2381 } 2382 #endif 2383 if (PyList_Append(d, v) != 0) { 2384 Py_DECREF(v); 2385 Py_DECREF(d); 2386 d = NULL; 2387 break; 2388 } 2389 Py_DECREF(v); 2390 } 2391 closedir(dirp); 2392 PyMem_Free(name); 2393 2394 return d; 2517 if (arg_is_unicode) { 2518 PyObject *w; 2519 2520 w = PyUnicode_FromEncodedObject(v, 2521 Py_FileSystemDefaultEncoding, 2522 "strict"); 2523 if (w != NULL) { 2524 Py_DECREF(v); 2525 v = w; 2526 } 2527 else { 2528 /* fall back to the original byte string, as 2529 discussed in patch #683592 */ 2530 PyErr_Clear(); 2531 } 2532 } 2533 #endif 2534 if (PyList_Append(d, v) != 0) { 2535 Py_DECREF(v); 2536 Py_DECREF(d); 2537 d = NULL; 2538 break; 2539 } 2540 Py_DECREF(v); 2541 } 2542 Py_BEGIN_ALLOW_THREADS 2543 closedir(dirp); 2544 Py_END_ALLOW_THREADS 2545 PyMem_Free(name); 2546 2547 return d; 2395 2548 2396 2549 #endif /* which OS */ … … 2402 2555 posix__getfullpathname(PyObject *self, PyObject *args) 2403 2556 { 2404 /* assume encoded strings won't more than double no of chars */ 2405 char inbuf[MAX_PATH*2]; 2406 char *inbufp = inbuf; 2407 Py_ssize_t insize = sizeof(inbuf); 2408 char outbuf[MAX_PATH*2]; 2409 char *temp; 2410 #ifdef Py_WIN_WIDE_FILENAMES 2411 if (unicode_file_names()) { 2412 PyUnicodeObject *po; 2413 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) { 2414 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po); 2415 Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf; 2416 Py_UNICODE *wtemp; 2417 DWORD result; 2418 PyObject *v; 2419 result = GetFullPathNameW(wpath, 2420 sizeof(woutbuf)/sizeof(woutbuf[0]), 2421 woutbuf, &wtemp); 2422 if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) { 2423 woutbufp = malloc(result * sizeof(Py_UNICODE)); 2424 if (!woutbufp) 2425 return PyErr_NoMemory(); 2426 result = GetFullPathNameW(wpath, result, woutbufp, &wtemp); 2427 } 2428 if (result) 2429 v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp)); 2430 else 2431 v = win32_error_unicode("GetFullPathNameW", wpath); 2432 if (woutbufp != woutbuf) 2433 free(woutbufp); 2434 return v; 2435 } 2436 /* Drop the argument parsing error as narrow strings 2437 are also valid. */ 2438 PyErr_Clear(); 2439 } 2440 #endif 2441 if (!PyArg_ParseTuple (args, "et#:_getfullpathname", 2442 Py_FileSystemDefaultEncoding, &inbufp, 2443 &insize)) 2444 return NULL; 2445 if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]), 2446 outbuf, &temp)) 2447 return win32_error("GetFullPathName", inbuf); 2448 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) { 2449 return PyUnicode_Decode(outbuf, strlen(outbuf), 2450 Py_FileSystemDefaultEncoding, NULL); 2451 } 2452 return PyString_FromString(outbuf); 2557 /* assume encoded strings won't more than double no of chars */ 2558 char inbuf[MAX_PATH*2]; 2559 char *inbufp = inbuf; 2560 Py_ssize_t insize = sizeof(inbuf); 2561 char outbuf[MAX_PATH*2]; 2562 char *temp; 2563 2564 PyUnicodeObject *po; 2565 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) { 2566 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po); 2567 Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf; 2568 Py_UNICODE *wtemp; 2569 DWORD result; 2570 PyObject *v; 2571 result = GetFullPathNameW(wpath, 2572 sizeof(woutbuf)/sizeof(woutbuf[0]), 2573 woutbuf, &wtemp); 2574 if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) { 2575 woutbufp = malloc(result * sizeof(Py_UNICODE)); 2576 if (!woutbufp) 2577 return PyErr_NoMemory(); 2578 result = GetFullPathNameW(wpath, result, woutbufp, &wtemp); 2579 } 2580 if (result) 2581 v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp)); 2582 else 2583 v = win32_error_unicode("GetFullPathNameW", wpath); 2584 if (woutbufp != woutbuf) 2585 free(woutbufp); 2586 return v; 2587 } 2588 /* Drop the argument parsing error as narrow strings 2589 are also valid. */ 2590 PyErr_Clear(); 2591 2592 if (!PyArg_ParseTuple (args, "et#:_getfullpathname", 2593 Py_FileSystemDefaultEncoding, &inbufp, 2594 &insize)) 2595 return NULL; 2596 if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]), 2597 outbuf, &temp)) 2598 return win32_error("GetFullPathName", inbuf); 2599 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) { 2600 return PyUnicode_Decode(outbuf, strlen(outbuf), 2601 Py_FileSystemDefaultEncoding, NULL); 2602 } 2603 return PyString_FromString(outbuf); 2453 2604 } /* end of posix__getfullpathname */ 2454 2605 #endif /* MS_WINDOWS */ … … 2461 2612 posix_mkdir(PyObject *self, PyObject *args) 2462 2613 { 2463 int res; 2464 char *path = NULL; 2465 int mode = 0777; 2466 2467 #ifdef Py_WIN_WIDE_FILENAMES 2468 if (unicode_file_names()) { 2469 PyUnicodeObject *po; 2470 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) { 2471 Py_BEGIN_ALLOW_THREADS 2472 /* PyUnicode_AS_UNICODE OK without thread lock as 2473 it is a simple dereference. */ 2474 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL); 2475 Py_END_ALLOW_THREADS 2476 if (!res) 2477 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po)); 2478 Py_INCREF(Py_None); 2479 return Py_None; 2480 } 2481 /* Drop the argument parsing error as narrow strings 2482 are also valid. */ 2483 PyErr_Clear(); 2484 } 2485 if (!PyArg_ParseTuple(args, "et|i:mkdir", 2486 Py_FileSystemDefaultEncoding, &path, &mode)) 2487 return NULL; 2488 Py_BEGIN_ALLOW_THREADS 2489 /* PyUnicode_AS_UNICODE OK without thread lock as 2490 it is a simple dereference. */ 2491 res = CreateDirectoryA(path, NULL); 2492 Py_END_ALLOW_THREADS 2493 if (!res) { 2494 win32_error("mkdir", path); 2495 PyMem_Free(path); 2496 return NULL; 2497 } 2498 PyMem_Free(path); 2499 Py_INCREF(Py_None); 2500 return Py_None; 2614 int res; 2615 char *path = NULL; 2616 int mode = 0777; 2617 2618 #ifdef MS_WINDOWS 2619 PyUnicodeObject *po; 2620 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) { 2621 Py_BEGIN_ALLOW_THREADS 2622 /* PyUnicode_AS_UNICODE OK without thread lock as 2623 it is a simple dereference. */ 2624 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL); 2625 Py_END_ALLOW_THREADS 2626 if (!res) 2627 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po)); 2628 Py_INCREF(Py_None); 2629 return Py_None; 2630 } 2631 /* Drop the argument parsing error as narrow strings 2632 are also valid. */ 2633 PyErr_Clear(); 2634 if (!PyArg_ParseTuple(args, "et|i:mkdir", 2635 Py_FileSystemDefaultEncoding, &path, &mode)) 2636 return NULL; 2637 Py_BEGIN_ALLOW_THREADS 2638 /* PyUnicode_AS_UNICODE OK without thread lock as 2639 it is a simple dereference. */ 2640 res = CreateDirectoryA(path, NULL); 2641 Py_END_ALLOW_THREADS 2642 if (!res) { 2643 win32_error("mkdir", path); 2644 PyMem_Free(path); 2645 return NULL; 2646 } 2647 PyMem_Free(path); 2648 Py_INCREF(Py_None); 2649 return Py_None; 2650 #else /* MS_WINDOWS */ 2651 2652 if (!PyArg_ParseTuple(args, "et|i:mkdir", 2653 Py_FileSystemDefaultEncoding, &path, &mode)) 2654 return NULL; 2655 Py_BEGIN_ALLOW_THREADS 2656 #if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__) 2657 res = mkdir(path); 2501 2658 #else 2502 2503 if (!PyArg_ParseTuple(args, "et|i:mkdir", 2504 Py_FileSystemDefaultEncoding, &path, &mode)) 2505 return NULL; 2506 Py_BEGIN_ALLOW_THREADS 2507 #if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__) 2508 res = mkdir(path); 2509 #else 2510 res = mkdir(path, mode); 2511 #endif 2512 Py_END_ALLOW_THREADS 2513 if (res < 0) 2514 return posix_error_with_allocated_filename(path); 2515 PyMem_Free(path); 2516 Py_INCREF(Py_None); 2517 return Py_None; 2518 #endif 2659 res = mkdir(path, mode); 2660 #endif 2661 Py_END_ALLOW_THREADS 2662 if (res < 0) 2663 return posix_error_with_allocated_filename(path); 2664 PyMem_Free(path); 2665 Py_INCREF(Py_None); 2666 return Py_None; 2667 #endif /* MS_WINDOWS */ 2519 2668 } 2520 2669 … … 2534 2683 posix_nice(PyObject *self, PyObject *args) 2535 2684 { 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2685 int increment, value; 2686 2687 if (!PyArg_ParseTuple(args, "i:nice", &increment)) 2688 return NULL; 2689 2690 /* There are two flavours of 'nice': one that returns the new 2691 priority (as required by almost all standards out there) and the 2692 Linux/FreeBSD/BSDI one, which returns '0' on success and advices 2693 the use of getpriority() to get the new priority. 2694 2695 If we are of the nice family that returns the new priority, we 2696 need to clear errno before the call, and check if errno is filled 2697 before calling posix_error() on a returnvalue of -1, because the 2698 -1 may be the actual new priority! */ 2699 2700 errno = 0; 2701 value = nice(increment); 2553 2702 #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) 2554 2555 2556 #endif 2557 2558 2559 2560 2703 if (value == 0) 2704 value = getpriority(PRIO_PROCESS, 0); 2705 #endif 2706 if (value == -1 && errno != 0) 2707 /* either nice() or getpriority() returned an error */ 2708 return posix_error(); 2709 return PyInt_FromLong((long) value); 2561 2710 } 2562 2711 #endif /* HAVE_NICE */ … … 2570 2719 { 2571 2720 #ifdef MS_WINDOWS 2572 PyObject *o1, *o2; 2573 char *p1, *p2; 2574 BOOL result; 2575 if (unicode_file_names()) { 2576 if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2)) 2577 goto error; 2578 if (!convert_to_unicode(&o1)) 2579 goto error; 2580 if (!convert_to_unicode(&o2)) { 2581 Py_DECREF(o1); 2582 goto error; 2583 } 2584 Py_BEGIN_ALLOW_THREADS 2585 result = MoveFileW(PyUnicode_AsUnicode(o1), 2586 PyUnicode_AsUnicode(o2)); 2587 Py_END_ALLOW_THREADS 2588 Py_DECREF(o1); 2589 Py_DECREF(o2); 2590 if (!result) 2591 return win32_error("rename", NULL); 2592 Py_INCREF(Py_None); 2593 return Py_None; 2721 PyObject *o1, *o2; 2722 char *p1, *p2; 2723 BOOL result; 2724 if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2)) 2725 goto error; 2726 if (!convert_to_unicode(&o1)) 2727 goto error; 2728 if (!convert_to_unicode(&o2)) { 2729 Py_DECREF(o1); 2730 goto error; 2731 } 2732 Py_BEGIN_ALLOW_THREADS 2733 result = MoveFileW(PyUnicode_AsUnicode(o1), 2734 PyUnicode_AsUnicode(o2)); 2735 Py_END_ALLOW_THREADS 2736 Py_DECREF(o1); 2737 Py_DECREF(o2); 2738 if (!result) 2739 return win32_error("rename", NULL); 2740 Py_INCREF(Py_None); 2741 return Py_None; 2594 2742 error: 2595 PyErr_Clear(); 2596 } 2597 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2)) 2598 return NULL; 2599 Py_BEGIN_ALLOW_THREADS 2600 result = MoveFileA(p1, p2); 2601 Py_END_ALLOW_THREADS 2602 if (!result) 2603 return win32_error("rename", NULL); 2604 Py_INCREF(Py_None); 2605 return Py_None; 2743 PyErr_Clear(); 2744 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2)) 2745 return NULL; 2746 Py_BEGIN_ALLOW_THREADS 2747 result = MoveFileA(p1, p2); 2748 Py_END_ALLOW_THREADS 2749 if (!result) 2750 return win32_error("rename", NULL); 2751 Py_INCREF(Py_None); 2752 return Py_None; 2606 2753 #else 2607 2754 return posix_2str(args, "etet:rename", rename); 2608 2755 #endif 2609 2756 } … … 2618 2765 { 2619 2766 #ifdef MS_WINDOWS 2620 2767 return win32_1str(args, "rmdir", "s:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW); 2621 2768 #else 2622 2769 return posix_1str(args, "et:rmdir", rmdir); 2623 2770 #endif 2624 2771 } … … 2633 2780 { 2634 2781 #ifdef MS_WINDOWS 2635 2782 return posix_do_stat(self, args, "et:stat", STAT, "U:stat", win32_wstat); 2636 2783 #else 2637 2784 return posix_do_stat(self, args, "et:stat", STAT, NULL, NULL); 2638 2785 #endif 2639 2786 } … … 2648 2795 posix_system(PyObject *self, PyObject *args) 2649 2796 { 2650 2651 2652 2653 2654 2655 2656 2657 2797 char *command; 2798 long sts; 2799 if (!PyArg_ParseTuple(args, "s:system", &command)) 2800 return NULL; 2801 Py_BEGIN_ALLOW_THREADS 2802 sts = system(command); 2803 Py_END_ALLOW_THREADS 2804 return PyInt_FromLong(sts); 2658 2805 } 2659 2806 #endif … … 2667 2814 posix_umask(PyObject *self, PyObject *args) 2668 2815 { 2669 2670 2671 2672 2673 2674 2675 2816 int i; 2817 if (!PyArg_ParseTuple(args, "i:umask", &i)) 2818 return NULL; 2819 i = (int)umask(i); 2820 if (i < 0) 2821 return posix_error(); 2822 return PyInt_FromLong((long)i); 2676 2823 } 2677 2824 … … 2689 2836 { 2690 2837 #ifdef MS_WINDOWS 2691 2838 return win32_1str(args, "remove", "s:remove", DeleteFileA, "U:remove", DeleteFileW); 2692 2839 #else 2693 2840 return posix_1str(args, "et:remove", unlink); 2694 2841 #endif 2695 2842 } … … 2704 2851 posix_uname(PyObject *self, PyObject *noargs) 2705 2852 { 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2853 struct utsname u; 2854 int res; 2855 2856 Py_BEGIN_ALLOW_THREADS 2857 res = uname(&u); 2858 Py_END_ALLOW_THREADS 2859 if (res < 0) 2860 return posix_error(); 2861 return Py_BuildValue("(sssss)", 2862 u.sysname, 2863 u.nodename, 2864 u.release, 2865 u.version, 2866 u.machine); 2720 2867 } 2721 2868 #endif /* HAVE_UNAME */ 2722 2869 2723 2870 static int 2724 extract_time(PyObject *t, long* sec, long* usec) 2725 { 2726 long intval; 2727 if (PyFloat_Check(t)) { 2728 double tval = PyFloat_AsDouble(t); 2729 PyObject *intobj = Py_TYPE(t)->tp_as_number->nb_int(t); 2730 if (!intobj) 2731 return -1; 2732 intval = PyInt_AsLong(intobj); 2733 Py_DECREF(intobj); 2734 if (intval == -1 && PyErr_Occurred()) 2735 return -1; 2736 *sec = intval; 2737 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */ 2738 if (*usec < 0) 2739 /* If rounding gave us a negative number, 2740 truncate. */ 2741 *usec = 0; 2742 return 0; 2743 } 2744 intval = PyInt_AsLong(t); 2745 if (intval == -1 && PyErr_Occurred()) 2746 return -1; 2747 *sec = intval; 2748 *usec = 0; 2871 extract_time(PyObject *t, time_t* sec, long* usec) 2872 { 2873 time_t intval; 2874 if (PyFloat_Check(t)) { 2875 double tval = PyFloat_AsDouble(t); 2876 PyObject *intobj = PyNumber_Long(t); 2877 if (!intobj) 2878 return -1; 2879 #if SIZEOF_TIME_T > SIZEOF_LONG 2880 intval = PyInt_AsUnsignedLongLongMask(intobj); 2881 #else 2882 intval = PyInt_AsLong(intobj); 2883 #endif 2884 Py_DECREF(intobj); 2885 if (intval == -1 && PyErr_Occurred()) 2886 return -1; 2887 *sec = intval; 2888 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */ 2889 if (*usec < 0) 2890 /* If rounding gave us a negative number, 2891 truncate. */ 2892 *usec = 0; 2749 2893 return 0; 2894 } 2895 #if SIZEOF_TIME_T > SIZEOF_LONG 2896 intval = PyInt_AsUnsignedLongLongMask(t); 2897 #else 2898 intval = PyInt_AsLong(t); 2899 #endif 2900 if (intval == -1 && PyErr_Occurred()) 2901 return -1; 2902 *sec = intval; 2903 *usec = 0; 2904 return 0; 2750 2905 } 2751 2906 … … 2759 2914 posix_utime(PyObject *self, PyObject *args) 2760 2915 { 2761 #ifdef Py_WIN_WIDE_FILENAMES 2762 PyObject *arg; 2763 PyUnicodeObject *obwpath; 2764 wchar_t *wpath = NULL; 2765 char *apath = NULL; 2766 HANDLE hFile; 2767 long atimesec, mtimesec, ausec, musec; 2768 FILETIME atime, mtime; 2769 PyObject *result = NULL; 2770 2771 if (unicode_file_names()) { 2772 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) { 2773 wpath = PyUnicode_AS_UNICODE(obwpath); 2774 Py_BEGIN_ALLOW_THREADS 2775 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0, 2776 NULL, OPEN_EXISTING, 2777 FILE_FLAG_BACKUP_SEMANTICS, NULL); 2778 Py_END_ALLOW_THREADS 2779 if (hFile == INVALID_HANDLE_VALUE) 2780 return win32_error_unicode("utime", wpath); 2781 } else 2782 /* Drop the argument parsing error as narrow strings 2783 are also valid. */ 2784 PyErr_Clear(); 2785 } 2786 if (!wpath) { 2787 if (!PyArg_ParseTuple(args, "etO:utime", 2788 Py_FileSystemDefaultEncoding, &apath, &arg)) 2789 return NULL; 2790 Py_BEGIN_ALLOW_THREADS 2791 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0, 2792 NULL, OPEN_EXISTING, 2793 FILE_FLAG_BACKUP_SEMANTICS, NULL); 2794 Py_END_ALLOW_THREADS 2795 if (hFile == INVALID_HANDLE_VALUE) { 2796 win32_error("utime", apath); 2797 PyMem_Free(apath); 2798 return NULL; 2799 } 2800 PyMem_Free(apath); 2801 } 2802 2803 if (arg == Py_None) { 2804 SYSTEMTIME now; 2805 GetSystemTime(&now); 2806 if (!SystemTimeToFileTime(&now, &mtime) || 2807 !SystemTimeToFileTime(&now, &atime)) { 2808 win32_error("utime", NULL); 2809 goto done; 2810 } 2811 } 2812 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { 2813 PyErr_SetString(PyExc_TypeError, 2814 "utime() arg 2 must be a tuple (atime, mtime)"); 2815 goto done; 2816 } 2817 else { 2818 if (extract_time(PyTuple_GET_ITEM(arg, 0), 2819 &atimesec, &ausec) == -1) 2820 goto done; 2821 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime); 2822 if (extract_time(PyTuple_GET_ITEM(arg, 1), 2823 &mtimesec, &musec) == -1) 2824 goto done; 2825 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime); 2826 } 2827 if (!SetFileTime(hFile, NULL, &atime, &mtime)) { 2828 /* Avoid putting the file name into the error here, 2829 as that may confuse the user into believing that 2830 something is wrong with the file, when it also 2831 could be the time stamp that gives a problem. */ 2832 win32_error("utime", NULL); 2833 } 2834 Py_INCREF(Py_None); 2835 result = Py_None; 2916 #ifdef MS_WINDOWS 2917 PyObject *arg; 2918 PyUnicodeObject *obwpath; 2919 wchar_t *wpath = NULL; 2920 char *apath = NULL; 2921 HANDLE hFile; 2922 time_t atimesec, mtimesec; 2923 long ausec, musec; 2924 FILETIME atime, mtime; 2925 PyObject *result = NULL; 2926 2927 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) { 2928 wpath = PyUnicode_AS_UNICODE(obwpath); 2929 Py_BEGIN_ALLOW_THREADS 2930 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0, 2931 NULL, OPEN_EXISTING, 2932 FILE_FLAG_BACKUP_SEMANTICS, NULL); 2933 Py_END_ALLOW_THREADS 2934 if (hFile == INVALID_HANDLE_VALUE) 2935 return win32_error_unicode("utime", wpath); 2936 } else 2937 /* Drop the argument parsing error as narrow strings 2938 are also valid. */ 2939 PyErr_Clear(); 2940 2941 if (!wpath) { 2942 if (!PyArg_ParseTuple(args, "etO:utime", 2943 Py_FileSystemDefaultEncoding, &apath, &arg)) 2944 return NULL; 2945 Py_BEGIN_ALLOW_THREADS 2946 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0, 2947 NULL, OPEN_EXISTING, 2948 FILE_FLAG_BACKUP_SEMANTICS, NULL); 2949 Py_END_ALLOW_THREADS 2950 if (hFile == INVALID_HANDLE_VALUE) { 2951 win32_error("utime", apath); 2952 PyMem_Free(apath); 2953 return NULL; 2954 } 2955 PyMem_Free(apath); 2956 } 2957 2958 if (arg == Py_None) { 2959 SYSTEMTIME now; 2960 GetSystemTime(&now); 2961 if (!SystemTimeToFileTime(&now, &mtime) || 2962 !SystemTimeToFileTime(&now, &atime)) { 2963 win32_error("utime", NULL); 2964 goto done; 2965 } 2966 } 2967 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { 2968 PyErr_SetString(PyExc_TypeError, 2969 "utime() arg 2 must be a tuple (atime, mtime)"); 2970 goto done; 2971 } 2972 else { 2973 if (extract_time(PyTuple_GET_ITEM(arg, 0), 2974 &atimesec, &ausec) == -1) 2975 goto done; 2976 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime); 2977 if (extract_time(PyTuple_GET_ITEM(arg, 1), 2978 &mtimesec, &musec) == -1) 2979 goto done; 2980 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime); 2981 } 2982 if (!SetFileTime(hFile, NULL, &atime, &mtime)) { 2983 /* Avoid putting the file name into the error here, 2984 as that may confuse the user into believing that 2985 something is wrong with the file, when it also 2986 could be the time stamp that gives a problem. */ 2987 win32_error("utime", NULL); 2988 goto done; 2989 } 2990 Py_INCREF(Py_None); 2991 result = Py_None; 2836 2992 done: 2837 CloseHandle(hFile); 2838 return result; 2839 #else /* Py_WIN_WIDE_FILENAMES */ 2840 2841 char *path = NULL; 2842 long atime, mtime, ausec, musec; 2843 int res; 2844 PyObject* arg; 2993 CloseHandle(hFile); 2994 return result; 2995 #else /* MS_WINDOWS */ 2996 2997 char *path = NULL; 2998 time_t atime, mtime; 2999 long ausec, musec; 3000 int res; 3001 PyObject* arg; 2845 3002 2846 3003 #if defined(HAVE_UTIMES) 2847 3004 struct timeval buf[2]; 2848 3005 #define ATIME buf[0].tv_sec 2849 3006 #define MTIME buf[1].tv_sec 2850 3007 #elif defined(HAVE_UTIME_H) 2851 3008 /* XXX should define struct utimbuf instead, above */ 2852 3009 struct utimbuf buf; 2853 3010 #define ATIME buf.actime 2854 3011 #define MTIME buf.modtime 2855 3012 #define UTIME_ARG &buf 2856 3013 #else /* HAVE_UTIMES */ 2857 3014 time_t buf[2]; 2858 3015 #define ATIME buf[0] 2859 3016 #define MTIME buf[1] … … 2862 3019 2863 3020 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 3021 if (!PyArg_ParseTuple(args, "etO:utime", 3022 Py_FileSystemDefaultEncoding, &path, &arg)) 3023 return NULL; 3024 if (arg == Py_None) { 3025 /* optional time values not given */ 3026 Py_BEGIN_ALLOW_THREADS 3027 res = utime(path, NULL); 3028 Py_END_ALLOW_THREADS 3029 } 3030 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) { 3031 PyErr_SetString(PyExc_TypeError, 3032 "utime() arg 2 must be a tuple (atime, mtime)"); 3033 PyMem_Free(path); 3034 return NULL; 3035 } 3036 else { 3037 if (extract_time(PyTuple_GET_ITEM(arg, 0), 3038 &atime, &ausec) == -1) { 3039 PyMem_Free(path); 3040 return NULL; 3041 } 3042 if (extract_time(PyTuple_GET_ITEM(arg, 1), 3043 &mtime, &musec) == -1) { 3044 PyMem_Free(path); 3045 return NULL; 3046 } 3047 ATIME = atime; 3048 MTIME = mtime; 2892 3049 #ifdef HAVE_UTIMES 2893 2894 2895 2896 2897 3050 buf[0].tv_usec = ausec; 3051 buf[1].tv_usec = musec; 3052 Py_BEGIN_ALLOW_THREADS 3053 res = utimes(path, buf); 3054 Py_END_ALLOW_THREADS 2898 3055 #else 2899 2900 2901 3056 Py_BEGIN_ALLOW_THREADS 3057 res = utime(path, UTIME_ARG); 3058 Py_END_ALLOW_THREADS 2902 3059 #endif /* HAVE_UTIMES */ 2903 2904 2905 2906 2907 2908 2909 3060 } 3061 if (res < 0) { 3062 return posix_error_with_allocated_filename(path); 3063 } 3064 PyMem_Free(path); 3065 Py_INCREF(Py_None); 3066 return Py_None; 2910 3067 #undef UTIME_ARG 2911 3068 #undef ATIME 2912 3069 #undef MTIME 2913 #endif /* Py_WIN_WIDE_FILENAMES */3070 #endif /* MS_WINDOWS */ 2914 3071 } 2915 3072 … … 2924 3081 posix__exit(PyObject *self, PyObject *args) 2925 3082 { 2926 2927 2928 2929 2930 3083 int sts; 3084 if (!PyArg_ParseTuple(args, "i:_exit", &sts)) 3085 return NULL; 3086 _exit(sts); 3087 return NULL; /* Make gcc -Wall happy */ 2931 3088 } 2932 3089 … … 2935 3092 free_string_array(char **array, Py_ssize_t count) 2936 3093 { 2937 2938 2939 2940 3094 Py_ssize_t i; 3095 for (i = 0; i < count; i++) 3096 PyMem_Free(array[i]); 3097 PyMem_DEL(array); 2941 3098 } 2942 3099 #endif … … 2948 3105 Execute an executable path with arguments, replacing current process.\n\ 2949 3106 \n\ 2950 2951 3107 path: path of executable file\n\ 3108 args: tuple or list of strings"); 2952 3109 2953 3110 static PyObject * 2954 3111 posix_execv(PyObject *self, PyObject *args) 2955 3112 { 2956 char *path; 2957 PyObject *argv; 2958 char **argvlist; 2959 Py_ssize_t i, argc; 2960 PyObject *(*getitem)(PyObject *, Py_ssize_t); 2961 2962 /* execv has two arguments: (path, argv), where 2963 argv is a list or tuple of strings. */ 2964 2965 if (!PyArg_ParseTuple(args, "etO:execv", 2966 Py_FileSystemDefaultEncoding, 2967 &path, &argv)) 2968 return NULL; 2969 if (PyList_Check(argv)) { 2970 argc = PyList_Size(argv); 2971 getitem = PyList_GetItem; 2972 } 2973 else if (PyTuple_Check(argv)) { 2974 argc = PyTuple_Size(argv); 2975 getitem = PyTuple_GetItem; 2976 } 2977 else { 2978 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list"); 2979 PyMem_Free(path); 2980 return NULL; 2981 } 2982 2983 argvlist = PyMem_NEW(char *, argc+1); 2984 if (argvlist == NULL) { 2985 PyMem_Free(path); 2986 return PyErr_NoMemory(); 2987 } 2988 for (i = 0; i < argc; i++) { 2989 if (!PyArg_Parse((*getitem)(argv, i), "et", 2990 Py_FileSystemDefaultEncoding, 2991 &argvlist[i])) { 2992 free_string_array(argvlist, i); 2993 PyErr_SetString(PyExc_TypeError, 2994 "execv() arg 2 must contain only strings"); 2995 PyMem_Free(path); 2996 return NULL; 2997 2998 } 2999 } 3000 argvlist[argc] = NULL; 3001 3002 execv(path, argvlist); 3003 3004 /* If we get here it's definitely an error */ 3005 3006 free_string_array(argvlist, argc); 3007 PyMem_Free(path); 3008 return posix_error(); 3113 char *path; 3114 PyObject *argv; 3115 char **argvlist; 3116 Py_ssize_t i, argc; 3117 PyObject *(*getitem)(PyObject *, Py_ssize_t); 3118 3119 /* execv has two arguments: (path, argv), where 3120 argv is a list or tuple of strings. */ 3121 3122 if (!PyArg_ParseTuple(args, "etO:execv", 3123 Py_FileSystemDefaultEncoding, 3124 &path, &argv)) 3125 return NULL; 3126 if (PyList_Check(argv)) { 3127 argc = PyList_Size(argv); 3128 getitem = PyList_GetItem; 3129 } 3130 else if (PyTuple_Check(argv)) { 3131 argc = PyTuple_Size(argv); 3132 getitem = PyTuple_GetItem; 3133 } 3134 else { 3135 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list"); 3136 PyMem_Free(path); 3137 return NULL; 3138 } 3139 if (argc < 1) { 3140 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); 3141 PyMem_Free(path); 3142 return NULL; 3143 } 3144 3145 argvlist = PyMem_NEW(char *, argc+1); 3146 if (argvlist == NULL) { 3147 PyMem_Free(path); 3148 return PyErr_NoMemory(); 3149 } 3150 for (i = 0; i < argc; i++) { 3151 if (!PyArg_Parse((*getitem)(argv, i), "et", 3152 Py_FileSystemDefaultEncoding, 3153 &argvlist[i])) { 3154 free_string_array(argvlist, i); 3155 PyErr_SetString(PyExc_TypeError, 3156 "execv() arg 2 must contain only strings"); 3157 PyMem_Free(path); 3158 return NULL; 3159 3160 } 3161 } 3162 argvlist[argc] = NULL; 3163 3164 execv(path, argvlist); 3165 3166 /* If we get here it's definitely an error */ 3167 3168 free_string_array(argvlist, argc); 3169 PyMem_Free(path); 3170 return posix_error(); 3009 3171 } 3010 3172 … … 3014 3176 Execute a path with arguments and environment, replacing current process.\n\ 3015 3177 \n\ 3016 3017 3018 3178 path: path of executable file\n\ 3179 args: tuple or list of arguments\n\ 3180 env: dictionary of strings mapping to strings"); 3019 3181 3020 3182 static PyObject * 3021 3183 posix_execve(PyObject *self, PyObject *args) 3022 3184 { 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3185 char *path; 3186 PyObject *argv, *env; 3187 char **argvlist; 3188 char **envlist; 3189 PyObject *key, *val, *keys=NULL, *vals=NULL; 3190 Py_ssize_t i, pos, argc, envc; 3191 PyObject *(*getitem)(PyObject *, Py_ssize_t); 3192 Py_ssize_t lastarg = 0; 3193 3194 /* execve has three arguments: (path, argv, env), where 3195 argv is a list or tuple of strings and env is a dictionary 3196 like posix.environ. */ 3197 3198 if (!PyArg_ParseTuple(args, "etOO:execve", 3199 Py_FileSystemDefaultEncoding, 3200 &path, &argv, &env)) 3201 return NULL; 3202 if (PyList_Check(argv)) { 3203 argc = PyList_Size(argv); 3204 getitem = PyList_GetItem; 3205 } 3206 else if (PyTuple_Check(argv)) { 3207 argc = PyTuple_Size(argv); 3208 getitem = PyTuple_GetItem; 3209 } 3210 else { 3211 PyErr_SetString(PyExc_TypeError, 3212 "execve() arg 2 must be a tuple or list"); 3213 goto fail_0; 3214 } 3215 if (!PyMapping_Check(env)) { 3216 PyErr_SetString(PyExc_TypeError, 3217 "execve() arg 3 must be a mapping object"); 3218 goto fail_0; 3219 } 3220 3221 argvlist = PyMem_NEW(char *, argc+1); 3222 if (argvlist == NULL) { 3223 PyErr_NoMemory(); 3224 goto fail_0; 3225 } 3226 for (i = 0; i < argc; i++) { 3227 if (!PyArg_Parse((*getitem)(argv, i), 3228 "et;execve() arg 2 must contain only strings", 3229 Py_FileSystemDefaultEncoding, 3230 &argvlist[i])) 3231 { 3232 lastarg = i; 3233 goto fail_1; 3234 } 3235 } 3236 lastarg = argc; 3237 argvlist[argc] = NULL; 3238 3239 i = PyMapping_Size(env); 3240 if (i < 0) 3241 goto fail_1; 3242 envlist = PyMem_NEW(char *, i + 1); 3243 if (envlist == NULL) { 3244 PyErr_NoMemory(); 3245 goto fail_1; 3246 } 3247 envc = 0; 3248 keys = PyMapping_Keys(env); 3249 vals = PyMapping_Values(env); 3250 if (!keys || !vals) 3251 goto fail_2; 3252 if (!PyList_Check(keys) || !PyList_Check(vals)) { 3253 PyErr_SetString(PyExc_TypeError, 3254 "execve(): env.keys() or env.values() is not a list"); 3255 goto fail_2; 3256 } 3257 3258 for (pos = 0; pos < i; pos++) { 3259 char *p, *k, *v; 3260 size_t len; 3261 3262 key = PyList_GetItem(keys, pos); 3263 val = PyList_GetItem(vals, pos); 3264 if (!key || !val) 3265 goto fail_2; 3266 3267 if (!PyArg_Parse( 3268 key, 3269 "s;execve() arg 3 contains a non-string key", 3270 &k) || 3271 !PyArg_Parse( 3272 val, 3273 "s;execve() arg 3 contains a non-string value", 3274 &v)) 3275 { 3276 goto fail_2; 3277 } 3116 3278 3117 3279 #if defined(PYOS_OS2) … … 3119 3281 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) { 3120 3282 #endif 3121 3122 3123 3124 3125 3126 3127 3128 3283 len = PyString_Size(key) + PyString_Size(val) + 2; 3284 p = PyMem_NEW(char, len); 3285 if (p == NULL) { 3286 PyErr_NoMemory(); 3287 goto fail_2; 3288 } 3289 PyOS_snprintf(p, len, "%s=%s", k, v); 3290 envlist[envc++] = p; 3129 3291 #if defined(PYOS_OS2) 3130 }3131 #endif 3132 3133 3134 3135 3136 3137 3138 3139 3292 } 3293 #endif 3294 } 3295 envlist[envc] = 0; 3296 3297 execve(path, argvlist, envlist); 3298 3299 /* If we get here it's definitely an error */ 3300 3301 (void) posix_error(); 3140 3302 3141 3303 fail_2: 3142 3143 3144 3304 while (--envc >= 0) 3305 PyMem_DEL(envlist[envc]); 3306 PyMem_DEL(envlist); 3145 3307 fail_1: 3146 3147 3148 3308 free_string_array(argvlist, lastarg); 3309 Py_XDECREF(vals); 3310 Py_XDECREF(keys); 3149 3311 fail_0: 3150 3151 3312 PyMem_Free(path); 3313 return NULL; 3152 3314 } 3153 3315 #endif /* HAVE_EXECV */ … … 3159 3321 Execute the program 'path' in a new process.\n\ 3160 3322 \n\ 3161 3162 3163 3323 mode: mode of process creation\n\ 3324 path: path of executable file\n\ 3325 args: tuple or list of strings"); 3164 3326 3165 3327 static PyObject * 3166 3328 posix_spawnv(PyObject *self, PyObject *args) 3167 3329 { 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3330 char *path; 3331 PyObject *argv; 3332 char **argvlist; 3333 int mode, i; 3334 Py_ssize_t argc; 3335 Py_intptr_t spawnval; 3336 PyObject *(*getitem)(PyObject *, Py_ssize_t); 3337 3338 /* spawnv has three arguments: (mode, path, argv), where 3339 argv is a list or tuple of strings. */ 3340 3341 if (!PyArg_ParseTuple(args, "ietO:spawnv", &mode, 3342 Py_FileSystemDefaultEncoding, 3343 &path, &argv)) 3344 return NULL; 3345 if (PyList_Check(argv)) { 3346 argc = PyList_Size(argv); 3347 getitem = PyList_GetItem; 3348 } 3349 else if (PyTuple_Check(argv)) { 3350 argc = PyTuple_Size(argv); 3351 getitem = PyTuple_GetItem; 3352 } 3353 else { 3354 PyErr_SetString(PyExc_TypeError, 3355 "spawnv() arg 2 must be a tuple or list"); 3356 PyMem_Free(path); 3357 return NULL; 3358 } 3359 3360 argvlist = PyMem_NEW(char *, argc+1); 3361 if (argvlist == NULL) { 3362 PyMem_Free(path); 3363 return PyErr_NoMemory(); 3364 } 3365 for (i = 0; i < argc; i++) { 3366 if (!PyArg_Parse((*getitem)(argv, i), "et", 3367 Py_FileSystemDefaultEncoding, 3368 &argvlist[i])) { 3369 free_string_array(argvlist, i); 3370 PyErr_SetString( 3371 PyExc_TypeError, 3372 "spawnv() arg 2 must contain only strings"); 3373 PyMem_Free(path); 3374 return NULL; 3375 } 3376 } 3377 argvlist[argc] = NULL; 3216 3378 3217 3379 #if defined(PYOS_OS2) && defined(PYCC_GCC) 3218 3219 3220 3380 Py_BEGIN_ALLOW_THREADS 3381 spawnval = spawnv(mode, path, argvlist); 3382 Py_END_ALLOW_THREADS 3221 3383 #else 3222 3223 3224 3225 3226 3227 3228 #endif 3229 3230 3231 3232 3233 3234 3235 3384 if (mode == _OLD_P_OVERLAY) 3385 mode = _P_OVERLAY; 3386 3387 Py_BEGIN_ALLOW_THREADS 3388 spawnval = _spawnv(mode, path, argvlist); 3389 Py_END_ALLOW_THREADS 3390 #endif 3391 3392 free_string_array(argvlist, argc); 3393 PyMem_Free(path); 3394 3395 if (spawnval == -1) 3396 return posix_error(); 3397 else 3236 3398 #if SIZEOF_LONG == SIZEOF_VOID_P 3237 3399 return Py_BuildValue("l", (long) spawnval); 3238 3400 #else 3239 3401 return Py_BuildValue("L", (PY_LONG_LONG) spawnval); 3240 3402 #endif 3241 3403 } … … 3246 3408 Execute the program 'path' in a new process.\n\ 3247 3409 \n\ 3248 3249 3250 3251 3410 mode: mode of process creation\n\ 3411 path: path of executable file\n\ 3412 args: tuple or list of arguments\n\ 3413 env: dictionary of strings mapping to strings"); 3252 3414 3253 3415 static PyObject * 3254 3416 posix_spawnve(PyObject *self, PyObject *args) 3255 3417 { 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3418 char *path; 3419 PyObject *argv, *env; 3420 char **argvlist; 3421 char **envlist; 3422 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; 3423 int mode, pos, envc; 3424 Py_ssize_t argc, i; 3425 Py_intptr_t spawnval; 3426 PyObject *(*getitem)(PyObject *, Py_ssize_t); 3427 Py_ssize_t lastarg = 0; 3428 3429 /* spawnve has four arguments: (mode, path, argv, env), where 3430 argv is a list or tuple of strings and env is a dictionary 3431 like posix.environ. */ 3432 3433 if (!PyArg_ParseTuple(args, "ietOO:spawnve", &mode, 3434 Py_FileSystemDefaultEncoding, 3435 &path, &argv, &env)) 3436 return NULL; 3437 if (PyList_Check(argv)) { 3438 argc = PyList_Size(argv); 3439 getitem = PyList_GetItem; 3440 } 3441 else if (PyTuple_Check(argv)) { 3442 argc = PyTuple_Size(argv); 3443 getitem = PyTuple_GetItem; 3444 } 3445 else { 3446 PyErr_SetString(PyExc_TypeError, 3447 "spawnve() arg 2 must be a tuple or list"); 3448 goto fail_0; 3449 } 3450 if (!PyMapping_Check(env)) { 3451 PyErr_SetString(PyExc_TypeError, 3452 "spawnve() arg 3 must be a mapping object"); 3453 goto fail_0; 3454 } 3455 3456 argvlist = PyMem_NEW(char *, argc+1); 3457 if (argvlist == NULL) { 3458 PyErr_NoMemory(); 3459 goto fail_0; 3460 } 3461 for (i = 0; i < argc; i++) { 3462 if (!PyArg_Parse((*getitem)(argv, i), 3463 "et;spawnve() arg 2 must contain only strings", 3464 Py_FileSystemDefaultEncoding, 3465 &argvlist[i])) 3466 { 3467 lastarg = i; 3468 goto fail_1; 3469 } 3470 } 3471 lastarg = argc; 3472 argvlist[argc] = NULL; 3473 3474 i = PyMapping_Size(env); 3475 if (i < 0) 3476 goto fail_1; 3477 envlist = PyMem_NEW(char *, i + 1); 3478 if (envlist == NULL) { 3479 PyErr_NoMemory(); 3480 goto fail_1; 3481 } 3482 envc = 0; 3483 keys = PyMapping_Keys(env); 3484 vals = PyMapping_Values(env); 3485 if (!keys || !vals) 3486 goto fail_2; 3487 if (!PyList_Check(keys) || !PyList_Check(vals)) { 3488 PyErr_SetString(PyExc_TypeError, 3489 "spawnve(): env.keys() or env.values() is not a list"); 3490 goto fail_2; 3491 } 3492 3493 for (pos = 0; pos < i; pos++) { 3494 char *p, *k, *v; 3495 size_t len; 3496 3497 key = PyList_GetItem(keys, pos); 3498 val = PyList_GetItem(vals, pos); 3499 if (!key || !val) 3500 goto fail_2; 3501 3502 if (!PyArg_Parse( 3503 key, 3504 "s;spawnve() arg 3 contains a non-string key", 3505 &k) || 3506 !PyArg_Parse( 3507 val, 3508 "s;spawnve() arg 3 contains a non-string value", 3509 &v)) 3510 { 3511 goto fail_2; 3512 } 3513 len = PyString_Size(key) + PyString_Size(val) + 2; 3514 p = PyMem_NEW(char, len); 3515 if (p == NULL) { 3516 PyErr_NoMemory(); 3517 goto fail_2; 3518 } 3519 PyOS_snprintf(p, len, "%s=%s", k, v); 3520 envlist[envc++] = p; 3521 } 3522 envlist[envc] = 0; 3361 3523 3362 3524 #if defined(PYOS_OS2) && defined(PYCC_GCC) 3363 3364 3365 3525 Py_BEGIN_ALLOW_THREADS 3526 spawnval = spawnve(mode, path, argvlist, envlist); 3527 Py_END_ALLOW_THREADS 3366 3528 #else 3367 3368 3369 3370 3371 3372 3373 #endif 3374 3375 3376 3377 3529 if (mode == _OLD_P_OVERLAY) 3530 mode = _P_OVERLAY; 3531 3532 Py_BEGIN_ALLOW_THREADS 3533 spawnval = _spawnve(mode, path, argvlist, envlist); 3534 Py_END_ALLOW_THREADS 3535 #endif 3536 3537 if (spawnval == -1) 3538 (void) posix_error(); 3539 else 3378 3540 #if SIZEOF_LONG == SIZEOF_VOID_P 3379 3541 res = Py_BuildValue("l", (long) spawnval); 3380 3542 #else 3381 3543 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval); 3382 3544 #endif 3383 3545 3384 3546 fail_2: 3385 3386 3387 3547 while (--envc >= 0) 3548 PyMem_DEL(envlist[envc]); 3549 PyMem_DEL(envlist); 3388 3550 fail_1: 3389 3390 3391 3551 free_string_array(argvlist, lastarg); 3552 Py_XDECREF(vals); 3553 Py_XDECREF(keys); 3392 3554 fail_0: 3393 3394 3555 PyMem_Free(path); 3556 return res; 3395 3557 } 3396 3558 … … 3402 3564 search path to find the file.\n\ 3403 3565 \n\ 3404 3405 3406 3566 mode: mode of process creation\n\ 3567 file: executable file name\n\ 3568 args: tuple or list of strings"); 3407 3569 3408 3570 static PyObject * 3409 3571 posix_spawnvp(PyObject *self, PyObject *args) 3410 3572 { 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3573 char *path; 3574 PyObject *argv; 3575 char **argvlist; 3576 int mode, i, argc; 3577 Py_intptr_t spawnval; 3578 PyObject *(*getitem)(PyObject *, Py_ssize_t); 3579 3580 /* spawnvp has three arguments: (mode, path, argv), where 3581 argv is a list or tuple of strings. */ 3582 3583 if (!PyArg_ParseTuple(args, "ietO:spawnvp", &mode, 3584 Py_FileSystemDefaultEncoding, 3585 &path, &argv)) 3586 return NULL; 3587 if (PyList_Check(argv)) { 3588 argc = PyList_Size(argv); 3589 getitem = PyList_GetItem; 3590 } 3591 else if (PyTuple_Check(argv)) { 3592 argc = PyTuple_Size(argv); 3593 getitem = PyTuple_GetItem; 3594 } 3595 else { 3596 PyErr_SetString(PyExc_TypeError, 3597 "spawnvp() arg 2 must be a tuple or list"); 3598 PyMem_Free(path); 3599 return NULL; 3600 } 3601 3602 argvlist = PyMem_NEW(char *, argc+1); 3603 if (argvlist == NULL) { 3604 PyMem_Free(path); 3605 return PyErr_NoMemory(); 3606 } 3607 for (i = 0; i < argc; i++) { 3608 if (!PyArg_Parse((*getitem)(argv, i), "et", 3609 Py_FileSystemDefaultEncoding, 3610 &argvlist[i])) { 3611 free_string_array(argvlist, i); 3612 PyErr_SetString( 3613 PyExc_TypeError, 3614 "spawnvp() arg 2 must contain only strings"); 3615 PyMem_Free(path); 3616 return NULL; 3617 } 3618 } 3619 argvlist[argc] = NULL; 3620 3621 Py_BEGIN_ALLOW_THREADS 3460 3622 #if defined(PYCC_GCC) 3461 3623 spawnval = spawnvp(mode, path, argvlist); 3462 3624 #else 3463 3464 #endif 3465 3466 3467 3468 3469 3470 3471 3472 3473 3625 spawnval = _spawnvp(mode, path, argvlist); 3626 #endif 3627 Py_END_ALLOW_THREADS 3628 3629 free_string_array(argvlist, argc); 3630 PyMem_Free(path); 3631 3632 if (spawnval == -1) 3633 return posix_error(); 3634 else 3635 return Py_BuildValue("l", (long) spawnval); 3474 3636 } 3475 3637 … … 3480 3642 search path to find the file.\n\ 3481 3643 \n\ 3482 3483 3484 3485 3644 mode: mode of process creation\n\ 3645 file: executable file name\n\ 3646 args: tuple or list of arguments\n\ 3647 env: dictionary of strings mapping to strings"); 3486 3648 3487 3649 static PyObject * 3488 3650 posix_spawnvpe(PyObject *self, PyObject *args) 3489 3651 { 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3652 char *path; 3653 PyObject *argv, *env; 3654 char **argvlist; 3655 char **envlist; 3656 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL; 3657 int mode, i, pos, argc, envc; 3658 Py_intptr_t spawnval; 3659 PyObject *(*getitem)(PyObject *, Py_ssize_t); 3660 int lastarg = 0; 3661 3662 /* spawnvpe has four arguments: (mode, path, argv, env), where 3663 argv is a list or tuple of strings and env is a dictionary 3664 like posix.environ. */ 3665 3666 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode, 3667 Py_FileSystemDefaultEncoding, 3668 &path, &argv, &env)) 3669 return NULL; 3670 if (PyList_Check(argv)) { 3671 argc = PyList_Size(argv); 3672 getitem = PyList_GetItem; 3673 } 3674 else if (PyTuple_Check(argv)) { 3675 argc = PyTuple_Size(argv); 3676 getitem = PyTuple_GetItem; 3677 } 3678 else { 3679 PyErr_SetString(PyExc_TypeError, 3680 "spawnvpe() arg 2 must be a tuple or list"); 3681 goto fail_0; 3682 } 3683 if (!PyMapping_Check(env)) { 3684 PyErr_SetString(PyExc_TypeError, 3685 "spawnvpe() arg 3 must be a mapping object"); 3686 goto fail_0; 3687 } 3688 3689 argvlist = PyMem_NEW(char *, argc+1); 3690 if (argvlist == NULL) { 3691 PyErr_NoMemory(); 3692 goto fail_0; 3693 } 3694 for (i = 0; i < argc; i++) { 3695 if (!PyArg_Parse((*getitem)(argv, i), 3696 "et;spawnvpe() arg 2 must contain only strings", 3697 Py_FileSystemDefaultEncoding, 3698 &argvlist[i])) 3699 { 3700 lastarg = i; 3701 goto fail_1; 3702 } 3703 } 3704 lastarg = argc; 3705 argvlist[argc] = NULL; 3706 3707 i = PyMapping_Size(env); 3708 if (i < 0) 3709 goto fail_1; 3710 envlist = PyMem_NEW(char *, i + 1); 3711 if (envlist == NULL) { 3712 PyErr_NoMemory(); 3713 goto fail_1; 3714 } 3715 envc = 0; 3716 keys = PyMapping_Keys(env); 3717 vals = PyMapping_Values(env); 3718 if (!keys || !vals) 3719 goto fail_2; 3720 if (!PyList_Check(keys) || !PyList_Check(vals)) { 3721 PyErr_SetString(PyExc_TypeError, 3722 "spawnvpe(): env.keys() or env.values() is not a list"); 3723 goto fail_2; 3724 } 3725 3726 for (pos = 0; pos < i; pos++) { 3727 char *p, *k, *v; 3728 size_t len; 3729 3730 key = PyList_GetItem(keys, pos); 3731 val = PyList_GetItem(vals, pos); 3732 if (!key || !val) 3733 goto fail_2; 3734 3735 if (!PyArg_Parse( 3736 key, 3737 "s;spawnvpe() arg 3 contains a non-string key", 3738 &k) || 3739 !PyArg_Parse( 3740 val, 3741 "s;spawnvpe() arg 3 contains a non-string value", 3742 &v)) 3743 { 3744 goto fail_2; 3745 } 3746 len = PyString_Size(key) + PyString_Size(val) + 2; 3747 p = PyMem_NEW(char, len); 3748 if (p == NULL) { 3749 PyErr_NoMemory(); 3750 goto fail_2; 3751 } 3752 PyOS_snprintf(p, len, "%s=%s", k, v); 3753 envlist[envc++] = p; 3754 } 3755 envlist[envc] = 0; 3756 3757 Py_BEGIN_ALLOW_THREADS 3596 3758 #if defined(PYCC_GCC) 3597 3759 spawnval = spawnvpe(mode, path, argvlist, envlist); 3598 3760 #else 3599 3600 #endif 3601 3602 3603 3604 3605 3606 3761 spawnval = _spawnvpe(mode, path, argvlist, envlist); 3762 #endif 3763 Py_END_ALLOW_THREADS 3764 3765 if (spawnval == -1) 3766 (void) posix_error(); 3767 else 3768 res = Py_BuildValue("l", (long) spawnval); 3607 3769 3608 3770 fail_2: 3609 3610 3611 3771 while (--envc >= 0) 3772 PyMem_DEL(envlist[envc]); 3773 PyMem_DEL(envlist); 3612 3774 fail_1: 3613 3614 3615 3775 free_string_array(argvlist, lastarg); 3776 Py_XDECREF(vals); 3777 Py_XDECREF(keys); 3616 3778 fail_0: 3617 3618 3779 PyMem_Free(path); 3780 return res; 3619 3781 } 3620 3782 #endif /* PYOS_OS2 */ … … 3632 3794 posix_fork1(PyObject *self, PyObject *noargs) 3633 3795 { 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3796 pid_t pid; 3797 int result = 0; 3798 _PyImport_AcquireLock(); 3799 pid = fork1(); 3800 if (pid == 0) { 3801 /* child: this clobbers and resets the import lock. */ 3802 PyOS_AfterFork(); 3803 } else { 3804 /* parent: release the import lock. */ 3805 result = _PyImport_ReleaseLock(); 3806 } 3807 if (pid == -1) 3808 return posix_error(); 3809 if (result < 0) { 3810 /* Don't clobber the OSError if the fork failed. */ 3811 PyErr_SetString(PyExc_RuntimeError, 3812 "not holding the import lock"); 3813 return NULL; 3814 } 3815 return PyLong_FromPid(pid); 3654 3816 } 3655 3817 #endif … … 3665 3827 posix_fork(PyObject *self, PyObject *noargs) 3666 3828 { 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3829 pid_t pid; 3830 int result = 0; 3831 _PyImport_AcquireLock(); 3832 pid = fork(); 3833 if (pid == 0) { 3834 /* child: this clobbers and resets the import lock. */ 3835 PyOS_AfterFork(); 3836 } else { 3837 /* parent: release the import lock. */ 3838 result = _PyImport_ReleaseLock(); 3839 } 3840 if (pid == -1) 3841 return posix_error(); 3842 if (result < 0) { 3843 /* Don't clobber the OSError if the fork failed. */ 3844 PyErr_SetString(PyExc_RuntimeError, 3845 "not holding the import lock"); 3846 return NULL; 3847 } 3848 return PyLong_FromPid(pid); 3687 3849 } 3688 3850 #endif … … 3703 3865 #ifdef HAVE_LIBUTIL_H 3704 3866 #include <libutil.h> 3867 #else 3868 #ifdef HAVE_UTIL_H 3869 #include <util.h> 3870 #endif /* HAVE_UTIL_H */ 3705 3871 #endif /* HAVE_LIBUTIL_H */ 3706 3872 #endif /* HAVE_PTY_H */ … … 3718 3884 posix_openpty(PyObject *self, PyObject *noargs) 3719 3885 { 3720 3886 int master_fd, slave_fd; 3721 3887 #ifndef HAVE_OPENPTY 3722 3888 char * slave_name; 3723 3889 #endif 3724 3890 #if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY) 3725 3891 PyOS_sighandler_t sig_saved; 3726 3892 #ifdef sun 3727 3893 extern char *ptsname(int fildes); 3728 3894 #endif 3729 3895 #endif 3730 3896 3731 3897 #ifdef HAVE_OPENPTY 3732 3733 3898 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) 3899 return posix_error(); 3734 3900 #elif defined(HAVE__GETPTY) 3735 3736 3737 3738 3739 3740 3741 3901 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); 3902 if (slave_name == NULL) 3903 return posix_error(); 3904 3905 slave_fd = open(slave_name, O_RDWR); 3906 if (slave_fd < 0) 3907 return posix_error(); 3742 3908 #else 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3909 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */ 3910 if (master_fd < 0) 3911 return posix_error(); 3912 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); 3913 /* change permission of slave */ 3914 if (grantpt(master_fd) < 0) { 3915 PyOS_setsig(SIGCHLD, sig_saved); 3916 return posix_error(); 3917 } 3918 /* unlock slave */ 3919 if (unlockpt(master_fd) < 0) { 3920 PyOS_setsig(SIGCHLD, sig_saved); 3921 return posix_error(); 3922 } 3923 PyOS_setsig(SIGCHLD, sig_saved); 3924 slave_name = ptsname(master_fd); /* get name of slave */ 3925 if (slave_name == NULL) 3926 return posix_error(); 3927 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ 3928 if (slave_fd < 0) 3929 return posix_error(); 3764 3930 #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC) 3765 3766 3931 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ 3932 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ 3767 3933 #ifndef __hpux 3768 3934 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ 3769 3935 #endif /* __hpux */ 3770 3936 #endif /* HAVE_CYGWIN */ 3771 3937 #endif /* HAVE_OPENPTY */ 3772 3938 3773 3939 return Py_BuildValue("(ii)", master_fd, slave_fd); 3774 3940 3775 3941 } … … 3786 3952 posix_forkpty(PyObject *self, PyObject *noargs) 3787 3953 { 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3954 int master_fd = -1, result = 0; 3955 pid_t pid; 3956 3957 _PyImport_AcquireLock(); 3958 pid = forkpty(&master_fd, NULL, NULL, NULL); 3959 if (pid == 0) { 3960 /* child: this clobbers and resets the import lock. */ 3961 PyOS_AfterFork(); 3962 } else { 3963 /* parent: release the import lock. */ 3964 result = _PyImport_ReleaseLock(); 3965 } 3966 if (pid == -1) 3967 return posix_error(); 3968 if (result < 0) { 3969 /* Don't clobber the OSError if the fork failed. */ 3970 PyErr_SetString(PyExc_RuntimeError, 3971 "not holding the import lock"); 3972 return NULL; 3973 } 3974 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd); 3809 3975 } 3810 3976 #endif … … 3818 3984 posix_getegid(PyObject *self, PyObject *noargs) 3819 3985 { 3820 return PyInt_FromLong((long)getegid());3986 return _PyInt_FromGid(getegid()); 3821 3987 } 3822 3988 #endif … … 3831 3997 posix_geteuid(PyObject *self, PyObject *noargs) 3832 3998 { 3833 return PyInt_FromLong((long)geteuid());3999 return _PyInt_FromUid(geteuid()); 3834 4000 } 3835 4001 #endif … … 3844 4010 posix_getgid(PyObject *self, PyObject *noargs) 3845 4011 { 3846 return PyInt_FromLong((long)getgid());4012 return _PyInt_FromGid(getgid()); 3847 4013 } 3848 4014 #endif … … 3856 4022 posix_getpid(PyObject *self, PyObject *noargs) 3857 4023 { 3858 4024 return PyLong_FromPid(getpid()); 3859 4025 } 3860 4026 … … 3873 4039 #define MAX_GROUPS NGROUPS_MAX 3874 4040 #else 3875 4041 /* defined to be 16 on Solaris7, so this should be a small number */ 3876 4042 #define MAX_GROUPS 64 3877 4043 #endif 3878 gid_t grouplist[MAX_GROUPS]; 3879 int n; 3880 3881 n = getgroups(MAX_GROUPS, grouplist); 3882 if (n < 0) 3883 posix_error(); 3884 else { 3885 result = PyList_New(n); 3886 if (result != NULL) { 3887 int i; 3888 for (i = 0; i < n; ++i) { 3889 PyObject *o = PyInt_FromLong((long)grouplist[i]); 3890 if (o == NULL) { 3891 Py_DECREF(result); 3892 result = NULL; 3893 break; 3894 } 3895 PyList_SET_ITEM(result, i, o); 4044 gid_t grouplist[MAX_GROUPS]; 4045 4046 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results 4047 * This is a helper variable to store the intermediate result when 4048 * that happens. 4049 * 4050 * To keep the code readable the OSX behaviour is unconditional, 4051 * according to the POSIX spec this should be safe on all unix-y 4052 * systems. 4053 */ 4054 gid_t* alt_grouplist = grouplist; 4055 int n; 4056 4057 #ifdef __APPLE__ 4058 /* Issue #17557: As of OS X 10.8, getgroups(2) no longer raises EINVAL if 4059 * there are more groups than can fit in grouplist. Therefore, on OS X 4060 * always first call getgroups with length 0 to get the actual number 4061 * of groups. 4062 */ 4063 n = getgroups(0, NULL); 4064 if (n < 0) { 4065 return posix_error(); 4066 } else if (n <= MAX_GROUPS) { 4067 /* groups will fit in existing array */ 4068 alt_grouplist = grouplist; 4069 } else { 4070 alt_grouplist = PyMem_Malloc(n * sizeof(gid_t)); 4071 if (alt_grouplist == NULL) { 4072 errno = EINVAL; 4073 return posix_error(); 4074 } 4075 } 4076 4077 n = getgroups(n, alt_grouplist); 4078 if (n == -1) { 4079 if (alt_grouplist != grouplist) { 4080 PyMem_Free(alt_grouplist); 4081 } 4082 return posix_error(); 4083 } 4084 #else 4085 n = getgroups(MAX_GROUPS, grouplist); 4086 if (n < 0) { 4087 if (errno == EINVAL) { 4088 n = getgroups(0, NULL); 4089 if (n == -1) { 4090 return posix_error(); 4091 } 4092 if (n == 0) { 4093 /* Avoid malloc(0) */ 4094 alt_grouplist = grouplist; 4095 } else { 4096 alt_grouplist = PyMem_Malloc(n * sizeof(gid_t)); 4097 if (alt_grouplist == NULL) { 4098 errno = EINVAL; 4099 return posix_error(); 4100 } 4101 n = getgroups(n, alt_grouplist); 4102 if (n == -1) { 4103 PyMem_Free(alt_grouplist); 4104 return posix_error(); 3896 4105 } 3897 4106 } 4107 } else { 4108 return posix_error(); 3898 4109 } 4110 } 4111 #endif 4112 4113 result = PyList_New(n); 4114 if (result != NULL) { 4115 int i; 4116 for (i = 0; i < n; ++i) { 4117 PyObject *o = _PyInt_FromGid(alt_grouplist[i]); 4118 if (o == NULL) { 4119 Py_DECREF(result); 4120 result = NULL; 4121 break; 4122 } 4123 PyList_SET_ITEM(result, i, o); 4124 } 4125 } 4126 4127 if (alt_grouplist != grouplist) { 4128 PyMem_Free(alt_grouplist); 4129 } 3899 4130 3900 4131 return result; 4132 } 4133 #endif 4134 4135 #ifdef HAVE_INITGROUPS 4136 PyDoc_STRVAR(posix_initgroups__doc__, 4137 "initgroups(username, gid) -> None\n\n\ 4138 Call the system initgroups() to initialize the group access list with all of\n\ 4139 the groups of which the specified username is a member, plus the specified\n\ 4140 group id."); 4141 4142 static PyObject * 4143 posix_initgroups(PyObject *self, PyObject *args) 4144 { 4145 char *username; 4146 #ifdef __APPLE__ 4147 int gid; 4148 #else 4149 gid_t gid; 4150 #endif 4151 4152 #ifdef __APPLE__ 4153 if (!PyArg_ParseTuple(args, "si:initgroups", &username, 4154 &gid)) 4155 #else 4156 if (!PyArg_ParseTuple(args, "sO&:initgroups", &username, 4157 _Py_Gid_Converter, &gid)) 4158 #endif 4159 return NULL; 4160 4161 if (initgroups(username, gid) == -1) 4162 return PyErr_SetFromErrno(PyExc_OSError); 4163 4164 Py_INCREF(Py_None); 4165 return Py_None; 3901 4166 } 3902 4167 #endif … … 3910 4175 posix_getpgid(PyObject *self, PyObject *args) 3911 4176 { 3912 3913 3914 3915 3916 3917 3918 4177 pid_t pid, pgid; 4178 if (!PyArg_ParseTuple(args, PARSE_PID ":getpgid", &pid)) 4179 return NULL; 4180 pgid = getpgid(pid); 4181 if (pgid < 0) 4182 return posix_error(); 4183 return PyLong_FromPid(pgid); 3919 4184 } 3920 4185 #endif /* HAVE_GETPGID */ … … 3930 4195 { 3931 4196 #ifdef GETPGRP_HAVE_ARG 3932 4197 return PyLong_FromPid(getpgrp(0)); 3933 4198 #else /* GETPGRP_HAVE_ARG */ 3934 4199 return PyLong_FromPid(getpgrp()); 3935 4200 #endif /* GETPGRP_HAVE_ARG */ 3936 4201 } … … 3941 4206 PyDoc_STRVAR(posix_setpgrp__doc__, 3942 4207 "setpgrp()\n\n\ 3943 Make this process a sessionleader.");4208 Make this process the process group leader."); 3944 4209 3945 4210 static PyObject * … … 3947 4212 { 3948 4213 #ifdef SETPGRP_HAVE_ARG 3949 4214 if (setpgrp(0, 0) < 0) 3950 4215 #else /* SETPGRP_HAVE_ARG */ 3951 4216 if (setpgrp() < 0) 3952 4217 #endif /* SETPGRP_HAVE_ARG */ 3953 3954 3955 4218 return posix_error(); 4219 Py_INCREF(Py_None); 4220 return Py_None; 3956 4221 } 3957 4222 … … 3966 4231 posix_getppid(PyObject *self, PyObject *noargs) 3967 4232 { 3968 4233 return PyLong_FromPid(getppid()); 3969 4234 } 3970 4235 #endif … … 3979 4244 posix_getlogin(PyObject *self, PyObject *noargs) 3980 4245 { 3981 PyObject *result = NULL; 3982 char *name; 3983 int old_errno = errno; 3984 3985 errno = 0; 3986 name = getlogin(); 3987 if (name == NULL) { 3988 if (errno) 3989 posix_error(); 3990 else 3991 PyErr_SetString(PyExc_OSError, 3992 "unable to determine login name"); 3993 } 4246 PyObject *result = NULL; 4247 char *name; 4248 int old_errno = errno; 4249 4250 errno = 0; 4251 name = getlogin(); 4252 if (name == NULL) { 4253 if (errno) 4254 posix_error(); 3994 4255 else 3995 result = PyString_FromString(name); 3996 errno = old_errno; 4256 PyErr_SetString(PyExc_OSError, 4257 "unable to determine login name"); 4258 } 4259 else 4260 result = PyString_FromString(name); 4261 errno = old_errno; 3997 4262 3998 4263 return result; … … 4008 4273 posix_getuid(PyObject *self, PyObject *noargs) 4009 4274 { 4010 return PyInt_FromLong((long)getuid());4275 return _PyInt_FromUid(getuid()); 4011 4276 } 4012 4277 #endif … … 4021 4286 posix_kill(PyObject *self, PyObject *args) 4022 4287 { 4023 4024 4025 4026 4288 pid_t pid; 4289 int sig; 4290 if (!PyArg_ParseTuple(args, PARSE_PID "i:kill", &pid, &sig)) 4291 return NULL; 4027 4292 #if defined(PYOS_OS2) && !defined(PYCC_GCC) 4028 4293 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) { … … 4039 4304 return NULL; /* Unrecognized Signal Requested */ 4040 4305 #else 4041 4042 4043 #endif 4044 4045 4306 if (kill(pid, sig) == -1) 4307 return posix_error(); 4308 #endif 4309 Py_INCREF(Py_None); 4310 return Py_None; 4046 4311 } 4047 4312 #endif … … 4055 4320 posix_killpg(PyObject *self, PyObject *args) 4056 4321 { 4057 int sig; 4058 pid_t pgid; 4059 /* XXX some man pages make the `pgid` parameter an int, others 4060 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should 4061 take the same type. Moreover, pid_t is always at least as wide as 4062 int (else compilation of this module fails), which is safe. */ 4063 if (!PyArg_ParseTuple(args, PARSE_PID "i:killpg", &pgid, &sig)) 4064 return NULL; 4065 if (killpg(pgid, sig) == -1) 4066 return posix_error(); 4067 Py_INCREF(Py_None); 4068 return Py_None; 4069 } 4070 #endif 4322 int sig; 4323 pid_t pgid; 4324 /* XXX some man pages make the `pgid` parameter an int, others 4325 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should 4326 take the same type. Moreover, pid_t is always at least as wide as 4327 int (else compilation of this module fails), which is safe. */ 4328 if (!PyArg_ParseTuple(args, PARSE_PID "i:killpg", &pgid, &sig)) 4329 return NULL; 4330 if (killpg(pgid, sig) == -1) 4331 return posix_error(); 4332 Py_INCREF(Py_None); 4333 return Py_None; 4334 } 4335 #endif 4336 4337 #ifdef MS_WINDOWS 4338 PyDoc_STRVAR(win32_kill__doc__, 4339 "kill(pid, sig)\n\n\ 4340 Kill a process with a signal."); 4341 4342 static PyObject * 4343 win32_kill(PyObject *self, PyObject *args) 4344 { 4345 PyObject *result; 4346 DWORD pid, sig, err; 4347 HANDLE handle; 4348 4349 if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig)) 4350 return NULL; 4351 4352 /* Console processes which share a common console can be sent CTRL+C or 4353 CTRL+BREAK events, provided they handle said events. */ 4354 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) { 4355 if (GenerateConsoleCtrlEvent(sig, pid) == 0) { 4356 err = GetLastError(); 4357 return PyErr_SetFromWindowsErr(err); 4358 } 4359 else 4360 Py_RETURN_NONE; 4361 } 4362 4363 /* If the signal is outside of what GenerateConsoleCtrlEvent can use, 4364 attempt to open and terminate the process. */ 4365 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); 4366 if (handle == NULL) { 4367 err = GetLastError(); 4368 return PyErr_SetFromWindowsErr(err); 4369 } 4370 4371 if (TerminateProcess(handle, sig) == 0) { 4372 err = GetLastError(); 4373 result = PyErr_SetFromWindowsErr(err); 4374 } else { 4375 Py_INCREF(Py_None); 4376 result = Py_None; 4377 } 4378 4379 CloseHandle(handle); 4380 return result; 4381 } 4382 4383 PyDoc_STRVAR(posix__isdir__doc__, 4384 "Return true if the pathname refers to an existing directory."); 4385 4386 static PyObject * 4387 posix__isdir(PyObject *self, PyObject *args) 4388 { 4389 PyObject *opath; 4390 char *path; 4391 PyUnicodeObject *po; 4392 DWORD attributes; 4393 4394 if (PyArg_ParseTuple(args, "U|:_isdir", &po)) { 4395 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po); 4396 4397 attributes = GetFileAttributesW(wpath); 4398 if (attributes == INVALID_FILE_ATTRIBUTES) 4399 Py_RETURN_FALSE; 4400 goto check; 4401 } 4402 /* Drop the argument parsing error as narrow strings 4403 are also valid. */ 4404 PyErr_Clear(); 4405 4406 if (!PyArg_ParseTuple(args, "et:_isdir", 4407 Py_FileSystemDefaultEncoding, &path)) 4408 return NULL; 4409 4410 attributes = GetFileAttributesA(path); 4411 PyMem_Free(path); 4412 if (attributes == INVALID_FILE_ATTRIBUTES) 4413 Py_RETURN_FALSE; 4414 4415 check: 4416 if (attributes & FILE_ATTRIBUTE_DIRECTORY) 4417 Py_RETURN_TRUE; 4418 else 4419 Py_RETURN_FALSE; 4420 } 4421 #endif /* MS_WINDOWS */ 4071 4422 4072 4423 #ifdef HAVE_PLOCK … … 4083 4434 posix_plock(PyObject *self, PyObject *args) 4084 4435 { 4085 4086 4087 4088 4089 4090 4091 4436 int op; 4437 if (!PyArg_ParseTuple(args, "i:plock", &op)) 4438 return NULL; 4439 if (plock(op) == -1) 4440 return posix_error(); 4441 Py_INCREF(Py_None); 4442 return Py_None; 4092 4443 } 4093 4444 #endif … … 4104 4455 async_system(const char *command) 4105 4456 { 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4457 char errormsg[256], args[1024]; 4458 RESULTCODES rcodes; 4459 APIRET rc; 4460 4461 char *shell = getenv("COMSPEC"); 4462 if (!shell) 4463 shell = "cmd"; 4464 4465 /* avoid overflowing the argument buffer */ 4466 if (strlen(shell) + 3 + strlen(command) >= 1024) 4467 return ERROR_NOT_ENOUGH_MEMORY 4468 4469 args[0] = '\0'; 4470 strcat(args, shell); 4471 strcat(args, "/c "); 4472 strcat(args, command); 4473 4474 /* execute asynchronously, inheriting the environment */ 4475 rc = DosExecPgm(errormsg, 4476 sizeof(errormsg), 4477 EXEC_ASYNC, 4478 args, 4479 NULL, 4480 &rcodes, 4481 shell); 4482 return rc; 4132 4483 } 4133 4484 … … 4135 4486 popen(const char *command, const char *mode, int pipesize, int *err) 4136 4487 { 4137 4138 4139 4140 4141 4142 4143 4144 4145 tgt_fd = 1;/* stdout */4146 4147 tgt_fd = 0;/* stdin */4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4488 int oldfd, tgtfd; 4489 HFILE pipeh[2]; 4490 APIRET rc; 4491 4492 /* mode determines which of stdin or stdout is reconnected to 4493 * the pipe to the child 4494 */ 4495 if (strchr(mode, 'r') != NULL) { 4496 tgt_fd = 1; /* stdout */ 4497 } else if (strchr(mode, 'w')) { 4498 tgt_fd = 0; /* stdin */ 4499 } else { 4500 *err = ERROR_INVALID_ACCESS; 4501 return NULL; 4502 } 4503 4504 /* setup the pipe */ 4505 if ((rc = DosCreatePipe(&pipeh[0], &pipeh[1], pipesize)) != NO_ERROR) { 4506 *err = rc; 4507 return NULL; 4508 } 4509 4510 /* prevent other threads accessing stdio */ 4511 DosEnterCritSec(); 4512 4513 /* reconnect stdio and execute child */ 4514 oldfd = dup(tgtfd); 4515 close(tgtfd); 4516 if (dup2(pipeh[tgtfd], tgtfd) == 0) { 4517 DosClose(pipeh[tgtfd]); 4518 rc = async_system(command); 4519 } 4520 4521 /* restore stdio */ 4522 dup2(oldfd, tgtfd); 4523 close(oldfd); 4524 4525 /* allow other threads access to stdio */ 4526 DosExitCritSec(); 4527 4528 /* if execution of child was successful return file stream */ 4529 if (rc == NO_ERROR) 4530 return fdopen(pipeh[1 - tgtfd], mode); 4531 else { 4532 DosClose(pipeh[1 - tgtfd]); 4533 *err = rc; 4534 return NULL; 4535 } 4185 4536 } 4186 4537 … … 4188 4539 posix_popen(PyObject *self, PyObject *args) 4189 4540 { 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4541 char *name; 4542 char *mode = "r"; 4543 int err, bufsize = -1; 4544 FILE *fp; 4545 PyObject *f; 4546 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) 4547 return NULL; 4548 Py_BEGIN_ALLOW_THREADS 4549 fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err); 4550 Py_END_ALLOW_THREADS 4551 if (fp == NULL) 4552 return os2_error(err); 4553 4554 f = PyFile_FromFile(fp, name, mode, fclose); 4555 if (f != NULL) 4556 PyFile_SetBufSize(f, bufsize); 4557 return f; 4207 4558 } 4208 4559 … … 4213 4564 posix_popen(PyObject *self, PyObject *args) 4214 4565 { 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4566 char *name; 4567 char *mode = "r"; 4568 int bufsize = -1; 4569 FILE *fp; 4570 PyObject *f; 4571 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) 4572 return NULL; 4573 Py_BEGIN_ALLOW_THREADS 4574 fp = popen(name, mode); 4575 Py_END_ALLOW_THREADS 4576 if (fp == NULL) 4577 return posix_error(); 4578 f = PyFile_FromFile(fp, name, mode, pclose); 4579 if (f != NULL) 4580 PyFile_SetBufSize(f, bufsize); 4581 return f; 4231 4582 } 4232 4583 … … 4262 4613 os2emx_popen2(PyObject *self, PyObject *args) 4263 4614 { 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4615 PyObject *f; 4616 int tm=0; 4617 4618 char *cmdstring; 4619 char *mode = "t"; 4620 int bufsize = -1; 4621 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize)) 4622 return NULL; 4623 4624 if (*mode == 't') 4625 tm = O_TEXT; 4626 else if (*mode != 'b') { 4627 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); 4628 return NULL; 4629 } else 4630 tm = O_BINARY; 4631 4632 f = _PyPopen(cmdstring, tm, POPEN_2, bufsize); 4633 4634 return f; 4284 4635 } 4285 4636 … … 4294 4645 os2emx_popen3(PyObject *self, PyObject *args) 4295 4646 { 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4647 PyObject *f; 4648 int tm = 0; 4649 4650 char *cmdstring; 4651 char *mode = "t"; 4652 int bufsize = -1; 4653 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize)) 4654 return NULL; 4655 4656 if (*mode == 't') 4657 tm = O_TEXT; 4658 else if (*mode != 'b') { 4659 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); 4660 return NULL; 4661 } else 4662 tm = O_BINARY; 4663 4664 f = _PyPopen(cmdstring, tm, POPEN_3, bufsize); 4665 4666 return f; 4316 4667 } 4317 4668 … … 4326 4677 os2emx_popen4(PyObject *self, PyObject *args) 4327 4678 { 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4679 PyObject *f; 4680 int tm = 0; 4681 4682 char *cmdstring; 4683 char *mode = "t"; 4684 int bufsize = -1; 4685 if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize)) 4686 return NULL; 4687 4688 if (*mode == 't') 4689 tm = O_TEXT; 4690 else if (*mode != 'b') { 4691 PyErr_SetString(PyExc_ValueError, "mode must be 't' or 'b'"); 4692 return NULL; 4693 } else 4694 tm = O_BINARY; 4695 4696 f = _PyPopen(cmdstring, tm, POPEN_4, bufsize); 4697 4698 return f; 4348 4699 } 4349 4700 … … 4353 4704 struct file_ref 4354 4705 { 4355 4356 4706 int handle; 4707 int flags; 4357 4708 }; 4358 4709 4359 4710 struct pipe_ref 4360 4711 { 4361 4362 4712 int rd; 4713 int wr; 4363 4714 }; 4364 4715 … … 4368 4719 _PyPopen(char *cmdstring, int mode, int n, int bufsize) 4369 4720 { 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4721 struct file_ref stdio[3]; 4722 struct pipe_ref p_fd[3]; 4723 FILE *p_s[3]; 4724 int file_count, i, pipe_err; 4725 pid_t pipe_pid; 4726 char *shell, *sh_name, *opt, *rd_mode, *wr_mode; 4727 PyObject *f, *p_f[3]; 4728 4729 /* file modes for subsequent fdopen's on pipe handles */ 4730 if (mode == O_TEXT) 4731 { 4732 rd_mode = "rt"; 4733 wr_mode = "wt"; 4734 } 4735 else 4736 { 4737 rd_mode = "rb"; 4738 wr_mode = "wb"; 4739 } 4740 4741 /* prepare shell references */ 4742 if ((shell = getenv("EMXSHELL")) == NULL) 4743 if ((shell = getenv("COMSPEC")) == NULL) 4744 { 4745 errno = ENOENT; 4746 return posix_error(); 4747 } 4748 4749 sh_name = _getname(shell); 4750 if (stricmp(sh_name, "cmd.exe") == 0 || stricmp(sh_name, "4os2.exe") == 0) 4751 opt = "/c"; 4752 else 4753 opt = "-c"; 4754 4755 /* save current stdio fds + their flags, and set not inheritable */ 4756 i = pipe_err = 0; 4757 while (pipe_err >= 0 && i < 3) 4758 { 4759 pipe_err = stdio[i].handle = dup(i); 4760 stdio[i].flags = fcntl(i, F_GETFD, 0); 4761 fcntl(stdio[i].handle, F_SETFD, stdio[i].flags | FD_CLOEXEC); 4762 i++; 4763 } 4764 if (pipe_err < 0) 4765 { 4766 /* didn't get them all saved - clean up and bail out */ 4767 int saved_err = errno; 4768 while (i-- > 0) 4769 { 4770 close(stdio[i].handle); 4771 } 4772 errno = saved_err; 4773 return posix_error(); 4774 } 4775 4776 /* create pipe ends */ 4777 file_count = 2; 4778 if (n == POPEN_3) 4779 file_count = 3; 4780 i = pipe_err = 0; 4781 while ((pipe_err == 0) && (i < file_count)) 4782 pipe_err = pipe((int *)&p_fd[i++]); 4783 if (pipe_err < 0) 4784 { 4785 /* didn't get them all made - clean up and bail out */ 4786 while (i-- > 0) 4787 { 4788 close(p_fd[i].wr); 4789 close(p_fd[i].rd); 4790 } 4791 errno = EPIPE; 4792 return posix_error(); 4793 } 4794 4795 /* change the actual standard IO streams over temporarily, 4796 * making the retained pipe ends non-inheritable 4797 */ 4798 pipe_err = 0; 4799 4800 /* - stdin */ 4801 if (dup2(p_fd[0].rd, 0) == 0) 4802 { 4803 close(p_fd[0].rd); 4804 i = fcntl(p_fd[0].wr, F_GETFD, 0); 4805 fcntl(p_fd[0].wr, F_SETFD, i | FD_CLOEXEC); 4806 if ((p_s[0] = fdopen(p_fd[0].wr, wr_mode)) == NULL) 4807 { 4808 close(p_fd[0].wr); 4809 pipe_err = -1; 4810 } 4811 } 4812 else 4813 { 4814 pipe_err = -1; 4815 } 4816 4817 /* - stdout */ 4818 if (pipe_err == 0) 4819 { 4820 if (dup2(p_fd[1].wr, 1) == 1) 4821 { 4822 close(p_fd[1].wr); 4823 i = fcntl(p_fd[1].rd, F_GETFD, 0); 4824 fcntl(p_fd[1].rd, F_SETFD, i | FD_CLOEXEC); 4825 if ((p_s[1] = fdopen(p_fd[1].rd, rd_mode)) == NULL) 4826 { 4827 close(p_fd[1].rd); 4828 pipe_err = -1; 4829 } 4830 } 4831 else 4832 { 4833 pipe_err = -1; 4834 } 4835 } 4836 4837 /* - stderr, as required */ 4838 if (pipe_err == 0) 4839 switch (n) 4840 { 4841 case POPEN_3: 4842 { 4843 if (dup2(p_fd[2].wr, 2) == 2) 4844 { 4845 close(p_fd[2].wr); 4846 i = fcntl(p_fd[2].rd, F_GETFD, 0); 4847 fcntl(p_fd[2].rd, F_SETFD, i | FD_CLOEXEC); 4848 if ((p_s[2] = fdopen(p_fd[2].rd, rd_mode)) == NULL) 4849 { 4850 close(p_fd[2].rd); 4851 pipe_err = -1; 4852 } 4853 } 4854 else 4855 { 4856 pipe_err = -1; 4857 } 4858 break; 4859 } 4860 4861 case POPEN_4: 4862 { 4863 if (dup2(1, 2) != 2) 4864 { 4865 pipe_err = -1; 4866 } 4867 break; 4868 } 4869 } 4870 4871 /* spawn the child process */ 4872 if (pipe_err == 0) 4873 { 4874 pipe_pid = spawnlp(P_NOWAIT, shell, shell, opt, cmdstring, (char *)0); 4875 if (pipe_pid == -1) 4876 { 4877 pipe_err = -1; 4878 } 4879 else 4880 { 4881 /* save the PID into the FILE structure 4882 * NOTE: this implementation doesn't actually 4883 * take advantage of this, but do it for 4884 * completeness - AIM Apr01 4885 */ 4886 for (i = 0; i < file_count; i++) 4887 p_s[i]->_pid = pipe_pid; 4888 } 4889 } 4890 4891 /* reset standard IO to normal */ 4892 for (i = 0; i < 3; i++) 4893 { 4894 dup2(stdio[i].handle, i); 4895 fcntl(i, F_SETFD, stdio[i].flags); 4896 close(stdio[i].handle); 4897 } 4898 4899 /* if any remnant problems, clean up and bail out */ 4900 if (pipe_err < 0) 4901 { 4902 for (i = 0; i < 3; i++) 4903 { 4904 close(p_fd[i].rd); 4905 close(p_fd[i].wr); 4906 } 4907 errno = EPIPE; 4908 return posix_error_with_filename(cmdstring); 4909 } 4910 4911 /* build tuple of file objects to return */ 4912 if ((p_f[0] = PyFile_FromFile(p_s[0], cmdstring, wr_mode, _PyPclose)) != NULL) 4913 PyFile_SetBufSize(p_f[0], bufsize); 4914 if ((p_f[1] = PyFile_FromFile(p_s[1], cmdstring, rd_mode, _PyPclose)) != NULL) 4915 PyFile_SetBufSize(p_f[1], bufsize); 4916 if (n == POPEN_3) 4917 { 4918 if ((p_f[2] = PyFile_FromFile(p_s[2], cmdstring, rd_mode, _PyPclose)) != NULL) 4919 PyFile_SetBufSize(p_f[0], bufsize); 4920 f = PyTuple_Pack(3, p_f[0], p_f[1], p_f[2]); 4921 } 4922 else 4923 f = PyTuple_Pack(2, p_f[0], p_f[1]); 4924 4925 /* 4926 * Insert the files we've created into the process dictionary 4927 * all referencing the list with the process handle and the 4928 * initial number of files (see description below in _PyPclose). 4929 * Since if _PyPclose later tried to wait on a process when all 4930 * handles weren't closed, it could create a deadlock with the 4931 * child, we spend some energy here to try to ensure that we 4932 * either insert all file handles into the dictionary or none 4933 * at all. It's a little clumsy with the various popen modes 4934 * and variable number of files involved. 4935 */ 4936 if (!_PyPopenProcs) 4937 { 4938 _PyPopenProcs = PyDict_New(); 4939 } 4940 4941 if (_PyPopenProcs) 4942 { 4943 PyObject *procObj, *pidObj, *intObj, *fileObj[3]; 4944 int ins_rc[3]; 4945 4946 fileObj[0] = fileObj[1] = fileObj[2] = NULL; 4947 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0; 4948 4949 procObj = PyList_New(2); 4950 pidObj = PyLong_FromPid(pipe_pid); 4951 intObj = PyInt_FromLong((long) file_count); 4952 4953 if (procObj && pidObj && intObj) 4954 { 4955 PyList_SetItem(procObj, 0, pidObj); 4956 PyList_SetItem(procObj, 1, intObj); 4957 4958 fileObj[0] = PyLong_FromVoidPtr(p_s[0]); 4959 if (fileObj[0]) 4960 { 4961 ins_rc[0] = PyDict_SetItem(_PyPopenProcs, 4962 fileObj[0], 4963 procObj); 4964 } 4965 fileObj[1] = PyLong_FromVoidPtr(p_s[1]); 4966 if (fileObj[1]) 4967 { 4968 ins_rc[1] = PyDict_SetItem(_PyPopenProcs, 4969 fileObj[1], 4970 procObj); 4971 } 4972 if (file_count >= 3) 4973 { 4974 fileObj[2] = PyLong_FromVoidPtr(p_s[2]); 4975 if (fileObj[2]) 4976 { 4977 ins_rc[2] = PyDict_SetItem(_PyPopenProcs, 4978 fileObj[2], 4979 procObj); 4980 } 4981 } 4982 4983 if (ins_rc[0] < 0 || !fileObj[0] || 4984 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) || 4985 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) 4986 { 4987 /* Something failed - remove any dictionary 4988 * entries that did make it. 4989 */ 4990 if (!ins_rc[0] && fileObj[0]) 4991 { 4992 PyDict_DelItem(_PyPopenProcs, 4993 fileObj[0]); 4994 } 4995 if (!ins_rc[1] && fileObj[1]) 4996 { 4997 PyDict_DelItem(_PyPopenProcs, 4998 fileObj[1]); 4999 } 5000 if (!ins_rc[2] && fileObj[2]) 5001 { 5002 PyDict_DelItem(_PyPopenProcs, 5003 fileObj[2]); 5004 } 5005 } 5006 } 5007 5008 /* 5009 * Clean up our localized references for the dictionary keys 5010 * and value since PyDict_SetItem will Py_INCREF any copies 5011 * that got placed in the dictionary. 5012 */ 5013 Py_XDECREF(procObj); 5014 Py_XDECREF(fileObj[0]); 5015 Py_XDECREF(fileObj[1]); 5016 Py_XDECREF(fileObj[2]); 5017 } 5018 5019 /* Child is launched. */ 5020 return f; 4670 5021 } 4671 5022 … … 4698 5049 static int _PyPclose(FILE *file) 4699 5050 { 4700 4701 4702 4703 4704 5051 int result; 5052 int exit_code; 5053 pid_t pipe_pid; 5054 PyObject *procObj, *pidObj, *intObj, *fileObj; 5055 int file_count; 4705 5056 #ifdef WITH_THREAD 4706 4707 #endif 4708 4709 4710 4711 4712 5057 PyGILState_STATE state; 5058 #endif 5059 5060 /* Close the file handle first, to ensure it can't block the 5061 * child from exiting if it's the last handle. 5062 */ 5063 result = fclose(file); 4713 5064 4714 5065 #ifdef WITH_THREAD 4715 4716 #endif 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 5066 state = PyGILState_Ensure(); 5067 #endif 5068 if (_PyPopenProcs) 5069 { 5070 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL && 5071 (procObj = PyDict_GetItem(_PyPopenProcs, 5072 fileObj)) != NULL && 5073 (pidObj = PyList_GetItem(procObj,0)) != NULL && 5074 (intObj = PyList_GetItem(procObj,1)) != NULL) 5075 { 5076 pipe_pid = (pid_t) PyLong_AsPid(pidObj); 5077 file_count = (int) PyInt_AsLong(intObj); 5078 5079 if (file_count > 1) 5080 { 5081 /* Still other files referencing process */ 5082 file_count--; 5083 PyList_SetItem(procObj,1, 5084 PyInt_FromLong((long) file_count)); 5085 } 5086 else 5087 { 5088 /* Last file for this process */ 5089 if (result != EOF && 5090 waitpid(pipe_pid, &exit_code, 0) == pipe_pid) 5091 { 5092 /* extract exit status */ 5093 if (WIFEXITED(exit_code)) 5094 { 5095 result = WEXITSTATUS(exit_code); 5096 } 5097 else 5098 { 5099 errno = EPIPE; 5100 result = -1; 5101 } 5102 } 5103 else 5104 { 5105 /* Indicate failure - this will cause the file object 5106 * to raise an I/O error and translate the last 5107 * error code from errno. We do have a problem with 5108 * last errors that overlap the normal errno table, 5109 * but that's a consistent problem with the file object. 5110 */ 5111 result = -1; 5112 } 5113 } 5114 5115 /* Remove this file pointer from dictionary */ 5116 PyDict_DelItem(_PyPopenProcs, fileObj); 5117 5118 if (PyDict_Size(_PyPopenProcs) == 0) 5119 { 5120 Py_DECREF(_PyPopenProcs); 5121 _PyPopenProcs = NULL; 5122 } 5123 5124 } /* if object retrieval ok */ 5125 5126 Py_XDECREF(fileObj); 5127 } /* if _PyPopenProcs */ 4777 5128 4778 5129 #ifdef WITH_THREAD 4779 4780 #endif 4781 5130 PyGILState_Release(state); 5131 #endif 5132 return result; 4782 5133 } 4783 5134 … … 4824 5175 posix_popen(PyObject *self, PyObject *args) 4825 5176 { 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 5177 PyObject *f; 5178 int tm = 0; 5179 5180 char *cmdstring; 5181 char *mode = "r"; 5182 int bufsize = -1; 5183 if (!PyArg_ParseTuple(args, "s|si:popen", &cmdstring, &mode, &bufsize)) 5184 return NULL; 5185 5186 if (*mode == 'r') 5187 tm = _O_RDONLY; 5188 else if (*mode != 'w') { 5189 PyErr_SetString(PyExc_ValueError, "popen() arg 2 must be 'r' or 'w'"); 5190 return NULL; 5191 } else 5192 tm = _O_WRONLY; 5193 5194 if (bufsize != -1) { 5195 PyErr_SetString(PyExc_ValueError, "popen() arg 3 must be -1"); 5196 return NULL; 5197 } 5198 5199 if (*(mode+1) == 't') 5200 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1); 5201 else if (*(mode+1) == 'b') 5202 f = _PyPopen(cmdstring, tm | _O_BINARY, POPEN_1); 5203 else 5204 f = _PyPopen(cmdstring, tm | _O_TEXT, POPEN_1); 5205 5206 return f; 4856 5207 } 4857 5208 … … 4865 5216 win32_popen2(PyObject *self, PyObject *args) 4866 5217 { 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 5218 PyObject *f; 5219 int tm=0; 5220 5221 char *cmdstring; 5222 char *mode = "t"; 5223 int bufsize = -1; 5224 if (!PyArg_ParseTuple(args, "s|si:popen2", &cmdstring, &mode, &bufsize)) 5225 return NULL; 5226 5227 if (*mode == 't') 5228 tm = _O_TEXT; 5229 else if (*mode != 'b') { 5230 PyErr_SetString(PyExc_ValueError, "popen2() arg 2 must be 't' or 'b'"); 5231 return NULL; 5232 } else 5233 tm = _O_BINARY; 5234 5235 if (bufsize != -1) { 5236 PyErr_SetString(PyExc_ValueError, "popen2() arg 3 must be -1"); 5237 return NULL; 5238 } 5239 5240 f = _PyPopen(cmdstring, tm, POPEN_2); 5241 5242 return f; 4892 5243 } 4893 5244 … … 4902 5253 win32_popen3(PyObject *self, PyObject *args) 4903 5254 { 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 5255 PyObject *f; 5256 int tm = 0; 5257 5258 char *cmdstring; 5259 char *mode = "t"; 5260 int bufsize = -1; 5261 if (!PyArg_ParseTuple(args, "s|si:popen3", &cmdstring, &mode, &bufsize)) 5262 return NULL; 5263 5264 if (*mode == 't') 5265 tm = _O_TEXT; 5266 else if (*mode != 'b') { 5267 PyErr_SetString(PyExc_ValueError, "popen3() arg 2 must be 't' or 'b'"); 5268 return NULL; 5269 } else 5270 tm = _O_BINARY; 5271 5272 if (bufsize != -1) { 5273 PyErr_SetString(PyExc_ValueError, "popen3() arg 3 must be -1"); 5274 return NULL; 5275 } 5276 5277 f = _PyPopen(cmdstring, tm, POPEN_3); 5278 5279 return f; 4929 5280 } 4930 5281 … … 4939 5290 win32_popen4(PyObject *self, PyObject *args) 4940 5291 { 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 5292 PyObject *f; 5293 int tm = 0; 5294 5295 char *cmdstring; 5296 char *mode = "t"; 5297 int bufsize = -1; 5298 if (!PyArg_ParseTuple(args, "s|si:popen4", &cmdstring, &mode, &bufsize)) 5299 return NULL; 5300 5301 if (*mode == 't') 5302 tm = _O_TEXT; 5303 else if (*mode != 'b') { 5304 PyErr_SetString(PyExc_ValueError, "popen4() arg 2 must be 't' or 'b'"); 5305 return NULL; 5306 } else 5307 tm = _O_BINARY; 5308 5309 if (bufsize != -1) { 5310 PyErr_SetString(PyExc_ValueError, "popen4() arg 3 must be -1"); 5311 return NULL; 5312 } 5313 5314 f = _PyPopen(cmdstring, tm, POPEN_4); 5315 5316 return f; 4966 5317 } 4967 5318 4968 5319 static BOOL 4969 5320 _PyPopenCreateProcess(char *cmdstring, 4970 4971 4972 4973 4974 { 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5321 HANDLE hStdin, 5322 HANDLE hStdout, 5323 HANDLE hStderr, 5324 HANDLE *hProcess) 5325 { 5326 PROCESS_INFORMATION piProcInfo; 5327 STARTUPINFO siStartInfo; 5328 DWORD dwProcessFlags = 0; /* no NEW_CONSOLE by default for Ctrl+C handling */ 5329 char *s1,*s2, *s3 = " /c "; 5330 const char *szConsoleSpawn = "w9xpopen.exe"; 5331 int i; 5332 Py_ssize_t x; 5333 5334 if (i = GetEnvironmentVariable("COMSPEC",NULL,0)) { 5335 char *comshell; 5336 5337 s1 = (char *)alloca(i); 5338 if (!(x = GetEnvironmentVariable("COMSPEC", s1, i))) 5339 /* x < i, so x fits into an integer */ 5340 return (int)x; 5341 5342 /* Explicitly check if we are using COMMAND.COM. If we are 5343 * then use the w9xpopen hack. 5344 */ 5345 comshell = s1 + x; 5346 while (comshell >= s1 && *comshell != '\\') 5347 --comshell; 5348 ++comshell; 5349 5350 if (GetVersion() < 0x80000000 && 5351 _stricmp(comshell, "command.com") != 0) { 5352 /* NT/2000 and not using command.com. */ 5353 x = i + strlen(s3) + strlen(cmdstring) + 1; 5354 s2 = (char *)alloca(x); 5355 ZeroMemory(s2, x); 5356 PyOS_snprintf(s2, x, "%s%s%s", s1, s3, cmdstring); 5357 } 5358 else { 5359 /* 5360 * Oh gag, we're on Win9x or using COMMAND.COM. Use 5361 * the workaround listed in KB: Q150956 5362 */ 5363 char modulepath[_MAX_PATH]; 5364 struct stat statinfo; 5365 GetModuleFileName(NULL, modulepath, sizeof(modulepath)); 5366 for (x = i = 0; modulepath[i]; i++) 5367 if (modulepath[i] == SEP) 5368 x = i+1; 5369 modulepath[x] = '\0'; 5370 /* Create the full-name to w9xpopen, so we can test it exists */ 5371 strncat(modulepath, 5372 szConsoleSpawn, 5373 (sizeof(modulepath)/sizeof(modulepath[0])) 5374 -strlen(modulepath)); 5375 if (stat(modulepath, &statinfo) != 0) { 5376 size_t mplen = sizeof(modulepath)/sizeof(modulepath[0]); 5377 /* Eeek - file-not-found - possibly an embedding 5378 situation - see if we can locate it in sys.prefix 5379 */ 5380 strncpy(modulepath, 5381 Py_GetExecPrefix(), 5382 mplen); 5383 modulepath[mplen-1] = '\0'; 5384 if (modulepath[strlen(modulepath)-1] != '\\') 5385 strcat(modulepath, "\\"); 5386 strncat(modulepath, 5387 szConsoleSpawn, 5388 mplen-strlen(modulepath)); 5389 /* No where else to look - raise an easily identifiable 5390 error, rather than leaving Windows to report 5391 "file not found" - as the user is probably blissfully 5392 unaware this shim EXE is used, and it will confuse them. 5393 (well, it confused me for a while ;-) 5394 */ 5395 if (stat(modulepath, &statinfo) != 0) { 5396 PyErr_Format(PyExc_RuntimeError, 5397 "Can not locate '%s' which is needed " 5398 "for popen to work with your shell " 5399 "or platform.", 5400 szConsoleSpawn); 5401 return FALSE; 5402 } 5403 } 5404 x = i + strlen(s3) + strlen(cmdstring) + 1 + 5405 strlen(modulepath) + 5406 strlen(szConsoleSpawn) + 1; 5407 5408 s2 = (char *)alloca(x); 5409 ZeroMemory(s2, x); 5410 /* To maintain correct argument passing semantics, 5411 we pass the command-line as it stands, and allow 5412 quoting to be applied. w9xpopen.exe will then 5413 use its argv vector, and re-quote the necessary 5414 args for the ultimate child process. 5415 */ 5416 PyOS_snprintf( 5417 s2, x, 5418 "\"%s\" %s%s%s", 5419 modulepath, 5420 s1, 5421 s3, 5422 cmdstring); 5423 /* Not passing CREATE_NEW_CONSOLE has been known to 5424 cause random failures on win9x. Specifically a 5425 dialog: 5426 "Your program accessed mem currently in use at xxx" 5427 and a hopeful warning about the stability of your 5428 system. 5429 Cost is Ctrl+C won't kill children, but anyone 5430 who cares can have a go! 5431 */ 5432 dwProcessFlags |= CREATE_NEW_CONSOLE; 5433 } 5434 } 5435 5436 /* Could be an else here to try cmd.exe / command.com in the path 5437 Now we'll just error out.. */ 5438 else { 5439 PyErr_SetString(PyExc_RuntimeError, 5440 "Cannot locate a COMSPEC environment variable to " 5441 "use as the shell"); 5442 return FALSE; 5443 } 5444 5445 ZeroMemory(&siStartInfo, sizeof(STARTUPINFO)); 5446 siStartInfo.cb = sizeof(STARTUPINFO); 5447 siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; 5448 siStartInfo.hStdInput = hStdin; 5449 siStartInfo.hStdOutput = hStdout; 5450 siStartInfo.hStdError = hStderr; 5451 siStartInfo.wShowWindow = SW_HIDE; 5452 5453 if (CreateProcess(NULL, 5454 s2, 5455 NULL, 5456 NULL, 5457 TRUE, 5458 dwProcessFlags, 5459 NULL, 5460 NULL, 5461 &siStartInfo, 5462 &piProcInfo) ) { 5463 /* Close the handles now so anyone waiting is woken. */ 5464 CloseHandle(piProcInfo.hThread); 5465 5466 /* Return process handle */ 5467 *hProcess = piProcInfo.hProcess; 5468 return TRUE; 5469 } 5470 win32_error("CreateProcess", s2); 5471 return FALSE; 5121 5472 } 5122 5473 … … 5126 5477 _PyPopen(char *cmdstring, int mode, int n) 5127 5478 { 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5479 HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr, 5480 hChildStderrRd, hChildStderrWr, hChildStdinWrDup, hChildStdoutRdDup, 5481 hChildStderrRdDup, hProcess; /* hChildStdoutWrDup; */ 5482 5483 SECURITY_ATTRIBUTES saAttr; 5484 BOOL fSuccess; 5485 int fd1, fd2, fd3; 5486 FILE *f1, *f2, *f3; 5487 long file_count; 5488 PyObject *f; 5489 5490 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); 5491 saAttr.bInheritHandle = TRUE; 5492 saAttr.lpSecurityDescriptor = NULL; 5493 5494 if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0)) 5495 return win32_error("CreatePipe", NULL); 5496 5497 /* Create new output read handle and the input write handle. Set 5498 * the inheritance properties to FALSE. Otherwise, the child inherits 5499 * these handles; resulting in non-closeable handles to the pipes 5500 * being created. */ 5501 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr, 5502 GetCurrentProcess(), &hChildStdinWrDup, 0, 5503 FALSE, 5504 DUPLICATE_SAME_ACCESS); 5505 if (!fSuccess) 5506 return win32_error("DuplicateHandle", NULL); 5507 5508 /* Close the inheritable version of ChildStdin 5509 that we're using. */ 5510 CloseHandle(hChildStdinWr); 5511 5512 if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0)) 5513 return win32_error("CreatePipe", NULL); 5514 5515 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd, 5516 GetCurrentProcess(), &hChildStdoutRdDup, 0, 5517 FALSE, DUPLICATE_SAME_ACCESS); 5518 if (!fSuccess) 5519 return win32_error("DuplicateHandle", NULL); 5520 5521 /* Close the inheritable version of ChildStdout 5522 that we're using. */ 5523 CloseHandle(hChildStdoutRd); 5524 5525 if (n != POPEN_4) { 5526 if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0)) 5527 return win32_error("CreatePipe", NULL); 5528 fSuccess = DuplicateHandle(GetCurrentProcess(), 5529 hChildStderrRd, 5530 GetCurrentProcess(), 5531 &hChildStderrRdDup, 0, 5532 FALSE, DUPLICATE_SAME_ACCESS); 5533 if (!fSuccess) 5534 return win32_error("DuplicateHandle", NULL); 5535 /* Close the inheritable version of ChildStdErr that we're using. */ 5536 CloseHandle(hChildStderrRd); 5537 } 5538 5539 switch (n) { 5540 case POPEN_1: 5541 switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY)) { 5542 case _O_WRONLY | _O_TEXT: 5543 /* Case for writing to child Stdin in text mode. */ 5544 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode); 5545 f1 = _fdopen(fd1, "w"); 5546 f = PyFile_FromFile(f1, cmdstring, "w", _PyPclose); 5547 PyFile_SetBufSize(f, 0); 5548 /* We don't care about these pipes anymore, so close them. */ 5549 CloseHandle(hChildStdoutRdDup); 5550 CloseHandle(hChildStderrRdDup); 5551 break; 5552 5553 case _O_RDONLY | _O_TEXT: 5554 /* Case for reading from child Stdout in text mode. */ 5555 fd1 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode); 5556 f1 = _fdopen(fd1, "r"); 5557 f = PyFile_FromFile(f1, cmdstring, "r", _PyPclose); 5558 PyFile_SetBufSize(f, 0); 5559 /* We don't care about these pipes anymore, so close them. */ 5560 CloseHandle(hChildStdinWrDup); 5561 CloseHandle(hChildStderrRdDup); 5562 break; 5563 5564 case _O_RDONLY | _O_BINARY: 5565 /* Case for readinig from child Stdout in binary mode. */ 5566 fd1 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode); 5567 f1 = _fdopen(fd1, "rb"); 5568 f = PyFile_FromFile(f1, cmdstring, "rb", _PyPclose); 5569 PyFile_SetBufSize(f, 0); 5570 /* We don't care about these pipes anymore, so close them. */ 5571 CloseHandle(hChildStdinWrDup); 5572 CloseHandle(hChildStderrRdDup); 5573 break; 5574 5575 case _O_WRONLY | _O_BINARY: 5576 /* Case for writing to child Stdin in binary mode. */ 5577 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode); 5578 f1 = _fdopen(fd1, "wb"); 5579 f = PyFile_FromFile(f1, cmdstring, "wb", _PyPclose); 5580 PyFile_SetBufSize(f, 0); 5581 /* We don't care about these pipes anymore, so close them. */ 5582 CloseHandle(hChildStdoutRdDup); 5583 CloseHandle(hChildStderrRdDup); 5584 break; 5585 } 5586 file_count = 1; 5587 break; 5588 5589 case POPEN_2: 5590 case POPEN_4: 5591 { 5592 char *m1, *m2; 5593 PyObject *p1, *p2; 5594 5595 if (mode & _O_TEXT) { 5596 m1 = "r"; 5597 m2 = "w"; 5598 } else { 5599 m1 = "rb"; 5600 m2 = "wb"; 5601 } 5602 5603 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode); 5604 f1 = _fdopen(fd1, m2); 5605 fd2 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode); 5606 f2 = _fdopen(fd2, m1); 5607 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose); 5608 PyFile_SetBufSize(p1, 0); 5609 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose); 5610 PyFile_SetBufSize(p2, 0); 5611 5612 if (n != 4) 5613 CloseHandle(hChildStderrRdDup); 5614 5615 f = PyTuple_Pack(2,p1,p2); 5616 Py_XDECREF(p1); 5617 Py_XDECREF(p2); 5618 file_count = 2; 5619 break; 5620 } 5621 5622 case POPEN_3: 5623 { 5624 char *m1, *m2; 5625 PyObject *p1, *p2, *p3; 5626 5627 if (mode & _O_TEXT) { 5628 m1 = "r"; 5629 m2 = "w"; 5630 } else { 5631 m1 = "rb"; 5632 m2 = "wb"; 5633 } 5634 5635 fd1 = _open_osfhandle((Py_intptr_t)hChildStdinWrDup, mode); 5636 f1 = _fdopen(fd1, m2); 5637 fd2 = _open_osfhandle((Py_intptr_t)hChildStdoutRdDup, mode); 5638 f2 = _fdopen(fd2, m1); 5639 fd3 = _open_osfhandle((Py_intptr_t)hChildStderrRdDup, mode); 5640 f3 = _fdopen(fd3, m1); 5641 p1 = PyFile_FromFile(f1, cmdstring, m2, _PyPclose); 5642 p2 = PyFile_FromFile(f2, cmdstring, m1, _PyPclose); 5643 p3 = PyFile_FromFile(f3, cmdstring, m1, _PyPclose); 5644 PyFile_SetBufSize(p1, 0); 5645 PyFile_SetBufSize(p2, 0); 5646 PyFile_SetBufSize(p3, 0); 5647 f = PyTuple_Pack(3,p1,p2,p3); 5648 Py_XDECREF(p1); 5649 Py_XDECREF(p2); 5650 Py_XDECREF(p3); 5651 file_count = 3; 5652 break; 5653 } 5654 } 5655 5656 if (n == POPEN_4) { 5657 if (!_PyPopenCreateProcess(cmdstring, 5658 hChildStdinRd, 5659 hChildStdoutWr, 5660 hChildStdoutWr, 5661 &hProcess)) 5662 return NULL; 5663 } 5664 else { 5665 if (!_PyPopenCreateProcess(cmdstring, 5666 hChildStdinRd, 5667 hChildStdoutWr, 5668 hChildStderrWr, 5669 &hProcess)) 5670 return NULL; 5671 } 5672 5673 /* 5674 * Insert the files we've created into the process dictionary 5675 * all referencing the list with the process handle and the 5676 * initial number of files (see description below in _PyPclose). 5677 * Since if _PyPclose later tried to wait on a process when all 5678 * handles weren't closed, it could create a deadlock with the 5679 * child, we spend some energy here to try to ensure that we 5680 * either insert all file handles into the dictionary or none 5681 * at all. It's a little clumsy with the various popen modes 5682 * and variable number of files involved. 5683 */ 5684 if (!_PyPopenProcs) { 5685 _PyPopenProcs = PyDict_New(); 5686 } 5687 5688 if (_PyPopenProcs) { 5689 PyObject *procObj, *hProcessObj, *intObj, *fileObj[3]; 5690 int ins_rc[3]; 5691 5692 fileObj[0] = fileObj[1] = fileObj[2] = NULL; 5693 ins_rc[0] = ins_rc[1] = ins_rc[2] = 0; 5694 5695 procObj = PyList_New(2); 5696 hProcessObj = PyLong_FromVoidPtr(hProcess); 5697 intObj = PyInt_FromLong(file_count); 5698 5699 if (procObj && hProcessObj && intObj) { 5700 PyList_SetItem(procObj,0,hProcessObj); 5701 PyList_SetItem(procObj,1,intObj); 5702 5703 fileObj[0] = PyLong_FromVoidPtr(f1); 5704 if (fileObj[0]) { 5705 ins_rc[0] = PyDict_SetItem(_PyPopenProcs, 5706 fileObj[0], 5707 procObj); 5708 } 5709 if (file_count >= 2) { 5710 fileObj[1] = PyLong_FromVoidPtr(f2); 5711 if (fileObj[1]) { 5712 ins_rc[1] = PyDict_SetItem(_PyPopenProcs, 5713 fileObj[1], 5714 procObj); 5715 } 5716 } 5717 if (file_count >= 3) { 5718 fileObj[2] = PyLong_FromVoidPtr(f3); 5719 if (fileObj[2]) { 5720 ins_rc[2] = PyDict_SetItem(_PyPopenProcs, 5721 fileObj[2], 5722 procObj); 5723 } 5724 } 5725 5726 if (ins_rc[0] < 0 || !fileObj[0] || 5727 ins_rc[1] < 0 || (file_count > 1 && !fileObj[1]) || 5728 ins_rc[2] < 0 || (file_count > 2 && !fileObj[2])) { 5729 /* Something failed - remove any dictionary 5730 * entries that did make it. 5731 */ 5732 if (!ins_rc[0] && fileObj[0]) { 5733 PyDict_DelItem(_PyPopenProcs, 5734 fileObj[0]); 5735 } 5736 if (!ins_rc[1] && fileObj[1]) { 5737 PyDict_DelItem(_PyPopenProcs, 5738 fileObj[1]); 5739 } 5740 if (!ins_rc[2] && fileObj[2]) { 5741 PyDict_DelItem(_PyPopenProcs, 5742 fileObj[2]); 5743 } 5744 } 5745 } 5746 5747 /* 5748 * Clean up our localized references for the dictionary keys 5749 * and value since PyDict_SetItem will Py_INCREF any copies 5750 * that got placed in the dictionary. 5751 */ 5752 Py_XDECREF(procObj); 5753 Py_XDECREF(fileObj[0]); 5754 Py_XDECREF(fileObj[1]); 5755 Py_XDECREF(fileObj[2]); 5756 } 5757 5758 /* Child is launched. Close the parents copy of those pipe 5759 * handles that only the child should have open. You need to 5760 * make sure that no handles to the write end of the output pipe 5761 * are maintained in this process or else the pipe will not close 5762 * when the child process exits and the ReadFile will hang. */ 5763 5764 if (!CloseHandle(hChildStdinRd)) 5765 return win32_error("CloseHandle", NULL); 5766 5767 if (!CloseHandle(hChildStdoutWr)) 5768 return win32_error("CloseHandle", NULL); 5769 5770 if ((n != 4) && (!CloseHandle(hChildStderrWr))) 5771 return win32_error("CloseHandle", NULL); 5772 5773 return f; 5423 5774 } 5424 5775 … … 5451 5802 static int _PyPclose(FILE *file) 5452 5803 { 5453 5454 5455 5456 5457 5804 int result; 5805 DWORD exit_code; 5806 HANDLE hProcess; 5807 PyObject *procObj, *hProcessObj, *intObj, *fileObj; 5808 long file_count; 5458 5809 #ifdef WITH_THREAD 5459 5460 #endif 5461 5462 5463 5464 5465 5810 PyGILState_STATE state; 5811 #endif 5812 5813 /* Close the file handle first, to ensure it can't block the 5814 * child from exiting if it's the last handle. 5815 */ 5816 result = fclose(file); 5466 5817 #ifdef WITH_THREAD 5467 5468 #endif 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5818 state = PyGILState_Ensure(); 5819 #endif 5820 if (_PyPopenProcs) { 5821 if ((fileObj = PyLong_FromVoidPtr(file)) != NULL && 5822 (procObj = PyDict_GetItem(_PyPopenProcs, 5823 fileObj)) != NULL && 5824 (hProcessObj = PyList_GetItem(procObj,0)) != NULL && 5825 (intObj = PyList_GetItem(procObj,1)) != NULL) { 5826 5827 hProcess = PyLong_AsVoidPtr(hProcessObj); 5828 file_count = PyInt_AsLong(intObj); 5829 5830 if (file_count > 1) { 5831 /* Still other files referencing process */ 5832 file_count--; 5833 PyList_SetItem(procObj,1, 5834 PyInt_FromLong(file_count)); 5835 } else { 5836 /* Last file for this process */ 5837 if (result != EOF && 5838 WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED && 5839 GetExitCodeProcess(hProcess, &exit_code)) { 5840 /* Possible truncation here in 16-bit environments, but 5841 * real exit codes are just the lower byte in any event. 5842 */ 5843 result = exit_code; 5844 } else { 5845 /* Indicate failure - this will cause the file object 5846 * to raise an I/O error and translate the last Win32 5847 * error code from errno. We do have a problem with 5848 * last errors that overlap the normal errno table, 5849 * but that's a consistent problem with the file object. 5850 */ 5851 if (result != EOF) { 5852 /* If the error wasn't from the fclose(), then 5853 * set errno for the file object error handling. 5854 */ 5855 errno = GetLastError(); 5856 } 5857 result = -1; 5858 } 5859 5860 /* Free up the native handle at this point */ 5861 CloseHandle(hProcess); 5862 } 5863 5864 /* Remove this file pointer from dictionary */ 5865 PyDict_DelItem(_PyPopenProcs, fileObj); 5866 5867 if (PyDict_Size(_PyPopenProcs) == 0) { 5868 Py_DECREF(_PyPopenProcs); 5869 _PyPopenProcs = NULL; 5870 } 5871 5872 } /* if object retrieval ok */ 5873 5874 Py_XDECREF(fileObj); 5875 } /* if _PyPopenProcs */ 5525 5876 5526 5877 #ifdef WITH_THREAD 5527 5528 #endif 5529 5878 PyGILState_Release(state); 5879 #endif 5880 return result; 5530 5881 } 5531 5882 … … 5534 5885 posix_popen(PyObject *self, PyObject *args) 5535 5886 { 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5887 char *name; 5888 char *mode = "r"; 5889 int bufsize = -1; 5890 FILE *fp; 5891 PyObject *f; 5892 if (!PyArg_ParseTuple(args, "s|si:popen", &name, &mode, &bufsize)) 5893 return NULL; 5894 /* Strip mode of binary or text modifiers */ 5895 if (strcmp(mode, "rb") == 0 || strcmp(mode, "rt") == 0) 5896 mode = "r"; 5897 else if (strcmp(mode, "wb") == 0 || strcmp(mode, "wt") == 0) 5898 mode = "w"; 5899 Py_BEGIN_ALLOW_THREADS 5900 fp = popen(name, mode); 5901 Py_END_ALLOW_THREADS 5902 if (fp == NULL) 5903 return posix_error(); 5904 f = PyFile_FromFile(fp, name, mode, pclose); 5905 if (f != NULL) 5906 PyFile_SetBufSize(f, bufsize); 5907 return f; 5557 5908 } 5558 5909 … … 5569 5920 posix_setuid(PyObject *self, PyObject *args) 5570 5921 { 5571 long uid_arg; 5572 uid_t uid; 5573 if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg)) 5574 return NULL; 5575 uid = uid_arg; 5576 if (uid != uid_arg) { 5577 PyErr_SetString(PyExc_OverflowError, "user id too big"); 5578 return NULL; 5579 } 5580 if (setuid(uid) < 0) 5581 return posix_error(); 5582 Py_INCREF(Py_None); 5583 return Py_None; 5922 uid_t uid; 5923 if (!PyArg_ParseTuple(args, "O&:setuid", _Py_Uid_Converter, &uid)) 5924 return NULL; 5925 if (setuid(uid) < 0) 5926 return posix_error(); 5927 Py_INCREF(Py_None); 5928 return Py_None; 5584 5929 } 5585 5930 #endif /* HAVE_SETUID */ … … 5594 5939 posix_seteuid (PyObject *self, PyObject *args) 5595 5940 { 5596 long euid_arg; 5597 uid_t euid; 5598 if (!PyArg_ParseTuple(args, "l", &euid_arg)) 5599 return NULL; 5600 euid = euid_arg; 5601 if (euid != euid_arg) { 5602 PyErr_SetString(PyExc_OverflowError, "user id too big"); 5603 return NULL; 5604 } 5605 if (seteuid(euid) < 0) { 5606 return posix_error(); 5607 } else { 5608 Py_INCREF(Py_None); 5609 return Py_None; 5610 } 5941 uid_t euid; 5942 if (!PyArg_ParseTuple(args, "O&:seteuid", _Py_Uid_Converter, &euid)) 5943 return NULL; 5944 if (seteuid(euid) < 0) { 5945 return posix_error(); 5946 } else { 5947 Py_INCREF(Py_None); 5948 return Py_None; 5949 } 5611 5950 } 5612 5951 #endif /* HAVE_SETEUID */ … … 5620 5959 posix_setegid (PyObject *self, PyObject *args) 5621 5960 { 5622 long egid_arg; 5623 gid_t egid; 5624 if (!PyArg_ParseTuple(args, "l", &egid_arg)) 5625 return NULL; 5626 egid = egid_arg; 5627 if (egid != egid_arg) { 5628 PyErr_SetString(PyExc_OverflowError, "group id too big"); 5629 return NULL; 5630 } 5631 if (setegid(egid) < 0) { 5632 return posix_error(); 5633 } else { 5634 Py_INCREF(Py_None); 5635 return Py_None; 5636 } 5961 gid_t egid; 5962 if (!PyArg_ParseTuple(args, "O&:setegid", _Py_Gid_Converter, &egid)) 5963 return NULL; 5964 if (setegid(egid) < 0) { 5965 return posix_error(); 5966 } else { 5967 Py_INCREF(Py_None); 5968 return Py_None; 5969 } 5637 5970 } 5638 5971 #endif /* HAVE_SETEGID */ … … 5646 5979 posix_setreuid (PyObject *self, PyObject *args) 5647 5980 { 5648 long ruid_arg, euid_arg; 5649 uid_t ruid, euid; 5650 if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg)) 5651 return NULL; 5652 if (ruid_arg == -1) 5653 ruid = (uid_t)-1; /* let the compiler choose how -1 fits */ 5654 else 5655 ruid = ruid_arg; /* otherwise, assign from our long */ 5656 if (euid_arg == -1) 5657 euid = (uid_t)-1; 5658 else 5659 euid = euid_arg; 5660 if ((euid_arg != -1 && euid != euid_arg) || 5661 (ruid_arg != -1 && ruid != ruid_arg)) { 5662 PyErr_SetString(PyExc_OverflowError, "user id too big"); 5663 return NULL; 5664 } 5665 if (setreuid(ruid, euid) < 0) { 5666 return posix_error(); 5667 } else { 5668 Py_INCREF(Py_None); 5669 return Py_None; 5670 } 5981 uid_t ruid, euid; 5982 if (!PyArg_ParseTuple(args, "O&O&:setreuid", 5983 _Py_Uid_Converter, &ruid, 5984 _Py_Uid_Converter, &euid)) 5985 return NULL; 5986 if (setreuid(ruid, euid) < 0) { 5987 return posix_error(); 5988 } else { 5989 Py_INCREF(Py_None); 5990 return Py_None; 5991 } 5671 5992 } 5672 5993 #endif /* HAVE_SETREUID */ … … 5680 6001 posix_setregid (PyObject *self, PyObject *args) 5681 6002 { 5682 long rgid_arg, egid_arg; 5683 gid_t rgid, egid; 5684 if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg)) 5685 return NULL; 5686 if (rgid_arg == -1) 5687 rgid = (gid_t)-1; /* let the compiler choose how -1 fits */ 5688 else 5689 rgid = rgid_arg; /* otherwise, assign from our long */ 5690 if (egid_arg == -1) 5691 egid = (gid_t)-1; 5692 else 5693 egid = egid_arg; 5694 if ((egid_arg != -1 && egid != egid_arg) || 5695 (rgid_arg != -1 && rgid != rgid_arg)) { 5696 PyErr_SetString(PyExc_OverflowError, "group id too big"); 5697 return NULL; 5698 } 5699 if (setregid(rgid, egid) < 0) { 5700 return posix_error(); 5701 } else { 5702 Py_INCREF(Py_None); 5703 return Py_None; 5704 } 6003 gid_t rgid, egid; 6004 if (!PyArg_ParseTuple(args, "O&O&:setregid", 6005 _Py_Gid_Converter, &rgid, 6006 _Py_Gid_Converter, &egid)) 6007 return NULL; 6008 if (setregid(rgid, egid) < 0) { 6009 return posix_error(); 6010 } else { 6011 Py_INCREF(Py_None); 6012 return Py_None; 6013 } 5705 6014 } 5706 6015 #endif /* HAVE_SETREGID */ … … 5714 6023 posix_setgid(PyObject *self, PyObject *args) 5715 6024 { 5716 long gid_arg; 5717 gid_t gid; 5718 if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg)) 5719 return NULL; 5720 gid = gid_arg; 5721 if (gid != gid_arg) { 5722 PyErr_SetString(PyExc_OverflowError, "group id too big"); 5723 return NULL; 5724 } 5725 if (setgid(gid) < 0) 5726 return posix_error(); 5727 Py_INCREF(Py_None); 5728 return Py_None; 6025 gid_t gid; 6026 if (!PyArg_ParseTuple(args, "O&:setgid", _Py_Gid_Converter, &gid)) 6027 return NULL; 6028 if (setgid(gid) < 0) 6029 return posix_error(); 6030 Py_INCREF(Py_None); 6031 return Py_None; 5729 6032 } 5730 6033 #endif /* HAVE_SETGID */ … … 5738 6041 posix_setgroups(PyObject *self, PyObject *groups) 5739 6042 { 5740 int i, len; 5741 gid_t grouplist[MAX_GROUPS]; 5742 5743 if (!PySequence_Check(groups)) { 5744 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); 5745 return NULL; 5746 } 5747 len = PySequence_Size(groups); 5748 if (len > MAX_GROUPS) { 5749 PyErr_SetString(PyExc_ValueError, "too many groups"); 5750 return NULL; 5751 } 5752 for(i = 0; i < len; i++) { 5753 PyObject *elem; 5754 elem = PySequence_GetItem(groups, i); 5755 if (!elem) 5756 return NULL; 5757 if (!PyInt_Check(elem)) { 5758 if (!PyLong_Check(elem)) { 5759 PyErr_SetString(PyExc_TypeError, 5760 "groups must be integers"); 5761 Py_DECREF(elem); 5762 return NULL; 5763 } else { 5764 unsigned long x = PyLong_AsUnsignedLong(elem); 5765 if (PyErr_Occurred()) { 5766 PyErr_SetString(PyExc_TypeError, 5767 "group id too big"); 5768 Py_DECREF(elem); 5769 return NULL; 5770 } 5771 grouplist[i] = x; 5772 /* read back to see if it fits in gid_t */ 5773 if (grouplist[i] != x) { 5774 PyErr_SetString(PyExc_TypeError, 5775 "group id too big"); 5776 Py_DECREF(elem); 5777 return NULL; 5778 } 5779 } 5780 } else { 5781 long x = PyInt_AsLong(elem); 5782 grouplist[i] = x; 5783 if (grouplist[i] != x) { 5784 PyErr_SetString(PyExc_TypeError, 5785 "group id too big"); 5786 Py_DECREF(elem); 5787 return NULL; 5788 } 5789 } 5790 Py_DECREF(elem); 5791 } 5792 5793 if (setgroups(len, grouplist) < 0) 5794 return posix_error(); 5795 Py_INCREF(Py_None); 5796 return Py_None; 6043 int i, len; 6044 gid_t grouplist[MAX_GROUPS]; 6045 6046 if (!PySequence_Check(groups)) { 6047 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); 6048 return NULL; 6049 } 6050 len = PySequence_Size(groups); 6051 if (len > MAX_GROUPS) { 6052 PyErr_SetString(PyExc_ValueError, "too many groups"); 6053 return NULL; 6054 } 6055 for(i = 0; i < len; i++) { 6056 PyObject *elem; 6057 elem = PySequence_GetItem(groups, i); 6058 if (!elem) 6059 return NULL; 6060 if (!PyInt_Check(elem) && !PyLong_Check(elem)) { 6061 PyErr_SetString(PyExc_TypeError, 6062 "groups must be integers"); 6063 Py_DECREF(elem); 6064 return NULL; 6065 } else { 6066 if (!_Py_Gid_Converter(elem, &grouplist[i])) { 6067 Py_DECREF(elem); 6068 return NULL; 6069 } 6070 } 6071 Py_DECREF(elem); 6072 } 6073 6074 if (setgroups(len, grouplist) < 0) 6075 return posix_error(); 6076 Py_INCREF(Py_None); 6077 return Py_None; 5797 6078 } 5798 6079 #endif /* HAVE_SETGROUPS */ … … 5802 6083 wait_helper(pid_t pid, int status, struct rusage *ru) 5803 6084 { 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 6085 PyObject *result; 6086 static PyObject *struct_rusage; 6087 6088 if (pid == -1) 6089 return posix_error(); 6090 6091 if (struct_rusage == NULL) { 6092 PyObject *m = PyImport_ImportModuleNoBlock("resource"); 6093 if (m == NULL) 6094 return NULL; 6095 struct_rusage = PyObject_GetAttrString(m, "struct_rusage"); 6096 Py_DECREF(m); 6097 if (struct_rusage == NULL) 6098 return NULL; 6099 } 6100 6101 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ 6102 result = PyStructSequence_New((PyTypeObject*) struct_rusage); 6103 if (!result) 6104 return NULL; 5824 6105 5825 6106 #ifndef doubletime … … 5827 6108 #endif 5828 6109 5829 5830 5831 5832 6110 PyStructSequence_SET_ITEM(result, 0, 6111 PyFloat_FromDouble(doubletime(ru->ru_utime))); 6112 PyStructSequence_SET_ITEM(result, 1, 6113 PyFloat_FromDouble(doubletime(ru->ru_stime))); 5833 6114 #define SET_INT(result, index, value)\ 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 6115 PyStructSequence_SET_ITEM(result, index, PyInt_FromLong(value)) 6116 SET_INT(result, 2, ru->ru_maxrss); 6117 SET_INT(result, 3, ru->ru_ixrss); 6118 SET_INT(result, 4, ru->ru_idrss); 6119 SET_INT(result, 5, ru->ru_isrss); 6120 SET_INT(result, 6, ru->ru_minflt); 6121 SET_INT(result, 7, ru->ru_majflt); 6122 SET_INT(result, 8, ru->ru_nswap); 6123 SET_INT(result, 9, ru->ru_inblock); 6124 SET_INT(result, 10, ru->ru_oublock); 6125 SET_INT(result, 11, ru->ru_msgsnd); 6126 SET_INT(result, 12, ru->ru_msgrcv); 6127 SET_INT(result, 13, ru->ru_nsignals); 6128 SET_INT(result, 14, ru->ru_nvcsw); 6129 SET_INT(result, 15, ru->ru_nivcsw); 5849 6130 #undef SET_INT 5850 6131 5851 5852 5853 5854 5855 5856 6132 if (PyErr_Occurred()) { 6133 Py_DECREF(result); 6134 return NULL; 6135 } 6136 6137 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result); 5857 6138 } 5858 6139 #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ … … 5866 6147 posix_wait3(PyObject *self, PyObject *args) 5867 6148 { 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 6149 pid_t pid; 6150 int options; 6151 struct rusage ru; 6152 WAIT_TYPE status; 6153 WAIT_STATUS_INT(status) = 0; 6154 6155 if (!PyArg_ParseTuple(args, "i:wait3", &options)) 6156 return NULL; 6157 6158 Py_BEGIN_ALLOW_THREADS 6159 pid = wait3(&status, options, &ru); 6160 Py_END_ALLOW_THREADS 6161 6162 return wait_helper(pid, WAIT_STATUS_INT(status), &ru); 5882 6163 } 5883 6164 #endif /* HAVE_WAIT3 */ … … 5891 6172 posix_wait4(PyObject *self, PyObject *args) 5892 6173 { 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 6174 pid_t pid; 6175 int options; 6176 struct rusage ru; 6177 WAIT_TYPE status; 6178 WAIT_STATUS_INT(status) = 0; 6179 6180 if (!PyArg_ParseTuple(args, PARSE_PID "i:wait4", &pid, &options)) 6181 return NULL; 6182 6183 Py_BEGIN_ALLOW_THREADS 6184 pid = wait4(pid, &status, options, &ru); 6185 Py_END_ALLOW_THREADS 6186 6187 return wait_helper(pid, WAIT_STATUS_INT(status), &ru); 5907 6188 } 5908 6189 #endif /* HAVE_WAIT4 */ … … 5916 6197 posix_waitpid(PyObject *self, PyObject *args) 5917 6198 { 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 6199 pid_t pid; 6200 int options; 6201 WAIT_TYPE status; 6202 WAIT_STATUS_INT(status) = 0; 6203 6204 if (!PyArg_ParseTuple(args, PARSE_PID "i:waitpid", &pid, &options)) 6205 return NULL; 6206 Py_BEGIN_ALLOW_THREADS 6207 pid = waitpid(pid, &status, options); 6208 Py_END_ALLOW_THREADS 6209 if (pid == -1) 6210 return posix_error(); 6211 6212 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); 5932 6213 } 5933 6214 … … 5942 6223 posix_waitpid(PyObject *self, PyObject *args) 5943 6224 { 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 6225 Py_intptr_t pid; 6226 int status, options; 6227 6228 if (!PyArg_ParseTuple(args, PARSE_PID "i:waitpid", &pid, &options)) 6229 return NULL; 6230 Py_BEGIN_ALLOW_THREADS 6231 pid = _cwait(&status, pid, options); 6232 Py_END_ALLOW_THREADS 6233 if (pid == -1) 6234 return posix_error(); 6235 6236 /* shift the status left a byte so this is more like the POSIX waitpid */ 6237 return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8); 5957 6238 } 5958 6239 #endif /* HAVE_WAITPID || HAVE_CWAIT */ … … 5966 6247 posix_wait(PyObject *self, PyObject *noargs) 5967 6248 { 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 6249 pid_t pid; 6250 WAIT_TYPE status; 6251 WAIT_STATUS_INT(status) = 0; 6252 6253 Py_BEGIN_ALLOW_THREADS 6254 pid = wait(&status); 6255 Py_END_ALLOW_THREADS 6256 if (pid == -1) 6257 return posix_error(); 6258 6259 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); 5979 6260 } 5980 6261 #endif … … 5989 6270 { 5990 6271 #ifdef HAVE_LSTAT 5991 6272 return posix_do_stat(self, args, "et:lstat", lstat, NULL, NULL); 5992 6273 #else /* !HAVE_LSTAT */ 5993 6274 #ifdef MS_WINDOWS 5994 6275 return posix_do_stat(self, args, "et:lstat", STAT, "U:lstat", win32_wstat); 5995 6276 #else 5996 6277 return posix_do_stat(self, args, "et:lstat", STAT, NULL, NULL); 5997 6278 #endif 5998 6279 #endif /* !HAVE_LSTAT */ … … 6008 6289 posix_readlink(PyObject *self, PyObject *args) 6009 6290 { 6010 6011 6012 6013 6291 PyObject* v; 6292 char buf[MAXPATHLEN]; 6293 char *path; 6294 int n; 6014 6295 #ifdef Py_USING_UNICODE 6015 6016 #endif 6017 6018 if (!PyArg_ParseTuple(args, "et:readlink", 6019 6020 6296 int arg_is_unicode = 0; 6297 #endif 6298 6299 if (!PyArg_ParseTuple(args, "et:readlink", 6300 Py_FileSystemDefaultEncoding, &path)) 6301 return NULL; 6021 6302 #ifdef Py_USING_UNICODE 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 #endif 6033 6034 6035 6036 6037 6038 6039 6040 6041 6303 v = PySequence_GetItem(args, 0); 6304 if (v == NULL) { 6305 PyMem_Free(path); 6306 return NULL; 6307 } 6308 6309 if (PyUnicode_Check(v)) { 6310 arg_is_unicode = 1; 6311 } 6312 Py_DECREF(v); 6313 #endif 6314 6315 Py_BEGIN_ALLOW_THREADS 6316 n = readlink(path, buf, (int) sizeof buf); 6317 Py_END_ALLOW_THREADS 6318 if (n < 0) 6319 return posix_error_with_allocated_filename(path); 6320 6321 PyMem_Free(path); 6322 v = PyString_FromStringAndSize(buf, n); 6042 6323 #ifdef Py_USING_UNICODE 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 #endif 6060 6324 if (arg_is_unicode) { 6325 PyObject *w; 6326 6327 w = PyUnicode_FromEncodedObject(v, 6328 Py_FileSystemDefaultEncoding, 6329 "strict"); 6330 if (w != NULL) { 6331 Py_DECREF(v); 6332 v = w; 6333 } 6334 else { 6335 /* fall back to the original byte string, as 6336 discussed in patch #683592 */ 6337 PyErr_Clear(); 6338 } 6339 } 6340 #endif 6341 return v; 6061 6342 } 6062 6343 #endif /* HAVE_READLINK */ … … 6071 6352 posix_symlink(PyObject *self, PyObject *args) 6072 6353 { 6073 6354 return posix_2str(args, "etet:symlink", symlink); 6074 6355 } 6075 6356 #endif /* HAVE_SYMLINK */ … … 6094 6375 { 6095 6376 /* Currently Only Uptime is Provided -- Others Later */ 6096 6097 6098 6099 6100 6101 6377 return Py_BuildValue("ddddd", 6378 (double)0 /* t.tms_utime / HZ */, 6379 (double)0 /* t.tms_stime / HZ */, 6380 (double)0 /* t.tms_cutime / HZ */, 6381 (double)0 /* t.tms_cstime / HZ */, 6382 (double)system_uptime() / 1000); 6102 6383 } 6103 6384 #else /* not OS2 */ … … 6107 6388 posix_times(PyObject *self, PyObject *noargs) 6108 6389 { 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6390 struct tms t; 6391 clock_t c; 6392 errno = 0; 6393 c = times(&t); 6394 if (c == (clock_t) -1) 6395 return posix_error(); 6396 return Py_BuildValue("ddddd", 6397 (double)t.tms_utime / ticks_per_second, 6398 (double)t.tms_stime / ticks_per_second, 6399 (double)t.tms_cutime / ticks_per_second, 6400 (double)t.tms_cstime / ticks_per_second, 6401 (double)c / ticks_per_second); 6121 6402 } 6122 6403 #endif /* not OS2 */ … … 6125 6406 6126 6407 #ifdef MS_WINDOWS 6127 #define HAVE_TIMES 6408 #define HAVE_TIMES /* so the method table will pick it up */ 6128 6409 static PyObject * 6129 6410 posix_times(PyObject *self, PyObject *noargs) 6130 6411 { 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6412 FILETIME create, exit, kernel, user; 6413 HANDLE hProc; 6414 hProc = GetCurrentProcess(); 6415 GetProcessTimes(hProc, &create, &exit, &kernel, &user); 6416 /* The fields of a FILETIME structure are the hi and lo part 6417 of a 64-bit value expressed in 100 nanosecond units. 6418 1e7 is one second in such units; 1e-7 the inverse. 6419 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. 6420 */ 6421 return Py_BuildValue( 6422 "ddddd", 6423 (double)(user.dwHighDateTime*429.4967296 + 6424 user.dwLowDateTime*1e-7), 6425 (double)(kernel.dwHighDateTime*429.4967296 + 6426 kernel.dwLowDateTime*1e-7), 6427 (double)0, 6428 (double)0, 6429 (double)0); 6149 6430 } 6150 6431 #endif /* MS_WINDOWS */ … … 6165 6446 posix_getsid(PyObject *self, PyObject *args) 6166 6447 { 6167 6168 6169 6170 6171 6172 6173 6174 6448 pid_t pid; 6449 int sid; 6450 if (!PyArg_ParseTuple(args, PARSE_PID ":getsid", &pid)) 6451 return NULL; 6452 sid = getsid(pid); 6453 if (sid < 0) 6454 return posix_error(); 6455 return PyInt_FromLong((long)sid); 6175 6456 } 6176 6457 #endif /* HAVE_GETSID */ … … 6185 6466 posix_setsid(PyObject *self, PyObject *noargs) 6186 6467 { 6187 6188 6189 6190 6468 if (setsid() < 0) 6469 return posix_error(); 6470 Py_INCREF(Py_None); 6471 return Py_None; 6191 6472 } 6192 6473 #endif /* HAVE_SETSID */ … … 6200 6481 posix_setpgid(PyObject *self, PyObject *args) 6201 6482 { 6202 6203 6204 6205 6206 6207 6208 6209 6483 pid_t pid; 6484 int pgrp; 6485 if (!PyArg_ParseTuple(args, PARSE_PID "i:setpgid", &pid, &pgrp)) 6486 return NULL; 6487 if (setpgid(pid, pgrp) < 0) 6488 return posix_error(); 6489 Py_INCREF(Py_None); 6490 return Py_None; 6210 6491 } 6211 6492 #endif /* HAVE_SETPGID */ … … 6220 6501 posix_tcgetpgrp(PyObject *self, PyObject *args) 6221 6502 { 6222 6223 6224 6225 6226 6227 6228 6229 6503 int fd; 6504 pid_t pgid; 6505 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd)) 6506 return NULL; 6507 pgid = tcgetpgrp(fd); 6508 if (pgid < 0) 6509 return posix_error(); 6510 return PyLong_FromPid(pgid); 6230 6511 } 6231 6512 #endif /* HAVE_TCGETPGRP */ … … 6240 6521 posix_tcsetpgrp(PyObject *self, PyObject *args) 6241 6522 { 6242 6243 6244 6245 6246 6247 6248 6249 6523 int fd; 6524 pid_t pgid; 6525 if (!PyArg_ParseTuple(args, "i" PARSE_PID ":tcsetpgrp", &fd, &pgid)) 6526 return NULL; 6527 if (tcsetpgrp(fd, pgid) < 0) 6528 return posix_error(); 6529 Py_INCREF(Py_None); 6530 return Py_None; 6250 6531 } 6251 6532 #endif /* HAVE_TCSETPGRP */ … … 6260 6541 posix_open(PyObject *self, PyObject *args) 6261 6542 { 6262 6263 6264 6265 6543 char *file = NULL; 6544 int flag; 6545 int mode = 0777; 6546 int fd; 6266 6547 6267 6548 #ifdef MS_WINDOWS 6268 if (unicode_file_names()) { 6269 PyUnicodeObject *po; 6270 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) { 6271 Py_BEGIN_ALLOW_THREADS 6272 /* PyUnicode_AS_UNICODE OK without thread 6273 lock as it is a simple dereference. */ 6274 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode); 6275 Py_END_ALLOW_THREADS 6276 if (fd < 0) 6277 return posix_error(); 6278 return PyInt_FromLong((long)fd); 6279 } 6280 /* Drop the argument parsing error as narrow strings 6281 are also valid. */ 6282 PyErr_Clear(); 6283 } 6284 #endif 6285 6286 if (!PyArg_ParseTuple(args, "eti|i", 6287 Py_FileSystemDefaultEncoding, &file, 6288 &flag, &mode)) 6289 return NULL; 6290 6291 Py_BEGIN_ALLOW_THREADS 6292 fd = open(file, flag, mode); 6293 Py_END_ALLOW_THREADS 6294 if (fd < 0) 6295 return posix_error_with_allocated_filename(file); 6296 PyMem_Free(file); 6297 return PyInt_FromLong((long)fd); 6549 PyUnicodeObject *po; 6550 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) { 6551 Py_BEGIN_ALLOW_THREADS 6552 /* PyUnicode_AS_UNICODE OK without thread 6553 lock as it is a simple dereference. */ 6554 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode); 6555 Py_END_ALLOW_THREADS 6556 if (fd < 0) 6557 return posix_error(); 6558 return PyInt_FromLong((long)fd); 6559 } 6560 /* Drop the argument parsing error as narrow strings 6561 are also valid. */ 6562 PyErr_Clear(); 6563 #endif 6564 6565 if (!PyArg_ParseTuple(args, "eti|i", 6566 Py_FileSystemDefaultEncoding, &file, 6567 &flag, &mode)) 6568 return NULL; 6569 6570 Py_BEGIN_ALLOW_THREADS 6571 fd = open(file, flag, mode); 6572 Py_END_ALLOW_THREADS 6573 if (fd < 0) 6574 return posix_error_with_allocated_filename(file); 6575 PyMem_Free(file); 6576 return PyInt_FromLong((long)fd); 6298 6577 } 6299 6578 … … 6306 6585 posix_close(PyObject *self, PyObject *args) 6307 6586 { 6308 int fd, res; 6309 if (!PyArg_ParseTuple(args, "i:close", &fd)) 6310 return NULL; 6311 Py_BEGIN_ALLOW_THREADS 6312 res = close(fd); 6313 Py_END_ALLOW_THREADS 6314 if (res < 0) 6315 return posix_error(); 6316 Py_INCREF(Py_None); 6317 return Py_None; 6318 } 6319 6320 6321 PyDoc_STRVAR(posix_closerange__doc__, 6587 int fd, res; 6588 if (!PyArg_ParseTuple(args, "i:close", &fd)) 6589 return NULL; 6590 if (!_PyVerify_fd(fd)) 6591 return posix_error(); 6592 Py_BEGIN_ALLOW_THREADS 6593 res = close(fd); 6594 Py_END_ALLOW_THREADS 6595 if (res < 0) 6596 return posix_error(); 6597 Py_INCREF(Py_None); 6598 return Py_None; 6599 } 6600 6601 6602 PyDoc_STRVAR(posix_closerange__doc__, 6322 6603 "closerange(fd_low, fd_high)\n\n\ 6323 6604 Closes all file descriptors in [fd_low, fd_high), ignoring errors."); … … 6326 6607 posix_closerange(PyObject *self, PyObject *args) 6327 6608 { 6328 int fd_from, fd_to, i; 6329 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to)) 6330 return NULL; 6331 Py_BEGIN_ALLOW_THREADS 6332 for (i = fd_from; i < fd_to; i++) 6333 close(i); 6334 Py_END_ALLOW_THREADS 6335 Py_RETURN_NONE; 6609 int fd_from, fd_to, i; 6610 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to)) 6611 return NULL; 6612 Py_BEGIN_ALLOW_THREADS 6613 for (i = fd_from; i < fd_to; i++) 6614 if (_PyVerify_fd(i)) 6615 close(i); 6616 Py_END_ALLOW_THREADS 6617 Py_RETURN_NONE; 6336 6618 } 6337 6619 … … 6344 6626 posix_dup(PyObject *self, PyObject *args) 6345 6627 { 6346 int fd; 6347 if (!PyArg_ParseTuple(args, "i:dup", &fd)) 6348 return NULL; 6349 Py_BEGIN_ALLOW_THREADS 6350 fd = dup(fd); 6351 Py_END_ALLOW_THREADS 6352 if (fd < 0) 6353 return posix_error(); 6354 return PyInt_FromLong((long)fd); 6628 int fd; 6629 if (!PyArg_ParseTuple(args, "i:dup", &fd)) 6630 return NULL; 6631 if (!_PyVerify_fd(fd)) 6632 return posix_error(); 6633 Py_BEGIN_ALLOW_THREADS 6634 fd = dup(fd); 6635 Py_END_ALLOW_THREADS 6636 if (fd < 0) 6637 return posix_error(); 6638 return PyInt_FromLong((long)fd); 6355 6639 } 6356 6640 … … 6363 6647 posix_dup2(PyObject *self, PyObject *args) 6364 6648 { 6365 int fd, fd2, res; 6366 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2)) 6367 return NULL; 6368 Py_BEGIN_ALLOW_THREADS 6369 res = dup2(fd, fd2); 6370 Py_END_ALLOW_THREADS 6371 if (res < 0) 6372 return posix_error(); 6373 Py_INCREF(Py_None); 6374 return Py_None; 6649 int fd, fd2, res; 6650 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2)) 6651 return NULL; 6652 if (!_PyVerify_fd_dup2(fd, fd2)) 6653 return posix_error(); 6654 Py_BEGIN_ALLOW_THREADS 6655 res = dup2(fd, fd2); 6656 Py_END_ALLOW_THREADS 6657 if (res < 0) 6658 return posix_error(); 6659 Py_INCREF(Py_None); 6660 return Py_None; 6375 6661 } 6376 6662 … … 6383 6669 posix_lseek(PyObject *self, PyObject *args) 6384 6670 { 6385 6671 int fd, how; 6386 6672 #if defined(MS_WIN64) || defined(MS_WINDOWS) 6387 6673 PY_LONG_LONG pos, res; 6388 6674 #else 6389 6390 #endif 6391 6392 6393 6675 off_t pos, res; 6676 #endif 6677 PyObject *posobj; 6678 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how)) 6679 return NULL; 6394 6680 #ifdef SEEK_SET 6395 6396 6397 6398 6399 6400 6681 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ 6682 switch (how) { 6683 case 0: how = SEEK_SET; break; 6684 case 1: how = SEEK_CUR; break; 6685 case 2: how = SEEK_END; break; 6686 } 6401 6687 #endif /* SEEK_END */ 6402 6688 6403 6689 #if !defined(HAVE_LARGEFILE_SUPPORT) 6404 6690 pos = PyInt_AsLong(posobj); 6405 6691 #else 6406 pos = PyLong_Check(posobj) ? 6407 PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj); 6408 #endif 6409 if (PyErr_Occurred()) 6410 return NULL; 6411 6412 Py_BEGIN_ALLOW_THREADS 6692 pos = PyLong_Check(posobj) ? 6693 PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj); 6694 #endif 6695 if (PyErr_Occurred()) 6696 return NULL; 6697 6698 if (!_PyVerify_fd(fd)) 6699 return posix_error(); 6700 Py_BEGIN_ALLOW_THREADS 6413 6701 #if defined(MS_WIN64) || defined(MS_WINDOWS) 6414 6702 res = _lseeki64(fd, pos, how); 6415 6703 #else 6416 6417 #endif 6418 6419 6420 6704 res = lseek(fd, pos, how); 6705 #endif 6706 Py_END_ALLOW_THREADS 6707 if (res < 0) 6708 return posix_error(); 6421 6709 6422 6710 #if !defined(HAVE_LARGEFILE_SUPPORT) 6423 6711 return PyInt_FromLong(res); 6424 6712 #else 6425 6713 return PyLong_FromLongLong(res); 6426 6714 #endif 6427 6715 } … … 6435 6723 posix_read(PyObject *self, PyObject *args) 6436 6724 { 6437 int fd, size, n; 6438 PyObject *buffer; 6439 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) 6440 return NULL; 6441 if (size < 0) { 6442 errno = EINVAL; 6443 return posix_error(); 6444 } 6445 buffer = PyString_FromStringAndSize((char *)NULL, size); 6446 if (buffer == NULL) 6447 return NULL; 6448 Py_BEGIN_ALLOW_THREADS 6449 n = read(fd, PyString_AsString(buffer), size); 6450 Py_END_ALLOW_THREADS 6451 if (n < 0) { 6452 Py_DECREF(buffer); 6453 return posix_error(); 6454 } 6455 if (n != size) 6456 _PyString_Resize(&buffer, n); 6457 return buffer; 6725 int fd, size, n; 6726 PyObject *buffer; 6727 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size)) 6728 return NULL; 6729 if (size < 0) { 6730 errno = EINVAL; 6731 return posix_error(); 6732 } 6733 buffer = PyString_FromStringAndSize((char *)NULL, size); 6734 if (buffer == NULL) 6735 return NULL; 6736 if (!_PyVerify_fd(fd)) { 6737 Py_DECREF(buffer); 6738 return posix_error(); 6739 } 6740 Py_BEGIN_ALLOW_THREADS 6741 n = read(fd, PyString_AsString(buffer), size); 6742 Py_END_ALLOW_THREADS 6743 if (n < 0) { 6744 Py_DECREF(buffer); 6745 return posix_error(); 6746 } 6747 if (n != size) 6748 _PyString_Resize(&buffer, n); 6749 return buffer; 6458 6750 } 6459 6751 … … 6466 6758 posix_write(PyObject *self, PyObject *args) 6467 6759 { 6468 Py_buffer pbuf; 6469 int fd; 6470 Py_ssize_t size; 6471 6472 if (!PyArg_ParseTuple(args, "is*:write", &fd, &pbuf)) 6473 return NULL; 6474 Py_BEGIN_ALLOW_THREADS 6475 size = write(fd, pbuf.buf, (size_t)pbuf.len); 6476 Py_END_ALLOW_THREADS 6477 PyBuffer_Release(&pbuf); 6478 if (size < 0) 6479 return posix_error(); 6480 return PyInt_FromSsize_t(size); 6760 Py_buffer pbuf; 6761 int fd; 6762 Py_ssize_t size, len; 6763 6764 if (!PyArg_ParseTuple(args, "is*:write", &fd, &pbuf)) 6765 return NULL; 6766 if (!_PyVerify_fd(fd)) { 6767 PyBuffer_Release(&pbuf); 6768 return posix_error(); 6769 } 6770 len = pbuf.len; 6771 Py_BEGIN_ALLOW_THREADS 6772 #if defined(MS_WIN64) || defined(MS_WINDOWS) 6773 if (len > INT_MAX) 6774 len = INT_MAX; 6775 size = write(fd, pbuf.buf, (int)len); 6776 #else 6777 size = write(fd, pbuf.buf, len); 6778 #endif 6779 Py_END_ALLOW_THREADS 6780 PyBuffer_Release(&pbuf); 6781 if (size < 0) 6782 return posix_error(); 6783 return PyInt_FromSsize_t(size); 6481 6784 } 6482 6785 … … 6489 6792 posix_fstat(PyObject *self, PyObject *args) 6490 6793 { 6491 6492 6493 6494 6495 6794 int fd; 6795 STRUCT_STAT st; 6796 int res; 6797 if (!PyArg_ParseTuple(args, "i:fstat", &fd)) 6798 return NULL; 6496 6799 #ifdef __VMS 6497 /* on OpenVMS we must ensure that all bytes are written to the file */ 6498 fsync(fd); 6499 #endif 6500 Py_BEGIN_ALLOW_THREADS 6501 res = FSTAT(fd, &st); 6502 Py_END_ALLOW_THREADS 6503 if (res != 0) { 6800 /* on OpenVMS we must ensure that all bytes are written to the file */ 6801 fsync(fd); 6802 #endif 6803 if (!_PyVerify_fd(fd)) 6804 return posix_error(); 6805 Py_BEGIN_ALLOW_THREADS 6806 res = FSTAT(fd, &st); 6807 Py_END_ALLOW_THREADS 6808 if (res != 0) { 6504 6809 #ifdef MS_WINDOWS 6505 6810 return win32_error("fstat", NULL); 6506 6811 #else 6507 6508 #endif 6509 6510 6511 6812 return posix_error(); 6813 #endif 6814 } 6815 6816 return _pystat_fromstructstat(&st); 6512 6817 } 6513 6818 … … 6520 6825 posix_fdopen(PyObject *self, PyObject *args) 6521 6826 { 6522 int fd; 6523 char *orgmode = "r"; 6524 int bufsize = -1; 6525 FILE *fp; 6526 PyObject *f; 6527 char *mode; 6528 if (!PyArg_ParseTuple(args, "i|si", &fd, &orgmode, &bufsize)) 6529 return NULL; 6530 6531 /* Sanitize mode. See fileobject.c */ 6532 mode = PyMem_MALLOC(strlen(orgmode)+3); 6533 if (!mode) { 6534 PyErr_NoMemory(); 6535 return NULL; 6536 } 6537 strcpy(mode, orgmode); 6538 if (_PyFile_SanitizeMode(mode)) { 6539 PyMem_FREE(mode); 6540 return NULL; 6541 } 6542 Py_BEGIN_ALLOW_THREADS 6827 int fd; 6828 char *orgmode = "r"; 6829 int bufsize = -1; 6830 FILE *fp; 6831 PyObject *f; 6832 char *mode; 6833 if (!PyArg_ParseTuple(args, "i|si", &fd, &orgmode, &bufsize)) 6834 return NULL; 6835 6836 /* Sanitize mode. See fileobject.c */ 6837 mode = PyMem_MALLOC(strlen(orgmode)+3); 6838 if (!mode) { 6839 PyErr_NoMemory(); 6840 return NULL; 6841 } 6842 strcpy(mode, orgmode); 6843 if (_PyFile_SanitizeMode(mode)) { 6844 PyMem_FREE(mode); 6845 return NULL; 6846 } 6847 if (!_PyVerify_fd(fd)) 6848 return posix_error(); 6849 Py_BEGIN_ALLOW_THREADS 6543 6850 #if !defined(MS_WINDOWS) && defined(HAVE_FCNTL_H) 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6851 if (mode[0] == 'a') { 6852 /* try to make sure the O_APPEND flag is set */ 6853 int flags; 6854 flags = fcntl(fd, F_GETFL); 6855 if (flags != -1) 6856 fcntl(fd, F_SETFL, flags | O_APPEND); 6857 fp = fdopen(fd, mode); 6858 if (fp == NULL && flags != -1) 6859 /* restore old mode if fdopen failed */ 6860 fcntl(fd, F_SETFL, flags); 6861 } else { 6862 fp = fdopen(fd, mode); 6863 } 6557 6864 #else 6558 fp = fdopen(fd, mode); 6559 #endif 6560 Py_END_ALLOW_THREADS 6561 PyMem_FREE(mode); 6562 if (fp == NULL) 6563 return posix_error(); 6564 f = PyFile_FromFile(fp, "<fdopen>", orgmode, fclose); 6565 if (f != NULL) 6566 PyFile_SetBufSize(f, bufsize); 6567 return f; 6865 fp = fdopen(fd, mode); 6866 #endif 6867 Py_END_ALLOW_THREADS 6868 PyMem_FREE(mode); 6869 if (fp == NULL) 6870 return posix_error(); 6871 /* The dummy filename used here must be kept in sync with the value 6872 tested against in gzip.GzipFile.__init__() - see issue #13781. */ 6873 f = PyFile_FromFile(fp, "<fdopen>", orgmode, fclose); 6874 if (f != NULL) 6875 PyFile_SetBufSize(f, bufsize); 6876 return f; 6568 6877 } 6569 6878 … … 6576 6885 posix_isatty(PyObject *self, PyObject *args) 6577 6886 { 6578 int fd; 6579 if (!PyArg_ParseTuple(args, "i:isatty", &fd)) 6580 return NULL; 6581 return PyBool_FromLong(isatty(fd)); 6887 int fd; 6888 if (!PyArg_ParseTuple(args, "i:isatty", &fd)) 6889 return NULL; 6890 if (!_PyVerify_fd(fd)) 6891 return PyBool_FromLong(0); 6892 return PyBool_FromLong(isatty(fd)); 6582 6893 } 6583 6894 … … 6594 6905 APIRET rc; 6595 6906 6596 6907 Py_BEGIN_ALLOW_THREADS 6597 6908 rc = DosCreatePipe( &read, &write, 4096); 6598 6909 Py_END_ALLOW_THREADS 6599 6910 if (rc != NO_ERROR) 6600 6911 return os2_error(rc); … … 6603 6914 #else 6604 6915 #if !defined(MS_WINDOWS) 6605 6606 6607 6608 6609 6610 6611 6612 6916 int fds[2]; 6917 int res; 6918 Py_BEGIN_ALLOW_THREADS 6919 res = pipe(fds); 6920 Py_END_ALLOW_THREADS 6921 if (res != 0) 6922 return posix_error(); 6923 return Py_BuildValue("(ii)", fds[0], fds[1]); 6613 6924 #else /* MS_WINDOWS */ 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6925 HANDLE read, write; 6926 int read_fd, write_fd; 6927 BOOL ok; 6928 Py_BEGIN_ALLOW_THREADS 6929 ok = CreatePipe(&read, &write, NULL, 0); 6930 Py_END_ALLOW_THREADS 6931 if (!ok) 6932 return win32_error("CreatePipe", NULL); 6933 read_fd = _open_osfhandle((Py_intptr_t)read, 0); 6934 write_fd = _open_osfhandle((Py_intptr_t)write, 1); 6935 return Py_BuildValue("(ii)", read_fd, write_fd); 6625 6936 #endif /* MS_WINDOWS */ 6626 6937 #endif … … 6637 6948 posix_mkfifo(PyObject *self, PyObject *args) 6638 6949 { 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6950 char *filename; 6951 int mode = 0666; 6952 int res; 6953 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode)) 6954 return NULL; 6955 Py_BEGIN_ALLOW_THREADS 6956 res = mkfifo(filename, mode); 6957 Py_END_ALLOW_THREADS 6958 if (res < 0) 6959 return posix_error(); 6960 Py_INCREF(Py_None); 6961 return Py_None; 6651 6962 } 6652 6963 #endif … … 6667 6978 posix_mknod(PyObject *self, PyObject *args) 6668 6979 { 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6980 char *filename; 6981 int mode = 0600; 6982 int device = 0; 6983 int res; 6984 if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device)) 6985 return NULL; 6986 Py_BEGIN_ALLOW_THREADS 6987 res = mknod(filename, mode, device); 6988 Py_END_ALLOW_THREADS 6989 if (res < 0) 6990 return posix_error(); 6991 Py_INCREF(Py_None); 6992 return Py_None; 6682 6993 } 6683 6994 #endif … … 6691 7002 posix_major(PyObject *self, PyObject *args) 6692 7003 { 6693 6694 6695 6696 7004 int device; 7005 if (!PyArg_ParseTuple(args, "i:major", &device)) 7006 return NULL; 7007 return PyInt_FromLong((long)major(device)); 6697 7008 } 6698 7009 … … 6704 7015 posix_minor(PyObject *self, PyObject *args) 6705 7016 { 6706 6707 6708 6709 7017 int device; 7018 if (!PyArg_ParseTuple(args, "i:minor", &device)) 7019 return NULL; 7020 return PyInt_FromLong((long)minor(device)); 6710 7021 } 6711 7022 … … 6717 7028 posix_makedev(PyObject *self, PyObject *args) 6718 7029 { 6719 6720 6721 6722 7030 int major, minor; 7031 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor)) 7032 return NULL; 7033 return PyInt_FromLong((long)makedev(major, minor)); 6723 7034 } 6724 7035 #endif /* device macros */ … … 6733 7044 posix_ftruncate(PyObject *self, PyObject *args) 6734 7045 { 6735 6736 6737 6738 6739 6740 6741 7046 int fd; 7047 off_t length; 7048 int res; 7049 PyObject *lenobj; 7050 7051 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj)) 7052 return NULL; 6742 7053 6743 7054 #if !defined(HAVE_LARGEFILE_SUPPORT) 6744 7055 length = PyInt_AsLong(lenobj); 6745 7056 #else 6746 length = PyLong_Check(lenobj) ? 6747 PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj); 6748 #endif 6749 if (PyErr_Occurred()) 6750 return NULL; 6751 6752 Py_BEGIN_ALLOW_THREADS 6753 res = ftruncate(fd, length); 6754 Py_END_ALLOW_THREADS 6755 if (res < 0) { 6756 PyErr_SetFromErrno(PyExc_IOError); 6757 return NULL; 6758 } 6759 Py_INCREF(Py_None); 6760 return Py_None; 7057 length = PyLong_Check(lenobj) ? 7058 PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj); 7059 #endif 7060 if (PyErr_Occurred()) 7061 return NULL; 7062 7063 Py_BEGIN_ALLOW_THREADS 7064 res = ftruncate(fd, length); 7065 Py_END_ALLOW_THREADS 7066 if (res < 0) 7067 return posix_error(); 7068 Py_INCREF(Py_None); 7069 return Py_None; 6761 7070 } 6762 7071 #endif … … 6774 7083 posix_putenv(PyObject *self, PyObject *args) 6775 7084 { 6776 6777 6778 6779 6780 6781 6782 7085 char *s1, *s2; 7086 char *newenv; 7087 PyObject *newstr; 7088 size_t len; 7089 7090 if (!PyArg_ParseTuple(args, "ss:putenv", &s1, &s2)) 7091 return NULL; 6783 7092 6784 7093 #if defined(PYOS_OS2) … … 6799 7108 #endif 6800 7109 6801 /* XXX This can leak memory -- not easy to fix :-( */ 6802 len = strlen(s1) + strlen(s2) + 2; 6803 /* len includes space for a trailing \0; the size arg to 6804 PyString_FromStringAndSize does not count that */ 6805 newstr = PyString_FromStringAndSize(NULL, (int)len - 1); 6806 if (newstr == NULL) 6807 return PyErr_NoMemory(); 6808 newenv = PyString_AS_STRING(newstr); 6809 PyOS_snprintf(newenv, len, "%s=%s", s1, s2); 6810 if (putenv(newenv)) { 6811 Py_DECREF(newstr); 6812 posix_error(); 6813 return NULL; 6814 } 6815 /* Install the first arg and newstr in posix_putenv_garbage; 6816 * this will cause previous value to be collected. This has to 6817 * happen after the real putenv() call because the old value 6818 * was still accessible until then. */ 6819 if (PyDict_SetItem(posix_putenv_garbage, 6820 PyTuple_GET_ITEM(args, 0), newstr)) { 6821 /* really not much we can do; just leak */ 6822 PyErr_Clear(); 6823 } 6824 else { 6825 Py_DECREF(newstr); 6826 } 7110 /* XXX This can leak memory -- not easy to fix :-( */ 7111 len = strlen(s1) + strlen(s2) + 2; 7112 #ifdef MS_WINDOWS 7113 if (_MAX_ENV < (len - 1)) { 7114 PyErr_Format(PyExc_ValueError, 7115 "the environment variable is longer than %u bytes", 7116 _MAX_ENV); 7117 return NULL; 7118 } 7119 #endif 7120 /* len includes space for a trailing \0; the size arg to 7121 PyString_FromStringAndSize does not count that */ 7122 newstr = PyString_FromStringAndSize(NULL, (int)len - 1); 7123 if (newstr == NULL) 7124 return PyErr_NoMemory(); 7125 newenv = PyString_AS_STRING(newstr); 7126 PyOS_snprintf(newenv, len, "%s=%s", s1, s2); 7127 if (putenv(newenv)) { 7128 Py_DECREF(newstr); 7129 posix_error(); 7130 return NULL; 7131 } 7132 /* Install the first arg and newstr in posix_putenv_garbage; 7133 * this will cause previous value to be collected. This has to 7134 * happen after the real putenv() call because the old value 7135 * was still accessible until then. */ 7136 if (PyDict_SetItem(posix_putenv_garbage, 7137 PyTuple_GET_ITEM(args, 0), newstr)) { 7138 /* really not much we can do; just leak */ 7139 PyErr_Clear(); 7140 } 7141 else { 7142 Py_DECREF(newstr); 7143 } 6827 7144 6828 7145 #if defined(PYOS_OS2) 6829 7146 } 6830 7147 #endif 6831 6832 7148 Py_INCREF(Py_None); 7149 return Py_None; 6833 7150 } 6834 7151 #endif /* putenv */ … … 6842 7159 posix_unsetenv(PyObject *self, PyObject *args) 6843 7160 { 6844 char *s1; 6845 6846 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1)) 6847 return NULL; 6848 6849 unsetenv(s1); 6850 6851 /* Remove the key from posix_putenv_garbage; 6852 * this will cause it to be collected. This has to 6853 * happen after the real unsetenv() call because the 6854 * old value was still accessible until then. 6855 */ 6856 if (PyDict_DelItem(posix_putenv_garbage, 6857 PyTuple_GET_ITEM(args, 0))) { 6858 /* really not much we can do; just leak */ 6859 PyErr_Clear(); 6860 } 6861 6862 Py_INCREF(Py_None); 6863 return Py_None; 7161 char *s1; 7162 #ifndef HAVE_BROKEN_UNSETENV 7163 int err; 7164 #endif 7165 7166 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1)) 7167 return NULL; 7168 7169 #ifdef HAVE_BROKEN_UNSETENV 7170 unsetenv(s1); 7171 #else 7172 err = unsetenv(s1); 7173 if (err) 7174 return posix_error(); 7175 #endif 7176 7177 /* Remove the key from posix_putenv_garbage; 7178 * this will cause it to be collected. This has to 7179 * happen after the real unsetenv() call because the 7180 * old value was still accessible until then. 7181 */ 7182 if (PyDict_DelItem(posix_putenv_garbage, 7183 PyTuple_GET_ITEM(args, 0))) { 7184 /* really not much we can do; just leak */ 7185 PyErr_Clear(); 7186 } 7187 7188 Py_INCREF(Py_None); 7189 return Py_None; 6864 7190 } 6865 7191 #endif /* unsetenv */ … … 6872 7198 posix_strerror(PyObject *self, PyObject *args) 6873 7199 { 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 7200 int code; 7201 char *message; 7202 if (!PyArg_ParseTuple(args, "i:strerror", &code)) 7203 return NULL; 7204 message = strerror(code); 7205 if (message == NULL) { 7206 PyErr_SetString(PyExc_ValueError, 7207 "strerror() argument out of range"); 7208 return NULL; 7209 } 7210 return PyString_FromString(message); 6885 7211 } 6886 7212 … … 6896 7222 posix_WCOREDUMP(PyObject *self, PyObject *args) 6897 7223 { 6898 6899 6900 6901 6902 6903 6904 7224 WAIT_TYPE status; 7225 WAIT_STATUS_INT(status) = 0; 7226 7227 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status))) 7228 return NULL; 7229 7230 return PyBool_FromLong(WCOREDUMP(status)); 6905 7231 } 6906 7232 #endif /* WCOREDUMP */ … … 6915 7241 posix_WIFCONTINUED(PyObject *self, PyObject *args) 6916 7242 { 6917 6918 6919 6920 6921 6922 6923 7243 WAIT_TYPE status; 7244 WAIT_STATUS_INT(status) = 0; 7245 7246 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status))) 7247 return NULL; 7248 7249 return PyBool_FromLong(WIFCONTINUED(status)); 6924 7250 } 6925 7251 #endif /* WIFCONTINUED */ … … 6933 7259 posix_WIFSTOPPED(PyObject *self, PyObject *args) 6934 7260 { 6935 6936 6937 6938 6939 6940 6941 7261 WAIT_TYPE status; 7262 WAIT_STATUS_INT(status) = 0; 7263 7264 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status))) 7265 return NULL; 7266 7267 return PyBool_FromLong(WIFSTOPPED(status)); 6942 7268 } 6943 7269 #endif /* WIFSTOPPED */ … … 6951 7277 posix_WIFSIGNALED(PyObject *self, PyObject *args) 6952 7278 { 6953 6954 6955 6956 6957 6958 6959 7279 WAIT_TYPE status; 7280 WAIT_STATUS_INT(status) = 0; 7281 7282 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status))) 7283 return NULL; 7284 7285 return PyBool_FromLong(WIFSIGNALED(status)); 6960 7286 } 6961 7287 #endif /* WIFSIGNALED */ … … 6970 7296 posix_WIFEXITED(PyObject *self, PyObject *args) 6971 7297 { 6972 6973 6974 6975 6976 6977 6978 7298 WAIT_TYPE status; 7299 WAIT_STATUS_INT(status) = 0; 7300 7301 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status))) 7302 return NULL; 7303 7304 return PyBool_FromLong(WIFEXITED(status)); 6979 7305 } 6980 7306 #endif /* WIFEXITED */ … … 6988 7314 posix_WEXITSTATUS(PyObject *self, PyObject *args) 6989 7315 { 6990 6991 6992 6993 6994 6995 6996 7316 WAIT_TYPE status; 7317 WAIT_STATUS_INT(status) = 0; 7318 7319 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status))) 7320 return NULL; 7321 7322 return Py_BuildValue("i", WEXITSTATUS(status)); 6997 7323 } 6998 7324 #endif /* WEXITSTATUS */ … … 7007 7333 posix_WTERMSIG(PyObject *self, PyObject *args) 7008 7334 { 7009 7010 7011 7012 7013 7014 7015 7335 WAIT_TYPE status; 7336 WAIT_STATUS_INT(status) = 0; 7337 7338 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status))) 7339 return NULL; 7340 7341 return Py_BuildValue("i", WTERMSIG(status)); 7016 7342 } 7017 7343 #endif /* WTERMSIG */ … … 7026 7352 posix_WSTOPSIG(PyObject *self, PyObject *args) 7027 7353 { 7028 7029 7030 7031 7032 7033 7034 7354 WAIT_TYPE status; 7355 WAIT_STATUS_INT(status) = 0; 7356 7357 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status))) 7358 return NULL; 7359 7360 return Py_BuildValue("i", WSTOPSIG(status)); 7035 7361 } 7036 7362 #endif /* WSTOPSIG */ … … 7049 7375 static PyObject* 7050 7376 _pystatvfs_fromstructstatvfs(struct statvfs st) { 7051 7052 7053 7377 PyObject *v = PyStructSequence_New(&StatVFSResultType); 7378 if (v == NULL) 7379 return NULL; 7054 7380 7055 7381 #if !defined(HAVE_LARGEFILE_SUPPORT) 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7382 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize)); 7383 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize)); 7384 PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long) st.f_blocks)); 7385 PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long) st.f_bfree)); 7386 PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long) st.f_bavail)); 7387 PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long) st.f_files)); 7388 PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong((long) st.f_ffree)); 7389 PyStructSequence_SET_ITEM(v, 7, PyInt_FromLong((long) st.f_favail)); 7390 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag)); 7391 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax)); 7066 7392 #else 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 #endif 7084 7085 7393 PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long) st.f_bsize)); 7394 PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long) st.f_frsize)); 7395 PyStructSequence_SET_ITEM(v, 2, 7396 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks)); 7397 PyStructSequence_SET_ITEM(v, 3, 7398 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree)); 7399 PyStructSequence_SET_ITEM(v, 4, 7400 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail)); 7401 PyStructSequence_SET_ITEM(v, 5, 7402 PyLong_FromLongLong((PY_LONG_LONG) st.f_files)); 7403 PyStructSequence_SET_ITEM(v, 6, 7404 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree)); 7405 PyStructSequence_SET_ITEM(v, 7, 7406 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail)); 7407 PyStructSequence_SET_ITEM(v, 8, PyInt_FromLong((long) st.f_flag)); 7408 PyStructSequence_SET_ITEM(v, 9, PyInt_FromLong((long) st.f_namemax)); 7409 #endif 7410 7411 return v; 7086 7412 } 7087 7413 … … 7093 7419 posix_fstatvfs(PyObject *self, PyObject *args) 7094 7420 { 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7421 int fd, res; 7422 struct statvfs st; 7423 7424 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd)) 7425 return NULL; 7426 Py_BEGIN_ALLOW_THREADS 7427 res = fstatvfs(fd, &st); 7428 Py_END_ALLOW_THREADS 7429 if (res != 0) 7430 return posix_error(); 7431 7432 return _pystatvfs_fromstructstatvfs(st); 7107 7433 } 7108 7434 #endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */ … … 7119 7445 posix_statvfs(PyObject *self, PyObject *args) 7120 7446 { 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7447 char *path; 7448 int res; 7449 struct statvfs st; 7450 if (!PyArg_ParseTuple(args, "s:statvfs", &path)) 7451 return NULL; 7452 Py_BEGIN_ALLOW_THREADS 7453 res = statvfs(path, &st); 7454 Py_END_ALLOW_THREADS 7455 if (res != 0) 7456 return posix_error_with_filename(path); 7457 7458 return _pystatvfs_fromstructstatvfs(st); 7133 7459 } 7134 7460 #endif /* HAVE_STATVFS */ … … 7151 7477 7152 7478 if (!PyArg_ParseTuple(args, "|zz:tempnam", &dir, &pfx)) 7153 7479 return NULL; 7154 7480 7155 7481 if (PyErr_Warn(PyExc_RuntimeWarning, 7156 "tempnam is a potential security risk to your program") < 0) 7157 return NULL; 7482 "tempnam is a potential security risk to your program") < 0) 7483 return NULL; 7484 7485 if (PyErr_WarnPy3k("tempnam has been removed in 3.x; " 7486 "use the tempfile module", 1) < 0) 7487 return NULL; 7158 7488 7159 7489 #ifdef MS_WINDOWS … … 7181 7511 FILE *fp; 7182 7512 7513 if (PyErr_WarnPy3k("tmpfile has been removed in 3.x; " 7514 "use the tempfile module", 1) < 0) 7515 return NULL; 7516 7183 7517 fp = tmpfile(); 7184 7518 if (fp == NULL) … … 7201 7535 7202 7536 if (PyErr_Warn(PyExc_RuntimeWarning, 7203 "tmpnam is a potential security risk to your program") < 0) 7204 return NULL; 7537 "tmpnam is a potential security risk to your program") < 0) 7538 return NULL; 7539 7540 if (PyErr_WarnPy3k("tmpnam has been removed in 3.x; " 7541 "use the tempfile module", 1) < 0) 7542 return NULL; 7205 7543 7206 7544 #ifdef USE_TMPNAM_R … … 7210 7548 #endif 7211 7549 if (name == NULL) { 7212 7550 PyObject *err = Py_BuildValue("is", 0, 7213 7551 #ifdef USE_TMPNAM_R 7214 7552 "unexpected NULL from tmpnam_r" … … 7217 7555 #endif 7218 7556 ); 7219 7220 7221 7557 PyErr_SetObject(PyExc_OSError, err); 7558 Py_XDECREF(err); 7559 return NULL; 7222 7560 } 7223 7561 return PyString_FromString(buffer); … … 7244 7582 static int 7245 7583 conv_confname(PyObject *arg, int *valuep, struct constdef *table, 7246 7584 size_t tablesize) 7247 7585 { 7248 7586 if (PyInt_Check(arg)) { … … 7253 7591 /* look up the value in the table using a binary search */ 7254 7592 size_t lo = 0; 7255 7593 size_t mid; 7256 7594 size_t hi = tablesize; 7257 7595 int cmp; … … 7281 7619 static struct constdef posix_constants_pathconf[] = { 7282 7620 #ifdef _PC_ABI_AIO_XFER_MAX 7283 {"PC_ABI_AIO_XFER_MAX", 7621 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, 7284 7622 #endif 7285 7623 #ifdef _PC_ABI_ASYNC_IO 7286 {"PC_ABI_ASYNC_IO", 7624 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, 7287 7625 #endif 7288 7626 #ifdef _PC_ASYNC_IO 7289 {"PC_ASYNC_IO", 7627 {"PC_ASYNC_IO", _PC_ASYNC_IO}, 7290 7628 #endif 7291 7629 #ifdef _PC_CHOWN_RESTRICTED 7292 {"PC_CHOWN_RESTRICTED", 7630 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, 7293 7631 #endif 7294 7632 #ifdef _PC_FILESIZEBITS 7295 {"PC_FILESIZEBITS", 7633 {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, 7296 7634 #endif 7297 7635 #ifdef _PC_LAST 7298 {"PC_LAST", 7636 {"PC_LAST", _PC_LAST}, 7299 7637 #endif 7300 7638 #ifdef _PC_LINK_MAX 7301 {"PC_LINK_MAX", 7639 {"PC_LINK_MAX", _PC_LINK_MAX}, 7302 7640 #endif 7303 7641 #ifdef _PC_MAX_CANON 7304 {"PC_MAX_CANON", 7642 {"PC_MAX_CANON", _PC_MAX_CANON}, 7305 7643 #endif 7306 7644 #ifdef _PC_MAX_INPUT 7307 {"PC_MAX_INPUT", 7645 {"PC_MAX_INPUT", _PC_MAX_INPUT}, 7308 7646 #endif 7309 7647 #ifdef _PC_NAME_MAX 7310 {"PC_NAME_MAX", 7648 {"PC_NAME_MAX", _PC_NAME_MAX}, 7311 7649 #endif 7312 7650 #ifdef _PC_NO_TRUNC 7313 {"PC_NO_TRUNC", 7651 {"PC_NO_TRUNC", _PC_NO_TRUNC}, 7314 7652 #endif 7315 7653 #ifdef _PC_PATH_MAX 7316 {"PC_PATH_MAX", 7654 {"PC_PATH_MAX", _PC_PATH_MAX}, 7317 7655 #endif 7318 7656 #ifdef _PC_PIPE_BUF 7319 {"PC_PIPE_BUF", 7657 {"PC_PIPE_BUF", _PC_PIPE_BUF}, 7320 7658 #endif 7321 7659 #ifdef _PC_PRIO_IO 7322 {"PC_PRIO_IO", 7660 {"PC_PRIO_IO", _PC_PRIO_IO}, 7323 7661 #endif 7324 7662 #ifdef _PC_SOCK_MAXBUF 7325 {"PC_SOCK_MAXBUF", 7663 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, 7326 7664 #endif 7327 7665 #ifdef _PC_SYNC_IO 7328 {"PC_SYNC_IO", 7666 {"PC_SYNC_IO", _PC_SYNC_IO}, 7329 7667 #endif 7330 7668 #ifdef _PC_VDISABLE 7331 {"PC_VDISABLE", 7669 {"PC_VDISABLE", _PC_VDISABLE}, 7332 7670 #endif 7333 7671 }; … … 7385 7723 if (PyArg_ParseTuple(args, "sO&:pathconf", &path, 7386 7724 conv_path_confname, &name)) { 7387 long limit; 7388 7389 errno = 0; 7390 limit = pathconf(path, name); 7391 if (limit == -1 && errno != 0) { 7392 if (errno == EINVAL) 7393 /* could be a path or name problem */ 7394 posix_error(); 7395 else 7396 posix_error_with_filename(path); 7397 } 7725 long limit; 7726 7727 errno = 0; 7728 limit = pathconf(path, name); 7729 if (limit == -1 && errno != 0) { 7730 if (errno == EINVAL) 7731 /* could be a path or name problem */ 7732 posix_error(); 7398 7733 else 7399 result = PyInt_FromLong(limit); 7734 posix_error_with_filename(path); 7735 } 7736 else 7737 result = PyInt_FromLong(limit); 7400 7738 } 7401 7739 return result; … … 7406 7744 static struct constdef posix_constants_confstr[] = { 7407 7745 #ifdef _CS_ARCHITECTURE 7408 {"CS_ARCHITECTURE", 7746 {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, 7409 7747 #endif 7410 7748 #ifdef _CS_HOSTNAME 7411 {"CS_HOSTNAME", 7749 {"CS_HOSTNAME", _CS_HOSTNAME}, 7412 7750 #endif 7413 7751 #ifdef _CS_HW_PROVIDER 7414 {"CS_HW_PROVIDER", 7752 {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, 7415 7753 #endif 7416 7754 #ifdef _CS_HW_SERIAL 7417 {"CS_HW_SERIAL", 7755 {"CS_HW_SERIAL", _CS_HW_SERIAL}, 7418 7756 #endif 7419 7757 #ifdef _CS_INITTAB_NAME 7420 {"CS_INITTAB_NAME", 7758 {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, 7421 7759 #endif 7422 7760 #ifdef _CS_LFS64_CFLAGS 7423 {"CS_LFS64_CFLAGS", 7761 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, 7424 7762 #endif 7425 7763 #ifdef _CS_LFS64_LDFLAGS 7426 {"CS_LFS64_LDFLAGS", 7764 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, 7427 7765 #endif 7428 7766 #ifdef _CS_LFS64_LIBS 7429 {"CS_LFS64_LIBS", 7767 {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, 7430 7768 #endif 7431 7769 #ifdef _CS_LFS64_LINTFLAGS 7432 {"CS_LFS64_LINTFLAGS", 7770 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, 7433 7771 #endif 7434 7772 #ifdef _CS_LFS_CFLAGS 7435 {"CS_LFS_CFLAGS", 7773 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, 7436 7774 #endif 7437 7775 #ifdef _CS_LFS_LDFLAGS 7438 {"CS_LFS_LDFLAGS", 7776 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, 7439 7777 #endif 7440 7778 #ifdef _CS_LFS_LIBS 7441 {"CS_LFS_LIBS", 7779 {"CS_LFS_LIBS", _CS_LFS_LIBS}, 7442 7780 #endif 7443 7781 #ifdef _CS_LFS_LINTFLAGS 7444 {"CS_LFS_LINTFLAGS", 7782 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, 7445 7783 #endif 7446 7784 #ifdef _CS_MACHINE 7447 {"CS_MACHINE", 7785 {"CS_MACHINE", _CS_MACHINE}, 7448 7786 #endif 7449 7787 #ifdef _CS_PATH 7450 {"CS_PATH", 7788 {"CS_PATH", _CS_PATH}, 7451 7789 #endif 7452 7790 #ifdef _CS_RELEASE 7453 {"CS_RELEASE", 7791 {"CS_RELEASE", _CS_RELEASE}, 7454 7792 #endif 7455 7793 #ifdef _CS_SRPC_DOMAIN 7456 {"CS_SRPC_DOMAIN", 7794 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, 7457 7795 #endif 7458 7796 #ifdef _CS_SYSNAME 7459 {"CS_SYSNAME", 7797 {"CS_SYSNAME", _CS_SYSNAME}, 7460 7798 #endif 7461 7799 #ifdef _CS_VERSION 7462 {"CS_VERSION", 7800 {"CS_VERSION", _CS_VERSION}, 7463 7801 #endif 7464 7802 #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS 7465 {"CS_XBS5_ILP32_OFF32_CFLAGS", 7803 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, 7466 7804 #endif 7467 7805 #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS 7468 {"CS_XBS5_ILP32_OFF32_LDFLAGS", 7806 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, 7469 7807 #endif 7470 7808 #ifdef _CS_XBS5_ILP32_OFF32_LIBS 7471 {"CS_XBS5_ILP32_OFF32_LIBS", 7809 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, 7472 7810 #endif 7473 7811 #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS 7474 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", 7812 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, 7475 7813 #endif 7476 7814 #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS 7477 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", 7815 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, 7478 7816 #endif 7479 7817 #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS 7480 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", 7818 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, 7481 7819 #endif 7482 7820 #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS 7483 {"CS_XBS5_ILP32_OFFBIG_LIBS", 7821 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, 7484 7822 #endif 7485 7823 #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS 7486 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", 7824 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, 7487 7825 #endif 7488 7826 #ifdef _CS_XBS5_LP64_OFF64_CFLAGS 7489 {"CS_XBS5_LP64_OFF64_CFLAGS", 7827 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, 7490 7828 #endif 7491 7829 #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS 7492 {"CS_XBS5_LP64_OFF64_LDFLAGS", 7830 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, 7493 7831 #endif 7494 7832 #ifdef _CS_XBS5_LP64_OFF64_LIBS 7495 {"CS_XBS5_LP64_OFF64_LIBS", 7833 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, 7496 7834 #endif 7497 7835 #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS 7498 {"CS_XBS5_LP64_OFF64_LINTFLAGS", 7836 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, 7499 7837 #endif 7500 7838 #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS 7501 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", 7839 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, 7502 7840 #endif 7503 7841 #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS 7504 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", 7842 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, 7505 7843 #endif 7506 7844 #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS 7507 {"CS_XBS5_LPBIG_OFFBIG_LIBS", 7845 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, 7508 7846 #endif 7509 7847 #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS 7510 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", 7848 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, 7511 7849 #endif 7512 7850 #ifdef _MIPS_CS_AVAIL_PROCESSORS 7513 {"MIPS_CS_AVAIL_PROCESSORS", 7851 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, 7514 7852 #endif 7515 7853 #ifdef _MIPS_CS_BASE 7516 {"MIPS_CS_BASE", 7854 {"MIPS_CS_BASE", _MIPS_CS_BASE}, 7517 7855 #endif 7518 7856 #ifdef _MIPS_CS_HOSTID 7519 {"MIPS_CS_HOSTID", 7857 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, 7520 7858 #endif 7521 7859 #ifdef _MIPS_CS_HW_NAME 7522 {"MIPS_CS_HW_NAME", 7860 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, 7523 7861 #endif 7524 7862 #ifdef _MIPS_CS_NUM_PROCESSORS 7525 {"MIPS_CS_NUM_PROCESSORS", 7863 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, 7526 7864 #endif 7527 7865 #ifdef _MIPS_CS_OSREL_MAJ 7528 {"MIPS_CS_OSREL_MAJ", 7866 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, 7529 7867 #endif 7530 7868 #ifdef _MIPS_CS_OSREL_MIN 7531 {"MIPS_CS_OSREL_MIN", 7869 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, 7532 7870 #endif 7533 7871 #ifdef _MIPS_CS_OSREL_PATCH 7534 {"MIPS_CS_OSREL_PATCH", 7872 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, 7535 7873 #endif 7536 7874 #ifdef _MIPS_CS_OS_NAME 7537 {"MIPS_CS_OS_NAME", 7875 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, 7538 7876 #endif 7539 7877 #ifdef _MIPS_CS_OS_PROVIDER 7540 {"MIPS_CS_OS_PROVIDER", 7878 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, 7541 7879 #endif 7542 7880 #ifdef _MIPS_CS_PROCESSORS 7543 {"MIPS_CS_PROCESSORS", 7881 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, 7544 7882 #endif 7545 7883 #ifdef _MIPS_CS_SERIAL 7546 {"MIPS_CS_SERIAL", 7884 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, 7547 7885 #endif 7548 7886 #ifdef _MIPS_CS_VENDOR 7549 {"MIPS_CS_VENDOR", 7887 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, 7550 7888 #endif 7551 7889 }; … … 7571 7909 7572 7910 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) { 7573 int len; 7574 7575 errno = 0; 7576 len = confstr(name, buffer, sizeof(buffer)); 7577 if (len == 0) { 7578 if (errno) { 7579 posix_error(); 7580 } 7581 else { 7582 result = Py_None; 7583 Py_INCREF(Py_None); 7584 } 7911 int len; 7912 7913 errno = 0; 7914 len = confstr(name, buffer, sizeof(buffer)); 7915 if (len == 0) { 7916 if (errno) { 7917 posix_error(); 7585 7918 } 7586 7919 else { 7587 if ((unsigned int)len >= sizeof(buffer)) { 7588 result = PyString_FromStringAndSize(NULL, len-1); 7589 if (result != NULL) 7590 confstr(name, PyString_AS_STRING(result), len); 7591 } 7592 else 7593 result = PyString_FromStringAndSize(buffer, len-1); 7920 result = Py_None; 7921 Py_INCREF(Py_None); 7594 7922 } 7923 } 7924 else { 7925 if ((unsigned int)len >= sizeof(buffer)) { 7926 result = PyString_FromStringAndSize(NULL, len-1); 7927 if (result != NULL) 7928 confstr(name, PyString_AS_STRING(result), len); 7929 } 7930 else 7931 result = PyString_FromStringAndSize(buffer, len-1); 7932 } 7595 7933 } 7596 7934 return result; … … 7602 7940 static struct constdef posix_constants_sysconf[] = { 7603 7941 #ifdef _SC_2_CHAR_TERM 7604 {"SC_2_CHAR_TERM", 7942 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, 7605 7943 #endif 7606 7944 #ifdef _SC_2_C_BIND 7607 {"SC_2_C_BIND", 7945 {"SC_2_C_BIND", _SC_2_C_BIND}, 7608 7946 #endif 7609 7947 #ifdef _SC_2_C_DEV 7610 {"SC_2_C_DEV", 7948 {"SC_2_C_DEV", _SC_2_C_DEV}, 7611 7949 #endif 7612 7950 #ifdef _SC_2_C_VERSION 7613 {"SC_2_C_VERSION", 7951 {"SC_2_C_VERSION", _SC_2_C_VERSION}, 7614 7952 #endif 7615 7953 #ifdef _SC_2_FORT_DEV 7616 {"SC_2_FORT_DEV", 7954 {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, 7617 7955 #endif 7618 7956 #ifdef _SC_2_FORT_RUN 7619 {"SC_2_FORT_RUN", 7957 {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, 7620 7958 #endif 7621 7959 #ifdef _SC_2_LOCALEDEF 7622 {"SC_2_LOCALEDEF", 7960 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, 7623 7961 #endif 7624 7962 #ifdef _SC_2_SW_DEV 7625 {"SC_2_SW_DEV", 7963 {"SC_2_SW_DEV", _SC_2_SW_DEV}, 7626 7964 #endif 7627 7965 #ifdef _SC_2_UPE 7628 {"SC_2_UPE", 7966 {"SC_2_UPE", _SC_2_UPE}, 7629 7967 #endif 7630 7968 #ifdef _SC_2_VERSION 7631 {"SC_2_VERSION", 7969 {"SC_2_VERSION", _SC_2_VERSION}, 7632 7970 #endif 7633 7971 #ifdef _SC_ABI_ASYNCHRONOUS_IO 7634 {"SC_ABI_ASYNCHRONOUS_IO", 7972 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, 7635 7973 #endif 7636 7974 #ifdef _SC_ACL 7637 {"SC_ACL", 7975 {"SC_ACL", _SC_ACL}, 7638 7976 #endif 7639 7977 #ifdef _SC_AIO_LISTIO_MAX 7640 {"SC_AIO_LISTIO_MAX", 7978 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, 7641 7979 #endif 7642 7980 #ifdef _SC_AIO_MAX 7643 {"SC_AIO_MAX", 7981 {"SC_AIO_MAX", _SC_AIO_MAX}, 7644 7982 #endif 7645 7983 #ifdef _SC_AIO_PRIO_DELTA_MAX 7646 {"SC_AIO_PRIO_DELTA_MAX", 7984 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, 7647 7985 #endif 7648 7986 #ifdef _SC_ARG_MAX 7649 {"SC_ARG_MAX", 7987 {"SC_ARG_MAX", _SC_ARG_MAX}, 7650 7988 #endif 7651 7989 #ifdef _SC_ASYNCHRONOUS_IO 7652 {"SC_ASYNCHRONOUS_IO", 7990 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, 7653 7991 #endif 7654 7992 #ifdef _SC_ATEXIT_MAX 7655 {"SC_ATEXIT_MAX", 7993 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, 7656 7994 #endif 7657 7995 #ifdef _SC_AUDIT 7658 {"SC_AUDIT", 7996 {"SC_AUDIT", _SC_AUDIT}, 7659 7997 #endif 7660 7998 #ifdef _SC_AVPHYS_PAGES 7661 {"SC_AVPHYS_PAGES", 7999 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, 7662 8000 #endif 7663 8001 #ifdef _SC_BC_BASE_MAX 7664 {"SC_BC_BASE_MAX", 8002 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, 7665 8003 #endif 7666 8004 #ifdef _SC_BC_DIM_MAX 7667 {"SC_BC_DIM_MAX", 8005 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, 7668 8006 #endif 7669 8007 #ifdef _SC_BC_SCALE_MAX 7670 {"SC_BC_SCALE_MAX", 8008 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, 7671 8009 #endif 7672 8010 #ifdef _SC_BC_STRING_MAX 7673 {"SC_BC_STRING_MAX", 8011 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, 7674 8012 #endif 7675 8013 #ifdef _SC_CAP 7676 {"SC_CAP", 8014 {"SC_CAP", _SC_CAP}, 7677 8015 #endif 7678 8016 #ifdef _SC_CHARCLASS_NAME_MAX 7679 {"SC_CHARCLASS_NAME_MAX", 8017 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, 7680 8018 #endif 7681 8019 #ifdef _SC_CHAR_BIT 7682 {"SC_CHAR_BIT", 8020 {"SC_CHAR_BIT", _SC_CHAR_BIT}, 7683 8021 #endif 7684 8022 #ifdef _SC_CHAR_MAX 7685 {"SC_CHAR_MAX", 8023 {"SC_CHAR_MAX", _SC_CHAR_MAX}, 7686 8024 #endif 7687 8025 #ifdef _SC_CHAR_MIN 7688 {"SC_CHAR_MIN", 8026 {"SC_CHAR_MIN", _SC_CHAR_MIN}, 7689 8027 #endif 7690 8028 #ifdef _SC_CHILD_MAX 7691 {"SC_CHILD_MAX", 8029 {"SC_CHILD_MAX", _SC_CHILD_MAX}, 7692 8030 #endif 7693 8031 #ifdef _SC_CLK_TCK 7694 {"SC_CLK_TCK", 8032 {"SC_CLK_TCK", _SC_CLK_TCK}, 7695 8033 #endif 7696 8034 #ifdef _SC_COHER_BLKSZ 7697 {"SC_COHER_BLKSZ", 8035 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, 7698 8036 #endif 7699 8037 #ifdef _SC_COLL_WEIGHTS_MAX 7700 {"SC_COLL_WEIGHTS_MAX", 8038 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, 7701 8039 #endif 7702 8040 #ifdef _SC_DCACHE_ASSOC 7703 {"SC_DCACHE_ASSOC", 8041 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, 7704 8042 #endif 7705 8043 #ifdef _SC_DCACHE_BLKSZ 7706 {"SC_DCACHE_BLKSZ", 8044 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, 7707 8045 #endif 7708 8046 #ifdef _SC_DCACHE_LINESZ 7709 {"SC_DCACHE_LINESZ", 8047 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, 7710 8048 #endif 7711 8049 #ifdef _SC_DCACHE_SZ 7712 {"SC_DCACHE_SZ", 8050 {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, 7713 8051 #endif 7714 8052 #ifdef _SC_DCACHE_TBLKSZ 7715 {"SC_DCACHE_TBLKSZ", 8053 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, 7716 8054 #endif 7717 8055 #ifdef _SC_DELAYTIMER_MAX 7718 {"SC_DELAYTIMER_MAX", 8056 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, 7719 8057 #endif 7720 8058 #ifdef _SC_EQUIV_CLASS_MAX 7721 {"SC_EQUIV_CLASS_MAX", 8059 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, 7722 8060 #endif 7723 8061 #ifdef _SC_EXPR_NEST_MAX 7724 {"SC_EXPR_NEST_MAX", 8062 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, 7725 8063 #endif 7726 8064 #ifdef _SC_FSYNC 7727 {"SC_FSYNC", 8065 {"SC_FSYNC", _SC_FSYNC}, 7728 8066 #endif 7729 8067 #ifdef _SC_GETGR_R_SIZE_MAX 7730 {"SC_GETGR_R_SIZE_MAX", 8068 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, 7731 8069 #endif 7732 8070 #ifdef _SC_GETPW_R_SIZE_MAX 7733 {"SC_GETPW_R_SIZE_MAX", 8071 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, 7734 8072 #endif 7735 8073 #ifdef _SC_ICACHE_ASSOC 7736 {"SC_ICACHE_ASSOC", 8074 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, 7737 8075 #endif 7738 8076 #ifdef _SC_ICACHE_BLKSZ 7739 {"SC_ICACHE_BLKSZ", 8077 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, 7740 8078 #endif 7741 8079 #ifdef _SC_ICACHE_LINESZ 7742 {"SC_ICACHE_LINESZ", 8080 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, 7743 8081 #endif 7744 8082 #ifdef _SC_ICACHE_SZ 7745 {"SC_ICACHE_SZ", 8083 {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, 7746 8084 #endif 7747 8085 #ifdef _SC_INF 7748 {"SC_INF", 8086 {"SC_INF", _SC_INF}, 7749 8087 #endif 7750 8088 #ifdef _SC_INT_MAX 7751 {"SC_INT_MAX", 8089 {"SC_INT_MAX", _SC_INT_MAX}, 7752 8090 #endif 7753 8091 #ifdef _SC_INT_MIN 7754 {"SC_INT_MIN", 8092 {"SC_INT_MIN", _SC_INT_MIN}, 7755 8093 #endif 7756 8094 #ifdef _SC_IOV_MAX 7757 {"SC_IOV_MAX", 8095 {"SC_IOV_MAX", _SC_IOV_MAX}, 7758 8096 #endif 7759 8097 #ifdef _SC_IP_SECOPTS 7760 {"SC_IP_SECOPTS", 8098 {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, 7761 8099 #endif 7762 8100 #ifdef _SC_JOB_CONTROL 7763 {"SC_JOB_CONTROL", 8101 {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, 7764 8102 #endif 7765 8103 #ifdef _SC_KERN_POINTERS 7766 {"SC_KERN_POINTERS", 8104 {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, 7767 8105 #endif 7768 8106 #ifdef _SC_KERN_SIM 7769 {"SC_KERN_SIM", 8107 {"SC_KERN_SIM", _SC_KERN_SIM}, 7770 8108 #endif 7771 8109 #ifdef _SC_LINE_MAX 7772 {"SC_LINE_MAX", 8110 {"SC_LINE_MAX", _SC_LINE_MAX}, 7773 8111 #endif 7774 8112 #ifdef _SC_LOGIN_NAME_MAX 7775 {"SC_LOGIN_NAME_MAX", 8113 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, 7776 8114 #endif 7777 8115 #ifdef _SC_LOGNAME_MAX 7778 {"SC_LOGNAME_MAX", 8116 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, 7779 8117 #endif 7780 8118 #ifdef _SC_LONG_BIT 7781 {"SC_LONG_BIT", 8119 {"SC_LONG_BIT", _SC_LONG_BIT}, 7782 8120 #endif 7783 8121 #ifdef _SC_MAC 7784 {"SC_MAC", 8122 {"SC_MAC", _SC_MAC}, 7785 8123 #endif 7786 8124 #ifdef _SC_MAPPED_FILES 7787 {"SC_MAPPED_FILES", 8125 {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, 7788 8126 #endif 7789 8127 #ifdef _SC_MAXPID 7790 {"SC_MAXPID", 8128 {"SC_MAXPID", _SC_MAXPID}, 7791 8129 #endif 7792 8130 #ifdef _SC_MB_LEN_MAX 7793 {"SC_MB_LEN_MAX", 8131 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, 7794 8132 #endif 7795 8133 #ifdef _SC_MEMLOCK 7796 {"SC_MEMLOCK", 8134 {"SC_MEMLOCK", _SC_MEMLOCK}, 7797 8135 #endif 7798 8136 #ifdef _SC_MEMLOCK_RANGE 7799 {"SC_MEMLOCK_RANGE", 8137 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, 7800 8138 #endif 7801 8139 #ifdef _SC_MEMORY_PROTECTION 7802 {"SC_MEMORY_PROTECTION", 8140 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, 7803 8141 #endif 7804 8142 #ifdef _SC_MESSAGE_PASSING 7805 {"SC_MESSAGE_PASSING", 8143 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, 7806 8144 #endif 7807 8145 #ifdef _SC_MMAP_FIXED_ALIGNMENT 7808 {"SC_MMAP_FIXED_ALIGNMENT", 8146 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, 7809 8147 #endif 7810 8148 #ifdef _SC_MQ_OPEN_MAX 7811 {"SC_MQ_OPEN_MAX", 8149 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, 7812 8150 #endif 7813 8151 #ifdef _SC_MQ_PRIO_MAX 7814 {"SC_MQ_PRIO_MAX", 8152 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, 7815 8153 #endif 7816 8154 #ifdef _SC_NACLS_MAX 7817 {"SC_NACLS_MAX", 8155 {"SC_NACLS_MAX", _SC_NACLS_MAX}, 7818 8156 #endif 7819 8157 #ifdef _SC_NGROUPS_MAX 7820 {"SC_NGROUPS_MAX", 8158 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, 7821 8159 #endif 7822 8160 #ifdef _SC_NL_ARGMAX 7823 {"SC_NL_ARGMAX", 8161 {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, 7824 8162 #endif 7825 8163 #ifdef _SC_NL_LANGMAX 7826 {"SC_NL_LANGMAX", 8164 {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, 7827 8165 #endif 7828 8166 #ifdef _SC_NL_MSGMAX 7829 {"SC_NL_MSGMAX", 8167 {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, 7830 8168 #endif 7831 8169 #ifdef _SC_NL_NMAX 7832 {"SC_NL_NMAX", 8170 {"SC_NL_NMAX", _SC_NL_NMAX}, 7833 8171 #endif 7834 8172 #ifdef _SC_NL_SETMAX 7835 {"SC_NL_SETMAX", 8173 {"SC_NL_SETMAX", _SC_NL_SETMAX}, 7836 8174 #endif 7837 8175 #ifdef _SC_NL_TEXTMAX 7838 {"SC_NL_TEXTMAX", 8176 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, 7839 8177 #endif 7840 8178 #ifdef _SC_NPROCESSORS_CONF 7841 {"SC_NPROCESSORS_CONF", 8179 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, 7842 8180 #endif 7843 8181 #ifdef _SC_NPROCESSORS_ONLN 7844 {"SC_NPROCESSORS_ONLN", 8182 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, 7845 8183 #endif 7846 8184 #ifdef _SC_NPROC_CONF 7847 {"SC_NPROC_CONF", 8185 {"SC_NPROC_CONF", _SC_NPROC_CONF}, 7848 8186 #endif 7849 8187 #ifdef _SC_NPROC_ONLN 7850 {"SC_NPROC_ONLN", 8188 {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, 7851 8189 #endif 7852 8190 #ifdef _SC_NZERO 7853 {"SC_NZERO", 8191 {"SC_NZERO", _SC_NZERO}, 7854 8192 #endif 7855 8193 #ifdef _SC_OPEN_MAX 7856 {"SC_OPEN_MAX", 8194 {"SC_OPEN_MAX", _SC_OPEN_MAX}, 7857 8195 #endif 7858 8196 #ifdef _SC_PAGESIZE 7859 {"SC_PAGESIZE", 8197 {"SC_PAGESIZE", _SC_PAGESIZE}, 7860 8198 #endif 7861 8199 #ifdef _SC_PAGE_SIZE 7862 {"SC_PAGE_SIZE", 8200 {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, 7863 8201 #endif 7864 8202 #ifdef _SC_PASS_MAX 7865 {"SC_PASS_MAX", 8203 {"SC_PASS_MAX", _SC_PASS_MAX}, 7866 8204 #endif 7867 8205 #ifdef _SC_PHYS_PAGES 7868 {"SC_PHYS_PAGES", 8206 {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, 7869 8207 #endif 7870 8208 #ifdef _SC_PII 7871 {"SC_PII", 8209 {"SC_PII", _SC_PII}, 7872 8210 #endif 7873 8211 #ifdef _SC_PII_INTERNET 7874 {"SC_PII_INTERNET", 8212 {"SC_PII_INTERNET", _SC_PII_INTERNET}, 7875 8213 #endif 7876 8214 #ifdef _SC_PII_INTERNET_DGRAM 7877 {"SC_PII_INTERNET_DGRAM", 8215 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, 7878 8216 #endif 7879 8217 #ifdef _SC_PII_INTERNET_STREAM 7880 {"SC_PII_INTERNET_STREAM", 8218 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, 7881 8219 #endif 7882 8220 #ifdef _SC_PII_OSI 7883 {"SC_PII_OSI", 8221 {"SC_PII_OSI", _SC_PII_OSI}, 7884 8222 #endif 7885 8223 #ifdef _SC_PII_OSI_CLTS 7886 {"SC_PII_OSI_CLTS", 8224 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, 7887 8225 #endif 7888 8226 #ifdef _SC_PII_OSI_COTS 7889 {"SC_PII_OSI_COTS", 8227 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, 7890 8228 #endif 7891 8229 #ifdef _SC_PII_OSI_M 7892 {"SC_PII_OSI_M", 8230 {"SC_PII_OSI_M", _SC_PII_OSI_M}, 7893 8231 #endif 7894 8232 #ifdef _SC_PII_SOCKET 7895 {"SC_PII_SOCKET", 8233 {"SC_PII_SOCKET", _SC_PII_SOCKET}, 7896 8234 #endif 7897 8235 #ifdef _SC_PII_XTI 7898 {"SC_PII_XTI", 8236 {"SC_PII_XTI", _SC_PII_XTI}, 7899 8237 #endif 7900 8238 #ifdef _SC_POLL 7901 {"SC_POLL", 8239 {"SC_POLL", _SC_POLL}, 7902 8240 #endif 7903 8241 #ifdef _SC_PRIORITIZED_IO 7904 {"SC_PRIORITIZED_IO", 8242 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, 7905 8243 #endif 7906 8244 #ifdef _SC_PRIORITY_SCHEDULING 7907 {"SC_PRIORITY_SCHEDULING", 8245 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, 7908 8246 #endif 7909 8247 #ifdef _SC_REALTIME_SIGNALS 7910 {"SC_REALTIME_SIGNALS", 8248 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, 7911 8249 #endif 7912 8250 #ifdef _SC_RE_DUP_MAX 7913 {"SC_RE_DUP_MAX", 8251 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, 7914 8252 #endif 7915 8253 #ifdef _SC_RTSIG_MAX 7916 {"SC_RTSIG_MAX", 8254 {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, 7917 8255 #endif 7918 8256 #ifdef _SC_SAVED_IDS 7919 {"SC_SAVED_IDS", 8257 {"SC_SAVED_IDS", _SC_SAVED_IDS}, 7920 8258 #endif 7921 8259 #ifdef _SC_SCHAR_MAX 7922 {"SC_SCHAR_MAX", 8260 {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, 7923 8261 #endif 7924 8262 #ifdef _SC_SCHAR_MIN 7925 {"SC_SCHAR_MIN", 8263 {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, 7926 8264 #endif 7927 8265 #ifdef _SC_SELECT 7928 {"SC_SELECT", 8266 {"SC_SELECT", _SC_SELECT}, 7929 8267 #endif 7930 8268 #ifdef _SC_SEMAPHORES 7931 {"SC_SEMAPHORES", 8269 {"SC_SEMAPHORES", _SC_SEMAPHORES}, 7932 8270 #endif 7933 8271 #ifdef _SC_SEM_NSEMS_MAX 7934 {"SC_SEM_NSEMS_MAX", 8272 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, 7935 8273 #endif 7936 8274 #ifdef _SC_SEM_VALUE_MAX 7937 {"SC_SEM_VALUE_MAX", 8275 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, 7938 8276 #endif 7939 8277 #ifdef _SC_SHARED_MEMORY_OBJECTS 7940 {"SC_SHARED_MEMORY_OBJECTS", 8278 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, 7941 8279 #endif 7942 8280 #ifdef _SC_SHRT_MAX 7943 {"SC_SHRT_MAX", 8281 {"SC_SHRT_MAX", _SC_SHRT_MAX}, 7944 8282 #endif 7945 8283 #ifdef _SC_SHRT_MIN 7946 {"SC_SHRT_MIN", 8284 {"SC_SHRT_MIN", _SC_SHRT_MIN}, 7947 8285 #endif 7948 8286 #ifdef _SC_SIGQUEUE_MAX 7949 {"SC_SIGQUEUE_MAX", 8287 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, 7950 8288 #endif 7951 8289 #ifdef _SC_SIGRT_MAX 7952 {"SC_SIGRT_MAX", 8290 {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, 7953 8291 #endif 7954 8292 #ifdef _SC_SIGRT_MIN 7955 {"SC_SIGRT_MIN", 8293 {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, 7956 8294 #endif 7957 8295 #ifdef _SC_SOFTPOWER 7958 {"SC_SOFTPOWER", 8296 {"SC_SOFTPOWER", _SC_SOFTPOWER}, 7959 8297 #endif 7960 8298 #ifdef _SC_SPLIT_CACHE 7961 {"SC_SPLIT_CACHE", 8299 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, 7962 8300 #endif 7963 8301 #ifdef _SC_SSIZE_MAX 7964 {"SC_SSIZE_MAX", 8302 {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, 7965 8303 #endif 7966 8304 #ifdef _SC_STACK_PROT 7967 {"SC_STACK_PROT", 8305 {"SC_STACK_PROT", _SC_STACK_PROT}, 7968 8306 #endif 7969 8307 #ifdef _SC_STREAM_MAX 7970 {"SC_STREAM_MAX", 8308 {"SC_STREAM_MAX", _SC_STREAM_MAX}, 7971 8309 #endif 7972 8310 #ifdef _SC_SYNCHRONIZED_IO 7973 {"SC_SYNCHRONIZED_IO", 8311 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, 7974 8312 #endif 7975 8313 #ifdef _SC_THREADS 7976 {"SC_THREADS", 8314 {"SC_THREADS", _SC_THREADS}, 7977 8315 #endif 7978 8316 #ifdef _SC_THREAD_ATTR_STACKADDR 7979 {"SC_THREAD_ATTR_STACKADDR", 8317 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, 7980 8318 #endif 7981 8319 #ifdef _SC_THREAD_ATTR_STACKSIZE 7982 {"SC_THREAD_ATTR_STACKSIZE", 8320 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, 7983 8321 #endif 7984 8322 #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS 7985 {"SC_THREAD_DESTRUCTOR_ITERATIONS", 8323 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, 7986 8324 #endif 7987 8325 #ifdef _SC_THREAD_KEYS_MAX 7988 {"SC_THREAD_KEYS_MAX", 8326 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, 7989 8327 #endif 7990 8328 #ifdef _SC_THREAD_PRIORITY_SCHEDULING 7991 {"SC_THREAD_PRIORITY_SCHEDULING", 8329 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, 7992 8330 #endif 7993 8331 #ifdef _SC_THREAD_PRIO_INHERIT 7994 {"SC_THREAD_PRIO_INHERIT", 8332 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, 7995 8333 #endif 7996 8334 #ifdef _SC_THREAD_PRIO_PROTECT 7997 {"SC_THREAD_PRIO_PROTECT", 8335 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, 7998 8336 #endif 7999 8337 #ifdef _SC_THREAD_PROCESS_SHARED 8000 {"SC_THREAD_PROCESS_SHARED", 8338 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, 8001 8339 #endif 8002 8340 #ifdef _SC_THREAD_SAFE_FUNCTIONS 8003 {"SC_THREAD_SAFE_FUNCTIONS", 8341 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, 8004 8342 #endif 8005 8343 #ifdef _SC_THREAD_STACK_MIN 8006 {"SC_THREAD_STACK_MIN", 8344 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, 8007 8345 #endif 8008 8346 #ifdef _SC_THREAD_THREADS_MAX 8009 {"SC_THREAD_THREADS_MAX", 8347 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, 8010 8348 #endif 8011 8349 #ifdef _SC_TIMERS 8012 {"SC_TIMERS", 8350 {"SC_TIMERS", _SC_TIMERS}, 8013 8351 #endif 8014 8352 #ifdef _SC_TIMER_MAX 8015 {"SC_TIMER_MAX", 8353 {"SC_TIMER_MAX", _SC_TIMER_MAX}, 8016 8354 #endif 8017 8355 #ifdef _SC_TTY_NAME_MAX 8018 {"SC_TTY_NAME_MAX", 8356 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, 8019 8357 #endif 8020 8358 #ifdef _SC_TZNAME_MAX 8021 {"SC_TZNAME_MAX", 8359 {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, 8022 8360 #endif 8023 8361 #ifdef _SC_T_IOV_MAX 8024 {"SC_T_IOV_MAX", 8362 {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, 8025 8363 #endif 8026 8364 #ifdef _SC_UCHAR_MAX 8027 {"SC_UCHAR_MAX", 8365 {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, 8028 8366 #endif 8029 8367 #ifdef _SC_UINT_MAX 8030 {"SC_UINT_MAX", 8368 {"SC_UINT_MAX", _SC_UINT_MAX}, 8031 8369 #endif 8032 8370 #ifdef _SC_UIO_MAXIOV 8033 {"SC_UIO_MAXIOV", 8371 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, 8034 8372 #endif 8035 8373 #ifdef _SC_ULONG_MAX 8036 {"SC_ULONG_MAX", 8374 {"SC_ULONG_MAX", _SC_ULONG_MAX}, 8037 8375 #endif 8038 8376 #ifdef _SC_USHRT_MAX 8039 {"SC_USHRT_MAX", 8377 {"SC_USHRT_MAX", _SC_USHRT_MAX}, 8040 8378 #endif 8041 8379 #ifdef _SC_VERSION 8042 {"SC_VERSION", 8380 {"SC_VERSION", _SC_VERSION}, 8043 8381 #endif 8044 8382 #ifdef _SC_WORD_BIT 8045 {"SC_WORD_BIT", 8383 {"SC_WORD_BIT", _SC_WORD_BIT}, 8046 8384 #endif 8047 8385 #ifdef _SC_XBS5_ILP32_OFF32 8048 {"SC_XBS5_ILP32_OFF32", 8386 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, 8049 8387 #endif 8050 8388 #ifdef _SC_XBS5_ILP32_OFFBIG 8051 {"SC_XBS5_ILP32_OFFBIG", 8389 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, 8052 8390 #endif 8053 8391 #ifdef _SC_XBS5_LP64_OFF64 8054 {"SC_XBS5_LP64_OFF64", 8392 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, 8055 8393 #endif 8056 8394 #ifdef _SC_XBS5_LPBIG_OFFBIG 8057 {"SC_XBS5_LPBIG_OFFBIG", 8395 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, 8058 8396 #endif 8059 8397 #ifdef _SC_XOPEN_CRYPT 8060 {"SC_XOPEN_CRYPT", 8398 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, 8061 8399 #endif 8062 8400 #ifdef _SC_XOPEN_ENH_I18N 8063 {"SC_XOPEN_ENH_I18N", 8401 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, 8064 8402 #endif 8065 8403 #ifdef _SC_XOPEN_LEGACY 8066 {"SC_XOPEN_LEGACY", 8404 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, 8067 8405 #endif 8068 8406 #ifdef _SC_XOPEN_REALTIME 8069 {"SC_XOPEN_REALTIME", 8407 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, 8070 8408 #endif 8071 8409 #ifdef _SC_XOPEN_REALTIME_THREADS 8072 {"SC_XOPEN_REALTIME_THREADS", 8410 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, 8073 8411 #endif 8074 8412 #ifdef _SC_XOPEN_SHM 8075 {"SC_XOPEN_SHM", 8413 {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, 8076 8414 #endif 8077 8415 #ifdef _SC_XOPEN_UNIX 8078 {"SC_XOPEN_UNIX", 8416 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, 8079 8417 #endif 8080 8418 #ifdef _SC_XOPEN_VERSION 8081 {"SC_XOPEN_VERSION", 8419 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, 8082 8420 #endif 8083 8421 #ifdef _SC_XOPEN_XCU_VERSION 8084 {"SC_XOPEN_XCU_VERSION", 8422 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, 8085 8423 #endif 8086 8424 #ifdef _SC_XOPEN_XPG2 8087 {"SC_XOPEN_XPG2", 8425 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, 8088 8426 #endif 8089 8427 #ifdef _SC_XOPEN_XPG3 8090 {"SC_XOPEN_XPG3", 8428 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, 8091 8429 #endif 8092 8430 #ifdef _SC_XOPEN_XPG4 8093 {"SC_XOPEN_XPG4", 8431 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, 8094 8432 #endif 8095 8433 }; … … 8142 8480 { 8143 8481 const struct constdef *c1 = 8144 8482 (const struct constdef *) v1; 8145 8483 const struct constdef *c2 = 8146 8484 (const struct constdef *) v2; 8147 8485 8148 8486 return strcmp(c1->name, c2->name); … … 8151 8489 static int 8152 8490 setup_confname_table(struct constdef *table, size_t tablesize, 8153 8491 char *tablename, PyObject *module) 8154 8492 { 8155 8493 PyObject *d = NULL; … … 8159 8497 d = PyDict_New(); 8160 8498 if (d == NULL) 8161 8499 return -1; 8162 8500 8163 8501 for (i=0; i < tablesize; ++i) { 8164 8165 8166 8167 8168 8169 8170 8502 PyObject *o = PyInt_FromLong(table[i].value); 8503 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { 8504 Py_XDECREF(o); 8505 Py_DECREF(d); 8506 return -1; 8507 } 8508 Py_DECREF(o); 8171 8509 } 8172 8510 return PyModule_AddObject(module, tablename, d); … … 8239 8577 win32_startfile(PyObject *self, PyObject *args) 8240 8578 { 8241 char *filepath; 8242 char *operation = NULL; 8243 HINSTANCE rc; 8244 #ifdef Py_WIN_WIDE_FILENAMES 8245 if (unicode_file_names()) { 8246 PyObject *unipath, *woperation = NULL; 8247 if (!PyArg_ParseTuple(args, "U|s:startfile", 8248 &unipath, &operation)) { 8249 PyErr_Clear(); 8250 goto normal; 8251 } 8252 8253 8254 if (operation) { 8255 woperation = PyUnicode_DecodeASCII(operation, 8256 strlen(operation), NULL); 8257 if (!woperation) { 8258 PyErr_Clear(); 8259 operation = NULL; 8260 goto normal; 8261 } 8262 } 8263 8264 Py_BEGIN_ALLOW_THREADS 8265 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0, 8266 PyUnicode_AS_UNICODE(unipath), 8267 NULL, NULL, SW_SHOWNORMAL); 8268 Py_END_ALLOW_THREADS 8269 8270 Py_XDECREF(woperation); 8271 if (rc <= (HINSTANCE)32) { 8272 PyObject *errval = win32_error_unicode("startfile", 8273 PyUnicode_AS_UNICODE(unipath)); 8274 return errval; 8275 } 8276 Py_INCREF(Py_None); 8277 return Py_None; 8278 } 8279 #endif 8579 char *filepath; 8580 char *operation = NULL; 8581 HINSTANCE rc; 8582 8583 PyObject *unipath, *woperation = NULL; 8584 if (!PyArg_ParseTuple(args, "U|s:startfile", 8585 &unipath, &operation)) { 8586 PyErr_Clear(); 8587 goto normal; 8588 } 8589 8590 if (operation) { 8591 woperation = PyUnicode_DecodeASCII(operation, 8592 strlen(operation), NULL); 8593 if (!woperation) { 8594 PyErr_Clear(); 8595 operation = NULL; 8596 goto normal; 8597 } 8598 } 8599 8600 Py_BEGIN_ALLOW_THREADS 8601 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0, 8602 PyUnicode_AS_UNICODE(unipath), 8603 NULL, NULL, SW_SHOWNORMAL); 8604 Py_END_ALLOW_THREADS 8605 8606 Py_XDECREF(woperation); 8607 if (rc <= (HINSTANCE)32) { 8608 PyObject *errval = win32_error_unicode("startfile", 8609 PyUnicode_AS_UNICODE(unipath)); 8610 return errval; 8611 } 8612 Py_INCREF(Py_None); 8613 return Py_None; 8280 8614 8281 8615 normal: 8282 if (!PyArg_ParseTuple(args, "et|s:startfile", 8283 Py_FileSystemDefaultEncoding, &filepath, 8284 8285 8286 8287 rc = ShellExecute((HWND)0, operation, filepath, 8288 8289 8290 8291 8292 8293 8294 8295 8296 8297 8298 } 8299 #endif 8616 if (!PyArg_ParseTuple(args, "et|s:startfile", 8617 Py_FileSystemDefaultEncoding, &filepath, 8618 &operation)) 8619 return NULL; 8620 Py_BEGIN_ALLOW_THREADS 8621 rc = ShellExecute((HWND)0, operation, filepath, 8622 NULL, NULL, SW_SHOWNORMAL); 8623 Py_END_ALLOW_THREADS 8624 if (rc <= (HINSTANCE)32) { 8625 PyObject *errval = win32_error("startfile", filepath); 8626 PyMem_Free(filepath); 8627 return errval; 8628 } 8629 PyMem_Free(filepath); 8630 Py_INCREF(Py_None); 8631 return Py_None; 8632 } 8633 #endif /* MS_WINDOWS */ 8300 8634 8301 8635 #ifdef HAVE_GETLOADAVG … … 8318 8652 #endif 8319 8653 8320 #ifdef MS_WINDOWS 8321 8322 PyDoc_STRVAR(win32_urandom__doc__, 8654 PyDoc_STRVAR(posix_urandom__doc__, 8323 8655 "urandom(n) -> str\n\n\ 8324 Return a string of n random bytes suitable for cryptographic use."); 8325 8326 typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\ 8327 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\ 8328 DWORD dwFlags ); 8329 typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\ 8330 BYTE *pbBuffer ); 8331 8332 static CRYPTGENRANDOM pCryptGenRandom = NULL; 8333 /* This handle is never explicitly released. Instead, the operating 8334 system will release it when the process terminates. */ 8335 static HCRYPTPROV hCryptProv = 0; 8656 Return n random bytes suitable for cryptographic use."); 8657 8658 static PyObject * 8659 posix_urandom(PyObject *self, PyObject *args) 8660 { 8661 Py_ssize_t size; 8662 PyObject *result; 8663 int ret; 8664 8665 /* Read arguments */ 8666 if (!PyArg_ParseTuple(args, "n:urandom", &size)) 8667 return NULL; 8668 if (size < 0) 8669 return PyErr_Format(PyExc_ValueError, 8670 "negative argument not allowed"); 8671 result = PyBytes_FromStringAndSize(NULL, size); 8672 if (result == NULL) 8673 return NULL; 8674 8675 ret = _PyOS_URandom(PyBytes_AS_STRING(result), 8676 PyBytes_GET_SIZE(result)); 8677 if (ret == -1) { 8678 Py_DECREF(result); 8679 return NULL; 8680 } 8681 return result; 8682 } 8683 8684 #ifdef HAVE_SETRESUID 8685 PyDoc_STRVAR(posix_setresuid__doc__, 8686 "setresuid(ruid, euid, suid)\n\n\ 8687 Set the current process's real, effective, and saved user ids."); 8336 8688 8337 8689 static PyObject* 8338 win32_urandom(PyObject *self, PyObject *args) 8339 { 8340 int howMany; 8341 PyObject* result; 8342 8343 /* Read arguments */ 8344 if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) 8345 return NULL; 8346 if (howMany < 0) 8347 return PyErr_Format(PyExc_ValueError, 8348 "negative argument not allowed"); 8349 8350 if (hCryptProv == 0) { 8351 HINSTANCE hAdvAPI32 = NULL; 8352 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL; 8353 8354 /* Obtain handle to the DLL containing CryptoAPI 8355 This should not fail */ 8356 hAdvAPI32 = GetModuleHandle("advapi32.dll"); 8357 if(hAdvAPI32 == NULL) 8358 return win32_error("GetModuleHandle", NULL); 8359 8360 /* Obtain pointers to the CryptoAPI functions 8361 This will fail on some early versions of Win95 */ 8362 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress( 8363 hAdvAPI32, 8364 "CryptAcquireContextA"); 8365 if (pCryptAcquireContext == NULL) 8366 return PyErr_Format(PyExc_NotImplementedError, 8367 "CryptAcquireContextA not found"); 8368 8369 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress( 8370 hAdvAPI32, "CryptGenRandom"); 8371 if (pCryptGenRandom == NULL) 8372 return PyErr_Format(PyExc_NotImplementedError, 8373 "CryptGenRandom not found"); 8374 8375 /* Acquire context */ 8376 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL, 8377 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) 8378 return win32_error("CryptAcquireContext", NULL); 8379 } 8380 8381 /* Allocate bytes */ 8382 result = PyString_FromStringAndSize(NULL, howMany); 8383 if (result != NULL) { 8384 /* Get random data */ 8385 memset(PyString_AS_STRING(result), 0, howMany); /* zero seed */ 8386 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*) 8387 PyString_AS_STRING(result))) { 8388 Py_DECREF(result); 8389 return win32_error("CryptGenRandom", NULL); 8390 } 8391 } 8392 return result; 8393 } 8394 #endif 8395 8396 #ifdef __VMS 8397 /* Use openssl random routine */ 8398 #include <openssl/rand.h> 8399 PyDoc_STRVAR(vms_urandom__doc__, 8400 "urandom(n) -> str\n\n\ 8401 Return a string of n random bytes suitable for cryptographic use."); 8690 posix_setresuid (PyObject *self, PyObject *args) 8691 { 8692 uid_t ruid, euid, suid; 8693 if (!PyArg_ParseTuple(args, "O&O&O&:setresuid", 8694 _Py_Uid_Converter, &ruid, 8695 _Py_Uid_Converter, &euid, 8696 _Py_Uid_Converter, &suid)) 8697 return NULL; 8698 if (setresuid(ruid, euid, suid) < 0) 8699 return posix_error(); 8700 Py_RETURN_NONE; 8701 } 8702 #endif 8703 8704 #ifdef HAVE_SETRESGID 8705 PyDoc_STRVAR(posix_setresgid__doc__, 8706 "setresgid(rgid, egid, sgid)\n\n\ 8707 Set the current process's real, effective, and saved group ids."); 8402 8708 8403 8709 static PyObject* 8404 vms_urandom(PyObject *self, PyObject *args) 8405 { 8406 int howMany; 8407 PyObject* result; 8408 8409 /* Read arguments */ 8410 if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) 8411 return NULL; 8412 if (howMany < 0) 8413 return PyErr_Format(PyExc_ValueError, 8414 "negative argument not allowed"); 8415 8416 /* Allocate bytes */ 8417 result = PyString_FromStringAndSize(NULL, howMany); 8418 if (result != NULL) { 8419 /* Get random data */ 8420 if (RAND_pseudo_bytes((unsigned char*) 8421 PyString_AS_STRING(result), 8422 howMany) < 0) { 8423 Py_DECREF(result); 8424 return PyErr_Format(PyExc_ValueError, 8425 "RAND_pseudo_bytes"); 8426 } 8427 } 8428 return result; 8710 posix_setresgid (PyObject *self, PyObject *args) 8711 { 8712 gid_t rgid, egid, sgid; 8713 if (!PyArg_ParseTuple(args, "O&O&O&:setresgid", 8714 _Py_Gid_Converter, &rgid, 8715 _Py_Gid_Converter, &egid, 8716 _Py_Gid_Converter, &sgid)) 8717 return NULL; 8718 if (setresgid(rgid, egid, sgid) < 0) 8719 return posix_error(); 8720 Py_RETURN_NONE; 8721 } 8722 #endif 8723 8724 #ifdef HAVE_GETRESUID 8725 PyDoc_STRVAR(posix_getresuid__doc__, 8726 "getresuid() -> (ruid, euid, suid)\n\n\ 8727 Get tuple of the current process's real, effective, and saved user ids."); 8728 8729 static PyObject* 8730 posix_getresuid (PyObject *self, PyObject *noargs) 8731 { 8732 uid_t ruid, euid, suid; 8733 if (getresuid(&ruid, &euid, &suid) < 0) 8734 return posix_error(); 8735 return Py_BuildValue("(NNN)", _PyInt_FromUid(ruid), 8736 _PyInt_FromUid(euid), 8737 _PyInt_FromUid(suid)); 8738 } 8739 #endif 8740 8741 #ifdef HAVE_GETRESGID 8742 PyDoc_STRVAR(posix_getresgid__doc__, 8743 "getresgid() -> (rgid, egid, sgid)\n\n\ 8744 Get tuple of the current process's real, effective, and saved group ids."); 8745 8746 static PyObject* 8747 posix_getresgid (PyObject *self, PyObject *noargs) 8748 { 8749 uid_t rgid, egid, sgid; 8750 if (getresgid(&rgid, &egid, &sgid) < 0) 8751 return posix_error(); 8752 return Py_BuildValue("(NNN)", _PyInt_FromGid(rgid), 8753 _PyInt_FromGid(egid), 8754 _PyInt_FromGid(sgid)); 8429 8755 } 8430 8756 #endif 8431 8757 8432 8758 static PyMethodDef posix_methods[] = { 8433 {"access",posix_access, METH_VARARGS, posix_access__doc__},8759 {"access", posix_access, METH_VARARGS, posix_access__doc__}, 8434 8760 #ifdef HAVE_TTYNAME 8435 {"ttyname",posix_ttyname, METH_VARARGS, posix_ttyname__doc__},8436 #endif 8437 {"chdir",posix_chdir, METH_VARARGS, posix_chdir__doc__},8761 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__}, 8762 #endif 8763 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__}, 8438 8764 #ifdef HAVE_CHFLAGS 8439 {"chflags",posix_chflags, METH_VARARGS, posix_chflags__doc__},8765 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__}, 8440 8766 #endif /* HAVE_CHFLAGS */ 8441 {"chmod",posix_chmod, METH_VARARGS, posix_chmod__doc__},8767 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__}, 8442 8768 #ifdef HAVE_FCHMOD 8443 {"fchmod",posix_fchmod, METH_VARARGS, posix_fchmod__doc__},8769 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__}, 8444 8770 #endif /* HAVE_FCHMOD */ 8445 8771 #ifdef HAVE_CHOWN 8446 {"chown",posix_chown, METH_VARARGS, posix_chown__doc__},8772 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__}, 8447 8773 #endif /* HAVE_CHOWN */ 8448 8774 #ifdef HAVE_LCHMOD 8449 {"lchmod",posix_lchmod, METH_VARARGS, posix_lchmod__doc__},8775 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__}, 8450 8776 #endif /* HAVE_LCHMOD */ 8451 8777 #ifdef HAVE_FCHOWN 8452 {"fchown",posix_fchown, METH_VARARGS, posix_fchown__doc__},8778 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__}, 8453 8779 #endif /* HAVE_FCHOWN */ 8454 8780 #ifdef HAVE_LCHFLAGS 8455 {"lchflags",posix_lchflags, METH_VARARGS, posix_lchflags__doc__},8781 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__}, 8456 8782 #endif /* HAVE_LCHFLAGS */ 8457 8783 #ifdef HAVE_LCHOWN 8458 {"lchown",posix_lchown, METH_VARARGS, posix_lchown__doc__},8784 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__}, 8459 8785 #endif /* HAVE_LCHOWN */ 8460 8786 #ifdef HAVE_CHROOT 8461 {"chroot",posix_chroot, METH_VARARGS, posix_chroot__doc__},8787 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__}, 8462 8788 #endif 8463 8789 #ifdef HAVE_CTERMID 8464 {"ctermid",posix_ctermid, METH_NOARGS, posix_ctermid__doc__},8790 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__}, 8465 8791 #endif 8466 8792 #ifdef HAVE_GETCWD 8467 {"getcwd",posix_getcwd, METH_NOARGS, posix_getcwd__doc__},8793 {"getcwd", posix_getcwd, METH_NOARGS, posix_getcwd__doc__}, 8468 8794 #ifdef Py_USING_UNICODE 8469 {"getcwdu",posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__},8795 {"getcwdu", posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__}, 8470 8796 #endif 8471 8797 #endif 8472 8798 #ifdef HAVE_LINK 8473 {"link",posix_link, METH_VARARGS, posix_link__doc__},8799 {"link", posix_link, METH_VARARGS, posix_link__doc__}, 8474 8800 #endif /* HAVE_LINK */ 8475 {"listdir",posix_listdir, METH_VARARGS, posix_listdir__doc__},8476 {"lstat",posix_lstat, METH_VARARGS, posix_lstat__doc__},8477 {"mkdir",posix_mkdir, METH_VARARGS, posix_mkdir__doc__},8801 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__}, 8802 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__}, 8803 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__}, 8478 8804 #ifdef HAVE_NICE 8479 {"nice",posix_nice, METH_VARARGS, posix_nice__doc__},8805 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, 8480 8806 #endif /* HAVE_NICE */ 8481 8807 #ifdef HAVE_READLINK 8482 {"readlink",posix_readlink, METH_VARARGS, posix_readlink__doc__},8808 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__}, 8483 8809 #endif /* HAVE_READLINK */ 8484 {"rename",posix_rename, METH_VARARGS, posix_rename__doc__},8485 {"rmdir",posix_rmdir, METH_VARARGS, posix_rmdir__doc__},8486 {"stat",posix_stat, METH_VARARGS, posix_stat__doc__},8487 8810 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__}, 8811 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__}, 8812 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__}, 8813 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__}, 8488 8814 #ifdef HAVE_SYMLINK 8489 {"symlink",posix_symlink, METH_VARARGS, posix_symlink__doc__},8815 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__}, 8490 8816 #endif /* HAVE_SYMLINK */ 8491 8817 #ifdef HAVE_SYSTEM 8492 {"system",posix_system, METH_VARARGS, posix_system__doc__},8493 #endif 8494 {"umask",posix_umask, METH_VARARGS, posix_umask__doc__},8818 {"system", posix_system, METH_VARARGS, posix_system__doc__}, 8819 #endif 8820 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__}, 8495 8821 #ifdef HAVE_UNAME 8496 {"uname",posix_uname, METH_NOARGS, posix_uname__doc__},8822 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__}, 8497 8823 #endif /* HAVE_UNAME */ 8498 {"unlink",posix_unlink, METH_VARARGS, posix_unlink__doc__},8499 {"remove",posix_unlink, METH_VARARGS, posix_remove__doc__},8500 {"utime",posix_utime, METH_VARARGS, posix_utime__doc__},8824 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__}, 8825 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__}, 8826 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__}, 8501 8827 #ifdef HAVE_TIMES 8502 {"times",posix_times, METH_NOARGS, posix_times__doc__},8828 {"times", posix_times, METH_NOARGS, posix_times__doc__}, 8503 8829 #endif /* HAVE_TIMES */ 8504 {"_exit",posix__exit, METH_VARARGS, posix__exit__doc__},8830 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__}, 8505 8831 #ifdef HAVE_EXECV 8506 {"execv",posix_execv, METH_VARARGS, posix_execv__doc__},8507 {"execve",posix_execve, METH_VARARGS, posix_execve__doc__},8832 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__}, 8833 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__}, 8508 8834 #endif /* HAVE_EXECV */ 8509 8835 #ifdef HAVE_SPAWNV 8510 {"spawnv",posix_spawnv, METH_VARARGS, posix_spawnv__doc__},8511 {"spawnve",posix_spawnve, METH_VARARGS, posix_spawnve__doc__},8836 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__}, 8837 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__}, 8512 8838 #if defined(PYOS_OS2) 8513 {"spawnvp",posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},8514 {"spawnvpe",posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},8839 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__}, 8840 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__}, 8515 8841 #endif /* PYOS_OS2 */ 8516 8842 #endif /* HAVE_SPAWNV */ 8517 8843 #ifdef HAVE_FORK1 8518 8844 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__}, 8519 8845 #endif /* HAVE_FORK1 */ 8520 8846 #ifdef HAVE_FORK 8521 {"fork",posix_fork, METH_NOARGS, posix_fork__doc__},8847 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__}, 8522 8848 #endif /* HAVE_FORK */ 8523 8849 #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) 8524 {"openpty",posix_openpty, METH_NOARGS, posix_openpty__doc__},8850 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__}, 8525 8851 #endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */ 8526 8852 #ifdef HAVE_FORKPTY 8527 {"forkpty",posix_forkpty, METH_NOARGS, posix_forkpty__doc__},8853 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__}, 8528 8854 #endif /* HAVE_FORKPTY */ 8529 8855 #ifdef HAVE_GETEGID 8530 {"getegid",posix_getegid, METH_NOARGS, posix_getegid__doc__},8856 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__}, 8531 8857 #endif /* HAVE_GETEGID */ 8532 8858 #ifdef HAVE_GETEUID 8533 {"geteuid",posix_geteuid, METH_NOARGS, posix_geteuid__doc__},8859 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__}, 8534 8860 #endif /* HAVE_GETEUID */ 8535 8861 #ifdef HAVE_GETGID 8536 {"getgid",posix_getgid, METH_NOARGS, posix_getgid__doc__},8862 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__}, 8537 8863 #endif /* HAVE_GETGID */ 8538 8864 #ifdef HAVE_GETGROUPS 8539 {"getgroups",posix_getgroups, METH_NOARGS, posix_getgroups__doc__},8540 #endif 8541 {"getpid",posix_getpid, METH_NOARGS, posix_getpid__doc__},8865 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__}, 8866 #endif 8867 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__}, 8542 8868 #ifdef HAVE_GETPGRP 8543 {"getpgrp",posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},8869 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__}, 8544 8870 #endif /* HAVE_GETPGRP */ 8545 8871 #ifdef HAVE_GETPPID 8546 {"getppid",posix_getppid, METH_NOARGS, posix_getppid__doc__},8872 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__}, 8547 8873 #endif /* HAVE_GETPPID */ 8548 8874 #ifdef HAVE_GETUID 8549 {"getuid",posix_getuid, METH_NOARGS, posix_getuid__doc__},8875 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__}, 8550 8876 #endif /* HAVE_GETUID */ 8551 8877 #ifdef HAVE_GETLOGIN 8552 {"getlogin",posix_getlogin, METH_NOARGS, posix_getlogin__doc__},8878 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__}, 8553 8879 #endif 8554 8880 #ifdef HAVE_KILL 8555 {"kill",posix_kill, METH_VARARGS, posix_kill__doc__},8881 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__}, 8556 8882 #endif /* HAVE_KILL */ 8557 8883 #ifdef HAVE_KILLPG 8558 {"killpg",posix_killpg, METH_VARARGS, posix_killpg__doc__},8884 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__}, 8559 8885 #endif /* HAVE_KILLPG */ 8560 8886 #ifdef HAVE_PLOCK 8561 {"plock",posix_plock, METH_VARARGS, posix_plock__doc__},8887 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__}, 8562 8888 #endif /* HAVE_PLOCK */ 8563 8889 #ifdef HAVE_POPEN 8564 {"popen",posix_popen, METH_VARARGS, posix_popen__doc__},8890 {"popen", posix_popen, METH_VARARGS, posix_popen__doc__}, 8565 8891 #ifdef MS_WINDOWS 8566 {"popen2", win32_popen2, METH_VARARGS}, 8567 {"popen3", win32_popen3, METH_VARARGS}, 8568 {"popen4", win32_popen4, METH_VARARGS}, 8569 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, 8892 {"popen2", win32_popen2, METH_VARARGS}, 8893 {"popen3", win32_popen3, METH_VARARGS}, 8894 {"popen4", win32_popen4, METH_VARARGS}, 8895 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, 8896 {"kill", win32_kill, METH_VARARGS, win32_kill__doc__}, 8570 8897 #else 8571 8898 #if defined(PYOS_OS2) && defined(PYCC_GCC) 8572 {"popen2",os2emx_popen2, METH_VARARGS},8573 {"popen3",os2emx_popen3, METH_VARARGS},8574 {"popen4",os2emx_popen4, METH_VARARGS},8899 {"popen2", os2emx_popen2, METH_VARARGS}, 8900 {"popen3", os2emx_popen3, METH_VARARGS}, 8901 {"popen4", os2emx_popen4, METH_VARARGS}, 8575 8902 #endif 8576 8903 #endif 8577 8904 #endif /* HAVE_POPEN */ 8578 8905 #ifdef HAVE_SETUID 8579 {"setuid",posix_setuid, METH_VARARGS, posix_setuid__doc__},8906 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__}, 8580 8907 #endif /* HAVE_SETUID */ 8581 8908 #ifdef HAVE_SETEUID 8582 {"seteuid",posix_seteuid, METH_VARARGS, posix_seteuid__doc__},8909 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__}, 8583 8910 #endif /* HAVE_SETEUID */ 8584 8911 #ifdef HAVE_SETEGID 8585 {"setegid",posix_setegid, METH_VARARGS, posix_setegid__doc__},8912 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__}, 8586 8913 #endif /* HAVE_SETEGID */ 8587 8914 #ifdef HAVE_SETREUID 8588 {"setreuid",posix_setreuid, METH_VARARGS, posix_setreuid__doc__},8915 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__}, 8589 8916 #endif /* HAVE_SETREUID */ 8590 8917 #ifdef HAVE_SETREGID 8591 {"setregid", posix_setregid,METH_VARARGS, posix_setregid__doc__},8918 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__}, 8592 8919 #endif /* HAVE_SETREGID */ 8593 8920 #ifdef HAVE_SETGID 8594 {"setgid",posix_setgid, METH_VARARGS, posix_setgid__doc__},8921 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__}, 8595 8922 #endif /* HAVE_SETGID */ 8596 8923 #ifdef HAVE_SETGROUPS 8597 {"setgroups",posix_setgroups, METH_O, posix_setgroups__doc__},8924 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__}, 8598 8925 #endif /* HAVE_SETGROUPS */ 8926 #ifdef HAVE_INITGROUPS 8927 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__}, 8928 #endif /* HAVE_INITGROUPS */ 8599 8929 #ifdef HAVE_GETPGID 8600 {"getpgid",posix_getpgid, METH_VARARGS, posix_getpgid__doc__},8930 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__}, 8601 8931 #endif /* HAVE_GETPGID */ 8602 8932 #ifdef HAVE_SETPGRP 8603 {"setpgrp",posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},8933 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__}, 8604 8934 #endif /* HAVE_SETPGRP */ 8605 8935 #ifdef HAVE_WAIT 8606 {"wait",posix_wait, METH_NOARGS, posix_wait__doc__},8936 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__}, 8607 8937 #endif /* HAVE_WAIT */ 8608 8938 #ifdef HAVE_WAIT3 8609 {"wait3",posix_wait3, METH_VARARGS, posix_wait3__doc__},8939 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__}, 8610 8940 #endif /* HAVE_WAIT3 */ 8611 8941 #ifdef HAVE_WAIT4 8612 {"wait4",posix_wait4, METH_VARARGS, posix_wait4__doc__},8942 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__}, 8613 8943 #endif /* HAVE_WAIT4 */ 8614 8944 #if defined(HAVE_WAITPID) || defined(HAVE_CWAIT) 8615 {"waitpid",posix_waitpid, METH_VARARGS, posix_waitpid__doc__},8945 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__}, 8616 8946 #endif /* HAVE_WAITPID */ 8617 8947 #ifdef HAVE_GETSID 8618 {"getsid",posix_getsid, METH_VARARGS, posix_getsid__doc__},8948 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__}, 8619 8949 #endif /* HAVE_GETSID */ 8620 8950 #ifdef HAVE_SETSID 8621 {"setsid",posix_setsid, METH_NOARGS, posix_setsid__doc__},8951 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__}, 8622 8952 #endif /* HAVE_SETSID */ 8623 8953 #ifdef HAVE_SETPGID 8624 {"setpgid",posix_setpgid, METH_VARARGS, posix_setpgid__doc__},8954 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__}, 8625 8955 #endif /* HAVE_SETPGID */ 8626 8956 #ifdef HAVE_TCGETPGRP 8627 {"tcgetpgrp",posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},8957 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__}, 8628 8958 #endif /* HAVE_TCGETPGRP */ 8629 8959 #ifdef HAVE_TCSETPGRP 8630 {"tcsetpgrp",posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},8960 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__}, 8631 8961 #endif /* HAVE_TCSETPGRP */ 8632 {"open",posix_open, METH_VARARGS, posix_open__doc__},8633 {"close",posix_close, METH_VARARGS, posix_close__doc__},8634 {"closerange",posix_closerange, METH_VARARGS, posix_closerange__doc__},8635 {"dup",posix_dup, METH_VARARGS, posix_dup__doc__},8636 {"dup2",posix_dup2, METH_VARARGS, posix_dup2__doc__},8637 {"lseek",posix_lseek, METH_VARARGS, posix_lseek__doc__},8638 {"read",posix_read, METH_VARARGS, posix_read__doc__},8639 {"write",posix_write, METH_VARARGS, posix_write__doc__},8640 {"fstat",posix_fstat, METH_VARARGS, posix_fstat__doc__},8641 {"fdopen",posix_fdopen, METH_VARARGS, posix_fdopen__doc__},8642 {"isatty",posix_isatty, METH_VARARGS, posix_isatty__doc__},8962 {"open", posix_open, METH_VARARGS, posix_open__doc__}, 8963 {"close", posix_close, METH_VARARGS, posix_close__doc__}, 8964 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__}, 8965 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, 8966 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__}, 8967 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, 8968 {"read", posix_read, METH_VARARGS, posix_read__doc__}, 8969 {"write", posix_write, METH_VARARGS, posix_write__doc__}, 8970 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__}, 8971 {"fdopen", posix_fdopen, METH_VARARGS, posix_fdopen__doc__}, 8972 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__}, 8643 8973 #ifdef HAVE_PIPE 8644 {"pipe",posix_pipe, METH_NOARGS, posix_pipe__doc__},8974 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__}, 8645 8975 #endif 8646 8976 #ifdef HAVE_MKFIFO 8647 {"mkfifo",posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},8977 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__}, 8648 8978 #endif 8649 8979 #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) 8650 {"mknod",posix_mknod, METH_VARARGS, posix_mknod__doc__},8980 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__}, 8651 8981 #endif 8652 8982 #ifdef HAVE_DEVICE_MACROS 8653 {"major",posix_major, METH_VARARGS, posix_major__doc__},8654 {"minor",posix_minor, METH_VARARGS, posix_minor__doc__},8655 {"makedev",posix_makedev, METH_VARARGS, posix_makedev__doc__},8983 {"major", posix_major, METH_VARARGS, posix_major__doc__}, 8984 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__}, 8985 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__}, 8656 8986 #endif 8657 8987 #ifdef HAVE_FTRUNCATE 8658 {"ftruncate",posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},8988 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__}, 8659 8989 #endif 8660 8990 #ifdef HAVE_PUTENV 8661 {"putenv",posix_putenv, METH_VARARGS, posix_putenv__doc__},8991 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__}, 8662 8992 #endif 8663 8993 #ifdef HAVE_UNSETENV 8664 {"unsetenv",posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},8665 #endif 8666 {"strerror",posix_strerror, METH_VARARGS, posix_strerror__doc__},8994 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__}, 8995 #endif 8996 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__}, 8667 8997 #ifdef HAVE_FCHDIR 8668 {"fchdir",posix_fchdir, METH_O, posix_fchdir__doc__},8998 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__}, 8669 8999 #endif 8670 9000 #ifdef HAVE_FSYNC 8671 9001 {"fsync", posix_fsync, METH_O, posix_fsync__doc__}, 8672 9002 #endif 8673 9003 #ifdef HAVE_FDATASYNC 8674 9004 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__}, 8675 9005 #endif 8676 9006 #ifdef HAVE_SYS_WAIT_H 8677 9007 #ifdef WCOREDUMP 8678 {"WCOREDUMP",posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},9008 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__}, 8679 9009 #endif /* WCOREDUMP */ 8680 9010 #ifdef WIFCONTINUED 8681 9011 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__}, 8682 9012 #endif /* WIFCONTINUED */ 8683 9013 #ifdef WIFSTOPPED 8684 {"WIFSTOPPED",posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},9014 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__}, 8685 9015 #endif /* WIFSTOPPED */ 8686 9016 #ifdef WIFSIGNALED 8687 {"WIFSIGNALED",posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},9017 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__}, 8688 9018 #endif /* WIFSIGNALED */ 8689 9019 #ifdef WIFEXITED 8690 {"WIFEXITED",posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},9020 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__}, 8691 9021 #endif /* WIFEXITED */ 8692 9022 #ifdef WEXITSTATUS 8693 {"WEXITSTATUS",posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},9023 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__}, 8694 9024 #endif /* WEXITSTATUS */ 8695 9025 #ifdef WTERMSIG 8696 {"WTERMSIG",posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},9026 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__}, 8697 9027 #endif /* WTERMSIG */ 8698 9028 #ifdef WSTOPSIG 8699 {"WSTOPSIG",posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},9029 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__}, 8700 9030 #endif /* WSTOPSIG */ 8701 9031 #endif /* HAVE_SYS_WAIT_H */ 8702 9032 #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) 8703 {"fstatvfs",posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},9033 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__}, 8704 9034 #endif 8705 9035 #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) 8706 {"statvfs",posix_statvfs, METH_VARARGS, posix_statvfs__doc__},9036 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__}, 8707 9037 #endif 8708 9038 #ifdef HAVE_TMPFILE 8709 {"tmpfile",posix_tmpfile, METH_NOARGS, posix_tmpfile__doc__},9039 {"tmpfile", posix_tmpfile, METH_NOARGS, posix_tmpfile__doc__}, 8710 9040 #endif 8711 9041 #ifdef HAVE_TEMPNAM 8712 {"tempnam",posix_tempnam, METH_VARARGS, posix_tempnam__doc__},9042 {"tempnam", posix_tempnam, METH_VARARGS, posix_tempnam__doc__}, 8713 9043 #endif 8714 9044 #ifdef HAVE_TMPNAM 8715 {"tmpnam",posix_tmpnam, METH_NOARGS, posix_tmpnam__doc__},9045 {"tmpnam", posix_tmpnam, METH_NOARGS, posix_tmpnam__doc__}, 8716 9046 #endif 8717 9047 #ifdef HAVE_CONFSTR 8718 {"confstr",posix_confstr, METH_VARARGS, posix_confstr__doc__},9048 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__}, 8719 9049 #endif 8720 9050 #ifdef HAVE_SYSCONF 8721 {"sysconf",posix_sysconf, METH_VARARGS, posix_sysconf__doc__},9051 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__}, 8722 9052 #endif 8723 9053 #ifdef HAVE_FPATHCONF 8724 {"fpathconf",posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},9054 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__}, 8725 9055 #endif 8726 9056 #ifdef HAVE_PATHCONF 8727 {"pathconf",posix_pathconf, METH_VARARGS, posix_pathconf__doc__},8728 #endif 8729 {"abort",posix_abort, METH_NOARGS, posix_abort__doc__},9057 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__}, 9058 #endif 9059 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__}, 8730 9060 #ifdef MS_WINDOWS 8731 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, 9061 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, 9062 {"_isdir", posix__isdir, METH_VARARGS, posix__isdir__doc__}, 8732 9063 #endif 8733 9064 #ifdef HAVE_GETLOADAVG 8734 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__}, 8735 #endif 8736 #ifdef MS_WINDOWS 8737 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__}, 8738 #endif 8739 #ifdef __VMS 8740 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__}, 8741 #endif 8742 {NULL, NULL} /* Sentinel */ 9065 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__}, 9066 #endif 9067 #ifdef HAVE_SETRESUID 9068 {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__}, 9069 #endif 9070 #ifdef HAVE_SETRESGID 9071 {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__}, 9072 #endif 9073 #ifdef HAVE_GETRESUID 9074 {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__}, 9075 #endif 9076 #ifdef HAVE_GETRESGID 9077 {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__}, 9078 #endif 9079 {"urandom", posix_urandom, METH_VARARGS, posix_urandom__doc__}, 9080 {NULL, NULL} /* Sentinel */ 8743 9081 }; 8744 9082 … … 8747 9085 ins(PyObject *module, char *symbol, long value) 8748 9086 { 8749 9087 return PyModule_AddIntConstant(module, symbol, value); 8750 9088 } 8751 9089 … … 8785 9123 default: 8786 9124 PyOS_snprintf(tmp, sizeof(tmp), 8787 9125 "%d-%d", values[QSV_VERSION_MAJOR], 8788 9126 values[QSV_VERSION_MINOR]); 8789 9127 ver = &tmp[0]; … … 8807 9145 { 8808 9146 #ifdef F_OK 8809 9147 if (ins(d, "F_OK", (long)F_OK)) return -1; 8810 9148 #endif 8811 9149 #ifdef R_OK 8812 9150 if (ins(d, "R_OK", (long)R_OK)) return -1; 8813 9151 #endif 8814 9152 #ifdef W_OK 8815 9153 if (ins(d, "W_OK", (long)W_OK)) return -1; 8816 9154 #endif 8817 9155 #ifdef X_OK 8818 9156 if (ins(d, "X_OK", (long)X_OK)) return -1; 8819 9157 #endif 8820 9158 #ifdef NGROUPS_MAX 8821 9159 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1; 8822 9160 #endif 8823 9161 #ifdef TMP_MAX 8824 9162 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1; 8825 9163 #endif 8826 9164 #ifdef WCONTINUED 8827 9165 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1; 8828 9166 #endif 8829 9167 #ifdef WNOHANG 8830 9168 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1; 8831 9169 #endif 8832 9170 #ifdef WUNTRACED 8833 9171 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1; 8834 9172 #endif 8835 9173 #ifdef O_RDONLY 8836 9174 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1; 8837 9175 #endif 8838 9176 #ifdef O_WRONLY 8839 9177 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1; 8840 9178 #endif 8841 9179 #ifdef O_RDWR 8842 9180 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1; 8843 9181 #endif 8844 9182 #ifdef O_NDELAY 8845 9183 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1; 8846 9184 #endif 8847 9185 #ifdef O_NONBLOCK 8848 9186 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1; 8849 9187 #endif 8850 9188 #ifdef O_APPEND 8851 9189 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1; 8852 9190 #endif 8853 9191 #ifdef O_DSYNC 8854 9192 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1; 8855 9193 #endif 8856 9194 #ifdef O_RSYNC 8857 9195 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1; 8858 9196 #endif 8859 9197 #ifdef O_SYNC 8860 9198 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1; 8861 9199 #endif 8862 9200 #ifdef O_NOCTTY 8863 9201 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1; 8864 9202 #endif 8865 9203 #ifdef O_CREAT 8866 9204 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1; 8867 9205 #endif 8868 9206 #ifdef O_EXCL 8869 9207 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1; 8870 9208 #endif 8871 9209 #ifdef O_TRUNC 8872 9210 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1; 8873 9211 #endif 8874 9212 #ifdef O_BINARY 8875 9213 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1; 8876 9214 #endif 8877 9215 #ifdef O_TEXT 8878 9216 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1; 8879 9217 #endif 8880 9218 #ifdef O_LARGEFILE 8881 9219 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1; 8882 9220 #endif 8883 9221 #ifdef O_SHLOCK 8884 9222 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1; 8885 9223 #endif 8886 9224 #ifdef O_EXLOCK 8887 9225 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1; 8888 9226 #endif 8889 9227 8890 9228 /* MS Windows */ 8891 9229 #ifdef O_NOINHERIT 8892 8893 9230 /* Don't inherit in child processes. */ 9231 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1; 8894 9232 #endif 8895 9233 #ifdef _O_SHORT_LIVED 8896 8897 8898 9234 /* Optimize for short life (keep in memory). */ 9235 /* MS forgot to define this one with a non-underscore form too. */ 9236 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1; 8899 9237 #endif 8900 9238 #ifdef O_TEMPORARY 8901 8902 9239 /* Automatically delete when last handle is closed. */ 9240 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1; 8903 9241 #endif 8904 9242 #ifdef O_RANDOM 8905 8906 9243 /* Optimize for random access. */ 9244 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1; 8907 9245 #endif 8908 9246 #ifdef O_SEQUENTIAL 8909 8910 9247 /* Optimize for sequential access. */ 9248 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1; 8911 9249 #endif 8912 9250 8913 9251 /* GNU extensions. */ 8914 9252 #ifdef O_ASYNC 8915 /* Send a SIGIO signal whenever input or output8916 8917 9253 /* Send a SIGIO signal whenever input or output 9254 becomes available on file descriptor */ 9255 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1; 8918 9256 #endif 8919 9257 #ifdef O_DIRECT 8920 8921 9258 /* Direct disk access. */ 9259 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1; 8922 9260 #endif 8923 9261 #ifdef O_DIRECTORY 8924 /* Must be a directory.*/8925 9262 /* Must be a directory. */ 9263 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1; 8926 9264 #endif 8927 9265 #ifdef O_NOFOLLOW 8928 /* Do not follow links.*/8929 9266 /* Do not follow links. */ 9267 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1; 8930 9268 #endif 8931 9269 #ifdef O_NOATIME 8932 8933 8934 #endif 8935 8936 9270 /* Do not update the access time. */ 9271 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1; 9272 #endif 9273 9274 /* These come from sysexits.h */ 8937 9275 #ifdef EX_OK 8938 9276 if (ins(d, "EX_OK", (long)EX_OK)) return -1; 8939 9277 #endif /* EX_OK */ 8940 9278 #ifdef EX_USAGE 8941 9279 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1; 8942 9280 #endif /* EX_USAGE */ 8943 9281 #ifdef EX_DATAERR 8944 9282 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1; 8945 9283 #endif /* EX_DATAERR */ 8946 9284 #ifdef EX_NOINPUT 8947 9285 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1; 8948 9286 #endif /* EX_NOINPUT */ 8949 9287 #ifdef EX_NOUSER 8950 9288 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1; 8951 9289 #endif /* EX_NOUSER */ 8952 9290 #ifdef EX_NOHOST 8953 9291 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1; 8954 9292 #endif /* EX_NOHOST */ 8955 9293 #ifdef EX_UNAVAILABLE 8956 9294 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1; 8957 9295 #endif /* EX_UNAVAILABLE */ 8958 9296 #ifdef EX_SOFTWARE 8959 9297 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1; 8960 9298 #endif /* EX_SOFTWARE */ 8961 9299 #ifdef EX_OSERR 8962 9300 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1; 8963 9301 #endif /* EX_OSERR */ 8964 9302 #ifdef EX_OSFILE 8965 9303 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1; 8966 9304 #endif /* EX_OSFILE */ 8967 9305 #ifdef EX_CANTCREAT 8968 9306 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1; 8969 9307 #endif /* EX_CANTCREAT */ 8970 9308 #ifdef EX_IOERR 8971 9309 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1; 8972 9310 #endif /* EX_IOERR */ 8973 9311 #ifdef EX_TEMPFAIL 8974 9312 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1; 8975 9313 #endif /* EX_TEMPFAIL */ 8976 9314 #ifdef EX_PROTOCOL 8977 9315 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1; 8978 9316 #endif /* EX_PROTOCOL */ 8979 9317 #ifdef EX_NOPERM 8980 9318 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1; 8981 9319 #endif /* EX_NOPERM */ 8982 9320 #ifdef EX_CONFIG 8983 9321 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1; 8984 9322 #endif /* EX_CONFIG */ 8985 9323 #ifdef EX_NOTFOUND 8986 9324 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1; 8987 9325 #endif /* EX_NOTFOUND */ 8988 9326 8989 9327 #ifdef HAVE_SPAWNV 8990 9328 #if defined(PYOS_OS2) && defined(PYCC_GCC) 8991 8992 8993 8994 8995 8996 8997 8998 8999 9000 9001 9002 9003 9004 9005 9006 9007 9008 9009 9010 9329 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1; 9330 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1; 9331 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1; 9332 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1; 9333 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1; 9334 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1; 9335 if (ins(d, "P_PM", (long)P_PM)) return -1; 9336 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1; 9337 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1; 9338 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1; 9339 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1; 9340 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1; 9341 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1; 9342 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1; 9343 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1; 9344 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1; 9345 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1; 9346 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1; 9347 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1; 9348 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1; 9011 9349 #else 9012 9013 9014 9015 9016 9350 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1; 9351 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1; 9352 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1; 9353 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1; 9354 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1; 9017 9355 #endif 9018 9356 #endif 9019 9357 9020 9358 #if defined(PYOS_OS2) 9021 9022 #endif 9023 9359 if (insertvalues(d)) return -1; 9360 #endif 9361 return 0; 9024 9362 } 9025 9363 … … 9041 9379 INITFUNC(void) 9042 9380 { 9043 9044 9045 9046 9047 9048 9049 9050 9051 9052 9053 9054 9055 9056 9057 9058 9059 9060 9061 9062 9063 9064 9065 9381 PyObject *m, *v; 9382 9383 m = Py_InitModule3(MODNAME, 9384 posix_methods, 9385 posix__doc__); 9386 if (m == NULL) 9387 return; 9388 9389 /* Initialize environ dictionary */ 9390 v = convertenviron(); 9391 Py_XINCREF(v); 9392 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) 9393 return; 9394 Py_DECREF(v); 9395 9396 if (all_ins(m)) 9397 return; 9398 9399 if (setup_confname_tables(m)) 9400 return; 9401 9402 Py_INCREF(PyExc_OSError); 9403 PyModule_AddObject(m, "error", PyExc_OSError); 9066 9404 9067 9405 #ifdef HAVE_PUTENV 9068 9069 9070 #endif 9071 9072 9073 9074 9075 9076 9077 9078 9079 9080 9081 9082 9406 if (posix_putenv_garbage == NULL) 9407 posix_putenv_garbage = PyDict_New(); 9408 #endif 9409 9410 if (!initialized) { 9411 stat_result_desc.name = MODNAME ".stat_result"; 9412 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; 9413 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; 9414 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; 9415 PyStructSequence_InitType(&StatResultType, &stat_result_desc); 9416 structseq_new = StatResultType.tp_new; 9417 StatResultType.tp_new = statresult_new; 9418 9419 statvfs_result_desc.name = MODNAME ".statvfs_result"; 9420 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc); 9083 9421 #ifdef NEED_TICKS_PER_SECOND 9084 9422 # if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK) 9085 9423 ticks_per_second = sysconf(_SC_CLK_TCK); 9086 9424 # elif defined(HZ) 9087 9425 ticks_per_second = HZ; 9088 9426 # else 9089 9427 ticks_per_second = 60; /* magic fallback value; may be bogus */ 9090 9428 # endif 9091 9429 #endif 9092 9093 9094 9095 9096 9097 9098 9430 } 9431 Py_INCREF((PyObject*) &StatResultType); 9432 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); 9433 Py_INCREF((PyObject*) &StatVFSResultType); 9434 PyModule_AddObject(m, "statvfs_result", 9435 (PyObject*) &StatVFSResultType); 9436 initialized = 1; 9099 9437 9100 9438 #ifdef __APPLE__ 9101 9102 9103 9104 9105 * currently active platform. 9106 9107 9108 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on 9109 9110 9439 /* 9440 * Step 2 of weak-linking support on Mac OS X. 9441 * 9442 * The code below removes functions that are not available on the 9443 * currently active platform. 9444 * 9445 * This block allow one to use a python binary that was build on 9446 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on 9447 * OSX 10.4. 9448 */ 9111 9449 #ifdef HAVE_FSTATVFS 9112 9113 9114 9115 9116 9450 if (fstatvfs == NULL) { 9451 if (PyObject_DelAttrString(m, "fstatvfs") == -1) { 9452 return; 9453 } 9454 } 9117 9455 #endif /* HAVE_FSTATVFS */ 9118 9456 9119 9457 #ifdef HAVE_STATVFS 9120 9121 9122 9123 9124 9458 if (statvfs == NULL) { 9459 if (PyObject_DelAttrString(m, "statvfs") == -1) { 9460 return; 9461 } 9462 } 9125 9463 #endif /* HAVE_STATVFS */ 9126 9464 9127 9465 # ifdef HAVE_LCHOWN 9128 9129 9130 9131 9132 9466 if (lchown == NULL) { 9467 if (PyObject_DelAttrString(m, "lchown") == -1) { 9468 return; 9469 } 9470 } 9133 9471 #endif /* HAVE_LCHOWN */ 9134 9472
Note:
See TracChangeset
for help on using the changeset viewer.