blob: 22cb94761de5c75d0b249b705c69c9bd5032130e [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])) {
619 _PyArg_BadArgument("chflags", 2, "int", args[1]);
620 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])) {
677 _PyArg_BadArgument("lchflags", 2, "int", args[1]);
678 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
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03001275PyDoc_STRVAR(os__isdir__doc__,
1276"_isdir($module, path, /)\n"
1277"--\n"
Serhiy Storchaka579f0382016-11-08 20:21:22 +02001278"\n"
1279"Return true if the pathname refers to an existing directory.");
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03001280
1281#define OS__ISDIR_METHODDEF \
1282 {"_isdir", (PyCFunction)os__isdir, METH_O, os__isdir__doc__},
1283
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03001284#endif /* defined(MS_WINDOWS) */
1285
1286#if defined(MS_WINDOWS)
1287
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001288PyDoc_STRVAR(os__getvolumepathname__doc__,
1289"_getvolumepathname($module, /, path)\n"
1290"--\n"
1291"\n"
1292"A helper function for ismount on Win32.");
1293
1294#define OS__GETVOLUMEPATHNAME_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02001295 {"_getvolumepathname", (PyCFunction)(void(*)(void))os__getvolumepathname, METH_FASTCALL|METH_KEYWORDS, os__getvolumepathname__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001296
1297static PyObject *
Steve Dower23ad6d02018-02-22 10:39:10 -08001298os__getvolumepathname_impl(PyObject *module, path_t *path);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001299
1300static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001301os__getvolumepathname(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001302{
1303 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001304 static const char * const _keywords[] = {"path", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02001305 static _PyArg_Parser _parser = {NULL, _keywords, "_getvolumepathname", 0};
1306 PyObject *argsbuf[1];
Steve Dower23ad6d02018-02-22 10:39:10 -08001307 path_t path = PATH_T_INITIALIZE("_getvolumepathname", "path", 0, 0);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001308
Serhiy Storchaka31913912019-03-14 10:32:22 +02001309 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
1310 if (!args) {
1311 goto exit;
1312 }
1313 if (!path_converter(args[0], &path)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001314 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001315 }
Steve Dower23ad6d02018-02-22 10:39:10 -08001316 return_value = os__getvolumepathname_impl(module, &path);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001317
1318exit:
Steve Dower23ad6d02018-02-22 10:39:10 -08001319 /* Cleanup for path */
1320 path_cleanup(&path);
1321
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001322 return return_value;
1323}
1324
1325#endif /* defined(MS_WINDOWS) */
1326
1327PyDoc_STRVAR(os_mkdir__doc__,
1328"mkdir($module, /, path, mode=511, *, dir_fd=None)\n"
1329"--\n"
1330"\n"
1331"Create a directory.\n"
1332"\n"
1333"If dir_fd is not None, it should be a file descriptor open to a directory,\n"
1334" and path should be relative; path will then be relative to that directory.\n"
1335"dir_fd may not be implemented on your platform.\n"
1336" If it is unavailable, using it will raise a NotImplementedError.\n"
1337"\n"
1338"The mode argument is ignored on Windows.");
1339
1340#define OS_MKDIR_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02001341 {"mkdir", (PyCFunction)(void(*)(void))os_mkdir, METH_FASTCALL|METH_KEYWORDS, os_mkdir__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001342
1343static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001344os_mkdir_impl(PyObject *module, path_t *path, int mode, int dir_fd);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001345
1346static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001347os_mkdir(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001348{
1349 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001350 static const char * const _keywords[] = {"path", "mode", "dir_fd", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02001351 static _PyArg_Parser _parser = {NULL, _keywords, "mkdir", 0};
1352 PyObject *argsbuf[3];
1353 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001354 path_t path = PATH_T_INITIALIZE("mkdir", "path", 0, 0);
1355 int mode = 511;
1356 int dir_fd = DEFAULT_DIR_FD;
1357
Serhiy Storchaka31913912019-03-14 10:32:22 +02001358 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
1359 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001360 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001361 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02001362 if (!path_converter(args[0], &path)) {
1363 goto exit;
1364 }
1365 if (!noptargs) {
1366 goto skip_optional_pos;
1367 }
1368 if (args[1]) {
1369 if (PyFloat_Check(args[1])) {
1370 PyErr_SetString(PyExc_TypeError,
1371 "integer argument expected, got float" );
1372 goto exit;
1373 }
1374 mode = _PyLong_AsInt(args[1]);
1375 if (mode == -1 && PyErr_Occurred()) {
1376 goto exit;
1377 }
1378 if (!--noptargs) {
1379 goto skip_optional_pos;
1380 }
1381 }
1382skip_optional_pos:
1383 if (!noptargs) {
1384 goto skip_optional_kwonly;
1385 }
1386 if (!MKDIRAT_DIR_FD_CONVERTER(args[2], &dir_fd)) {
1387 goto exit;
1388 }
1389skip_optional_kwonly:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001390 return_value = os_mkdir_impl(module, &path, mode, dir_fd);
1391
1392exit:
1393 /* Cleanup for path */
1394 path_cleanup(&path);
1395
1396 return return_value;
1397}
1398
1399#if defined(HAVE_NICE)
1400
1401PyDoc_STRVAR(os_nice__doc__,
1402"nice($module, increment, /)\n"
1403"--\n"
1404"\n"
1405"Add increment to the priority of process and return the new priority.");
1406
1407#define OS_NICE_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03001408 {"nice", (PyCFunction)os_nice, METH_O, os_nice__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001409
1410static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001411os_nice_impl(PyObject *module, int increment);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001412
1413static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001414os_nice(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001415{
1416 PyObject *return_value = NULL;
1417 int increment;
1418
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02001419 if (PyFloat_Check(arg)) {
1420 PyErr_SetString(PyExc_TypeError,
1421 "integer argument expected, got float" );
1422 goto exit;
1423 }
1424 increment = _PyLong_AsInt(arg);
1425 if (increment == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001426 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001427 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001428 return_value = os_nice_impl(module, increment);
1429
1430exit:
1431 return return_value;
1432}
1433
1434#endif /* defined(HAVE_NICE) */
1435
1436#if defined(HAVE_GETPRIORITY)
1437
1438PyDoc_STRVAR(os_getpriority__doc__,
1439"getpriority($module, /, which, who)\n"
1440"--\n"
1441"\n"
1442"Return program scheduling priority.");
1443
1444#define OS_GETPRIORITY_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02001445 {"getpriority", (PyCFunction)(void(*)(void))os_getpriority, METH_FASTCALL|METH_KEYWORDS, os_getpriority__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001446
1447static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001448os_getpriority_impl(PyObject *module, int which, int who);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001449
1450static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001451os_getpriority(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001452{
1453 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001454 static const char * const _keywords[] = {"which", "who", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02001455 static _PyArg_Parser _parser = {NULL, _keywords, "getpriority", 0};
1456 PyObject *argsbuf[2];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001457 int which;
1458 int who;
1459
Serhiy Storchaka31913912019-03-14 10:32:22 +02001460 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf);
1461 if (!args) {
1462 goto exit;
1463 }
1464 if (PyFloat_Check(args[0])) {
1465 PyErr_SetString(PyExc_TypeError,
1466 "integer argument expected, got float" );
1467 goto exit;
1468 }
1469 which = _PyLong_AsInt(args[0]);
1470 if (which == -1 && PyErr_Occurred()) {
1471 goto exit;
1472 }
1473 if (PyFloat_Check(args[1])) {
1474 PyErr_SetString(PyExc_TypeError,
1475 "integer argument expected, got float" );
1476 goto exit;
1477 }
1478 who = _PyLong_AsInt(args[1]);
1479 if (who == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001480 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001481 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001482 return_value = os_getpriority_impl(module, which, who);
1483
1484exit:
1485 return return_value;
1486}
1487
1488#endif /* defined(HAVE_GETPRIORITY) */
1489
1490#if defined(HAVE_SETPRIORITY)
1491
1492PyDoc_STRVAR(os_setpriority__doc__,
1493"setpriority($module, /, which, who, priority)\n"
1494"--\n"
1495"\n"
1496"Set program scheduling priority.");
1497
1498#define OS_SETPRIORITY_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02001499 {"setpriority", (PyCFunction)(void(*)(void))os_setpriority, METH_FASTCALL|METH_KEYWORDS, os_setpriority__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001500
1501static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001502os_setpriority_impl(PyObject *module, int which, int who, int priority);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001503
1504static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001505os_setpriority(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001506{
1507 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001508 static const char * const _keywords[] = {"which", "who", "priority", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02001509 static _PyArg_Parser _parser = {NULL, _keywords, "setpriority", 0};
1510 PyObject *argsbuf[3];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001511 int which;
1512 int who;
1513 int priority;
1514
Serhiy Storchaka31913912019-03-14 10:32:22 +02001515 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 3, 3, 0, argsbuf);
1516 if (!args) {
1517 goto exit;
1518 }
1519 if (PyFloat_Check(args[0])) {
1520 PyErr_SetString(PyExc_TypeError,
1521 "integer argument expected, got float" );
1522 goto exit;
1523 }
1524 which = _PyLong_AsInt(args[0]);
1525 if (which == -1 && PyErr_Occurred()) {
1526 goto exit;
1527 }
1528 if (PyFloat_Check(args[1])) {
1529 PyErr_SetString(PyExc_TypeError,
1530 "integer argument expected, got float" );
1531 goto exit;
1532 }
1533 who = _PyLong_AsInt(args[1]);
1534 if (who == -1 && PyErr_Occurred()) {
1535 goto exit;
1536 }
1537 if (PyFloat_Check(args[2])) {
1538 PyErr_SetString(PyExc_TypeError,
1539 "integer argument expected, got float" );
1540 goto exit;
1541 }
1542 priority = _PyLong_AsInt(args[2]);
1543 if (priority == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001544 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001545 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001546 return_value = os_setpriority_impl(module, which, who, priority);
1547
1548exit:
1549 return return_value;
1550}
1551
1552#endif /* defined(HAVE_SETPRIORITY) */
1553
1554PyDoc_STRVAR(os_rename__doc__,
1555"rename($module, /, src, dst, *, src_dir_fd=None, dst_dir_fd=None)\n"
1556"--\n"
1557"\n"
1558"Rename a file or directory.\n"
1559"\n"
1560"If either src_dir_fd or dst_dir_fd is not None, it should be a file\n"
1561" descriptor open to a directory, and the respective path string (src or dst)\n"
1562" should be relative; the path will then be relative to that directory.\n"
1563"src_dir_fd and dst_dir_fd, may not be implemented on your platform.\n"
1564" If they are unavailable, using them will raise a NotImplementedError.");
1565
1566#define OS_RENAME_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02001567 {"rename", (PyCFunction)(void(*)(void))os_rename, METH_FASTCALL|METH_KEYWORDS, os_rename__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001568
1569static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001570os_rename_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04001571 int dst_dir_fd);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001572
1573static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001574os_rename(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001575{
1576 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001577 static const char * const _keywords[] = {"src", "dst", "src_dir_fd", "dst_dir_fd", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02001578 static _PyArg_Parser _parser = {NULL, _keywords, "rename", 0};
1579 PyObject *argsbuf[4];
1580 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001581 path_t src = PATH_T_INITIALIZE("rename", "src", 0, 0);
1582 path_t dst = PATH_T_INITIALIZE("rename", "dst", 0, 0);
1583 int src_dir_fd = DEFAULT_DIR_FD;
1584 int dst_dir_fd = DEFAULT_DIR_FD;
1585
Serhiy Storchaka31913912019-03-14 10:32:22 +02001586 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf);
1587 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001588 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001589 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02001590 if (!path_converter(args[0], &src)) {
1591 goto exit;
1592 }
1593 if (!path_converter(args[1], &dst)) {
1594 goto exit;
1595 }
1596 if (!noptargs) {
1597 goto skip_optional_kwonly;
1598 }
1599 if (args[2]) {
1600 if (!dir_fd_converter(args[2], &src_dir_fd)) {
1601 goto exit;
1602 }
1603 if (!--noptargs) {
1604 goto skip_optional_kwonly;
1605 }
1606 }
1607 if (!dir_fd_converter(args[3], &dst_dir_fd)) {
1608 goto exit;
1609 }
1610skip_optional_kwonly:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001611 return_value = os_rename_impl(module, &src, &dst, src_dir_fd, dst_dir_fd);
1612
1613exit:
1614 /* Cleanup for src */
1615 path_cleanup(&src);
1616 /* Cleanup for dst */
1617 path_cleanup(&dst);
1618
1619 return return_value;
1620}
1621
1622PyDoc_STRVAR(os_replace__doc__,
1623"replace($module, /, src, dst, *, src_dir_fd=None, dst_dir_fd=None)\n"
1624"--\n"
1625"\n"
1626"Rename a file or directory, overwriting the destination.\n"
1627"\n"
1628"If either src_dir_fd or dst_dir_fd is not None, it should be a file\n"
1629" descriptor open to a directory, and the respective path string (src or dst)\n"
1630" should be relative; the path will then be relative to that directory.\n"
1631"src_dir_fd and dst_dir_fd, may not be implemented on your platform.\n"
Anthony Sottile73d60022019-02-12 23:15:54 -05001632" If they are unavailable, using them will raise a NotImplementedError.");
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001633
1634#define OS_REPLACE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02001635 {"replace", (PyCFunction)(void(*)(void))os_replace, METH_FASTCALL|METH_KEYWORDS, os_replace__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001636
1637static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001638os_replace_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
1639 int dst_dir_fd);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001640
1641static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001642os_replace(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001643{
1644 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001645 static const char * const _keywords[] = {"src", "dst", "src_dir_fd", "dst_dir_fd", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02001646 static _PyArg_Parser _parser = {NULL, _keywords, "replace", 0};
1647 PyObject *argsbuf[4];
1648 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001649 path_t src = PATH_T_INITIALIZE("replace", "src", 0, 0);
1650 path_t dst = PATH_T_INITIALIZE("replace", "dst", 0, 0);
1651 int src_dir_fd = DEFAULT_DIR_FD;
1652 int dst_dir_fd = DEFAULT_DIR_FD;
1653
Serhiy Storchaka31913912019-03-14 10:32:22 +02001654 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf);
1655 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001656 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001657 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02001658 if (!path_converter(args[0], &src)) {
1659 goto exit;
1660 }
1661 if (!path_converter(args[1], &dst)) {
1662 goto exit;
1663 }
1664 if (!noptargs) {
1665 goto skip_optional_kwonly;
1666 }
1667 if (args[2]) {
1668 if (!dir_fd_converter(args[2], &src_dir_fd)) {
1669 goto exit;
1670 }
1671 if (!--noptargs) {
1672 goto skip_optional_kwonly;
1673 }
1674 }
1675 if (!dir_fd_converter(args[3], &dst_dir_fd)) {
1676 goto exit;
1677 }
1678skip_optional_kwonly:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001679 return_value = os_replace_impl(module, &src, &dst, src_dir_fd, dst_dir_fd);
1680
1681exit:
1682 /* Cleanup for src */
1683 path_cleanup(&src);
1684 /* Cleanup for dst */
1685 path_cleanup(&dst);
1686
1687 return return_value;
1688}
1689
1690PyDoc_STRVAR(os_rmdir__doc__,
1691"rmdir($module, /, path, *, dir_fd=None)\n"
1692"--\n"
1693"\n"
1694"Remove a directory.\n"
1695"\n"
1696"If dir_fd is not None, it should be a file descriptor open to a directory,\n"
1697" and path should be relative; path will then be relative to that directory.\n"
1698"dir_fd may not be implemented on your platform.\n"
1699" If it is unavailable, using it will raise a NotImplementedError.");
1700
1701#define OS_RMDIR_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02001702 {"rmdir", (PyCFunction)(void(*)(void))os_rmdir, METH_FASTCALL|METH_KEYWORDS, os_rmdir__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001703
1704static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001705os_rmdir_impl(PyObject *module, path_t *path, int dir_fd);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001706
1707static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001708os_rmdir(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001709{
1710 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001711 static const char * const _keywords[] = {"path", "dir_fd", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02001712 static _PyArg_Parser _parser = {NULL, _keywords, "rmdir", 0};
1713 PyObject *argsbuf[2];
1714 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001715 path_t path = PATH_T_INITIALIZE("rmdir", "path", 0, 0);
1716 int dir_fd = DEFAULT_DIR_FD;
1717
Serhiy Storchaka31913912019-03-14 10:32:22 +02001718 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
1719 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001720 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001721 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02001722 if (!path_converter(args[0], &path)) {
1723 goto exit;
1724 }
1725 if (!noptargs) {
1726 goto skip_optional_kwonly;
1727 }
1728 if (!UNLINKAT_DIR_FD_CONVERTER(args[1], &dir_fd)) {
1729 goto exit;
1730 }
1731skip_optional_kwonly:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001732 return_value = os_rmdir_impl(module, &path, dir_fd);
1733
1734exit:
1735 /* Cleanup for path */
1736 path_cleanup(&path);
1737
1738 return return_value;
1739}
1740
1741#if defined(HAVE_SYSTEM) && defined(MS_WINDOWS)
1742
1743PyDoc_STRVAR(os_system__doc__,
1744"system($module, /, command)\n"
1745"--\n"
1746"\n"
1747"Execute the command in a subshell.");
1748
1749#define OS_SYSTEM_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02001750 {"system", (PyCFunction)(void(*)(void))os_system, METH_FASTCALL|METH_KEYWORDS, os_system__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001751
1752static long
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001753os_system_impl(PyObject *module, const Py_UNICODE *command);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001754
1755static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001756os_system(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001757{
1758 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001759 static const char * const _keywords[] = {"command", NULL};
1760 static _PyArg_Parser _parser = {"u:system", _keywords, 0};
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001761 const Py_UNICODE *command;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001762 long _return_value;
1763
Victor Stinner3e1fad62017-01-17 01:29:01 +01001764 if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001765 &command)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001766 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001767 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001768 _return_value = os_system_impl(module, command);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001769 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001770 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001771 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001772 return_value = PyLong_FromLong(_return_value);
1773
1774exit:
1775 return return_value;
1776}
1777
1778#endif /* defined(HAVE_SYSTEM) && defined(MS_WINDOWS) */
1779
1780#if defined(HAVE_SYSTEM) && !defined(MS_WINDOWS)
1781
1782PyDoc_STRVAR(os_system__doc__,
1783"system($module, /, command)\n"
1784"--\n"
1785"\n"
1786"Execute the command in a subshell.");
1787
1788#define OS_SYSTEM_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02001789 {"system", (PyCFunction)(void(*)(void))os_system, METH_FASTCALL|METH_KEYWORDS, os_system__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001790
1791static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001792os_system_impl(PyObject *module, PyObject *command);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001793
1794static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001795os_system(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001796{
1797 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001798 static const char * const _keywords[] = {"command", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02001799 static _PyArg_Parser _parser = {NULL, _keywords, "system", 0};
1800 PyObject *argsbuf[1];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001801 PyObject *command = NULL;
1802 long _return_value;
1803
Serhiy Storchaka31913912019-03-14 10:32:22 +02001804 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
1805 if (!args) {
1806 goto exit;
1807 }
1808 if (!PyUnicode_FSConverter(args[0], &command)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001809 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001810 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001811 _return_value = os_system_impl(module, command);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001812 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001813 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001814 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001815 return_value = PyLong_FromLong(_return_value);
1816
1817exit:
1818 /* Cleanup for command */
1819 Py_XDECREF(command);
1820
1821 return return_value;
1822}
1823
1824#endif /* defined(HAVE_SYSTEM) && !defined(MS_WINDOWS) */
1825
1826PyDoc_STRVAR(os_umask__doc__,
1827"umask($module, mask, /)\n"
1828"--\n"
1829"\n"
1830"Set the current numeric umask and return the previous umask.");
1831
1832#define OS_UMASK_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03001833 {"umask", (PyCFunction)os_umask, METH_O, os_umask__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001834
1835static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001836os_umask_impl(PyObject *module, int mask);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001837
1838static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001839os_umask(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001840{
1841 PyObject *return_value = NULL;
1842 int mask;
1843
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02001844 if (PyFloat_Check(arg)) {
1845 PyErr_SetString(PyExc_TypeError,
1846 "integer argument expected, got float" );
1847 goto exit;
1848 }
1849 mask = _PyLong_AsInt(arg);
1850 if (mask == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001851 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001852 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001853 return_value = os_umask_impl(module, mask);
1854
1855exit:
1856 return return_value;
1857}
1858
1859PyDoc_STRVAR(os_unlink__doc__,
1860"unlink($module, /, path, *, dir_fd=None)\n"
1861"--\n"
1862"\n"
1863"Remove a file (same as remove()).\n"
1864"\n"
1865"If dir_fd is not None, it should be a file descriptor open to a directory,\n"
1866" and path should be relative; path will then be relative to that directory.\n"
1867"dir_fd may not be implemented on your platform.\n"
1868" If it is unavailable, using it will raise a NotImplementedError.");
1869
1870#define OS_UNLINK_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02001871 {"unlink", (PyCFunction)(void(*)(void))os_unlink, METH_FASTCALL|METH_KEYWORDS, os_unlink__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001872
1873static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001874os_unlink_impl(PyObject *module, path_t *path, int dir_fd);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001875
1876static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001877os_unlink(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001878{
1879 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001880 static const char * const _keywords[] = {"path", "dir_fd", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02001881 static _PyArg_Parser _parser = {NULL, _keywords, "unlink", 0};
1882 PyObject *argsbuf[2];
1883 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001884 path_t path = PATH_T_INITIALIZE("unlink", "path", 0, 0);
1885 int dir_fd = DEFAULT_DIR_FD;
1886
Serhiy Storchaka31913912019-03-14 10:32:22 +02001887 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
1888 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001889 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001890 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02001891 if (!path_converter(args[0], &path)) {
1892 goto exit;
1893 }
1894 if (!noptargs) {
1895 goto skip_optional_kwonly;
1896 }
1897 if (!UNLINKAT_DIR_FD_CONVERTER(args[1], &dir_fd)) {
1898 goto exit;
1899 }
1900skip_optional_kwonly:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001901 return_value = os_unlink_impl(module, &path, dir_fd);
1902
1903exit:
1904 /* Cleanup for path */
1905 path_cleanup(&path);
1906
1907 return return_value;
1908}
1909
1910PyDoc_STRVAR(os_remove__doc__,
1911"remove($module, /, path, *, dir_fd=None)\n"
1912"--\n"
1913"\n"
1914"Remove a file (same as unlink()).\n"
1915"\n"
1916"If dir_fd is not None, it should be a file descriptor open to a directory,\n"
1917" and path should be relative; path will then be relative to that directory.\n"
1918"dir_fd may not be implemented on your platform.\n"
1919" If it is unavailable, using it will raise a NotImplementedError.");
1920
1921#define OS_REMOVE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02001922 {"remove", (PyCFunction)(void(*)(void))os_remove, METH_FASTCALL|METH_KEYWORDS, os_remove__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001923
1924static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001925os_remove_impl(PyObject *module, path_t *path, int dir_fd);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001926
1927static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001928os_remove(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001929{
1930 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001931 static const char * const _keywords[] = {"path", "dir_fd", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02001932 static _PyArg_Parser _parser = {NULL, _keywords, "remove", 0};
1933 PyObject *argsbuf[2];
1934 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001935 path_t path = PATH_T_INITIALIZE("remove", "path", 0, 0);
1936 int dir_fd = DEFAULT_DIR_FD;
1937
Serhiy Storchaka31913912019-03-14 10:32:22 +02001938 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
1939 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001940 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001941 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02001942 if (!path_converter(args[0], &path)) {
1943 goto exit;
1944 }
1945 if (!noptargs) {
1946 goto skip_optional_kwonly;
1947 }
1948 if (!UNLINKAT_DIR_FD_CONVERTER(args[1], &dir_fd)) {
1949 goto exit;
1950 }
1951skip_optional_kwonly:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001952 return_value = os_remove_impl(module, &path, dir_fd);
1953
1954exit:
1955 /* Cleanup for path */
1956 path_cleanup(&path);
1957
1958 return return_value;
1959}
1960
1961#if defined(HAVE_UNAME)
1962
1963PyDoc_STRVAR(os_uname__doc__,
1964"uname($module, /)\n"
1965"--\n"
1966"\n"
1967"Return an object identifying the current operating system.\n"
1968"\n"
1969"The object behaves like a named tuple with the following fields:\n"
1970" (sysname, nodename, release, version, machine)");
1971
1972#define OS_UNAME_METHODDEF \
1973 {"uname", (PyCFunction)os_uname, METH_NOARGS, os_uname__doc__},
1974
1975static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001976os_uname_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001977
1978static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001979os_uname(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001980{
1981 return os_uname_impl(module);
1982}
1983
1984#endif /* defined(HAVE_UNAME) */
1985
1986PyDoc_STRVAR(os_utime__doc__,
1987"utime($module, /, path, times=None, *, ns=None, dir_fd=None,\n"
1988" follow_symlinks=True)\n"
1989"--\n"
1990"\n"
1991"Set the access and modified time of path.\n"
1992"\n"
1993"path may always be specified as a string.\n"
1994"On some platforms, path may also be specified as an open file descriptor.\n"
1995" If this functionality is unavailable, using it raises an exception.\n"
1996"\n"
1997"If times is not None, it must be a tuple (atime, mtime);\n"
1998" atime and mtime should be expressed as float seconds since the epoch.\n"
Martin Panter0ff89092015-09-09 01:56:53 +00001999"If ns is specified, it must be a tuple (atime_ns, mtime_ns);\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002000" atime_ns and mtime_ns should be expressed as integer nanoseconds\n"
2001" since the epoch.\n"
Martin Panter0ff89092015-09-09 01:56:53 +00002002"If times is None and ns is unspecified, utime uses the current time.\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002003"Specifying tuples for both times and ns is an error.\n"
2004"\n"
2005"If dir_fd is not None, it should be a file descriptor open to a directory,\n"
2006" and path should be relative; path will then be relative to that directory.\n"
2007"If follow_symlinks is False, and the last element of the path is a symbolic\n"
2008" link, utime will modify the symbolic link itself instead of the file the\n"
2009" link points to.\n"
2010"It is an error to use dir_fd or follow_symlinks when specifying path\n"
2011" as an open file descriptor.\n"
2012"dir_fd and follow_symlinks may not be available on your platform.\n"
2013" If they are unavailable, using them will raise a NotImplementedError.");
2014
2015#define OS_UTIME_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02002016 {"utime", (PyCFunction)(void(*)(void))os_utime, METH_FASTCALL|METH_KEYWORDS, os_utime__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002017
2018static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002019os_utime_impl(PyObject *module, path_t *path, PyObject *times, PyObject *ns,
2020 int dir_fd, int follow_symlinks);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002021
2022static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02002023os_utime(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002024{
2025 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002026 static const char * const _keywords[] = {"path", "times", "ns", "dir_fd", "follow_symlinks", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02002027 static _PyArg_Parser _parser = {NULL, _keywords, "utime", 0};
2028 PyObject *argsbuf[5];
2029 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002030 path_t path = PATH_T_INITIALIZE("utime", "path", 0, PATH_UTIME_HAVE_FD);
2031 PyObject *times = NULL;
2032 PyObject *ns = NULL;
2033 int dir_fd = DEFAULT_DIR_FD;
2034 int follow_symlinks = 1;
2035
Serhiy Storchaka31913912019-03-14 10:32:22 +02002036 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
2037 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002038 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03002039 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02002040 if (!path_converter(args[0], &path)) {
2041 goto exit;
2042 }
2043 if (!noptargs) {
2044 goto skip_optional_pos;
2045 }
2046 if (args[1]) {
2047 times = args[1];
2048 if (!--noptargs) {
2049 goto skip_optional_pos;
2050 }
2051 }
2052skip_optional_pos:
2053 if (!noptargs) {
2054 goto skip_optional_kwonly;
2055 }
2056 if (args[2]) {
2057 ns = args[2];
2058 if (!--noptargs) {
2059 goto skip_optional_kwonly;
2060 }
2061 }
2062 if (args[3]) {
2063 if (!FUTIMENSAT_DIR_FD_CONVERTER(args[3], &dir_fd)) {
2064 goto exit;
2065 }
2066 if (!--noptargs) {
2067 goto skip_optional_kwonly;
2068 }
2069 }
2070 follow_symlinks = PyObject_IsTrue(args[4]);
2071 if (follow_symlinks < 0) {
2072 goto exit;
2073 }
2074skip_optional_kwonly:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002075 return_value = os_utime_impl(module, &path, times, ns, dir_fd, follow_symlinks);
2076
2077exit:
2078 /* Cleanup for path */
2079 path_cleanup(&path);
2080
2081 return return_value;
2082}
2083
2084PyDoc_STRVAR(os__exit__doc__,
2085"_exit($module, /, status)\n"
2086"--\n"
2087"\n"
2088"Exit to the system with specified status, without normal exit processing.");
2089
2090#define OS__EXIT_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02002091 {"_exit", (PyCFunction)(void(*)(void))os__exit, METH_FASTCALL|METH_KEYWORDS, os__exit__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002092
2093static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002094os__exit_impl(PyObject *module, int status);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002095
2096static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02002097os__exit(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002098{
2099 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002100 static const char * const _keywords[] = {"status", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02002101 static _PyArg_Parser _parser = {NULL, _keywords, "_exit", 0};
2102 PyObject *argsbuf[1];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002103 int status;
2104
Serhiy Storchaka31913912019-03-14 10:32:22 +02002105 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
2106 if (!args) {
2107 goto exit;
2108 }
2109 if (PyFloat_Check(args[0])) {
2110 PyErr_SetString(PyExc_TypeError,
2111 "integer argument expected, got float" );
2112 goto exit;
2113 }
2114 status = _PyLong_AsInt(args[0]);
2115 if (status == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002116 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03002117 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002118 return_value = os__exit_impl(module, status);
2119
2120exit:
2121 return return_value;
2122}
2123
2124#if defined(HAVE_EXECV)
2125
2126PyDoc_STRVAR(os_execv__doc__,
2127"execv($module, path, argv, /)\n"
2128"--\n"
2129"\n"
2130"Execute an executable path with arguments, replacing current process.\n"
2131"\n"
2132" path\n"
2133" Path of executable file.\n"
2134" argv\n"
2135" Tuple or list of strings.");
2136
2137#define OS_EXECV_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02002138 {"execv", (PyCFunction)(void(*)(void))os_execv, METH_FASTCALL, os_execv__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002139
2140static PyObject *
Steve Dowercc16be82016-09-08 10:35:16 -07002141os_execv_impl(PyObject *module, path_t *path, PyObject *argv);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002142
2143static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02002144os_execv(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002145{
2146 PyObject *return_value = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07002147 path_t path = PATH_T_INITIALIZE("execv", "path", 0, 0);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002148 PyObject *argv;
2149
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002150 if (!_PyArg_CheckPositional("execv", nargs, 2, 2)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01002151 goto exit;
2152 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002153 if (!path_converter(args[0], &path)) {
2154 goto exit;
2155 }
2156 argv = args[1];
Steve Dowercc16be82016-09-08 10:35:16 -07002157 return_value = os_execv_impl(module, &path, argv);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002158
2159exit:
2160 /* Cleanup for path */
Steve Dowercc16be82016-09-08 10:35:16 -07002161 path_cleanup(&path);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002162
2163 return return_value;
2164}
2165
2166#endif /* defined(HAVE_EXECV) */
2167
2168#if defined(HAVE_EXECV)
2169
2170PyDoc_STRVAR(os_execve__doc__,
2171"execve($module, /, path, argv, env)\n"
2172"--\n"
2173"\n"
2174"Execute an executable path with arguments, replacing current process.\n"
2175"\n"
2176" path\n"
2177" Path of executable file.\n"
2178" argv\n"
2179" Tuple or list of strings.\n"
2180" env\n"
2181" Dictionary of strings mapping to strings.");
2182
2183#define OS_EXECVE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02002184 {"execve", (PyCFunction)(void(*)(void))os_execve, METH_FASTCALL|METH_KEYWORDS, os_execve__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002185
2186static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002187os_execve_impl(PyObject *module, path_t *path, PyObject *argv, PyObject *env);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002188
2189static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02002190os_execve(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002191{
2192 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002193 static const char * const _keywords[] = {"path", "argv", "env", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02002194 static _PyArg_Parser _parser = {NULL, _keywords, "execve", 0};
2195 PyObject *argsbuf[3];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002196 path_t path = PATH_T_INITIALIZE("execve", "path", 0, PATH_HAVE_FEXECVE);
2197 PyObject *argv;
2198 PyObject *env;
2199
Serhiy Storchaka31913912019-03-14 10:32:22 +02002200 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 3, 3, 0, argsbuf);
2201 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002202 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03002203 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02002204 if (!path_converter(args[0], &path)) {
2205 goto exit;
2206 }
2207 argv = args[1];
2208 env = args[2];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002209 return_value = os_execve_impl(module, &path, argv, env);
2210
2211exit:
2212 /* Cleanup for path */
2213 path_cleanup(&path);
2214
2215 return return_value;
2216}
2217
2218#endif /* defined(HAVE_EXECV) */
2219
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00002220#if defined(HAVE_POSIX_SPAWN)
2221
2222PyDoc_STRVAR(os_posix_spawn__doc__,
Serhiy Storchakad700f972018-09-08 14:48:18 +03002223"posix_spawn($module, path, argv, env, /, *, file_actions=(),\n"
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03002224" setpgroup=None, resetids=False, setsid=False,\n"
2225" setsigmask=(), setsigdef=(), scheduler=None)\n"
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00002226"--\n"
2227"\n"
2228"Execute the program specified by path in a new process.\n"
2229"\n"
2230" path\n"
2231" Path of executable file.\n"
2232" argv\n"
2233" Tuple or list of strings.\n"
2234" env\n"
2235" Dictionary of strings mapping to strings.\n"
2236" file_actions\n"
Pablo Galindo254a4662018-09-07 16:44:24 +01002237" A sequence of file action tuples.\n"
2238" setpgroup\n"
2239" The pgroup to use with the POSIX_SPAWN_SETPGROUP flag.\n"
2240" resetids\n"
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03002241" If the value is `true` the POSIX_SPAWN_RESETIDS will be activated.\n"
2242" setsid\n"
2243" 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 +01002244" setsigmask\n"
2245" The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag.\n"
2246" setsigdef\n"
2247" The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag.\n"
2248" scheduler\n"
2249" A tuple with the scheduler policy (optional) and parameters.");
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00002250
2251#define OS_POSIX_SPAWN_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02002252 {"posix_spawn", (PyCFunction)(void(*)(void))os_posix_spawn, METH_FASTCALL|METH_KEYWORDS, os_posix_spawn__doc__},
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00002253
2254static PyObject *
2255os_posix_spawn_impl(PyObject *module, path_t *path, PyObject *argv,
Pablo Galindo254a4662018-09-07 16:44:24 +01002256 PyObject *env, PyObject *file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03002257 PyObject *setpgroup, int resetids, int setsid,
2258 PyObject *setsigmask, PyObject *setsigdef,
2259 PyObject *scheduler);
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00002260
2261static PyObject *
Pablo Galindo254a4662018-09-07 16:44:24 +01002262os_posix_spawn(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00002263{
2264 PyObject *return_value = NULL;
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03002265 static const char * const _keywords[] = {"", "", "", "file_actions", "setpgroup", "resetids", "setsid", "setsigmask", "setsigdef", "scheduler", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02002266 static _PyArg_Parser _parser = {NULL, _keywords, "posix_spawn", 0};
2267 PyObject *argsbuf[10];
2268 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 3;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00002269 path_t path = PATH_T_INITIALIZE("posix_spawn", "path", 0, 0);
2270 PyObject *argv;
2271 PyObject *env;
Serhiy Storchakad700f972018-09-08 14:48:18 +03002272 PyObject *file_actions = NULL;
Pablo Galindo254a4662018-09-07 16:44:24 +01002273 PyObject *setpgroup = NULL;
2274 int resetids = 0;
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03002275 int setsid = 0;
Pablo Galindo254a4662018-09-07 16:44:24 +01002276 PyObject *setsigmask = NULL;
2277 PyObject *setsigdef = NULL;
2278 PyObject *scheduler = NULL;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00002279
Serhiy Storchaka31913912019-03-14 10:32:22 +02002280 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 3, 3, 0, argsbuf);
2281 if (!args) {
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00002282 goto exit;
2283 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02002284 if (!path_converter(args[0], &path)) {
2285 goto exit;
2286 }
2287 argv = args[1];
2288 env = args[2];
2289 if (!noptargs) {
2290 goto skip_optional_kwonly;
2291 }
2292 if (args[3]) {
2293 file_actions = args[3];
2294 if (!--noptargs) {
2295 goto skip_optional_kwonly;
2296 }
2297 }
2298 if (args[4]) {
2299 setpgroup = args[4];
2300 if (!--noptargs) {
2301 goto skip_optional_kwonly;
2302 }
2303 }
2304 if (args[5]) {
2305 if (PyFloat_Check(args[5])) {
2306 PyErr_SetString(PyExc_TypeError,
2307 "integer argument expected, got float" );
2308 goto exit;
2309 }
2310 resetids = _PyLong_AsInt(args[5]);
2311 if (resetids == -1 && PyErr_Occurred()) {
2312 goto exit;
2313 }
2314 if (!--noptargs) {
2315 goto skip_optional_kwonly;
2316 }
2317 }
2318 if (args[6]) {
2319 if (PyFloat_Check(args[6])) {
2320 PyErr_SetString(PyExc_TypeError,
2321 "integer argument expected, got float" );
2322 goto exit;
2323 }
2324 setsid = _PyLong_AsInt(args[6]);
2325 if (setsid == -1 && PyErr_Occurred()) {
2326 goto exit;
2327 }
2328 if (!--noptargs) {
2329 goto skip_optional_kwonly;
2330 }
2331 }
2332 if (args[7]) {
2333 setsigmask = args[7];
2334 if (!--noptargs) {
2335 goto skip_optional_kwonly;
2336 }
2337 }
2338 if (args[8]) {
2339 setsigdef = args[8];
2340 if (!--noptargs) {
2341 goto skip_optional_kwonly;
2342 }
2343 }
2344 scheduler = args[9];
2345skip_optional_kwonly:
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03002346 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 +00002347
2348exit:
2349 /* Cleanup for path */
2350 path_cleanup(&path);
2351
2352 return return_value;
2353}
2354
2355#endif /* defined(HAVE_POSIX_SPAWN) */
2356
Joannah Nanjekye92b83222019-01-16 16:29:26 +03002357#if defined(HAVE_POSIX_SPAWNP)
2358
2359PyDoc_STRVAR(os_posix_spawnp__doc__,
2360"posix_spawnp($module, path, argv, env, /, *, file_actions=(),\n"
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03002361" setpgroup=None, resetids=False, setsid=False,\n"
2362" setsigmask=(), setsigdef=(), scheduler=None)\n"
Joannah Nanjekye92b83222019-01-16 16:29:26 +03002363"--\n"
2364"\n"
2365"Execute the program specified by path in a new process.\n"
2366"\n"
2367" path\n"
2368" Path of executable file.\n"
2369" argv\n"
2370" Tuple or list of strings.\n"
2371" env\n"
2372" Dictionary of strings mapping to strings.\n"
2373" file_actions\n"
2374" A sequence of file action tuples.\n"
2375" setpgroup\n"
2376" The pgroup to use with the POSIX_SPAWN_SETPGROUP flag.\n"
2377" resetids\n"
2378" If the value is `True` the POSIX_SPAWN_RESETIDS will be activated.\n"
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03002379" setsid\n"
2380" 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 +03002381" setsigmask\n"
2382" The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag.\n"
2383" setsigdef\n"
2384" The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag.\n"
2385" scheduler\n"
2386" A tuple with the scheduler policy (optional) and parameters.");
2387
2388#define OS_POSIX_SPAWNP_METHODDEF \
2389 {"posix_spawnp", (PyCFunction)(void(*)(void))os_posix_spawnp, METH_FASTCALL|METH_KEYWORDS, os_posix_spawnp__doc__},
2390
2391static PyObject *
2392os_posix_spawnp_impl(PyObject *module, path_t *path, PyObject *argv,
2393 PyObject *env, PyObject *file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03002394 PyObject *setpgroup, int resetids, int setsid,
2395 PyObject *setsigmask, PyObject *setsigdef,
2396 PyObject *scheduler);
Joannah Nanjekye92b83222019-01-16 16:29:26 +03002397
2398static PyObject *
2399os_posix_spawnp(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
2400{
2401 PyObject *return_value = NULL;
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03002402 static const char * const _keywords[] = {"", "", "", "file_actions", "setpgroup", "resetids", "setsid", "setsigmask", "setsigdef", "scheduler", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02002403 static _PyArg_Parser _parser = {NULL, _keywords, "posix_spawnp", 0};
2404 PyObject *argsbuf[10];
2405 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 3;
Joannah Nanjekye92b83222019-01-16 16:29:26 +03002406 path_t path = PATH_T_INITIALIZE("posix_spawnp", "path", 0, 0);
2407 PyObject *argv;
2408 PyObject *env;
2409 PyObject *file_actions = NULL;
2410 PyObject *setpgroup = NULL;
2411 int resetids = 0;
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03002412 int setsid = 0;
Joannah Nanjekye92b83222019-01-16 16:29:26 +03002413 PyObject *setsigmask = NULL;
2414 PyObject *setsigdef = NULL;
2415 PyObject *scheduler = NULL;
2416
Serhiy Storchaka31913912019-03-14 10:32:22 +02002417 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 3, 3, 0, argsbuf);
2418 if (!args) {
Joannah Nanjekye92b83222019-01-16 16:29:26 +03002419 goto exit;
2420 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02002421 if (!path_converter(args[0], &path)) {
2422 goto exit;
2423 }
2424 argv = args[1];
2425 env = args[2];
2426 if (!noptargs) {
2427 goto skip_optional_kwonly;
2428 }
2429 if (args[3]) {
2430 file_actions = args[3];
2431 if (!--noptargs) {
2432 goto skip_optional_kwonly;
2433 }
2434 }
2435 if (args[4]) {
2436 setpgroup = args[4];
2437 if (!--noptargs) {
2438 goto skip_optional_kwonly;
2439 }
2440 }
2441 if (args[5]) {
2442 if (PyFloat_Check(args[5])) {
2443 PyErr_SetString(PyExc_TypeError,
2444 "integer argument expected, got float" );
2445 goto exit;
2446 }
2447 resetids = _PyLong_AsInt(args[5]);
2448 if (resetids == -1 && PyErr_Occurred()) {
2449 goto exit;
2450 }
2451 if (!--noptargs) {
2452 goto skip_optional_kwonly;
2453 }
2454 }
2455 if (args[6]) {
2456 if (PyFloat_Check(args[6])) {
2457 PyErr_SetString(PyExc_TypeError,
2458 "integer argument expected, got float" );
2459 goto exit;
2460 }
2461 setsid = _PyLong_AsInt(args[6]);
2462 if (setsid == -1 && PyErr_Occurred()) {
2463 goto exit;
2464 }
2465 if (!--noptargs) {
2466 goto skip_optional_kwonly;
2467 }
2468 }
2469 if (args[7]) {
2470 setsigmask = args[7];
2471 if (!--noptargs) {
2472 goto skip_optional_kwonly;
2473 }
2474 }
2475 if (args[8]) {
2476 setsigdef = args[8];
2477 if (!--noptargs) {
2478 goto skip_optional_kwonly;
2479 }
2480 }
2481 scheduler = args[9];
2482skip_optional_kwonly:
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03002483 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 +03002484
2485exit:
2486 /* Cleanup for path */
2487 path_cleanup(&path);
2488
2489 return return_value;
2490}
2491
2492#endif /* defined(HAVE_POSIX_SPAWNP) */
2493
pxinwrf2d7ac72019-05-21 18:46:37 +08002494#if (defined(HAVE_SPAWNV) || defined(HAVE_WSPAWNV) || defined(HAVE_RTPSPAWN))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002495
2496PyDoc_STRVAR(os_spawnv__doc__,
2497"spawnv($module, mode, path, argv, /)\n"
2498"--\n"
2499"\n"
2500"Execute the program specified by path in a new process.\n"
2501"\n"
2502" mode\n"
2503" Mode of process creation.\n"
2504" path\n"
2505" Path of executable file.\n"
2506" argv\n"
2507" Tuple or list of strings.");
2508
2509#define OS_SPAWNV_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02002510 {"spawnv", (PyCFunction)(void(*)(void))os_spawnv, METH_FASTCALL, os_spawnv__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002511
2512static PyObject *
Steve Dowercc16be82016-09-08 10:35:16 -07002513os_spawnv_impl(PyObject *module, int mode, path_t *path, PyObject *argv);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002514
2515static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02002516os_spawnv(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002517{
2518 PyObject *return_value = NULL;
2519 int mode;
Steve Dowercc16be82016-09-08 10:35:16 -07002520 path_t path = PATH_T_INITIALIZE("spawnv", "path", 0, 0);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002521 PyObject *argv;
2522
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002523 if (!_PyArg_CheckPositional("spawnv", nargs, 3, 3)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01002524 goto exit;
2525 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002526 if (PyFloat_Check(args[0])) {
2527 PyErr_SetString(PyExc_TypeError,
2528 "integer argument expected, got float" );
2529 goto exit;
2530 }
2531 mode = _PyLong_AsInt(args[0]);
2532 if (mode == -1 && PyErr_Occurred()) {
2533 goto exit;
2534 }
2535 if (!path_converter(args[1], &path)) {
2536 goto exit;
2537 }
2538 argv = args[2];
Steve Dowercc16be82016-09-08 10:35:16 -07002539 return_value = os_spawnv_impl(module, mode, &path, argv);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002540
2541exit:
2542 /* Cleanup for path */
Steve Dowercc16be82016-09-08 10:35:16 -07002543 path_cleanup(&path);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002544
2545 return return_value;
2546}
2547
pxinwrf2d7ac72019-05-21 18:46:37 +08002548#endif /* (defined(HAVE_SPAWNV) || defined(HAVE_WSPAWNV) || defined(HAVE_RTPSPAWN)) */
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002549
pxinwrf2d7ac72019-05-21 18:46:37 +08002550#if (defined(HAVE_SPAWNV) || defined(HAVE_WSPAWNV) || defined(HAVE_RTPSPAWN))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002551
2552PyDoc_STRVAR(os_spawnve__doc__,
2553"spawnve($module, mode, path, argv, env, /)\n"
2554"--\n"
2555"\n"
2556"Execute the program specified by path in a new process.\n"
2557"\n"
2558" mode\n"
2559" Mode of process creation.\n"
2560" path\n"
2561" Path of executable file.\n"
2562" argv\n"
2563" Tuple or list of strings.\n"
2564" env\n"
2565" Dictionary of strings mapping to strings.");
2566
2567#define OS_SPAWNVE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02002568 {"spawnve", (PyCFunction)(void(*)(void))os_spawnve, METH_FASTCALL, os_spawnve__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002569
2570static PyObject *
Steve Dowercc16be82016-09-08 10:35:16 -07002571os_spawnve_impl(PyObject *module, int mode, path_t *path, PyObject *argv,
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002572 PyObject *env);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002573
2574static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02002575os_spawnve(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002576{
2577 PyObject *return_value = NULL;
2578 int mode;
Steve Dowercc16be82016-09-08 10:35:16 -07002579 path_t path = PATH_T_INITIALIZE("spawnve", "path", 0, 0);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002580 PyObject *argv;
2581 PyObject *env;
2582
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002583 if (!_PyArg_CheckPositional("spawnve", nargs, 4, 4)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01002584 goto exit;
2585 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002586 if (PyFloat_Check(args[0])) {
2587 PyErr_SetString(PyExc_TypeError,
2588 "integer argument expected, got float" );
2589 goto exit;
2590 }
2591 mode = _PyLong_AsInt(args[0]);
2592 if (mode == -1 && PyErr_Occurred()) {
2593 goto exit;
2594 }
2595 if (!path_converter(args[1], &path)) {
2596 goto exit;
2597 }
2598 argv = args[2];
2599 env = args[3];
Steve Dowercc16be82016-09-08 10:35:16 -07002600 return_value = os_spawnve_impl(module, mode, &path, argv, env);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002601
2602exit:
2603 /* Cleanup for path */
Steve Dowercc16be82016-09-08 10:35:16 -07002604 path_cleanup(&path);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002605
2606 return return_value;
2607}
2608
pxinwrf2d7ac72019-05-21 18:46:37 +08002609#endif /* (defined(HAVE_SPAWNV) || defined(HAVE_WSPAWNV) || defined(HAVE_RTPSPAWN)) */
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002610
Antoine Pitrou346cbd32017-05-27 17:50:54 +02002611#if defined(HAVE_FORK)
2612
2613PyDoc_STRVAR(os_register_at_fork__doc__,
Gregory P. Smith163468a2017-05-29 10:03:41 -07002614"register_at_fork($module, /, *, before=None, after_in_child=None,\n"
2615" after_in_parent=None)\n"
Antoine Pitrou346cbd32017-05-27 17:50:54 +02002616"--\n"
2617"\n"
Gregory P. Smith163468a2017-05-29 10:03:41 -07002618"Register callables to be called when forking a new process.\n"
Antoine Pitrou346cbd32017-05-27 17:50:54 +02002619"\n"
Gregory P. Smith163468a2017-05-29 10:03:41 -07002620" before\n"
2621" A callable to be called in the parent before the fork() syscall.\n"
2622" after_in_child\n"
2623" A callable to be called in the child after fork().\n"
2624" after_in_parent\n"
2625" A callable to be called in the parent after fork().\n"
Antoine Pitrou346cbd32017-05-27 17:50:54 +02002626"\n"
Gregory P. Smith163468a2017-05-29 10:03:41 -07002627"\'before\' callbacks are called in reverse order.\n"
2628"\'after_in_child\' and \'after_in_parent\' callbacks are called in order.");
Antoine Pitrou346cbd32017-05-27 17:50:54 +02002629
2630#define OS_REGISTER_AT_FORK_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02002631 {"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 +02002632
2633static PyObject *
Gregory P. Smith163468a2017-05-29 10:03:41 -07002634os_register_at_fork_impl(PyObject *module, PyObject *before,
2635 PyObject *after_in_child, PyObject *after_in_parent);
Antoine Pitrou346cbd32017-05-27 17:50:54 +02002636
2637static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02002638os_register_at_fork(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Antoine Pitrou346cbd32017-05-27 17:50:54 +02002639{
2640 PyObject *return_value = NULL;
Gregory P. Smith163468a2017-05-29 10:03:41 -07002641 static const char * const _keywords[] = {"before", "after_in_child", "after_in_parent", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02002642 static _PyArg_Parser _parser = {NULL, _keywords, "register_at_fork", 0};
2643 PyObject *argsbuf[3];
2644 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
Gregory P. Smith163468a2017-05-29 10:03:41 -07002645 PyObject *before = NULL;
2646 PyObject *after_in_child = NULL;
2647 PyObject *after_in_parent = NULL;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02002648
Serhiy Storchaka31913912019-03-14 10:32:22 +02002649 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 0, 0, argsbuf);
2650 if (!args) {
Antoine Pitrou346cbd32017-05-27 17:50:54 +02002651 goto exit;
2652 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02002653 if (!noptargs) {
2654 goto skip_optional_kwonly;
2655 }
2656 if (args[0]) {
2657 before = args[0];
2658 if (!--noptargs) {
2659 goto skip_optional_kwonly;
2660 }
2661 }
2662 if (args[1]) {
2663 after_in_child = args[1];
2664 if (!--noptargs) {
2665 goto skip_optional_kwonly;
2666 }
2667 }
2668 after_in_parent = args[2];
2669skip_optional_kwonly:
Gregory P. Smith163468a2017-05-29 10:03:41 -07002670 return_value = os_register_at_fork_impl(module, before, after_in_child, after_in_parent);
Antoine Pitrou346cbd32017-05-27 17:50:54 +02002671
2672exit:
2673 return return_value;
2674}
2675
2676#endif /* defined(HAVE_FORK) */
2677
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002678#if defined(HAVE_FORK1)
2679
2680PyDoc_STRVAR(os_fork1__doc__,
2681"fork1($module, /)\n"
2682"--\n"
2683"\n"
2684"Fork a child process with a single multiplexed (i.e., not bound) thread.\n"
2685"\n"
2686"Return 0 to child process and PID of child to parent process.");
2687
2688#define OS_FORK1_METHODDEF \
2689 {"fork1", (PyCFunction)os_fork1, METH_NOARGS, os_fork1__doc__},
2690
2691static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002692os_fork1_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002693
2694static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002695os_fork1(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002696{
2697 return os_fork1_impl(module);
2698}
2699
2700#endif /* defined(HAVE_FORK1) */
2701
2702#if defined(HAVE_FORK)
2703
2704PyDoc_STRVAR(os_fork__doc__,
2705"fork($module, /)\n"
2706"--\n"
2707"\n"
2708"Fork a child process.\n"
2709"\n"
2710"Return 0 to child process and PID of child to parent process.");
2711
2712#define OS_FORK_METHODDEF \
2713 {"fork", (PyCFunction)os_fork, METH_NOARGS, os_fork__doc__},
2714
2715static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002716os_fork_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002717
2718static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002719os_fork(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002720{
2721 return os_fork_impl(module);
2722}
2723
2724#endif /* defined(HAVE_FORK) */
2725
2726#if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_GET_PRIORITY_MAX)
2727
2728PyDoc_STRVAR(os_sched_get_priority_max__doc__,
2729"sched_get_priority_max($module, /, policy)\n"
2730"--\n"
2731"\n"
2732"Get the maximum scheduling priority for policy.");
2733
2734#define OS_SCHED_GET_PRIORITY_MAX_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02002735 {"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 +03002736
2737static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002738os_sched_get_priority_max_impl(PyObject *module, int policy);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002739
2740static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02002741os_sched_get_priority_max(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002742{
2743 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002744 static const char * const _keywords[] = {"policy", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02002745 static _PyArg_Parser _parser = {NULL, _keywords, "sched_get_priority_max", 0};
2746 PyObject *argsbuf[1];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002747 int policy;
2748
Serhiy Storchaka31913912019-03-14 10:32:22 +02002749 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
2750 if (!args) {
2751 goto exit;
2752 }
2753 if (PyFloat_Check(args[0])) {
2754 PyErr_SetString(PyExc_TypeError,
2755 "integer argument expected, got float" );
2756 goto exit;
2757 }
2758 policy = _PyLong_AsInt(args[0]);
2759 if (policy == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002760 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03002761 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002762 return_value = os_sched_get_priority_max_impl(module, policy);
2763
2764exit:
2765 return return_value;
2766}
2767
2768#endif /* defined(HAVE_SCHED_H) && defined(HAVE_SCHED_GET_PRIORITY_MAX) */
2769
2770#if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_GET_PRIORITY_MAX)
2771
2772PyDoc_STRVAR(os_sched_get_priority_min__doc__,
2773"sched_get_priority_min($module, /, policy)\n"
2774"--\n"
2775"\n"
2776"Get the minimum scheduling priority for policy.");
2777
2778#define OS_SCHED_GET_PRIORITY_MIN_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02002779 {"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 +03002780
2781static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002782os_sched_get_priority_min_impl(PyObject *module, int policy);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002783
2784static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02002785os_sched_get_priority_min(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002786{
2787 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002788 static const char * const _keywords[] = {"policy", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02002789 static _PyArg_Parser _parser = {NULL, _keywords, "sched_get_priority_min", 0};
2790 PyObject *argsbuf[1];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002791 int policy;
2792
Serhiy Storchaka31913912019-03-14 10:32:22 +02002793 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
2794 if (!args) {
2795 goto exit;
2796 }
2797 if (PyFloat_Check(args[0])) {
2798 PyErr_SetString(PyExc_TypeError,
2799 "integer argument expected, got float" );
2800 goto exit;
2801 }
2802 policy = _PyLong_AsInt(args[0]);
2803 if (policy == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002804 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03002805 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002806 return_value = os_sched_get_priority_min_impl(module, policy);
2807
2808exit:
2809 return return_value;
2810}
2811
2812#endif /* defined(HAVE_SCHED_H) && defined(HAVE_SCHED_GET_PRIORITY_MAX) */
2813
2814#if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_SETSCHEDULER)
2815
2816PyDoc_STRVAR(os_sched_getscheduler__doc__,
2817"sched_getscheduler($module, pid, /)\n"
2818"--\n"
2819"\n"
2820"Get the scheduling policy for the process identifiedy by pid.\n"
2821"\n"
2822"Passing 0 for pid returns the scheduling policy for the calling process.");
2823
2824#define OS_SCHED_GETSCHEDULER_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03002825 {"sched_getscheduler", (PyCFunction)os_sched_getscheduler, METH_O, os_sched_getscheduler__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002826
2827static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002828os_sched_getscheduler_impl(PyObject *module, pid_t pid);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002829
2830static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002831os_sched_getscheduler(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002832{
2833 PyObject *return_value = NULL;
2834 pid_t pid;
2835
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03002836 if (!PyArg_Parse(arg, "" _Py_PARSE_PID ":sched_getscheduler", &pid)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002837 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03002838 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002839 return_value = os_sched_getscheduler_impl(module, pid);
2840
2841exit:
2842 return return_value;
2843}
2844
2845#endif /* defined(HAVE_SCHED_H) && defined(HAVE_SCHED_SETSCHEDULER) */
2846
William Orr81574b82018-10-01 22:19:56 -07002847#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 +03002848
2849PyDoc_STRVAR(os_sched_param__doc__,
2850"sched_param(sched_priority)\n"
2851"--\n"
2852"\n"
2853"Current has only one field: sched_priority\");\n"
2854"\n"
2855" sched_priority\n"
2856" A scheduling parameter.");
2857
2858static PyObject *
2859os_sched_param_impl(PyTypeObject *type, PyObject *sched_priority);
2860
2861static PyObject *
2862os_sched_param(PyTypeObject *type, PyObject *args, PyObject *kwargs)
2863{
2864 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002865 static const char * const _keywords[] = {"sched_priority", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02002866 static _PyArg_Parser _parser = {NULL, _keywords, "sched_param", 0};
2867 PyObject *argsbuf[1];
2868 PyObject * const *fastargs;
2869 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002870 PyObject *sched_priority;
2871
Serhiy Storchaka31913912019-03-14 10:32:22 +02002872 fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 1, 1, 0, argsbuf);
2873 if (!fastargs) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002874 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03002875 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02002876 sched_priority = fastargs[0];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002877 return_value = os_sched_param_impl(type, sched_priority);
2878
2879exit:
2880 return return_value;
2881}
2882
William Orr81574b82018-10-01 22:19:56 -07002883#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 +03002884
2885#if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_SETSCHEDULER)
2886
2887PyDoc_STRVAR(os_sched_setscheduler__doc__,
2888"sched_setscheduler($module, pid, policy, param, /)\n"
2889"--\n"
2890"\n"
2891"Set the scheduling policy for the process identified by pid.\n"
2892"\n"
2893"If pid is 0, the calling process is changed.\n"
2894"param is an instance of sched_param.");
2895
2896#define OS_SCHED_SETSCHEDULER_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02002897 {"sched_setscheduler", (PyCFunction)(void(*)(void))os_sched_setscheduler, METH_FASTCALL, os_sched_setscheduler__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002898
2899static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002900os_sched_setscheduler_impl(PyObject *module, pid_t pid, int policy,
Larry Hastings89964c42015-04-14 18:07:59 -04002901 struct sched_param *param);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002902
2903static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02002904os_sched_setscheduler(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002905{
2906 PyObject *return_value = NULL;
2907 pid_t pid;
2908 int policy;
2909 struct sched_param param;
2910
Sylvain74453812017-06-10 06:51:48 +02002911 if (!_PyArg_ParseStack(args, nargs, "" _Py_PARSE_PID "iO&:sched_setscheduler",
2912 &pid, &policy, convert_sched_param, &param)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01002913 goto exit;
2914 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002915 return_value = os_sched_setscheduler_impl(module, pid, policy, &param);
2916
2917exit:
2918 return return_value;
2919}
2920
2921#endif /* defined(HAVE_SCHED_H) && defined(HAVE_SCHED_SETSCHEDULER) */
2922
2923#if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_SETPARAM)
2924
2925PyDoc_STRVAR(os_sched_getparam__doc__,
2926"sched_getparam($module, pid, /)\n"
2927"--\n"
2928"\n"
2929"Returns scheduling parameters for the process identified by pid.\n"
2930"\n"
2931"If pid is 0, returns parameters for the calling process.\n"
2932"Return value is an instance of sched_param.");
2933
2934#define OS_SCHED_GETPARAM_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03002935 {"sched_getparam", (PyCFunction)os_sched_getparam, METH_O, os_sched_getparam__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002936
2937static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002938os_sched_getparam_impl(PyObject *module, pid_t pid);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002939
2940static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002941os_sched_getparam(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002942{
2943 PyObject *return_value = NULL;
2944 pid_t pid;
2945
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03002946 if (!PyArg_Parse(arg, "" _Py_PARSE_PID ":sched_getparam", &pid)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002947 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03002948 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002949 return_value = os_sched_getparam_impl(module, pid);
2950
2951exit:
2952 return return_value;
2953}
2954
2955#endif /* defined(HAVE_SCHED_H) && defined(HAVE_SCHED_SETPARAM) */
2956
2957#if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_SETPARAM)
2958
2959PyDoc_STRVAR(os_sched_setparam__doc__,
2960"sched_setparam($module, pid, param, /)\n"
2961"--\n"
2962"\n"
2963"Set scheduling parameters for the process identified by pid.\n"
2964"\n"
2965"If pid is 0, sets parameters for the calling process.\n"
2966"param should be an instance of sched_param.");
2967
2968#define OS_SCHED_SETPARAM_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02002969 {"sched_setparam", (PyCFunction)(void(*)(void))os_sched_setparam, METH_FASTCALL, os_sched_setparam__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002970
2971static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002972os_sched_setparam_impl(PyObject *module, pid_t pid,
Larry Hastings89964c42015-04-14 18:07:59 -04002973 struct sched_param *param);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002974
2975static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02002976os_sched_setparam(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002977{
2978 PyObject *return_value = NULL;
2979 pid_t pid;
2980 struct sched_param param;
2981
Sylvain74453812017-06-10 06:51:48 +02002982 if (!_PyArg_ParseStack(args, nargs, "" _Py_PARSE_PID "O&:sched_setparam",
2983 &pid, convert_sched_param, &param)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01002984 goto exit;
2985 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002986 return_value = os_sched_setparam_impl(module, pid, &param);
2987
2988exit:
2989 return return_value;
2990}
2991
2992#endif /* defined(HAVE_SCHED_H) && defined(HAVE_SCHED_SETPARAM) */
2993
2994#if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_RR_GET_INTERVAL)
2995
2996PyDoc_STRVAR(os_sched_rr_get_interval__doc__,
2997"sched_rr_get_interval($module, pid, /)\n"
2998"--\n"
2999"\n"
3000"Return the round-robin quantum for the process identified by pid, in seconds.\n"
3001"\n"
3002"Value returned is a float.");
3003
3004#define OS_SCHED_RR_GET_INTERVAL_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03003005 {"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 +03003006
3007static double
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003008os_sched_rr_get_interval_impl(PyObject *module, pid_t pid);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003009
3010static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003011os_sched_rr_get_interval(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003012{
3013 PyObject *return_value = NULL;
3014 pid_t pid;
3015 double _return_value;
3016
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03003017 if (!PyArg_Parse(arg, "" _Py_PARSE_PID ":sched_rr_get_interval", &pid)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003018 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03003019 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003020 _return_value = os_sched_rr_get_interval_impl(module, pid);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03003021 if ((_return_value == -1.0) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003022 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03003023 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003024 return_value = PyFloat_FromDouble(_return_value);
3025
3026exit:
3027 return return_value;
3028}
3029
3030#endif /* defined(HAVE_SCHED_H) && defined(HAVE_SCHED_RR_GET_INTERVAL) */
3031
3032#if defined(HAVE_SCHED_H)
3033
3034PyDoc_STRVAR(os_sched_yield__doc__,
3035"sched_yield($module, /)\n"
3036"--\n"
3037"\n"
3038"Voluntarily relinquish the CPU.");
3039
3040#define OS_SCHED_YIELD_METHODDEF \
3041 {"sched_yield", (PyCFunction)os_sched_yield, METH_NOARGS, os_sched_yield__doc__},
3042
3043static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003044os_sched_yield_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003045
3046static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003047os_sched_yield(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003048{
3049 return os_sched_yield_impl(module);
3050}
3051
3052#endif /* defined(HAVE_SCHED_H) */
3053
3054#if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_SETAFFINITY)
3055
3056PyDoc_STRVAR(os_sched_setaffinity__doc__,
3057"sched_setaffinity($module, pid, mask, /)\n"
3058"--\n"
3059"\n"
3060"Set the CPU affinity of the process identified by pid to mask.\n"
3061"\n"
3062"mask should be an iterable of integers identifying CPUs.");
3063
3064#define OS_SCHED_SETAFFINITY_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02003065 {"sched_setaffinity", (PyCFunction)(void(*)(void))os_sched_setaffinity, METH_FASTCALL, os_sched_setaffinity__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003066
3067static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003068os_sched_setaffinity_impl(PyObject *module, pid_t pid, PyObject *mask);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003069
3070static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003071os_sched_setaffinity(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003072{
3073 PyObject *return_value = NULL;
3074 pid_t pid;
3075 PyObject *mask;
3076
Sylvain74453812017-06-10 06:51:48 +02003077 if (!_PyArg_ParseStack(args, nargs, "" _Py_PARSE_PID "O:sched_setaffinity",
3078 &pid, &mask)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01003079 goto exit;
3080 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003081 return_value = os_sched_setaffinity_impl(module, pid, mask);
3082
3083exit:
3084 return return_value;
3085}
3086
3087#endif /* defined(HAVE_SCHED_H) && defined(HAVE_SCHED_SETAFFINITY) */
3088
3089#if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_SETAFFINITY)
3090
3091PyDoc_STRVAR(os_sched_getaffinity__doc__,
3092"sched_getaffinity($module, pid, /)\n"
3093"--\n"
3094"\n"
Charles-François Natali80d62e62015-08-13 20:37:08 +01003095"Return the affinity of the process identified by pid (or the current process if zero).\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003096"\n"
3097"The affinity is returned as a set of CPU identifiers.");
3098
3099#define OS_SCHED_GETAFFINITY_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03003100 {"sched_getaffinity", (PyCFunction)os_sched_getaffinity, METH_O, os_sched_getaffinity__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003101
3102static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003103os_sched_getaffinity_impl(PyObject *module, pid_t pid);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003104
3105static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003106os_sched_getaffinity(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003107{
3108 PyObject *return_value = NULL;
3109 pid_t pid;
3110
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03003111 if (!PyArg_Parse(arg, "" _Py_PARSE_PID ":sched_getaffinity", &pid)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003112 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03003113 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003114 return_value = os_sched_getaffinity_impl(module, pid);
3115
3116exit:
3117 return return_value;
3118}
3119
3120#endif /* defined(HAVE_SCHED_H) && defined(HAVE_SCHED_SETAFFINITY) */
3121
3122#if (defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX))
3123
3124PyDoc_STRVAR(os_openpty__doc__,
3125"openpty($module, /)\n"
3126"--\n"
3127"\n"
3128"Open a pseudo-terminal.\n"
3129"\n"
3130"Return a tuple of (master_fd, slave_fd) containing open file descriptors\n"
3131"for both the master and slave ends.");
3132
3133#define OS_OPENPTY_METHODDEF \
3134 {"openpty", (PyCFunction)os_openpty, METH_NOARGS, os_openpty__doc__},
3135
3136static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003137os_openpty_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003138
3139static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003140os_openpty(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003141{
3142 return os_openpty_impl(module);
3143}
3144
3145#endif /* (defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)) */
3146
3147#if defined(HAVE_FORKPTY)
3148
3149PyDoc_STRVAR(os_forkpty__doc__,
3150"forkpty($module, /)\n"
3151"--\n"
3152"\n"
3153"Fork a new process with a new pseudo-terminal as controlling tty.\n"
3154"\n"
3155"Returns a tuple of (pid, master_fd).\n"
3156"Like fork(), return pid of 0 to the child process,\n"
3157"and pid of child to the parent process.\n"
3158"To both, return fd of newly opened pseudo-terminal.");
3159
3160#define OS_FORKPTY_METHODDEF \
3161 {"forkpty", (PyCFunction)os_forkpty, METH_NOARGS, os_forkpty__doc__},
3162
3163static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003164os_forkpty_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003165
3166static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003167os_forkpty(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003168{
3169 return os_forkpty_impl(module);
3170}
3171
3172#endif /* defined(HAVE_FORKPTY) */
3173
3174#if defined(HAVE_GETEGID)
3175
3176PyDoc_STRVAR(os_getegid__doc__,
3177"getegid($module, /)\n"
3178"--\n"
3179"\n"
3180"Return the current process\'s effective group id.");
3181
3182#define OS_GETEGID_METHODDEF \
3183 {"getegid", (PyCFunction)os_getegid, METH_NOARGS, os_getegid__doc__},
3184
3185static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003186os_getegid_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003187
3188static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003189os_getegid(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003190{
3191 return os_getegid_impl(module);
3192}
3193
3194#endif /* defined(HAVE_GETEGID) */
3195
3196#if defined(HAVE_GETEUID)
3197
3198PyDoc_STRVAR(os_geteuid__doc__,
3199"geteuid($module, /)\n"
3200"--\n"
3201"\n"
3202"Return the current process\'s effective user id.");
3203
3204#define OS_GETEUID_METHODDEF \
3205 {"geteuid", (PyCFunction)os_geteuid, METH_NOARGS, os_geteuid__doc__},
3206
3207static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003208os_geteuid_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003209
3210static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003211os_geteuid(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003212{
3213 return os_geteuid_impl(module);
3214}
3215
3216#endif /* defined(HAVE_GETEUID) */
3217
3218#if defined(HAVE_GETGID)
3219
3220PyDoc_STRVAR(os_getgid__doc__,
3221"getgid($module, /)\n"
3222"--\n"
3223"\n"
3224"Return the current process\'s group id.");
3225
3226#define OS_GETGID_METHODDEF \
3227 {"getgid", (PyCFunction)os_getgid, METH_NOARGS, os_getgid__doc__},
3228
3229static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003230os_getgid_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003231
3232static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003233os_getgid(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003234{
3235 return os_getgid_impl(module);
3236}
3237
3238#endif /* defined(HAVE_GETGID) */
3239
Berker Peksag39404992016-09-15 20:45:16 +03003240#if defined(HAVE_GETPID)
3241
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003242PyDoc_STRVAR(os_getpid__doc__,
3243"getpid($module, /)\n"
3244"--\n"
3245"\n"
3246"Return the current process id.");
3247
3248#define OS_GETPID_METHODDEF \
3249 {"getpid", (PyCFunction)os_getpid, METH_NOARGS, os_getpid__doc__},
3250
3251static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003252os_getpid_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003253
3254static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003255os_getpid(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003256{
3257 return os_getpid_impl(module);
3258}
3259
Berker Peksag39404992016-09-15 20:45:16 +03003260#endif /* defined(HAVE_GETPID) */
3261
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003262#if defined(HAVE_GETGROUPS)
3263
3264PyDoc_STRVAR(os_getgroups__doc__,
3265"getgroups($module, /)\n"
3266"--\n"
3267"\n"
3268"Return list of supplemental group IDs for the process.");
3269
3270#define OS_GETGROUPS_METHODDEF \
3271 {"getgroups", (PyCFunction)os_getgroups, METH_NOARGS, os_getgroups__doc__},
3272
3273static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003274os_getgroups_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003275
3276static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003277os_getgroups(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003278{
3279 return os_getgroups_impl(module);
3280}
3281
3282#endif /* defined(HAVE_GETGROUPS) */
3283
3284#if defined(HAVE_GETPGID)
3285
3286PyDoc_STRVAR(os_getpgid__doc__,
3287"getpgid($module, /, pid)\n"
3288"--\n"
3289"\n"
3290"Call the system call getpgid(), and return the result.");
3291
3292#define OS_GETPGID_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02003293 {"getpgid", (PyCFunction)(void(*)(void))os_getpgid, METH_FASTCALL|METH_KEYWORDS, os_getpgid__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003294
3295static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003296os_getpgid_impl(PyObject *module, pid_t pid);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003297
3298static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003299os_getpgid(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003300{
3301 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03003302 static const char * const _keywords[] = {"pid", NULL};
3303 static _PyArg_Parser _parser = {"" _Py_PARSE_PID ":getpgid", _keywords, 0};
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003304 pid_t pid;
3305
Victor Stinner3e1fad62017-01-17 01:29:01 +01003306 if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03003307 &pid)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003308 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03003309 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003310 return_value = os_getpgid_impl(module, pid);
3311
3312exit:
3313 return return_value;
3314}
3315
3316#endif /* defined(HAVE_GETPGID) */
3317
3318#if defined(HAVE_GETPGRP)
3319
3320PyDoc_STRVAR(os_getpgrp__doc__,
3321"getpgrp($module, /)\n"
3322"--\n"
3323"\n"
3324"Return the current process group id.");
3325
3326#define OS_GETPGRP_METHODDEF \
3327 {"getpgrp", (PyCFunction)os_getpgrp, METH_NOARGS, os_getpgrp__doc__},
3328
3329static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003330os_getpgrp_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003331
3332static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003333os_getpgrp(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003334{
3335 return os_getpgrp_impl(module);
3336}
3337
3338#endif /* defined(HAVE_GETPGRP) */
3339
3340#if defined(HAVE_SETPGRP)
3341
3342PyDoc_STRVAR(os_setpgrp__doc__,
3343"setpgrp($module, /)\n"
3344"--\n"
3345"\n"
3346"Make the current process the leader of its process group.");
3347
3348#define OS_SETPGRP_METHODDEF \
3349 {"setpgrp", (PyCFunction)os_setpgrp, METH_NOARGS, os_setpgrp__doc__},
3350
3351static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003352os_setpgrp_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003353
3354static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003355os_setpgrp(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003356{
3357 return os_setpgrp_impl(module);
3358}
3359
3360#endif /* defined(HAVE_SETPGRP) */
3361
3362#if defined(HAVE_GETPPID)
3363
3364PyDoc_STRVAR(os_getppid__doc__,
3365"getppid($module, /)\n"
3366"--\n"
3367"\n"
3368"Return the parent\'s process id.\n"
3369"\n"
3370"If the parent process has already exited, Windows machines will still\n"
3371"return its id; others systems will return the id of the \'init\' process (1).");
3372
3373#define OS_GETPPID_METHODDEF \
3374 {"getppid", (PyCFunction)os_getppid, METH_NOARGS, os_getppid__doc__},
3375
3376static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003377os_getppid_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003378
3379static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003380os_getppid(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003381{
3382 return os_getppid_impl(module);
3383}
3384
3385#endif /* defined(HAVE_GETPPID) */
3386
3387#if defined(HAVE_GETLOGIN)
3388
3389PyDoc_STRVAR(os_getlogin__doc__,
3390"getlogin($module, /)\n"
3391"--\n"
3392"\n"
3393"Return the actual login name.");
3394
3395#define OS_GETLOGIN_METHODDEF \
3396 {"getlogin", (PyCFunction)os_getlogin, METH_NOARGS, os_getlogin__doc__},
3397
3398static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003399os_getlogin_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003400
3401static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003402os_getlogin(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003403{
3404 return os_getlogin_impl(module);
3405}
3406
3407#endif /* defined(HAVE_GETLOGIN) */
3408
3409#if defined(HAVE_GETUID)
3410
3411PyDoc_STRVAR(os_getuid__doc__,
3412"getuid($module, /)\n"
3413"--\n"
3414"\n"
3415"Return the current process\'s user id.");
3416
3417#define OS_GETUID_METHODDEF \
3418 {"getuid", (PyCFunction)os_getuid, METH_NOARGS, os_getuid__doc__},
3419
3420static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003421os_getuid_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003422
3423static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003424os_getuid(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003425{
3426 return os_getuid_impl(module);
3427}
3428
3429#endif /* defined(HAVE_GETUID) */
3430
3431#if defined(HAVE_KILL)
3432
3433PyDoc_STRVAR(os_kill__doc__,
3434"kill($module, pid, signal, /)\n"
3435"--\n"
3436"\n"
3437"Kill a process with a signal.");
3438
3439#define OS_KILL_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02003440 {"kill", (PyCFunction)(void(*)(void))os_kill, METH_FASTCALL, os_kill__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003441
3442static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003443os_kill_impl(PyObject *module, pid_t pid, Py_ssize_t signal);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003444
3445static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003446os_kill(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003447{
3448 PyObject *return_value = NULL;
3449 pid_t pid;
3450 Py_ssize_t signal;
3451
Sylvain74453812017-06-10 06:51:48 +02003452 if (!_PyArg_ParseStack(args, nargs, "" _Py_PARSE_PID "n:kill",
3453 &pid, &signal)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01003454 goto exit;
3455 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003456 return_value = os_kill_impl(module, pid, signal);
3457
3458exit:
3459 return return_value;
3460}
3461
3462#endif /* defined(HAVE_KILL) */
3463
3464#if defined(HAVE_KILLPG)
3465
3466PyDoc_STRVAR(os_killpg__doc__,
3467"killpg($module, pgid, signal, /)\n"
3468"--\n"
3469"\n"
3470"Kill a process group with a signal.");
3471
3472#define OS_KILLPG_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02003473 {"killpg", (PyCFunction)(void(*)(void))os_killpg, METH_FASTCALL, os_killpg__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003474
3475static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003476os_killpg_impl(PyObject *module, pid_t pgid, int signal);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003477
3478static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003479os_killpg(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003480{
3481 PyObject *return_value = NULL;
3482 pid_t pgid;
3483 int signal;
3484
Sylvain74453812017-06-10 06:51:48 +02003485 if (!_PyArg_ParseStack(args, nargs, "" _Py_PARSE_PID "i:killpg",
3486 &pgid, &signal)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01003487 goto exit;
3488 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003489 return_value = os_killpg_impl(module, pgid, signal);
3490
3491exit:
3492 return return_value;
3493}
3494
3495#endif /* defined(HAVE_KILLPG) */
3496
3497#if defined(HAVE_PLOCK)
3498
3499PyDoc_STRVAR(os_plock__doc__,
3500"plock($module, op, /)\n"
3501"--\n"
3502"\n"
3503"Lock program segments into memory.\");");
3504
3505#define OS_PLOCK_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03003506 {"plock", (PyCFunction)os_plock, METH_O, os_plock__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003507
3508static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003509os_plock_impl(PyObject *module, int op);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003510
3511static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003512os_plock(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003513{
3514 PyObject *return_value = NULL;
3515 int op;
3516
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02003517 if (PyFloat_Check(arg)) {
3518 PyErr_SetString(PyExc_TypeError,
3519 "integer argument expected, got float" );
3520 goto exit;
3521 }
3522 op = _PyLong_AsInt(arg);
3523 if (op == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003524 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03003525 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003526 return_value = os_plock_impl(module, op);
3527
3528exit:
3529 return return_value;
3530}
3531
3532#endif /* defined(HAVE_PLOCK) */
3533
3534#if defined(HAVE_SETUID)
3535
3536PyDoc_STRVAR(os_setuid__doc__,
3537"setuid($module, uid, /)\n"
3538"--\n"
3539"\n"
3540"Set the current process\'s user id.");
3541
3542#define OS_SETUID_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03003543 {"setuid", (PyCFunction)os_setuid, METH_O, os_setuid__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003544
3545static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003546os_setuid_impl(PyObject *module, uid_t uid);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003547
3548static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003549os_setuid(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003550{
3551 PyObject *return_value = NULL;
3552 uid_t uid;
3553
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02003554 if (!_Py_Uid_Converter(arg, &uid)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003555 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03003556 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003557 return_value = os_setuid_impl(module, uid);
3558
3559exit:
3560 return return_value;
3561}
3562
3563#endif /* defined(HAVE_SETUID) */
3564
3565#if defined(HAVE_SETEUID)
3566
3567PyDoc_STRVAR(os_seteuid__doc__,
3568"seteuid($module, euid, /)\n"
3569"--\n"
3570"\n"
3571"Set the current process\'s effective user id.");
3572
3573#define OS_SETEUID_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03003574 {"seteuid", (PyCFunction)os_seteuid, METH_O, os_seteuid__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003575
3576static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003577os_seteuid_impl(PyObject *module, uid_t euid);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003578
3579static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003580os_seteuid(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003581{
3582 PyObject *return_value = NULL;
3583 uid_t euid;
3584
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02003585 if (!_Py_Uid_Converter(arg, &euid)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003586 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03003587 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003588 return_value = os_seteuid_impl(module, euid);
3589
3590exit:
3591 return return_value;
3592}
3593
3594#endif /* defined(HAVE_SETEUID) */
3595
3596#if defined(HAVE_SETEGID)
3597
3598PyDoc_STRVAR(os_setegid__doc__,
3599"setegid($module, egid, /)\n"
3600"--\n"
3601"\n"
3602"Set the current process\'s effective group id.");
3603
3604#define OS_SETEGID_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03003605 {"setegid", (PyCFunction)os_setegid, METH_O, os_setegid__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003606
3607static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003608os_setegid_impl(PyObject *module, gid_t egid);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003609
3610static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003611os_setegid(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003612{
3613 PyObject *return_value = NULL;
3614 gid_t egid;
3615
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02003616 if (!_Py_Gid_Converter(arg, &egid)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003617 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03003618 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003619 return_value = os_setegid_impl(module, egid);
3620
3621exit:
3622 return return_value;
3623}
3624
3625#endif /* defined(HAVE_SETEGID) */
3626
3627#if defined(HAVE_SETREUID)
3628
3629PyDoc_STRVAR(os_setreuid__doc__,
3630"setreuid($module, ruid, euid, /)\n"
3631"--\n"
3632"\n"
3633"Set the current process\'s real and effective user ids.");
3634
3635#define OS_SETREUID_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02003636 {"setreuid", (PyCFunction)(void(*)(void))os_setreuid, METH_FASTCALL, os_setreuid__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003637
3638static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003639os_setreuid_impl(PyObject *module, uid_t ruid, uid_t euid);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003640
3641static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003642os_setreuid(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003643{
3644 PyObject *return_value = NULL;
3645 uid_t ruid;
3646 uid_t euid;
3647
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02003648 if (!_PyArg_CheckPositional("setreuid", nargs, 2, 2)) {
3649 goto exit;
3650 }
3651 if (!_Py_Uid_Converter(args[0], &ruid)) {
3652 goto exit;
3653 }
3654 if (!_Py_Uid_Converter(args[1], &euid)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01003655 goto exit;
3656 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003657 return_value = os_setreuid_impl(module, ruid, euid);
3658
3659exit:
3660 return return_value;
3661}
3662
3663#endif /* defined(HAVE_SETREUID) */
3664
3665#if defined(HAVE_SETREGID)
3666
3667PyDoc_STRVAR(os_setregid__doc__,
3668"setregid($module, rgid, egid, /)\n"
3669"--\n"
3670"\n"
3671"Set the current process\'s real and effective group ids.");
3672
3673#define OS_SETREGID_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02003674 {"setregid", (PyCFunction)(void(*)(void))os_setregid, METH_FASTCALL, os_setregid__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003675
3676static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003677os_setregid_impl(PyObject *module, gid_t rgid, gid_t egid);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003678
3679static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003680os_setregid(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003681{
3682 PyObject *return_value = NULL;
3683 gid_t rgid;
3684 gid_t egid;
3685
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02003686 if (!_PyArg_CheckPositional("setregid", nargs, 2, 2)) {
3687 goto exit;
3688 }
3689 if (!_Py_Gid_Converter(args[0], &rgid)) {
3690 goto exit;
3691 }
3692 if (!_Py_Gid_Converter(args[1], &egid)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01003693 goto exit;
3694 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003695 return_value = os_setregid_impl(module, rgid, egid);
3696
3697exit:
3698 return return_value;
3699}
3700
3701#endif /* defined(HAVE_SETREGID) */
3702
3703#if defined(HAVE_SETGID)
3704
3705PyDoc_STRVAR(os_setgid__doc__,
3706"setgid($module, gid, /)\n"
3707"--\n"
3708"\n"
3709"Set the current process\'s group id.");
3710
3711#define OS_SETGID_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03003712 {"setgid", (PyCFunction)os_setgid, METH_O, os_setgid__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003713
3714static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003715os_setgid_impl(PyObject *module, gid_t gid);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003716
3717static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003718os_setgid(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003719{
3720 PyObject *return_value = NULL;
3721 gid_t gid;
3722
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02003723 if (!_Py_Gid_Converter(arg, &gid)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003724 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03003725 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003726 return_value = os_setgid_impl(module, gid);
3727
3728exit:
3729 return return_value;
3730}
3731
3732#endif /* defined(HAVE_SETGID) */
3733
3734#if defined(HAVE_SETGROUPS)
3735
3736PyDoc_STRVAR(os_setgroups__doc__,
3737"setgroups($module, groups, /)\n"
3738"--\n"
3739"\n"
3740"Set the groups of the current process to list.");
3741
3742#define OS_SETGROUPS_METHODDEF \
3743 {"setgroups", (PyCFunction)os_setgroups, METH_O, os_setgroups__doc__},
3744
3745#endif /* defined(HAVE_SETGROUPS) */
3746
3747#if defined(HAVE_WAIT3)
3748
3749PyDoc_STRVAR(os_wait3__doc__,
3750"wait3($module, /, options)\n"
3751"--\n"
3752"\n"
3753"Wait for completion of a child process.\n"
3754"\n"
3755"Returns a tuple of information about the child process:\n"
3756" (pid, status, rusage)");
3757
3758#define OS_WAIT3_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02003759 {"wait3", (PyCFunction)(void(*)(void))os_wait3, METH_FASTCALL|METH_KEYWORDS, os_wait3__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003760
3761static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003762os_wait3_impl(PyObject *module, int options);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003763
3764static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003765os_wait3(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003766{
3767 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03003768 static const char * const _keywords[] = {"options", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02003769 static _PyArg_Parser _parser = {NULL, _keywords, "wait3", 0};
3770 PyObject *argsbuf[1];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003771 int options;
3772
Serhiy Storchaka31913912019-03-14 10:32:22 +02003773 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
3774 if (!args) {
3775 goto exit;
3776 }
3777 if (PyFloat_Check(args[0])) {
3778 PyErr_SetString(PyExc_TypeError,
3779 "integer argument expected, got float" );
3780 goto exit;
3781 }
3782 options = _PyLong_AsInt(args[0]);
3783 if (options == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003784 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03003785 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003786 return_value = os_wait3_impl(module, options);
3787
3788exit:
3789 return return_value;
3790}
3791
3792#endif /* defined(HAVE_WAIT3) */
3793
3794#if defined(HAVE_WAIT4)
3795
3796PyDoc_STRVAR(os_wait4__doc__,
3797"wait4($module, /, pid, options)\n"
3798"--\n"
3799"\n"
3800"Wait for completion of a specific child process.\n"
3801"\n"
3802"Returns a tuple of information about the child process:\n"
3803" (pid, status, rusage)");
3804
3805#define OS_WAIT4_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02003806 {"wait4", (PyCFunction)(void(*)(void))os_wait4, METH_FASTCALL|METH_KEYWORDS, os_wait4__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003807
3808static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003809os_wait4_impl(PyObject *module, pid_t pid, int options);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003810
3811static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003812os_wait4(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003813{
3814 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03003815 static const char * const _keywords[] = {"pid", "options", NULL};
3816 static _PyArg_Parser _parser = {"" _Py_PARSE_PID "i:wait4", _keywords, 0};
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003817 pid_t pid;
3818 int options;
3819
Victor Stinner3e1fad62017-01-17 01:29:01 +01003820 if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03003821 &pid, &options)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003822 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03003823 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003824 return_value = os_wait4_impl(module, pid, options);
3825
3826exit:
3827 return return_value;
3828}
3829
3830#endif /* defined(HAVE_WAIT4) */
3831
3832#if (defined(HAVE_WAITID) && !defined(__APPLE__))
3833
3834PyDoc_STRVAR(os_waitid__doc__,
3835"waitid($module, idtype, id, options, /)\n"
3836"--\n"
3837"\n"
3838"Returns the result of waiting for a process or processes.\n"
3839"\n"
3840" idtype\n"
3841" Must be one of be P_PID, P_PGID or P_ALL.\n"
3842" id\n"
3843" The id to wait on.\n"
3844" options\n"
3845" Constructed from the ORing of one or more of WEXITED, WSTOPPED\n"
3846" or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.\n"
3847"\n"
3848"Returns either waitid_result or None if WNOHANG is specified and there are\n"
3849"no children in a waitable state.");
3850
3851#define OS_WAITID_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02003852 {"waitid", (PyCFunction)(void(*)(void))os_waitid, METH_FASTCALL, os_waitid__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003853
3854static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003855os_waitid_impl(PyObject *module, idtype_t idtype, id_t id, int options);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003856
3857static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003858os_waitid(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003859{
3860 PyObject *return_value = NULL;
3861 idtype_t idtype;
3862 id_t id;
3863 int options;
3864
Sylvain74453812017-06-10 06:51:48 +02003865 if (!_PyArg_ParseStack(args, nargs, "i" _Py_PARSE_PID "i:waitid",
3866 &idtype, &id, &options)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01003867 goto exit;
3868 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003869 return_value = os_waitid_impl(module, idtype, id, options);
3870
3871exit:
3872 return return_value;
3873}
3874
3875#endif /* (defined(HAVE_WAITID) && !defined(__APPLE__)) */
3876
3877#if defined(HAVE_WAITPID)
3878
3879PyDoc_STRVAR(os_waitpid__doc__,
3880"waitpid($module, pid, options, /)\n"
3881"--\n"
3882"\n"
3883"Wait for completion of a given child process.\n"
3884"\n"
3885"Returns a tuple of information regarding the child process:\n"
3886" (pid, status)\n"
3887"\n"
3888"The options argument is ignored on Windows.");
3889
3890#define OS_WAITPID_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02003891 {"waitpid", (PyCFunction)(void(*)(void))os_waitpid, METH_FASTCALL, os_waitpid__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003892
3893static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003894os_waitpid_impl(PyObject *module, pid_t pid, int options);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003895
3896static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003897os_waitpid(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003898{
3899 PyObject *return_value = NULL;
3900 pid_t pid;
3901 int options;
3902
Sylvain74453812017-06-10 06:51:48 +02003903 if (!_PyArg_ParseStack(args, nargs, "" _Py_PARSE_PID "i:waitpid",
3904 &pid, &options)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01003905 goto exit;
3906 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003907 return_value = os_waitpid_impl(module, pid, options);
3908
3909exit:
3910 return return_value;
3911}
3912
3913#endif /* defined(HAVE_WAITPID) */
3914
3915#if defined(HAVE_CWAIT)
3916
3917PyDoc_STRVAR(os_waitpid__doc__,
3918"waitpid($module, pid, options, /)\n"
3919"--\n"
3920"\n"
3921"Wait for completion of a given process.\n"
3922"\n"
3923"Returns a tuple of information regarding the process:\n"
3924" (pid, status << 8)\n"
3925"\n"
3926"The options argument is ignored on Windows.");
3927
3928#define OS_WAITPID_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02003929 {"waitpid", (PyCFunction)(void(*)(void))os_waitpid, METH_FASTCALL, os_waitpid__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003930
3931static PyObject *
Victor Stinner581139c2016-09-06 15:54:20 -07003932os_waitpid_impl(PyObject *module, intptr_t pid, int options);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003933
3934static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003935os_waitpid(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003936{
3937 PyObject *return_value = NULL;
Victor Stinner581139c2016-09-06 15:54:20 -07003938 intptr_t pid;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003939 int options;
3940
Sylvain74453812017-06-10 06:51:48 +02003941 if (!_PyArg_ParseStack(args, nargs, "" _Py_PARSE_INTPTR "i:waitpid",
3942 &pid, &options)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01003943 goto exit;
3944 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003945 return_value = os_waitpid_impl(module, pid, options);
3946
3947exit:
3948 return return_value;
3949}
3950
3951#endif /* defined(HAVE_CWAIT) */
3952
3953#if defined(HAVE_WAIT)
3954
3955PyDoc_STRVAR(os_wait__doc__,
3956"wait($module, /)\n"
3957"--\n"
3958"\n"
3959"Wait for completion of a child process.\n"
3960"\n"
3961"Returns a tuple of information about the child process:\n"
3962" (pid, status)");
3963
3964#define OS_WAIT_METHODDEF \
3965 {"wait", (PyCFunction)os_wait, METH_NOARGS, os_wait__doc__},
3966
3967static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003968os_wait_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003969
3970static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003971os_wait(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003972{
3973 return os_wait_impl(module);
3974}
3975
3976#endif /* defined(HAVE_WAIT) */
3977
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03003978#if (defined(HAVE_READLINK) || defined(MS_WINDOWS))
3979
3980PyDoc_STRVAR(os_readlink__doc__,
3981"readlink($module, /, path, *, dir_fd=None)\n"
3982"--\n"
3983"\n"
3984"Return a string representing the path to which the symbolic link points.\n"
3985"\n"
3986"If dir_fd is not None, it should be a file descriptor open to a directory,\n"
3987"and path should be relative; path will then be relative to that directory.\n"
3988"\n"
3989"dir_fd may not be implemented on your platform. If it is unavailable,\n"
3990"using it will raise a NotImplementedError.");
3991
3992#define OS_READLINK_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02003993 {"readlink", (PyCFunction)(void(*)(void))os_readlink, METH_FASTCALL|METH_KEYWORDS, os_readlink__doc__},
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03003994
3995static PyObject *
3996os_readlink_impl(PyObject *module, path_t *path, int dir_fd);
3997
3998static PyObject *
3999os_readlink(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
4000{
4001 PyObject *return_value = NULL;
4002 static const char * const _keywords[] = {"path", "dir_fd", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02004003 static _PyArg_Parser _parser = {NULL, _keywords, "readlink", 0};
4004 PyObject *argsbuf[2];
4005 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03004006 path_t path = PATH_T_INITIALIZE("readlink", "path", 0, 0);
4007 int dir_fd = DEFAULT_DIR_FD;
4008
Serhiy Storchaka31913912019-03-14 10:32:22 +02004009 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
4010 if (!args) {
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03004011 goto exit;
4012 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02004013 if (!path_converter(args[0], &path)) {
4014 goto exit;
4015 }
4016 if (!noptargs) {
4017 goto skip_optional_kwonly;
4018 }
4019 if (!READLINKAT_DIR_FD_CONVERTER(args[1], &dir_fd)) {
4020 goto exit;
4021 }
4022skip_optional_kwonly:
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03004023 return_value = os_readlink_impl(module, &path, dir_fd);
4024
4025exit:
4026 /* Cleanup for path */
4027 path_cleanup(&path);
4028
4029 return return_value;
4030}
4031
4032#endif /* (defined(HAVE_READLINK) || defined(MS_WINDOWS)) */
4033
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004034#if defined(HAVE_SYMLINK)
4035
4036PyDoc_STRVAR(os_symlink__doc__,
4037"symlink($module, /, src, dst, target_is_directory=False, *, dir_fd=None)\n"
4038"--\n"
4039"\n"
4040"Create a symbolic link pointing to src named dst.\n"
4041"\n"
4042"target_is_directory is required on Windows if the target is to be\n"
4043" interpreted as a directory. (On Windows, symlink requires\n"
4044" Windows 6.0 or greater, and raises a NotImplementedError otherwise.)\n"
4045" target_is_directory is ignored on non-Windows platforms.\n"
4046"\n"
4047"If dir_fd is not None, it should be a file descriptor open to a directory,\n"
4048" and path should be relative; path will then be relative to that directory.\n"
4049"dir_fd may not be implemented on your platform.\n"
4050" If it is unavailable, using it will raise a NotImplementedError.");
4051
4052#define OS_SYMLINK_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02004053 {"symlink", (PyCFunction)(void(*)(void))os_symlink, METH_FASTCALL|METH_KEYWORDS, os_symlink__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004054
4055static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004056os_symlink_impl(PyObject *module, path_t *src, path_t *dst,
Larry Hastings89964c42015-04-14 18:07:59 -04004057 int target_is_directory, int dir_fd);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004058
4059static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004060os_symlink(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004061{
4062 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03004063 static const char * const _keywords[] = {"src", "dst", "target_is_directory", "dir_fd", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02004064 static _PyArg_Parser _parser = {NULL, _keywords, "symlink", 0};
4065 PyObject *argsbuf[4];
4066 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004067 path_t src = PATH_T_INITIALIZE("symlink", "src", 0, 0);
4068 path_t dst = PATH_T_INITIALIZE("symlink", "dst", 0, 0);
4069 int target_is_directory = 0;
4070 int dir_fd = DEFAULT_DIR_FD;
4071
Serhiy Storchaka31913912019-03-14 10:32:22 +02004072 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 3, 0, argsbuf);
4073 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004074 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03004075 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02004076 if (!path_converter(args[0], &src)) {
4077 goto exit;
4078 }
4079 if (!path_converter(args[1], &dst)) {
4080 goto exit;
4081 }
4082 if (!noptargs) {
4083 goto skip_optional_pos;
4084 }
4085 if (args[2]) {
4086 target_is_directory = PyObject_IsTrue(args[2]);
4087 if (target_is_directory < 0) {
4088 goto exit;
4089 }
4090 if (!--noptargs) {
4091 goto skip_optional_pos;
4092 }
4093 }
4094skip_optional_pos:
4095 if (!noptargs) {
4096 goto skip_optional_kwonly;
4097 }
4098 if (!SYMLINKAT_DIR_FD_CONVERTER(args[3], &dir_fd)) {
4099 goto exit;
4100 }
4101skip_optional_kwonly:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004102 return_value = os_symlink_impl(module, &src, &dst, target_is_directory, dir_fd);
4103
4104exit:
4105 /* Cleanup for src */
4106 path_cleanup(&src);
4107 /* Cleanup for dst */
4108 path_cleanup(&dst);
4109
4110 return return_value;
4111}
4112
4113#endif /* defined(HAVE_SYMLINK) */
4114
4115#if defined(HAVE_TIMES)
4116
4117PyDoc_STRVAR(os_times__doc__,
4118"times($module, /)\n"
4119"--\n"
4120"\n"
4121"Return a collection containing process timing information.\n"
4122"\n"
4123"The object returned behaves like a named tuple with these fields:\n"
4124" (utime, stime, cutime, cstime, elapsed_time)\n"
4125"All fields are floating point numbers.");
4126
4127#define OS_TIMES_METHODDEF \
4128 {"times", (PyCFunction)os_times, METH_NOARGS, os_times__doc__},
4129
4130static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004131os_times_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004132
4133static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004134os_times(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004135{
4136 return os_times_impl(module);
4137}
4138
4139#endif /* defined(HAVE_TIMES) */
4140
4141#if defined(HAVE_GETSID)
4142
4143PyDoc_STRVAR(os_getsid__doc__,
4144"getsid($module, pid, /)\n"
4145"--\n"
4146"\n"
4147"Call the system call getsid(pid) and return the result.");
4148
4149#define OS_GETSID_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03004150 {"getsid", (PyCFunction)os_getsid, METH_O, os_getsid__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004151
4152static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004153os_getsid_impl(PyObject *module, pid_t pid);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004154
4155static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004156os_getsid(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004157{
4158 PyObject *return_value = NULL;
4159 pid_t pid;
4160
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03004161 if (!PyArg_Parse(arg, "" _Py_PARSE_PID ":getsid", &pid)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004162 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03004163 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004164 return_value = os_getsid_impl(module, pid);
4165
4166exit:
4167 return return_value;
4168}
4169
4170#endif /* defined(HAVE_GETSID) */
4171
4172#if defined(HAVE_SETSID)
4173
4174PyDoc_STRVAR(os_setsid__doc__,
4175"setsid($module, /)\n"
4176"--\n"
4177"\n"
4178"Call the system call setsid().");
4179
4180#define OS_SETSID_METHODDEF \
4181 {"setsid", (PyCFunction)os_setsid, METH_NOARGS, os_setsid__doc__},
4182
4183static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004184os_setsid_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004185
4186static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004187os_setsid(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004188{
4189 return os_setsid_impl(module);
4190}
4191
4192#endif /* defined(HAVE_SETSID) */
4193
4194#if defined(HAVE_SETPGID)
4195
4196PyDoc_STRVAR(os_setpgid__doc__,
4197"setpgid($module, pid, pgrp, /)\n"
4198"--\n"
4199"\n"
4200"Call the system call setpgid(pid, pgrp).");
4201
4202#define OS_SETPGID_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02004203 {"setpgid", (PyCFunction)(void(*)(void))os_setpgid, METH_FASTCALL, os_setpgid__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004204
4205static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004206os_setpgid_impl(PyObject *module, pid_t pid, pid_t pgrp);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004207
4208static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004209os_setpgid(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004210{
4211 PyObject *return_value = NULL;
4212 pid_t pid;
4213 pid_t pgrp;
4214
Sylvain74453812017-06-10 06:51:48 +02004215 if (!_PyArg_ParseStack(args, nargs, "" _Py_PARSE_PID "" _Py_PARSE_PID ":setpgid",
4216 &pid, &pgrp)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01004217 goto exit;
4218 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004219 return_value = os_setpgid_impl(module, pid, pgrp);
4220
4221exit:
4222 return return_value;
4223}
4224
4225#endif /* defined(HAVE_SETPGID) */
4226
4227#if defined(HAVE_TCGETPGRP)
4228
4229PyDoc_STRVAR(os_tcgetpgrp__doc__,
4230"tcgetpgrp($module, fd, /)\n"
4231"--\n"
4232"\n"
4233"Return the process group associated with the terminal specified by fd.");
4234
4235#define OS_TCGETPGRP_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03004236 {"tcgetpgrp", (PyCFunction)os_tcgetpgrp, METH_O, os_tcgetpgrp__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004237
4238static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004239os_tcgetpgrp_impl(PyObject *module, int fd);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004240
4241static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004242os_tcgetpgrp(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004243{
4244 PyObject *return_value = NULL;
4245 int fd;
4246
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02004247 if (PyFloat_Check(arg)) {
4248 PyErr_SetString(PyExc_TypeError,
4249 "integer argument expected, got float" );
4250 goto exit;
4251 }
4252 fd = _PyLong_AsInt(arg);
4253 if (fd == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004254 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03004255 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004256 return_value = os_tcgetpgrp_impl(module, fd);
4257
4258exit:
4259 return return_value;
4260}
4261
4262#endif /* defined(HAVE_TCGETPGRP) */
4263
4264#if defined(HAVE_TCSETPGRP)
4265
4266PyDoc_STRVAR(os_tcsetpgrp__doc__,
4267"tcsetpgrp($module, fd, pgid, /)\n"
4268"--\n"
4269"\n"
4270"Set the process group associated with the terminal specified by fd.");
4271
4272#define OS_TCSETPGRP_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02004273 {"tcsetpgrp", (PyCFunction)(void(*)(void))os_tcsetpgrp, METH_FASTCALL, os_tcsetpgrp__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004274
4275static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004276os_tcsetpgrp_impl(PyObject *module, int fd, pid_t pgid);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004277
4278static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004279os_tcsetpgrp(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004280{
4281 PyObject *return_value = NULL;
4282 int fd;
4283 pid_t pgid;
4284
Sylvain74453812017-06-10 06:51:48 +02004285 if (!_PyArg_ParseStack(args, nargs, "i" _Py_PARSE_PID ":tcsetpgrp",
4286 &fd, &pgid)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01004287 goto exit;
4288 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004289 return_value = os_tcsetpgrp_impl(module, fd, pgid);
4290
4291exit:
4292 return return_value;
4293}
4294
4295#endif /* defined(HAVE_TCSETPGRP) */
4296
4297PyDoc_STRVAR(os_open__doc__,
4298"open($module, /, path, flags, mode=511, *, dir_fd=None)\n"
4299"--\n"
4300"\n"
4301"Open a file for low level IO. Returns a file descriptor (integer).\n"
4302"\n"
4303"If dir_fd is not None, it should be a file descriptor open to a directory,\n"
4304" and path should be relative; path will then be relative to that directory.\n"
4305"dir_fd may not be implemented on your platform.\n"
4306" If it is unavailable, using it will raise a NotImplementedError.");
4307
4308#define OS_OPEN_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02004309 {"open", (PyCFunction)(void(*)(void))os_open, METH_FASTCALL|METH_KEYWORDS, os_open__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004310
4311static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004312os_open_impl(PyObject *module, path_t *path, int flags, int mode, int dir_fd);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004313
4314static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004315os_open(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004316{
4317 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03004318 static const char * const _keywords[] = {"path", "flags", "mode", "dir_fd", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02004319 static _PyArg_Parser _parser = {NULL, _keywords, "open", 0};
4320 PyObject *argsbuf[4];
4321 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004322 path_t path = PATH_T_INITIALIZE("open", "path", 0, 0);
4323 int flags;
4324 int mode = 511;
4325 int dir_fd = DEFAULT_DIR_FD;
4326 int _return_value;
4327
Serhiy Storchaka31913912019-03-14 10:32:22 +02004328 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 3, 0, argsbuf);
4329 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004330 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03004331 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02004332 if (!path_converter(args[0], &path)) {
4333 goto exit;
4334 }
4335 if (PyFloat_Check(args[1])) {
4336 PyErr_SetString(PyExc_TypeError,
4337 "integer argument expected, got float" );
4338 goto exit;
4339 }
4340 flags = _PyLong_AsInt(args[1]);
4341 if (flags == -1 && PyErr_Occurred()) {
4342 goto exit;
4343 }
4344 if (!noptargs) {
4345 goto skip_optional_pos;
4346 }
4347 if (args[2]) {
4348 if (PyFloat_Check(args[2])) {
4349 PyErr_SetString(PyExc_TypeError,
4350 "integer argument expected, got float" );
4351 goto exit;
4352 }
4353 mode = _PyLong_AsInt(args[2]);
4354 if (mode == -1 && PyErr_Occurred()) {
4355 goto exit;
4356 }
4357 if (!--noptargs) {
4358 goto skip_optional_pos;
4359 }
4360 }
4361skip_optional_pos:
4362 if (!noptargs) {
4363 goto skip_optional_kwonly;
4364 }
4365 if (!OPENAT_DIR_FD_CONVERTER(args[3], &dir_fd)) {
4366 goto exit;
4367 }
4368skip_optional_kwonly:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004369 _return_value = os_open_impl(module, &path, flags, mode, dir_fd);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03004370 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004371 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03004372 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004373 return_value = PyLong_FromLong((long)_return_value);
4374
4375exit:
4376 /* Cleanup for path */
4377 path_cleanup(&path);
4378
4379 return return_value;
4380}
4381
4382PyDoc_STRVAR(os_close__doc__,
4383"close($module, /, fd)\n"
4384"--\n"
4385"\n"
4386"Close a file descriptor.");
4387
4388#define OS_CLOSE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02004389 {"close", (PyCFunction)(void(*)(void))os_close, METH_FASTCALL|METH_KEYWORDS, os_close__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004390
4391static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004392os_close_impl(PyObject *module, int fd);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004393
4394static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004395os_close(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004396{
4397 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03004398 static const char * const _keywords[] = {"fd", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02004399 static _PyArg_Parser _parser = {NULL, _keywords, "close", 0};
4400 PyObject *argsbuf[1];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004401 int fd;
4402
Serhiy Storchaka31913912019-03-14 10:32:22 +02004403 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
4404 if (!args) {
4405 goto exit;
4406 }
4407 if (PyFloat_Check(args[0])) {
4408 PyErr_SetString(PyExc_TypeError,
4409 "integer argument expected, got float" );
4410 goto exit;
4411 }
4412 fd = _PyLong_AsInt(args[0]);
4413 if (fd == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004414 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03004415 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004416 return_value = os_close_impl(module, fd);
4417
4418exit:
4419 return return_value;
4420}
4421
4422PyDoc_STRVAR(os_closerange__doc__,
4423"closerange($module, fd_low, fd_high, /)\n"
4424"--\n"
4425"\n"
4426"Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
4427
4428#define OS_CLOSERANGE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02004429 {"closerange", (PyCFunction)(void(*)(void))os_closerange, METH_FASTCALL, os_closerange__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004430
4431static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004432os_closerange_impl(PyObject *module, int fd_low, int fd_high);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004433
4434static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004435os_closerange(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004436{
4437 PyObject *return_value = NULL;
4438 int fd_low;
4439 int fd_high;
4440
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02004441 if (!_PyArg_CheckPositional("closerange", nargs, 2, 2)) {
4442 goto exit;
4443 }
4444 if (PyFloat_Check(args[0])) {
4445 PyErr_SetString(PyExc_TypeError,
4446 "integer argument expected, got float" );
4447 goto exit;
4448 }
4449 fd_low = _PyLong_AsInt(args[0]);
4450 if (fd_low == -1 && PyErr_Occurred()) {
4451 goto exit;
4452 }
4453 if (PyFloat_Check(args[1])) {
4454 PyErr_SetString(PyExc_TypeError,
4455 "integer argument expected, got float" );
4456 goto exit;
4457 }
4458 fd_high = _PyLong_AsInt(args[1]);
4459 if (fd_high == -1 && PyErr_Occurred()) {
Victor Stinner259f0e42017-01-17 01:35:17 +01004460 goto exit;
4461 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004462 return_value = os_closerange_impl(module, fd_low, fd_high);
4463
4464exit:
4465 return return_value;
4466}
4467
4468PyDoc_STRVAR(os_dup__doc__,
4469"dup($module, fd, /)\n"
4470"--\n"
4471"\n"
4472"Return a duplicate of a file descriptor.");
4473
4474#define OS_DUP_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03004475 {"dup", (PyCFunction)os_dup, METH_O, os_dup__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004476
4477static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004478os_dup_impl(PyObject *module, int fd);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004479
4480static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004481os_dup(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004482{
4483 PyObject *return_value = NULL;
4484 int fd;
4485 int _return_value;
4486
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02004487 if (PyFloat_Check(arg)) {
4488 PyErr_SetString(PyExc_TypeError,
4489 "integer argument expected, got float" );
4490 goto exit;
4491 }
4492 fd = _PyLong_AsInt(arg);
4493 if (fd == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004494 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03004495 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004496 _return_value = os_dup_impl(module, fd);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03004497 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004498 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03004499 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004500 return_value = PyLong_FromLong((long)_return_value);
4501
4502exit:
4503 return return_value;
4504}
4505
4506PyDoc_STRVAR(os_dup2__doc__,
4507"dup2($module, /, fd, fd2, inheritable=True)\n"
4508"--\n"
4509"\n"
4510"Duplicate file descriptor.");
4511
4512#define OS_DUP2_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02004513 {"dup2", (PyCFunction)(void(*)(void))os_dup2, METH_FASTCALL|METH_KEYWORDS, os_dup2__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004514
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08004515static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004516os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004517
4518static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004519os_dup2(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004520{
4521 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03004522 static const char * const _keywords[] = {"fd", "fd2", "inheritable", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02004523 static _PyArg_Parser _parser = {NULL, _keywords, "dup2", 0};
4524 PyObject *argsbuf[3];
4525 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004526 int fd;
4527 int fd2;
4528 int inheritable = 1;
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08004529 int _return_value;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004530
Serhiy Storchaka31913912019-03-14 10:32:22 +02004531 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 3, 0, argsbuf);
4532 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004533 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03004534 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02004535 if (PyFloat_Check(args[0])) {
4536 PyErr_SetString(PyExc_TypeError,
4537 "integer argument expected, got float" );
4538 goto exit;
4539 }
4540 fd = _PyLong_AsInt(args[0]);
4541 if (fd == -1 && PyErr_Occurred()) {
4542 goto exit;
4543 }
4544 if (PyFloat_Check(args[1])) {
4545 PyErr_SetString(PyExc_TypeError,
4546 "integer argument expected, got float" );
4547 goto exit;
4548 }
4549 fd2 = _PyLong_AsInt(args[1]);
4550 if (fd2 == -1 && PyErr_Occurred()) {
4551 goto exit;
4552 }
4553 if (!noptargs) {
4554 goto skip_optional_pos;
4555 }
4556 inheritable = PyObject_IsTrue(args[2]);
4557 if (inheritable < 0) {
4558 goto exit;
4559 }
4560skip_optional_pos:
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08004561 _return_value = os_dup2_impl(module, fd, fd2, inheritable);
4562 if ((_return_value == -1) && PyErr_Occurred()) {
4563 goto exit;
4564 }
4565 return_value = PyLong_FromLong((long)_return_value);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004566
4567exit:
4568 return return_value;
4569}
4570
4571#if defined(HAVE_LOCKF)
4572
4573PyDoc_STRVAR(os_lockf__doc__,
4574"lockf($module, fd, command, length, /)\n"
4575"--\n"
4576"\n"
4577"Apply, test or remove a POSIX lock on an open file descriptor.\n"
4578"\n"
4579" fd\n"
4580" An open file descriptor.\n"
4581" command\n"
4582" One of F_LOCK, F_TLOCK, F_ULOCK or F_TEST.\n"
4583" length\n"
4584" The number of bytes to lock, starting at the current position.");
4585
4586#define OS_LOCKF_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02004587 {"lockf", (PyCFunction)(void(*)(void))os_lockf, METH_FASTCALL, os_lockf__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004588
4589static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004590os_lockf_impl(PyObject *module, int fd, int command, Py_off_t length);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004591
4592static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004593os_lockf(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004594{
4595 PyObject *return_value = NULL;
4596 int fd;
4597 int command;
4598 Py_off_t length;
4599
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02004600 if (!_PyArg_CheckPositional("lockf", nargs, 3, 3)) {
4601 goto exit;
4602 }
4603 if (PyFloat_Check(args[0])) {
4604 PyErr_SetString(PyExc_TypeError,
4605 "integer argument expected, got float" );
4606 goto exit;
4607 }
4608 fd = _PyLong_AsInt(args[0]);
4609 if (fd == -1 && PyErr_Occurred()) {
4610 goto exit;
4611 }
4612 if (PyFloat_Check(args[1])) {
4613 PyErr_SetString(PyExc_TypeError,
4614 "integer argument expected, got float" );
4615 goto exit;
4616 }
4617 command = _PyLong_AsInt(args[1]);
4618 if (command == -1 && PyErr_Occurred()) {
4619 goto exit;
4620 }
4621 if (!Py_off_t_converter(args[2], &length)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01004622 goto exit;
4623 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004624 return_value = os_lockf_impl(module, fd, command, length);
4625
4626exit:
4627 return return_value;
4628}
4629
4630#endif /* defined(HAVE_LOCKF) */
4631
4632PyDoc_STRVAR(os_lseek__doc__,
4633"lseek($module, fd, position, how, /)\n"
4634"--\n"
4635"\n"
4636"Set the position of a file descriptor. Return the new position.\n"
4637"\n"
4638"Return the new cursor position in number of bytes\n"
4639"relative to the beginning of the file.");
4640
4641#define OS_LSEEK_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02004642 {"lseek", (PyCFunction)(void(*)(void))os_lseek, METH_FASTCALL, os_lseek__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004643
4644static Py_off_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004645os_lseek_impl(PyObject *module, int fd, Py_off_t position, int how);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004646
4647static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004648os_lseek(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004649{
4650 PyObject *return_value = NULL;
4651 int fd;
4652 Py_off_t position;
4653 int how;
4654 Py_off_t _return_value;
4655
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02004656 if (!_PyArg_CheckPositional("lseek", nargs, 3, 3)) {
4657 goto exit;
4658 }
4659 if (PyFloat_Check(args[0])) {
4660 PyErr_SetString(PyExc_TypeError,
4661 "integer argument expected, got float" );
4662 goto exit;
4663 }
4664 fd = _PyLong_AsInt(args[0]);
4665 if (fd == -1 && PyErr_Occurred()) {
4666 goto exit;
4667 }
4668 if (!Py_off_t_converter(args[1], &position)) {
4669 goto exit;
4670 }
4671 if (PyFloat_Check(args[2])) {
4672 PyErr_SetString(PyExc_TypeError,
4673 "integer argument expected, got float" );
4674 goto exit;
4675 }
4676 how = _PyLong_AsInt(args[2]);
4677 if (how == -1 && PyErr_Occurred()) {
Victor Stinner259f0e42017-01-17 01:35:17 +01004678 goto exit;
4679 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004680 _return_value = os_lseek_impl(module, fd, position, how);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03004681 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004682 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03004683 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004684 return_value = PyLong_FromPy_off_t(_return_value);
4685
4686exit:
4687 return return_value;
4688}
4689
4690PyDoc_STRVAR(os_read__doc__,
4691"read($module, fd, length, /)\n"
4692"--\n"
4693"\n"
4694"Read from a file descriptor. Returns a bytes object.");
4695
4696#define OS_READ_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02004697 {"read", (PyCFunction)(void(*)(void))os_read, METH_FASTCALL, os_read__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004698
4699static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004700os_read_impl(PyObject *module, int fd, Py_ssize_t length);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004701
4702static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004703os_read(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004704{
4705 PyObject *return_value = NULL;
4706 int fd;
4707 Py_ssize_t length;
4708
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02004709 if (!_PyArg_CheckPositional("read", nargs, 2, 2)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01004710 goto exit;
4711 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02004712 if (PyFloat_Check(args[0])) {
4713 PyErr_SetString(PyExc_TypeError,
4714 "integer argument expected, got float" );
4715 goto exit;
4716 }
4717 fd = _PyLong_AsInt(args[0]);
4718 if (fd == -1 && PyErr_Occurred()) {
4719 goto exit;
4720 }
4721 if (PyFloat_Check(args[1])) {
4722 PyErr_SetString(PyExc_TypeError,
4723 "integer argument expected, got float" );
4724 goto exit;
4725 }
4726 {
4727 Py_ssize_t ival = -1;
4728 PyObject *iobj = PyNumber_Index(args[1]);
4729 if (iobj != NULL) {
4730 ival = PyLong_AsSsize_t(iobj);
4731 Py_DECREF(iobj);
4732 }
4733 if (ival == -1 && PyErr_Occurred()) {
4734 goto exit;
4735 }
4736 length = ival;
4737 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004738 return_value = os_read_impl(module, fd, length);
4739
4740exit:
4741 return return_value;
4742}
4743
4744#if defined(HAVE_READV)
4745
4746PyDoc_STRVAR(os_readv__doc__,
4747"readv($module, fd, buffers, /)\n"
4748"--\n"
4749"\n"
4750"Read from a file descriptor fd into an iterable of buffers.\n"
4751"\n"
4752"The buffers should be mutable buffers accepting bytes.\n"
4753"readv will transfer data into each buffer until it is full\n"
4754"and then move on to the next buffer in the sequence to hold\n"
4755"the rest of the data.\n"
4756"\n"
4757"readv returns the total number of bytes read,\n"
4758"which may be less than the total capacity of all the buffers.");
4759
4760#define OS_READV_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02004761 {"readv", (PyCFunction)(void(*)(void))os_readv, METH_FASTCALL, os_readv__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004762
4763static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004764os_readv_impl(PyObject *module, int fd, PyObject *buffers);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004765
4766static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004767os_readv(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004768{
4769 PyObject *return_value = NULL;
4770 int fd;
4771 PyObject *buffers;
4772 Py_ssize_t _return_value;
4773
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02004774 if (!_PyArg_CheckPositional("readv", nargs, 2, 2)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01004775 goto exit;
4776 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02004777 if (PyFloat_Check(args[0])) {
4778 PyErr_SetString(PyExc_TypeError,
4779 "integer argument expected, got float" );
4780 goto exit;
4781 }
4782 fd = _PyLong_AsInt(args[0]);
4783 if (fd == -1 && PyErr_Occurred()) {
4784 goto exit;
4785 }
4786 buffers = args[1];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004787 _return_value = os_readv_impl(module, fd, buffers);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03004788 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004789 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03004790 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004791 return_value = PyLong_FromSsize_t(_return_value);
4792
4793exit:
4794 return return_value;
4795}
4796
4797#endif /* defined(HAVE_READV) */
4798
4799#if defined(HAVE_PREAD)
4800
4801PyDoc_STRVAR(os_pread__doc__,
4802"pread($module, fd, length, offset, /)\n"
4803"--\n"
4804"\n"
4805"Read a number of bytes from a file descriptor starting at a particular offset.\n"
4806"\n"
4807"Read length bytes from file descriptor fd, starting at offset bytes from\n"
4808"the beginning of the file. The file offset remains unchanged.");
4809
4810#define OS_PREAD_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02004811 {"pread", (PyCFunction)(void(*)(void))os_pread, METH_FASTCALL, os_pread__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004812
4813static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004814os_pread_impl(PyObject *module, int fd, int length, Py_off_t offset);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004815
4816static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004817os_pread(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004818{
4819 PyObject *return_value = NULL;
4820 int fd;
4821 int length;
4822 Py_off_t offset;
4823
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02004824 if (!_PyArg_CheckPositional("pread", nargs, 3, 3)) {
4825 goto exit;
4826 }
4827 if (PyFloat_Check(args[0])) {
4828 PyErr_SetString(PyExc_TypeError,
4829 "integer argument expected, got float" );
4830 goto exit;
4831 }
4832 fd = _PyLong_AsInt(args[0]);
4833 if (fd == -1 && PyErr_Occurred()) {
4834 goto exit;
4835 }
4836 if (PyFloat_Check(args[1])) {
4837 PyErr_SetString(PyExc_TypeError,
4838 "integer argument expected, got float" );
4839 goto exit;
4840 }
4841 length = _PyLong_AsInt(args[1]);
4842 if (length == -1 && PyErr_Occurred()) {
4843 goto exit;
4844 }
4845 if (!Py_off_t_converter(args[2], &offset)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01004846 goto exit;
4847 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004848 return_value = os_pread_impl(module, fd, length, offset);
4849
4850exit:
4851 return return_value;
4852}
4853
4854#endif /* defined(HAVE_PREAD) */
4855
Pablo Galindo4defba32018-01-27 16:16:37 +00004856#if (defined(HAVE_PREADV) || defined (HAVE_PREADV2))
4857
4858PyDoc_STRVAR(os_preadv__doc__,
4859"preadv($module, fd, buffers, offset, flags=0, /)\n"
4860"--\n"
4861"\n"
4862"Reads from a file descriptor into a number of mutable bytes-like objects.\n"
4863"\n"
4864"Combines the functionality of readv() and pread(). As readv(), it will\n"
4865"transfer data into each buffer until it is full and then move on to the next\n"
4866"buffer in the sequence to hold the rest of the data. Its fourth argument,\n"
4867"specifies the file offset at which the input operation is to be performed. It\n"
4868"will return the total number of bytes read (which can be less than the total\n"
4869"capacity of all the objects).\n"
4870"\n"
4871"The flags argument contains a bitwise OR of zero or more of the following flags:\n"
4872"\n"
4873"- RWF_HIPRI\n"
4874"- RWF_NOWAIT\n"
4875"\n"
4876"Using non-zero flags requires Linux 4.6 or newer.");
4877
4878#define OS_PREADV_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02004879 {"preadv", (PyCFunction)(void(*)(void))os_preadv, METH_FASTCALL, os_preadv__doc__},
Pablo Galindo4defba32018-01-27 16:16:37 +00004880
4881static Py_ssize_t
4882os_preadv_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset,
4883 int flags);
4884
4885static PyObject *
4886os_preadv(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
4887{
4888 PyObject *return_value = NULL;
4889 int fd;
4890 PyObject *buffers;
4891 Py_off_t offset;
4892 int flags = 0;
4893 Py_ssize_t _return_value;
4894
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02004895 if (!_PyArg_CheckPositional("preadv", nargs, 3, 4)) {
Pablo Galindo4defba32018-01-27 16:16:37 +00004896 goto exit;
4897 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02004898 if (PyFloat_Check(args[0])) {
4899 PyErr_SetString(PyExc_TypeError,
4900 "integer argument expected, got float" );
4901 goto exit;
4902 }
4903 fd = _PyLong_AsInt(args[0]);
4904 if (fd == -1 && PyErr_Occurred()) {
4905 goto exit;
4906 }
4907 buffers = args[1];
4908 if (!Py_off_t_converter(args[2], &offset)) {
4909 goto exit;
4910 }
4911 if (nargs < 4) {
4912 goto skip_optional;
4913 }
4914 if (PyFloat_Check(args[3])) {
4915 PyErr_SetString(PyExc_TypeError,
4916 "integer argument expected, got float" );
4917 goto exit;
4918 }
4919 flags = _PyLong_AsInt(args[3]);
4920 if (flags == -1 && PyErr_Occurred()) {
4921 goto exit;
4922 }
4923skip_optional:
Pablo Galindo4defba32018-01-27 16:16:37 +00004924 _return_value = os_preadv_impl(module, fd, buffers, offset, flags);
4925 if ((_return_value == -1) && PyErr_Occurred()) {
4926 goto exit;
4927 }
4928 return_value = PyLong_FromSsize_t(_return_value);
4929
4930exit:
4931 return return_value;
4932}
4933
4934#endif /* (defined(HAVE_PREADV) || defined (HAVE_PREADV2)) */
4935
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004936PyDoc_STRVAR(os_write__doc__,
4937"write($module, fd, data, /)\n"
4938"--\n"
4939"\n"
4940"Write a bytes object to a file descriptor.");
4941
4942#define OS_WRITE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02004943 {"write", (PyCFunction)(void(*)(void))os_write, METH_FASTCALL, os_write__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004944
4945static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004946os_write_impl(PyObject *module, int fd, Py_buffer *data);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004947
4948static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004949os_write(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004950{
4951 PyObject *return_value = NULL;
4952 int fd;
4953 Py_buffer data = {NULL, NULL};
4954 Py_ssize_t _return_value;
4955
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02004956 if (!_PyArg_CheckPositional("write", nargs, 2, 2)) {
4957 goto exit;
4958 }
4959 if (PyFloat_Check(args[0])) {
4960 PyErr_SetString(PyExc_TypeError,
4961 "integer argument expected, got float" );
4962 goto exit;
4963 }
4964 fd = _PyLong_AsInt(args[0]);
4965 if (fd == -1 && PyErr_Occurred()) {
4966 goto exit;
4967 }
4968 if (PyObject_GetBuffer(args[1], &data, PyBUF_SIMPLE) != 0) {
4969 goto exit;
4970 }
4971 if (!PyBuffer_IsContiguous(&data, 'C')) {
4972 _PyArg_BadArgument("write", 2, "contiguous buffer", args[1]);
Victor Stinner259f0e42017-01-17 01:35:17 +01004973 goto exit;
4974 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004975 _return_value = os_write_impl(module, fd, &data);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03004976 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004977 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03004978 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004979 return_value = PyLong_FromSsize_t(_return_value);
4980
4981exit:
4982 /* Cleanup for data */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03004983 if (data.obj) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004984 PyBuffer_Release(&data);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03004985 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004986
4987 return return_value;
4988}
4989
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02004990#if defined(__APPLE__)
4991
4992PyDoc_STRVAR(os__fcopyfile__doc__,
4993"_fcopyfile($module, infd, outfd, flags, /)\n"
4994"--\n"
4995"\n"
Giampaolo Rodolac7f02a92018-06-19 08:27:29 -07004996"Efficiently copy content or metadata of 2 regular file descriptors (macOS).");
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02004997
4998#define OS__FCOPYFILE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02004999 {"_fcopyfile", (PyCFunction)(void(*)(void))os__fcopyfile, METH_FASTCALL, os__fcopyfile__doc__},
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02005000
5001static PyObject *
5002os__fcopyfile_impl(PyObject *module, int infd, int outfd, int flags);
5003
5004static PyObject *
5005os__fcopyfile(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
5006{
5007 PyObject *return_value = NULL;
5008 int infd;
5009 int outfd;
5010 int flags;
5011
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02005012 if (!_PyArg_CheckPositional("_fcopyfile", nargs, 3, 3)) {
5013 goto exit;
5014 }
5015 if (PyFloat_Check(args[0])) {
5016 PyErr_SetString(PyExc_TypeError,
5017 "integer argument expected, got float" );
5018 goto exit;
5019 }
5020 infd = _PyLong_AsInt(args[0]);
5021 if (infd == -1 && PyErr_Occurred()) {
5022 goto exit;
5023 }
5024 if (PyFloat_Check(args[1])) {
5025 PyErr_SetString(PyExc_TypeError,
5026 "integer argument expected, got float" );
5027 goto exit;
5028 }
5029 outfd = _PyLong_AsInt(args[1]);
5030 if (outfd == -1 && PyErr_Occurred()) {
5031 goto exit;
5032 }
5033 if (PyFloat_Check(args[2])) {
5034 PyErr_SetString(PyExc_TypeError,
5035 "integer argument expected, got float" );
5036 goto exit;
5037 }
5038 flags = _PyLong_AsInt(args[2]);
5039 if (flags == -1 && PyErr_Occurred()) {
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02005040 goto exit;
5041 }
5042 return_value = os__fcopyfile_impl(module, infd, outfd, flags);
5043
5044exit:
5045 return return_value;
5046}
5047
5048#endif /* defined(__APPLE__) */
5049
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005050PyDoc_STRVAR(os_fstat__doc__,
5051"fstat($module, /, fd)\n"
5052"--\n"
5053"\n"
5054"Perform a stat system call on the given file descriptor.\n"
5055"\n"
5056"Like stat(), but for an open file descriptor.\n"
5057"Equivalent to os.stat(fd).");
5058
5059#define OS_FSTAT_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02005060 {"fstat", (PyCFunction)(void(*)(void))os_fstat, METH_FASTCALL|METH_KEYWORDS, os_fstat__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005061
5062static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005063os_fstat_impl(PyObject *module, int fd);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005064
5065static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02005066os_fstat(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005067{
5068 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03005069 static const char * const _keywords[] = {"fd", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02005070 static _PyArg_Parser _parser = {NULL, _keywords, "fstat", 0};
5071 PyObject *argsbuf[1];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005072 int fd;
5073
Serhiy Storchaka31913912019-03-14 10:32:22 +02005074 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
5075 if (!args) {
5076 goto exit;
5077 }
5078 if (PyFloat_Check(args[0])) {
5079 PyErr_SetString(PyExc_TypeError,
5080 "integer argument expected, got float" );
5081 goto exit;
5082 }
5083 fd = _PyLong_AsInt(args[0]);
5084 if (fd == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005085 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005086 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005087 return_value = os_fstat_impl(module, fd);
5088
5089exit:
5090 return return_value;
5091}
5092
5093PyDoc_STRVAR(os_isatty__doc__,
5094"isatty($module, fd, /)\n"
5095"--\n"
5096"\n"
5097"Return True if the fd is connected to a terminal.\n"
5098"\n"
5099"Return True if the file descriptor is an open file descriptor\n"
5100"connected to the slave end of a terminal.");
5101
5102#define OS_ISATTY_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03005103 {"isatty", (PyCFunction)os_isatty, METH_O, os_isatty__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005104
5105static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005106os_isatty_impl(PyObject *module, int fd);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005107
5108static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005109os_isatty(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005110{
5111 PyObject *return_value = NULL;
5112 int fd;
5113 int _return_value;
5114
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02005115 if (PyFloat_Check(arg)) {
5116 PyErr_SetString(PyExc_TypeError,
5117 "integer argument expected, got float" );
5118 goto exit;
5119 }
5120 fd = _PyLong_AsInt(arg);
5121 if (fd == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005122 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005123 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005124 _return_value = os_isatty_impl(module, fd);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005125 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005126 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005127 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005128 return_value = PyBool_FromLong((long)_return_value);
5129
5130exit:
5131 return return_value;
5132}
5133
5134#if defined(HAVE_PIPE)
5135
5136PyDoc_STRVAR(os_pipe__doc__,
5137"pipe($module, /)\n"
5138"--\n"
5139"\n"
5140"Create a pipe.\n"
5141"\n"
5142"Returns a tuple of two file descriptors:\n"
5143" (read_fd, write_fd)");
5144
5145#define OS_PIPE_METHODDEF \
5146 {"pipe", (PyCFunction)os_pipe, METH_NOARGS, os_pipe__doc__},
5147
5148static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005149os_pipe_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005150
5151static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005152os_pipe(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005153{
5154 return os_pipe_impl(module);
5155}
5156
5157#endif /* defined(HAVE_PIPE) */
5158
5159#if defined(HAVE_PIPE2)
5160
5161PyDoc_STRVAR(os_pipe2__doc__,
5162"pipe2($module, flags, /)\n"
5163"--\n"
5164"\n"
5165"Create a pipe with flags set atomically.\n"
5166"\n"
5167"Returns a tuple of two file descriptors:\n"
5168" (read_fd, write_fd)\n"
5169"\n"
5170"flags can be constructed by ORing together one or more of these values:\n"
5171"O_NONBLOCK, O_CLOEXEC.");
5172
5173#define OS_PIPE2_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03005174 {"pipe2", (PyCFunction)os_pipe2, METH_O, os_pipe2__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005175
5176static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005177os_pipe2_impl(PyObject *module, int flags);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005178
5179static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005180os_pipe2(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005181{
5182 PyObject *return_value = NULL;
5183 int flags;
5184
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02005185 if (PyFloat_Check(arg)) {
5186 PyErr_SetString(PyExc_TypeError,
5187 "integer argument expected, got float" );
5188 goto exit;
5189 }
5190 flags = _PyLong_AsInt(arg);
5191 if (flags == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005192 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005193 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005194 return_value = os_pipe2_impl(module, flags);
5195
5196exit:
5197 return return_value;
5198}
5199
5200#endif /* defined(HAVE_PIPE2) */
5201
5202#if defined(HAVE_WRITEV)
5203
5204PyDoc_STRVAR(os_writev__doc__,
5205"writev($module, fd, buffers, /)\n"
5206"--\n"
5207"\n"
5208"Iterate over buffers, and write the contents of each to a file descriptor.\n"
5209"\n"
5210"Returns the total number of bytes written.\n"
5211"buffers must be a sequence of bytes-like objects.");
5212
5213#define OS_WRITEV_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02005214 {"writev", (PyCFunction)(void(*)(void))os_writev, METH_FASTCALL, os_writev__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005215
5216static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005217os_writev_impl(PyObject *module, int fd, PyObject *buffers);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005218
5219static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02005220os_writev(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005221{
5222 PyObject *return_value = NULL;
5223 int fd;
5224 PyObject *buffers;
5225 Py_ssize_t _return_value;
5226
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02005227 if (!_PyArg_CheckPositional("writev", nargs, 2, 2)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01005228 goto exit;
5229 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02005230 if (PyFloat_Check(args[0])) {
5231 PyErr_SetString(PyExc_TypeError,
5232 "integer argument expected, got float" );
5233 goto exit;
5234 }
5235 fd = _PyLong_AsInt(args[0]);
5236 if (fd == -1 && PyErr_Occurred()) {
5237 goto exit;
5238 }
5239 buffers = args[1];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005240 _return_value = os_writev_impl(module, fd, buffers);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005241 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005242 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005243 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005244 return_value = PyLong_FromSsize_t(_return_value);
5245
5246exit:
5247 return return_value;
5248}
5249
5250#endif /* defined(HAVE_WRITEV) */
5251
5252#if defined(HAVE_PWRITE)
5253
5254PyDoc_STRVAR(os_pwrite__doc__,
5255"pwrite($module, fd, buffer, offset, /)\n"
5256"--\n"
5257"\n"
5258"Write bytes to a file descriptor starting at a particular offset.\n"
5259"\n"
5260"Write buffer to fd, starting at offset bytes from the beginning of\n"
5261"the file. Returns the number of bytes writte. Does not change the\n"
5262"current file offset.");
5263
5264#define OS_PWRITE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02005265 {"pwrite", (PyCFunction)(void(*)(void))os_pwrite, METH_FASTCALL, os_pwrite__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005266
5267static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005268os_pwrite_impl(PyObject *module, int fd, Py_buffer *buffer, Py_off_t offset);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005269
5270static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02005271os_pwrite(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005272{
5273 PyObject *return_value = NULL;
5274 int fd;
5275 Py_buffer buffer = {NULL, NULL};
5276 Py_off_t offset;
5277 Py_ssize_t _return_value;
5278
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02005279 if (!_PyArg_CheckPositional("pwrite", nargs, 3, 3)) {
5280 goto exit;
5281 }
5282 if (PyFloat_Check(args[0])) {
5283 PyErr_SetString(PyExc_TypeError,
5284 "integer argument expected, got float" );
5285 goto exit;
5286 }
5287 fd = _PyLong_AsInt(args[0]);
5288 if (fd == -1 && PyErr_Occurred()) {
5289 goto exit;
5290 }
5291 if (PyObject_GetBuffer(args[1], &buffer, PyBUF_SIMPLE) != 0) {
5292 goto exit;
5293 }
5294 if (!PyBuffer_IsContiguous(&buffer, 'C')) {
5295 _PyArg_BadArgument("pwrite", 2, "contiguous buffer", args[1]);
5296 goto exit;
5297 }
5298 if (!Py_off_t_converter(args[2], &offset)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01005299 goto exit;
5300 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005301 _return_value = os_pwrite_impl(module, fd, &buffer, offset);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005302 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005303 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005304 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005305 return_value = PyLong_FromSsize_t(_return_value);
5306
5307exit:
5308 /* Cleanup for buffer */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005309 if (buffer.obj) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005310 PyBuffer_Release(&buffer);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005311 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005312
5313 return return_value;
5314}
5315
5316#endif /* defined(HAVE_PWRITE) */
5317
Pablo Galindo4defba32018-01-27 16:16:37 +00005318#if (defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2))
5319
5320PyDoc_STRVAR(os_pwritev__doc__,
5321"pwritev($module, fd, buffers, offset, flags=0, /)\n"
5322"--\n"
5323"\n"
5324"Writes the contents of bytes-like objects to a file descriptor at a given offset.\n"
5325"\n"
5326"Combines the functionality of writev() and pwrite(). All buffers must be a sequence\n"
5327"of bytes-like objects. Buffers are processed in array order. Entire contents of first\n"
5328"buffer is written before proceeding to second, and so on. The operating system may\n"
5329"set a limit (sysconf() value SC_IOV_MAX) on the number of buffers that can be used.\n"
5330"This function writes the contents of each object to the file descriptor and returns\n"
5331"the total number of bytes written.\n"
5332"\n"
5333"The flags argument contains a bitwise OR of zero or more of the following flags:\n"
5334"\n"
5335"- RWF_DSYNC\n"
5336"- RWF_SYNC\n"
5337"\n"
5338"Using non-zero flags requires Linux 4.7 or newer.");
5339
5340#define OS_PWRITEV_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02005341 {"pwritev", (PyCFunction)(void(*)(void))os_pwritev, METH_FASTCALL, os_pwritev__doc__},
Pablo Galindo4defba32018-01-27 16:16:37 +00005342
5343static Py_ssize_t
5344os_pwritev_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset,
5345 int flags);
5346
5347static PyObject *
5348os_pwritev(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
5349{
5350 PyObject *return_value = NULL;
5351 int fd;
5352 PyObject *buffers;
5353 Py_off_t offset;
5354 int flags = 0;
5355 Py_ssize_t _return_value;
5356
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02005357 if (!_PyArg_CheckPositional("pwritev", nargs, 3, 4)) {
Pablo Galindo4defba32018-01-27 16:16:37 +00005358 goto exit;
5359 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02005360 if (PyFloat_Check(args[0])) {
5361 PyErr_SetString(PyExc_TypeError,
5362 "integer argument expected, got float" );
5363 goto exit;
5364 }
5365 fd = _PyLong_AsInt(args[0]);
5366 if (fd == -1 && PyErr_Occurred()) {
5367 goto exit;
5368 }
5369 buffers = args[1];
5370 if (!Py_off_t_converter(args[2], &offset)) {
5371 goto exit;
5372 }
5373 if (nargs < 4) {
5374 goto skip_optional;
5375 }
5376 if (PyFloat_Check(args[3])) {
5377 PyErr_SetString(PyExc_TypeError,
5378 "integer argument expected, got float" );
5379 goto exit;
5380 }
5381 flags = _PyLong_AsInt(args[3]);
5382 if (flags == -1 && PyErr_Occurred()) {
5383 goto exit;
5384 }
5385skip_optional:
Pablo Galindo4defba32018-01-27 16:16:37 +00005386 _return_value = os_pwritev_impl(module, fd, buffers, offset, flags);
5387 if ((_return_value == -1) && PyErr_Occurred()) {
5388 goto exit;
5389 }
5390 return_value = PyLong_FromSsize_t(_return_value);
5391
5392exit:
5393 return return_value;
5394}
5395
5396#endif /* (defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2)) */
5397
Pablo Galindoaac4d032019-05-31 19:39:47 +01005398#if defined(HAVE_COPY_FILE_RANGE)
5399
5400PyDoc_STRVAR(os_copy_file_range__doc__,
5401"copy_file_range($module, /, src, dst, count, offset_src=None,\n"
5402" offset_dst=None)\n"
5403"--\n"
5404"\n"
5405"Copy count bytes from one file descriptor to another.\n"
5406"\n"
5407" src\n"
5408" Source file descriptor.\n"
5409" dst\n"
5410" Destination file descriptor.\n"
5411" count\n"
5412" Number of bytes to copy.\n"
5413" offset_src\n"
5414" Starting offset in src.\n"
5415" offset_dst\n"
5416" Starting offset in dst.\n"
5417"\n"
5418"If offset_src is None, then src is read from the current position;\n"
5419"respectively for offset_dst.");
5420
5421#define OS_COPY_FILE_RANGE_METHODDEF \
5422 {"copy_file_range", (PyCFunction)(void(*)(void))os_copy_file_range, METH_FASTCALL|METH_KEYWORDS, os_copy_file_range__doc__},
5423
5424static PyObject *
5425os_copy_file_range_impl(PyObject *module, int src, int dst, Py_ssize_t count,
5426 PyObject *offset_src, PyObject *offset_dst);
5427
5428static PyObject *
5429os_copy_file_range(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
5430{
5431 PyObject *return_value = NULL;
5432 static const char * const _keywords[] = {"src", "dst", "count", "offset_src", "offset_dst", NULL};
5433 static _PyArg_Parser _parser = {NULL, _keywords, "copy_file_range", 0};
5434 PyObject *argsbuf[5];
5435 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 3;
5436 int src;
5437 int dst;
5438 Py_ssize_t count;
5439 PyObject *offset_src = Py_None;
5440 PyObject *offset_dst = Py_None;
5441
5442 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 3, 5, 0, argsbuf);
5443 if (!args) {
5444 goto exit;
5445 }
5446 if (PyFloat_Check(args[0])) {
5447 PyErr_SetString(PyExc_TypeError,
5448 "integer argument expected, got float" );
5449 goto exit;
5450 }
5451 src = _PyLong_AsInt(args[0]);
5452 if (src == -1 && PyErr_Occurred()) {
5453 goto exit;
5454 }
5455 if (PyFloat_Check(args[1])) {
5456 PyErr_SetString(PyExc_TypeError,
5457 "integer argument expected, got float" );
5458 goto exit;
5459 }
5460 dst = _PyLong_AsInt(args[1]);
5461 if (dst == -1 && PyErr_Occurred()) {
5462 goto exit;
5463 }
5464 if (PyFloat_Check(args[2])) {
5465 PyErr_SetString(PyExc_TypeError,
5466 "integer argument expected, got float" );
5467 goto exit;
5468 }
5469 {
5470 Py_ssize_t ival = -1;
5471 PyObject *iobj = PyNumber_Index(args[2]);
5472 if (iobj != NULL) {
5473 ival = PyLong_AsSsize_t(iobj);
5474 Py_DECREF(iobj);
5475 }
5476 if (ival == -1 && PyErr_Occurred()) {
5477 goto exit;
5478 }
5479 count = ival;
5480 }
5481 if (!noptargs) {
5482 goto skip_optional_pos;
5483 }
5484 if (args[3]) {
5485 offset_src = args[3];
5486 if (!--noptargs) {
5487 goto skip_optional_pos;
5488 }
5489 }
5490 offset_dst = args[4];
5491skip_optional_pos:
5492 return_value = os_copy_file_range_impl(module, src, dst, count, offset_src, offset_dst);
5493
5494exit:
5495 return return_value;
5496}
5497
5498#endif /* defined(HAVE_COPY_FILE_RANGE) */
5499
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005500#if defined(HAVE_MKFIFO)
5501
5502PyDoc_STRVAR(os_mkfifo__doc__,
5503"mkfifo($module, /, path, mode=438, *, dir_fd=None)\n"
5504"--\n"
5505"\n"
5506"Create a \"fifo\" (a POSIX named pipe).\n"
5507"\n"
5508"If dir_fd is not None, it should be a file descriptor open to a directory,\n"
5509" and path should be relative; path will then be relative to that directory.\n"
5510"dir_fd may not be implemented on your platform.\n"
5511" If it is unavailable, using it will raise a NotImplementedError.");
5512
5513#define OS_MKFIFO_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02005514 {"mkfifo", (PyCFunction)(void(*)(void))os_mkfifo, METH_FASTCALL|METH_KEYWORDS, os_mkfifo__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005515
5516static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005517os_mkfifo_impl(PyObject *module, path_t *path, int mode, int dir_fd);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005518
5519static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02005520os_mkfifo(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005521{
5522 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03005523 static const char * const _keywords[] = {"path", "mode", "dir_fd", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02005524 static _PyArg_Parser _parser = {NULL, _keywords, "mkfifo", 0};
5525 PyObject *argsbuf[3];
5526 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005527 path_t path = PATH_T_INITIALIZE("mkfifo", "path", 0, 0);
5528 int mode = 438;
5529 int dir_fd = DEFAULT_DIR_FD;
5530
Serhiy Storchaka31913912019-03-14 10:32:22 +02005531 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
5532 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005533 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005534 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02005535 if (!path_converter(args[0], &path)) {
5536 goto exit;
5537 }
5538 if (!noptargs) {
5539 goto skip_optional_pos;
5540 }
5541 if (args[1]) {
5542 if (PyFloat_Check(args[1])) {
5543 PyErr_SetString(PyExc_TypeError,
5544 "integer argument expected, got float" );
5545 goto exit;
5546 }
5547 mode = _PyLong_AsInt(args[1]);
5548 if (mode == -1 && PyErr_Occurred()) {
5549 goto exit;
5550 }
5551 if (!--noptargs) {
5552 goto skip_optional_pos;
5553 }
5554 }
5555skip_optional_pos:
5556 if (!noptargs) {
5557 goto skip_optional_kwonly;
5558 }
5559 if (!MKFIFOAT_DIR_FD_CONVERTER(args[2], &dir_fd)) {
5560 goto exit;
5561 }
5562skip_optional_kwonly:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005563 return_value = os_mkfifo_impl(module, &path, mode, dir_fd);
5564
5565exit:
5566 /* Cleanup for path */
5567 path_cleanup(&path);
5568
5569 return return_value;
5570}
5571
5572#endif /* defined(HAVE_MKFIFO) */
5573
5574#if (defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV))
5575
5576PyDoc_STRVAR(os_mknod__doc__,
5577"mknod($module, /, path, mode=384, device=0, *, dir_fd=None)\n"
5578"--\n"
5579"\n"
5580"Create a node in the file system.\n"
5581"\n"
5582"Create a node in the file system (file, device special file or named pipe)\n"
5583"at path. mode specifies both the permissions to use and the\n"
5584"type of node to be created, being combined (bitwise OR) with one of\n"
5585"S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. If S_IFCHR or S_IFBLK is set on mode,\n"
5586"device defines the newly created device special file (probably using\n"
5587"os.makedev()). Otherwise device is ignored.\n"
5588"\n"
5589"If dir_fd is not None, it should be a file descriptor open to a directory,\n"
5590" and path should be relative; path will then be relative to that directory.\n"
5591"dir_fd may not be implemented on your platform.\n"
5592" If it is unavailable, using it will raise a NotImplementedError.");
5593
5594#define OS_MKNOD_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02005595 {"mknod", (PyCFunction)(void(*)(void))os_mknod, METH_FASTCALL|METH_KEYWORDS, os_mknod__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005596
5597static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005598os_mknod_impl(PyObject *module, path_t *path, int mode, dev_t device,
Larry Hastings89964c42015-04-14 18:07:59 -04005599 int dir_fd);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005600
5601static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02005602os_mknod(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005603{
5604 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03005605 static const char * const _keywords[] = {"path", "mode", "device", "dir_fd", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02005606 static _PyArg_Parser _parser = {NULL, _keywords, "mknod", 0};
5607 PyObject *argsbuf[4];
5608 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005609 path_t path = PATH_T_INITIALIZE("mknod", "path", 0, 0);
5610 int mode = 384;
5611 dev_t device = 0;
5612 int dir_fd = DEFAULT_DIR_FD;
5613
Serhiy Storchaka31913912019-03-14 10:32:22 +02005614 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 3, 0, argsbuf);
5615 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005616 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005617 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02005618 if (!path_converter(args[0], &path)) {
5619 goto exit;
5620 }
5621 if (!noptargs) {
5622 goto skip_optional_pos;
5623 }
5624 if (args[1]) {
5625 if (PyFloat_Check(args[1])) {
5626 PyErr_SetString(PyExc_TypeError,
5627 "integer argument expected, got float" );
5628 goto exit;
5629 }
5630 mode = _PyLong_AsInt(args[1]);
5631 if (mode == -1 && PyErr_Occurred()) {
5632 goto exit;
5633 }
5634 if (!--noptargs) {
5635 goto skip_optional_pos;
5636 }
5637 }
5638 if (args[2]) {
5639 if (!_Py_Dev_Converter(args[2], &device)) {
5640 goto exit;
5641 }
5642 if (!--noptargs) {
5643 goto skip_optional_pos;
5644 }
5645 }
5646skip_optional_pos:
5647 if (!noptargs) {
5648 goto skip_optional_kwonly;
5649 }
5650 if (!MKNODAT_DIR_FD_CONVERTER(args[3], &dir_fd)) {
5651 goto exit;
5652 }
5653skip_optional_kwonly:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005654 return_value = os_mknod_impl(module, &path, mode, device, dir_fd);
5655
5656exit:
5657 /* Cleanup for path */
5658 path_cleanup(&path);
5659
5660 return return_value;
5661}
5662
5663#endif /* (defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)) */
5664
5665#if defined(HAVE_DEVICE_MACROS)
5666
5667PyDoc_STRVAR(os_major__doc__,
5668"major($module, device, /)\n"
5669"--\n"
5670"\n"
5671"Extracts a device major number from a raw device number.");
5672
5673#define OS_MAJOR_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03005674 {"major", (PyCFunction)os_major, METH_O, os_major__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005675
5676static unsigned int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005677os_major_impl(PyObject *module, dev_t device);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005678
5679static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005680os_major(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005681{
5682 PyObject *return_value = NULL;
5683 dev_t device;
5684 unsigned int _return_value;
5685
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02005686 if (!_Py_Dev_Converter(arg, &device)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005687 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005688 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005689 _return_value = os_major_impl(module, device);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005690 if ((_return_value == (unsigned int)-1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005691 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005692 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005693 return_value = PyLong_FromUnsignedLong((unsigned long)_return_value);
5694
5695exit:
5696 return return_value;
5697}
5698
5699#endif /* defined(HAVE_DEVICE_MACROS) */
5700
5701#if defined(HAVE_DEVICE_MACROS)
5702
5703PyDoc_STRVAR(os_minor__doc__,
5704"minor($module, device, /)\n"
5705"--\n"
5706"\n"
5707"Extracts a device minor number from a raw device number.");
5708
5709#define OS_MINOR_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03005710 {"minor", (PyCFunction)os_minor, METH_O, os_minor__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005711
5712static unsigned int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005713os_minor_impl(PyObject *module, dev_t device);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005714
5715static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005716os_minor(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005717{
5718 PyObject *return_value = NULL;
5719 dev_t device;
5720 unsigned int _return_value;
5721
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02005722 if (!_Py_Dev_Converter(arg, &device)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005723 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005724 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005725 _return_value = os_minor_impl(module, device);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005726 if ((_return_value == (unsigned int)-1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005727 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005728 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005729 return_value = PyLong_FromUnsignedLong((unsigned long)_return_value);
5730
5731exit:
5732 return return_value;
5733}
5734
5735#endif /* defined(HAVE_DEVICE_MACROS) */
5736
5737#if defined(HAVE_DEVICE_MACROS)
5738
5739PyDoc_STRVAR(os_makedev__doc__,
5740"makedev($module, major, minor, /)\n"
5741"--\n"
5742"\n"
5743"Composes a raw device number from the major and minor device numbers.");
5744
5745#define OS_MAKEDEV_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02005746 {"makedev", (PyCFunction)(void(*)(void))os_makedev, METH_FASTCALL, os_makedev__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005747
5748static dev_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005749os_makedev_impl(PyObject *module, int major, int minor);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005750
5751static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02005752os_makedev(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005753{
5754 PyObject *return_value = NULL;
5755 int major;
5756 int minor;
5757 dev_t _return_value;
5758
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02005759 if (!_PyArg_CheckPositional("makedev", nargs, 2, 2)) {
5760 goto exit;
5761 }
5762 if (PyFloat_Check(args[0])) {
5763 PyErr_SetString(PyExc_TypeError,
5764 "integer argument expected, got float" );
5765 goto exit;
5766 }
5767 major = _PyLong_AsInt(args[0]);
5768 if (major == -1 && PyErr_Occurred()) {
5769 goto exit;
5770 }
5771 if (PyFloat_Check(args[1])) {
5772 PyErr_SetString(PyExc_TypeError,
5773 "integer argument expected, got float" );
5774 goto exit;
5775 }
5776 minor = _PyLong_AsInt(args[1]);
5777 if (minor == -1 && PyErr_Occurred()) {
Victor Stinner259f0e42017-01-17 01:35:17 +01005778 goto exit;
5779 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005780 _return_value = os_makedev_impl(module, major, minor);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005781 if ((_return_value == (dev_t)-1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005782 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005783 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005784 return_value = _PyLong_FromDev(_return_value);
5785
5786exit:
5787 return return_value;
5788}
5789
5790#endif /* defined(HAVE_DEVICE_MACROS) */
5791
Steve Dowerf7377032015-04-12 15:44:54 -04005792#if (defined HAVE_FTRUNCATE || defined MS_WINDOWS)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005793
5794PyDoc_STRVAR(os_ftruncate__doc__,
5795"ftruncate($module, fd, length, /)\n"
5796"--\n"
5797"\n"
5798"Truncate a file, specified by file descriptor, to a specific length.");
5799
5800#define OS_FTRUNCATE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02005801 {"ftruncate", (PyCFunction)(void(*)(void))os_ftruncate, METH_FASTCALL, os_ftruncate__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005802
5803static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005804os_ftruncate_impl(PyObject *module, int fd, Py_off_t length);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005805
5806static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02005807os_ftruncate(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005808{
5809 PyObject *return_value = NULL;
5810 int fd;
5811 Py_off_t length;
5812
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02005813 if (!_PyArg_CheckPositional("ftruncate", nargs, 2, 2)) {
5814 goto exit;
5815 }
5816 if (PyFloat_Check(args[0])) {
5817 PyErr_SetString(PyExc_TypeError,
5818 "integer argument expected, got float" );
5819 goto exit;
5820 }
5821 fd = _PyLong_AsInt(args[0]);
5822 if (fd == -1 && PyErr_Occurred()) {
5823 goto exit;
5824 }
5825 if (!Py_off_t_converter(args[1], &length)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01005826 goto exit;
5827 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005828 return_value = os_ftruncate_impl(module, fd, length);
5829
5830exit:
5831 return return_value;
5832}
5833
Steve Dowerf7377032015-04-12 15:44:54 -04005834#endif /* (defined HAVE_FTRUNCATE || defined MS_WINDOWS) */
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005835
Steve Dowerf7377032015-04-12 15:44:54 -04005836#if (defined HAVE_TRUNCATE || defined MS_WINDOWS)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005837
5838PyDoc_STRVAR(os_truncate__doc__,
5839"truncate($module, /, path, length)\n"
5840"--\n"
5841"\n"
5842"Truncate a file, specified by path, to a specific length.\n"
5843"\n"
5844"On some platforms, path may also be specified as an open file descriptor.\n"
5845" If this functionality is unavailable, using it raises an exception.");
5846
5847#define OS_TRUNCATE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02005848 {"truncate", (PyCFunction)(void(*)(void))os_truncate, METH_FASTCALL|METH_KEYWORDS, os_truncate__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005849
5850static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005851os_truncate_impl(PyObject *module, path_t *path, Py_off_t length);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005852
5853static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02005854os_truncate(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005855{
5856 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03005857 static const char * const _keywords[] = {"path", "length", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02005858 static _PyArg_Parser _parser = {NULL, _keywords, "truncate", 0};
5859 PyObject *argsbuf[2];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005860 path_t path = PATH_T_INITIALIZE("truncate", "path", 0, PATH_HAVE_FTRUNCATE);
5861 Py_off_t length;
5862
Serhiy Storchaka31913912019-03-14 10:32:22 +02005863 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf);
5864 if (!args) {
5865 goto exit;
5866 }
5867 if (!path_converter(args[0], &path)) {
5868 goto exit;
5869 }
5870 if (!Py_off_t_converter(args[1], &length)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005871 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005872 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005873 return_value = os_truncate_impl(module, &path, length);
5874
5875exit:
5876 /* Cleanup for path */
5877 path_cleanup(&path);
5878
5879 return return_value;
5880}
5881
Steve Dowerf7377032015-04-12 15:44:54 -04005882#endif /* (defined HAVE_TRUNCATE || defined MS_WINDOWS) */
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005883
5884#if (defined(HAVE_POSIX_FALLOCATE) && !defined(POSIX_FADVISE_AIX_BUG))
5885
5886PyDoc_STRVAR(os_posix_fallocate__doc__,
5887"posix_fallocate($module, fd, offset, length, /)\n"
5888"--\n"
5889"\n"
5890"Ensure a file has allocated at least a particular number of bytes on disk.\n"
5891"\n"
5892"Ensure that the file specified by fd encompasses a range of bytes\n"
5893"starting at offset bytes from the beginning and continuing for length bytes.");
5894
5895#define OS_POSIX_FALLOCATE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02005896 {"posix_fallocate", (PyCFunction)(void(*)(void))os_posix_fallocate, METH_FASTCALL, os_posix_fallocate__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005897
5898static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005899os_posix_fallocate_impl(PyObject *module, int fd, Py_off_t offset,
Larry Hastings89964c42015-04-14 18:07:59 -04005900 Py_off_t length);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005901
5902static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02005903os_posix_fallocate(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005904{
5905 PyObject *return_value = NULL;
5906 int fd;
5907 Py_off_t offset;
5908 Py_off_t length;
5909
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02005910 if (!_PyArg_CheckPositional("posix_fallocate", nargs, 3, 3)) {
5911 goto exit;
5912 }
5913 if (PyFloat_Check(args[0])) {
5914 PyErr_SetString(PyExc_TypeError,
5915 "integer argument expected, got float" );
5916 goto exit;
5917 }
5918 fd = _PyLong_AsInt(args[0]);
5919 if (fd == -1 && PyErr_Occurred()) {
5920 goto exit;
5921 }
5922 if (!Py_off_t_converter(args[1], &offset)) {
5923 goto exit;
5924 }
5925 if (!Py_off_t_converter(args[2], &length)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01005926 goto exit;
5927 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005928 return_value = os_posix_fallocate_impl(module, fd, offset, length);
5929
5930exit:
5931 return return_value;
5932}
5933
5934#endif /* (defined(HAVE_POSIX_FALLOCATE) && !defined(POSIX_FADVISE_AIX_BUG)) */
5935
5936#if (defined(HAVE_POSIX_FADVISE) && !defined(POSIX_FADVISE_AIX_BUG))
5937
5938PyDoc_STRVAR(os_posix_fadvise__doc__,
5939"posix_fadvise($module, fd, offset, length, advice, /)\n"
5940"--\n"
5941"\n"
5942"Announce an intention to access data in a specific pattern.\n"
5943"\n"
5944"Announce an intention to access data in a specific pattern, thus allowing\n"
5945"the kernel to make optimizations.\n"
5946"The advice applies to the region of the file specified by fd starting at\n"
5947"offset and continuing for length bytes.\n"
5948"advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,\n"
5949"POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED, or\n"
5950"POSIX_FADV_DONTNEED.");
5951
5952#define OS_POSIX_FADVISE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02005953 {"posix_fadvise", (PyCFunction)(void(*)(void))os_posix_fadvise, METH_FASTCALL, os_posix_fadvise__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005954
5955static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005956os_posix_fadvise_impl(PyObject *module, int fd, Py_off_t offset,
Larry Hastings89964c42015-04-14 18:07:59 -04005957 Py_off_t length, int advice);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005958
5959static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02005960os_posix_fadvise(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005961{
5962 PyObject *return_value = NULL;
5963 int fd;
5964 Py_off_t offset;
5965 Py_off_t length;
5966 int advice;
5967
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02005968 if (!_PyArg_CheckPositional("posix_fadvise", nargs, 4, 4)) {
5969 goto exit;
5970 }
5971 if (PyFloat_Check(args[0])) {
5972 PyErr_SetString(PyExc_TypeError,
5973 "integer argument expected, got float" );
5974 goto exit;
5975 }
5976 fd = _PyLong_AsInt(args[0]);
5977 if (fd == -1 && PyErr_Occurred()) {
5978 goto exit;
5979 }
5980 if (!Py_off_t_converter(args[1], &offset)) {
5981 goto exit;
5982 }
5983 if (!Py_off_t_converter(args[2], &length)) {
5984 goto exit;
5985 }
5986 if (PyFloat_Check(args[3])) {
5987 PyErr_SetString(PyExc_TypeError,
5988 "integer argument expected, got float" );
5989 goto exit;
5990 }
5991 advice = _PyLong_AsInt(args[3]);
5992 if (advice == -1 && PyErr_Occurred()) {
Victor Stinner259f0e42017-01-17 01:35:17 +01005993 goto exit;
5994 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005995 return_value = os_posix_fadvise_impl(module, fd, offset, length, advice);
5996
5997exit:
5998 return return_value;
5999}
6000
6001#endif /* (defined(HAVE_POSIX_FADVISE) && !defined(POSIX_FADVISE_AIX_BUG)) */
6002
6003#if defined(HAVE_PUTENV) && defined(MS_WINDOWS)
6004
6005PyDoc_STRVAR(os_putenv__doc__,
6006"putenv($module, name, value, /)\n"
6007"--\n"
6008"\n"
6009"Change or add an environment variable.");
6010
6011#define OS_PUTENV_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02006012 {"putenv", (PyCFunction)(void(*)(void))os_putenv, METH_FASTCALL, os_putenv__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006013
6014static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006015os_putenv_impl(PyObject *module, PyObject *name, PyObject *value);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006016
6017static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02006018os_putenv(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006019{
6020 PyObject *return_value = NULL;
6021 PyObject *name;
6022 PyObject *value;
6023
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02006024 if (!_PyArg_CheckPositional("putenv", nargs, 2, 2)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01006025 goto exit;
6026 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02006027 if (!PyUnicode_Check(args[0])) {
6028 _PyArg_BadArgument("putenv", 1, "str", args[0]);
6029 goto exit;
6030 }
6031 if (PyUnicode_READY(args[0]) == -1) {
6032 goto exit;
6033 }
6034 name = args[0];
6035 if (!PyUnicode_Check(args[1])) {
6036 _PyArg_BadArgument("putenv", 2, "str", args[1]);
6037 goto exit;
6038 }
6039 if (PyUnicode_READY(args[1]) == -1) {
6040 goto exit;
6041 }
6042 value = args[1];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006043 return_value = os_putenv_impl(module, name, value);
6044
6045exit:
6046 return return_value;
6047}
6048
6049#endif /* defined(HAVE_PUTENV) && defined(MS_WINDOWS) */
6050
6051#if defined(HAVE_PUTENV) && !defined(MS_WINDOWS)
6052
6053PyDoc_STRVAR(os_putenv__doc__,
6054"putenv($module, name, value, /)\n"
6055"--\n"
6056"\n"
6057"Change or add an environment variable.");
6058
6059#define OS_PUTENV_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02006060 {"putenv", (PyCFunction)(void(*)(void))os_putenv, METH_FASTCALL, os_putenv__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006061
6062static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006063os_putenv_impl(PyObject *module, PyObject *name, PyObject *value);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006064
6065static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02006066os_putenv(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006067{
6068 PyObject *return_value = NULL;
6069 PyObject *name = NULL;
6070 PyObject *value = NULL;
6071
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02006072 if (!_PyArg_CheckPositional("putenv", nargs, 2, 2)) {
6073 goto exit;
6074 }
6075 if (!PyUnicode_FSConverter(args[0], &name)) {
6076 goto exit;
6077 }
6078 if (!PyUnicode_FSConverter(args[1], &value)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01006079 goto exit;
6080 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006081 return_value = os_putenv_impl(module, name, value);
6082
6083exit:
6084 /* Cleanup for name */
6085 Py_XDECREF(name);
6086 /* Cleanup for value */
6087 Py_XDECREF(value);
6088
6089 return return_value;
6090}
6091
6092#endif /* defined(HAVE_PUTENV) && !defined(MS_WINDOWS) */
6093
6094#if defined(HAVE_UNSETENV)
6095
6096PyDoc_STRVAR(os_unsetenv__doc__,
6097"unsetenv($module, name, /)\n"
6098"--\n"
6099"\n"
6100"Delete an environment variable.");
6101
6102#define OS_UNSETENV_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03006103 {"unsetenv", (PyCFunction)os_unsetenv, METH_O, os_unsetenv__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006104
6105static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006106os_unsetenv_impl(PyObject *module, PyObject *name);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006107
6108static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006109os_unsetenv(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006110{
6111 PyObject *return_value = NULL;
6112 PyObject *name = NULL;
6113
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02006114 if (!PyUnicode_FSConverter(arg, &name)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006115 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006116 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006117 return_value = os_unsetenv_impl(module, name);
6118
6119exit:
6120 /* Cleanup for name */
6121 Py_XDECREF(name);
6122
6123 return return_value;
6124}
6125
6126#endif /* defined(HAVE_UNSETENV) */
6127
6128PyDoc_STRVAR(os_strerror__doc__,
6129"strerror($module, code, /)\n"
6130"--\n"
6131"\n"
6132"Translate an error code to a message string.");
6133
6134#define OS_STRERROR_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03006135 {"strerror", (PyCFunction)os_strerror, METH_O, os_strerror__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006136
6137static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006138os_strerror_impl(PyObject *module, int code);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006139
6140static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006141os_strerror(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006142{
6143 PyObject *return_value = NULL;
6144 int code;
6145
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02006146 if (PyFloat_Check(arg)) {
6147 PyErr_SetString(PyExc_TypeError,
6148 "integer argument expected, got float" );
6149 goto exit;
6150 }
6151 code = _PyLong_AsInt(arg);
6152 if (code == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006153 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006154 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006155 return_value = os_strerror_impl(module, code);
6156
6157exit:
6158 return return_value;
6159}
6160
6161#if defined(HAVE_SYS_WAIT_H) && defined(WCOREDUMP)
6162
6163PyDoc_STRVAR(os_WCOREDUMP__doc__,
6164"WCOREDUMP($module, status, /)\n"
6165"--\n"
6166"\n"
6167"Return True if the process returning status was dumped to a core file.");
6168
6169#define OS_WCOREDUMP_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03006170 {"WCOREDUMP", (PyCFunction)os_WCOREDUMP, METH_O, os_WCOREDUMP__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006171
6172static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006173os_WCOREDUMP_impl(PyObject *module, int status);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006174
6175static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006176os_WCOREDUMP(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006177{
6178 PyObject *return_value = NULL;
6179 int status;
6180 int _return_value;
6181
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02006182 if (PyFloat_Check(arg)) {
6183 PyErr_SetString(PyExc_TypeError,
6184 "integer argument expected, got float" );
6185 goto exit;
6186 }
6187 status = _PyLong_AsInt(arg);
6188 if (status == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006189 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006190 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006191 _return_value = os_WCOREDUMP_impl(module, status);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006192 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006193 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006194 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006195 return_value = PyBool_FromLong((long)_return_value);
6196
6197exit:
6198 return return_value;
6199}
6200
6201#endif /* defined(HAVE_SYS_WAIT_H) && defined(WCOREDUMP) */
6202
6203#if defined(HAVE_SYS_WAIT_H) && defined(WIFCONTINUED)
6204
6205PyDoc_STRVAR(os_WIFCONTINUED__doc__,
6206"WIFCONTINUED($module, /, status)\n"
6207"--\n"
6208"\n"
6209"Return True if a particular process was continued from a job control stop.\n"
6210"\n"
6211"Return True if the process returning status was continued from a\n"
6212"job control stop.");
6213
6214#define OS_WIFCONTINUED_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02006215 {"WIFCONTINUED", (PyCFunction)(void(*)(void))os_WIFCONTINUED, METH_FASTCALL|METH_KEYWORDS, os_WIFCONTINUED__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006216
6217static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006218os_WIFCONTINUED_impl(PyObject *module, int status);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006219
6220static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02006221os_WIFCONTINUED(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006222{
6223 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03006224 static const char * const _keywords[] = {"status", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02006225 static _PyArg_Parser _parser = {NULL, _keywords, "WIFCONTINUED", 0};
6226 PyObject *argsbuf[1];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006227 int status;
6228 int _return_value;
6229
Serhiy Storchaka31913912019-03-14 10:32:22 +02006230 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
6231 if (!args) {
6232 goto exit;
6233 }
6234 if (PyFloat_Check(args[0])) {
6235 PyErr_SetString(PyExc_TypeError,
6236 "integer argument expected, got float" );
6237 goto exit;
6238 }
6239 status = _PyLong_AsInt(args[0]);
6240 if (status == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006241 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006242 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006243 _return_value = os_WIFCONTINUED_impl(module, status);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006244 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006245 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006246 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006247 return_value = PyBool_FromLong((long)_return_value);
6248
6249exit:
6250 return return_value;
6251}
6252
6253#endif /* defined(HAVE_SYS_WAIT_H) && defined(WIFCONTINUED) */
6254
6255#if defined(HAVE_SYS_WAIT_H) && defined(WIFSTOPPED)
6256
6257PyDoc_STRVAR(os_WIFSTOPPED__doc__,
6258"WIFSTOPPED($module, /, status)\n"
6259"--\n"
6260"\n"
6261"Return True if the process returning status was stopped.");
6262
6263#define OS_WIFSTOPPED_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02006264 {"WIFSTOPPED", (PyCFunction)(void(*)(void))os_WIFSTOPPED, METH_FASTCALL|METH_KEYWORDS, os_WIFSTOPPED__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006265
6266static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006267os_WIFSTOPPED_impl(PyObject *module, int status);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006268
6269static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02006270os_WIFSTOPPED(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006271{
6272 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03006273 static const char * const _keywords[] = {"status", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02006274 static _PyArg_Parser _parser = {NULL, _keywords, "WIFSTOPPED", 0};
6275 PyObject *argsbuf[1];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006276 int status;
6277 int _return_value;
6278
Serhiy Storchaka31913912019-03-14 10:32:22 +02006279 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
6280 if (!args) {
6281 goto exit;
6282 }
6283 if (PyFloat_Check(args[0])) {
6284 PyErr_SetString(PyExc_TypeError,
6285 "integer argument expected, got float" );
6286 goto exit;
6287 }
6288 status = _PyLong_AsInt(args[0]);
6289 if (status == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006290 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006291 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006292 _return_value = os_WIFSTOPPED_impl(module, status);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006293 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006294 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006295 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006296 return_value = PyBool_FromLong((long)_return_value);
6297
6298exit:
6299 return return_value;
6300}
6301
6302#endif /* defined(HAVE_SYS_WAIT_H) && defined(WIFSTOPPED) */
6303
6304#if defined(HAVE_SYS_WAIT_H) && defined(WIFSIGNALED)
6305
6306PyDoc_STRVAR(os_WIFSIGNALED__doc__,
6307"WIFSIGNALED($module, /, status)\n"
6308"--\n"
6309"\n"
6310"Return True if the process returning status was terminated by a signal.");
6311
6312#define OS_WIFSIGNALED_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02006313 {"WIFSIGNALED", (PyCFunction)(void(*)(void))os_WIFSIGNALED, METH_FASTCALL|METH_KEYWORDS, os_WIFSIGNALED__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006314
6315static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006316os_WIFSIGNALED_impl(PyObject *module, int status);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006317
6318static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02006319os_WIFSIGNALED(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006320{
6321 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03006322 static const char * const _keywords[] = {"status", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02006323 static _PyArg_Parser _parser = {NULL, _keywords, "WIFSIGNALED", 0};
6324 PyObject *argsbuf[1];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006325 int status;
6326 int _return_value;
6327
Serhiy Storchaka31913912019-03-14 10:32:22 +02006328 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
6329 if (!args) {
6330 goto exit;
6331 }
6332 if (PyFloat_Check(args[0])) {
6333 PyErr_SetString(PyExc_TypeError,
6334 "integer argument expected, got float" );
6335 goto exit;
6336 }
6337 status = _PyLong_AsInt(args[0]);
6338 if (status == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006339 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006340 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006341 _return_value = os_WIFSIGNALED_impl(module, status);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006342 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006343 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006344 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006345 return_value = PyBool_FromLong((long)_return_value);
6346
6347exit:
6348 return return_value;
6349}
6350
6351#endif /* defined(HAVE_SYS_WAIT_H) && defined(WIFSIGNALED) */
6352
6353#if defined(HAVE_SYS_WAIT_H) && defined(WIFEXITED)
6354
6355PyDoc_STRVAR(os_WIFEXITED__doc__,
6356"WIFEXITED($module, /, status)\n"
6357"--\n"
6358"\n"
6359"Return True if the process returning status exited via the exit() system call.");
6360
6361#define OS_WIFEXITED_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02006362 {"WIFEXITED", (PyCFunction)(void(*)(void))os_WIFEXITED, METH_FASTCALL|METH_KEYWORDS, os_WIFEXITED__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006363
6364static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006365os_WIFEXITED_impl(PyObject *module, int status);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006366
6367static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02006368os_WIFEXITED(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006369{
6370 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03006371 static const char * const _keywords[] = {"status", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02006372 static _PyArg_Parser _parser = {NULL, _keywords, "WIFEXITED", 0};
6373 PyObject *argsbuf[1];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006374 int status;
6375 int _return_value;
6376
Serhiy Storchaka31913912019-03-14 10:32:22 +02006377 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
6378 if (!args) {
6379 goto exit;
6380 }
6381 if (PyFloat_Check(args[0])) {
6382 PyErr_SetString(PyExc_TypeError,
6383 "integer argument expected, got float" );
6384 goto exit;
6385 }
6386 status = _PyLong_AsInt(args[0]);
6387 if (status == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006388 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006389 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006390 _return_value = os_WIFEXITED_impl(module, status);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006391 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006392 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006393 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006394 return_value = PyBool_FromLong((long)_return_value);
6395
6396exit:
6397 return return_value;
6398}
6399
6400#endif /* defined(HAVE_SYS_WAIT_H) && defined(WIFEXITED) */
6401
6402#if defined(HAVE_SYS_WAIT_H) && defined(WEXITSTATUS)
6403
6404PyDoc_STRVAR(os_WEXITSTATUS__doc__,
6405"WEXITSTATUS($module, /, status)\n"
6406"--\n"
6407"\n"
6408"Return the process return code from status.");
6409
6410#define OS_WEXITSTATUS_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02006411 {"WEXITSTATUS", (PyCFunction)(void(*)(void))os_WEXITSTATUS, METH_FASTCALL|METH_KEYWORDS, os_WEXITSTATUS__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006412
6413static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006414os_WEXITSTATUS_impl(PyObject *module, int status);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006415
6416static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02006417os_WEXITSTATUS(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006418{
6419 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03006420 static const char * const _keywords[] = {"status", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02006421 static _PyArg_Parser _parser = {NULL, _keywords, "WEXITSTATUS", 0};
6422 PyObject *argsbuf[1];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006423 int status;
6424 int _return_value;
6425
Serhiy Storchaka31913912019-03-14 10:32:22 +02006426 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
6427 if (!args) {
6428 goto exit;
6429 }
6430 if (PyFloat_Check(args[0])) {
6431 PyErr_SetString(PyExc_TypeError,
6432 "integer argument expected, got float" );
6433 goto exit;
6434 }
6435 status = _PyLong_AsInt(args[0]);
6436 if (status == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006437 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006438 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006439 _return_value = os_WEXITSTATUS_impl(module, status);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006440 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006441 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006442 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006443 return_value = PyLong_FromLong((long)_return_value);
6444
6445exit:
6446 return return_value;
6447}
6448
6449#endif /* defined(HAVE_SYS_WAIT_H) && defined(WEXITSTATUS) */
6450
6451#if defined(HAVE_SYS_WAIT_H) && defined(WTERMSIG)
6452
6453PyDoc_STRVAR(os_WTERMSIG__doc__,
6454"WTERMSIG($module, /, status)\n"
6455"--\n"
6456"\n"
6457"Return the signal that terminated the process that provided the status value.");
6458
6459#define OS_WTERMSIG_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02006460 {"WTERMSIG", (PyCFunction)(void(*)(void))os_WTERMSIG, METH_FASTCALL|METH_KEYWORDS, os_WTERMSIG__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006461
6462static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006463os_WTERMSIG_impl(PyObject *module, int status);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006464
6465static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02006466os_WTERMSIG(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006467{
6468 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03006469 static const char * const _keywords[] = {"status", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02006470 static _PyArg_Parser _parser = {NULL, _keywords, "WTERMSIG", 0};
6471 PyObject *argsbuf[1];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006472 int status;
6473 int _return_value;
6474
Serhiy Storchaka31913912019-03-14 10:32:22 +02006475 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
6476 if (!args) {
6477 goto exit;
6478 }
6479 if (PyFloat_Check(args[0])) {
6480 PyErr_SetString(PyExc_TypeError,
6481 "integer argument expected, got float" );
6482 goto exit;
6483 }
6484 status = _PyLong_AsInt(args[0]);
6485 if (status == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006486 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006487 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006488 _return_value = os_WTERMSIG_impl(module, status);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006489 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006490 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006491 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006492 return_value = PyLong_FromLong((long)_return_value);
6493
6494exit:
6495 return return_value;
6496}
6497
6498#endif /* defined(HAVE_SYS_WAIT_H) && defined(WTERMSIG) */
6499
6500#if defined(HAVE_SYS_WAIT_H) && defined(WSTOPSIG)
6501
6502PyDoc_STRVAR(os_WSTOPSIG__doc__,
6503"WSTOPSIG($module, /, status)\n"
6504"--\n"
6505"\n"
6506"Return the signal that stopped the process that provided the status value.");
6507
6508#define OS_WSTOPSIG_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02006509 {"WSTOPSIG", (PyCFunction)(void(*)(void))os_WSTOPSIG, METH_FASTCALL|METH_KEYWORDS, os_WSTOPSIG__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006510
6511static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006512os_WSTOPSIG_impl(PyObject *module, int status);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006513
6514static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02006515os_WSTOPSIG(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006516{
6517 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03006518 static const char * const _keywords[] = {"status", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02006519 static _PyArg_Parser _parser = {NULL, _keywords, "WSTOPSIG", 0};
6520 PyObject *argsbuf[1];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006521 int status;
6522 int _return_value;
6523
Serhiy Storchaka31913912019-03-14 10:32:22 +02006524 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
6525 if (!args) {
6526 goto exit;
6527 }
6528 if (PyFloat_Check(args[0])) {
6529 PyErr_SetString(PyExc_TypeError,
6530 "integer argument expected, got float" );
6531 goto exit;
6532 }
6533 status = _PyLong_AsInt(args[0]);
6534 if (status == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006535 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006536 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006537 _return_value = os_WSTOPSIG_impl(module, status);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006538 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006539 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006540 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006541 return_value = PyLong_FromLong((long)_return_value);
6542
6543exit:
6544 return return_value;
6545}
6546
6547#endif /* defined(HAVE_SYS_WAIT_H) && defined(WSTOPSIG) */
6548
6549#if (defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H))
6550
6551PyDoc_STRVAR(os_fstatvfs__doc__,
6552"fstatvfs($module, fd, /)\n"
6553"--\n"
6554"\n"
6555"Perform an fstatvfs system call on the given fd.\n"
6556"\n"
6557"Equivalent to statvfs(fd).");
6558
6559#define OS_FSTATVFS_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03006560 {"fstatvfs", (PyCFunction)os_fstatvfs, METH_O, os_fstatvfs__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006561
6562static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006563os_fstatvfs_impl(PyObject *module, int fd);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006564
6565static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006566os_fstatvfs(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006567{
6568 PyObject *return_value = NULL;
6569 int fd;
6570
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02006571 if (PyFloat_Check(arg)) {
6572 PyErr_SetString(PyExc_TypeError,
6573 "integer argument expected, got float" );
6574 goto exit;
6575 }
6576 fd = _PyLong_AsInt(arg);
6577 if (fd == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006578 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006579 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006580 return_value = os_fstatvfs_impl(module, fd);
6581
6582exit:
6583 return return_value;
6584}
6585
6586#endif /* (defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)) */
6587
6588#if (defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H))
6589
6590PyDoc_STRVAR(os_statvfs__doc__,
6591"statvfs($module, /, path)\n"
6592"--\n"
6593"\n"
6594"Perform a statvfs system call on the given path.\n"
6595"\n"
6596"path may always be specified as a string.\n"
6597"On some platforms, path may also be specified as an open file descriptor.\n"
6598" If this functionality is unavailable, using it raises an exception.");
6599
6600#define OS_STATVFS_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02006601 {"statvfs", (PyCFunction)(void(*)(void))os_statvfs, METH_FASTCALL|METH_KEYWORDS, os_statvfs__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006602
6603static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006604os_statvfs_impl(PyObject *module, path_t *path);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006605
6606static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02006607os_statvfs(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006608{
6609 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03006610 static const char * const _keywords[] = {"path", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02006611 static _PyArg_Parser _parser = {NULL, _keywords, "statvfs", 0};
6612 PyObject *argsbuf[1];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006613 path_t path = PATH_T_INITIALIZE("statvfs", "path", 0, PATH_HAVE_FSTATVFS);
6614
Serhiy Storchaka31913912019-03-14 10:32:22 +02006615 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
6616 if (!args) {
6617 goto exit;
6618 }
6619 if (!path_converter(args[0], &path)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006620 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006621 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006622 return_value = os_statvfs_impl(module, &path);
6623
6624exit:
6625 /* Cleanup for path */
6626 path_cleanup(&path);
6627
6628 return return_value;
6629}
6630
6631#endif /* (defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)) */
6632
6633#if defined(MS_WINDOWS)
6634
6635PyDoc_STRVAR(os__getdiskusage__doc__,
6636"_getdiskusage($module, /, path)\n"
6637"--\n"
6638"\n"
6639"Return disk usage statistics about the given path as a (total, free) tuple.");
6640
6641#define OS__GETDISKUSAGE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02006642 {"_getdiskusage", (PyCFunction)(void(*)(void))os__getdiskusage, METH_FASTCALL|METH_KEYWORDS, os__getdiskusage__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006643
6644static PyObject *
Steve Dower23ad6d02018-02-22 10:39:10 -08006645os__getdiskusage_impl(PyObject *module, path_t *path);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006646
6647static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02006648os__getdiskusage(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006649{
6650 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03006651 static const char * const _keywords[] = {"path", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02006652 static _PyArg_Parser _parser = {NULL, _keywords, "_getdiskusage", 0};
6653 PyObject *argsbuf[1];
Steve Dower23ad6d02018-02-22 10:39:10 -08006654 path_t path = PATH_T_INITIALIZE("_getdiskusage", "path", 0, 0);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006655
Serhiy Storchaka31913912019-03-14 10:32:22 +02006656 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
6657 if (!args) {
6658 goto exit;
6659 }
6660 if (!path_converter(args[0], &path)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006661 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006662 }
Steve Dower23ad6d02018-02-22 10:39:10 -08006663 return_value = os__getdiskusage_impl(module, &path);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006664
6665exit:
Steve Dower23ad6d02018-02-22 10:39:10 -08006666 /* Cleanup for path */
6667 path_cleanup(&path);
6668
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006669 return return_value;
6670}
6671
6672#endif /* defined(MS_WINDOWS) */
6673
6674#if defined(HAVE_FPATHCONF)
6675
6676PyDoc_STRVAR(os_fpathconf__doc__,
6677"fpathconf($module, fd, name, /)\n"
6678"--\n"
6679"\n"
6680"Return the configuration limit name for the file descriptor fd.\n"
6681"\n"
6682"If there is no limit, return -1.");
6683
6684#define OS_FPATHCONF_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02006685 {"fpathconf", (PyCFunction)(void(*)(void))os_fpathconf, METH_FASTCALL, os_fpathconf__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006686
6687static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006688os_fpathconf_impl(PyObject *module, int fd, int name);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006689
6690static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02006691os_fpathconf(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006692{
6693 PyObject *return_value = NULL;
6694 int fd;
6695 int name;
6696 long _return_value;
6697
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02006698 if (!_PyArg_CheckPositional("fpathconf", nargs, 2, 2)) {
6699 goto exit;
6700 }
6701 if (PyFloat_Check(args[0])) {
6702 PyErr_SetString(PyExc_TypeError,
6703 "integer argument expected, got float" );
6704 goto exit;
6705 }
6706 fd = _PyLong_AsInt(args[0]);
6707 if (fd == -1 && PyErr_Occurred()) {
6708 goto exit;
6709 }
6710 if (!conv_path_confname(args[1], &name)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01006711 goto exit;
6712 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006713 _return_value = os_fpathconf_impl(module, fd, name);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006714 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006715 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006716 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006717 return_value = PyLong_FromLong(_return_value);
6718
6719exit:
6720 return return_value;
6721}
6722
6723#endif /* defined(HAVE_FPATHCONF) */
6724
6725#if defined(HAVE_PATHCONF)
6726
6727PyDoc_STRVAR(os_pathconf__doc__,
6728"pathconf($module, /, path, name)\n"
6729"--\n"
6730"\n"
6731"Return the configuration limit name for the file or directory path.\n"
6732"\n"
6733"If there is no limit, return -1.\n"
6734"On some platforms, path may also be specified as an open file descriptor.\n"
6735" If this functionality is unavailable, using it raises an exception.");
6736
6737#define OS_PATHCONF_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02006738 {"pathconf", (PyCFunction)(void(*)(void))os_pathconf, METH_FASTCALL|METH_KEYWORDS, os_pathconf__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006739
6740static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006741os_pathconf_impl(PyObject *module, path_t *path, int name);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006742
6743static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02006744os_pathconf(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006745{
6746 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03006747 static const char * const _keywords[] = {"path", "name", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02006748 static _PyArg_Parser _parser = {NULL, _keywords, "pathconf", 0};
6749 PyObject *argsbuf[2];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006750 path_t path = PATH_T_INITIALIZE("pathconf", "path", 0, PATH_HAVE_FPATHCONF);
6751 int name;
6752 long _return_value;
6753
Serhiy Storchaka31913912019-03-14 10:32:22 +02006754 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf);
6755 if (!args) {
6756 goto exit;
6757 }
6758 if (!path_converter(args[0], &path)) {
6759 goto exit;
6760 }
6761 if (!conv_path_confname(args[1], &name)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006762 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006763 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006764 _return_value = os_pathconf_impl(module, &path, name);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006765 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006766 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006767 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006768 return_value = PyLong_FromLong(_return_value);
6769
6770exit:
6771 /* Cleanup for path */
6772 path_cleanup(&path);
6773
6774 return return_value;
6775}
6776
6777#endif /* defined(HAVE_PATHCONF) */
6778
6779#if defined(HAVE_CONFSTR)
6780
6781PyDoc_STRVAR(os_confstr__doc__,
6782"confstr($module, name, /)\n"
6783"--\n"
6784"\n"
6785"Return a string-valued system configuration variable.");
6786
6787#define OS_CONFSTR_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03006788 {"confstr", (PyCFunction)os_confstr, METH_O, os_confstr__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006789
6790static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006791os_confstr_impl(PyObject *module, int name);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006792
6793static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006794os_confstr(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006795{
6796 PyObject *return_value = NULL;
6797 int name;
6798
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02006799 if (!conv_confstr_confname(arg, &name)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006800 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006801 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006802 return_value = os_confstr_impl(module, name);
6803
6804exit:
6805 return return_value;
6806}
6807
6808#endif /* defined(HAVE_CONFSTR) */
6809
6810#if defined(HAVE_SYSCONF)
6811
6812PyDoc_STRVAR(os_sysconf__doc__,
6813"sysconf($module, name, /)\n"
6814"--\n"
6815"\n"
6816"Return an integer-valued system configuration variable.");
6817
6818#define OS_SYSCONF_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03006819 {"sysconf", (PyCFunction)os_sysconf, METH_O, os_sysconf__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006820
6821static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006822os_sysconf_impl(PyObject *module, int name);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006823
6824static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006825os_sysconf(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006826{
6827 PyObject *return_value = NULL;
6828 int name;
6829 long _return_value;
6830
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02006831 if (!conv_sysconf_confname(arg, &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_sysconf_impl(module, 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 return return_value;
6842}
6843
6844#endif /* defined(HAVE_SYSCONF) */
6845
6846PyDoc_STRVAR(os_abort__doc__,
6847"abort($module, /)\n"
6848"--\n"
6849"\n"
6850"Abort the interpreter immediately.\n"
6851"\n"
6852"This function \'dumps core\' or otherwise fails in the hardest way possible\n"
6853"on the hosting operating system. This function never returns.");
6854
6855#define OS_ABORT_METHODDEF \
6856 {"abort", (PyCFunction)os_abort, METH_NOARGS, os_abort__doc__},
6857
6858static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006859os_abort_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006860
6861static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006862os_abort(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006863{
6864 return os_abort_impl(module);
6865}
6866
Steve Dowercc16be82016-09-08 10:35:16 -07006867#if defined(MS_WINDOWS)
6868
6869PyDoc_STRVAR(os_startfile__doc__,
6870"startfile($module, /, filepath, operation=None)\n"
6871"--\n"
6872"\n"
6873"startfile(filepath [, operation])\n"
6874"\n"
6875"Start a file with its associated application.\n"
6876"\n"
6877"When \"operation\" is not specified or \"open\", this acts like\n"
6878"double-clicking the file in Explorer, or giving the file name as an\n"
6879"argument to the DOS \"start\" command: the file is opened with whatever\n"
6880"application (if any) its extension is associated.\n"
6881"When another \"operation\" is given, it specifies what should be done with\n"
6882"the file. A typical operation is \"print\".\n"
6883"\n"
6884"startfile returns as soon as the associated application is launched.\n"
6885"There is no option to wait for the application to close, and no way\n"
6886"to retrieve the application\'s exit status.\n"
6887"\n"
6888"The filepath is relative to the current directory. If you want to use\n"
6889"an absolute path, make sure the first character is not a slash (\"/\");\n"
6890"the underlying Win32 ShellExecute function doesn\'t work if it is.");
6891
6892#define OS_STARTFILE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02006893 {"startfile", (PyCFunction)(void(*)(void))os_startfile, METH_FASTCALL|METH_KEYWORDS, os_startfile__doc__},
Steve Dowercc16be82016-09-08 10:35:16 -07006894
6895static PyObject *
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02006896os_startfile_impl(PyObject *module, path_t *filepath,
6897 const Py_UNICODE *operation);
Steve Dowercc16be82016-09-08 10:35:16 -07006898
6899static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02006900os_startfile(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Steve Dowercc16be82016-09-08 10:35:16 -07006901{
6902 PyObject *return_value = NULL;
Victor Stinner37e4ef72016-09-09 20:00:13 -07006903 static const char * const _keywords[] = {"filepath", "operation", NULL};
6904 static _PyArg_Parser _parser = {"O&|u:startfile", _keywords, 0};
Steve Dowercc16be82016-09-08 10:35:16 -07006905 path_t filepath = PATH_T_INITIALIZE("startfile", "filepath", 0, 0);
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02006906 const Py_UNICODE *operation = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07006907
Victor Stinner3e1fad62017-01-17 01:29:01 +01006908 if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
Steve Dowercc16be82016-09-08 10:35:16 -07006909 path_converter, &filepath, &operation)) {
6910 goto exit;
6911 }
6912 return_value = os_startfile_impl(module, &filepath, operation);
6913
6914exit:
6915 /* Cleanup for filepath */
6916 path_cleanup(&filepath);
6917
6918 return return_value;
6919}
6920
6921#endif /* defined(MS_WINDOWS) */
6922
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006923#if defined(HAVE_GETLOADAVG)
6924
6925PyDoc_STRVAR(os_getloadavg__doc__,
6926"getloadavg($module, /)\n"
6927"--\n"
6928"\n"
6929"Return average recent system load information.\n"
6930"\n"
6931"Return the number of processes in the system run queue averaged over\n"
6932"the last 1, 5, and 15 minutes as a tuple of three floats.\n"
6933"Raises OSError if the load average was unobtainable.");
6934
6935#define OS_GETLOADAVG_METHODDEF \
6936 {"getloadavg", (PyCFunction)os_getloadavg, METH_NOARGS, os_getloadavg__doc__},
6937
6938static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006939os_getloadavg_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006940
6941static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006942os_getloadavg(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006943{
6944 return os_getloadavg_impl(module);
6945}
6946
6947#endif /* defined(HAVE_GETLOADAVG) */
6948
6949PyDoc_STRVAR(os_device_encoding__doc__,
6950"device_encoding($module, /, fd)\n"
6951"--\n"
6952"\n"
6953"Return a string describing the encoding of a terminal\'s file descriptor.\n"
6954"\n"
6955"The file descriptor must be attached to a terminal.\n"
6956"If the device is not a terminal, return None.");
6957
6958#define OS_DEVICE_ENCODING_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02006959 {"device_encoding", (PyCFunction)(void(*)(void))os_device_encoding, METH_FASTCALL|METH_KEYWORDS, os_device_encoding__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006960
6961static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006962os_device_encoding_impl(PyObject *module, int fd);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006963
6964static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02006965os_device_encoding(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006966{
6967 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03006968 static const char * const _keywords[] = {"fd", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02006969 static _PyArg_Parser _parser = {NULL, _keywords, "device_encoding", 0};
6970 PyObject *argsbuf[1];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006971 int fd;
6972
Serhiy Storchaka31913912019-03-14 10:32:22 +02006973 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
6974 if (!args) {
6975 goto exit;
6976 }
6977 if (PyFloat_Check(args[0])) {
6978 PyErr_SetString(PyExc_TypeError,
6979 "integer argument expected, got float" );
6980 goto exit;
6981 }
6982 fd = _PyLong_AsInt(args[0]);
6983 if (fd == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006984 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006985 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006986 return_value = os_device_encoding_impl(module, fd);
6987
6988exit:
6989 return return_value;
6990}
6991
6992#if defined(HAVE_SETRESUID)
6993
6994PyDoc_STRVAR(os_setresuid__doc__,
6995"setresuid($module, ruid, euid, suid, /)\n"
6996"--\n"
6997"\n"
6998"Set the current process\'s real, effective, and saved user ids.");
6999
7000#define OS_SETRESUID_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02007001 {"setresuid", (PyCFunction)(void(*)(void))os_setresuid, METH_FASTCALL, os_setresuid__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007002
7003static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007004os_setresuid_impl(PyObject *module, uid_t ruid, uid_t euid, uid_t suid);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007005
7006static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02007007os_setresuid(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007008{
7009 PyObject *return_value = NULL;
7010 uid_t ruid;
7011 uid_t euid;
7012 uid_t suid;
7013
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02007014 if (!_PyArg_CheckPositional("setresuid", nargs, 3, 3)) {
7015 goto exit;
7016 }
7017 if (!_Py_Uid_Converter(args[0], &ruid)) {
7018 goto exit;
7019 }
7020 if (!_Py_Uid_Converter(args[1], &euid)) {
7021 goto exit;
7022 }
7023 if (!_Py_Uid_Converter(args[2], &suid)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01007024 goto exit;
7025 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007026 return_value = os_setresuid_impl(module, ruid, euid, suid);
7027
7028exit:
7029 return return_value;
7030}
7031
7032#endif /* defined(HAVE_SETRESUID) */
7033
7034#if defined(HAVE_SETRESGID)
7035
7036PyDoc_STRVAR(os_setresgid__doc__,
7037"setresgid($module, rgid, egid, sgid, /)\n"
7038"--\n"
7039"\n"
7040"Set the current process\'s real, effective, and saved group ids.");
7041
7042#define OS_SETRESGID_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02007043 {"setresgid", (PyCFunction)(void(*)(void))os_setresgid, METH_FASTCALL, os_setresgid__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007044
7045static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007046os_setresgid_impl(PyObject *module, gid_t rgid, gid_t egid, gid_t sgid);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007047
7048static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02007049os_setresgid(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007050{
7051 PyObject *return_value = NULL;
7052 gid_t rgid;
7053 gid_t egid;
7054 gid_t sgid;
7055
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02007056 if (!_PyArg_CheckPositional("setresgid", nargs, 3, 3)) {
7057 goto exit;
7058 }
7059 if (!_Py_Gid_Converter(args[0], &rgid)) {
7060 goto exit;
7061 }
7062 if (!_Py_Gid_Converter(args[1], &egid)) {
7063 goto exit;
7064 }
7065 if (!_Py_Gid_Converter(args[2], &sgid)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01007066 goto exit;
7067 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007068 return_value = os_setresgid_impl(module, rgid, egid, sgid);
7069
7070exit:
7071 return return_value;
7072}
7073
7074#endif /* defined(HAVE_SETRESGID) */
7075
7076#if defined(HAVE_GETRESUID)
7077
7078PyDoc_STRVAR(os_getresuid__doc__,
7079"getresuid($module, /)\n"
7080"--\n"
7081"\n"
7082"Return a tuple of the current process\'s real, effective, and saved user ids.");
7083
7084#define OS_GETRESUID_METHODDEF \
7085 {"getresuid", (PyCFunction)os_getresuid, METH_NOARGS, os_getresuid__doc__},
7086
7087static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007088os_getresuid_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007089
7090static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007091os_getresuid(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007092{
7093 return os_getresuid_impl(module);
7094}
7095
7096#endif /* defined(HAVE_GETRESUID) */
7097
7098#if defined(HAVE_GETRESGID)
7099
7100PyDoc_STRVAR(os_getresgid__doc__,
7101"getresgid($module, /)\n"
7102"--\n"
7103"\n"
7104"Return a tuple of the current process\'s real, effective, and saved group ids.");
7105
7106#define OS_GETRESGID_METHODDEF \
7107 {"getresgid", (PyCFunction)os_getresgid, METH_NOARGS, os_getresgid__doc__},
7108
7109static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007110os_getresgid_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007111
7112static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007113os_getresgid(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007114{
7115 return os_getresgid_impl(module);
7116}
7117
7118#endif /* defined(HAVE_GETRESGID) */
7119
7120#if defined(USE_XATTRS)
7121
7122PyDoc_STRVAR(os_getxattr__doc__,
7123"getxattr($module, /, path, attribute, *, follow_symlinks=True)\n"
7124"--\n"
7125"\n"
7126"Return the value of extended attribute attribute on path.\n"
7127"\n"
BNMetricsb9427072018-11-02 15:20:19 +00007128"path may be either a string, a path-like object, or an open file descriptor.\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007129"If follow_symlinks is False, and the last element of the path is a symbolic\n"
7130" link, getxattr will examine the symbolic link itself instead of the file\n"
7131" the link points to.");
7132
7133#define OS_GETXATTR_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02007134 {"getxattr", (PyCFunction)(void(*)(void))os_getxattr, METH_FASTCALL|METH_KEYWORDS, os_getxattr__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007135
7136static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007137os_getxattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -04007138 int follow_symlinks);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007139
7140static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02007141os_getxattr(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007142{
7143 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03007144 static const char * const _keywords[] = {"path", "attribute", "follow_symlinks", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02007145 static _PyArg_Parser _parser = {NULL, _keywords, "getxattr", 0};
7146 PyObject *argsbuf[3];
7147 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007148 path_t path = PATH_T_INITIALIZE("getxattr", "path", 0, 1);
7149 path_t attribute = PATH_T_INITIALIZE("getxattr", "attribute", 0, 0);
7150 int follow_symlinks = 1;
7151
Serhiy Storchaka31913912019-03-14 10:32:22 +02007152 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf);
7153 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007154 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03007155 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02007156 if (!path_converter(args[0], &path)) {
7157 goto exit;
7158 }
7159 if (!path_converter(args[1], &attribute)) {
7160 goto exit;
7161 }
7162 if (!noptargs) {
7163 goto skip_optional_kwonly;
7164 }
7165 follow_symlinks = PyObject_IsTrue(args[2]);
7166 if (follow_symlinks < 0) {
7167 goto exit;
7168 }
7169skip_optional_kwonly:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007170 return_value = os_getxattr_impl(module, &path, &attribute, follow_symlinks);
7171
7172exit:
7173 /* Cleanup for path */
7174 path_cleanup(&path);
7175 /* Cleanup for attribute */
7176 path_cleanup(&attribute);
7177
7178 return return_value;
7179}
7180
7181#endif /* defined(USE_XATTRS) */
7182
7183#if defined(USE_XATTRS)
7184
7185PyDoc_STRVAR(os_setxattr__doc__,
7186"setxattr($module, /, path, attribute, value, flags=0, *,\n"
7187" follow_symlinks=True)\n"
7188"--\n"
7189"\n"
7190"Set extended attribute attribute on path to value.\n"
7191"\n"
BNMetricsb9427072018-11-02 15:20:19 +00007192"path may be either a string, a path-like object, or an open file descriptor.\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007193"If follow_symlinks is False, and the last element of the path is a symbolic\n"
7194" link, setxattr will modify the symbolic link itself instead of the file\n"
7195" the link points to.");
7196
7197#define OS_SETXATTR_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02007198 {"setxattr", (PyCFunction)(void(*)(void))os_setxattr, METH_FASTCALL|METH_KEYWORDS, os_setxattr__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007199
7200static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007201os_setxattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -04007202 Py_buffer *value, int flags, int follow_symlinks);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007203
7204static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02007205os_setxattr(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007206{
7207 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03007208 static const char * const _keywords[] = {"path", "attribute", "value", "flags", "follow_symlinks", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02007209 static _PyArg_Parser _parser = {NULL, _keywords, "setxattr", 0};
7210 PyObject *argsbuf[5];
7211 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 3;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007212 path_t path = PATH_T_INITIALIZE("setxattr", "path", 0, 1);
7213 path_t attribute = PATH_T_INITIALIZE("setxattr", "attribute", 0, 0);
7214 Py_buffer value = {NULL, NULL};
7215 int flags = 0;
7216 int follow_symlinks = 1;
7217
Serhiy Storchaka31913912019-03-14 10:32:22 +02007218 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 3, 4, 0, argsbuf);
7219 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007220 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03007221 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02007222 if (!path_converter(args[0], &path)) {
7223 goto exit;
7224 }
7225 if (!path_converter(args[1], &attribute)) {
7226 goto exit;
7227 }
7228 if (PyObject_GetBuffer(args[2], &value, PyBUF_SIMPLE) != 0) {
7229 goto exit;
7230 }
7231 if (!PyBuffer_IsContiguous(&value, 'C')) {
7232 _PyArg_BadArgument("setxattr", 3, "contiguous buffer", args[2]);
7233 goto exit;
7234 }
7235 if (!noptargs) {
7236 goto skip_optional_pos;
7237 }
7238 if (args[3]) {
7239 if (PyFloat_Check(args[3])) {
7240 PyErr_SetString(PyExc_TypeError,
7241 "integer argument expected, got float" );
7242 goto exit;
7243 }
7244 flags = _PyLong_AsInt(args[3]);
7245 if (flags == -1 && PyErr_Occurred()) {
7246 goto exit;
7247 }
7248 if (!--noptargs) {
7249 goto skip_optional_pos;
7250 }
7251 }
7252skip_optional_pos:
7253 if (!noptargs) {
7254 goto skip_optional_kwonly;
7255 }
7256 follow_symlinks = PyObject_IsTrue(args[4]);
7257 if (follow_symlinks < 0) {
7258 goto exit;
7259 }
7260skip_optional_kwonly:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007261 return_value = os_setxattr_impl(module, &path, &attribute, &value, flags, follow_symlinks);
7262
7263exit:
7264 /* Cleanup for path */
7265 path_cleanup(&path);
7266 /* Cleanup for attribute */
7267 path_cleanup(&attribute);
7268 /* Cleanup for value */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03007269 if (value.obj) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007270 PyBuffer_Release(&value);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03007271 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007272
7273 return return_value;
7274}
7275
7276#endif /* defined(USE_XATTRS) */
7277
7278#if defined(USE_XATTRS)
7279
7280PyDoc_STRVAR(os_removexattr__doc__,
7281"removexattr($module, /, path, attribute, *, follow_symlinks=True)\n"
7282"--\n"
7283"\n"
7284"Remove extended attribute attribute on path.\n"
7285"\n"
BNMetricsb9427072018-11-02 15:20:19 +00007286"path may be either a string, a path-like object, or an open file descriptor.\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007287"If follow_symlinks is False, and the last element of the path is a symbolic\n"
7288" link, removexattr will modify the symbolic link itself instead of the file\n"
7289" the link points to.");
7290
7291#define OS_REMOVEXATTR_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02007292 {"removexattr", (PyCFunction)(void(*)(void))os_removexattr, METH_FASTCALL|METH_KEYWORDS, os_removexattr__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007293
7294static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007295os_removexattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -04007296 int follow_symlinks);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007297
7298static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02007299os_removexattr(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007300{
7301 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03007302 static const char * const _keywords[] = {"path", "attribute", "follow_symlinks", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02007303 static _PyArg_Parser _parser = {NULL, _keywords, "removexattr", 0};
7304 PyObject *argsbuf[3];
7305 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007306 path_t path = PATH_T_INITIALIZE("removexattr", "path", 0, 1);
7307 path_t attribute = PATH_T_INITIALIZE("removexattr", "attribute", 0, 0);
7308 int follow_symlinks = 1;
7309
Serhiy Storchaka31913912019-03-14 10:32:22 +02007310 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf);
7311 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007312 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03007313 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02007314 if (!path_converter(args[0], &path)) {
7315 goto exit;
7316 }
7317 if (!path_converter(args[1], &attribute)) {
7318 goto exit;
7319 }
7320 if (!noptargs) {
7321 goto skip_optional_kwonly;
7322 }
7323 follow_symlinks = PyObject_IsTrue(args[2]);
7324 if (follow_symlinks < 0) {
7325 goto exit;
7326 }
7327skip_optional_kwonly:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007328 return_value = os_removexattr_impl(module, &path, &attribute, follow_symlinks);
7329
7330exit:
7331 /* Cleanup for path */
7332 path_cleanup(&path);
7333 /* Cleanup for attribute */
7334 path_cleanup(&attribute);
7335
7336 return return_value;
7337}
7338
7339#endif /* defined(USE_XATTRS) */
7340
7341#if defined(USE_XATTRS)
7342
7343PyDoc_STRVAR(os_listxattr__doc__,
7344"listxattr($module, /, path=None, *, follow_symlinks=True)\n"
7345"--\n"
7346"\n"
7347"Return a list of extended attributes on path.\n"
7348"\n"
BNMetricsb9427072018-11-02 15:20:19 +00007349"path may be either None, a string, a path-like object, or an open file descriptor.\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007350"if path is None, listxattr will examine the current directory.\n"
7351"If follow_symlinks is False, and the last element of the path is a symbolic\n"
7352" link, listxattr will examine the symbolic link itself instead of the file\n"
7353" the link points to.");
7354
7355#define OS_LISTXATTR_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02007356 {"listxattr", (PyCFunction)(void(*)(void))os_listxattr, METH_FASTCALL|METH_KEYWORDS, os_listxattr__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007357
7358static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007359os_listxattr_impl(PyObject *module, path_t *path, int follow_symlinks);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007360
7361static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02007362os_listxattr(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007363{
7364 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03007365 static const char * const _keywords[] = {"path", "follow_symlinks", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02007366 static _PyArg_Parser _parser = {NULL, _keywords, "listxattr", 0};
7367 PyObject *argsbuf[2];
7368 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007369 path_t path = PATH_T_INITIALIZE("listxattr", "path", 1, 1);
7370 int follow_symlinks = 1;
7371
Serhiy Storchaka31913912019-03-14 10:32:22 +02007372 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
7373 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007374 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03007375 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02007376 if (!noptargs) {
7377 goto skip_optional_pos;
7378 }
7379 if (args[0]) {
7380 if (!path_converter(args[0], &path)) {
7381 goto exit;
7382 }
7383 if (!--noptargs) {
7384 goto skip_optional_pos;
7385 }
7386 }
7387skip_optional_pos:
7388 if (!noptargs) {
7389 goto skip_optional_kwonly;
7390 }
7391 follow_symlinks = PyObject_IsTrue(args[1]);
7392 if (follow_symlinks < 0) {
7393 goto exit;
7394 }
7395skip_optional_kwonly:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007396 return_value = os_listxattr_impl(module, &path, follow_symlinks);
7397
7398exit:
7399 /* Cleanup for path */
7400 path_cleanup(&path);
7401
7402 return return_value;
7403}
7404
7405#endif /* defined(USE_XATTRS) */
7406
7407PyDoc_STRVAR(os_urandom__doc__,
7408"urandom($module, size, /)\n"
7409"--\n"
7410"\n"
7411"Return a bytes object containing random bytes suitable for cryptographic use.");
7412
7413#define OS_URANDOM_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03007414 {"urandom", (PyCFunction)os_urandom, METH_O, os_urandom__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007415
7416static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007417os_urandom_impl(PyObject *module, Py_ssize_t size);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007418
7419static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007420os_urandom(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007421{
7422 PyObject *return_value = NULL;
7423 Py_ssize_t size;
7424
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02007425 if (PyFloat_Check(arg)) {
7426 PyErr_SetString(PyExc_TypeError,
7427 "integer argument expected, got float" );
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007428 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03007429 }
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02007430 {
7431 Py_ssize_t ival = -1;
7432 PyObject *iobj = PyNumber_Index(arg);
7433 if (iobj != NULL) {
7434 ival = PyLong_AsSsize_t(iobj);
7435 Py_DECREF(iobj);
7436 }
7437 if (ival == -1 && PyErr_Occurred()) {
7438 goto exit;
7439 }
7440 size = ival;
7441 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007442 return_value = os_urandom_impl(module, size);
7443
7444exit:
7445 return return_value;
7446}
7447
Zackery Spytz43fdbd22019-05-29 13:57:07 -06007448#if defined(HAVE_MEMFD_CREATE)
7449
7450PyDoc_STRVAR(os_memfd_create__doc__,
7451"memfd_create($module, /, name, flags=MFD_CLOEXEC)\n"
7452"--\n"
7453"\n");
7454
7455#define OS_MEMFD_CREATE_METHODDEF \
7456 {"memfd_create", (PyCFunction)(void(*)(void))os_memfd_create, METH_FASTCALL|METH_KEYWORDS, os_memfd_create__doc__},
7457
7458static PyObject *
7459os_memfd_create_impl(PyObject *module, PyObject *name, unsigned int flags);
7460
7461static PyObject *
7462os_memfd_create(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
7463{
7464 PyObject *return_value = NULL;
7465 static const char * const _keywords[] = {"name", "flags", NULL};
7466 static _PyArg_Parser _parser = {NULL, _keywords, "memfd_create", 0};
7467 PyObject *argsbuf[2];
7468 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
7469 PyObject *name = NULL;
7470 unsigned int flags = MFD_CLOEXEC;
7471
7472 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
7473 if (!args) {
7474 goto exit;
7475 }
7476 if (!PyUnicode_FSConverter(args[0], &name)) {
7477 goto exit;
7478 }
7479 if (!noptargs) {
7480 goto skip_optional_pos;
7481 }
7482 if (PyFloat_Check(args[1])) {
7483 PyErr_SetString(PyExc_TypeError,
7484 "integer argument expected, got float" );
7485 goto exit;
7486 }
7487 flags = (unsigned int)PyLong_AsUnsignedLongMask(args[1]);
7488 if (flags == (unsigned int)-1 && PyErr_Occurred()) {
7489 goto exit;
7490 }
7491skip_optional_pos:
7492 return_value = os_memfd_create_impl(module, name, flags);
7493
7494exit:
7495 /* Cleanup for name */
7496 Py_XDECREF(name);
7497
7498 return return_value;
7499}
7500
7501#endif /* defined(HAVE_MEMFD_CREATE) */
7502
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007503PyDoc_STRVAR(os_cpu_count__doc__,
7504"cpu_count($module, /)\n"
7505"--\n"
7506"\n"
Charles-François Natali80d62e62015-08-13 20:37:08 +01007507"Return the number of CPUs in the system; return None if indeterminable.\n"
7508"\n"
7509"This number is not equivalent to the number of CPUs the current process can\n"
7510"use. The number of usable CPUs can be obtained with\n"
7511"``len(os.sched_getaffinity(0))``");
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007512
7513#define OS_CPU_COUNT_METHODDEF \
7514 {"cpu_count", (PyCFunction)os_cpu_count, METH_NOARGS, os_cpu_count__doc__},
7515
7516static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007517os_cpu_count_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007518
7519static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007520os_cpu_count(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007521{
7522 return os_cpu_count_impl(module);
7523}
7524
7525PyDoc_STRVAR(os_get_inheritable__doc__,
7526"get_inheritable($module, fd, /)\n"
7527"--\n"
7528"\n"
7529"Get the close-on-exe flag of the specified file descriptor.");
7530
7531#define OS_GET_INHERITABLE_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03007532 {"get_inheritable", (PyCFunction)os_get_inheritable, METH_O, os_get_inheritable__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007533
7534static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007535os_get_inheritable_impl(PyObject *module, int fd);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007536
7537static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007538os_get_inheritable(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007539{
7540 PyObject *return_value = NULL;
7541 int fd;
7542 int _return_value;
7543
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02007544 if (PyFloat_Check(arg)) {
7545 PyErr_SetString(PyExc_TypeError,
7546 "integer argument expected, got float" );
7547 goto exit;
7548 }
7549 fd = _PyLong_AsInt(arg);
7550 if (fd == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007551 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03007552 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007553 _return_value = os_get_inheritable_impl(module, fd);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03007554 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007555 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03007556 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007557 return_value = PyBool_FromLong((long)_return_value);
7558
7559exit:
7560 return return_value;
7561}
7562
7563PyDoc_STRVAR(os_set_inheritable__doc__,
7564"set_inheritable($module, fd, inheritable, /)\n"
7565"--\n"
7566"\n"
7567"Set the inheritable flag of the specified file descriptor.");
7568
7569#define OS_SET_INHERITABLE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02007570 {"set_inheritable", (PyCFunction)(void(*)(void))os_set_inheritable, METH_FASTCALL, os_set_inheritable__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007571
7572static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007573os_set_inheritable_impl(PyObject *module, int fd, int inheritable);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007574
7575static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02007576os_set_inheritable(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007577{
7578 PyObject *return_value = NULL;
7579 int fd;
7580 int inheritable;
7581
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02007582 if (!_PyArg_CheckPositional("set_inheritable", nargs, 2, 2)) {
7583 goto exit;
7584 }
7585 if (PyFloat_Check(args[0])) {
7586 PyErr_SetString(PyExc_TypeError,
7587 "integer argument expected, got float" );
7588 goto exit;
7589 }
7590 fd = _PyLong_AsInt(args[0]);
7591 if (fd == -1 && PyErr_Occurred()) {
7592 goto exit;
7593 }
7594 if (PyFloat_Check(args[1])) {
7595 PyErr_SetString(PyExc_TypeError,
7596 "integer argument expected, got float" );
7597 goto exit;
7598 }
7599 inheritable = _PyLong_AsInt(args[1]);
7600 if (inheritable == -1 && PyErr_Occurred()) {
Victor Stinner259f0e42017-01-17 01:35:17 +01007601 goto exit;
7602 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007603 return_value = os_set_inheritable_impl(module, fd, inheritable);
7604
7605exit:
7606 return return_value;
7607}
7608
7609#if defined(MS_WINDOWS)
7610
7611PyDoc_STRVAR(os_get_handle_inheritable__doc__,
7612"get_handle_inheritable($module, handle, /)\n"
7613"--\n"
7614"\n"
7615"Get the close-on-exe flag of the specified file descriptor.");
7616
7617#define OS_GET_HANDLE_INHERITABLE_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03007618 {"get_handle_inheritable", (PyCFunction)os_get_handle_inheritable, METH_O, os_get_handle_inheritable__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007619
7620static int
Victor Stinner581139c2016-09-06 15:54:20 -07007621os_get_handle_inheritable_impl(PyObject *module, intptr_t handle);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007622
7623static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007624os_get_handle_inheritable(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007625{
7626 PyObject *return_value = NULL;
Victor Stinner581139c2016-09-06 15:54:20 -07007627 intptr_t handle;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007628 int _return_value;
7629
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03007630 if (!PyArg_Parse(arg, "" _Py_PARSE_INTPTR ":get_handle_inheritable", &handle)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007631 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03007632 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007633 _return_value = os_get_handle_inheritable_impl(module, handle);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03007634 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007635 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03007636 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007637 return_value = PyBool_FromLong((long)_return_value);
7638
7639exit:
7640 return return_value;
7641}
7642
7643#endif /* defined(MS_WINDOWS) */
7644
7645#if defined(MS_WINDOWS)
7646
7647PyDoc_STRVAR(os_set_handle_inheritable__doc__,
7648"set_handle_inheritable($module, handle, inheritable, /)\n"
7649"--\n"
7650"\n"
7651"Set the inheritable flag of the specified handle.");
7652
7653#define OS_SET_HANDLE_INHERITABLE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02007654 {"set_handle_inheritable", (PyCFunction)(void(*)(void))os_set_handle_inheritable, METH_FASTCALL, os_set_handle_inheritable__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007655
7656static PyObject *
Victor Stinner581139c2016-09-06 15:54:20 -07007657os_set_handle_inheritable_impl(PyObject *module, intptr_t handle,
Larry Hastings89964c42015-04-14 18:07:59 -04007658 int inheritable);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007659
7660static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02007661os_set_handle_inheritable(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007662{
7663 PyObject *return_value = NULL;
Victor Stinner581139c2016-09-06 15:54:20 -07007664 intptr_t handle;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007665 int inheritable;
7666
Sylvain74453812017-06-10 06:51:48 +02007667 if (!_PyArg_ParseStack(args, nargs, "" _Py_PARSE_INTPTR "p:set_handle_inheritable",
7668 &handle, &inheritable)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01007669 goto exit;
7670 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007671 return_value = os_set_handle_inheritable_impl(module, handle, inheritable);
7672
7673exit:
7674 return return_value;
7675}
7676
7677#endif /* defined(MS_WINDOWS) */
7678
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007679#if !defined(MS_WINDOWS)
7680
7681PyDoc_STRVAR(os_get_blocking__doc__,
7682"get_blocking($module, fd, /)\n"
7683"--\n"
7684"\n"
7685"Get the blocking mode of the file descriptor.\n"
7686"\n"
7687"Return False if the O_NONBLOCK flag is set, True if the flag is cleared.");
7688
7689#define OS_GET_BLOCKING_METHODDEF \
7690 {"get_blocking", (PyCFunction)os_get_blocking, METH_O, os_get_blocking__doc__},
7691
7692static int
7693os_get_blocking_impl(PyObject *module, int fd);
7694
7695static PyObject *
7696os_get_blocking(PyObject *module, PyObject *arg)
7697{
7698 PyObject *return_value = NULL;
7699 int fd;
7700 int _return_value;
7701
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02007702 if (PyFloat_Check(arg)) {
7703 PyErr_SetString(PyExc_TypeError,
7704 "integer argument expected, got float" );
7705 goto exit;
7706 }
7707 fd = _PyLong_AsInt(arg);
7708 if (fd == -1 && PyErr_Occurred()) {
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007709 goto exit;
7710 }
7711 _return_value = os_get_blocking_impl(module, fd);
7712 if ((_return_value == -1) && PyErr_Occurred()) {
7713 goto exit;
7714 }
7715 return_value = PyBool_FromLong((long)_return_value);
7716
7717exit:
7718 return return_value;
7719}
7720
7721#endif /* !defined(MS_WINDOWS) */
7722
7723#if !defined(MS_WINDOWS)
7724
7725PyDoc_STRVAR(os_set_blocking__doc__,
7726"set_blocking($module, fd, blocking, /)\n"
7727"--\n"
7728"\n"
7729"Set the blocking mode of the specified file descriptor.\n"
7730"\n"
7731"Set the O_NONBLOCK flag if blocking is False,\n"
7732"clear the O_NONBLOCK flag otherwise.");
7733
7734#define OS_SET_BLOCKING_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02007735 {"set_blocking", (PyCFunction)(void(*)(void))os_set_blocking, METH_FASTCALL, os_set_blocking__doc__},
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007736
7737static PyObject *
7738os_set_blocking_impl(PyObject *module, int fd, int blocking);
7739
7740static PyObject *
7741os_set_blocking(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
7742{
7743 PyObject *return_value = NULL;
7744 int fd;
7745 int blocking;
7746
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02007747 if (!_PyArg_CheckPositional("set_blocking", nargs, 2, 2)) {
7748 goto exit;
7749 }
7750 if (PyFloat_Check(args[0])) {
7751 PyErr_SetString(PyExc_TypeError,
7752 "integer argument expected, got float" );
7753 goto exit;
7754 }
7755 fd = _PyLong_AsInt(args[0]);
7756 if (fd == -1 && PyErr_Occurred()) {
7757 goto exit;
7758 }
7759 if (PyFloat_Check(args[1])) {
7760 PyErr_SetString(PyExc_TypeError,
7761 "integer argument expected, got float" );
7762 goto exit;
7763 }
7764 blocking = _PyLong_AsInt(args[1]);
7765 if (blocking == -1 && PyErr_Occurred()) {
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007766 goto exit;
7767 }
7768 return_value = os_set_blocking_impl(module, fd, blocking);
7769
7770exit:
7771 return return_value;
7772}
7773
7774#endif /* !defined(MS_WINDOWS) */
7775
Serhiy Storchaka49d02d12016-11-06 13:45:33 +02007776PyDoc_STRVAR(os_DirEntry_is_symlink__doc__,
7777"is_symlink($self, /)\n"
7778"--\n"
7779"\n"
7780"Return True if the entry is a symbolic link; cached per entry.");
7781
7782#define OS_DIRENTRY_IS_SYMLINK_METHODDEF \
7783 {"is_symlink", (PyCFunction)os_DirEntry_is_symlink, METH_NOARGS, os_DirEntry_is_symlink__doc__},
7784
7785static int
7786os_DirEntry_is_symlink_impl(DirEntry *self);
7787
7788static PyObject *
7789os_DirEntry_is_symlink(DirEntry *self, PyObject *Py_UNUSED(ignored))
7790{
7791 PyObject *return_value = NULL;
7792 int _return_value;
7793
7794 _return_value = os_DirEntry_is_symlink_impl(self);
7795 if ((_return_value == -1) && PyErr_Occurred()) {
7796 goto exit;
7797 }
7798 return_value = PyBool_FromLong((long)_return_value);
7799
7800exit:
7801 return return_value;
7802}
7803
7804PyDoc_STRVAR(os_DirEntry_stat__doc__,
7805"stat($self, /, *, follow_symlinks=True)\n"
7806"--\n"
7807"\n"
7808"Return stat_result object for the entry; cached per entry.");
7809
7810#define OS_DIRENTRY_STAT_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02007811 {"stat", (PyCFunction)(void(*)(void))os_DirEntry_stat, METH_FASTCALL|METH_KEYWORDS, os_DirEntry_stat__doc__},
Serhiy Storchaka49d02d12016-11-06 13:45:33 +02007812
7813static PyObject *
7814os_DirEntry_stat_impl(DirEntry *self, int follow_symlinks);
7815
7816static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02007817os_DirEntry_stat(DirEntry *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka49d02d12016-11-06 13:45:33 +02007818{
7819 PyObject *return_value = NULL;
7820 static const char * const _keywords[] = {"follow_symlinks", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02007821 static _PyArg_Parser _parser = {NULL, _keywords, "stat", 0};
7822 PyObject *argsbuf[1];
7823 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
Serhiy Storchaka49d02d12016-11-06 13:45:33 +02007824 int follow_symlinks = 1;
7825
Serhiy Storchaka31913912019-03-14 10:32:22 +02007826 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 0, 0, argsbuf);
7827 if (!args) {
Serhiy Storchaka49d02d12016-11-06 13:45:33 +02007828 goto exit;
7829 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02007830 if (!noptargs) {
7831 goto skip_optional_kwonly;
7832 }
7833 follow_symlinks = PyObject_IsTrue(args[0]);
7834 if (follow_symlinks < 0) {
7835 goto exit;
7836 }
7837skip_optional_kwonly:
Serhiy Storchaka49d02d12016-11-06 13:45:33 +02007838 return_value = os_DirEntry_stat_impl(self, follow_symlinks);
7839
7840exit:
7841 return return_value;
7842}
7843
7844PyDoc_STRVAR(os_DirEntry_is_dir__doc__,
7845"is_dir($self, /, *, follow_symlinks=True)\n"
7846"--\n"
7847"\n"
7848"Return True if the entry is a directory; cached per entry.");
7849
7850#define OS_DIRENTRY_IS_DIR_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02007851 {"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 +02007852
7853static int
7854os_DirEntry_is_dir_impl(DirEntry *self, int follow_symlinks);
7855
7856static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02007857os_DirEntry_is_dir(DirEntry *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka49d02d12016-11-06 13:45:33 +02007858{
7859 PyObject *return_value = NULL;
7860 static const char * const _keywords[] = {"follow_symlinks", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02007861 static _PyArg_Parser _parser = {NULL, _keywords, "is_dir", 0};
7862 PyObject *argsbuf[1];
7863 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
Serhiy Storchaka49d02d12016-11-06 13:45:33 +02007864 int follow_symlinks = 1;
7865 int _return_value;
7866
Serhiy Storchaka31913912019-03-14 10:32:22 +02007867 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 0, 0, argsbuf);
7868 if (!args) {
Serhiy Storchaka49d02d12016-11-06 13:45:33 +02007869 goto exit;
7870 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02007871 if (!noptargs) {
7872 goto skip_optional_kwonly;
7873 }
7874 follow_symlinks = PyObject_IsTrue(args[0]);
7875 if (follow_symlinks < 0) {
7876 goto exit;
7877 }
7878skip_optional_kwonly:
Serhiy Storchaka49d02d12016-11-06 13:45:33 +02007879 _return_value = os_DirEntry_is_dir_impl(self, follow_symlinks);
7880 if ((_return_value == -1) && PyErr_Occurred()) {
7881 goto exit;
7882 }
7883 return_value = PyBool_FromLong((long)_return_value);
7884
7885exit:
7886 return return_value;
7887}
7888
7889PyDoc_STRVAR(os_DirEntry_is_file__doc__,
7890"is_file($self, /, *, follow_symlinks=True)\n"
7891"--\n"
7892"\n"
7893"Return True if the entry is a file; cached per entry.");
7894
7895#define OS_DIRENTRY_IS_FILE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02007896 {"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 +02007897
7898static int
7899os_DirEntry_is_file_impl(DirEntry *self, int follow_symlinks);
7900
7901static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02007902os_DirEntry_is_file(DirEntry *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka49d02d12016-11-06 13:45:33 +02007903{
7904 PyObject *return_value = NULL;
7905 static const char * const _keywords[] = {"follow_symlinks", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02007906 static _PyArg_Parser _parser = {NULL, _keywords, "is_file", 0};
7907 PyObject *argsbuf[1];
7908 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
Serhiy Storchaka49d02d12016-11-06 13:45:33 +02007909 int follow_symlinks = 1;
7910 int _return_value;
7911
Serhiy Storchaka31913912019-03-14 10:32:22 +02007912 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 0, 0, argsbuf);
7913 if (!args) {
Serhiy Storchaka49d02d12016-11-06 13:45:33 +02007914 goto exit;
7915 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02007916 if (!noptargs) {
7917 goto skip_optional_kwonly;
7918 }
7919 follow_symlinks = PyObject_IsTrue(args[0]);
7920 if (follow_symlinks < 0) {
7921 goto exit;
7922 }
7923skip_optional_kwonly:
Serhiy Storchaka49d02d12016-11-06 13:45:33 +02007924 _return_value = os_DirEntry_is_file_impl(self, follow_symlinks);
7925 if ((_return_value == -1) && PyErr_Occurred()) {
7926 goto exit;
7927 }
7928 return_value = PyBool_FromLong((long)_return_value);
7929
7930exit:
7931 return return_value;
7932}
7933
7934PyDoc_STRVAR(os_DirEntry_inode__doc__,
7935"inode($self, /)\n"
7936"--\n"
7937"\n"
7938"Return inode of the entry; cached per entry.");
7939
7940#define OS_DIRENTRY_INODE_METHODDEF \
7941 {"inode", (PyCFunction)os_DirEntry_inode, METH_NOARGS, os_DirEntry_inode__doc__},
7942
7943static PyObject *
7944os_DirEntry_inode_impl(DirEntry *self);
7945
7946static PyObject *
7947os_DirEntry_inode(DirEntry *self, PyObject *Py_UNUSED(ignored))
7948{
7949 return os_DirEntry_inode_impl(self);
7950}
7951
7952PyDoc_STRVAR(os_DirEntry___fspath____doc__,
7953"__fspath__($self, /)\n"
7954"--\n"
7955"\n"
7956"Returns the path for the entry.");
7957
7958#define OS_DIRENTRY___FSPATH___METHODDEF \
7959 {"__fspath__", (PyCFunction)os_DirEntry___fspath__, METH_NOARGS, os_DirEntry___fspath____doc__},
7960
7961static PyObject *
7962os_DirEntry___fspath___impl(DirEntry *self);
7963
7964static PyObject *
7965os_DirEntry___fspath__(DirEntry *self, PyObject *Py_UNUSED(ignored))
7966{
7967 return os_DirEntry___fspath___impl(self);
7968}
7969
7970PyDoc_STRVAR(os_scandir__doc__,
7971"scandir($module, /, path=None)\n"
7972"--\n"
7973"\n"
7974"Return an iterator of DirEntry objects for given path.\n"
7975"\n"
BNMetricsb9427072018-11-02 15:20:19 +00007976"path can be specified as either str, bytes, or a path-like object. If path\n"
Serhiy Storchaka49d02d12016-11-06 13:45:33 +02007977"is bytes, the names of yielded DirEntry objects will also be bytes; in\n"
7978"all other circumstances they will be str.\n"
7979"\n"
7980"If path is None, uses the path=\'.\'.");
7981
7982#define OS_SCANDIR_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02007983 {"scandir", (PyCFunction)(void(*)(void))os_scandir, METH_FASTCALL|METH_KEYWORDS, os_scandir__doc__},
Serhiy Storchaka49d02d12016-11-06 13:45:33 +02007984
7985static PyObject *
7986os_scandir_impl(PyObject *module, path_t *path);
7987
7988static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02007989os_scandir(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka49d02d12016-11-06 13:45:33 +02007990{
7991 PyObject *return_value = NULL;
7992 static const char * const _keywords[] = {"path", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02007993 static _PyArg_Parser _parser = {NULL, _keywords, "scandir", 0};
7994 PyObject *argsbuf[1];
7995 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
Serhiy Storchakaea720fe2017-03-30 09:12:31 +03007996 path_t path = PATH_T_INITIALIZE("scandir", "path", 1, PATH_HAVE_FDOPENDIR);
Serhiy Storchaka49d02d12016-11-06 13:45:33 +02007997
Serhiy Storchaka31913912019-03-14 10:32:22 +02007998 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
7999 if (!args) {
Serhiy Storchaka49d02d12016-11-06 13:45:33 +02008000 goto exit;
8001 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02008002 if (!noptargs) {
8003 goto skip_optional_pos;
8004 }
8005 if (!path_converter(args[0], &path)) {
8006 goto exit;
8007 }
8008skip_optional_pos:
Serhiy Storchaka49d02d12016-11-06 13:45:33 +02008009 return_value = os_scandir_impl(module, &path);
8010
8011exit:
8012 /* Cleanup for path */
8013 path_cleanup(&path);
8014
8015 return return_value;
8016}
8017
Ethan Furman410ef8e2016-06-04 12:06:26 -07008018PyDoc_STRVAR(os_fspath__doc__,
8019"fspath($module, /, path)\n"
8020"--\n"
8021"\n"
8022"Return the file system path representation of the object.\n"
8023"\n"
Brett Cannonb4f43e92016-06-09 14:32:08 -07008024"If the object is str or bytes, then allow it to pass through as-is. If the\n"
8025"object defines __fspath__(), then return the result of that method. All other\n"
8026"types raise a TypeError.");
Ethan Furman410ef8e2016-06-04 12:06:26 -07008027
8028#define OS_FSPATH_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02008029 {"fspath", (PyCFunction)(void(*)(void))os_fspath, METH_FASTCALL|METH_KEYWORDS, os_fspath__doc__},
Ethan Furman410ef8e2016-06-04 12:06:26 -07008030
8031static PyObject *
Serhiy Storchaka2954f832016-07-07 18:20:03 +03008032os_fspath_impl(PyObject *module, PyObject *path);
Ethan Furman410ef8e2016-06-04 12:06:26 -07008033
8034static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02008035os_fspath(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Ethan Furman410ef8e2016-06-04 12:06:26 -07008036{
8037 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03008038 static const char * const _keywords[] = {"path", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02008039 static _PyArg_Parser _parser = {NULL, _keywords, "fspath", 0};
8040 PyObject *argsbuf[1];
Ethan Furman410ef8e2016-06-04 12:06:26 -07008041 PyObject *path;
8042
Serhiy Storchaka31913912019-03-14 10:32:22 +02008043 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
8044 if (!args) {
Ethan Furman410ef8e2016-06-04 12:06:26 -07008045 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03008046 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02008047 path = args[0];
Ethan Furman410ef8e2016-06-04 12:06:26 -07008048 return_value = os_fspath_impl(module, path);
8049
8050exit:
8051 return return_value;
8052}
8053
Victor Stinner9b1f4742016-09-06 16:18:52 -07008054#if defined(HAVE_GETRANDOM_SYSCALL)
8055
8056PyDoc_STRVAR(os_getrandom__doc__,
8057"getrandom($module, /, size, flags=0)\n"
8058"--\n"
8059"\n"
8060"Obtain a series of random bytes.");
8061
8062#define OS_GETRANDOM_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02008063 {"getrandom", (PyCFunction)(void(*)(void))os_getrandom, METH_FASTCALL|METH_KEYWORDS, os_getrandom__doc__},
Victor Stinner9b1f4742016-09-06 16:18:52 -07008064
8065static PyObject *
8066os_getrandom_impl(PyObject *module, Py_ssize_t size, int flags);
8067
8068static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02008069os_getrandom(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Victor Stinner9b1f4742016-09-06 16:18:52 -07008070{
8071 PyObject *return_value = NULL;
8072 static const char * const _keywords[] = {"size", "flags", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02008073 static _PyArg_Parser _parser = {NULL, _keywords, "getrandom", 0};
8074 PyObject *argsbuf[2];
8075 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
Victor Stinner9b1f4742016-09-06 16:18:52 -07008076 Py_ssize_t size;
8077 int flags = 0;
8078
Serhiy Storchaka31913912019-03-14 10:32:22 +02008079 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
8080 if (!args) {
Victor Stinner9b1f4742016-09-06 16:18:52 -07008081 goto exit;
8082 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02008083 if (PyFloat_Check(args[0])) {
8084 PyErr_SetString(PyExc_TypeError,
8085 "integer argument expected, got float" );
8086 goto exit;
8087 }
8088 {
8089 Py_ssize_t ival = -1;
8090 PyObject *iobj = PyNumber_Index(args[0]);
8091 if (iobj != NULL) {
8092 ival = PyLong_AsSsize_t(iobj);
8093 Py_DECREF(iobj);
8094 }
8095 if (ival == -1 && PyErr_Occurred()) {
8096 goto exit;
8097 }
8098 size = ival;
8099 }
8100 if (!noptargs) {
8101 goto skip_optional_pos;
8102 }
8103 if (PyFloat_Check(args[1])) {
8104 PyErr_SetString(PyExc_TypeError,
8105 "integer argument expected, got float" );
8106 goto exit;
8107 }
8108 flags = _PyLong_AsInt(args[1]);
8109 if (flags == -1 && PyErr_Occurred()) {
8110 goto exit;
8111 }
8112skip_optional_pos:
Victor Stinner9b1f4742016-09-06 16:18:52 -07008113 return_value = os_getrandom_impl(module, size, flags);
8114
8115exit:
8116 return return_value;
8117}
8118
8119#endif /* defined(HAVE_GETRANDOM_SYSCALL) */
8120
Steve Dower2438cdf2019-03-29 16:37:16 -07008121#if defined(MS_WINDOWS)
8122
8123PyDoc_STRVAR(os__add_dll_directory__doc__,
8124"_add_dll_directory($module, /, path)\n"
8125"--\n"
8126"\n"
8127"Add a path to the DLL search path.\n"
8128"\n"
8129"This search path is used when resolving dependencies for imported\n"
8130"extension modules (the module itself is resolved through sys.path),\n"
8131"and also by ctypes.\n"
8132"\n"
8133"Returns an opaque value that may be passed to os.remove_dll_directory\n"
8134"to remove this directory from the search path.");
8135
8136#define OS__ADD_DLL_DIRECTORY_METHODDEF \
8137 {"_add_dll_directory", (PyCFunction)(void(*)(void))os__add_dll_directory, METH_FASTCALL|METH_KEYWORDS, os__add_dll_directory__doc__},
8138
8139static PyObject *
8140os__add_dll_directory_impl(PyObject *module, path_t *path);
8141
8142static PyObject *
8143os__add_dll_directory(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
8144{
8145 PyObject *return_value = NULL;
8146 static const char * const _keywords[] = {"path", NULL};
8147 static _PyArg_Parser _parser = {NULL, _keywords, "_add_dll_directory", 0};
8148 PyObject *argsbuf[1];
8149 path_t path = PATH_T_INITIALIZE("_add_dll_directory", "path", 0, 0);
8150
8151 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
8152 if (!args) {
8153 goto exit;
8154 }
8155 if (!path_converter(args[0], &path)) {
8156 goto exit;
8157 }
8158 return_value = os__add_dll_directory_impl(module, &path);
8159
8160exit:
8161 /* Cleanup for path */
8162 path_cleanup(&path);
8163
8164 return return_value;
8165}
8166
8167#endif /* defined(MS_WINDOWS) */
8168
8169#if defined(MS_WINDOWS)
8170
8171PyDoc_STRVAR(os__remove_dll_directory__doc__,
8172"_remove_dll_directory($module, /, cookie)\n"
8173"--\n"
8174"\n"
8175"Removes a path from the DLL search path.\n"
8176"\n"
8177"The parameter is an opaque value that was returned from\n"
8178"os.add_dll_directory. You can only remove directories that you added\n"
8179"yourself.");
8180
8181#define OS__REMOVE_DLL_DIRECTORY_METHODDEF \
8182 {"_remove_dll_directory", (PyCFunction)(void(*)(void))os__remove_dll_directory, METH_FASTCALL|METH_KEYWORDS, os__remove_dll_directory__doc__},
8183
8184static PyObject *
8185os__remove_dll_directory_impl(PyObject *module, PyObject *cookie);
8186
8187static PyObject *
8188os__remove_dll_directory(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
8189{
8190 PyObject *return_value = NULL;
8191 static const char * const _keywords[] = {"cookie", NULL};
8192 static _PyArg_Parser _parser = {NULL, _keywords, "_remove_dll_directory", 0};
8193 PyObject *argsbuf[1];
8194 PyObject *cookie;
8195
8196 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
8197 if (!args) {
8198 goto exit;
8199 }
8200 cookie = args[0];
8201 return_value = os__remove_dll_directory_impl(module, cookie);
8202
8203exit:
8204 return return_value;
8205}
8206
8207#endif /* defined(MS_WINDOWS) */
8208
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03008209#ifndef OS_TTYNAME_METHODDEF
8210 #define OS_TTYNAME_METHODDEF
8211#endif /* !defined(OS_TTYNAME_METHODDEF) */
8212
8213#ifndef OS_CTERMID_METHODDEF
8214 #define OS_CTERMID_METHODDEF
8215#endif /* !defined(OS_CTERMID_METHODDEF) */
8216
8217#ifndef OS_FCHDIR_METHODDEF
8218 #define OS_FCHDIR_METHODDEF
8219#endif /* !defined(OS_FCHDIR_METHODDEF) */
8220
8221#ifndef OS_FCHMOD_METHODDEF
8222 #define OS_FCHMOD_METHODDEF
8223#endif /* !defined(OS_FCHMOD_METHODDEF) */
8224
8225#ifndef OS_LCHMOD_METHODDEF
8226 #define OS_LCHMOD_METHODDEF
8227#endif /* !defined(OS_LCHMOD_METHODDEF) */
8228
8229#ifndef OS_CHFLAGS_METHODDEF
8230 #define OS_CHFLAGS_METHODDEF
8231#endif /* !defined(OS_CHFLAGS_METHODDEF) */
8232
8233#ifndef OS_LCHFLAGS_METHODDEF
8234 #define OS_LCHFLAGS_METHODDEF
8235#endif /* !defined(OS_LCHFLAGS_METHODDEF) */
8236
8237#ifndef OS_CHROOT_METHODDEF
8238 #define OS_CHROOT_METHODDEF
8239#endif /* !defined(OS_CHROOT_METHODDEF) */
8240
8241#ifndef OS_FSYNC_METHODDEF
8242 #define OS_FSYNC_METHODDEF
8243#endif /* !defined(OS_FSYNC_METHODDEF) */
8244
8245#ifndef OS_SYNC_METHODDEF
8246 #define OS_SYNC_METHODDEF
8247#endif /* !defined(OS_SYNC_METHODDEF) */
8248
8249#ifndef OS_FDATASYNC_METHODDEF
8250 #define OS_FDATASYNC_METHODDEF
8251#endif /* !defined(OS_FDATASYNC_METHODDEF) */
8252
8253#ifndef OS_CHOWN_METHODDEF
8254 #define OS_CHOWN_METHODDEF
8255#endif /* !defined(OS_CHOWN_METHODDEF) */
8256
8257#ifndef OS_FCHOWN_METHODDEF
8258 #define OS_FCHOWN_METHODDEF
8259#endif /* !defined(OS_FCHOWN_METHODDEF) */
8260
8261#ifndef OS_LCHOWN_METHODDEF
8262 #define OS_LCHOWN_METHODDEF
8263#endif /* !defined(OS_LCHOWN_METHODDEF) */
8264
8265#ifndef OS_LINK_METHODDEF
8266 #define OS_LINK_METHODDEF
8267#endif /* !defined(OS_LINK_METHODDEF) */
8268
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03008269#ifndef OS__GETFULLPATHNAME_METHODDEF
8270 #define OS__GETFULLPATHNAME_METHODDEF
8271#endif /* !defined(OS__GETFULLPATHNAME_METHODDEF) */
8272
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03008273#ifndef OS__GETFINALPATHNAME_METHODDEF
8274 #define OS__GETFINALPATHNAME_METHODDEF
8275#endif /* !defined(OS__GETFINALPATHNAME_METHODDEF) */
8276
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03008277#ifndef OS__ISDIR_METHODDEF
8278 #define OS__ISDIR_METHODDEF
8279#endif /* !defined(OS__ISDIR_METHODDEF) */
8280
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03008281#ifndef OS__GETVOLUMEPATHNAME_METHODDEF
8282 #define OS__GETVOLUMEPATHNAME_METHODDEF
8283#endif /* !defined(OS__GETVOLUMEPATHNAME_METHODDEF) */
8284
8285#ifndef OS_NICE_METHODDEF
8286 #define OS_NICE_METHODDEF
8287#endif /* !defined(OS_NICE_METHODDEF) */
8288
8289#ifndef OS_GETPRIORITY_METHODDEF
8290 #define OS_GETPRIORITY_METHODDEF
8291#endif /* !defined(OS_GETPRIORITY_METHODDEF) */
8292
8293#ifndef OS_SETPRIORITY_METHODDEF
8294 #define OS_SETPRIORITY_METHODDEF
8295#endif /* !defined(OS_SETPRIORITY_METHODDEF) */
8296
8297#ifndef OS_SYSTEM_METHODDEF
8298 #define OS_SYSTEM_METHODDEF
8299#endif /* !defined(OS_SYSTEM_METHODDEF) */
8300
8301#ifndef OS_UNAME_METHODDEF
8302 #define OS_UNAME_METHODDEF
8303#endif /* !defined(OS_UNAME_METHODDEF) */
8304
8305#ifndef OS_EXECV_METHODDEF
8306 #define OS_EXECV_METHODDEF
8307#endif /* !defined(OS_EXECV_METHODDEF) */
8308
8309#ifndef OS_EXECVE_METHODDEF
8310 #define OS_EXECVE_METHODDEF
8311#endif /* !defined(OS_EXECVE_METHODDEF) */
8312
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00008313#ifndef OS_POSIX_SPAWN_METHODDEF
8314 #define OS_POSIX_SPAWN_METHODDEF
8315#endif /* !defined(OS_POSIX_SPAWN_METHODDEF) */
8316
Joannah Nanjekye92b83222019-01-16 16:29:26 +03008317#ifndef OS_POSIX_SPAWNP_METHODDEF
8318 #define OS_POSIX_SPAWNP_METHODDEF
8319#endif /* !defined(OS_POSIX_SPAWNP_METHODDEF) */
8320
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03008321#ifndef OS_SPAWNV_METHODDEF
8322 #define OS_SPAWNV_METHODDEF
8323#endif /* !defined(OS_SPAWNV_METHODDEF) */
8324
8325#ifndef OS_SPAWNVE_METHODDEF
8326 #define OS_SPAWNVE_METHODDEF
8327#endif /* !defined(OS_SPAWNVE_METHODDEF) */
8328
Antoine Pitrou346cbd32017-05-27 17:50:54 +02008329#ifndef OS_REGISTER_AT_FORK_METHODDEF
8330 #define OS_REGISTER_AT_FORK_METHODDEF
8331#endif /* !defined(OS_REGISTER_AT_FORK_METHODDEF) */
8332
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03008333#ifndef OS_FORK1_METHODDEF
8334 #define OS_FORK1_METHODDEF
8335#endif /* !defined(OS_FORK1_METHODDEF) */
8336
8337#ifndef OS_FORK_METHODDEF
8338 #define OS_FORK_METHODDEF
8339#endif /* !defined(OS_FORK_METHODDEF) */
8340
8341#ifndef OS_SCHED_GET_PRIORITY_MAX_METHODDEF
8342 #define OS_SCHED_GET_PRIORITY_MAX_METHODDEF
8343#endif /* !defined(OS_SCHED_GET_PRIORITY_MAX_METHODDEF) */
8344
8345#ifndef OS_SCHED_GET_PRIORITY_MIN_METHODDEF
8346 #define OS_SCHED_GET_PRIORITY_MIN_METHODDEF
8347#endif /* !defined(OS_SCHED_GET_PRIORITY_MIN_METHODDEF) */
8348
8349#ifndef OS_SCHED_GETSCHEDULER_METHODDEF
8350 #define OS_SCHED_GETSCHEDULER_METHODDEF
8351#endif /* !defined(OS_SCHED_GETSCHEDULER_METHODDEF) */
8352
8353#ifndef OS_SCHED_SETSCHEDULER_METHODDEF
8354 #define OS_SCHED_SETSCHEDULER_METHODDEF
8355#endif /* !defined(OS_SCHED_SETSCHEDULER_METHODDEF) */
8356
8357#ifndef OS_SCHED_GETPARAM_METHODDEF
8358 #define OS_SCHED_GETPARAM_METHODDEF
8359#endif /* !defined(OS_SCHED_GETPARAM_METHODDEF) */
8360
8361#ifndef OS_SCHED_SETPARAM_METHODDEF
8362 #define OS_SCHED_SETPARAM_METHODDEF
8363#endif /* !defined(OS_SCHED_SETPARAM_METHODDEF) */
8364
8365#ifndef OS_SCHED_RR_GET_INTERVAL_METHODDEF
8366 #define OS_SCHED_RR_GET_INTERVAL_METHODDEF
8367#endif /* !defined(OS_SCHED_RR_GET_INTERVAL_METHODDEF) */
8368
8369#ifndef OS_SCHED_YIELD_METHODDEF
8370 #define OS_SCHED_YIELD_METHODDEF
8371#endif /* !defined(OS_SCHED_YIELD_METHODDEF) */
8372
8373#ifndef OS_SCHED_SETAFFINITY_METHODDEF
8374 #define OS_SCHED_SETAFFINITY_METHODDEF
8375#endif /* !defined(OS_SCHED_SETAFFINITY_METHODDEF) */
8376
8377#ifndef OS_SCHED_GETAFFINITY_METHODDEF
8378 #define OS_SCHED_GETAFFINITY_METHODDEF
8379#endif /* !defined(OS_SCHED_GETAFFINITY_METHODDEF) */
8380
8381#ifndef OS_OPENPTY_METHODDEF
8382 #define OS_OPENPTY_METHODDEF
8383#endif /* !defined(OS_OPENPTY_METHODDEF) */
8384
8385#ifndef OS_FORKPTY_METHODDEF
8386 #define OS_FORKPTY_METHODDEF
8387#endif /* !defined(OS_FORKPTY_METHODDEF) */
8388
8389#ifndef OS_GETEGID_METHODDEF
8390 #define OS_GETEGID_METHODDEF
8391#endif /* !defined(OS_GETEGID_METHODDEF) */
8392
8393#ifndef OS_GETEUID_METHODDEF
8394 #define OS_GETEUID_METHODDEF
8395#endif /* !defined(OS_GETEUID_METHODDEF) */
8396
8397#ifndef OS_GETGID_METHODDEF
8398 #define OS_GETGID_METHODDEF
8399#endif /* !defined(OS_GETGID_METHODDEF) */
8400
Berker Peksag39404992016-09-15 20:45:16 +03008401#ifndef OS_GETPID_METHODDEF
8402 #define OS_GETPID_METHODDEF
8403#endif /* !defined(OS_GETPID_METHODDEF) */
8404
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03008405#ifndef OS_GETGROUPS_METHODDEF
8406 #define OS_GETGROUPS_METHODDEF
8407#endif /* !defined(OS_GETGROUPS_METHODDEF) */
8408
8409#ifndef OS_GETPGID_METHODDEF
8410 #define OS_GETPGID_METHODDEF
8411#endif /* !defined(OS_GETPGID_METHODDEF) */
8412
8413#ifndef OS_GETPGRP_METHODDEF
8414 #define OS_GETPGRP_METHODDEF
8415#endif /* !defined(OS_GETPGRP_METHODDEF) */
8416
8417#ifndef OS_SETPGRP_METHODDEF
8418 #define OS_SETPGRP_METHODDEF
8419#endif /* !defined(OS_SETPGRP_METHODDEF) */
8420
8421#ifndef OS_GETPPID_METHODDEF
8422 #define OS_GETPPID_METHODDEF
8423#endif /* !defined(OS_GETPPID_METHODDEF) */
8424
8425#ifndef OS_GETLOGIN_METHODDEF
8426 #define OS_GETLOGIN_METHODDEF
8427#endif /* !defined(OS_GETLOGIN_METHODDEF) */
8428
8429#ifndef OS_GETUID_METHODDEF
8430 #define OS_GETUID_METHODDEF
8431#endif /* !defined(OS_GETUID_METHODDEF) */
8432
8433#ifndef OS_KILL_METHODDEF
8434 #define OS_KILL_METHODDEF
8435#endif /* !defined(OS_KILL_METHODDEF) */
8436
8437#ifndef OS_KILLPG_METHODDEF
8438 #define OS_KILLPG_METHODDEF
8439#endif /* !defined(OS_KILLPG_METHODDEF) */
8440
8441#ifndef OS_PLOCK_METHODDEF
8442 #define OS_PLOCK_METHODDEF
8443#endif /* !defined(OS_PLOCK_METHODDEF) */
8444
8445#ifndef OS_SETUID_METHODDEF
8446 #define OS_SETUID_METHODDEF
8447#endif /* !defined(OS_SETUID_METHODDEF) */
8448
8449#ifndef OS_SETEUID_METHODDEF
8450 #define OS_SETEUID_METHODDEF
8451#endif /* !defined(OS_SETEUID_METHODDEF) */
8452
8453#ifndef OS_SETEGID_METHODDEF
8454 #define OS_SETEGID_METHODDEF
8455#endif /* !defined(OS_SETEGID_METHODDEF) */
8456
8457#ifndef OS_SETREUID_METHODDEF
8458 #define OS_SETREUID_METHODDEF
8459#endif /* !defined(OS_SETREUID_METHODDEF) */
8460
8461#ifndef OS_SETREGID_METHODDEF
8462 #define OS_SETREGID_METHODDEF
8463#endif /* !defined(OS_SETREGID_METHODDEF) */
8464
8465#ifndef OS_SETGID_METHODDEF
8466 #define OS_SETGID_METHODDEF
8467#endif /* !defined(OS_SETGID_METHODDEF) */
8468
8469#ifndef OS_SETGROUPS_METHODDEF
8470 #define OS_SETGROUPS_METHODDEF
8471#endif /* !defined(OS_SETGROUPS_METHODDEF) */
8472
8473#ifndef OS_WAIT3_METHODDEF
8474 #define OS_WAIT3_METHODDEF
8475#endif /* !defined(OS_WAIT3_METHODDEF) */
8476
8477#ifndef OS_WAIT4_METHODDEF
8478 #define OS_WAIT4_METHODDEF
8479#endif /* !defined(OS_WAIT4_METHODDEF) */
8480
8481#ifndef OS_WAITID_METHODDEF
8482 #define OS_WAITID_METHODDEF
8483#endif /* !defined(OS_WAITID_METHODDEF) */
8484
8485#ifndef OS_WAITPID_METHODDEF
8486 #define OS_WAITPID_METHODDEF
8487#endif /* !defined(OS_WAITPID_METHODDEF) */
8488
8489#ifndef OS_WAIT_METHODDEF
8490 #define OS_WAIT_METHODDEF
8491#endif /* !defined(OS_WAIT_METHODDEF) */
8492
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03008493#ifndef OS_READLINK_METHODDEF
8494 #define OS_READLINK_METHODDEF
8495#endif /* !defined(OS_READLINK_METHODDEF) */
8496
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03008497#ifndef OS_SYMLINK_METHODDEF
8498 #define OS_SYMLINK_METHODDEF
8499#endif /* !defined(OS_SYMLINK_METHODDEF) */
8500
8501#ifndef OS_TIMES_METHODDEF
8502 #define OS_TIMES_METHODDEF
8503#endif /* !defined(OS_TIMES_METHODDEF) */
8504
8505#ifndef OS_GETSID_METHODDEF
8506 #define OS_GETSID_METHODDEF
8507#endif /* !defined(OS_GETSID_METHODDEF) */
8508
8509#ifndef OS_SETSID_METHODDEF
8510 #define OS_SETSID_METHODDEF
8511#endif /* !defined(OS_SETSID_METHODDEF) */
8512
8513#ifndef OS_SETPGID_METHODDEF
8514 #define OS_SETPGID_METHODDEF
8515#endif /* !defined(OS_SETPGID_METHODDEF) */
8516
8517#ifndef OS_TCGETPGRP_METHODDEF
8518 #define OS_TCGETPGRP_METHODDEF
8519#endif /* !defined(OS_TCGETPGRP_METHODDEF) */
8520
8521#ifndef OS_TCSETPGRP_METHODDEF
8522 #define OS_TCSETPGRP_METHODDEF
8523#endif /* !defined(OS_TCSETPGRP_METHODDEF) */
8524
8525#ifndef OS_LOCKF_METHODDEF
8526 #define OS_LOCKF_METHODDEF
8527#endif /* !defined(OS_LOCKF_METHODDEF) */
8528
8529#ifndef OS_READV_METHODDEF
8530 #define OS_READV_METHODDEF
8531#endif /* !defined(OS_READV_METHODDEF) */
8532
8533#ifndef OS_PREAD_METHODDEF
8534 #define OS_PREAD_METHODDEF
8535#endif /* !defined(OS_PREAD_METHODDEF) */
8536
Pablo Galindo4defba32018-01-27 16:16:37 +00008537#ifndef OS_PREADV_METHODDEF
8538 #define OS_PREADV_METHODDEF
8539#endif /* !defined(OS_PREADV_METHODDEF) */
8540
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02008541#ifndef OS__FCOPYFILE_METHODDEF
8542 #define OS__FCOPYFILE_METHODDEF
8543#endif /* !defined(OS__FCOPYFILE_METHODDEF) */
8544
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03008545#ifndef OS_PIPE_METHODDEF
8546 #define OS_PIPE_METHODDEF
8547#endif /* !defined(OS_PIPE_METHODDEF) */
8548
8549#ifndef OS_PIPE2_METHODDEF
8550 #define OS_PIPE2_METHODDEF
8551#endif /* !defined(OS_PIPE2_METHODDEF) */
8552
8553#ifndef OS_WRITEV_METHODDEF
8554 #define OS_WRITEV_METHODDEF
8555#endif /* !defined(OS_WRITEV_METHODDEF) */
8556
8557#ifndef OS_PWRITE_METHODDEF
8558 #define OS_PWRITE_METHODDEF
8559#endif /* !defined(OS_PWRITE_METHODDEF) */
8560
Pablo Galindo4defba32018-01-27 16:16:37 +00008561#ifndef OS_PWRITEV_METHODDEF
8562 #define OS_PWRITEV_METHODDEF
8563#endif /* !defined(OS_PWRITEV_METHODDEF) */
8564
Pablo Galindoaac4d032019-05-31 19:39:47 +01008565#ifndef OS_COPY_FILE_RANGE_METHODDEF
8566 #define OS_COPY_FILE_RANGE_METHODDEF
8567#endif /* !defined(OS_COPY_FILE_RANGE_METHODDEF) */
8568
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03008569#ifndef OS_MKFIFO_METHODDEF
8570 #define OS_MKFIFO_METHODDEF
8571#endif /* !defined(OS_MKFIFO_METHODDEF) */
8572
8573#ifndef OS_MKNOD_METHODDEF
8574 #define OS_MKNOD_METHODDEF
8575#endif /* !defined(OS_MKNOD_METHODDEF) */
8576
8577#ifndef OS_MAJOR_METHODDEF
8578 #define OS_MAJOR_METHODDEF
8579#endif /* !defined(OS_MAJOR_METHODDEF) */
8580
8581#ifndef OS_MINOR_METHODDEF
8582 #define OS_MINOR_METHODDEF
8583#endif /* !defined(OS_MINOR_METHODDEF) */
8584
8585#ifndef OS_MAKEDEV_METHODDEF
8586 #define OS_MAKEDEV_METHODDEF
8587#endif /* !defined(OS_MAKEDEV_METHODDEF) */
8588
8589#ifndef OS_FTRUNCATE_METHODDEF
8590 #define OS_FTRUNCATE_METHODDEF
8591#endif /* !defined(OS_FTRUNCATE_METHODDEF) */
8592
8593#ifndef OS_TRUNCATE_METHODDEF
8594 #define OS_TRUNCATE_METHODDEF
8595#endif /* !defined(OS_TRUNCATE_METHODDEF) */
8596
8597#ifndef OS_POSIX_FALLOCATE_METHODDEF
8598 #define OS_POSIX_FALLOCATE_METHODDEF
8599#endif /* !defined(OS_POSIX_FALLOCATE_METHODDEF) */
8600
8601#ifndef OS_POSIX_FADVISE_METHODDEF
8602 #define OS_POSIX_FADVISE_METHODDEF
8603#endif /* !defined(OS_POSIX_FADVISE_METHODDEF) */
8604
8605#ifndef OS_PUTENV_METHODDEF
8606 #define OS_PUTENV_METHODDEF
8607#endif /* !defined(OS_PUTENV_METHODDEF) */
8608
8609#ifndef OS_UNSETENV_METHODDEF
8610 #define OS_UNSETENV_METHODDEF
8611#endif /* !defined(OS_UNSETENV_METHODDEF) */
8612
8613#ifndef OS_WCOREDUMP_METHODDEF
8614 #define OS_WCOREDUMP_METHODDEF
8615#endif /* !defined(OS_WCOREDUMP_METHODDEF) */
8616
8617#ifndef OS_WIFCONTINUED_METHODDEF
8618 #define OS_WIFCONTINUED_METHODDEF
8619#endif /* !defined(OS_WIFCONTINUED_METHODDEF) */
8620
8621#ifndef OS_WIFSTOPPED_METHODDEF
8622 #define OS_WIFSTOPPED_METHODDEF
8623#endif /* !defined(OS_WIFSTOPPED_METHODDEF) */
8624
8625#ifndef OS_WIFSIGNALED_METHODDEF
8626 #define OS_WIFSIGNALED_METHODDEF
8627#endif /* !defined(OS_WIFSIGNALED_METHODDEF) */
8628
8629#ifndef OS_WIFEXITED_METHODDEF
8630 #define OS_WIFEXITED_METHODDEF
8631#endif /* !defined(OS_WIFEXITED_METHODDEF) */
8632
8633#ifndef OS_WEXITSTATUS_METHODDEF
8634 #define OS_WEXITSTATUS_METHODDEF
8635#endif /* !defined(OS_WEXITSTATUS_METHODDEF) */
8636
8637#ifndef OS_WTERMSIG_METHODDEF
8638 #define OS_WTERMSIG_METHODDEF
8639#endif /* !defined(OS_WTERMSIG_METHODDEF) */
8640
8641#ifndef OS_WSTOPSIG_METHODDEF
8642 #define OS_WSTOPSIG_METHODDEF
8643#endif /* !defined(OS_WSTOPSIG_METHODDEF) */
8644
8645#ifndef OS_FSTATVFS_METHODDEF
8646 #define OS_FSTATVFS_METHODDEF
8647#endif /* !defined(OS_FSTATVFS_METHODDEF) */
8648
8649#ifndef OS_STATVFS_METHODDEF
8650 #define OS_STATVFS_METHODDEF
8651#endif /* !defined(OS_STATVFS_METHODDEF) */
8652
8653#ifndef OS__GETDISKUSAGE_METHODDEF
8654 #define OS__GETDISKUSAGE_METHODDEF
8655#endif /* !defined(OS__GETDISKUSAGE_METHODDEF) */
8656
8657#ifndef OS_FPATHCONF_METHODDEF
8658 #define OS_FPATHCONF_METHODDEF
8659#endif /* !defined(OS_FPATHCONF_METHODDEF) */
8660
8661#ifndef OS_PATHCONF_METHODDEF
8662 #define OS_PATHCONF_METHODDEF
8663#endif /* !defined(OS_PATHCONF_METHODDEF) */
8664
8665#ifndef OS_CONFSTR_METHODDEF
8666 #define OS_CONFSTR_METHODDEF
8667#endif /* !defined(OS_CONFSTR_METHODDEF) */
8668
8669#ifndef OS_SYSCONF_METHODDEF
8670 #define OS_SYSCONF_METHODDEF
8671#endif /* !defined(OS_SYSCONF_METHODDEF) */
8672
Steve Dowercc16be82016-09-08 10:35:16 -07008673#ifndef OS_STARTFILE_METHODDEF
8674 #define OS_STARTFILE_METHODDEF
8675#endif /* !defined(OS_STARTFILE_METHODDEF) */
8676
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03008677#ifndef OS_GETLOADAVG_METHODDEF
8678 #define OS_GETLOADAVG_METHODDEF
8679#endif /* !defined(OS_GETLOADAVG_METHODDEF) */
8680
8681#ifndef OS_SETRESUID_METHODDEF
8682 #define OS_SETRESUID_METHODDEF
8683#endif /* !defined(OS_SETRESUID_METHODDEF) */
8684
8685#ifndef OS_SETRESGID_METHODDEF
8686 #define OS_SETRESGID_METHODDEF
8687#endif /* !defined(OS_SETRESGID_METHODDEF) */
8688
8689#ifndef OS_GETRESUID_METHODDEF
8690 #define OS_GETRESUID_METHODDEF
8691#endif /* !defined(OS_GETRESUID_METHODDEF) */
8692
8693#ifndef OS_GETRESGID_METHODDEF
8694 #define OS_GETRESGID_METHODDEF
8695#endif /* !defined(OS_GETRESGID_METHODDEF) */
8696
8697#ifndef OS_GETXATTR_METHODDEF
8698 #define OS_GETXATTR_METHODDEF
8699#endif /* !defined(OS_GETXATTR_METHODDEF) */
8700
8701#ifndef OS_SETXATTR_METHODDEF
8702 #define OS_SETXATTR_METHODDEF
8703#endif /* !defined(OS_SETXATTR_METHODDEF) */
8704
8705#ifndef OS_REMOVEXATTR_METHODDEF
8706 #define OS_REMOVEXATTR_METHODDEF
8707#endif /* !defined(OS_REMOVEXATTR_METHODDEF) */
8708
8709#ifndef OS_LISTXATTR_METHODDEF
8710 #define OS_LISTXATTR_METHODDEF
8711#endif /* !defined(OS_LISTXATTR_METHODDEF) */
8712
Zackery Spytz43fdbd22019-05-29 13:57:07 -06008713#ifndef OS_MEMFD_CREATE_METHODDEF
8714 #define OS_MEMFD_CREATE_METHODDEF
8715#endif /* !defined(OS_MEMFD_CREATE_METHODDEF) */
8716
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03008717#ifndef OS_GET_HANDLE_INHERITABLE_METHODDEF
8718 #define OS_GET_HANDLE_INHERITABLE_METHODDEF
8719#endif /* !defined(OS_GET_HANDLE_INHERITABLE_METHODDEF) */
8720
8721#ifndef OS_SET_HANDLE_INHERITABLE_METHODDEF
8722 #define OS_SET_HANDLE_INHERITABLE_METHODDEF
8723#endif /* !defined(OS_SET_HANDLE_INHERITABLE_METHODDEF) */
Victor Stinner9b1f4742016-09-06 16:18:52 -07008724
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03008725#ifndef OS_GET_BLOCKING_METHODDEF
8726 #define OS_GET_BLOCKING_METHODDEF
8727#endif /* !defined(OS_GET_BLOCKING_METHODDEF) */
8728
8729#ifndef OS_SET_BLOCKING_METHODDEF
8730 #define OS_SET_BLOCKING_METHODDEF
8731#endif /* !defined(OS_SET_BLOCKING_METHODDEF) */
8732
Victor Stinner9b1f4742016-09-06 16:18:52 -07008733#ifndef OS_GETRANDOM_METHODDEF
8734 #define OS_GETRANDOM_METHODDEF
8735#endif /* !defined(OS_GETRANDOM_METHODDEF) */
Steve Dower2438cdf2019-03-29 16:37:16 -07008736
8737#ifndef OS__ADD_DLL_DIRECTORY_METHODDEF
8738 #define OS__ADD_DLL_DIRECTORY_METHODDEF
8739#endif /* !defined(OS__ADD_DLL_DIRECTORY_METHODDEF) */
8740
8741#ifndef OS__REMOVE_DLL_DIRECTORY_METHODDEF
8742 #define OS__REMOVE_DLL_DIRECTORY_METHODDEF
8743#endif /* !defined(OS__REMOVE_DLL_DIRECTORY_METHODDEF) */
Pablo Galindoaac4d032019-05-31 19:39:47 +01008744/*[clinic end generated code: output=b3ae8afd275ea5cd input=a9049054013a1b77]*/