Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(10)

Delta Between Two Patch Sets: Modules/posixmodule.c

Issue 26826: Expose new copy_file_range() syscal in os module and use it to improve shutils.copy()
Left Patch Set: Created 3 years, 10 months ago
Right Patch Set: Created 3 years, 7 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/test/test_os.py ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 1
2 /* POSIX module implementation */ 2 /* POSIX module implementation */
3 3
4 /* This file is also used for Windows NT/MS-Win. In that case the 4 /* This file is also used for Windows NT/MS-Win. In that case the
5 module actually calls itself 'nt', not 'posix', and a few 5 module actually calls itself 'nt', not 'posix', and a few
6 functions are either unimplemented or implemented differently. The source 6 functions are either unimplemented or implemented differently. The source
7 assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent 7 assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent
8 of the compiler used. Different compilers define their own feature 8 of the compiler used. Different compilers define their own feature
9 test macro, e.g. '_MSC_VER'. */ 9 test macro, e.g. '_MSC_VER'. */
10 10
(...skipping 13 matching lines...) Expand all
24 24
25 #define PY_SSIZE_T_CLEAN 25 #define PY_SSIZE_T_CLEAN
26 26
27 #include "Python.h" 27 #include "Python.h"
28 #include "structmember.h" 28 #include "structmember.h"
29 #ifndef MS_WINDOWS 29 #ifndef MS_WINDOWS
30 #include "posixmodule.h" 30 #include "posixmodule.h"
31 #else 31 #else
32 #include "winreparse.h" 32 #include "winreparse.h"
33 #endif 33 #endif
34
35 /* On android API level 21, 'AT_EACCESS' is not declared although
36 * HAVE_FACCESSAT is defined. */
37 #ifdef __ANDROID__
38 #undef HAVE_FACCESSAT
39 #endif
40
41 #include <stdio.h> /* needed for ctermid() */
34 42
35 #ifdef __cplusplus 43 #ifdef __cplusplus
36 extern "C" { 44 extern "C" {
37 #endif 45 #endif
38 46
39 PyDoc_STRVAR(posix__doc__, 47 PyDoc_STRVAR(posix__doc__,
40 "This module provides access to operating system functionality that is\n\ 48 "This module provides access to operating system functionality that is\n\
41 standardized by the C Standard and the POSIX standard (a thinly\n\ 49 standardized by the C Standard and the POSIX standard (a thinly\n\
42 disguised Unix interface). Refer to the library manual and\n\ 50 disguised Unix interface). Refer to the library manual and\n\
43 corresponding Unix manual entries for more information on calls."); 51 corresponding Unix manual entries for more information on calls.");
(...skipping 758 matching lines...) Expand 10 before | Expand all | Expand 10 after
802 * path_cleanup(&path); 810 * path_cleanup(&path);
803 * 811 *
804 * (Note that if PyArg_Parse fails you don't need to call 812 * (Note that if PyArg_Parse fails you don't need to call
805 * path_cleanup(). However it is safe to do so.) 813 * path_cleanup(). However it is safe to do so.)
806 */ 814 */
807 typedef struct { 815 typedef struct {
808 const char *function_name; 816 const char *function_name;
809 const char *argument_name; 817 const char *argument_name;
810 int nullable; 818 int nullable;
811 int allow_fd; 819 int allow_fd;
812 wchar_t *wide; 820 const wchar_t *wide;
813 char *narrow; 821 const char *narrow;
814 int fd; 822 int fd;
815 Py_ssize_t length; 823 Py_ssize_t length;
816 PyObject *object; 824 PyObject *object;
817 PyObject *cleanup; 825 PyObject *cleanup;
818 } path_t; 826 } path_t;
819 827
820 #define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \ 828 #define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \
821 {function_name, argument_name, nullable, allow_fd, NULL, NULL, -1, 0, NULL, NULL} 829 {function_name, argument_name, nullable, allow_fd, NULL, NULL, -1, 0, NULL, NULL}
822 830
823 static void 831 static void
824 path_cleanup(path_t *path) { 832 path_cleanup(path_t *path) {
825 if (path->cleanup) { 833 if (path->cleanup) {
826 Py_CLEAR(path->cleanup); 834 Py_CLEAR(path->cleanup);
827 } 835 }
828 } 836 }
829 837
830 static int 838 static int
831 path_converter(PyObject *o, void *p) 839 path_converter(PyObject *o, void *p)
832 { 840 {
833 path_t *path = (path_t *)p; 841 path_t *path = (path_t *)p;
834 PyObject *bytes; 842 PyObject *bytes;
835 Py_ssize_t length; 843 Py_ssize_t length;
836 char *narrow; 844 const char *narrow;
837 845
838 #define FORMAT_EXCEPTION(exc, fmt) \ 846 #define FORMAT_EXCEPTION(exc, fmt) \
839 PyErr_Format(exc, "%s%s" fmt, \ 847 PyErr_Format(exc, "%s%s" fmt, \
840 path->function_name ? path->function_name : "", \ 848 path->function_name ? path->function_name : "", \
841 path->function_name ? ": " : "", \ 849 path->function_name ? ": " : "", \
842 path->argument_name ? path->argument_name : "path") 850 path->argument_name ? path->argument_name : "path")
843 851
844 /* Py_CLEANUP_SUPPORTED support */ 852 /* Py_CLEANUP_SUPPORTED support */
845 if (o == NULL) { 853 if (o == NULL) {
846 path_cleanup(path); 854 path_cleanup(path);
847 return 1; 855 return 1;
848 } 856 }
849 857
850 /* ensure it's always safe to call path_cleanup() */ 858 /* ensure it's always safe to call path_cleanup() */
851 path->cleanup = NULL; 859 path->cleanup = NULL;
852 860
853 if ((o == Py_None) && path->nullable) { 861 if ((o == Py_None) && path->nullable) {
854 path->wide = NULL; 862 path->wide = NULL;
855 path->narrow = NULL; 863 path->narrow = NULL;
856 path->length = 0; 864 path->length = 0;
857 path->object = o; 865 path->object = o;
858 path->fd = -1; 866 path->fd = -1;
859 return 1; 867 return 1;
860 } 868 }
861 869
862 if (PyUnicode_Check(o)) { 870 if (PyUnicode_Check(o)) {
863 #ifdef MS_WINDOWS 871 #ifdef MS_WINDOWS
864 wchar_t *wide; 872 const wchar_t *wide;
865 873
866 wide = PyUnicode_AsUnicodeAndSize(o, &length); 874 wide = PyUnicode_AsUnicodeAndSize(o, &length);
867 if (!wide) { 875 if (!wide) {
868 return 0; 876 return 0;
869 } 877 }
870 if (length > 32767) { 878 if (length > 32767) {
871 FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows"); 879 FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows");
872 return 0; 880 return 0;
873 } 881 }
874 if (wcslen(wide) != length) { 882 if (wcslen(wide) != length) {
(...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after
1156 #ifdef MS_WINDOWS 1164 #ifdef MS_WINDOWS
1157 /* _wenviron must be initialized in this way if the program is started 1165 /* _wenviron must be initialized in this way if the program is started
1158 through main() instead of wmain(). */ 1166 through main() instead of wmain(). */
1159 _wgetenv(L""); 1167 _wgetenv(L"");
1160 if (_wenviron == NULL) 1168 if (_wenviron == NULL)
1161 return d; 1169 return d;
1162 /* This part ignores errors */ 1170 /* This part ignores errors */
1163 for (e = _wenviron; *e != NULL; e++) { 1171 for (e = _wenviron; *e != NULL; e++) {
1164 PyObject *k; 1172 PyObject *k;
1165 PyObject *v; 1173 PyObject *v;
1166 wchar_t *p = wcschr(*e, L'='); 1174 const wchar_t *p = wcschr(*e, L'=');
1167 if (p == NULL) 1175 if (p == NULL)
1168 continue; 1176 continue;
1169 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e)); 1177 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
1170 if (k == NULL) { 1178 if (k == NULL) {
1171 PyErr_Clear(); 1179 PyErr_Clear();
1172 continue; 1180 continue;
1173 } 1181 }
1174 v = PyUnicode_FromWideChar(p+1, wcslen(p+1)); 1182 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
1175 if (v == NULL) { 1183 if (v == NULL) {
1176 PyErr_Clear(); 1184 PyErr_Clear();
1177 Py_DECREF(k); 1185 Py_DECREF(k);
1178 continue; 1186 continue;
1179 } 1187 }
1180 if (PyDict_GetItem(d, k) == NULL) { 1188 if (PyDict_GetItem(d, k) == NULL) {
1181 if (PyDict_SetItem(d, k, v) != 0) 1189 if (PyDict_SetItem(d, k, v) != 0)
1182 PyErr_Clear(); 1190 PyErr_Clear();
1183 } 1191 }
1184 Py_DECREF(k); 1192 Py_DECREF(k);
1185 Py_DECREF(v); 1193 Py_DECREF(v);
1186 } 1194 }
1187 #else 1195 #else
1188 if (environ == NULL) 1196 if (environ == NULL)
1189 return d; 1197 return d;
1190 /* This part ignores errors */ 1198 /* This part ignores errors */
1191 for (e = environ; *e != NULL; e++) { 1199 for (e = environ; *e != NULL; e++) {
1192 PyObject *k; 1200 PyObject *k;
1193 PyObject *v; 1201 PyObject *v;
1194 char *p = strchr(*e, '='); 1202 const char *p = strchr(*e, '=');
1195 if (p == NULL) 1203 if (p == NULL)
1196 continue; 1204 continue;
1197 k = PyBytes_FromStringAndSize(*e, (int)(p-*e)); 1205 k = PyBytes_FromStringAndSize(*e, (int)(p-*e));
1198 if (k == NULL) { 1206 if (k == NULL) {
1199 PyErr_Clear(); 1207 PyErr_Clear();
1200 continue; 1208 continue;
1201 } 1209 }
1202 v = PyBytes_FromStringAndSize(p+1, strlen(p+1)); 1210 v = PyBytes_FromStringAndSize(p+1, strlen(p+1));
1203 if (v == NULL) { 1211 if (v == NULL) {
1204 PyErr_Clear(); 1212 PyErr_Clear();
(...skipping 2270 matching lines...) Expand 10 before | Expand all | Expand 10 after
3475 HANDLE hFindFile = INVALID_HANDLE_VALUE; 3483 HANDLE hFindFile = INVALID_HANDLE_VALUE;
3476 BOOL result; 3484 BOOL result;
3477 WIN32_FIND_DATA FileData; 3485 WIN32_FIND_DATA FileData;
3478 char namebuf[MAX_PATH+4]; /* Overallocate for "\*.*" */ 3486 char namebuf[MAX_PATH+4]; /* Overallocate for "\*.*" */
3479 /* only claim to have space for MAX_PATH */ 3487 /* only claim to have space for MAX_PATH */
3480 Py_ssize_t len = Py_ARRAY_LENGTH(namebuf)-4; 3488 Py_ssize_t len = Py_ARRAY_LENGTH(namebuf)-4;
3481 wchar_t *wnamebuf = NULL; 3489 wchar_t *wnamebuf = NULL;
3482 3490
3483 if (!path->narrow) { 3491 if (!path->narrow) {
3484 WIN32_FIND_DATAW wFileData; 3492 WIN32_FIND_DATAW wFileData;
3485 wchar_t *po_wchars; 3493 const wchar_t *po_wchars;
3486 3494
3487 if (!path->wide) { /* Default arg: "." */ 3495 if (!path->wide) { /* Default arg: "." */
3488 po_wchars = L"."; 3496 po_wchars = L".";
3489 len = 1; 3497 len = 1;
3490 } else { 3498 } else {
3491 po_wchars = path->wide; 3499 po_wchars = path->wide;
3492 len = wcslen(path->wide); 3500 len = wcslen(path->wide);
3493 } 3501 }
3494 /* The +5 is so we can append "\\*.*\0" */ 3502 /* The +5 is so we can append "\\*.*\0" */
3495 wnamebuf = PyMem_New(wchar_t, len + 5); 3503 wnamebuf = PyMem_New(wchar_t, len + 5);
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
3641 3649
3642 return_str = 1; 3650 return_str = 1;
3643 3651
3644 Py_BEGIN_ALLOW_THREADS 3652 Py_BEGIN_ALLOW_THREADS
3645 dirp = fdopendir(fd); 3653 dirp = fdopendir(fd);
3646 Py_END_ALLOW_THREADS 3654 Py_END_ALLOW_THREADS
3647 } 3655 }
3648 else 3656 else
3649 #endif 3657 #endif
3650 { 3658 {
3651 char *name; 3659 const char *name;
3652 if (path->narrow) { 3660 if (path->narrow) {
3653 name = path->narrow; 3661 name = path->narrow;
3654 /* only return bytes if they specified a bytes object */ 3662 /* only return bytes if they specified a bytes object */
3655 return_str = !(PyBytes_Check(path->object)); 3663 return_str = !(PyBytes_Check(path->object));
3656 } 3664 }
3657 else { 3665 else {
3658 name = "."; 3666 name = ".";
3659 return_str = 1; 3667 return_str = 1;
3660 } 3668 }
3661 3669
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after
3823 3831
3824 static PyObject * 3832 static PyObject *
3825 os__getfinalpathname_impl(PyModuleDef *module, PyObject *path) 3833 os__getfinalpathname_impl(PyModuleDef *module, PyObject *path)
3826 /*[clinic end generated code: output=8be81a5f51a34bcf input=71d5e89334891bf4]*/ 3834 /*[clinic end generated code: output=8be81a5f51a34bcf input=71d5e89334891bf4]*/
3827 { 3835 {
3828 HANDLE hFile; 3836 HANDLE hFile;
3829 int buf_size; 3837 int buf_size;
3830 wchar_t *target_path; 3838 wchar_t *target_path;
3831 int result_length; 3839 int result_length;
3832 PyObject *result; 3840 PyObject *result;
3833 wchar_t *path_wchar; 3841 const wchar_t *path_wchar;
3834 3842
3835 path_wchar = PyUnicode_AsUnicode(path); 3843 path_wchar = PyUnicode_AsUnicode(path);
3836 if (path_wchar == NULL) 3844 if (path_wchar == NULL)
3837 return NULL; 3845 return NULL;
3838 3846
3839 hFile = CreateFileW( 3847 hFile = CreateFileW(
3840 path_wchar, 3848 path_wchar,
3841 0, /* desired access */ 3849 0, /* desired access */
3842 0, /* share mode */ 3850 0, /* share mode */
3843 NULL, /* security attributes */ 3851 NULL, /* security attributes */
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
3912 path: unicode 3920 path: unicode
3913 3921
3914 A helper function for ismount on Win32. 3922 A helper function for ismount on Win32.
3915 [clinic start generated code]*/ 3923 [clinic start generated code]*/
3916 3924
3917 static PyObject * 3925 static PyObject *
3918 os__getvolumepathname_impl(PyModuleDef *module, PyObject *path) 3926 os__getvolumepathname_impl(PyModuleDef *module, PyObject *path)
3919 /*[clinic end generated code: output=79a0ba729f956dbe input=7eacadc40acbda6b]*/ 3927 /*[clinic end generated code: output=79a0ba729f956dbe input=7eacadc40acbda6b]*/
3920 { 3928 {
3921 PyObject *result; 3929 PyObject *result;
3922 wchar_t *path_wchar, *mountpath=NULL; 3930 const wchar_t *path_wchar;
3931 wchar_t *mountpath=NULL;
3923 size_t buflen; 3932 size_t buflen;
3924 BOOL ret; 3933 BOOL ret;
3925 3934
3926 path_wchar = PyUnicode_AsUnicodeAndSize(path, &buflen); 3935 path_wchar = PyUnicode_AsUnicodeAndSize(path, &buflen);
3927 if (path_wchar == NULL) 3936 if (path_wchar == NULL)
3928 return NULL; 3937 return NULL;
3929 buflen += 1; 3938 buflen += 1;
3930 3939
3931 /* Volume path should be shorter than entire path */ 3940 /* Volume path should be shorter than entire path */
3932 buflen = Py_MAX(buflen, MAX_PATH); 3941 buflen = Py_MAX(buflen, MAX_PATH);
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after
4110 if (retval == -1) 4119 if (retval == -1)
4111 return posix_error(); 4120 return posix_error();
4112 Py_RETURN_NONE; 4121 Py_RETURN_NONE;
4113 } 4122 }
4114 #endif /* HAVE_SETPRIORITY */ 4123 #endif /* HAVE_SETPRIORITY */
4115 4124
4116 4125
4117 static PyObject * 4126 static PyObject *
4118 internal_rename(path_t *src, path_t *dst, int src_dir_fd, int dst_dir_fd, int is _replace) 4127 internal_rename(path_t *src, path_t *dst, int src_dir_fd, int dst_dir_fd, int is _replace)
4119 { 4128 {
4120 char *function_name = is_replace ? "replace" : "rename"; 4129 const char *function_name = is_replace ? "replace" : "rename";
4121 int dir_fd_specified; 4130 int dir_fd_specified;
4122 4131
4123 #ifdef MS_WINDOWS 4132 #ifdef MS_WINDOWS
4124 BOOL result; 4133 BOOL result;
4125 int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0; 4134 int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0;
4126 #else 4135 #else
4127 int result; 4136 int result;
4128 #endif 4137 #endif
4129 4138
4130 dir_fd_specified = (src_dir_fd != DEFAULT_DIR_FD) || 4139 dir_fd_specified = (src_dir_fd != DEFAULT_DIR_FD) ||
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after
4290 command: FSConverter 4299 command: FSConverter
4291 4300
4292 Execute the command in a subshell. 4301 Execute the command in a subshell.
4293 [clinic start generated code]*/ 4302 [clinic start generated code]*/
4294 4303
4295 static long 4304 static long
4296 os_system_impl(PyModuleDef *module, PyObject *command) 4305 os_system_impl(PyModuleDef *module, PyObject *command)
4297 /*[clinic end generated code: output=800f775e10b7be55 input=86a58554ba6094af]*/ 4306 /*[clinic end generated code: output=800f775e10b7be55 input=86a58554ba6094af]*/
4298 { 4307 {
4299 long result; 4308 long result;
4300 char *bytes = PyBytes_AsString(command); 4309 const char *bytes = PyBytes_AsString(command);
4301 Py_BEGIN_ALLOW_THREADS 4310 Py_BEGIN_ALLOW_THREADS
4302 result = system(bytes); 4311 result = system(bytes);
4303 Py_END_ALLOW_THREADS 4312 Py_END_ALLOW_THREADS
4304 return result; 4313 return result;
4305 } 4314 }
4306 #endif 4315 #endif
4307 #endif /* HAVE_SYSTEM */ 4316 #endif /* HAVE_SYSTEM */
4308 4317
4309 4318
4310 /*[clinic input] 4319 /*[clinic input]
(...skipping 618 matching lines...) Expand 10 before | Expand all | Expand 10 after
4929 #endif 4938 #endif
4930 4939
4931 #if defined(HAVE_EXECV) || defined (HAVE_FEXECVE) 4940 #if defined(HAVE_EXECV) || defined (HAVE_FEXECVE)
4932 static char** 4941 static char**
4933 parse_envlist(PyObject* env, Py_ssize_t *envc_ptr) 4942 parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
4934 { 4943 {
4935 char **envlist; 4944 char **envlist;
4936 Py_ssize_t i, pos, envc; 4945 Py_ssize_t i, pos, envc;
4937 PyObject *keys=NULL, *vals=NULL; 4946 PyObject *keys=NULL, *vals=NULL;
4938 PyObject *key, *val, *key2, *val2; 4947 PyObject *key, *val, *key2, *val2;
4939 char *p, *k, *v; 4948 char *p;
4949 const char *k, *v;
4940 size_t len; 4950 size_t len;
4941 4951
4942 i = PyMapping_Size(env); 4952 i = PyMapping_Size(env);
4943 if (i < 0) 4953 if (i < 0)
4944 return NULL; 4954 return NULL;
4945 envlist = PyMem_NEW(char *, i + 1); 4955 envlist = PyMem_NEW(char *, i + 1);
4946 if (envlist == NULL) { 4956 if (envlist == NULL) {
4947 PyErr_NoMemory(); 4957 PyErr_NoMemory();
4948 return NULL; 4958 return NULL;
4949 } 4959 }
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
5044 Tuple or list of strings. 5054 Tuple or list of strings.
5045 / 5055 /
5046 5056
5047 Execute an executable path with arguments, replacing current process. 5057 Execute an executable path with arguments, replacing current process.
5048 [clinic start generated code]*/ 5058 [clinic start generated code]*/
5049 5059
5050 static PyObject * 5060 static PyObject *
5051 os_execv_impl(PyModuleDef *module, PyObject *path, PyObject *argv) 5061 os_execv_impl(PyModuleDef *module, PyObject *path, PyObject *argv)
5052 /*[clinic end generated code: output=9221f08143146fff input=96041559925e5229]*/ 5062 /*[clinic end generated code: output=9221f08143146fff input=96041559925e5229]*/
5053 { 5063 {
5054 char *path_char; 5064 const char *path_char;
5055 char **argvlist; 5065 char **argvlist;
5056 Py_ssize_t argc; 5066 Py_ssize_t argc;
5057 5067
5058 /* execv has two arguments: (path, argv), where 5068 /* execv has two arguments: (path, argv), where
5059 argv is a list or tuple of strings. */ 5069 argv is a list or tuple of strings. */
5060 5070
5061 path_char = PyBytes_AsString(path); 5071 path_char = PyBytes_AsString(path);
5062 if (!PyList_Check(argv) && !PyTuple_Check(argv)) { 5072 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
5063 PyErr_SetString(PyExc_TypeError, 5073 PyErr_SetString(PyExc_TypeError,
5064 "execv() arg 2 must be a tuple or list"); 5074 "execv() arg 2 must be a tuple or list");
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
5165 Tuple or list of strings. 5175 Tuple or list of strings.
5166 / 5176 /
5167 5177
5168 Execute the program specified by path in a new process. 5178 Execute the program specified by path in a new process.
5169 [clinic start generated code]*/ 5179 [clinic start generated code]*/
5170 5180
5171 static PyObject * 5181 static PyObject *
5172 os_spawnv_impl(PyModuleDef *module, int mode, PyObject *path, PyObject *argv) 5182 os_spawnv_impl(PyModuleDef *module, int mode, PyObject *path, PyObject *argv)
5173 /*[clinic end generated code: output=140a7945484c8cc5 input=042c91dfc1e6debc]*/ 5183 /*[clinic end generated code: output=140a7945484c8cc5 input=042c91dfc1e6debc]*/
5174 { 5184 {
5175 char *path_char; 5185 const char *path_char;
5176 char **argvlist; 5186 char **argvlist;
5177 int i; 5187 int i;
5178 Py_ssize_t argc; 5188 Py_ssize_t argc;
5179 Py_intptr_t spawnval; 5189 Py_intptr_t spawnval;
5180 PyObject *(*getitem)(PyObject *, Py_ssize_t); 5190 PyObject *(*getitem)(PyObject *, Py_ssize_t);
5181 5191
5182 /* spawnv has three arguments: (mode, path, argv), where 5192 /* spawnv has three arguments: (mode, path, argv), where
5183 argv is a list or tuple of strings. */ 5193 argv is a list or tuple of strings. */
5184 5194
5185 path_char = PyBytes_AsString(path); 5195 path_char = PyBytes_AsString(path);
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
5243 / 5253 /
5244 5254
5245 Execute the program specified by path in a new process. 5255 Execute the program specified by path in a new process.
5246 [clinic start generated code]*/ 5256 [clinic start generated code]*/
5247 5257
5248 static PyObject * 5258 static PyObject *
5249 os_spawnve_impl(PyModuleDef *module, int mode, PyObject *path, 5259 os_spawnve_impl(PyModuleDef *module, int mode, PyObject *path,
5250 PyObject *argv, PyObject *env) 5260 PyObject *argv, PyObject *env)
5251 /*[clinic end generated code: output=e7f5f0703610531f input=02362fd937963f8f]*/ 5261 /*[clinic end generated code: output=e7f5f0703610531f input=02362fd937963f8f]*/
5252 { 5262 {
5253 char *path_char; 5263 const char *path_char;
5254 char **argvlist; 5264 char **argvlist;
5255 char **envlist; 5265 char **envlist;
5256 PyObject *res = NULL; 5266 PyObject *res = NULL;
5257 Py_ssize_t argc, i, envc; 5267 Py_ssize_t argc, i, envc;
5258 Py_intptr_t spawnval; 5268 Py_intptr_t spawnval;
5259 PyObject *(*getitem)(PyObject *, Py_ssize_t); 5269 PyObject *(*getitem)(PyObject *, Py_ssize_t);
5260 Py_ssize_t lastarg = 0; 5270 Py_ssize_t lastarg = 0;
5261 5271
5262 /* spawnve has four arguments: (mode, path, argv, env), where 5272 /* spawnve has four arguments: (mode, path, argv, env), where
5263 argv is a list or tuple of strings and env is a dictionary 5273 argv is a list or tuple of strings and env is a dictionary
(...skipping 667 matching lines...) Expand 10 before | Expand all | Expand 10 after
5931 if (slave_name == NULL) 5941 if (slave_name == NULL)
5932 goto posix_error; 5942 goto posix_error;
5933 5943
5934 slave_fd = _Py_open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ 5944 slave_fd = _Py_open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
5935 if (slave_fd == -1) 5945 if (slave_fd == -1)
5936 goto error; 5946 goto error;
5937 5947
5938 if (_Py_set_inheritable(master_fd, 0, NULL) < 0) 5948 if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
5939 goto posix_error; 5949 goto posix_error;
5940 5950
5941 #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC) 5951 #if !defined(__CYGWIN__) && !defined(__ANDROID__) && !defined(HAVE_DEV_PTC)
5942 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ 5952 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
5943 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ 5953 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
5944 #ifndef __hpux 5954 #ifndef __hpux
5945 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ 5955 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
5946 #endif /* __hpux */ 5956 #endif /* __hpux */
5947 #endif /* HAVE_CYGWIN */ 5957 #endif /* HAVE_CYGWIN */
5948 #endif /* HAVE_OPENPTY */ 5958 #endif /* HAVE_OPENPTY */
5949 5959
5950 return Py_BuildValue("(ii)", master_fd, slave_fd); 5960 return Py_BuildValue("(ii)", master_fd, slave_fd);
5951 5961
(...skipping 304 matching lines...) Expand 10 before | Expand all | Expand 10 after
6256 "initgroups(username, gid) -> None\n\n\ 6266 "initgroups(username, gid) -> None\n\n\
6257 Call the system initgroups() to initialize the group access list with all of\n\ 6267 Call the system initgroups() to initialize the group access list with all of\n\
6258 the groups of which the specified username is a member, plus the specified\n\ 6268 the groups of which the specified username is a member, plus the specified\n\
6259 group id."); 6269 group id.");
6260 6270
6261 /* AC 3.5: funny apple logic */ 6271 /* AC 3.5: funny apple logic */
6262 static PyObject * 6272 static PyObject *
6263 posix_initgroups(PyObject *self, PyObject *args) 6273 posix_initgroups(PyObject *self, PyObject *args)
6264 { 6274 {
6265 PyObject *oname; 6275 PyObject *oname;
6266 char *username; 6276 const char *username;
6267 int res; 6277 int res;
6268 #ifdef __APPLE__ 6278 #ifdef __APPLE__
6269 int gid; 6279 int gid;
6270 #else 6280 #else
6271 gid_t gid; 6281 gid_t gid;
6272 #endif 6282 #endif
6273 6283
6274 #ifdef __APPLE__ 6284 #ifdef __APPLE__
6275 if (!PyArg_ParseTuple(args, "O&i:initgroups", 6285 if (!PyArg_ParseTuple(args, "O&i:initgroups",
6276 PyUnicode_FSConverter, &oname, 6286 PyUnicode_FSConverter, &oname,
(...skipping 853 matching lines...) Expand 10 before | Expand all | Expand 10 after
7130 return return_value; 7140 return return_value;
7131 } 7141 }
7132 7142
7133 #endif /* HAVE_READLINK */ 7143 #endif /* HAVE_READLINK */
7134 7144
7135 #if !defined(HAVE_READLINK) && defined(MS_WINDOWS) 7145 #if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
7136 7146
7137 static PyObject * 7147 static PyObject *
7138 win_readlink(PyObject *self, PyObject *args, PyObject *kwargs) 7148 win_readlink(PyObject *self, PyObject *args, PyObject *kwargs)
7139 { 7149 {
7140 wchar_t *path; 7150 const wchar_t *path;
7141 DWORD n_bytes_returned; 7151 DWORD n_bytes_returned;
7142 DWORD io_result; 7152 DWORD io_result;
7143 PyObject *po, *result; 7153 PyObject *po, *result;
7144 int dir_fd; 7154 int dir_fd;
7145 HANDLE reparse_point_handle; 7155 HANDLE reparse_point_handle;
7146 7156
7147 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; 7157 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
7148 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer; 7158 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
7149 wchar_t *print_name; 7159 const wchar_t *print_name;
7150 7160
7151 static char *keywords[] = {"path", "dir_fd", NULL}; 7161 static char *keywords[] = {"path", "dir_fd", NULL};
7152 7162
7153 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "U|$O&:readlink", keywords, 7163 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "U|$O&:readlink", keywords,
7154 &po, 7164 &po,
7155 dir_fd_unavailable, &dir_fd 7165 dir_fd_unavailable, &dir_fd
7156 )) 7166 ))
7157 return NULL; 7167 return NULL;
7158 7168
7159 path = PyUnicode_AsUnicode(po); 7169 path = PyUnicode_AsUnicode(po);
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
7207 7217
7208 #endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */ 7218 #endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
7209 7219
7210 7220
7211 7221
7212 #ifdef HAVE_SYMLINK 7222 #ifdef HAVE_SYMLINK
7213 7223
7214 #if defined(MS_WINDOWS) 7224 #if defined(MS_WINDOWS)
7215 7225
7216 /* Grab CreateSymbolicLinkW dynamically from kernel32 */ 7226 /* Grab CreateSymbolicLinkW dynamically from kernel32 */
7217 static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD) = NULL; 7227 static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPCWSTR, LPCWSTR, DWORD) = NULL;
7218 static DWORD (CALLBACK *Py_CreateSymbolicLinkA)(LPSTR, LPSTR, DWORD) = NULL; 7228 static DWORD (CALLBACK *Py_CreateSymbolicLinkA)(LPCSTR, LPCSTR, DWORD) = NULL;
7219 7229
7220 static int 7230 static int
7221 check_CreateSymbolicLink(void) 7231 check_CreateSymbolicLink(void)
7222 { 7232 {
7223 HINSTANCE hKernel32; 7233 HINSTANCE hKernel32;
7224 /* only recheck */ 7234 /* only recheck */
7225 if (Py_CreateSymbolicLinkW && Py_CreateSymbolicLinkA) 7235 if (Py_CreateSymbolicLinkW && Py_CreateSymbolicLinkA)
7226 return 1; 7236 return 1;
7227 hKernel32 = GetModuleHandleW(L"KERNEL32"); 7237 hKernel32 = GetModuleHandleW(L"KERNEL32");
7228 *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32, 7238 *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32,
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
7313 strcpy(dest_path, root); 7323 strcpy(dest_path, root);
7314 if(root_len) { 7324 if(root_len) {
7315 dest_path[root_len] = '\\'; 7325 dest_path[root_len] = '\\';
7316 root_len++; 7326 root_len++;
7317 } 7327 }
7318 strcpy(dest_path+root_len, rest); 7328 strcpy(dest_path+root_len, rest);
7319 } 7329 }
7320 7330
7321 /* Return True if the path at src relative to dest is a directory */ 7331 /* Return True if the path at src relative to dest is a directory */
7322 static int 7332 static int
7323 _check_dirW(WCHAR *src, WCHAR *dest) 7333 _check_dirW(LPCWSTR src, LPCWSTR dest)
7324 { 7334 {
7325 WIN32_FILE_ATTRIBUTE_DATA src_info; 7335 WIN32_FILE_ATTRIBUTE_DATA src_info;
7326 WCHAR dest_parent[MAX_PATH]; 7336 WCHAR dest_parent[MAX_PATH];
7327 WCHAR src_resolved[MAX_PATH] = L""; 7337 WCHAR src_resolved[MAX_PATH] = L"";
7328 7338
7329 /* dest_parent = os.path.dirname(dest) */ 7339 /* dest_parent = os.path.dirname(dest) */
7330 wcscpy(dest_parent, dest); 7340 wcscpy(dest_parent, dest);
7331 _dirnameW(dest_parent); 7341 _dirnameW(dest_parent);
7332 /* src_resolved = os.path.join(dest_parent, src) */ 7342 /* src_resolved = os.path.join(dest_parent, src) */
7333 _joinW(src_resolved, dest_parent, src); 7343 _joinW(src_resolved, dest_parent, src);
7334 return ( 7344 return (
7335 GetFileAttributesExW(src_resolved, GetFileExInfoStandard, &src_info) 7345 GetFileAttributesExW(src_resolved, GetFileExInfoStandard, &src_info)
7336 && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY 7346 && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY
7337 ); 7347 );
7338 } 7348 }
7339 7349
7340 /* Return True if the path at src relative to dest is a directory */ 7350 /* Return True if the path at src relative to dest is a directory */
7341 static int 7351 static int
7342 _check_dirA(const char *src, char *dest) 7352 _check_dirA(LPCSTR src, LPCSTR dest)
7343 { 7353 {
7344 WIN32_FILE_ATTRIBUTE_DATA src_info; 7354 WIN32_FILE_ATTRIBUTE_DATA src_info;
7345 char dest_parent[MAX_PATH]; 7355 char dest_parent[MAX_PATH];
7346 char src_resolved[MAX_PATH] = ""; 7356 char src_resolved[MAX_PATH] = "";
7347 7357
7348 /* dest_parent = os.path.dirname(dest) */ 7358 /* dest_parent = os.path.dirname(dest) */
7349 strcpy(dest_parent, dest); 7359 strcpy(dest_parent, dest);
7350 _dirnameA(dest_parent); 7360 _dirnameA(dest_parent);
7351 /* src_resolved = os.path.join(dest_parent, src) */ 7361 /* src_resolved = os.path.join(dest_parent, src) */
7352 _joinA(src_resolved, dest_parent, src); 7362 _joinA(src_resolved, dest_parent, src);
(...skipping 1039 matching lines...) Expand 10 before | Expand all | Expand 10 after
8392 ret = sendfile(out, in, &offset, count); 8402 ret = sendfile(out, in, &offset, count);
8393 Py_END_ALLOW_THREADS 8403 Py_END_ALLOW_THREADS
8394 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); 8404 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
8395 if (ret < 0) 8405 if (ret < 0)
8396 return (!async_err) ? posix_error() : NULL; 8406 return (!async_err) ? posix_error() : NULL;
8397 return Py_BuildValue("n", ret); 8407 return Py_BuildValue("n", ret);
8398 #endif 8408 #endif
8399 } 8409 }
8400 #endif /* HAVE_SENDFILE */ 8410 #endif /* HAVE_SENDFILE */
8401 8411
8402 #ifdef HAVE_COPY_FILE_RANGE
8403 /* from copy_file_range()'s man page: */
8404 static loff_t
8405 copy_file_range(int fd_in, loff_t *off_in, int fd_out, loff_t *off_out,
8406 size_t len, unsigned int flags)
8407 {
8408 return syscall(__NR_copy_file_range, fd_in, off_in, fd_out, off_out,
8409 len, flags);
8410 }
8411
8412 /* TODO: use clinic? */
8413 PyDoc_STRVAR(posix_copy_file_range__doc__,
8414 "copy_file_range(in, offset_in, out, offset_out, count, flags=0) -> \
8415 bytes_written\n\
8416 Copy count bytes from file descriptor in, starting from offset offset_in, \
8417 to file descriptor out, starting from offset offset_out. \
8418 If offset_in is None, then in is read from the current position; \
8419 respectively for offset_out.");
8420
8421 static PyObject *
8422 posix_copy_file_range(PyObject *self, PyObject *args, PyObject *kwdict)
8423 {
8424 int in, out;
8425 off_t offset_in, *p_offset_in, offset_out, *p_offset_out, count;
8426 Py_ssize_t ret;
8427 int async_err = 0;
8428 int flags = 0;
8429 PyObject *offset_in_obj, *offset_out_obj;
8430 static char *keywords[] = {"in", "offset_in", "out", "offset_out",
8431 "count", "flags", NULL};
8432
8433 /* O types are used to handle offset_*_obj==None -> from current position */
8434 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iOiOn|i:copy_file_range",
8435 keywords, &in, &offset_in_obj, &out, &offset_out_obj, &count, &flags))
8436 return NULL;
8437
8438 /* offset handling is complex. we convert the argument to a Python object.
8439 * if None, then the corresponding pointer is NULL.
8440 * otherwise, try to convert; if successful, use that
8441 * and point the pointer there.
8442 */
8443 if (offset_in_obj == Py_None)
8444 p_offset_in = NULL;
8445 else {
8446 if (!Py_off_t_converter(offset_in_obj, &offset_in))
8447 return NULL;
8448 p_offset_in = &offset_in;
8449 }
8450 if (offset_out_obj == Py_None)
8451 p_offset_out = NULL;
8452 else {
8453 if (!Py_off_t_converter(offset_out_obj, &offset_out))
8454 return NULL;
8455 p_offset_out = &offset_out;
8456 }
8457
8458 do {
8459 Py_BEGIN_ALLOW_THREADS
8460 ret = copy_file_range(in, p_offset_in, out, p_offset_out, count, flags);
8461 Py_END_ALLOW_THREADS
8462 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
8463 if (ret < 0)
8464 return async_err ? NULL : posix_error();
8465 return Py_BuildValue("n", ret);
8466 }
8467 #endif /* HAVE_COPY_FILE_RANGE */
8468 8412
8469 /*[clinic input] 8413 /*[clinic input]
8470 os.fstat 8414 os.fstat
8471 8415
8472 fd : int 8416 fd : int
8473 8417
8474 Perform a stat system call on the given file descriptor. 8418 Perform a stat system call on the given file descriptor.
8475 8419
8476 Like stat(), but for an open file descriptor. 8420 Like stat(), but for an open file descriptor.
8477 Equivalent to os.stat(fd). 8421 Equivalent to os.stat(fd).
(...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after
8724 _Py_END_SUPPRESS_IPH 8668 _Py_END_SUPPRESS_IPH
8725 Py_END_ALLOW_THREADS 8669 Py_END_ALLOW_THREADS
8726 } while (size < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); 8670 } while (size < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
8727 8671
8728 if (size < 0 && !async_err) 8672 if (size < 0 && !async_err)
8729 posix_error(); 8673 posix_error();
8730 return size; 8674 return size;
8731 } 8675 }
8732 #endif /* HAVE_PWRITE */ 8676 #endif /* HAVE_PWRITE */
8733 8677
8678 #ifdef HAVE_COPY_FILE_RANGE
8679 /* modified example found in copy_file_range()'s man page: */
8680 /* changed the return type to ssize_t so it can return -1 */
8681 static ssize_t
8682 copy_file_range(int fd_in, loff_t *off_in, int fd_out, loff_t *off_out,
8683 size_t len, unsigned int flags)
8684 {
8685 return syscall(__NR_copy_file_range, fd_in, off_in, fd_out, off_out,
8686 len, flags);
8687 }
8688
8689 PyDoc_STRVAR(posix_copy_file_range__doc__,
8690 "copy_file_range(src, dst, count, offset_src=None, offset_dst=None) -> \
8691 bytes_written\n\
8692 Copy count bytes from file descriptor src, starting from offset offset_src, \
8693 to file descriptor dst, starting from offset offset_dst. \
8694 If offset_src is None, then src is read from the current position; \
8695 respectively for offset_dst.");
8696
8697 static PyObject *
8698 posix_copy_file_range(PyObject *self, PyObject *args, PyObject *kwdict)
8699 {
8700 int src, dst;
8701 off_t offset_src, *p_offset_src, offset_dst, *p_offset_dst;
8702 Py_ssize_t count, ret;
8703 int async_err = 0;
8704 int flags = 0;
8705 PyObject *offset_src_obj = Py_None, *offset_dst_obj = Py_None;
8706 static char *keywords[] = {"src", "dst", "count",
8707 "offset_src", "offset_dst", NULL};
8708
8709 /* O types are used to handle offset_*_obj==None -> from current position */
8710 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iin|OO:copy_file_range",
8711 keywords, &src, &dst, &count, &offset_src_obj, &offset_dst_obj))
8712 return NULL;
8713
8714 /* offset handling is complex. we convert the argument to a Python object.
8715 * if None, then the corresponding pointer is NULL.
8716 * otherwise, try to convert; if successful, use that
8717 * and point the pointer there.
8718 */
8719 if (offset_src_obj == Py_None)
8720 p_offset_src = NULL;
8721 else {
8722 if (!Py_off_t_converter(offset_src_obj, &offset_src))
8723 return NULL;
8724 p_offset_src = &offset_src;
8725 }
8726 if (offset_dst_obj == Py_None)
8727 p_offset_dst = NULL;
8728 else {
8729 if (!Py_off_t_converter(offset_dst_obj, &offset_dst))
8730 return NULL;
8731 p_offset_dst = &offset_dst;
8732 }
8733
8734 do {
8735 Py_BEGIN_ALLOW_THREADS
8736 ret = copy_file_range(src, p_offset_src, dst, p_offset_dst, count, flags );
8737 Py_END_ALLOW_THREADS
8738 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
8739
8740 if (ret < 0)
8741 return async_err ? NULL : posix_error();
8742
8743 /* we currently ignore the changes to the offsets */
8744 return Py_BuildValue("n", ret);
8745 }
8746 #endif /* HAVE_COPY_FILE_RANGE */
8734 8747
8735 #ifdef HAVE_MKFIFO 8748 #ifdef HAVE_MKFIFO
8736 /*[clinic input] 8749 /*[clinic input]
8737 os.mkfifo 8750 os.mkfifo
8738 8751
8739 path: path_t 8752 path: path_t
8740 mode: int=0o666 8753 mode: int=0o666
8741 * 8754 *
8742 dir_fd: dir_fd(requires='mkfifoat')=None 8755 dir_fd: dir_fd(requires='mkfifoat')=None
8743 8756
(...skipping 344 matching lines...) Expand 10 before | Expand all | Expand 10 after
9088 value: unicode 9101 value: unicode
9089 / 9102 /
9090 9103
9091 Change or add an environment variable. 9104 Change or add an environment variable.
9092 [clinic start generated code]*/ 9105 [clinic start generated code]*/
9093 9106
9094 static PyObject * 9107 static PyObject *
9095 os_putenv_impl(PyModuleDef *module, PyObject *name, PyObject *value) 9108 os_putenv_impl(PyModuleDef *module, PyObject *name, PyObject *value)
9096 /*[clinic end generated code: output=a2438cf95e5a0c1c input=ba586581c2e6105f]*/ 9109 /*[clinic end generated code: output=a2438cf95e5a0c1c input=ba586581c2e6105f]*/
9097 { 9110 {
9098 wchar_t *env; 9111 const wchar_t *env;
9099 9112
9100 PyObject *unicode = PyUnicode_FromFormat("%U=%U", name, value); 9113 PyObject *unicode = PyUnicode_FromFormat("%U=%U", name, value);
9101 if (unicode == NULL) { 9114 if (unicode == NULL) {
9102 PyErr_NoMemory(); 9115 PyErr_NoMemory();
9103 return NULL; 9116 return NULL;
9104 } 9117 }
9105 if (_MAX_ENV < PyUnicode_GET_LENGTH(unicode)) { 9118 if (_MAX_ENV < PyUnicode_GET_LENGTH(unicode)) {
9106 PyErr_Format(PyExc_ValueError, 9119 PyErr_Format(PyExc_ValueError,
9107 "the environment variable is longer than %u characters", 9120 "the environment variable is longer than %u characters",
9108 _MAX_ENV); 9121 _MAX_ENV);
(...skipping 25 matching lines...) Expand all
9134 9147
9135 Change or add an environment variable. 9148 Change or add an environment variable.
9136 [clinic start generated code]*/ 9149 [clinic start generated code]*/
9137 9150
9138 static PyObject * 9151 static PyObject *
9139 os_putenv_impl(PyModuleDef *module, PyObject *name, PyObject *value) 9152 os_putenv_impl(PyModuleDef *module, PyObject *name, PyObject *value)
9140 /*[clinic end generated code: output=a2438cf95e5a0c1c input=a97bc6152f688d31]*/ 9153 /*[clinic end generated code: output=a2438cf95e5a0c1c input=a97bc6152f688d31]*/
9141 { 9154 {
9142 PyObject *bytes = NULL; 9155 PyObject *bytes = NULL;
9143 char *env; 9156 char *env;
9144 char *name_string = PyBytes_AsString(name); 9157 const char *name_string = PyBytes_AsString(name);
9145 char *value_string = PyBytes_AsString(value); 9158 const char *value_string = PyBytes_AsString(value);
9146 9159
9147 bytes = PyBytes_FromFormat("%s=%s", name_string, value_string); 9160 bytes = PyBytes_FromFormat("%s=%s", name_string, value_string);
9148 if (bytes == NULL) { 9161 if (bytes == NULL) {
9149 PyErr_NoMemory(); 9162 PyErr_NoMemory();
9150 return NULL; 9163 return NULL;
9151 } 9164 }
9152 9165
9153 env = PyBytes_AS_STRING(bytes); 9166 env = PyBytes_AS_STRING(bytes);
9154 if (putenv(env)) { 9167 if (putenv(env)) {
9155 Py_DECREF(bytes); 9168 Py_DECREF(bytes);
(...skipping 1371 matching lines...) Expand 10 before | Expand all | Expand 10 after
10527 const struct constdef *c1 = 10540 const struct constdef *c1 =
10528 (const struct constdef *) v1; 10541 (const struct constdef *) v1;
10529 const struct constdef *c2 = 10542 const struct constdef *c2 =
10530 (const struct constdef *) v2; 10543 (const struct constdef *) v2;
10531 10544
10532 return strcmp(c1->name, c2->name); 10545 return strcmp(c1->name, c2->name);
10533 } 10546 }
10534 10547
10535 static int 10548 static int
10536 setup_confname_table(struct constdef *table, size_t tablesize, 10549 setup_confname_table(struct constdef *table, size_t tablesize,
10537 char *tablename, PyObject *module) 10550 const char *tablename, PyObject *module)
10538 { 10551 {
10539 PyObject *d = NULL; 10552 PyObject *d = NULL;
10540 size_t i; 10553 size_t i;
10541 10554
10542 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); 10555 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
10543 d = PyDict_New(); 10556 d = PyDict_New();
10544 if (d == NULL) 10557 if (d == NULL)
10545 return -1; 10558 return -1;
10546 10559
10547 for (i=0; i < tablesize; ++i) { 10560 for (i=0; i < tablesize; ++i) {
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
10654 } 10667 }
10655 } 10668 }
10656 return has_ShellExecute; 10669 return has_ShellExecute;
10657 } 10670 }
10658 10671
10659 10672
10660 static PyObject * 10673 static PyObject *
10661 win32_startfile(PyObject *self, PyObject *args) 10674 win32_startfile(PyObject *self, PyObject *args)
10662 { 10675 {
10663 PyObject *ofilepath; 10676 PyObject *ofilepath;
10664 char *filepath; 10677 const char *filepath;
10665 char *operation = NULL; 10678 const char *operation = NULL;
10666 wchar_t *wpath, *woperation; 10679 const wchar_t *wpath, *woperation;
10667 HINSTANCE rc; 10680 HINSTANCE rc;
10668 10681
10669 PyObject *unipath, *uoperation = NULL; 10682 PyObject *unipath, *uoperation = NULL;
10670 10683
10671 if(!check_ShellExecute()) { 10684 if(!check_ShellExecute()) {
10672 /* If the OS doesn't have ShellExecute, return a 10685 /* If the OS doesn't have ShellExecute, return a
10673 NotImplementedError. */ 10686 NotImplementedError. */
10674 return PyErr_Format(PyExc_NotImplementedError, 10687 return PyErr_Format(PyExc_NotImplementedError,
10675 "startfile not available on this platform"); 10688 "startfile not available on this platform");
10676 } 10689 }
(...skipping 384 matching lines...) Expand 10 before | Expand all | Expand 10 after
11061 PyObject *result = NULL; 11074 PyObject *result = NULL;
11062 const char *name; 11075 const char *name;
11063 char *buffer = NULL; 11076 char *buffer = NULL;
11064 11077
11065 if (fd_and_follow_symlinks_invalid("listxattr", path->fd, follow_symlinks)) 11078 if (fd_and_follow_symlinks_invalid("listxattr", path->fd, follow_symlinks))
11066 goto exit; 11079 goto exit;
11067 11080
11068 name = path->narrow ? path->narrow : "."; 11081 name = path->narrow ? path->narrow : ".";
11069 11082
11070 for (i = 0; ; i++) { 11083 for (i = 0; ; i++) {
11071 char *start, *trace, *end; 11084 const char *start, *trace, *end;
11072 ssize_t length; 11085 ssize_t length;
11073 static const Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 }; 11086 static const Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 };
11074 Py_ssize_t buffer_size = buffer_sizes[i]; 11087 Py_ssize_t buffer_size = buffer_sizes[i];
11075 if (!buffer_size) { 11088 if (!buffer_size) {
11076 /* ERANGE */ 11089 /* ERANGE */
11077 path_error(path); 11090 path_error(path);
11078 break; 11091 break;
11079 } 11092 }
11080 buffer = PyMem_MALLOC(buffer_size); 11093 buffer = PyMem_MALLOC(buffer_size);
11081 if (!buffer) { 11094 if (!buffer) {
(...skipping 458 matching lines...) Expand 10 before | Expand all | Expand 10 after
11540 return PyBool_FromLong(result); 11553 return PyBool_FromLong(result);
11541 } 11554 }
11542 11555
11543 static PyObject * 11556 static PyObject *
11544 DirEntry_fetch_stat(DirEntry *self, int follow_symlinks) 11557 DirEntry_fetch_stat(DirEntry *self, int follow_symlinks)
11545 { 11558 {
11546 int result; 11559 int result;
11547 struct _Py_stat_struct st; 11560 struct _Py_stat_struct st;
11548 11561
11549 #ifdef MS_WINDOWS 11562 #ifdef MS_WINDOWS
11550 wchar_t *path; 11563 const wchar_t *path;
11551 11564
11552 path = PyUnicode_AsUnicode(self->path); 11565 path = PyUnicode_AsUnicode(self->path);
11553 if (!path) 11566 if (!path)
11554 return NULL; 11567 return NULL;
11555 11568
11556 if (follow_symlinks) 11569 if (follow_symlinks)
11557 result = win32_stat_w(path, &st); 11570 result = win32_stat_w(path, &st);
11558 else 11571 else
11559 result = win32_lstat_w(path, &st); 11572 result = win32_lstat_w(path, &st);
11560 11573
11561 if (result != 0) { 11574 if (result != 0) {
11562 return PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError, 11575 return PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError,
11563 0, self->path); 11576 0, self->path);
11564 } 11577 }
11565 #else /* POSIX */ 11578 #else /* POSIX */
11566 PyObject *bytes; 11579 PyObject *bytes;
11567 char *path; 11580 const char *path;
11568 11581
11569 if (!PyUnicode_FSConverter(self->path, &bytes)) 11582 if (!PyUnicode_FSConverter(self->path, &bytes))
11570 return NULL; 11583 return NULL;
11571 path = PyBytes_AS_STRING(bytes); 11584 path = PyBytes_AS_STRING(bytes);
11572 11585
11573 if (follow_symlinks) 11586 if (follow_symlinks)
11574 result = STAT(path, &st); 11587 result = STAT(path, &st);
11575 else 11588 else
11576 result = LSTAT(path, &st); 11589 result = LSTAT(path, &st);
11577 Py_DECREF(bytes); 11590 Py_DECREF(bytes);
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after
11741 return NULL; 11754 return NULL;
11742 11755
11743 return DirEntry_py_test_mode(self, follow_symlinks, S_IFREG); 11756 return DirEntry_py_test_mode(self, follow_symlinks, S_IFREG);
11744 } 11757 }
11745 11758
11746 static PyObject * 11759 static PyObject *
11747 DirEntry_inode(DirEntry *self) 11760 DirEntry_inode(DirEntry *self)
11748 { 11761 {
11749 #ifdef MS_WINDOWS 11762 #ifdef MS_WINDOWS
11750 if (!self->got_file_index) { 11763 if (!self->got_file_index) {
11751 wchar_t *path; 11764 const wchar_t *path;
11752 struct _Py_stat_struct stat; 11765 struct _Py_stat_struct stat;
11753 11766
11754 path = PyUnicode_AsUnicode(self->path); 11767 path = PyUnicode_AsUnicode(self->path);
11755 if (!path) 11768 if (!path)
11756 return NULL; 11769 return NULL;
11757 11770
11758 if (win32_lstat_w(path, &stat) != 0) { 11771 if (win32_lstat_w(path, &stat) != 0) {
11759 return PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError, 11772 return PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError,
11760 0, self->path); 11773 0, self->path);
11761 } 11774 }
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
11835 0, /* tp_weaklistoffset */ 11848 0, /* tp_weaklistoffset */
11836 0, /* tp_iter */ 11849 0, /* tp_iter */
11837 0, /* tp_iternext */ 11850 0, /* tp_iternext */
11838 DirEntry_methods, /* tp_methods */ 11851 DirEntry_methods, /* tp_methods */
11839 DirEntry_members, /* tp_members */ 11852 DirEntry_members, /* tp_members */
11840 }; 11853 };
11841 11854
11842 #ifdef MS_WINDOWS 11855 #ifdef MS_WINDOWS
11843 11856
11844 static wchar_t * 11857 static wchar_t *
11845 join_path_filenameW(wchar_t *path_wide, wchar_t* filename) 11858 join_path_filenameW(const wchar_t *path_wide, const wchar_t *filename)
11846 { 11859 {
11847 Py_ssize_t path_len; 11860 Py_ssize_t path_len;
11848 Py_ssize_t size; 11861 Py_ssize_t size;
11849 wchar_t *result; 11862 wchar_t *result;
11850 wchar_t ch; 11863 wchar_t ch;
11851 11864
11852 if (!path_wide) { /* Default arg: "." */ 11865 if (!path_wide) { /* Default arg: "." */
11853 path_wide = L"."; 11866 path_wide = L".";
11854 path_len = 1; 11867 path_len = 1;
11855 } 11868 }
(...skipping 410 matching lines...) Expand 10 before | Expand all | Expand 10 after
12266 }; 12279 };
12267 12280
12268 static PyObject * 12281 static PyObject *
12269 posix_scandir(PyObject *self, PyObject *args, PyObject *kwargs) 12282 posix_scandir(PyObject *self, PyObject *args, PyObject *kwargs)
12270 { 12283 {
12271 ScandirIterator *iterator; 12284 ScandirIterator *iterator;
12272 static char *keywords[] = {"path", NULL}; 12285 static char *keywords[] = {"path", NULL};
12273 #ifdef MS_WINDOWS 12286 #ifdef MS_WINDOWS
12274 wchar_t *path_strW; 12287 wchar_t *path_strW;
12275 #else 12288 #else
12276 char *path; 12289 const char *path;
12277 #endif 12290 #endif
12278 12291
12279 iterator = PyObject_New(ScandirIterator, &ScandirIteratorType); 12292 iterator = PyObject_New(ScandirIterator, &ScandirIteratorType);
12280 if (!iterator) 12293 if (!iterator)
12281 return NULL; 12294 return NULL;
12282 memset(&iterator->path, 0, sizeof(path_t)); 12295 memset(&iterator->path, 0, sizeof(path_t));
12283 iterator->path.function_name = "scandir"; 12296 iterator->path.function_name = "scandir";
12284 iterator->path.nullable = 1; 12297 iterator->path.nullable = 1;
12285 12298
12286 #ifdef MS_WINDOWS 12299 #ifdef MS_WINDOWS
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
12338 } 12351 }
12339 #endif 12352 #endif
12340 12353
12341 return (PyObject *)iterator; 12354 return (PyObject *)iterator;
12342 12355
12343 error: 12356 error:
12344 Py_DECREF(iterator); 12357 Py_DECREF(iterator);
12345 return NULL; 12358 return NULL;
12346 } 12359 }
12347 12360
12361 /*
12362 Return the file system path representation of the object.
12363
12364 If the object is str or bytes, then allow it to pass through with
12365 an incremented refcount. If the object defines __fspath__(), then
12366 return the result of that method. All other types raise a TypeError.
12367 */
12368 PyObject *
12369 PyOS_FSPath(PyObject *path)
12370 {
12371 _Py_IDENTIFIER(__fspath__);
12372 PyObject *func = NULL;
12373 PyObject *path_repr = NULL;
12374
12375 if (PyUnicode_Check(path) || PyBytes_Check(path)) {
12376 Py_INCREF(path);
12377 return path;
12378 }
12379
12380 func = _PyObject_LookupSpecial(path, &PyId___fspath__);
12381 if (NULL == func) {
12382 return PyErr_Format(PyExc_TypeError,
12383 "expected str, bytes or os.PathLike object, "
12384 "not %S",
12385 path->ob_type);
12386 }
12387
12388 path_repr = PyObject_CallFunctionObjArgs(func, NULL);
12389 Py_DECREF(func);
12390 return path_repr;
12391 }
12392
12393 /*[clinic input]
12394 os.fspath
12395
12396 path: object
12397
12398 Return the file system path representation of the object.
12399
12400 If the object is str or bytes, then allow it to pass through with
12401 an incremented refcount. If the object defines __fspath__(), then
12402 return the result of that method. All other types raise a TypeError.
12403 [clinic start generated code]*/
12404
12405 static PyObject *
12406 os_fspath_impl(PyModuleDef *module, PyObject *path)
12407 /*[clinic end generated code: output=51ef0c2772c1932a input=652c7c37e4be1c13]*/
12408 {
12409 return PyOS_FSPath(path);
12410 }
12348 12411
12349 #include "clinic/posixmodule.c.h" 12412 #include "clinic/posixmodule.c.h"
12350 12413
12351 /*[clinic input] 12414 /*[clinic input]
12352 dump buffer 12415 dump buffer
12353 [clinic start generated code]*/ 12416 [clinic start generated code]*/
12354 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=524ce2e021e4eba6]*/ 12417 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=524ce2e021e4eba6]*/
12355 12418
12356 12419
12357 static PyMethodDef posix_methods[] = { 12420 static PyMethodDef posix_methods[] = {
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after
12542 OS_SET_INHERITABLE_METHODDEF 12605 OS_SET_INHERITABLE_METHODDEF
12543 OS_GET_HANDLE_INHERITABLE_METHODDEF 12606 OS_GET_HANDLE_INHERITABLE_METHODDEF
12544 OS_SET_HANDLE_INHERITABLE_METHODDEF 12607 OS_SET_HANDLE_INHERITABLE_METHODDEF
12545 #ifndef MS_WINDOWS 12608 #ifndef MS_WINDOWS
12546 {"get_blocking", posix_get_blocking, METH_VARARGS, get_blocking__doc__}, 12609 {"get_blocking", posix_get_blocking, METH_VARARGS, get_blocking__doc__},
12547 {"set_blocking", posix_set_blocking, METH_VARARGS, set_blocking__doc__}, 12610 {"set_blocking", posix_set_blocking, METH_VARARGS, set_blocking__doc__},
12548 #endif 12611 #endif
12549 {"scandir", (PyCFunction)posix_scandir, 12612 {"scandir", (PyCFunction)posix_scandir,
12550 METH_VARARGS | METH_KEYWORDS, 12613 METH_VARARGS | METH_KEYWORDS,
12551 posix_scandir__doc__}, 12614 posix_scandir__doc__},
12615 OS_FSPATH_METHODDEF
12552 {NULL, NULL} /* Sentinel */ 12616 {NULL, NULL} /* Sentinel */
12553 }; 12617 };
12554 12618
12555 12619
12556 #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) 12620 #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
12557 static int 12621 static int
12558 enable_symlink() 12622 enable_symlink()
12559 { 12623 {
12560 HANDLE tok; 12624 HANDLE tok;
12561 TOKEN_PRIVILEGES tok_priv; 12625 TOKEN_PRIVILEGES tok_priv;
(...skipping 395 matching lines...) Expand 10 before | Expand all | Expand 10 after
12957 if (PyModule_AddIntConstant(m, "SCHED_FX", SCHED_FSS)) return -1; 13021 if (PyModule_AddIntConstant(m, "SCHED_FX", SCHED_FSS)) return -1;
12958 #endif 13022 #endif
12959 #endif 13023 #endif
12960 13024
12961 #ifdef USE_XATTRS 13025 #ifdef USE_XATTRS
12962 if (PyModule_AddIntMacro(m, XATTR_CREATE)) return -1; 13026 if (PyModule_AddIntMacro(m, XATTR_CREATE)) return -1;
12963 if (PyModule_AddIntMacro(m, XATTR_REPLACE)) return -1; 13027 if (PyModule_AddIntMacro(m, XATTR_REPLACE)) return -1;
12964 if (PyModule_AddIntMacro(m, XATTR_SIZE_MAX)) return -1; 13028 if (PyModule_AddIntMacro(m, XATTR_SIZE_MAX)) return -1;
12965 #endif 13029 #endif
12966 13030
12967 #ifdef RTLD_LAZY 13031 #if HAVE_DECL_RTLD_LAZY
12968 if (PyModule_AddIntMacro(m, RTLD_LAZY)) return -1; 13032 if (PyModule_AddIntMacro(m, RTLD_LAZY)) return -1;
12969 #endif 13033 #endif
12970 #ifdef RTLD_NOW 13034 #if HAVE_DECL_RTLD_NOW
12971 if (PyModule_AddIntMacro(m, RTLD_NOW)) return -1; 13035 if (PyModule_AddIntMacro(m, RTLD_NOW)) return -1;
12972 #endif 13036 #endif
12973 #ifdef RTLD_GLOBAL 13037 #if HAVE_DECL_RTLD_GLOBAL
12974 if (PyModule_AddIntMacro(m, RTLD_GLOBAL)) return -1; 13038 if (PyModule_AddIntMacro(m, RTLD_GLOBAL)) return -1;
12975 #endif 13039 #endif
12976 #ifdef RTLD_LOCAL 13040 #if HAVE_DECL_RTLD_LOCAL
12977 if (PyModule_AddIntMacro(m, RTLD_LOCAL)) return -1; 13041 if (PyModule_AddIntMacro(m, RTLD_LOCAL)) return -1;
12978 #endif 13042 #endif
12979 #ifdef RTLD_NODELETE 13043 #if HAVE_DECL_RTLD_NODELETE
12980 if (PyModule_AddIntMacro(m, RTLD_NODELETE)) return -1; 13044 if (PyModule_AddIntMacro(m, RTLD_NODELETE)) return -1;
12981 #endif 13045 #endif
12982 #ifdef RTLD_NOLOAD 13046 #if HAVE_DECL_RTLD_NOLOAD
12983 if (PyModule_AddIntMacro(m, RTLD_NOLOAD)) return -1; 13047 if (PyModule_AddIntMacro(m, RTLD_NOLOAD)) return -1;
12984 #endif 13048 #endif
12985 #ifdef RTLD_DEEPBIND 13049 #if HAVE_DECL_RTLD_DEEPBIND
12986 if (PyModule_AddIntMacro(m, RTLD_DEEPBIND)) return -1; 13050 if (PyModule_AddIntMacro(m, RTLD_DEEPBIND)) return -1;
12987 #endif 13051 #endif
12988 13052
12989 return 0; 13053 return 0;
12990 } 13054 }
12991 13055
12992 13056
12993 static struct PyModuleDef posixmodule = { 13057 static struct PyModuleDef posixmodule = {
12994 PyModuleDef_HEAD_INIT, 13058 PyModuleDef_HEAD_INIT,
12995 MODNAME, 13059 MODNAME,
(...skipping 319 matching lines...) Expand 10 before | Expand all | Expand 10 after
13315 PyModule_AddObject(m, "_have_functions", list); 13379 PyModule_AddObject(m, "_have_functions", list);
13316 13380
13317 initialized = 1; 13381 initialized = 1;
13318 13382
13319 return m; 13383 return m;
13320 } 13384 }
13321 13385
13322 #ifdef __cplusplus 13386 #ifdef __cplusplus
13323 } 13387 }
13324 #endif 13388 #endif
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+