1 /*[clinic input]
2 preserve
3 [clinic start generated code]*/
4 
5 PyDoc_STRVAR(os_stat__doc__,
6 "stat($module, /, path, *, dir_fd=None, follow_symlinks=True)\n"
7 "--\n"
8 "\n"
9 "Perform a stat system call on the given path.\n"
10 "\n"
11 "  path\n"
12 "    Path to be examined; can be string, bytes, a path-like object or\n"
13 "    open-file-descriptor int.\n"
14 "  dir_fd\n"
15 "    If not None, it should be a file descriptor open to a directory,\n"
16 "    and path should be a relative string; path will then be relative to\n"
17 "    that directory.\n"
18 "  follow_symlinks\n"
19 "    If False, and the last element of the path is a symbolic link,\n"
20 "    stat will examine the symbolic link itself instead of the file\n"
21 "    the link points to.\n"
22 "\n"
23 "dir_fd and follow_symlinks may not be implemented\n"
24 "  on your platform.  If they are unavailable, using them will raise a\n"
25 "  NotImplementedError.\n"
26 "\n"
27 "It\'s an error to use dir_fd or follow_symlinks when specifying path as\n"
28 "  an open file descriptor.");
29 
30 #define OS_STAT_METHODDEF    \
31     {"stat", (PyCFunction)(void(*)(void))os_stat, METH_FASTCALL|METH_KEYWORDS, os_stat__doc__},
32 
33 static PyObject *
34 os_stat_impl(PyObject *module, path_t *path, int dir_fd, int follow_symlinks);
35 
36 static PyObject *
os_stat(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)37 os_stat(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
38 {
39     PyObject *return_value = NULL;
40     static const char * const _keywords[] = {"path", "dir_fd", "follow_symlinks", NULL};
41     static _PyArg_Parser _parser = {NULL, _keywords, "stat", 0};
42     PyObject *argsbuf[3];
43     Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
44     path_t path = PATH_T_INITIALIZE("stat", "path", 0, 1);
45     int dir_fd = DEFAULT_DIR_FD;
46     int follow_symlinks = 1;
47 
48     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
49     if (!args) {
50         goto exit;
51     }
52     if (!path_converter(args[0], &path)) {
53         goto exit;
54     }
55     if (!noptargs) {
56         goto skip_optional_kwonly;
57     }
58     if (args[1]) {
59         if (!FSTATAT_DIR_FD_CONVERTER(args[1], &dir_fd)) {
60             goto exit;
61         }
62         if (!--noptargs) {
63             goto skip_optional_kwonly;
64         }
65     }
66     follow_symlinks = PyObject_IsTrue(args[2]);
67     if (follow_symlinks < 0) {
68         goto exit;
69     }
70 skip_optional_kwonly:
71     return_value = os_stat_impl(module, &path, dir_fd, follow_symlinks);
72 
73 exit:
74     /* Cleanup for path */
75     path_cleanup(&path);
76 
77     return return_value;
78 }
79 
80 PyDoc_STRVAR(os_lstat__doc__,
81 "lstat($module, /, path, *, dir_fd=None)\n"
82 "--\n"
83 "\n"
84 "Perform a stat system call on the given path, without following symbolic links.\n"
85 "\n"
86 "Like stat(), but do not follow symbolic links.\n"
87 "Equivalent to stat(path, follow_symlinks=False).");
88 
89 #define OS_LSTAT_METHODDEF    \
90     {"lstat", (PyCFunction)(void(*)(void))os_lstat, METH_FASTCALL|METH_KEYWORDS, os_lstat__doc__},
91 
92 static PyObject *
93 os_lstat_impl(PyObject *module, path_t *path, int dir_fd);
94 
95 static PyObject *
os_lstat(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)96 os_lstat(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
97 {
98     PyObject *return_value = NULL;
99     static const char * const _keywords[] = {"path", "dir_fd", NULL};
100     static _PyArg_Parser _parser = {NULL, _keywords, "lstat", 0};
101     PyObject *argsbuf[2];
102     Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
103     path_t path = PATH_T_INITIALIZE("lstat", "path", 0, 0);
104     int dir_fd = DEFAULT_DIR_FD;
105 
106     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
107     if (!args) {
108         goto exit;
109     }
110     if (!path_converter(args[0], &path)) {
111         goto exit;
112     }
113     if (!noptargs) {
114         goto skip_optional_kwonly;
115     }
116     if (!FSTATAT_DIR_FD_CONVERTER(args[1], &dir_fd)) {
117         goto exit;
118     }
119 skip_optional_kwonly:
120     return_value = os_lstat_impl(module, &path, dir_fd);
121 
122 exit:
123     /* Cleanup for path */
124     path_cleanup(&path);
125 
126     return return_value;
127 }
128 
129 PyDoc_STRVAR(os_access__doc__,
130 "access($module, /, path, mode, *, dir_fd=None, effective_ids=False,\n"
131 "       follow_symlinks=True)\n"
132 "--\n"
133 "\n"
134 "Use the real uid/gid to test for access to a path.\n"
135 "\n"
136 "  path\n"
137 "    Path to be tested; can be string, bytes, or a path-like object.\n"
138 "  mode\n"
139 "    Operating-system mode bitfield.  Can be F_OK to test existence,\n"
140 "    or the inclusive-OR of R_OK, W_OK, and X_OK.\n"
141 "  dir_fd\n"
142 "    If not None, it should be a file descriptor open to a directory,\n"
143 "    and path should be relative; path will then be relative to that\n"
144 "    directory.\n"
145 "  effective_ids\n"
146 "    If True, access will use the effective uid/gid instead of\n"
147 "    the real uid/gid.\n"
148 "  follow_symlinks\n"
149 "    If False, and the last element of the path is a symbolic link,\n"
150 "    access will examine the symbolic link itself instead of the file\n"
151 "    the link points to.\n"
152 "\n"
153 "dir_fd, effective_ids, and follow_symlinks may not be implemented\n"
154 "  on your platform.  If they are unavailable, using them will raise a\n"
155 "  NotImplementedError.\n"
156 "\n"
157 "Note that most operations will use the effective uid/gid, therefore this\n"
158 "  routine can be used in a suid/sgid environment to test if the invoking user\n"
159 "  has the specified access to the path.");
160 
161 #define OS_ACCESS_METHODDEF    \
162     {"access", (PyCFunction)(void(*)(void))os_access, METH_FASTCALL|METH_KEYWORDS, os_access__doc__},
163 
164 static int
165 os_access_impl(PyObject *module, path_t *path, int mode, int dir_fd,
166                int effective_ids, int follow_symlinks);
167 
168 static PyObject *
os_access(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)169 os_access(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
170 {
171     PyObject *return_value = NULL;
172     static const char * const _keywords[] = {"path", "mode", "dir_fd", "effective_ids", "follow_symlinks", NULL};
173     static _PyArg_Parser _parser = {NULL, _keywords, "access", 0};
174     PyObject *argsbuf[5];
175     Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
176     path_t path = PATH_T_INITIALIZE("access", "path", 0, 0);
177     int mode;
178     int dir_fd = DEFAULT_DIR_FD;
179     int effective_ids = 0;
180     int follow_symlinks = 1;
181     int _return_value;
182 
183     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf);
184     if (!args) {
185         goto exit;
186     }
187     if (!path_converter(args[0], &path)) {
188         goto exit;
189     }
190     mode = _PyLong_AsInt(args[1]);
191     if (mode == -1 && PyErr_Occurred()) {
192         goto exit;
193     }
194     if (!noptargs) {
195         goto skip_optional_kwonly;
196     }
197     if (args[2]) {
198         if (!FACCESSAT_DIR_FD_CONVERTER(args[2], &dir_fd)) {
199             goto exit;
200         }
201         if (!--noptargs) {
202             goto skip_optional_kwonly;
203         }
204     }
205     if (args[3]) {
206         effective_ids = PyObject_IsTrue(args[3]);
207         if (effective_ids < 0) {
208             goto exit;
209         }
210         if (!--noptargs) {
211             goto skip_optional_kwonly;
212         }
213     }
214     follow_symlinks = PyObject_IsTrue(args[4]);
215     if (follow_symlinks < 0) {
216         goto exit;
217     }
218 skip_optional_kwonly:
219     _return_value = os_access_impl(module, &path, mode, dir_fd, effective_ids, follow_symlinks);
220     if ((_return_value == -1) && PyErr_Occurred()) {
221         goto exit;
222     }
223     return_value = PyBool_FromLong((long)_return_value);
224 
225 exit:
226     /* Cleanup for path */
227     path_cleanup(&path);
228 
229     return return_value;
230 }
231 
232 #if defined(HAVE_TTYNAME)
233 
234 PyDoc_STRVAR(os_ttyname__doc__,
235 "ttyname($module, fd, /)\n"
236 "--\n"
237 "\n"
238 "Return the name of the terminal device connected to \'fd\'.\n"
239 "\n"
240 "  fd\n"
241 "    Integer file descriptor handle.");
242 
243 #define OS_TTYNAME_METHODDEF    \
244     {"ttyname", (PyCFunction)os_ttyname, METH_O, os_ttyname__doc__},
245 
246 static PyObject *
247 os_ttyname_impl(PyObject *module, int fd);
248 
249 static PyObject *
os_ttyname(PyObject * module,PyObject * arg)250 os_ttyname(PyObject *module, PyObject *arg)
251 {
252     PyObject *return_value = NULL;
253     int fd;
254 
255     fd = _PyLong_AsInt(arg);
256     if (fd == -1 && PyErr_Occurred()) {
257         goto exit;
258     }
259     return_value = os_ttyname_impl(module, fd);
260 
261 exit:
262     return return_value;
263 }
264 
265 #endif /* defined(HAVE_TTYNAME) */
266 
267 #if defined(HAVE_CTERMID)
268 
269 PyDoc_STRVAR(os_ctermid__doc__,
270 "ctermid($module, /)\n"
271 "--\n"
272 "\n"
273 "Return the name of the controlling terminal for this process.");
274 
275 #define OS_CTERMID_METHODDEF    \
276     {"ctermid", (PyCFunction)os_ctermid, METH_NOARGS, os_ctermid__doc__},
277 
278 static PyObject *
279 os_ctermid_impl(PyObject *module);
280 
281 static PyObject *
os_ctermid(PyObject * module,PyObject * Py_UNUSED (ignored))282 os_ctermid(PyObject *module, PyObject *Py_UNUSED(ignored))
283 {
284     return os_ctermid_impl(module);
285 }
286 
287 #endif /* defined(HAVE_CTERMID) */
288 
289 PyDoc_STRVAR(os_chdir__doc__,
290 "chdir($module, /, path)\n"
291 "--\n"
292 "\n"
293 "Change the current working directory to the specified path.\n"
294 "\n"
295 "path may always be specified as a string.\n"
296 "On some platforms, path may also be specified as an open file descriptor.\n"
297 "  If this functionality is unavailable, using it raises an exception.");
298 
299 #define OS_CHDIR_METHODDEF    \
300     {"chdir", (PyCFunction)(void(*)(void))os_chdir, METH_FASTCALL|METH_KEYWORDS, os_chdir__doc__},
301 
302 static PyObject *
303 os_chdir_impl(PyObject *module, path_t *path);
304 
305 static PyObject *
os_chdir(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)306 os_chdir(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
307 {
308     PyObject *return_value = NULL;
309     static const char * const _keywords[] = {"path", NULL};
310     static _PyArg_Parser _parser = {NULL, _keywords, "chdir", 0};
311     PyObject *argsbuf[1];
312     path_t path = PATH_T_INITIALIZE("chdir", "path", 0, PATH_HAVE_FCHDIR);
313 
314     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
315     if (!args) {
316         goto exit;
317     }
318     if (!path_converter(args[0], &path)) {
319         goto exit;
320     }
321     return_value = os_chdir_impl(module, &path);
322 
323 exit:
324     /* Cleanup for path */
325     path_cleanup(&path);
326 
327     return return_value;
328 }
329 
330 #if defined(HAVE_FCHDIR)
331 
332 PyDoc_STRVAR(os_fchdir__doc__,
333 "fchdir($module, /, fd)\n"
334 "--\n"
335 "\n"
336 "Change to the directory of the given file descriptor.\n"
337 "\n"
338 "fd must be opened on a directory, not a file.\n"
339 "Equivalent to os.chdir(fd).");
340 
341 #define OS_FCHDIR_METHODDEF    \
342     {"fchdir", (PyCFunction)(void(*)(void))os_fchdir, METH_FASTCALL|METH_KEYWORDS, os_fchdir__doc__},
343 
344 static PyObject *
345 os_fchdir_impl(PyObject *module, int fd);
346 
347 static PyObject *
os_fchdir(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)348 os_fchdir(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
349 {
350     PyObject *return_value = NULL;
351     static const char * const _keywords[] = {"fd", NULL};
352     static _PyArg_Parser _parser = {NULL, _keywords, "fchdir", 0};
353     PyObject *argsbuf[1];
354     int fd;
355 
356     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
357     if (!args) {
358         goto exit;
359     }
360     if (!_PyLong_FileDescriptor_Converter(args[0], &fd)) {
361         goto exit;
362     }
363     return_value = os_fchdir_impl(module, fd);
364 
365 exit:
366     return return_value;
367 }
368 
369 #endif /* defined(HAVE_FCHDIR) */
370 
371 PyDoc_STRVAR(os_chmod__doc__,
372 "chmod($module, /, path, mode, *, dir_fd=None, follow_symlinks=True)\n"
373 "--\n"
374 "\n"
375 "Change the access permissions of a file.\n"
376 "\n"
377 "  path\n"
378 "    Path to be modified.  May always be specified as a str, bytes, or a path-like object.\n"
379 "    On some platforms, path may also be specified as an open file descriptor.\n"
380 "    If this functionality is unavailable, using it raises an exception.\n"
381 "  mode\n"
382 "    Operating-system mode bitfield.\n"
383 "  dir_fd\n"
384 "    If not None, it should be a file descriptor open to a directory,\n"
385 "    and path should be relative; path will then be relative to that\n"
386 "    directory.\n"
387 "  follow_symlinks\n"
388 "    If False, and the last element of the path is a symbolic link,\n"
389 "    chmod will modify the symbolic link itself instead of the file\n"
390 "    the link points to.\n"
391 "\n"
392 "It is an error to use dir_fd or follow_symlinks when specifying path as\n"
393 "  an open file descriptor.\n"
394 "dir_fd and follow_symlinks may not be implemented on your platform.\n"
395 "  If they are unavailable, using them will raise a NotImplementedError.");
396 
397 #define OS_CHMOD_METHODDEF    \
398     {"chmod", (PyCFunction)(void(*)(void))os_chmod, METH_FASTCALL|METH_KEYWORDS, os_chmod__doc__},
399 
400 static PyObject *
401 os_chmod_impl(PyObject *module, path_t *path, int mode, int dir_fd,
402               int follow_symlinks);
403 
404 static PyObject *
os_chmod(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)405 os_chmod(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
406 {
407     PyObject *return_value = NULL;
408     static const char * const _keywords[] = {"path", "mode", "dir_fd", "follow_symlinks", NULL};
409     static _PyArg_Parser _parser = {NULL, _keywords, "chmod", 0};
410     PyObject *argsbuf[4];
411     Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
412     path_t path = PATH_T_INITIALIZE("chmod", "path", 0, PATH_HAVE_FCHMOD);
413     int mode;
414     int dir_fd = DEFAULT_DIR_FD;
415     int follow_symlinks = 1;
416 
417     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf);
418     if (!args) {
419         goto exit;
420     }
421     if (!path_converter(args[0], &path)) {
422         goto exit;
423     }
424     mode = _PyLong_AsInt(args[1]);
425     if (mode == -1 && PyErr_Occurred()) {
426         goto exit;
427     }
428     if (!noptargs) {
429         goto skip_optional_kwonly;
430     }
431     if (args[2]) {
432         if (!FCHMODAT_DIR_FD_CONVERTER(args[2], &dir_fd)) {
433             goto exit;
434         }
435         if (!--noptargs) {
436             goto skip_optional_kwonly;
437         }
438     }
439     follow_symlinks = PyObject_IsTrue(args[3]);
440     if (follow_symlinks < 0) {
441         goto exit;
442     }
443 skip_optional_kwonly:
444     return_value = os_chmod_impl(module, &path, mode, dir_fd, follow_symlinks);
445 
446 exit:
447     /* Cleanup for path */
448     path_cleanup(&path);
449 
450     return return_value;
451 }
452 
453 #if defined(HAVE_FCHMOD)
454 
455 PyDoc_STRVAR(os_fchmod__doc__,
456 "fchmod($module, /, fd, mode)\n"
457 "--\n"
458 "\n"
459 "Change the access permissions of the file given by file descriptor fd.\n"
460 "\n"
461 "Equivalent to os.chmod(fd, mode).");
462 
463 #define OS_FCHMOD_METHODDEF    \
464     {"fchmod", (PyCFunction)(void(*)(void))os_fchmod, METH_FASTCALL|METH_KEYWORDS, os_fchmod__doc__},
465 
466 static PyObject *
467 os_fchmod_impl(PyObject *module, int fd, int mode);
468 
469 static PyObject *
os_fchmod(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)470 os_fchmod(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
471 {
472     PyObject *return_value = NULL;
473     static const char * const _keywords[] = {"fd", "mode", NULL};
474     static _PyArg_Parser _parser = {NULL, _keywords, "fchmod", 0};
475     PyObject *argsbuf[2];
476     int fd;
477     int mode;
478 
479     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf);
480     if (!args) {
481         goto exit;
482     }
483     fd = _PyLong_AsInt(args[0]);
484     if (fd == -1 && PyErr_Occurred()) {
485         goto exit;
486     }
487     mode = _PyLong_AsInt(args[1]);
488     if (mode == -1 && PyErr_Occurred()) {
489         goto exit;
490     }
491     return_value = os_fchmod_impl(module, fd, mode);
492 
493 exit:
494     return return_value;
495 }
496 
497 #endif /* defined(HAVE_FCHMOD) */
498 
499 #if defined(HAVE_LCHMOD)
500 
501 PyDoc_STRVAR(os_lchmod__doc__,
502 "lchmod($module, /, path, mode)\n"
503 "--\n"
504 "\n"
505 "Change the access permissions of a file, without following symbolic links.\n"
506 "\n"
507 "If path is a symlink, this affects the link itself rather than the target.\n"
508 "Equivalent to chmod(path, mode, follow_symlinks=False).\"");
509 
510 #define OS_LCHMOD_METHODDEF    \
511     {"lchmod", (PyCFunction)(void(*)(void))os_lchmod, METH_FASTCALL|METH_KEYWORDS, os_lchmod__doc__},
512 
513 static PyObject *
514 os_lchmod_impl(PyObject *module, path_t *path, int mode);
515 
516 static PyObject *
os_lchmod(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)517 os_lchmod(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
518 {
519     PyObject *return_value = NULL;
520     static const char * const _keywords[] = {"path", "mode", NULL};
521     static _PyArg_Parser _parser = {NULL, _keywords, "lchmod", 0};
522     PyObject *argsbuf[2];
523     path_t path = PATH_T_INITIALIZE("lchmod", "path", 0, 0);
524     int mode;
525 
526     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf);
527     if (!args) {
528         goto exit;
529     }
530     if (!path_converter(args[0], &path)) {
531         goto exit;
532     }
533     mode = _PyLong_AsInt(args[1]);
534     if (mode == -1 && PyErr_Occurred()) {
535         goto exit;
536     }
537     return_value = os_lchmod_impl(module, &path, mode);
538 
539 exit:
540     /* Cleanup for path */
541     path_cleanup(&path);
542 
543     return return_value;
544 }
545 
546 #endif /* defined(HAVE_LCHMOD) */
547 
548 #if defined(HAVE_CHFLAGS)
549 
550 PyDoc_STRVAR(os_chflags__doc__,
551 "chflags($module, /, path, flags, follow_symlinks=True)\n"
552 "--\n"
553 "\n"
554 "Set file flags.\n"
555 "\n"
556 "If follow_symlinks is False, and the last element of the path is a symbolic\n"
557 "  link, chflags will change flags on the symbolic link itself instead of the\n"
558 "  file the link points to.\n"
559 "follow_symlinks may not be implemented on your platform.  If it is\n"
560 "unavailable, using it will raise a NotImplementedError.");
561 
562 #define OS_CHFLAGS_METHODDEF    \
563     {"chflags", (PyCFunction)(void(*)(void))os_chflags, METH_FASTCALL|METH_KEYWORDS, os_chflags__doc__},
564 
565 static PyObject *
566 os_chflags_impl(PyObject *module, path_t *path, unsigned long flags,
567                 int follow_symlinks);
568 
569 static PyObject *
os_chflags(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)570 os_chflags(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
571 {
572     PyObject *return_value = NULL;
573     static const char * const _keywords[] = {"path", "flags", "follow_symlinks", NULL};
574     static _PyArg_Parser _parser = {NULL, _keywords, "chflags", 0};
575     PyObject *argsbuf[3];
576     Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
577     path_t path = PATH_T_INITIALIZE("chflags", "path", 0, 0);
578     unsigned long flags;
579     int follow_symlinks = 1;
580 
581     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 3, 0, argsbuf);
582     if (!args) {
583         goto exit;
584     }
585     if (!path_converter(args[0], &path)) {
586         goto exit;
587     }
588     if (!PyLong_Check(args[1])) {
589         _PyArg_BadArgument("chflags", "argument 'flags'", "int", args[1]);
590         goto exit;
591     }
592     flags = PyLong_AsUnsignedLongMask(args[1]);
593     if (!noptargs) {
594         goto skip_optional_pos;
595     }
596     follow_symlinks = PyObject_IsTrue(args[2]);
597     if (follow_symlinks < 0) {
598         goto exit;
599     }
600 skip_optional_pos:
601     return_value = os_chflags_impl(module, &path, flags, follow_symlinks);
602 
603 exit:
604     /* Cleanup for path */
605     path_cleanup(&path);
606 
607     return return_value;
608 }
609 
610 #endif /* defined(HAVE_CHFLAGS) */
611 
612 #if defined(HAVE_LCHFLAGS)
613 
614 PyDoc_STRVAR(os_lchflags__doc__,
615 "lchflags($module, /, path, flags)\n"
616 "--\n"
617 "\n"
618 "Set file flags.\n"
619 "\n"
620 "This function will not follow symbolic links.\n"
621 "Equivalent to chflags(path, flags, follow_symlinks=False).");
622 
623 #define OS_LCHFLAGS_METHODDEF    \
624     {"lchflags", (PyCFunction)(void(*)(void))os_lchflags, METH_FASTCALL|METH_KEYWORDS, os_lchflags__doc__},
625 
626 static PyObject *
627 os_lchflags_impl(PyObject *module, path_t *path, unsigned long flags);
628 
629 static PyObject *
os_lchflags(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)630 os_lchflags(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
631 {
632     PyObject *return_value = NULL;
633     static const char * const _keywords[] = {"path", "flags", NULL};
634     static _PyArg_Parser _parser = {NULL, _keywords, "lchflags", 0};
635     PyObject *argsbuf[2];
636     path_t path = PATH_T_INITIALIZE("lchflags", "path", 0, 0);
637     unsigned long flags;
638 
639     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf);
640     if (!args) {
641         goto exit;
642     }
643     if (!path_converter(args[0], &path)) {
644         goto exit;
645     }
646     if (!PyLong_Check(args[1])) {
647         _PyArg_BadArgument("lchflags", "argument 'flags'", "int", args[1]);
648         goto exit;
649     }
650     flags = PyLong_AsUnsignedLongMask(args[1]);
651     return_value = os_lchflags_impl(module, &path, flags);
652 
653 exit:
654     /* Cleanup for path */
655     path_cleanup(&path);
656 
657     return return_value;
658 }
659 
660 #endif /* defined(HAVE_LCHFLAGS) */
661 
662 #if defined(HAVE_CHROOT)
663 
664 PyDoc_STRVAR(os_chroot__doc__,
665 "chroot($module, /, path)\n"
666 "--\n"
667 "\n"
668 "Change root directory to path.");
669 
670 #define OS_CHROOT_METHODDEF    \
671     {"chroot", (PyCFunction)(void(*)(void))os_chroot, METH_FASTCALL|METH_KEYWORDS, os_chroot__doc__},
672 
673 static PyObject *
674 os_chroot_impl(PyObject *module, path_t *path);
675 
676 static PyObject *
os_chroot(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)677 os_chroot(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
678 {
679     PyObject *return_value = NULL;
680     static const char * const _keywords[] = {"path", NULL};
681     static _PyArg_Parser _parser = {NULL, _keywords, "chroot", 0};
682     PyObject *argsbuf[1];
683     path_t path = PATH_T_INITIALIZE("chroot", "path", 0, 0);
684 
685     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
686     if (!args) {
687         goto exit;
688     }
689     if (!path_converter(args[0], &path)) {
690         goto exit;
691     }
692     return_value = os_chroot_impl(module, &path);
693 
694 exit:
695     /* Cleanup for path */
696     path_cleanup(&path);
697 
698     return return_value;
699 }
700 
701 #endif /* defined(HAVE_CHROOT) */
702 
703 #if defined(HAVE_FSYNC)
704 
705 PyDoc_STRVAR(os_fsync__doc__,
706 "fsync($module, /, fd)\n"
707 "--\n"
708 "\n"
709 "Force write of fd to disk.");
710 
711 #define OS_FSYNC_METHODDEF    \
712     {"fsync", (PyCFunction)(void(*)(void))os_fsync, METH_FASTCALL|METH_KEYWORDS, os_fsync__doc__},
713 
714 static PyObject *
715 os_fsync_impl(PyObject *module, int fd);
716 
717 static PyObject *
os_fsync(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)718 os_fsync(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
719 {
720     PyObject *return_value = NULL;
721     static const char * const _keywords[] = {"fd", NULL};
722     static _PyArg_Parser _parser = {NULL, _keywords, "fsync", 0};
723     PyObject *argsbuf[1];
724     int fd;
725 
726     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
727     if (!args) {
728         goto exit;
729     }
730     if (!_PyLong_FileDescriptor_Converter(args[0], &fd)) {
731         goto exit;
732     }
733     return_value = os_fsync_impl(module, fd);
734 
735 exit:
736     return return_value;
737 }
738 
739 #endif /* defined(HAVE_FSYNC) */
740 
741 #if defined(HAVE_SYNC)
742 
743 PyDoc_STRVAR(os_sync__doc__,
744 "sync($module, /)\n"
745 "--\n"
746 "\n"
747 "Force write of everything to disk.");
748 
749 #define OS_SYNC_METHODDEF    \
750     {"sync", (PyCFunction)os_sync, METH_NOARGS, os_sync__doc__},
751 
752 static PyObject *
753 os_sync_impl(PyObject *module);
754 
755 static PyObject *
os_sync(PyObject * module,PyObject * Py_UNUSED (ignored))756 os_sync(PyObject *module, PyObject *Py_UNUSED(ignored))
757 {
758     return os_sync_impl(module);
759 }
760 
761 #endif /* defined(HAVE_SYNC) */
762 
763 #if defined(HAVE_FDATASYNC)
764 
765 PyDoc_STRVAR(os_fdatasync__doc__,
766 "fdatasync($module, /, fd)\n"
767 "--\n"
768 "\n"
769 "Force write of fd to disk without forcing update of metadata.");
770 
771 #define OS_FDATASYNC_METHODDEF    \
772     {"fdatasync", (PyCFunction)(void(*)(void))os_fdatasync, METH_FASTCALL|METH_KEYWORDS, os_fdatasync__doc__},
773 
774 static PyObject *
775 os_fdatasync_impl(PyObject *module, int fd);
776 
777 static PyObject *
os_fdatasync(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)778 os_fdatasync(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
779 {
780     PyObject *return_value = NULL;
781     static const char * const _keywords[] = {"fd", NULL};
782     static _PyArg_Parser _parser = {NULL, _keywords, "fdatasync", 0};
783     PyObject *argsbuf[1];
784     int fd;
785 
786     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
787     if (!args) {
788         goto exit;
789     }
790     if (!_PyLong_FileDescriptor_Converter(args[0], &fd)) {
791         goto exit;
792     }
793     return_value = os_fdatasync_impl(module, fd);
794 
795 exit:
796     return return_value;
797 }
798 
799 #endif /* defined(HAVE_FDATASYNC) */
800 
801 #if defined(HAVE_CHOWN)
802 
803 PyDoc_STRVAR(os_chown__doc__,
804 "chown($module, /, path, uid, gid, *, dir_fd=None, follow_symlinks=True)\n"
805 "--\n"
806 "\n"
807 "Change the owner and group id of path to the numeric uid and gid.\\\n"
808 "\n"
809 "  path\n"
810 "    Path to be examined; can be string, bytes, a path-like object, or open-file-descriptor int.\n"
811 "  dir_fd\n"
812 "    If not None, it should be a file descriptor open to a directory,\n"
813 "    and path should be relative; path will then be relative to that\n"
814 "    directory.\n"
815 "  follow_symlinks\n"
816 "    If False, and the last element of the path is a symbolic link,\n"
817 "    stat will examine the symbolic link itself instead of the file\n"
818 "    the link points to.\n"
819 "\n"
820 "path may always be specified as a string.\n"
821 "On some platforms, path may also be specified as an open file descriptor.\n"
822 "  If this functionality is unavailable, using it raises an exception.\n"
823 "If dir_fd is not None, it should be a file descriptor open to a directory,\n"
824 "  and path should be relative; path will then be relative to that directory.\n"
825 "If follow_symlinks is False, and the last element of the path is a symbolic\n"
826 "  link, chown will modify the symbolic link itself instead of the file the\n"
827 "  link points to.\n"
828 "It is an error to use dir_fd or follow_symlinks when specifying path as\n"
829 "  an open file descriptor.\n"
830 "dir_fd and follow_symlinks may not be implemented on your platform.\n"
831 "  If they are unavailable, using them will raise a NotImplementedError.");
832 
833 #define OS_CHOWN_METHODDEF    \
834     {"chown", (PyCFunction)(void(*)(void))os_chown, METH_FASTCALL|METH_KEYWORDS, os_chown__doc__},
835 
836 static PyObject *
837 os_chown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid,
838               int dir_fd, int follow_symlinks);
839 
840 static PyObject *
os_chown(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)841 os_chown(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
842 {
843     PyObject *return_value = NULL;
844     static const char * const _keywords[] = {"path", "uid", "gid", "dir_fd", "follow_symlinks", NULL};
845     static _PyArg_Parser _parser = {NULL, _keywords, "chown", 0};
846     PyObject *argsbuf[5];
847     Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 3;
848     path_t path = PATH_T_INITIALIZE("chown", "path", 0, PATH_HAVE_FCHOWN);
849     uid_t uid;
850     gid_t gid;
851     int dir_fd = DEFAULT_DIR_FD;
852     int follow_symlinks = 1;
853 
854     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 3, 3, 0, argsbuf);
855     if (!args) {
856         goto exit;
857     }
858     if (!path_converter(args[0], &path)) {
859         goto exit;
860     }
861     if (!_Py_Uid_Converter(args[1], &uid)) {
862         goto exit;
863     }
864     if (!_Py_Gid_Converter(args[2], &gid)) {
865         goto exit;
866     }
867     if (!noptargs) {
868         goto skip_optional_kwonly;
869     }
870     if (args[3]) {
871         if (!FCHOWNAT_DIR_FD_CONVERTER(args[3], &dir_fd)) {
872             goto exit;
873         }
874         if (!--noptargs) {
875             goto skip_optional_kwonly;
876         }
877     }
878     follow_symlinks = PyObject_IsTrue(args[4]);
879     if (follow_symlinks < 0) {
880         goto exit;
881     }
882 skip_optional_kwonly:
883     return_value = os_chown_impl(module, &path, uid, gid, dir_fd, follow_symlinks);
884 
885 exit:
886     /* Cleanup for path */
887     path_cleanup(&path);
888 
889     return return_value;
890 }
891 
892 #endif /* defined(HAVE_CHOWN) */
893 
894 #if defined(HAVE_FCHOWN)
895 
896 PyDoc_STRVAR(os_fchown__doc__,
897 "fchown($module, /, fd, uid, gid)\n"
898 "--\n"
899 "\n"
900 "Change the owner and group id of the file specified by file descriptor.\n"
901 "\n"
902 "Equivalent to os.chown(fd, uid, gid).");
903 
904 #define OS_FCHOWN_METHODDEF    \
905     {"fchown", (PyCFunction)(void(*)(void))os_fchown, METH_FASTCALL|METH_KEYWORDS, os_fchown__doc__},
906 
907 static PyObject *
908 os_fchown_impl(PyObject *module, int fd, uid_t uid, gid_t gid);
909 
910 static PyObject *
os_fchown(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)911 os_fchown(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
912 {
913     PyObject *return_value = NULL;
914     static const char * const _keywords[] = {"fd", "uid", "gid", NULL};
915     static _PyArg_Parser _parser = {NULL, _keywords, "fchown", 0};
916     PyObject *argsbuf[3];
917     int fd;
918     uid_t uid;
919     gid_t gid;
920 
921     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 3, 3, 0, argsbuf);
922     if (!args) {
923         goto exit;
924     }
925     fd = _PyLong_AsInt(args[0]);
926     if (fd == -1 && PyErr_Occurred()) {
927         goto exit;
928     }
929     if (!_Py_Uid_Converter(args[1], &uid)) {
930         goto exit;
931     }
932     if (!_Py_Gid_Converter(args[2], &gid)) {
933         goto exit;
934     }
935     return_value = os_fchown_impl(module, fd, uid, gid);
936 
937 exit:
938     return return_value;
939 }
940 
941 #endif /* defined(HAVE_FCHOWN) */
942 
943 #if defined(HAVE_LCHOWN)
944 
945 PyDoc_STRVAR(os_lchown__doc__,
946 "lchown($module, /, path, uid, gid)\n"
947 "--\n"
948 "\n"
949 "Change the owner and group id of path to the numeric uid and gid.\n"
950 "\n"
951 "This function will not follow symbolic links.\n"
952 "Equivalent to os.chown(path, uid, gid, follow_symlinks=False).");
953 
954 #define OS_LCHOWN_METHODDEF    \
955     {"lchown", (PyCFunction)(void(*)(void))os_lchown, METH_FASTCALL|METH_KEYWORDS, os_lchown__doc__},
956 
957 static PyObject *
958 os_lchown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid);
959 
960 static PyObject *
os_lchown(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)961 os_lchown(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
962 {
963     PyObject *return_value = NULL;
964     static const char * const _keywords[] = {"path", "uid", "gid", NULL};
965     static _PyArg_Parser _parser = {NULL, _keywords, "lchown", 0};
966     PyObject *argsbuf[3];
967     path_t path = PATH_T_INITIALIZE("lchown", "path", 0, 0);
968     uid_t uid;
969     gid_t gid;
970 
971     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 3, 3, 0, argsbuf);
972     if (!args) {
973         goto exit;
974     }
975     if (!path_converter(args[0], &path)) {
976         goto exit;
977     }
978     if (!_Py_Uid_Converter(args[1], &uid)) {
979         goto exit;
980     }
981     if (!_Py_Gid_Converter(args[2], &gid)) {
982         goto exit;
983     }
984     return_value = os_lchown_impl(module, &path, uid, gid);
985 
986 exit:
987     /* Cleanup for path */
988     path_cleanup(&path);
989 
990     return return_value;
991 }
992 
993 #endif /* defined(HAVE_LCHOWN) */
994 
995 PyDoc_STRVAR(os_getcwd__doc__,
996 "getcwd($module, /)\n"
997 "--\n"
998 "\n"
999 "Return a unicode string representing the current working directory.");
1000 
1001 #define OS_GETCWD_METHODDEF    \
1002     {"getcwd", (PyCFunction)os_getcwd, METH_NOARGS, os_getcwd__doc__},
1003 
1004 static PyObject *
1005 os_getcwd_impl(PyObject *module);
1006 
1007 static PyObject *
os_getcwd(PyObject * module,PyObject * Py_UNUSED (ignored))1008 os_getcwd(PyObject *module, PyObject *Py_UNUSED(ignored))
1009 {
1010     return os_getcwd_impl(module);
1011 }
1012 
1013 PyDoc_STRVAR(os_getcwdb__doc__,
1014 "getcwdb($module, /)\n"
1015 "--\n"
1016 "\n"
1017 "Return a bytes string representing the current working directory.");
1018 
1019 #define OS_GETCWDB_METHODDEF    \
1020     {"getcwdb", (PyCFunction)os_getcwdb, METH_NOARGS, os_getcwdb__doc__},
1021 
1022 static PyObject *
1023 os_getcwdb_impl(PyObject *module);
1024 
1025 static PyObject *
os_getcwdb(PyObject * module,PyObject * Py_UNUSED (ignored))1026 os_getcwdb(PyObject *module, PyObject *Py_UNUSED(ignored))
1027 {
1028     return os_getcwdb_impl(module);
1029 }
1030 
1031 #if defined(HAVE_LINK)
1032 
1033 PyDoc_STRVAR(os_link__doc__,
1034 "link($module, /, src, dst, *, src_dir_fd=None, dst_dir_fd=None,\n"
1035 "     follow_symlinks=True)\n"
1036 "--\n"
1037 "\n"
1038 "Create a hard link to a file.\n"
1039 "\n"
1040 "If either src_dir_fd or dst_dir_fd is not None, it should be a file\n"
1041 "  descriptor open to a directory, and the respective path string (src or dst)\n"
1042 "  should be relative; the path will then be relative to that directory.\n"
1043 "If follow_symlinks is False, and the last element of src is a symbolic\n"
1044 "  link, link will create a link to the symbolic link itself instead of the\n"
1045 "  file the link points to.\n"
1046 "src_dir_fd, dst_dir_fd, and follow_symlinks may not be implemented on your\n"
1047 "  platform.  If they are unavailable, using them will raise a\n"
1048 "  NotImplementedError.");
1049 
1050 #define OS_LINK_METHODDEF    \
1051     {"link", (PyCFunction)(void(*)(void))os_link, METH_FASTCALL|METH_KEYWORDS, os_link__doc__},
1052 
1053 static PyObject *
1054 os_link_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
1055              int dst_dir_fd, int follow_symlinks);
1056 
1057 static PyObject *
os_link(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)1058 os_link(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
1059 {
1060     PyObject *return_value = NULL;
1061     static const char * const _keywords[] = {"src", "dst", "src_dir_fd", "dst_dir_fd", "follow_symlinks", NULL};
1062     static _PyArg_Parser _parser = {NULL, _keywords, "link", 0};
1063     PyObject *argsbuf[5];
1064     Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
1065     path_t src = PATH_T_INITIALIZE("link", "src", 0, 0);
1066     path_t dst = PATH_T_INITIALIZE("link", "dst", 0, 0);
1067     int src_dir_fd = DEFAULT_DIR_FD;
1068     int dst_dir_fd = DEFAULT_DIR_FD;
1069     int follow_symlinks = 1;
1070 
1071     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf);
1072     if (!args) {
1073         goto exit;
1074     }
1075     if (!path_converter(args[0], &src)) {
1076         goto exit;
1077     }
1078     if (!path_converter(args[1], &dst)) {
1079         goto exit;
1080     }
1081     if (!noptargs) {
1082         goto skip_optional_kwonly;
1083     }
1084     if (args[2]) {
1085         if (!dir_fd_converter(args[2], &src_dir_fd)) {
1086             goto exit;
1087         }
1088         if (!--noptargs) {
1089             goto skip_optional_kwonly;
1090         }
1091     }
1092     if (args[3]) {
1093         if (!dir_fd_converter(args[3], &dst_dir_fd)) {
1094             goto exit;
1095         }
1096         if (!--noptargs) {
1097             goto skip_optional_kwonly;
1098         }
1099     }
1100     follow_symlinks = PyObject_IsTrue(args[4]);
1101     if (follow_symlinks < 0) {
1102         goto exit;
1103     }
1104 skip_optional_kwonly:
1105     return_value = os_link_impl(module, &src, &dst, src_dir_fd, dst_dir_fd, follow_symlinks);
1106 
1107 exit:
1108     /* Cleanup for src */
1109     path_cleanup(&src);
1110     /* Cleanup for dst */
1111     path_cleanup(&dst);
1112 
1113     return return_value;
1114 }
1115 
1116 #endif /* defined(HAVE_LINK) */
1117 
1118 PyDoc_STRVAR(os_listdir__doc__,
1119 "listdir($module, /, path=None)\n"
1120 "--\n"
1121 "\n"
1122 "Return a list containing the names of the files in the directory.\n"
1123 "\n"
1124 "path can be specified as either str, bytes, or a path-like object.  If path is bytes,\n"
1125 "  the filenames returned will also be bytes; in all other circumstances\n"
1126 "  the filenames returned will be str.\n"
1127 "If path is None, uses the path=\'.\'.\n"
1128 "On some platforms, path may also be specified as an open file descriptor;\\\n"
1129 "  the file descriptor must refer to a directory.\n"
1130 "  If this functionality is unavailable, using it raises NotImplementedError.\n"
1131 "\n"
1132 "The list is in arbitrary order.  It does not include the special\n"
1133 "entries \'.\' and \'..\' even if they are present in the directory.");
1134 
1135 #define OS_LISTDIR_METHODDEF    \
1136     {"listdir", (PyCFunction)(void(*)(void))os_listdir, METH_FASTCALL|METH_KEYWORDS, os_listdir__doc__},
1137 
1138 static PyObject *
1139 os_listdir_impl(PyObject *module, path_t *path);
1140 
1141 static PyObject *
os_listdir(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)1142 os_listdir(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
1143 {
1144     PyObject *return_value = NULL;
1145     static const char * const _keywords[] = {"path", NULL};
1146     static _PyArg_Parser _parser = {NULL, _keywords, "listdir", 0};
1147     PyObject *argsbuf[1];
1148     Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
1149     path_t path = PATH_T_INITIALIZE("listdir", "path", 1, PATH_HAVE_FDOPENDIR);
1150 
1151     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
1152     if (!args) {
1153         goto exit;
1154     }
1155     if (!noptargs) {
1156         goto skip_optional_pos;
1157     }
1158     if (!path_converter(args[0], &path)) {
1159         goto exit;
1160     }
1161 skip_optional_pos:
1162     return_value = os_listdir_impl(module, &path);
1163 
1164 exit:
1165     /* Cleanup for path */
1166     path_cleanup(&path);
1167 
1168     return return_value;
1169 }
1170 
1171 #if defined(MS_WINDOWS)
1172 
1173 PyDoc_STRVAR(os__getfullpathname__doc__,
1174 "_getfullpathname($module, path, /)\n"
1175 "--\n"
1176 "\n");
1177 
1178 #define OS__GETFULLPATHNAME_METHODDEF    \
1179     {"_getfullpathname", (PyCFunction)os__getfullpathname, METH_O, os__getfullpathname__doc__},
1180 
1181 static PyObject *
1182 os__getfullpathname_impl(PyObject *module, path_t *path);
1183 
1184 static PyObject *
os__getfullpathname(PyObject * module,PyObject * arg)1185 os__getfullpathname(PyObject *module, PyObject *arg)
1186 {
1187     PyObject *return_value = NULL;
1188     path_t path = PATH_T_INITIALIZE("_getfullpathname", "path", 0, 0);
1189 
1190     if (!path_converter(arg, &path)) {
1191         goto exit;
1192     }
1193     return_value = os__getfullpathname_impl(module, &path);
1194 
1195 exit:
1196     /* Cleanup for path */
1197     path_cleanup(&path);
1198 
1199     return return_value;
1200 }
1201 
1202 #endif /* defined(MS_WINDOWS) */
1203 
1204 #if defined(MS_WINDOWS)
1205 
1206 PyDoc_STRVAR(os__getfinalpathname__doc__,
1207 "_getfinalpathname($module, path, /)\n"
1208 "--\n"
1209 "\n"
1210 "A helper function for samepath on windows.");
1211 
1212 #define OS__GETFINALPATHNAME_METHODDEF    \
1213     {"_getfinalpathname", (PyCFunction)os__getfinalpathname, METH_O, os__getfinalpathname__doc__},
1214 
1215 static PyObject *
1216 os__getfinalpathname_impl(PyObject *module, path_t *path);
1217 
1218 static PyObject *
os__getfinalpathname(PyObject * module,PyObject * arg)1219 os__getfinalpathname(PyObject *module, PyObject *arg)
1220 {
1221     PyObject *return_value = NULL;
1222     path_t path = PATH_T_INITIALIZE("_getfinalpathname", "path", 0, 0);
1223 
1224     if (!path_converter(arg, &path)) {
1225         goto exit;
1226     }
1227     return_value = os__getfinalpathname_impl(module, &path);
1228 
1229 exit:
1230     /* Cleanup for path */
1231     path_cleanup(&path);
1232 
1233     return return_value;
1234 }
1235 
1236 #endif /* defined(MS_WINDOWS) */
1237 
1238 #if defined(MS_WINDOWS)
1239 
1240 PyDoc_STRVAR(os__getvolumepathname__doc__,
1241 "_getvolumepathname($module, /, path)\n"
1242 "--\n"
1243 "\n"
1244 "A helper function for ismount on Win32.");
1245 
1246 #define OS__GETVOLUMEPATHNAME_METHODDEF    \
1247     {"_getvolumepathname", (PyCFunction)(void(*)(void))os__getvolumepathname, METH_FASTCALL|METH_KEYWORDS, os__getvolumepathname__doc__},
1248 
1249 static PyObject *
1250 os__getvolumepathname_impl(PyObject *module, path_t *path);
1251 
1252 static PyObject *
os__getvolumepathname(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)1253 os__getvolumepathname(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
1254 {
1255     PyObject *return_value = NULL;
1256     static const char * const _keywords[] = {"path", NULL};
1257     static _PyArg_Parser _parser = {NULL, _keywords, "_getvolumepathname", 0};
1258     PyObject *argsbuf[1];
1259     path_t path = PATH_T_INITIALIZE("_getvolumepathname", "path", 0, 0);
1260 
1261     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
1262     if (!args) {
1263         goto exit;
1264     }
1265     if (!path_converter(args[0], &path)) {
1266         goto exit;
1267     }
1268     return_value = os__getvolumepathname_impl(module, &path);
1269 
1270 exit:
1271     /* Cleanup for path */
1272     path_cleanup(&path);
1273 
1274     return return_value;
1275 }
1276 
1277 #endif /* defined(MS_WINDOWS) */
1278 
1279 #if defined(MS_WINDOWS)
1280 
1281 PyDoc_STRVAR(os__path_splitroot__doc__,
1282 "_path_splitroot($module, /, path)\n"
1283 "--\n"
1284 "\n"
1285 "Removes everything after the root on Win32.");
1286 
1287 #define OS__PATH_SPLITROOT_METHODDEF    \
1288     {"_path_splitroot", (PyCFunction)(void(*)(void))os__path_splitroot, METH_FASTCALL|METH_KEYWORDS, os__path_splitroot__doc__},
1289 
1290 static PyObject *
1291 os__path_splitroot_impl(PyObject *module, path_t *path);
1292 
1293 static PyObject *
os__path_splitroot(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)1294 os__path_splitroot(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
1295 {
1296     PyObject *return_value = NULL;
1297     static const char * const _keywords[] = {"path", NULL};
1298     static _PyArg_Parser _parser = {NULL, _keywords, "_path_splitroot", 0};
1299     PyObject *argsbuf[1];
1300     path_t path = PATH_T_INITIALIZE("_path_splitroot", "path", 0, 0);
1301 
1302     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
1303     if (!args) {
1304         goto exit;
1305     }
1306     if (!path_converter(args[0], &path)) {
1307         goto exit;
1308     }
1309     return_value = os__path_splitroot_impl(module, &path);
1310 
1311 exit:
1312     /* Cleanup for path */
1313     path_cleanup(&path);
1314 
1315     return return_value;
1316 }
1317 
1318 #endif /* defined(MS_WINDOWS) */
1319 
1320 PyDoc_STRVAR(os__path_normpath__doc__,
1321 "_path_normpath($module, /, path)\n"
1322 "--\n"
1323 "\n"
1324 "Basic path normalization.");
1325 
1326 #define OS__PATH_NORMPATH_METHODDEF    \
1327     {"_path_normpath", (PyCFunction)(void(*)(void))os__path_normpath, METH_FASTCALL|METH_KEYWORDS, os__path_normpath__doc__},
1328 
1329 static PyObject *
1330 os__path_normpath_impl(PyObject *module, PyObject *path);
1331 
1332 static PyObject *
os__path_normpath(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)1333 os__path_normpath(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
1334 {
1335     PyObject *return_value = NULL;
1336     static const char * const _keywords[] = {"path", NULL};
1337     static _PyArg_Parser _parser = {NULL, _keywords, "_path_normpath", 0};
1338     PyObject *argsbuf[1];
1339     PyObject *path;
1340 
1341     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
1342     if (!args) {
1343         goto exit;
1344     }
1345     path = args[0];
1346     return_value = os__path_normpath_impl(module, path);
1347 
1348 exit:
1349     return return_value;
1350 }
1351 
1352 PyDoc_STRVAR(os_mkdir__doc__,
1353 "mkdir($module, /, path, mode=511, *, dir_fd=None)\n"
1354 "--\n"
1355 "\n"
1356 "Create a directory.\n"
1357 "\n"
1358 "If dir_fd is not None, it should be a file descriptor open to a directory,\n"
1359 "  and path should be relative; path will then be relative to that directory.\n"
1360 "dir_fd may not be implemented on your platform.\n"
1361 "  If it is unavailable, using it will raise a NotImplementedError.\n"
1362 "\n"
1363 "The mode argument is ignored on Windows.");
1364 
1365 #define OS_MKDIR_METHODDEF    \
1366     {"mkdir", (PyCFunction)(void(*)(void))os_mkdir, METH_FASTCALL|METH_KEYWORDS, os_mkdir__doc__},
1367 
1368 static PyObject *
1369 os_mkdir_impl(PyObject *module, path_t *path, int mode, int dir_fd);
1370 
1371 static PyObject *
os_mkdir(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)1372 os_mkdir(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
1373 {
1374     PyObject *return_value = NULL;
1375     static const char * const _keywords[] = {"path", "mode", "dir_fd", NULL};
1376     static _PyArg_Parser _parser = {NULL, _keywords, "mkdir", 0};
1377     PyObject *argsbuf[3];
1378     Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
1379     path_t path = PATH_T_INITIALIZE("mkdir", "path", 0, 0);
1380     int mode = 511;
1381     int dir_fd = DEFAULT_DIR_FD;
1382 
1383     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
1384     if (!args) {
1385         goto exit;
1386     }
1387     if (!path_converter(args[0], &path)) {
1388         goto exit;
1389     }
1390     if (!noptargs) {
1391         goto skip_optional_pos;
1392     }
1393     if (args[1]) {
1394         mode = _PyLong_AsInt(args[1]);
1395         if (mode == -1 && PyErr_Occurred()) {
1396             goto exit;
1397         }
1398         if (!--noptargs) {
1399             goto skip_optional_pos;
1400         }
1401     }
1402 skip_optional_pos:
1403     if (!noptargs) {
1404         goto skip_optional_kwonly;
1405     }
1406     if (!MKDIRAT_DIR_FD_CONVERTER(args[2], &dir_fd)) {
1407         goto exit;
1408     }
1409 skip_optional_kwonly:
1410     return_value = os_mkdir_impl(module, &path, mode, dir_fd);
1411 
1412 exit:
1413     /* Cleanup for path */
1414     path_cleanup(&path);
1415 
1416     return return_value;
1417 }
1418 
1419 #if defined(HAVE_NICE)
1420 
1421 PyDoc_STRVAR(os_nice__doc__,
1422 "nice($module, increment, /)\n"
1423 "--\n"
1424 "\n"
1425 "Add increment to the priority of process and return the new priority.");
1426 
1427 #define OS_NICE_METHODDEF    \
1428     {"nice", (PyCFunction)os_nice, METH_O, os_nice__doc__},
1429 
1430 static PyObject *
1431 os_nice_impl(PyObject *module, int increment);
1432 
1433 static PyObject *
os_nice(PyObject * module,PyObject * arg)1434 os_nice(PyObject *module, PyObject *arg)
1435 {
1436     PyObject *return_value = NULL;
1437     int increment;
1438 
1439     increment = _PyLong_AsInt(arg);
1440     if (increment == -1 && PyErr_Occurred()) {
1441         goto exit;
1442     }
1443     return_value = os_nice_impl(module, increment);
1444 
1445 exit:
1446     return return_value;
1447 }
1448 
1449 #endif /* defined(HAVE_NICE) */
1450 
1451 #if defined(HAVE_GETPRIORITY)
1452 
1453 PyDoc_STRVAR(os_getpriority__doc__,
1454 "getpriority($module, /, which, who)\n"
1455 "--\n"
1456 "\n"
1457 "Return program scheduling priority.");
1458 
1459 #define OS_GETPRIORITY_METHODDEF    \
1460     {"getpriority", (PyCFunction)(void(*)(void))os_getpriority, METH_FASTCALL|METH_KEYWORDS, os_getpriority__doc__},
1461 
1462 static PyObject *
1463 os_getpriority_impl(PyObject *module, int which, int who);
1464 
1465 static PyObject *
os_getpriority(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)1466 os_getpriority(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
1467 {
1468     PyObject *return_value = NULL;
1469     static const char * const _keywords[] = {"which", "who", NULL};
1470     static _PyArg_Parser _parser = {NULL, _keywords, "getpriority", 0};
1471     PyObject *argsbuf[2];
1472     int which;
1473     int who;
1474 
1475     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf);
1476     if (!args) {
1477         goto exit;
1478     }
1479     which = _PyLong_AsInt(args[0]);
1480     if (which == -1 && PyErr_Occurred()) {
1481         goto exit;
1482     }
1483     who = _PyLong_AsInt(args[1]);
1484     if (who == -1 && PyErr_Occurred()) {
1485         goto exit;
1486     }
1487     return_value = os_getpriority_impl(module, which, who);
1488 
1489 exit:
1490     return return_value;
1491 }
1492 
1493 #endif /* defined(HAVE_GETPRIORITY) */
1494 
1495 #if defined(HAVE_SETPRIORITY)
1496 
1497 PyDoc_STRVAR(os_setpriority__doc__,
1498 "setpriority($module, /, which, who, priority)\n"
1499 "--\n"
1500 "\n"
1501 "Set program scheduling priority.");
1502 
1503 #define OS_SETPRIORITY_METHODDEF    \
1504     {"setpriority", (PyCFunction)(void(*)(void))os_setpriority, METH_FASTCALL|METH_KEYWORDS, os_setpriority__doc__},
1505 
1506 static PyObject *
1507 os_setpriority_impl(PyObject *module, int which, int who, int priority);
1508 
1509 static PyObject *
os_setpriority(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)1510 os_setpriority(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
1511 {
1512     PyObject *return_value = NULL;
1513     static const char * const _keywords[] = {"which", "who", "priority", NULL};
1514     static _PyArg_Parser _parser = {NULL, _keywords, "setpriority", 0};
1515     PyObject *argsbuf[3];
1516     int which;
1517     int who;
1518     int priority;
1519 
1520     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 3, 3, 0, argsbuf);
1521     if (!args) {
1522         goto exit;
1523     }
1524     which = _PyLong_AsInt(args[0]);
1525     if (which == -1 && PyErr_Occurred()) {
1526         goto exit;
1527     }
1528     who = _PyLong_AsInt(args[1]);
1529     if (who == -1 && PyErr_Occurred()) {
1530         goto exit;
1531     }
1532     priority = _PyLong_AsInt(args[2]);
1533     if (priority == -1 && PyErr_Occurred()) {
1534         goto exit;
1535     }
1536     return_value = os_setpriority_impl(module, which, who, priority);
1537 
1538 exit:
1539     return return_value;
1540 }
1541 
1542 #endif /* defined(HAVE_SETPRIORITY) */
1543 
1544 PyDoc_STRVAR(os_rename__doc__,
1545 "rename($module, /, src, dst, *, src_dir_fd=None, dst_dir_fd=None)\n"
1546 "--\n"
1547 "\n"
1548 "Rename a file or directory.\n"
1549 "\n"
1550 "If either src_dir_fd or dst_dir_fd is not None, it should be a file\n"
1551 "  descriptor open to a directory, and the respective path string (src or dst)\n"
1552 "  should be relative; the path will then be relative to that directory.\n"
1553 "src_dir_fd and dst_dir_fd, may not be implemented on your platform.\n"
1554 "  If they are unavailable, using them will raise a NotImplementedError.");
1555 
1556 #define OS_RENAME_METHODDEF    \
1557     {"rename", (PyCFunction)(void(*)(void))os_rename, METH_FASTCALL|METH_KEYWORDS, os_rename__doc__},
1558 
1559 static PyObject *
1560 os_rename_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
1561                int dst_dir_fd);
1562 
1563 static PyObject *
os_rename(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)1564 os_rename(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
1565 {
1566     PyObject *return_value = NULL;
1567     static const char * const _keywords[] = {"src", "dst", "src_dir_fd", "dst_dir_fd", NULL};
1568     static _PyArg_Parser _parser = {NULL, _keywords, "rename", 0};
1569     PyObject *argsbuf[4];
1570     Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
1571     path_t src = PATH_T_INITIALIZE("rename", "src", 0, 0);
1572     path_t dst = PATH_T_INITIALIZE("rename", "dst", 0, 0);
1573     int src_dir_fd = DEFAULT_DIR_FD;
1574     int dst_dir_fd = DEFAULT_DIR_FD;
1575 
1576     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf);
1577     if (!args) {
1578         goto exit;
1579     }
1580     if (!path_converter(args[0], &src)) {
1581         goto exit;
1582     }
1583     if (!path_converter(args[1], &dst)) {
1584         goto exit;
1585     }
1586     if (!noptargs) {
1587         goto skip_optional_kwonly;
1588     }
1589     if (args[2]) {
1590         if (!dir_fd_converter(args[2], &src_dir_fd)) {
1591             goto exit;
1592         }
1593         if (!--noptargs) {
1594             goto skip_optional_kwonly;
1595         }
1596     }
1597     if (!dir_fd_converter(args[3], &dst_dir_fd)) {
1598         goto exit;
1599     }
1600 skip_optional_kwonly:
1601     return_value = os_rename_impl(module, &src, &dst, src_dir_fd, dst_dir_fd);
1602 
1603 exit:
1604     /* Cleanup for src */
1605     path_cleanup(&src);
1606     /* Cleanup for dst */
1607     path_cleanup(&dst);
1608 
1609     return return_value;
1610 }
1611 
1612 PyDoc_STRVAR(os_replace__doc__,
1613 "replace($module, /, src, dst, *, src_dir_fd=None, dst_dir_fd=None)\n"
1614 "--\n"
1615 "\n"
1616 "Rename a file or directory, overwriting the destination.\n"
1617 "\n"
1618 "If either src_dir_fd or dst_dir_fd is not None, it should be a file\n"
1619 "  descriptor open to a directory, and the respective path string (src or dst)\n"
1620 "  should be relative; the path will then be relative to that directory.\n"
1621 "src_dir_fd and dst_dir_fd, may not be implemented on your platform.\n"
1622 "  If they are unavailable, using them will raise a NotImplementedError.");
1623 
1624 #define OS_REPLACE_METHODDEF    \
1625     {"replace", (PyCFunction)(void(*)(void))os_replace, METH_FASTCALL|METH_KEYWORDS, os_replace__doc__},
1626 
1627 static PyObject *
1628 os_replace_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
1629                 int dst_dir_fd);
1630 
1631 static PyObject *
os_replace(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)1632 os_replace(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
1633 {
1634     PyObject *return_value = NULL;
1635     static const char * const _keywords[] = {"src", "dst", "src_dir_fd", "dst_dir_fd", NULL};
1636     static _PyArg_Parser _parser = {NULL, _keywords, "replace", 0};
1637     PyObject *argsbuf[4];
1638     Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
1639     path_t src = PATH_T_INITIALIZE("replace", "src", 0, 0);
1640     path_t dst = PATH_T_INITIALIZE("replace", "dst", 0, 0);
1641     int src_dir_fd = DEFAULT_DIR_FD;
1642     int dst_dir_fd = DEFAULT_DIR_FD;
1643 
1644     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf);
1645     if (!args) {
1646         goto exit;
1647     }
1648     if (!path_converter(args[0], &src)) {
1649         goto exit;
1650     }
1651     if (!path_converter(args[1], &dst)) {
1652         goto exit;
1653     }
1654     if (!noptargs) {
1655         goto skip_optional_kwonly;
1656     }
1657     if (args[2]) {
1658         if (!dir_fd_converter(args[2], &src_dir_fd)) {
1659             goto exit;
1660         }
1661         if (!--noptargs) {
1662             goto skip_optional_kwonly;
1663         }
1664     }
1665     if (!dir_fd_converter(args[3], &dst_dir_fd)) {
1666         goto exit;
1667     }
1668 skip_optional_kwonly:
1669     return_value = os_replace_impl(module, &src, &dst, src_dir_fd, dst_dir_fd);
1670 
1671 exit:
1672     /* Cleanup for src */
1673     path_cleanup(&src);
1674     /* Cleanup for dst */
1675     path_cleanup(&dst);
1676 
1677     return return_value;
1678 }
1679 
1680 PyDoc_STRVAR(os_rmdir__doc__,
1681 "rmdir($module, /, path, *, dir_fd=None)\n"
1682 "--\n"
1683 "\n"
1684 "Remove a directory.\n"
1685 "\n"
1686 "If dir_fd is not None, it should be a file descriptor open to a directory,\n"
1687 "  and path should be relative; path will then be relative to that directory.\n"
1688 "dir_fd may not be implemented on your platform.\n"
1689 "  If it is unavailable, using it will raise a NotImplementedError.");
1690 
1691 #define OS_RMDIR_METHODDEF    \
1692     {"rmdir", (PyCFunction)(void(*)(void))os_rmdir, METH_FASTCALL|METH_KEYWORDS, os_rmdir__doc__},
1693 
1694 static PyObject *
1695 os_rmdir_impl(PyObject *module, path_t *path, int dir_fd);
1696 
1697 static PyObject *
os_rmdir(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)1698 os_rmdir(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
1699 {
1700     PyObject *return_value = NULL;
1701     static const char * const _keywords[] = {"path", "dir_fd", NULL};
1702     static _PyArg_Parser _parser = {NULL, _keywords, "rmdir", 0};
1703     PyObject *argsbuf[2];
1704     Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
1705     path_t path = PATH_T_INITIALIZE("rmdir", "path", 0, 0);
1706     int dir_fd = DEFAULT_DIR_FD;
1707 
1708     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
1709     if (!args) {
1710         goto exit;
1711     }
1712     if (!path_converter(args[0], &path)) {
1713         goto exit;
1714     }
1715     if (!noptargs) {
1716         goto skip_optional_kwonly;
1717     }
1718     if (!UNLINKAT_DIR_FD_CONVERTER(args[1], &dir_fd)) {
1719         goto exit;
1720     }
1721 skip_optional_kwonly:
1722     return_value = os_rmdir_impl(module, &path, dir_fd);
1723 
1724 exit:
1725     /* Cleanup for path */
1726     path_cleanup(&path);
1727 
1728     return return_value;
1729 }
1730 
1731 #if defined(HAVE_SYSTEM) && defined(MS_WINDOWS)
1732 
1733 PyDoc_STRVAR(os_system__doc__,
1734 "system($module, /, command)\n"
1735 "--\n"
1736 "\n"
1737 "Execute the command in a subshell.");
1738 
1739 #define OS_SYSTEM_METHODDEF    \
1740     {"system", (PyCFunction)(void(*)(void))os_system, METH_FASTCALL|METH_KEYWORDS, os_system__doc__},
1741 
1742 static long
1743 os_system_impl(PyObject *module, const Py_UNICODE *command);
1744 
1745 static PyObject *
os_system(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)1746 os_system(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
1747 {
1748     PyObject *return_value = NULL;
1749     static const char * const _keywords[] = {"command", NULL};
1750     static _PyArg_Parser _parser = {NULL, _keywords, "system", 0};
1751     PyObject *argsbuf[1];
1752     const Py_UNICODE *command;
1753     long _return_value;
1754 
1755     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
1756     if (!args) {
1757         goto exit;
1758     }
1759     if (!PyUnicode_Check(args[0])) {
1760         _PyArg_BadArgument("system", "argument 'command'", "str", args[0]);
1761         goto exit;
1762     }
1763     #if USE_UNICODE_WCHAR_CACHE
1764     command = _PyUnicode_AsUnicode(args[0]);
1765     #else /* USE_UNICODE_WCHAR_CACHE */
1766     command = PyUnicode_AsWideCharString(args[0], NULL);
1767     #endif /* USE_UNICODE_WCHAR_CACHE */
1768     if (command == NULL) {
1769         goto exit;
1770     }
1771     _return_value = os_system_impl(module, command);
1772     if ((_return_value == -1) && PyErr_Occurred()) {
1773         goto exit;
1774     }
1775     return_value = PyLong_FromLong(_return_value);
1776 
1777 exit:
1778     /* Cleanup for command */
1779     #if !USE_UNICODE_WCHAR_CACHE
1780     PyMem_Free((void *)command);
1781     #endif /* USE_UNICODE_WCHAR_CACHE */
1782 
1783     return return_value;
1784 }
1785 
1786 #endif /* defined(HAVE_SYSTEM) && defined(MS_WINDOWS) */
1787 
1788 #if defined(HAVE_SYSTEM) && !defined(MS_WINDOWS)
1789 
1790 PyDoc_STRVAR(os_system__doc__,
1791 "system($module, /, command)\n"
1792 "--\n"
1793 "\n"
1794 "Execute the command in a subshell.");
1795 
1796 #define OS_SYSTEM_METHODDEF    \
1797     {"system", (PyCFunction)(void(*)(void))os_system, METH_FASTCALL|METH_KEYWORDS, os_system__doc__},
1798 
1799 static long
1800 os_system_impl(PyObject *module, PyObject *command);
1801 
1802 static PyObject *
os_system(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)1803 os_system(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
1804 {
1805     PyObject *return_value = NULL;
1806     static const char * const _keywords[] = {"command", NULL};
1807     static _PyArg_Parser _parser = {NULL, _keywords, "system", 0};
1808     PyObject *argsbuf[1];
1809     PyObject *command = NULL;
1810     long _return_value;
1811 
1812     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
1813     if (!args) {
1814         goto exit;
1815     }
1816     if (!PyUnicode_FSConverter(args[0], &command)) {
1817         goto exit;
1818     }
1819     _return_value = os_system_impl(module, command);
1820     if ((_return_value == -1) && PyErr_Occurred()) {
1821         goto exit;
1822     }
1823     return_value = PyLong_FromLong(_return_value);
1824 
1825 exit:
1826     /* Cleanup for command */
1827     Py_XDECREF(command);
1828 
1829     return return_value;
1830 }
1831 
1832 #endif /* defined(HAVE_SYSTEM) && !defined(MS_WINDOWS) */
1833 
1834 PyDoc_STRVAR(os_umask__doc__,
1835 "umask($module, mask, /)\n"
1836 "--\n"
1837 "\n"
1838 "Set the current numeric umask and return the previous umask.");
1839 
1840 #define OS_UMASK_METHODDEF    \
1841     {"umask", (PyCFunction)os_umask, METH_O, os_umask__doc__},
1842 
1843 static PyObject *
1844 os_umask_impl(PyObject *module, int mask);
1845 
1846 static PyObject *
os_umask(PyObject * module,PyObject * arg)1847 os_umask(PyObject *module, PyObject *arg)
1848 {
1849     PyObject *return_value = NULL;
1850     int mask;
1851 
1852     mask = _PyLong_AsInt(arg);
1853     if (mask == -1 && PyErr_Occurred()) {
1854         goto exit;
1855     }
1856     return_value = os_umask_impl(module, mask);
1857 
1858 exit:
1859     return return_value;
1860 }
1861 
1862 PyDoc_STRVAR(os_unlink__doc__,
1863 "unlink($module, /, path, *, dir_fd=None)\n"
1864 "--\n"
1865 "\n"
1866 "Remove a file (same as remove()).\n"
1867 "\n"
1868 "If dir_fd is not None, it should be a file descriptor open to a directory,\n"
1869 "  and path should be relative; path will then be relative to that directory.\n"
1870 "dir_fd may not be implemented on your platform.\n"
1871 "  If it is unavailable, using it will raise a NotImplementedError.");
1872 
1873 #define OS_UNLINK_METHODDEF    \
1874     {"unlink", (PyCFunction)(void(*)(void))os_unlink, METH_FASTCALL|METH_KEYWORDS, os_unlink__doc__},
1875 
1876 static PyObject *
1877 os_unlink_impl(PyObject *module, path_t *path, int dir_fd);
1878 
1879 static PyObject *
os_unlink(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)1880 os_unlink(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
1881 {
1882     PyObject *return_value = NULL;
1883     static const char * const _keywords[] = {"path", "dir_fd", NULL};
1884     static _PyArg_Parser _parser = {NULL, _keywords, "unlink", 0};
1885     PyObject *argsbuf[2];
1886     Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
1887     path_t path = PATH_T_INITIALIZE("unlink", "path", 0, 0);
1888     int dir_fd = DEFAULT_DIR_FD;
1889 
1890     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
1891     if (!args) {
1892         goto exit;
1893     }
1894     if (!path_converter(args[0], &path)) {
1895         goto exit;
1896     }
1897     if (!noptargs) {
1898         goto skip_optional_kwonly;
1899     }
1900     if (!UNLINKAT_DIR_FD_CONVERTER(args[1], &dir_fd)) {
1901         goto exit;
1902     }
1903 skip_optional_kwonly:
1904     return_value = os_unlink_impl(module, &path, dir_fd);
1905 
1906 exit:
1907     /* Cleanup for path */
1908     path_cleanup(&path);
1909 
1910     return return_value;
1911 }
1912 
1913 PyDoc_STRVAR(os_remove__doc__,
1914 "remove($module, /, path, *, dir_fd=None)\n"
1915 "--\n"
1916 "\n"
1917 "Remove a file (same as unlink()).\n"
1918 "\n"
1919 "If dir_fd is not None, it should be a file descriptor open to a directory,\n"
1920 "  and path should be relative; path will then be relative to that directory.\n"
1921 "dir_fd may not be implemented on your platform.\n"
1922 "  If it is unavailable, using it will raise a NotImplementedError.");
1923 
1924 #define OS_REMOVE_METHODDEF    \
1925     {"remove", (PyCFunction)(void(*)(void))os_remove, METH_FASTCALL|METH_KEYWORDS, os_remove__doc__},
1926 
1927 static PyObject *
1928 os_remove_impl(PyObject *module, path_t *path, int dir_fd);
1929 
1930 static PyObject *
os_remove(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)1931 os_remove(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
1932 {
1933     PyObject *return_value = NULL;
1934     static const char * const _keywords[] = {"path", "dir_fd", NULL};
1935     static _PyArg_Parser _parser = {NULL, _keywords, "remove", 0};
1936     PyObject *argsbuf[2];
1937     Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
1938     path_t path = PATH_T_INITIALIZE("remove", "path", 0, 0);
1939     int dir_fd = DEFAULT_DIR_FD;
1940 
1941     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
1942     if (!args) {
1943         goto exit;
1944     }
1945     if (!path_converter(args[0], &path)) {
1946         goto exit;
1947     }
1948     if (!noptargs) {
1949         goto skip_optional_kwonly;
1950     }
1951     if (!UNLINKAT_DIR_FD_CONVERTER(args[1], &dir_fd)) {
1952         goto exit;
1953     }
1954 skip_optional_kwonly:
1955     return_value = os_remove_impl(module, &path, dir_fd);
1956 
1957 exit:
1958     /* Cleanup for path */
1959     path_cleanup(&path);
1960 
1961     return return_value;
1962 }
1963 
1964 #if defined(HAVE_UNAME)
1965 
1966 PyDoc_STRVAR(os_uname__doc__,
1967 "uname($module, /)\n"
1968 "--\n"
1969 "\n"
1970 "Return an object identifying the current operating system.\n"
1971 "\n"
1972 "The object behaves like a named tuple with the following fields:\n"
1973 "  (sysname, nodename, release, version, machine)");
1974 
1975 #define OS_UNAME_METHODDEF    \
1976     {"uname", (PyCFunction)os_uname, METH_NOARGS, os_uname__doc__},
1977 
1978 static PyObject *
1979 os_uname_impl(PyObject *module);
1980 
1981 static PyObject *
os_uname(PyObject * module,PyObject * Py_UNUSED (ignored))1982 os_uname(PyObject *module, PyObject *Py_UNUSED(ignored))
1983 {
1984     return os_uname_impl(module);
1985 }
1986 
1987 #endif /* defined(HAVE_UNAME) */
1988 
1989 PyDoc_STRVAR(os_utime__doc__,
1990 "utime($module, /, path, times=None, *, ns=<unrepresentable>,\n"
1991 "      dir_fd=None, follow_symlinks=True)\n"
1992 "--\n"
1993 "\n"
1994 "Set the access and modified time of path.\n"
1995 "\n"
1996 "path may always be specified as a string.\n"
1997 "On some platforms, path may also be specified as an open file descriptor.\n"
1998 "  If this functionality is unavailable, using it raises an exception.\n"
1999 "\n"
2000 "If times is not None, it must be a tuple (atime, mtime);\n"
2001 "    atime and mtime should be expressed as float seconds since the epoch.\n"
2002 "If ns is specified, it must be a tuple (atime_ns, mtime_ns);\n"
2003 "    atime_ns and mtime_ns should be expressed as integer nanoseconds\n"
2004 "    since the epoch.\n"
2005 "If times is None and ns is unspecified, utime uses the current time.\n"
2006 "Specifying tuples for both times and ns is an error.\n"
2007 "\n"
2008 "If dir_fd is not None, it should be a file descriptor open to a directory,\n"
2009 "  and path should be relative; path will then be relative to that directory.\n"
2010 "If follow_symlinks is False, and the last element of the path is a symbolic\n"
2011 "  link, utime will modify the symbolic link itself instead of the file the\n"
2012 "  link points to.\n"
2013 "It is an error to use dir_fd or follow_symlinks when specifying path\n"
2014 "  as an open file descriptor.\n"
2015 "dir_fd and follow_symlinks may not be available on your platform.\n"
2016 "  If they are unavailable, using them will raise a NotImplementedError.");
2017 
2018 #define OS_UTIME_METHODDEF    \
2019     {"utime", (PyCFunction)(void(*)(void))os_utime, METH_FASTCALL|METH_KEYWORDS, os_utime__doc__},
2020 
2021 static PyObject *
2022 os_utime_impl(PyObject *module, path_t *path, PyObject *times, PyObject *ns,
2023               int dir_fd, int follow_symlinks);
2024 
2025 static PyObject *
os_utime(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)2026 os_utime(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
2027 {
2028     PyObject *return_value = NULL;
2029     static const char * const _keywords[] = {"path", "times", "ns", "dir_fd", "follow_symlinks", NULL};
2030     static _PyArg_Parser _parser = {NULL, _keywords, "utime", 0};
2031     PyObject *argsbuf[5];
2032     Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
2033     path_t path = PATH_T_INITIALIZE("utime", "path", 0, PATH_UTIME_HAVE_FD);
2034     PyObject *times = Py_None;
2035     PyObject *ns = NULL;
2036     int dir_fd = DEFAULT_DIR_FD;
2037     int follow_symlinks = 1;
2038 
2039     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
2040     if (!args) {
2041         goto exit;
2042     }
2043     if (!path_converter(args[0], &path)) {
2044         goto exit;
2045     }
2046     if (!noptargs) {
2047         goto skip_optional_pos;
2048     }
2049     if (args[1]) {
2050         times = args[1];
2051         if (!--noptargs) {
2052             goto skip_optional_pos;
2053         }
2054     }
2055 skip_optional_pos:
2056     if (!noptargs) {
2057         goto skip_optional_kwonly;
2058     }
2059     if (args[2]) {
2060         ns = args[2];
2061         if (!--noptargs) {
2062             goto skip_optional_kwonly;
2063         }
2064     }
2065     if (args[3]) {
2066         if (!FUTIMENSAT_DIR_FD_CONVERTER(args[3], &dir_fd)) {
2067             goto exit;
2068         }
2069         if (!--noptargs) {
2070             goto skip_optional_kwonly;
2071         }
2072     }
2073     follow_symlinks = PyObject_IsTrue(args[4]);
2074     if (follow_symlinks < 0) {
2075         goto exit;
2076     }
2077 skip_optional_kwonly:
2078     return_value = os_utime_impl(module, &path, times, ns, dir_fd, follow_symlinks);
2079 
2080 exit:
2081     /* Cleanup for path */
2082     path_cleanup(&path);
2083 
2084     return return_value;
2085 }
2086 
2087 PyDoc_STRVAR(os__exit__doc__,
2088 "_exit($module, /, status)\n"
2089 "--\n"
2090 "\n"
2091 "Exit to the system with specified status, without normal exit processing.");
2092 
2093 #define OS__EXIT_METHODDEF    \
2094     {"_exit", (PyCFunction)(void(*)(void))os__exit, METH_FASTCALL|METH_KEYWORDS, os__exit__doc__},
2095 
2096 static PyObject *
2097 os__exit_impl(PyObject *module, int status);
2098 
2099 static PyObject *
os__exit(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)2100 os__exit(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
2101 {
2102     PyObject *return_value = NULL;
2103     static const char * const _keywords[] = {"status", NULL};
2104     static _PyArg_Parser _parser = {NULL, _keywords, "_exit", 0};
2105     PyObject *argsbuf[1];
2106     int status;
2107 
2108     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
2109     if (!args) {
2110         goto exit;
2111     }
2112     status = _PyLong_AsInt(args[0]);
2113     if (status == -1 && PyErr_Occurred()) {
2114         goto exit;
2115     }
2116     return_value = os__exit_impl(module, status);
2117 
2118 exit:
2119     return return_value;
2120 }
2121 
2122 #if defined(HAVE_EXECV)
2123 
2124 PyDoc_STRVAR(os_execv__doc__,
2125 "execv($module, path, argv, /)\n"
2126 "--\n"
2127 "\n"
2128 "Execute an executable path with arguments, replacing current process.\n"
2129 "\n"
2130 "  path\n"
2131 "    Path of executable file.\n"
2132 "  argv\n"
2133 "    Tuple or list of strings.");
2134 
2135 #define OS_EXECV_METHODDEF    \
2136     {"execv", (PyCFunction)(void(*)(void))os_execv, METH_FASTCALL, os_execv__doc__},
2137 
2138 static PyObject *
2139 os_execv_impl(PyObject *module, path_t *path, PyObject *argv);
2140 
2141 static PyObject *
os_execv(PyObject * module,PyObject * const * args,Py_ssize_t nargs)2142 os_execv(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
2143 {
2144     PyObject *return_value = NULL;
2145     path_t path = PATH_T_INITIALIZE("execv", "path", 0, 0);
2146     PyObject *argv;
2147 
2148     if (!_PyArg_CheckPositional("execv", nargs, 2, 2)) {
2149         goto exit;
2150     }
2151     if (!path_converter(args[0], &path)) {
2152         goto exit;
2153     }
2154     argv = args[1];
2155     return_value = os_execv_impl(module, &path, argv);
2156 
2157 exit:
2158     /* Cleanup for path */
2159     path_cleanup(&path);
2160 
2161     return return_value;
2162 }
2163 
2164 #endif /* defined(HAVE_EXECV) */
2165 
2166 #if defined(HAVE_EXECV)
2167 
2168 PyDoc_STRVAR(os_execve__doc__,
2169 "execve($module, /, path, argv, env)\n"
2170 "--\n"
2171 "\n"
2172 "Execute an executable path with arguments, replacing current process.\n"
2173 "\n"
2174 "  path\n"
2175 "    Path of executable file.\n"
2176 "  argv\n"
2177 "    Tuple or list of strings.\n"
2178 "  env\n"
2179 "    Dictionary of strings mapping to strings.");
2180 
2181 #define OS_EXECVE_METHODDEF    \
2182     {"execve", (PyCFunction)(void(*)(void))os_execve, METH_FASTCALL|METH_KEYWORDS, os_execve__doc__},
2183 
2184 static PyObject *
2185 os_execve_impl(PyObject *module, path_t *path, PyObject *argv, PyObject *env);
2186 
2187 static PyObject *
os_execve(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)2188 os_execve(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
2189 {
2190     PyObject *return_value = NULL;
2191     static const char * const _keywords[] = {"path", "argv", "env", NULL};
2192     static _PyArg_Parser _parser = {NULL, _keywords, "execve", 0};
2193     PyObject *argsbuf[3];
2194     path_t path = PATH_T_INITIALIZE("execve", "path", 0, PATH_HAVE_FEXECVE);
2195     PyObject *argv;
2196     PyObject *env;
2197 
2198     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 3, 3, 0, argsbuf);
2199     if (!args) {
2200         goto exit;
2201     }
2202     if (!path_converter(args[0], &path)) {
2203         goto exit;
2204     }
2205     argv = args[1];
2206     env = args[2];
2207     return_value = os_execve_impl(module, &path, argv, env);
2208 
2209 exit:
2210     /* Cleanup for path */
2211     path_cleanup(&path);
2212 
2213     return return_value;
2214 }
2215 
2216 #endif /* defined(HAVE_EXECV) */
2217 
2218 #if defined(HAVE_POSIX_SPAWN)
2219 
2220 PyDoc_STRVAR(os_posix_spawn__doc__,
2221 "posix_spawn($module, path, argv, env, /, *, file_actions=(),\n"
2222 "            setpgroup=<unrepresentable>, resetids=False, setsid=False,\n"
2223 "            setsigmask=(), setsigdef=(), scheduler=<unrepresentable>)\n"
2224 "--\n"
2225 "\n"
2226 "Execute the program specified by path in a new process.\n"
2227 "\n"
2228 "  path\n"
2229 "    Path of executable file.\n"
2230 "  argv\n"
2231 "    Tuple or list of strings.\n"
2232 "  env\n"
2233 "    Dictionary of strings mapping to strings.\n"
2234 "  file_actions\n"
2235 "    A sequence of file action tuples.\n"
2236 "  setpgroup\n"
2237 "    The pgroup to use with the POSIX_SPAWN_SETPGROUP flag.\n"
2238 "  resetids\n"
2239 "    If the value is `true` the POSIX_SPAWN_RESETIDS will be activated.\n"
2240 "  setsid\n"
2241 "    If the value is `true` the POSIX_SPAWN_SETSID or POSIX_SPAWN_SETSID_NP will be activated.\n"
2242 "  setsigmask\n"
2243 "    The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag.\n"
2244 "  setsigdef\n"
2245 "    The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag.\n"
2246 "  scheduler\n"
2247 "    A tuple with the scheduler policy (optional) and parameters.");
2248 
2249 #define OS_POSIX_SPAWN_METHODDEF    \
2250     {"posix_spawn", (PyCFunction)(void(*)(void))os_posix_spawn, METH_FASTCALL|METH_KEYWORDS, os_posix_spawn__doc__},
2251 
2252 static PyObject *
2253 os_posix_spawn_impl(PyObject *module, path_t *path, PyObject *argv,
2254                     PyObject *env, PyObject *file_actions,
2255                     PyObject *setpgroup, int resetids, int setsid,
2256                     PyObject *setsigmask, PyObject *setsigdef,
2257                     PyObject *scheduler);
2258 
2259 static PyObject *
os_posix_spawn(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)2260 os_posix_spawn(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
2261 {
2262     PyObject *return_value = NULL;
2263     static const char * const _keywords[] = {"", "", "", "file_actions", "setpgroup", "resetids", "setsid", "setsigmask", "setsigdef", "scheduler", NULL};
2264     static _PyArg_Parser _parser = {NULL, _keywords, "posix_spawn", 0};
2265     PyObject *argsbuf[10];
2266     Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 3;
2267     path_t path = PATH_T_INITIALIZE("posix_spawn", "path", 0, 0);
2268     PyObject *argv;
2269     PyObject *env;
2270     PyObject *file_actions = NULL;
2271     PyObject *setpgroup = NULL;
2272     int resetids = 0;
2273     int setsid = 0;
2274     PyObject *setsigmask = NULL;
2275     PyObject *setsigdef = NULL;
2276     PyObject *scheduler = NULL;
2277 
2278     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 3, 3, 0, argsbuf);
2279     if (!args) {
2280         goto exit;
2281     }
2282     if (!path_converter(args[0], &path)) {
2283         goto exit;
2284     }
2285     argv = args[1];
2286     env = args[2];
2287     if (!noptargs) {
2288         goto skip_optional_kwonly;
2289     }
2290     if (args[3]) {
2291         file_actions = args[3];
2292         if (!--noptargs) {
2293             goto skip_optional_kwonly;
2294         }
2295     }
2296     if (args[4]) {
2297         setpgroup = args[4];
2298         if (!--noptargs) {
2299             goto skip_optional_kwonly;
2300         }
2301     }
2302     if (args[5]) {
2303         resetids = _PyLong_AsInt(args[5]);
2304         if (resetids == -1 && PyErr_Occurred()) {
2305             goto exit;
2306         }
2307         if (!--noptargs) {
2308             goto skip_optional_kwonly;
2309         }
2310     }
2311     if (args[6]) {
2312         setsid = _PyLong_AsInt(args[6]);
2313         if (setsid == -1 && PyErr_Occurred()) {
2314             goto exit;
2315         }
2316         if (!--noptargs) {
2317             goto skip_optional_kwonly;
2318         }
2319     }
2320     if (args[7]) {
2321         setsigmask = args[7];
2322         if (!--noptargs) {
2323             goto skip_optional_kwonly;
2324         }
2325     }
2326     if (args[8]) {
2327         setsigdef = args[8];
2328         if (!--noptargs) {
2329             goto skip_optional_kwonly;
2330         }
2331     }
2332     scheduler = args[9];
2333 skip_optional_kwonly:
2334     return_value = os_posix_spawn_impl(module, &path, argv, env, file_actions, setpgroup, resetids, setsid, setsigmask, setsigdef, scheduler);
2335 
2336 exit:
2337     /* Cleanup for path */
2338     path_cleanup(&path);
2339 
2340     return return_value;
2341 }
2342 
2343 #endif /* defined(HAVE_POSIX_SPAWN) */
2344 
2345 #if defined(HAVE_POSIX_SPAWNP)
2346 
2347 PyDoc_STRVAR(os_posix_spawnp__doc__,
2348 "posix_spawnp($module, path, argv, env, /, *, file_actions=(),\n"
2349 "             setpgroup=<unrepresentable>, resetids=False, setsid=False,\n"
2350 "             setsigmask=(), setsigdef=(), scheduler=<unrepresentable>)\n"
2351 "--\n"
2352 "\n"
2353 "Execute the program specified by path in a new process.\n"
2354 "\n"
2355 "  path\n"
2356 "    Path of executable file.\n"
2357 "  argv\n"
2358 "    Tuple or list of strings.\n"
2359 "  env\n"
2360 "    Dictionary of strings mapping to strings.\n"
2361 "  file_actions\n"
2362 "    A sequence of file action tuples.\n"
2363 "  setpgroup\n"
2364 "    The pgroup to use with the POSIX_SPAWN_SETPGROUP flag.\n"
2365 "  resetids\n"
2366 "    If the value is `True` the POSIX_SPAWN_RESETIDS will be activated.\n"
2367 "  setsid\n"
2368 "    If the value is `True` the POSIX_SPAWN_SETSID or POSIX_SPAWN_SETSID_NP will be activated.\n"
2369 "  setsigmask\n"
2370 "    The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag.\n"
2371 "  setsigdef\n"
2372 "    The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag.\n"
2373 "  scheduler\n"
2374 "    A tuple with the scheduler policy (optional) and parameters.");
2375 
2376 #define OS_POSIX_SPAWNP_METHODDEF    \
2377     {"posix_spawnp", (PyCFunction)(void(*)(void))os_posix_spawnp, METH_FASTCALL|METH_KEYWORDS, os_posix_spawnp__doc__},
2378 
2379 static PyObject *
2380 os_posix_spawnp_impl(PyObject *module, path_t *path, PyObject *argv,
2381                      PyObject *env, PyObject *file_actions,
2382                      PyObject *setpgroup, int resetids, int setsid,
2383                      PyObject *setsigmask, PyObject *setsigdef,
2384                      PyObject *scheduler);
2385 
2386 static PyObject *
os_posix_spawnp(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)2387 os_posix_spawnp(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
2388 {
2389     PyObject *return_value = NULL;
2390     static const char * const _keywords[] = {"", "", "", "file_actions", "setpgroup", "resetids", "setsid", "setsigmask", "setsigdef", "scheduler", NULL};
2391     static _PyArg_Parser _parser = {NULL, _keywords, "posix_spawnp", 0};
2392     PyObject *argsbuf[10];
2393     Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 3;
2394     path_t path = PATH_T_INITIALIZE("posix_spawnp", "path", 0, 0);
2395     PyObject *argv;
2396     PyObject *env;
2397     PyObject *file_actions = NULL;
2398     PyObject *setpgroup = NULL;
2399     int resetids = 0;
2400     int setsid = 0;
2401     PyObject *setsigmask = NULL;
2402     PyObject *setsigdef = NULL;
2403     PyObject *scheduler = NULL;
2404 
2405     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 3, 3, 0, argsbuf);
2406     if (!args) {
2407         goto exit;
2408     }
2409     if (!path_converter(args[0], &path)) {
2410         goto exit;
2411     }
2412     argv = args[1];
2413     env = args[2];
2414     if (!noptargs) {
2415         goto skip_optional_kwonly;
2416     }
2417     if (args[3]) {
2418         file_actions = args[3];
2419         if (!--noptargs) {
2420             goto skip_optional_kwonly;
2421         }
2422     }
2423     if (args[4]) {
2424         setpgroup = args[4];
2425         if (!--noptargs) {
2426             goto skip_optional_kwonly;
2427         }
2428     }
2429     if (args[5]) {
2430         resetids = _PyLong_AsInt(args[5]);
2431         if (resetids == -1 && PyErr_Occurred()) {
2432             goto exit;
2433         }
2434         if (!--noptargs) {
2435             goto skip_optional_kwonly;
2436         }
2437     }
2438     if (args[6]) {
2439         setsid = _PyLong_AsInt(args[6]);
2440         if (setsid == -1 && PyErr_Occurred()) {
2441             goto exit;
2442         }
2443         if (!--noptargs) {
2444             goto skip_optional_kwonly;
2445         }
2446     }
2447     if (args[7]) {
2448         setsigmask = args[7];
2449         if (!--noptargs) {
2450             goto skip_optional_kwonly;
2451         }
2452     }
2453     if (args[8]) {
2454         setsigdef = args[8];
2455         if (!--noptargs) {
2456             goto skip_optional_kwonly;
2457         }
2458     }
2459     scheduler = args[9];
2460 skip_optional_kwonly:
2461     return_value = os_posix_spawnp_impl(module, &path, argv, env, file_actions, setpgroup, resetids, setsid, setsigmask, setsigdef, scheduler);
2462 
2463 exit:
2464     /* Cleanup for path */
2465     path_cleanup(&path);
2466 
2467     return return_value;
2468 }
2469 
2470 #endif /* defined(HAVE_POSIX_SPAWNP) */
2471 
2472 #if (defined(HAVE_SPAWNV) || defined(HAVE_WSPAWNV) || defined(HAVE_RTPSPAWN))
2473 
2474 PyDoc_STRVAR(os_spawnv__doc__,
2475 "spawnv($module, mode, path, argv, /)\n"
2476 "--\n"
2477 "\n"
2478 "Execute the program specified by path in a new process.\n"
2479 "\n"
2480 "  mode\n"
2481 "    Mode of process creation.\n"
2482 "  path\n"
2483 "    Path of executable file.\n"
2484 "  argv\n"
2485 "    Tuple or list of strings.");
2486 
2487 #define OS_SPAWNV_METHODDEF    \
2488     {"spawnv", (PyCFunction)(void(*)(void))os_spawnv, METH_FASTCALL, os_spawnv__doc__},
2489 
2490 static PyObject *
2491 os_spawnv_impl(PyObject *module, int mode, path_t *path, PyObject *argv);
2492 
2493 static PyObject *
os_spawnv(PyObject * module,PyObject * const * args,Py_ssize_t nargs)2494 os_spawnv(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
2495 {
2496     PyObject *return_value = NULL;
2497     int mode;
2498     path_t path = PATH_T_INITIALIZE("spawnv", "path", 0, 0);
2499     PyObject *argv;
2500 
2501     if (!_PyArg_CheckPositional("spawnv", nargs, 3, 3)) {
2502         goto exit;
2503     }
2504     mode = _PyLong_AsInt(args[0]);
2505     if (mode == -1 && PyErr_Occurred()) {
2506         goto exit;
2507     }
2508     if (!path_converter(args[1], &path)) {
2509         goto exit;
2510     }
2511     argv = args[2];
2512     return_value = os_spawnv_impl(module, mode, &path, argv);
2513 
2514 exit:
2515     /* Cleanup for path */
2516     path_cleanup(&path);
2517 
2518     return return_value;
2519 }
2520 
2521 #endif /* (defined(HAVE_SPAWNV) || defined(HAVE_WSPAWNV) || defined(HAVE_RTPSPAWN)) */
2522 
2523 #if (defined(HAVE_SPAWNV) || defined(HAVE_WSPAWNV) || defined(HAVE_RTPSPAWN))
2524 
2525 PyDoc_STRVAR(os_spawnve__doc__,
2526 "spawnve($module, mode, path, argv, env, /)\n"
2527 "--\n"
2528 "\n"
2529 "Execute the program specified by path in a new process.\n"
2530 "\n"
2531 "  mode\n"
2532 "    Mode of process creation.\n"
2533 "  path\n"
2534 "    Path of executable file.\n"
2535 "  argv\n"
2536 "    Tuple or list of strings.\n"
2537 "  env\n"
2538 "    Dictionary of strings mapping to strings.");
2539 
2540 #define OS_SPAWNVE_METHODDEF    \
2541     {"spawnve", (PyCFunction)(void(*)(void))os_spawnve, METH_FASTCALL, os_spawnve__doc__},
2542 
2543 static PyObject *
2544 os_spawnve_impl(PyObject *module, int mode, path_t *path, PyObject *argv,
2545                 PyObject *env);
2546 
2547 static PyObject *
os_spawnve(PyObject * module,PyObject * const * args,Py_ssize_t nargs)2548 os_spawnve(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
2549 {
2550     PyObject *return_value = NULL;
2551     int mode;
2552     path_t path = PATH_T_INITIALIZE("spawnve", "path", 0, 0);
2553     PyObject *argv;
2554     PyObject *env;
2555 
2556     if (!_PyArg_CheckPositional("spawnve", nargs, 4, 4)) {
2557         goto exit;
2558     }
2559     mode = _PyLong_AsInt(args[0]);
2560     if (mode == -1 && PyErr_Occurred()) {
2561         goto exit;
2562     }
2563     if (!path_converter(args[1], &path)) {
2564         goto exit;
2565     }
2566     argv = args[2];
2567     env = args[3];
2568     return_value = os_spawnve_impl(module, mode, &path, argv, env);
2569 
2570 exit:
2571     /* Cleanup for path */
2572     path_cleanup(&path);
2573 
2574     return return_value;
2575 }
2576 
2577 #endif /* (defined(HAVE_SPAWNV) || defined(HAVE_WSPAWNV) || defined(HAVE_RTPSPAWN)) */
2578 
2579 #if defined(HAVE_FORK)
2580 
2581 PyDoc_STRVAR(os_register_at_fork__doc__,
2582 "register_at_fork($module, /, *, before=<unrepresentable>,\n"
2583 "                 after_in_child=<unrepresentable>,\n"
2584 "                 after_in_parent=<unrepresentable>)\n"
2585 "--\n"
2586 "\n"
2587 "Register callables to be called when forking a new process.\n"
2588 "\n"
2589 "  before\n"
2590 "    A callable to be called in the parent before the fork() syscall.\n"
2591 "  after_in_child\n"
2592 "    A callable to be called in the child after fork().\n"
2593 "  after_in_parent\n"
2594 "    A callable to be called in the parent after fork().\n"
2595 "\n"
2596 "\'before\' callbacks are called in reverse order.\n"
2597 "\'after_in_child\' and \'after_in_parent\' callbacks are called in order.");
2598 
2599 #define OS_REGISTER_AT_FORK_METHODDEF    \
2600     {"register_at_fork", (PyCFunction)(void(*)(void))os_register_at_fork, METH_FASTCALL|METH_KEYWORDS, os_register_at_fork__doc__},
2601 
2602 static PyObject *
2603 os_register_at_fork_impl(PyObject *module, PyObject *before,
2604                          PyObject *after_in_child, PyObject *after_in_parent);
2605 
2606 static PyObject *
os_register_at_fork(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)2607 os_register_at_fork(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
2608 {
2609     PyObject *return_value = NULL;
2610     static const char * const _keywords[] = {"before", "after_in_child", "after_in_parent", NULL};
2611     static _PyArg_Parser _parser = {NULL, _keywords, "register_at_fork", 0};
2612     PyObject *argsbuf[3];
2613     Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
2614     PyObject *before = NULL;
2615     PyObject *after_in_child = NULL;
2616     PyObject *after_in_parent = NULL;
2617 
2618     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 0, 0, argsbuf);
2619     if (!args) {
2620         goto exit;
2621     }
2622     if (!noptargs) {
2623         goto skip_optional_kwonly;
2624     }
2625     if (args[0]) {
2626         before = args[0];
2627         if (!--noptargs) {
2628             goto skip_optional_kwonly;
2629         }
2630     }
2631     if (args[1]) {
2632         after_in_child = args[1];
2633         if (!--noptargs) {
2634             goto skip_optional_kwonly;
2635         }
2636     }
2637     after_in_parent = args[2];
2638 skip_optional_kwonly:
2639     return_value = os_register_at_fork_impl(module, before, after_in_child, after_in_parent);
2640 
2641 exit:
2642     return return_value;
2643 }
2644 
2645 #endif /* defined(HAVE_FORK) */
2646 
2647 #if defined(HAVE_FORK1)
2648 
2649 PyDoc_STRVAR(os_fork1__doc__,
2650 "fork1($module, /)\n"
2651 "--\n"
2652 "\n"
2653 "Fork a child process with a single multiplexed (i.e., not bound) thread.\n"
2654 "\n"
2655 "Return 0 to child process and PID of child to parent process.");
2656 
2657 #define OS_FORK1_METHODDEF    \
2658     {"fork1", (PyCFunction)os_fork1, METH_NOARGS, os_fork1__doc__},
2659 
2660 static PyObject *
2661 os_fork1_impl(PyObject *module);
2662 
2663 static PyObject *
os_fork1(PyObject * module,PyObject * Py_UNUSED (ignored))2664 os_fork1(PyObject *module, PyObject *Py_UNUSED(ignored))
2665 {
2666     return os_fork1_impl(module);
2667 }
2668 
2669 #endif /* defined(HAVE_FORK1) */
2670 
2671 #if defined(HAVE_FORK)
2672 
2673 PyDoc_STRVAR(os_fork__doc__,
2674 "fork($module, /)\n"
2675 "--\n"
2676 "\n"
2677 "Fork a child process.\n"
2678 "\n"
2679 "Return 0 to child process and PID of child to parent process.");
2680 
2681 #define OS_FORK_METHODDEF    \
2682     {"fork", (PyCFunction)os_fork, METH_NOARGS, os_fork__doc__},
2683 
2684 static PyObject *
2685 os_fork_impl(PyObject *module);
2686 
2687 static PyObject *
os_fork(PyObject * module,PyObject * Py_UNUSED (ignored))2688 os_fork(PyObject *module, PyObject *Py_UNUSED(ignored))
2689 {
2690     return os_fork_impl(module);
2691 }
2692 
2693 #endif /* defined(HAVE_FORK) */
2694 
2695 #if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_GET_PRIORITY_MAX)
2696 
2697 PyDoc_STRVAR(os_sched_get_priority_max__doc__,
2698 "sched_get_priority_max($module, /, policy)\n"
2699 "--\n"
2700 "\n"
2701 "Get the maximum scheduling priority for policy.");
2702 
2703 #define OS_SCHED_GET_PRIORITY_MAX_METHODDEF    \
2704     {"sched_get_priority_max", (PyCFunction)(void(*)(void))os_sched_get_priority_max, METH_FASTCALL|METH_KEYWORDS, os_sched_get_priority_max__doc__},
2705 
2706 static PyObject *
2707 os_sched_get_priority_max_impl(PyObject *module, int policy);
2708 
2709 static PyObject *
os_sched_get_priority_max(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)2710 os_sched_get_priority_max(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
2711 {
2712     PyObject *return_value = NULL;
2713     static const char * const _keywords[] = {"policy", NULL};
2714     static _PyArg_Parser _parser = {NULL, _keywords, "sched_get_priority_max", 0};
2715     PyObject *argsbuf[1];
2716     int policy;
2717 
2718     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
2719     if (!args) {
2720         goto exit;
2721     }
2722     policy = _PyLong_AsInt(args[0]);
2723     if (policy == -1 && PyErr_Occurred()) {
2724         goto exit;
2725     }
2726     return_value = os_sched_get_priority_max_impl(module, policy);
2727 
2728 exit:
2729     return return_value;
2730 }
2731 
2732 #endif /* defined(HAVE_SCHED_H) && defined(HAVE_SCHED_GET_PRIORITY_MAX) */
2733 
2734 #if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_GET_PRIORITY_MAX)
2735 
2736 PyDoc_STRVAR(os_sched_get_priority_min__doc__,
2737 "sched_get_priority_min($module, /, policy)\n"
2738 "--\n"
2739 "\n"
2740 "Get the minimum scheduling priority for policy.");
2741 
2742 #define OS_SCHED_GET_PRIORITY_MIN_METHODDEF    \
2743     {"sched_get_priority_min", (PyCFunction)(void(*)(void))os_sched_get_priority_min, METH_FASTCALL|METH_KEYWORDS, os_sched_get_priority_min__doc__},
2744 
2745 static PyObject *
2746 os_sched_get_priority_min_impl(PyObject *module, int policy);
2747 
2748 static PyObject *
os_sched_get_priority_min(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)2749 os_sched_get_priority_min(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
2750 {
2751     PyObject *return_value = NULL;
2752     static const char * const _keywords[] = {"policy", NULL};
2753     static _PyArg_Parser _parser = {NULL, _keywords, "sched_get_priority_min", 0};
2754     PyObject *argsbuf[1];
2755     int policy;
2756 
2757     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
2758     if (!args) {
2759         goto exit;
2760     }
2761     policy = _PyLong_AsInt(args[0]);
2762     if (policy == -1 && PyErr_Occurred()) {
2763         goto exit;
2764     }
2765     return_value = os_sched_get_priority_min_impl(module, policy);
2766 
2767 exit:
2768     return return_value;
2769 }
2770 
2771 #endif /* defined(HAVE_SCHED_H) && defined(HAVE_SCHED_GET_PRIORITY_MAX) */
2772 
2773 #if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_SETSCHEDULER)
2774 
2775 PyDoc_STRVAR(os_sched_getscheduler__doc__,
2776 "sched_getscheduler($module, pid, /)\n"
2777 "--\n"
2778 "\n"
2779 "Get the scheduling policy for the process identified by pid.\n"
2780 "\n"
2781 "Passing 0 for pid returns the scheduling policy for the calling process.");
2782 
2783 #define OS_SCHED_GETSCHEDULER_METHODDEF    \
2784     {"sched_getscheduler", (PyCFunction)os_sched_getscheduler, METH_O, os_sched_getscheduler__doc__},
2785 
2786 static PyObject *
2787 os_sched_getscheduler_impl(PyObject *module, pid_t pid);
2788 
2789 static PyObject *
os_sched_getscheduler(PyObject * module,PyObject * arg)2790 os_sched_getscheduler(PyObject *module, PyObject *arg)
2791 {
2792     PyObject *return_value = NULL;
2793     pid_t pid;
2794 
2795     if (!PyArg_Parse(arg, "" _Py_PARSE_PID ":sched_getscheduler", &pid)) {
2796         goto exit;
2797     }
2798     return_value = os_sched_getscheduler_impl(module, pid);
2799 
2800 exit:
2801     return return_value;
2802 }
2803 
2804 #endif /* defined(HAVE_SCHED_H) && defined(HAVE_SCHED_SETSCHEDULER) */
2805 
2806 #if defined(HAVE_SCHED_H) && (defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM))
2807 
2808 PyDoc_STRVAR(os_sched_param__doc__,
2809 "sched_param(sched_priority)\n"
2810 "--\n"
2811 "\n"
2812 "Currently has only one field: sched_priority\n"
2813 "\n"
2814 "  sched_priority\n"
2815 "    A scheduling parameter.");
2816 
2817 static PyObject *
2818 os_sched_param_impl(PyTypeObject *type, PyObject *sched_priority);
2819 
2820 static PyObject *
os_sched_param(PyTypeObject * type,PyObject * args,PyObject * kwargs)2821 os_sched_param(PyTypeObject *type, PyObject *args, PyObject *kwargs)
2822 {
2823     PyObject *return_value = NULL;
2824     static const char * const _keywords[] = {"sched_priority", NULL};
2825     static _PyArg_Parser _parser = {NULL, _keywords, "sched_param", 0};
2826     PyObject *argsbuf[1];
2827     PyObject * const *fastargs;
2828     Py_ssize_t nargs = PyTuple_GET_SIZE(args);
2829     PyObject *sched_priority;
2830 
2831     fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 1, 1, 0, argsbuf);
2832     if (!fastargs) {
2833         goto exit;
2834     }
2835     sched_priority = fastargs[0];
2836     return_value = os_sched_param_impl(type, sched_priority);
2837 
2838 exit:
2839     return return_value;
2840 }
2841 
2842 #endif /* defined(HAVE_SCHED_H) && (defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)) */
2843 
2844 #if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_SETSCHEDULER)
2845 
2846 PyDoc_STRVAR(os_sched_setscheduler__doc__,
2847 "sched_setscheduler($module, pid, policy, param, /)\n"
2848 "--\n"
2849 "\n"
2850 "Set the scheduling policy for the process identified by pid.\n"
2851 "\n"
2852 "If pid is 0, the calling process is changed.\n"
2853 "param is an instance of sched_param.");
2854 
2855 #define OS_SCHED_SETSCHEDULER_METHODDEF    \
2856     {"sched_setscheduler", (PyCFunction)(void(*)(void))os_sched_setscheduler, METH_FASTCALL, os_sched_setscheduler__doc__},
2857 
2858 static PyObject *
2859 os_sched_setscheduler_impl(PyObject *module, pid_t pid, int policy,
2860                            PyObject *param_obj);
2861 
2862 static PyObject *
os_sched_setscheduler(PyObject * module,PyObject * const * args,Py_ssize_t nargs)2863 os_sched_setscheduler(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
2864 {
2865     PyObject *return_value = NULL;
2866     pid_t pid;
2867     int policy;
2868     PyObject *param_obj;
2869 
2870     if (!_PyArg_ParseStack(args, nargs, "" _Py_PARSE_PID "iO:sched_setscheduler",
2871         &pid, &policy, &param_obj)) {
2872         goto exit;
2873     }
2874     return_value = os_sched_setscheduler_impl(module, pid, policy, param_obj);
2875 
2876 exit:
2877     return return_value;
2878 }
2879 
2880 #endif /* defined(HAVE_SCHED_H) && defined(HAVE_SCHED_SETSCHEDULER) */
2881 
2882 #if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_SETPARAM)
2883 
2884 PyDoc_STRVAR(os_sched_getparam__doc__,
2885 "sched_getparam($module, pid, /)\n"
2886 "--\n"
2887 "\n"
2888 "Returns scheduling parameters for the process identified by pid.\n"
2889 "\n"
2890 "If pid is 0, returns parameters for the calling process.\n"
2891 "Return value is an instance of sched_param.");
2892 
2893 #define OS_SCHED_GETPARAM_METHODDEF    \
2894     {"sched_getparam", (PyCFunction)os_sched_getparam, METH_O, os_sched_getparam__doc__},
2895 
2896 static PyObject *
2897 os_sched_getparam_impl(PyObject *module, pid_t pid);
2898 
2899 static PyObject *
os_sched_getparam(PyObject * module,PyObject * arg)2900 os_sched_getparam(PyObject *module, PyObject *arg)
2901 {
2902     PyObject *return_value = NULL;
2903     pid_t pid;
2904 
2905     if (!PyArg_Parse(arg, "" _Py_PARSE_PID ":sched_getparam", &pid)) {
2906         goto exit;
2907     }
2908     return_value = os_sched_getparam_impl(module, pid);
2909 
2910 exit:
2911     return return_value;
2912 }
2913 
2914 #endif /* defined(HAVE_SCHED_H) && defined(HAVE_SCHED_SETPARAM) */
2915 
2916 #if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_SETPARAM)
2917 
2918 PyDoc_STRVAR(os_sched_setparam__doc__,
2919 "sched_setparam($module, pid, param, /)\n"
2920 "--\n"
2921 "\n"
2922 "Set scheduling parameters for the process identified by pid.\n"
2923 "\n"
2924 "If pid is 0, sets parameters for the calling process.\n"
2925 "param should be an instance of sched_param.");
2926 
2927 #define OS_SCHED_SETPARAM_METHODDEF    \
2928     {"sched_setparam", (PyCFunction)(void(*)(void))os_sched_setparam, METH_FASTCALL, os_sched_setparam__doc__},
2929 
2930 static PyObject *
2931 os_sched_setparam_impl(PyObject *module, pid_t pid, PyObject *param_obj);
2932 
2933 static PyObject *
os_sched_setparam(PyObject * module,PyObject * const * args,Py_ssize_t nargs)2934 os_sched_setparam(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
2935 {
2936     PyObject *return_value = NULL;
2937     pid_t pid;
2938     PyObject *param_obj;
2939 
2940     if (!_PyArg_ParseStack(args, nargs, "" _Py_PARSE_PID "O:sched_setparam",
2941         &pid, &param_obj)) {
2942         goto exit;
2943     }
2944     return_value = os_sched_setparam_impl(module, pid, param_obj);
2945 
2946 exit:
2947     return return_value;
2948 }
2949 
2950 #endif /* defined(HAVE_SCHED_H) && defined(HAVE_SCHED_SETPARAM) */
2951 
2952 #if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_RR_GET_INTERVAL)
2953 
2954 PyDoc_STRVAR(os_sched_rr_get_interval__doc__,
2955 "sched_rr_get_interval($module, pid, /)\n"
2956 "--\n"
2957 "\n"
2958 "Return the round-robin quantum for the process identified by pid, in seconds.\n"
2959 "\n"
2960 "Value returned is a float.");
2961 
2962 #define OS_SCHED_RR_GET_INTERVAL_METHODDEF    \
2963     {"sched_rr_get_interval", (PyCFunction)os_sched_rr_get_interval, METH_O, os_sched_rr_get_interval__doc__},
2964 
2965 static double
2966 os_sched_rr_get_interval_impl(PyObject *module, pid_t pid);
2967 
2968 static PyObject *
os_sched_rr_get_interval(PyObject * module,PyObject * arg)2969 os_sched_rr_get_interval(PyObject *module, PyObject *arg)
2970 {
2971     PyObject *return_value = NULL;
2972     pid_t pid;
2973     double _return_value;
2974 
2975     if (!PyArg_Parse(arg, "" _Py_PARSE_PID ":sched_rr_get_interval", &pid)) {
2976         goto exit;
2977     }
2978     _return_value = os_sched_rr_get_interval_impl(module, pid);
2979     if ((_return_value == -1.0) && PyErr_Occurred()) {
2980         goto exit;
2981     }
2982     return_value = PyFloat_FromDouble(_return_value);
2983 
2984 exit:
2985     return return_value;
2986 }
2987 
2988 #endif /* defined(HAVE_SCHED_H) && defined(HAVE_SCHED_RR_GET_INTERVAL) */
2989 
2990 #if defined(HAVE_SCHED_H)
2991 
2992 PyDoc_STRVAR(os_sched_yield__doc__,
2993 "sched_yield($module, /)\n"
2994 "--\n"
2995 "\n"
2996 "Voluntarily relinquish the CPU.");
2997 
2998 #define OS_SCHED_YIELD_METHODDEF    \
2999     {"sched_yield", (PyCFunction)os_sched_yield, METH_NOARGS, os_sched_yield__doc__},
3000 
3001 static PyObject *
3002 os_sched_yield_impl(PyObject *module);
3003 
3004 static PyObject *
os_sched_yield(PyObject * module,PyObject * Py_UNUSED (ignored))3005 os_sched_yield(PyObject *module, PyObject *Py_UNUSED(ignored))
3006 {
3007     return os_sched_yield_impl(module);
3008 }
3009 
3010 #endif /* defined(HAVE_SCHED_H) */
3011 
3012 #if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_SETAFFINITY)
3013 
3014 PyDoc_STRVAR(os_sched_setaffinity__doc__,
3015 "sched_setaffinity($module, pid, mask, /)\n"
3016 "--\n"
3017 "\n"
3018 "Set the CPU affinity of the process identified by pid to mask.\n"
3019 "\n"
3020 "mask should be an iterable of integers identifying CPUs.");
3021 
3022 #define OS_SCHED_SETAFFINITY_METHODDEF    \
3023     {"sched_setaffinity", (PyCFunction)(void(*)(void))os_sched_setaffinity, METH_FASTCALL, os_sched_setaffinity__doc__},
3024 
3025 static PyObject *
3026 os_sched_setaffinity_impl(PyObject *module, pid_t pid, PyObject *mask);
3027 
3028 static PyObject *
os_sched_setaffinity(PyObject * module,PyObject * const * args,Py_ssize_t nargs)3029 os_sched_setaffinity(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
3030 {
3031     PyObject *return_value = NULL;
3032     pid_t pid;
3033     PyObject *mask;
3034 
3035     if (!_PyArg_ParseStack(args, nargs, "" _Py_PARSE_PID "O:sched_setaffinity",
3036         &pid, &mask)) {
3037         goto exit;
3038     }
3039     return_value = os_sched_setaffinity_impl(module, pid, mask);
3040 
3041 exit:
3042     return return_value;
3043 }
3044 
3045 #endif /* defined(HAVE_SCHED_H) && defined(HAVE_SCHED_SETAFFINITY) */
3046 
3047 #if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_SETAFFINITY)
3048 
3049 PyDoc_STRVAR(os_sched_getaffinity__doc__,
3050 "sched_getaffinity($module, pid, /)\n"
3051 "--\n"
3052 "\n"
3053 "Return the affinity of the process identified by pid (or the current process if zero).\n"
3054 "\n"
3055 "The affinity is returned as a set of CPU identifiers.");
3056 
3057 #define OS_SCHED_GETAFFINITY_METHODDEF    \
3058     {"sched_getaffinity", (PyCFunction)os_sched_getaffinity, METH_O, os_sched_getaffinity__doc__},
3059 
3060 static PyObject *
3061 os_sched_getaffinity_impl(PyObject *module, pid_t pid);
3062 
3063 static PyObject *
os_sched_getaffinity(PyObject * module,PyObject * arg)3064 os_sched_getaffinity(PyObject *module, PyObject *arg)
3065 {
3066     PyObject *return_value = NULL;
3067     pid_t pid;
3068 
3069     if (!PyArg_Parse(arg, "" _Py_PARSE_PID ":sched_getaffinity", &pid)) {
3070         goto exit;
3071     }
3072     return_value = os_sched_getaffinity_impl(module, pid);
3073 
3074 exit:
3075     return return_value;
3076 }
3077 
3078 #endif /* defined(HAVE_SCHED_H) && defined(HAVE_SCHED_SETAFFINITY) */
3079 
3080 #if (defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX))
3081 
3082 PyDoc_STRVAR(os_openpty__doc__,
3083 "openpty($module, /)\n"
3084 "--\n"
3085 "\n"
3086 "Open a pseudo-terminal.\n"
3087 "\n"
3088 "Return a tuple of (master_fd, slave_fd) containing open file descriptors\n"
3089 "for both the master and slave ends.");
3090 
3091 #define OS_OPENPTY_METHODDEF    \
3092     {"openpty", (PyCFunction)os_openpty, METH_NOARGS, os_openpty__doc__},
3093 
3094 static PyObject *
3095 os_openpty_impl(PyObject *module);
3096 
3097 static PyObject *
os_openpty(PyObject * module,PyObject * Py_UNUSED (ignored))3098 os_openpty(PyObject *module, PyObject *Py_UNUSED(ignored))
3099 {
3100     return os_openpty_impl(module);
3101 }
3102 
3103 #endif /* (defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)) */
3104 
3105 #if defined(HAVE_FORKPTY)
3106 
3107 PyDoc_STRVAR(os_forkpty__doc__,
3108 "forkpty($module, /)\n"
3109 "--\n"
3110 "\n"
3111 "Fork a new process with a new pseudo-terminal as controlling tty.\n"
3112 "\n"
3113 "Returns a tuple of (pid, master_fd).\n"
3114 "Like fork(), return pid of 0 to the child process,\n"
3115 "and pid of child to the parent process.\n"
3116 "To both, return fd of newly opened pseudo-terminal.");
3117 
3118 #define OS_FORKPTY_METHODDEF    \
3119     {"forkpty", (PyCFunction)os_forkpty, METH_NOARGS, os_forkpty__doc__},
3120 
3121 static PyObject *
3122 os_forkpty_impl(PyObject *module);
3123 
3124 static PyObject *
os_forkpty(PyObject * module,PyObject * Py_UNUSED (ignored))3125 os_forkpty(PyObject *module, PyObject *Py_UNUSED(ignored))
3126 {
3127     return os_forkpty_impl(module);
3128 }
3129 
3130 #endif /* defined(HAVE_FORKPTY) */
3131 
3132 #if defined(HAVE_GETEGID)
3133 
3134 PyDoc_STRVAR(os_getegid__doc__,
3135 "getegid($module, /)\n"
3136 "--\n"
3137 "\n"
3138 "Return the current process\'s effective group id.");
3139 
3140 #define OS_GETEGID_METHODDEF    \
3141     {"getegid", (PyCFunction)os_getegid, METH_NOARGS, os_getegid__doc__},
3142 
3143 static PyObject *
3144 os_getegid_impl(PyObject *module);
3145 
3146 static PyObject *
os_getegid(PyObject * module,PyObject * Py_UNUSED (ignored))3147 os_getegid(PyObject *module, PyObject *Py_UNUSED(ignored))
3148 {
3149     return os_getegid_impl(module);
3150 }
3151 
3152 #endif /* defined(HAVE_GETEGID) */
3153 
3154 #if defined(HAVE_GETEUID)
3155 
3156 PyDoc_STRVAR(os_geteuid__doc__,
3157 "geteuid($module, /)\n"
3158 "--\n"
3159 "\n"
3160 "Return the current process\'s effective user id.");
3161 
3162 #define OS_GETEUID_METHODDEF    \
3163     {"geteuid", (PyCFunction)os_geteuid, METH_NOARGS, os_geteuid__doc__},
3164 
3165 static PyObject *
3166 os_geteuid_impl(PyObject *module);
3167 
3168 static PyObject *
os_geteuid(PyObject * module,PyObject * Py_UNUSED (ignored))3169 os_geteuid(PyObject *module, PyObject *Py_UNUSED(ignored))
3170 {
3171     return os_geteuid_impl(module);
3172 }
3173 
3174 #endif /* defined(HAVE_GETEUID) */
3175 
3176 #if defined(HAVE_GETGID)
3177 
3178 PyDoc_STRVAR(os_getgid__doc__,
3179 "getgid($module, /)\n"
3180 "--\n"
3181 "\n"
3182 "Return the current process\'s group id.");
3183 
3184 #define OS_GETGID_METHODDEF    \
3185     {"getgid", (PyCFunction)os_getgid, METH_NOARGS, os_getgid__doc__},
3186 
3187 static PyObject *
3188 os_getgid_impl(PyObject *module);
3189 
3190 static PyObject *
os_getgid(PyObject * module,PyObject * Py_UNUSED (ignored))3191 os_getgid(PyObject *module, PyObject *Py_UNUSED(ignored))
3192 {
3193     return os_getgid_impl(module);
3194 }
3195 
3196 #endif /* defined(HAVE_GETGID) */
3197 
3198 #if defined(HAVE_GETPID)
3199 
3200 PyDoc_STRVAR(os_getpid__doc__,
3201 "getpid($module, /)\n"
3202 "--\n"
3203 "\n"
3204 "Return the current process id.");
3205 
3206 #define OS_GETPID_METHODDEF    \
3207     {"getpid", (PyCFunction)os_getpid, METH_NOARGS, os_getpid__doc__},
3208 
3209 static PyObject *
3210 os_getpid_impl(PyObject *module);
3211 
3212 static PyObject *
os_getpid(PyObject * module,PyObject * Py_UNUSED (ignored))3213 os_getpid(PyObject *module, PyObject *Py_UNUSED(ignored))
3214 {
3215     return os_getpid_impl(module);
3216 }
3217 
3218 #endif /* defined(HAVE_GETPID) */
3219 
3220 #if defined(HAVE_GETGROUPLIST) && defined(__APPLE__)
3221 
3222 PyDoc_STRVAR(os_getgrouplist__doc__,
3223 "getgrouplist($module, user, group, /)\n"
3224 "--\n"
3225 "\n"
3226 "Returns a list of groups to which a user belongs.\n"
3227 "\n"
3228 "  user\n"
3229 "    username to lookup\n"
3230 "  group\n"
3231 "    base group id of the user");
3232 
3233 #define OS_GETGROUPLIST_METHODDEF    \
3234     {"getgrouplist", (PyCFunction)(void(*)(void))os_getgrouplist, METH_FASTCALL, os_getgrouplist__doc__},
3235 
3236 static PyObject *
3237 os_getgrouplist_impl(PyObject *module, const char *user, int basegid);
3238 
3239 static PyObject *
os_getgrouplist(PyObject * module,PyObject * const * args,Py_ssize_t nargs)3240 os_getgrouplist(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
3241 {
3242     PyObject *return_value = NULL;
3243     const char *user;
3244     int basegid;
3245 
3246     if (!_PyArg_CheckPositional("getgrouplist", nargs, 2, 2)) {
3247         goto exit;
3248     }
3249     if (!PyUnicode_Check(args[0])) {
3250         _PyArg_BadArgument("getgrouplist", "argument 1", "str", args[0]);
3251         goto exit;
3252     }
3253     Py_ssize_t user_length;
3254     user = PyUnicode_AsUTF8AndSize(args[0], &user_length);
3255     if (user == NULL) {
3256         goto exit;
3257     }
3258     if (strlen(user) != (size_t)user_length) {
3259         PyErr_SetString(PyExc_ValueError, "embedded null character");
3260         goto exit;
3261     }
3262     basegid = _PyLong_AsInt(args[1]);
3263     if (basegid == -1 && PyErr_Occurred()) {
3264         goto exit;
3265     }
3266     return_value = os_getgrouplist_impl(module, user, basegid);
3267 
3268 exit:
3269     return return_value;
3270 }
3271 
3272 #endif /* defined(HAVE_GETGROUPLIST) && defined(__APPLE__) */
3273 
3274 #if defined(HAVE_GETGROUPLIST) && !defined(__APPLE__)
3275 
3276 PyDoc_STRVAR(os_getgrouplist__doc__,
3277 "getgrouplist($module, user, group, /)\n"
3278 "--\n"
3279 "\n"
3280 "Returns a list of groups to which a user belongs.\n"
3281 "\n"
3282 "  user\n"
3283 "    username to lookup\n"
3284 "  group\n"
3285 "    base group id of the user");
3286 
3287 #define OS_GETGROUPLIST_METHODDEF    \
3288     {"getgrouplist", (PyCFunction)(void(*)(void))os_getgrouplist, METH_FASTCALL, os_getgrouplist__doc__},
3289 
3290 static PyObject *
3291 os_getgrouplist_impl(PyObject *module, const char *user, gid_t basegid);
3292 
3293 static PyObject *
os_getgrouplist(PyObject * module,PyObject * const * args,Py_ssize_t nargs)3294 os_getgrouplist(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
3295 {
3296     PyObject *return_value = NULL;
3297     const char *user;
3298     gid_t basegid;
3299 
3300     if (!_PyArg_CheckPositional("getgrouplist", nargs, 2, 2)) {
3301         goto exit;
3302     }
3303     if (!PyUnicode_Check(args[0])) {
3304         _PyArg_BadArgument("getgrouplist", "argument 1", "str", args[0]);
3305         goto exit;
3306     }
3307     Py_ssize_t user_length;
3308     user = PyUnicode_AsUTF8AndSize(args[0], &user_length);
3309     if (user == NULL) {
3310         goto exit;
3311     }
3312     if (strlen(user) != (size_t)user_length) {
3313         PyErr_SetString(PyExc_ValueError, "embedded null character");
3314         goto exit;
3315     }
3316     if (!_Py_Gid_Converter(args[1], &basegid)) {
3317         goto exit;
3318     }
3319     return_value = os_getgrouplist_impl(module, user, basegid);
3320 
3321 exit:
3322     return return_value;
3323 }
3324 
3325 #endif /* defined(HAVE_GETGROUPLIST) && !defined(__APPLE__) */
3326 
3327 #if defined(HAVE_GETGROUPS)
3328 
3329 PyDoc_STRVAR(os_getgroups__doc__,
3330 "getgroups($module, /)\n"
3331 "--\n"
3332 "\n"
3333 "Return list of supplemental group IDs for the process.");
3334 
3335 #define OS_GETGROUPS_METHODDEF    \
3336     {"getgroups", (PyCFunction)os_getgroups, METH_NOARGS, os_getgroups__doc__},
3337 
3338 static PyObject *
3339 os_getgroups_impl(PyObject *module);
3340 
3341 static PyObject *
os_getgroups(PyObject * module,PyObject * Py_UNUSED (ignored))3342 os_getgroups(PyObject *module, PyObject *Py_UNUSED(ignored))
3343 {
3344     return os_getgroups_impl(module);
3345 }
3346 
3347 #endif /* defined(HAVE_GETGROUPS) */
3348 
3349 #if defined(HAVE_INITGROUPS) && defined(__APPLE__)
3350 
3351 PyDoc_STRVAR(os_initgroups__doc__,
3352 "initgroups($module, username, gid, /)\n"
3353 "--\n"
3354 "\n"
3355 "Initialize the group access list.\n"
3356 "\n"
3357 "Call the system initgroups() to initialize the group access list with all of\n"
3358 "the groups of which the specified username is a member, plus the specified\n"
3359 "group id.");
3360 
3361 #define OS_INITGROUPS_METHODDEF    \
3362     {"initgroups", (PyCFunction)(void(*)(void))os_initgroups, METH_FASTCALL, os_initgroups__doc__},
3363 
3364 static PyObject *
3365 os_initgroups_impl(PyObject *module, PyObject *oname, int gid);
3366 
3367 static PyObject *
os_initgroups(PyObject * module,PyObject * const * args,Py_ssize_t nargs)3368 os_initgroups(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
3369 {
3370     PyObject *return_value = NULL;
3371     PyObject *oname = NULL;
3372     int gid;
3373 
3374     if (!_PyArg_CheckPositional("initgroups", nargs, 2, 2)) {
3375         goto exit;
3376     }
3377     if (!PyUnicode_FSConverter(args[0], &oname)) {
3378         goto exit;
3379     }
3380     gid = _PyLong_AsInt(args[1]);
3381     if (gid == -1 && PyErr_Occurred()) {
3382         goto exit;
3383     }
3384     return_value = os_initgroups_impl(module, oname, gid);
3385 
3386 exit:
3387     /* Cleanup for oname */
3388     Py_XDECREF(oname);
3389 
3390     return return_value;
3391 }
3392 
3393 #endif /* defined(HAVE_INITGROUPS) && defined(__APPLE__) */
3394 
3395 #if defined(HAVE_INITGROUPS) && !defined(__APPLE__)
3396 
3397 PyDoc_STRVAR(os_initgroups__doc__,
3398 "initgroups($module, username, gid, /)\n"
3399 "--\n"
3400 "\n"
3401 "Initialize the group access list.\n"
3402 "\n"
3403 "Call the system initgroups() to initialize the group access list with all of\n"
3404 "the groups of which the specified username is a member, plus the specified\n"
3405 "group id.");
3406 
3407 #define OS_INITGROUPS_METHODDEF    \
3408     {"initgroups", (PyCFunction)(void(*)(void))os_initgroups, METH_FASTCALL, os_initgroups__doc__},
3409 
3410 static PyObject *
3411 os_initgroups_impl(PyObject *module, PyObject *oname, gid_t gid);
3412 
3413 static PyObject *
os_initgroups(PyObject * module,PyObject * const * args,Py_ssize_t nargs)3414 os_initgroups(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
3415 {
3416     PyObject *return_value = NULL;
3417     PyObject *oname = NULL;
3418     gid_t gid;
3419 
3420     if (!_PyArg_CheckPositional("initgroups", nargs, 2, 2)) {
3421         goto exit;
3422     }
3423     if (!PyUnicode_FSConverter(args[0], &oname)) {
3424         goto exit;
3425     }
3426     if (!_Py_Gid_Converter(args[1], &gid)) {
3427         goto exit;
3428     }
3429     return_value = os_initgroups_impl(module, oname, gid);
3430 
3431 exit:
3432     /* Cleanup for oname */
3433     Py_XDECREF(oname);
3434 
3435     return return_value;
3436 }
3437 
3438 #endif /* defined(HAVE_INITGROUPS) && !defined(__APPLE__) */
3439 
3440 #if defined(HAVE_GETPGID)
3441 
3442 PyDoc_STRVAR(os_getpgid__doc__,
3443 "getpgid($module, /, pid)\n"
3444 "--\n"
3445 "\n"
3446 "Call the system call getpgid(), and return the result.");
3447 
3448 #define OS_GETPGID_METHODDEF    \
3449     {"getpgid", (PyCFunction)(void(*)(void))os_getpgid, METH_FASTCALL|METH_KEYWORDS, os_getpgid__doc__},
3450 
3451 static PyObject *
3452 os_getpgid_impl(PyObject *module, pid_t pid);
3453 
3454 static PyObject *
os_getpgid(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)3455 os_getpgid(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
3456 {
3457     PyObject *return_value = NULL;
3458     static const char * const _keywords[] = {"pid", NULL};
3459     static _PyArg_Parser _parser = {"" _Py_PARSE_PID ":getpgid", _keywords, 0};
3460     pid_t pid;
3461 
3462     if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
3463         &pid)) {
3464         goto exit;
3465     }
3466     return_value = os_getpgid_impl(module, pid);
3467 
3468 exit:
3469     return return_value;
3470 }
3471 
3472 #endif /* defined(HAVE_GETPGID) */
3473 
3474 #if defined(HAVE_GETPGRP)
3475 
3476 PyDoc_STRVAR(os_getpgrp__doc__,
3477 "getpgrp($module, /)\n"
3478 "--\n"
3479 "\n"
3480 "Return the current process group id.");
3481 
3482 #define OS_GETPGRP_METHODDEF    \
3483     {"getpgrp", (PyCFunction)os_getpgrp, METH_NOARGS, os_getpgrp__doc__},
3484 
3485 static PyObject *
3486 os_getpgrp_impl(PyObject *module);
3487 
3488 static PyObject *
os_getpgrp(PyObject * module,PyObject * Py_UNUSED (ignored))3489 os_getpgrp(PyObject *module, PyObject *Py_UNUSED(ignored))
3490 {
3491     return os_getpgrp_impl(module);
3492 }
3493 
3494 #endif /* defined(HAVE_GETPGRP) */
3495 
3496 #if defined(HAVE_SETPGRP)
3497 
3498 PyDoc_STRVAR(os_setpgrp__doc__,
3499 "setpgrp($module, /)\n"
3500 "--\n"
3501 "\n"
3502 "Make the current process the leader of its process group.");
3503 
3504 #define OS_SETPGRP_METHODDEF    \
3505     {"setpgrp", (PyCFunction)os_setpgrp, METH_NOARGS, os_setpgrp__doc__},
3506 
3507 static PyObject *
3508 os_setpgrp_impl(PyObject *module);
3509 
3510 static PyObject *
os_setpgrp(PyObject * module,PyObject * Py_UNUSED (ignored))3511 os_setpgrp(PyObject *module, PyObject *Py_UNUSED(ignored))
3512 {
3513     return os_setpgrp_impl(module);
3514 }
3515 
3516 #endif /* defined(HAVE_SETPGRP) */
3517 
3518 #if defined(HAVE_GETPPID)
3519 
3520 PyDoc_STRVAR(os_getppid__doc__,
3521 "getppid($module, /)\n"
3522 "--\n"
3523 "\n"
3524 "Return the parent\'s process id.\n"
3525 "\n"
3526 "If the parent process has already exited, Windows machines will still\n"
3527 "return its id; others systems will return the id of the \'init\' process (1).");
3528 
3529 #define OS_GETPPID_METHODDEF    \
3530     {"getppid", (PyCFunction)os_getppid, METH_NOARGS, os_getppid__doc__},
3531 
3532 static PyObject *
3533 os_getppid_impl(PyObject *module);
3534 
3535 static PyObject *
os_getppid(PyObject * module,PyObject * Py_UNUSED (ignored))3536 os_getppid(PyObject *module, PyObject *Py_UNUSED(ignored))
3537 {
3538     return os_getppid_impl(module);
3539 }
3540 
3541 #endif /* defined(HAVE_GETPPID) */
3542 
3543 #if defined(HAVE_GETLOGIN)
3544 
3545 PyDoc_STRVAR(os_getlogin__doc__,
3546 "getlogin($module, /)\n"
3547 "--\n"
3548 "\n"
3549 "Return the actual login name.");
3550 
3551 #define OS_GETLOGIN_METHODDEF    \
3552     {"getlogin", (PyCFunction)os_getlogin, METH_NOARGS, os_getlogin__doc__},
3553 
3554 static PyObject *
3555 os_getlogin_impl(PyObject *module);
3556 
3557 static PyObject *
os_getlogin(PyObject * module,PyObject * Py_UNUSED (ignored))3558 os_getlogin(PyObject *module, PyObject *Py_UNUSED(ignored))
3559 {
3560     return os_getlogin_impl(module);
3561 }
3562 
3563 #endif /* defined(HAVE_GETLOGIN) */
3564 
3565 #if defined(HAVE_GETUID)
3566 
3567 PyDoc_STRVAR(os_getuid__doc__,
3568 "getuid($module, /)\n"
3569 "--\n"
3570 "\n"
3571 "Return the current process\'s user id.");
3572 
3573 #define OS_GETUID_METHODDEF    \
3574     {"getuid", (PyCFunction)os_getuid, METH_NOARGS, os_getuid__doc__},
3575 
3576 static PyObject *
3577 os_getuid_impl(PyObject *module);
3578 
3579 static PyObject *
os_getuid(PyObject * module,PyObject * Py_UNUSED (ignored))3580 os_getuid(PyObject *module, PyObject *Py_UNUSED(ignored))
3581 {
3582     return os_getuid_impl(module);
3583 }
3584 
3585 #endif /* defined(HAVE_GETUID) */
3586 
3587 #if defined(HAVE_KILL)
3588 
3589 PyDoc_STRVAR(os_kill__doc__,
3590 "kill($module, pid, signal, /)\n"
3591 "--\n"
3592 "\n"
3593 "Kill a process with a signal.");
3594 
3595 #define OS_KILL_METHODDEF    \
3596     {"kill", (PyCFunction)(void(*)(void))os_kill, METH_FASTCALL, os_kill__doc__},
3597 
3598 static PyObject *
3599 os_kill_impl(PyObject *module, pid_t pid, Py_ssize_t signal);
3600 
3601 static PyObject *
os_kill(PyObject * module,PyObject * const * args,Py_ssize_t nargs)3602 os_kill(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
3603 {
3604     PyObject *return_value = NULL;
3605     pid_t pid;
3606     Py_ssize_t signal;
3607 
3608     if (!_PyArg_ParseStack(args, nargs, "" _Py_PARSE_PID "n:kill",
3609         &pid, &signal)) {
3610         goto exit;
3611     }
3612     return_value = os_kill_impl(module, pid, signal);
3613 
3614 exit:
3615     return return_value;
3616 }
3617 
3618 #endif /* defined(HAVE_KILL) */
3619 
3620 #if defined(HAVE_KILLPG)
3621 
3622 PyDoc_STRVAR(os_killpg__doc__,
3623 "killpg($module, pgid, signal, /)\n"
3624 "--\n"
3625 "\n"
3626 "Kill a process group with a signal.");
3627 
3628 #define OS_KILLPG_METHODDEF    \
3629     {"killpg", (PyCFunction)(void(*)(void))os_killpg, METH_FASTCALL, os_killpg__doc__},
3630 
3631 static PyObject *
3632 os_killpg_impl(PyObject *module, pid_t pgid, int signal);
3633 
3634 static PyObject *
os_killpg(PyObject * module,PyObject * const * args,Py_ssize_t nargs)3635 os_killpg(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
3636 {
3637     PyObject *return_value = NULL;
3638     pid_t pgid;
3639     int signal;
3640 
3641     if (!_PyArg_ParseStack(args, nargs, "" _Py_PARSE_PID "i:killpg",
3642         &pgid, &signal)) {
3643         goto exit;
3644     }
3645     return_value = os_killpg_impl(module, pgid, signal);
3646 
3647 exit:
3648     return return_value;
3649 }
3650 
3651 #endif /* defined(HAVE_KILLPG) */
3652 
3653 #if defined(HAVE_PLOCK)
3654 
3655 PyDoc_STRVAR(os_plock__doc__,
3656 "plock($module, op, /)\n"
3657 "--\n"
3658 "\n"
3659 "Lock program segments into memory.\");");
3660 
3661 #define OS_PLOCK_METHODDEF    \
3662     {"plock", (PyCFunction)os_plock, METH_O, os_plock__doc__},
3663 
3664 static PyObject *
3665 os_plock_impl(PyObject *module, int op);
3666 
3667 static PyObject *
os_plock(PyObject * module,PyObject * arg)3668 os_plock(PyObject *module, PyObject *arg)
3669 {
3670     PyObject *return_value = NULL;
3671     int op;
3672 
3673     op = _PyLong_AsInt(arg);
3674     if (op == -1 && PyErr_Occurred()) {
3675         goto exit;
3676     }
3677     return_value = os_plock_impl(module, op);
3678 
3679 exit:
3680     return return_value;
3681 }
3682 
3683 #endif /* defined(HAVE_PLOCK) */
3684 
3685 #if defined(HAVE_SETUID)
3686 
3687 PyDoc_STRVAR(os_setuid__doc__,
3688 "setuid($module, uid, /)\n"
3689 "--\n"
3690 "\n"
3691 "Set the current process\'s user id.");
3692 
3693 #define OS_SETUID_METHODDEF    \
3694     {"setuid", (PyCFunction)os_setuid, METH_O, os_setuid__doc__},
3695 
3696 static PyObject *
3697 os_setuid_impl(PyObject *module, uid_t uid);
3698 
3699 static PyObject *
os_setuid(PyObject * module,PyObject * arg)3700 os_setuid(PyObject *module, PyObject *arg)
3701 {
3702     PyObject *return_value = NULL;
3703     uid_t uid;
3704 
3705     if (!_Py_Uid_Converter(arg, &uid)) {
3706         goto exit;
3707     }
3708     return_value = os_setuid_impl(module, uid);
3709 
3710 exit:
3711     return return_value;
3712 }
3713 
3714 #endif /* defined(HAVE_SETUID) */
3715 
3716 #if defined(HAVE_SETEUID)
3717 
3718 PyDoc_STRVAR(os_seteuid__doc__,
3719 "seteuid($module, euid, /)\n"
3720 "--\n"
3721 "\n"
3722 "Set the current process\'s effective user id.");
3723 
3724 #define OS_SETEUID_METHODDEF    \
3725     {"seteuid", (PyCFunction)os_seteuid, METH_O, os_seteuid__doc__},
3726 
3727 static PyObject *
3728 os_seteuid_impl(PyObject *module, uid_t euid);
3729 
3730 static PyObject *
os_seteuid(PyObject * module,PyObject * arg)3731 os_seteuid(PyObject *module, PyObject *arg)
3732 {
3733     PyObject *return_value = NULL;
3734     uid_t euid;
3735 
3736     if (!_Py_Uid_Converter(arg, &euid)) {
3737         goto exit;
3738     }
3739     return_value = os_seteuid_impl(module, euid);
3740 
3741 exit:
3742     return return_value;
3743 }
3744 
3745 #endif /* defined(HAVE_SETEUID) */
3746 
3747 #if defined(HAVE_SETEGID)
3748 
3749 PyDoc_STRVAR(os_setegid__doc__,
3750 "setegid($module, egid, /)\n"
3751 "--\n"
3752 "\n"
3753 "Set the current process\'s effective group id.");
3754 
3755 #define OS_SETEGID_METHODDEF    \
3756     {"setegid", (PyCFunction)os_setegid, METH_O, os_setegid__doc__},
3757 
3758 static PyObject *
3759 os_setegid_impl(PyObject *module, gid_t egid);
3760 
3761 static PyObject *
os_setegid(PyObject * module,PyObject * arg)3762 os_setegid(PyObject *module, PyObject *arg)
3763 {
3764     PyObject *return_value = NULL;
3765     gid_t egid;
3766 
3767     if (!_Py_Gid_Converter(arg, &egid)) {
3768         goto exit;
3769     }
3770     return_value = os_setegid_impl(module, egid);
3771 
3772 exit:
3773     return return_value;
3774 }
3775 
3776 #endif /* defined(HAVE_SETEGID) */
3777 
3778 #if defined(HAVE_SETREUID)
3779 
3780 PyDoc_STRVAR(os_setreuid__doc__,
3781 "setreuid($module, ruid, euid, /)\n"
3782 "--\n"
3783 "\n"
3784 "Set the current process\'s real and effective user ids.");
3785 
3786 #define OS_SETREUID_METHODDEF    \
3787     {"setreuid", (PyCFunction)(void(*)(void))os_setreuid, METH_FASTCALL, os_setreuid__doc__},
3788 
3789 static PyObject *
3790 os_setreuid_impl(PyObject *module, uid_t ruid, uid_t euid);
3791 
3792 static PyObject *
os_setreuid(PyObject * module,PyObject * const * args,Py_ssize_t nargs)3793 os_setreuid(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
3794 {
3795     PyObject *return_value = NULL;
3796     uid_t ruid;
3797     uid_t euid;
3798 
3799     if (!_PyArg_CheckPositional("setreuid", nargs, 2, 2)) {
3800         goto exit;
3801     }
3802     if (!_Py_Uid_Converter(args[0], &ruid)) {
3803         goto exit;
3804     }
3805     if (!_Py_Uid_Converter(args[1], &euid)) {
3806         goto exit;
3807     }
3808     return_value = os_setreuid_impl(module, ruid, euid);
3809 
3810 exit:
3811     return return_value;
3812 }
3813 
3814 #endif /* defined(HAVE_SETREUID) */
3815 
3816 #if defined(HAVE_SETREGID)
3817 
3818 PyDoc_STRVAR(os_setregid__doc__,
3819 "setregid($module, rgid, egid, /)\n"
3820 "--\n"
3821 "\n"
3822 "Set the current process\'s real and effective group ids.");
3823 
3824 #define OS_SETREGID_METHODDEF    \
3825     {"setregid", (PyCFunction)(void(*)(void))os_setregid, METH_FASTCALL, os_setregid__doc__},
3826 
3827 static PyObject *
3828 os_setregid_impl(PyObject *module, gid_t rgid, gid_t egid);
3829 
3830 static PyObject *
os_setregid(PyObject * module,PyObject * const * args,Py_ssize_t nargs)3831 os_setregid(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
3832 {
3833     PyObject *return_value = NULL;
3834     gid_t rgid;
3835     gid_t egid;
3836 
3837     if (!_PyArg_CheckPositional("setregid", nargs, 2, 2)) {
3838         goto exit;
3839     }
3840     if (!_Py_Gid_Converter(args[0], &rgid)) {
3841         goto exit;
3842     }
3843     if (!_Py_Gid_Converter(args[1], &egid)) {
3844         goto exit;
3845     }
3846     return_value = os_setregid_impl(module, rgid, egid);
3847 
3848 exit:
3849     return return_value;
3850 }
3851 
3852 #endif /* defined(HAVE_SETREGID) */
3853 
3854 #if defined(HAVE_SETGID)
3855 
3856 PyDoc_STRVAR(os_setgid__doc__,
3857 "setgid($module, gid, /)\n"
3858 "--\n"
3859 "\n"
3860 "Set the current process\'s group id.");
3861 
3862 #define OS_SETGID_METHODDEF    \
3863     {"setgid", (PyCFunction)os_setgid, METH_O, os_setgid__doc__},
3864 
3865 static PyObject *
3866 os_setgid_impl(PyObject *module, gid_t gid);
3867 
3868 static PyObject *
os_setgid(PyObject * module,PyObject * arg)3869 os_setgid(PyObject *module, PyObject *arg)
3870 {
3871     PyObject *return_value = NULL;
3872     gid_t gid;
3873 
3874     if (!_Py_Gid_Converter(arg, &gid)) {
3875         goto exit;
3876     }
3877     return_value = os_setgid_impl(module, gid);
3878 
3879 exit:
3880     return return_value;
3881 }
3882 
3883 #endif /* defined(HAVE_SETGID) */
3884 
3885 #if defined(HAVE_SETGROUPS)
3886 
3887 PyDoc_STRVAR(os_setgroups__doc__,
3888 "setgroups($module, groups, /)\n"
3889 "--\n"
3890 "\n"
3891 "Set the groups of the current process to list.");
3892 
3893 #define OS_SETGROUPS_METHODDEF    \
3894     {"setgroups", (PyCFunction)os_setgroups, METH_O, os_setgroups__doc__},
3895 
3896 #endif /* defined(HAVE_SETGROUPS) */
3897 
3898 #if defined(HAVE_WAIT3)
3899 
3900 PyDoc_STRVAR(os_wait3__doc__,
3901 "wait3($module, /, options)\n"
3902 "--\n"
3903 "\n"
3904 "Wait for completion of a child process.\n"
3905 "\n"
3906 "Returns a tuple of information about the child process:\n"
3907 "  (pid, status, rusage)");
3908 
3909 #define OS_WAIT3_METHODDEF    \
3910     {"wait3", (PyCFunction)(void(*)(void))os_wait3, METH_FASTCALL|METH_KEYWORDS, os_wait3__doc__},
3911 
3912 static PyObject *
3913 os_wait3_impl(PyObject *module, int options);
3914 
3915 static PyObject *
os_wait3(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)3916 os_wait3(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
3917 {
3918     PyObject *return_value = NULL;
3919     static const char * const _keywords[] = {"options", NULL};
3920     static _PyArg_Parser _parser = {NULL, _keywords, "wait3", 0};
3921     PyObject *argsbuf[1];
3922     int options;
3923 
3924     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
3925     if (!args) {
3926         goto exit;
3927     }
3928     options = _PyLong_AsInt(args[0]);
3929     if (options == -1 && PyErr_Occurred()) {
3930         goto exit;
3931     }
3932     return_value = os_wait3_impl(module, options);
3933 
3934 exit:
3935     return return_value;
3936 }
3937 
3938 #endif /* defined(HAVE_WAIT3) */
3939 
3940 #if defined(HAVE_WAIT4)
3941 
3942 PyDoc_STRVAR(os_wait4__doc__,
3943 "wait4($module, /, pid, options)\n"
3944 "--\n"
3945 "\n"
3946 "Wait for completion of a specific child process.\n"
3947 "\n"
3948 "Returns a tuple of information about the child process:\n"
3949 "  (pid, status, rusage)");
3950 
3951 #define OS_WAIT4_METHODDEF    \
3952     {"wait4", (PyCFunction)(void(*)(void))os_wait4, METH_FASTCALL|METH_KEYWORDS, os_wait4__doc__},
3953 
3954 static PyObject *
3955 os_wait4_impl(PyObject *module, pid_t pid, int options);
3956 
3957 static PyObject *
os_wait4(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)3958 os_wait4(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
3959 {
3960     PyObject *return_value = NULL;
3961     static const char * const _keywords[] = {"pid", "options", NULL};
3962     static _PyArg_Parser _parser = {"" _Py_PARSE_PID "i:wait4", _keywords, 0};
3963     pid_t pid;
3964     int options;
3965 
3966     if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
3967         &pid, &options)) {
3968         goto exit;
3969     }
3970     return_value = os_wait4_impl(module, pid, options);
3971 
3972 exit:
3973     return return_value;
3974 }
3975 
3976 #endif /* defined(HAVE_WAIT4) */
3977 
3978 #if (defined(HAVE_WAITID) && !defined(__APPLE__))
3979 
3980 PyDoc_STRVAR(os_waitid__doc__,
3981 "waitid($module, idtype, id, options, /)\n"
3982 "--\n"
3983 "\n"
3984 "Returns the result of waiting for a process or processes.\n"
3985 "\n"
3986 "  idtype\n"
3987 "    Must be one of be P_PID, P_PGID or P_ALL.\n"
3988 "  id\n"
3989 "    The id to wait on.\n"
3990 "  options\n"
3991 "    Constructed from the ORing of one or more of WEXITED, WSTOPPED\n"
3992 "    or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.\n"
3993 "\n"
3994 "Returns either waitid_result or None if WNOHANG is specified and there are\n"
3995 "no children in a waitable state.");
3996 
3997 #define OS_WAITID_METHODDEF    \
3998     {"waitid", (PyCFunction)(void(*)(void))os_waitid, METH_FASTCALL, os_waitid__doc__},
3999 
4000 static PyObject *
4001 os_waitid_impl(PyObject *module, idtype_t idtype, id_t id, int options);
4002 
4003 static PyObject *
os_waitid(PyObject * module,PyObject * const * args,Py_ssize_t nargs)4004 os_waitid(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
4005 {
4006     PyObject *return_value = NULL;
4007     idtype_t idtype;
4008     id_t id;
4009     int options;
4010 
4011     if (!_PyArg_ParseStack(args, nargs, "i" _Py_PARSE_PID "i:waitid",
4012         &idtype, &id, &options)) {
4013         goto exit;
4014     }
4015     return_value = os_waitid_impl(module, idtype, id, options);
4016 
4017 exit:
4018     return return_value;
4019 }
4020 
4021 #endif /* (defined(HAVE_WAITID) && !defined(__APPLE__)) */
4022 
4023 #if defined(HAVE_WAITPID)
4024 
4025 PyDoc_STRVAR(os_waitpid__doc__,
4026 "waitpid($module, pid, options, /)\n"
4027 "--\n"
4028 "\n"
4029 "Wait for completion of a given child process.\n"
4030 "\n"
4031 "Returns a tuple of information regarding the child process:\n"
4032 "    (pid, status)\n"
4033 "\n"
4034 "The options argument is ignored on Windows.");
4035 
4036 #define OS_WAITPID_METHODDEF    \
4037     {"waitpid", (PyCFunction)(void(*)(void))os_waitpid, METH_FASTCALL, os_waitpid__doc__},
4038 
4039 static PyObject *
4040 os_waitpid_impl(PyObject *module, pid_t pid, int options);
4041 
4042 static PyObject *
os_waitpid(PyObject * module,PyObject * const * args,Py_ssize_t nargs)4043 os_waitpid(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
4044 {
4045     PyObject *return_value = NULL;
4046     pid_t pid;
4047     int options;
4048 
4049     if (!_PyArg_ParseStack(args, nargs, "" _Py_PARSE_PID "i:waitpid",
4050         &pid, &options)) {
4051         goto exit;
4052     }
4053     return_value = os_waitpid_impl(module, pid, options);
4054 
4055 exit:
4056     return return_value;
4057 }
4058 
4059 #endif /* defined(HAVE_WAITPID) */
4060 
4061 #if !defined(HAVE_WAITPID) && defined(HAVE_CWAIT)
4062 
4063 PyDoc_STRVAR(os_waitpid__doc__,
4064 "waitpid($module, pid, options, /)\n"
4065 "--\n"
4066 "\n"
4067 "Wait for completion of a given process.\n"
4068 "\n"
4069 "Returns a tuple of information regarding the process:\n"
4070 "    (pid, status << 8)\n"
4071 "\n"
4072 "The options argument is ignored on Windows.");
4073 
4074 #define OS_WAITPID_METHODDEF    \
4075     {"waitpid", (PyCFunction)(void(*)(void))os_waitpid, METH_FASTCALL, os_waitpid__doc__},
4076 
4077 static PyObject *
4078 os_waitpid_impl(PyObject *module, intptr_t pid, int options);
4079 
4080 static PyObject *
os_waitpid(PyObject * module,PyObject * const * args,Py_ssize_t nargs)4081 os_waitpid(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
4082 {
4083     PyObject *return_value = NULL;
4084     intptr_t pid;
4085     int options;
4086 
4087     if (!_PyArg_ParseStack(args, nargs, "" _Py_PARSE_INTPTR "i:waitpid",
4088         &pid, &options)) {
4089         goto exit;
4090     }
4091     return_value = os_waitpid_impl(module, pid, options);
4092 
4093 exit:
4094     return return_value;
4095 }
4096 
4097 #endif /* !defined(HAVE_WAITPID) && defined(HAVE_CWAIT) */
4098 
4099 #if defined(HAVE_WAIT)
4100 
4101 PyDoc_STRVAR(os_wait__doc__,
4102 "wait($module, /)\n"
4103 "--\n"
4104 "\n"
4105 "Wait for completion of a child process.\n"
4106 "\n"
4107 "Returns a tuple of information about the child process:\n"
4108 "    (pid, status)");
4109 
4110 #define OS_WAIT_METHODDEF    \
4111     {"wait", (PyCFunction)os_wait, METH_NOARGS, os_wait__doc__},
4112 
4113 static PyObject *
4114 os_wait_impl(PyObject *module);
4115 
4116 static PyObject *
os_wait(PyObject * module,PyObject * Py_UNUSED (ignored))4117 os_wait(PyObject *module, PyObject *Py_UNUSED(ignored))
4118 {
4119     return os_wait_impl(module);
4120 }
4121 
4122 #endif /* defined(HAVE_WAIT) */
4123 
4124 #if (defined(__linux__) && defined(__NR_pidfd_open))
4125 
4126 PyDoc_STRVAR(os_pidfd_open__doc__,
4127 "pidfd_open($module, /, pid, flags=0)\n"
4128 "--\n"
4129 "\n"
4130 "Return a file descriptor referring to the process *pid*.\n"
4131 "\n"
4132 "The descriptor can be used to perform process management without races and\n"
4133 "signals.");
4134 
4135 #define OS_PIDFD_OPEN_METHODDEF    \
4136     {"pidfd_open", (PyCFunction)(void(*)(void))os_pidfd_open, METH_FASTCALL|METH_KEYWORDS, os_pidfd_open__doc__},
4137 
4138 static PyObject *
4139 os_pidfd_open_impl(PyObject *module, pid_t pid, unsigned int flags);
4140 
4141 static PyObject *
os_pidfd_open(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)4142 os_pidfd_open(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
4143 {
4144     PyObject *return_value = NULL;
4145     static const char * const _keywords[] = {"pid", "flags", NULL};
4146     static _PyArg_Parser _parser = {"" _Py_PARSE_PID "|O&:pidfd_open", _keywords, 0};
4147     pid_t pid;
4148     unsigned int flags = 0;
4149 
4150     if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
4151         &pid, _PyLong_UnsignedInt_Converter, &flags)) {
4152         goto exit;
4153     }
4154     return_value = os_pidfd_open_impl(module, pid, flags);
4155 
4156 exit:
4157     return return_value;
4158 }
4159 
4160 #endif /* (defined(__linux__) && defined(__NR_pidfd_open)) */
4161 
4162 #if (defined(HAVE_READLINK) || defined(MS_WINDOWS))
4163 
4164 PyDoc_STRVAR(os_readlink__doc__,
4165 "readlink($module, /, path, *, dir_fd=None)\n"
4166 "--\n"
4167 "\n"
4168 "Return a string representing the path to which the symbolic link points.\n"
4169 "\n"
4170 "If dir_fd is not None, it should be a file descriptor open to a directory,\n"
4171 "and path should be relative; path will then be relative to that directory.\n"
4172 "\n"
4173 "dir_fd may not be implemented on your platform.  If it is unavailable,\n"
4174 "using it will raise a NotImplementedError.");
4175 
4176 #define OS_READLINK_METHODDEF    \
4177     {"readlink", (PyCFunction)(void(*)(void))os_readlink, METH_FASTCALL|METH_KEYWORDS, os_readlink__doc__},
4178 
4179 static PyObject *
4180 os_readlink_impl(PyObject *module, path_t *path, int dir_fd);
4181 
4182 static PyObject *
os_readlink(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)4183 os_readlink(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
4184 {
4185     PyObject *return_value = NULL;
4186     static const char * const _keywords[] = {"path", "dir_fd", NULL};
4187     static _PyArg_Parser _parser = {NULL, _keywords, "readlink", 0};
4188     PyObject *argsbuf[2];
4189     Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
4190     path_t path = PATH_T_INITIALIZE("readlink", "path", 0, 0);
4191     int dir_fd = DEFAULT_DIR_FD;
4192 
4193     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
4194     if (!args) {
4195         goto exit;
4196     }
4197     if (!path_converter(args[0], &path)) {
4198         goto exit;
4199     }
4200     if (!noptargs) {
4201         goto skip_optional_kwonly;
4202     }
4203     if (!READLINKAT_DIR_FD_CONVERTER(args[1], &dir_fd)) {
4204         goto exit;
4205     }
4206 skip_optional_kwonly:
4207     return_value = os_readlink_impl(module, &path, dir_fd);
4208 
4209 exit:
4210     /* Cleanup for path */
4211     path_cleanup(&path);
4212 
4213     return return_value;
4214 }
4215 
4216 #endif /* (defined(HAVE_READLINK) || defined(MS_WINDOWS)) */
4217 
4218 #if defined(HAVE_SYMLINK)
4219 
4220 PyDoc_STRVAR(os_symlink__doc__,
4221 "symlink($module, /, src, dst, target_is_directory=False, *, dir_fd=None)\n"
4222 "--\n"
4223 "\n"
4224 "Create a symbolic link pointing to src named dst.\n"
4225 "\n"
4226 "target_is_directory is required on Windows if the target is to be\n"
4227 "  interpreted as a directory.  (On Windows, symlink requires\n"
4228 "  Windows 6.0 or greater, and raises a NotImplementedError otherwise.)\n"
4229 "  target_is_directory is ignored on non-Windows platforms.\n"
4230 "\n"
4231 "If dir_fd is not None, it should be a file descriptor open to a directory,\n"
4232 "  and path should be relative; path will then be relative to that directory.\n"
4233 "dir_fd may not be implemented on your platform.\n"
4234 "  If it is unavailable, using it will raise a NotImplementedError.");
4235 
4236 #define OS_SYMLINK_METHODDEF    \
4237     {"symlink", (PyCFunction)(void(*)(void))os_symlink, METH_FASTCALL|METH_KEYWORDS, os_symlink__doc__},
4238 
4239 static PyObject *
4240 os_symlink_impl(PyObject *module, path_t *src, path_t *dst,
4241                 int target_is_directory, int dir_fd);
4242 
4243 static PyObject *
os_symlink(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)4244 os_symlink(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
4245 {
4246     PyObject *return_value = NULL;
4247     static const char * const _keywords[] = {"src", "dst", "target_is_directory", "dir_fd", NULL};
4248     static _PyArg_Parser _parser = {NULL, _keywords, "symlink", 0};
4249     PyObject *argsbuf[4];
4250     Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
4251     path_t src = PATH_T_INITIALIZE("symlink", "src", 0, 0);
4252     path_t dst = PATH_T_INITIALIZE("symlink", "dst", 0, 0);
4253     int target_is_directory = 0;
4254     int dir_fd = DEFAULT_DIR_FD;
4255 
4256     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 3, 0, argsbuf);
4257     if (!args) {
4258         goto exit;
4259     }
4260     if (!path_converter(args[0], &src)) {
4261         goto exit;
4262     }
4263     if (!path_converter(args[1], &dst)) {
4264         goto exit;
4265     }
4266     if (!noptargs) {
4267         goto skip_optional_pos;
4268     }
4269     if (args[2]) {
4270         target_is_directory = PyObject_IsTrue(args[2]);
4271         if (target_is_directory < 0) {
4272             goto exit;
4273         }
4274         if (!--noptargs) {
4275             goto skip_optional_pos;
4276         }
4277     }
4278 skip_optional_pos:
4279     if (!noptargs) {
4280         goto skip_optional_kwonly;
4281     }
4282     if (!SYMLINKAT_DIR_FD_CONVERTER(args[3], &dir_fd)) {
4283         goto exit;
4284     }
4285 skip_optional_kwonly:
4286     return_value = os_symlink_impl(module, &src, &dst, target_is_directory, dir_fd);
4287 
4288 exit:
4289     /* Cleanup for src */
4290     path_cleanup(&src);
4291     /* Cleanup for dst */
4292     path_cleanup(&dst);
4293 
4294     return return_value;
4295 }
4296 
4297 #endif /* defined(HAVE_SYMLINK) */
4298 
4299 #if defined(HAVE_TIMES)
4300 
4301 PyDoc_STRVAR(os_times__doc__,
4302 "times($module, /)\n"
4303 "--\n"
4304 "\n"
4305 "Return a collection containing process timing information.\n"
4306 "\n"
4307 "The object returned behaves like a named tuple with these fields:\n"
4308 "  (utime, stime, cutime, cstime, elapsed_time)\n"
4309 "All fields are floating point numbers.");
4310 
4311 #define OS_TIMES_METHODDEF    \
4312     {"times", (PyCFunction)os_times, METH_NOARGS, os_times__doc__},
4313 
4314 static PyObject *
4315 os_times_impl(PyObject *module);
4316 
4317 static PyObject *
os_times(PyObject * module,PyObject * Py_UNUSED (ignored))4318 os_times(PyObject *module, PyObject *Py_UNUSED(ignored))
4319 {
4320     return os_times_impl(module);
4321 }
4322 
4323 #endif /* defined(HAVE_TIMES) */
4324 
4325 #if defined(HAVE_GETSID)
4326 
4327 PyDoc_STRVAR(os_getsid__doc__,
4328 "getsid($module, pid, /)\n"
4329 "--\n"
4330 "\n"
4331 "Call the system call getsid(pid) and return the result.");
4332 
4333 #define OS_GETSID_METHODDEF    \
4334     {"getsid", (PyCFunction)os_getsid, METH_O, os_getsid__doc__},
4335 
4336 static PyObject *
4337 os_getsid_impl(PyObject *module, pid_t pid);
4338 
4339 static PyObject *
os_getsid(PyObject * module,PyObject * arg)4340 os_getsid(PyObject *module, PyObject *arg)
4341 {
4342     PyObject *return_value = NULL;
4343     pid_t pid;
4344 
4345     if (!PyArg_Parse(arg, "" _Py_PARSE_PID ":getsid", &pid)) {
4346         goto exit;
4347     }
4348     return_value = os_getsid_impl(module, pid);
4349 
4350 exit:
4351     return return_value;
4352 }
4353 
4354 #endif /* defined(HAVE_GETSID) */
4355 
4356 #if defined(HAVE_SETSID)
4357 
4358 PyDoc_STRVAR(os_setsid__doc__,
4359 "setsid($module, /)\n"
4360 "--\n"
4361 "\n"
4362 "Call the system call setsid().");
4363 
4364 #define OS_SETSID_METHODDEF    \
4365     {"setsid", (PyCFunction)os_setsid, METH_NOARGS, os_setsid__doc__},
4366 
4367 static PyObject *
4368 os_setsid_impl(PyObject *module);
4369 
4370 static PyObject *
os_setsid(PyObject * module,PyObject * Py_UNUSED (ignored))4371 os_setsid(PyObject *module, PyObject *Py_UNUSED(ignored))
4372 {
4373     return os_setsid_impl(module);
4374 }
4375 
4376 #endif /* defined(HAVE_SETSID) */
4377 
4378 #if defined(HAVE_SETPGID)
4379 
4380 PyDoc_STRVAR(os_setpgid__doc__,
4381 "setpgid($module, pid, pgrp, /)\n"
4382 "--\n"
4383 "\n"
4384 "Call the system call setpgid(pid, pgrp).");
4385 
4386 #define OS_SETPGID_METHODDEF    \
4387     {"setpgid", (PyCFunction)(void(*)(void))os_setpgid, METH_FASTCALL, os_setpgid__doc__},
4388 
4389 static PyObject *
4390 os_setpgid_impl(PyObject *module, pid_t pid, pid_t pgrp);
4391 
4392 static PyObject *
os_setpgid(PyObject * module,PyObject * const * args,Py_ssize_t nargs)4393 os_setpgid(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
4394 {
4395     PyObject *return_value = NULL;
4396     pid_t pid;
4397     pid_t pgrp;
4398 
4399     if (!_PyArg_ParseStack(args, nargs, "" _Py_PARSE_PID "" _Py_PARSE_PID ":setpgid",
4400         &pid, &pgrp)) {
4401         goto exit;
4402     }
4403     return_value = os_setpgid_impl(module, pid, pgrp);
4404 
4405 exit:
4406     return return_value;
4407 }
4408 
4409 #endif /* defined(HAVE_SETPGID) */
4410 
4411 #if defined(HAVE_TCGETPGRP)
4412 
4413 PyDoc_STRVAR(os_tcgetpgrp__doc__,
4414 "tcgetpgrp($module, fd, /)\n"
4415 "--\n"
4416 "\n"
4417 "Return the process group associated with the terminal specified by fd.");
4418 
4419 #define OS_TCGETPGRP_METHODDEF    \
4420     {"tcgetpgrp", (PyCFunction)os_tcgetpgrp, METH_O, os_tcgetpgrp__doc__},
4421 
4422 static PyObject *
4423 os_tcgetpgrp_impl(PyObject *module, int fd);
4424 
4425 static PyObject *
os_tcgetpgrp(PyObject * module,PyObject * arg)4426 os_tcgetpgrp(PyObject *module, PyObject *arg)
4427 {
4428     PyObject *return_value = NULL;
4429     int fd;
4430 
4431     fd = _PyLong_AsInt(arg);
4432     if (fd == -1 && PyErr_Occurred()) {
4433         goto exit;
4434     }
4435     return_value = os_tcgetpgrp_impl(module, fd);
4436 
4437 exit:
4438     return return_value;
4439 }
4440 
4441 #endif /* defined(HAVE_TCGETPGRP) */
4442 
4443 #if defined(HAVE_TCSETPGRP)
4444 
4445 PyDoc_STRVAR(os_tcsetpgrp__doc__,
4446 "tcsetpgrp($module, fd, pgid, /)\n"
4447 "--\n"
4448 "\n"
4449 "Set the process group associated with the terminal specified by fd.");
4450 
4451 #define OS_TCSETPGRP_METHODDEF    \
4452     {"tcsetpgrp", (PyCFunction)(void(*)(void))os_tcsetpgrp, METH_FASTCALL, os_tcsetpgrp__doc__},
4453 
4454 static PyObject *
4455 os_tcsetpgrp_impl(PyObject *module, int fd, pid_t pgid);
4456 
4457 static PyObject *
os_tcsetpgrp(PyObject * module,PyObject * const * args,Py_ssize_t nargs)4458 os_tcsetpgrp(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
4459 {
4460     PyObject *return_value = NULL;
4461     int fd;
4462     pid_t pgid;
4463 
4464     if (!_PyArg_ParseStack(args, nargs, "i" _Py_PARSE_PID ":tcsetpgrp",
4465         &fd, &pgid)) {
4466         goto exit;
4467     }
4468     return_value = os_tcsetpgrp_impl(module, fd, pgid);
4469 
4470 exit:
4471     return return_value;
4472 }
4473 
4474 #endif /* defined(HAVE_TCSETPGRP) */
4475 
4476 PyDoc_STRVAR(os_open__doc__,
4477 "open($module, /, path, flags, mode=511, *, dir_fd=None)\n"
4478 "--\n"
4479 "\n"
4480 "Open a file for low level IO.  Returns a file descriptor (integer).\n"
4481 "\n"
4482 "If dir_fd is not None, it should be a file descriptor open to a directory,\n"
4483 "  and path should be relative; path will then be relative to that directory.\n"
4484 "dir_fd may not be implemented on your platform.\n"
4485 "  If it is unavailable, using it will raise a NotImplementedError.");
4486 
4487 #define OS_OPEN_METHODDEF    \
4488     {"open", (PyCFunction)(void(*)(void))os_open, METH_FASTCALL|METH_KEYWORDS, os_open__doc__},
4489 
4490 static int
4491 os_open_impl(PyObject *module, path_t *path, int flags, int mode, int dir_fd);
4492 
4493 static PyObject *
os_open(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)4494 os_open(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
4495 {
4496     PyObject *return_value = NULL;
4497     static const char * const _keywords[] = {"path", "flags", "mode", "dir_fd", NULL};
4498     static _PyArg_Parser _parser = {NULL, _keywords, "open", 0};
4499     PyObject *argsbuf[4];
4500     Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
4501     path_t path = PATH_T_INITIALIZE("open", "path", 0, 0);
4502     int flags;
4503     int mode = 511;
4504     int dir_fd = DEFAULT_DIR_FD;
4505     int _return_value;
4506 
4507     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 3, 0, argsbuf);
4508     if (!args) {
4509         goto exit;
4510     }
4511     if (!path_converter(args[0], &path)) {
4512         goto exit;
4513     }
4514     flags = _PyLong_AsInt(args[1]);
4515     if (flags == -1 && PyErr_Occurred()) {
4516         goto exit;
4517     }
4518     if (!noptargs) {
4519         goto skip_optional_pos;
4520     }
4521     if (args[2]) {
4522         mode = _PyLong_AsInt(args[2]);
4523         if (mode == -1 && PyErr_Occurred()) {
4524             goto exit;
4525         }
4526         if (!--noptargs) {
4527             goto skip_optional_pos;
4528         }
4529     }
4530 skip_optional_pos:
4531     if (!noptargs) {
4532         goto skip_optional_kwonly;
4533     }
4534     if (!OPENAT_DIR_FD_CONVERTER(args[3], &dir_fd)) {
4535         goto exit;
4536     }
4537 skip_optional_kwonly:
4538     _return_value = os_open_impl(module, &path, flags, mode, dir_fd);
4539     if ((_return_value == -1) && PyErr_Occurred()) {
4540         goto exit;
4541     }
4542     return_value = PyLong_FromLong((long)_return_value);
4543 
4544 exit:
4545     /* Cleanup for path */
4546     path_cleanup(&path);
4547 
4548     return return_value;
4549 }
4550 
4551 PyDoc_STRVAR(os_close__doc__,
4552 "close($module, /, fd)\n"
4553 "--\n"
4554 "\n"
4555 "Close a file descriptor.");
4556 
4557 #define OS_CLOSE_METHODDEF    \
4558     {"close", (PyCFunction)(void(*)(void))os_close, METH_FASTCALL|METH_KEYWORDS, os_close__doc__},
4559 
4560 static PyObject *
4561 os_close_impl(PyObject *module, int fd);
4562 
4563 static PyObject *
os_close(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)4564 os_close(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
4565 {
4566     PyObject *return_value = NULL;
4567     static const char * const _keywords[] = {"fd", NULL};
4568     static _PyArg_Parser _parser = {NULL, _keywords, "close", 0};
4569     PyObject *argsbuf[1];
4570     int fd;
4571 
4572     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
4573     if (!args) {
4574         goto exit;
4575     }
4576     fd = _PyLong_AsInt(args[0]);
4577     if (fd == -1 && PyErr_Occurred()) {
4578         goto exit;
4579     }
4580     return_value = os_close_impl(module, fd);
4581 
4582 exit:
4583     return return_value;
4584 }
4585 
4586 PyDoc_STRVAR(os_closerange__doc__,
4587 "closerange($module, fd_low, fd_high, /)\n"
4588 "--\n"
4589 "\n"
4590 "Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
4591 
4592 #define OS_CLOSERANGE_METHODDEF    \
4593     {"closerange", (PyCFunction)(void(*)(void))os_closerange, METH_FASTCALL, os_closerange__doc__},
4594 
4595 static PyObject *
4596 os_closerange_impl(PyObject *module, int fd_low, int fd_high);
4597 
4598 static PyObject *
os_closerange(PyObject * module,PyObject * const * args,Py_ssize_t nargs)4599 os_closerange(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
4600 {
4601     PyObject *return_value = NULL;
4602     int fd_low;
4603     int fd_high;
4604 
4605     if (!_PyArg_CheckPositional("closerange", nargs, 2, 2)) {
4606         goto exit;
4607     }
4608     fd_low = _PyLong_AsInt(args[0]);
4609     if (fd_low == -1 && PyErr_Occurred()) {
4610         goto exit;
4611     }
4612     fd_high = _PyLong_AsInt(args[1]);
4613     if (fd_high == -1 && PyErr_Occurred()) {
4614         goto exit;
4615     }
4616     return_value = os_closerange_impl(module, fd_low, fd_high);
4617 
4618 exit:
4619     return return_value;
4620 }
4621 
4622 PyDoc_STRVAR(os_dup__doc__,
4623 "dup($module, fd, /)\n"
4624 "--\n"
4625 "\n"
4626 "Return a duplicate of a file descriptor.");
4627 
4628 #define OS_DUP_METHODDEF    \
4629     {"dup", (PyCFunction)os_dup, METH_O, os_dup__doc__},
4630 
4631 static int
4632 os_dup_impl(PyObject *module, int fd);
4633 
4634 static PyObject *
os_dup(PyObject * module,PyObject * arg)4635 os_dup(PyObject *module, PyObject *arg)
4636 {
4637     PyObject *return_value = NULL;
4638     int fd;
4639     int _return_value;
4640 
4641     fd = _PyLong_AsInt(arg);
4642     if (fd == -1 && PyErr_Occurred()) {
4643         goto exit;
4644     }
4645     _return_value = os_dup_impl(module, fd);
4646     if ((_return_value == -1) && PyErr_Occurred()) {
4647         goto exit;
4648     }
4649     return_value = PyLong_FromLong((long)_return_value);
4650 
4651 exit:
4652     return return_value;
4653 }
4654 
4655 PyDoc_STRVAR(os_dup2__doc__,
4656 "dup2($module, /, fd, fd2, inheritable=True)\n"
4657 "--\n"
4658 "\n"
4659 "Duplicate file descriptor.");
4660 
4661 #define OS_DUP2_METHODDEF    \
4662     {"dup2", (PyCFunction)(void(*)(void))os_dup2, METH_FASTCALL|METH_KEYWORDS, os_dup2__doc__},
4663 
4664 static int
4665 os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable);
4666 
4667 static PyObject *
os_dup2(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)4668 os_dup2(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
4669 {
4670     PyObject *return_value = NULL;
4671     static const char * const _keywords[] = {"fd", "fd2", "inheritable", NULL};
4672     static _PyArg_Parser _parser = {NULL, _keywords, "dup2", 0};
4673     PyObject *argsbuf[3];
4674     Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
4675     int fd;
4676     int fd2;
4677     int inheritable = 1;
4678     int _return_value;
4679 
4680     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 3, 0, argsbuf);
4681     if (!args) {
4682         goto exit;
4683     }
4684     fd = _PyLong_AsInt(args[0]);
4685     if (fd == -1 && PyErr_Occurred()) {
4686         goto exit;
4687     }
4688     fd2 = _PyLong_AsInt(args[1]);
4689     if (fd2 == -1 && PyErr_Occurred()) {
4690         goto exit;
4691     }
4692     if (!noptargs) {
4693         goto skip_optional_pos;
4694     }
4695     inheritable = PyObject_IsTrue(args[2]);
4696     if (inheritable < 0) {
4697         goto exit;
4698     }
4699 skip_optional_pos:
4700     _return_value = os_dup2_impl(module, fd, fd2, inheritable);
4701     if ((_return_value == -1) && PyErr_Occurred()) {
4702         goto exit;
4703     }
4704     return_value = PyLong_FromLong((long)_return_value);
4705 
4706 exit:
4707     return return_value;
4708 }
4709 
4710 #if defined(HAVE_LOCKF)
4711 
4712 PyDoc_STRVAR(os_lockf__doc__,
4713 "lockf($module, fd, command, length, /)\n"
4714 "--\n"
4715 "\n"
4716 "Apply, test or remove a POSIX lock on an open file descriptor.\n"
4717 "\n"
4718 "  fd\n"
4719 "    An open file descriptor.\n"
4720 "  command\n"
4721 "    One of F_LOCK, F_TLOCK, F_ULOCK or F_TEST.\n"
4722 "  length\n"
4723 "    The number of bytes to lock, starting at the current position.");
4724 
4725 #define OS_LOCKF_METHODDEF    \
4726     {"lockf", (PyCFunction)(void(*)(void))os_lockf, METH_FASTCALL, os_lockf__doc__},
4727 
4728 static PyObject *
4729 os_lockf_impl(PyObject *module, int fd, int command, Py_off_t length);
4730 
4731 static PyObject *
os_lockf(PyObject * module,PyObject * const * args,Py_ssize_t nargs)4732 os_lockf(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
4733 {
4734     PyObject *return_value = NULL;
4735     int fd;
4736     int command;
4737     Py_off_t length;
4738 
4739     if (!_PyArg_CheckPositional("lockf", nargs, 3, 3)) {
4740         goto exit;
4741     }
4742     fd = _PyLong_AsInt(args[0]);
4743     if (fd == -1 && PyErr_Occurred()) {
4744         goto exit;
4745     }
4746     command = _PyLong_AsInt(args[1]);
4747     if (command == -1 && PyErr_Occurred()) {
4748         goto exit;
4749     }
4750     if (!Py_off_t_converter(args[2], &length)) {
4751         goto exit;
4752     }
4753     return_value = os_lockf_impl(module, fd, command, length);
4754 
4755 exit:
4756     return return_value;
4757 }
4758 
4759 #endif /* defined(HAVE_LOCKF) */
4760 
4761 PyDoc_STRVAR(os_lseek__doc__,
4762 "lseek($module, fd, position, how, /)\n"
4763 "--\n"
4764 "\n"
4765 "Set the position of a file descriptor.  Return the new position.\n"
4766 "\n"
4767 "Return the new cursor position in number of bytes\n"
4768 "relative to the beginning of the file.");
4769 
4770 #define OS_LSEEK_METHODDEF    \
4771     {"lseek", (PyCFunction)(void(*)(void))os_lseek, METH_FASTCALL, os_lseek__doc__},
4772 
4773 static Py_off_t
4774 os_lseek_impl(PyObject *module, int fd, Py_off_t position, int how);
4775 
4776 static PyObject *
os_lseek(PyObject * module,PyObject * const * args,Py_ssize_t nargs)4777 os_lseek(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
4778 {
4779     PyObject *return_value = NULL;
4780     int fd;
4781     Py_off_t position;
4782     int how;
4783     Py_off_t _return_value;
4784 
4785     if (!_PyArg_CheckPositional("lseek", nargs, 3, 3)) {
4786         goto exit;
4787     }
4788     fd = _PyLong_AsInt(args[0]);
4789     if (fd == -1 && PyErr_Occurred()) {
4790         goto exit;
4791     }
4792     if (!Py_off_t_converter(args[1], &position)) {
4793         goto exit;
4794     }
4795     how = _PyLong_AsInt(args[2]);
4796     if (how == -1 && PyErr_Occurred()) {
4797         goto exit;
4798     }
4799     _return_value = os_lseek_impl(module, fd, position, how);
4800     if ((_return_value == -1) && PyErr_Occurred()) {
4801         goto exit;
4802     }
4803     return_value = PyLong_FromPy_off_t(_return_value);
4804 
4805 exit:
4806     return return_value;
4807 }
4808 
4809 PyDoc_STRVAR(os_read__doc__,
4810 "read($module, fd, length, /)\n"
4811 "--\n"
4812 "\n"
4813 "Read from a file descriptor.  Returns a bytes object.");
4814 
4815 #define OS_READ_METHODDEF    \
4816     {"read", (PyCFunction)(void(*)(void))os_read, METH_FASTCALL, os_read__doc__},
4817 
4818 static PyObject *
4819 os_read_impl(PyObject *module, int fd, Py_ssize_t length);
4820 
4821 static PyObject *
os_read(PyObject * module,PyObject * const * args,Py_ssize_t nargs)4822 os_read(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
4823 {
4824     PyObject *return_value = NULL;
4825     int fd;
4826     Py_ssize_t length;
4827 
4828     if (!_PyArg_CheckPositional("read", nargs, 2, 2)) {
4829         goto exit;
4830     }
4831     fd = _PyLong_AsInt(args[0]);
4832     if (fd == -1 && PyErr_Occurred()) {
4833         goto exit;
4834     }
4835     {
4836         Py_ssize_t ival = -1;
4837         PyObject *iobj = _PyNumber_Index(args[1]);
4838         if (iobj != NULL) {
4839             ival = PyLong_AsSsize_t(iobj);
4840             Py_DECREF(iobj);
4841         }
4842         if (ival == -1 && PyErr_Occurred()) {
4843             goto exit;
4844         }
4845         length = ival;
4846     }
4847     return_value = os_read_impl(module, fd, length);
4848 
4849 exit:
4850     return return_value;
4851 }
4852 
4853 #if defined(HAVE_READV)
4854 
4855 PyDoc_STRVAR(os_readv__doc__,
4856 "readv($module, fd, buffers, /)\n"
4857 "--\n"
4858 "\n"
4859 "Read from a file descriptor fd into an iterable of buffers.\n"
4860 "\n"
4861 "The buffers should be mutable buffers accepting bytes.\n"
4862 "readv will transfer data into each buffer until it is full\n"
4863 "and then move on to the next buffer in the sequence to hold\n"
4864 "the rest of the data.\n"
4865 "\n"
4866 "readv returns the total number of bytes read,\n"
4867 "which may be less than the total capacity of all the buffers.");
4868 
4869 #define OS_READV_METHODDEF    \
4870     {"readv", (PyCFunction)(void(*)(void))os_readv, METH_FASTCALL, os_readv__doc__},
4871 
4872 static Py_ssize_t
4873 os_readv_impl(PyObject *module, int fd, PyObject *buffers);
4874 
4875 static PyObject *
os_readv(PyObject * module,PyObject * const * args,Py_ssize_t nargs)4876 os_readv(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
4877 {
4878     PyObject *return_value = NULL;
4879     int fd;
4880     PyObject *buffers;
4881     Py_ssize_t _return_value;
4882 
4883     if (!_PyArg_CheckPositional("readv", nargs, 2, 2)) {
4884         goto exit;
4885     }
4886     fd = _PyLong_AsInt(args[0]);
4887     if (fd == -1 && PyErr_Occurred()) {
4888         goto exit;
4889     }
4890     buffers = args[1];
4891     _return_value = os_readv_impl(module, fd, buffers);
4892     if ((_return_value == -1) && PyErr_Occurred()) {
4893         goto exit;
4894     }
4895     return_value = PyLong_FromSsize_t(_return_value);
4896 
4897 exit:
4898     return return_value;
4899 }
4900 
4901 #endif /* defined(HAVE_READV) */
4902 
4903 #if defined(HAVE_PREAD)
4904 
4905 PyDoc_STRVAR(os_pread__doc__,
4906 "pread($module, fd, length, offset, /)\n"
4907 "--\n"
4908 "\n"
4909 "Read a number of bytes from a file descriptor starting at a particular offset.\n"
4910 "\n"
4911 "Read length bytes from file descriptor fd, starting at offset bytes from\n"
4912 "the beginning of the file.  The file offset remains unchanged.");
4913 
4914 #define OS_PREAD_METHODDEF    \
4915     {"pread", (PyCFunction)(void(*)(void))os_pread, METH_FASTCALL, os_pread__doc__},
4916 
4917 static PyObject *
4918 os_pread_impl(PyObject *module, int fd, Py_ssize_t length, Py_off_t offset);
4919 
4920 static PyObject *
os_pread(PyObject * module,PyObject * const * args,Py_ssize_t nargs)4921 os_pread(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
4922 {
4923     PyObject *return_value = NULL;
4924     int fd;
4925     Py_ssize_t length;
4926     Py_off_t offset;
4927 
4928     if (!_PyArg_CheckPositional("pread", nargs, 3, 3)) {
4929         goto exit;
4930     }
4931     fd = _PyLong_AsInt(args[0]);
4932     if (fd == -1 && PyErr_Occurred()) {
4933         goto exit;
4934     }
4935     {
4936         Py_ssize_t ival = -1;
4937         PyObject *iobj = _PyNumber_Index(args[1]);
4938         if (iobj != NULL) {
4939             ival = PyLong_AsSsize_t(iobj);
4940             Py_DECREF(iobj);
4941         }
4942         if (ival == -1 && PyErr_Occurred()) {
4943             goto exit;
4944         }
4945         length = ival;
4946     }
4947     if (!Py_off_t_converter(args[2], &offset)) {
4948         goto exit;
4949     }
4950     return_value = os_pread_impl(module, fd, length, offset);
4951 
4952 exit:
4953     return return_value;
4954 }
4955 
4956 #endif /* defined(HAVE_PREAD) */
4957 
4958 #if (defined(HAVE_PREADV) || defined (HAVE_PREADV2))
4959 
4960 PyDoc_STRVAR(os_preadv__doc__,
4961 "preadv($module, fd, buffers, offset, flags=0, /)\n"
4962 "--\n"
4963 "\n"
4964 "Reads from a file descriptor into a number of mutable bytes-like objects.\n"
4965 "\n"
4966 "Combines the functionality of readv() and pread(). As readv(), it will\n"
4967 "transfer data into each buffer until it is full and then move on to the next\n"
4968 "buffer in the sequence to hold the rest of the data. Its fourth argument,\n"
4969 "specifies the file offset at which the input operation is to be performed. It\n"
4970 "will return the total number of bytes read (which can be less than the total\n"
4971 "capacity of all the objects).\n"
4972 "\n"
4973 "The flags argument contains a bitwise OR of zero or more of the following flags:\n"
4974 "\n"
4975 "- RWF_HIPRI\n"
4976 "- RWF_NOWAIT\n"
4977 "\n"
4978 "Using non-zero flags requires Linux 4.6 or newer.");
4979 
4980 #define OS_PREADV_METHODDEF    \
4981     {"preadv", (PyCFunction)(void(*)(void))os_preadv, METH_FASTCALL, os_preadv__doc__},
4982 
4983 static Py_ssize_t
4984 os_preadv_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset,
4985                int flags);
4986 
4987 static PyObject *
os_preadv(PyObject * module,PyObject * const * args,Py_ssize_t nargs)4988 os_preadv(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
4989 {
4990     PyObject *return_value = NULL;
4991     int fd;
4992     PyObject *buffers;
4993     Py_off_t offset;
4994     int flags = 0;
4995     Py_ssize_t _return_value;
4996 
4997     if (!_PyArg_CheckPositional("preadv", nargs, 3, 4)) {
4998         goto exit;
4999     }
5000     fd = _PyLong_AsInt(args[0]);
5001     if (fd == -1 && PyErr_Occurred()) {
5002         goto exit;
5003     }
5004     buffers = args[1];
5005     if (!Py_off_t_converter(args[2], &offset)) {
5006         goto exit;
5007     }
5008     if (nargs < 4) {
5009         goto skip_optional;
5010     }
5011     flags = _PyLong_AsInt(args[3]);
5012     if (flags == -1 && PyErr_Occurred()) {
5013         goto exit;
5014     }
5015 skip_optional:
5016     _return_value = os_preadv_impl(module, fd, buffers, offset, flags);
5017     if ((_return_value == -1) && PyErr_Occurred()) {
5018         goto exit;
5019     }
5020     return_value = PyLong_FromSsize_t(_return_value);
5021 
5022 exit:
5023     return return_value;
5024 }
5025 
5026 #endif /* (defined(HAVE_PREADV) || defined (HAVE_PREADV2)) */
5027 
5028 PyDoc_STRVAR(os_write__doc__,
5029 "write($module, fd, data, /)\n"
5030 "--\n"
5031 "\n"
5032 "Write a bytes object to a file descriptor.");
5033 
5034 #define OS_WRITE_METHODDEF    \
5035     {"write", (PyCFunction)(void(*)(void))os_write, METH_FASTCALL, os_write__doc__},
5036 
5037 static Py_ssize_t
5038 os_write_impl(PyObject *module, int fd, Py_buffer *data);
5039 
5040 static PyObject *
os_write(PyObject * module,PyObject * const * args,Py_ssize_t nargs)5041 os_write(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
5042 {
5043     PyObject *return_value = NULL;
5044     int fd;
5045     Py_buffer data = {NULL, NULL};
5046     Py_ssize_t _return_value;
5047 
5048     if (!_PyArg_CheckPositional("write", nargs, 2, 2)) {
5049         goto exit;
5050     }
5051     fd = _PyLong_AsInt(args[0]);
5052     if (fd == -1 && PyErr_Occurred()) {
5053         goto exit;
5054     }
5055     if (PyObject_GetBuffer(args[1], &data, PyBUF_SIMPLE) != 0) {
5056         goto exit;
5057     }
5058     if (!PyBuffer_IsContiguous(&data, 'C')) {
5059         _PyArg_BadArgument("write", "argument 2", "contiguous buffer", args[1]);
5060         goto exit;
5061     }
5062     _return_value = os_write_impl(module, fd, &data);
5063     if ((_return_value == -1) && PyErr_Occurred()) {
5064         goto exit;
5065     }
5066     return_value = PyLong_FromSsize_t(_return_value);
5067 
5068 exit:
5069     /* Cleanup for data */
5070     if (data.obj) {
5071        PyBuffer_Release(&data);
5072     }
5073 
5074     return return_value;
5075 }
5076 
5077 #if defined(HAVE_SENDFILE) && defined(__APPLE__)
5078 
5079 PyDoc_STRVAR(os_sendfile__doc__,
5080 "sendfile($module, /, out_fd, in_fd, offset, count, headers=(),\n"
5081 "         trailers=(), flags=0)\n"
5082 "--\n"
5083 "\n"
5084 "Copy count bytes from file descriptor in_fd to file descriptor out_fd.");
5085 
5086 #define OS_SENDFILE_METHODDEF    \
5087     {"sendfile", (PyCFunction)(void(*)(void))os_sendfile, METH_FASTCALL|METH_KEYWORDS, os_sendfile__doc__},
5088 
5089 static PyObject *
5090 os_sendfile_impl(PyObject *module, int out_fd, int in_fd, Py_off_t offset,
5091                  Py_off_t sbytes, PyObject *headers, PyObject *trailers,
5092                  int flags);
5093 
5094 static PyObject *
os_sendfile(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)5095 os_sendfile(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
5096 {
5097     PyObject *return_value = NULL;
5098     static const char * const _keywords[] = {"out_fd", "in_fd", "offset", "count", "headers", "trailers", "flags", NULL};
5099     static _PyArg_Parser _parser = {NULL, _keywords, "sendfile", 0};
5100     PyObject *argsbuf[7];
5101     Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 4;
5102     int out_fd;
5103     int in_fd;
5104     Py_off_t offset;
5105     Py_off_t sbytes;
5106     PyObject *headers = NULL;
5107     PyObject *trailers = NULL;
5108     int flags = 0;
5109 
5110     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 4, 7, 0, argsbuf);
5111     if (!args) {
5112         goto exit;
5113     }
5114     out_fd = _PyLong_AsInt(args[0]);
5115     if (out_fd == -1 && PyErr_Occurred()) {
5116         goto exit;
5117     }
5118     in_fd = _PyLong_AsInt(args[1]);
5119     if (in_fd == -1 && PyErr_Occurred()) {
5120         goto exit;
5121     }
5122     if (!Py_off_t_converter(args[2], &offset)) {
5123         goto exit;
5124     }
5125     if (!Py_off_t_converter(args[3], &sbytes)) {
5126         goto exit;
5127     }
5128     if (!noptargs) {
5129         goto skip_optional_pos;
5130     }
5131     if (args[4]) {
5132         headers = args[4];
5133         if (!--noptargs) {
5134             goto skip_optional_pos;
5135         }
5136     }
5137     if (args[5]) {
5138         trailers = args[5];
5139         if (!--noptargs) {
5140             goto skip_optional_pos;
5141         }
5142     }
5143     flags = _PyLong_AsInt(args[6]);
5144     if (flags == -1 && PyErr_Occurred()) {
5145         goto exit;
5146     }
5147 skip_optional_pos:
5148     return_value = os_sendfile_impl(module, out_fd, in_fd, offset, sbytes, headers, trailers, flags);
5149 
5150 exit:
5151     return return_value;
5152 }
5153 
5154 #endif /* defined(HAVE_SENDFILE) && defined(__APPLE__) */
5155 
5156 #if defined(HAVE_SENDFILE) && !defined(__APPLE__) && (defined(__FreeBSD__) || defined(__DragonFly__))
5157 
5158 PyDoc_STRVAR(os_sendfile__doc__,
5159 "sendfile($module, /, out_fd, in_fd, offset, count, headers=(),\n"
5160 "         trailers=(), flags=0)\n"
5161 "--\n"
5162 "\n"
5163 "Copy count bytes from file descriptor in_fd to file descriptor out_fd.");
5164 
5165 #define OS_SENDFILE_METHODDEF    \
5166     {"sendfile", (PyCFunction)(void(*)(void))os_sendfile, METH_FASTCALL|METH_KEYWORDS, os_sendfile__doc__},
5167 
5168 static PyObject *
5169 os_sendfile_impl(PyObject *module, int out_fd, int in_fd, Py_off_t offset,
5170                  Py_ssize_t count, PyObject *headers, PyObject *trailers,
5171                  int flags);
5172 
5173 static PyObject *
os_sendfile(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)5174 os_sendfile(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
5175 {
5176     PyObject *return_value = NULL;
5177     static const char * const _keywords[] = {"out_fd", "in_fd", "offset", "count", "headers", "trailers", "flags", NULL};
5178     static _PyArg_Parser _parser = {NULL, _keywords, "sendfile", 0};
5179     PyObject *argsbuf[7];
5180     Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 4;
5181     int out_fd;
5182     int in_fd;
5183     Py_off_t offset;
5184     Py_ssize_t count;
5185     PyObject *headers = NULL;
5186     PyObject *trailers = NULL;
5187     int flags = 0;
5188 
5189     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 4, 7, 0, argsbuf);
5190     if (!args) {
5191         goto exit;
5192     }
5193     out_fd = _PyLong_AsInt(args[0]);
5194     if (out_fd == -1 && PyErr_Occurred()) {
5195         goto exit;
5196     }
5197     in_fd = _PyLong_AsInt(args[1]);
5198     if (in_fd == -1 && PyErr_Occurred()) {
5199         goto exit;
5200     }
5201     if (!Py_off_t_converter(args[2], &offset)) {
5202         goto exit;
5203     }
5204     {
5205         Py_ssize_t ival = -1;
5206         PyObject *iobj = _PyNumber_Index(args[3]);
5207         if (iobj != NULL) {
5208             ival = PyLong_AsSsize_t(iobj);
5209             Py_DECREF(iobj);
5210         }
5211         if (ival == -1 && PyErr_Occurred()) {
5212             goto exit;
5213         }
5214         count = ival;
5215     }
5216     if (!noptargs) {
5217         goto skip_optional_pos;
5218     }
5219     if (args[4]) {
5220         headers = args[4];
5221         if (!--noptargs) {
5222             goto skip_optional_pos;
5223         }
5224     }
5225     if (args[5]) {
5226         trailers = args[5];
5227         if (!--noptargs) {
5228             goto skip_optional_pos;
5229         }
5230     }
5231     flags = _PyLong_AsInt(args[6]);
5232     if (flags == -1 && PyErr_Occurred()) {
5233         goto exit;
5234     }
5235 skip_optional_pos:
5236     return_value = os_sendfile_impl(module, out_fd, in_fd, offset, count, headers, trailers, flags);
5237 
5238 exit:
5239     return return_value;
5240 }
5241 
5242 #endif /* defined(HAVE_SENDFILE) && !defined(__APPLE__) && (defined(__FreeBSD__) || defined(__DragonFly__)) */
5243 
5244 #if defined(HAVE_SENDFILE) && !defined(__APPLE__) && !(defined(__FreeBSD__) || defined(__DragonFly__))
5245 
5246 PyDoc_STRVAR(os_sendfile__doc__,
5247 "sendfile($module, /, out_fd, in_fd, offset, count)\n"
5248 "--\n"
5249 "\n"
5250 "Copy count bytes from file descriptor in_fd to file descriptor out_fd.");
5251 
5252 #define OS_SENDFILE_METHODDEF    \
5253     {"sendfile", (PyCFunction)(void(*)(void))os_sendfile, METH_FASTCALL|METH_KEYWORDS, os_sendfile__doc__},
5254 
5255 static PyObject *
5256 os_sendfile_impl(PyObject *module, int out_fd, int in_fd, PyObject *offobj,
5257                  Py_ssize_t count);
5258 
5259 static PyObject *
os_sendfile(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)5260 os_sendfile(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
5261 {
5262     PyObject *return_value = NULL;
5263     static const char * const _keywords[] = {"out_fd", "in_fd", "offset", "count", NULL};
5264     static _PyArg_Parser _parser = {NULL, _keywords, "sendfile", 0};
5265     PyObject *argsbuf[4];
5266     int out_fd;
5267     int in_fd;
5268     PyObject *offobj;
5269     Py_ssize_t count;
5270 
5271     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 4, 4, 0, argsbuf);
5272     if (!args) {
5273         goto exit;
5274     }
5275     out_fd = _PyLong_AsInt(args[0]);
5276     if (out_fd == -1 && PyErr_Occurred()) {
5277         goto exit;
5278     }
5279     in_fd = _PyLong_AsInt(args[1]);
5280     if (in_fd == -1 && PyErr_Occurred()) {
5281         goto exit;
5282     }
5283     offobj = args[2];
5284     {
5285         Py_ssize_t ival = -1;
5286         PyObject *iobj = _PyNumber_Index(args[3]);
5287         if (iobj != NULL) {
5288             ival = PyLong_AsSsize_t(iobj);
5289             Py_DECREF(iobj);
5290         }
5291         if (ival == -1 && PyErr_Occurred()) {
5292             goto exit;
5293         }
5294         count = ival;
5295     }
5296     return_value = os_sendfile_impl(module, out_fd, in_fd, offobj, count);
5297 
5298 exit:
5299     return return_value;
5300 }
5301 
5302 #endif /* defined(HAVE_SENDFILE) && !defined(__APPLE__) && !(defined(__FreeBSD__) || defined(__DragonFly__)) */
5303 
5304 #if defined(__APPLE__)
5305 
5306 PyDoc_STRVAR(os__fcopyfile__doc__,
5307 "_fcopyfile($module, in_fd, out_fd, flags, /)\n"
5308 "--\n"
5309 "\n"
5310 "Efficiently copy content or metadata of 2 regular file descriptors (macOS).");
5311 
5312 #define OS__FCOPYFILE_METHODDEF    \
5313     {"_fcopyfile", (PyCFunction)(void(*)(void))os__fcopyfile, METH_FASTCALL, os__fcopyfile__doc__},
5314 
5315 static PyObject *
5316 os__fcopyfile_impl(PyObject *module, int in_fd, int out_fd, int flags);
5317 
5318 static PyObject *
os__fcopyfile(PyObject * module,PyObject * const * args,Py_ssize_t nargs)5319 os__fcopyfile(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
5320 {
5321     PyObject *return_value = NULL;
5322     int in_fd;
5323     int out_fd;
5324     int flags;
5325 
5326     if (!_PyArg_CheckPositional("_fcopyfile", nargs, 3, 3)) {
5327         goto exit;
5328     }
5329     in_fd = _PyLong_AsInt(args[0]);
5330     if (in_fd == -1 && PyErr_Occurred()) {
5331         goto exit;
5332     }
5333     out_fd = _PyLong_AsInt(args[1]);
5334     if (out_fd == -1 && PyErr_Occurred()) {
5335         goto exit;
5336     }
5337     flags = _PyLong_AsInt(args[2]);
5338     if (flags == -1 && PyErr_Occurred()) {
5339         goto exit;
5340     }
5341     return_value = os__fcopyfile_impl(module, in_fd, out_fd, flags);
5342 
5343 exit:
5344     return return_value;
5345 }
5346 
5347 #endif /* defined(__APPLE__) */
5348 
5349 PyDoc_STRVAR(os_fstat__doc__,
5350 "fstat($module, /, fd)\n"
5351 "--\n"
5352 "\n"
5353 "Perform a stat system call on the given file descriptor.\n"
5354 "\n"
5355 "Like stat(), but for an open file descriptor.\n"
5356 "Equivalent to os.stat(fd).");
5357 
5358 #define OS_FSTAT_METHODDEF    \
5359     {"fstat", (PyCFunction)(void(*)(void))os_fstat, METH_FASTCALL|METH_KEYWORDS, os_fstat__doc__},
5360 
5361 static PyObject *
5362 os_fstat_impl(PyObject *module, int fd);
5363 
5364 static PyObject *
os_fstat(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)5365 os_fstat(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
5366 {
5367     PyObject *return_value = NULL;
5368     static const char * const _keywords[] = {"fd", NULL};
5369     static _PyArg_Parser _parser = {NULL, _keywords, "fstat", 0};
5370     PyObject *argsbuf[1];
5371     int fd;
5372 
5373     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
5374     if (!args) {
5375         goto exit;
5376     }
5377     fd = _PyLong_AsInt(args[0]);
5378     if (fd == -1 && PyErr_Occurred()) {
5379         goto exit;
5380     }
5381     return_value = os_fstat_impl(module, fd);
5382 
5383 exit:
5384     return return_value;
5385 }
5386 
5387 PyDoc_STRVAR(os_isatty__doc__,
5388 "isatty($module, fd, /)\n"
5389 "--\n"
5390 "\n"
5391 "Return True if the fd is connected to a terminal.\n"
5392 "\n"
5393 "Return True if the file descriptor is an open file descriptor\n"
5394 "connected to the slave end of a terminal.");
5395 
5396 #define OS_ISATTY_METHODDEF    \
5397     {"isatty", (PyCFunction)os_isatty, METH_O, os_isatty__doc__},
5398 
5399 static int
5400 os_isatty_impl(PyObject *module, int fd);
5401 
5402 static PyObject *
os_isatty(PyObject * module,PyObject * arg)5403 os_isatty(PyObject *module, PyObject *arg)
5404 {
5405     PyObject *return_value = NULL;
5406     int fd;
5407     int _return_value;
5408 
5409     fd = _PyLong_AsInt(arg);
5410     if (fd == -1 && PyErr_Occurred()) {
5411         goto exit;
5412     }
5413     _return_value = os_isatty_impl(module, fd);
5414     if ((_return_value == -1) && PyErr_Occurred()) {
5415         goto exit;
5416     }
5417     return_value = PyBool_FromLong((long)_return_value);
5418 
5419 exit:
5420     return return_value;
5421 }
5422 
5423 #if defined(HAVE_PIPE)
5424 
5425 PyDoc_STRVAR(os_pipe__doc__,
5426 "pipe($module, /)\n"
5427 "--\n"
5428 "\n"
5429 "Create a pipe.\n"
5430 "\n"
5431 "Returns a tuple of two file descriptors:\n"
5432 "  (read_fd, write_fd)");
5433 
5434 #define OS_PIPE_METHODDEF    \
5435     {"pipe", (PyCFunction)os_pipe, METH_NOARGS, os_pipe__doc__},
5436 
5437 static PyObject *
5438 os_pipe_impl(PyObject *module);
5439 
5440 static PyObject *
os_pipe(PyObject * module,PyObject * Py_UNUSED (ignored))5441 os_pipe(PyObject *module, PyObject *Py_UNUSED(ignored))
5442 {
5443     return os_pipe_impl(module);
5444 }
5445 
5446 #endif /* defined(HAVE_PIPE) */
5447 
5448 #if defined(HAVE_PIPE2)
5449 
5450 PyDoc_STRVAR(os_pipe2__doc__,
5451 "pipe2($module, flags, /)\n"
5452 "--\n"
5453 "\n"
5454 "Create a pipe with flags set atomically.\n"
5455 "\n"
5456 "Returns a tuple of two file descriptors:\n"
5457 "  (read_fd, write_fd)\n"
5458 "\n"
5459 "flags can be constructed by ORing together one or more of these values:\n"
5460 "O_NONBLOCK, O_CLOEXEC.");
5461 
5462 #define OS_PIPE2_METHODDEF    \
5463     {"pipe2", (PyCFunction)os_pipe2, METH_O, os_pipe2__doc__},
5464 
5465 static PyObject *
5466 os_pipe2_impl(PyObject *module, int flags);
5467 
5468 static PyObject *
os_pipe2(PyObject * module,PyObject * arg)5469 os_pipe2(PyObject *module, PyObject *arg)
5470 {
5471     PyObject *return_value = NULL;
5472     int flags;
5473 
5474     flags = _PyLong_AsInt(arg);
5475     if (flags == -1 && PyErr_Occurred()) {
5476         goto exit;
5477     }
5478     return_value = os_pipe2_impl(module, flags);
5479 
5480 exit:
5481     return return_value;
5482 }
5483 
5484 #endif /* defined(HAVE_PIPE2) */
5485 
5486 #if defined(HAVE_WRITEV)
5487 
5488 PyDoc_STRVAR(os_writev__doc__,
5489 "writev($module, fd, buffers, /)\n"
5490 "--\n"
5491 "\n"
5492 "Iterate over buffers, and write the contents of each to a file descriptor.\n"
5493 "\n"
5494 "Returns the total number of bytes written.\n"
5495 "buffers must be a sequence of bytes-like objects.");
5496 
5497 #define OS_WRITEV_METHODDEF    \
5498     {"writev", (PyCFunction)(void(*)(void))os_writev, METH_FASTCALL, os_writev__doc__},
5499 
5500 static Py_ssize_t
5501 os_writev_impl(PyObject *module, int fd, PyObject *buffers);
5502 
5503 static PyObject *
os_writev(PyObject * module,PyObject * const * args,Py_ssize_t nargs)5504 os_writev(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
5505 {
5506     PyObject *return_value = NULL;
5507     int fd;
5508     PyObject *buffers;
5509     Py_ssize_t _return_value;
5510 
5511     if (!_PyArg_CheckPositional("writev", nargs, 2, 2)) {
5512         goto exit;
5513     }
5514     fd = _PyLong_AsInt(args[0]);
5515     if (fd == -1 && PyErr_Occurred()) {
5516         goto exit;
5517     }
5518     buffers = args[1];
5519     _return_value = os_writev_impl(module, fd, buffers);
5520     if ((_return_value == -1) && PyErr_Occurred()) {
5521         goto exit;
5522     }
5523     return_value = PyLong_FromSsize_t(_return_value);
5524 
5525 exit:
5526     return return_value;
5527 }
5528 
5529 #endif /* defined(HAVE_WRITEV) */
5530 
5531 #if defined(HAVE_PWRITE)
5532 
5533 PyDoc_STRVAR(os_pwrite__doc__,
5534 "pwrite($module, fd, buffer, offset, /)\n"
5535 "--\n"
5536 "\n"
5537 "Write bytes to a file descriptor starting at a particular offset.\n"
5538 "\n"
5539 "Write buffer to fd, starting at offset bytes from the beginning of\n"
5540 "the file.  Returns the number of bytes writte.  Does not change the\n"
5541 "current file offset.");
5542 
5543 #define OS_PWRITE_METHODDEF    \
5544     {"pwrite", (PyCFunction)(void(*)(void))os_pwrite, METH_FASTCALL, os_pwrite__doc__},
5545 
5546 static Py_ssize_t
5547 os_pwrite_impl(PyObject *module, int fd, Py_buffer *buffer, Py_off_t offset);
5548 
5549 static PyObject *
os_pwrite(PyObject * module,PyObject * const * args,Py_ssize_t nargs)5550 os_pwrite(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
5551 {
5552     PyObject *return_value = NULL;
5553     int fd;
5554     Py_buffer buffer = {NULL, NULL};
5555     Py_off_t offset;
5556     Py_ssize_t _return_value;
5557 
5558     if (!_PyArg_CheckPositional("pwrite", nargs, 3, 3)) {
5559         goto exit;
5560     }
5561     fd = _PyLong_AsInt(args[0]);
5562     if (fd == -1 && PyErr_Occurred()) {
5563         goto exit;
5564     }
5565     if (PyObject_GetBuffer(args[1], &buffer, PyBUF_SIMPLE) != 0) {
5566         goto exit;
5567     }
5568     if (!PyBuffer_IsContiguous(&buffer, 'C')) {
5569         _PyArg_BadArgument("pwrite", "argument 2", "contiguous buffer", args[1]);
5570         goto exit;
5571     }
5572     if (!Py_off_t_converter(args[2], &offset)) {
5573         goto exit;
5574     }
5575     _return_value = os_pwrite_impl(module, fd, &buffer, offset);
5576     if ((_return_value == -1) && PyErr_Occurred()) {
5577         goto exit;
5578     }
5579     return_value = PyLong_FromSsize_t(_return_value);
5580 
5581 exit:
5582     /* Cleanup for buffer */
5583     if (buffer.obj) {
5584        PyBuffer_Release(&buffer);
5585     }
5586 
5587     return return_value;
5588 }
5589 
5590 #endif /* defined(HAVE_PWRITE) */
5591 
5592 #if (defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2))
5593 
5594 PyDoc_STRVAR(os_pwritev__doc__,
5595 "pwritev($module, fd, buffers, offset, flags=0, /)\n"
5596 "--\n"
5597 "\n"
5598 "Writes the contents of bytes-like objects to a file descriptor at a given offset.\n"
5599 "\n"
5600 "Combines the functionality of writev() and pwrite(). All buffers must be a sequence\n"
5601 "of bytes-like objects. Buffers are processed in array order. Entire contents of first\n"
5602 "buffer is written before proceeding to second, and so on. The operating system may\n"
5603 "set a limit (sysconf() value SC_IOV_MAX) on the number of buffers that can be used.\n"
5604 "This function writes the contents of each object to the file descriptor and returns\n"
5605 "the total number of bytes written.\n"
5606 "\n"
5607 "The flags argument contains a bitwise OR of zero or more of the following flags:\n"
5608 "\n"
5609 "- RWF_DSYNC\n"
5610 "- RWF_SYNC\n"
5611 "- RWF_APPEND\n"
5612 "\n"
5613 "Using non-zero flags requires Linux 4.7 or newer.");
5614 
5615 #define OS_PWRITEV_METHODDEF    \
5616     {"pwritev", (PyCFunction)(void(*)(void))os_pwritev, METH_FASTCALL, os_pwritev__doc__},
5617 
5618 static Py_ssize_t
5619 os_pwritev_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset,
5620                 int flags);
5621 
5622 static PyObject *
os_pwritev(PyObject * module,PyObject * const * args,Py_ssize_t nargs)5623 os_pwritev(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
5624 {
5625     PyObject *return_value = NULL;
5626     int fd;
5627     PyObject *buffers;
5628     Py_off_t offset;
5629     int flags = 0;
5630     Py_ssize_t _return_value;
5631 
5632     if (!_PyArg_CheckPositional("pwritev", nargs, 3, 4)) {
5633         goto exit;
5634     }
5635     fd = _PyLong_AsInt(args[0]);
5636     if (fd == -1 && PyErr_Occurred()) {
5637         goto exit;
5638     }
5639     buffers = args[1];
5640     if (!Py_off_t_converter(args[2], &offset)) {
5641         goto exit;
5642     }
5643     if (nargs < 4) {
5644         goto skip_optional;
5645     }
5646     flags = _PyLong_AsInt(args[3]);
5647     if (flags == -1 && PyErr_Occurred()) {
5648         goto exit;
5649     }
5650 skip_optional:
5651     _return_value = os_pwritev_impl(module, fd, buffers, offset, flags);
5652     if ((_return_value == -1) && PyErr_Occurred()) {
5653         goto exit;
5654     }
5655     return_value = PyLong_FromSsize_t(_return_value);
5656 
5657 exit:
5658     return return_value;
5659 }
5660 
5661 #endif /* (defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2)) */
5662 
5663 #if defined(HAVE_COPY_FILE_RANGE)
5664 
5665 PyDoc_STRVAR(os_copy_file_range__doc__,
5666 "copy_file_range($module, /, src, dst, count, offset_src=None,\n"
5667 "                offset_dst=None)\n"
5668 "--\n"
5669 "\n"
5670 "Copy count bytes from one file descriptor to another.\n"
5671 "\n"
5672 "  src\n"
5673 "    Source file descriptor.\n"
5674 "  dst\n"
5675 "    Destination file descriptor.\n"
5676 "  count\n"
5677 "    Number of bytes to copy.\n"
5678 "  offset_src\n"
5679 "    Starting offset in src.\n"
5680 "  offset_dst\n"
5681 "    Starting offset in dst.\n"
5682 "\n"
5683 "If offset_src is None, then src is read from the current position;\n"
5684 "respectively for offset_dst.");
5685 
5686 #define OS_COPY_FILE_RANGE_METHODDEF    \
5687     {"copy_file_range", (PyCFunction)(void(*)(void))os_copy_file_range, METH_FASTCALL|METH_KEYWORDS, os_copy_file_range__doc__},
5688 
5689 static PyObject *
5690 os_copy_file_range_impl(PyObject *module, int src, int dst, Py_ssize_t count,
5691                         PyObject *offset_src, PyObject *offset_dst);
5692 
5693 static PyObject *
os_copy_file_range(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)5694 os_copy_file_range(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
5695 {
5696     PyObject *return_value = NULL;
5697     static const char * const _keywords[] = {"src", "dst", "count", "offset_src", "offset_dst", NULL};
5698     static _PyArg_Parser _parser = {NULL, _keywords, "copy_file_range", 0};
5699     PyObject *argsbuf[5];
5700     Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 3;
5701     int src;
5702     int dst;
5703     Py_ssize_t count;
5704     PyObject *offset_src = Py_None;
5705     PyObject *offset_dst = Py_None;
5706 
5707     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 3, 5, 0, argsbuf);
5708     if (!args) {
5709         goto exit;
5710     }
5711     src = _PyLong_AsInt(args[0]);
5712     if (src == -1 && PyErr_Occurred()) {
5713         goto exit;
5714     }
5715     dst = _PyLong_AsInt(args[1]);
5716     if (dst == -1 && PyErr_Occurred()) {
5717         goto exit;
5718     }
5719     {
5720         Py_ssize_t ival = -1;
5721         PyObject *iobj = _PyNumber_Index(args[2]);
5722         if (iobj != NULL) {
5723             ival = PyLong_AsSsize_t(iobj);
5724             Py_DECREF(iobj);
5725         }
5726         if (ival == -1 && PyErr_Occurred()) {
5727             goto exit;
5728         }
5729         count = ival;
5730     }
5731     if (!noptargs) {
5732         goto skip_optional_pos;
5733     }
5734     if (args[3]) {
5735         offset_src = args[3];
5736         if (!--noptargs) {
5737             goto skip_optional_pos;
5738         }
5739     }
5740     offset_dst = args[4];
5741 skip_optional_pos:
5742     return_value = os_copy_file_range_impl(module, src, dst, count, offset_src, offset_dst);
5743 
5744 exit:
5745     return return_value;
5746 }
5747 
5748 #endif /* defined(HAVE_COPY_FILE_RANGE) */
5749 
5750 #if ((defined(HAVE_SPLICE) && !defined(_AIX)))
5751 
5752 PyDoc_STRVAR(os_splice__doc__,
5753 "splice($module, /, src, dst, count, offset_src=None, offset_dst=None,\n"
5754 "       flags=0)\n"
5755 "--\n"
5756 "\n"
5757 "Transfer count bytes from one pipe to a descriptor or vice versa.\n"
5758 "\n"
5759 "  src\n"
5760 "    Source file descriptor.\n"
5761 "  dst\n"
5762 "    Destination file descriptor.\n"
5763 "  count\n"
5764 "    Number of bytes to copy.\n"
5765 "  offset_src\n"
5766 "    Starting offset in src.\n"
5767 "  offset_dst\n"
5768 "    Starting offset in dst.\n"
5769 "  flags\n"
5770 "    Flags to modify the semantics of the call.\n"
5771 "\n"
5772 "If offset_src is None, then src is read from the current position;\n"
5773 "respectively for offset_dst. The offset associated to the file\n"
5774 "descriptor that refers to a pipe must be None.");
5775 
5776 #define OS_SPLICE_METHODDEF    \
5777     {"splice", (PyCFunction)(void(*)(void))os_splice, METH_FASTCALL|METH_KEYWORDS, os_splice__doc__},
5778 
5779 static PyObject *
5780 os_splice_impl(PyObject *module, int src, int dst, Py_ssize_t count,
5781                PyObject *offset_src, PyObject *offset_dst,
5782                unsigned int flags);
5783 
5784 static PyObject *
os_splice(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)5785 os_splice(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
5786 {
5787     PyObject *return_value = NULL;
5788     static const char * const _keywords[] = {"src", "dst", "count", "offset_src", "offset_dst", "flags", NULL};
5789     static _PyArg_Parser _parser = {NULL, _keywords, "splice", 0};
5790     PyObject *argsbuf[6];
5791     Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 3;
5792     int src;
5793     int dst;
5794     Py_ssize_t count;
5795     PyObject *offset_src = Py_None;
5796     PyObject *offset_dst = Py_None;
5797     unsigned int flags = 0;
5798 
5799     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 3, 6, 0, argsbuf);
5800     if (!args) {
5801         goto exit;
5802     }
5803     src = _PyLong_AsInt(args[0]);
5804     if (src == -1 && PyErr_Occurred()) {
5805         goto exit;
5806     }
5807     dst = _PyLong_AsInt(args[1]);
5808     if (dst == -1 && PyErr_Occurred()) {
5809         goto exit;
5810     }
5811     {
5812         Py_ssize_t ival = -1;
5813         PyObject *iobj = _PyNumber_Index(args[2]);
5814         if (iobj != NULL) {
5815             ival = PyLong_AsSsize_t(iobj);
5816             Py_DECREF(iobj);
5817         }
5818         if (ival == -1 && PyErr_Occurred()) {
5819             goto exit;
5820         }
5821         count = ival;
5822     }
5823     if (!noptargs) {
5824         goto skip_optional_pos;
5825     }
5826     if (args[3]) {
5827         offset_src = args[3];
5828         if (!--noptargs) {
5829             goto skip_optional_pos;
5830         }
5831     }
5832     if (args[4]) {
5833         offset_dst = args[4];
5834         if (!--noptargs) {
5835             goto skip_optional_pos;
5836         }
5837     }
5838     if (!_PyLong_UnsignedInt_Converter(args[5], &flags)) {
5839         goto exit;
5840     }
5841 skip_optional_pos:
5842     return_value = os_splice_impl(module, src, dst, count, offset_src, offset_dst, flags);
5843 
5844 exit:
5845     return return_value;
5846 }
5847 
5848 #endif /* ((defined(HAVE_SPLICE) && !defined(_AIX))) */
5849 
5850 #if defined(HAVE_MKFIFO)
5851 
5852 PyDoc_STRVAR(os_mkfifo__doc__,
5853 "mkfifo($module, /, path, mode=438, *, dir_fd=None)\n"
5854 "--\n"
5855 "\n"
5856 "Create a \"fifo\" (a POSIX named pipe).\n"
5857 "\n"
5858 "If dir_fd is not None, it should be a file descriptor open to a directory,\n"
5859 "  and path should be relative; path will then be relative to that directory.\n"
5860 "dir_fd may not be implemented on your platform.\n"
5861 "  If it is unavailable, using it will raise a NotImplementedError.");
5862 
5863 #define OS_MKFIFO_METHODDEF    \
5864     {"mkfifo", (PyCFunction)(void(*)(void))os_mkfifo, METH_FASTCALL|METH_KEYWORDS, os_mkfifo__doc__},
5865 
5866 static PyObject *
5867 os_mkfifo_impl(PyObject *module, path_t *path, int mode, int dir_fd);
5868 
5869 static PyObject *
os_mkfifo(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)5870 os_mkfifo(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
5871 {
5872     PyObject *return_value = NULL;
5873     static const char * const _keywords[] = {"path", "mode", "dir_fd", NULL};
5874     static _PyArg_Parser _parser = {NULL, _keywords, "mkfifo", 0};
5875     PyObject *argsbuf[3];
5876     Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
5877     path_t path = PATH_T_INITIALIZE("mkfifo", "path", 0, 0);
5878     int mode = 438;
5879     int dir_fd = DEFAULT_DIR_FD;
5880 
5881     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
5882     if (!args) {
5883         goto exit;
5884     }
5885     if (!path_converter(args[0], &path)) {
5886         goto exit;
5887     }
5888     if (!noptargs) {
5889         goto skip_optional_pos;
5890     }
5891     if (args[1]) {
5892         mode = _PyLong_AsInt(args[1]);
5893         if (mode == -1 && PyErr_Occurred()) {
5894             goto exit;
5895         }
5896         if (!--noptargs) {
5897             goto skip_optional_pos;
5898         }
5899     }
5900 skip_optional_pos:
5901     if (!noptargs) {
5902         goto skip_optional_kwonly;
5903     }
5904     if (!MKFIFOAT_DIR_FD_CONVERTER(args[2], &dir_fd)) {
5905         goto exit;
5906     }
5907 skip_optional_kwonly:
5908     return_value = os_mkfifo_impl(module, &path, mode, dir_fd);
5909 
5910 exit:
5911     /* Cleanup for path */
5912     path_cleanup(&path);
5913 
5914     return return_value;
5915 }
5916 
5917 #endif /* defined(HAVE_MKFIFO) */
5918 
5919 #if (defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV))
5920 
5921 PyDoc_STRVAR(os_mknod__doc__,
5922 "mknod($module, /, path, mode=384, device=0, *, dir_fd=None)\n"
5923 "--\n"
5924 "\n"
5925 "Create a node in the file system.\n"
5926 "\n"
5927 "Create a node in the file system (file, device special file or named pipe)\n"
5928 "at path.  mode specifies both the permissions to use and the\n"
5929 "type of node to be created, being combined (bitwise OR) with one of\n"
5930 "S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO.  If S_IFCHR or S_IFBLK is set on mode,\n"
5931 "device defines the newly created device special file (probably using\n"
5932 "os.makedev()).  Otherwise device is ignored.\n"
5933 "\n"
5934 "If dir_fd is not None, it should be a file descriptor open to a directory,\n"
5935 "  and path should be relative; path will then be relative to that directory.\n"
5936 "dir_fd may not be implemented on your platform.\n"
5937 "  If it is unavailable, using it will raise a NotImplementedError.");
5938 
5939 #define OS_MKNOD_METHODDEF    \
5940     {"mknod", (PyCFunction)(void(*)(void))os_mknod, METH_FASTCALL|METH_KEYWORDS, os_mknod__doc__},
5941 
5942 static PyObject *
5943 os_mknod_impl(PyObject *module, path_t *path, int mode, dev_t device,
5944               int dir_fd);
5945 
5946 static PyObject *
os_mknod(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)5947 os_mknod(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
5948 {
5949     PyObject *return_value = NULL;
5950     static const char * const _keywords[] = {"path", "mode", "device", "dir_fd", NULL};
5951     static _PyArg_Parser _parser = {NULL, _keywords, "mknod", 0};
5952     PyObject *argsbuf[4];
5953     Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
5954     path_t path = PATH_T_INITIALIZE("mknod", "path", 0, 0);
5955     int mode = 384;
5956     dev_t device = 0;
5957     int dir_fd = DEFAULT_DIR_FD;
5958 
5959     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 3, 0, argsbuf);
5960     if (!args) {
5961         goto exit;
5962     }
5963     if (!path_converter(args[0], &path)) {
5964         goto exit;
5965     }
5966     if (!noptargs) {
5967         goto skip_optional_pos;
5968     }
5969     if (args[1]) {
5970         mode = _PyLong_AsInt(args[1]);
5971         if (mode == -1 && PyErr_Occurred()) {
5972             goto exit;
5973         }
5974         if (!--noptargs) {
5975             goto skip_optional_pos;
5976         }
5977     }
5978     if (args[2]) {
5979         if (!_Py_Dev_Converter(args[2], &device)) {
5980             goto exit;
5981         }
5982         if (!--noptargs) {
5983             goto skip_optional_pos;
5984         }
5985     }
5986 skip_optional_pos:
5987     if (!noptargs) {
5988         goto skip_optional_kwonly;
5989     }
5990     if (!MKNODAT_DIR_FD_CONVERTER(args[3], &dir_fd)) {
5991         goto exit;
5992     }
5993 skip_optional_kwonly:
5994     return_value = os_mknod_impl(module, &path, mode, device, dir_fd);
5995 
5996 exit:
5997     /* Cleanup for path */
5998     path_cleanup(&path);
5999 
6000     return return_value;
6001 }
6002 
6003 #endif /* (defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)) */
6004 
6005 #if defined(HAVE_DEVICE_MACROS)
6006 
6007 PyDoc_STRVAR(os_major__doc__,
6008 "major($module, device, /)\n"
6009 "--\n"
6010 "\n"
6011 "Extracts a device major number from a raw device number.");
6012 
6013 #define OS_MAJOR_METHODDEF    \
6014     {"major", (PyCFunction)os_major, METH_O, os_major__doc__},
6015 
6016 static unsigned int
6017 os_major_impl(PyObject *module, dev_t device);
6018 
6019 static PyObject *
os_major(PyObject * module,PyObject * arg)6020 os_major(PyObject *module, PyObject *arg)
6021 {
6022     PyObject *return_value = NULL;
6023     dev_t device;
6024     unsigned int _return_value;
6025 
6026     if (!_Py_Dev_Converter(arg, &device)) {
6027         goto exit;
6028     }
6029     _return_value = os_major_impl(module, device);
6030     if ((_return_value == (unsigned int)-1) && PyErr_Occurred()) {
6031         goto exit;
6032     }
6033     return_value = PyLong_FromUnsignedLong((unsigned long)_return_value);
6034 
6035 exit:
6036     return return_value;
6037 }
6038 
6039 #endif /* defined(HAVE_DEVICE_MACROS) */
6040 
6041 #if defined(HAVE_DEVICE_MACROS)
6042 
6043 PyDoc_STRVAR(os_minor__doc__,
6044 "minor($module, device, /)\n"
6045 "--\n"
6046 "\n"
6047 "Extracts a device minor number from a raw device number.");
6048 
6049 #define OS_MINOR_METHODDEF    \
6050     {"minor", (PyCFunction)os_minor, METH_O, os_minor__doc__},
6051 
6052 static unsigned int
6053 os_minor_impl(PyObject *module, dev_t device);
6054 
6055 static PyObject *
os_minor(PyObject * module,PyObject * arg)6056 os_minor(PyObject *module, PyObject *arg)
6057 {
6058     PyObject *return_value = NULL;
6059     dev_t device;
6060     unsigned int _return_value;
6061 
6062     if (!_Py_Dev_Converter(arg, &device)) {
6063         goto exit;
6064     }
6065     _return_value = os_minor_impl(module, device);
6066     if ((_return_value == (unsigned int)-1) && PyErr_Occurred()) {
6067         goto exit;
6068     }
6069     return_value = PyLong_FromUnsignedLong((unsigned long)_return_value);
6070 
6071 exit:
6072     return return_value;
6073 }
6074 
6075 #endif /* defined(HAVE_DEVICE_MACROS) */
6076 
6077 #if defined(HAVE_DEVICE_MACROS)
6078 
6079 PyDoc_STRVAR(os_makedev__doc__,
6080 "makedev($module, major, minor, /)\n"
6081 "--\n"
6082 "\n"
6083 "Composes a raw device number from the major and minor device numbers.");
6084 
6085 #define OS_MAKEDEV_METHODDEF    \
6086     {"makedev", (PyCFunction)(void(*)(void))os_makedev, METH_FASTCALL, os_makedev__doc__},
6087 
6088 static dev_t
6089 os_makedev_impl(PyObject *module, int major, int minor);
6090 
6091 static PyObject *
os_makedev(PyObject * module,PyObject * const * args,Py_ssize_t nargs)6092 os_makedev(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
6093 {
6094     PyObject *return_value = NULL;
6095     int major;
6096     int minor;
6097     dev_t _return_value;
6098 
6099     if (!_PyArg_CheckPositional("makedev", nargs, 2, 2)) {
6100         goto exit;
6101     }
6102     major = _PyLong_AsInt(args[0]);
6103     if (major == -1 && PyErr_Occurred()) {
6104         goto exit;
6105     }
6106     minor = _PyLong_AsInt(args[1]);
6107     if (minor == -1 && PyErr_Occurred()) {
6108         goto exit;
6109     }
6110     _return_value = os_makedev_impl(module, major, minor);
6111     if ((_return_value == (dev_t)-1) && PyErr_Occurred()) {
6112         goto exit;
6113     }
6114     return_value = _PyLong_FromDev(_return_value);
6115 
6116 exit:
6117     return return_value;
6118 }
6119 
6120 #endif /* defined(HAVE_DEVICE_MACROS) */
6121 
6122 #if (defined HAVE_FTRUNCATE || defined MS_WINDOWS)
6123 
6124 PyDoc_STRVAR(os_ftruncate__doc__,
6125 "ftruncate($module, fd, length, /)\n"
6126 "--\n"
6127 "\n"
6128 "Truncate a file, specified by file descriptor, to a specific length.");
6129 
6130 #define OS_FTRUNCATE_METHODDEF    \
6131     {"ftruncate", (PyCFunction)(void(*)(void))os_ftruncate, METH_FASTCALL, os_ftruncate__doc__},
6132 
6133 static PyObject *
6134 os_ftruncate_impl(PyObject *module, int fd, Py_off_t length);
6135 
6136 static PyObject *
os_ftruncate(PyObject * module,PyObject * const * args,Py_ssize_t nargs)6137 os_ftruncate(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
6138 {
6139     PyObject *return_value = NULL;
6140     int fd;
6141     Py_off_t length;
6142 
6143     if (!_PyArg_CheckPositional("ftruncate", nargs, 2, 2)) {
6144         goto exit;
6145     }
6146     fd = _PyLong_AsInt(args[0]);
6147     if (fd == -1 && PyErr_Occurred()) {
6148         goto exit;
6149     }
6150     if (!Py_off_t_converter(args[1], &length)) {
6151         goto exit;
6152     }
6153     return_value = os_ftruncate_impl(module, fd, length);
6154 
6155 exit:
6156     return return_value;
6157 }
6158 
6159 #endif /* (defined HAVE_FTRUNCATE || defined MS_WINDOWS) */
6160 
6161 #if (defined HAVE_TRUNCATE || defined MS_WINDOWS)
6162 
6163 PyDoc_STRVAR(os_truncate__doc__,
6164 "truncate($module, /, path, length)\n"
6165 "--\n"
6166 "\n"
6167 "Truncate a file, specified by path, to a specific length.\n"
6168 "\n"
6169 "On some platforms, path may also be specified as an open file descriptor.\n"
6170 "  If this functionality is unavailable, using it raises an exception.");
6171 
6172 #define OS_TRUNCATE_METHODDEF    \
6173     {"truncate", (PyCFunction)(void(*)(void))os_truncate, METH_FASTCALL|METH_KEYWORDS, os_truncate__doc__},
6174 
6175 static PyObject *
6176 os_truncate_impl(PyObject *module, path_t *path, Py_off_t length);
6177 
6178 static PyObject *
os_truncate(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)6179 os_truncate(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
6180 {
6181     PyObject *return_value = NULL;
6182     static const char * const _keywords[] = {"path", "length", NULL};
6183     static _PyArg_Parser _parser = {NULL, _keywords, "truncate", 0};
6184     PyObject *argsbuf[2];
6185     path_t path = PATH_T_INITIALIZE("truncate", "path", 0, PATH_HAVE_FTRUNCATE);
6186     Py_off_t length;
6187 
6188     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf);
6189     if (!args) {
6190         goto exit;
6191     }
6192     if (!path_converter(args[0], &path)) {
6193         goto exit;
6194     }
6195     if (!Py_off_t_converter(args[1], &length)) {
6196         goto exit;
6197     }
6198     return_value = os_truncate_impl(module, &path, length);
6199 
6200 exit:
6201     /* Cleanup for path */
6202     path_cleanup(&path);
6203 
6204     return return_value;
6205 }
6206 
6207 #endif /* (defined HAVE_TRUNCATE || defined MS_WINDOWS) */
6208 
6209 #if (defined(HAVE_POSIX_FALLOCATE) && !defined(POSIX_FADVISE_AIX_BUG))
6210 
6211 PyDoc_STRVAR(os_posix_fallocate__doc__,
6212 "posix_fallocate($module, fd, offset, length, /)\n"
6213 "--\n"
6214 "\n"
6215 "Ensure a file has allocated at least a particular number of bytes on disk.\n"
6216 "\n"
6217 "Ensure that the file specified by fd encompasses a range of bytes\n"
6218 "starting at offset bytes from the beginning and continuing for length bytes.");
6219 
6220 #define OS_POSIX_FALLOCATE_METHODDEF    \
6221     {"posix_fallocate", (PyCFunction)(void(*)(void))os_posix_fallocate, METH_FASTCALL, os_posix_fallocate__doc__},
6222 
6223 static PyObject *
6224 os_posix_fallocate_impl(PyObject *module, int fd, Py_off_t offset,
6225                         Py_off_t length);
6226 
6227 static PyObject *
os_posix_fallocate(PyObject * module,PyObject * const * args,Py_ssize_t nargs)6228 os_posix_fallocate(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
6229 {
6230     PyObject *return_value = NULL;
6231     int fd;
6232     Py_off_t offset;
6233     Py_off_t length;
6234 
6235     if (!_PyArg_CheckPositional("posix_fallocate", nargs, 3, 3)) {
6236         goto exit;
6237     }
6238     fd = _PyLong_AsInt(args[0]);
6239     if (fd == -1 && PyErr_Occurred()) {
6240         goto exit;
6241     }
6242     if (!Py_off_t_converter(args[1], &offset)) {
6243         goto exit;
6244     }
6245     if (!Py_off_t_converter(args[2], &length)) {
6246         goto exit;
6247     }
6248     return_value = os_posix_fallocate_impl(module, fd, offset, length);
6249 
6250 exit:
6251     return return_value;
6252 }
6253 
6254 #endif /* (defined(HAVE_POSIX_FALLOCATE) && !defined(POSIX_FADVISE_AIX_BUG)) */
6255 
6256 #if (defined(HAVE_POSIX_FADVISE) && !defined(POSIX_FADVISE_AIX_BUG))
6257 
6258 PyDoc_STRVAR(os_posix_fadvise__doc__,
6259 "posix_fadvise($module, fd, offset, length, advice, /)\n"
6260 "--\n"
6261 "\n"
6262 "Announce an intention to access data in a specific pattern.\n"
6263 "\n"
6264 "Announce an intention to access data in a specific pattern, thus allowing\n"
6265 "the kernel to make optimizations.\n"
6266 "The advice applies to the region of the file specified by fd starting at\n"
6267 "offset and continuing for length bytes.\n"
6268 "advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,\n"
6269 "POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED, or\n"
6270 "POSIX_FADV_DONTNEED.");
6271 
6272 #define OS_POSIX_FADVISE_METHODDEF    \
6273     {"posix_fadvise", (PyCFunction)(void(*)(void))os_posix_fadvise, METH_FASTCALL, os_posix_fadvise__doc__},
6274 
6275 static PyObject *
6276 os_posix_fadvise_impl(PyObject *module, int fd, Py_off_t offset,
6277                       Py_off_t length, int advice);
6278 
6279 static PyObject *
os_posix_fadvise(PyObject * module,PyObject * const * args,Py_ssize_t nargs)6280 os_posix_fadvise(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
6281 {
6282     PyObject *return_value = NULL;
6283     int fd;
6284     Py_off_t offset;
6285     Py_off_t length;
6286     int advice;
6287 
6288     if (!_PyArg_CheckPositional("posix_fadvise", nargs, 4, 4)) {
6289         goto exit;
6290     }
6291     fd = _PyLong_AsInt(args[0]);
6292     if (fd == -1 && PyErr_Occurred()) {
6293         goto exit;
6294     }
6295     if (!Py_off_t_converter(args[1], &offset)) {
6296         goto exit;
6297     }
6298     if (!Py_off_t_converter(args[2], &length)) {
6299         goto exit;
6300     }
6301     advice = _PyLong_AsInt(args[3]);
6302     if (advice == -1 && PyErr_Occurred()) {
6303         goto exit;
6304     }
6305     return_value = os_posix_fadvise_impl(module, fd, offset, length, advice);
6306 
6307 exit:
6308     return return_value;
6309 }
6310 
6311 #endif /* (defined(HAVE_POSIX_FADVISE) && !defined(POSIX_FADVISE_AIX_BUG)) */
6312 
6313 #if defined(MS_WINDOWS)
6314 
6315 PyDoc_STRVAR(os_putenv__doc__,
6316 "putenv($module, name, value, /)\n"
6317 "--\n"
6318 "\n"
6319 "Change or add an environment variable.");
6320 
6321 #define OS_PUTENV_METHODDEF    \
6322     {"putenv", (PyCFunction)(void(*)(void))os_putenv, METH_FASTCALL, os_putenv__doc__},
6323 
6324 static PyObject *
6325 os_putenv_impl(PyObject *module, PyObject *name, PyObject *value);
6326 
6327 static PyObject *
os_putenv(PyObject * module,PyObject * const * args,Py_ssize_t nargs)6328 os_putenv(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
6329 {
6330     PyObject *return_value = NULL;
6331     PyObject *name;
6332     PyObject *value;
6333 
6334     if (!_PyArg_CheckPositional("putenv", nargs, 2, 2)) {
6335         goto exit;
6336     }
6337     if (!PyUnicode_Check(args[0])) {
6338         _PyArg_BadArgument("putenv", "argument 1", "str", args[0]);
6339         goto exit;
6340     }
6341     if (PyUnicode_READY(args[0]) == -1) {
6342         goto exit;
6343     }
6344     name = args[0];
6345     if (!PyUnicode_Check(args[1])) {
6346         _PyArg_BadArgument("putenv", "argument 2", "str", args[1]);
6347         goto exit;
6348     }
6349     if (PyUnicode_READY(args[1]) == -1) {
6350         goto exit;
6351     }
6352     value = args[1];
6353     return_value = os_putenv_impl(module, name, value);
6354 
6355 exit:
6356     return return_value;
6357 }
6358 
6359 #endif /* defined(MS_WINDOWS) */
6360 
6361 #if !defined(MS_WINDOWS)
6362 
6363 PyDoc_STRVAR(os_putenv__doc__,
6364 "putenv($module, name, value, /)\n"
6365 "--\n"
6366 "\n"
6367 "Change or add an environment variable.");
6368 
6369 #define OS_PUTENV_METHODDEF    \
6370     {"putenv", (PyCFunction)(void(*)(void))os_putenv, METH_FASTCALL, os_putenv__doc__},
6371 
6372 static PyObject *
6373 os_putenv_impl(PyObject *module, PyObject *name, PyObject *value);
6374 
6375 static PyObject *
os_putenv(PyObject * module,PyObject * const * args,Py_ssize_t nargs)6376 os_putenv(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
6377 {
6378     PyObject *return_value = NULL;
6379     PyObject *name = NULL;
6380     PyObject *value = NULL;
6381 
6382     if (!_PyArg_CheckPositional("putenv", nargs, 2, 2)) {
6383         goto exit;
6384     }
6385     if (!PyUnicode_FSConverter(args[0], &name)) {
6386         goto exit;
6387     }
6388     if (!PyUnicode_FSConverter(args[1], &value)) {
6389         goto exit;
6390     }
6391     return_value = os_putenv_impl(module, name, value);
6392 
6393 exit:
6394     /* Cleanup for name */
6395     Py_XDECREF(name);
6396     /* Cleanup for value */
6397     Py_XDECREF(value);
6398 
6399     return return_value;
6400 }
6401 
6402 #endif /* !defined(MS_WINDOWS) */
6403 
6404 #if defined(MS_WINDOWS)
6405 
6406 PyDoc_STRVAR(os_unsetenv__doc__,
6407 "unsetenv($module, name, /)\n"
6408 "--\n"
6409 "\n"
6410 "Delete an environment variable.");
6411 
6412 #define OS_UNSETENV_METHODDEF    \
6413     {"unsetenv", (PyCFunction)os_unsetenv, METH_O, os_unsetenv__doc__},
6414 
6415 static PyObject *
6416 os_unsetenv_impl(PyObject *module, PyObject *name);
6417 
6418 static PyObject *
os_unsetenv(PyObject * module,PyObject * arg)6419 os_unsetenv(PyObject *module, PyObject *arg)
6420 {
6421     PyObject *return_value = NULL;
6422     PyObject *name;
6423 
6424     if (!PyUnicode_Check(arg)) {
6425         _PyArg_BadArgument("unsetenv", "argument", "str", arg);
6426         goto exit;
6427     }
6428     if (PyUnicode_READY(arg) == -1) {
6429         goto exit;
6430     }
6431     name = arg;
6432     return_value = os_unsetenv_impl(module, name);
6433 
6434 exit:
6435     return return_value;
6436 }
6437 
6438 #endif /* defined(MS_WINDOWS) */
6439 
6440 #if !defined(MS_WINDOWS)
6441 
6442 PyDoc_STRVAR(os_unsetenv__doc__,
6443 "unsetenv($module, name, /)\n"
6444 "--\n"
6445 "\n"
6446 "Delete an environment variable.");
6447 
6448 #define OS_UNSETENV_METHODDEF    \
6449     {"unsetenv", (PyCFunction)os_unsetenv, METH_O, os_unsetenv__doc__},
6450 
6451 static PyObject *
6452 os_unsetenv_impl(PyObject *module, PyObject *name);
6453 
6454 static PyObject *
os_unsetenv(PyObject * module,PyObject * arg)6455 os_unsetenv(PyObject *module, PyObject *arg)
6456 {
6457     PyObject *return_value = NULL;
6458     PyObject *name = NULL;
6459 
6460     if (!PyUnicode_FSConverter(arg, &name)) {
6461         goto exit;
6462     }
6463     return_value = os_unsetenv_impl(module, name);
6464 
6465 exit:
6466     /* Cleanup for name */
6467     Py_XDECREF(name);
6468 
6469     return return_value;
6470 }
6471 
6472 #endif /* !defined(MS_WINDOWS) */
6473 
6474 PyDoc_STRVAR(os_strerror__doc__,
6475 "strerror($module, code, /)\n"
6476 "--\n"
6477 "\n"
6478 "Translate an error code to a message string.");
6479 
6480 #define OS_STRERROR_METHODDEF    \
6481     {"strerror", (PyCFunction)os_strerror, METH_O, os_strerror__doc__},
6482 
6483 static PyObject *
6484 os_strerror_impl(PyObject *module, int code);
6485 
6486 static PyObject *
os_strerror(PyObject * module,PyObject * arg)6487 os_strerror(PyObject *module, PyObject *arg)
6488 {
6489     PyObject *return_value = NULL;
6490     int code;
6491 
6492     code = _PyLong_AsInt(arg);
6493     if (code == -1 && PyErr_Occurred()) {
6494         goto exit;
6495     }
6496     return_value = os_strerror_impl(module, code);
6497 
6498 exit:
6499     return return_value;
6500 }
6501 
6502 #if defined(HAVE_SYS_WAIT_H) && defined(WCOREDUMP)
6503 
6504 PyDoc_STRVAR(os_WCOREDUMP__doc__,
6505 "WCOREDUMP($module, status, /)\n"
6506 "--\n"
6507 "\n"
6508 "Return True if the process returning status was dumped to a core file.");
6509 
6510 #define OS_WCOREDUMP_METHODDEF    \
6511     {"WCOREDUMP", (PyCFunction)os_WCOREDUMP, METH_O, os_WCOREDUMP__doc__},
6512 
6513 static int
6514 os_WCOREDUMP_impl(PyObject *module, int status);
6515 
6516 static PyObject *
os_WCOREDUMP(PyObject * module,PyObject * arg)6517 os_WCOREDUMP(PyObject *module, PyObject *arg)
6518 {
6519     PyObject *return_value = NULL;
6520     int status;
6521     int _return_value;
6522 
6523     status = _PyLong_AsInt(arg);
6524     if (status == -1 && PyErr_Occurred()) {
6525         goto exit;
6526     }
6527     _return_value = os_WCOREDUMP_impl(module, status);
6528     if ((_return_value == -1) && PyErr_Occurred()) {
6529         goto exit;
6530     }
6531     return_value = PyBool_FromLong((long)_return_value);
6532 
6533 exit:
6534     return return_value;
6535 }
6536 
6537 #endif /* defined(HAVE_SYS_WAIT_H) && defined(WCOREDUMP) */
6538 
6539 #if defined(HAVE_SYS_WAIT_H) && defined(WIFCONTINUED)
6540 
6541 PyDoc_STRVAR(os_WIFCONTINUED__doc__,
6542 "WIFCONTINUED($module, /, status)\n"
6543 "--\n"
6544 "\n"
6545 "Return True if a particular process was continued from a job control stop.\n"
6546 "\n"
6547 "Return True if the process returning status was continued from a\n"
6548 "job control stop.");
6549 
6550 #define OS_WIFCONTINUED_METHODDEF    \
6551     {"WIFCONTINUED", (PyCFunction)(void(*)(void))os_WIFCONTINUED, METH_FASTCALL|METH_KEYWORDS, os_WIFCONTINUED__doc__},
6552 
6553 static int
6554 os_WIFCONTINUED_impl(PyObject *module, int status);
6555 
6556 static PyObject *
os_WIFCONTINUED(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)6557 os_WIFCONTINUED(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
6558 {
6559     PyObject *return_value = NULL;
6560     static const char * const _keywords[] = {"status", NULL};
6561     static _PyArg_Parser _parser = {NULL, _keywords, "WIFCONTINUED", 0};
6562     PyObject *argsbuf[1];
6563     int status;
6564     int _return_value;
6565 
6566     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
6567     if (!args) {
6568         goto exit;
6569     }
6570     status = _PyLong_AsInt(args[0]);
6571     if (status == -1 && PyErr_Occurred()) {
6572         goto exit;
6573     }
6574     _return_value = os_WIFCONTINUED_impl(module, status);
6575     if ((_return_value == -1) && PyErr_Occurred()) {
6576         goto exit;
6577     }
6578     return_value = PyBool_FromLong((long)_return_value);
6579 
6580 exit:
6581     return return_value;
6582 }
6583 
6584 #endif /* defined(HAVE_SYS_WAIT_H) && defined(WIFCONTINUED) */
6585 
6586 #if defined(HAVE_SYS_WAIT_H) && defined(WIFSTOPPED)
6587 
6588 PyDoc_STRVAR(os_WIFSTOPPED__doc__,
6589 "WIFSTOPPED($module, /, status)\n"
6590 "--\n"
6591 "\n"
6592 "Return True if the process returning status was stopped.");
6593 
6594 #define OS_WIFSTOPPED_METHODDEF    \
6595     {"WIFSTOPPED", (PyCFunction)(void(*)(void))os_WIFSTOPPED, METH_FASTCALL|METH_KEYWORDS, os_WIFSTOPPED__doc__},
6596 
6597 static int
6598 os_WIFSTOPPED_impl(PyObject *module, int status);
6599 
6600 static PyObject *
os_WIFSTOPPED(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)6601 os_WIFSTOPPED(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
6602 {
6603     PyObject *return_value = NULL;
6604     static const char * const _keywords[] = {"status", NULL};
6605     static _PyArg_Parser _parser = {NULL, _keywords, "WIFSTOPPED", 0};
6606     PyObject *argsbuf[1];
6607     int status;
6608     int _return_value;
6609 
6610     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
6611     if (!args) {
6612         goto exit;
6613     }
6614     status = _PyLong_AsInt(args[0]);
6615     if (status == -1 && PyErr_Occurred()) {
6616         goto exit;
6617     }
6618     _return_value = os_WIFSTOPPED_impl(module, status);
6619     if ((_return_value == -1) && PyErr_Occurred()) {
6620         goto exit;
6621     }
6622     return_value = PyBool_FromLong((long)_return_value);
6623 
6624 exit:
6625     return return_value;
6626 }
6627 
6628 #endif /* defined(HAVE_SYS_WAIT_H) && defined(WIFSTOPPED) */
6629 
6630 #if defined(HAVE_SYS_WAIT_H) && defined(WIFSIGNALED)
6631 
6632 PyDoc_STRVAR(os_WIFSIGNALED__doc__,
6633 "WIFSIGNALED($module, /, status)\n"
6634 "--\n"
6635 "\n"
6636 "Return True if the process returning status was terminated by a signal.");
6637 
6638 #define OS_WIFSIGNALED_METHODDEF    \
6639     {"WIFSIGNALED", (PyCFunction)(void(*)(void))os_WIFSIGNALED, METH_FASTCALL|METH_KEYWORDS, os_WIFSIGNALED__doc__},
6640 
6641 static int
6642 os_WIFSIGNALED_impl(PyObject *module, int status);
6643 
6644 static PyObject *
os_WIFSIGNALED(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)6645 os_WIFSIGNALED(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
6646 {
6647     PyObject *return_value = NULL;
6648     static const char * const _keywords[] = {"status", NULL};
6649     static _PyArg_Parser _parser = {NULL, _keywords, "WIFSIGNALED", 0};
6650     PyObject *argsbuf[1];
6651     int status;
6652     int _return_value;
6653 
6654     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
6655     if (!args) {
6656         goto exit;
6657     }
6658     status = _PyLong_AsInt(args[0]);
6659     if (status == -1 && PyErr_Occurred()) {
6660         goto exit;
6661     }
6662     _return_value = os_WIFSIGNALED_impl(module, status);
6663     if ((_return_value == -1) && PyErr_Occurred()) {
6664         goto exit;
6665     }
6666     return_value = PyBool_FromLong((long)_return_value);
6667 
6668 exit:
6669     return return_value;
6670 }
6671 
6672 #endif /* defined(HAVE_SYS_WAIT_H) && defined(WIFSIGNALED) */
6673 
6674 #if defined(HAVE_SYS_WAIT_H) && defined(WIFEXITED)
6675 
6676 PyDoc_STRVAR(os_WIFEXITED__doc__,
6677 "WIFEXITED($module, /, status)\n"
6678 "--\n"
6679 "\n"
6680 "Return True if the process returning status exited via the exit() system call.");
6681 
6682 #define OS_WIFEXITED_METHODDEF    \
6683     {"WIFEXITED", (PyCFunction)(void(*)(void))os_WIFEXITED, METH_FASTCALL|METH_KEYWORDS, os_WIFEXITED__doc__},
6684 
6685 static int
6686 os_WIFEXITED_impl(PyObject *module, int status);
6687 
6688 static PyObject *
os_WIFEXITED(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)6689 os_WIFEXITED(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
6690 {
6691     PyObject *return_value = NULL;
6692     static const char * const _keywords[] = {"status", NULL};
6693     static _PyArg_Parser _parser = {NULL, _keywords, "WIFEXITED", 0};
6694     PyObject *argsbuf[1];
6695     int status;
6696     int _return_value;
6697 
6698     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
6699     if (!args) {
6700         goto exit;
6701     }
6702     status = _PyLong_AsInt(args[0]);
6703     if (status == -1 && PyErr_Occurred()) {
6704         goto exit;
6705     }
6706     _return_value = os_WIFEXITED_impl(module, status);
6707     if ((_return_value == -1) && PyErr_Occurred()) {
6708         goto exit;
6709     }
6710     return_value = PyBool_FromLong((long)_return_value);
6711 
6712 exit:
6713     return return_value;
6714 }
6715 
6716 #endif /* defined(HAVE_SYS_WAIT_H) && defined(WIFEXITED) */
6717 
6718 #if defined(HAVE_SYS_WAIT_H) && defined(WEXITSTATUS)
6719 
6720 PyDoc_STRVAR(os_WEXITSTATUS__doc__,
6721 "WEXITSTATUS($module, /, status)\n"
6722 "--\n"
6723 "\n"
6724 "Return the process return code from status.");
6725 
6726 #define OS_WEXITSTATUS_METHODDEF    \
6727     {"WEXITSTATUS", (PyCFunction)(void(*)(void))os_WEXITSTATUS, METH_FASTCALL|METH_KEYWORDS, os_WEXITSTATUS__doc__},
6728 
6729 static int
6730 os_WEXITSTATUS_impl(PyObject *module, int status);
6731 
6732 static PyObject *
os_WEXITSTATUS(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)6733 os_WEXITSTATUS(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
6734 {
6735     PyObject *return_value = NULL;
6736     static const char * const _keywords[] = {"status", NULL};
6737     static _PyArg_Parser _parser = {NULL, _keywords, "WEXITSTATUS", 0};
6738     PyObject *argsbuf[1];
6739     int status;
6740     int _return_value;
6741 
6742     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
6743     if (!args) {
6744         goto exit;
6745     }
6746     status = _PyLong_AsInt(args[0]);
6747     if (status == -1 && PyErr_Occurred()) {
6748         goto exit;
6749     }
6750     _return_value = os_WEXITSTATUS_impl(module, status);
6751     if ((_return_value == -1) && PyErr_Occurred()) {
6752         goto exit;
6753     }
6754     return_value = PyLong_FromLong((long)_return_value);
6755 
6756 exit:
6757     return return_value;
6758 }
6759 
6760 #endif /* defined(HAVE_SYS_WAIT_H) && defined(WEXITSTATUS) */
6761 
6762 #if defined(HAVE_SYS_WAIT_H) && defined(WTERMSIG)
6763 
6764 PyDoc_STRVAR(os_WTERMSIG__doc__,
6765 "WTERMSIG($module, /, status)\n"
6766 "--\n"
6767 "\n"
6768 "Return the signal that terminated the process that provided the status value.");
6769 
6770 #define OS_WTERMSIG_METHODDEF    \
6771     {"WTERMSIG", (PyCFunction)(void(*)(void))os_WTERMSIG, METH_FASTCALL|METH_KEYWORDS, os_WTERMSIG__doc__},
6772 
6773 static int
6774 os_WTERMSIG_impl(PyObject *module, int status);
6775 
6776 static PyObject *
os_WTERMSIG(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)6777 os_WTERMSIG(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
6778 {
6779     PyObject *return_value = NULL;
6780     static const char * const _keywords[] = {"status", NULL};
6781     static _PyArg_Parser _parser = {NULL, _keywords, "WTERMSIG", 0};
6782     PyObject *argsbuf[1];
6783     int status;
6784     int _return_value;
6785 
6786     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
6787     if (!args) {
6788         goto exit;
6789     }
6790     status = _PyLong_AsInt(args[0]);
6791     if (status == -1 && PyErr_Occurred()) {
6792         goto exit;
6793     }
6794     _return_value = os_WTERMSIG_impl(module, status);
6795     if ((_return_value == -1) && PyErr_Occurred()) {
6796         goto exit;
6797     }
6798     return_value = PyLong_FromLong((long)_return_value);
6799 
6800 exit:
6801     return return_value;
6802 }
6803 
6804 #endif /* defined(HAVE_SYS_WAIT_H) && defined(WTERMSIG) */
6805 
6806 #if defined(HAVE_SYS_WAIT_H) && defined(WSTOPSIG)
6807 
6808 PyDoc_STRVAR(os_WSTOPSIG__doc__,
6809 "WSTOPSIG($module, /, status)\n"
6810 "--\n"
6811 "\n"
6812 "Return the signal that stopped the process that provided the status value.");
6813 
6814 #define OS_WSTOPSIG_METHODDEF    \
6815     {"WSTOPSIG", (PyCFunction)(void(*)(void))os_WSTOPSIG, METH_FASTCALL|METH_KEYWORDS, os_WSTOPSIG__doc__},
6816 
6817 static int
6818 os_WSTOPSIG_impl(PyObject *module, int status);
6819 
6820 static PyObject *
os_WSTOPSIG(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)6821 os_WSTOPSIG(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
6822 {
6823     PyObject *return_value = NULL;
6824     static const char * const _keywords[] = {"status", NULL};
6825     static _PyArg_Parser _parser = {NULL, _keywords, "WSTOPSIG", 0};
6826     PyObject *argsbuf[1];
6827     int status;
6828     int _return_value;
6829 
6830     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
6831     if (!args) {
6832         goto exit;
6833     }
6834     status = _PyLong_AsInt(args[0]);
6835     if (status == -1 && PyErr_Occurred()) {
6836         goto exit;
6837     }
6838     _return_value = os_WSTOPSIG_impl(module, status);
6839     if ((_return_value == -1) && PyErr_Occurred()) {
6840         goto exit;
6841     }
6842     return_value = PyLong_FromLong((long)_return_value);
6843 
6844 exit:
6845     return return_value;
6846 }
6847 
6848 #endif /* defined(HAVE_SYS_WAIT_H) && defined(WSTOPSIG) */
6849 
6850 #if (defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H))
6851 
6852 PyDoc_STRVAR(os_fstatvfs__doc__,
6853 "fstatvfs($module, fd, /)\n"
6854 "--\n"
6855 "\n"
6856 "Perform an fstatvfs system call on the given fd.\n"
6857 "\n"
6858 "Equivalent to statvfs(fd).");
6859 
6860 #define OS_FSTATVFS_METHODDEF    \
6861     {"fstatvfs", (PyCFunction)os_fstatvfs, METH_O, os_fstatvfs__doc__},
6862 
6863 static PyObject *
6864 os_fstatvfs_impl(PyObject *module, int fd);
6865 
6866 static PyObject *
os_fstatvfs(PyObject * module,PyObject * arg)6867 os_fstatvfs(PyObject *module, PyObject *arg)
6868 {
6869     PyObject *return_value = NULL;
6870     int fd;
6871 
6872     fd = _PyLong_AsInt(arg);
6873     if (fd == -1 && PyErr_Occurred()) {
6874         goto exit;
6875     }
6876     return_value = os_fstatvfs_impl(module, fd);
6877 
6878 exit:
6879     return return_value;
6880 }
6881 
6882 #endif /* (defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)) */
6883 
6884 #if (defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H))
6885 
6886 PyDoc_STRVAR(os_statvfs__doc__,
6887 "statvfs($module, /, path)\n"
6888 "--\n"
6889 "\n"
6890 "Perform a statvfs system call on the given path.\n"
6891 "\n"
6892 "path may always be specified as a string.\n"
6893 "On some platforms, path may also be specified as an open file descriptor.\n"
6894 "  If this functionality is unavailable, using it raises an exception.");
6895 
6896 #define OS_STATVFS_METHODDEF    \
6897     {"statvfs", (PyCFunction)(void(*)(void))os_statvfs, METH_FASTCALL|METH_KEYWORDS, os_statvfs__doc__},
6898 
6899 static PyObject *
6900 os_statvfs_impl(PyObject *module, path_t *path);
6901 
6902 static PyObject *
os_statvfs(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)6903 os_statvfs(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
6904 {
6905     PyObject *return_value = NULL;
6906     static const char * const _keywords[] = {"path", NULL};
6907     static _PyArg_Parser _parser = {NULL, _keywords, "statvfs", 0};
6908     PyObject *argsbuf[1];
6909     path_t path = PATH_T_INITIALIZE("statvfs", "path", 0, PATH_HAVE_FSTATVFS);
6910 
6911     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
6912     if (!args) {
6913         goto exit;
6914     }
6915     if (!path_converter(args[0], &path)) {
6916         goto exit;
6917     }
6918     return_value = os_statvfs_impl(module, &path);
6919 
6920 exit:
6921     /* Cleanup for path */
6922     path_cleanup(&path);
6923 
6924     return return_value;
6925 }
6926 
6927 #endif /* (defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)) */
6928 
6929 #if defined(MS_WINDOWS)
6930 
6931 PyDoc_STRVAR(os__getdiskusage__doc__,
6932 "_getdiskusage($module, /, path)\n"
6933 "--\n"
6934 "\n"
6935 "Return disk usage statistics about the given path as a (total, free) tuple.");
6936 
6937 #define OS__GETDISKUSAGE_METHODDEF    \
6938     {"_getdiskusage", (PyCFunction)(void(*)(void))os__getdiskusage, METH_FASTCALL|METH_KEYWORDS, os__getdiskusage__doc__},
6939 
6940 static PyObject *
6941 os__getdiskusage_impl(PyObject *module, path_t *path);
6942 
6943 static PyObject *
os__getdiskusage(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)6944 os__getdiskusage(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
6945 {
6946     PyObject *return_value = NULL;
6947     static const char * const _keywords[] = {"path", NULL};
6948     static _PyArg_Parser _parser = {NULL, _keywords, "_getdiskusage", 0};
6949     PyObject *argsbuf[1];
6950     path_t path = PATH_T_INITIALIZE("_getdiskusage", "path", 0, 0);
6951 
6952     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
6953     if (!args) {
6954         goto exit;
6955     }
6956     if (!path_converter(args[0], &path)) {
6957         goto exit;
6958     }
6959     return_value = os__getdiskusage_impl(module, &path);
6960 
6961 exit:
6962     /* Cleanup for path */
6963     path_cleanup(&path);
6964 
6965     return return_value;
6966 }
6967 
6968 #endif /* defined(MS_WINDOWS) */
6969 
6970 #if defined(HAVE_FPATHCONF)
6971 
6972 PyDoc_STRVAR(os_fpathconf__doc__,
6973 "fpathconf($module, fd, name, /)\n"
6974 "--\n"
6975 "\n"
6976 "Return the configuration limit name for the file descriptor fd.\n"
6977 "\n"
6978 "If there is no limit, return -1.");
6979 
6980 #define OS_FPATHCONF_METHODDEF    \
6981     {"fpathconf", (PyCFunction)(void(*)(void))os_fpathconf, METH_FASTCALL, os_fpathconf__doc__},
6982 
6983 static long
6984 os_fpathconf_impl(PyObject *module, int fd, int name);
6985 
6986 static PyObject *
os_fpathconf(PyObject * module,PyObject * const * args,Py_ssize_t nargs)6987 os_fpathconf(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
6988 {
6989     PyObject *return_value = NULL;
6990     int fd;
6991     int name;
6992     long _return_value;
6993 
6994     if (!_PyArg_CheckPositional("fpathconf", nargs, 2, 2)) {
6995         goto exit;
6996     }
6997     if (!_PyLong_FileDescriptor_Converter(args[0], &fd)) {
6998         goto exit;
6999     }
7000     if (!conv_path_confname(args[1], &name)) {
7001         goto exit;
7002     }
7003     _return_value = os_fpathconf_impl(module, fd, name);
7004     if ((_return_value == -1) && PyErr_Occurred()) {
7005         goto exit;
7006     }
7007     return_value = PyLong_FromLong(_return_value);
7008 
7009 exit:
7010     return return_value;
7011 }
7012 
7013 #endif /* defined(HAVE_FPATHCONF) */
7014 
7015 #if defined(HAVE_PATHCONF)
7016 
7017 PyDoc_STRVAR(os_pathconf__doc__,
7018 "pathconf($module, /, path, name)\n"
7019 "--\n"
7020 "\n"
7021 "Return the configuration limit name for the file or directory path.\n"
7022 "\n"
7023 "If there is no limit, return -1.\n"
7024 "On some platforms, path may also be specified as an open file descriptor.\n"
7025 "  If this functionality is unavailable, using it raises an exception.");
7026 
7027 #define OS_PATHCONF_METHODDEF    \
7028     {"pathconf", (PyCFunction)(void(*)(void))os_pathconf, METH_FASTCALL|METH_KEYWORDS, os_pathconf__doc__},
7029 
7030 static long
7031 os_pathconf_impl(PyObject *module, path_t *path, int name);
7032 
7033 static PyObject *
os_pathconf(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)7034 os_pathconf(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
7035 {
7036     PyObject *return_value = NULL;
7037     static const char * const _keywords[] = {"path", "name", NULL};
7038     static _PyArg_Parser _parser = {NULL, _keywords, "pathconf", 0};
7039     PyObject *argsbuf[2];
7040     path_t path = PATH_T_INITIALIZE("pathconf", "path", 0, PATH_HAVE_FPATHCONF);
7041     int name;
7042     long _return_value;
7043 
7044     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf);
7045     if (!args) {
7046         goto exit;
7047     }
7048     if (!path_converter(args[0], &path)) {
7049         goto exit;
7050     }
7051     if (!conv_path_confname(args[1], &name)) {
7052         goto exit;
7053     }
7054     _return_value = os_pathconf_impl(module, &path, name);
7055     if ((_return_value == -1) && PyErr_Occurred()) {
7056         goto exit;
7057     }
7058     return_value = PyLong_FromLong(_return_value);
7059 
7060 exit:
7061     /* Cleanup for path */
7062     path_cleanup(&path);
7063 
7064     return return_value;
7065 }
7066 
7067 #endif /* defined(HAVE_PATHCONF) */
7068 
7069 #if defined(HAVE_CONFSTR)
7070 
7071 PyDoc_STRVAR(os_confstr__doc__,
7072 "confstr($module, name, /)\n"
7073 "--\n"
7074 "\n"
7075 "Return a string-valued system configuration variable.");
7076 
7077 #define OS_CONFSTR_METHODDEF    \
7078     {"confstr", (PyCFunction)os_confstr, METH_O, os_confstr__doc__},
7079 
7080 static PyObject *
7081 os_confstr_impl(PyObject *module, int name);
7082 
7083 static PyObject *
os_confstr(PyObject * module,PyObject * arg)7084 os_confstr(PyObject *module, PyObject *arg)
7085 {
7086     PyObject *return_value = NULL;
7087     int name;
7088 
7089     if (!conv_confstr_confname(arg, &name)) {
7090         goto exit;
7091     }
7092     return_value = os_confstr_impl(module, name);
7093 
7094 exit:
7095     return return_value;
7096 }
7097 
7098 #endif /* defined(HAVE_CONFSTR) */
7099 
7100 #if defined(HAVE_SYSCONF)
7101 
7102 PyDoc_STRVAR(os_sysconf__doc__,
7103 "sysconf($module, name, /)\n"
7104 "--\n"
7105 "\n"
7106 "Return an integer-valued system configuration variable.");
7107 
7108 #define OS_SYSCONF_METHODDEF    \
7109     {"sysconf", (PyCFunction)os_sysconf, METH_O, os_sysconf__doc__},
7110 
7111 static long
7112 os_sysconf_impl(PyObject *module, int name);
7113 
7114 static PyObject *
os_sysconf(PyObject * module,PyObject * arg)7115 os_sysconf(PyObject *module, PyObject *arg)
7116 {
7117     PyObject *return_value = NULL;
7118     int name;
7119     long _return_value;
7120 
7121     if (!conv_sysconf_confname(arg, &name)) {
7122         goto exit;
7123     }
7124     _return_value = os_sysconf_impl(module, name);
7125     if ((_return_value == -1) && PyErr_Occurred()) {
7126         goto exit;
7127     }
7128     return_value = PyLong_FromLong(_return_value);
7129 
7130 exit:
7131     return return_value;
7132 }
7133 
7134 #endif /* defined(HAVE_SYSCONF) */
7135 
7136 PyDoc_STRVAR(os_abort__doc__,
7137 "abort($module, /)\n"
7138 "--\n"
7139 "\n"
7140 "Abort the interpreter immediately.\n"
7141 "\n"
7142 "This function \'dumps core\' or otherwise fails in the hardest way possible\n"
7143 "on the hosting operating system.  This function never returns.");
7144 
7145 #define OS_ABORT_METHODDEF    \
7146     {"abort", (PyCFunction)os_abort, METH_NOARGS, os_abort__doc__},
7147 
7148 static PyObject *
7149 os_abort_impl(PyObject *module);
7150 
7151 static PyObject *
os_abort(PyObject * module,PyObject * Py_UNUSED (ignored))7152 os_abort(PyObject *module, PyObject *Py_UNUSED(ignored))
7153 {
7154     return os_abort_impl(module);
7155 }
7156 
7157 #if defined(MS_WINDOWS)
7158 
7159 PyDoc_STRVAR(os_startfile__doc__,
7160 "startfile($module, /, filepath, operation=<unrepresentable>,\n"
7161 "          arguments=<unrepresentable>, cwd=None, show_cmd=1)\n"
7162 "--\n"
7163 "\n"
7164 "Start a file with its associated application.\n"
7165 "\n"
7166 "When \"operation\" is not specified or \"open\", this acts like\n"
7167 "double-clicking the file in Explorer, or giving the file name as an\n"
7168 "argument to the DOS \"start\" command: the file is opened with whatever\n"
7169 "application (if any) its extension is associated.\n"
7170 "When another \"operation\" is given, it specifies what should be done with\n"
7171 "the file.  A typical operation is \"print\".\n"
7172 "\n"
7173 "\"arguments\" is passed to the application, but should be omitted if the\n"
7174 "file is a document.\n"
7175 "\n"
7176 "\"cwd\" is the working directory for the operation. If \"filepath\" is\n"
7177 "relative, it will be resolved against this directory. This argument\n"
7178 "should usually be an absolute path.\n"
7179 "\n"
7180 "\"show_cmd\" can be used to override the recommended visibility option.\n"
7181 "See the Windows ShellExecute documentation for values.\n"
7182 "\n"
7183 "startfile returns as soon as the associated application is launched.\n"
7184 "There is no option to wait for the application to close, and no way\n"
7185 "to retrieve the application\'s exit status.\n"
7186 "\n"
7187 "The filepath is relative to the current directory.  If you want to use\n"
7188 "an absolute path, make sure the first character is not a slash (\"/\");\n"
7189 "the underlying Win32 ShellExecute function doesn\'t work if it is.");
7190 
7191 #define OS_STARTFILE_METHODDEF    \
7192     {"startfile", (PyCFunction)(void(*)(void))os_startfile, METH_FASTCALL|METH_KEYWORDS, os_startfile__doc__},
7193 
7194 static PyObject *
7195 os_startfile_impl(PyObject *module, path_t *filepath,
7196                   const Py_UNICODE *operation, const Py_UNICODE *arguments,
7197                   path_t *cwd, int show_cmd);
7198 
7199 static PyObject *
os_startfile(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)7200 os_startfile(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
7201 {
7202     PyObject *return_value = NULL;
7203     static const char * const _keywords[] = {"filepath", "operation", "arguments", "cwd", "show_cmd", NULL};
7204     static _PyArg_Parser _parser = {NULL, _keywords, "startfile", 0};
7205     PyObject *argsbuf[5];
7206     Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
7207     path_t filepath = PATH_T_INITIALIZE("startfile", "filepath", 0, 0);
7208     const Py_UNICODE *operation = NULL;
7209     const Py_UNICODE *arguments = NULL;
7210     path_t cwd = PATH_T_INITIALIZE("startfile", "cwd", 1, 0);
7211     int show_cmd = 1;
7212 
7213     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 5, 0, argsbuf);
7214     if (!args) {
7215         goto exit;
7216     }
7217     if (!path_converter(args[0], &filepath)) {
7218         goto exit;
7219     }
7220     if (!noptargs) {
7221         goto skip_optional_pos;
7222     }
7223     if (args[1]) {
7224         if (!PyUnicode_Check(args[1])) {
7225             _PyArg_BadArgument("startfile", "argument 'operation'", "str", args[1]);
7226             goto exit;
7227         }
7228         #if USE_UNICODE_WCHAR_CACHE
7229         operation = _PyUnicode_AsUnicode(args[1]);
7230         #else /* USE_UNICODE_WCHAR_CACHE */
7231         operation = PyUnicode_AsWideCharString(args[1], NULL);
7232         #endif /* USE_UNICODE_WCHAR_CACHE */
7233         if (operation == NULL) {
7234             goto exit;
7235         }
7236         if (!--noptargs) {
7237             goto skip_optional_pos;
7238         }
7239     }
7240     if (args[2]) {
7241         if (!PyUnicode_Check(args[2])) {
7242             _PyArg_BadArgument("startfile", "argument 'arguments'", "str", args[2]);
7243             goto exit;
7244         }
7245         #if USE_UNICODE_WCHAR_CACHE
7246         arguments = _PyUnicode_AsUnicode(args[2]);
7247         #else /* USE_UNICODE_WCHAR_CACHE */
7248         arguments = PyUnicode_AsWideCharString(args[2], NULL);
7249         #endif /* USE_UNICODE_WCHAR_CACHE */
7250         if (arguments == NULL) {
7251             goto exit;
7252         }
7253         if (!--noptargs) {
7254             goto skip_optional_pos;
7255         }
7256     }
7257     if (args[3]) {
7258         if (!path_converter(args[3], &cwd)) {
7259             goto exit;
7260         }
7261         if (!--noptargs) {
7262             goto skip_optional_pos;
7263         }
7264     }
7265     show_cmd = _PyLong_AsInt(args[4]);
7266     if (show_cmd == -1 && PyErr_Occurred()) {
7267         goto exit;
7268     }
7269 skip_optional_pos:
7270     return_value = os_startfile_impl(module, &filepath, operation, arguments, &cwd, show_cmd);
7271 
7272 exit:
7273     /* Cleanup for filepath */
7274     path_cleanup(&filepath);
7275     /* Cleanup for operation */
7276     #if !USE_UNICODE_WCHAR_CACHE
7277     PyMem_Free((void *)operation);
7278     #endif /* USE_UNICODE_WCHAR_CACHE */
7279     /* Cleanup for arguments */
7280     #if !USE_UNICODE_WCHAR_CACHE
7281     PyMem_Free((void *)arguments);
7282     #endif /* USE_UNICODE_WCHAR_CACHE */
7283     /* Cleanup for cwd */
7284     path_cleanup(&cwd);
7285 
7286     return return_value;
7287 }
7288 
7289 #endif /* defined(MS_WINDOWS) */
7290 
7291 #if defined(HAVE_GETLOADAVG)
7292 
7293 PyDoc_STRVAR(os_getloadavg__doc__,
7294 "getloadavg($module, /)\n"
7295 "--\n"
7296 "\n"
7297 "Return average recent system load information.\n"
7298 "\n"
7299 "Return the number of processes in the system run queue averaged over\n"
7300 "the last 1, 5, and 15 minutes as a tuple of three floats.\n"
7301 "Raises OSError if the load average was unobtainable.");
7302 
7303 #define OS_GETLOADAVG_METHODDEF    \
7304     {"getloadavg", (PyCFunction)os_getloadavg, METH_NOARGS, os_getloadavg__doc__},
7305 
7306 static PyObject *
7307 os_getloadavg_impl(PyObject *module);
7308 
7309 static PyObject *
os_getloadavg(PyObject * module,PyObject * Py_UNUSED (ignored))7310 os_getloadavg(PyObject *module, PyObject *Py_UNUSED(ignored))
7311 {
7312     return os_getloadavg_impl(module);
7313 }
7314 
7315 #endif /* defined(HAVE_GETLOADAVG) */
7316 
7317 PyDoc_STRVAR(os_device_encoding__doc__,
7318 "device_encoding($module, /, fd)\n"
7319 "--\n"
7320 "\n"
7321 "Return a string describing the encoding of a terminal\'s file descriptor.\n"
7322 "\n"
7323 "The file descriptor must be attached to a terminal.\n"
7324 "If the device is not a terminal, return None.");
7325 
7326 #define OS_DEVICE_ENCODING_METHODDEF    \
7327     {"device_encoding", (PyCFunction)(void(*)(void))os_device_encoding, METH_FASTCALL|METH_KEYWORDS, os_device_encoding__doc__},
7328 
7329 static PyObject *
7330 os_device_encoding_impl(PyObject *module, int fd);
7331 
7332 static PyObject *
os_device_encoding(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)7333 os_device_encoding(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
7334 {
7335     PyObject *return_value = NULL;
7336     static const char * const _keywords[] = {"fd", NULL};
7337     static _PyArg_Parser _parser = {NULL, _keywords, "device_encoding", 0};
7338     PyObject *argsbuf[1];
7339     int fd;
7340 
7341     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
7342     if (!args) {
7343         goto exit;
7344     }
7345     fd = _PyLong_AsInt(args[0]);
7346     if (fd == -1 && PyErr_Occurred()) {
7347         goto exit;
7348     }
7349     return_value = os_device_encoding_impl(module, fd);
7350 
7351 exit:
7352     return return_value;
7353 }
7354 
7355 #if defined(HAVE_SETRESUID)
7356 
7357 PyDoc_STRVAR(os_setresuid__doc__,
7358 "setresuid($module, ruid, euid, suid, /)\n"
7359 "--\n"
7360 "\n"
7361 "Set the current process\'s real, effective, and saved user ids.");
7362 
7363 #define OS_SETRESUID_METHODDEF    \
7364     {"setresuid", (PyCFunction)(void(*)(void))os_setresuid, METH_FASTCALL, os_setresuid__doc__},
7365 
7366 static PyObject *
7367 os_setresuid_impl(PyObject *module, uid_t ruid, uid_t euid, uid_t suid);
7368 
7369 static PyObject *
os_setresuid(PyObject * module,PyObject * const * args,Py_ssize_t nargs)7370 os_setresuid(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
7371 {
7372     PyObject *return_value = NULL;
7373     uid_t ruid;
7374     uid_t euid;
7375     uid_t suid;
7376 
7377     if (!_PyArg_CheckPositional("setresuid", nargs, 3, 3)) {
7378         goto exit;
7379     }
7380     if (!_Py_Uid_Converter(args[0], &ruid)) {
7381         goto exit;
7382     }
7383     if (!_Py_Uid_Converter(args[1], &euid)) {
7384         goto exit;
7385     }
7386     if (!_Py_Uid_Converter(args[2], &suid)) {
7387         goto exit;
7388     }
7389     return_value = os_setresuid_impl(module, ruid, euid, suid);
7390 
7391 exit:
7392     return return_value;
7393 }
7394 
7395 #endif /* defined(HAVE_SETRESUID) */
7396 
7397 #if defined(HAVE_SETRESGID)
7398 
7399 PyDoc_STRVAR(os_setresgid__doc__,
7400 "setresgid($module, rgid, egid, sgid, /)\n"
7401 "--\n"
7402 "\n"
7403 "Set the current process\'s real, effective, and saved group ids.");
7404 
7405 #define OS_SETRESGID_METHODDEF    \
7406     {"setresgid", (PyCFunction)(void(*)(void))os_setresgid, METH_FASTCALL, os_setresgid__doc__},
7407 
7408 static PyObject *
7409 os_setresgid_impl(PyObject *module, gid_t rgid, gid_t egid, gid_t sgid);
7410 
7411 static PyObject *
os_setresgid(PyObject * module,PyObject * const * args,Py_ssize_t nargs)7412 os_setresgid(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
7413 {
7414     PyObject *return_value = NULL;
7415     gid_t rgid;
7416     gid_t egid;
7417     gid_t sgid;
7418 
7419     if (!_PyArg_CheckPositional("setresgid", nargs, 3, 3)) {
7420         goto exit;
7421     }
7422     if (!_Py_Gid_Converter(args[0], &rgid)) {
7423         goto exit;
7424     }
7425     if (!_Py_Gid_Converter(args[1], &egid)) {
7426         goto exit;
7427     }
7428     if (!_Py_Gid_Converter(args[2], &sgid)) {
7429         goto exit;
7430     }
7431     return_value = os_setresgid_impl(module, rgid, egid, sgid);
7432 
7433 exit:
7434     return return_value;
7435 }
7436 
7437 #endif /* defined(HAVE_SETRESGID) */
7438 
7439 #if defined(HAVE_GETRESUID)
7440 
7441 PyDoc_STRVAR(os_getresuid__doc__,
7442 "getresuid($module, /)\n"
7443 "--\n"
7444 "\n"
7445 "Return a tuple of the current process\'s real, effective, and saved user ids.");
7446 
7447 #define OS_GETRESUID_METHODDEF    \
7448     {"getresuid", (PyCFunction)os_getresuid, METH_NOARGS, os_getresuid__doc__},
7449 
7450 static PyObject *
7451 os_getresuid_impl(PyObject *module);
7452 
7453 static PyObject *
os_getresuid(PyObject * module,PyObject * Py_UNUSED (ignored))7454 os_getresuid(PyObject *module, PyObject *Py_UNUSED(ignored))
7455 {
7456     return os_getresuid_impl(module);
7457 }
7458 
7459 #endif /* defined(HAVE_GETRESUID) */
7460 
7461 #if defined(HAVE_GETRESGID)
7462 
7463 PyDoc_STRVAR(os_getresgid__doc__,
7464 "getresgid($module, /)\n"
7465 "--\n"
7466 "\n"
7467 "Return a tuple of the current process\'s real, effective, and saved group ids.");
7468 
7469 #define OS_GETRESGID_METHODDEF    \
7470     {"getresgid", (PyCFunction)os_getresgid, METH_NOARGS, os_getresgid__doc__},
7471 
7472 static PyObject *
7473 os_getresgid_impl(PyObject *module);
7474 
7475 static PyObject *
os_getresgid(PyObject * module,PyObject * Py_UNUSED (ignored))7476 os_getresgid(PyObject *module, PyObject *Py_UNUSED(ignored))
7477 {
7478     return os_getresgid_impl(module);
7479 }
7480 
7481 #endif /* defined(HAVE_GETRESGID) */
7482 
7483 #if defined(USE_XATTRS)
7484 
7485 PyDoc_STRVAR(os_getxattr__doc__,
7486 "getxattr($module, /, path, attribute, *, follow_symlinks=True)\n"
7487 "--\n"
7488 "\n"
7489 "Return the value of extended attribute attribute on path.\n"
7490 "\n"
7491 "path may be either a string, a path-like object, or an open file descriptor.\n"
7492 "If follow_symlinks is False, and the last element of the path is a symbolic\n"
7493 "  link, getxattr will examine the symbolic link itself instead of the file\n"
7494 "  the link points to.");
7495 
7496 #define OS_GETXATTR_METHODDEF    \
7497     {"getxattr", (PyCFunction)(void(*)(void))os_getxattr, METH_FASTCALL|METH_KEYWORDS, os_getxattr__doc__},
7498 
7499 static PyObject *
7500 os_getxattr_impl(PyObject *module, path_t *path, path_t *attribute,
7501                  int follow_symlinks);
7502 
7503 static PyObject *
os_getxattr(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)7504 os_getxattr(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
7505 {
7506     PyObject *return_value = NULL;
7507     static const char * const _keywords[] = {"path", "attribute", "follow_symlinks", NULL};
7508     static _PyArg_Parser _parser = {NULL, _keywords, "getxattr", 0};
7509     PyObject *argsbuf[3];
7510     Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
7511     path_t path = PATH_T_INITIALIZE("getxattr", "path", 0, 1);
7512     path_t attribute = PATH_T_INITIALIZE("getxattr", "attribute", 0, 0);
7513     int follow_symlinks = 1;
7514 
7515     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf);
7516     if (!args) {
7517         goto exit;
7518     }
7519     if (!path_converter(args[0], &path)) {
7520         goto exit;
7521     }
7522     if (!path_converter(args[1], &attribute)) {
7523         goto exit;
7524     }
7525     if (!noptargs) {
7526         goto skip_optional_kwonly;
7527     }
7528     follow_symlinks = PyObject_IsTrue(args[2]);
7529     if (follow_symlinks < 0) {
7530         goto exit;
7531     }
7532 skip_optional_kwonly:
7533     return_value = os_getxattr_impl(module, &path, &attribute, follow_symlinks);
7534 
7535 exit:
7536     /* Cleanup for path */
7537     path_cleanup(&path);
7538     /* Cleanup for attribute */
7539     path_cleanup(&attribute);
7540 
7541     return return_value;
7542 }
7543 
7544 #endif /* defined(USE_XATTRS) */
7545 
7546 #if defined(USE_XATTRS)
7547 
7548 PyDoc_STRVAR(os_setxattr__doc__,
7549 "setxattr($module, /, path, attribute, value, flags=0, *,\n"
7550 "         follow_symlinks=True)\n"
7551 "--\n"
7552 "\n"
7553 "Set extended attribute attribute on path to value.\n"
7554 "\n"
7555 "path may be either a string, a path-like object,  or an open file descriptor.\n"
7556 "If follow_symlinks is False, and the last element of the path is a symbolic\n"
7557 "  link, setxattr will modify the symbolic link itself instead of the file\n"
7558 "  the link points to.");
7559 
7560 #define OS_SETXATTR_METHODDEF    \
7561     {"setxattr", (PyCFunction)(void(*)(void))os_setxattr, METH_FASTCALL|METH_KEYWORDS, os_setxattr__doc__},
7562 
7563 static PyObject *
7564 os_setxattr_impl(PyObject *module, path_t *path, path_t *attribute,
7565                  Py_buffer *value, int flags, int follow_symlinks);
7566 
7567 static PyObject *
os_setxattr(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)7568 os_setxattr(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
7569 {
7570     PyObject *return_value = NULL;
7571     static const char * const _keywords[] = {"path", "attribute", "value", "flags", "follow_symlinks", NULL};
7572     static _PyArg_Parser _parser = {NULL, _keywords, "setxattr", 0};
7573     PyObject *argsbuf[5];
7574     Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 3;
7575     path_t path = PATH_T_INITIALIZE("setxattr", "path", 0, 1);
7576     path_t attribute = PATH_T_INITIALIZE("setxattr", "attribute", 0, 0);
7577     Py_buffer value = {NULL, NULL};
7578     int flags = 0;
7579     int follow_symlinks = 1;
7580 
7581     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 3, 4, 0, argsbuf);
7582     if (!args) {
7583         goto exit;
7584     }
7585     if (!path_converter(args[0], &path)) {
7586         goto exit;
7587     }
7588     if (!path_converter(args[1], &attribute)) {
7589         goto exit;
7590     }
7591     if (PyObject_GetBuffer(args[2], &value, PyBUF_SIMPLE) != 0) {
7592         goto exit;
7593     }
7594     if (!PyBuffer_IsContiguous(&value, 'C')) {
7595         _PyArg_BadArgument("setxattr", "argument 'value'", "contiguous buffer", args[2]);
7596         goto exit;
7597     }
7598     if (!noptargs) {
7599         goto skip_optional_pos;
7600     }
7601     if (args[3]) {
7602         flags = _PyLong_AsInt(args[3]);
7603         if (flags == -1 && PyErr_Occurred()) {
7604             goto exit;
7605         }
7606         if (!--noptargs) {
7607             goto skip_optional_pos;
7608         }
7609     }
7610 skip_optional_pos:
7611     if (!noptargs) {
7612         goto skip_optional_kwonly;
7613     }
7614     follow_symlinks = PyObject_IsTrue(args[4]);
7615     if (follow_symlinks < 0) {
7616         goto exit;
7617     }
7618 skip_optional_kwonly:
7619     return_value = os_setxattr_impl(module, &path, &attribute, &value, flags, follow_symlinks);
7620 
7621 exit:
7622     /* Cleanup for path */
7623     path_cleanup(&path);
7624     /* Cleanup for attribute */
7625     path_cleanup(&attribute);
7626     /* Cleanup for value */
7627     if (value.obj) {
7628        PyBuffer_Release(&value);
7629     }
7630 
7631     return return_value;
7632 }
7633 
7634 #endif /* defined(USE_XATTRS) */
7635 
7636 #if defined(USE_XATTRS)
7637 
7638 PyDoc_STRVAR(os_removexattr__doc__,
7639 "removexattr($module, /, path, attribute, *, follow_symlinks=True)\n"
7640 "--\n"
7641 "\n"
7642 "Remove extended attribute attribute on path.\n"
7643 "\n"
7644 "path may be either a string, a path-like object, or an open file descriptor.\n"
7645 "If follow_symlinks is False, and the last element of the path is a symbolic\n"
7646 "  link, removexattr will modify the symbolic link itself instead of the file\n"
7647 "  the link points to.");
7648 
7649 #define OS_REMOVEXATTR_METHODDEF    \
7650     {"removexattr", (PyCFunction)(void(*)(void))os_removexattr, METH_FASTCALL|METH_KEYWORDS, os_removexattr__doc__},
7651 
7652 static PyObject *
7653 os_removexattr_impl(PyObject *module, path_t *path, path_t *attribute,
7654                     int follow_symlinks);
7655 
7656 static PyObject *
os_removexattr(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)7657 os_removexattr(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
7658 {
7659     PyObject *return_value = NULL;
7660     static const char * const _keywords[] = {"path", "attribute", "follow_symlinks", NULL};
7661     static _PyArg_Parser _parser = {NULL, _keywords, "removexattr", 0};
7662     PyObject *argsbuf[3];
7663     Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
7664     path_t path = PATH_T_INITIALIZE("removexattr", "path", 0, 1);
7665     path_t attribute = PATH_T_INITIALIZE("removexattr", "attribute", 0, 0);
7666     int follow_symlinks = 1;
7667 
7668     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf);
7669     if (!args) {
7670         goto exit;
7671     }
7672     if (!path_converter(args[0], &path)) {
7673         goto exit;
7674     }
7675     if (!path_converter(args[1], &attribute)) {
7676         goto exit;
7677     }
7678     if (!noptargs) {
7679         goto skip_optional_kwonly;
7680     }
7681     follow_symlinks = PyObject_IsTrue(args[2]);
7682     if (follow_symlinks < 0) {
7683         goto exit;
7684     }
7685 skip_optional_kwonly:
7686     return_value = os_removexattr_impl(module, &path, &attribute, follow_symlinks);
7687 
7688 exit:
7689     /* Cleanup for path */
7690     path_cleanup(&path);
7691     /* Cleanup for attribute */
7692     path_cleanup(&attribute);
7693 
7694     return return_value;
7695 }
7696 
7697 #endif /* defined(USE_XATTRS) */
7698 
7699 #if defined(USE_XATTRS)
7700 
7701 PyDoc_STRVAR(os_listxattr__doc__,
7702 "listxattr($module, /, path=None, *, follow_symlinks=True)\n"
7703 "--\n"
7704 "\n"
7705 "Return a list of extended attributes on path.\n"
7706 "\n"
7707 "path may be either None, a string, a path-like object, or an open file descriptor.\n"
7708 "if path is None, listxattr will examine the current directory.\n"
7709 "If follow_symlinks is False, and the last element of the path is a symbolic\n"
7710 "  link, listxattr will examine the symbolic link itself instead of the file\n"
7711 "  the link points to.");
7712 
7713 #define OS_LISTXATTR_METHODDEF    \
7714     {"listxattr", (PyCFunction)(void(*)(void))os_listxattr, METH_FASTCALL|METH_KEYWORDS, os_listxattr__doc__},
7715 
7716 static PyObject *
7717 os_listxattr_impl(PyObject *module, path_t *path, int follow_symlinks);
7718 
7719 static PyObject *
os_listxattr(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)7720 os_listxattr(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
7721 {
7722     PyObject *return_value = NULL;
7723     static const char * const _keywords[] = {"path", "follow_symlinks", NULL};
7724     static _PyArg_Parser _parser = {NULL, _keywords, "listxattr", 0};
7725     PyObject *argsbuf[2];
7726     Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
7727     path_t path = PATH_T_INITIALIZE("listxattr", "path", 1, 1);
7728     int follow_symlinks = 1;
7729 
7730     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
7731     if (!args) {
7732         goto exit;
7733     }
7734     if (!noptargs) {
7735         goto skip_optional_pos;
7736     }
7737     if (args[0]) {
7738         if (!path_converter(args[0], &path)) {
7739             goto exit;
7740         }
7741         if (!--noptargs) {
7742             goto skip_optional_pos;
7743         }
7744     }
7745 skip_optional_pos:
7746     if (!noptargs) {
7747         goto skip_optional_kwonly;
7748     }
7749     follow_symlinks = PyObject_IsTrue(args[1]);
7750     if (follow_symlinks < 0) {
7751         goto exit;
7752     }
7753 skip_optional_kwonly:
7754     return_value = os_listxattr_impl(module, &path, follow_symlinks);
7755 
7756 exit:
7757     /* Cleanup for path */
7758     path_cleanup(&path);
7759 
7760     return return_value;
7761 }
7762 
7763 #endif /* defined(USE_XATTRS) */
7764 
7765 PyDoc_STRVAR(os_urandom__doc__,
7766 "urandom($module, size, /)\n"
7767 "--\n"
7768 "\n"
7769 "Return a bytes object containing random bytes suitable for cryptographic use.");
7770 
7771 #define OS_URANDOM_METHODDEF    \
7772     {"urandom", (PyCFunction)os_urandom, METH_O, os_urandom__doc__},
7773 
7774 static PyObject *
7775 os_urandom_impl(PyObject *module, Py_ssize_t size);
7776 
7777 static PyObject *
os_urandom(PyObject * module,PyObject * arg)7778 os_urandom(PyObject *module, PyObject *arg)
7779 {
7780     PyObject *return_value = NULL;
7781     Py_ssize_t size;
7782 
7783     {
7784         Py_ssize_t ival = -1;
7785         PyObject *iobj = _PyNumber_Index(arg);
7786         if (iobj != NULL) {
7787             ival = PyLong_AsSsize_t(iobj);
7788             Py_DECREF(iobj);
7789         }
7790         if (ival == -1 && PyErr_Occurred()) {
7791             goto exit;
7792         }
7793         size = ival;
7794     }
7795     return_value = os_urandom_impl(module, size);
7796 
7797 exit:
7798     return return_value;
7799 }
7800 
7801 #if defined(HAVE_MEMFD_CREATE)
7802 
7803 PyDoc_STRVAR(os_memfd_create__doc__,
7804 "memfd_create($module, /, name, flags=MFD_CLOEXEC)\n"
7805 "--\n"
7806 "\n");
7807 
7808 #define OS_MEMFD_CREATE_METHODDEF    \
7809     {"memfd_create", (PyCFunction)(void(*)(void))os_memfd_create, METH_FASTCALL|METH_KEYWORDS, os_memfd_create__doc__},
7810 
7811 static PyObject *
7812 os_memfd_create_impl(PyObject *module, PyObject *name, unsigned int flags);
7813 
7814 static PyObject *
os_memfd_create(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)7815 os_memfd_create(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
7816 {
7817     PyObject *return_value = NULL;
7818     static const char * const _keywords[] = {"name", "flags", NULL};
7819     static _PyArg_Parser _parser = {NULL, _keywords, "memfd_create", 0};
7820     PyObject *argsbuf[2];
7821     Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
7822     PyObject *name = NULL;
7823     unsigned int flags = MFD_CLOEXEC;
7824 
7825     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
7826     if (!args) {
7827         goto exit;
7828     }
7829     if (!PyUnicode_FSConverter(args[0], &name)) {
7830         goto exit;
7831     }
7832     if (!noptargs) {
7833         goto skip_optional_pos;
7834     }
7835     flags = (unsigned int)PyLong_AsUnsignedLongMask(args[1]);
7836     if (flags == (unsigned int)-1 && PyErr_Occurred()) {
7837         goto exit;
7838     }
7839 skip_optional_pos:
7840     return_value = os_memfd_create_impl(module, name, flags);
7841 
7842 exit:
7843     /* Cleanup for name */
7844     Py_XDECREF(name);
7845 
7846     return return_value;
7847 }
7848 
7849 #endif /* defined(HAVE_MEMFD_CREATE) */
7850 
7851 #if defined(HAVE_EVENTFD)
7852 
7853 PyDoc_STRVAR(os_eventfd__doc__,
7854 "eventfd($module, /, initval, flags=EFD_CLOEXEC)\n"
7855 "--\n"
7856 "\n"
7857 "Creates and returns an event notification file descriptor.");
7858 
7859 #define OS_EVENTFD_METHODDEF    \
7860     {"eventfd", (PyCFunction)(void(*)(void))os_eventfd, METH_FASTCALL|METH_KEYWORDS, os_eventfd__doc__},
7861 
7862 static PyObject *
7863 os_eventfd_impl(PyObject *module, unsigned int initval, int flags);
7864 
7865 static PyObject *
os_eventfd(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)7866 os_eventfd(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
7867 {
7868     PyObject *return_value = NULL;
7869     static const char * const _keywords[] = {"initval", "flags", NULL};
7870     static _PyArg_Parser _parser = {NULL, _keywords, "eventfd", 0};
7871     PyObject *argsbuf[2];
7872     Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
7873     unsigned int initval;
7874     int flags = EFD_CLOEXEC;
7875 
7876     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
7877     if (!args) {
7878         goto exit;
7879     }
7880     if (!_PyLong_UnsignedInt_Converter(args[0], &initval)) {
7881         goto exit;
7882     }
7883     if (!noptargs) {
7884         goto skip_optional_pos;
7885     }
7886     flags = _PyLong_AsInt(args[1]);
7887     if (flags == -1 && PyErr_Occurred()) {
7888         goto exit;
7889     }
7890 skip_optional_pos:
7891     return_value = os_eventfd_impl(module, initval, flags);
7892 
7893 exit:
7894     return return_value;
7895 }
7896 
7897 #endif /* defined(HAVE_EVENTFD) */
7898 
7899 #if defined(HAVE_EVENTFD)
7900 
7901 PyDoc_STRVAR(os_eventfd_read__doc__,
7902 "eventfd_read($module, /, fd)\n"
7903 "--\n"
7904 "\n"
7905 "Read eventfd value");
7906 
7907 #define OS_EVENTFD_READ_METHODDEF    \
7908     {"eventfd_read", (PyCFunction)(void(*)(void))os_eventfd_read, METH_FASTCALL|METH_KEYWORDS, os_eventfd_read__doc__},
7909 
7910 static PyObject *
7911 os_eventfd_read_impl(PyObject *module, int fd);
7912 
7913 static PyObject *
os_eventfd_read(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)7914 os_eventfd_read(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
7915 {
7916     PyObject *return_value = NULL;
7917     static const char * const _keywords[] = {"fd", NULL};
7918     static _PyArg_Parser _parser = {NULL, _keywords, "eventfd_read", 0};
7919     PyObject *argsbuf[1];
7920     int fd;
7921 
7922     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
7923     if (!args) {
7924         goto exit;
7925     }
7926     if (!_PyLong_FileDescriptor_Converter(args[0], &fd)) {
7927         goto exit;
7928     }
7929     return_value = os_eventfd_read_impl(module, fd);
7930 
7931 exit:
7932     return return_value;
7933 }
7934 
7935 #endif /* defined(HAVE_EVENTFD) */
7936 
7937 #if defined(HAVE_EVENTFD)
7938 
7939 PyDoc_STRVAR(os_eventfd_write__doc__,
7940 "eventfd_write($module, /, fd, value)\n"
7941 "--\n"
7942 "\n"
7943 "Write eventfd value.");
7944 
7945 #define OS_EVENTFD_WRITE_METHODDEF    \
7946     {"eventfd_write", (PyCFunction)(void(*)(void))os_eventfd_write, METH_FASTCALL|METH_KEYWORDS, os_eventfd_write__doc__},
7947 
7948 static PyObject *
7949 os_eventfd_write_impl(PyObject *module, int fd, unsigned long long value);
7950 
7951 static PyObject *
os_eventfd_write(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)7952 os_eventfd_write(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
7953 {
7954     PyObject *return_value = NULL;
7955     static const char * const _keywords[] = {"fd", "value", NULL};
7956     static _PyArg_Parser _parser = {NULL, _keywords, "eventfd_write", 0};
7957     PyObject *argsbuf[2];
7958     int fd;
7959     unsigned long long value;
7960 
7961     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf);
7962     if (!args) {
7963         goto exit;
7964     }
7965     if (!_PyLong_FileDescriptor_Converter(args[0], &fd)) {
7966         goto exit;
7967     }
7968     if (!_PyLong_UnsignedLongLong_Converter(args[1], &value)) {
7969         goto exit;
7970     }
7971     return_value = os_eventfd_write_impl(module, fd, value);
7972 
7973 exit:
7974     return return_value;
7975 }
7976 
7977 #endif /* defined(HAVE_EVENTFD) */
7978 
7979 #if (defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL))
7980 
7981 PyDoc_STRVAR(os_get_terminal_size__doc__,
7982 "get_terminal_size($module, fd=<unrepresentable>, /)\n"
7983 "--\n"
7984 "\n"
7985 "Return the size of the terminal window as (columns, lines).\n"
7986 "\n"
7987 "The optional argument fd (default standard output) specifies\n"
7988 "which file descriptor should be queried.\n"
7989 "\n"
7990 "If the file descriptor is not connected to a terminal, an OSError\n"
7991 "is thrown.\n"
7992 "\n"
7993 "This function will only be defined if an implementation is\n"
7994 "available for this system.\n"
7995 "\n"
7996 "shutil.get_terminal_size is the high-level function which should\n"
7997 "normally be used, os.get_terminal_size is the low-level implementation.");
7998 
7999 #define OS_GET_TERMINAL_SIZE_METHODDEF    \
8000     {"get_terminal_size", (PyCFunction)(void(*)(void))os_get_terminal_size, METH_FASTCALL, os_get_terminal_size__doc__},
8001 
8002 static PyObject *
8003 os_get_terminal_size_impl(PyObject *module, int fd);
8004 
8005 static PyObject *
os_get_terminal_size(PyObject * module,PyObject * const * args,Py_ssize_t nargs)8006 os_get_terminal_size(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
8007 {
8008     PyObject *return_value = NULL;
8009     int fd = fileno(stdout);
8010 
8011     if (!_PyArg_CheckPositional("get_terminal_size", nargs, 0, 1)) {
8012         goto exit;
8013     }
8014     if (nargs < 1) {
8015         goto skip_optional;
8016     }
8017     fd = _PyLong_AsInt(args[0]);
8018     if (fd == -1 && PyErr_Occurred()) {
8019         goto exit;
8020     }
8021 skip_optional:
8022     return_value = os_get_terminal_size_impl(module, fd);
8023 
8024 exit:
8025     return return_value;
8026 }
8027 
8028 #endif /* (defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL)) */
8029 
8030 PyDoc_STRVAR(os_cpu_count__doc__,
8031 "cpu_count($module, /)\n"
8032 "--\n"
8033 "\n"
8034 "Return the number of CPUs in the system; return None if indeterminable.\n"
8035 "\n"
8036 "This number is not equivalent to the number of CPUs the current process can\n"
8037 "use.  The number of usable CPUs can be obtained with\n"
8038 "``len(os.sched_getaffinity(0))``");
8039 
8040 #define OS_CPU_COUNT_METHODDEF    \
8041     {"cpu_count", (PyCFunction)os_cpu_count, METH_NOARGS, os_cpu_count__doc__},
8042 
8043 static PyObject *
8044 os_cpu_count_impl(PyObject *module);
8045 
8046 static PyObject *
os_cpu_count(PyObject * module,PyObject * Py_UNUSED (ignored))8047 os_cpu_count(PyObject *module, PyObject *Py_UNUSED(ignored))
8048 {
8049     return os_cpu_count_impl(module);
8050 }
8051 
8052 PyDoc_STRVAR(os_get_inheritable__doc__,
8053 "get_inheritable($module, fd, /)\n"
8054 "--\n"
8055 "\n"
8056 "Get the close-on-exe flag of the specified file descriptor.");
8057 
8058 #define OS_GET_INHERITABLE_METHODDEF    \
8059     {"get_inheritable", (PyCFunction)os_get_inheritable, METH_O, os_get_inheritable__doc__},
8060 
8061 static int
8062 os_get_inheritable_impl(PyObject *module, int fd);
8063 
8064 static PyObject *
os_get_inheritable(PyObject * module,PyObject * arg)8065 os_get_inheritable(PyObject *module, PyObject *arg)
8066 {
8067     PyObject *return_value = NULL;
8068     int fd;
8069     int _return_value;
8070 
8071     fd = _PyLong_AsInt(arg);
8072     if (fd == -1 && PyErr_Occurred()) {
8073         goto exit;
8074     }
8075     _return_value = os_get_inheritable_impl(module, fd);
8076     if ((_return_value == -1) && PyErr_Occurred()) {
8077         goto exit;
8078     }
8079     return_value = PyBool_FromLong((long)_return_value);
8080 
8081 exit:
8082     return return_value;
8083 }
8084 
8085 PyDoc_STRVAR(os_set_inheritable__doc__,
8086 "set_inheritable($module, fd, inheritable, /)\n"
8087 "--\n"
8088 "\n"
8089 "Set the inheritable flag of the specified file descriptor.");
8090 
8091 #define OS_SET_INHERITABLE_METHODDEF    \
8092     {"set_inheritable", (PyCFunction)(void(*)(void))os_set_inheritable, METH_FASTCALL, os_set_inheritable__doc__},
8093 
8094 static PyObject *
8095 os_set_inheritable_impl(PyObject *module, int fd, int inheritable);
8096 
8097 static PyObject *
os_set_inheritable(PyObject * module,PyObject * const * args,Py_ssize_t nargs)8098 os_set_inheritable(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
8099 {
8100     PyObject *return_value = NULL;
8101     int fd;
8102     int inheritable;
8103 
8104     if (!_PyArg_CheckPositional("set_inheritable", nargs, 2, 2)) {
8105         goto exit;
8106     }
8107     fd = _PyLong_AsInt(args[0]);
8108     if (fd == -1 && PyErr_Occurred()) {
8109         goto exit;
8110     }
8111     inheritable = _PyLong_AsInt(args[1]);
8112     if (inheritable == -1 && PyErr_Occurred()) {
8113         goto exit;
8114     }
8115     return_value = os_set_inheritable_impl(module, fd, inheritable);
8116 
8117 exit:
8118     return return_value;
8119 }
8120 
8121 #if defined(MS_WINDOWS)
8122 
8123 PyDoc_STRVAR(os_get_handle_inheritable__doc__,
8124 "get_handle_inheritable($module, handle, /)\n"
8125 "--\n"
8126 "\n"
8127 "Get the close-on-exe flag of the specified file descriptor.");
8128 
8129 #define OS_GET_HANDLE_INHERITABLE_METHODDEF    \
8130     {"get_handle_inheritable", (PyCFunction)os_get_handle_inheritable, METH_O, os_get_handle_inheritable__doc__},
8131 
8132 static int
8133 os_get_handle_inheritable_impl(PyObject *module, intptr_t handle);
8134 
8135 static PyObject *
os_get_handle_inheritable(PyObject * module,PyObject * arg)8136 os_get_handle_inheritable(PyObject *module, PyObject *arg)
8137 {
8138     PyObject *return_value = NULL;
8139     intptr_t handle;
8140     int _return_value;
8141 
8142     if (!PyArg_Parse(arg, "" _Py_PARSE_INTPTR ":get_handle_inheritable", &handle)) {
8143         goto exit;
8144     }
8145     _return_value = os_get_handle_inheritable_impl(module, handle);
8146     if ((_return_value == -1) && PyErr_Occurred()) {
8147         goto exit;
8148     }
8149     return_value = PyBool_FromLong((long)_return_value);
8150 
8151 exit:
8152     return return_value;
8153 }
8154 
8155 #endif /* defined(MS_WINDOWS) */
8156 
8157 #if defined(MS_WINDOWS)
8158 
8159 PyDoc_STRVAR(os_set_handle_inheritable__doc__,
8160 "set_handle_inheritable($module, handle, inheritable, /)\n"
8161 "--\n"
8162 "\n"
8163 "Set the inheritable flag of the specified handle.");
8164 
8165 #define OS_SET_HANDLE_INHERITABLE_METHODDEF    \
8166     {"set_handle_inheritable", (PyCFunction)(void(*)(void))os_set_handle_inheritable, METH_FASTCALL, os_set_handle_inheritable__doc__},
8167 
8168 static PyObject *
8169 os_set_handle_inheritable_impl(PyObject *module, intptr_t handle,
8170                                int inheritable);
8171 
8172 static PyObject *
os_set_handle_inheritable(PyObject * module,PyObject * const * args,Py_ssize_t nargs)8173 os_set_handle_inheritable(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
8174 {
8175     PyObject *return_value = NULL;
8176     intptr_t handle;
8177     int inheritable;
8178 
8179     if (!_PyArg_ParseStack(args, nargs, "" _Py_PARSE_INTPTR "p:set_handle_inheritable",
8180         &handle, &inheritable)) {
8181         goto exit;
8182     }
8183     return_value = os_set_handle_inheritable_impl(module, handle, inheritable);
8184 
8185 exit:
8186     return return_value;
8187 }
8188 
8189 #endif /* defined(MS_WINDOWS) */
8190 
8191 #if !defined(MS_WINDOWS)
8192 
8193 PyDoc_STRVAR(os_get_blocking__doc__,
8194 "get_blocking($module, fd, /)\n"
8195 "--\n"
8196 "\n"
8197 "Get the blocking mode of the file descriptor.\n"
8198 "\n"
8199 "Return False if the O_NONBLOCK flag is set, True if the flag is cleared.");
8200 
8201 #define OS_GET_BLOCKING_METHODDEF    \
8202     {"get_blocking", (PyCFunction)os_get_blocking, METH_O, os_get_blocking__doc__},
8203 
8204 static int
8205 os_get_blocking_impl(PyObject *module, int fd);
8206 
8207 static PyObject *
os_get_blocking(PyObject * module,PyObject * arg)8208 os_get_blocking(PyObject *module, PyObject *arg)
8209 {
8210     PyObject *return_value = NULL;
8211     int fd;
8212     int _return_value;
8213 
8214     fd = _PyLong_AsInt(arg);
8215     if (fd == -1 && PyErr_Occurred()) {
8216         goto exit;
8217     }
8218     _return_value = os_get_blocking_impl(module, fd);
8219     if ((_return_value == -1) && PyErr_Occurred()) {
8220         goto exit;
8221     }
8222     return_value = PyBool_FromLong((long)_return_value);
8223 
8224 exit:
8225     return return_value;
8226 }
8227 
8228 #endif /* !defined(MS_WINDOWS) */
8229 
8230 #if !defined(MS_WINDOWS)
8231 
8232 PyDoc_STRVAR(os_set_blocking__doc__,
8233 "set_blocking($module, fd, blocking, /)\n"
8234 "--\n"
8235 "\n"
8236 "Set the blocking mode of the specified file descriptor.\n"
8237 "\n"
8238 "Set the O_NONBLOCK flag if blocking is False,\n"
8239 "clear the O_NONBLOCK flag otherwise.");
8240 
8241 #define OS_SET_BLOCKING_METHODDEF    \
8242     {"set_blocking", (PyCFunction)(void(*)(void))os_set_blocking, METH_FASTCALL, os_set_blocking__doc__},
8243 
8244 static PyObject *
8245 os_set_blocking_impl(PyObject *module, int fd, int blocking);
8246 
8247 static PyObject *
os_set_blocking(PyObject * module,PyObject * const * args,Py_ssize_t nargs)8248 os_set_blocking(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
8249 {
8250     PyObject *return_value = NULL;
8251     int fd;
8252     int blocking;
8253 
8254     if (!_PyArg_CheckPositional("set_blocking", nargs, 2, 2)) {
8255         goto exit;
8256     }
8257     fd = _PyLong_AsInt(args[0]);
8258     if (fd == -1 && PyErr_Occurred()) {
8259         goto exit;
8260     }
8261     blocking = _PyLong_AsInt(args[1]);
8262     if (blocking == -1 && PyErr_Occurred()) {
8263         goto exit;
8264     }
8265     return_value = os_set_blocking_impl(module, fd, blocking);
8266 
8267 exit:
8268     return return_value;
8269 }
8270 
8271 #endif /* !defined(MS_WINDOWS) */
8272 
8273 PyDoc_STRVAR(os_DirEntry_is_symlink__doc__,
8274 "is_symlink($self, /)\n"
8275 "--\n"
8276 "\n"
8277 "Return True if the entry is a symbolic link; cached per entry.");
8278 
8279 #define OS_DIRENTRY_IS_SYMLINK_METHODDEF    \
8280     {"is_symlink", (PyCFunction)(void(*)(void))os_DirEntry_is_symlink, METH_METHOD|METH_FASTCALL|METH_KEYWORDS, os_DirEntry_is_symlink__doc__},
8281 
8282 static int
8283 os_DirEntry_is_symlink_impl(DirEntry *self, PyTypeObject *defining_class);
8284 
8285 static PyObject *
os_DirEntry_is_symlink(DirEntry * self,PyTypeObject * defining_class,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)8286 os_DirEntry_is_symlink(DirEntry *self, PyTypeObject *defining_class, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
8287 {
8288     PyObject *return_value = NULL;
8289     static const char * const _keywords[] = { NULL};
8290     static _PyArg_Parser _parser = {":is_symlink", _keywords, 0};
8291     int _return_value;
8292 
8293     if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser
8294         )) {
8295         goto exit;
8296     }
8297     _return_value = os_DirEntry_is_symlink_impl(self, defining_class);
8298     if ((_return_value == -1) && PyErr_Occurred()) {
8299         goto exit;
8300     }
8301     return_value = PyBool_FromLong((long)_return_value);
8302 
8303 exit:
8304     return return_value;
8305 }
8306 
8307 PyDoc_STRVAR(os_DirEntry_stat__doc__,
8308 "stat($self, /, *, follow_symlinks=True)\n"
8309 "--\n"
8310 "\n"
8311 "Return stat_result object for the entry; cached per entry.");
8312 
8313 #define OS_DIRENTRY_STAT_METHODDEF    \
8314     {"stat", (PyCFunction)(void(*)(void))os_DirEntry_stat, METH_METHOD|METH_FASTCALL|METH_KEYWORDS, os_DirEntry_stat__doc__},
8315 
8316 static PyObject *
8317 os_DirEntry_stat_impl(DirEntry *self, PyTypeObject *defining_class,
8318                       int follow_symlinks);
8319 
8320 static PyObject *
os_DirEntry_stat(DirEntry * self,PyTypeObject * defining_class,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)8321 os_DirEntry_stat(DirEntry *self, PyTypeObject *defining_class, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
8322 {
8323     PyObject *return_value = NULL;
8324     static const char * const _keywords[] = {"follow_symlinks", NULL};
8325     static _PyArg_Parser _parser = {"|$p:stat", _keywords, 0};
8326     int follow_symlinks = 1;
8327 
8328     if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
8329         &follow_symlinks)) {
8330         goto exit;
8331     }
8332     return_value = os_DirEntry_stat_impl(self, defining_class, follow_symlinks);
8333 
8334 exit:
8335     return return_value;
8336 }
8337 
8338 PyDoc_STRVAR(os_DirEntry_is_dir__doc__,
8339 "is_dir($self, /, *, follow_symlinks=True)\n"
8340 "--\n"
8341 "\n"
8342 "Return True if the entry is a directory; cached per entry.");
8343 
8344 #define OS_DIRENTRY_IS_DIR_METHODDEF    \
8345     {"is_dir", (PyCFunction)(void(*)(void))os_DirEntry_is_dir, METH_METHOD|METH_FASTCALL|METH_KEYWORDS, os_DirEntry_is_dir__doc__},
8346 
8347 static int
8348 os_DirEntry_is_dir_impl(DirEntry *self, PyTypeObject *defining_class,
8349                         int follow_symlinks);
8350 
8351 static PyObject *
os_DirEntry_is_dir(DirEntry * self,PyTypeObject * defining_class,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)8352 os_DirEntry_is_dir(DirEntry *self, PyTypeObject *defining_class, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
8353 {
8354     PyObject *return_value = NULL;
8355     static const char * const _keywords[] = {"follow_symlinks", NULL};
8356     static _PyArg_Parser _parser = {"|$p:is_dir", _keywords, 0};
8357     int follow_symlinks = 1;
8358     int _return_value;
8359 
8360     if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
8361         &follow_symlinks)) {
8362         goto exit;
8363     }
8364     _return_value = os_DirEntry_is_dir_impl(self, defining_class, follow_symlinks);
8365     if ((_return_value == -1) && PyErr_Occurred()) {
8366         goto exit;
8367     }
8368     return_value = PyBool_FromLong((long)_return_value);
8369 
8370 exit:
8371     return return_value;
8372 }
8373 
8374 PyDoc_STRVAR(os_DirEntry_is_file__doc__,
8375 "is_file($self, /, *, follow_symlinks=True)\n"
8376 "--\n"
8377 "\n"
8378 "Return True if the entry is a file; cached per entry.");
8379 
8380 #define OS_DIRENTRY_IS_FILE_METHODDEF    \
8381     {"is_file", (PyCFunction)(void(*)(void))os_DirEntry_is_file, METH_METHOD|METH_FASTCALL|METH_KEYWORDS, os_DirEntry_is_file__doc__},
8382 
8383 static int
8384 os_DirEntry_is_file_impl(DirEntry *self, PyTypeObject *defining_class,
8385                          int follow_symlinks);
8386 
8387 static PyObject *
os_DirEntry_is_file(DirEntry * self,PyTypeObject * defining_class,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)8388 os_DirEntry_is_file(DirEntry *self, PyTypeObject *defining_class, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
8389 {
8390     PyObject *return_value = NULL;
8391     static const char * const _keywords[] = {"follow_symlinks", NULL};
8392     static _PyArg_Parser _parser = {"|$p:is_file", _keywords, 0};
8393     int follow_symlinks = 1;
8394     int _return_value;
8395 
8396     if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
8397         &follow_symlinks)) {
8398         goto exit;
8399     }
8400     _return_value = os_DirEntry_is_file_impl(self, defining_class, follow_symlinks);
8401     if ((_return_value == -1) && PyErr_Occurred()) {
8402         goto exit;
8403     }
8404     return_value = PyBool_FromLong((long)_return_value);
8405 
8406 exit:
8407     return return_value;
8408 }
8409 
8410 PyDoc_STRVAR(os_DirEntry_inode__doc__,
8411 "inode($self, /)\n"
8412 "--\n"
8413 "\n"
8414 "Return inode of the entry; cached per entry.");
8415 
8416 #define OS_DIRENTRY_INODE_METHODDEF    \
8417     {"inode", (PyCFunction)os_DirEntry_inode, METH_NOARGS, os_DirEntry_inode__doc__},
8418 
8419 static PyObject *
8420 os_DirEntry_inode_impl(DirEntry *self);
8421 
8422 static PyObject *
os_DirEntry_inode(DirEntry * self,PyObject * Py_UNUSED (ignored))8423 os_DirEntry_inode(DirEntry *self, PyObject *Py_UNUSED(ignored))
8424 {
8425     return os_DirEntry_inode_impl(self);
8426 }
8427 
8428 PyDoc_STRVAR(os_DirEntry___fspath____doc__,
8429 "__fspath__($self, /)\n"
8430 "--\n"
8431 "\n"
8432 "Returns the path for the entry.");
8433 
8434 #define OS_DIRENTRY___FSPATH___METHODDEF    \
8435     {"__fspath__", (PyCFunction)os_DirEntry___fspath__, METH_NOARGS, os_DirEntry___fspath____doc__},
8436 
8437 static PyObject *
8438 os_DirEntry___fspath___impl(DirEntry *self);
8439 
8440 static PyObject *
os_DirEntry___fspath__(DirEntry * self,PyObject * Py_UNUSED (ignored))8441 os_DirEntry___fspath__(DirEntry *self, PyObject *Py_UNUSED(ignored))
8442 {
8443     return os_DirEntry___fspath___impl(self);
8444 }
8445 
8446 PyDoc_STRVAR(os_scandir__doc__,
8447 "scandir($module, /, path=None)\n"
8448 "--\n"
8449 "\n"
8450 "Return an iterator of DirEntry objects for given path.\n"
8451 "\n"
8452 "path can be specified as either str, bytes, or a path-like object.  If path\n"
8453 "is bytes, the names of yielded DirEntry objects will also be bytes; in\n"
8454 "all other circumstances they will be str.\n"
8455 "\n"
8456 "If path is None, uses the path=\'.\'.");
8457 
8458 #define OS_SCANDIR_METHODDEF    \
8459     {"scandir", (PyCFunction)(void(*)(void))os_scandir, METH_FASTCALL|METH_KEYWORDS, os_scandir__doc__},
8460 
8461 static PyObject *
8462 os_scandir_impl(PyObject *module, path_t *path);
8463 
8464 static PyObject *
os_scandir(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)8465 os_scandir(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
8466 {
8467     PyObject *return_value = NULL;
8468     static const char * const _keywords[] = {"path", NULL};
8469     static _PyArg_Parser _parser = {NULL, _keywords, "scandir", 0};
8470     PyObject *argsbuf[1];
8471     Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
8472     path_t path = PATH_T_INITIALIZE("scandir", "path", 1, PATH_HAVE_FDOPENDIR);
8473 
8474     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
8475     if (!args) {
8476         goto exit;
8477     }
8478     if (!noptargs) {
8479         goto skip_optional_pos;
8480     }
8481     if (!path_converter(args[0], &path)) {
8482         goto exit;
8483     }
8484 skip_optional_pos:
8485     return_value = os_scandir_impl(module, &path);
8486 
8487 exit:
8488     /* Cleanup for path */
8489     path_cleanup(&path);
8490 
8491     return return_value;
8492 }
8493 
8494 PyDoc_STRVAR(os_fspath__doc__,
8495 "fspath($module, /, path)\n"
8496 "--\n"
8497 "\n"
8498 "Return the file system path representation of the object.\n"
8499 "\n"
8500 "If the object is str or bytes, then allow it to pass through as-is. If the\n"
8501 "object defines __fspath__(), then return the result of that method. All other\n"
8502 "types raise a TypeError.");
8503 
8504 #define OS_FSPATH_METHODDEF    \
8505     {"fspath", (PyCFunction)(void(*)(void))os_fspath, METH_FASTCALL|METH_KEYWORDS, os_fspath__doc__},
8506 
8507 static PyObject *
8508 os_fspath_impl(PyObject *module, PyObject *path);
8509 
8510 static PyObject *
os_fspath(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)8511 os_fspath(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
8512 {
8513     PyObject *return_value = NULL;
8514     static const char * const _keywords[] = {"path", NULL};
8515     static _PyArg_Parser _parser = {NULL, _keywords, "fspath", 0};
8516     PyObject *argsbuf[1];
8517     PyObject *path;
8518 
8519     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
8520     if (!args) {
8521         goto exit;
8522     }
8523     path = args[0];
8524     return_value = os_fspath_impl(module, path);
8525 
8526 exit:
8527     return return_value;
8528 }
8529 
8530 #if defined(HAVE_GETRANDOM_SYSCALL)
8531 
8532 PyDoc_STRVAR(os_getrandom__doc__,
8533 "getrandom($module, /, size, flags=0)\n"
8534 "--\n"
8535 "\n"
8536 "Obtain a series of random bytes.");
8537 
8538 #define OS_GETRANDOM_METHODDEF    \
8539     {"getrandom", (PyCFunction)(void(*)(void))os_getrandom, METH_FASTCALL|METH_KEYWORDS, os_getrandom__doc__},
8540 
8541 static PyObject *
8542 os_getrandom_impl(PyObject *module, Py_ssize_t size, int flags);
8543 
8544 static PyObject *
os_getrandom(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)8545 os_getrandom(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
8546 {
8547     PyObject *return_value = NULL;
8548     static const char * const _keywords[] = {"size", "flags", NULL};
8549     static _PyArg_Parser _parser = {NULL, _keywords, "getrandom", 0};
8550     PyObject *argsbuf[2];
8551     Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
8552     Py_ssize_t size;
8553     int flags = 0;
8554 
8555     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
8556     if (!args) {
8557         goto exit;
8558     }
8559     {
8560         Py_ssize_t ival = -1;
8561         PyObject *iobj = _PyNumber_Index(args[0]);
8562         if (iobj != NULL) {
8563             ival = PyLong_AsSsize_t(iobj);
8564             Py_DECREF(iobj);
8565         }
8566         if (ival == -1 && PyErr_Occurred()) {
8567             goto exit;
8568         }
8569         size = ival;
8570     }
8571     if (!noptargs) {
8572         goto skip_optional_pos;
8573     }
8574     flags = _PyLong_AsInt(args[1]);
8575     if (flags == -1 && PyErr_Occurred()) {
8576         goto exit;
8577     }
8578 skip_optional_pos:
8579     return_value = os_getrandom_impl(module, size, flags);
8580 
8581 exit:
8582     return return_value;
8583 }
8584 
8585 #endif /* defined(HAVE_GETRANDOM_SYSCALL) */
8586 
8587 #if defined(MS_WINDOWS)
8588 
8589 PyDoc_STRVAR(os__add_dll_directory__doc__,
8590 "_add_dll_directory($module, /, path)\n"
8591 "--\n"
8592 "\n"
8593 "Add a path to the DLL search path.\n"
8594 "\n"
8595 "This search path is used when resolving dependencies for imported\n"
8596 "extension modules (the module itself is resolved through sys.path),\n"
8597 "and also by ctypes.\n"
8598 "\n"
8599 "Returns an opaque value that may be passed to os.remove_dll_directory\n"
8600 "to remove this directory from the search path.");
8601 
8602 #define OS__ADD_DLL_DIRECTORY_METHODDEF    \
8603     {"_add_dll_directory", (PyCFunction)(void(*)(void))os__add_dll_directory, METH_FASTCALL|METH_KEYWORDS, os__add_dll_directory__doc__},
8604 
8605 static PyObject *
8606 os__add_dll_directory_impl(PyObject *module, path_t *path);
8607 
8608 static PyObject *
os__add_dll_directory(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)8609 os__add_dll_directory(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
8610 {
8611     PyObject *return_value = NULL;
8612     static const char * const _keywords[] = {"path", NULL};
8613     static _PyArg_Parser _parser = {NULL, _keywords, "_add_dll_directory", 0};
8614     PyObject *argsbuf[1];
8615     path_t path = PATH_T_INITIALIZE("_add_dll_directory", "path", 0, 0);
8616 
8617     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
8618     if (!args) {
8619         goto exit;
8620     }
8621     if (!path_converter(args[0], &path)) {
8622         goto exit;
8623     }
8624     return_value = os__add_dll_directory_impl(module, &path);
8625 
8626 exit:
8627     /* Cleanup for path */
8628     path_cleanup(&path);
8629 
8630     return return_value;
8631 }
8632 
8633 #endif /* defined(MS_WINDOWS) */
8634 
8635 #if defined(MS_WINDOWS)
8636 
8637 PyDoc_STRVAR(os__remove_dll_directory__doc__,
8638 "_remove_dll_directory($module, /, cookie)\n"
8639 "--\n"
8640 "\n"
8641 "Removes a path from the DLL search path.\n"
8642 "\n"
8643 "The parameter is an opaque value that was returned from\n"
8644 "os.add_dll_directory. You can only remove directories that you added\n"
8645 "yourself.");
8646 
8647 #define OS__REMOVE_DLL_DIRECTORY_METHODDEF    \
8648     {"_remove_dll_directory", (PyCFunction)(void(*)(void))os__remove_dll_directory, METH_FASTCALL|METH_KEYWORDS, os__remove_dll_directory__doc__},
8649 
8650 static PyObject *
8651 os__remove_dll_directory_impl(PyObject *module, PyObject *cookie);
8652 
8653 static PyObject *
os__remove_dll_directory(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)8654 os__remove_dll_directory(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
8655 {
8656     PyObject *return_value = NULL;
8657     static const char * const _keywords[] = {"cookie", NULL};
8658     static _PyArg_Parser _parser = {NULL, _keywords, "_remove_dll_directory", 0};
8659     PyObject *argsbuf[1];
8660     PyObject *cookie;
8661 
8662     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
8663     if (!args) {
8664         goto exit;
8665     }
8666     cookie = args[0];
8667     return_value = os__remove_dll_directory_impl(module, cookie);
8668 
8669 exit:
8670     return return_value;
8671 }
8672 
8673 #endif /* defined(MS_WINDOWS) */
8674 
8675 #if (defined(WIFEXITED) || defined(MS_WINDOWS))
8676 
8677 PyDoc_STRVAR(os_waitstatus_to_exitcode__doc__,
8678 "waitstatus_to_exitcode($module, /, status)\n"
8679 "--\n"
8680 "\n"
8681 "Convert a wait status to an exit code.\n"
8682 "\n"
8683 "On Unix:\n"
8684 "\n"
8685 "* If WIFEXITED(status) is true, return WEXITSTATUS(status).\n"
8686 "* If WIFSIGNALED(status) is true, return -WTERMSIG(status).\n"
8687 "* Otherwise, raise a ValueError.\n"
8688 "\n"
8689 "On Windows, return status shifted right by 8 bits.\n"
8690 "\n"
8691 "On Unix, if the process is being traced or if waitpid() was called with\n"
8692 "WUNTRACED option, the caller must first check if WIFSTOPPED(status) is true.\n"
8693 "This function must not be called if WIFSTOPPED(status) is true.");
8694 
8695 #define OS_WAITSTATUS_TO_EXITCODE_METHODDEF    \
8696     {"waitstatus_to_exitcode", (PyCFunction)(void(*)(void))os_waitstatus_to_exitcode, METH_FASTCALL|METH_KEYWORDS, os_waitstatus_to_exitcode__doc__},
8697 
8698 static PyObject *
8699 os_waitstatus_to_exitcode_impl(PyObject *module, PyObject *status_obj);
8700 
8701 static PyObject *
os_waitstatus_to_exitcode(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)8702 os_waitstatus_to_exitcode(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
8703 {
8704     PyObject *return_value = NULL;
8705     static const char * const _keywords[] = {"status", NULL};
8706     static _PyArg_Parser _parser = {NULL, _keywords, "waitstatus_to_exitcode", 0};
8707     PyObject *argsbuf[1];
8708     PyObject *status_obj;
8709 
8710     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
8711     if (!args) {
8712         goto exit;
8713     }
8714     status_obj = args[0];
8715     return_value = os_waitstatus_to_exitcode_impl(module, status_obj);
8716 
8717 exit:
8718     return return_value;
8719 }
8720 
8721 #endif /* (defined(WIFEXITED) || defined(MS_WINDOWS)) */
8722 
8723 #ifndef OS_TTYNAME_METHODDEF
8724     #define OS_TTYNAME_METHODDEF
8725 #endif /* !defined(OS_TTYNAME_METHODDEF) */
8726 
8727 #ifndef OS_CTERMID_METHODDEF
8728     #define OS_CTERMID_METHODDEF
8729 #endif /* !defined(OS_CTERMID_METHODDEF) */
8730 
8731 #ifndef OS_FCHDIR_METHODDEF
8732     #define OS_FCHDIR_METHODDEF
8733 #endif /* !defined(OS_FCHDIR_METHODDEF) */
8734 
8735 #ifndef OS_FCHMOD_METHODDEF
8736     #define OS_FCHMOD_METHODDEF
8737 #endif /* !defined(OS_FCHMOD_METHODDEF) */
8738 
8739 #ifndef OS_LCHMOD_METHODDEF
8740     #define OS_LCHMOD_METHODDEF
8741 #endif /* !defined(OS_LCHMOD_METHODDEF) */
8742 
8743 #ifndef OS_CHFLAGS_METHODDEF
8744     #define OS_CHFLAGS_METHODDEF
8745 #endif /* !defined(OS_CHFLAGS_METHODDEF) */
8746 
8747 #ifndef OS_LCHFLAGS_METHODDEF
8748     #define OS_LCHFLAGS_METHODDEF
8749 #endif /* !defined(OS_LCHFLAGS_METHODDEF) */
8750 
8751 #ifndef OS_CHROOT_METHODDEF
8752     #define OS_CHROOT_METHODDEF
8753 #endif /* !defined(OS_CHROOT_METHODDEF) */
8754 
8755 #ifndef OS_FSYNC_METHODDEF
8756     #define OS_FSYNC_METHODDEF
8757 #endif /* !defined(OS_FSYNC_METHODDEF) */
8758 
8759 #ifndef OS_SYNC_METHODDEF
8760     #define OS_SYNC_METHODDEF
8761 #endif /* !defined(OS_SYNC_METHODDEF) */
8762 
8763 #ifndef OS_FDATASYNC_METHODDEF
8764     #define OS_FDATASYNC_METHODDEF
8765 #endif /* !defined(OS_FDATASYNC_METHODDEF) */
8766 
8767 #ifndef OS_CHOWN_METHODDEF
8768     #define OS_CHOWN_METHODDEF
8769 #endif /* !defined(OS_CHOWN_METHODDEF) */
8770 
8771 #ifndef OS_FCHOWN_METHODDEF
8772     #define OS_FCHOWN_METHODDEF
8773 #endif /* !defined(OS_FCHOWN_METHODDEF) */
8774 
8775 #ifndef OS_LCHOWN_METHODDEF
8776     #define OS_LCHOWN_METHODDEF
8777 #endif /* !defined(OS_LCHOWN_METHODDEF) */
8778 
8779 #ifndef OS_LINK_METHODDEF
8780     #define OS_LINK_METHODDEF
8781 #endif /* !defined(OS_LINK_METHODDEF) */
8782 
8783 #ifndef OS__GETFULLPATHNAME_METHODDEF
8784     #define OS__GETFULLPATHNAME_METHODDEF
8785 #endif /* !defined(OS__GETFULLPATHNAME_METHODDEF) */
8786 
8787 #ifndef OS__GETFINALPATHNAME_METHODDEF
8788     #define OS__GETFINALPATHNAME_METHODDEF
8789 #endif /* !defined(OS__GETFINALPATHNAME_METHODDEF) */
8790 
8791 #ifndef OS__GETVOLUMEPATHNAME_METHODDEF
8792     #define OS__GETVOLUMEPATHNAME_METHODDEF
8793 #endif /* !defined(OS__GETVOLUMEPATHNAME_METHODDEF) */
8794 
8795 #ifndef OS__PATH_SPLITROOT_METHODDEF
8796     #define OS__PATH_SPLITROOT_METHODDEF
8797 #endif /* !defined(OS__PATH_SPLITROOT_METHODDEF) */
8798 
8799 #ifndef OS_NICE_METHODDEF
8800     #define OS_NICE_METHODDEF
8801 #endif /* !defined(OS_NICE_METHODDEF) */
8802 
8803 #ifndef OS_GETPRIORITY_METHODDEF
8804     #define OS_GETPRIORITY_METHODDEF
8805 #endif /* !defined(OS_GETPRIORITY_METHODDEF) */
8806 
8807 #ifndef OS_SETPRIORITY_METHODDEF
8808     #define OS_SETPRIORITY_METHODDEF
8809 #endif /* !defined(OS_SETPRIORITY_METHODDEF) */
8810 
8811 #ifndef OS_SYSTEM_METHODDEF
8812     #define OS_SYSTEM_METHODDEF
8813 #endif /* !defined(OS_SYSTEM_METHODDEF) */
8814 
8815 #ifndef OS_UNAME_METHODDEF
8816     #define OS_UNAME_METHODDEF
8817 #endif /* !defined(OS_UNAME_METHODDEF) */
8818 
8819 #ifndef OS_EXECV_METHODDEF
8820     #define OS_EXECV_METHODDEF
8821 #endif /* !defined(OS_EXECV_METHODDEF) */
8822 
8823 #ifndef OS_EXECVE_METHODDEF
8824     #define OS_EXECVE_METHODDEF
8825 #endif /* !defined(OS_EXECVE_METHODDEF) */
8826 
8827 #ifndef OS_POSIX_SPAWN_METHODDEF
8828     #define OS_POSIX_SPAWN_METHODDEF
8829 #endif /* !defined(OS_POSIX_SPAWN_METHODDEF) */
8830 
8831 #ifndef OS_POSIX_SPAWNP_METHODDEF
8832     #define OS_POSIX_SPAWNP_METHODDEF
8833 #endif /* !defined(OS_POSIX_SPAWNP_METHODDEF) */
8834 
8835 #ifndef OS_SPAWNV_METHODDEF
8836     #define OS_SPAWNV_METHODDEF
8837 #endif /* !defined(OS_SPAWNV_METHODDEF) */
8838 
8839 #ifndef OS_SPAWNVE_METHODDEF
8840     #define OS_SPAWNVE_METHODDEF
8841 #endif /* !defined(OS_SPAWNVE_METHODDEF) */
8842 
8843 #ifndef OS_REGISTER_AT_FORK_METHODDEF
8844     #define OS_REGISTER_AT_FORK_METHODDEF
8845 #endif /* !defined(OS_REGISTER_AT_FORK_METHODDEF) */
8846 
8847 #ifndef OS_FORK1_METHODDEF
8848     #define OS_FORK1_METHODDEF
8849 #endif /* !defined(OS_FORK1_METHODDEF) */
8850 
8851 #ifndef OS_FORK_METHODDEF
8852     #define OS_FORK_METHODDEF
8853 #endif /* !defined(OS_FORK_METHODDEF) */
8854 
8855 #ifndef OS_SCHED_GET_PRIORITY_MAX_METHODDEF
8856     #define OS_SCHED_GET_PRIORITY_MAX_METHODDEF
8857 #endif /* !defined(OS_SCHED_GET_PRIORITY_MAX_METHODDEF) */
8858 
8859 #ifndef OS_SCHED_GET_PRIORITY_MIN_METHODDEF
8860     #define OS_SCHED_GET_PRIORITY_MIN_METHODDEF
8861 #endif /* !defined(OS_SCHED_GET_PRIORITY_MIN_METHODDEF) */
8862 
8863 #ifndef OS_SCHED_GETSCHEDULER_METHODDEF
8864     #define OS_SCHED_GETSCHEDULER_METHODDEF
8865 #endif /* !defined(OS_SCHED_GETSCHEDULER_METHODDEF) */
8866 
8867 #ifndef OS_SCHED_SETSCHEDULER_METHODDEF
8868     #define OS_SCHED_SETSCHEDULER_METHODDEF
8869 #endif /* !defined(OS_SCHED_SETSCHEDULER_METHODDEF) */
8870 
8871 #ifndef OS_SCHED_GETPARAM_METHODDEF
8872     #define OS_SCHED_GETPARAM_METHODDEF
8873 #endif /* !defined(OS_SCHED_GETPARAM_METHODDEF) */
8874 
8875 #ifndef OS_SCHED_SETPARAM_METHODDEF
8876     #define OS_SCHED_SETPARAM_METHODDEF
8877 #endif /* !defined(OS_SCHED_SETPARAM_METHODDEF) */
8878 
8879 #ifndef OS_SCHED_RR_GET_INTERVAL_METHODDEF
8880     #define OS_SCHED_RR_GET_INTERVAL_METHODDEF
8881 #endif /* !defined(OS_SCHED_RR_GET_INTERVAL_METHODDEF) */
8882 
8883 #ifndef OS_SCHED_YIELD_METHODDEF
8884     #define OS_SCHED_YIELD_METHODDEF
8885 #endif /* !defined(OS_SCHED_YIELD_METHODDEF) */
8886 
8887 #ifndef OS_SCHED_SETAFFINITY_METHODDEF
8888     #define OS_SCHED_SETAFFINITY_METHODDEF
8889 #endif /* !defined(OS_SCHED_SETAFFINITY_METHODDEF) */
8890 
8891 #ifndef OS_SCHED_GETAFFINITY_METHODDEF
8892     #define OS_SCHED_GETAFFINITY_METHODDEF
8893 #endif /* !defined(OS_SCHED_GETAFFINITY_METHODDEF) */
8894 
8895 #ifndef OS_OPENPTY_METHODDEF
8896     #define OS_OPENPTY_METHODDEF
8897 #endif /* !defined(OS_OPENPTY_METHODDEF) */
8898 
8899 #ifndef OS_FORKPTY_METHODDEF
8900     #define OS_FORKPTY_METHODDEF
8901 #endif /* !defined(OS_FORKPTY_METHODDEF) */
8902 
8903 #ifndef OS_GETEGID_METHODDEF
8904     #define OS_GETEGID_METHODDEF
8905 #endif /* !defined(OS_GETEGID_METHODDEF) */
8906 
8907 #ifndef OS_GETEUID_METHODDEF
8908     #define OS_GETEUID_METHODDEF
8909 #endif /* !defined(OS_GETEUID_METHODDEF) */
8910 
8911 #ifndef OS_GETGID_METHODDEF
8912     #define OS_GETGID_METHODDEF
8913 #endif /* !defined(OS_GETGID_METHODDEF) */
8914 
8915 #ifndef OS_GETPID_METHODDEF
8916     #define OS_GETPID_METHODDEF
8917 #endif /* !defined(OS_GETPID_METHODDEF) */
8918 
8919 #ifndef OS_GETGROUPLIST_METHODDEF
8920     #define OS_GETGROUPLIST_METHODDEF
8921 #endif /* !defined(OS_GETGROUPLIST_METHODDEF) */
8922 
8923 #ifndef OS_GETGROUPS_METHODDEF
8924     #define OS_GETGROUPS_METHODDEF
8925 #endif /* !defined(OS_GETGROUPS_METHODDEF) */
8926 
8927 #ifndef OS_INITGROUPS_METHODDEF
8928     #define OS_INITGROUPS_METHODDEF
8929 #endif /* !defined(OS_INITGROUPS_METHODDEF) */
8930 
8931 #ifndef OS_GETPGID_METHODDEF
8932     #define OS_GETPGID_METHODDEF
8933 #endif /* !defined(OS_GETPGID_METHODDEF) */
8934 
8935 #ifndef OS_GETPGRP_METHODDEF
8936     #define OS_GETPGRP_METHODDEF
8937 #endif /* !defined(OS_GETPGRP_METHODDEF) */
8938 
8939 #ifndef OS_SETPGRP_METHODDEF
8940     #define OS_SETPGRP_METHODDEF
8941 #endif /* !defined(OS_SETPGRP_METHODDEF) */
8942 
8943 #ifndef OS_GETPPID_METHODDEF
8944     #define OS_GETPPID_METHODDEF
8945 #endif /* !defined(OS_GETPPID_METHODDEF) */
8946 
8947 #ifndef OS_GETLOGIN_METHODDEF
8948     #define OS_GETLOGIN_METHODDEF
8949 #endif /* !defined(OS_GETLOGIN_METHODDEF) */
8950 
8951 #ifndef OS_GETUID_METHODDEF
8952     #define OS_GETUID_METHODDEF
8953 #endif /* !defined(OS_GETUID_METHODDEF) */
8954 
8955 #ifndef OS_KILL_METHODDEF
8956     #define OS_KILL_METHODDEF
8957 #endif /* !defined(OS_KILL_METHODDEF) */
8958 
8959 #ifndef OS_KILLPG_METHODDEF
8960     #define OS_KILLPG_METHODDEF
8961 #endif /* !defined(OS_KILLPG_METHODDEF) */
8962 
8963 #ifndef OS_PLOCK_METHODDEF
8964     #define OS_PLOCK_METHODDEF
8965 #endif /* !defined(OS_PLOCK_METHODDEF) */
8966 
8967 #ifndef OS_SETUID_METHODDEF
8968     #define OS_SETUID_METHODDEF
8969 #endif /* !defined(OS_SETUID_METHODDEF) */
8970 
8971 #ifndef OS_SETEUID_METHODDEF
8972     #define OS_SETEUID_METHODDEF
8973 #endif /* !defined(OS_SETEUID_METHODDEF) */
8974 
8975 #ifndef OS_SETEGID_METHODDEF
8976     #define OS_SETEGID_METHODDEF
8977 #endif /* !defined(OS_SETEGID_METHODDEF) */
8978 
8979 #ifndef OS_SETREUID_METHODDEF
8980     #define OS_SETREUID_METHODDEF
8981 #endif /* !defined(OS_SETREUID_METHODDEF) */
8982 
8983 #ifndef OS_SETREGID_METHODDEF
8984     #define OS_SETREGID_METHODDEF
8985 #endif /* !defined(OS_SETREGID_METHODDEF) */
8986 
8987 #ifndef OS_SETGID_METHODDEF
8988     #define OS_SETGID_METHODDEF
8989 #endif /* !defined(OS_SETGID_METHODDEF) */
8990 
8991 #ifndef OS_SETGROUPS_METHODDEF
8992     #define OS_SETGROUPS_METHODDEF
8993 #endif /* !defined(OS_SETGROUPS_METHODDEF) */
8994 
8995 #ifndef OS_WAIT3_METHODDEF
8996     #define OS_WAIT3_METHODDEF
8997 #endif /* !defined(OS_WAIT3_METHODDEF) */
8998 
8999 #ifndef OS_WAIT4_METHODDEF
9000     #define OS_WAIT4_METHODDEF
9001 #endif /* !defined(OS_WAIT4_METHODDEF) */
9002 
9003 #ifndef OS_WAITID_METHODDEF
9004     #define OS_WAITID_METHODDEF
9005 #endif /* !defined(OS_WAITID_METHODDEF) */
9006 
9007 #ifndef OS_WAITPID_METHODDEF
9008     #define OS_WAITPID_METHODDEF
9009 #endif /* !defined(OS_WAITPID_METHODDEF) */
9010 
9011 #ifndef OS_WAIT_METHODDEF
9012     #define OS_WAIT_METHODDEF
9013 #endif /* !defined(OS_WAIT_METHODDEF) */
9014 
9015 #ifndef OS_PIDFD_OPEN_METHODDEF
9016     #define OS_PIDFD_OPEN_METHODDEF
9017 #endif /* !defined(OS_PIDFD_OPEN_METHODDEF) */
9018 
9019 #ifndef OS_READLINK_METHODDEF
9020     #define OS_READLINK_METHODDEF
9021 #endif /* !defined(OS_READLINK_METHODDEF) */
9022 
9023 #ifndef OS_SYMLINK_METHODDEF
9024     #define OS_SYMLINK_METHODDEF
9025 #endif /* !defined(OS_SYMLINK_METHODDEF) */
9026 
9027 #ifndef OS_TIMES_METHODDEF
9028     #define OS_TIMES_METHODDEF
9029 #endif /* !defined(OS_TIMES_METHODDEF) */
9030 
9031 #ifndef OS_GETSID_METHODDEF
9032     #define OS_GETSID_METHODDEF
9033 #endif /* !defined(OS_GETSID_METHODDEF) */
9034 
9035 #ifndef OS_SETSID_METHODDEF
9036     #define OS_SETSID_METHODDEF
9037 #endif /* !defined(OS_SETSID_METHODDEF) */
9038 
9039 #ifndef OS_SETPGID_METHODDEF
9040     #define OS_SETPGID_METHODDEF
9041 #endif /* !defined(OS_SETPGID_METHODDEF) */
9042 
9043 #ifndef OS_TCGETPGRP_METHODDEF
9044     #define OS_TCGETPGRP_METHODDEF
9045 #endif /* !defined(OS_TCGETPGRP_METHODDEF) */
9046 
9047 #ifndef OS_TCSETPGRP_METHODDEF
9048     #define OS_TCSETPGRP_METHODDEF
9049 #endif /* !defined(OS_TCSETPGRP_METHODDEF) */
9050 
9051 #ifndef OS_LOCKF_METHODDEF
9052     #define OS_LOCKF_METHODDEF
9053 #endif /* !defined(OS_LOCKF_METHODDEF) */
9054 
9055 #ifndef OS_READV_METHODDEF
9056     #define OS_READV_METHODDEF
9057 #endif /* !defined(OS_READV_METHODDEF) */
9058 
9059 #ifndef OS_PREAD_METHODDEF
9060     #define OS_PREAD_METHODDEF
9061 #endif /* !defined(OS_PREAD_METHODDEF) */
9062 
9063 #ifndef OS_PREADV_METHODDEF
9064     #define OS_PREADV_METHODDEF
9065 #endif /* !defined(OS_PREADV_METHODDEF) */
9066 
9067 #ifndef OS_SENDFILE_METHODDEF
9068     #define OS_SENDFILE_METHODDEF
9069 #endif /* !defined(OS_SENDFILE_METHODDEF) */
9070 
9071 #ifndef OS__FCOPYFILE_METHODDEF
9072     #define OS__FCOPYFILE_METHODDEF
9073 #endif /* !defined(OS__FCOPYFILE_METHODDEF) */
9074 
9075 #ifndef OS_PIPE_METHODDEF
9076     #define OS_PIPE_METHODDEF
9077 #endif /* !defined(OS_PIPE_METHODDEF) */
9078 
9079 #ifndef OS_PIPE2_METHODDEF
9080     #define OS_PIPE2_METHODDEF
9081 #endif /* !defined(OS_PIPE2_METHODDEF) */
9082 
9083 #ifndef OS_WRITEV_METHODDEF
9084     #define OS_WRITEV_METHODDEF
9085 #endif /* !defined(OS_WRITEV_METHODDEF) */
9086 
9087 #ifndef OS_PWRITE_METHODDEF
9088     #define OS_PWRITE_METHODDEF
9089 #endif /* !defined(OS_PWRITE_METHODDEF) */
9090 
9091 #ifndef OS_PWRITEV_METHODDEF
9092     #define OS_PWRITEV_METHODDEF
9093 #endif /* !defined(OS_PWRITEV_METHODDEF) */
9094 
9095 #ifndef OS_COPY_FILE_RANGE_METHODDEF
9096     #define OS_COPY_FILE_RANGE_METHODDEF
9097 #endif /* !defined(OS_COPY_FILE_RANGE_METHODDEF) */
9098 
9099 #ifndef OS_SPLICE_METHODDEF
9100     #define OS_SPLICE_METHODDEF
9101 #endif /* !defined(OS_SPLICE_METHODDEF) */
9102 
9103 #ifndef OS_MKFIFO_METHODDEF
9104     #define OS_MKFIFO_METHODDEF
9105 #endif /* !defined(OS_MKFIFO_METHODDEF) */
9106 
9107 #ifndef OS_MKNOD_METHODDEF
9108     #define OS_MKNOD_METHODDEF
9109 #endif /* !defined(OS_MKNOD_METHODDEF) */
9110 
9111 #ifndef OS_MAJOR_METHODDEF
9112     #define OS_MAJOR_METHODDEF
9113 #endif /* !defined(OS_MAJOR_METHODDEF) */
9114 
9115 #ifndef OS_MINOR_METHODDEF
9116     #define OS_MINOR_METHODDEF
9117 #endif /* !defined(OS_MINOR_METHODDEF) */
9118 
9119 #ifndef OS_MAKEDEV_METHODDEF
9120     #define OS_MAKEDEV_METHODDEF
9121 #endif /* !defined(OS_MAKEDEV_METHODDEF) */
9122 
9123 #ifndef OS_FTRUNCATE_METHODDEF
9124     #define OS_FTRUNCATE_METHODDEF
9125 #endif /* !defined(OS_FTRUNCATE_METHODDEF) */
9126 
9127 #ifndef OS_TRUNCATE_METHODDEF
9128     #define OS_TRUNCATE_METHODDEF
9129 #endif /* !defined(OS_TRUNCATE_METHODDEF) */
9130 
9131 #ifndef OS_POSIX_FALLOCATE_METHODDEF
9132     #define OS_POSIX_FALLOCATE_METHODDEF
9133 #endif /* !defined(OS_POSIX_FALLOCATE_METHODDEF) */
9134 
9135 #ifndef OS_POSIX_FADVISE_METHODDEF
9136     #define OS_POSIX_FADVISE_METHODDEF
9137 #endif /* !defined(OS_POSIX_FADVISE_METHODDEF) */
9138 
9139 #ifndef OS_PUTENV_METHODDEF
9140     #define OS_PUTENV_METHODDEF
9141 #endif /* !defined(OS_PUTENV_METHODDEF) */
9142 
9143 #ifndef OS_UNSETENV_METHODDEF
9144     #define OS_UNSETENV_METHODDEF
9145 #endif /* !defined(OS_UNSETENV_METHODDEF) */
9146 
9147 #ifndef OS_WCOREDUMP_METHODDEF
9148     #define OS_WCOREDUMP_METHODDEF
9149 #endif /* !defined(OS_WCOREDUMP_METHODDEF) */
9150 
9151 #ifndef OS_WIFCONTINUED_METHODDEF
9152     #define OS_WIFCONTINUED_METHODDEF
9153 #endif /* !defined(OS_WIFCONTINUED_METHODDEF) */
9154 
9155 #ifndef OS_WIFSTOPPED_METHODDEF
9156     #define OS_WIFSTOPPED_METHODDEF
9157 #endif /* !defined(OS_WIFSTOPPED_METHODDEF) */
9158 
9159 #ifndef OS_WIFSIGNALED_METHODDEF
9160     #define OS_WIFSIGNALED_METHODDEF
9161 #endif /* !defined(OS_WIFSIGNALED_METHODDEF) */
9162 
9163 #ifndef OS_WIFEXITED_METHODDEF
9164     #define OS_WIFEXITED_METHODDEF
9165 #endif /* !defined(OS_WIFEXITED_METHODDEF) */
9166 
9167 #ifndef OS_WEXITSTATUS_METHODDEF
9168     #define OS_WEXITSTATUS_METHODDEF
9169 #endif /* !defined(OS_WEXITSTATUS_METHODDEF) */
9170 
9171 #ifndef OS_WTERMSIG_METHODDEF
9172     #define OS_WTERMSIG_METHODDEF
9173 #endif /* !defined(OS_WTERMSIG_METHODDEF) */
9174 
9175 #ifndef OS_WSTOPSIG_METHODDEF
9176     #define OS_WSTOPSIG_METHODDEF
9177 #endif /* !defined(OS_WSTOPSIG_METHODDEF) */
9178 
9179 #ifndef OS_FSTATVFS_METHODDEF
9180     #define OS_FSTATVFS_METHODDEF
9181 #endif /* !defined(OS_FSTATVFS_METHODDEF) */
9182 
9183 #ifndef OS_STATVFS_METHODDEF
9184     #define OS_STATVFS_METHODDEF
9185 #endif /* !defined(OS_STATVFS_METHODDEF) */
9186 
9187 #ifndef OS__GETDISKUSAGE_METHODDEF
9188     #define OS__GETDISKUSAGE_METHODDEF
9189 #endif /* !defined(OS__GETDISKUSAGE_METHODDEF) */
9190 
9191 #ifndef OS_FPATHCONF_METHODDEF
9192     #define OS_FPATHCONF_METHODDEF
9193 #endif /* !defined(OS_FPATHCONF_METHODDEF) */
9194 
9195 #ifndef OS_PATHCONF_METHODDEF
9196     #define OS_PATHCONF_METHODDEF
9197 #endif /* !defined(OS_PATHCONF_METHODDEF) */
9198 
9199 #ifndef OS_CONFSTR_METHODDEF
9200     #define OS_CONFSTR_METHODDEF
9201 #endif /* !defined(OS_CONFSTR_METHODDEF) */
9202 
9203 #ifndef OS_SYSCONF_METHODDEF
9204     #define OS_SYSCONF_METHODDEF
9205 #endif /* !defined(OS_SYSCONF_METHODDEF) */
9206 
9207 #ifndef OS_STARTFILE_METHODDEF
9208     #define OS_STARTFILE_METHODDEF
9209 #endif /* !defined(OS_STARTFILE_METHODDEF) */
9210 
9211 #ifndef OS_GETLOADAVG_METHODDEF
9212     #define OS_GETLOADAVG_METHODDEF
9213 #endif /* !defined(OS_GETLOADAVG_METHODDEF) */
9214 
9215 #ifndef OS_SETRESUID_METHODDEF
9216     #define OS_SETRESUID_METHODDEF
9217 #endif /* !defined(OS_SETRESUID_METHODDEF) */
9218 
9219 #ifndef OS_SETRESGID_METHODDEF
9220     #define OS_SETRESGID_METHODDEF
9221 #endif /* !defined(OS_SETRESGID_METHODDEF) */
9222 
9223 #ifndef OS_GETRESUID_METHODDEF
9224     #define OS_GETRESUID_METHODDEF
9225 #endif /* !defined(OS_GETRESUID_METHODDEF) */
9226 
9227 #ifndef OS_GETRESGID_METHODDEF
9228     #define OS_GETRESGID_METHODDEF
9229 #endif /* !defined(OS_GETRESGID_METHODDEF) */
9230 
9231 #ifndef OS_GETXATTR_METHODDEF
9232     #define OS_GETXATTR_METHODDEF
9233 #endif /* !defined(OS_GETXATTR_METHODDEF) */
9234 
9235 #ifndef OS_SETXATTR_METHODDEF
9236     #define OS_SETXATTR_METHODDEF
9237 #endif /* !defined(OS_SETXATTR_METHODDEF) */
9238 
9239 #ifndef OS_REMOVEXATTR_METHODDEF
9240     #define OS_REMOVEXATTR_METHODDEF
9241 #endif /* !defined(OS_REMOVEXATTR_METHODDEF) */
9242 
9243 #ifndef OS_LISTXATTR_METHODDEF
9244     #define OS_LISTXATTR_METHODDEF
9245 #endif /* !defined(OS_LISTXATTR_METHODDEF) */
9246 
9247 #ifndef OS_MEMFD_CREATE_METHODDEF
9248     #define OS_MEMFD_CREATE_METHODDEF
9249 #endif /* !defined(OS_MEMFD_CREATE_METHODDEF) */
9250 
9251 #ifndef OS_EVENTFD_METHODDEF
9252     #define OS_EVENTFD_METHODDEF
9253 #endif /* !defined(OS_EVENTFD_METHODDEF) */
9254 
9255 #ifndef OS_EVENTFD_READ_METHODDEF
9256     #define OS_EVENTFD_READ_METHODDEF
9257 #endif /* !defined(OS_EVENTFD_READ_METHODDEF) */
9258 
9259 #ifndef OS_EVENTFD_WRITE_METHODDEF
9260     #define OS_EVENTFD_WRITE_METHODDEF
9261 #endif /* !defined(OS_EVENTFD_WRITE_METHODDEF) */
9262 
9263 #ifndef OS_GET_TERMINAL_SIZE_METHODDEF
9264     #define OS_GET_TERMINAL_SIZE_METHODDEF
9265 #endif /* !defined(OS_GET_TERMINAL_SIZE_METHODDEF) */
9266 
9267 #ifndef OS_GET_HANDLE_INHERITABLE_METHODDEF
9268     #define OS_GET_HANDLE_INHERITABLE_METHODDEF
9269 #endif /* !defined(OS_GET_HANDLE_INHERITABLE_METHODDEF) */
9270 
9271 #ifndef OS_SET_HANDLE_INHERITABLE_METHODDEF
9272     #define OS_SET_HANDLE_INHERITABLE_METHODDEF
9273 #endif /* !defined(OS_SET_HANDLE_INHERITABLE_METHODDEF) */
9274 
9275 #ifndef OS_GET_BLOCKING_METHODDEF
9276     #define OS_GET_BLOCKING_METHODDEF
9277 #endif /* !defined(OS_GET_BLOCKING_METHODDEF) */
9278 
9279 #ifndef OS_SET_BLOCKING_METHODDEF
9280     #define OS_SET_BLOCKING_METHODDEF
9281 #endif /* !defined(OS_SET_BLOCKING_METHODDEF) */
9282 
9283 #ifndef OS_GETRANDOM_METHODDEF
9284     #define OS_GETRANDOM_METHODDEF
9285 #endif /* !defined(OS_GETRANDOM_METHODDEF) */
9286 
9287 #ifndef OS__ADD_DLL_DIRECTORY_METHODDEF
9288     #define OS__ADD_DLL_DIRECTORY_METHODDEF
9289 #endif /* !defined(OS__ADD_DLL_DIRECTORY_METHODDEF) */
9290 
9291 #ifndef OS__REMOVE_DLL_DIRECTORY_METHODDEF
9292     #define OS__REMOVE_DLL_DIRECTORY_METHODDEF
9293 #endif /* !defined(OS__REMOVE_DLL_DIRECTORY_METHODDEF) */
9294 
9295 #ifndef OS_WAITSTATUS_TO_EXITCODE_METHODDEF
9296     #define OS_WAITSTATUS_TO_EXITCODE_METHODDEF
9297 #endif /* !defined(OS_WAITSTATUS_TO_EXITCODE_METHODDEF) */
9298 /*[clinic end generated code: output=05505f171cdcff72 input=a9049054013a1b77]*/
9299