blob: 48dd7a74b3bf22f6d21cd5f852c1d7a596492c0d [file] [log] [blame]
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001/*[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"
BNMetricsb9427072018-11-02 15:20:19 +000012" Path to be examined; can be string, bytes, a path-like object or\n"
Xiang Zhang4459e002017-01-22 13:04:17 +080013" open-file-descriptor int.\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030014" 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 \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +020031 {"stat", (PyCFunction)(void(*)(void))os_stat, METH_FASTCALL|METH_KEYWORDS, os_stat__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030032
33static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030034os_stat_impl(PyObject *module, path_t *path, int dir_fd, int follow_symlinks);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030035
36static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +020037os_stat(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030038{
39 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +030040 static const char * const _keywords[] = {"path", "dir_fd", "follow_symlinks", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +020041 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;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030044 path_t path = PATH_T_INITIALIZE("stat", "path", 0, 1);
45 int dir_fd = DEFAULT_DIR_FD;
46 int follow_symlinks = 1;
47
Serhiy Storchaka31913912019-03-14 10:32:22 +020048 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
49 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030050 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +030051 }
Serhiy Storchaka31913912019-03-14 10:32:22 +020052 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:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030071 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 \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +020090 {"lstat", (PyCFunction)(void(*)(void))os_lstat, METH_FASTCALL|METH_KEYWORDS, os_lstat__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030091
92static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030093os_lstat_impl(PyObject *module, path_t *path, int dir_fd);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030094
95static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +020096os_lstat(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030097{
98 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +030099 static const char * const _keywords[] = {"path", "dir_fd", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200100 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;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300103 path_t path = PATH_T_INITIALIZE("lstat", "path", 0, 0);
104 int dir_fd = DEFAULT_DIR_FD;
105
Serhiy Storchaka31913912019-03-14 10:32:22 +0200106 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
107 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300108 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300109 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200110 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:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300120 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"
BNMetricsb9427072018-11-02 15:20:19 +0000137" Path to be tested; can be string, bytes, or a path-like object.\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300138" 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 \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200162 {"access", (PyCFunction)(void(*)(void))os_access, METH_FASTCALL|METH_KEYWORDS, os_access__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300163
164static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300165os_access_impl(PyObject *module, path_t *path, int mode, int dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -0400166 int effective_ids, int follow_symlinks);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300167
168static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200169os_access(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300170{
171 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300172 static const char * const _keywords[] = {"path", "mode", "dir_fd", "effective_ids", "follow_symlinks", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200173 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;
Benjamin Peterson768f3b42016-09-05 15:29:33 -0700176 path_t path = PATH_T_INITIALIZE("access", "path", 0, 0);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300177 int mode;
178 int dir_fd = DEFAULT_DIR_FD;
179 int effective_ids = 0;
180 int follow_symlinks = 1;
181 int _return_value;
182
Serhiy Storchaka31913912019-03-14 10:32:22 +0200183 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf);
184 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300185 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300186 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200187 if (!path_converter(args[0], &path)) {
188 goto exit;
189 }
190 if (PyFloat_Check(args[1])) {
191 PyErr_SetString(PyExc_TypeError,
192 "integer argument expected, got float" );
193 goto exit;
194 }
195 mode = _PyLong_AsInt(args[1]);
196 if (mode == -1 && PyErr_Occurred()) {
197 goto exit;
198 }
199 if (!noptargs) {
200 goto skip_optional_kwonly;
201 }
202 if (args[2]) {
203 if (!FACCESSAT_DIR_FD_CONVERTER(args[2], &dir_fd)) {
204 goto exit;
205 }
206 if (!--noptargs) {
207 goto skip_optional_kwonly;
208 }
209 }
210 if (args[3]) {
211 effective_ids = PyObject_IsTrue(args[3]);
212 if (effective_ids < 0) {
213 goto exit;
214 }
215 if (!--noptargs) {
216 goto skip_optional_kwonly;
217 }
218 }
219 follow_symlinks = PyObject_IsTrue(args[4]);
220 if (follow_symlinks < 0) {
221 goto exit;
222 }
223skip_optional_kwonly:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300224 _return_value = os_access_impl(module, &path, mode, dir_fd, effective_ids, follow_symlinks);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300225 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300226 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300227 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300228 return_value = PyBool_FromLong((long)_return_value);
229
230exit:
231 /* Cleanup for path */
232 path_cleanup(&path);
233
234 return return_value;
235}
236
237#if defined(HAVE_TTYNAME)
238
239PyDoc_STRVAR(os_ttyname__doc__,
240"ttyname($module, fd, /)\n"
241"--\n"
242"\n"
243"Return the name of the terminal device connected to \'fd\'.\n"
244"\n"
245" fd\n"
246" Integer file descriptor handle.");
247
248#define OS_TTYNAME_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300249 {"ttyname", (PyCFunction)os_ttyname, METH_O, os_ttyname__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300250
Serhiy Storchaka4db62e12018-12-17 16:47:45 +0200251static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300252os_ttyname_impl(PyObject *module, int fd);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300253
254static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300255os_ttyname(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300256{
257 PyObject *return_value = NULL;
258 int fd;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300259
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200260 if (PyFloat_Check(arg)) {
261 PyErr_SetString(PyExc_TypeError,
262 "integer argument expected, got float" );
263 goto exit;
264 }
265 fd = _PyLong_AsInt(arg);
266 if (fd == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300267 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300268 }
Serhiy Storchaka4db62e12018-12-17 16:47:45 +0200269 return_value = os_ttyname_impl(module, fd);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300270
271exit:
272 return return_value;
273}
274
275#endif /* defined(HAVE_TTYNAME) */
276
277#if defined(HAVE_CTERMID)
278
279PyDoc_STRVAR(os_ctermid__doc__,
280"ctermid($module, /)\n"
281"--\n"
282"\n"
283"Return the name of the controlling terminal for this process.");
284
285#define OS_CTERMID_METHODDEF \
286 {"ctermid", (PyCFunction)os_ctermid, METH_NOARGS, os_ctermid__doc__},
287
288static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300289os_ctermid_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300290
291static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300292os_ctermid(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300293{
294 return os_ctermid_impl(module);
295}
296
297#endif /* defined(HAVE_CTERMID) */
298
299PyDoc_STRVAR(os_chdir__doc__,
300"chdir($module, /, path)\n"
301"--\n"
302"\n"
303"Change the current working directory to the specified path.\n"
304"\n"
305"path may always be specified as a string.\n"
306"On some platforms, path may also be specified as an open file descriptor.\n"
307" If this functionality is unavailable, using it raises an exception.");
308
309#define OS_CHDIR_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200310 {"chdir", (PyCFunction)(void(*)(void))os_chdir, METH_FASTCALL|METH_KEYWORDS, os_chdir__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300311
312static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300313os_chdir_impl(PyObject *module, path_t *path);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300314
315static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200316os_chdir(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300317{
318 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300319 static const char * const _keywords[] = {"path", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200320 static _PyArg_Parser _parser = {NULL, _keywords, "chdir", 0};
321 PyObject *argsbuf[1];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300322 path_t path = PATH_T_INITIALIZE("chdir", "path", 0, PATH_HAVE_FCHDIR);
323
Serhiy Storchaka31913912019-03-14 10:32:22 +0200324 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
325 if (!args) {
326 goto exit;
327 }
328 if (!path_converter(args[0], &path)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300329 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300330 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300331 return_value = os_chdir_impl(module, &path);
332
333exit:
334 /* Cleanup for path */
335 path_cleanup(&path);
336
337 return return_value;
338}
339
340#if defined(HAVE_FCHDIR)
341
342PyDoc_STRVAR(os_fchdir__doc__,
343"fchdir($module, /, fd)\n"
344"--\n"
345"\n"
346"Change to the directory of the given file descriptor.\n"
347"\n"
348"fd must be opened on a directory, not a file.\n"
349"Equivalent to os.chdir(fd).");
350
351#define OS_FCHDIR_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200352 {"fchdir", (PyCFunction)(void(*)(void))os_fchdir, METH_FASTCALL|METH_KEYWORDS, os_fchdir__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300353
354static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300355os_fchdir_impl(PyObject *module, int fd);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300356
357static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200358os_fchdir(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300359{
360 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300361 static const char * const _keywords[] = {"fd", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200362 static _PyArg_Parser _parser = {NULL, _keywords, "fchdir", 0};
363 PyObject *argsbuf[1];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300364 int fd;
365
Serhiy Storchaka31913912019-03-14 10:32:22 +0200366 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
367 if (!args) {
368 goto exit;
369 }
370 if (!fildes_converter(args[0], &fd)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300371 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300372 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300373 return_value = os_fchdir_impl(module, fd);
374
375exit:
376 return return_value;
377}
378
379#endif /* defined(HAVE_FCHDIR) */
380
381PyDoc_STRVAR(os_chmod__doc__,
382"chmod($module, /, path, mode, *, dir_fd=None, follow_symlinks=True)\n"
383"--\n"
384"\n"
385"Change the access permissions of a file.\n"
386"\n"
387" path\n"
BNMetricsb9427072018-11-02 15:20:19 +0000388" Path to be modified. May always be specified as a str, bytes, or a path-like object.\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300389" On some platforms, path may also be specified as an open file descriptor.\n"
390" If this functionality is unavailable, using it raises an exception.\n"
391" mode\n"
392" Operating-system mode bitfield.\n"
393" dir_fd\n"
394" If not None, it should be a file descriptor open to a directory,\n"
395" and path should be relative; path will then be relative to that\n"
396" directory.\n"
397" follow_symlinks\n"
398" If False, and the last element of the path is a symbolic link,\n"
399" chmod will modify the symbolic link itself instead of the file\n"
400" the link points to.\n"
401"\n"
402"It is an error to use dir_fd or follow_symlinks when specifying path as\n"
403" an open file descriptor.\n"
404"dir_fd and follow_symlinks may not be implemented on your platform.\n"
405" If they are unavailable, using them will raise a NotImplementedError.");
406
407#define OS_CHMOD_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200408 {"chmod", (PyCFunction)(void(*)(void))os_chmod, METH_FASTCALL|METH_KEYWORDS, os_chmod__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300409
410static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300411os_chmod_impl(PyObject *module, path_t *path, int mode, int dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -0400412 int follow_symlinks);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300413
414static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200415os_chmod(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300416{
417 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300418 static const char * const _keywords[] = {"path", "mode", "dir_fd", "follow_symlinks", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200419 static _PyArg_Parser _parser = {NULL, _keywords, "chmod", 0};
420 PyObject *argsbuf[4];
421 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300422 path_t path = PATH_T_INITIALIZE("chmod", "path", 0, PATH_HAVE_FCHMOD);
423 int mode;
424 int dir_fd = DEFAULT_DIR_FD;
425 int follow_symlinks = 1;
426
Serhiy Storchaka31913912019-03-14 10:32:22 +0200427 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf);
428 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300429 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300430 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200431 if (!path_converter(args[0], &path)) {
432 goto exit;
433 }
434 if (PyFloat_Check(args[1])) {
435 PyErr_SetString(PyExc_TypeError,
436 "integer argument expected, got float" );
437 goto exit;
438 }
439 mode = _PyLong_AsInt(args[1]);
440 if (mode == -1 && PyErr_Occurred()) {
441 goto exit;
442 }
443 if (!noptargs) {
444 goto skip_optional_kwonly;
445 }
446 if (args[2]) {
447 if (!FCHMODAT_DIR_FD_CONVERTER(args[2], &dir_fd)) {
448 goto exit;
449 }
450 if (!--noptargs) {
451 goto skip_optional_kwonly;
452 }
453 }
454 follow_symlinks = PyObject_IsTrue(args[3]);
455 if (follow_symlinks < 0) {
456 goto exit;
457 }
458skip_optional_kwonly:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300459 return_value = os_chmod_impl(module, &path, mode, dir_fd, follow_symlinks);
460
461exit:
462 /* Cleanup for path */
463 path_cleanup(&path);
464
465 return return_value;
466}
467
468#if defined(HAVE_FCHMOD)
469
470PyDoc_STRVAR(os_fchmod__doc__,
471"fchmod($module, /, fd, mode)\n"
472"--\n"
473"\n"
474"Change the access permissions of the file given by file descriptor fd.\n"
475"\n"
476"Equivalent to os.chmod(fd, mode).");
477
478#define OS_FCHMOD_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200479 {"fchmod", (PyCFunction)(void(*)(void))os_fchmod, METH_FASTCALL|METH_KEYWORDS, os_fchmod__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300480
481static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300482os_fchmod_impl(PyObject *module, int fd, int mode);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300483
484static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200485os_fchmod(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300486{
487 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300488 static const char * const _keywords[] = {"fd", "mode", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200489 static _PyArg_Parser _parser = {NULL, _keywords, "fchmod", 0};
490 PyObject *argsbuf[2];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300491 int fd;
492 int mode;
493
Serhiy Storchaka31913912019-03-14 10:32:22 +0200494 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf);
495 if (!args) {
496 goto exit;
497 }
498 if (PyFloat_Check(args[0])) {
499 PyErr_SetString(PyExc_TypeError,
500 "integer argument expected, got float" );
501 goto exit;
502 }
503 fd = _PyLong_AsInt(args[0]);
504 if (fd == -1 && PyErr_Occurred()) {
505 goto exit;
506 }
507 if (PyFloat_Check(args[1])) {
508 PyErr_SetString(PyExc_TypeError,
509 "integer argument expected, got float" );
510 goto exit;
511 }
512 mode = _PyLong_AsInt(args[1]);
513 if (mode == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300514 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300515 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300516 return_value = os_fchmod_impl(module, fd, mode);
517
518exit:
519 return return_value;
520}
521
522#endif /* defined(HAVE_FCHMOD) */
523
524#if defined(HAVE_LCHMOD)
525
526PyDoc_STRVAR(os_lchmod__doc__,
527"lchmod($module, /, path, mode)\n"
528"--\n"
529"\n"
530"Change the access permissions of a file, without following symbolic links.\n"
531"\n"
532"If path is a symlink, this affects the link itself rather than the target.\n"
533"Equivalent to chmod(path, mode, follow_symlinks=False).\"");
534
535#define OS_LCHMOD_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200536 {"lchmod", (PyCFunction)(void(*)(void))os_lchmod, METH_FASTCALL|METH_KEYWORDS, os_lchmod__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300537
538static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300539os_lchmod_impl(PyObject *module, path_t *path, int mode);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300540
541static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200542os_lchmod(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300543{
544 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300545 static const char * const _keywords[] = {"path", "mode", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200546 static _PyArg_Parser _parser = {NULL, _keywords, "lchmod", 0};
547 PyObject *argsbuf[2];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300548 path_t path = PATH_T_INITIALIZE("lchmod", "path", 0, 0);
549 int mode;
550
Serhiy Storchaka31913912019-03-14 10:32:22 +0200551 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf);
552 if (!args) {
553 goto exit;
554 }
555 if (!path_converter(args[0], &path)) {
556 goto exit;
557 }
558 if (PyFloat_Check(args[1])) {
559 PyErr_SetString(PyExc_TypeError,
560 "integer argument expected, got float" );
561 goto exit;
562 }
563 mode = _PyLong_AsInt(args[1]);
564 if (mode == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300565 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300566 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300567 return_value = os_lchmod_impl(module, &path, mode);
568
569exit:
570 /* Cleanup for path */
571 path_cleanup(&path);
572
573 return return_value;
574}
575
576#endif /* defined(HAVE_LCHMOD) */
577
578#if defined(HAVE_CHFLAGS)
579
580PyDoc_STRVAR(os_chflags__doc__,
581"chflags($module, /, path, flags, follow_symlinks=True)\n"
582"--\n"
583"\n"
584"Set file flags.\n"
585"\n"
586"If follow_symlinks is False, and the last element of the path is a symbolic\n"
587" link, chflags will change flags on the symbolic link itself instead of the\n"
588" file the link points to.\n"
589"follow_symlinks may not be implemented on your platform. If it is\n"
590"unavailable, using it will raise a NotImplementedError.");
591
592#define OS_CHFLAGS_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200593 {"chflags", (PyCFunction)(void(*)(void))os_chflags, METH_FASTCALL|METH_KEYWORDS, os_chflags__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300594
595static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300596os_chflags_impl(PyObject *module, path_t *path, unsigned long flags,
Larry Hastings89964c42015-04-14 18:07:59 -0400597 int follow_symlinks);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300598
599static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200600os_chflags(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300601{
602 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300603 static const char * const _keywords[] = {"path", "flags", "follow_symlinks", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200604 static _PyArg_Parser _parser = {NULL, _keywords, "chflags", 0};
605 PyObject *argsbuf[3];
606 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300607 path_t path = PATH_T_INITIALIZE("chflags", "path", 0, 0);
608 unsigned long flags;
609 int follow_symlinks = 1;
610
Serhiy Storchaka31913912019-03-14 10:32:22 +0200611 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 3, 0, argsbuf);
612 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300613 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300614 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200615 if (!path_converter(args[0], &path)) {
616 goto exit;
617 }
618 if (!PyLong_Check(args[1])) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200619 _PyArg_BadArgument("chflags", "argument 'flags'", "int", args[1]);
Serhiy Storchaka31913912019-03-14 10:32:22 +0200620 goto exit;
621 }
622 flags = PyLong_AsUnsignedLongMask(args[1]);
623 if (!noptargs) {
624 goto skip_optional_pos;
625 }
626 follow_symlinks = PyObject_IsTrue(args[2]);
627 if (follow_symlinks < 0) {
628 goto exit;
629 }
630skip_optional_pos:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300631 return_value = os_chflags_impl(module, &path, flags, follow_symlinks);
632
633exit:
634 /* Cleanup for path */
635 path_cleanup(&path);
636
637 return return_value;
638}
639
640#endif /* defined(HAVE_CHFLAGS) */
641
642#if defined(HAVE_LCHFLAGS)
643
644PyDoc_STRVAR(os_lchflags__doc__,
645"lchflags($module, /, path, flags)\n"
646"--\n"
647"\n"
648"Set file flags.\n"
649"\n"
650"This function will not follow symbolic links.\n"
651"Equivalent to chflags(path, flags, follow_symlinks=False).");
652
653#define OS_LCHFLAGS_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200654 {"lchflags", (PyCFunction)(void(*)(void))os_lchflags, METH_FASTCALL|METH_KEYWORDS, os_lchflags__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300655
656static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300657os_lchflags_impl(PyObject *module, path_t *path, unsigned long flags);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300658
659static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200660os_lchflags(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300661{
662 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300663 static const char * const _keywords[] = {"path", "flags", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200664 static _PyArg_Parser _parser = {NULL, _keywords, "lchflags", 0};
665 PyObject *argsbuf[2];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300666 path_t path = PATH_T_INITIALIZE("lchflags", "path", 0, 0);
667 unsigned long flags;
668
Serhiy Storchaka31913912019-03-14 10:32:22 +0200669 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf);
670 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300671 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300672 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200673 if (!path_converter(args[0], &path)) {
674 goto exit;
675 }
676 if (!PyLong_Check(args[1])) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200677 _PyArg_BadArgument("lchflags", "argument 'flags'", "int", args[1]);
Serhiy Storchaka31913912019-03-14 10:32:22 +0200678 goto exit;
679 }
680 flags = PyLong_AsUnsignedLongMask(args[1]);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300681 return_value = os_lchflags_impl(module, &path, flags);
682
683exit:
684 /* Cleanup for path */
685 path_cleanup(&path);
686
687 return return_value;
688}
689
690#endif /* defined(HAVE_LCHFLAGS) */
691
692#if defined(HAVE_CHROOT)
693
694PyDoc_STRVAR(os_chroot__doc__,
695"chroot($module, /, path)\n"
696"--\n"
697"\n"
698"Change root directory to path.");
699
700#define OS_CHROOT_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200701 {"chroot", (PyCFunction)(void(*)(void))os_chroot, METH_FASTCALL|METH_KEYWORDS, os_chroot__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300702
703static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300704os_chroot_impl(PyObject *module, path_t *path);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300705
706static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200707os_chroot(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300708{
709 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300710 static const char * const _keywords[] = {"path", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200711 static _PyArg_Parser _parser = {NULL, _keywords, "chroot", 0};
712 PyObject *argsbuf[1];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300713 path_t path = PATH_T_INITIALIZE("chroot", "path", 0, 0);
714
Serhiy Storchaka31913912019-03-14 10:32:22 +0200715 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
716 if (!args) {
717 goto exit;
718 }
719 if (!path_converter(args[0], &path)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300720 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300721 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300722 return_value = os_chroot_impl(module, &path);
723
724exit:
725 /* Cleanup for path */
726 path_cleanup(&path);
727
728 return return_value;
729}
730
731#endif /* defined(HAVE_CHROOT) */
732
733#if defined(HAVE_FSYNC)
734
735PyDoc_STRVAR(os_fsync__doc__,
736"fsync($module, /, fd)\n"
737"--\n"
738"\n"
739"Force write of fd to disk.");
740
741#define OS_FSYNC_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200742 {"fsync", (PyCFunction)(void(*)(void))os_fsync, METH_FASTCALL|METH_KEYWORDS, os_fsync__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300743
744static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300745os_fsync_impl(PyObject *module, int fd);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300746
747static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200748os_fsync(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300749{
750 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300751 static const char * const _keywords[] = {"fd", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200752 static _PyArg_Parser _parser = {NULL, _keywords, "fsync", 0};
753 PyObject *argsbuf[1];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300754 int fd;
755
Serhiy Storchaka31913912019-03-14 10:32:22 +0200756 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
757 if (!args) {
758 goto exit;
759 }
760 if (!fildes_converter(args[0], &fd)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300761 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300762 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300763 return_value = os_fsync_impl(module, fd);
764
765exit:
766 return return_value;
767}
768
769#endif /* defined(HAVE_FSYNC) */
770
771#if defined(HAVE_SYNC)
772
773PyDoc_STRVAR(os_sync__doc__,
774"sync($module, /)\n"
775"--\n"
776"\n"
777"Force write of everything to disk.");
778
779#define OS_SYNC_METHODDEF \
780 {"sync", (PyCFunction)os_sync, METH_NOARGS, os_sync__doc__},
781
782static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300783os_sync_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300784
785static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300786os_sync(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300787{
788 return os_sync_impl(module);
789}
790
791#endif /* defined(HAVE_SYNC) */
792
793#if defined(HAVE_FDATASYNC)
794
795PyDoc_STRVAR(os_fdatasync__doc__,
796"fdatasync($module, /, fd)\n"
797"--\n"
798"\n"
799"Force write of fd to disk without forcing update of metadata.");
800
801#define OS_FDATASYNC_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200802 {"fdatasync", (PyCFunction)(void(*)(void))os_fdatasync, METH_FASTCALL|METH_KEYWORDS, os_fdatasync__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300803
804static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300805os_fdatasync_impl(PyObject *module, int fd);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300806
807static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200808os_fdatasync(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300809{
810 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300811 static const char * const _keywords[] = {"fd", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200812 static _PyArg_Parser _parser = {NULL, _keywords, "fdatasync", 0};
813 PyObject *argsbuf[1];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300814 int fd;
815
Serhiy Storchaka31913912019-03-14 10:32:22 +0200816 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
817 if (!args) {
818 goto exit;
819 }
820 if (!fildes_converter(args[0], &fd)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300821 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300822 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300823 return_value = os_fdatasync_impl(module, fd);
824
825exit:
826 return return_value;
827}
828
829#endif /* defined(HAVE_FDATASYNC) */
830
831#if defined(HAVE_CHOWN)
832
833PyDoc_STRVAR(os_chown__doc__,
834"chown($module, /, path, uid, gid, *, dir_fd=None, follow_symlinks=True)\n"
835"--\n"
836"\n"
837"Change the owner and group id of path to the numeric uid and gid.\\\n"
838"\n"
839" path\n"
BNMetricsb9427072018-11-02 15:20:19 +0000840" Path to be examined; can be string, bytes, a path-like object, or open-file-descriptor int.\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300841" dir_fd\n"
842" If not None, it should be a file descriptor open to a directory,\n"
843" and path should be relative; path will then be relative to that\n"
844" directory.\n"
845" follow_symlinks\n"
846" If False, and the last element of the path is a symbolic link,\n"
847" stat will examine the symbolic link itself instead of the file\n"
848" the link points to.\n"
849"\n"
850"path may always be specified as a string.\n"
851"On some platforms, path may also be specified as an open file descriptor.\n"
852" If this functionality is unavailable, using it raises an exception.\n"
853"If dir_fd is not None, it should be a file descriptor open to a directory,\n"
854" and path should be relative; path will then be relative to that directory.\n"
855"If follow_symlinks is False, and the last element of the path is a symbolic\n"
856" link, chown will modify the symbolic link itself instead of the file the\n"
857" link points to.\n"
858"It is an error to use dir_fd or follow_symlinks when specifying path as\n"
859" an open file descriptor.\n"
860"dir_fd and follow_symlinks may not be implemented on your platform.\n"
861" If they are unavailable, using them will raise a NotImplementedError.");
862
863#define OS_CHOWN_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200864 {"chown", (PyCFunction)(void(*)(void))os_chown, METH_FASTCALL|METH_KEYWORDS, os_chown__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300865
866static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300867os_chown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid,
Larry Hastings89964c42015-04-14 18:07:59 -0400868 int dir_fd, int follow_symlinks);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300869
870static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200871os_chown(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300872{
873 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300874 static const char * const _keywords[] = {"path", "uid", "gid", "dir_fd", "follow_symlinks", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200875 static _PyArg_Parser _parser = {NULL, _keywords, "chown", 0};
876 PyObject *argsbuf[5];
877 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 3;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300878 path_t path = PATH_T_INITIALIZE("chown", "path", 0, PATH_HAVE_FCHOWN);
879 uid_t uid;
880 gid_t gid;
881 int dir_fd = DEFAULT_DIR_FD;
882 int follow_symlinks = 1;
883
Serhiy Storchaka31913912019-03-14 10:32:22 +0200884 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 3, 3, 0, argsbuf);
885 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300886 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300887 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200888 if (!path_converter(args[0], &path)) {
889 goto exit;
890 }
891 if (!_Py_Uid_Converter(args[1], &uid)) {
892 goto exit;
893 }
894 if (!_Py_Gid_Converter(args[2], &gid)) {
895 goto exit;
896 }
897 if (!noptargs) {
898 goto skip_optional_kwonly;
899 }
900 if (args[3]) {
901 if (!FCHOWNAT_DIR_FD_CONVERTER(args[3], &dir_fd)) {
902 goto exit;
903 }
904 if (!--noptargs) {
905 goto skip_optional_kwonly;
906 }
907 }
908 follow_symlinks = PyObject_IsTrue(args[4]);
909 if (follow_symlinks < 0) {
910 goto exit;
911 }
912skip_optional_kwonly:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300913 return_value = os_chown_impl(module, &path, uid, gid, dir_fd, follow_symlinks);
914
915exit:
916 /* Cleanup for path */
917 path_cleanup(&path);
918
919 return return_value;
920}
921
922#endif /* defined(HAVE_CHOWN) */
923
924#if defined(HAVE_FCHOWN)
925
926PyDoc_STRVAR(os_fchown__doc__,
927"fchown($module, /, fd, uid, gid)\n"
928"--\n"
929"\n"
930"Change the owner and group id of the file specified by file descriptor.\n"
931"\n"
932"Equivalent to os.chown(fd, uid, gid).");
933
934#define OS_FCHOWN_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200935 {"fchown", (PyCFunction)(void(*)(void))os_fchown, METH_FASTCALL|METH_KEYWORDS, os_fchown__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300936
937static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300938os_fchown_impl(PyObject *module, int fd, uid_t uid, gid_t gid);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300939
940static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200941os_fchown(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300942{
943 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300944 static const char * const _keywords[] = {"fd", "uid", "gid", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200945 static _PyArg_Parser _parser = {NULL, _keywords, "fchown", 0};
946 PyObject *argsbuf[3];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300947 int fd;
948 uid_t uid;
949 gid_t gid;
950
Serhiy Storchaka31913912019-03-14 10:32:22 +0200951 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 3, 3, 0, argsbuf);
952 if (!args) {
953 goto exit;
954 }
955 if (PyFloat_Check(args[0])) {
956 PyErr_SetString(PyExc_TypeError,
957 "integer argument expected, got float" );
958 goto exit;
959 }
960 fd = _PyLong_AsInt(args[0]);
961 if (fd == -1 && PyErr_Occurred()) {
962 goto exit;
963 }
964 if (!_Py_Uid_Converter(args[1], &uid)) {
965 goto exit;
966 }
967 if (!_Py_Gid_Converter(args[2], &gid)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300968 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300969 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300970 return_value = os_fchown_impl(module, fd, uid, gid);
971
972exit:
973 return return_value;
974}
975
976#endif /* defined(HAVE_FCHOWN) */
977
978#if defined(HAVE_LCHOWN)
979
980PyDoc_STRVAR(os_lchown__doc__,
981"lchown($module, /, path, uid, gid)\n"
982"--\n"
983"\n"
984"Change the owner and group id of path to the numeric uid and gid.\n"
985"\n"
986"This function will not follow symbolic links.\n"
987"Equivalent to os.chown(path, uid, gid, follow_symlinks=False).");
988
989#define OS_LCHOWN_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200990 {"lchown", (PyCFunction)(void(*)(void))os_lchown, METH_FASTCALL|METH_KEYWORDS, os_lchown__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300991
992static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300993os_lchown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300994
995static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200996os_lchown(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300997{
998 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300999 static const char * const _keywords[] = {"path", "uid", "gid", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02001000 static _PyArg_Parser _parser = {NULL, _keywords, "lchown", 0};
1001 PyObject *argsbuf[3];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001002 path_t path = PATH_T_INITIALIZE("lchown", "path", 0, 0);
1003 uid_t uid;
1004 gid_t gid;
1005
Serhiy Storchaka31913912019-03-14 10:32:22 +02001006 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 3, 3, 0, argsbuf);
1007 if (!args) {
1008 goto exit;
1009 }
1010 if (!path_converter(args[0], &path)) {
1011 goto exit;
1012 }
1013 if (!_Py_Uid_Converter(args[1], &uid)) {
1014 goto exit;
1015 }
1016 if (!_Py_Gid_Converter(args[2], &gid)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001017 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001018 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001019 return_value = os_lchown_impl(module, &path, uid, gid);
1020
1021exit:
1022 /* Cleanup for path */
1023 path_cleanup(&path);
1024
1025 return return_value;
1026}
1027
1028#endif /* defined(HAVE_LCHOWN) */
1029
1030PyDoc_STRVAR(os_getcwd__doc__,
1031"getcwd($module, /)\n"
1032"--\n"
1033"\n"
1034"Return a unicode string representing the current working directory.");
1035
1036#define OS_GETCWD_METHODDEF \
1037 {"getcwd", (PyCFunction)os_getcwd, METH_NOARGS, os_getcwd__doc__},
1038
1039static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001040os_getcwd_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001041
1042static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001043os_getcwd(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001044{
1045 return os_getcwd_impl(module);
1046}
1047
1048PyDoc_STRVAR(os_getcwdb__doc__,
1049"getcwdb($module, /)\n"
1050"--\n"
1051"\n"
1052"Return a bytes string representing the current working directory.");
1053
1054#define OS_GETCWDB_METHODDEF \
1055 {"getcwdb", (PyCFunction)os_getcwdb, METH_NOARGS, os_getcwdb__doc__},
1056
1057static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001058os_getcwdb_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001059
1060static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001061os_getcwdb(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001062{
1063 return os_getcwdb_impl(module);
1064}
1065
1066#if defined(HAVE_LINK)
1067
1068PyDoc_STRVAR(os_link__doc__,
1069"link($module, /, src, dst, *, src_dir_fd=None, dst_dir_fd=None,\n"
1070" follow_symlinks=True)\n"
1071"--\n"
1072"\n"
1073"Create a hard link to a file.\n"
1074"\n"
1075"If either src_dir_fd or dst_dir_fd is not None, it should be a file\n"
1076" descriptor open to a directory, and the respective path string (src or dst)\n"
1077" should be relative; the path will then be relative to that directory.\n"
1078"If follow_symlinks is False, and the last element of src is a symbolic\n"
1079" link, link will create a link to the symbolic link itself instead of the\n"
1080" file the link points to.\n"
1081"src_dir_fd, dst_dir_fd, and follow_symlinks may not be implemented on your\n"
1082" platform. If they are unavailable, using them will raise a\n"
1083" NotImplementedError.");
1084
1085#define OS_LINK_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02001086 {"link", (PyCFunction)(void(*)(void))os_link, METH_FASTCALL|METH_KEYWORDS, os_link__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001087
1088static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001089os_link_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04001090 int dst_dir_fd, int follow_symlinks);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001091
1092static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001093os_link(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001094{
1095 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001096 static const char * const _keywords[] = {"src", "dst", "src_dir_fd", "dst_dir_fd", "follow_symlinks", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02001097 static _PyArg_Parser _parser = {NULL, _keywords, "link", 0};
1098 PyObject *argsbuf[5];
1099 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001100 path_t src = PATH_T_INITIALIZE("link", "src", 0, 0);
1101 path_t dst = PATH_T_INITIALIZE("link", "dst", 0, 0);
1102 int src_dir_fd = DEFAULT_DIR_FD;
1103 int dst_dir_fd = DEFAULT_DIR_FD;
1104 int follow_symlinks = 1;
1105
Serhiy Storchaka31913912019-03-14 10:32:22 +02001106 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf);
1107 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001108 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001109 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02001110 if (!path_converter(args[0], &src)) {
1111 goto exit;
1112 }
1113 if (!path_converter(args[1], &dst)) {
1114 goto exit;
1115 }
1116 if (!noptargs) {
1117 goto skip_optional_kwonly;
1118 }
1119 if (args[2]) {
1120 if (!dir_fd_converter(args[2], &src_dir_fd)) {
1121 goto exit;
1122 }
1123 if (!--noptargs) {
1124 goto skip_optional_kwonly;
1125 }
1126 }
1127 if (args[3]) {
1128 if (!dir_fd_converter(args[3], &dst_dir_fd)) {
1129 goto exit;
1130 }
1131 if (!--noptargs) {
1132 goto skip_optional_kwonly;
1133 }
1134 }
1135 follow_symlinks = PyObject_IsTrue(args[4]);
1136 if (follow_symlinks < 0) {
1137 goto exit;
1138 }
1139skip_optional_kwonly:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001140 return_value = os_link_impl(module, &src, &dst, src_dir_fd, dst_dir_fd, follow_symlinks);
1141
1142exit:
1143 /* Cleanup for src */
1144 path_cleanup(&src);
1145 /* Cleanup for dst */
1146 path_cleanup(&dst);
1147
1148 return return_value;
1149}
1150
1151#endif /* defined(HAVE_LINK) */
1152
1153PyDoc_STRVAR(os_listdir__doc__,
1154"listdir($module, /, path=None)\n"
1155"--\n"
1156"\n"
1157"Return a list containing the names of the files in the directory.\n"
1158"\n"
BNMetricsb9427072018-11-02 15:20:19 +00001159"path can be specified as either str, bytes, or a path-like object. If path is bytes,\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001160" the filenames returned will also be bytes; in all other circumstances\n"
1161" the filenames returned will be str.\n"
1162"If path is None, uses the path=\'.\'.\n"
1163"On some platforms, path may also be specified as an open file descriptor;\\\n"
1164" the file descriptor must refer to a directory.\n"
1165" If this functionality is unavailable, using it raises NotImplementedError.\n"
1166"\n"
1167"The list is in arbitrary order. It does not include the special\n"
1168"entries \'.\' and \'..\' even if they are present in the directory.");
1169
1170#define OS_LISTDIR_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02001171 {"listdir", (PyCFunction)(void(*)(void))os_listdir, METH_FASTCALL|METH_KEYWORDS, os_listdir__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001172
1173static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001174os_listdir_impl(PyObject *module, path_t *path);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001175
1176static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001177os_listdir(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001178{
1179 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001180 static const char * const _keywords[] = {"path", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02001181 static _PyArg_Parser _parser = {NULL, _keywords, "listdir", 0};
1182 PyObject *argsbuf[1];
1183 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001184 path_t path = PATH_T_INITIALIZE("listdir", "path", 1, PATH_HAVE_FDOPENDIR);
1185
Serhiy Storchaka31913912019-03-14 10:32:22 +02001186 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
1187 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001188 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001189 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02001190 if (!noptargs) {
1191 goto skip_optional_pos;
1192 }
1193 if (!path_converter(args[0], &path)) {
1194 goto exit;
1195 }
1196skip_optional_pos:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001197 return_value = os_listdir_impl(module, &path);
1198
1199exit:
1200 /* Cleanup for path */
1201 path_cleanup(&path);
1202
1203 return return_value;
1204}
1205
1206#if defined(MS_WINDOWS)
1207
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03001208PyDoc_STRVAR(os__getfullpathname__doc__,
1209"_getfullpathname($module, path, /)\n"
1210"--\n"
1211"\n");
1212
1213#define OS__GETFULLPATHNAME_METHODDEF \
1214 {"_getfullpathname", (PyCFunction)os__getfullpathname, METH_O, os__getfullpathname__doc__},
1215
1216static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001217os__getfullpathname_impl(PyObject *module, path_t *path);
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03001218
1219static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001220os__getfullpathname(PyObject *module, PyObject *arg)
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03001221{
1222 PyObject *return_value = NULL;
1223 path_t path = PATH_T_INITIALIZE("_getfullpathname", "path", 0, 0);
1224
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02001225 if (!path_converter(arg, &path)) {
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03001226 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001227 }
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03001228 return_value = os__getfullpathname_impl(module, &path);
1229
1230exit:
1231 /* Cleanup for path */
1232 path_cleanup(&path);
1233
1234 return return_value;
1235}
1236
1237#endif /* defined(MS_WINDOWS) */
1238
1239#if defined(MS_WINDOWS)
1240
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001241PyDoc_STRVAR(os__getfinalpathname__doc__,
1242"_getfinalpathname($module, path, /)\n"
1243"--\n"
1244"\n"
1245"A helper function for samepath on windows.");
1246
1247#define OS__GETFINALPATHNAME_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03001248 {"_getfinalpathname", (PyCFunction)os__getfinalpathname, METH_O, os__getfinalpathname__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001249
1250static PyObject *
Steve Dower23ad6d02018-02-22 10:39:10 -08001251os__getfinalpathname_impl(PyObject *module, path_t *path);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001252
1253static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001254os__getfinalpathname(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001255{
1256 PyObject *return_value = NULL;
Steve Dower23ad6d02018-02-22 10:39:10 -08001257 path_t path = PATH_T_INITIALIZE("_getfinalpathname", "path", 0, 0);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001258
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02001259 if (!path_converter(arg, &path)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001260 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001261 }
Steve Dower23ad6d02018-02-22 10:39:10 -08001262 return_value = os__getfinalpathname_impl(module, &path);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001263
1264exit:
Steve Dower23ad6d02018-02-22 10:39:10 -08001265 /* Cleanup for path */
1266 path_cleanup(&path);
1267
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001268 return return_value;
1269}
1270
1271#endif /* defined(MS_WINDOWS) */
1272
1273#if defined(MS_WINDOWS)
1274
1275PyDoc_STRVAR(os__getvolumepathname__doc__,
1276"_getvolumepathname($module, /, path)\n"
1277"--\n"
1278"\n"
1279"A helper function for ismount on Win32.");
1280
1281#define OS__GETVOLUMEPATHNAME_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02001282 {"_getvolumepathname", (PyCFunction)(void(*)(void))os__getvolumepathname, METH_FASTCALL|METH_KEYWORDS, os__getvolumepathname__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001283
1284static PyObject *
Steve Dower23ad6d02018-02-22 10:39:10 -08001285os__getvolumepathname_impl(PyObject *module, path_t *path);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001286
1287static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001288os__getvolumepathname(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001289{
1290 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001291 static const char * const _keywords[] = {"path", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02001292 static _PyArg_Parser _parser = {NULL, _keywords, "_getvolumepathname", 0};
1293 PyObject *argsbuf[1];
Steve Dower23ad6d02018-02-22 10:39:10 -08001294 path_t path = PATH_T_INITIALIZE("_getvolumepathname", "path", 0, 0);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001295
Serhiy Storchaka31913912019-03-14 10:32:22 +02001296 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
1297 if (!args) {
1298 goto exit;
1299 }
1300 if (!path_converter(args[0], &path)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001301 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001302 }
Steve Dower23ad6d02018-02-22 10:39:10 -08001303 return_value = os__getvolumepathname_impl(module, &path);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001304
1305exit:
Steve Dower23ad6d02018-02-22 10:39:10 -08001306 /* Cleanup for path */
1307 path_cleanup(&path);
1308
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001309 return return_value;
1310}
1311
1312#endif /* defined(MS_WINDOWS) */
1313
1314PyDoc_STRVAR(os_mkdir__doc__,
1315"mkdir($module, /, path, mode=511, *, dir_fd=None)\n"
1316"--\n"
1317"\n"
1318"Create a directory.\n"
1319"\n"
1320"If dir_fd is not None, it should be a file descriptor open to a directory,\n"
1321" and path should be relative; path will then be relative to that directory.\n"
1322"dir_fd may not be implemented on your platform.\n"
1323" If it is unavailable, using it will raise a NotImplementedError.\n"
1324"\n"
1325"The mode argument is ignored on Windows.");
1326
1327#define OS_MKDIR_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02001328 {"mkdir", (PyCFunction)(void(*)(void))os_mkdir, METH_FASTCALL|METH_KEYWORDS, os_mkdir__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001329
1330static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001331os_mkdir_impl(PyObject *module, path_t *path, int mode, int dir_fd);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001332
1333static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001334os_mkdir(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001335{
1336 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001337 static const char * const _keywords[] = {"path", "mode", "dir_fd", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02001338 static _PyArg_Parser _parser = {NULL, _keywords, "mkdir", 0};
1339 PyObject *argsbuf[3];
1340 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001341 path_t path = PATH_T_INITIALIZE("mkdir", "path", 0, 0);
1342 int mode = 511;
1343 int dir_fd = DEFAULT_DIR_FD;
1344
Serhiy Storchaka31913912019-03-14 10:32:22 +02001345 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
1346 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001347 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001348 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02001349 if (!path_converter(args[0], &path)) {
1350 goto exit;
1351 }
1352 if (!noptargs) {
1353 goto skip_optional_pos;
1354 }
1355 if (args[1]) {
1356 if (PyFloat_Check(args[1])) {
1357 PyErr_SetString(PyExc_TypeError,
1358 "integer argument expected, got float" );
1359 goto exit;
1360 }
1361 mode = _PyLong_AsInt(args[1]);
1362 if (mode == -1 && PyErr_Occurred()) {
1363 goto exit;
1364 }
1365 if (!--noptargs) {
1366 goto skip_optional_pos;
1367 }
1368 }
1369skip_optional_pos:
1370 if (!noptargs) {
1371 goto skip_optional_kwonly;
1372 }
1373 if (!MKDIRAT_DIR_FD_CONVERTER(args[2], &dir_fd)) {
1374 goto exit;
1375 }
1376skip_optional_kwonly:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001377 return_value = os_mkdir_impl(module, &path, mode, dir_fd);
1378
1379exit:
1380 /* Cleanup for path */
1381 path_cleanup(&path);
1382
1383 return return_value;
1384}
1385
1386#if defined(HAVE_NICE)
1387
1388PyDoc_STRVAR(os_nice__doc__,
1389"nice($module, increment, /)\n"
1390"--\n"
1391"\n"
1392"Add increment to the priority of process and return the new priority.");
1393
1394#define OS_NICE_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03001395 {"nice", (PyCFunction)os_nice, METH_O, os_nice__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001396
1397static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001398os_nice_impl(PyObject *module, int increment);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001399
1400static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001401os_nice(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001402{
1403 PyObject *return_value = NULL;
1404 int increment;
1405
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02001406 if (PyFloat_Check(arg)) {
1407 PyErr_SetString(PyExc_TypeError,
1408 "integer argument expected, got float" );
1409 goto exit;
1410 }
1411 increment = _PyLong_AsInt(arg);
1412 if (increment == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001413 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001414 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001415 return_value = os_nice_impl(module, increment);
1416
1417exit:
1418 return return_value;
1419}
1420
1421#endif /* defined(HAVE_NICE) */
1422
1423#if defined(HAVE_GETPRIORITY)
1424
1425PyDoc_STRVAR(os_getpriority__doc__,
1426"getpriority($module, /, which, who)\n"
1427"--\n"
1428"\n"
1429"Return program scheduling priority.");
1430
1431#define OS_GETPRIORITY_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02001432 {"getpriority", (PyCFunction)(void(*)(void))os_getpriority, METH_FASTCALL|METH_KEYWORDS, os_getpriority__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001433
1434static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001435os_getpriority_impl(PyObject *module, int which, int who);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001436
1437static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001438os_getpriority(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001439{
1440 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001441 static const char * const _keywords[] = {"which", "who", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02001442 static _PyArg_Parser _parser = {NULL, _keywords, "getpriority", 0};
1443 PyObject *argsbuf[2];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001444 int which;
1445 int who;
1446
Serhiy Storchaka31913912019-03-14 10:32:22 +02001447 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf);
1448 if (!args) {
1449 goto exit;
1450 }
1451 if (PyFloat_Check(args[0])) {
1452 PyErr_SetString(PyExc_TypeError,
1453 "integer argument expected, got float" );
1454 goto exit;
1455 }
1456 which = _PyLong_AsInt(args[0]);
1457 if (which == -1 && PyErr_Occurred()) {
1458 goto exit;
1459 }
1460 if (PyFloat_Check(args[1])) {
1461 PyErr_SetString(PyExc_TypeError,
1462 "integer argument expected, got float" );
1463 goto exit;
1464 }
1465 who = _PyLong_AsInt(args[1]);
1466 if (who == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001467 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001468 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001469 return_value = os_getpriority_impl(module, which, who);
1470
1471exit:
1472 return return_value;
1473}
1474
1475#endif /* defined(HAVE_GETPRIORITY) */
1476
1477#if defined(HAVE_SETPRIORITY)
1478
1479PyDoc_STRVAR(os_setpriority__doc__,
1480"setpriority($module, /, which, who, priority)\n"
1481"--\n"
1482"\n"
1483"Set program scheduling priority.");
1484
1485#define OS_SETPRIORITY_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02001486 {"setpriority", (PyCFunction)(void(*)(void))os_setpriority, METH_FASTCALL|METH_KEYWORDS, os_setpriority__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001487
1488static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001489os_setpriority_impl(PyObject *module, int which, int who, int priority);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001490
1491static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001492os_setpriority(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001493{
1494 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001495 static const char * const _keywords[] = {"which", "who", "priority", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02001496 static _PyArg_Parser _parser = {NULL, _keywords, "setpriority", 0};
1497 PyObject *argsbuf[3];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001498 int which;
1499 int who;
1500 int priority;
1501
Serhiy Storchaka31913912019-03-14 10:32:22 +02001502 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 3, 3, 0, argsbuf);
1503 if (!args) {
1504 goto exit;
1505 }
1506 if (PyFloat_Check(args[0])) {
1507 PyErr_SetString(PyExc_TypeError,
1508 "integer argument expected, got float" );
1509 goto exit;
1510 }
1511 which = _PyLong_AsInt(args[0]);
1512 if (which == -1 && PyErr_Occurred()) {
1513 goto exit;
1514 }
1515 if (PyFloat_Check(args[1])) {
1516 PyErr_SetString(PyExc_TypeError,
1517 "integer argument expected, got float" );
1518 goto exit;
1519 }
1520 who = _PyLong_AsInt(args[1]);
1521 if (who == -1 && PyErr_Occurred()) {
1522 goto exit;
1523 }
1524 if (PyFloat_Check(args[2])) {
1525 PyErr_SetString(PyExc_TypeError,
1526 "integer argument expected, got float" );
1527 goto exit;
1528 }
1529 priority = _PyLong_AsInt(args[2]);
1530 if (priority == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001531 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001532 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001533 return_value = os_setpriority_impl(module, which, who, priority);
1534
1535exit:
1536 return return_value;
1537}
1538
1539#endif /* defined(HAVE_SETPRIORITY) */
1540
1541PyDoc_STRVAR(os_rename__doc__,
1542"rename($module, /, src, dst, *, src_dir_fd=None, dst_dir_fd=None)\n"
1543"--\n"
1544"\n"
1545"Rename a file or directory.\n"
1546"\n"
1547"If either src_dir_fd or dst_dir_fd is not None, it should be a file\n"
1548" descriptor open to a directory, and the respective path string (src or dst)\n"
1549" should be relative; the path will then be relative to that directory.\n"
1550"src_dir_fd and dst_dir_fd, may not be implemented on your platform.\n"
1551" If they are unavailable, using them will raise a NotImplementedError.");
1552
1553#define OS_RENAME_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02001554 {"rename", (PyCFunction)(void(*)(void))os_rename, METH_FASTCALL|METH_KEYWORDS, os_rename__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001555
1556static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001557os_rename_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04001558 int dst_dir_fd);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001559
1560static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001561os_rename(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001562{
1563 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001564 static const char * const _keywords[] = {"src", "dst", "src_dir_fd", "dst_dir_fd", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02001565 static _PyArg_Parser _parser = {NULL, _keywords, "rename", 0};
1566 PyObject *argsbuf[4];
1567 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001568 path_t src = PATH_T_INITIALIZE("rename", "src", 0, 0);
1569 path_t dst = PATH_T_INITIALIZE("rename", "dst", 0, 0);
1570 int src_dir_fd = DEFAULT_DIR_FD;
1571 int dst_dir_fd = DEFAULT_DIR_FD;
1572
Serhiy Storchaka31913912019-03-14 10:32:22 +02001573 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf);
1574 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001575 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001576 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02001577 if (!path_converter(args[0], &src)) {
1578 goto exit;
1579 }
1580 if (!path_converter(args[1], &dst)) {
1581 goto exit;
1582 }
1583 if (!noptargs) {
1584 goto skip_optional_kwonly;
1585 }
1586 if (args[2]) {
1587 if (!dir_fd_converter(args[2], &src_dir_fd)) {
1588 goto exit;
1589 }
1590 if (!--noptargs) {
1591 goto skip_optional_kwonly;
1592 }
1593 }
1594 if (!dir_fd_converter(args[3], &dst_dir_fd)) {
1595 goto exit;
1596 }
1597skip_optional_kwonly:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001598 return_value = os_rename_impl(module, &src, &dst, src_dir_fd, dst_dir_fd);
1599
1600exit:
1601 /* Cleanup for src */
1602 path_cleanup(&src);
1603 /* Cleanup for dst */
1604 path_cleanup(&dst);
1605
1606 return return_value;
1607}
1608
1609PyDoc_STRVAR(os_replace__doc__,
1610"replace($module, /, src, dst, *, src_dir_fd=None, dst_dir_fd=None)\n"
1611"--\n"
1612"\n"
1613"Rename a file or directory, overwriting the destination.\n"
1614"\n"
1615"If either src_dir_fd or dst_dir_fd is not None, it should be a file\n"
1616" descriptor open to a directory, and the respective path string (src or dst)\n"
1617" should be relative; the path will then be relative to that directory.\n"
1618"src_dir_fd and dst_dir_fd, may not be implemented on your platform.\n"
Anthony Sottile73d60022019-02-12 23:15:54 -05001619" If they are unavailable, using them will raise a NotImplementedError.");
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001620
1621#define OS_REPLACE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02001622 {"replace", (PyCFunction)(void(*)(void))os_replace, METH_FASTCALL|METH_KEYWORDS, os_replace__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001623
1624static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001625os_replace_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
1626 int dst_dir_fd);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001627
1628static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001629os_replace(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001630{
1631 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001632 static const char * const _keywords[] = {"src", "dst", "src_dir_fd", "dst_dir_fd", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02001633 static _PyArg_Parser _parser = {NULL, _keywords, "replace", 0};
1634 PyObject *argsbuf[4];
1635 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001636 path_t src = PATH_T_INITIALIZE("replace", "src", 0, 0);
1637 path_t dst = PATH_T_INITIALIZE("replace", "dst", 0, 0);
1638 int src_dir_fd = DEFAULT_DIR_FD;
1639 int dst_dir_fd = DEFAULT_DIR_FD;
1640
Serhiy Storchaka31913912019-03-14 10:32:22 +02001641 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf);
1642 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001643 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001644 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02001645 if (!path_converter(args[0], &src)) {
1646 goto exit;
1647 }
1648 if (!path_converter(args[1], &dst)) {
1649 goto exit;
1650 }
1651 if (!noptargs) {
1652 goto skip_optional_kwonly;
1653 }
1654 if (args[2]) {
1655 if (!dir_fd_converter(args[2], &src_dir_fd)) {
1656 goto exit;
1657 }
1658 if (!--noptargs) {
1659 goto skip_optional_kwonly;
1660 }
1661 }
1662 if (!dir_fd_converter(args[3], &dst_dir_fd)) {
1663 goto exit;
1664 }
1665skip_optional_kwonly:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001666 return_value = os_replace_impl(module, &src, &dst, src_dir_fd, dst_dir_fd);
1667
1668exit:
1669 /* Cleanup for src */
1670 path_cleanup(&src);
1671 /* Cleanup for dst */
1672 path_cleanup(&dst);
1673
1674 return return_value;
1675}
1676
1677PyDoc_STRVAR(os_rmdir__doc__,
1678"rmdir($module, /, path, *, dir_fd=None)\n"
1679"--\n"
1680"\n"
1681"Remove a directory.\n"
1682"\n"
1683"If dir_fd is not None, it should be a file descriptor open to a directory,\n"
1684" and path should be relative; path will then be relative to that directory.\n"
1685"dir_fd may not be implemented on your platform.\n"
1686" If it is unavailable, using it will raise a NotImplementedError.");
1687
1688#define OS_RMDIR_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02001689 {"rmdir", (PyCFunction)(void(*)(void))os_rmdir, METH_FASTCALL|METH_KEYWORDS, os_rmdir__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001690
1691static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001692os_rmdir_impl(PyObject *module, path_t *path, int dir_fd);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001693
1694static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001695os_rmdir(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001696{
1697 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001698 static const char * const _keywords[] = {"path", "dir_fd", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02001699 static _PyArg_Parser _parser = {NULL, _keywords, "rmdir", 0};
1700 PyObject *argsbuf[2];
1701 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001702 path_t path = PATH_T_INITIALIZE("rmdir", "path", 0, 0);
1703 int dir_fd = DEFAULT_DIR_FD;
1704
Serhiy Storchaka31913912019-03-14 10:32:22 +02001705 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
1706 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001707 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001708 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02001709 if (!path_converter(args[0], &path)) {
1710 goto exit;
1711 }
1712 if (!noptargs) {
1713 goto skip_optional_kwonly;
1714 }
1715 if (!UNLINKAT_DIR_FD_CONVERTER(args[1], &dir_fd)) {
1716 goto exit;
1717 }
1718skip_optional_kwonly:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001719 return_value = os_rmdir_impl(module, &path, dir_fd);
1720
1721exit:
1722 /* Cleanup for path */
1723 path_cleanup(&path);
1724
1725 return return_value;
1726}
1727
1728#if defined(HAVE_SYSTEM) && defined(MS_WINDOWS)
1729
1730PyDoc_STRVAR(os_system__doc__,
1731"system($module, /, command)\n"
1732"--\n"
1733"\n"
1734"Execute the command in a subshell.");
1735
1736#define OS_SYSTEM_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02001737 {"system", (PyCFunction)(void(*)(void))os_system, METH_FASTCALL|METH_KEYWORDS, os_system__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001738
1739static long
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001740os_system_impl(PyObject *module, const Py_UNICODE *command);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001741
1742static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001743os_system(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001744{
1745 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001746 static const char * const _keywords[] = {"command", NULL};
1747 static _PyArg_Parser _parser = {"u:system", _keywords, 0};
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001748 const Py_UNICODE *command;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001749 long _return_value;
1750
Victor Stinner3e1fad62017-01-17 01:29:01 +01001751 if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001752 &command)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001753 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001754 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001755 _return_value = os_system_impl(module, command);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001756 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001757 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001758 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001759 return_value = PyLong_FromLong(_return_value);
1760
1761exit:
1762 return return_value;
1763}
1764
1765#endif /* defined(HAVE_SYSTEM) && defined(MS_WINDOWS) */
1766
1767#if defined(HAVE_SYSTEM) && !defined(MS_WINDOWS)
1768
1769PyDoc_STRVAR(os_system__doc__,
1770"system($module, /, command)\n"
1771"--\n"
1772"\n"
1773"Execute the command in a subshell.");
1774
1775#define OS_SYSTEM_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02001776 {"system", (PyCFunction)(void(*)(void))os_system, METH_FASTCALL|METH_KEYWORDS, os_system__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001777
1778static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001779os_system_impl(PyObject *module, PyObject *command);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001780
1781static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001782os_system(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001783{
1784 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001785 static const char * const _keywords[] = {"command", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02001786 static _PyArg_Parser _parser = {NULL, _keywords, "system", 0};
1787 PyObject *argsbuf[1];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001788 PyObject *command = NULL;
1789 long _return_value;
1790
Serhiy Storchaka31913912019-03-14 10:32:22 +02001791 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
1792 if (!args) {
1793 goto exit;
1794 }
1795 if (!PyUnicode_FSConverter(args[0], &command)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001796 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001797 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001798 _return_value = os_system_impl(module, command);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001799 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001800 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001801 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001802 return_value = PyLong_FromLong(_return_value);
1803
1804exit:
1805 /* Cleanup for command */
1806 Py_XDECREF(command);
1807
1808 return return_value;
1809}
1810
1811#endif /* defined(HAVE_SYSTEM) && !defined(MS_WINDOWS) */
1812
1813PyDoc_STRVAR(os_umask__doc__,
1814"umask($module, mask, /)\n"
1815"--\n"
1816"\n"
1817"Set the current numeric umask and return the previous umask.");
1818
1819#define OS_UMASK_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03001820 {"umask", (PyCFunction)os_umask, METH_O, os_umask__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001821
1822static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001823os_umask_impl(PyObject *module, int mask);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001824
1825static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001826os_umask(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001827{
1828 PyObject *return_value = NULL;
1829 int mask;
1830
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02001831 if (PyFloat_Check(arg)) {
1832 PyErr_SetString(PyExc_TypeError,
1833 "integer argument expected, got float" );
1834 goto exit;
1835 }
1836 mask = _PyLong_AsInt(arg);
1837 if (mask == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001838 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001839 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001840 return_value = os_umask_impl(module, mask);
1841
1842exit:
1843 return return_value;
1844}
1845
1846PyDoc_STRVAR(os_unlink__doc__,
1847"unlink($module, /, path, *, dir_fd=None)\n"
1848"--\n"
1849"\n"
1850"Remove a file (same as remove()).\n"
1851"\n"
1852"If dir_fd is not None, it should be a file descriptor open to a directory,\n"
1853" and path should be relative; path will then be relative to that directory.\n"
1854"dir_fd may not be implemented on your platform.\n"
1855" If it is unavailable, using it will raise a NotImplementedError.");
1856
1857#define OS_UNLINK_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02001858 {"unlink", (PyCFunction)(void(*)(void))os_unlink, METH_FASTCALL|METH_KEYWORDS, os_unlink__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001859
1860static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001861os_unlink_impl(PyObject *module, path_t *path, int dir_fd);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001862
1863static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001864os_unlink(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001865{
1866 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001867 static const char * const _keywords[] = {"path", "dir_fd", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02001868 static _PyArg_Parser _parser = {NULL, _keywords, "unlink", 0};
1869 PyObject *argsbuf[2];
1870 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001871 path_t path = PATH_T_INITIALIZE("unlink", "path", 0, 0);
1872 int dir_fd = DEFAULT_DIR_FD;
1873
Serhiy Storchaka31913912019-03-14 10:32:22 +02001874 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
1875 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001876 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001877 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02001878 if (!path_converter(args[0], &path)) {
1879 goto exit;
1880 }
1881 if (!noptargs) {
1882 goto skip_optional_kwonly;
1883 }
1884 if (!UNLINKAT_DIR_FD_CONVERTER(args[1], &dir_fd)) {
1885 goto exit;
1886 }
1887skip_optional_kwonly:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001888 return_value = os_unlink_impl(module, &path, dir_fd);
1889
1890exit:
1891 /* Cleanup for path */
1892 path_cleanup(&path);
1893
1894 return return_value;
1895}
1896
1897PyDoc_STRVAR(os_remove__doc__,
1898"remove($module, /, path, *, dir_fd=None)\n"
1899"--\n"
1900"\n"
1901"Remove a file (same as unlink()).\n"
1902"\n"
1903"If dir_fd is not None, it should be a file descriptor open to a directory,\n"
1904" and path should be relative; path will then be relative to that directory.\n"
1905"dir_fd may not be implemented on your platform.\n"
1906" If it is unavailable, using it will raise a NotImplementedError.");
1907
1908#define OS_REMOVE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02001909 {"remove", (PyCFunction)(void(*)(void))os_remove, METH_FASTCALL|METH_KEYWORDS, os_remove__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001910
1911static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001912os_remove_impl(PyObject *module, path_t *path, int dir_fd);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001913
1914static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001915os_remove(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001916{
1917 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001918 static const char * const _keywords[] = {"path", "dir_fd", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02001919 static _PyArg_Parser _parser = {NULL, _keywords, "remove", 0};
1920 PyObject *argsbuf[2];
1921 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001922 path_t path = PATH_T_INITIALIZE("remove", "path", 0, 0);
1923 int dir_fd = DEFAULT_DIR_FD;
1924
Serhiy Storchaka31913912019-03-14 10:32:22 +02001925 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
1926 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001927 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001928 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02001929 if (!path_converter(args[0], &path)) {
1930 goto exit;
1931 }
1932 if (!noptargs) {
1933 goto skip_optional_kwonly;
1934 }
1935 if (!UNLINKAT_DIR_FD_CONVERTER(args[1], &dir_fd)) {
1936 goto exit;
1937 }
1938skip_optional_kwonly:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001939 return_value = os_remove_impl(module, &path, dir_fd);
1940
1941exit:
1942 /* Cleanup for path */
1943 path_cleanup(&path);
1944
1945 return return_value;
1946}
1947
1948#if defined(HAVE_UNAME)
1949
1950PyDoc_STRVAR(os_uname__doc__,
1951"uname($module, /)\n"
1952"--\n"
1953"\n"
1954"Return an object identifying the current operating system.\n"
1955"\n"
1956"The object behaves like a named tuple with the following fields:\n"
1957" (sysname, nodename, release, version, machine)");
1958
1959#define OS_UNAME_METHODDEF \
1960 {"uname", (PyCFunction)os_uname, METH_NOARGS, os_uname__doc__},
1961
1962static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001963os_uname_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001964
1965static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001966os_uname(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001967{
1968 return os_uname_impl(module);
1969}
1970
1971#endif /* defined(HAVE_UNAME) */
1972
1973PyDoc_STRVAR(os_utime__doc__,
Serhiy Storchaka279f4462019-09-14 12:24:05 +03001974"utime($module, /, path, times=None, *, ns=<unrepresentable>,\n"
1975" dir_fd=None, follow_symlinks=True)\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001976"--\n"
1977"\n"
1978"Set the access and modified time of path.\n"
1979"\n"
1980"path may always be specified as a string.\n"
1981"On some platforms, path may also be specified as an open file descriptor.\n"
1982" If this functionality is unavailable, using it raises an exception.\n"
1983"\n"
1984"If times is not None, it must be a tuple (atime, mtime);\n"
1985" atime and mtime should be expressed as float seconds since the epoch.\n"
Martin Panter0ff89092015-09-09 01:56:53 +00001986"If ns is specified, it must be a tuple (atime_ns, mtime_ns);\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001987" atime_ns and mtime_ns should be expressed as integer nanoseconds\n"
1988" since the epoch.\n"
Martin Panter0ff89092015-09-09 01:56:53 +00001989"If times is None and ns is unspecified, utime uses the current time.\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001990"Specifying tuples for both times and ns is an error.\n"
1991"\n"
1992"If dir_fd is not None, it should be a file descriptor open to a directory,\n"
1993" and path should be relative; path will then be relative to that directory.\n"
1994"If follow_symlinks is False, and the last element of the path is a symbolic\n"
1995" link, utime will modify the symbolic link itself instead of the file the\n"
1996" link points to.\n"
1997"It is an error to use dir_fd or follow_symlinks when specifying path\n"
1998" as an open file descriptor.\n"
1999"dir_fd and follow_symlinks may not be available on your platform.\n"
2000" If they are unavailable, using them will raise a NotImplementedError.");
2001
2002#define OS_UTIME_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02002003 {"utime", (PyCFunction)(void(*)(void))os_utime, METH_FASTCALL|METH_KEYWORDS, os_utime__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002004
2005static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002006os_utime_impl(PyObject *module, path_t *path, PyObject *times, PyObject *ns,
2007 int dir_fd, int follow_symlinks);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002008
2009static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02002010os_utime(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002011{
2012 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002013 static const char * const _keywords[] = {"path", "times", "ns", "dir_fd", "follow_symlinks", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02002014 static _PyArg_Parser _parser = {NULL, _keywords, "utime", 0};
2015 PyObject *argsbuf[5];
2016 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002017 path_t path = PATH_T_INITIALIZE("utime", "path", 0, PATH_UTIME_HAVE_FD);
Serhiy Storchaka279f4462019-09-14 12:24:05 +03002018 PyObject *times = Py_None;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002019 PyObject *ns = NULL;
2020 int dir_fd = DEFAULT_DIR_FD;
2021 int follow_symlinks = 1;
2022
Serhiy Storchaka31913912019-03-14 10:32:22 +02002023 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
2024 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002025 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03002026 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02002027 if (!path_converter(args[0], &path)) {
2028 goto exit;
2029 }
2030 if (!noptargs) {
2031 goto skip_optional_pos;
2032 }
2033 if (args[1]) {
2034 times = args[1];
2035 if (!--noptargs) {
2036 goto skip_optional_pos;
2037 }
2038 }
2039skip_optional_pos:
2040 if (!noptargs) {
2041 goto skip_optional_kwonly;
2042 }
2043 if (args[2]) {
2044 ns = args[2];
2045 if (!--noptargs) {
2046 goto skip_optional_kwonly;
2047 }
2048 }
2049 if (args[3]) {
2050 if (!FUTIMENSAT_DIR_FD_CONVERTER(args[3], &dir_fd)) {
2051 goto exit;
2052 }
2053 if (!--noptargs) {
2054 goto skip_optional_kwonly;
2055 }
2056 }
2057 follow_symlinks = PyObject_IsTrue(args[4]);
2058 if (follow_symlinks < 0) {
2059 goto exit;
2060 }
2061skip_optional_kwonly:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002062 return_value = os_utime_impl(module, &path, times, ns, dir_fd, follow_symlinks);
2063
2064exit:
2065 /* Cleanup for path */
2066 path_cleanup(&path);
2067
2068 return return_value;
2069}
2070
2071PyDoc_STRVAR(os__exit__doc__,
2072"_exit($module, /, status)\n"
2073"--\n"
2074"\n"
2075"Exit to the system with specified status, without normal exit processing.");
2076
2077#define OS__EXIT_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02002078 {"_exit", (PyCFunction)(void(*)(void))os__exit, METH_FASTCALL|METH_KEYWORDS, os__exit__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002079
2080static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002081os__exit_impl(PyObject *module, int status);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002082
2083static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02002084os__exit(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002085{
2086 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002087 static const char * const _keywords[] = {"status", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02002088 static _PyArg_Parser _parser = {NULL, _keywords, "_exit", 0};
2089 PyObject *argsbuf[1];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002090 int status;
2091
Serhiy Storchaka31913912019-03-14 10:32:22 +02002092 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
2093 if (!args) {
2094 goto exit;
2095 }
2096 if (PyFloat_Check(args[0])) {
2097 PyErr_SetString(PyExc_TypeError,
2098 "integer argument expected, got float" );
2099 goto exit;
2100 }
2101 status = _PyLong_AsInt(args[0]);
2102 if (status == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002103 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03002104 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002105 return_value = os__exit_impl(module, status);
2106
2107exit:
2108 return return_value;
2109}
2110
2111#if defined(HAVE_EXECV)
2112
2113PyDoc_STRVAR(os_execv__doc__,
2114"execv($module, path, argv, /)\n"
2115"--\n"
2116"\n"
2117"Execute an executable path with arguments, replacing current process.\n"
2118"\n"
2119" path\n"
2120" Path of executable file.\n"
2121" argv\n"
2122" Tuple or list of strings.");
2123
2124#define OS_EXECV_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02002125 {"execv", (PyCFunction)(void(*)(void))os_execv, METH_FASTCALL, os_execv__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002126
2127static PyObject *
Steve Dowercc16be82016-09-08 10:35:16 -07002128os_execv_impl(PyObject *module, path_t *path, PyObject *argv);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002129
2130static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02002131os_execv(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002132{
2133 PyObject *return_value = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07002134 path_t path = PATH_T_INITIALIZE("execv", "path", 0, 0);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002135 PyObject *argv;
2136
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002137 if (!_PyArg_CheckPositional("execv", nargs, 2, 2)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01002138 goto exit;
2139 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002140 if (!path_converter(args[0], &path)) {
2141 goto exit;
2142 }
2143 argv = args[1];
Steve Dowercc16be82016-09-08 10:35:16 -07002144 return_value = os_execv_impl(module, &path, argv);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002145
2146exit:
2147 /* Cleanup for path */
Steve Dowercc16be82016-09-08 10:35:16 -07002148 path_cleanup(&path);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002149
2150 return return_value;
2151}
2152
2153#endif /* defined(HAVE_EXECV) */
2154
2155#if defined(HAVE_EXECV)
2156
2157PyDoc_STRVAR(os_execve__doc__,
2158"execve($module, /, path, argv, env)\n"
2159"--\n"
2160"\n"
2161"Execute an executable path with arguments, replacing current process.\n"
2162"\n"
2163" path\n"
2164" Path of executable file.\n"
2165" argv\n"
2166" Tuple or list of strings.\n"
2167" env\n"
2168" Dictionary of strings mapping to strings.");
2169
2170#define OS_EXECVE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02002171 {"execve", (PyCFunction)(void(*)(void))os_execve, METH_FASTCALL|METH_KEYWORDS, os_execve__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002172
2173static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002174os_execve_impl(PyObject *module, path_t *path, PyObject *argv, PyObject *env);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002175
2176static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02002177os_execve(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002178{
2179 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002180 static const char * const _keywords[] = {"path", "argv", "env", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02002181 static _PyArg_Parser _parser = {NULL, _keywords, "execve", 0};
2182 PyObject *argsbuf[3];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002183 path_t path = PATH_T_INITIALIZE("execve", "path", 0, PATH_HAVE_FEXECVE);
2184 PyObject *argv;
2185 PyObject *env;
2186
Serhiy Storchaka31913912019-03-14 10:32:22 +02002187 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 3, 3, 0, argsbuf);
2188 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002189 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03002190 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02002191 if (!path_converter(args[0], &path)) {
2192 goto exit;
2193 }
2194 argv = args[1];
2195 env = args[2];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002196 return_value = os_execve_impl(module, &path, argv, env);
2197
2198exit:
2199 /* Cleanup for path */
2200 path_cleanup(&path);
2201
2202 return return_value;
2203}
2204
2205#endif /* defined(HAVE_EXECV) */
2206
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00002207#if defined(HAVE_POSIX_SPAWN)
2208
2209PyDoc_STRVAR(os_posix_spawn__doc__,
Serhiy Storchakad700f972018-09-08 14:48:18 +03002210"posix_spawn($module, path, argv, env, /, *, file_actions=(),\n"
Serhiy Storchaka279f4462019-09-14 12:24:05 +03002211" setpgroup=<unrepresentable>, resetids=False, setsid=False,\n"
2212" setsigmask=(), setsigdef=(), scheduler=<unrepresentable>)\n"
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00002213"--\n"
2214"\n"
2215"Execute the program specified by path in a new process.\n"
2216"\n"
2217" path\n"
2218" Path of executable file.\n"
2219" argv\n"
2220" Tuple or list of strings.\n"
2221" env\n"
2222" Dictionary of strings mapping to strings.\n"
2223" file_actions\n"
Pablo Galindo254a4662018-09-07 16:44:24 +01002224" A sequence of file action tuples.\n"
2225" setpgroup\n"
2226" The pgroup to use with the POSIX_SPAWN_SETPGROUP flag.\n"
2227" resetids\n"
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03002228" If the value is `true` the POSIX_SPAWN_RESETIDS will be activated.\n"
2229" setsid\n"
2230" If the value is `true` the POSIX_SPAWN_SETSID or POSIX_SPAWN_SETSID_NP will be activated.\n"
Pablo Galindo254a4662018-09-07 16:44:24 +01002231" setsigmask\n"
2232" The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag.\n"
2233" setsigdef\n"
2234" The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag.\n"
2235" scheduler\n"
2236" A tuple with the scheduler policy (optional) and parameters.");
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00002237
2238#define OS_POSIX_SPAWN_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02002239 {"posix_spawn", (PyCFunction)(void(*)(void))os_posix_spawn, METH_FASTCALL|METH_KEYWORDS, os_posix_spawn__doc__},
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00002240
2241static PyObject *
2242os_posix_spawn_impl(PyObject *module, path_t *path, PyObject *argv,
Pablo Galindo254a4662018-09-07 16:44:24 +01002243 PyObject *env, PyObject *file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03002244 PyObject *setpgroup, int resetids, int setsid,
2245 PyObject *setsigmask, PyObject *setsigdef,
2246 PyObject *scheduler);
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00002247
2248static PyObject *
Pablo Galindo254a4662018-09-07 16:44:24 +01002249os_posix_spawn(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00002250{
2251 PyObject *return_value = NULL;
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03002252 static const char * const _keywords[] = {"", "", "", "file_actions", "setpgroup", "resetids", "setsid", "setsigmask", "setsigdef", "scheduler", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02002253 static _PyArg_Parser _parser = {NULL, _keywords, "posix_spawn", 0};
2254 PyObject *argsbuf[10];
2255 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 3;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00002256 path_t path = PATH_T_INITIALIZE("posix_spawn", "path", 0, 0);
2257 PyObject *argv;
2258 PyObject *env;
Serhiy Storchakad700f972018-09-08 14:48:18 +03002259 PyObject *file_actions = NULL;
Pablo Galindo254a4662018-09-07 16:44:24 +01002260 PyObject *setpgroup = NULL;
2261 int resetids = 0;
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03002262 int setsid = 0;
Pablo Galindo254a4662018-09-07 16:44:24 +01002263 PyObject *setsigmask = NULL;
2264 PyObject *setsigdef = NULL;
2265 PyObject *scheduler = NULL;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00002266
Serhiy Storchaka31913912019-03-14 10:32:22 +02002267 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 3, 3, 0, argsbuf);
2268 if (!args) {
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00002269 goto exit;
2270 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02002271 if (!path_converter(args[0], &path)) {
2272 goto exit;
2273 }
2274 argv = args[1];
2275 env = args[2];
2276 if (!noptargs) {
2277 goto skip_optional_kwonly;
2278 }
2279 if (args[3]) {
2280 file_actions = args[3];
2281 if (!--noptargs) {
2282 goto skip_optional_kwonly;
2283 }
2284 }
2285 if (args[4]) {
2286 setpgroup = args[4];
2287 if (!--noptargs) {
2288 goto skip_optional_kwonly;
2289 }
2290 }
2291 if (args[5]) {
2292 if (PyFloat_Check(args[5])) {
2293 PyErr_SetString(PyExc_TypeError,
2294 "integer argument expected, got float" );
2295 goto exit;
2296 }
2297 resetids = _PyLong_AsInt(args[5]);
2298 if (resetids == -1 && PyErr_Occurred()) {
2299 goto exit;
2300 }
2301 if (!--noptargs) {
2302 goto skip_optional_kwonly;
2303 }
2304 }
2305 if (args[6]) {
2306 if (PyFloat_Check(args[6])) {
2307 PyErr_SetString(PyExc_TypeError,
2308 "integer argument expected, got float" );
2309 goto exit;
2310 }
2311 setsid = _PyLong_AsInt(args[6]);
2312 if (setsid == -1 && PyErr_Occurred()) {
2313 goto exit;
2314 }
2315 if (!--noptargs) {
2316 goto skip_optional_kwonly;
2317 }
2318 }
2319 if (args[7]) {
2320 setsigmask = args[7];
2321 if (!--noptargs) {
2322 goto skip_optional_kwonly;
2323 }
2324 }
2325 if (args[8]) {
2326 setsigdef = args[8];
2327 if (!--noptargs) {
2328 goto skip_optional_kwonly;
2329 }
2330 }
2331 scheduler = args[9];
2332skip_optional_kwonly:
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03002333 return_value = os_posix_spawn_impl(module, &path, argv, env, file_actions, setpgroup, resetids, setsid, setsigmask, setsigdef, scheduler);
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00002334
2335exit:
2336 /* Cleanup for path */
2337 path_cleanup(&path);
2338
2339 return return_value;
2340}
2341
2342#endif /* defined(HAVE_POSIX_SPAWN) */
2343
Joannah Nanjekye92b83222019-01-16 16:29:26 +03002344#if defined(HAVE_POSIX_SPAWNP)
2345
2346PyDoc_STRVAR(os_posix_spawnp__doc__,
2347"posix_spawnp($module, path, argv, env, /, *, file_actions=(),\n"
Serhiy Storchaka279f4462019-09-14 12:24:05 +03002348" setpgroup=<unrepresentable>, resetids=False, setsid=False,\n"
2349" setsigmask=(), setsigdef=(), scheduler=<unrepresentable>)\n"
Joannah Nanjekye92b83222019-01-16 16:29:26 +03002350"--\n"
2351"\n"
2352"Execute the program specified by path in a new process.\n"
2353"\n"
2354" path\n"
2355" Path of executable file.\n"
2356" argv\n"
2357" Tuple or list of strings.\n"
2358" env\n"
2359" Dictionary of strings mapping to strings.\n"
2360" file_actions\n"
2361" A sequence of file action tuples.\n"
2362" setpgroup\n"
2363" The pgroup to use with the POSIX_SPAWN_SETPGROUP flag.\n"
2364" resetids\n"
2365" If the value is `True` the POSIX_SPAWN_RESETIDS will be activated.\n"
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03002366" setsid\n"
2367" If the value is `True` the POSIX_SPAWN_SETSID or POSIX_SPAWN_SETSID_NP will be activated.\n"
Joannah Nanjekye92b83222019-01-16 16:29:26 +03002368" setsigmask\n"
2369" The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag.\n"
2370" setsigdef\n"
2371" The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag.\n"
2372" scheduler\n"
2373" A tuple with the scheduler policy (optional) and parameters.");
2374
2375#define OS_POSIX_SPAWNP_METHODDEF \
2376 {"posix_spawnp", (PyCFunction)(void(*)(void))os_posix_spawnp, METH_FASTCALL|METH_KEYWORDS, os_posix_spawnp__doc__},
2377
2378static PyObject *
2379os_posix_spawnp_impl(PyObject *module, path_t *path, PyObject *argv,
2380 PyObject *env, PyObject *file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03002381 PyObject *setpgroup, int resetids, int setsid,
2382 PyObject *setsigmask, PyObject *setsigdef,
2383 PyObject *scheduler);
Joannah Nanjekye92b83222019-01-16 16:29:26 +03002384
2385static PyObject *
2386os_posix_spawnp(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
2387{
2388 PyObject *return_value = NULL;
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03002389 static const char * const _keywords[] = {"", "", "", "file_actions", "setpgroup", "resetids", "setsid", "setsigmask", "setsigdef", "scheduler", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02002390 static _PyArg_Parser _parser = {NULL, _keywords, "posix_spawnp", 0};
2391 PyObject *argsbuf[10];
2392 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 3;
Joannah Nanjekye92b83222019-01-16 16:29:26 +03002393 path_t path = PATH_T_INITIALIZE("posix_spawnp", "path", 0, 0);
2394 PyObject *argv;
2395 PyObject *env;
2396 PyObject *file_actions = NULL;
2397 PyObject *setpgroup = NULL;
2398 int resetids = 0;
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03002399 int setsid = 0;
Joannah Nanjekye92b83222019-01-16 16:29:26 +03002400 PyObject *setsigmask = NULL;
2401 PyObject *setsigdef = NULL;
2402 PyObject *scheduler = NULL;
2403
Serhiy Storchaka31913912019-03-14 10:32:22 +02002404 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 3, 3, 0, argsbuf);
2405 if (!args) {
Joannah Nanjekye92b83222019-01-16 16:29:26 +03002406 goto exit;
2407 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02002408 if (!path_converter(args[0], &path)) {
2409 goto exit;
2410 }
2411 argv = args[1];
2412 env = args[2];
2413 if (!noptargs) {
2414 goto skip_optional_kwonly;
2415 }
2416 if (args[3]) {
2417 file_actions = args[3];
2418 if (!--noptargs) {
2419 goto skip_optional_kwonly;
2420 }
2421 }
2422 if (args[4]) {
2423 setpgroup = args[4];
2424 if (!--noptargs) {
2425 goto skip_optional_kwonly;
2426 }
2427 }
2428 if (args[5]) {
2429 if (PyFloat_Check(args[5])) {
2430 PyErr_SetString(PyExc_TypeError,
2431 "integer argument expected, got float" );
2432 goto exit;
2433 }
2434 resetids = _PyLong_AsInt(args[5]);
2435 if (resetids == -1 && PyErr_Occurred()) {
2436 goto exit;
2437 }
2438 if (!--noptargs) {
2439 goto skip_optional_kwonly;
2440 }
2441 }
2442 if (args[6]) {
2443 if (PyFloat_Check(args[6])) {
2444 PyErr_SetString(PyExc_TypeError,
2445 "integer argument expected, got float" );
2446 goto exit;
2447 }
2448 setsid = _PyLong_AsInt(args[6]);
2449 if (setsid == -1 && PyErr_Occurred()) {
2450 goto exit;
2451 }
2452 if (!--noptargs) {
2453 goto skip_optional_kwonly;
2454 }
2455 }
2456 if (args[7]) {
2457 setsigmask = args[7];
2458 if (!--noptargs) {
2459 goto skip_optional_kwonly;
2460 }
2461 }
2462 if (args[8]) {
2463 setsigdef = args[8];
2464 if (!--noptargs) {
2465 goto skip_optional_kwonly;
2466 }
2467 }
2468 scheduler = args[9];
2469skip_optional_kwonly:
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03002470 return_value = os_posix_spawnp_impl(module, &path, argv, env, file_actions, setpgroup, resetids, setsid, setsigmask, setsigdef, scheduler);
Joannah Nanjekye92b83222019-01-16 16:29:26 +03002471
2472exit:
2473 /* Cleanup for path */
2474 path_cleanup(&path);
2475
2476 return return_value;
2477}
2478
2479#endif /* defined(HAVE_POSIX_SPAWNP) */
2480
pxinwrf2d7ac72019-05-21 18:46:37 +08002481#if (defined(HAVE_SPAWNV) || defined(HAVE_WSPAWNV) || defined(HAVE_RTPSPAWN))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002482
2483PyDoc_STRVAR(os_spawnv__doc__,
2484"spawnv($module, mode, path, argv, /)\n"
2485"--\n"
2486"\n"
2487"Execute the program specified by path in a new process.\n"
2488"\n"
2489" mode\n"
2490" Mode of process creation.\n"
2491" path\n"
2492" Path of executable file.\n"
2493" argv\n"
2494" Tuple or list of strings.");
2495
2496#define OS_SPAWNV_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02002497 {"spawnv", (PyCFunction)(void(*)(void))os_spawnv, METH_FASTCALL, os_spawnv__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002498
2499static PyObject *
Steve Dowercc16be82016-09-08 10:35:16 -07002500os_spawnv_impl(PyObject *module, int mode, path_t *path, PyObject *argv);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002501
2502static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02002503os_spawnv(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002504{
2505 PyObject *return_value = NULL;
2506 int mode;
Steve Dowercc16be82016-09-08 10:35:16 -07002507 path_t path = PATH_T_INITIALIZE("spawnv", "path", 0, 0);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002508 PyObject *argv;
2509
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002510 if (!_PyArg_CheckPositional("spawnv", nargs, 3, 3)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01002511 goto exit;
2512 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002513 if (PyFloat_Check(args[0])) {
2514 PyErr_SetString(PyExc_TypeError,
2515 "integer argument expected, got float" );
2516 goto exit;
2517 }
2518 mode = _PyLong_AsInt(args[0]);
2519 if (mode == -1 && PyErr_Occurred()) {
2520 goto exit;
2521 }
2522 if (!path_converter(args[1], &path)) {
2523 goto exit;
2524 }
2525 argv = args[2];
Steve Dowercc16be82016-09-08 10:35:16 -07002526 return_value = os_spawnv_impl(module, mode, &path, argv);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002527
2528exit:
2529 /* Cleanup for path */
Steve Dowercc16be82016-09-08 10:35:16 -07002530 path_cleanup(&path);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002531
2532 return return_value;
2533}
2534
pxinwrf2d7ac72019-05-21 18:46:37 +08002535#endif /* (defined(HAVE_SPAWNV) || defined(HAVE_WSPAWNV) || defined(HAVE_RTPSPAWN)) */
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002536
pxinwrf2d7ac72019-05-21 18:46:37 +08002537#if (defined(HAVE_SPAWNV) || defined(HAVE_WSPAWNV) || defined(HAVE_RTPSPAWN))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002538
2539PyDoc_STRVAR(os_spawnve__doc__,
2540"spawnve($module, mode, path, argv, env, /)\n"
2541"--\n"
2542"\n"
2543"Execute the program specified by path in a new process.\n"
2544"\n"
2545" mode\n"
2546" Mode of process creation.\n"
2547" path\n"
2548" Path of executable file.\n"
2549" argv\n"
2550" Tuple or list of strings.\n"
2551" env\n"
2552" Dictionary of strings mapping to strings.");
2553
2554#define OS_SPAWNVE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02002555 {"spawnve", (PyCFunction)(void(*)(void))os_spawnve, METH_FASTCALL, os_spawnve__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002556
2557static PyObject *
Steve Dowercc16be82016-09-08 10:35:16 -07002558os_spawnve_impl(PyObject *module, int mode, path_t *path, PyObject *argv,
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002559 PyObject *env);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002560
2561static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02002562os_spawnve(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002563{
2564 PyObject *return_value = NULL;
2565 int mode;
Steve Dowercc16be82016-09-08 10:35:16 -07002566 path_t path = PATH_T_INITIALIZE("spawnve", "path", 0, 0);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002567 PyObject *argv;
2568 PyObject *env;
2569
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002570 if (!_PyArg_CheckPositional("spawnve", nargs, 4, 4)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01002571 goto exit;
2572 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002573 if (PyFloat_Check(args[0])) {
2574 PyErr_SetString(PyExc_TypeError,
2575 "integer argument expected, got float" );
2576 goto exit;
2577 }
2578 mode = _PyLong_AsInt(args[0]);
2579 if (mode == -1 && PyErr_Occurred()) {
2580 goto exit;
2581 }
2582 if (!path_converter(args[1], &path)) {
2583 goto exit;
2584 }
2585 argv = args[2];
2586 env = args[3];
Steve Dowercc16be82016-09-08 10:35:16 -07002587 return_value = os_spawnve_impl(module, mode, &path, argv, env);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002588
2589exit:
2590 /* Cleanup for path */
Steve Dowercc16be82016-09-08 10:35:16 -07002591 path_cleanup(&path);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002592
2593 return return_value;
2594}
2595
pxinwrf2d7ac72019-05-21 18:46:37 +08002596#endif /* (defined(HAVE_SPAWNV) || defined(HAVE_WSPAWNV) || defined(HAVE_RTPSPAWN)) */
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002597
Antoine Pitrou346cbd32017-05-27 17:50:54 +02002598#if defined(HAVE_FORK)
2599
2600PyDoc_STRVAR(os_register_at_fork__doc__,
Serhiy Storchaka279f4462019-09-14 12:24:05 +03002601"register_at_fork($module, /, *, before=<unrepresentable>,\n"
2602" after_in_child=<unrepresentable>,\n"
2603" after_in_parent=<unrepresentable>)\n"
Antoine Pitrou346cbd32017-05-27 17:50:54 +02002604"--\n"
2605"\n"
Gregory P. Smith163468a2017-05-29 10:03:41 -07002606"Register callables to be called when forking a new process.\n"
Antoine Pitrou346cbd32017-05-27 17:50:54 +02002607"\n"
Gregory P. Smith163468a2017-05-29 10:03:41 -07002608" before\n"
2609" A callable to be called in the parent before the fork() syscall.\n"
2610" after_in_child\n"
2611" A callable to be called in the child after fork().\n"
2612" after_in_parent\n"
2613" A callable to be called in the parent after fork().\n"
Antoine Pitrou346cbd32017-05-27 17:50:54 +02002614"\n"
Gregory P. Smith163468a2017-05-29 10:03:41 -07002615"\'before\' callbacks are called in reverse order.\n"
2616"\'after_in_child\' and \'after_in_parent\' callbacks are called in order.");
Antoine Pitrou346cbd32017-05-27 17:50:54 +02002617
2618#define OS_REGISTER_AT_FORK_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02002619 {"register_at_fork", (PyCFunction)(void(*)(void))os_register_at_fork, METH_FASTCALL|METH_KEYWORDS, os_register_at_fork__doc__},
Antoine Pitrou346cbd32017-05-27 17:50:54 +02002620
2621static PyObject *
Gregory P. Smith163468a2017-05-29 10:03:41 -07002622os_register_at_fork_impl(PyObject *module, PyObject *before,
2623 PyObject *after_in_child, PyObject *after_in_parent);
Antoine Pitrou346cbd32017-05-27 17:50:54 +02002624
2625static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02002626os_register_at_fork(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Antoine Pitrou346cbd32017-05-27 17:50:54 +02002627{
2628 PyObject *return_value = NULL;
Gregory P. Smith163468a2017-05-29 10:03:41 -07002629 static const char * const _keywords[] = {"before", "after_in_child", "after_in_parent", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02002630 static _PyArg_Parser _parser = {NULL, _keywords, "register_at_fork", 0};
2631 PyObject *argsbuf[3];
2632 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
Gregory P. Smith163468a2017-05-29 10:03:41 -07002633 PyObject *before = NULL;
2634 PyObject *after_in_child = NULL;
2635 PyObject *after_in_parent = NULL;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02002636
Serhiy Storchaka31913912019-03-14 10:32:22 +02002637 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 0, 0, argsbuf);
2638 if (!args) {
Antoine Pitrou346cbd32017-05-27 17:50:54 +02002639 goto exit;
2640 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02002641 if (!noptargs) {
2642 goto skip_optional_kwonly;
2643 }
2644 if (args[0]) {
2645 before = args[0];
2646 if (!--noptargs) {
2647 goto skip_optional_kwonly;
2648 }
2649 }
2650 if (args[1]) {
2651 after_in_child = args[1];
2652 if (!--noptargs) {
2653 goto skip_optional_kwonly;
2654 }
2655 }
2656 after_in_parent = args[2];
2657skip_optional_kwonly:
Gregory P. Smith163468a2017-05-29 10:03:41 -07002658 return_value = os_register_at_fork_impl(module, before, after_in_child, after_in_parent);
Antoine Pitrou346cbd32017-05-27 17:50:54 +02002659
2660exit:
2661 return return_value;
2662}
2663
2664#endif /* defined(HAVE_FORK) */
2665
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002666#if defined(HAVE_FORK1)
2667
2668PyDoc_STRVAR(os_fork1__doc__,
2669"fork1($module, /)\n"
2670"--\n"
2671"\n"
2672"Fork a child process with a single multiplexed (i.e., not bound) thread.\n"
2673"\n"
2674"Return 0 to child process and PID of child to parent process.");
2675
2676#define OS_FORK1_METHODDEF \
2677 {"fork1", (PyCFunction)os_fork1, METH_NOARGS, os_fork1__doc__},
2678
2679static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002680os_fork1_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002681
2682static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002683os_fork1(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002684{
2685 return os_fork1_impl(module);
2686}
2687
2688#endif /* defined(HAVE_FORK1) */
2689
2690#if defined(HAVE_FORK)
2691
2692PyDoc_STRVAR(os_fork__doc__,
2693"fork($module, /)\n"
2694"--\n"
2695"\n"
2696"Fork a child process.\n"
2697"\n"
2698"Return 0 to child process and PID of child to parent process.");
2699
2700#define OS_FORK_METHODDEF \
2701 {"fork", (PyCFunction)os_fork, METH_NOARGS, os_fork__doc__},
2702
2703static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002704os_fork_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002705
2706static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002707os_fork(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002708{
2709 return os_fork_impl(module);
2710}
2711
2712#endif /* defined(HAVE_FORK) */
2713
2714#if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_GET_PRIORITY_MAX)
2715
2716PyDoc_STRVAR(os_sched_get_priority_max__doc__,
2717"sched_get_priority_max($module, /, policy)\n"
2718"--\n"
2719"\n"
2720"Get the maximum scheduling priority for policy.");
2721
2722#define OS_SCHED_GET_PRIORITY_MAX_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02002723 {"sched_get_priority_max", (PyCFunction)(void(*)(void))os_sched_get_priority_max, METH_FASTCALL|METH_KEYWORDS, os_sched_get_priority_max__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002724
2725static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002726os_sched_get_priority_max_impl(PyObject *module, int policy);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002727
2728static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02002729os_sched_get_priority_max(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002730{
2731 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002732 static const char * const _keywords[] = {"policy", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02002733 static _PyArg_Parser _parser = {NULL, _keywords, "sched_get_priority_max", 0};
2734 PyObject *argsbuf[1];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002735 int policy;
2736
Serhiy Storchaka31913912019-03-14 10:32:22 +02002737 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
2738 if (!args) {
2739 goto exit;
2740 }
2741 if (PyFloat_Check(args[0])) {
2742 PyErr_SetString(PyExc_TypeError,
2743 "integer argument expected, got float" );
2744 goto exit;
2745 }
2746 policy = _PyLong_AsInt(args[0]);
2747 if (policy == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002748 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03002749 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002750 return_value = os_sched_get_priority_max_impl(module, policy);
2751
2752exit:
2753 return return_value;
2754}
2755
2756#endif /* defined(HAVE_SCHED_H) && defined(HAVE_SCHED_GET_PRIORITY_MAX) */
2757
2758#if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_GET_PRIORITY_MAX)
2759
2760PyDoc_STRVAR(os_sched_get_priority_min__doc__,
2761"sched_get_priority_min($module, /, policy)\n"
2762"--\n"
2763"\n"
2764"Get the minimum scheduling priority for policy.");
2765
2766#define OS_SCHED_GET_PRIORITY_MIN_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02002767 {"sched_get_priority_min", (PyCFunction)(void(*)(void))os_sched_get_priority_min, METH_FASTCALL|METH_KEYWORDS, os_sched_get_priority_min__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002768
2769static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002770os_sched_get_priority_min_impl(PyObject *module, int policy);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002771
2772static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02002773os_sched_get_priority_min(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002774{
2775 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002776 static const char * const _keywords[] = {"policy", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02002777 static _PyArg_Parser _parser = {NULL, _keywords, "sched_get_priority_min", 0};
2778 PyObject *argsbuf[1];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002779 int policy;
2780
Serhiy Storchaka31913912019-03-14 10:32:22 +02002781 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
2782 if (!args) {
2783 goto exit;
2784 }
2785 if (PyFloat_Check(args[0])) {
2786 PyErr_SetString(PyExc_TypeError,
2787 "integer argument expected, got float" );
2788 goto exit;
2789 }
2790 policy = _PyLong_AsInt(args[0]);
2791 if (policy == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002792 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03002793 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002794 return_value = os_sched_get_priority_min_impl(module, policy);
2795
2796exit:
2797 return return_value;
2798}
2799
2800#endif /* defined(HAVE_SCHED_H) && defined(HAVE_SCHED_GET_PRIORITY_MAX) */
2801
2802#if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_SETSCHEDULER)
2803
2804PyDoc_STRVAR(os_sched_getscheduler__doc__,
2805"sched_getscheduler($module, pid, /)\n"
2806"--\n"
2807"\n"
Min ho Kimc4cacc82019-07-31 08:16:13 +10002808"Get the scheduling policy for the process identified by pid.\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002809"\n"
2810"Passing 0 for pid returns the scheduling policy for the calling process.");
2811
2812#define OS_SCHED_GETSCHEDULER_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03002813 {"sched_getscheduler", (PyCFunction)os_sched_getscheduler, METH_O, os_sched_getscheduler__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002814
2815static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002816os_sched_getscheduler_impl(PyObject *module, pid_t pid);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002817
2818static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002819os_sched_getscheduler(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002820{
2821 PyObject *return_value = NULL;
2822 pid_t pid;
2823
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03002824 if (!PyArg_Parse(arg, "" _Py_PARSE_PID ":sched_getscheduler", &pid)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002825 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03002826 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002827 return_value = os_sched_getscheduler_impl(module, pid);
2828
2829exit:
2830 return return_value;
2831}
2832
2833#endif /* defined(HAVE_SCHED_H) && defined(HAVE_SCHED_SETSCHEDULER) */
2834
William Orr81574b82018-10-01 22:19:56 -07002835#if defined(HAVE_SCHED_H) && (defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002836
2837PyDoc_STRVAR(os_sched_param__doc__,
2838"sched_param(sched_priority)\n"
2839"--\n"
2840"\n"
Eddie Elizondob3966632019-11-05 07:16:14 -08002841"Currently has only one field: sched_priority\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002842"\n"
2843" sched_priority\n"
2844" A scheduling parameter.");
2845
2846static PyObject *
2847os_sched_param_impl(PyTypeObject *type, PyObject *sched_priority);
2848
2849static PyObject *
2850os_sched_param(PyTypeObject *type, PyObject *args, PyObject *kwargs)
2851{
2852 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002853 static const char * const _keywords[] = {"sched_priority", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02002854 static _PyArg_Parser _parser = {NULL, _keywords, "sched_param", 0};
2855 PyObject *argsbuf[1];
2856 PyObject * const *fastargs;
2857 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002858 PyObject *sched_priority;
2859
Serhiy Storchaka31913912019-03-14 10:32:22 +02002860 fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 1, 1, 0, argsbuf);
2861 if (!fastargs) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002862 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03002863 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02002864 sched_priority = fastargs[0];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002865 return_value = os_sched_param_impl(type, sched_priority);
2866
2867exit:
2868 return return_value;
2869}
2870
William Orr81574b82018-10-01 22:19:56 -07002871#endif /* defined(HAVE_SCHED_H) && (defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)) */
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002872
2873#if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_SETSCHEDULER)
2874
2875PyDoc_STRVAR(os_sched_setscheduler__doc__,
2876"sched_setscheduler($module, pid, policy, param, /)\n"
2877"--\n"
2878"\n"
2879"Set the scheduling policy for the process identified by pid.\n"
2880"\n"
2881"If pid is 0, the calling process is changed.\n"
2882"param is an instance of sched_param.");
2883
2884#define OS_SCHED_SETSCHEDULER_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02002885 {"sched_setscheduler", (PyCFunction)(void(*)(void))os_sched_setscheduler, METH_FASTCALL, os_sched_setscheduler__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002886
2887static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002888os_sched_setscheduler_impl(PyObject *module, pid_t pid, int policy,
Larry Hastings89964c42015-04-14 18:07:59 -04002889 struct sched_param *param);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002890
2891static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02002892os_sched_setscheduler(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002893{
2894 PyObject *return_value = NULL;
2895 pid_t pid;
2896 int policy;
2897 struct sched_param param;
2898
Sylvain74453812017-06-10 06:51:48 +02002899 if (!_PyArg_ParseStack(args, nargs, "" _Py_PARSE_PID "iO&:sched_setscheduler",
2900 &pid, &policy, convert_sched_param, &param)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01002901 goto exit;
2902 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002903 return_value = os_sched_setscheduler_impl(module, pid, policy, &param);
2904
2905exit:
2906 return return_value;
2907}
2908
2909#endif /* defined(HAVE_SCHED_H) && defined(HAVE_SCHED_SETSCHEDULER) */
2910
2911#if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_SETPARAM)
2912
2913PyDoc_STRVAR(os_sched_getparam__doc__,
2914"sched_getparam($module, pid, /)\n"
2915"--\n"
2916"\n"
2917"Returns scheduling parameters for the process identified by pid.\n"
2918"\n"
2919"If pid is 0, returns parameters for the calling process.\n"
2920"Return value is an instance of sched_param.");
2921
2922#define OS_SCHED_GETPARAM_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03002923 {"sched_getparam", (PyCFunction)os_sched_getparam, METH_O, os_sched_getparam__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002924
2925static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002926os_sched_getparam_impl(PyObject *module, pid_t pid);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002927
2928static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002929os_sched_getparam(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002930{
2931 PyObject *return_value = NULL;
2932 pid_t pid;
2933
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03002934 if (!PyArg_Parse(arg, "" _Py_PARSE_PID ":sched_getparam", &pid)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002935 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03002936 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002937 return_value = os_sched_getparam_impl(module, pid);
2938
2939exit:
2940 return return_value;
2941}
2942
2943#endif /* defined(HAVE_SCHED_H) && defined(HAVE_SCHED_SETPARAM) */
2944
2945#if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_SETPARAM)
2946
2947PyDoc_STRVAR(os_sched_setparam__doc__,
2948"sched_setparam($module, pid, param, /)\n"
2949"--\n"
2950"\n"
2951"Set scheduling parameters for the process identified by pid.\n"
2952"\n"
2953"If pid is 0, sets parameters for the calling process.\n"
2954"param should be an instance of sched_param.");
2955
2956#define OS_SCHED_SETPARAM_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02002957 {"sched_setparam", (PyCFunction)(void(*)(void))os_sched_setparam, METH_FASTCALL, os_sched_setparam__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002958
2959static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002960os_sched_setparam_impl(PyObject *module, pid_t pid,
Larry Hastings89964c42015-04-14 18:07:59 -04002961 struct sched_param *param);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002962
2963static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02002964os_sched_setparam(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002965{
2966 PyObject *return_value = NULL;
2967 pid_t pid;
2968 struct sched_param param;
2969
Sylvain74453812017-06-10 06:51:48 +02002970 if (!_PyArg_ParseStack(args, nargs, "" _Py_PARSE_PID "O&:sched_setparam",
2971 &pid, convert_sched_param, &param)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01002972 goto exit;
2973 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002974 return_value = os_sched_setparam_impl(module, pid, &param);
2975
2976exit:
2977 return return_value;
2978}
2979
2980#endif /* defined(HAVE_SCHED_H) && defined(HAVE_SCHED_SETPARAM) */
2981
2982#if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_RR_GET_INTERVAL)
2983
2984PyDoc_STRVAR(os_sched_rr_get_interval__doc__,
2985"sched_rr_get_interval($module, pid, /)\n"
2986"--\n"
2987"\n"
2988"Return the round-robin quantum for the process identified by pid, in seconds.\n"
2989"\n"
2990"Value returned is a float.");
2991
2992#define OS_SCHED_RR_GET_INTERVAL_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03002993 {"sched_rr_get_interval", (PyCFunction)os_sched_rr_get_interval, METH_O, os_sched_rr_get_interval__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002994
2995static double
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002996os_sched_rr_get_interval_impl(PyObject *module, pid_t pid);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002997
2998static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002999os_sched_rr_get_interval(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003000{
3001 PyObject *return_value = NULL;
3002 pid_t pid;
3003 double _return_value;
3004
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03003005 if (!PyArg_Parse(arg, "" _Py_PARSE_PID ":sched_rr_get_interval", &pid)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003006 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03003007 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003008 _return_value = os_sched_rr_get_interval_impl(module, pid);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03003009 if ((_return_value == -1.0) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003010 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03003011 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003012 return_value = PyFloat_FromDouble(_return_value);
3013
3014exit:
3015 return return_value;
3016}
3017
3018#endif /* defined(HAVE_SCHED_H) && defined(HAVE_SCHED_RR_GET_INTERVAL) */
3019
3020#if defined(HAVE_SCHED_H)
3021
3022PyDoc_STRVAR(os_sched_yield__doc__,
3023"sched_yield($module, /)\n"
3024"--\n"
3025"\n"
3026"Voluntarily relinquish the CPU.");
3027
3028#define OS_SCHED_YIELD_METHODDEF \
3029 {"sched_yield", (PyCFunction)os_sched_yield, METH_NOARGS, os_sched_yield__doc__},
3030
3031static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003032os_sched_yield_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003033
3034static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003035os_sched_yield(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003036{
3037 return os_sched_yield_impl(module);
3038}
3039
3040#endif /* defined(HAVE_SCHED_H) */
3041
3042#if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_SETAFFINITY)
3043
3044PyDoc_STRVAR(os_sched_setaffinity__doc__,
3045"sched_setaffinity($module, pid, mask, /)\n"
3046"--\n"
3047"\n"
3048"Set the CPU affinity of the process identified by pid to mask.\n"
3049"\n"
3050"mask should be an iterable of integers identifying CPUs.");
3051
3052#define OS_SCHED_SETAFFINITY_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02003053 {"sched_setaffinity", (PyCFunction)(void(*)(void))os_sched_setaffinity, METH_FASTCALL, os_sched_setaffinity__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003054
3055static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003056os_sched_setaffinity_impl(PyObject *module, pid_t pid, PyObject *mask);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003057
3058static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003059os_sched_setaffinity(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003060{
3061 PyObject *return_value = NULL;
3062 pid_t pid;
3063 PyObject *mask;
3064
Sylvain74453812017-06-10 06:51:48 +02003065 if (!_PyArg_ParseStack(args, nargs, "" _Py_PARSE_PID "O:sched_setaffinity",
3066 &pid, &mask)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01003067 goto exit;
3068 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003069 return_value = os_sched_setaffinity_impl(module, pid, mask);
3070
3071exit:
3072 return return_value;
3073}
3074
3075#endif /* defined(HAVE_SCHED_H) && defined(HAVE_SCHED_SETAFFINITY) */
3076
3077#if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_SETAFFINITY)
3078
3079PyDoc_STRVAR(os_sched_getaffinity__doc__,
3080"sched_getaffinity($module, pid, /)\n"
3081"--\n"
3082"\n"
Charles-François Natali80d62e62015-08-13 20:37:08 +01003083"Return the affinity of the process identified by pid (or the current process if zero).\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003084"\n"
3085"The affinity is returned as a set of CPU identifiers.");
3086
3087#define OS_SCHED_GETAFFINITY_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03003088 {"sched_getaffinity", (PyCFunction)os_sched_getaffinity, METH_O, os_sched_getaffinity__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003089
3090static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003091os_sched_getaffinity_impl(PyObject *module, pid_t pid);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003092
3093static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003094os_sched_getaffinity(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003095{
3096 PyObject *return_value = NULL;
3097 pid_t pid;
3098
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03003099 if (!PyArg_Parse(arg, "" _Py_PARSE_PID ":sched_getaffinity", &pid)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003100 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03003101 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003102 return_value = os_sched_getaffinity_impl(module, pid);
3103
3104exit:
3105 return return_value;
3106}
3107
3108#endif /* defined(HAVE_SCHED_H) && defined(HAVE_SCHED_SETAFFINITY) */
3109
3110#if (defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX))
3111
3112PyDoc_STRVAR(os_openpty__doc__,
3113"openpty($module, /)\n"
3114"--\n"
3115"\n"
3116"Open a pseudo-terminal.\n"
3117"\n"
3118"Return a tuple of (master_fd, slave_fd) containing open file descriptors\n"
3119"for both the master and slave ends.");
3120
3121#define OS_OPENPTY_METHODDEF \
3122 {"openpty", (PyCFunction)os_openpty, METH_NOARGS, os_openpty__doc__},
3123
3124static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003125os_openpty_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003126
3127static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003128os_openpty(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003129{
3130 return os_openpty_impl(module);
3131}
3132
3133#endif /* (defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)) */
3134
3135#if defined(HAVE_FORKPTY)
3136
3137PyDoc_STRVAR(os_forkpty__doc__,
3138"forkpty($module, /)\n"
3139"--\n"
3140"\n"
3141"Fork a new process with a new pseudo-terminal as controlling tty.\n"
3142"\n"
3143"Returns a tuple of (pid, master_fd).\n"
3144"Like fork(), return pid of 0 to the child process,\n"
3145"and pid of child to the parent process.\n"
3146"To both, return fd of newly opened pseudo-terminal.");
3147
3148#define OS_FORKPTY_METHODDEF \
3149 {"forkpty", (PyCFunction)os_forkpty, METH_NOARGS, os_forkpty__doc__},
3150
3151static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003152os_forkpty_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003153
3154static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003155os_forkpty(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003156{
3157 return os_forkpty_impl(module);
3158}
3159
3160#endif /* defined(HAVE_FORKPTY) */
3161
3162#if defined(HAVE_GETEGID)
3163
3164PyDoc_STRVAR(os_getegid__doc__,
3165"getegid($module, /)\n"
3166"--\n"
3167"\n"
3168"Return the current process\'s effective group id.");
3169
3170#define OS_GETEGID_METHODDEF \
3171 {"getegid", (PyCFunction)os_getegid, METH_NOARGS, os_getegid__doc__},
3172
3173static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003174os_getegid_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003175
3176static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003177os_getegid(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003178{
3179 return os_getegid_impl(module);
3180}
3181
3182#endif /* defined(HAVE_GETEGID) */
3183
3184#if defined(HAVE_GETEUID)
3185
3186PyDoc_STRVAR(os_geteuid__doc__,
3187"geteuid($module, /)\n"
3188"--\n"
3189"\n"
3190"Return the current process\'s effective user id.");
3191
3192#define OS_GETEUID_METHODDEF \
3193 {"geteuid", (PyCFunction)os_geteuid, METH_NOARGS, os_geteuid__doc__},
3194
3195static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003196os_geteuid_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003197
3198static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003199os_geteuid(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003200{
3201 return os_geteuid_impl(module);
3202}
3203
3204#endif /* defined(HAVE_GETEUID) */
3205
3206#if defined(HAVE_GETGID)
3207
3208PyDoc_STRVAR(os_getgid__doc__,
3209"getgid($module, /)\n"
3210"--\n"
3211"\n"
3212"Return the current process\'s group id.");
3213
3214#define OS_GETGID_METHODDEF \
3215 {"getgid", (PyCFunction)os_getgid, METH_NOARGS, os_getgid__doc__},
3216
3217static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003218os_getgid_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003219
3220static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003221os_getgid(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003222{
3223 return os_getgid_impl(module);
3224}
3225
3226#endif /* defined(HAVE_GETGID) */
3227
Berker Peksag39404992016-09-15 20:45:16 +03003228#if defined(HAVE_GETPID)
3229
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003230PyDoc_STRVAR(os_getpid__doc__,
3231"getpid($module, /)\n"
3232"--\n"
3233"\n"
3234"Return the current process id.");
3235
3236#define OS_GETPID_METHODDEF \
3237 {"getpid", (PyCFunction)os_getpid, METH_NOARGS, os_getpid__doc__},
3238
3239static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003240os_getpid_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003241
3242static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003243os_getpid(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003244{
3245 return os_getpid_impl(module);
3246}
3247
Berker Peksag39404992016-09-15 20:45:16 +03003248#endif /* defined(HAVE_GETPID) */
3249
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003250#if defined(HAVE_GETGROUPS)
3251
3252PyDoc_STRVAR(os_getgroups__doc__,
3253"getgroups($module, /)\n"
3254"--\n"
3255"\n"
3256"Return list of supplemental group IDs for the process.");
3257
3258#define OS_GETGROUPS_METHODDEF \
3259 {"getgroups", (PyCFunction)os_getgroups, METH_NOARGS, os_getgroups__doc__},
3260
3261static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003262os_getgroups_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003263
3264static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003265os_getgroups(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003266{
3267 return os_getgroups_impl(module);
3268}
3269
3270#endif /* defined(HAVE_GETGROUPS) */
3271
3272#if defined(HAVE_GETPGID)
3273
3274PyDoc_STRVAR(os_getpgid__doc__,
3275"getpgid($module, /, pid)\n"
3276"--\n"
3277"\n"
3278"Call the system call getpgid(), and return the result.");
3279
3280#define OS_GETPGID_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02003281 {"getpgid", (PyCFunction)(void(*)(void))os_getpgid, METH_FASTCALL|METH_KEYWORDS, os_getpgid__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003282
3283static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003284os_getpgid_impl(PyObject *module, pid_t pid);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003285
3286static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003287os_getpgid(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003288{
3289 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03003290 static const char * const _keywords[] = {"pid", NULL};
3291 static _PyArg_Parser _parser = {"" _Py_PARSE_PID ":getpgid", _keywords, 0};
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003292 pid_t pid;
3293
Victor Stinner3e1fad62017-01-17 01:29:01 +01003294 if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03003295 &pid)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003296 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03003297 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003298 return_value = os_getpgid_impl(module, pid);
3299
3300exit:
3301 return return_value;
3302}
3303
3304#endif /* defined(HAVE_GETPGID) */
3305
3306#if defined(HAVE_GETPGRP)
3307
3308PyDoc_STRVAR(os_getpgrp__doc__,
3309"getpgrp($module, /)\n"
3310"--\n"
3311"\n"
3312"Return the current process group id.");
3313
3314#define OS_GETPGRP_METHODDEF \
3315 {"getpgrp", (PyCFunction)os_getpgrp, METH_NOARGS, os_getpgrp__doc__},
3316
3317static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003318os_getpgrp_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003319
3320static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003321os_getpgrp(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003322{
3323 return os_getpgrp_impl(module);
3324}
3325
3326#endif /* defined(HAVE_GETPGRP) */
3327
3328#if defined(HAVE_SETPGRP)
3329
3330PyDoc_STRVAR(os_setpgrp__doc__,
3331"setpgrp($module, /)\n"
3332"--\n"
3333"\n"
3334"Make the current process the leader of its process group.");
3335
3336#define OS_SETPGRP_METHODDEF \
3337 {"setpgrp", (PyCFunction)os_setpgrp, METH_NOARGS, os_setpgrp__doc__},
3338
3339static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003340os_setpgrp_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003341
3342static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003343os_setpgrp(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003344{
3345 return os_setpgrp_impl(module);
3346}
3347
3348#endif /* defined(HAVE_SETPGRP) */
3349
3350#if defined(HAVE_GETPPID)
3351
3352PyDoc_STRVAR(os_getppid__doc__,
3353"getppid($module, /)\n"
3354"--\n"
3355"\n"
3356"Return the parent\'s process id.\n"
3357"\n"
3358"If the parent process has already exited, Windows machines will still\n"
3359"return its id; others systems will return the id of the \'init\' process (1).");
3360
3361#define OS_GETPPID_METHODDEF \
3362 {"getppid", (PyCFunction)os_getppid, METH_NOARGS, os_getppid__doc__},
3363
3364static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003365os_getppid_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003366
3367static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003368os_getppid(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003369{
3370 return os_getppid_impl(module);
3371}
3372
3373#endif /* defined(HAVE_GETPPID) */
3374
3375#if defined(HAVE_GETLOGIN)
3376
3377PyDoc_STRVAR(os_getlogin__doc__,
3378"getlogin($module, /)\n"
3379"--\n"
3380"\n"
3381"Return the actual login name.");
3382
3383#define OS_GETLOGIN_METHODDEF \
3384 {"getlogin", (PyCFunction)os_getlogin, METH_NOARGS, os_getlogin__doc__},
3385
3386static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003387os_getlogin_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003388
3389static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003390os_getlogin(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003391{
3392 return os_getlogin_impl(module);
3393}
3394
3395#endif /* defined(HAVE_GETLOGIN) */
3396
3397#if defined(HAVE_GETUID)
3398
3399PyDoc_STRVAR(os_getuid__doc__,
3400"getuid($module, /)\n"
3401"--\n"
3402"\n"
3403"Return the current process\'s user id.");
3404
3405#define OS_GETUID_METHODDEF \
3406 {"getuid", (PyCFunction)os_getuid, METH_NOARGS, os_getuid__doc__},
3407
3408static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003409os_getuid_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003410
3411static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003412os_getuid(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003413{
3414 return os_getuid_impl(module);
3415}
3416
3417#endif /* defined(HAVE_GETUID) */
3418
3419#if defined(HAVE_KILL)
3420
3421PyDoc_STRVAR(os_kill__doc__,
3422"kill($module, pid, signal, /)\n"
3423"--\n"
3424"\n"
3425"Kill a process with a signal.");
3426
3427#define OS_KILL_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02003428 {"kill", (PyCFunction)(void(*)(void))os_kill, METH_FASTCALL, os_kill__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003429
3430static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003431os_kill_impl(PyObject *module, pid_t pid, Py_ssize_t signal);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003432
3433static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003434os_kill(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003435{
3436 PyObject *return_value = NULL;
3437 pid_t pid;
3438 Py_ssize_t signal;
3439
Sylvain74453812017-06-10 06:51:48 +02003440 if (!_PyArg_ParseStack(args, nargs, "" _Py_PARSE_PID "n:kill",
3441 &pid, &signal)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01003442 goto exit;
3443 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003444 return_value = os_kill_impl(module, pid, signal);
3445
3446exit:
3447 return return_value;
3448}
3449
3450#endif /* defined(HAVE_KILL) */
3451
3452#if defined(HAVE_KILLPG)
3453
3454PyDoc_STRVAR(os_killpg__doc__,
3455"killpg($module, pgid, signal, /)\n"
3456"--\n"
3457"\n"
3458"Kill a process group with a signal.");
3459
3460#define OS_KILLPG_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02003461 {"killpg", (PyCFunction)(void(*)(void))os_killpg, METH_FASTCALL, os_killpg__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003462
3463static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003464os_killpg_impl(PyObject *module, pid_t pgid, int signal);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003465
3466static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003467os_killpg(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003468{
3469 PyObject *return_value = NULL;
3470 pid_t pgid;
3471 int signal;
3472
Sylvain74453812017-06-10 06:51:48 +02003473 if (!_PyArg_ParseStack(args, nargs, "" _Py_PARSE_PID "i:killpg",
3474 &pgid, &signal)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01003475 goto exit;
3476 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003477 return_value = os_killpg_impl(module, pgid, signal);
3478
3479exit:
3480 return return_value;
3481}
3482
3483#endif /* defined(HAVE_KILLPG) */
3484
3485#if defined(HAVE_PLOCK)
3486
3487PyDoc_STRVAR(os_plock__doc__,
3488"plock($module, op, /)\n"
3489"--\n"
3490"\n"
3491"Lock program segments into memory.\");");
3492
3493#define OS_PLOCK_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03003494 {"plock", (PyCFunction)os_plock, METH_O, os_plock__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003495
3496static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003497os_plock_impl(PyObject *module, int op);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003498
3499static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003500os_plock(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003501{
3502 PyObject *return_value = NULL;
3503 int op;
3504
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02003505 if (PyFloat_Check(arg)) {
3506 PyErr_SetString(PyExc_TypeError,
3507 "integer argument expected, got float" );
3508 goto exit;
3509 }
3510 op = _PyLong_AsInt(arg);
3511 if (op == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003512 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03003513 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003514 return_value = os_plock_impl(module, op);
3515
3516exit:
3517 return return_value;
3518}
3519
3520#endif /* defined(HAVE_PLOCK) */
3521
3522#if defined(HAVE_SETUID)
3523
3524PyDoc_STRVAR(os_setuid__doc__,
3525"setuid($module, uid, /)\n"
3526"--\n"
3527"\n"
3528"Set the current process\'s user id.");
3529
3530#define OS_SETUID_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03003531 {"setuid", (PyCFunction)os_setuid, METH_O, os_setuid__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003532
3533static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003534os_setuid_impl(PyObject *module, uid_t uid);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003535
3536static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003537os_setuid(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003538{
3539 PyObject *return_value = NULL;
3540 uid_t uid;
3541
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02003542 if (!_Py_Uid_Converter(arg, &uid)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003543 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03003544 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003545 return_value = os_setuid_impl(module, uid);
3546
3547exit:
3548 return return_value;
3549}
3550
3551#endif /* defined(HAVE_SETUID) */
3552
3553#if defined(HAVE_SETEUID)
3554
3555PyDoc_STRVAR(os_seteuid__doc__,
3556"seteuid($module, euid, /)\n"
3557"--\n"
3558"\n"
3559"Set the current process\'s effective user id.");
3560
3561#define OS_SETEUID_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03003562 {"seteuid", (PyCFunction)os_seteuid, METH_O, os_seteuid__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003563
3564static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003565os_seteuid_impl(PyObject *module, uid_t euid);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003566
3567static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003568os_seteuid(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003569{
3570 PyObject *return_value = NULL;
3571 uid_t euid;
3572
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02003573 if (!_Py_Uid_Converter(arg, &euid)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003574 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03003575 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003576 return_value = os_seteuid_impl(module, euid);
3577
3578exit:
3579 return return_value;
3580}
3581
3582#endif /* defined(HAVE_SETEUID) */
3583
3584#if defined(HAVE_SETEGID)
3585
3586PyDoc_STRVAR(os_setegid__doc__,
3587"setegid($module, egid, /)\n"
3588"--\n"
3589"\n"
3590"Set the current process\'s effective group id.");
3591
3592#define OS_SETEGID_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03003593 {"setegid", (PyCFunction)os_setegid, METH_O, os_setegid__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003594
3595static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003596os_setegid_impl(PyObject *module, gid_t egid);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003597
3598static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003599os_setegid(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003600{
3601 PyObject *return_value = NULL;
3602 gid_t egid;
3603
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02003604 if (!_Py_Gid_Converter(arg, &egid)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003605 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03003606 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003607 return_value = os_setegid_impl(module, egid);
3608
3609exit:
3610 return return_value;
3611}
3612
3613#endif /* defined(HAVE_SETEGID) */
3614
3615#if defined(HAVE_SETREUID)
3616
3617PyDoc_STRVAR(os_setreuid__doc__,
3618"setreuid($module, ruid, euid, /)\n"
3619"--\n"
3620"\n"
3621"Set the current process\'s real and effective user ids.");
3622
3623#define OS_SETREUID_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02003624 {"setreuid", (PyCFunction)(void(*)(void))os_setreuid, METH_FASTCALL, os_setreuid__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003625
3626static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003627os_setreuid_impl(PyObject *module, uid_t ruid, uid_t euid);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003628
3629static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003630os_setreuid(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003631{
3632 PyObject *return_value = NULL;
3633 uid_t ruid;
3634 uid_t euid;
3635
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02003636 if (!_PyArg_CheckPositional("setreuid", nargs, 2, 2)) {
3637 goto exit;
3638 }
3639 if (!_Py_Uid_Converter(args[0], &ruid)) {
3640 goto exit;
3641 }
3642 if (!_Py_Uid_Converter(args[1], &euid)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01003643 goto exit;
3644 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003645 return_value = os_setreuid_impl(module, ruid, euid);
3646
3647exit:
3648 return return_value;
3649}
3650
3651#endif /* defined(HAVE_SETREUID) */
3652
3653#if defined(HAVE_SETREGID)
3654
3655PyDoc_STRVAR(os_setregid__doc__,
3656"setregid($module, rgid, egid, /)\n"
3657"--\n"
3658"\n"
3659"Set the current process\'s real and effective group ids.");
3660
3661#define OS_SETREGID_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02003662 {"setregid", (PyCFunction)(void(*)(void))os_setregid, METH_FASTCALL, os_setregid__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003663
3664static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003665os_setregid_impl(PyObject *module, gid_t rgid, gid_t egid);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003666
3667static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003668os_setregid(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003669{
3670 PyObject *return_value = NULL;
3671 gid_t rgid;
3672 gid_t egid;
3673
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02003674 if (!_PyArg_CheckPositional("setregid", nargs, 2, 2)) {
3675 goto exit;
3676 }
3677 if (!_Py_Gid_Converter(args[0], &rgid)) {
3678 goto exit;
3679 }
3680 if (!_Py_Gid_Converter(args[1], &egid)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01003681 goto exit;
3682 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003683 return_value = os_setregid_impl(module, rgid, egid);
3684
3685exit:
3686 return return_value;
3687}
3688
3689#endif /* defined(HAVE_SETREGID) */
3690
3691#if defined(HAVE_SETGID)
3692
3693PyDoc_STRVAR(os_setgid__doc__,
3694"setgid($module, gid, /)\n"
3695"--\n"
3696"\n"
3697"Set the current process\'s group id.");
3698
3699#define OS_SETGID_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03003700 {"setgid", (PyCFunction)os_setgid, METH_O, os_setgid__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003701
3702static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003703os_setgid_impl(PyObject *module, gid_t gid);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003704
3705static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003706os_setgid(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003707{
3708 PyObject *return_value = NULL;
3709 gid_t gid;
3710
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02003711 if (!_Py_Gid_Converter(arg, &gid)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003712 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03003713 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003714 return_value = os_setgid_impl(module, gid);
3715
3716exit:
3717 return return_value;
3718}
3719
3720#endif /* defined(HAVE_SETGID) */
3721
3722#if defined(HAVE_SETGROUPS)
3723
3724PyDoc_STRVAR(os_setgroups__doc__,
3725"setgroups($module, groups, /)\n"
3726"--\n"
3727"\n"
3728"Set the groups of the current process to list.");
3729
3730#define OS_SETGROUPS_METHODDEF \
3731 {"setgroups", (PyCFunction)os_setgroups, METH_O, os_setgroups__doc__},
3732
3733#endif /* defined(HAVE_SETGROUPS) */
3734
3735#if defined(HAVE_WAIT3)
3736
3737PyDoc_STRVAR(os_wait3__doc__,
3738"wait3($module, /, options)\n"
3739"--\n"
3740"\n"
3741"Wait for completion of a child process.\n"
3742"\n"
3743"Returns a tuple of information about the child process:\n"
3744" (pid, status, rusage)");
3745
3746#define OS_WAIT3_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02003747 {"wait3", (PyCFunction)(void(*)(void))os_wait3, METH_FASTCALL|METH_KEYWORDS, os_wait3__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003748
3749static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003750os_wait3_impl(PyObject *module, int options);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003751
3752static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003753os_wait3(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003754{
3755 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03003756 static const char * const _keywords[] = {"options", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02003757 static _PyArg_Parser _parser = {NULL, _keywords, "wait3", 0};
3758 PyObject *argsbuf[1];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003759 int options;
3760
Serhiy Storchaka31913912019-03-14 10:32:22 +02003761 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
3762 if (!args) {
3763 goto exit;
3764 }
3765 if (PyFloat_Check(args[0])) {
3766 PyErr_SetString(PyExc_TypeError,
3767 "integer argument expected, got float" );
3768 goto exit;
3769 }
3770 options = _PyLong_AsInt(args[0]);
3771 if (options == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003772 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03003773 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003774 return_value = os_wait3_impl(module, options);
3775
3776exit:
3777 return return_value;
3778}
3779
3780#endif /* defined(HAVE_WAIT3) */
3781
3782#if defined(HAVE_WAIT4)
3783
3784PyDoc_STRVAR(os_wait4__doc__,
3785"wait4($module, /, pid, options)\n"
3786"--\n"
3787"\n"
3788"Wait for completion of a specific child process.\n"
3789"\n"
3790"Returns a tuple of information about the child process:\n"
3791" (pid, status, rusage)");
3792
3793#define OS_WAIT4_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02003794 {"wait4", (PyCFunction)(void(*)(void))os_wait4, METH_FASTCALL|METH_KEYWORDS, os_wait4__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003795
3796static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003797os_wait4_impl(PyObject *module, pid_t pid, int options);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003798
3799static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003800os_wait4(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003801{
3802 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03003803 static const char * const _keywords[] = {"pid", "options", NULL};
3804 static _PyArg_Parser _parser = {"" _Py_PARSE_PID "i:wait4", _keywords, 0};
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003805 pid_t pid;
3806 int options;
3807
Victor Stinner3e1fad62017-01-17 01:29:01 +01003808 if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03003809 &pid, &options)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003810 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03003811 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003812 return_value = os_wait4_impl(module, pid, options);
3813
3814exit:
3815 return return_value;
3816}
3817
3818#endif /* defined(HAVE_WAIT4) */
3819
3820#if (defined(HAVE_WAITID) && !defined(__APPLE__))
3821
3822PyDoc_STRVAR(os_waitid__doc__,
3823"waitid($module, idtype, id, options, /)\n"
3824"--\n"
3825"\n"
3826"Returns the result of waiting for a process or processes.\n"
3827"\n"
3828" idtype\n"
3829" Must be one of be P_PID, P_PGID or P_ALL.\n"
3830" id\n"
3831" The id to wait on.\n"
3832" options\n"
3833" Constructed from the ORing of one or more of WEXITED, WSTOPPED\n"
3834" or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.\n"
3835"\n"
3836"Returns either waitid_result or None if WNOHANG is specified and there are\n"
3837"no children in a waitable state.");
3838
3839#define OS_WAITID_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02003840 {"waitid", (PyCFunction)(void(*)(void))os_waitid, METH_FASTCALL, os_waitid__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003841
3842static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003843os_waitid_impl(PyObject *module, idtype_t idtype, id_t id, int options);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003844
3845static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003846os_waitid(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003847{
3848 PyObject *return_value = NULL;
3849 idtype_t idtype;
3850 id_t id;
3851 int options;
3852
Sylvain74453812017-06-10 06:51:48 +02003853 if (!_PyArg_ParseStack(args, nargs, "i" _Py_PARSE_PID "i:waitid",
3854 &idtype, &id, &options)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01003855 goto exit;
3856 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003857 return_value = os_waitid_impl(module, idtype, id, options);
3858
3859exit:
3860 return return_value;
3861}
3862
3863#endif /* (defined(HAVE_WAITID) && !defined(__APPLE__)) */
3864
3865#if defined(HAVE_WAITPID)
3866
3867PyDoc_STRVAR(os_waitpid__doc__,
3868"waitpid($module, pid, options, /)\n"
3869"--\n"
3870"\n"
3871"Wait for completion of a given child process.\n"
3872"\n"
3873"Returns a tuple of information regarding the child process:\n"
3874" (pid, status)\n"
3875"\n"
3876"The options argument is ignored on Windows.");
3877
3878#define OS_WAITPID_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02003879 {"waitpid", (PyCFunction)(void(*)(void))os_waitpid, METH_FASTCALL, os_waitpid__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003880
3881static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003882os_waitpid_impl(PyObject *module, pid_t pid, int options);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003883
3884static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003885os_waitpid(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003886{
3887 PyObject *return_value = NULL;
3888 pid_t pid;
3889 int options;
3890
Sylvain74453812017-06-10 06:51:48 +02003891 if (!_PyArg_ParseStack(args, nargs, "" _Py_PARSE_PID "i:waitpid",
3892 &pid, &options)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01003893 goto exit;
3894 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003895 return_value = os_waitpid_impl(module, pid, options);
3896
3897exit:
3898 return return_value;
3899}
3900
3901#endif /* defined(HAVE_WAITPID) */
3902
3903#if defined(HAVE_CWAIT)
3904
3905PyDoc_STRVAR(os_waitpid__doc__,
3906"waitpid($module, pid, options, /)\n"
3907"--\n"
3908"\n"
3909"Wait for completion of a given process.\n"
3910"\n"
3911"Returns a tuple of information regarding the process:\n"
3912" (pid, status << 8)\n"
3913"\n"
3914"The options argument is ignored on Windows.");
3915
3916#define OS_WAITPID_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02003917 {"waitpid", (PyCFunction)(void(*)(void))os_waitpid, METH_FASTCALL, os_waitpid__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003918
3919static PyObject *
Victor Stinner581139c2016-09-06 15:54:20 -07003920os_waitpid_impl(PyObject *module, intptr_t pid, int options);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003921
3922static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003923os_waitpid(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003924{
3925 PyObject *return_value = NULL;
Victor Stinner581139c2016-09-06 15:54:20 -07003926 intptr_t pid;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003927 int options;
3928
Sylvain74453812017-06-10 06:51:48 +02003929 if (!_PyArg_ParseStack(args, nargs, "" _Py_PARSE_INTPTR "i:waitpid",
3930 &pid, &options)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01003931 goto exit;
3932 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003933 return_value = os_waitpid_impl(module, pid, options);
3934
3935exit:
3936 return return_value;
3937}
3938
3939#endif /* defined(HAVE_CWAIT) */
3940
3941#if defined(HAVE_WAIT)
3942
3943PyDoc_STRVAR(os_wait__doc__,
3944"wait($module, /)\n"
3945"--\n"
3946"\n"
3947"Wait for completion of a child process.\n"
3948"\n"
3949"Returns a tuple of information about the child process:\n"
3950" (pid, status)");
3951
3952#define OS_WAIT_METHODDEF \
3953 {"wait", (PyCFunction)os_wait, METH_NOARGS, os_wait__doc__},
3954
3955static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003956os_wait_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003957
3958static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003959os_wait(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003960{
3961 return os_wait_impl(module);
3962}
3963
3964#endif /* defined(HAVE_WAIT) */
3965
Benjamin Peterson6c4c45e2019-11-05 19:21:29 -08003966#if (defined(__linux__) && defined(__NR_pidfd_open))
3967
3968PyDoc_STRVAR(os_pidfd_open__doc__,
3969"pidfd_open($module, /, pid, flags=0)\n"
3970"--\n"
3971"\n"
3972"Return a file descriptor referring to the process *pid*.\n"
3973"\n"
3974"The descriptor can be used to perform process management without races and\n"
3975"signals.");
3976
3977#define OS_PIDFD_OPEN_METHODDEF \
3978 {"pidfd_open", (PyCFunction)(void(*)(void))os_pidfd_open, METH_FASTCALL|METH_KEYWORDS, os_pidfd_open__doc__},
3979
3980static PyObject *
3981os_pidfd_open_impl(PyObject *module, pid_t pid, unsigned int flags);
3982
3983static PyObject *
3984os_pidfd_open(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
3985{
3986 PyObject *return_value = NULL;
3987 static const char * const _keywords[] = {"pid", "flags", NULL};
3988 static _PyArg_Parser _parser = {"" _Py_PARSE_PID "|O&:pidfd_open", _keywords, 0};
3989 pid_t pid;
3990 unsigned int flags = 0;
3991
3992 if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
3993 &pid, _PyLong_UnsignedInt_Converter, &flags)) {
3994 goto exit;
3995 }
3996 return_value = os_pidfd_open_impl(module, pid, flags);
3997
3998exit:
3999 return return_value;
4000}
4001
4002#endif /* (defined(__linux__) && defined(__NR_pidfd_open)) */
4003
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03004004#if (defined(HAVE_READLINK) || defined(MS_WINDOWS))
4005
4006PyDoc_STRVAR(os_readlink__doc__,
4007"readlink($module, /, path, *, dir_fd=None)\n"
4008"--\n"
4009"\n"
4010"Return a string representing the path to which the symbolic link points.\n"
4011"\n"
4012"If dir_fd is not None, it should be a file descriptor open to a directory,\n"
4013"and path should be relative; path will then be relative to that directory.\n"
4014"\n"
4015"dir_fd may not be implemented on your platform. If it is unavailable,\n"
4016"using it will raise a NotImplementedError.");
4017
4018#define OS_READLINK_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02004019 {"readlink", (PyCFunction)(void(*)(void))os_readlink, METH_FASTCALL|METH_KEYWORDS, os_readlink__doc__},
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03004020
4021static PyObject *
4022os_readlink_impl(PyObject *module, path_t *path, int dir_fd);
4023
4024static PyObject *
4025os_readlink(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
4026{
4027 PyObject *return_value = NULL;
4028 static const char * const _keywords[] = {"path", "dir_fd", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02004029 static _PyArg_Parser _parser = {NULL, _keywords, "readlink", 0};
4030 PyObject *argsbuf[2];
4031 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03004032 path_t path = PATH_T_INITIALIZE("readlink", "path", 0, 0);
4033 int dir_fd = DEFAULT_DIR_FD;
4034
Serhiy Storchaka31913912019-03-14 10:32:22 +02004035 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
4036 if (!args) {
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03004037 goto exit;
4038 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02004039 if (!path_converter(args[0], &path)) {
4040 goto exit;
4041 }
4042 if (!noptargs) {
4043 goto skip_optional_kwonly;
4044 }
4045 if (!READLINKAT_DIR_FD_CONVERTER(args[1], &dir_fd)) {
4046 goto exit;
4047 }
4048skip_optional_kwonly:
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03004049 return_value = os_readlink_impl(module, &path, dir_fd);
4050
4051exit:
4052 /* Cleanup for path */
4053 path_cleanup(&path);
4054
4055 return return_value;
4056}
4057
4058#endif /* (defined(HAVE_READLINK) || defined(MS_WINDOWS)) */
4059
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004060#if defined(HAVE_SYMLINK)
4061
4062PyDoc_STRVAR(os_symlink__doc__,
4063"symlink($module, /, src, dst, target_is_directory=False, *, dir_fd=None)\n"
4064"--\n"
4065"\n"
4066"Create a symbolic link pointing to src named dst.\n"
4067"\n"
4068"target_is_directory is required on Windows if the target is to be\n"
4069" interpreted as a directory. (On Windows, symlink requires\n"
4070" Windows 6.0 or greater, and raises a NotImplementedError otherwise.)\n"
4071" target_is_directory is ignored on non-Windows platforms.\n"
4072"\n"
4073"If dir_fd is not None, it should be a file descriptor open to a directory,\n"
4074" and path should be relative; path will then be relative to that directory.\n"
4075"dir_fd may not be implemented on your platform.\n"
4076" If it is unavailable, using it will raise a NotImplementedError.");
4077
4078#define OS_SYMLINK_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02004079 {"symlink", (PyCFunction)(void(*)(void))os_symlink, METH_FASTCALL|METH_KEYWORDS, os_symlink__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004080
4081static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004082os_symlink_impl(PyObject *module, path_t *src, path_t *dst,
Larry Hastings89964c42015-04-14 18:07:59 -04004083 int target_is_directory, int dir_fd);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004084
4085static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004086os_symlink(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004087{
4088 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03004089 static const char * const _keywords[] = {"src", "dst", "target_is_directory", "dir_fd", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02004090 static _PyArg_Parser _parser = {NULL, _keywords, "symlink", 0};
4091 PyObject *argsbuf[4];
4092 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004093 path_t src = PATH_T_INITIALIZE("symlink", "src", 0, 0);
4094 path_t dst = PATH_T_INITIALIZE("symlink", "dst", 0, 0);
4095 int target_is_directory = 0;
4096 int dir_fd = DEFAULT_DIR_FD;
4097
Serhiy Storchaka31913912019-03-14 10:32:22 +02004098 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 3, 0, argsbuf);
4099 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004100 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03004101 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02004102 if (!path_converter(args[0], &src)) {
4103 goto exit;
4104 }
4105 if (!path_converter(args[1], &dst)) {
4106 goto exit;
4107 }
4108 if (!noptargs) {
4109 goto skip_optional_pos;
4110 }
4111 if (args[2]) {
4112 target_is_directory = PyObject_IsTrue(args[2]);
4113 if (target_is_directory < 0) {
4114 goto exit;
4115 }
4116 if (!--noptargs) {
4117 goto skip_optional_pos;
4118 }
4119 }
4120skip_optional_pos:
4121 if (!noptargs) {
4122 goto skip_optional_kwonly;
4123 }
4124 if (!SYMLINKAT_DIR_FD_CONVERTER(args[3], &dir_fd)) {
4125 goto exit;
4126 }
4127skip_optional_kwonly:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004128 return_value = os_symlink_impl(module, &src, &dst, target_is_directory, dir_fd);
4129
4130exit:
4131 /* Cleanup for src */
4132 path_cleanup(&src);
4133 /* Cleanup for dst */
4134 path_cleanup(&dst);
4135
4136 return return_value;
4137}
4138
4139#endif /* defined(HAVE_SYMLINK) */
4140
4141#if defined(HAVE_TIMES)
4142
4143PyDoc_STRVAR(os_times__doc__,
4144"times($module, /)\n"
4145"--\n"
4146"\n"
4147"Return a collection containing process timing information.\n"
4148"\n"
4149"The object returned behaves like a named tuple with these fields:\n"
4150" (utime, stime, cutime, cstime, elapsed_time)\n"
4151"All fields are floating point numbers.");
4152
4153#define OS_TIMES_METHODDEF \
4154 {"times", (PyCFunction)os_times, METH_NOARGS, os_times__doc__},
4155
4156static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004157os_times_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004158
4159static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004160os_times(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004161{
4162 return os_times_impl(module);
4163}
4164
4165#endif /* defined(HAVE_TIMES) */
4166
4167#if defined(HAVE_GETSID)
4168
4169PyDoc_STRVAR(os_getsid__doc__,
4170"getsid($module, pid, /)\n"
4171"--\n"
4172"\n"
4173"Call the system call getsid(pid) and return the result.");
4174
4175#define OS_GETSID_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03004176 {"getsid", (PyCFunction)os_getsid, METH_O, os_getsid__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004177
4178static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004179os_getsid_impl(PyObject *module, pid_t pid);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004180
4181static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004182os_getsid(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004183{
4184 PyObject *return_value = NULL;
4185 pid_t pid;
4186
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03004187 if (!PyArg_Parse(arg, "" _Py_PARSE_PID ":getsid", &pid)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004188 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03004189 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004190 return_value = os_getsid_impl(module, pid);
4191
4192exit:
4193 return return_value;
4194}
4195
4196#endif /* defined(HAVE_GETSID) */
4197
4198#if defined(HAVE_SETSID)
4199
4200PyDoc_STRVAR(os_setsid__doc__,
4201"setsid($module, /)\n"
4202"--\n"
4203"\n"
4204"Call the system call setsid().");
4205
4206#define OS_SETSID_METHODDEF \
4207 {"setsid", (PyCFunction)os_setsid, METH_NOARGS, os_setsid__doc__},
4208
4209static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004210os_setsid_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004211
4212static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004213os_setsid(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004214{
4215 return os_setsid_impl(module);
4216}
4217
4218#endif /* defined(HAVE_SETSID) */
4219
4220#if defined(HAVE_SETPGID)
4221
4222PyDoc_STRVAR(os_setpgid__doc__,
4223"setpgid($module, pid, pgrp, /)\n"
4224"--\n"
4225"\n"
4226"Call the system call setpgid(pid, pgrp).");
4227
4228#define OS_SETPGID_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02004229 {"setpgid", (PyCFunction)(void(*)(void))os_setpgid, METH_FASTCALL, os_setpgid__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004230
4231static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004232os_setpgid_impl(PyObject *module, pid_t pid, pid_t pgrp);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004233
4234static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004235os_setpgid(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004236{
4237 PyObject *return_value = NULL;
4238 pid_t pid;
4239 pid_t pgrp;
4240
Sylvain74453812017-06-10 06:51:48 +02004241 if (!_PyArg_ParseStack(args, nargs, "" _Py_PARSE_PID "" _Py_PARSE_PID ":setpgid",
4242 &pid, &pgrp)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01004243 goto exit;
4244 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004245 return_value = os_setpgid_impl(module, pid, pgrp);
4246
4247exit:
4248 return return_value;
4249}
4250
4251#endif /* defined(HAVE_SETPGID) */
4252
4253#if defined(HAVE_TCGETPGRP)
4254
4255PyDoc_STRVAR(os_tcgetpgrp__doc__,
4256"tcgetpgrp($module, fd, /)\n"
4257"--\n"
4258"\n"
4259"Return the process group associated with the terminal specified by fd.");
4260
4261#define OS_TCGETPGRP_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03004262 {"tcgetpgrp", (PyCFunction)os_tcgetpgrp, METH_O, os_tcgetpgrp__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004263
4264static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004265os_tcgetpgrp_impl(PyObject *module, int fd);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004266
4267static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004268os_tcgetpgrp(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004269{
4270 PyObject *return_value = NULL;
4271 int fd;
4272
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02004273 if (PyFloat_Check(arg)) {
4274 PyErr_SetString(PyExc_TypeError,
4275 "integer argument expected, got float" );
4276 goto exit;
4277 }
4278 fd = _PyLong_AsInt(arg);
4279 if (fd == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004280 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03004281 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004282 return_value = os_tcgetpgrp_impl(module, fd);
4283
4284exit:
4285 return return_value;
4286}
4287
4288#endif /* defined(HAVE_TCGETPGRP) */
4289
4290#if defined(HAVE_TCSETPGRP)
4291
4292PyDoc_STRVAR(os_tcsetpgrp__doc__,
4293"tcsetpgrp($module, fd, pgid, /)\n"
4294"--\n"
4295"\n"
4296"Set the process group associated with the terminal specified by fd.");
4297
4298#define OS_TCSETPGRP_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02004299 {"tcsetpgrp", (PyCFunction)(void(*)(void))os_tcsetpgrp, METH_FASTCALL, os_tcsetpgrp__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004300
4301static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004302os_tcsetpgrp_impl(PyObject *module, int fd, pid_t pgid);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004303
4304static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004305os_tcsetpgrp(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004306{
4307 PyObject *return_value = NULL;
4308 int fd;
4309 pid_t pgid;
4310
Sylvain74453812017-06-10 06:51:48 +02004311 if (!_PyArg_ParseStack(args, nargs, "i" _Py_PARSE_PID ":tcsetpgrp",
4312 &fd, &pgid)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01004313 goto exit;
4314 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004315 return_value = os_tcsetpgrp_impl(module, fd, pgid);
4316
4317exit:
4318 return return_value;
4319}
4320
4321#endif /* defined(HAVE_TCSETPGRP) */
4322
4323PyDoc_STRVAR(os_open__doc__,
4324"open($module, /, path, flags, mode=511, *, dir_fd=None)\n"
4325"--\n"
4326"\n"
4327"Open a file for low level IO. Returns a file descriptor (integer).\n"
4328"\n"
4329"If dir_fd is not None, it should be a file descriptor open to a directory,\n"
4330" and path should be relative; path will then be relative to that directory.\n"
4331"dir_fd may not be implemented on your platform.\n"
4332" If it is unavailable, using it will raise a NotImplementedError.");
4333
4334#define OS_OPEN_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02004335 {"open", (PyCFunction)(void(*)(void))os_open, METH_FASTCALL|METH_KEYWORDS, os_open__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004336
4337static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004338os_open_impl(PyObject *module, path_t *path, int flags, int mode, int dir_fd);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004339
4340static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004341os_open(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004342{
4343 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03004344 static const char * const _keywords[] = {"path", "flags", "mode", "dir_fd", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02004345 static _PyArg_Parser _parser = {NULL, _keywords, "open", 0};
4346 PyObject *argsbuf[4];
4347 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004348 path_t path = PATH_T_INITIALIZE("open", "path", 0, 0);
4349 int flags;
4350 int mode = 511;
4351 int dir_fd = DEFAULT_DIR_FD;
4352 int _return_value;
4353
Serhiy Storchaka31913912019-03-14 10:32:22 +02004354 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 3, 0, argsbuf);
4355 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004356 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03004357 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02004358 if (!path_converter(args[0], &path)) {
4359 goto exit;
4360 }
4361 if (PyFloat_Check(args[1])) {
4362 PyErr_SetString(PyExc_TypeError,
4363 "integer argument expected, got float" );
4364 goto exit;
4365 }
4366 flags = _PyLong_AsInt(args[1]);
4367 if (flags == -1 && PyErr_Occurred()) {
4368 goto exit;
4369 }
4370 if (!noptargs) {
4371 goto skip_optional_pos;
4372 }
4373 if (args[2]) {
4374 if (PyFloat_Check(args[2])) {
4375 PyErr_SetString(PyExc_TypeError,
4376 "integer argument expected, got float" );
4377 goto exit;
4378 }
4379 mode = _PyLong_AsInt(args[2]);
4380 if (mode == -1 && PyErr_Occurred()) {
4381 goto exit;
4382 }
4383 if (!--noptargs) {
4384 goto skip_optional_pos;
4385 }
4386 }
4387skip_optional_pos:
4388 if (!noptargs) {
4389 goto skip_optional_kwonly;
4390 }
4391 if (!OPENAT_DIR_FD_CONVERTER(args[3], &dir_fd)) {
4392 goto exit;
4393 }
4394skip_optional_kwonly:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004395 _return_value = os_open_impl(module, &path, flags, mode, dir_fd);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03004396 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004397 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03004398 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004399 return_value = PyLong_FromLong((long)_return_value);
4400
4401exit:
4402 /* Cleanup for path */
4403 path_cleanup(&path);
4404
4405 return return_value;
4406}
4407
4408PyDoc_STRVAR(os_close__doc__,
4409"close($module, /, fd)\n"
4410"--\n"
4411"\n"
4412"Close a file descriptor.");
4413
4414#define OS_CLOSE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02004415 {"close", (PyCFunction)(void(*)(void))os_close, METH_FASTCALL|METH_KEYWORDS, os_close__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004416
4417static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004418os_close_impl(PyObject *module, int fd);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004419
4420static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004421os_close(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004422{
4423 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03004424 static const char * const _keywords[] = {"fd", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02004425 static _PyArg_Parser _parser = {NULL, _keywords, "close", 0};
4426 PyObject *argsbuf[1];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004427 int fd;
4428
Serhiy Storchaka31913912019-03-14 10:32:22 +02004429 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
4430 if (!args) {
4431 goto exit;
4432 }
4433 if (PyFloat_Check(args[0])) {
4434 PyErr_SetString(PyExc_TypeError,
4435 "integer argument expected, got float" );
4436 goto exit;
4437 }
4438 fd = _PyLong_AsInt(args[0]);
4439 if (fd == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004440 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03004441 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004442 return_value = os_close_impl(module, fd);
4443
4444exit:
4445 return return_value;
4446}
4447
4448PyDoc_STRVAR(os_closerange__doc__,
4449"closerange($module, fd_low, fd_high, /)\n"
4450"--\n"
4451"\n"
4452"Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
4453
4454#define OS_CLOSERANGE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02004455 {"closerange", (PyCFunction)(void(*)(void))os_closerange, METH_FASTCALL, os_closerange__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004456
4457static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004458os_closerange_impl(PyObject *module, int fd_low, int fd_high);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004459
4460static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004461os_closerange(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004462{
4463 PyObject *return_value = NULL;
4464 int fd_low;
4465 int fd_high;
4466
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02004467 if (!_PyArg_CheckPositional("closerange", nargs, 2, 2)) {
4468 goto exit;
4469 }
4470 if (PyFloat_Check(args[0])) {
4471 PyErr_SetString(PyExc_TypeError,
4472 "integer argument expected, got float" );
4473 goto exit;
4474 }
4475 fd_low = _PyLong_AsInt(args[0]);
4476 if (fd_low == -1 && PyErr_Occurred()) {
4477 goto exit;
4478 }
4479 if (PyFloat_Check(args[1])) {
4480 PyErr_SetString(PyExc_TypeError,
4481 "integer argument expected, got float" );
4482 goto exit;
4483 }
4484 fd_high = _PyLong_AsInt(args[1]);
4485 if (fd_high == -1 && PyErr_Occurred()) {
Victor Stinner259f0e42017-01-17 01:35:17 +01004486 goto exit;
4487 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004488 return_value = os_closerange_impl(module, fd_low, fd_high);
4489
4490exit:
4491 return return_value;
4492}
4493
4494PyDoc_STRVAR(os_dup__doc__,
4495"dup($module, fd, /)\n"
4496"--\n"
4497"\n"
4498"Return a duplicate of a file descriptor.");
4499
4500#define OS_DUP_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03004501 {"dup", (PyCFunction)os_dup, METH_O, os_dup__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004502
4503static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004504os_dup_impl(PyObject *module, int fd);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004505
4506static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004507os_dup(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004508{
4509 PyObject *return_value = NULL;
4510 int fd;
4511 int _return_value;
4512
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02004513 if (PyFloat_Check(arg)) {
4514 PyErr_SetString(PyExc_TypeError,
4515 "integer argument expected, got float" );
4516 goto exit;
4517 }
4518 fd = _PyLong_AsInt(arg);
4519 if (fd == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004520 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03004521 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004522 _return_value = os_dup_impl(module, fd);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03004523 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004524 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03004525 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004526 return_value = PyLong_FromLong((long)_return_value);
4527
4528exit:
4529 return return_value;
4530}
4531
4532PyDoc_STRVAR(os_dup2__doc__,
4533"dup2($module, /, fd, fd2, inheritable=True)\n"
4534"--\n"
4535"\n"
4536"Duplicate file descriptor.");
4537
4538#define OS_DUP2_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02004539 {"dup2", (PyCFunction)(void(*)(void))os_dup2, METH_FASTCALL|METH_KEYWORDS, os_dup2__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004540
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08004541static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004542os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004543
4544static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004545os_dup2(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004546{
4547 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03004548 static const char * const _keywords[] = {"fd", "fd2", "inheritable", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02004549 static _PyArg_Parser _parser = {NULL, _keywords, "dup2", 0};
4550 PyObject *argsbuf[3];
4551 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004552 int fd;
4553 int fd2;
4554 int inheritable = 1;
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08004555 int _return_value;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004556
Serhiy Storchaka31913912019-03-14 10:32:22 +02004557 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 3, 0, argsbuf);
4558 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004559 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03004560 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02004561 if (PyFloat_Check(args[0])) {
4562 PyErr_SetString(PyExc_TypeError,
4563 "integer argument expected, got float" );
4564 goto exit;
4565 }
4566 fd = _PyLong_AsInt(args[0]);
4567 if (fd == -1 && PyErr_Occurred()) {
4568 goto exit;
4569 }
4570 if (PyFloat_Check(args[1])) {
4571 PyErr_SetString(PyExc_TypeError,
4572 "integer argument expected, got float" );
4573 goto exit;
4574 }
4575 fd2 = _PyLong_AsInt(args[1]);
4576 if (fd2 == -1 && PyErr_Occurred()) {
4577 goto exit;
4578 }
4579 if (!noptargs) {
4580 goto skip_optional_pos;
4581 }
4582 inheritable = PyObject_IsTrue(args[2]);
4583 if (inheritable < 0) {
4584 goto exit;
4585 }
4586skip_optional_pos:
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08004587 _return_value = os_dup2_impl(module, fd, fd2, inheritable);
4588 if ((_return_value == -1) && PyErr_Occurred()) {
4589 goto exit;
4590 }
4591 return_value = PyLong_FromLong((long)_return_value);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004592
4593exit:
4594 return return_value;
4595}
4596
4597#if defined(HAVE_LOCKF)
4598
4599PyDoc_STRVAR(os_lockf__doc__,
4600"lockf($module, fd, command, length, /)\n"
4601"--\n"
4602"\n"
4603"Apply, test or remove a POSIX lock on an open file descriptor.\n"
4604"\n"
4605" fd\n"
4606" An open file descriptor.\n"
4607" command\n"
4608" One of F_LOCK, F_TLOCK, F_ULOCK or F_TEST.\n"
4609" length\n"
4610" The number of bytes to lock, starting at the current position.");
4611
4612#define OS_LOCKF_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02004613 {"lockf", (PyCFunction)(void(*)(void))os_lockf, METH_FASTCALL, os_lockf__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004614
4615static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004616os_lockf_impl(PyObject *module, int fd, int command, Py_off_t length);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004617
4618static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004619os_lockf(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004620{
4621 PyObject *return_value = NULL;
4622 int fd;
4623 int command;
4624 Py_off_t length;
4625
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02004626 if (!_PyArg_CheckPositional("lockf", nargs, 3, 3)) {
4627 goto exit;
4628 }
4629 if (PyFloat_Check(args[0])) {
4630 PyErr_SetString(PyExc_TypeError,
4631 "integer argument expected, got float" );
4632 goto exit;
4633 }
4634 fd = _PyLong_AsInt(args[0]);
4635 if (fd == -1 && PyErr_Occurred()) {
4636 goto exit;
4637 }
4638 if (PyFloat_Check(args[1])) {
4639 PyErr_SetString(PyExc_TypeError,
4640 "integer argument expected, got float" );
4641 goto exit;
4642 }
4643 command = _PyLong_AsInt(args[1]);
4644 if (command == -1 && PyErr_Occurred()) {
4645 goto exit;
4646 }
4647 if (!Py_off_t_converter(args[2], &length)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01004648 goto exit;
4649 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004650 return_value = os_lockf_impl(module, fd, command, length);
4651
4652exit:
4653 return return_value;
4654}
4655
4656#endif /* defined(HAVE_LOCKF) */
4657
4658PyDoc_STRVAR(os_lseek__doc__,
4659"lseek($module, fd, position, how, /)\n"
4660"--\n"
4661"\n"
4662"Set the position of a file descriptor. Return the new position.\n"
4663"\n"
4664"Return the new cursor position in number of bytes\n"
4665"relative to the beginning of the file.");
4666
4667#define OS_LSEEK_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02004668 {"lseek", (PyCFunction)(void(*)(void))os_lseek, METH_FASTCALL, os_lseek__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004669
4670static Py_off_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004671os_lseek_impl(PyObject *module, int fd, Py_off_t position, int how);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004672
4673static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004674os_lseek(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004675{
4676 PyObject *return_value = NULL;
4677 int fd;
4678 Py_off_t position;
4679 int how;
4680 Py_off_t _return_value;
4681
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02004682 if (!_PyArg_CheckPositional("lseek", nargs, 3, 3)) {
4683 goto exit;
4684 }
4685 if (PyFloat_Check(args[0])) {
4686 PyErr_SetString(PyExc_TypeError,
4687 "integer argument expected, got float" );
4688 goto exit;
4689 }
4690 fd = _PyLong_AsInt(args[0]);
4691 if (fd == -1 && PyErr_Occurred()) {
4692 goto exit;
4693 }
4694 if (!Py_off_t_converter(args[1], &position)) {
4695 goto exit;
4696 }
4697 if (PyFloat_Check(args[2])) {
4698 PyErr_SetString(PyExc_TypeError,
4699 "integer argument expected, got float" );
4700 goto exit;
4701 }
4702 how = _PyLong_AsInt(args[2]);
4703 if (how == -1 && PyErr_Occurred()) {
Victor Stinner259f0e42017-01-17 01:35:17 +01004704 goto exit;
4705 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004706 _return_value = os_lseek_impl(module, fd, position, how);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03004707 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004708 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03004709 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004710 return_value = PyLong_FromPy_off_t(_return_value);
4711
4712exit:
4713 return return_value;
4714}
4715
4716PyDoc_STRVAR(os_read__doc__,
4717"read($module, fd, length, /)\n"
4718"--\n"
4719"\n"
4720"Read from a file descriptor. Returns a bytes object.");
4721
4722#define OS_READ_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02004723 {"read", (PyCFunction)(void(*)(void))os_read, METH_FASTCALL, os_read__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004724
4725static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004726os_read_impl(PyObject *module, int fd, Py_ssize_t length);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004727
4728static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004729os_read(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004730{
4731 PyObject *return_value = NULL;
4732 int fd;
4733 Py_ssize_t length;
4734
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02004735 if (!_PyArg_CheckPositional("read", nargs, 2, 2)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01004736 goto exit;
4737 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02004738 if (PyFloat_Check(args[0])) {
4739 PyErr_SetString(PyExc_TypeError,
4740 "integer argument expected, got float" );
4741 goto exit;
4742 }
4743 fd = _PyLong_AsInt(args[0]);
4744 if (fd == -1 && PyErr_Occurred()) {
4745 goto exit;
4746 }
4747 if (PyFloat_Check(args[1])) {
4748 PyErr_SetString(PyExc_TypeError,
4749 "integer argument expected, got float" );
4750 goto exit;
4751 }
4752 {
4753 Py_ssize_t ival = -1;
4754 PyObject *iobj = PyNumber_Index(args[1]);
4755 if (iobj != NULL) {
4756 ival = PyLong_AsSsize_t(iobj);
4757 Py_DECREF(iobj);
4758 }
4759 if (ival == -1 && PyErr_Occurred()) {
4760 goto exit;
4761 }
4762 length = ival;
4763 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004764 return_value = os_read_impl(module, fd, length);
4765
4766exit:
4767 return return_value;
4768}
4769
4770#if defined(HAVE_READV)
4771
4772PyDoc_STRVAR(os_readv__doc__,
4773"readv($module, fd, buffers, /)\n"
4774"--\n"
4775"\n"
4776"Read from a file descriptor fd into an iterable of buffers.\n"
4777"\n"
4778"The buffers should be mutable buffers accepting bytes.\n"
4779"readv will transfer data into each buffer until it is full\n"
4780"and then move on to the next buffer in the sequence to hold\n"
4781"the rest of the data.\n"
4782"\n"
4783"readv returns the total number of bytes read,\n"
4784"which may be less than the total capacity of all the buffers.");
4785
4786#define OS_READV_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02004787 {"readv", (PyCFunction)(void(*)(void))os_readv, METH_FASTCALL, os_readv__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004788
4789static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004790os_readv_impl(PyObject *module, int fd, PyObject *buffers);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004791
4792static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004793os_readv(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004794{
4795 PyObject *return_value = NULL;
4796 int fd;
4797 PyObject *buffers;
4798 Py_ssize_t _return_value;
4799
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02004800 if (!_PyArg_CheckPositional("readv", nargs, 2, 2)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01004801 goto exit;
4802 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02004803 if (PyFloat_Check(args[0])) {
4804 PyErr_SetString(PyExc_TypeError,
4805 "integer argument expected, got float" );
4806 goto exit;
4807 }
4808 fd = _PyLong_AsInt(args[0]);
4809 if (fd == -1 && PyErr_Occurred()) {
4810 goto exit;
4811 }
4812 buffers = args[1];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004813 _return_value = os_readv_impl(module, fd, buffers);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03004814 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004815 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03004816 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004817 return_value = PyLong_FromSsize_t(_return_value);
4818
4819exit:
4820 return return_value;
4821}
4822
4823#endif /* defined(HAVE_READV) */
4824
4825#if defined(HAVE_PREAD)
4826
4827PyDoc_STRVAR(os_pread__doc__,
4828"pread($module, fd, length, offset, /)\n"
4829"--\n"
4830"\n"
4831"Read a number of bytes from a file descriptor starting at a particular offset.\n"
4832"\n"
4833"Read length bytes from file descriptor fd, starting at offset bytes from\n"
4834"the beginning of the file. The file offset remains unchanged.");
4835
4836#define OS_PREAD_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02004837 {"pread", (PyCFunction)(void(*)(void))os_pread, METH_FASTCALL, os_pread__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004838
4839static PyObject *
Dong-hee Naad7736f2019-09-25 14:47:04 +09004840os_pread_impl(PyObject *module, int fd, Py_ssize_t length, Py_off_t offset);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004841
4842static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004843os_pread(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004844{
4845 PyObject *return_value = NULL;
4846 int fd;
Dong-hee Naad7736f2019-09-25 14:47:04 +09004847 Py_ssize_t length;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004848 Py_off_t offset;
4849
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02004850 if (!_PyArg_CheckPositional("pread", nargs, 3, 3)) {
4851 goto exit;
4852 }
4853 if (PyFloat_Check(args[0])) {
4854 PyErr_SetString(PyExc_TypeError,
4855 "integer argument expected, got float" );
4856 goto exit;
4857 }
4858 fd = _PyLong_AsInt(args[0]);
4859 if (fd == -1 && PyErr_Occurred()) {
4860 goto exit;
4861 }
4862 if (PyFloat_Check(args[1])) {
4863 PyErr_SetString(PyExc_TypeError,
4864 "integer argument expected, got float" );
4865 goto exit;
4866 }
Dong-hee Naad7736f2019-09-25 14:47:04 +09004867 {
4868 Py_ssize_t ival = -1;
4869 PyObject *iobj = PyNumber_Index(args[1]);
4870 if (iobj != NULL) {
4871 ival = PyLong_AsSsize_t(iobj);
4872 Py_DECREF(iobj);
4873 }
4874 if (ival == -1 && PyErr_Occurred()) {
4875 goto exit;
4876 }
4877 length = ival;
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02004878 }
4879 if (!Py_off_t_converter(args[2], &offset)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01004880 goto exit;
4881 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004882 return_value = os_pread_impl(module, fd, length, offset);
4883
4884exit:
4885 return return_value;
4886}
4887
4888#endif /* defined(HAVE_PREAD) */
4889
Pablo Galindo4defba32018-01-27 16:16:37 +00004890#if (defined(HAVE_PREADV) || defined (HAVE_PREADV2))
4891
4892PyDoc_STRVAR(os_preadv__doc__,
4893"preadv($module, fd, buffers, offset, flags=0, /)\n"
4894"--\n"
4895"\n"
4896"Reads from a file descriptor into a number of mutable bytes-like objects.\n"
4897"\n"
4898"Combines the functionality of readv() and pread(). As readv(), it will\n"
4899"transfer data into each buffer until it is full and then move on to the next\n"
4900"buffer in the sequence to hold the rest of the data. Its fourth argument,\n"
4901"specifies the file offset at which the input operation is to be performed. It\n"
4902"will return the total number of bytes read (which can be less than the total\n"
4903"capacity of all the objects).\n"
4904"\n"
4905"The flags argument contains a bitwise OR of zero or more of the following flags:\n"
4906"\n"
4907"- RWF_HIPRI\n"
4908"- RWF_NOWAIT\n"
4909"\n"
4910"Using non-zero flags requires Linux 4.6 or newer.");
4911
4912#define OS_PREADV_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02004913 {"preadv", (PyCFunction)(void(*)(void))os_preadv, METH_FASTCALL, os_preadv__doc__},
Pablo Galindo4defba32018-01-27 16:16:37 +00004914
4915static Py_ssize_t
4916os_preadv_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset,
4917 int flags);
4918
4919static PyObject *
4920os_preadv(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
4921{
4922 PyObject *return_value = NULL;
4923 int fd;
4924 PyObject *buffers;
4925 Py_off_t offset;
4926 int flags = 0;
4927 Py_ssize_t _return_value;
4928
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02004929 if (!_PyArg_CheckPositional("preadv", nargs, 3, 4)) {
Pablo Galindo4defba32018-01-27 16:16:37 +00004930 goto exit;
4931 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02004932 if (PyFloat_Check(args[0])) {
4933 PyErr_SetString(PyExc_TypeError,
4934 "integer argument expected, got float" );
4935 goto exit;
4936 }
4937 fd = _PyLong_AsInt(args[0]);
4938 if (fd == -1 && PyErr_Occurred()) {
4939 goto exit;
4940 }
4941 buffers = args[1];
4942 if (!Py_off_t_converter(args[2], &offset)) {
4943 goto exit;
4944 }
4945 if (nargs < 4) {
4946 goto skip_optional;
4947 }
4948 if (PyFloat_Check(args[3])) {
4949 PyErr_SetString(PyExc_TypeError,
4950 "integer argument expected, got float" );
4951 goto exit;
4952 }
4953 flags = _PyLong_AsInt(args[3]);
4954 if (flags == -1 && PyErr_Occurred()) {
4955 goto exit;
4956 }
4957skip_optional:
Pablo Galindo4defba32018-01-27 16:16:37 +00004958 _return_value = os_preadv_impl(module, fd, buffers, offset, flags);
4959 if ((_return_value == -1) && PyErr_Occurred()) {
4960 goto exit;
4961 }
4962 return_value = PyLong_FromSsize_t(_return_value);
4963
4964exit:
4965 return return_value;
4966}
4967
4968#endif /* (defined(HAVE_PREADV) || defined (HAVE_PREADV2)) */
4969
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004970PyDoc_STRVAR(os_write__doc__,
4971"write($module, fd, data, /)\n"
4972"--\n"
4973"\n"
4974"Write a bytes object to a file descriptor.");
4975
4976#define OS_WRITE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02004977 {"write", (PyCFunction)(void(*)(void))os_write, METH_FASTCALL, os_write__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004978
4979static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004980os_write_impl(PyObject *module, int fd, Py_buffer *data);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004981
4982static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004983os_write(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004984{
4985 PyObject *return_value = NULL;
4986 int fd;
4987 Py_buffer data = {NULL, NULL};
4988 Py_ssize_t _return_value;
4989
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02004990 if (!_PyArg_CheckPositional("write", nargs, 2, 2)) {
4991 goto exit;
4992 }
4993 if (PyFloat_Check(args[0])) {
4994 PyErr_SetString(PyExc_TypeError,
4995 "integer argument expected, got float" );
4996 goto exit;
4997 }
4998 fd = _PyLong_AsInt(args[0]);
4999 if (fd == -1 && PyErr_Occurred()) {
5000 goto exit;
5001 }
5002 if (PyObject_GetBuffer(args[1], &data, PyBUF_SIMPLE) != 0) {
5003 goto exit;
5004 }
5005 if (!PyBuffer_IsContiguous(&data, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02005006 _PyArg_BadArgument("write", "argument 2", "contiguous buffer", args[1]);
Victor Stinner259f0e42017-01-17 01:35:17 +01005007 goto exit;
5008 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005009 _return_value = os_write_impl(module, fd, &data);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005010 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005011 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005012 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005013 return_value = PyLong_FromSsize_t(_return_value);
5014
5015exit:
5016 /* Cleanup for data */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005017 if (data.obj) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005018 PyBuffer_Release(&data);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005019 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005020
5021 return return_value;
5022}
5023
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02005024#if defined(__APPLE__)
5025
5026PyDoc_STRVAR(os__fcopyfile__doc__,
Serhiy Storchaka140a7d12019-10-13 11:59:31 +03005027"_fcopyfile($module, in_fd, out_fd, flags, /)\n"
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02005028"--\n"
5029"\n"
Giampaolo Rodolac7f02a92018-06-19 08:27:29 -07005030"Efficiently copy content or metadata of 2 regular file descriptors (macOS).");
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02005031
5032#define OS__FCOPYFILE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02005033 {"_fcopyfile", (PyCFunction)(void(*)(void))os__fcopyfile, METH_FASTCALL, os__fcopyfile__doc__},
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02005034
5035static PyObject *
Serhiy Storchaka140a7d12019-10-13 11:59:31 +03005036os__fcopyfile_impl(PyObject *module, int in_fd, int out_fd, int flags);
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02005037
5038static PyObject *
5039os__fcopyfile(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
5040{
5041 PyObject *return_value = NULL;
Serhiy Storchaka140a7d12019-10-13 11:59:31 +03005042 int in_fd;
5043 int out_fd;
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02005044 int flags;
5045
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02005046 if (!_PyArg_CheckPositional("_fcopyfile", nargs, 3, 3)) {
5047 goto exit;
5048 }
5049 if (PyFloat_Check(args[0])) {
5050 PyErr_SetString(PyExc_TypeError,
5051 "integer argument expected, got float" );
5052 goto exit;
5053 }
Serhiy Storchaka140a7d12019-10-13 11:59:31 +03005054 in_fd = _PyLong_AsInt(args[0]);
5055 if (in_fd == -1 && PyErr_Occurred()) {
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02005056 goto exit;
5057 }
5058 if (PyFloat_Check(args[1])) {
5059 PyErr_SetString(PyExc_TypeError,
5060 "integer argument expected, got float" );
5061 goto exit;
5062 }
Serhiy Storchaka140a7d12019-10-13 11:59:31 +03005063 out_fd = _PyLong_AsInt(args[1]);
5064 if (out_fd == -1 && PyErr_Occurred()) {
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02005065 goto exit;
5066 }
5067 if (PyFloat_Check(args[2])) {
5068 PyErr_SetString(PyExc_TypeError,
5069 "integer argument expected, got float" );
5070 goto exit;
5071 }
5072 flags = _PyLong_AsInt(args[2]);
5073 if (flags == -1 && PyErr_Occurred()) {
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02005074 goto exit;
5075 }
Serhiy Storchaka140a7d12019-10-13 11:59:31 +03005076 return_value = os__fcopyfile_impl(module, in_fd, out_fd, flags);
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02005077
5078exit:
5079 return return_value;
5080}
5081
5082#endif /* defined(__APPLE__) */
5083
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005084PyDoc_STRVAR(os_fstat__doc__,
5085"fstat($module, /, fd)\n"
5086"--\n"
5087"\n"
5088"Perform a stat system call on the given file descriptor.\n"
5089"\n"
5090"Like stat(), but for an open file descriptor.\n"
5091"Equivalent to os.stat(fd).");
5092
5093#define OS_FSTAT_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02005094 {"fstat", (PyCFunction)(void(*)(void))os_fstat, METH_FASTCALL|METH_KEYWORDS, os_fstat__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005095
5096static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005097os_fstat_impl(PyObject *module, int fd);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005098
5099static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02005100os_fstat(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005101{
5102 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03005103 static const char * const _keywords[] = {"fd", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02005104 static _PyArg_Parser _parser = {NULL, _keywords, "fstat", 0};
5105 PyObject *argsbuf[1];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005106 int fd;
5107
Serhiy Storchaka31913912019-03-14 10:32:22 +02005108 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
5109 if (!args) {
5110 goto exit;
5111 }
5112 if (PyFloat_Check(args[0])) {
5113 PyErr_SetString(PyExc_TypeError,
5114 "integer argument expected, got float" );
5115 goto exit;
5116 }
5117 fd = _PyLong_AsInt(args[0]);
5118 if (fd == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005119 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005120 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005121 return_value = os_fstat_impl(module, fd);
5122
5123exit:
5124 return return_value;
5125}
5126
5127PyDoc_STRVAR(os_isatty__doc__,
5128"isatty($module, fd, /)\n"
5129"--\n"
5130"\n"
5131"Return True if the fd is connected to a terminal.\n"
5132"\n"
5133"Return True if the file descriptor is an open file descriptor\n"
5134"connected to the slave end of a terminal.");
5135
5136#define OS_ISATTY_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03005137 {"isatty", (PyCFunction)os_isatty, METH_O, os_isatty__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005138
5139static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005140os_isatty_impl(PyObject *module, int fd);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005141
5142static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005143os_isatty(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005144{
5145 PyObject *return_value = NULL;
5146 int fd;
5147 int _return_value;
5148
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02005149 if (PyFloat_Check(arg)) {
5150 PyErr_SetString(PyExc_TypeError,
5151 "integer argument expected, got float" );
5152 goto exit;
5153 }
5154 fd = _PyLong_AsInt(arg);
5155 if (fd == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005156 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005157 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005158 _return_value = os_isatty_impl(module, fd);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005159 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005160 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005161 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005162 return_value = PyBool_FromLong((long)_return_value);
5163
5164exit:
5165 return return_value;
5166}
5167
5168#if defined(HAVE_PIPE)
5169
5170PyDoc_STRVAR(os_pipe__doc__,
5171"pipe($module, /)\n"
5172"--\n"
5173"\n"
5174"Create a pipe.\n"
5175"\n"
5176"Returns a tuple of two file descriptors:\n"
5177" (read_fd, write_fd)");
5178
5179#define OS_PIPE_METHODDEF \
5180 {"pipe", (PyCFunction)os_pipe, METH_NOARGS, os_pipe__doc__},
5181
5182static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005183os_pipe_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005184
5185static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005186os_pipe(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005187{
5188 return os_pipe_impl(module);
5189}
5190
5191#endif /* defined(HAVE_PIPE) */
5192
5193#if defined(HAVE_PIPE2)
5194
5195PyDoc_STRVAR(os_pipe2__doc__,
5196"pipe2($module, flags, /)\n"
5197"--\n"
5198"\n"
5199"Create a pipe with flags set atomically.\n"
5200"\n"
5201"Returns a tuple of two file descriptors:\n"
5202" (read_fd, write_fd)\n"
5203"\n"
5204"flags can be constructed by ORing together one or more of these values:\n"
5205"O_NONBLOCK, O_CLOEXEC.");
5206
5207#define OS_PIPE2_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03005208 {"pipe2", (PyCFunction)os_pipe2, METH_O, os_pipe2__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005209
5210static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005211os_pipe2_impl(PyObject *module, int flags);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005212
5213static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005214os_pipe2(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005215{
5216 PyObject *return_value = NULL;
5217 int flags;
5218
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02005219 if (PyFloat_Check(arg)) {
5220 PyErr_SetString(PyExc_TypeError,
5221 "integer argument expected, got float" );
5222 goto exit;
5223 }
5224 flags = _PyLong_AsInt(arg);
5225 if (flags == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005226 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005227 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005228 return_value = os_pipe2_impl(module, flags);
5229
5230exit:
5231 return return_value;
5232}
5233
5234#endif /* defined(HAVE_PIPE2) */
5235
5236#if defined(HAVE_WRITEV)
5237
5238PyDoc_STRVAR(os_writev__doc__,
5239"writev($module, fd, buffers, /)\n"
5240"--\n"
5241"\n"
5242"Iterate over buffers, and write the contents of each to a file descriptor.\n"
5243"\n"
5244"Returns the total number of bytes written.\n"
5245"buffers must be a sequence of bytes-like objects.");
5246
5247#define OS_WRITEV_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02005248 {"writev", (PyCFunction)(void(*)(void))os_writev, METH_FASTCALL, os_writev__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005249
5250static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005251os_writev_impl(PyObject *module, int fd, PyObject *buffers);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005252
5253static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02005254os_writev(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005255{
5256 PyObject *return_value = NULL;
5257 int fd;
5258 PyObject *buffers;
5259 Py_ssize_t _return_value;
5260
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02005261 if (!_PyArg_CheckPositional("writev", nargs, 2, 2)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01005262 goto exit;
5263 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02005264 if (PyFloat_Check(args[0])) {
5265 PyErr_SetString(PyExc_TypeError,
5266 "integer argument expected, got float" );
5267 goto exit;
5268 }
5269 fd = _PyLong_AsInt(args[0]);
5270 if (fd == -1 && PyErr_Occurred()) {
5271 goto exit;
5272 }
5273 buffers = args[1];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005274 _return_value = os_writev_impl(module, fd, buffers);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005275 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005276 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005277 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005278 return_value = PyLong_FromSsize_t(_return_value);
5279
5280exit:
5281 return return_value;
5282}
5283
5284#endif /* defined(HAVE_WRITEV) */
5285
5286#if defined(HAVE_PWRITE)
5287
5288PyDoc_STRVAR(os_pwrite__doc__,
5289"pwrite($module, fd, buffer, offset, /)\n"
5290"--\n"
5291"\n"
5292"Write bytes to a file descriptor starting at a particular offset.\n"
5293"\n"
5294"Write buffer to fd, starting at offset bytes from the beginning of\n"
5295"the file. Returns the number of bytes writte. Does not change the\n"
5296"current file offset.");
5297
5298#define OS_PWRITE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02005299 {"pwrite", (PyCFunction)(void(*)(void))os_pwrite, METH_FASTCALL, os_pwrite__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005300
5301static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005302os_pwrite_impl(PyObject *module, int fd, Py_buffer *buffer, Py_off_t offset);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005303
5304static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02005305os_pwrite(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005306{
5307 PyObject *return_value = NULL;
5308 int fd;
5309 Py_buffer buffer = {NULL, NULL};
5310 Py_off_t offset;
5311 Py_ssize_t _return_value;
5312
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02005313 if (!_PyArg_CheckPositional("pwrite", nargs, 3, 3)) {
5314 goto exit;
5315 }
5316 if (PyFloat_Check(args[0])) {
5317 PyErr_SetString(PyExc_TypeError,
5318 "integer argument expected, got float" );
5319 goto exit;
5320 }
5321 fd = _PyLong_AsInt(args[0]);
5322 if (fd == -1 && PyErr_Occurred()) {
5323 goto exit;
5324 }
5325 if (PyObject_GetBuffer(args[1], &buffer, PyBUF_SIMPLE) != 0) {
5326 goto exit;
5327 }
5328 if (!PyBuffer_IsContiguous(&buffer, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02005329 _PyArg_BadArgument("pwrite", "argument 2", "contiguous buffer", args[1]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02005330 goto exit;
5331 }
5332 if (!Py_off_t_converter(args[2], &offset)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01005333 goto exit;
5334 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005335 _return_value = os_pwrite_impl(module, fd, &buffer, offset);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005336 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005337 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005338 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005339 return_value = PyLong_FromSsize_t(_return_value);
5340
5341exit:
5342 /* Cleanup for buffer */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005343 if (buffer.obj) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005344 PyBuffer_Release(&buffer);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005345 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005346
5347 return return_value;
5348}
5349
5350#endif /* defined(HAVE_PWRITE) */
5351
Pablo Galindo4defba32018-01-27 16:16:37 +00005352#if (defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2))
5353
5354PyDoc_STRVAR(os_pwritev__doc__,
5355"pwritev($module, fd, buffers, offset, flags=0, /)\n"
5356"--\n"
5357"\n"
5358"Writes the contents of bytes-like objects to a file descriptor at a given offset.\n"
5359"\n"
5360"Combines the functionality of writev() and pwrite(). All buffers must be a sequence\n"
5361"of bytes-like objects. Buffers are processed in array order. Entire contents of first\n"
5362"buffer is written before proceeding to second, and so on. The operating system may\n"
5363"set a limit (sysconf() value SC_IOV_MAX) on the number of buffers that can be used.\n"
5364"This function writes the contents of each object to the file descriptor and returns\n"
5365"the total number of bytes written.\n"
5366"\n"
5367"The flags argument contains a bitwise OR of zero or more of the following flags:\n"
5368"\n"
5369"- RWF_DSYNC\n"
5370"- RWF_SYNC\n"
5371"\n"
5372"Using non-zero flags requires Linux 4.7 or newer.");
5373
5374#define OS_PWRITEV_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02005375 {"pwritev", (PyCFunction)(void(*)(void))os_pwritev, METH_FASTCALL, os_pwritev__doc__},
Pablo Galindo4defba32018-01-27 16:16:37 +00005376
5377static Py_ssize_t
5378os_pwritev_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset,
5379 int flags);
5380
5381static PyObject *
5382os_pwritev(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
5383{
5384 PyObject *return_value = NULL;
5385 int fd;
5386 PyObject *buffers;
5387 Py_off_t offset;
5388 int flags = 0;
5389 Py_ssize_t _return_value;
5390
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02005391 if (!_PyArg_CheckPositional("pwritev", nargs, 3, 4)) {
Pablo Galindo4defba32018-01-27 16:16:37 +00005392 goto exit;
5393 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02005394 if (PyFloat_Check(args[0])) {
5395 PyErr_SetString(PyExc_TypeError,
5396 "integer argument expected, got float" );
5397 goto exit;
5398 }
5399 fd = _PyLong_AsInt(args[0]);
5400 if (fd == -1 && PyErr_Occurred()) {
5401 goto exit;
5402 }
5403 buffers = args[1];
5404 if (!Py_off_t_converter(args[2], &offset)) {
5405 goto exit;
5406 }
5407 if (nargs < 4) {
5408 goto skip_optional;
5409 }
5410 if (PyFloat_Check(args[3])) {
5411 PyErr_SetString(PyExc_TypeError,
5412 "integer argument expected, got float" );
5413 goto exit;
5414 }
5415 flags = _PyLong_AsInt(args[3]);
5416 if (flags == -1 && PyErr_Occurred()) {
5417 goto exit;
5418 }
5419skip_optional:
Pablo Galindo4defba32018-01-27 16:16:37 +00005420 _return_value = os_pwritev_impl(module, fd, buffers, offset, flags);
5421 if ((_return_value == -1) && PyErr_Occurred()) {
5422 goto exit;
5423 }
5424 return_value = PyLong_FromSsize_t(_return_value);
5425
5426exit:
5427 return return_value;
5428}
5429
5430#endif /* (defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2)) */
5431
Pablo Galindoaac4d032019-05-31 19:39:47 +01005432#if defined(HAVE_COPY_FILE_RANGE)
5433
5434PyDoc_STRVAR(os_copy_file_range__doc__,
5435"copy_file_range($module, /, src, dst, count, offset_src=None,\n"
5436" offset_dst=None)\n"
5437"--\n"
5438"\n"
5439"Copy count bytes from one file descriptor to another.\n"
5440"\n"
5441" src\n"
5442" Source file descriptor.\n"
5443" dst\n"
5444" Destination file descriptor.\n"
5445" count\n"
5446" Number of bytes to copy.\n"
5447" offset_src\n"
5448" Starting offset in src.\n"
5449" offset_dst\n"
5450" Starting offset in dst.\n"
5451"\n"
5452"If offset_src is None, then src is read from the current position;\n"
5453"respectively for offset_dst.");
5454
5455#define OS_COPY_FILE_RANGE_METHODDEF \
5456 {"copy_file_range", (PyCFunction)(void(*)(void))os_copy_file_range, METH_FASTCALL|METH_KEYWORDS, os_copy_file_range__doc__},
5457
5458static PyObject *
5459os_copy_file_range_impl(PyObject *module, int src, int dst, Py_ssize_t count,
5460 PyObject *offset_src, PyObject *offset_dst);
5461
5462static PyObject *
5463os_copy_file_range(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
5464{
5465 PyObject *return_value = NULL;
5466 static const char * const _keywords[] = {"src", "dst", "count", "offset_src", "offset_dst", NULL};
5467 static _PyArg_Parser _parser = {NULL, _keywords, "copy_file_range", 0};
5468 PyObject *argsbuf[5];
5469 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 3;
5470 int src;
5471 int dst;
5472 Py_ssize_t count;
5473 PyObject *offset_src = Py_None;
5474 PyObject *offset_dst = Py_None;
5475
5476 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 3, 5, 0, argsbuf);
5477 if (!args) {
5478 goto exit;
5479 }
5480 if (PyFloat_Check(args[0])) {
5481 PyErr_SetString(PyExc_TypeError,
5482 "integer argument expected, got float" );
5483 goto exit;
5484 }
5485 src = _PyLong_AsInt(args[0]);
5486 if (src == -1 && PyErr_Occurred()) {
5487 goto exit;
5488 }
5489 if (PyFloat_Check(args[1])) {
5490 PyErr_SetString(PyExc_TypeError,
5491 "integer argument expected, got float" );
5492 goto exit;
5493 }
5494 dst = _PyLong_AsInt(args[1]);
5495 if (dst == -1 && PyErr_Occurred()) {
5496 goto exit;
5497 }
5498 if (PyFloat_Check(args[2])) {
5499 PyErr_SetString(PyExc_TypeError,
5500 "integer argument expected, got float" );
5501 goto exit;
5502 }
5503 {
5504 Py_ssize_t ival = -1;
5505 PyObject *iobj = PyNumber_Index(args[2]);
5506 if (iobj != NULL) {
5507 ival = PyLong_AsSsize_t(iobj);
5508 Py_DECREF(iobj);
5509 }
5510 if (ival == -1 && PyErr_Occurred()) {
5511 goto exit;
5512 }
5513 count = ival;
5514 }
5515 if (!noptargs) {
5516 goto skip_optional_pos;
5517 }
5518 if (args[3]) {
5519 offset_src = args[3];
5520 if (!--noptargs) {
5521 goto skip_optional_pos;
5522 }
5523 }
5524 offset_dst = args[4];
5525skip_optional_pos:
5526 return_value = os_copy_file_range_impl(module, src, dst, count, offset_src, offset_dst);
5527
5528exit:
5529 return return_value;
5530}
5531
5532#endif /* defined(HAVE_COPY_FILE_RANGE) */
5533
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005534#if defined(HAVE_MKFIFO)
5535
5536PyDoc_STRVAR(os_mkfifo__doc__,
5537"mkfifo($module, /, path, mode=438, *, dir_fd=None)\n"
5538"--\n"
5539"\n"
5540"Create a \"fifo\" (a POSIX named pipe).\n"
5541"\n"
5542"If dir_fd is not None, it should be a file descriptor open to a directory,\n"
5543" and path should be relative; path will then be relative to that directory.\n"
5544"dir_fd may not be implemented on your platform.\n"
5545" If it is unavailable, using it will raise a NotImplementedError.");
5546
5547#define OS_MKFIFO_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02005548 {"mkfifo", (PyCFunction)(void(*)(void))os_mkfifo, METH_FASTCALL|METH_KEYWORDS, os_mkfifo__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005549
5550static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005551os_mkfifo_impl(PyObject *module, path_t *path, int mode, int dir_fd);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005552
5553static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02005554os_mkfifo(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005555{
5556 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03005557 static const char * const _keywords[] = {"path", "mode", "dir_fd", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02005558 static _PyArg_Parser _parser = {NULL, _keywords, "mkfifo", 0};
5559 PyObject *argsbuf[3];
5560 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005561 path_t path = PATH_T_INITIALIZE("mkfifo", "path", 0, 0);
5562 int mode = 438;
5563 int dir_fd = DEFAULT_DIR_FD;
5564
Serhiy Storchaka31913912019-03-14 10:32:22 +02005565 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
5566 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005567 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005568 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02005569 if (!path_converter(args[0], &path)) {
5570 goto exit;
5571 }
5572 if (!noptargs) {
5573 goto skip_optional_pos;
5574 }
5575 if (args[1]) {
5576 if (PyFloat_Check(args[1])) {
5577 PyErr_SetString(PyExc_TypeError,
5578 "integer argument expected, got float" );
5579 goto exit;
5580 }
5581 mode = _PyLong_AsInt(args[1]);
5582 if (mode == -1 && PyErr_Occurred()) {
5583 goto exit;
5584 }
5585 if (!--noptargs) {
5586 goto skip_optional_pos;
5587 }
5588 }
5589skip_optional_pos:
5590 if (!noptargs) {
5591 goto skip_optional_kwonly;
5592 }
5593 if (!MKFIFOAT_DIR_FD_CONVERTER(args[2], &dir_fd)) {
5594 goto exit;
5595 }
5596skip_optional_kwonly:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005597 return_value = os_mkfifo_impl(module, &path, mode, dir_fd);
5598
5599exit:
5600 /* Cleanup for path */
5601 path_cleanup(&path);
5602
5603 return return_value;
5604}
5605
5606#endif /* defined(HAVE_MKFIFO) */
5607
5608#if (defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV))
5609
5610PyDoc_STRVAR(os_mknod__doc__,
5611"mknod($module, /, path, mode=384, device=0, *, dir_fd=None)\n"
5612"--\n"
5613"\n"
5614"Create a node in the file system.\n"
5615"\n"
5616"Create a node in the file system (file, device special file or named pipe)\n"
5617"at path. mode specifies both the permissions to use and the\n"
5618"type of node to be created, being combined (bitwise OR) with one of\n"
5619"S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. If S_IFCHR or S_IFBLK is set on mode,\n"
5620"device defines the newly created device special file (probably using\n"
5621"os.makedev()). Otherwise device is ignored.\n"
5622"\n"
5623"If dir_fd is not None, it should be a file descriptor open to a directory,\n"
5624" and path should be relative; path will then be relative to that directory.\n"
5625"dir_fd may not be implemented on your platform.\n"
5626" If it is unavailable, using it will raise a NotImplementedError.");
5627
5628#define OS_MKNOD_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02005629 {"mknod", (PyCFunction)(void(*)(void))os_mknod, METH_FASTCALL|METH_KEYWORDS, os_mknod__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005630
5631static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005632os_mknod_impl(PyObject *module, path_t *path, int mode, dev_t device,
Larry Hastings89964c42015-04-14 18:07:59 -04005633 int dir_fd);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005634
5635static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02005636os_mknod(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005637{
5638 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03005639 static const char * const _keywords[] = {"path", "mode", "device", "dir_fd", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02005640 static _PyArg_Parser _parser = {NULL, _keywords, "mknod", 0};
5641 PyObject *argsbuf[4];
5642 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005643 path_t path = PATH_T_INITIALIZE("mknod", "path", 0, 0);
5644 int mode = 384;
5645 dev_t device = 0;
5646 int dir_fd = DEFAULT_DIR_FD;
5647
Serhiy Storchaka31913912019-03-14 10:32:22 +02005648 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 3, 0, argsbuf);
5649 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005650 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005651 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02005652 if (!path_converter(args[0], &path)) {
5653 goto exit;
5654 }
5655 if (!noptargs) {
5656 goto skip_optional_pos;
5657 }
5658 if (args[1]) {
5659 if (PyFloat_Check(args[1])) {
5660 PyErr_SetString(PyExc_TypeError,
5661 "integer argument expected, got float" );
5662 goto exit;
5663 }
5664 mode = _PyLong_AsInt(args[1]);
5665 if (mode == -1 && PyErr_Occurred()) {
5666 goto exit;
5667 }
5668 if (!--noptargs) {
5669 goto skip_optional_pos;
5670 }
5671 }
5672 if (args[2]) {
5673 if (!_Py_Dev_Converter(args[2], &device)) {
5674 goto exit;
5675 }
5676 if (!--noptargs) {
5677 goto skip_optional_pos;
5678 }
5679 }
5680skip_optional_pos:
5681 if (!noptargs) {
5682 goto skip_optional_kwonly;
5683 }
5684 if (!MKNODAT_DIR_FD_CONVERTER(args[3], &dir_fd)) {
5685 goto exit;
5686 }
5687skip_optional_kwonly:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005688 return_value = os_mknod_impl(module, &path, mode, device, dir_fd);
5689
5690exit:
5691 /* Cleanup for path */
5692 path_cleanup(&path);
5693
5694 return return_value;
5695}
5696
5697#endif /* (defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)) */
5698
5699#if defined(HAVE_DEVICE_MACROS)
5700
5701PyDoc_STRVAR(os_major__doc__,
5702"major($module, device, /)\n"
5703"--\n"
5704"\n"
5705"Extracts a device major number from a raw device number.");
5706
5707#define OS_MAJOR_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03005708 {"major", (PyCFunction)os_major, METH_O, os_major__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005709
5710static unsigned int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005711os_major_impl(PyObject *module, dev_t device);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005712
5713static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005714os_major(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005715{
5716 PyObject *return_value = NULL;
5717 dev_t device;
5718 unsigned int _return_value;
5719
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02005720 if (!_Py_Dev_Converter(arg, &device)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005721 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005722 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005723 _return_value = os_major_impl(module, device);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005724 if ((_return_value == (unsigned int)-1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005725 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005726 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005727 return_value = PyLong_FromUnsignedLong((unsigned long)_return_value);
5728
5729exit:
5730 return return_value;
5731}
5732
5733#endif /* defined(HAVE_DEVICE_MACROS) */
5734
5735#if defined(HAVE_DEVICE_MACROS)
5736
5737PyDoc_STRVAR(os_minor__doc__,
5738"minor($module, device, /)\n"
5739"--\n"
5740"\n"
5741"Extracts a device minor number from a raw device number.");
5742
5743#define OS_MINOR_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03005744 {"minor", (PyCFunction)os_minor, METH_O, os_minor__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005745
5746static unsigned int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005747os_minor_impl(PyObject *module, dev_t device);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005748
5749static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005750os_minor(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005751{
5752 PyObject *return_value = NULL;
5753 dev_t device;
5754 unsigned int _return_value;
5755
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02005756 if (!_Py_Dev_Converter(arg, &device)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005757 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005758 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005759 _return_value = os_minor_impl(module, device);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005760 if ((_return_value == (unsigned int)-1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005761 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005762 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005763 return_value = PyLong_FromUnsignedLong((unsigned long)_return_value);
5764
5765exit:
5766 return return_value;
5767}
5768
5769#endif /* defined(HAVE_DEVICE_MACROS) */
5770
5771#if defined(HAVE_DEVICE_MACROS)
5772
5773PyDoc_STRVAR(os_makedev__doc__,
5774"makedev($module, major, minor, /)\n"
5775"--\n"
5776"\n"
5777"Composes a raw device number from the major and minor device numbers.");
5778
5779#define OS_MAKEDEV_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02005780 {"makedev", (PyCFunction)(void(*)(void))os_makedev, METH_FASTCALL, os_makedev__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005781
5782static dev_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005783os_makedev_impl(PyObject *module, int major, int minor);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005784
5785static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02005786os_makedev(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005787{
5788 PyObject *return_value = NULL;
5789 int major;
5790 int minor;
5791 dev_t _return_value;
5792
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02005793 if (!_PyArg_CheckPositional("makedev", nargs, 2, 2)) {
5794 goto exit;
5795 }
5796 if (PyFloat_Check(args[0])) {
5797 PyErr_SetString(PyExc_TypeError,
5798 "integer argument expected, got float" );
5799 goto exit;
5800 }
5801 major = _PyLong_AsInt(args[0]);
5802 if (major == -1 && PyErr_Occurred()) {
5803 goto exit;
5804 }
5805 if (PyFloat_Check(args[1])) {
5806 PyErr_SetString(PyExc_TypeError,
5807 "integer argument expected, got float" );
5808 goto exit;
5809 }
5810 minor = _PyLong_AsInt(args[1]);
5811 if (minor == -1 && PyErr_Occurred()) {
Victor Stinner259f0e42017-01-17 01:35:17 +01005812 goto exit;
5813 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005814 _return_value = os_makedev_impl(module, major, minor);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005815 if ((_return_value == (dev_t)-1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005816 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005817 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005818 return_value = _PyLong_FromDev(_return_value);
5819
5820exit:
5821 return return_value;
5822}
5823
5824#endif /* defined(HAVE_DEVICE_MACROS) */
5825
Steve Dowerf7377032015-04-12 15:44:54 -04005826#if (defined HAVE_FTRUNCATE || defined MS_WINDOWS)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005827
5828PyDoc_STRVAR(os_ftruncate__doc__,
5829"ftruncate($module, fd, length, /)\n"
5830"--\n"
5831"\n"
5832"Truncate a file, specified by file descriptor, to a specific length.");
5833
5834#define OS_FTRUNCATE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02005835 {"ftruncate", (PyCFunction)(void(*)(void))os_ftruncate, METH_FASTCALL, os_ftruncate__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005836
5837static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005838os_ftruncate_impl(PyObject *module, int fd, Py_off_t length);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005839
5840static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02005841os_ftruncate(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005842{
5843 PyObject *return_value = NULL;
5844 int fd;
5845 Py_off_t length;
5846
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02005847 if (!_PyArg_CheckPositional("ftruncate", nargs, 2, 2)) {
5848 goto exit;
5849 }
5850 if (PyFloat_Check(args[0])) {
5851 PyErr_SetString(PyExc_TypeError,
5852 "integer argument expected, got float" );
5853 goto exit;
5854 }
5855 fd = _PyLong_AsInt(args[0]);
5856 if (fd == -1 && PyErr_Occurred()) {
5857 goto exit;
5858 }
5859 if (!Py_off_t_converter(args[1], &length)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01005860 goto exit;
5861 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005862 return_value = os_ftruncate_impl(module, fd, length);
5863
5864exit:
5865 return return_value;
5866}
5867
Steve Dowerf7377032015-04-12 15:44:54 -04005868#endif /* (defined HAVE_FTRUNCATE || defined MS_WINDOWS) */
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005869
Steve Dowerf7377032015-04-12 15:44:54 -04005870#if (defined HAVE_TRUNCATE || defined MS_WINDOWS)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005871
5872PyDoc_STRVAR(os_truncate__doc__,
5873"truncate($module, /, path, length)\n"
5874"--\n"
5875"\n"
5876"Truncate a file, specified by path, to a specific length.\n"
5877"\n"
5878"On some platforms, path may also be specified as an open file descriptor.\n"
5879" If this functionality is unavailable, using it raises an exception.");
5880
5881#define OS_TRUNCATE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02005882 {"truncate", (PyCFunction)(void(*)(void))os_truncate, METH_FASTCALL|METH_KEYWORDS, os_truncate__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005883
5884static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005885os_truncate_impl(PyObject *module, path_t *path, Py_off_t length);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005886
5887static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02005888os_truncate(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005889{
5890 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03005891 static const char * const _keywords[] = {"path", "length", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02005892 static _PyArg_Parser _parser = {NULL, _keywords, "truncate", 0};
5893 PyObject *argsbuf[2];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005894 path_t path = PATH_T_INITIALIZE("truncate", "path", 0, PATH_HAVE_FTRUNCATE);
5895 Py_off_t length;
5896
Serhiy Storchaka31913912019-03-14 10:32:22 +02005897 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf);
5898 if (!args) {
5899 goto exit;
5900 }
5901 if (!path_converter(args[0], &path)) {
5902 goto exit;
5903 }
5904 if (!Py_off_t_converter(args[1], &length)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005905 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005906 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005907 return_value = os_truncate_impl(module, &path, length);
5908
5909exit:
5910 /* Cleanup for path */
5911 path_cleanup(&path);
5912
5913 return return_value;
5914}
5915
Steve Dowerf7377032015-04-12 15:44:54 -04005916#endif /* (defined HAVE_TRUNCATE || defined MS_WINDOWS) */
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005917
5918#if (defined(HAVE_POSIX_FALLOCATE) && !defined(POSIX_FADVISE_AIX_BUG))
5919
5920PyDoc_STRVAR(os_posix_fallocate__doc__,
5921"posix_fallocate($module, fd, offset, length, /)\n"
5922"--\n"
5923"\n"
5924"Ensure a file has allocated at least a particular number of bytes on disk.\n"
5925"\n"
5926"Ensure that the file specified by fd encompasses a range of bytes\n"
5927"starting at offset bytes from the beginning and continuing for length bytes.");
5928
5929#define OS_POSIX_FALLOCATE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02005930 {"posix_fallocate", (PyCFunction)(void(*)(void))os_posix_fallocate, METH_FASTCALL, os_posix_fallocate__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005931
5932static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005933os_posix_fallocate_impl(PyObject *module, int fd, Py_off_t offset,
Larry Hastings89964c42015-04-14 18:07:59 -04005934 Py_off_t length);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005935
5936static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02005937os_posix_fallocate(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005938{
5939 PyObject *return_value = NULL;
5940 int fd;
5941 Py_off_t offset;
5942 Py_off_t length;
5943
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02005944 if (!_PyArg_CheckPositional("posix_fallocate", nargs, 3, 3)) {
5945 goto exit;
5946 }
5947 if (PyFloat_Check(args[0])) {
5948 PyErr_SetString(PyExc_TypeError,
5949 "integer argument expected, got float" );
5950 goto exit;
5951 }
5952 fd = _PyLong_AsInt(args[0]);
5953 if (fd == -1 && PyErr_Occurred()) {
5954 goto exit;
5955 }
5956 if (!Py_off_t_converter(args[1], &offset)) {
5957 goto exit;
5958 }
5959 if (!Py_off_t_converter(args[2], &length)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01005960 goto exit;
5961 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005962 return_value = os_posix_fallocate_impl(module, fd, offset, length);
5963
5964exit:
5965 return return_value;
5966}
5967
5968#endif /* (defined(HAVE_POSIX_FALLOCATE) && !defined(POSIX_FADVISE_AIX_BUG)) */
5969
5970#if (defined(HAVE_POSIX_FADVISE) && !defined(POSIX_FADVISE_AIX_BUG))
5971
5972PyDoc_STRVAR(os_posix_fadvise__doc__,
5973"posix_fadvise($module, fd, offset, length, advice, /)\n"
5974"--\n"
5975"\n"
5976"Announce an intention to access data in a specific pattern.\n"
5977"\n"
5978"Announce an intention to access data in a specific pattern, thus allowing\n"
5979"the kernel to make optimizations.\n"
5980"The advice applies to the region of the file specified by fd starting at\n"
5981"offset and continuing for length bytes.\n"
5982"advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,\n"
5983"POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED, or\n"
5984"POSIX_FADV_DONTNEED.");
5985
5986#define OS_POSIX_FADVISE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02005987 {"posix_fadvise", (PyCFunction)(void(*)(void))os_posix_fadvise, METH_FASTCALL, os_posix_fadvise__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005988
5989static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005990os_posix_fadvise_impl(PyObject *module, int fd, Py_off_t offset,
Larry Hastings89964c42015-04-14 18:07:59 -04005991 Py_off_t length, int advice);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005992
5993static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02005994os_posix_fadvise(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005995{
5996 PyObject *return_value = NULL;
5997 int fd;
5998 Py_off_t offset;
5999 Py_off_t length;
6000 int advice;
6001
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02006002 if (!_PyArg_CheckPositional("posix_fadvise", nargs, 4, 4)) {
6003 goto exit;
6004 }
6005 if (PyFloat_Check(args[0])) {
6006 PyErr_SetString(PyExc_TypeError,
6007 "integer argument expected, got float" );
6008 goto exit;
6009 }
6010 fd = _PyLong_AsInt(args[0]);
6011 if (fd == -1 && PyErr_Occurred()) {
6012 goto exit;
6013 }
6014 if (!Py_off_t_converter(args[1], &offset)) {
6015 goto exit;
6016 }
6017 if (!Py_off_t_converter(args[2], &length)) {
6018 goto exit;
6019 }
6020 if (PyFloat_Check(args[3])) {
6021 PyErr_SetString(PyExc_TypeError,
6022 "integer argument expected, got float" );
6023 goto exit;
6024 }
6025 advice = _PyLong_AsInt(args[3]);
6026 if (advice == -1 && PyErr_Occurred()) {
Victor Stinner259f0e42017-01-17 01:35:17 +01006027 goto exit;
6028 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006029 return_value = os_posix_fadvise_impl(module, fd, offset, length, advice);
6030
6031exit:
6032 return return_value;
6033}
6034
6035#endif /* (defined(HAVE_POSIX_FADVISE) && !defined(POSIX_FADVISE_AIX_BUG)) */
6036
Victor Stinnerb477d192020-01-22 22:48:16 +01006037#if defined(MS_WINDOWS)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006038
6039PyDoc_STRVAR(os_putenv__doc__,
6040"putenv($module, name, value, /)\n"
6041"--\n"
6042"\n"
6043"Change or add an environment variable.");
6044
6045#define OS_PUTENV_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02006046 {"putenv", (PyCFunction)(void(*)(void))os_putenv, METH_FASTCALL, os_putenv__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006047
6048static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006049os_putenv_impl(PyObject *module, PyObject *name, PyObject *value);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006050
6051static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02006052os_putenv(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006053{
6054 PyObject *return_value = NULL;
6055 PyObject *name;
6056 PyObject *value;
6057
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02006058 if (!_PyArg_CheckPositional("putenv", nargs, 2, 2)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01006059 goto exit;
6060 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02006061 if (!PyUnicode_Check(args[0])) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02006062 _PyArg_BadArgument("putenv", "argument 1", "str", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02006063 goto exit;
6064 }
6065 if (PyUnicode_READY(args[0]) == -1) {
6066 goto exit;
6067 }
6068 name = args[0];
6069 if (!PyUnicode_Check(args[1])) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02006070 _PyArg_BadArgument("putenv", "argument 2", "str", args[1]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02006071 goto exit;
6072 }
6073 if (PyUnicode_READY(args[1]) == -1) {
6074 goto exit;
6075 }
6076 value = args[1];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006077 return_value = os_putenv_impl(module, name, value);
6078
6079exit:
6080 return return_value;
6081}
6082
Victor Stinnerb477d192020-01-22 22:48:16 +01006083#endif /* defined(MS_WINDOWS) */
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006084
Victor Stinnerb8d12622020-01-24 14:05:48 +01006085#if !defined(MS_WINDOWS)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006086
6087PyDoc_STRVAR(os_putenv__doc__,
6088"putenv($module, name, value, /)\n"
6089"--\n"
6090"\n"
6091"Change or add an environment variable.");
6092
6093#define OS_PUTENV_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02006094 {"putenv", (PyCFunction)(void(*)(void))os_putenv, METH_FASTCALL, os_putenv__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006095
6096static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006097os_putenv_impl(PyObject *module, PyObject *name, PyObject *value);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006098
6099static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02006100os_putenv(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006101{
6102 PyObject *return_value = NULL;
6103 PyObject *name = NULL;
6104 PyObject *value = NULL;
6105
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02006106 if (!_PyArg_CheckPositional("putenv", nargs, 2, 2)) {
6107 goto exit;
6108 }
6109 if (!PyUnicode_FSConverter(args[0], &name)) {
6110 goto exit;
6111 }
6112 if (!PyUnicode_FSConverter(args[1], &value)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01006113 goto exit;
6114 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006115 return_value = os_putenv_impl(module, name, value);
6116
6117exit:
6118 /* Cleanup for name */
6119 Py_XDECREF(name);
6120 /* Cleanup for value */
6121 Py_XDECREF(value);
6122
6123 return return_value;
6124}
6125
Victor Stinnerb8d12622020-01-24 14:05:48 +01006126#endif /* !defined(MS_WINDOWS) */
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006127
Victor Stinner161e7b32020-01-24 11:53:44 +01006128#if defined(MS_WINDOWS)
6129
6130PyDoc_STRVAR(os_unsetenv__doc__,
6131"unsetenv($module, name, /)\n"
6132"--\n"
6133"\n"
6134"Delete an environment variable.");
6135
6136#define OS_UNSETENV_METHODDEF \
6137 {"unsetenv", (PyCFunction)os_unsetenv, METH_O, os_unsetenv__doc__},
6138
6139static PyObject *
6140os_unsetenv_impl(PyObject *module, PyObject *name);
6141
6142static PyObject *
6143os_unsetenv(PyObject *module, PyObject *arg)
6144{
6145 PyObject *return_value = NULL;
6146 PyObject *name;
6147
6148 if (!PyUnicode_Check(arg)) {
6149 _PyArg_BadArgument("unsetenv", "argument", "str", arg);
6150 goto exit;
6151 }
6152 if (PyUnicode_READY(arg) == -1) {
6153 goto exit;
6154 }
6155 name = arg;
6156 return_value = os_unsetenv_impl(module, name);
6157
6158exit:
6159 return return_value;
6160}
6161
6162#endif /* defined(MS_WINDOWS) */
6163
Victor Stinnerb8d12622020-01-24 14:05:48 +01006164#if !defined(MS_WINDOWS)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006165
6166PyDoc_STRVAR(os_unsetenv__doc__,
6167"unsetenv($module, name, /)\n"
6168"--\n"
6169"\n"
6170"Delete an environment variable.");
6171
6172#define OS_UNSETENV_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03006173 {"unsetenv", (PyCFunction)os_unsetenv, METH_O, os_unsetenv__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006174
6175static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006176os_unsetenv_impl(PyObject *module, PyObject *name);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006177
6178static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006179os_unsetenv(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006180{
6181 PyObject *return_value = NULL;
6182 PyObject *name = NULL;
6183
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02006184 if (!PyUnicode_FSConverter(arg, &name)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006185 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006186 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006187 return_value = os_unsetenv_impl(module, name);
6188
6189exit:
6190 /* Cleanup for name */
6191 Py_XDECREF(name);
6192
6193 return return_value;
6194}
6195
Victor Stinnerb8d12622020-01-24 14:05:48 +01006196#endif /* !defined(MS_WINDOWS) */
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006197
6198PyDoc_STRVAR(os_strerror__doc__,
6199"strerror($module, code, /)\n"
6200"--\n"
6201"\n"
6202"Translate an error code to a message string.");
6203
6204#define OS_STRERROR_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03006205 {"strerror", (PyCFunction)os_strerror, METH_O, os_strerror__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006206
6207static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006208os_strerror_impl(PyObject *module, int code);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006209
6210static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006211os_strerror(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006212{
6213 PyObject *return_value = NULL;
6214 int code;
6215
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02006216 if (PyFloat_Check(arg)) {
6217 PyErr_SetString(PyExc_TypeError,
6218 "integer argument expected, got float" );
6219 goto exit;
6220 }
6221 code = _PyLong_AsInt(arg);
6222 if (code == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006223 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006224 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006225 return_value = os_strerror_impl(module, code);
6226
6227exit:
6228 return return_value;
6229}
6230
6231#if defined(HAVE_SYS_WAIT_H) && defined(WCOREDUMP)
6232
6233PyDoc_STRVAR(os_WCOREDUMP__doc__,
6234"WCOREDUMP($module, status, /)\n"
6235"--\n"
6236"\n"
6237"Return True if the process returning status was dumped to a core file.");
6238
6239#define OS_WCOREDUMP_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03006240 {"WCOREDUMP", (PyCFunction)os_WCOREDUMP, METH_O, os_WCOREDUMP__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006241
6242static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006243os_WCOREDUMP_impl(PyObject *module, int status);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006244
6245static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006246os_WCOREDUMP(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006247{
6248 PyObject *return_value = NULL;
6249 int status;
6250 int _return_value;
6251
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02006252 if (PyFloat_Check(arg)) {
6253 PyErr_SetString(PyExc_TypeError,
6254 "integer argument expected, got float" );
6255 goto exit;
6256 }
6257 status = _PyLong_AsInt(arg);
6258 if (status == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006259 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006260 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006261 _return_value = os_WCOREDUMP_impl(module, status);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006262 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006263 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006264 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006265 return_value = PyBool_FromLong((long)_return_value);
6266
6267exit:
6268 return return_value;
6269}
6270
6271#endif /* defined(HAVE_SYS_WAIT_H) && defined(WCOREDUMP) */
6272
6273#if defined(HAVE_SYS_WAIT_H) && defined(WIFCONTINUED)
6274
6275PyDoc_STRVAR(os_WIFCONTINUED__doc__,
6276"WIFCONTINUED($module, /, status)\n"
6277"--\n"
6278"\n"
6279"Return True if a particular process was continued from a job control stop.\n"
6280"\n"
6281"Return True if the process returning status was continued from a\n"
6282"job control stop.");
6283
6284#define OS_WIFCONTINUED_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02006285 {"WIFCONTINUED", (PyCFunction)(void(*)(void))os_WIFCONTINUED, METH_FASTCALL|METH_KEYWORDS, os_WIFCONTINUED__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006286
6287static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006288os_WIFCONTINUED_impl(PyObject *module, int status);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006289
6290static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02006291os_WIFCONTINUED(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006292{
6293 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03006294 static const char * const _keywords[] = {"status", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02006295 static _PyArg_Parser _parser = {NULL, _keywords, "WIFCONTINUED", 0};
6296 PyObject *argsbuf[1];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006297 int status;
6298 int _return_value;
6299
Serhiy Storchaka31913912019-03-14 10:32:22 +02006300 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
6301 if (!args) {
6302 goto exit;
6303 }
6304 if (PyFloat_Check(args[0])) {
6305 PyErr_SetString(PyExc_TypeError,
6306 "integer argument expected, got float" );
6307 goto exit;
6308 }
6309 status = _PyLong_AsInt(args[0]);
6310 if (status == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006311 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006312 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006313 _return_value = os_WIFCONTINUED_impl(module, status);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006314 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006315 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006316 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006317 return_value = PyBool_FromLong((long)_return_value);
6318
6319exit:
6320 return return_value;
6321}
6322
6323#endif /* defined(HAVE_SYS_WAIT_H) && defined(WIFCONTINUED) */
6324
6325#if defined(HAVE_SYS_WAIT_H) && defined(WIFSTOPPED)
6326
6327PyDoc_STRVAR(os_WIFSTOPPED__doc__,
6328"WIFSTOPPED($module, /, status)\n"
6329"--\n"
6330"\n"
6331"Return True if the process returning status was stopped.");
6332
6333#define OS_WIFSTOPPED_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02006334 {"WIFSTOPPED", (PyCFunction)(void(*)(void))os_WIFSTOPPED, METH_FASTCALL|METH_KEYWORDS, os_WIFSTOPPED__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006335
6336static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006337os_WIFSTOPPED_impl(PyObject *module, int status);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006338
6339static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02006340os_WIFSTOPPED(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006341{
6342 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03006343 static const char * const _keywords[] = {"status", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02006344 static _PyArg_Parser _parser = {NULL, _keywords, "WIFSTOPPED", 0};
6345 PyObject *argsbuf[1];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006346 int status;
6347 int _return_value;
6348
Serhiy Storchaka31913912019-03-14 10:32:22 +02006349 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
6350 if (!args) {
6351 goto exit;
6352 }
6353 if (PyFloat_Check(args[0])) {
6354 PyErr_SetString(PyExc_TypeError,
6355 "integer argument expected, got float" );
6356 goto exit;
6357 }
6358 status = _PyLong_AsInt(args[0]);
6359 if (status == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006360 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006361 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006362 _return_value = os_WIFSTOPPED_impl(module, status);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006363 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006364 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006365 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006366 return_value = PyBool_FromLong((long)_return_value);
6367
6368exit:
6369 return return_value;
6370}
6371
6372#endif /* defined(HAVE_SYS_WAIT_H) && defined(WIFSTOPPED) */
6373
6374#if defined(HAVE_SYS_WAIT_H) && defined(WIFSIGNALED)
6375
6376PyDoc_STRVAR(os_WIFSIGNALED__doc__,
6377"WIFSIGNALED($module, /, status)\n"
6378"--\n"
6379"\n"
6380"Return True if the process returning status was terminated by a signal.");
6381
6382#define OS_WIFSIGNALED_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02006383 {"WIFSIGNALED", (PyCFunction)(void(*)(void))os_WIFSIGNALED, METH_FASTCALL|METH_KEYWORDS, os_WIFSIGNALED__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006384
6385static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006386os_WIFSIGNALED_impl(PyObject *module, int status);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006387
6388static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02006389os_WIFSIGNALED(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006390{
6391 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03006392 static const char * const _keywords[] = {"status", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02006393 static _PyArg_Parser _parser = {NULL, _keywords, "WIFSIGNALED", 0};
6394 PyObject *argsbuf[1];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006395 int status;
6396 int _return_value;
6397
Serhiy Storchaka31913912019-03-14 10:32:22 +02006398 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
6399 if (!args) {
6400 goto exit;
6401 }
6402 if (PyFloat_Check(args[0])) {
6403 PyErr_SetString(PyExc_TypeError,
6404 "integer argument expected, got float" );
6405 goto exit;
6406 }
6407 status = _PyLong_AsInt(args[0]);
6408 if (status == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006409 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006410 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006411 _return_value = os_WIFSIGNALED_impl(module, status);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006412 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006413 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006414 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006415 return_value = PyBool_FromLong((long)_return_value);
6416
6417exit:
6418 return return_value;
6419}
6420
6421#endif /* defined(HAVE_SYS_WAIT_H) && defined(WIFSIGNALED) */
6422
6423#if defined(HAVE_SYS_WAIT_H) && defined(WIFEXITED)
6424
6425PyDoc_STRVAR(os_WIFEXITED__doc__,
6426"WIFEXITED($module, /, status)\n"
6427"--\n"
6428"\n"
6429"Return True if the process returning status exited via the exit() system call.");
6430
6431#define OS_WIFEXITED_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02006432 {"WIFEXITED", (PyCFunction)(void(*)(void))os_WIFEXITED, METH_FASTCALL|METH_KEYWORDS, os_WIFEXITED__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006433
6434static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006435os_WIFEXITED_impl(PyObject *module, int status);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006436
6437static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02006438os_WIFEXITED(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006439{
6440 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03006441 static const char * const _keywords[] = {"status", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02006442 static _PyArg_Parser _parser = {NULL, _keywords, "WIFEXITED", 0};
6443 PyObject *argsbuf[1];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006444 int status;
6445 int _return_value;
6446
Serhiy Storchaka31913912019-03-14 10:32:22 +02006447 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
6448 if (!args) {
6449 goto exit;
6450 }
6451 if (PyFloat_Check(args[0])) {
6452 PyErr_SetString(PyExc_TypeError,
6453 "integer argument expected, got float" );
6454 goto exit;
6455 }
6456 status = _PyLong_AsInt(args[0]);
6457 if (status == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006458 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006459 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006460 _return_value = os_WIFEXITED_impl(module, status);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006461 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006462 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006463 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006464 return_value = PyBool_FromLong((long)_return_value);
6465
6466exit:
6467 return return_value;
6468}
6469
6470#endif /* defined(HAVE_SYS_WAIT_H) && defined(WIFEXITED) */
6471
6472#if defined(HAVE_SYS_WAIT_H) && defined(WEXITSTATUS)
6473
6474PyDoc_STRVAR(os_WEXITSTATUS__doc__,
6475"WEXITSTATUS($module, /, status)\n"
6476"--\n"
6477"\n"
6478"Return the process return code from status.");
6479
6480#define OS_WEXITSTATUS_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02006481 {"WEXITSTATUS", (PyCFunction)(void(*)(void))os_WEXITSTATUS, METH_FASTCALL|METH_KEYWORDS, os_WEXITSTATUS__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006482
6483static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006484os_WEXITSTATUS_impl(PyObject *module, int status);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006485
6486static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02006487os_WEXITSTATUS(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006488{
6489 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03006490 static const char * const _keywords[] = {"status", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02006491 static _PyArg_Parser _parser = {NULL, _keywords, "WEXITSTATUS", 0};
6492 PyObject *argsbuf[1];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006493 int status;
6494 int _return_value;
6495
Serhiy Storchaka31913912019-03-14 10:32:22 +02006496 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
6497 if (!args) {
6498 goto exit;
6499 }
6500 if (PyFloat_Check(args[0])) {
6501 PyErr_SetString(PyExc_TypeError,
6502 "integer argument expected, got float" );
6503 goto exit;
6504 }
6505 status = _PyLong_AsInt(args[0]);
6506 if (status == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006507 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006508 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006509 _return_value = os_WEXITSTATUS_impl(module, status);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006510 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006511 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006512 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006513 return_value = PyLong_FromLong((long)_return_value);
6514
6515exit:
6516 return return_value;
6517}
6518
6519#endif /* defined(HAVE_SYS_WAIT_H) && defined(WEXITSTATUS) */
6520
6521#if defined(HAVE_SYS_WAIT_H) && defined(WTERMSIG)
6522
6523PyDoc_STRVAR(os_WTERMSIG__doc__,
6524"WTERMSIG($module, /, status)\n"
6525"--\n"
6526"\n"
6527"Return the signal that terminated the process that provided the status value.");
6528
6529#define OS_WTERMSIG_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02006530 {"WTERMSIG", (PyCFunction)(void(*)(void))os_WTERMSIG, METH_FASTCALL|METH_KEYWORDS, os_WTERMSIG__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006531
6532static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006533os_WTERMSIG_impl(PyObject *module, int status);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006534
6535static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02006536os_WTERMSIG(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006537{
6538 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03006539 static const char * const _keywords[] = {"status", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02006540 static _PyArg_Parser _parser = {NULL, _keywords, "WTERMSIG", 0};
6541 PyObject *argsbuf[1];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006542 int status;
6543 int _return_value;
6544
Serhiy Storchaka31913912019-03-14 10:32:22 +02006545 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
6546 if (!args) {
6547 goto exit;
6548 }
6549 if (PyFloat_Check(args[0])) {
6550 PyErr_SetString(PyExc_TypeError,
6551 "integer argument expected, got float" );
6552 goto exit;
6553 }
6554 status = _PyLong_AsInt(args[0]);
6555 if (status == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006556 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006557 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006558 _return_value = os_WTERMSIG_impl(module, status);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006559 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006560 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006561 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006562 return_value = PyLong_FromLong((long)_return_value);
6563
6564exit:
6565 return return_value;
6566}
6567
6568#endif /* defined(HAVE_SYS_WAIT_H) && defined(WTERMSIG) */
6569
6570#if defined(HAVE_SYS_WAIT_H) && defined(WSTOPSIG)
6571
6572PyDoc_STRVAR(os_WSTOPSIG__doc__,
6573"WSTOPSIG($module, /, status)\n"
6574"--\n"
6575"\n"
6576"Return the signal that stopped the process that provided the status value.");
6577
6578#define OS_WSTOPSIG_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02006579 {"WSTOPSIG", (PyCFunction)(void(*)(void))os_WSTOPSIG, METH_FASTCALL|METH_KEYWORDS, os_WSTOPSIG__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006580
6581static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006582os_WSTOPSIG_impl(PyObject *module, int status);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006583
6584static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02006585os_WSTOPSIG(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006586{
6587 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03006588 static const char * const _keywords[] = {"status", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02006589 static _PyArg_Parser _parser = {NULL, _keywords, "WSTOPSIG", 0};
6590 PyObject *argsbuf[1];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006591 int status;
6592 int _return_value;
6593
Serhiy Storchaka31913912019-03-14 10:32:22 +02006594 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
6595 if (!args) {
6596 goto exit;
6597 }
6598 if (PyFloat_Check(args[0])) {
6599 PyErr_SetString(PyExc_TypeError,
6600 "integer argument expected, got float" );
6601 goto exit;
6602 }
6603 status = _PyLong_AsInt(args[0]);
6604 if (status == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006605 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006606 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006607 _return_value = os_WSTOPSIG_impl(module, status);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006608 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006609 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006610 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006611 return_value = PyLong_FromLong((long)_return_value);
6612
6613exit:
6614 return return_value;
6615}
6616
6617#endif /* defined(HAVE_SYS_WAIT_H) && defined(WSTOPSIG) */
6618
6619#if (defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H))
6620
6621PyDoc_STRVAR(os_fstatvfs__doc__,
6622"fstatvfs($module, fd, /)\n"
6623"--\n"
6624"\n"
6625"Perform an fstatvfs system call on the given fd.\n"
6626"\n"
6627"Equivalent to statvfs(fd).");
6628
6629#define OS_FSTATVFS_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03006630 {"fstatvfs", (PyCFunction)os_fstatvfs, METH_O, os_fstatvfs__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006631
6632static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006633os_fstatvfs_impl(PyObject *module, int fd);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006634
6635static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006636os_fstatvfs(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006637{
6638 PyObject *return_value = NULL;
6639 int fd;
6640
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02006641 if (PyFloat_Check(arg)) {
6642 PyErr_SetString(PyExc_TypeError,
6643 "integer argument expected, got float" );
6644 goto exit;
6645 }
6646 fd = _PyLong_AsInt(arg);
6647 if (fd == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006648 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006649 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006650 return_value = os_fstatvfs_impl(module, fd);
6651
6652exit:
6653 return return_value;
6654}
6655
6656#endif /* (defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)) */
6657
6658#if (defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H))
6659
6660PyDoc_STRVAR(os_statvfs__doc__,
6661"statvfs($module, /, path)\n"
6662"--\n"
6663"\n"
6664"Perform a statvfs system call on the given path.\n"
6665"\n"
6666"path may always be specified as a string.\n"
6667"On some platforms, path may also be specified as an open file descriptor.\n"
6668" If this functionality is unavailable, using it raises an exception.");
6669
6670#define OS_STATVFS_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02006671 {"statvfs", (PyCFunction)(void(*)(void))os_statvfs, METH_FASTCALL|METH_KEYWORDS, os_statvfs__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006672
6673static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006674os_statvfs_impl(PyObject *module, path_t *path);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006675
6676static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02006677os_statvfs(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006678{
6679 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03006680 static const char * const _keywords[] = {"path", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02006681 static _PyArg_Parser _parser = {NULL, _keywords, "statvfs", 0};
6682 PyObject *argsbuf[1];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006683 path_t path = PATH_T_INITIALIZE("statvfs", "path", 0, PATH_HAVE_FSTATVFS);
6684
Serhiy Storchaka31913912019-03-14 10:32:22 +02006685 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
6686 if (!args) {
6687 goto exit;
6688 }
6689 if (!path_converter(args[0], &path)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006690 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006691 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006692 return_value = os_statvfs_impl(module, &path);
6693
6694exit:
6695 /* Cleanup for path */
6696 path_cleanup(&path);
6697
6698 return return_value;
6699}
6700
6701#endif /* (defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)) */
6702
6703#if defined(MS_WINDOWS)
6704
6705PyDoc_STRVAR(os__getdiskusage__doc__,
6706"_getdiskusage($module, /, path)\n"
6707"--\n"
6708"\n"
6709"Return disk usage statistics about the given path as a (total, free) tuple.");
6710
6711#define OS__GETDISKUSAGE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02006712 {"_getdiskusage", (PyCFunction)(void(*)(void))os__getdiskusage, METH_FASTCALL|METH_KEYWORDS, os__getdiskusage__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006713
6714static PyObject *
Steve Dower23ad6d02018-02-22 10:39:10 -08006715os__getdiskusage_impl(PyObject *module, path_t *path);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006716
6717static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02006718os__getdiskusage(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006719{
6720 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03006721 static const char * const _keywords[] = {"path", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02006722 static _PyArg_Parser _parser = {NULL, _keywords, "_getdiskusage", 0};
6723 PyObject *argsbuf[1];
Steve Dower23ad6d02018-02-22 10:39:10 -08006724 path_t path = PATH_T_INITIALIZE("_getdiskusage", "path", 0, 0);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006725
Serhiy Storchaka31913912019-03-14 10:32:22 +02006726 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
6727 if (!args) {
6728 goto exit;
6729 }
6730 if (!path_converter(args[0], &path)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006731 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006732 }
Steve Dower23ad6d02018-02-22 10:39:10 -08006733 return_value = os__getdiskusage_impl(module, &path);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006734
6735exit:
Steve Dower23ad6d02018-02-22 10:39:10 -08006736 /* Cleanup for path */
6737 path_cleanup(&path);
6738
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006739 return return_value;
6740}
6741
6742#endif /* defined(MS_WINDOWS) */
6743
6744#if defined(HAVE_FPATHCONF)
6745
6746PyDoc_STRVAR(os_fpathconf__doc__,
6747"fpathconf($module, fd, name, /)\n"
6748"--\n"
6749"\n"
6750"Return the configuration limit name for the file descriptor fd.\n"
6751"\n"
6752"If there is no limit, return -1.");
6753
6754#define OS_FPATHCONF_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02006755 {"fpathconf", (PyCFunction)(void(*)(void))os_fpathconf, METH_FASTCALL, os_fpathconf__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006756
6757static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006758os_fpathconf_impl(PyObject *module, int fd, int name);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006759
6760static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02006761os_fpathconf(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006762{
6763 PyObject *return_value = NULL;
6764 int fd;
6765 int name;
6766 long _return_value;
6767
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02006768 if (!_PyArg_CheckPositional("fpathconf", nargs, 2, 2)) {
6769 goto exit;
6770 }
6771 if (PyFloat_Check(args[0])) {
6772 PyErr_SetString(PyExc_TypeError,
6773 "integer argument expected, got float" );
6774 goto exit;
6775 }
6776 fd = _PyLong_AsInt(args[0]);
6777 if (fd == -1 && PyErr_Occurred()) {
6778 goto exit;
6779 }
6780 if (!conv_path_confname(args[1], &name)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01006781 goto exit;
6782 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006783 _return_value = os_fpathconf_impl(module, fd, name);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006784 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006785 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006786 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006787 return_value = PyLong_FromLong(_return_value);
6788
6789exit:
6790 return return_value;
6791}
6792
6793#endif /* defined(HAVE_FPATHCONF) */
6794
6795#if defined(HAVE_PATHCONF)
6796
6797PyDoc_STRVAR(os_pathconf__doc__,
6798"pathconf($module, /, path, name)\n"
6799"--\n"
6800"\n"
6801"Return the configuration limit name for the file or directory path.\n"
6802"\n"
6803"If there is no limit, return -1.\n"
6804"On some platforms, path may also be specified as an open file descriptor.\n"
6805" If this functionality is unavailable, using it raises an exception.");
6806
6807#define OS_PATHCONF_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02006808 {"pathconf", (PyCFunction)(void(*)(void))os_pathconf, METH_FASTCALL|METH_KEYWORDS, os_pathconf__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006809
6810static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006811os_pathconf_impl(PyObject *module, path_t *path, int name);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006812
6813static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02006814os_pathconf(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006815{
6816 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03006817 static const char * const _keywords[] = {"path", "name", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02006818 static _PyArg_Parser _parser = {NULL, _keywords, "pathconf", 0};
6819 PyObject *argsbuf[2];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006820 path_t path = PATH_T_INITIALIZE("pathconf", "path", 0, PATH_HAVE_FPATHCONF);
6821 int name;
6822 long _return_value;
6823
Serhiy Storchaka31913912019-03-14 10:32:22 +02006824 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf);
6825 if (!args) {
6826 goto exit;
6827 }
6828 if (!path_converter(args[0], &path)) {
6829 goto exit;
6830 }
6831 if (!conv_path_confname(args[1], &name)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006832 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006833 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006834 _return_value = os_pathconf_impl(module, &path, name);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006835 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006836 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006837 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006838 return_value = PyLong_FromLong(_return_value);
6839
6840exit:
6841 /* Cleanup for path */
6842 path_cleanup(&path);
6843
6844 return return_value;
6845}
6846
6847#endif /* defined(HAVE_PATHCONF) */
6848
6849#if defined(HAVE_CONFSTR)
6850
6851PyDoc_STRVAR(os_confstr__doc__,
6852"confstr($module, name, /)\n"
6853"--\n"
6854"\n"
6855"Return a string-valued system configuration variable.");
6856
6857#define OS_CONFSTR_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03006858 {"confstr", (PyCFunction)os_confstr, METH_O, os_confstr__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006859
6860static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006861os_confstr_impl(PyObject *module, int name);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006862
6863static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006864os_confstr(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006865{
6866 PyObject *return_value = NULL;
6867 int name;
6868
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02006869 if (!conv_confstr_confname(arg, &name)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006870 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006871 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006872 return_value = os_confstr_impl(module, name);
6873
6874exit:
6875 return return_value;
6876}
6877
6878#endif /* defined(HAVE_CONFSTR) */
6879
6880#if defined(HAVE_SYSCONF)
6881
6882PyDoc_STRVAR(os_sysconf__doc__,
6883"sysconf($module, name, /)\n"
6884"--\n"
6885"\n"
6886"Return an integer-valued system configuration variable.");
6887
6888#define OS_SYSCONF_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03006889 {"sysconf", (PyCFunction)os_sysconf, METH_O, os_sysconf__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006890
6891static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006892os_sysconf_impl(PyObject *module, int name);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006893
6894static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006895os_sysconf(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006896{
6897 PyObject *return_value = NULL;
6898 int name;
6899 long _return_value;
6900
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02006901 if (!conv_sysconf_confname(arg, &name)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006902 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006903 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006904 _return_value = os_sysconf_impl(module, name);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006905 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006906 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006907 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006908 return_value = PyLong_FromLong(_return_value);
6909
6910exit:
6911 return return_value;
6912}
6913
6914#endif /* defined(HAVE_SYSCONF) */
6915
6916PyDoc_STRVAR(os_abort__doc__,
6917"abort($module, /)\n"
6918"--\n"
6919"\n"
6920"Abort the interpreter immediately.\n"
6921"\n"
6922"This function \'dumps core\' or otherwise fails in the hardest way possible\n"
6923"on the hosting operating system. This function never returns.");
6924
6925#define OS_ABORT_METHODDEF \
6926 {"abort", (PyCFunction)os_abort, METH_NOARGS, os_abort__doc__},
6927
6928static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006929os_abort_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006930
6931static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006932os_abort(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006933{
6934 return os_abort_impl(module);
6935}
6936
Steve Dowercc16be82016-09-08 10:35:16 -07006937#if defined(MS_WINDOWS)
6938
6939PyDoc_STRVAR(os_startfile__doc__,
Serhiy Storchaka279f4462019-09-14 12:24:05 +03006940"startfile($module, /, filepath, operation=<unrepresentable>)\n"
Steve Dowercc16be82016-09-08 10:35:16 -07006941"--\n"
6942"\n"
Steve Dowercc16be82016-09-08 10:35:16 -07006943"Start a file with its associated application.\n"
6944"\n"
6945"When \"operation\" is not specified or \"open\", this acts like\n"
6946"double-clicking the file in Explorer, or giving the file name as an\n"
6947"argument to the DOS \"start\" command: the file is opened with whatever\n"
6948"application (if any) its extension is associated.\n"
6949"When another \"operation\" is given, it specifies what should be done with\n"
6950"the file. A typical operation is \"print\".\n"
6951"\n"
6952"startfile returns as soon as the associated application is launched.\n"
6953"There is no option to wait for the application to close, and no way\n"
6954"to retrieve the application\'s exit status.\n"
6955"\n"
6956"The filepath is relative to the current directory. If you want to use\n"
6957"an absolute path, make sure the first character is not a slash (\"/\");\n"
6958"the underlying Win32 ShellExecute function doesn\'t work if it is.");
6959
6960#define OS_STARTFILE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02006961 {"startfile", (PyCFunction)(void(*)(void))os_startfile, METH_FASTCALL|METH_KEYWORDS, os_startfile__doc__},
Steve Dowercc16be82016-09-08 10:35:16 -07006962
6963static PyObject *
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02006964os_startfile_impl(PyObject *module, path_t *filepath,
6965 const Py_UNICODE *operation);
Steve Dowercc16be82016-09-08 10:35:16 -07006966
6967static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02006968os_startfile(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Steve Dowercc16be82016-09-08 10:35:16 -07006969{
6970 PyObject *return_value = NULL;
Victor Stinner37e4ef72016-09-09 20:00:13 -07006971 static const char * const _keywords[] = {"filepath", "operation", NULL};
6972 static _PyArg_Parser _parser = {"O&|u:startfile", _keywords, 0};
Steve Dowercc16be82016-09-08 10:35:16 -07006973 path_t filepath = PATH_T_INITIALIZE("startfile", "filepath", 0, 0);
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02006974 const Py_UNICODE *operation = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07006975
Victor Stinner3e1fad62017-01-17 01:29:01 +01006976 if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
Steve Dowercc16be82016-09-08 10:35:16 -07006977 path_converter, &filepath, &operation)) {
6978 goto exit;
6979 }
6980 return_value = os_startfile_impl(module, &filepath, operation);
6981
6982exit:
6983 /* Cleanup for filepath */
6984 path_cleanup(&filepath);
6985
6986 return return_value;
6987}
6988
6989#endif /* defined(MS_WINDOWS) */
6990
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006991#if defined(HAVE_GETLOADAVG)
6992
6993PyDoc_STRVAR(os_getloadavg__doc__,
6994"getloadavg($module, /)\n"
6995"--\n"
6996"\n"
6997"Return average recent system load information.\n"
6998"\n"
6999"Return the number of processes in the system run queue averaged over\n"
7000"the last 1, 5, and 15 minutes as a tuple of three floats.\n"
7001"Raises OSError if the load average was unobtainable.");
7002
7003#define OS_GETLOADAVG_METHODDEF \
7004 {"getloadavg", (PyCFunction)os_getloadavg, METH_NOARGS, os_getloadavg__doc__},
7005
7006static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007007os_getloadavg_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007008
7009static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007010os_getloadavg(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007011{
7012 return os_getloadavg_impl(module);
7013}
7014
7015#endif /* defined(HAVE_GETLOADAVG) */
7016
7017PyDoc_STRVAR(os_device_encoding__doc__,
7018"device_encoding($module, /, fd)\n"
7019"--\n"
7020"\n"
7021"Return a string describing the encoding of a terminal\'s file descriptor.\n"
7022"\n"
7023"The file descriptor must be attached to a terminal.\n"
7024"If the device is not a terminal, return None.");
7025
7026#define OS_DEVICE_ENCODING_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02007027 {"device_encoding", (PyCFunction)(void(*)(void))os_device_encoding, METH_FASTCALL|METH_KEYWORDS, os_device_encoding__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007028
7029static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007030os_device_encoding_impl(PyObject *module, int fd);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007031
7032static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02007033os_device_encoding(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007034{
7035 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03007036 static const char * const _keywords[] = {"fd", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02007037 static _PyArg_Parser _parser = {NULL, _keywords, "device_encoding", 0};
7038 PyObject *argsbuf[1];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007039 int fd;
7040
Serhiy Storchaka31913912019-03-14 10:32:22 +02007041 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
7042 if (!args) {
7043 goto exit;
7044 }
7045 if (PyFloat_Check(args[0])) {
7046 PyErr_SetString(PyExc_TypeError,
7047 "integer argument expected, got float" );
7048 goto exit;
7049 }
7050 fd = _PyLong_AsInt(args[0]);
7051 if (fd == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007052 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03007053 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007054 return_value = os_device_encoding_impl(module, fd);
7055
7056exit:
7057 return return_value;
7058}
7059
7060#if defined(HAVE_SETRESUID)
7061
7062PyDoc_STRVAR(os_setresuid__doc__,
7063"setresuid($module, ruid, euid, suid, /)\n"
7064"--\n"
7065"\n"
7066"Set the current process\'s real, effective, and saved user ids.");
7067
7068#define OS_SETRESUID_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02007069 {"setresuid", (PyCFunction)(void(*)(void))os_setresuid, METH_FASTCALL, os_setresuid__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007070
7071static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007072os_setresuid_impl(PyObject *module, uid_t ruid, uid_t euid, uid_t suid);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007073
7074static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02007075os_setresuid(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007076{
7077 PyObject *return_value = NULL;
7078 uid_t ruid;
7079 uid_t euid;
7080 uid_t suid;
7081
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02007082 if (!_PyArg_CheckPositional("setresuid", nargs, 3, 3)) {
7083 goto exit;
7084 }
7085 if (!_Py_Uid_Converter(args[0], &ruid)) {
7086 goto exit;
7087 }
7088 if (!_Py_Uid_Converter(args[1], &euid)) {
7089 goto exit;
7090 }
7091 if (!_Py_Uid_Converter(args[2], &suid)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01007092 goto exit;
7093 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007094 return_value = os_setresuid_impl(module, ruid, euid, suid);
7095
7096exit:
7097 return return_value;
7098}
7099
7100#endif /* defined(HAVE_SETRESUID) */
7101
7102#if defined(HAVE_SETRESGID)
7103
7104PyDoc_STRVAR(os_setresgid__doc__,
7105"setresgid($module, rgid, egid, sgid, /)\n"
7106"--\n"
7107"\n"
7108"Set the current process\'s real, effective, and saved group ids.");
7109
7110#define OS_SETRESGID_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02007111 {"setresgid", (PyCFunction)(void(*)(void))os_setresgid, METH_FASTCALL, os_setresgid__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007112
7113static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007114os_setresgid_impl(PyObject *module, gid_t rgid, gid_t egid, gid_t sgid);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007115
7116static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02007117os_setresgid(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007118{
7119 PyObject *return_value = NULL;
7120 gid_t rgid;
7121 gid_t egid;
7122 gid_t sgid;
7123
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02007124 if (!_PyArg_CheckPositional("setresgid", nargs, 3, 3)) {
7125 goto exit;
7126 }
7127 if (!_Py_Gid_Converter(args[0], &rgid)) {
7128 goto exit;
7129 }
7130 if (!_Py_Gid_Converter(args[1], &egid)) {
7131 goto exit;
7132 }
7133 if (!_Py_Gid_Converter(args[2], &sgid)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01007134 goto exit;
7135 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007136 return_value = os_setresgid_impl(module, rgid, egid, sgid);
7137
7138exit:
7139 return return_value;
7140}
7141
7142#endif /* defined(HAVE_SETRESGID) */
7143
7144#if defined(HAVE_GETRESUID)
7145
7146PyDoc_STRVAR(os_getresuid__doc__,
7147"getresuid($module, /)\n"
7148"--\n"
7149"\n"
7150"Return a tuple of the current process\'s real, effective, and saved user ids.");
7151
7152#define OS_GETRESUID_METHODDEF \
7153 {"getresuid", (PyCFunction)os_getresuid, METH_NOARGS, os_getresuid__doc__},
7154
7155static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007156os_getresuid_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007157
7158static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007159os_getresuid(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007160{
7161 return os_getresuid_impl(module);
7162}
7163
7164#endif /* defined(HAVE_GETRESUID) */
7165
7166#if defined(HAVE_GETRESGID)
7167
7168PyDoc_STRVAR(os_getresgid__doc__,
7169"getresgid($module, /)\n"
7170"--\n"
7171"\n"
7172"Return a tuple of the current process\'s real, effective, and saved group ids.");
7173
7174#define OS_GETRESGID_METHODDEF \
7175 {"getresgid", (PyCFunction)os_getresgid, METH_NOARGS, os_getresgid__doc__},
7176
7177static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007178os_getresgid_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007179
7180static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007181os_getresgid(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007182{
7183 return os_getresgid_impl(module);
7184}
7185
7186#endif /* defined(HAVE_GETRESGID) */
7187
7188#if defined(USE_XATTRS)
7189
7190PyDoc_STRVAR(os_getxattr__doc__,
7191"getxattr($module, /, path, attribute, *, follow_symlinks=True)\n"
7192"--\n"
7193"\n"
7194"Return the value of extended attribute attribute on path.\n"
7195"\n"
BNMetricsb9427072018-11-02 15:20:19 +00007196"path may be either a string, a path-like object, or an open file descriptor.\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007197"If follow_symlinks is False, and the last element of the path is a symbolic\n"
7198" link, getxattr will examine the symbolic link itself instead of the file\n"
7199" the link points to.");
7200
7201#define OS_GETXATTR_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02007202 {"getxattr", (PyCFunction)(void(*)(void))os_getxattr, METH_FASTCALL|METH_KEYWORDS, os_getxattr__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007203
7204static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007205os_getxattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -04007206 int follow_symlinks);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007207
7208static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02007209os_getxattr(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007210{
7211 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03007212 static const char * const _keywords[] = {"path", "attribute", "follow_symlinks", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02007213 static _PyArg_Parser _parser = {NULL, _keywords, "getxattr", 0};
7214 PyObject *argsbuf[3];
7215 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007216 path_t path = PATH_T_INITIALIZE("getxattr", "path", 0, 1);
7217 path_t attribute = PATH_T_INITIALIZE("getxattr", "attribute", 0, 0);
7218 int follow_symlinks = 1;
7219
Serhiy Storchaka31913912019-03-14 10:32:22 +02007220 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf);
7221 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007222 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03007223 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02007224 if (!path_converter(args[0], &path)) {
7225 goto exit;
7226 }
7227 if (!path_converter(args[1], &attribute)) {
7228 goto exit;
7229 }
7230 if (!noptargs) {
7231 goto skip_optional_kwonly;
7232 }
7233 follow_symlinks = PyObject_IsTrue(args[2]);
7234 if (follow_symlinks < 0) {
7235 goto exit;
7236 }
7237skip_optional_kwonly:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007238 return_value = os_getxattr_impl(module, &path, &attribute, follow_symlinks);
7239
7240exit:
7241 /* Cleanup for path */
7242 path_cleanup(&path);
7243 /* Cleanup for attribute */
7244 path_cleanup(&attribute);
7245
7246 return return_value;
7247}
7248
7249#endif /* defined(USE_XATTRS) */
7250
7251#if defined(USE_XATTRS)
7252
7253PyDoc_STRVAR(os_setxattr__doc__,
7254"setxattr($module, /, path, attribute, value, flags=0, *,\n"
7255" follow_symlinks=True)\n"
7256"--\n"
7257"\n"
7258"Set extended attribute attribute on path to value.\n"
7259"\n"
BNMetricsb9427072018-11-02 15:20:19 +00007260"path may be either a string, a path-like object, or an open file descriptor.\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007261"If follow_symlinks is False, and the last element of the path is a symbolic\n"
7262" link, setxattr will modify the symbolic link itself instead of the file\n"
7263" the link points to.");
7264
7265#define OS_SETXATTR_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02007266 {"setxattr", (PyCFunction)(void(*)(void))os_setxattr, METH_FASTCALL|METH_KEYWORDS, os_setxattr__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007267
7268static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007269os_setxattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -04007270 Py_buffer *value, int flags, int follow_symlinks);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007271
7272static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02007273os_setxattr(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007274{
7275 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03007276 static const char * const _keywords[] = {"path", "attribute", "value", "flags", "follow_symlinks", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02007277 static _PyArg_Parser _parser = {NULL, _keywords, "setxattr", 0};
7278 PyObject *argsbuf[5];
7279 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 3;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007280 path_t path = PATH_T_INITIALIZE("setxattr", "path", 0, 1);
7281 path_t attribute = PATH_T_INITIALIZE("setxattr", "attribute", 0, 0);
7282 Py_buffer value = {NULL, NULL};
7283 int flags = 0;
7284 int follow_symlinks = 1;
7285
Serhiy Storchaka31913912019-03-14 10:32:22 +02007286 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 3, 4, 0, argsbuf);
7287 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007288 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03007289 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02007290 if (!path_converter(args[0], &path)) {
7291 goto exit;
7292 }
7293 if (!path_converter(args[1], &attribute)) {
7294 goto exit;
7295 }
7296 if (PyObject_GetBuffer(args[2], &value, PyBUF_SIMPLE) != 0) {
7297 goto exit;
7298 }
7299 if (!PyBuffer_IsContiguous(&value, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02007300 _PyArg_BadArgument("setxattr", "argument 'value'", "contiguous buffer", args[2]);
Serhiy Storchaka31913912019-03-14 10:32:22 +02007301 goto exit;
7302 }
7303 if (!noptargs) {
7304 goto skip_optional_pos;
7305 }
7306 if (args[3]) {
7307 if (PyFloat_Check(args[3])) {
7308 PyErr_SetString(PyExc_TypeError,
7309 "integer argument expected, got float" );
7310 goto exit;
7311 }
7312 flags = _PyLong_AsInt(args[3]);
7313 if (flags == -1 && PyErr_Occurred()) {
7314 goto exit;
7315 }
7316 if (!--noptargs) {
7317 goto skip_optional_pos;
7318 }
7319 }
7320skip_optional_pos:
7321 if (!noptargs) {
7322 goto skip_optional_kwonly;
7323 }
7324 follow_symlinks = PyObject_IsTrue(args[4]);
7325 if (follow_symlinks < 0) {
7326 goto exit;
7327 }
7328skip_optional_kwonly:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007329 return_value = os_setxattr_impl(module, &path, &attribute, &value, flags, follow_symlinks);
7330
7331exit:
7332 /* Cleanup for path */
7333 path_cleanup(&path);
7334 /* Cleanup for attribute */
7335 path_cleanup(&attribute);
7336 /* Cleanup for value */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03007337 if (value.obj) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007338 PyBuffer_Release(&value);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03007339 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007340
7341 return return_value;
7342}
7343
7344#endif /* defined(USE_XATTRS) */
7345
7346#if defined(USE_XATTRS)
7347
7348PyDoc_STRVAR(os_removexattr__doc__,
7349"removexattr($module, /, path, attribute, *, follow_symlinks=True)\n"
7350"--\n"
7351"\n"
7352"Remove extended attribute attribute on path.\n"
7353"\n"
BNMetricsb9427072018-11-02 15:20:19 +00007354"path may be either a string, a path-like object, or an open file descriptor.\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007355"If follow_symlinks is False, and the last element of the path is a symbolic\n"
7356" link, removexattr will modify the symbolic link itself instead of the file\n"
7357" the link points to.");
7358
7359#define OS_REMOVEXATTR_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02007360 {"removexattr", (PyCFunction)(void(*)(void))os_removexattr, METH_FASTCALL|METH_KEYWORDS, os_removexattr__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007361
7362static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007363os_removexattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -04007364 int follow_symlinks);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007365
7366static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02007367os_removexattr(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007368{
7369 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03007370 static const char * const _keywords[] = {"path", "attribute", "follow_symlinks", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02007371 static _PyArg_Parser _parser = {NULL, _keywords, "removexattr", 0};
7372 PyObject *argsbuf[3];
7373 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007374 path_t path = PATH_T_INITIALIZE("removexattr", "path", 0, 1);
7375 path_t attribute = PATH_T_INITIALIZE("removexattr", "attribute", 0, 0);
7376 int follow_symlinks = 1;
7377
Serhiy Storchaka31913912019-03-14 10:32:22 +02007378 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf);
7379 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007380 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03007381 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02007382 if (!path_converter(args[0], &path)) {
7383 goto exit;
7384 }
7385 if (!path_converter(args[1], &attribute)) {
7386 goto exit;
7387 }
7388 if (!noptargs) {
7389 goto skip_optional_kwonly;
7390 }
7391 follow_symlinks = PyObject_IsTrue(args[2]);
7392 if (follow_symlinks < 0) {
7393 goto exit;
7394 }
7395skip_optional_kwonly:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007396 return_value = os_removexattr_impl(module, &path, &attribute, follow_symlinks);
7397
7398exit:
7399 /* Cleanup for path */
7400 path_cleanup(&path);
7401 /* Cleanup for attribute */
7402 path_cleanup(&attribute);
7403
7404 return return_value;
7405}
7406
7407#endif /* defined(USE_XATTRS) */
7408
7409#if defined(USE_XATTRS)
7410
7411PyDoc_STRVAR(os_listxattr__doc__,
7412"listxattr($module, /, path=None, *, follow_symlinks=True)\n"
7413"--\n"
7414"\n"
7415"Return a list of extended attributes on path.\n"
7416"\n"
BNMetricsb9427072018-11-02 15:20:19 +00007417"path may be either None, a string, a path-like object, or an open file descriptor.\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007418"if path is None, listxattr will examine the current directory.\n"
7419"If follow_symlinks is False, and the last element of the path is a symbolic\n"
7420" link, listxattr will examine the symbolic link itself instead of the file\n"
7421" the link points to.");
7422
7423#define OS_LISTXATTR_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02007424 {"listxattr", (PyCFunction)(void(*)(void))os_listxattr, METH_FASTCALL|METH_KEYWORDS, os_listxattr__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007425
7426static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007427os_listxattr_impl(PyObject *module, path_t *path, int follow_symlinks);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007428
7429static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02007430os_listxattr(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007431{
7432 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03007433 static const char * const _keywords[] = {"path", "follow_symlinks", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02007434 static _PyArg_Parser _parser = {NULL, _keywords, "listxattr", 0};
7435 PyObject *argsbuf[2];
7436 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007437 path_t path = PATH_T_INITIALIZE("listxattr", "path", 1, 1);
7438 int follow_symlinks = 1;
7439
Serhiy Storchaka31913912019-03-14 10:32:22 +02007440 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
7441 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007442 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03007443 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02007444 if (!noptargs) {
7445 goto skip_optional_pos;
7446 }
7447 if (args[0]) {
7448 if (!path_converter(args[0], &path)) {
7449 goto exit;
7450 }
7451 if (!--noptargs) {
7452 goto skip_optional_pos;
7453 }
7454 }
7455skip_optional_pos:
7456 if (!noptargs) {
7457 goto skip_optional_kwonly;
7458 }
7459 follow_symlinks = PyObject_IsTrue(args[1]);
7460 if (follow_symlinks < 0) {
7461 goto exit;
7462 }
7463skip_optional_kwonly:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007464 return_value = os_listxattr_impl(module, &path, follow_symlinks);
7465
7466exit:
7467 /* Cleanup for path */
7468 path_cleanup(&path);
7469
7470 return return_value;
7471}
7472
7473#endif /* defined(USE_XATTRS) */
7474
7475PyDoc_STRVAR(os_urandom__doc__,
7476"urandom($module, size, /)\n"
7477"--\n"
7478"\n"
7479"Return a bytes object containing random bytes suitable for cryptographic use.");
7480
7481#define OS_URANDOM_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03007482 {"urandom", (PyCFunction)os_urandom, METH_O, os_urandom__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007483
7484static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007485os_urandom_impl(PyObject *module, Py_ssize_t size);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007486
7487static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007488os_urandom(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007489{
7490 PyObject *return_value = NULL;
7491 Py_ssize_t size;
7492
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02007493 if (PyFloat_Check(arg)) {
7494 PyErr_SetString(PyExc_TypeError,
7495 "integer argument expected, got float" );
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007496 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03007497 }
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02007498 {
7499 Py_ssize_t ival = -1;
7500 PyObject *iobj = PyNumber_Index(arg);
7501 if (iobj != NULL) {
7502 ival = PyLong_AsSsize_t(iobj);
7503 Py_DECREF(iobj);
7504 }
7505 if (ival == -1 && PyErr_Occurred()) {
7506 goto exit;
7507 }
7508 size = ival;
7509 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007510 return_value = os_urandom_impl(module, size);
7511
7512exit:
7513 return return_value;
7514}
7515
Zackery Spytz43fdbd22019-05-29 13:57:07 -06007516#if defined(HAVE_MEMFD_CREATE)
7517
7518PyDoc_STRVAR(os_memfd_create__doc__,
7519"memfd_create($module, /, name, flags=MFD_CLOEXEC)\n"
7520"--\n"
7521"\n");
7522
7523#define OS_MEMFD_CREATE_METHODDEF \
7524 {"memfd_create", (PyCFunction)(void(*)(void))os_memfd_create, METH_FASTCALL|METH_KEYWORDS, os_memfd_create__doc__},
7525
7526static PyObject *
7527os_memfd_create_impl(PyObject *module, PyObject *name, unsigned int flags);
7528
7529static PyObject *
7530os_memfd_create(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
7531{
7532 PyObject *return_value = NULL;
7533 static const char * const _keywords[] = {"name", "flags", NULL};
7534 static _PyArg_Parser _parser = {NULL, _keywords, "memfd_create", 0};
7535 PyObject *argsbuf[2];
7536 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
7537 PyObject *name = NULL;
7538 unsigned int flags = MFD_CLOEXEC;
7539
7540 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
7541 if (!args) {
7542 goto exit;
7543 }
7544 if (!PyUnicode_FSConverter(args[0], &name)) {
7545 goto exit;
7546 }
7547 if (!noptargs) {
7548 goto skip_optional_pos;
7549 }
7550 if (PyFloat_Check(args[1])) {
7551 PyErr_SetString(PyExc_TypeError,
7552 "integer argument expected, got float" );
7553 goto exit;
7554 }
7555 flags = (unsigned int)PyLong_AsUnsignedLongMask(args[1]);
7556 if (flags == (unsigned int)-1 && PyErr_Occurred()) {
7557 goto exit;
7558 }
7559skip_optional_pos:
7560 return_value = os_memfd_create_impl(module, name, flags);
7561
7562exit:
7563 /* Cleanup for name */
7564 Py_XDECREF(name);
7565
7566 return return_value;
7567}
7568
7569#endif /* defined(HAVE_MEMFD_CREATE) */
7570
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007571PyDoc_STRVAR(os_cpu_count__doc__,
7572"cpu_count($module, /)\n"
7573"--\n"
7574"\n"
Charles-François Natali80d62e62015-08-13 20:37:08 +01007575"Return the number of CPUs in the system; return None if indeterminable.\n"
7576"\n"
7577"This number is not equivalent to the number of CPUs the current process can\n"
7578"use. The number of usable CPUs can be obtained with\n"
7579"``len(os.sched_getaffinity(0))``");
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007580
7581#define OS_CPU_COUNT_METHODDEF \
7582 {"cpu_count", (PyCFunction)os_cpu_count, METH_NOARGS, os_cpu_count__doc__},
7583
7584static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007585os_cpu_count_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007586
7587static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007588os_cpu_count(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007589{
7590 return os_cpu_count_impl(module);
7591}
7592
7593PyDoc_STRVAR(os_get_inheritable__doc__,
7594"get_inheritable($module, fd, /)\n"
7595"--\n"
7596"\n"
7597"Get the close-on-exe flag of the specified file descriptor.");
7598
7599#define OS_GET_INHERITABLE_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03007600 {"get_inheritable", (PyCFunction)os_get_inheritable, METH_O, os_get_inheritable__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007601
7602static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007603os_get_inheritable_impl(PyObject *module, int fd);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007604
7605static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007606os_get_inheritable(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007607{
7608 PyObject *return_value = NULL;
7609 int fd;
7610 int _return_value;
7611
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02007612 if (PyFloat_Check(arg)) {
7613 PyErr_SetString(PyExc_TypeError,
7614 "integer argument expected, got float" );
7615 goto exit;
7616 }
7617 fd = _PyLong_AsInt(arg);
7618 if (fd == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007619 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03007620 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007621 _return_value = os_get_inheritable_impl(module, fd);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03007622 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007623 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03007624 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007625 return_value = PyBool_FromLong((long)_return_value);
7626
7627exit:
7628 return return_value;
7629}
7630
7631PyDoc_STRVAR(os_set_inheritable__doc__,
7632"set_inheritable($module, fd, inheritable, /)\n"
7633"--\n"
7634"\n"
7635"Set the inheritable flag of the specified file descriptor.");
7636
7637#define OS_SET_INHERITABLE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02007638 {"set_inheritable", (PyCFunction)(void(*)(void))os_set_inheritable, METH_FASTCALL, os_set_inheritable__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007639
7640static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007641os_set_inheritable_impl(PyObject *module, int fd, int inheritable);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007642
7643static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02007644os_set_inheritable(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007645{
7646 PyObject *return_value = NULL;
7647 int fd;
7648 int inheritable;
7649
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02007650 if (!_PyArg_CheckPositional("set_inheritable", nargs, 2, 2)) {
7651 goto exit;
7652 }
7653 if (PyFloat_Check(args[0])) {
7654 PyErr_SetString(PyExc_TypeError,
7655 "integer argument expected, got float" );
7656 goto exit;
7657 }
7658 fd = _PyLong_AsInt(args[0]);
7659 if (fd == -1 && PyErr_Occurred()) {
7660 goto exit;
7661 }
7662 if (PyFloat_Check(args[1])) {
7663 PyErr_SetString(PyExc_TypeError,
7664 "integer argument expected, got float" );
7665 goto exit;
7666 }
7667 inheritable = _PyLong_AsInt(args[1]);
7668 if (inheritable == -1 && PyErr_Occurred()) {
Victor Stinner259f0e42017-01-17 01:35:17 +01007669 goto exit;
7670 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007671 return_value = os_set_inheritable_impl(module, fd, inheritable);
7672
7673exit:
7674 return return_value;
7675}
7676
7677#if defined(MS_WINDOWS)
7678
7679PyDoc_STRVAR(os_get_handle_inheritable__doc__,
7680"get_handle_inheritable($module, handle, /)\n"
7681"--\n"
7682"\n"
7683"Get the close-on-exe flag of the specified file descriptor.");
7684
7685#define OS_GET_HANDLE_INHERITABLE_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03007686 {"get_handle_inheritable", (PyCFunction)os_get_handle_inheritable, METH_O, os_get_handle_inheritable__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007687
7688static int
Victor Stinner581139c2016-09-06 15:54:20 -07007689os_get_handle_inheritable_impl(PyObject *module, intptr_t handle);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007690
7691static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007692os_get_handle_inheritable(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007693{
7694 PyObject *return_value = NULL;
Victor Stinner581139c2016-09-06 15:54:20 -07007695 intptr_t handle;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007696 int _return_value;
7697
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03007698 if (!PyArg_Parse(arg, "" _Py_PARSE_INTPTR ":get_handle_inheritable", &handle)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007699 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03007700 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007701 _return_value = os_get_handle_inheritable_impl(module, handle);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03007702 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007703 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03007704 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007705 return_value = PyBool_FromLong((long)_return_value);
7706
7707exit:
7708 return return_value;
7709}
7710
7711#endif /* defined(MS_WINDOWS) */
7712
7713#if defined(MS_WINDOWS)
7714
7715PyDoc_STRVAR(os_set_handle_inheritable__doc__,
7716"set_handle_inheritable($module, handle, inheritable, /)\n"
7717"--\n"
7718"\n"
7719"Set the inheritable flag of the specified handle.");
7720
7721#define OS_SET_HANDLE_INHERITABLE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02007722 {"set_handle_inheritable", (PyCFunction)(void(*)(void))os_set_handle_inheritable, METH_FASTCALL, os_set_handle_inheritable__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007723
7724static PyObject *
Victor Stinner581139c2016-09-06 15:54:20 -07007725os_set_handle_inheritable_impl(PyObject *module, intptr_t handle,
Larry Hastings89964c42015-04-14 18:07:59 -04007726 int inheritable);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007727
7728static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02007729os_set_handle_inheritable(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007730{
7731 PyObject *return_value = NULL;
Victor Stinner581139c2016-09-06 15:54:20 -07007732 intptr_t handle;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007733 int inheritable;
7734
Sylvain74453812017-06-10 06:51:48 +02007735 if (!_PyArg_ParseStack(args, nargs, "" _Py_PARSE_INTPTR "p:set_handle_inheritable",
7736 &handle, &inheritable)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01007737 goto exit;
7738 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007739 return_value = os_set_handle_inheritable_impl(module, handle, inheritable);
7740
7741exit:
7742 return return_value;
7743}
7744
7745#endif /* defined(MS_WINDOWS) */
7746
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007747#if !defined(MS_WINDOWS)
7748
7749PyDoc_STRVAR(os_get_blocking__doc__,
7750"get_blocking($module, fd, /)\n"
7751"--\n"
7752"\n"
7753"Get the blocking mode of the file descriptor.\n"
7754"\n"
7755"Return False if the O_NONBLOCK flag is set, True if the flag is cleared.");
7756
7757#define OS_GET_BLOCKING_METHODDEF \
7758 {"get_blocking", (PyCFunction)os_get_blocking, METH_O, os_get_blocking__doc__},
7759
7760static int
7761os_get_blocking_impl(PyObject *module, int fd);
7762
7763static PyObject *
7764os_get_blocking(PyObject *module, PyObject *arg)
7765{
7766 PyObject *return_value = NULL;
7767 int fd;
7768 int _return_value;
7769
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02007770 if (PyFloat_Check(arg)) {
7771 PyErr_SetString(PyExc_TypeError,
7772 "integer argument expected, got float" );
7773 goto exit;
7774 }
7775 fd = _PyLong_AsInt(arg);
7776 if (fd == -1 && PyErr_Occurred()) {
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007777 goto exit;
7778 }
7779 _return_value = os_get_blocking_impl(module, fd);
7780 if ((_return_value == -1) && PyErr_Occurred()) {
7781 goto exit;
7782 }
7783 return_value = PyBool_FromLong((long)_return_value);
7784
7785exit:
7786 return return_value;
7787}
7788
7789#endif /* !defined(MS_WINDOWS) */
7790
7791#if !defined(MS_WINDOWS)
7792
7793PyDoc_STRVAR(os_set_blocking__doc__,
7794"set_blocking($module, fd, blocking, /)\n"
7795"--\n"
7796"\n"
7797"Set the blocking mode of the specified file descriptor.\n"
7798"\n"
7799"Set the O_NONBLOCK flag if blocking is False,\n"
7800"clear the O_NONBLOCK flag otherwise.");
7801
7802#define OS_SET_BLOCKING_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02007803 {"set_blocking", (PyCFunction)(void(*)(void))os_set_blocking, METH_FASTCALL, os_set_blocking__doc__},
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007804
7805static PyObject *
7806os_set_blocking_impl(PyObject *module, int fd, int blocking);
7807
7808static PyObject *
7809os_set_blocking(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
7810{
7811 PyObject *return_value = NULL;
7812 int fd;
7813 int blocking;
7814
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02007815 if (!_PyArg_CheckPositional("set_blocking", nargs, 2, 2)) {
7816 goto exit;
7817 }
7818 if (PyFloat_Check(args[0])) {
7819 PyErr_SetString(PyExc_TypeError,
7820 "integer argument expected, got float" );
7821 goto exit;
7822 }
7823 fd = _PyLong_AsInt(args[0]);
7824 if (fd == -1 && PyErr_Occurred()) {
7825 goto exit;
7826 }
7827 if (PyFloat_Check(args[1])) {
7828 PyErr_SetString(PyExc_TypeError,
7829 "integer argument expected, got float" );
7830 goto exit;
7831 }
7832 blocking = _PyLong_AsInt(args[1]);
7833 if (blocking == -1 && PyErr_Occurred()) {
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007834 goto exit;
7835 }
7836 return_value = os_set_blocking_impl(module, fd, blocking);
7837
7838exit:
7839 return return_value;
7840}
7841
7842#endif /* !defined(MS_WINDOWS) */
7843
Serhiy Storchaka49d02d12016-11-06 13:45:33 +02007844PyDoc_STRVAR(os_DirEntry_is_symlink__doc__,
7845"is_symlink($self, /)\n"
7846"--\n"
7847"\n"
7848"Return True if the entry is a symbolic link; cached per entry.");
7849
7850#define OS_DIRENTRY_IS_SYMLINK_METHODDEF \
7851 {"is_symlink", (PyCFunction)os_DirEntry_is_symlink, METH_NOARGS, os_DirEntry_is_symlink__doc__},
7852
7853static int
7854os_DirEntry_is_symlink_impl(DirEntry *self);
7855
7856static PyObject *
7857os_DirEntry_is_symlink(DirEntry *self, PyObject *Py_UNUSED(ignored))
7858{
7859 PyObject *return_value = NULL;
7860 int _return_value;
7861
7862 _return_value = os_DirEntry_is_symlink_impl(self);
7863 if ((_return_value == -1) && PyErr_Occurred()) {
7864 goto exit;
7865 }
7866 return_value = PyBool_FromLong((long)_return_value);
7867
7868exit:
7869 return return_value;
7870}
7871
7872PyDoc_STRVAR(os_DirEntry_stat__doc__,
7873"stat($self, /, *, follow_symlinks=True)\n"
7874"--\n"
7875"\n"
7876"Return stat_result object for the entry; cached per entry.");
7877
7878#define OS_DIRENTRY_STAT_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02007879 {"stat", (PyCFunction)(void(*)(void))os_DirEntry_stat, METH_FASTCALL|METH_KEYWORDS, os_DirEntry_stat__doc__},
Serhiy Storchaka49d02d12016-11-06 13:45:33 +02007880
7881static PyObject *
7882os_DirEntry_stat_impl(DirEntry *self, int follow_symlinks);
7883
7884static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02007885os_DirEntry_stat(DirEntry *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka49d02d12016-11-06 13:45:33 +02007886{
7887 PyObject *return_value = NULL;
7888 static const char * const _keywords[] = {"follow_symlinks", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02007889 static _PyArg_Parser _parser = {NULL, _keywords, "stat", 0};
7890 PyObject *argsbuf[1];
7891 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
Serhiy Storchaka49d02d12016-11-06 13:45:33 +02007892 int follow_symlinks = 1;
7893
Serhiy Storchaka31913912019-03-14 10:32:22 +02007894 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 0, 0, argsbuf);
7895 if (!args) {
Serhiy Storchaka49d02d12016-11-06 13:45:33 +02007896 goto exit;
7897 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02007898 if (!noptargs) {
7899 goto skip_optional_kwonly;
7900 }
7901 follow_symlinks = PyObject_IsTrue(args[0]);
7902 if (follow_symlinks < 0) {
7903 goto exit;
7904 }
7905skip_optional_kwonly:
Serhiy Storchaka49d02d12016-11-06 13:45:33 +02007906 return_value = os_DirEntry_stat_impl(self, follow_symlinks);
7907
7908exit:
7909 return return_value;
7910}
7911
7912PyDoc_STRVAR(os_DirEntry_is_dir__doc__,
7913"is_dir($self, /, *, follow_symlinks=True)\n"
7914"--\n"
7915"\n"
7916"Return True if the entry is a directory; cached per entry.");
7917
7918#define OS_DIRENTRY_IS_DIR_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02007919 {"is_dir", (PyCFunction)(void(*)(void))os_DirEntry_is_dir, METH_FASTCALL|METH_KEYWORDS, os_DirEntry_is_dir__doc__},
Serhiy Storchaka49d02d12016-11-06 13:45:33 +02007920
7921static int
7922os_DirEntry_is_dir_impl(DirEntry *self, int follow_symlinks);
7923
7924static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02007925os_DirEntry_is_dir(DirEntry *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka49d02d12016-11-06 13:45:33 +02007926{
7927 PyObject *return_value = NULL;
7928 static const char * const _keywords[] = {"follow_symlinks", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02007929 static _PyArg_Parser _parser = {NULL, _keywords, "is_dir", 0};
7930 PyObject *argsbuf[1];
7931 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
Serhiy Storchaka49d02d12016-11-06 13:45:33 +02007932 int follow_symlinks = 1;
7933 int _return_value;
7934
Serhiy Storchaka31913912019-03-14 10:32:22 +02007935 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 0, 0, argsbuf);
7936 if (!args) {
Serhiy Storchaka49d02d12016-11-06 13:45:33 +02007937 goto exit;
7938 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02007939 if (!noptargs) {
7940 goto skip_optional_kwonly;
7941 }
7942 follow_symlinks = PyObject_IsTrue(args[0]);
7943 if (follow_symlinks < 0) {
7944 goto exit;
7945 }
7946skip_optional_kwonly:
Serhiy Storchaka49d02d12016-11-06 13:45:33 +02007947 _return_value = os_DirEntry_is_dir_impl(self, follow_symlinks);
7948 if ((_return_value == -1) && PyErr_Occurred()) {
7949 goto exit;
7950 }
7951 return_value = PyBool_FromLong((long)_return_value);
7952
7953exit:
7954 return return_value;
7955}
7956
7957PyDoc_STRVAR(os_DirEntry_is_file__doc__,
7958"is_file($self, /, *, follow_symlinks=True)\n"
7959"--\n"
7960"\n"
7961"Return True if the entry is a file; cached per entry.");
7962
7963#define OS_DIRENTRY_IS_FILE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02007964 {"is_file", (PyCFunction)(void(*)(void))os_DirEntry_is_file, METH_FASTCALL|METH_KEYWORDS, os_DirEntry_is_file__doc__},
Serhiy Storchaka49d02d12016-11-06 13:45:33 +02007965
7966static int
7967os_DirEntry_is_file_impl(DirEntry *self, int follow_symlinks);
7968
7969static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02007970os_DirEntry_is_file(DirEntry *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka49d02d12016-11-06 13:45:33 +02007971{
7972 PyObject *return_value = NULL;
7973 static const char * const _keywords[] = {"follow_symlinks", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02007974 static _PyArg_Parser _parser = {NULL, _keywords, "is_file", 0};
7975 PyObject *argsbuf[1];
7976 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
Serhiy Storchaka49d02d12016-11-06 13:45:33 +02007977 int follow_symlinks = 1;
7978 int _return_value;
7979
Serhiy Storchaka31913912019-03-14 10:32:22 +02007980 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 0, 0, argsbuf);
7981 if (!args) {
Serhiy Storchaka49d02d12016-11-06 13:45:33 +02007982 goto exit;
7983 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02007984 if (!noptargs) {
7985 goto skip_optional_kwonly;
7986 }
7987 follow_symlinks = PyObject_IsTrue(args[0]);
7988 if (follow_symlinks < 0) {
7989 goto exit;
7990 }
7991skip_optional_kwonly:
Serhiy Storchaka49d02d12016-11-06 13:45:33 +02007992 _return_value = os_DirEntry_is_file_impl(self, follow_symlinks);
7993 if ((_return_value == -1) && PyErr_Occurred()) {
7994 goto exit;
7995 }
7996 return_value = PyBool_FromLong((long)_return_value);
7997
7998exit:
7999 return return_value;
8000}
8001
8002PyDoc_STRVAR(os_DirEntry_inode__doc__,
8003"inode($self, /)\n"
8004"--\n"
8005"\n"
8006"Return inode of the entry; cached per entry.");
8007
8008#define OS_DIRENTRY_INODE_METHODDEF \
8009 {"inode", (PyCFunction)os_DirEntry_inode, METH_NOARGS, os_DirEntry_inode__doc__},
8010
8011static PyObject *
8012os_DirEntry_inode_impl(DirEntry *self);
8013
8014static PyObject *
8015os_DirEntry_inode(DirEntry *self, PyObject *Py_UNUSED(ignored))
8016{
8017 return os_DirEntry_inode_impl(self);
8018}
8019
8020PyDoc_STRVAR(os_DirEntry___fspath____doc__,
8021"__fspath__($self, /)\n"
8022"--\n"
8023"\n"
8024"Returns the path for the entry.");
8025
8026#define OS_DIRENTRY___FSPATH___METHODDEF \
8027 {"__fspath__", (PyCFunction)os_DirEntry___fspath__, METH_NOARGS, os_DirEntry___fspath____doc__},
8028
8029static PyObject *
8030os_DirEntry___fspath___impl(DirEntry *self);
8031
8032static PyObject *
8033os_DirEntry___fspath__(DirEntry *self, PyObject *Py_UNUSED(ignored))
8034{
8035 return os_DirEntry___fspath___impl(self);
8036}
8037
8038PyDoc_STRVAR(os_scandir__doc__,
8039"scandir($module, /, path=None)\n"
8040"--\n"
8041"\n"
8042"Return an iterator of DirEntry objects for given path.\n"
8043"\n"
BNMetricsb9427072018-11-02 15:20:19 +00008044"path can be specified as either str, bytes, or a path-like object. If path\n"
Serhiy Storchaka49d02d12016-11-06 13:45:33 +02008045"is bytes, the names of yielded DirEntry objects will also be bytes; in\n"
8046"all other circumstances they will be str.\n"
8047"\n"
8048"If path is None, uses the path=\'.\'.");
8049
8050#define OS_SCANDIR_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02008051 {"scandir", (PyCFunction)(void(*)(void))os_scandir, METH_FASTCALL|METH_KEYWORDS, os_scandir__doc__},
Serhiy Storchaka49d02d12016-11-06 13:45:33 +02008052
8053static PyObject *
8054os_scandir_impl(PyObject *module, path_t *path);
8055
8056static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02008057os_scandir(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka49d02d12016-11-06 13:45:33 +02008058{
8059 PyObject *return_value = NULL;
8060 static const char * const _keywords[] = {"path", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02008061 static _PyArg_Parser _parser = {NULL, _keywords, "scandir", 0};
8062 PyObject *argsbuf[1];
8063 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
Serhiy Storchakaea720fe2017-03-30 09:12:31 +03008064 path_t path = PATH_T_INITIALIZE("scandir", "path", 1, PATH_HAVE_FDOPENDIR);
Serhiy Storchaka49d02d12016-11-06 13:45:33 +02008065
Serhiy Storchaka31913912019-03-14 10:32:22 +02008066 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
8067 if (!args) {
Serhiy Storchaka49d02d12016-11-06 13:45:33 +02008068 goto exit;
8069 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02008070 if (!noptargs) {
8071 goto skip_optional_pos;
8072 }
8073 if (!path_converter(args[0], &path)) {
8074 goto exit;
8075 }
8076skip_optional_pos:
Serhiy Storchaka49d02d12016-11-06 13:45:33 +02008077 return_value = os_scandir_impl(module, &path);
8078
8079exit:
8080 /* Cleanup for path */
8081 path_cleanup(&path);
8082
8083 return return_value;
8084}
8085
Ethan Furman410ef8e2016-06-04 12:06:26 -07008086PyDoc_STRVAR(os_fspath__doc__,
8087"fspath($module, /, path)\n"
8088"--\n"
8089"\n"
8090"Return the file system path representation of the object.\n"
8091"\n"
Brett Cannonb4f43e92016-06-09 14:32:08 -07008092"If the object is str or bytes, then allow it to pass through as-is. If the\n"
8093"object defines __fspath__(), then return the result of that method. All other\n"
8094"types raise a TypeError.");
Ethan Furman410ef8e2016-06-04 12:06:26 -07008095
8096#define OS_FSPATH_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02008097 {"fspath", (PyCFunction)(void(*)(void))os_fspath, METH_FASTCALL|METH_KEYWORDS, os_fspath__doc__},
Ethan Furman410ef8e2016-06-04 12:06:26 -07008098
8099static PyObject *
Serhiy Storchaka2954f832016-07-07 18:20:03 +03008100os_fspath_impl(PyObject *module, PyObject *path);
Ethan Furman410ef8e2016-06-04 12:06:26 -07008101
8102static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02008103os_fspath(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Ethan Furman410ef8e2016-06-04 12:06:26 -07008104{
8105 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03008106 static const char * const _keywords[] = {"path", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02008107 static _PyArg_Parser _parser = {NULL, _keywords, "fspath", 0};
8108 PyObject *argsbuf[1];
Ethan Furman410ef8e2016-06-04 12:06:26 -07008109 PyObject *path;
8110
Serhiy Storchaka31913912019-03-14 10:32:22 +02008111 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
8112 if (!args) {
Ethan Furman410ef8e2016-06-04 12:06:26 -07008113 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03008114 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02008115 path = args[0];
Ethan Furman410ef8e2016-06-04 12:06:26 -07008116 return_value = os_fspath_impl(module, path);
8117
8118exit:
8119 return return_value;
8120}
8121
Victor Stinner9b1f4742016-09-06 16:18:52 -07008122#if defined(HAVE_GETRANDOM_SYSCALL)
8123
8124PyDoc_STRVAR(os_getrandom__doc__,
8125"getrandom($module, /, size, flags=0)\n"
8126"--\n"
8127"\n"
8128"Obtain a series of random bytes.");
8129
8130#define OS_GETRANDOM_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02008131 {"getrandom", (PyCFunction)(void(*)(void))os_getrandom, METH_FASTCALL|METH_KEYWORDS, os_getrandom__doc__},
Victor Stinner9b1f4742016-09-06 16:18:52 -07008132
8133static PyObject *
8134os_getrandom_impl(PyObject *module, Py_ssize_t size, int flags);
8135
8136static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02008137os_getrandom(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Victor Stinner9b1f4742016-09-06 16:18:52 -07008138{
8139 PyObject *return_value = NULL;
8140 static const char * const _keywords[] = {"size", "flags", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02008141 static _PyArg_Parser _parser = {NULL, _keywords, "getrandom", 0};
8142 PyObject *argsbuf[2];
8143 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
Victor Stinner9b1f4742016-09-06 16:18:52 -07008144 Py_ssize_t size;
8145 int flags = 0;
8146
Serhiy Storchaka31913912019-03-14 10:32:22 +02008147 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
8148 if (!args) {
Victor Stinner9b1f4742016-09-06 16:18:52 -07008149 goto exit;
8150 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02008151 if (PyFloat_Check(args[0])) {
8152 PyErr_SetString(PyExc_TypeError,
8153 "integer argument expected, got float" );
8154 goto exit;
8155 }
8156 {
8157 Py_ssize_t ival = -1;
8158 PyObject *iobj = PyNumber_Index(args[0]);
8159 if (iobj != NULL) {
8160 ival = PyLong_AsSsize_t(iobj);
8161 Py_DECREF(iobj);
8162 }
8163 if (ival == -1 && PyErr_Occurred()) {
8164 goto exit;
8165 }
8166 size = ival;
8167 }
8168 if (!noptargs) {
8169 goto skip_optional_pos;
8170 }
8171 if (PyFloat_Check(args[1])) {
8172 PyErr_SetString(PyExc_TypeError,
8173 "integer argument expected, got float" );
8174 goto exit;
8175 }
8176 flags = _PyLong_AsInt(args[1]);
8177 if (flags == -1 && PyErr_Occurred()) {
8178 goto exit;
8179 }
8180skip_optional_pos:
Victor Stinner9b1f4742016-09-06 16:18:52 -07008181 return_value = os_getrandom_impl(module, size, flags);
8182
8183exit:
8184 return return_value;
8185}
8186
8187#endif /* defined(HAVE_GETRANDOM_SYSCALL) */
8188
Steve Dower2438cdf2019-03-29 16:37:16 -07008189#if defined(MS_WINDOWS)
8190
8191PyDoc_STRVAR(os__add_dll_directory__doc__,
8192"_add_dll_directory($module, /, path)\n"
8193"--\n"
8194"\n"
8195"Add a path to the DLL search path.\n"
8196"\n"
8197"This search path is used when resolving dependencies for imported\n"
8198"extension modules (the module itself is resolved through sys.path),\n"
8199"and also by ctypes.\n"
8200"\n"
8201"Returns an opaque value that may be passed to os.remove_dll_directory\n"
8202"to remove this directory from the search path.");
8203
8204#define OS__ADD_DLL_DIRECTORY_METHODDEF \
8205 {"_add_dll_directory", (PyCFunction)(void(*)(void))os__add_dll_directory, METH_FASTCALL|METH_KEYWORDS, os__add_dll_directory__doc__},
8206
8207static PyObject *
8208os__add_dll_directory_impl(PyObject *module, path_t *path);
8209
8210static PyObject *
8211os__add_dll_directory(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
8212{
8213 PyObject *return_value = NULL;
8214 static const char * const _keywords[] = {"path", NULL};
8215 static _PyArg_Parser _parser = {NULL, _keywords, "_add_dll_directory", 0};
8216 PyObject *argsbuf[1];
8217 path_t path = PATH_T_INITIALIZE("_add_dll_directory", "path", 0, 0);
8218
8219 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
8220 if (!args) {
8221 goto exit;
8222 }
8223 if (!path_converter(args[0], &path)) {
8224 goto exit;
8225 }
8226 return_value = os__add_dll_directory_impl(module, &path);
8227
8228exit:
8229 /* Cleanup for path */
8230 path_cleanup(&path);
8231
8232 return return_value;
8233}
8234
8235#endif /* defined(MS_WINDOWS) */
8236
8237#if defined(MS_WINDOWS)
8238
8239PyDoc_STRVAR(os__remove_dll_directory__doc__,
8240"_remove_dll_directory($module, /, cookie)\n"
8241"--\n"
8242"\n"
8243"Removes a path from the DLL search path.\n"
8244"\n"
8245"The parameter is an opaque value that was returned from\n"
8246"os.add_dll_directory. You can only remove directories that you added\n"
8247"yourself.");
8248
8249#define OS__REMOVE_DLL_DIRECTORY_METHODDEF \
8250 {"_remove_dll_directory", (PyCFunction)(void(*)(void))os__remove_dll_directory, METH_FASTCALL|METH_KEYWORDS, os__remove_dll_directory__doc__},
8251
8252static PyObject *
8253os__remove_dll_directory_impl(PyObject *module, PyObject *cookie);
8254
8255static PyObject *
8256os__remove_dll_directory(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
8257{
8258 PyObject *return_value = NULL;
8259 static const char * const _keywords[] = {"cookie", NULL};
8260 static _PyArg_Parser _parser = {NULL, _keywords, "_remove_dll_directory", 0};
8261 PyObject *argsbuf[1];
8262 PyObject *cookie;
8263
8264 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
8265 if (!args) {
8266 goto exit;
8267 }
8268 cookie = args[0];
8269 return_value = os__remove_dll_directory_impl(module, cookie);
8270
8271exit:
8272 return return_value;
8273}
8274
8275#endif /* defined(MS_WINDOWS) */
8276
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03008277#ifndef OS_TTYNAME_METHODDEF
8278 #define OS_TTYNAME_METHODDEF
8279#endif /* !defined(OS_TTYNAME_METHODDEF) */
8280
8281#ifndef OS_CTERMID_METHODDEF
8282 #define OS_CTERMID_METHODDEF
8283#endif /* !defined(OS_CTERMID_METHODDEF) */
8284
8285#ifndef OS_FCHDIR_METHODDEF
8286 #define OS_FCHDIR_METHODDEF
8287#endif /* !defined(OS_FCHDIR_METHODDEF) */
8288
8289#ifndef OS_FCHMOD_METHODDEF
8290 #define OS_FCHMOD_METHODDEF
8291#endif /* !defined(OS_FCHMOD_METHODDEF) */
8292
8293#ifndef OS_LCHMOD_METHODDEF
8294 #define OS_LCHMOD_METHODDEF
8295#endif /* !defined(OS_LCHMOD_METHODDEF) */
8296
8297#ifndef OS_CHFLAGS_METHODDEF
8298 #define OS_CHFLAGS_METHODDEF
8299#endif /* !defined(OS_CHFLAGS_METHODDEF) */
8300
8301#ifndef OS_LCHFLAGS_METHODDEF
8302 #define OS_LCHFLAGS_METHODDEF
8303#endif /* !defined(OS_LCHFLAGS_METHODDEF) */
8304
8305#ifndef OS_CHROOT_METHODDEF
8306 #define OS_CHROOT_METHODDEF
8307#endif /* !defined(OS_CHROOT_METHODDEF) */
8308
8309#ifndef OS_FSYNC_METHODDEF
8310 #define OS_FSYNC_METHODDEF
8311#endif /* !defined(OS_FSYNC_METHODDEF) */
8312
8313#ifndef OS_SYNC_METHODDEF
8314 #define OS_SYNC_METHODDEF
8315#endif /* !defined(OS_SYNC_METHODDEF) */
8316
8317#ifndef OS_FDATASYNC_METHODDEF
8318 #define OS_FDATASYNC_METHODDEF
8319#endif /* !defined(OS_FDATASYNC_METHODDEF) */
8320
8321#ifndef OS_CHOWN_METHODDEF
8322 #define OS_CHOWN_METHODDEF
8323#endif /* !defined(OS_CHOWN_METHODDEF) */
8324
8325#ifndef OS_FCHOWN_METHODDEF
8326 #define OS_FCHOWN_METHODDEF
8327#endif /* !defined(OS_FCHOWN_METHODDEF) */
8328
8329#ifndef OS_LCHOWN_METHODDEF
8330 #define OS_LCHOWN_METHODDEF
8331#endif /* !defined(OS_LCHOWN_METHODDEF) */
8332
8333#ifndef OS_LINK_METHODDEF
8334 #define OS_LINK_METHODDEF
8335#endif /* !defined(OS_LINK_METHODDEF) */
8336
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03008337#ifndef OS__GETFULLPATHNAME_METHODDEF
8338 #define OS__GETFULLPATHNAME_METHODDEF
8339#endif /* !defined(OS__GETFULLPATHNAME_METHODDEF) */
8340
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03008341#ifndef OS__GETFINALPATHNAME_METHODDEF
8342 #define OS__GETFINALPATHNAME_METHODDEF
8343#endif /* !defined(OS__GETFINALPATHNAME_METHODDEF) */
8344
8345#ifndef OS__GETVOLUMEPATHNAME_METHODDEF
8346 #define OS__GETVOLUMEPATHNAME_METHODDEF
8347#endif /* !defined(OS__GETVOLUMEPATHNAME_METHODDEF) */
8348
8349#ifndef OS_NICE_METHODDEF
8350 #define OS_NICE_METHODDEF
8351#endif /* !defined(OS_NICE_METHODDEF) */
8352
8353#ifndef OS_GETPRIORITY_METHODDEF
8354 #define OS_GETPRIORITY_METHODDEF
8355#endif /* !defined(OS_GETPRIORITY_METHODDEF) */
8356
8357#ifndef OS_SETPRIORITY_METHODDEF
8358 #define OS_SETPRIORITY_METHODDEF
8359#endif /* !defined(OS_SETPRIORITY_METHODDEF) */
8360
8361#ifndef OS_SYSTEM_METHODDEF
8362 #define OS_SYSTEM_METHODDEF
8363#endif /* !defined(OS_SYSTEM_METHODDEF) */
8364
8365#ifndef OS_UNAME_METHODDEF
8366 #define OS_UNAME_METHODDEF
8367#endif /* !defined(OS_UNAME_METHODDEF) */
8368
8369#ifndef OS_EXECV_METHODDEF
8370 #define OS_EXECV_METHODDEF
8371#endif /* !defined(OS_EXECV_METHODDEF) */
8372
8373#ifndef OS_EXECVE_METHODDEF
8374 #define OS_EXECVE_METHODDEF
8375#endif /* !defined(OS_EXECVE_METHODDEF) */
8376
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00008377#ifndef OS_POSIX_SPAWN_METHODDEF
8378 #define OS_POSIX_SPAWN_METHODDEF
8379#endif /* !defined(OS_POSIX_SPAWN_METHODDEF) */
8380
Joannah Nanjekye92b83222019-01-16 16:29:26 +03008381#ifndef OS_POSIX_SPAWNP_METHODDEF
8382 #define OS_POSIX_SPAWNP_METHODDEF
8383#endif /* !defined(OS_POSIX_SPAWNP_METHODDEF) */
8384
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03008385#ifndef OS_SPAWNV_METHODDEF
8386 #define OS_SPAWNV_METHODDEF
8387#endif /* !defined(OS_SPAWNV_METHODDEF) */
8388
8389#ifndef OS_SPAWNVE_METHODDEF
8390 #define OS_SPAWNVE_METHODDEF
8391#endif /* !defined(OS_SPAWNVE_METHODDEF) */
8392
Antoine Pitrou346cbd32017-05-27 17:50:54 +02008393#ifndef OS_REGISTER_AT_FORK_METHODDEF
8394 #define OS_REGISTER_AT_FORK_METHODDEF
8395#endif /* !defined(OS_REGISTER_AT_FORK_METHODDEF) */
8396
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03008397#ifndef OS_FORK1_METHODDEF
8398 #define OS_FORK1_METHODDEF
8399#endif /* !defined(OS_FORK1_METHODDEF) */
8400
8401#ifndef OS_FORK_METHODDEF
8402 #define OS_FORK_METHODDEF
8403#endif /* !defined(OS_FORK_METHODDEF) */
8404
8405#ifndef OS_SCHED_GET_PRIORITY_MAX_METHODDEF
8406 #define OS_SCHED_GET_PRIORITY_MAX_METHODDEF
8407#endif /* !defined(OS_SCHED_GET_PRIORITY_MAX_METHODDEF) */
8408
8409#ifndef OS_SCHED_GET_PRIORITY_MIN_METHODDEF
8410 #define OS_SCHED_GET_PRIORITY_MIN_METHODDEF
8411#endif /* !defined(OS_SCHED_GET_PRIORITY_MIN_METHODDEF) */
8412
8413#ifndef OS_SCHED_GETSCHEDULER_METHODDEF
8414 #define OS_SCHED_GETSCHEDULER_METHODDEF
8415#endif /* !defined(OS_SCHED_GETSCHEDULER_METHODDEF) */
8416
8417#ifndef OS_SCHED_SETSCHEDULER_METHODDEF
8418 #define OS_SCHED_SETSCHEDULER_METHODDEF
8419#endif /* !defined(OS_SCHED_SETSCHEDULER_METHODDEF) */
8420
8421#ifndef OS_SCHED_GETPARAM_METHODDEF
8422 #define OS_SCHED_GETPARAM_METHODDEF
8423#endif /* !defined(OS_SCHED_GETPARAM_METHODDEF) */
8424
8425#ifndef OS_SCHED_SETPARAM_METHODDEF
8426 #define OS_SCHED_SETPARAM_METHODDEF
8427#endif /* !defined(OS_SCHED_SETPARAM_METHODDEF) */
8428
8429#ifndef OS_SCHED_RR_GET_INTERVAL_METHODDEF
8430 #define OS_SCHED_RR_GET_INTERVAL_METHODDEF
8431#endif /* !defined(OS_SCHED_RR_GET_INTERVAL_METHODDEF) */
8432
8433#ifndef OS_SCHED_YIELD_METHODDEF
8434 #define OS_SCHED_YIELD_METHODDEF
8435#endif /* !defined(OS_SCHED_YIELD_METHODDEF) */
8436
8437#ifndef OS_SCHED_SETAFFINITY_METHODDEF
8438 #define OS_SCHED_SETAFFINITY_METHODDEF
8439#endif /* !defined(OS_SCHED_SETAFFINITY_METHODDEF) */
8440
8441#ifndef OS_SCHED_GETAFFINITY_METHODDEF
8442 #define OS_SCHED_GETAFFINITY_METHODDEF
8443#endif /* !defined(OS_SCHED_GETAFFINITY_METHODDEF) */
8444
8445#ifndef OS_OPENPTY_METHODDEF
8446 #define OS_OPENPTY_METHODDEF
8447#endif /* !defined(OS_OPENPTY_METHODDEF) */
8448
8449#ifndef OS_FORKPTY_METHODDEF
8450 #define OS_FORKPTY_METHODDEF
8451#endif /* !defined(OS_FORKPTY_METHODDEF) */
8452
8453#ifndef OS_GETEGID_METHODDEF
8454 #define OS_GETEGID_METHODDEF
8455#endif /* !defined(OS_GETEGID_METHODDEF) */
8456
8457#ifndef OS_GETEUID_METHODDEF
8458 #define OS_GETEUID_METHODDEF
8459#endif /* !defined(OS_GETEUID_METHODDEF) */
8460
8461#ifndef OS_GETGID_METHODDEF
8462 #define OS_GETGID_METHODDEF
8463#endif /* !defined(OS_GETGID_METHODDEF) */
8464
Berker Peksag39404992016-09-15 20:45:16 +03008465#ifndef OS_GETPID_METHODDEF
8466 #define OS_GETPID_METHODDEF
8467#endif /* !defined(OS_GETPID_METHODDEF) */
8468
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03008469#ifndef OS_GETGROUPS_METHODDEF
8470 #define OS_GETGROUPS_METHODDEF
8471#endif /* !defined(OS_GETGROUPS_METHODDEF) */
8472
8473#ifndef OS_GETPGID_METHODDEF
8474 #define OS_GETPGID_METHODDEF
8475#endif /* !defined(OS_GETPGID_METHODDEF) */
8476
8477#ifndef OS_GETPGRP_METHODDEF
8478 #define OS_GETPGRP_METHODDEF
8479#endif /* !defined(OS_GETPGRP_METHODDEF) */
8480
8481#ifndef OS_SETPGRP_METHODDEF
8482 #define OS_SETPGRP_METHODDEF
8483#endif /* !defined(OS_SETPGRP_METHODDEF) */
8484
8485#ifndef OS_GETPPID_METHODDEF
8486 #define OS_GETPPID_METHODDEF
8487#endif /* !defined(OS_GETPPID_METHODDEF) */
8488
8489#ifndef OS_GETLOGIN_METHODDEF
8490 #define OS_GETLOGIN_METHODDEF
8491#endif /* !defined(OS_GETLOGIN_METHODDEF) */
8492
8493#ifndef OS_GETUID_METHODDEF
8494 #define OS_GETUID_METHODDEF
8495#endif /* !defined(OS_GETUID_METHODDEF) */
8496
8497#ifndef OS_KILL_METHODDEF
8498 #define OS_KILL_METHODDEF
8499#endif /* !defined(OS_KILL_METHODDEF) */
8500
8501#ifndef OS_KILLPG_METHODDEF
8502 #define OS_KILLPG_METHODDEF
8503#endif /* !defined(OS_KILLPG_METHODDEF) */
8504
8505#ifndef OS_PLOCK_METHODDEF
8506 #define OS_PLOCK_METHODDEF
8507#endif /* !defined(OS_PLOCK_METHODDEF) */
8508
8509#ifndef OS_SETUID_METHODDEF
8510 #define OS_SETUID_METHODDEF
8511#endif /* !defined(OS_SETUID_METHODDEF) */
8512
8513#ifndef OS_SETEUID_METHODDEF
8514 #define OS_SETEUID_METHODDEF
8515#endif /* !defined(OS_SETEUID_METHODDEF) */
8516
8517#ifndef OS_SETEGID_METHODDEF
8518 #define OS_SETEGID_METHODDEF
8519#endif /* !defined(OS_SETEGID_METHODDEF) */
8520
8521#ifndef OS_SETREUID_METHODDEF
8522 #define OS_SETREUID_METHODDEF
8523#endif /* !defined(OS_SETREUID_METHODDEF) */
8524
8525#ifndef OS_SETREGID_METHODDEF
8526 #define OS_SETREGID_METHODDEF
8527#endif /* !defined(OS_SETREGID_METHODDEF) */
8528
8529#ifndef OS_SETGID_METHODDEF
8530 #define OS_SETGID_METHODDEF
8531#endif /* !defined(OS_SETGID_METHODDEF) */
8532
8533#ifndef OS_SETGROUPS_METHODDEF
8534 #define OS_SETGROUPS_METHODDEF
8535#endif /* !defined(OS_SETGROUPS_METHODDEF) */
8536
8537#ifndef OS_WAIT3_METHODDEF
8538 #define OS_WAIT3_METHODDEF
8539#endif /* !defined(OS_WAIT3_METHODDEF) */
8540
8541#ifndef OS_WAIT4_METHODDEF
8542 #define OS_WAIT4_METHODDEF
8543#endif /* !defined(OS_WAIT4_METHODDEF) */
8544
8545#ifndef OS_WAITID_METHODDEF
8546 #define OS_WAITID_METHODDEF
8547#endif /* !defined(OS_WAITID_METHODDEF) */
8548
8549#ifndef OS_WAITPID_METHODDEF
8550 #define OS_WAITPID_METHODDEF
8551#endif /* !defined(OS_WAITPID_METHODDEF) */
8552
8553#ifndef OS_WAIT_METHODDEF
8554 #define OS_WAIT_METHODDEF
8555#endif /* !defined(OS_WAIT_METHODDEF) */
8556
Benjamin Peterson6c4c45e2019-11-05 19:21:29 -08008557#ifndef OS_PIDFD_OPEN_METHODDEF
8558 #define OS_PIDFD_OPEN_METHODDEF
8559#endif /* !defined(OS_PIDFD_OPEN_METHODDEF) */
8560
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03008561#ifndef OS_READLINK_METHODDEF
8562 #define OS_READLINK_METHODDEF
8563#endif /* !defined(OS_READLINK_METHODDEF) */
8564
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03008565#ifndef OS_SYMLINK_METHODDEF
8566 #define OS_SYMLINK_METHODDEF
8567#endif /* !defined(OS_SYMLINK_METHODDEF) */
8568
8569#ifndef OS_TIMES_METHODDEF
8570 #define OS_TIMES_METHODDEF
8571#endif /* !defined(OS_TIMES_METHODDEF) */
8572
8573#ifndef OS_GETSID_METHODDEF
8574 #define OS_GETSID_METHODDEF
8575#endif /* !defined(OS_GETSID_METHODDEF) */
8576
8577#ifndef OS_SETSID_METHODDEF
8578 #define OS_SETSID_METHODDEF
8579#endif /* !defined(OS_SETSID_METHODDEF) */
8580
8581#ifndef OS_SETPGID_METHODDEF
8582 #define OS_SETPGID_METHODDEF
8583#endif /* !defined(OS_SETPGID_METHODDEF) */
8584
8585#ifndef OS_TCGETPGRP_METHODDEF
8586 #define OS_TCGETPGRP_METHODDEF
8587#endif /* !defined(OS_TCGETPGRP_METHODDEF) */
8588
8589#ifndef OS_TCSETPGRP_METHODDEF
8590 #define OS_TCSETPGRP_METHODDEF
8591#endif /* !defined(OS_TCSETPGRP_METHODDEF) */
8592
8593#ifndef OS_LOCKF_METHODDEF
8594 #define OS_LOCKF_METHODDEF
8595#endif /* !defined(OS_LOCKF_METHODDEF) */
8596
8597#ifndef OS_READV_METHODDEF
8598 #define OS_READV_METHODDEF
8599#endif /* !defined(OS_READV_METHODDEF) */
8600
8601#ifndef OS_PREAD_METHODDEF
8602 #define OS_PREAD_METHODDEF
8603#endif /* !defined(OS_PREAD_METHODDEF) */
8604
Pablo Galindo4defba32018-01-27 16:16:37 +00008605#ifndef OS_PREADV_METHODDEF
8606 #define OS_PREADV_METHODDEF
8607#endif /* !defined(OS_PREADV_METHODDEF) */
8608
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02008609#ifndef OS__FCOPYFILE_METHODDEF
8610 #define OS__FCOPYFILE_METHODDEF
8611#endif /* !defined(OS__FCOPYFILE_METHODDEF) */
8612
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03008613#ifndef OS_PIPE_METHODDEF
8614 #define OS_PIPE_METHODDEF
8615#endif /* !defined(OS_PIPE_METHODDEF) */
8616
8617#ifndef OS_PIPE2_METHODDEF
8618 #define OS_PIPE2_METHODDEF
8619#endif /* !defined(OS_PIPE2_METHODDEF) */
8620
8621#ifndef OS_WRITEV_METHODDEF
8622 #define OS_WRITEV_METHODDEF
8623#endif /* !defined(OS_WRITEV_METHODDEF) */
8624
8625#ifndef OS_PWRITE_METHODDEF
8626 #define OS_PWRITE_METHODDEF
8627#endif /* !defined(OS_PWRITE_METHODDEF) */
8628
Pablo Galindo4defba32018-01-27 16:16:37 +00008629#ifndef OS_PWRITEV_METHODDEF
8630 #define OS_PWRITEV_METHODDEF
8631#endif /* !defined(OS_PWRITEV_METHODDEF) */
8632
Pablo Galindoaac4d032019-05-31 19:39:47 +01008633#ifndef OS_COPY_FILE_RANGE_METHODDEF
8634 #define OS_COPY_FILE_RANGE_METHODDEF
8635#endif /* !defined(OS_COPY_FILE_RANGE_METHODDEF) */
8636
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03008637#ifndef OS_MKFIFO_METHODDEF
8638 #define OS_MKFIFO_METHODDEF
8639#endif /* !defined(OS_MKFIFO_METHODDEF) */
8640
8641#ifndef OS_MKNOD_METHODDEF
8642 #define OS_MKNOD_METHODDEF
8643#endif /* !defined(OS_MKNOD_METHODDEF) */
8644
8645#ifndef OS_MAJOR_METHODDEF
8646 #define OS_MAJOR_METHODDEF
8647#endif /* !defined(OS_MAJOR_METHODDEF) */
8648
8649#ifndef OS_MINOR_METHODDEF
8650 #define OS_MINOR_METHODDEF
8651#endif /* !defined(OS_MINOR_METHODDEF) */
8652
8653#ifndef OS_MAKEDEV_METHODDEF
8654 #define OS_MAKEDEV_METHODDEF
8655#endif /* !defined(OS_MAKEDEV_METHODDEF) */
8656
8657#ifndef OS_FTRUNCATE_METHODDEF
8658 #define OS_FTRUNCATE_METHODDEF
8659#endif /* !defined(OS_FTRUNCATE_METHODDEF) */
8660
8661#ifndef OS_TRUNCATE_METHODDEF
8662 #define OS_TRUNCATE_METHODDEF
8663#endif /* !defined(OS_TRUNCATE_METHODDEF) */
8664
8665#ifndef OS_POSIX_FALLOCATE_METHODDEF
8666 #define OS_POSIX_FALLOCATE_METHODDEF
8667#endif /* !defined(OS_POSIX_FALLOCATE_METHODDEF) */
8668
8669#ifndef OS_POSIX_FADVISE_METHODDEF
8670 #define OS_POSIX_FADVISE_METHODDEF
8671#endif /* !defined(OS_POSIX_FADVISE_METHODDEF) */
8672
8673#ifndef OS_PUTENV_METHODDEF
8674 #define OS_PUTENV_METHODDEF
8675#endif /* !defined(OS_PUTENV_METHODDEF) */
8676
8677#ifndef OS_UNSETENV_METHODDEF
8678 #define OS_UNSETENV_METHODDEF
8679#endif /* !defined(OS_UNSETENV_METHODDEF) */
8680
8681#ifndef OS_WCOREDUMP_METHODDEF
8682 #define OS_WCOREDUMP_METHODDEF
8683#endif /* !defined(OS_WCOREDUMP_METHODDEF) */
8684
8685#ifndef OS_WIFCONTINUED_METHODDEF
8686 #define OS_WIFCONTINUED_METHODDEF
8687#endif /* !defined(OS_WIFCONTINUED_METHODDEF) */
8688
8689#ifndef OS_WIFSTOPPED_METHODDEF
8690 #define OS_WIFSTOPPED_METHODDEF
8691#endif /* !defined(OS_WIFSTOPPED_METHODDEF) */
8692
8693#ifndef OS_WIFSIGNALED_METHODDEF
8694 #define OS_WIFSIGNALED_METHODDEF
8695#endif /* !defined(OS_WIFSIGNALED_METHODDEF) */
8696
8697#ifndef OS_WIFEXITED_METHODDEF
8698 #define OS_WIFEXITED_METHODDEF
8699#endif /* !defined(OS_WIFEXITED_METHODDEF) */
8700
8701#ifndef OS_WEXITSTATUS_METHODDEF
8702 #define OS_WEXITSTATUS_METHODDEF
8703#endif /* !defined(OS_WEXITSTATUS_METHODDEF) */
8704
8705#ifndef OS_WTERMSIG_METHODDEF
8706 #define OS_WTERMSIG_METHODDEF
8707#endif /* !defined(OS_WTERMSIG_METHODDEF) */
8708
8709#ifndef OS_WSTOPSIG_METHODDEF
8710 #define OS_WSTOPSIG_METHODDEF
8711#endif /* !defined(OS_WSTOPSIG_METHODDEF) */
8712
8713#ifndef OS_FSTATVFS_METHODDEF
8714 #define OS_FSTATVFS_METHODDEF
8715#endif /* !defined(OS_FSTATVFS_METHODDEF) */
8716
8717#ifndef OS_STATVFS_METHODDEF
8718 #define OS_STATVFS_METHODDEF
8719#endif /* !defined(OS_STATVFS_METHODDEF) */
8720
8721#ifndef OS__GETDISKUSAGE_METHODDEF
8722 #define OS__GETDISKUSAGE_METHODDEF
8723#endif /* !defined(OS__GETDISKUSAGE_METHODDEF) */
8724
8725#ifndef OS_FPATHCONF_METHODDEF
8726 #define OS_FPATHCONF_METHODDEF
8727#endif /* !defined(OS_FPATHCONF_METHODDEF) */
8728
8729#ifndef OS_PATHCONF_METHODDEF
8730 #define OS_PATHCONF_METHODDEF
8731#endif /* !defined(OS_PATHCONF_METHODDEF) */
8732
8733#ifndef OS_CONFSTR_METHODDEF
8734 #define OS_CONFSTR_METHODDEF
8735#endif /* !defined(OS_CONFSTR_METHODDEF) */
8736
8737#ifndef OS_SYSCONF_METHODDEF
8738 #define OS_SYSCONF_METHODDEF
8739#endif /* !defined(OS_SYSCONF_METHODDEF) */
8740
Steve Dowercc16be82016-09-08 10:35:16 -07008741#ifndef OS_STARTFILE_METHODDEF
8742 #define OS_STARTFILE_METHODDEF
8743#endif /* !defined(OS_STARTFILE_METHODDEF) */
8744
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03008745#ifndef OS_GETLOADAVG_METHODDEF
8746 #define OS_GETLOADAVG_METHODDEF
8747#endif /* !defined(OS_GETLOADAVG_METHODDEF) */
8748
8749#ifndef OS_SETRESUID_METHODDEF
8750 #define OS_SETRESUID_METHODDEF
8751#endif /* !defined(OS_SETRESUID_METHODDEF) */
8752
8753#ifndef OS_SETRESGID_METHODDEF
8754 #define OS_SETRESGID_METHODDEF
8755#endif /* !defined(OS_SETRESGID_METHODDEF) */
8756
8757#ifndef OS_GETRESUID_METHODDEF
8758 #define OS_GETRESUID_METHODDEF
8759#endif /* !defined(OS_GETRESUID_METHODDEF) */
8760
8761#ifndef OS_GETRESGID_METHODDEF
8762 #define OS_GETRESGID_METHODDEF
8763#endif /* !defined(OS_GETRESGID_METHODDEF) */
8764
8765#ifndef OS_GETXATTR_METHODDEF
8766 #define OS_GETXATTR_METHODDEF
8767#endif /* !defined(OS_GETXATTR_METHODDEF) */
8768
8769#ifndef OS_SETXATTR_METHODDEF
8770 #define OS_SETXATTR_METHODDEF
8771#endif /* !defined(OS_SETXATTR_METHODDEF) */
8772
8773#ifndef OS_REMOVEXATTR_METHODDEF
8774 #define OS_REMOVEXATTR_METHODDEF
8775#endif /* !defined(OS_REMOVEXATTR_METHODDEF) */
8776
8777#ifndef OS_LISTXATTR_METHODDEF
8778 #define OS_LISTXATTR_METHODDEF
8779#endif /* !defined(OS_LISTXATTR_METHODDEF) */
8780
Zackery Spytz43fdbd22019-05-29 13:57:07 -06008781#ifndef OS_MEMFD_CREATE_METHODDEF
8782 #define OS_MEMFD_CREATE_METHODDEF
8783#endif /* !defined(OS_MEMFD_CREATE_METHODDEF) */
8784
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03008785#ifndef OS_GET_HANDLE_INHERITABLE_METHODDEF
8786 #define OS_GET_HANDLE_INHERITABLE_METHODDEF
8787#endif /* !defined(OS_GET_HANDLE_INHERITABLE_METHODDEF) */
8788
8789#ifndef OS_SET_HANDLE_INHERITABLE_METHODDEF
8790 #define OS_SET_HANDLE_INHERITABLE_METHODDEF
8791#endif /* !defined(OS_SET_HANDLE_INHERITABLE_METHODDEF) */
Victor Stinner9b1f4742016-09-06 16:18:52 -07008792
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03008793#ifndef OS_GET_BLOCKING_METHODDEF
8794 #define OS_GET_BLOCKING_METHODDEF
8795#endif /* !defined(OS_GET_BLOCKING_METHODDEF) */
8796
8797#ifndef OS_SET_BLOCKING_METHODDEF
8798 #define OS_SET_BLOCKING_METHODDEF
8799#endif /* !defined(OS_SET_BLOCKING_METHODDEF) */
8800
Victor Stinner9b1f4742016-09-06 16:18:52 -07008801#ifndef OS_GETRANDOM_METHODDEF
8802 #define OS_GETRANDOM_METHODDEF
8803#endif /* !defined(OS_GETRANDOM_METHODDEF) */
Steve Dower2438cdf2019-03-29 16:37:16 -07008804
8805#ifndef OS__ADD_DLL_DIRECTORY_METHODDEF
8806 #define OS__ADD_DLL_DIRECTORY_METHODDEF
8807#endif /* !defined(OS__ADD_DLL_DIRECTORY_METHODDEF) */
8808
8809#ifndef OS__REMOVE_DLL_DIRECTORY_METHODDEF
8810 #define OS__REMOVE_DLL_DIRECTORY_METHODDEF
8811#endif /* !defined(OS__REMOVE_DLL_DIRECTORY_METHODDEF) */
Victor Stinnerb8d12622020-01-24 14:05:48 +01008812/*[clinic end generated code: output=5d99f90cead7c0e1 input=a9049054013a1b77]*/