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