blob: c4ebe3996aa731f424f5426e2736a5fda1fa734a [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
1275PyDoc_STRVAR(os__getvolumepathname__doc__,
1276"_getvolumepathname($module, /, path)\n"
1277"--\n"
1278"\n"
1279"A helper function for ismount on Win32.");
1280
1281#define OS__GETVOLUMEPATHNAME_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02001282 {"_getvolumepathname", (PyCFunction)(void(*)(void))os__getvolumepathname, METH_FASTCALL|METH_KEYWORDS, os__getvolumepathname__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001283
1284static PyObject *
Steve Dower23ad6d02018-02-22 10:39:10 -08001285os__getvolumepathname_impl(PyObject *module, path_t *path);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001286
1287static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001288os__getvolumepathname(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001289{
1290 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001291 static const char * const _keywords[] = {"path", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02001292 static _PyArg_Parser _parser = {NULL, _keywords, "_getvolumepathname", 0};
1293 PyObject *argsbuf[1];
Steve Dower23ad6d02018-02-22 10:39:10 -08001294 path_t path = PATH_T_INITIALIZE("_getvolumepathname", "path", 0, 0);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001295
Serhiy Storchaka31913912019-03-14 10:32:22 +02001296 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
1297 if (!args) {
1298 goto exit;
1299 }
1300 if (!path_converter(args[0], &path)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001301 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001302 }
Steve Dower23ad6d02018-02-22 10:39:10 -08001303 return_value = os__getvolumepathname_impl(module, &path);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001304
1305exit:
Steve Dower23ad6d02018-02-22 10:39:10 -08001306 /* Cleanup for path */
1307 path_cleanup(&path);
1308
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001309 return return_value;
1310}
1311
1312#endif /* defined(MS_WINDOWS) */
1313
1314PyDoc_STRVAR(os_mkdir__doc__,
1315"mkdir($module, /, path, mode=511, *, dir_fd=None)\n"
1316"--\n"
1317"\n"
1318"Create a directory.\n"
1319"\n"
1320"If dir_fd is not None, it should be a file descriptor open to a directory,\n"
1321" and path should be relative; path will then be relative to that directory.\n"
1322"dir_fd may not be implemented on your platform.\n"
1323" If it is unavailable, using it will raise a NotImplementedError.\n"
1324"\n"
1325"The mode argument is ignored on Windows.");
1326
1327#define OS_MKDIR_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02001328 {"mkdir", (PyCFunction)(void(*)(void))os_mkdir, METH_FASTCALL|METH_KEYWORDS, os_mkdir__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001329
1330static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001331os_mkdir_impl(PyObject *module, path_t *path, int mode, int dir_fd);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001332
1333static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001334os_mkdir(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001335{
1336 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001337 static const char * const _keywords[] = {"path", "mode", "dir_fd", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02001338 static _PyArg_Parser _parser = {NULL, _keywords, "mkdir", 0};
1339 PyObject *argsbuf[3];
1340 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001341 path_t path = PATH_T_INITIALIZE("mkdir", "path", 0, 0);
1342 int mode = 511;
1343 int dir_fd = DEFAULT_DIR_FD;
1344
Serhiy Storchaka31913912019-03-14 10:32:22 +02001345 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
1346 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001347 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001348 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02001349 if (!path_converter(args[0], &path)) {
1350 goto exit;
1351 }
1352 if (!noptargs) {
1353 goto skip_optional_pos;
1354 }
1355 if (args[1]) {
1356 if (PyFloat_Check(args[1])) {
1357 PyErr_SetString(PyExc_TypeError,
1358 "integer argument expected, got float" );
1359 goto exit;
1360 }
1361 mode = _PyLong_AsInt(args[1]);
1362 if (mode == -1 && PyErr_Occurred()) {
1363 goto exit;
1364 }
1365 if (!--noptargs) {
1366 goto skip_optional_pos;
1367 }
1368 }
1369skip_optional_pos:
1370 if (!noptargs) {
1371 goto skip_optional_kwonly;
1372 }
1373 if (!MKDIRAT_DIR_FD_CONVERTER(args[2], &dir_fd)) {
1374 goto exit;
1375 }
1376skip_optional_kwonly:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001377 return_value = os_mkdir_impl(module, &path, mode, dir_fd);
1378
1379exit:
1380 /* Cleanup for path */
1381 path_cleanup(&path);
1382
1383 return return_value;
1384}
1385
1386#if defined(HAVE_NICE)
1387
1388PyDoc_STRVAR(os_nice__doc__,
1389"nice($module, increment, /)\n"
1390"--\n"
1391"\n"
1392"Add increment to the priority of process and return the new priority.");
1393
1394#define OS_NICE_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03001395 {"nice", (PyCFunction)os_nice, METH_O, os_nice__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001396
1397static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001398os_nice_impl(PyObject *module, int increment);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001399
1400static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001401os_nice(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001402{
1403 PyObject *return_value = NULL;
1404 int increment;
1405
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02001406 if (PyFloat_Check(arg)) {
1407 PyErr_SetString(PyExc_TypeError,
1408 "integer argument expected, got float" );
1409 goto exit;
1410 }
1411 increment = _PyLong_AsInt(arg);
1412 if (increment == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001413 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001414 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001415 return_value = os_nice_impl(module, increment);
1416
1417exit:
1418 return return_value;
1419}
1420
1421#endif /* defined(HAVE_NICE) */
1422
1423#if defined(HAVE_GETPRIORITY)
1424
1425PyDoc_STRVAR(os_getpriority__doc__,
1426"getpriority($module, /, which, who)\n"
1427"--\n"
1428"\n"
1429"Return program scheduling priority.");
1430
1431#define OS_GETPRIORITY_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02001432 {"getpriority", (PyCFunction)(void(*)(void))os_getpriority, METH_FASTCALL|METH_KEYWORDS, os_getpriority__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001433
1434static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001435os_getpriority_impl(PyObject *module, int which, int who);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001436
1437static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001438os_getpriority(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001439{
1440 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001441 static const char * const _keywords[] = {"which", "who", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02001442 static _PyArg_Parser _parser = {NULL, _keywords, "getpriority", 0};
1443 PyObject *argsbuf[2];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001444 int which;
1445 int who;
1446
Serhiy Storchaka31913912019-03-14 10:32:22 +02001447 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf);
1448 if (!args) {
1449 goto exit;
1450 }
1451 if (PyFloat_Check(args[0])) {
1452 PyErr_SetString(PyExc_TypeError,
1453 "integer argument expected, got float" );
1454 goto exit;
1455 }
1456 which = _PyLong_AsInt(args[0]);
1457 if (which == -1 && PyErr_Occurred()) {
1458 goto exit;
1459 }
1460 if (PyFloat_Check(args[1])) {
1461 PyErr_SetString(PyExc_TypeError,
1462 "integer argument expected, got float" );
1463 goto exit;
1464 }
1465 who = _PyLong_AsInt(args[1]);
1466 if (who == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001467 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001468 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001469 return_value = os_getpriority_impl(module, which, who);
1470
1471exit:
1472 return return_value;
1473}
1474
1475#endif /* defined(HAVE_GETPRIORITY) */
1476
1477#if defined(HAVE_SETPRIORITY)
1478
1479PyDoc_STRVAR(os_setpriority__doc__,
1480"setpriority($module, /, which, who, priority)\n"
1481"--\n"
1482"\n"
1483"Set program scheduling priority.");
1484
1485#define OS_SETPRIORITY_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02001486 {"setpriority", (PyCFunction)(void(*)(void))os_setpriority, METH_FASTCALL|METH_KEYWORDS, os_setpriority__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001487
1488static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001489os_setpriority_impl(PyObject *module, int which, int who, int priority);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001490
1491static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001492os_setpriority(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001493{
1494 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001495 static const char * const _keywords[] = {"which", "who", "priority", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02001496 static _PyArg_Parser _parser = {NULL, _keywords, "setpriority", 0};
1497 PyObject *argsbuf[3];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001498 int which;
1499 int who;
1500 int priority;
1501
Serhiy Storchaka31913912019-03-14 10:32:22 +02001502 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 3, 3, 0, argsbuf);
1503 if (!args) {
1504 goto exit;
1505 }
1506 if (PyFloat_Check(args[0])) {
1507 PyErr_SetString(PyExc_TypeError,
1508 "integer argument expected, got float" );
1509 goto exit;
1510 }
1511 which = _PyLong_AsInt(args[0]);
1512 if (which == -1 && PyErr_Occurred()) {
1513 goto exit;
1514 }
1515 if (PyFloat_Check(args[1])) {
1516 PyErr_SetString(PyExc_TypeError,
1517 "integer argument expected, got float" );
1518 goto exit;
1519 }
1520 who = _PyLong_AsInt(args[1]);
1521 if (who == -1 && PyErr_Occurred()) {
1522 goto exit;
1523 }
1524 if (PyFloat_Check(args[2])) {
1525 PyErr_SetString(PyExc_TypeError,
1526 "integer argument expected, got float" );
1527 goto exit;
1528 }
1529 priority = _PyLong_AsInt(args[2]);
1530 if (priority == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001531 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001532 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001533 return_value = os_setpriority_impl(module, which, who, priority);
1534
1535exit:
1536 return return_value;
1537}
1538
1539#endif /* defined(HAVE_SETPRIORITY) */
1540
1541PyDoc_STRVAR(os_rename__doc__,
1542"rename($module, /, src, dst, *, src_dir_fd=None, dst_dir_fd=None)\n"
1543"--\n"
1544"\n"
1545"Rename a file or directory.\n"
1546"\n"
1547"If either src_dir_fd or dst_dir_fd is not None, it should be a file\n"
1548" descriptor open to a directory, and the respective path string (src or dst)\n"
1549" should be relative; the path will then be relative to that directory.\n"
1550"src_dir_fd and dst_dir_fd, may not be implemented on your platform.\n"
1551" If they are unavailable, using them will raise a NotImplementedError.");
1552
1553#define OS_RENAME_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02001554 {"rename", (PyCFunction)(void(*)(void))os_rename, METH_FASTCALL|METH_KEYWORDS, os_rename__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001555
1556static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001557os_rename_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04001558 int dst_dir_fd);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001559
1560static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001561os_rename(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001562{
1563 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001564 static const char * const _keywords[] = {"src", "dst", "src_dir_fd", "dst_dir_fd", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02001565 static _PyArg_Parser _parser = {NULL, _keywords, "rename", 0};
1566 PyObject *argsbuf[4];
1567 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001568 path_t src = PATH_T_INITIALIZE("rename", "src", 0, 0);
1569 path_t dst = PATH_T_INITIALIZE("rename", "dst", 0, 0);
1570 int src_dir_fd = DEFAULT_DIR_FD;
1571 int dst_dir_fd = DEFAULT_DIR_FD;
1572
Serhiy Storchaka31913912019-03-14 10:32:22 +02001573 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf);
1574 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001575 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001576 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02001577 if (!path_converter(args[0], &src)) {
1578 goto exit;
1579 }
1580 if (!path_converter(args[1], &dst)) {
1581 goto exit;
1582 }
1583 if (!noptargs) {
1584 goto skip_optional_kwonly;
1585 }
1586 if (args[2]) {
1587 if (!dir_fd_converter(args[2], &src_dir_fd)) {
1588 goto exit;
1589 }
1590 if (!--noptargs) {
1591 goto skip_optional_kwonly;
1592 }
1593 }
1594 if (!dir_fd_converter(args[3], &dst_dir_fd)) {
1595 goto exit;
1596 }
1597skip_optional_kwonly:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001598 return_value = os_rename_impl(module, &src, &dst, src_dir_fd, dst_dir_fd);
1599
1600exit:
1601 /* Cleanup for src */
1602 path_cleanup(&src);
1603 /* Cleanup for dst */
1604 path_cleanup(&dst);
1605
1606 return return_value;
1607}
1608
1609PyDoc_STRVAR(os_replace__doc__,
1610"replace($module, /, src, dst, *, src_dir_fd=None, dst_dir_fd=None)\n"
1611"--\n"
1612"\n"
1613"Rename a file or directory, overwriting the destination.\n"
1614"\n"
1615"If either src_dir_fd or dst_dir_fd is not None, it should be a file\n"
1616" descriptor open to a directory, and the respective path string (src or dst)\n"
1617" should be relative; the path will then be relative to that directory.\n"
1618"src_dir_fd and dst_dir_fd, may not be implemented on your platform.\n"
Anthony Sottile73d60022019-02-12 23:15:54 -05001619" If they are unavailable, using them will raise a NotImplementedError.");
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001620
1621#define OS_REPLACE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02001622 {"replace", (PyCFunction)(void(*)(void))os_replace, METH_FASTCALL|METH_KEYWORDS, os_replace__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001623
1624static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001625os_replace_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
1626 int dst_dir_fd);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001627
1628static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001629os_replace(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001630{
1631 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001632 static const char * const _keywords[] = {"src", "dst", "src_dir_fd", "dst_dir_fd", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02001633 static _PyArg_Parser _parser = {NULL, _keywords, "replace", 0};
1634 PyObject *argsbuf[4];
1635 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001636 path_t src = PATH_T_INITIALIZE("replace", "src", 0, 0);
1637 path_t dst = PATH_T_INITIALIZE("replace", "dst", 0, 0);
1638 int src_dir_fd = DEFAULT_DIR_FD;
1639 int dst_dir_fd = DEFAULT_DIR_FD;
1640
Serhiy Storchaka31913912019-03-14 10:32:22 +02001641 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf);
1642 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001643 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001644 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02001645 if (!path_converter(args[0], &src)) {
1646 goto exit;
1647 }
1648 if (!path_converter(args[1], &dst)) {
1649 goto exit;
1650 }
1651 if (!noptargs) {
1652 goto skip_optional_kwonly;
1653 }
1654 if (args[2]) {
1655 if (!dir_fd_converter(args[2], &src_dir_fd)) {
1656 goto exit;
1657 }
1658 if (!--noptargs) {
1659 goto skip_optional_kwonly;
1660 }
1661 }
1662 if (!dir_fd_converter(args[3], &dst_dir_fd)) {
1663 goto exit;
1664 }
1665skip_optional_kwonly:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001666 return_value = os_replace_impl(module, &src, &dst, src_dir_fd, dst_dir_fd);
1667
1668exit:
1669 /* Cleanup for src */
1670 path_cleanup(&src);
1671 /* Cleanup for dst */
1672 path_cleanup(&dst);
1673
1674 return return_value;
1675}
1676
1677PyDoc_STRVAR(os_rmdir__doc__,
1678"rmdir($module, /, path, *, dir_fd=None)\n"
1679"--\n"
1680"\n"
1681"Remove a directory.\n"
1682"\n"
1683"If dir_fd is not None, it should be a file descriptor open to a directory,\n"
1684" and path should be relative; path will then be relative to that directory.\n"
1685"dir_fd may not be implemented on your platform.\n"
1686" If it is unavailable, using it will raise a NotImplementedError.");
1687
1688#define OS_RMDIR_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02001689 {"rmdir", (PyCFunction)(void(*)(void))os_rmdir, METH_FASTCALL|METH_KEYWORDS, os_rmdir__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001690
1691static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001692os_rmdir_impl(PyObject *module, path_t *path, int dir_fd);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001693
1694static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001695os_rmdir(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001696{
1697 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001698 static const char * const _keywords[] = {"path", "dir_fd", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02001699 static _PyArg_Parser _parser = {NULL, _keywords, "rmdir", 0};
1700 PyObject *argsbuf[2];
1701 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001702 path_t path = PATH_T_INITIALIZE("rmdir", "path", 0, 0);
1703 int dir_fd = DEFAULT_DIR_FD;
1704
Serhiy Storchaka31913912019-03-14 10:32:22 +02001705 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
1706 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001707 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001708 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02001709 if (!path_converter(args[0], &path)) {
1710 goto exit;
1711 }
1712 if (!noptargs) {
1713 goto skip_optional_kwonly;
1714 }
1715 if (!UNLINKAT_DIR_FD_CONVERTER(args[1], &dir_fd)) {
1716 goto exit;
1717 }
1718skip_optional_kwonly:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001719 return_value = os_rmdir_impl(module, &path, dir_fd);
1720
1721exit:
1722 /* Cleanup for path */
1723 path_cleanup(&path);
1724
1725 return return_value;
1726}
1727
1728#if defined(HAVE_SYSTEM) && defined(MS_WINDOWS)
1729
1730PyDoc_STRVAR(os_system__doc__,
1731"system($module, /, command)\n"
1732"--\n"
1733"\n"
1734"Execute the command in a subshell.");
1735
1736#define OS_SYSTEM_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02001737 {"system", (PyCFunction)(void(*)(void))os_system, METH_FASTCALL|METH_KEYWORDS, os_system__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001738
1739static long
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001740os_system_impl(PyObject *module, const Py_UNICODE *command);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001741
1742static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001743os_system(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001744{
1745 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001746 static const char * const _keywords[] = {"command", NULL};
1747 static _PyArg_Parser _parser = {"u:system", _keywords, 0};
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001748 const Py_UNICODE *command;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001749 long _return_value;
1750
Victor Stinner3e1fad62017-01-17 01:29:01 +01001751 if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001752 &command)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001753 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001754 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001755 _return_value = os_system_impl(module, command);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001756 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001757 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001758 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001759 return_value = PyLong_FromLong(_return_value);
1760
1761exit:
1762 return return_value;
1763}
1764
1765#endif /* defined(HAVE_SYSTEM) && defined(MS_WINDOWS) */
1766
1767#if defined(HAVE_SYSTEM) && !defined(MS_WINDOWS)
1768
1769PyDoc_STRVAR(os_system__doc__,
1770"system($module, /, command)\n"
1771"--\n"
1772"\n"
1773"Execute the command in a subshell.");
1774
1775#define OS_SYSTEM_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02001776 {"system", (PyCFunction)(void(*)(void))os_system, METH_FASTCALL|METH_KEYWORDS, os_system__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001777
1778static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001779os_system_impl(PyObject *module, PyObject *command);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001780
1781static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001782os_system(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001783{
1784 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001785 static const char * const _keywords[] = {"command", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02001786 static _PyArg_Parser _parser = {NULL, _keywords, "system", 0};
1787 PyObject *argsbuf[1];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001788 PyObject *command = NULL;
1789 long _return_value;
1790
Serhiy Storchaka31913912019-03-14 10:32:22 +02001791 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
1792 if (!args) {
1793 goto exit;
1794 }
1795 if (!PyUnicode_FSConverter(args[0], &command)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001796 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001797 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001798 _return_value = os_system_impl(module, command);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001799 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001800 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001801 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001802 return_value = PyLong_FromLong(_return_value);
1803
1804exit:
1805 /* Cleanup for command */
1806 Py_XDECREF(command);
1807
1808 return return_value;
1809}
1810
1811#endif /* defined(HAVE_SYSTEM) && !defined(MS_WINDOWS) */
1812
1813PyDoc_STRVAR(os_umask__doc__,
1814"umask($module, mask, /)\n"
1815"--\n"
1816"\n"
1817"Set the current numeric umask and return the previous umask.");
1818
1819#define OS_UMASK_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03001820 {"umask", (PyCFunction)os_umask, METH_O, os_umask__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001821
1822static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001823os_umask_impl(PyObject *module, int mask);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001824
1825static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001826os_umask(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001827{
1828 PyObject *return_value = NULL;
1829 int mask;
1830
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02001831 if (PyFloat_Check(arg)) {
1832 PyErr_SetString(PyExc_TypeError,
1833 "integer argument expected, got float" );
1834 goto exit;
1835 }
1836 mask = _PyLong_AsInt(arg);
1837 if (mask == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001838 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001839 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001840 return_value = os_umask_impl(module, mask);
1841
1842exit:
1843 return return_value;
1844}
1845
1846PyDoc_STRVAR(os_unlink__doc__,
1847"unlink($module, /, path, *, dir_fd=None)\n"
1848"--\n"
1849"\n"
1850"Remove a file (same as remove()).\n"
1851"\n"
1852"If dir_fd is not None, it should be a file descriptor open to a directory,\n"
1853" and path should be relative; path will then be relative to that directory.\n"
1854"dir_fd may not be implemented on your platform.\n"
1855" If it is unavailable, using it will raise a NotImplementedError.");
1856
1857#define OS_UNLINK_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02001858 {"unlink", (PyCFunction)(void(*)(void))os_unlink, METH_FASTCALL|METH_KEYWORDS, os_unlink__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001859
1860static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001861os_unlink_impl(PyObject *module, path_t *path, int dir_fd);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001862
1863static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001864os_unlink(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001865{
1866 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001867 static const char * const _keywords[] = {"path", "dir_fd", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02001868 static _PyArg_Parser _parser = {NULL, _keywords, "unlink", 0};
1869 PyObject *argsbuf[2];
1870 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001871 path_t path = PATH_T_INITIALIZE("unlink", "path", 0, 0);
1872 int dir_fd = DEFAULT_DIR_FD;
1873
Serhiy Storchaka31913912019-03-14 10:32:22 +02001874 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
1875 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001876 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001877 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02001878 if (!path_converter(args[0], &path)) {
1879 goto exit;
1880 }
1881 if (!noptargs) {
1882 goto skip_optional_kwonly;
1883 }
1884 if (!UNLINKAT_DIR_FD_CONVERTER(args[1], &dir_fd)) {
1885 goto exit;
1886 }
1887skip_optional_kwonly:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001888 return_value = os_unlink_impl(module, &path, dir_fd);
1889
1890exit:
1891 /* Cleanup for path */
1892 path_cleanup(&path);
1893
1894 return return_value;
1895}
1896
1897PyDoc_STRVAR(os_remove__doc__,
1898"remove($module, /, path, *, dir_fd=None)\n"
1899"--\n"
1900"\n"
1901"Remove a file (same as unlink()).\n"
1902"\n"
1903"If dir_fd is not None, it should be a file descriptor open to a directory,\n"
1904" and path should be relative; path will then be relative to that directory.\n"
1905"dir_fd may not be implemented on your platform.\n"
1906" If it is unavailable, using it will raise a NotImplementedError.");
1907
1908#define OS_REMOVE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02001909 {"remove", (PyCFunction)(void(*)(void))os_remove, METH_FASTCALL|METH_KEYWORDS, os_remove__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001910
1911static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001912os_remove_impl(PyObject *module, path_t *path, int dir_fd);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001913
1914static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001915os_remove(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001916{
1917 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001918 static const char * const _keywords[] = {"path", "dir_fd", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02001919 static _PyArg_Parser _parser = {NULL, _keywords, "remove", 0};
1920 PyObject *argsbuf[2];
1921 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001922 path_t path = PATH_T_INITIALIZE("remove", "path", 0, 0);
1923 int dir_fd = DEFAULT_DIR_FD;
1924
Serhiy Storchaka31913912019-03-14 10:32:22 +02001925 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
1926 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001927 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001928 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02001929 if (!path_converter(args[0], &path)) {
1930 goto exit;
1931 }
1932 if (!noptargs) {
1933 goto skip_optional_kwonly;
1934 }
1935 if (!UNLINKAT_DIR_FD_CONVERTER(args[1], &dir_fd)) {
1936 goto exit;
1937 }
1938skip_optional_kwonly:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001939 return_value = os_remove_impl(module, &path, dir_fd);
1940
1941exit:
1942 /* Cleanup for path */
1943 path_cleanup(&path);
1944
1945 return return_value;
1946}
1947
1948#if defined(HAVE_UNAME)
1949
1950PyDoc_STRVAR(os_uname__doc__,
1951"uname($module, /)\n"
1952"--\n"
1953"\n"
1954"Return an object identifying the current operating system.\n"
1955"\n"
1956"The object behaves like a named tuple with the following fields:\n"
1957" (sysname, nodename, release, version, machine)");
1958
1959#define OS_UNAME_METHODDEF \
1960 {"uname", (PyCFunction)os_uname, METH_NOARGS, os_uname__doc__},
1961
1962static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001963os_uname_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001964
1965static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001966os_uname(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001967{
1968 return os_uname_impl(module);
1969}
1970
1971#endif /* defined(HAVE_UNAME) */
1972
1973PyDoc_STRVAR(os_utime__doc__,
1974"utime($module, /, path, times=None, *, ns=None, dir_fd=None,\n"
1975" follow_symlinks=True)\n"
1976"--\n"
1977"\n"
1978"Set the access and modified time of path.\n"
1979"\n"
1980"path may always be specified as a string.\n"
1981"On some platforms, path may also be specified as an open file descriptor.\n"
1982" If this functionality is unavailable, using it raises an exception.\n"
1983"\n"
1984"If times is not None, it must be a tuple (atime, mtime);\n"
1985" atime and mtime should be expressed as float seconds since the epoch.\n"
Martin Panter0ff89092015-09-09 01:56:53 +00001986"If ns is specified, it must be a tuple (atime_ns, mtime_ns);\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001987" atime_ns and mtime_ns should be expressed as integer nanoseconds\n"
1988" since the epoch.\n"
Martin Panter0ff89092015-09-09 01:56:53 +00001989"If times is None and ns is unspecified, utime uses the current time.\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001990"Specifying tuples for both times and ns is an error.\n"
1991"\n"
1992"If dir_fd is not None, it should be a file descriptor open to a directory,\n"
1993" and path should be relative; path will then be relative to that directory.\n"
1994"If follow_symlinks is False, and the last element of the path is a symbolic\n"
1995" link, utime will modify the symbolic link itself instead of the file the\n"
1996" link points to.\n"
1997"It is an error to use dir_fd or follow_symlinks when specifying path\n"
1998" as an open file descriptor.\n"
1999"dir_fd and follow_symlinks may not be available on your platform.\n"
2000" If they are unavailable, using them will raise a NotImplementedError.");
2001
2002#define OS_UTIME_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02002003 {"utime", (PyCFunction)(void(*)(void))os_utime, METH_FASTCALL|METH_KEYWORDS, os_utime__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002004
2005static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002006os_utime_impl(PyObject *module, path_t *path, PyObject *times, PyObject *ns,
2007 int dir_fd, int follow_symlinks);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002008
2009static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02002010os_utime(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002011{
2012 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002013 static const char * const _keywords[] = {"path", "times", "ns", "dir_fd", "follow_symlinks", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02002014 static _PyArg_Parser _parser = {NULL, _keywords, "utime", 0};
2015 PyObject *argsbuf[5];
2016 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002017 path_t path = PATH_T_INITIALIZE("utime", "path", 0, PATH_UTIME_HAVE_FD);
2018 PyObject *times = NULL;
2019 PyObject *ns = NULL;
2020 int dir_fd = DEFAULT_DIR_FD;
2021 int follow_symlinks = 1;
2022
Serhiy Storchaka31913912019-03-14 10:32:22 +02002023 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
2024 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002025 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03002026 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02002027 if (!path_converter(args[0], &path)) {
2028 goto exit;
2029 }
2030 if (!noptargs) {
2031 goto skip_optional_pos;
2032 }
2033 if (args[1]) {
2034 times = args[1];
2035 if (!--noptargs) {
2036 goto skip_optional_pos;
2037 }
2038 }
2039skip_optional_pos:
2040 if (!noptargs) {
2041 goto skip_optional_kwonly;
2042 }
2043 if (args[2]) {
2044 ns = args[2];
2045 if (!--noptargs) {
2046 goto skip_optional_kwonly;
2047 }
2048 }
2049 if (args[3]) {
2050 if (!FUTIMENSAT_DIR_FD_CONVERTER(args[3], &dir_fd)) {
2051 goto exit;
2052 }
2053 if (!--noptargs) {
2054 goto skip_optional_kwonly;
2055 }
2056 }
2057 follow_symlinks = PyObject_IsTrue(args[4]);
2058 if (follow_symlinks < 0) {
2059 goto exit;
2060 }
2061skip_optional_kwonly:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002062 return_value = os_utime_impl(module, &path, times, ns, dir_fd, follow_symlinks);
2063
2064exit:
2065 /* Cleanup for path */
2066 path_cleanup(&path);
2067
2068 return return_value;
2069}
2070
2071PyDoc_STRVAR(os__exit__doc__,
2072"_exit($module, /, status)\n"
2073"--\n"
2074"\n"
2075"Exit to the system with specified status, without normal exit processing.");
2076
2077#define OS__EXIT_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02002078 {"_exit", (PyCFunction)(void(*)(void))os__exit, METH_FASTCALL|METH_KEYWORDS, os__exit__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002079
2080static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002081os__exit_impl(PyObject *module, int status);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002082
2083static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02002084os__exit(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002085{
2086 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002087 static const char * const _keywords[] = {"status", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02002088 static _PyArg_Parser _parser = {NULL, _keywords, "_exit", 0};
2089 PyObject *argsbuf[1];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002090 int status;
2091
Serhiy Storchaka31913912019-03-14 10:32:22 +02002092 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
2093 if (!args) {
2094 goto exit;
2095 }
2096 if (PyFloat_Check(args[0])) {
2097 PyErr_SetString(PyExc_TypeError,
2098 "integer argument expected, got float" );
2099 goto exit;
2100 }
2101 status = _PyLong_AsInt(args[0]);
2102 if (status == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002103 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03002104 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002105 return_value = os__exit_impl(module, status);
2106
2107exit:
2108 return return_value;
2109}
2110
2111#if defined(HAVE_EXECV)
2112
2113PyDoc_STRVAR(os_execv__doc__,
2114"execv($module, path, argv, /)\n"
2115"--\n"
2116"\n"
2117"Execute an executable path with arguments, replacing current process.\n"
2118"\n"
2119" path\n"
2120" Path of executable file.\n"
2121" argv\n"
2122" Tuple or list of strings.");
2123
2124#define OS_EXECV_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02002125 {"execv", (PyCFunction)(void(*)(void))os_execv, METH_FASTCALL, os_execv__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002126
2127static PyObject *
Steve Dowercc16be82016-09-08 10:35:16 -07002128os_execv_impl(PyObject *module, path_t *path, PyObject *argv);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002129
2130static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02002131os_execv(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002132{
2133 PyObject *return_value = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07002134 path_t path = PATH_T_INITIALIZE("execv", "path", 0, 0);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002135 PyObject *argv;
2136
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002137 if (!_PyArg_CheckPositional("execv", nargs, 2, 2)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01002138 goto exit;
2139 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002140 if (!path_converter(args[0], &path)) {
2141 goto exit;
2142 }
2143 argv = args[1];
Steve Dowercc16be82016-09-08 10:35:16 -07002144 return_value = os_execv_impl(module, &path, argv);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002145
2146exit:
2147 /* Cleanup for path */
Steve Dowercc16be82016-09-08 10:35:16 -07002148 path_cleanup(&path);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002149
2150 return return_value;
2151}
2152
2153#endif /* defined(HAVE_EXECV) */
2154
2155#if defined(HAVE_EXECV)
2156
2157PyDoc_STRVAR(os_execve__doc__,
2158"execve($module, /, path, argv, env)\n"
2159"--\n"
2160"\n"
2161"Execute an executable path with arguments, replacing current process.\n"
2162"\n"
2163" path\n"
2164" Path of executable file.\n"
2165" argv\n"
2166" Tuple or list of strings.\n"
2167" env\n"
2168" Dictionary of strings mapping to strings.");
2169
2170#define OS_EXECVE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02002171 {"execve", (PyCFunction)(void(*)(void))os_execve, METH_FASTCALL|METH_KEYWORDS, os_execve__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002172
2173static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002174os_execve_impl(PyObject *module, path_t *path, PyObject *argv, PyObject *env);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002175
2176static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02002177os_execve(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002178{
2179 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002180 static const char * const _keywords[] = {"path", "argv", "env", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02002181 static _PyArg_Parser _parser = {NULL, _keywords, "execve", 0};
2182 PyObject *argsbuf[3];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002183 path_t path = PATH_T_INITIALIZE("execve", "path", 0, PATH_HAVE_FEXECVE);
2184 PyObject *argv;
2185 PyObject *env;
2186
Serhiy Storchaka31913912019-03-14 10:32:22 +02002187 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 3, 3, 0, argsbuf);
2188 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002189 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03002190 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02002191 if (!path_converter(args[0], &path)) {
2192 goto exit;
2193 }
2194 argv = args[1];
2195 env = args[2];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002196 return_value = os_execve_impl(module, &path, argv, env);
2197
2198exit:
2199 /* Cleanup for path */
2200 path_cleanup(&path);
2201
2202 return return_value;
2203}
2204
2205#endif /* defined(HAVE_EXECV) */
2206
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00002207#if defined(HAVE_POSIX_SPAWN)
2208
2209PyDoc_STRVAR(os_posix_spawn__doc__,
Serhiy Storchakad700f972018-09-08 14:48:18 +03002210"posix_spawn($module, path, argv, env, /, *, file_actions=(),\n"
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03002211" setpgroup=None, resetids=False, setsid=False,\n"
2212" setsigmask=(), setsigdef=(), scheduler=None)\n"
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00002213"--\n"
2214"\n"
2215"Execute the program specified by path in a new process.\n"
2216"\n"
2217" path\n"
2218" Path of executable file.\n"
2219" argv\n"
2220" Tuple or list of strings.\n"
2221" env\n"
2222" Dictionary of strings mapping to strings.\n"
2223" file_actions\n"
Pablo Galindo254a4662018-09-07 16:44:24 +01002224" A sequence of file action tuples.\n"
2225" setpgroup\n"
2226" The pgroup to use with the POSIX_SPAWN_SETPGROUP flag.\n"
2227" resetids\n"
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03002228" If the value is `true` the POSIX_SPAWN_RESETIDS will be activated.\n"
2229" setsid\n"
2230" If the value is `true` the POSIX_SPAWN_SETSID or POSIX_SPAWN_SETSID_NP will be activated.\n"
Pablo Galindo254a4662018-09-07 16:44:24 +01002231" setsigmask\n"
2232" The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag.\n"
2233" setsigdef\n"
2234" The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag.\n"
2235" scheduler\n"
2236" A tuple with the scheduler policy (optional) and parameters.");
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00002237
2238#define OS_POSIX_SPAWN_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02002239 {"posix_spawn", (PyCFunction)(void(*)(void))os_posix_spawn, METH_FASTCALL|METH_KEYWORDS, os_posix_spawn__doc__},
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00002240
2241static PyObject *
2242os_posix_spawn_impl(PyObject *module, path_t *path, PyObject *argv,
Pablo Galindo254a4662018-09-07 16:44:24 +01002243 PyObject *env, PyObject *file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03002244 PyObject *setpgroup, int resetids, int setsid,
2245 PyObject *setsigmask, PyObject *setsigdef,
2246 PyObject *scheduler);
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00002247
2248static PyObject *
Pablo Galindo254a4662018-09-07 16:44:24 +01002249os_posix_spawn(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00002250{
2251 PyObject *return_value = NULL;
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03002252 static const char * const _keywords[] = {"", "", "", "file_actions", "setpgroup", "resetids", "setsid", "setsigmask", "setsigdef", "scheduler", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02002253 static _PyArg_Parser _parser = {NULL, _keywords, "posix_spawn", 0};
2254 PyObject *argsbuf[10];
2255 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 3;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00002256 path_t path = PATH_T_INITIALIZE("posix_spawn", "path", 0, 0);
2257 PyObject *argv;
2258 PyObject *env;
Serhiy Storchakad700f972018-09-08 14:48:18 +03002259 PyObject *file_actions = NULL;
Pablo Galindo254a4662018-09-07 16:44:24 +01002260 PyObject *setpgroup = NULL;
2261 int resetids = 0;
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03002262 int setsid = 0;
Pablo Galindo254a4662018-09-07 16:44:24 +01002263 PyObject *setsigmask = NULL;
2264 PyObject *setsigdef = NULL;
2265 PyObject *scheduler = NULL;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00002266
Serhiy Storchaka31913912019-03-14 10:32:22 +02002267 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 3, 3, 0, argsbuf);
2268 if (!args) {
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00002269 goto exit;
2270 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02002271 if (!path_converter(args[0], &path)) {
2272 goto exit;
2273 }
2274 argv = args[1];
2275 env = args[2];
2276 if (!noptargs) {
2277 goto skip_optional_kwonly;
2278 }
2279 if (args[3]) {
2280 file_actions = args[3];
2281 if (!--noptargs) {
2282 goto skip_optional_kwonly;
2283 }
2284 }
2285 if (args[4]) {
2286 setpgroup = args[4];
2287 if (!--noptargs) {
2288 goto skip_optional_kwonly;
2289 }
2290 }
2291 if (args[5]) {
2292 if (PyFloat_Check(args[5])) {
2293 PyErr_SetString(PyExc_TypeError,
2294 "integer argument expected, got float" );
2295 goto exit;
2296 }
2297 resetids = _PyLong_AsInt(args[5]);
2298 if (resetids == -1 && PyErr_Occurred()) {
2299 goto exit;
2300 }
2301 if (!--noptargs) {
2302 goto skip_optional_kwonly;
2303 }
2304 }
2305 if (args[6]) {
2306 if (PyFloat_Check(args[6])) {
2307 PyErr_SetString(PyExc_TypeError,
2308 "integer argument expected, got float" );
2309 goto exit;
2310 }
2311 setsid = _PyLong_AsInt(args[6]);
2312 if (setsid == -1 && PyErr_Occurred()) {
2313 goto exit;
2314 }
2315 if (!--noptargs) {
2316 goto skip_optional_kwonly;
2317 }
2318 }
2319 if (args[7]) {
2320 setsigmask = args[7];
2321 if (!--noptargs) {
2322 goto skip_optional_kwonly;
2323 }
2324 }
2325 if (args[8]) {
2326 setsigdef = args[8];
2327 if (!--noptargs) {
2328 goto skip_optional_kwonly;
2329 }
2330 }
2331 scheduler = args[9];
2332skip_optional_kwonly:
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03002333 return_value = os_posix_spawn_impl(module, &path, argv, env, file_actions, setpgroup, resetids, setsid, setsigmask, setsigdef, scheduler);
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00002334
2335exit:
2336 /* Cleanup for path */
2337 path_cleanup(&path);
2338
2339 return return_value;
2340}
2341
2342#endif /* defined(HAVE_POSIX_SPAWN) */
2343
Joannah Nanjekye92b83222019-01-16 16:29:26 +03002344#if defined(HAVE_POSIX_SPAWNP)
2345
2346PyDoc_STRVAR(os_posix_spawnp__doc__,
2347"posix_spawnp($module, path, argv, env, /, *, file_actions=(),\n"
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03002348" setpgroup=None, resetids=False, setsid=False,\n"
2349" setsigmask=(), setsigdef=(), scheduler=None)\n"
Joannah Nanjekye92b83222019-01-16 16:29:26 +03002350"--\n"
2351"\n"
2352"Execute the program specified by path in a new process.\n"
2353"\n"
2354" path\n"
2355" Path of executable file.\n"
2356" argv\n"
2357" Tuple or list of strings.\n"
2358" env\n"
2359" Dictionary of strings mapping to strings.\n"
2360" file_actions\n"
2361" A sequence of file action tuples.\n"
2362" setpgroup\n"
2363" The pgroup to use with the POSIX_SPAWN_SETPGROUP flag.\n"
2364" resetids\n"
2365" If the value is `True` the POSIX_SPAWN_RESETIDS will be activated.\n"
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03002366" setsid\n"
2367" If the value is `True` the POSIX_SPAWN_SETSID or POSIX_SPAWN_SETSID_NP will be activated.\n"
Joannah Nanjekye92b83222019-01-16 16:29:26 +03002368" setsigmask\n"
2369" The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag.\n"
2370" setsigdef\n"
2371" The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag.\n"
2372" scheduler\n"
2373" A tuple with the scheduler policy (optional) and parameters.");
2374
2375#define OS_POSIX_SPAWNP_METHODDEF \
2376 {"posix_spawnp", (PyCFunction)(void(*)(void))os_posix_spawnp, METH_FASTCALL|METH_KEYWORDS, os_posix_spawnp__doc__},
2377
2378static PyObject *
2379os_posix_spawnp_impl(PyObject *module, path_t *path, PyObject *argv,
2380 PyObject *env, PyObject *file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03002381 PyObject *setpgroup, int resetids, int setsid,
2382 PyObject *setsigmask, PyObject *setsigdef,
2383 PyObject *scheduler);
Joannah Nanjekye92b83222019-01-16 16:29:26 +03002384
2385static PyObject *
2386os_posix_spawnp(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
2387{
2388 PyObject *return_value = NULL;
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03002389 static const char * const _keywords[] = {"", "", "", "file_actions", "setpgroup", "resetids", "setsid", "setsigmask", "setsigdef", "scheduler", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02002390 static _PyArg_Parser _parser = {NULL, _keywords, "posix_spawnp", 0};
2391 PyObject *argsbuf[10];
2392 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 3;
Joannah Nanjekye92b83222019-01-16 16:29:26 +03002393 path_t path = PATH_T_INITIALIZE("posix_spawnp", "path", 0, 0);
2394 PyObject *argv;
2395 PyObject *env;
2396 PyObject *file_actions = NULL;
2397 PyObject *setpgroup = NULL;
2398 int resetids = 0;
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03002399 int setsid = 0;
Joannah Nanjekye92b83222019-01-16 16:29:26 +03002400 PyObject *setsigmask = NULL;
2401 PyObject *setsigdef = NULL;
2402 PyObject *scheduler = NULL;
2403
Serhiy Storchaka31913912019-03-14 10:32:22 +02002404 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 3, 3, 0, argsbuf);
2405 if (!args) {
Joannah Nanjekye92b83222019-01-16 16:29:26 +03002406 goto exit;
2407 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02002408 if (!path_converter(args[0], &path)) {
2409 goto exit;
2410 }
2411 argv = args[1];
2412 env = args[2];
2413 if (!noptargs) {
2414 goto skip_optional_kwonly;
2415 }
2416 if (args[3]) {
2417 file_actions = args[3];
2418 if (!--noptargs) {
2419 goto skip_optional_kwonly;
2420 }
2421 }
2422 if (args[4]) {
2423 setpgroup = args[4];
2424 if (!--noptargs) {
2425 goto skip_optional_kwonly;
2426 }
2427 }
2428 if (args[5]) {
2429 if (PyFloat_Check(args[5])) {
2430 PyErr_SetString(PyExc_TypeError,
2431 "integer argument expected, got float" );
2432 goto exit;
2433 }
2434 resetids = _PyLong_AsInt(args[5]);
2435 if (resetids == -1 && PyErr_Occurred()) {
2436 goto exit;
2437 }
2438 if (!--noptargs) {
2439 goto skip_optional_kwonly;
2440 }
2441 }
2442 if (args[6]) {
2443 if (PyFloat_Check(args[6])) {
2444 PyErr_SetString(PyExc_TypeError,
2445 "integer argument expected, got float" );
2446 goto exit;
2447 }
2448 setsid = _PyLong_AsInt(args[6]);
2449 if (setsid == -1 && PyErr_Occurred()) {
2450 goto exit;
2451 }
2452 if (!--noptargs) {
2453 goto skip_optional_kwonly;
2454 }
2455 }
2456 if (args[7]) {
2457 setsigmask = args[7];
2458 if (!--noptargs) {
2459 goto skip_optional_kwonly;
2460 }
2461 }
2462 if (args[8]) {
2463 setsigdef = args[8];
2464 if (!--noptargs) {
2465 goto skip_optional_kwonly;
2466 }
2467 }
2468 scheduler = args[9];
2469skip_optional_kwonly:
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03002470 return_value = os_posix_spawnp_impl(module, &path, argv, env, file_actions, setpgroup, resetids, setsid, setsigmask, setsigdef, scheduler);
Joannah Nanjekye92b83222019-01-16 16:29:26 +03002471
2472exit:
2473 /* Cleanup for path */
2474 path_cleanup(&path);
2475
2476 return return_value;
2477}
2478
2479#endif /* defined(HAVE_POSIX_SPAWNP) */
2480
pxinwrf2d7ac72019-05-21 18:46:37 +08002481#if (defined(HAVE_SPAWNV) || defined(HAVE_WSPAWNV) || defined(HAVE_RTPSPAWN))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002482
2483PyDoc_STRVAR(os_spawnv__doc__,
2484"spawnv($module, mode, path, argv, /)\n"
2485"--\n"
2486"\n"
2487"Execute the program specified by path in a new process.\n"
2488"\n"
2489" mode\n"
2490" Mode of process creation.\n"
2491" path\n"
2492" Path of executable file.\n"
2493" argv\n"
2494" Tuple or list of strings.");
2495
2496#define OS_SPAWNV_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02002497 {"spawnv", (PyCFunction)(void(*)(void))os_spawnv, METH_FASTCALL, os_spawnv__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002498
2499static PyObject *
Steve Dowercc16be82016-09-08 10:35:16 -07002500os_spawnv_impl(PyObject *module, int mode, path_t *path, PyObject *argv);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002501
2502static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02002503os_spawnv(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002504{
2505 PyObject *return_value = NULL;
2506 int mode;
Steve Dowercc16be82016-09-08 10:35:16 -07002507 path_t path = PATH_T_INITIALIZE("spawnv", "path", 0, 0);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002508 PyObject *argv;
2509
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002510 if (!_PyArg_CheckPositional("spawnv", nargs, 3, 3)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01002511 goto exit;
2512 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002513 if (PyFloat_Check(args[0])) {
2514 PyErr_SetString(PyExc_TypeError,
2515 "integer argument expected, got float" );
2516 goto exit;
2517 }
2518 mode = _PyLong_AsInt(args[0]);
2519 if (mode == -1 && PyErr_Occurred()) {
2520 goto exit;
2521 }
2522 if (!path_converter(args[1], &path)) {
2523 goto exit;
2524 }
2525 argv = args[2];
Steve Dowercc16be82016-09-08 10:35:16 -07002526 return_value = os_spawnv_impl(module, mode, &path, argv);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002527
2528exit:
2529 /* Cleanup for path */
Steve Dowercc16be82016-09-08 10:35:16 -07002530 path_cleanup(&path);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002531
2532 return return_value;
2533}
2534
pxinwrf2d7ac72019-05-21 18:46:37 +08002535#endif /* (defined(HAVE_SPAWNV) || defined(HAVE_WSPAWNV) || defined(HAVE_RTPSPAWN)) */
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002536
pxinwrf2d7ac72019-05-21 18:46:37 +08002537#if (defined(HAVE_SPAWNV) || defined(HAVE_WSPAWNV) || defined(HAVE_RTPSPAWN))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002538
2539PyDoc_STRVAR(os_spawnve__doc__,
2540"spawnve($module, mode, path, argv, env, /)\n"
2541"--\n"
2542"\n"
2543"Execute the program specified by path in a new process.\n"
2544"\n"
2545" mode\n"
2546" Mode of process creation.\n"
2547" path\n"
2548" Path of executable file.\n"
2549" argv\n"
2550" Tuple or list of strings.\n"
2551" env\n"
2552" Dictionary of strings mapping to strings.");
2553
2554#define OS_SPAWNVE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02002555 {"spawnve", (PyCFunction)(void(*)(void))os_spawnve, METH_FASTCALL, os_spawnve__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002556
2557static PyObject *
Steve Dowercc16be82016-09-08 10:35:16 -07002558os_spawnve_impl(PyObject *module, int mode, path_t *path, PyObject *argv,
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002559 PyObject *env);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002560
2561static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02002562os_spawnve(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002563{
2564 PyObject *return_value = NULL;
2565 int mode;
Steve Dowercc16be82016-09-08 10:35:16 -07002566 path_t path = PATH_T_INITIALIZE("spawnve", "path", 0, 0);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002567 PyObject *argv;
2568 PyObject *env;
2569
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002570 if (!_PyArg_CheckPositional("spawnve", nargs, 4, 4)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01002571 goto exit;
2572 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002573 if (PyFloat_Check(args[0])) {
2574 PyErr_SetString(PyExc_TypeError,
2575 "integer argument expected, got float" );
2576 goto exit;
2577 }
2578 mode = _PyLong_AsInt(args[0]);
2579 if (mode == -1 && PyErr_Occurred()) {
2580 goto exit;
2581 }
2582 if (!path_converter(args[1], &path)) {
2583 goto exit;
2584 }
2585 argv = args[2];
2586 env = args[3];
Steve Dowercc16be82016-09-08 10:35:16 -07002587 return_value = os_spawnve_impl(module, mode, &path, argv, env);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002588
2589exit:
2590 /* Cleanup for path */
Steve Dowercc16be82016-09-08 10:35:16 -07002591 path_cleanup(&path);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002592
2593 return return_value;
2594}
2595
pxinwrf2d7ac72019-05-21 18:46:37 +08002596#endif /* (defined(HAVE_SPAWNV) || defined(HAVE_WSPAWNV) || defined(HAVE_RTPSPAWN)) */
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002597
Antoine Pitrou346cbd32017-05-27 17:50:54 +02002598#if defined(HAVE_FORK)
2599
2600PyDoc_STRVAR(os_register_at_fork__doc__,
Gregory P. Smith163468a2017-05-29 10:03:41 -07002601"register_at_fork($module, /, *, before=None, after_in_child=None,\n"
2602" after_in_parent=None)\n"
Antoine Pitrou346cbd32017-05-27 17:50:54 +02002603"--\n"
2604"\n"
Gregory P. Smith163468a2017-05-29 10:03:41 -07002605"Register callables to be called when forking a new process.\n"
Antoine Pitrou346cbd32017-05-27 17:50:54 +02002606"\n"
Gregory P. Smith163468a2017-05-29 10:03:41 -07002607" before\n"
2608" A callable to be called in the parent before the fork() syscall.\n"
2609" after_in_child\n"
2610" A callable to be called in the child after fork().\n"
2611" after_in_parent\n"
2612" A callable to be called in the parent after fork().\n"
Antoine Pitrou346cbd32017-05-27 17:50:54 +02002613"\n"
Gregory P. Smith163468a2017-05-29 10:03:41 -07002614"\'before\' callbacks are called in reverse order.\n"
2615"\'after_in_child\' and \'after_in_parent\' callbacks are called in order.");
Antoine Pitrou346cbd32017-05-27 17:50:54 +02002616
2617#define OS_REGISTER_AT_FORK_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02002618 {"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 +02002619
2620static PyObject *
Gregory P. Smith163468a2017-05-29 10:03:41 -07002621os_register_at_fork_impl(PyObject *module, PyObject *before,
2622 PyObject *after_in_child, PyObject *after_in_parent);
Antoine Pitrou346cbd32017-05-27 17:50:54 +02002623
2624static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02002625os_register_at_fork(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Antoine Pitrou346cbd32017-05-27 17:50:54 +02002626{
2627 PyObject *return_value = NULL;
Gregory P. Smith163468a2017-05-29 10:03:41 -07002628 static const char * const _keywords[] = {"before", "after_in_child", "after_in_parent", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02002629 static _PyArg_Parser _parser = {NULL, _keywords, "register_at_fork", 0};
2630 PyObject *argsbuf[3];
2631 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
Gregory P. Smith163468a2017-05-29 10:03:41 -07002632 PyObject *before = NULL;
2633 PyObject *after_in_child = NULL;
2634 PyObject *after_in_parent = NULL;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02002635
Serhiy Storchaka31913912019-03-14 10:32:22 +02002636 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 0, 0, argsbuf);
2637 if (!args) {
Antoine Pitrou346cbd32017-05-27 17:50:54 +02002638 goto exit;
2639 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02002640 if (!noptargs) {
2641 goto skip_optional_kwonly;
2642 }
2643 if (args[0]) {
2644 before = args[0];
2645 if (!--noptargs) {
2646 goto skip_optional_kwonly;
2647 }
2648 }
2649 if (args[1]) {
2650 after_in_child = args[1];
2651 if (!--noptargs) {
2652 goto skip_optional_kwonly;
2653 }
2654 }
2655 after_in_parent = args[2];
2656skip_optional_kwonly:
Gregory P. Smith163468a2017-05-29 10:03:41 -07002657 return_value = os_register_at_fork_impl(module, before, after_in_child, after_in_parent);
Antoine Pitrou346cbd32017-05-27 17:50:54 +02002658
2659exit:
2660 return return_value;
2661}
2662
2663#endif /* defined(HAVE_FORK) */
2664
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002665#if defined(HAVE_FORK1)
2666
2667PyDoc_STRVAR(os_fork1__doc__,
2668"fork1($module, /)\n"
2669"--\n"
2670"\n"
2671"Fork a child process with a single multiplexed (i.e., not bound) thread.\n"
2672"\n"
2673"Return 0 to child process and PID of child to parent process.");
2674
2675#define OS_FORK1_METHODDEF \
2676 {"fork1", (PyCFunction)os_fork1, METH_NOARGS, os_fork1__doc__},
2677
2678static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002679os_fork1_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002680
2681static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002682os_fork1(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002683{
2684 return os_fork1_impl(module);
2685}
2686
2687#endif /* defined(HAVE_FORK1) */
2688
2689#if defined(HAVE_FORK)
2690
2691PyDoc_STRVAR(os_fork__doc__,
2692"fork($module, /)\n"
2693"--\n"
2694"\n"
2695"Fork a child process.\n"
2696"\n"
2697"Return 0 to child process and PID of child to parent process.");
2698
2699#define OS_FORK_METHODDEF \
2700 {"fork", (PyCFunction)os_fork, METH_NOARGS, os_fork__doc__},
2701
2702static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002703os_fork_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002704
2705static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002706os_fork(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002707{
2708 return os_fork_impl(module);
2709}
2710
2711#endif /* defined(HAVE_FORK) */
2712
2713#if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_GET_PRIORITY_MAX)
2714
2715PyDoc_STRVAR(os_sched_get_priority_max__doc__,
2716"sched_get_priority_max($module, /, policy)\n"
2717"--\n"
2718"\n"
2719"Get the maximum scheduling priority for policy.");
2720
2721#define OS_SCHED_GET_PRIORITY_MAX_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02002722 {"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 +03002723
2724static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002725os_sched_get_priority_max_impl(PyObject *module, int policy);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002726
2727static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02002728os_sched_get_priority_max(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002729{
2730 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002731 static const char * const _keywords[] = {"policy", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02002732 static _PyArg_Parser _parser = {NULL, _keywords, "sched_get_priority_max", 0};
2733 PyObject *argsbuf[1];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002734 int policy;
2735
Serhiy Storchaka31913912019-03-14 10:32:22 +02002736 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
2737 if (!args) {
2738 goto exit;
2739 }
2740 if (PyFloat_Check(args[0])) {
2741 PyErr_SetString(PyExc_TypeError,
2742 "integer argument expected, got float" );
2743 goto exit;
2744 }
2745 policy = _PyLong_AsInt(args[0]);
2746 if (policy == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002747 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03002748 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002749 return_value = os_sched_get_priority_max_impl(module, policy);
2750
2751exit:
2752 return return_value;
2753}
2754
2755#endif /* defined(HAVE_SCHED_H) && defined(HAVE_SCHED_GET_PRIORITY_MAX) */
2756
2757#if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_GET_PRIORITY_MAX)
2758
2759PyDoc_STRVAR(os_sched_get_priority_min__doc__,
2760"sched_get_priority_min($module, /, policy)\n"
2761"--\n"
2762"\n"
2763"Get the minimum scheduling priority for policy.");
2764
2765#define OS_SCHED_GET_PRIORITY_MIN_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02002766 {"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 +03002767
2768static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002769os_sched_get_priority_min_impl(PyObject *module, int policy);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002770
2771static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02002772os_sched_get_priority_min(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002773{
2774 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002775 static const char * const _keywords[] = {"policy", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02002776 static _PyArg_Parser _parser = {NULL, _keywords, "sched_get_priority_min", 0};
2777 PyObject *argsbuf[1];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002778 int policy;
2779
Serhiy Storchaka31913912019-03-14 10:32:22 +02002780 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
2781 if (!args) {
2782 goto exit;
2783 }
2784 if (PyFloat_Check(args[0])) {
2785 PyErr_SetString(PyExc_TypeError,
2786 "integer argument expected, got float" );
2787 goto exit;
2788 }
2789 policy = _PyLong_AsInt(args[0]);
2790 if (policy == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002791 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03002792 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002793 return_value = os_sched_get_priority_min_impl(module, policy);
2794
2795exit:
2796 return return_value;
2797}
2798
2799#endif /* defined(HAVE_SCHED_H) && defined(HAVE_SCHED_GET_PRIORITY_MAX) */
2800
2801#if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_SETSCHEDULER)
2802
2803PyDoc_STRVAR(os_sched_getscheduler__doc__,
2804"sched_getscheduler($module, pid, /)\n"
2805"--\n"
2806"\n"
Min ho Kimc4cacc82019-07-31 08:16:13 +10002807"Get the scheduling policy for the process identified by pid.\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002808"\n"
2809"Passing 0 for pid returns the scheduling policy for the calling process.");
2810
2811#define OS_SCHED_GETSCHEDULER_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03002812 {"sched_getscheduler", (PyCFunction)os_sched_getscheduler, METH_O, os_sched_getscheduler__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002813
2814static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002815os_sched_getscheduler_impl(PyObject *module, pid_t pid);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002816
2817static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002818os_sched_getscheduler(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002819{
2820 PyObject *return_value = NULL;
2821 pid_t pid;
2822
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03002823 if (!PyArg_Parse(arg, "" _Py_PARSE_PID ":sched_getscheduler", &pid)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002824 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03002825 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002826 return_value = os_sched_getscheduler_impl(module, pid);
2827
2828exit:
2829 return return_value;
2830}
2831
2832#endif /* defined(HAVE_SCHED_H) && defined(HAVE_SCHED_SETSCHEDULER) */
2833
William Orr81574b82018-10-01 22:19:56 -07002834#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 +03002835
2836PyDoc_STRVAR(os_sched_param__doc__,
2837"sched_param(sched_priority)\n"
2838"--\n"
2839"\n"
2840"Current has only one field: sched_priority\");\n"
2841"\n"
2842" sched_priority\n"
2843" A scheduling parameter.");
2844
2845static PyObject *
2846os_sched_param_impl(PyTypeObject *type, PyObject *sched_priority);
2847
2848static PyObject *
2849os_sched_param(PyTypeObject *type, PyObject *args, PyObject *kwargs)
2850{
2851 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002852 static const char * const _keywords[] = {"sched_priority", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02002853 static _PyArg_Parser _parser = {NULL, _keywords, "sched_param", 0};
2854 PyObject *argsbuf[1];
2855 PyObject * const *fastargs;
2856 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002857 PyObject *sched_priority;
2858
Serhiy Storchaka31913912019-03-14 10:32:22 +02002859 fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 1, 1, 0, argsbuf);
2860 if (!fastargs) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002861 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03002862 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02002863 sched_priority = fastargs[0];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002864 return_value = os_sched_param_impl(type, sched_priority);
2865
2866exit:
2867 return return_value;
2868}
2869
William Orr81574b82018-10-01 22:19:56 -07002870#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 +03002871
2872#if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_SETSCHEDULER)
2873
2874PyDoc_STRVAR(os_sched_setscheduler__doc__,
2875"sched_setscheduler($module, pid, policy, param, /)\n"
2876"--\n"
2877"\n"
2878"Set the scheduling policy for the process identified by pid.\n"
2879"\n"
2880"If pid is 0, the calling process is changed.\n"
2881"param is an instance of sched_param.");
2882
2883#define OS_SCHED_SETSCHEDULER_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02002884 {"sched_setscheduler", (PyCFunction)(void(*)(void))os_sched_setscheduler, METH_FASTCALL, os_sched_setscheduler__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002885
2886static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002887os_sched_setscheduler_impl(PyObject *module, pid_t pid, int policy,
Larry Hastings89964c42015-04-14 18:07:59 -04002888 struct sched_param *param);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002889
2890static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02002891os_sched_setscheduler(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002892{
2893 PyObject *return_value = NULL;
2894 pid_t pid;
2895 int policy;
2896 struct sched_param param;
2897
Sylvain74453812017-06-10 06:51:48 +02002898 if (!_PyArg_ParseStack(args, nargs, "" _Py_PARSE_PID "iO&:sched_setscheduler",
2899 &pid, &policy, convert_sched_param, &param)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01002900 goto exit;
2901 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002902 return_value = os_sched_setscheduler_impl(module, pid, policy, &param);
2903
2904exit:
2905 return return_value;
2906}
2907
2908#endif /* defined(HAVE_SCHED_H) && defined(HAVE_SCHED_SETSCHEDULER) */
2909
2910#if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_SETPARAM)
2911
2912PyDoc_STRVAR(os_sched_getparam__doc__,
2913"sched_getparam($module, pid, /)\n"
2914"--\n"
2915"\n"
2916"Returns scheduling parameters for the process identified by pid.\n"
2917"\n"
2918"If pid is 0, returns parameters for the calling process.\n"
2919"Return value is an instance of sched_param.");
2920
2921#define OS_SCHED_GETPARAM_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03002922 {"sched_getparam", (PyCFunction)os_sched_getparam, METH_O, os_sched_getparam__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002923
2924static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002925os_sched_getparam_impl(PyObject *module, pid_t pid);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002926
2927static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002928os_sched_getparam(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002929{
2930 PyObject *return_value = NULL;
2931 pid_t pid;
2932
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03002933 if (!PyArg_Parse(arg, "" _Py_PARSE_PID ":sched_getparam", &pid)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002934 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03002935 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002936 return_value = os_sched_getparam_impl(module, pid);
2937
2938exit:
2939 return return_value;
2940}
2941
2942#endif /* defined(HAVE_SCHED_H) && defined(HAVE_SCHED_SETPARAM) */
2943
2944#if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_SETPARAM)
2945
2946PyDoc_STRVAR(os_sched_setparam__doc__,
2947"sched_setparam($module, pid, param, /)\n"
2948"--\n"
2949"\n"
2950"Set scheduling parameters for the process identified by pid.\n"
2951"\n"
2952"If pid is 0, sets parameters for the calling process.\n"
2953"param should be an instance of sched_param.");
2954
2955#define OS_SCHED_SETPARAM_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02002956 {"sched_setparam", (PyCFunction)(void(*)(void))os_sched_setparam, METH_FASTCALL, os_sched_setparam__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002957
2958static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002959os_sched_setparam_impl(PyObject *module, pid_t pid,
Larry Hastings89964c42015-04-14 18:07:59 -04002960 struct sched_param *param);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002961
2962static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02002963os_sched_setparam(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002964{
2965 PyObject *return_value = NULL;
2966 pid_t pid;
2967 struct sched_param param;
2968
Sylvain74453812017-06-10 06:51:48 +02002969 if (!_PyArg_ParseStack(args, nargs, "" _Py_PARSE_PID "O&:sched_setparam",
2970 &pid, convert_sched_param, &param)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01002971 goto exit;
2972 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002973 return_value = os_sched_setparam_impl(module, pid, &param);
2974
2975exit:
2976 return return_value;
2977}
2978
2979#endif /* defined(HAVE_SCHED_H) && defined(HAVE_SCHED_SETPARAM) */
2980
2981#if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_RR_GET_INTERVAL)
2982
2983PyDoc_STRVAR(os_sched_rr_get_interval__doc__,
2984"sched_rr_get_interval($module, pid, /)\n"
2985"--\n"
2986"\n"
2987"Return the round-robin quantum for the process identified by pid, in seconds.\n"
2988"\n"
2989"Value returned is a float.");
2990
2991#define OS_SCHED_RR_GET_INTERVAL_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03002992 {"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 +03002993
2994static double
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002995os_sched_rr_get_interval_impl(PyObject *module, pid_t pid);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002996
2997static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002998os_sched_rr_get_interval(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002999{
3000 PyObject *return_value = NULL;
3001 pid_t pid;
3002 double _return_value;
3003
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03003004 if (!PyArg_Parse(arg, "" _Py_PARSE_PID ":sched_rr_get_interval", &pid)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003005 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03003006 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003007 _return_value = os_sched_rr_get_interval_impl(module, pid);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03003008 if ((_return_value == -1.0) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003009 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03003010 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003011 return_value = PyFloat_FromDouble(_return_value);
3012
3013exit:
3014 return return_value;
3015}
3016
3017#endif /* defined(HAVE_SCHED_H) && defined(HAVE_SCHED_RR_GET_INTERVAL) */
3018
3019#if defined(HAVE_SCHED_H)
3020
3021PyDoc_STRVAR(os_sched_yield__doc__,
3022"sched_yield($module, /)\n"
3023"--\n"
3024"\n"
3025"Voluntarily relinquish the CPU.");
3026
3027#define OS_SCHED_YIELD_METHODDEF \
3028 {"sched_yield", (PyCFunction)os_sched_yield, METH_NOARGS, os_sched_yield__doc__},
3029
3030static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003031os_sched_yield_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003032
3033static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003034os_sched_yield(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003035{
3036 return os_sched_yield_impl(module);
3037}
3038
3039#endif /* defined(HAVE_SCHED_H) */
3040
3041#if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_SETAFFINITY)
3042
3043PyDoc_STRVAR(os_sched_setaffinity__doc__,
3044"sched_setaffinity($module, pid, mask, /)\n"
3045"--\n"
3046"\n"
3047"Set the CPU affinity of the process identified by pid to mask.\n"
3048"\n"
3049"mask should be an iterable of integers identifying CPUs.");
3050
3051#define OS_SCHED_SETAFFINITY_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02003052 {"sched_setaffinity", (PyCFunction)(void(*)(void))os_sched_setaffinity, METH_FASTCALL, os_sched_setaffinity__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003053
3054static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003055os_sched_setaffinity_impl(PyObject *module, pid_t pid, PyObject *mask);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003056
3057static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003058os_sched_setaffinity(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003059{
3060 PyObject *return_value = NULL;
3061 pid_t pid;
3062 PyObject *mask;
3063
Sylvain74453812017-06-10 06:51:48 +02003064 if (!_PyArg_ParseStack(args, nargs, "" _Py_PARSE_PID "O:sched_setaffinity",
3065 &pid, &mask)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01003066 goto exit;
3067 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003068 return_value = os_sched_setaffinity_impl(module, pid, mask);
3069
3070exit:
3071 return return_value;
3072}
3073
3074#endif /* defined(HAVE_SCHED_H) && defined(HAVE_SCHED_SETAFFINITY) */
3075
3076#if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_SETAFFINITY)
3077
3078PyDoc_STRVAR(os_sched_getaffinity__doc__,
3079"sched_getaffinity($module, pid, /)\n"
3080"--\n"
3081"\n"
Charles-François Natali80d62e62015-08-13 20:37:08 +01003082"Return the affinity of the process identified by pid (or the current process if zero).\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003083"\n"
3084"The affinity is returned as a set of CPU identifiers.");
3085
3086#define OS_SCHED_GETAFFINITY_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03003087 {"sched_getaffinity", (PyCFunction)os_sched_getaffinity, METH_O, os_sched_getaffinity__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003088
3089static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003090os_sched_getaffinity_impl(PyObject *module, pid_t pid);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003091
3092static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003093os_sched_getaffinity(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003094{
3095 PyObject *return_value = NULL;
3096 pid_t pid;
3097
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03003098 if (!PyArg_Parse(arg, "" _Py_PARSE_PID ":sched_getaffinity", &pid)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003099 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03003100 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003101 return_value = os_sched_getaffinity_impl(module, pid);
3102
3103exit:
3104 return return_value;
3105}
3106
3107#endif /* defined(HAVE_SCHED_H) && defined(HAVE_SCHED_SETAFFINITY) */
3108
3109#if (defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX))
3110
3111PyDoc_STRVAR(os_openpty__doc__,
3112"openpty($module, /)\n"
3113"--\n"
3114"\n"
3115"Open a pseudo-terminal.\n"
3116"\n"
3117"Return a tuple of (master_fd, slave_fd) containing open file descriptors\n"
3118"for both the master and slave ends.");
3119
3120#define OS_OPENPTY_METHODDEF \
3121 {"openpty", (PyCFunction)os_openpty, METH_NOARGS, os_openpty__doc__},
3122
3123static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003124os_openpty_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003125
3126static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003127os_openpty(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003128{
3129 return os_openpty_impl(module);
3130}
3131
3132#endif /* (defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)) */
3133
3134#if defined(HAVE_FORKPTY)
3135
3136PyDoc_STRVAR(os_forkpty__doc__,
3137"forkpty($module, /)\n"
3138"--\n"
3139"\n"
3140"Fork a new process with a new pseudo-terminal as controlling tty.\n"
3141"\n"
3142"Returns a tuple of (pid, master_fd).\n"
3143"Like fork(), return pid of 0 to the child process,\n"
3144"and pid of child to the parent process.\n"
3145"To both, return fd of newly opened pseudo-terminal.");
3146
3147#define OS_FORKPTY_METHODDEF \
3148 {"forkpty", (PyCFunction)os_forkpty, METH_NOARGS, os_forkpty__doc__},
3149
3150static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003151os_forkpty_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003152
3153static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003154os_forkpty(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003155{
3156 return os_forkpty_impl(module);
3157}
3158
3159#endif /* defined(HAVE_FORKPTY) */
3160
3161#if defined(HAVE_GETEGID)
3162
3163PyDoc_STRVAR(os_getegid__doc__,
3164"getegid($module, /)\n"
3165"--\n"
3166"\n"
3167"Return the current process\'s effective group id.");
3168
3169#define OS_GETEGID_METHODDEF \
3170 {"getegid", (PyCFunction)os_getegid, METH_NOARGS, os_getegid__doc__},
3171
3172static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003173os_getegid_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003174
3175static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003176os_getegid(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003177{
3178 return os_getegid_impl(module);
3179}
3180
3181#endif /* defined(HAVE_GETEGID) */
3182
3183#if defined(HAVE_GETEUID)
3184
3185PyDoc_STRVAR(os_geteuid__doc__,
3186"geteuid($module, /)\n"
3187"--\n"
3188"\n"
3189"Return the current process\'s effective user id.");
3190
3191#define OS_GETEUID_METHODDEF \
3192 {"geteuid", (PyCFunction)os_geteuid, METH_NOARGS, os_geteuid__doc__},
3193
3194static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003195os_geteuid_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003196
3197static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003198os_geteuid(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003199{
3200 return os_geteuid_impl(module);
3201}
3202
3203#endif /* defined(HAVE_GETEUID) */
3204
3205#if defined(HAVE_GETGID)
3206
3207PyDoc_STRVAR(os_getgid__doc__,
3208"getgid($module, /)\n"
3209"--\n"
3210"\n"
3211"Return the current process\'s group id.");
3212
3213#define OS_GETGID_METHODDEF \
3214 {"getgid", (PyCFunction)os_getgid, METH_NOARGS, os_getgid__doc__},
3215
3216static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003217os_getgid_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003218
3219static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003220os_getgid(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003221{
3222 return os_getgid_impl(module);
3223}
3224
3225#endif /* defined(HAVE_GETGID) */
3226
Berker Peksag39404992016-09-15 20:45:16 +03003227#if defined(HAVE_GETPID)
3228
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003229PyDoc_STRVAR(os_getpid__doc__,
3230"getpid($module, /)\n"
3231"--\n"
3232"\n"
3233"Return the current process id.");
3234
3235#define OS_GETPID_METHODDEF \
3236 {"getpid", (PyCFunction)os_getpid, METH_NOARGS, os_getpid__doc__},
3237
3238static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003239os_getpid_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003240
3241static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003242os_getpid(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003243{
3244 return os_getpid_impl(module);
3245}
3246
Berker Peksag39404992016-09-15 20:45:16 +03003247#endif /* defined(HAVE_GETPID) */
3248
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003249#if defined(HAVE_GETGROUPS)
3250
3251PyDoc_STRVAR(os_getgroups__doc__,
3252"getgroups($module, /)\n"
3253"--\n"
3254"\n"
3255"Return list of supplemental group IDs for the process.");
3256
3257#define OS_GETGROUPS_METHODDEF \
3258 {"getgroups", (PyCFunction)os_getgroups, METH_NOARGS, os_getgroups__doc__},
3259
3260static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003261os_getgroups_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003262
3263static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003264os_getgroups(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003265{
3266 return os_getgroups_impl(module);
3267}
3268
3269#endif /* defined(HAVE_GETGROUPS) */
3270
3271#if defined(HAVE_GETPGID)
3272
3273PyDoc_STRVAR(os_getpgid__doc__,
3274"getpgid($module, /, pid)\n"
3275"--\n"
3276"\n"
3277"Call the system call getpgid(), and return the result.");
3278
3279#define OS_GETPGID_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02003280 {"getpgid", (PyCFunction)(void(*)(void))os_getpgid, METH_FASTCALL|METH_KEYWORDS, os_getpgid__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003281
3282static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003283os_getpgid_impl(PyObject *module, pid_t pid);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003284
3285static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003286os_getpgid(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003287{
3288 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03003289 static const char * const _keywords[] = {"pid", NULL};
3290 static _PyArg_Parser _parser = {"" _Py_PARSE_PID ":getpgid", _keywords, 0};
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003291 pid_t pid;
3292
Victor Stinner3e1fad62017-01-17 01:29:01 +01003293 if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03003294 &pid)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003295 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03003296 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003297 return_value = os_getpgid_impl(module, pid);
3298
3299exit:
3300 return return_value;
3301}
3302
3303#endif /* defined(HAVE_GETPGID) */
3304
3305#if defined(HAVE_GETPGRP)
3306
3307PyDoc_STRVAR(os_getpgrp__doc__,
3308"getpgrp($module, /)\n"
3309"--\n"
3310"\n"
3311"Return the current process group id.");
3312
3313#define OS_GETPGRP_METHODDEF \
3314 {"getpgrp", (PyCFunction)os_getpgrp, METH_NOARGS, os_getpgrp__doc__},
3315
3316static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003317os_getpgrp_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003318
3319static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003320os_getpgrp(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003321{
3322 return os_getpgrp_impl(module);
3323}
3324
3325#endif /* defined(HAVE_GETPGRP) */
3326
3327#if defined(HAVE_SETPGRP)
3328
3329PyDoc_STRVAR(os_setpgrp__doc__,
3330"setpgrp($module, /)\n"
3331"--\n"
3332"\n"
3333"Make the current process the leader of its process group.");
3334
3335#define OS_SETPGRP_METHODDEF \
3336 {"setpgrp", (PyCFunction)os_setpgrp, METH_NOARGS, os_setpgrp__doc__},
3337
3338static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003339os_setpgrp_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003340
3341static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003342os_setpgrp(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003343{
3344 return os_setpgrp_impl(module);
3345}
3346
3347#endif /* defined(HAVE_SETPGRP) */
3348
3349#if defined(HAVE_GETPPID)
3350
3351PyDoc_STRVAR(os_getppid__doc__,
3352"getppid($module, /)\n"
3353"--\n"
3354"\n"
3355"Return the parent\'s process id.\n"
3356"\n"
3357"If the parent process has already exited, Windows machines will still\n"
3358"return its id; others systems will return the id of the \'init\' process (1).");
3359
3360#define OS_GETPPID_METHODDEF \
3361 {"getppid", (PyCFunction)os_getppid, METH_NOARGS, os_getppid__doc__},
3362
3363static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003364os_getppid_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003365
3366static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003367os_getppid(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003368{
3369 return os_getppid_impl(module);
3370}
3371
3372#endif /* defined(HAVE_GETPPID) */
3373
3374#if defined(HAVE_GETLOGIN)
3375
3376PyDoc_STRVAR(os_getlogin__doc__,
3377"getlogin($module, /)\n"
3378"--\n"
3379"\n"
3380"Return the actual login name.");
3381
3382#define OS_GETLOGIN_METHODDEF \
3383 {"getlogin", (PyCFunction)os_getlogin, METH_NOARGS, os_getlogin__doc__},
3384
3385static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003386os_getlogin_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003387
3388static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003389os_getlogin(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003390{
3391 return os_getlogin_impl(module);
3392}
3393
3394#endif /* defined(HAVE_GETLOGIN) */
3395
3396#if defined(HAVE_GETUID)
3397
3398PyDoc_STRVAR(os_getuid__doc__,
3399"getuid($module, /)\n"
3400"--\n"
3401"\n"
3402"Return the current process\'s user id.");
3403
3404#define OS_GETUID_METHODDEF \
3405 {"getuid", (PyCFunction)os_getuid, METH_NOARGS, os_getuid__doc__},
3406
3407static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003408os_getuid_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003409
3410static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003411os_getuid(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003412{
3413 return os_getuid_impl(module);
3414}
3415
3416#endif /* defined(HAVE_GETUID) */
3417
3418#if defined(HAVE_KILL)
3419
3420PyDoc_STRVAR(os_kill__doc__,
3421"kill($module, pid, signal, /)\n"
3422"--\n"
3423"\n"
3424"Kill a process with a signal.");
3425
3426#define OS_KILL_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02003427 {"kill", (PyCFunction)(void(*)(void))os_kill, METH_FASTCALL, os_kill__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003428
3429static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003430os_kill_impl(PyObject *module, pid_t pid, Py_ssize_t signal);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003431
3432static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003433os_kill(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003434{
3435 PyObject *return_value = NULL;
3436 pid_t pid;
3437 Py_ssize_t signal;
3438
Sylvain74453812017-06-10 06:51:48 +02003439 if (!_PyArg_ParseStack(args, nargs, "" _Py_PARSE_PID "n:kill",
3440 &pid, &signal)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01003441 goto exit;
3442 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003443 return_value = os_kill_impl(module, pid, signal);
3444
3445exit:
3446 return return_value;
3447}
3448
3449#endif /* defined(HAVE_KILL) */
3450
3451#if defined(HAVE_KILLPG)
3452
3453PyDoc_STRVAR(os_killpg__doc__,
3454"killpg($module, pgid, signal, /)\n"
3455"--\n"
3456"\n"
3457"Kill a process group with a signal.");
3458
3459#define OS_KILLPG_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02003460 {"killpg", (PyCFunction)(void(*)(void))os_killpg, METH_FASTCALL, os_killpg__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003461
3462static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003463os_killpg_impl(PyObject *module, pid_t pgid, int signal);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003464
3465static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003466os_killpg(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003467{
3468 PyObject *return_value = NULL;
3469 pid_t pgid;
3470 int signal;
3471
Sylvain74453812017-06-10 06:51:48 +02003472 if (!_PyArg_ParseStack(args, nargs, "" _Py_PARSE_PID "i:killpg",
3473 &pgid, &signal)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01003474 goto exit;
3475 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003476 return_value = os_killpg_impl(module, pgid, signal);
3477
3478exit:
3479 return return_value;
3480}
3481
3482#endif /* defined(HAVE_KILLPG) */
3483
3484#if defined(HAVE_PLOCK)
3485
3486PyDoc_STRVAR(os_plock__doc__,
3487"plock($module, op, /)\n"
3488"--\n"
3489"\n"
3490"Lock program segments into memory.\");");
3491
3492#define OS_PLOCK_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03003493 {"plock", (PyCFunction)os_plock, METH_O, os_plock__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003494
3495static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003496os_plock_impl(PyObject *module, int op);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003497
3498static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003499os_plock(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003500{
3501 PyObject *return_value = NULL;
3502 int op;
3503
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02003504 if (PyFloat_Check(arg)) {
3505 PyErr_SetString(PyExc_TypeError,
3506 "integer argument expected, got float" );
3507 goto exit;
3508 }
3509 op = _PyLong_AsInt(arg);
3510 if (op == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003511 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03003512 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003513 return_value = os_plock_impl(module, op);
3514
3515exit:
3516 return return_value;
3517}
3518
3519#endif /* defined(HAVE_PLOCK) */
3520
3521#if defined(HAVE_SETUID)
3522
3523PyDoc_STRVAR(os_setuid__doc__,
3524"setuid($module, uid, /)\n"
3525"--\n"
3526"\n"
3527"Set the current process\'s user id.");
3528
3529#define OS_SETUID_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03003530 {"setuid", (PyCFunction)os_setuid, METH_O, os_setuid__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003531
3532static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003533os_setuid_impl(PyObject *module, uid_t uid);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003534
3535static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003536os_setuid(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003537{
3538 PyObject *return_value = NULL;
3539 uid_t uid;
3540
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02003541 if (!_Py_Uid_Converter(arg, &uid)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003542 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03003543 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003544 return_value = os_setuid_impl(module, uid);
3545
3546exit:
3547 return return_value;
3548}
3549
3550#endif /* defined(HAVE_SETUID) */
3551
3552#if defined(HAVE_SETEUID)
3553
3554PyDoc_STRVAR(os_seteuid__doc__,
3555"seteuid($module, euid, /)\n"
3556"--\n"
3557"\n"
3558"Set the current process\'s effective user id.");
3559
3560#define OS_SETEUID_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03003561 {"seteuid", (PyCFunction)os_seteuid, METH_O, os_seteuid__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003562
3563static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003564os_seteuid_impl(PyObject *module, uid_t euid);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003565
3566static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003567os_seteuid(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003568{
3569 PyObject *return_value = NULL;
3570 uid_t euid;
3571
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02003572 if (!_Py_Uid_Converter(arg, &euid)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003573 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03003574 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003575 return_value = os_seteuid_impl(module, euid);
3576
3577exit:
3578 return return_value;
3579}
3580
3581#endif /* defined(HAVE_SETEUID) */
3582
3583#if defined(HAVE_SETEGID)
3584
3585PyDoc_STRVAR(os_setegid__doc__,
3586"setegid($module, egid, /)\n"
3587"--\n"
3588"\n"
3589"Set the current process\'s effective group id.");
3590
3591#define OS_SETEGID_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03003592 {"setegid", (PyCFunction)os_setegid, METH_O, os_setegid__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003593
3594static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003595os_setegid_impl(PyObject *module, gid_t egid);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003596
3597static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003598os_setegid(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003599{
3600 PyObject *return_value = NULL;
3601 gid_t egid;
3602
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02003603 if (!_Py_Gid_Converter(arg, &egid)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003604 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03003605 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003606 return_value = os_setegid_impl(module, egid);
3607
3608exit:
3609 return return_value;
3610}
3611
3612#endif /* defined(HAVE_SETEGID) */
3613
3614#if defined(HAVE_SETREUID)
3615
3616PyDoc_STRVAR(os_setreuid__doc__,
3617"setreuid($module, ruid, euid, /)\n"
3618"--\n"
3619"\n"
3620"Set the current process\'s real and effective user ids.");
3621
3622#define OS_SETREUID_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02003623 {"setreuid", (PyCFunction)(void(*)(void))os_setreuid, METH_FASTCALL, os_setreuid__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003624
3625static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003626os_setreuid_impl(PyObject *module, uid_t ruid, uid_t euid);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003627
3628static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003629os_setreuid(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003630{
3631 PyObject *return_value = NULL;
3632 uid_t ruid;
3633 uid_t euid;
3634
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02003635 if (!_PyArg_CheckPositional("setreuid", nargs, 2, 2)) {
3636 goto exit;
3637 }
3638 if (!_Py_Uid_Converter(args[0], &ruid)) {
3639 goto exit;
3640 }
3641 if (!_Py_Uid_Converter(args[1], &euid)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01003642 goto exit;
3643 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003644 return_value = os_setreuid_impl(module, ruid, euid);
3645
3646exit:
3647 return return_value;
3648}
3649
3650#endif /* defined(HAVE_SETREUID) */
3651
3652#if defined(HAVE_SETREGID)
3653
3654PyDoc_STRVAR(os_setregid__doc__,
3655"setregid($module, rgid, egid, /)\n"
3656"--\n"
3657"\n"
3658"Set the current process\'s real and effective group ids.");
3659
3660#define OS_SETREGID_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02003661 {"setregid", (PyCFunction)(void(*)(void))os_setregid, METH_FASTCALL, os_setregid__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003662
3663static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003664os_setregid_impl(PyObject *module, gid_t rgid, gid_t egid);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003665
3666static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003667os_setregid(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003668{
3669 PyObject *return_value = NULL;
3670 gid_t rgid;
3671 gid_t egid;
3672
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02003673 if (!_PyArg_CheckPositional("setregid", nargs, 2, 2)) {
3674 goto exit;
3675 }
3676 if (!_Py_Gid_Converter(args[0], &rgid)) {
3677 goto exit;
3678 }
3679 if (!_Py_Gid_Converter(args[1], &egid)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01003680 goto exit;
3681 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003682 return_value = os_setregid_impl(module, rgid, egid);
3683
3684exit:
3685 return return_value;
3686}
3687
3688#endif /* defined(HAVE_SETREGID) */
3689
3690#if defined(HAVE_SETGID)
3691
3692PyDoc_STRVAR(os_setgid__doc__,
3693"setgid($module, gid, /)\n"
3694"--\n"
3695"\n"
3696"Set the current process\'s group id.");
3697
3698#define OS_SETGID_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03003699 {"setgid", (PyCFunction)os_setgid, METH_O, os_setgid__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003700
3701static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003702os_setgid_impl(PyObject *module, gid_t gid);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003703
3704static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003705os_setgid(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003706{
3707 PyObject *return_value = NULL;
3708 gid_t gid;
3709
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02003710 if (!_Py_Gid_Converter(arg, &gid)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003711 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03003712 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003713 return_value = os_setgid_impl(module, gid);
3714
3715exit:
3716 return return_value;
3717}
3718
3719#endif /* defined(HAVE_SETGID) */
3720
3721#if defined(HAVE_SETGROUPS)
3722
3723PyDoc_STRVAR(os_setgroups__doc__,
3724"setgroups($module, groups, /)\n"
3725"--\n"
3726"\n"
3727"Set the groups of the current process to list.");
3728
3729#define OS_SETGROUPS_METHODDEF \
3730 {"setgroups", (PyCFunction)os_setgroups, METH_O, os_setgroups__doc__},
3731
3732#endif /* defined(HAVE_SETGROUPS) */
3733
3734#if defined(HAVE_WAIT3)
3735
3736PyDoc_STRVAR(os_wait3__doc__,
3737"wait3($module, /, options)\n"
3738"--\n"
3739"\n"
3740"Wait for completion of a child process.\n"
3741"\n"
3742"Returns a tuple of information about the child process:\n"
3743" (pid, status, rusage)");
3744
3745#define OS_WAIT3_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02003746 {"wait3", (PyCFunction)(void(*)(void))os_wait3, METH_FASTCALL|METH_KEYWORDS, os_wait3__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003747
3748static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003749os_wait3_impl(PyObject *module, int options);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003750
3751static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003752os_wait3(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003753{
3754 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03003755 static const char * const _keywords[] = {"options", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02003756 static _PyArg_Parser _parser = {NULL, _keywords, "wait3", 0};
3757 PyObject *argsbuf[1];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003758 int options;
3759
Serhiy Storchaka31913912019-03-14 10:32:22 +02003760 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
3761 if (!args) {
3762 goto exit;
3763 }
3764 if (PyFloat_Check(args[0])) {
3765 PyErr_SetString(PyExc_TypeError,
3766 "integer argument expected, got float" );
3767 goto exit;
3768 }
3769 options = _PyLong_AsInt(args[0]);
3770 if (options == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003771 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03003772 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003773 return_value = os_wait3_impl(module, options);
3774
3775exit:
3776 return return_value;
3777}
3778
3779#endif /* defined(HAVE_WAIT3) */
3780
3781#if defined(HAVE_WAIT4)
3782
3783PyDoc_STRVAR(os_wait4__doc__,
3784"wait4($module, /, pid, options)\n"
3785"--\n"
3786"\n"
3787"Wait for completion of a specific child process.\n"
3788"\n"
3789"Returns a tuple of information about the child process:\n"
3790" (pid, status, rusage)");
3791
3792#define OS_WAIT4_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02003793 {"wait4", (PyCFunction)(void(*)(void))os_wait4, METH_FASTCALL|METH_KEYWORDS, os_wait4__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003794
3795static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003796os_wait4_impl(PyObject *module, pid_t pid, int options);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003797
3798static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003799os_wait4(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003800{
3801 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03003802 static const char * const _keywords[] = {"pid", "options", NULL};
3803 static _PyArg_Parser _parser = {"" _Py_PARSE_PID "i:wait4", _keywords, 0};
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003804 pid_t pid;
3805 int options;
3806
Victor Stinner3e1fad62017-01-17 01:29:01 +01003807 if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03003808 &pid, &options)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003809 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03003810 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003811 return_value = os_wait4_impl(module, pid, options);
3812
3813exit:
3814 return return_value;
3815}
3816
3817#endif /* defined(HAVE_WAIT4) */
3818
3819#if (defined(HAVE_WAITID) && !defined(__APPLE__))
3820
3821PyDoc_STRVAR(os_waitid__doc__,
3822"waitid($module, idtype, id, options, /)\n"
3823"--\n"
3824"\n"
3825"Returns the result of waiting for a process or processes.\n"
3826"\n"
3827" idtype\n"
3828" Must be one of be P_PID, P_PGID or P_ALL.\n"
3829" id\n"
3830" The id to wait on.\n"
3831" options\n"
3832" Constructed from the ORing of one or more of WEXITED, WSTOPPED\n"
3833" or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.\n"
3834"\n"
3835"Returns either waitid_result or None if WNOHANG is specified and there are\n"
3836"no children in a waitable state.");
3837
3838#define OS_WAITID_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02003839 {"waitid", (PyCFunction)(void(*)(void))os_waitid, METH_FASTCALL, os_waitid__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003840
3841static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003842os_waitid_impl(PyObject *module, idtype_t idtype, id_t id, int options);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003843
3844static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003845os_waitid(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003846{
3847 PyObject *return_value = NULL;
3848 idtype_t idtype;
3849 id_t id;
3850 int options;
3851
Sylvain74453812017-06-10 06:51:48 +02003852 if (!_PyArg_ParseStack(args, nargs, "i" _Py_PARSE_PID "i:waitid",
3853 &idtype, &id, &options)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01003854 goto exit;
3855 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003856 return_value = os_waitid_impl(module, idtype, id, options);
3857
3858exit:
3859 return return_value;
3860}
3861
3862#endif /* (defined(HAVE_WAITID) && !defined(__APPLE__)) */
3863
3864#if defined(HAVE_WAITPID)
3865
3866PyDoc_STRVAR(os_waitpid__doc__,
3867"waitpid($module, pid, options, /)\n"
3868"--\n"
3869"\n"
3870"Wait for completion of a given child process.\n"
3871"\n"
3872"Returns a tuple of information regarding the child process:\n"
3873" (pid, status)\n"
3874"\n"
3875"The options argument is ignored on Windows.");
3876
3877#define OS_WAITPID_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02003878 {"waitpid", (PyCFunction)(void(*)(void))os_waitpid, METH_FASTCALL, os_waitpid__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003879
3880static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003881os_waitpid_impl(PyObject *module, pid_t pid, int options);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003882
3883static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003884os_waitpid(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003885{
3886 PyObject *return_value = NULL;
3887 pid_t pid;
3888 int options;
3889
Sylvain74453812017-06-10 06:51:48 +02003890 if (!_PyArg_ParseStack(args, nargs, "" _Py_PARSE_PID "i:waitpid",
3891 &pid, &options)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01003892 goto exit;
3893 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003894 return_value = os_waitpid_impl(module, pid, options);
3895
3896exit:
3897 return return_value;
3898}
3899
3900#endif /* defined(HAVE_WAITPID) */
3901
3902#if defined(HAVE_CWAIT)
3903
3904PyDoc_STRVAR(os_waitpid__doc__,
3905"waitpid($module, pid, options, /)\n"
3906"--\n"
3907"\n"
3908"Wait for completion of a given process.\n"
3909"\n"
3910"Returns a tuple of information regarding the process:\n"
3911" (pid, status << 8)\n"
3912"\n"
3913"The options argument is ignored on Windows.");
3914
3915#define OS_WAITPID_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02003916 {"waitpid", (PyCFunction)(void(*)(void))os_waitpid, METH_FASTCALL, os_waitpid__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003917
3918static PyObject *
Victor Stinner581139c2016-09-06 15:54:20 -07003919os_waitpid_impl(PyObject *module, intptr_t pid, int options);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003920
3921static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003922os_waitpid(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003923{
3924 PyObject *return_value = NULL;
Victor Stinner581139c2016-09-06 15:54:20 -07003925 intptr_t pid;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003926 int options;
3927
Sylvain74453812017-06-10 06:51:48 +02003928 if (!_PyArg_ParseStack(args, nargs, "" _Py_PARSE_INTPTR "i:waitpid",
3929 &pid, &options)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01003930 goto exit;
3931 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003932 return_value = os_waitpid_impl(module, pid, options);
3933
3934exit:
3935 return return_value;
3936}
3937
3938#endif /* defined(HAVE_CWAIT) */
3939
3940#if defined(HAVE_WAIT)
3941
3942PyDoc_STRVAR(os_wait__doc__,
3943"wait($module, /)\n"
3944"--\n"
3945"\n"
3946"Wait for completion of a child process.\n"
3947"\n"
3948"Returns a tuple of information about the child process:\n"
3949" (pid, status)");
3950
3951#define OS_WAIT_METHODDEF \
3952 {"wait", (PyCFunction)os_wait, METH_NOARGS, os_wait__doc__},
3953
3954static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003955os_wait_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003956
3957static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003958os_wait(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03003959{
3960 return os_wait_impl(module);
3961}
3962
3963#endif /* defined(HAVE_WAIT) */
3964
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03003965#if (defined(HAVE_READLINK) || defined(MS_WINDOWS))
3966
3967PyDoc_STRVAR(os_readlink__doc__,
3968"readlink($module, /, path, *, dir_fd=None)\n"
3969"--\n"
3970"\n"
3971"Return a string representing the path to which the symbolic link points.\n"
3972"\n"
3973"If dir_fd is not None, it should be a file descriptor open to a directory,\n"
3974"and path should be relative; path will then be relative to that directory.\n"
3975"\n"
3976"dir_fd may not be implemented on your platform. If it is unavailable,\n"
3977"using it will raise a NotImplementedError.");
3978
3979#define OS_READLINK_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02003980 {"readlink", (PyCFunction)(void(*)(void))os_readlink, METH_FASTCALL|METH_KEYWORDS, os_readlink__doc__},
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03003981
3982static PyObject *
3983os_readlink_impl(PyObject *module, path_t *path, int dir_fd);
3984
3985static PyObject *
3986os_readlink(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
3987{
3988 PyObject *return_value = NULL;
3989 static const char * const _keywords[] = {"path", "dir_fd", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02003990 static _PyArg_Parser _parser = {NULL, _keywords, "readlink", 0};
3991 PyObject *argsbuf[2];
3992 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03003993 path_t path = PATH_T_INITIALIZE("readlink", "path", 0, 0);
3994 int dir_fd = DEFAULT_DIR_FD;
3995
Serhiy Storchaka31913912019-03-14 10:32:22 +02003996 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
3997 if (!args) {
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03003998 goto exit;
3999 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02004000 if (!path_converter(args[0], &path)) {
4001 goto exit;
4002 }
4003 if (!noptargs) {
4004 goto skip_optional_kwonly;
4005 }
4006 if (!READLINKAT_DIR_FD_CONVERTER(args[1], &dir_fd)) {
4007 goto exit;
4008 }
4009skip_optional_kwonly:
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03004010 return_value = os_readlink_impl(module, &path, dir_fd);
4011
4012exit:
4013 /* Cleanup for path */
4014 path_cleanup(&path);
4015
4016 return return_value;
4017}
4018
4019#endif /* (defined(HAVE_READLINK) || defined(MS_WINDOWS)) */
4020
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004021#if defined(HAVE_SYMLINK)
4022
4023PyDoc_STRVAR(os_symlink__doc__,
4024"symlink($module, /, src, dst, target_is_directory=False, *, dir_fd=None)\n"
4025"--\n"
4026"\n"
4027"Create a symbolic link pointing to src named dst.\n"
4028"\n"
4029"target_is_directory is required on Windows if the target is to be\n"
4030" interpreted as a directory. (On Windows, symlink requires\n"
4031" Windows 6.0 or greater, and raises a NotImplementedError otherwise.)\n"
4032" target_is_directory is ignored on non-Windows platforms.\n"
4033"\n"
4034"If dir_fd is not None, it should be a file descriptor open to a directory,\n"
4035" and path should be relative; path will then be relative to that directory.\n"
4036"dir_fd may not be implemented on your platform.\n"
4037" If it is unavailable, using it will raise a NotImplementedError.");
4038
4039#define OS_SYMLINK_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02004040 {"symlink", (PyCFunction)(void(*)(void))os_symlink, METH_FASTCALL|METH_KEYWORDS, os_symlink__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004041
4042static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004043os_symlink_impl(PyObject *module, path_t *src, path_t *dst,
Larry Hastings89964c42015-04-14 18:07:59 -04004044 int target_is_directory, int dir_fd);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004045
4046static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004047os_symlink(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004048{
4049 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03004050 static const char * const _keywords[] = {"src", "dst", "target_is_directory", "dir_fd", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02004051 static _PyArg_Parser _parser = {NULL, _keywords, "symlink", 0};
4052 PyObject *argsbuf[4];
4053 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004054 path_t src = PATH_T_INITIALIZE("symlink", "src", 0, 0);
4055 path_t dst = PATH_T_INITIALIZE("symlink", "dst", 0, 0);
4056 int target_is_directory = 0;
4057 int dir_fd = DEFAULT_DIR_FD;
4058
Serhiy Storchaka31913912019-03-14 10:32:22 +02004059 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 3, 0, argsbuf);
4060 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004061 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03004062 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02004063 if (!path_converter(args[0], &src)) {
4064 goto exit;
4065 }
4066 if (!path_converter(args[1], &dst)) {
4067 goto exit;
4068 }
4069 if (!noptargs) {
4070 goto skip_optional_pos;
4071 }
4072 if (args[2]) {
4073 target_is_directory = PyObject_IsTrue(args[2]);
4074 if (target_is_directory < 0) {
4075 goto exit;
4076 }
4077 if (!--noptargs) {
4078 goto skip_optional_pos;
4079 }
4080 }
4081skip_optional_pos:
4082 if (!noptargs) {
4083 goto skip_optional_kwonly;
4084 }
4085 if (!SYMLINKAT_DIR_FD_CONVERTER(args[3], &dir_fd)) {
4086 goto exit;
4087 }
4088skip_optional_kwonly:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004089 return_value = os_symlink_impl(module, &src, &dst, target_is_directory, dir_fd);
4090
4091exit:
4092 /* Cleanup for src */
4093 path_cleanup(&src);
4094 /* Cleanup for dst */
4095 path_cleanup(&dst);
4096
4097 return return_value;
4098}
4099
4100#endif /* defined(HAVE_SYMLINK) */
4101
4102#if defined(HAVE_TIMES)
4103
4104PyDoc_STRVAR(os_times__doc__,
4105"times($module, /)\n"
4106"--\n"
4107"\n"
4108"Return a collection containing process timing information.\n"
4109"\n"
4110"The object returned behaves like a named tuple with these fields:\n"
4111" (utime, stime, cutime, cstime, elapsed_time)\n"
4112"All fields are floating point numbers.");
4113
4114#define OS_TIMES_METHODDEF \
4115 {"times", (PyCFunction)os_times, METH_NOARGS, os_times__doc__},
4116
4117static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004118os_times_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004119
4120static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004121os_times(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004122{
4123 return os_times_impl(module);
4124}
4125
4126#endif /* defined(HAVE_TIMES) */
4127
4128#if defined(HAVE_GETSID)
4129
4130PyDoc_STRVAR(os_getsid__doc__,
4131"getsid($module, pid, /)\n"
4132"--\n"
4133"\n"
4134"Call the system call getsid(pid) and return the result.");
4135
4136#define OS_GETSID_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03004137 {"getsid", (PyCFunction)os_getsid, METH_O, os_getsid__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004138
4139static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004140os_getsid_impl(PyObject *module, pid_t pid);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004141
4142static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004143os_getsid(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004144{
4145 PyObject *return_value = NULL;
4146 pid_t pid;
4147
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03004148 if (!PyArg_Parse(arg, "" _Py_PARSE_PID ":getsid", &pid)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004149 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03004150 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004151 return_value = os_getsid_impl(module, pid);
4152
4153exit:
4154 return return_value;
4155}
4156
4157#endif /* defined(HAVE_GETSID) */
4158
4159#if defined(HAVE_SETSID)
4160
4161PyDoc_STRVAR(os_setsid__doc__,
4162"setsid($module, /)\n"
4163"--\n"
4164"\n"
4165"Call the system call setsid().");
4166
4167#define OS_SETSID_METHODDEF \
4168 {"setsid", (PyCFunction)os_setsid, METH_NOARGS, os_setsid__doc__},
4169
4170static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004171os_setsid_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004172
4173static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004174os_setsid(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004175{
4176 return os_setsid_impl(module);
4177}
4178
4179#endif /* defined(HAVE_SETSID) */
4180
4181#if defined(HAVE_SETPGID)
4182
4183PyDoc_STRVAR(os_setpgid__doc__,
4184"setpgid($module, pid, pgrp, /)\n"
4185"--\n"
4186"\n"
4187"Call the system call setpgid(pid, pgrp).");
4188
4189#define OS_SETPGID_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02004190 {"setpgid", (PyCFunction)(void(*)(void))os_setpgid, METH_FASTCALL, os_setpgid__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004191
4192static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004193os_setpgid_impl(PyObject *module, pid_t pid, pid_t pgrp);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004194
4195static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004196os_setpgid(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004197{
4198 PyObject *return_value = NULL;
4199 pid_t pid;
4200 pid_t pgrp;
4201
Sylvain74453812017-06-10 06:51:48 +02004202 if (!_PyArg_ParseStack(args, nargs, "" _Py_PARSE_PID "" _Py_PARSE_PID ":setpgid",
4203 &pid, &pgrp)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01004204 goto exit;
4205 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004206 return_value = os_setpgid_impl(module, pid, pgrp);
4207
4208exit:
4209 return return_value;
4210}
4211
4212#endif /* defined(HAVE_SETPGID) */
4213
4214#if defined(HAVE_TCGETPGRP)
4215
4216PyDoc_STRVAR(os_tcgetpgrp__doc__,
4217"tcgetpgrp($module, fd, /)\n"
4218"--\n"
4219"\n"
4220"Return the process group associated with the terminal specified by fd.");
4221
4222#define OS_TCGETPGRP_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03004223 {"tcgetpgrp", (PyCFunction)os_tcgetpgrp, METH_O, os_tcgetpgrp__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004224
4225static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004226os_tcgetpgrp_impl(PyObject *module, int fd);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004227
4228static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004229os_tcgetpgrp(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004230{
4231 PyObject *return_value = NULL;
4232 int fd;
4233
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02004234 if (PyFloat_Check(arg)) {
4235 PyErr_SetString(PyExc_TypeError,
4236 "integer argument expected, got float" );
4237 goto exit;
4238 }
4239 fd = _PyLong_AsInt(arg);
4240 if (fd == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004241 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03004242 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004243 return_value = os_tcgetpgrp_impl(module, fd);
4244
4245exit:
4246 return return_value;
4247}
4248
4249#endif /* defined(HAVE_TCGETPGRP) */
4250
4251#if defined(HAVE_TCSETPGRP)
4252
4253PyDoc_STRVAR(os_tcsetpgrp__doc__,
4254"tcsetpgrp($module, fd, pgid, /)\n"
4255"--\n"
4256"\n"
4257"Set the process group associated with the terminal specified by fd.");
4258
4259#define OS_TCSETPGRP_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02004260 {"tcsetpgrp", (PyCFunction)(void(*)(void))os_tcsetpgrp, METH_FASTCALL, os_tcsetpgrp__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004261
4262static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004263os_tcsetpgrp_impl(PyObject *module, int fd, pid_t pgid);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004264
4265static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004266os_tcsetpgrp(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004267{
4268 PyObject *return_value = NULL;
4269 int fd;
4270 pid_t pgid;
4271
Sylvain74453812017-06-10 06:51:48 +02004272 if (!_PyArg_ParseStack(args, nargs, "i" _Py_PARSE_PID ":tcsetpgrp",
4273 &fd, &pgid)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01004274 goto exit;
4275 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004276 return_value = os_tcsetpgrp_impl(module, fd, pgid);
4277
4278exit:
4279 return return_value;
4280}
4281
4282#endif /* defined(HAVE_TCSETPGRP) */
4283
4284PyDoc_STRVAR(os_open__doc__,
4285"open($module, /, path, flags, mode=511, *, dir_fd=None)\n"
4286"--\n"
4287"\n"
4288"Open a file for low level IO. Returns a file descriptor (integer).\n"
4289"\n"
4290"If dir_fd is not None, it should be a file descriptor open to a directory,\n"
4291" and path should be relative; path will then be relative to that directory.\n"
4292"dir_fd may not be implemented on your platform.\n"
4293" If it is unavailable, using it will raise a NotImplementedError.");
4294
4295#define OS_OPEN_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02004296 {"open", (PyCFunction)(void(*)(void))os_open, METH_FASTCALL|METH_KEYWORDS, os_open__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004297
4298static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004299os_open_impl(PyObject *module, path_t *path, int flags, int mode, int dir_fd);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004300
4301static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004302os_open(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004303{
4304 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03004305 static const char * const _keywords[] = {"path", "flags", "mode", "dir_fd", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02004306 static _PyArg_Parser _parser = {NULL, _keywords, "open", 0};
4307 PyObject *argsbuf[4];
4308 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004309 path_t path = PATH_T_INITIALIZE("open", "path", 0, 0);
4310 int flags;
4311 int mode = 511;
4312 int dir_fd = DEFAULT_DIR_FD;
4313 int _return_value;
4314
Serhiy Storchaka31913912019-03-14 10:32:22 +02004315 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 3, 0, argsbuf);
4316 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004317 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03004318 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02004319 if (!path_converter(args[0], &path)) {
4320 goto exit;
4321 }
4322 if (PyFloat_Check(args[1])) {
4323 PyErr_SetString(PyExc_TypeError,
4324 "integer argument expected, got float" );
4325 goto exit;
4326 }
4327 flags = _PyLong_AsInt(args[1]);
4328 if (flags == -1 && PyErr_Occurred()) {
4329 goto exit;
4330 }
4331 if (!noptargs) {
4332 goto skip_optional_pos;
4333 }
4334 if (args[2]) {
4335 if (PyFloat_Check(args[2])) {
4336 PyErr_SetString(PyExc_TypeError,
4337 "integer argument expected, got float" );
4338 goto exit;
4339 }
4340 mode = _PyLong_AsInt(args[2]);
4341 if (mode == -1 && PyErr_Occurred()) {
4342 goto exit;
4343 }
4344 if (!--noptargs) {
4345 goto skip_optional_pos;
4346 }
4347 }
4348skip_optional_pos:
4349 if (!noptargs) {
4350 goto skip_optional_kwonly;
4351 }
4352 if (!OPENAT_DIR_FD_CONVERTER(args[3], &dir_fd)) {
4353 goto exit;
4354 }
4355skip_optional_kwonly:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004356 _return_value = os_open_impl(module, &path, flags, mode, dir_fd);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03004357 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004358 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03004359 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004360 return_value = PyLong_FromLong((long)_return_value);
4361
4362exit:
4363 /* Cleanup for path */
4364 path_cleanup(&path);
4365
4366 return return_value;
4367}
4368
4369PyDoc_STRVAR(os_close__doc__,
4370"close($module, /, fd)\n"
4371"--\n"
4372"\n"
4373"Close a file descriptor.");
4374
4375#define OS_CLOSE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02004376 {"close", (PyCFunction)(void(*)(void))os_close, METH_FASTCALL|METH_KEYWORDS, os_close__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004377
4378static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004379os_close_impl(PyObject *module, int fd);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004380
4381static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004382os_close(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004383{
4384 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03004385 static const char * const _keywords[] = {"fd", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02004386 static _PyArg_Parser _parser = {NULL, _keywords, "close", 0};
4387 PyObject *argsbuf[1];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004388 int fd;
4389
Serhiy Storchaka31913912019-03-14 10:32:22 +02004390 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
4391 if (!args) {
4392 goto exit;
4393 }
4394 if (PyFloat_Check(args[0])) {
4395 PyErr_SetString(PyExc_TypeError,
4396 "integer argument expected, got float" );
4397 goto exit;
4398 }
4399 fd = _PyLong_AsInt(args[0]);
4400 if (fd == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004401 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03004402 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004403 return_value = os_close_impl(module, fd);
4404
4405exit:
4406 return return_value;
4407}
4408
4409PyDoc_STRVAR(os_closerange__doc__,
4410"closerange($module, fd_low, fd_high, /)\n"
4411"--\n"
4412"\n"
4413"Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
4414
4415#define OS_CLOSERANGE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02004416 {"closerange", (PyCFunction)(void(*)(void))os_closerange, METH_FASTCALL, os_closerange__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004417
4418static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004419os_closerange_impl(PyObject *module, int fd_low, int fd_high);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004420
4421static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004422os_closerange(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004423{
4424 PyObject *return_value = NULL;
4425 int fd_low;
4426 int fd_high;
4427
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02004428 if (!_PyArg_CheckPositional("closerange", nargs, 2, 2)) {
4429 goto exit;
4430 }
4431 if (PyFloat_Check(args[0])) {
4432 PyErr_SetString(PyExc_TypeError,
4433 "integer argument expected, got float" );
4434 goto exit;
4435 }
4436 fd_low = _PyLong_AsInt(args[0]);
4437 if (fd_low == -1 && PyErr_Occurred()) {
4438 goto exit;
4439 }
4440 if (PyFloat_Check(args[1])) {
4441 PyErr_SetString(PyExc_TypeError,
4442 "integer argument expected, got float" );
4443 goto exit;
4444 }
4445 fd_high = _PyLong_AsInt(args[1]);
4446 if (fd_high == -1 && PyErr_Occurred()) {
Victor Stinner259f0e42017-01-17 01:35:17 +01004447 goto exit;
4448 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004449 return_value = os_closerange_impl(module, fd_low, fd_high);
4450
4451exit:
4452 return return_value;
4453}
4454
4455PyDoc_STRVAR(os_dup__doc__,
4456"dup($module, fd, /)\n"
4457"--\n"
4458"\n"
4459"Return a duplicate of a file descriptor.");
4460
4461#define OS_DUP_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03004462 {"dup", (PyCFunction)os_dup, METH_O, os_dup__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004463
4464static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004465os_dup_impl(PyObject *module, int fd);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004466
4467static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004468os_dup(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004469{
4470 PyObject *return_value = NULL;
4471 int fd;
4472 int _return_value;
4473
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02004474 if (PyFloat_Check(arg)) {
4475 PyErr_SetString(PyExc_TypeError,
4476 "integer argument expected, got float" );
4477 goto exit;
4478 }
4479 fd = _PyLong_AsInt(arg);
4480 if (fd == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004481 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03004482 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004483 _return_value = os_dup_impl(module, fd);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03004484 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004485 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03004486 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004487 return_value = PyLong_FromLong((long)_return_value);
4488
4489exit:
4490 return return_value;
4491}
4492
4493PyDoc_STRVAR(os_dup2__doc__,
4494"dup2($module, /, fd, fd2, inheritable=True)\n"
4495"--\n"
4496"\n"
4497"Duplicate file descriptor.");
4498
4499#define OS_DUP2_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02004500 {"dup2", (PyCFunction)(void(*)(void))os_dup2, METH_FASTCALL|METH_KEYWORDS, os_dup2__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004501
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08004502static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004503os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004504
4505static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004506os_dup2(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004507{
4508 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03004509 static const char * const _keywords[] = {"fd", "fd2", "inheritable", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02004510 static _PyArg_Parser _parser = {NULL, _keywords, "dup2", 0};
4511 PyObject *argsbuf[3];
4512 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004513 int fd;
4514 int fd2;
4515 int inheritable = 1;
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08004516 int _return_value;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004517
Serhiy Storchaka31913912019-03-14 10:32:22 +02004518 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 3, 0, argsbuf);
4519 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004520 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03004521 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02004522 if (PyFloat_Check(args[0])) {
4523 PyErr_SetString(PyExc_TypeError,
4524 "integer argument expected, got float" );
4525 goto exit;
4526 }
4527 fd = _PyLong_AsInt(args[0]);
4528 if (fd == -1 && PyErr_Occurred()) {
4529 goto exit;
4530 }
4531 if (PyFloat_Check(args[1])) {
4532 PyErr_SetString(PyExc_TypeError,
4533 "integer argument expected, got float" );
4534 goto exit;
4535 }
4536 fd2 = _PyLong_AsInt(args[1]);
4537 if (fd2 == -1 && PyErr_Occurred()) {
4538 goto exit;
4539 }
4540 if (!noptargs) {
4541 goto skip_optional_pos;
4542 }
4543 inheritable = PyObject_IsTrue(args[2]);
4544 if (inheritable < 0) {
4545 goto exit;
4546 }
4547skip_optional_pos:
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08004548 _return_value = os_dup2_impl(module, fd, fd2, inheritable);
4549 if ((_return_value == -1) && PyErr_Occurred()) {
4550 goto exit;
4551 }
4552 return_value = PyLong_FromLong((long)_return_value);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004553
4554exit:
4555 return return_value;
4556}
4557
4558#if defined(HAVE_LOCKF)
4559
4560PyDoc_STRVAR(os_lockf__doc__,
4561"lockf($module, fd, command, length, /)\n"
4562"--\n"
4563"\n"
4564"Apply, test or remove a POSIX lock on an open file descriptor.\n"
4565"\n"
4566" fd\n"
4567" An open file descriptor.\n"
4568" command\n"
4569" One of F_LOCK, F_TLOCK, F_ULOCK or F_TEST.\n"
4570" length\n"
4571" The number of bytes to lock, starting at the current position.");
4572
4573#define OS_LOCKF_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02004574 {"lockf", (PyCFunction)(void(*)(void))os_lockf, METH_FASTCALL, os_lockf__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004575
4576static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004577os_lockf_impl(PyObject *module, int fd, int command, Py_off_t length);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004578
4579static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004580os_lockf(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004581{
4582 PyObject *return_value = NULL;
4583 int fd;
4584 int command;
4585 Py_off_t length;
4586
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02004587 if (!_PyArg_CheckPositional("lockf", nargs, 3, 3)) {
4588 goto exit;
4589 }
4590 if (PyFloat_Check(args[0])) {
4591 PyErr_SetString(PyExc_TypeError,
4592 "integer argument expected, got float" );
4593 goto exit;
4594 }
4595 fd = _PyLong_AsInt(args[0]);
4596 if (fd == -1 && PyErr_Occurred()) {
4597 goto exit;
4598 }
4599 if (PyFloat_Check(args[1])) {
4600 PyErr_SetString(PyExc_TypeError,
4601 "integer argument expected, got float" );
4602 goto exit;
4603 }
4604 command = _PyLong_AsInt(args[1]);
4605 if (command == -1 && PyErr_Occurred()) {
4606 goto exit;
4607 }
4608 if (!Py_off_t_converter(args[2], &length)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01004609 goto exit;
4610 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004611 return_value = os_lockf_impl(module, fd, command, length);
4612
4613exit:
4614 return return_value;
4615}
4616
4617#endif /* defined(HAVE_LOCKF) */
4618
4619PyDoc_STRVAR(os_lseek__doc__,
4620"lseek($module, fd, position, how, /)\n"
4621"--\n"
4622"\n"
4623"Set the position of a file descriptor. Return the new position.\n"
4624"\n"
4625"Return the new cursor position in number of bytes\n"
4626"relative to the beginning of the file.");
4627
4628#define OS_LSEEK_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02004629 {"lseek", (PyCFunction)(void(*)(void))os_lseek, METH_FASTCALL, os_lseek__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004630
4631static Py_off_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004632os_lseek_impl(PyObject *module, int fd, Py_off_t position, int how);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004633
4634static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004635os_lseek(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004636{
4637 PyObject *return_value = NULL;
4638 int fd;
4639 Py_off_t position;
4640 int how;
4641 Py_off_t _return_value;
4642
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02004643 if (!_PyArg_CheckPositional("lseek", nargs, 3, 3)) {
4644 goto exit;
4645 }
4646 if (PyFloat_Check(args[0])) {
4647 PyErr_SetString(PyExc_TypeError,
4648 "integer argument expected, got float" );
4649 goto exit;
4650 }
4651 fd = _PyLong_AsInt(args[0]);
4652 if (fd == -1 && PyErr_Occurred()) {
4653 goto exit;
4654 }
4655 if (!Py_off_t_converter(args[1], &position)) {
4656 goto exit;
4657 }
4658 if (PyFloat_Check(args[2])) {
4659 PyErr_SetString(PyExc_TypeError,
4660 "integer argument expected, got float" );
4661 goto exit;
4662 }
4663 how = _PyLong_AsInt(args[2]);
4664 if (how == -1 && PyErr_Occurred()) {
Victor Stinner259f0e42017-01-17 01:35:17 +01004665 goto exit;
4666 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004667 _return_value = os_lseek_impl(module, fd, position, how);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03004668 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004669 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03004670 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004671 return_value = PyLong_FromPy_off_t(_return_value);
4672
4673exit:
4674 return return_value;
4675}
4676
4677PyDoc_STRVAR(os_read__doc__,
4678"read($module, fd, length, /)\n"
4679"--\n"
4680"\n"
4681"Read from a file descriptor. Returns a bytes object.");
4682
4683#define OS_READ_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02004684 {"read", (PyCFunction)(void(*)(void))os_read, METH_FASTCALL, os_read__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004685
4686static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004687os_read_impl(PyObject *module, int fd, Py_ssize_t length);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004688
4689static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004690os_read(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004691{
4692 PyObject *return_value = NULL;
4693 int fd;
4694 Py_ssize_t length;
4695
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02004696 if (!_PyArg_CheckPositional("read", nargs, 2, 2)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01004697 goto exit;
4698 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02004699 if (PyFloat_Check(args[0])) {
4700 PyErr_SetString(PyExc_TypeError,
4701 "integer argument expected, got float" );
4702 goto exit;
4703 }
4704 fd = _PyLong_AsInt(args[0]);
4705 if (fd == -1 && PyErr_Occurred()) {
4706 goto exit;
4707 }
4708 if (PyFloat_Check(args[1])) {
4709 PyErr_SetString(PyExc_TypeError,
4710 "integer argument expected, got float" );
4711 goto exit;
4712 }
4713 {
4714 Py_ssize_t ival = -1;
4715 PyObject *iobj = PyNumber_Index(args[1]);
4716 if (iobj != NULL) {
4717 ival = PyLong_AsSsize_t(iobj);
4718 Py_DECREF(iobj);
4719 }
4720 if (ival == -1 && PyErr_Occurred()) {
4721 goto exit;
4722 }
4723 length = ival;
4724 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004725 return_value = os_read_impl(module, fd, length);
4726
4727exit:
4728 return return_value;
4729}
4730
4731#if defined(HAVE_READV)
4732
4733PyDoc_STRVAR(os_readv__doc__,
4734"readv($module, fd, buffers, /)\n"
4735"--\n"
4736"\n"
4737"Read from a file descriptor fd into an iterable of buffers.\n"
4738"\n"
4739"The buffers should be mutable buffers accepting bytes.\n"
4740"readv will transfer data into each buffer until it is full\n"
4741"and then move on to the next buffer in the sequence to hold\n"
4742"the rest of the data.\n"
4743"\n"
4744"readv returns the total number of bytes read,\n"
4745"which may be less than the total capacity of all the buffers.");
4746
4747#define OS_READV_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02004748 {"readv", (PyCFunction)(void(*)(void))os_readv, METH_FASTCALL, os_readv__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004749
4750static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004751os_readv_impl(PyObject *module, int fd, PyObject *buffers);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004752
4753static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004754os_readv(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004755{
4756 PyObject *return_value = NULL;
4757 int fd;
4758 PyObject *buffers;
4759 Py_ssize_t _return_value;
4760
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02004761 if (!_PyArg_CheckPositional("readv", nargs, 2, 2)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01004762 goto exit;
4763 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02004764 if (PyFloat_Check(args[0])) {
4765 PyErr_SetString(PyExc_TypeError,
4766 "integer argument expected, got float" );
4767 goto exit;
4768 }
4769 fd = _PyLong_AsInt(args[0]);
4770 if (fd == -1 && PyErr_Occurred()) {
4771 goto exit;
4772 }
4773 buffers = args[1];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004774 _return_value = os_readv_impl(module, fd, buffers);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03004775 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004776 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03004777 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004778 return_value = PyLong_FromSsize_t(_return_value);
4779
4780exit:
4781 return return_value;
4782}
4783
4784#endif /* defined(HAVE_READV) */
4785
4786#if defined(HAVE_PREAD)
4787
4788PyDoc_STRVAR(os_pread__doc__,
4789"pread($module, fd, length, offset, /)\n"
4790"--\n"
4791"\n"
4792"Read a number of bytes from a file descriptor starting at a particular offset.\n"
4793"\n"
4794"Read length bytes from file descriptor fd, starting at offset bytes from\n"
4795"the beginning of the file. The file offset remains unchanged.");
4796
4797#define OS_PREAD_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02004798 {"pread", (PyCFunction)(void(*)(void))os_pread, METH_FASTCALL, os_pread__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004799
4800static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004801os_pread_impl(PyObject *module, int fd, int length, Py_off_t offset);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004802
4803static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004804os_pread(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004805{
4806 PyObject *return_value = NULL;
4807 int fd;
4808 int length;
4809 Py_off_t offset;
4810
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02004811 if (!_PyArg_CheckPositional("pread", nargs, 3, 3)) {
4812 goto exit;
4813 }
4814 if (PyFloat_Check(args[0])) {
4815 PyErr_SetString(PyExc_TypeError,
4816 "integer argument expected, got float" );
4817 goto exit;
4818 }
4819 fd = _PyLong_AsInt(args[0]);
4820 if (fd == -1 && PyErr_Occurred()) {
4821 goto exit;
4822 }
4823 if (PyFloat_Check(args[1])) {
4824 PyErr_SetString(PyExc_TypeError,
4825 "integer argument expected, got float" );
4826 goto exit;
4827 }
4828 length = _PyLong_AsInt(args[1]);
4829 if (length == -1 && PyErr_Occurred()) {
4830 goto exit;
4831 }
4832 if (!Py_off_t_converter(args[2], &offset)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01004833 goto exit;
4834 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004835 return_value = os_pread_impl(module, fd, length, offset);
4836
4837exit:
4838 return return_value;
4839}
4840
4841#endif /* defined(HAVE_PREAD) */
4842
Pablo Galindo4defba32018-01-27 16:16:37 +00004843#if (defined(HAVE_PREADV) || defined (HAVE_PREADV2))
4844
4845PyDoc_STRVAR(os_preadv__doc__,
4846"preadv($module, fd, buffers, offset, flags=0, /)\n"
4847"--\n"
4848"\n"
4849"Reads from a file descriptor into a number of mutable bytes-like objects.\n"
4850"\n"
4851"Combines the functionality of readv() and pread(). As readv(), it will\n"
4852"transfer data into each buffer until it is full and then move on to the next\n"
4853"buffer in the sequence to hold the rest of the data. Its fourth argument,\n"
4854"specifies the file offset at which the input operation is to be performed. It\n"
4855"will return the total number of bytes read (which can be less than the total\n"
4856"capacity of all the objects).\n"
4857"\n"
4858"The flags argument contains a bitwise OR of zero or more of the following flags:\n"
4859"\n"
4860"- RWF_HIPRI\n"
4861"- RWF_NOWAIT\n"
4862"\n"
4863"Using non-zero flags requires Linux 4.6 or newer.");
4864
4865#define OS_PREADV_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02004866 {"preadv", (PyCFunction)(void(*)(void))os_preadv, METH_FASTCALL, os_preadv__doc__},
Pablo Galindo4defba32018-01-27 16:16:37 +00004867
4868static Py_ssize_t
4869os_preadv_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset,
4870 int flags);
4871
4872static PyObject *
4873os_preadv(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
4874{
4875 PyObject *return_value = NULL;
4876 int fd;
4877 PyObject *buffers;
4878 Py_off_t offset;
4879 int flags = 0;
4880 Py_ssize_t _return_value;
4881
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02004882 if (!_PyArg_CheckPositional("preadv", nargs, 3, 4)) {
Pablo Galindo4defba32018-01-27 16:16:37 +00004883 goto exit;
4884 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02004885 if (PyFloat_Check(args[0])) {
4886 PyErr_SetString(PyExc_TypeError,
4887 "integer argument expected, got float" );
4888 goto exit;
4889 }
4890 fd = _PyLong_AsInt(args[0]);
4891 if (fd == -1 && PyErr_Occurred()) {
4892 goto exit;
4893 }
4894 buffers = args[1];
4895 if (!Py_off_t_converter(args[2], &offset)) {
4896 goto exit;
4897 }
4898 if (nargs < 4) {
4899 goto skip_optional;
4900 }
4901 if (PyFloat_Check(args[3])) {
4902 PyErr_SetString(PyExc_TypeError,
4903 "integer argument expected, got float" );
4904 goto exit;
4905 }
4906 flags = _PyLong_AsInt(args[3]);
4907 if (flags == -1 && PyErr_Occurred()) {
4908 goto exit;
4909 }
4910skip_optional:
Pablo Galindo4defba32018-01-27 16:16:37 +00004911 _return_value = os_preadv_impl(module, fd, buffers, offset, flags);
4912 if ((_return_value == -1) && PyErr_Occurred()) {
4913 goto exit;
4914 }
4915 return_value = PyLong_FromSsize_t(_return_value);
4916
4917exit:
4918 return return_value;
4919}
4920
4921#endif /* (defined(HAVE_PREADV) || defined (HAVE_PREADV2)) */
4922
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004923PyDoc_STRVAR(os_write__doc__,
4924"write($module, fd, data, /)\n"
4925"--\n"
4926"\n"
4927"Write a bytes object to a file descriptor.");
4928
4929#define OS_WRITE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02004930 {"write", (PyCFunction)(void(*)(void))os_write, METH_FASTCALL, os_write__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004931
4932static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004933os_write_impl(PyObject *module, int fd, Py_buffer *data);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004934
4935static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004936os_write(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004937{
4938 PyObject *return_value = NULL;
4939 int fd;
4940 Py_buffer data = {NULL, NULL};
4941 Py_ssize_t _return_value;
4942
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02004943 if (!_PyArg_CheckPositional("write", nargs, 2, 2)) {
4944 goto exit;
4945 }
4946 if (PyFloat_Check(args[0])) {
4947 PyErr_SetString(PyExc_TypeError,
4948 "integer argument expected, got float" );
4949 goto exit;
4950 }
4951 fd = _PyLong_AsInt(args[0]);
4952 if (fd == -1 && PyErr_Occurred()) {
4953 goto exit;
4954 }
4955 if (PyObject_GetBuffer(args[1], &data, PyBUF_SIMPLE) != 0) {
4956 goto exit;
4957 }
4958 if (!PyBuffer_IsContiguous(&data, 'C')) {
4959 _PyArg_BadArgument("write", 2, "contiguous buffer", args[1]);
Victor Stinner259f0e42017-01-17 01:35:17 +01004960 goto exit;
4961 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004962 _return_value = os_write_impl(module, fd, &data);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03004963 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004964 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03004965 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004966 return_value = PyLong_FromSsize_t(_return_value);
4967
4968exit:
4969 /* Cleanup for data */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03004970 if (data.obj) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004971 PyBuffer_Release(&data);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03004972 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03004973
4974 return return_value;
4975}
4976
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02004977#if defined(__APPLE__)
4978
4979PyDoc_STRVAR(os__fcopyfile__doc__,
4980"_fcopyfile($module, infd, outfd, flags, /)\n"
4981"--\n"
4982"\n"
Giampaolo Rodolac7f02a92018-06-19 08:27:29 -07004983"Efficiently copy content or metadata of 2 regular file descriptors (macOS).");
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02004984
4985#define OS__FCOPYFILE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02004986 {"_fcopyfile", (PyCFunction)(void(*)(void))os__fcopyfile, METH_FASTCALL, os__fcopyfile__doc__},
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02004987
4988static PyObject *
4989os__fcopyfile_impl(PyObject *module, int infd, int outfd, int flags);
4990
4991static PyObject *
4992os__fcopyfile(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
4993{
4994 PyObject *return_value = NULL;
4995 int infd;
4996 int outfd;
4997 int flags;
4998
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02004999 if (!_PyArg_CheckPositional("_fcopyfile", nargs, 3, 3)) {
5000 goto exit;
5001 }
5002 if (PyFloat_Check(args[0])) {
5003 PyErr_SetString(PyExc_TypeError,
5004 "integer argument expected, got float" );
5005 goto exit;
5006 }
5007 infd = _PyLong_AsInt(args[0]);
5008 if (infd == -1 && PyErr_Occurred()) {
5009 goto exit;
5010 }
5011 if (PyFloat_Check(args[1])) {
5012 PyErr_SetString(PyExc_TypeError,
5013 "integer argument expected, got float" );
5014 goto exit;
5015 }
5016 outfd = _PyLong_AsInt(args[1]);
5017 if (outfd == -1 && PyErr_Occurred()) {
5018 goto exit;
5019 }
5020 if (PyFloat_Check(args[2])) {
5021 PyErr_SetString(PyExc_TypeError,
5022 "integer argument expected, got float" );
5023 goto exit;
5024 }
5025 flags = _PyLong_AsInt(args[2]);
5026 if (flags == -1 && PyErr_Occurred()) {
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02005027 goto exit;
5028 }
5029 return_value = os__fcopyfile_impl(module, infd, outfd, flags);
5030
5031exit:
5032 return return_value;
5033}
5034
5035#endif /* defined(__APPLE__) */
5036
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005037PyDoc_STRVAR(os_fstat__doc__,
5038"fstat($module, /, fd)\n"
5039"--\n"
5040"\n"
5041"Perform a stat system call on the given file descriptor.\n"
5042"\n"
5043"Like stat(), but for an open file descriptor.\n"
5044"Equivalent to os.stat(fd).");
5045
5046#define OS_FSTAT_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02005047 {"fstat", (PyCFunction)(void(*)(void))os_fstat, METH_FASTCALL|METH_KEYWORDS, os_fstat__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005048
5049static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005050os_fstat_impl(PyObject *module, int fd);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005051
5052static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02005053os_fstat(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005054{
5055 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03005056 static const char * const _keywords[] = {"fd", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02005057 static _PyArg_Parser _parser = {NULL, _keywords, "fstat", 0};
5058 PyObject *argsbuf[1];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005059 int fd;
5060
Serhiy Storchaka31913912019-03-14 10:32:22 +02005061 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
5062 if (!args) {
5063 goto exit;
5064 }
5065 if (PyFloat_Check(args[0])) {
5066 PyErr_SetString(PyExc_TypeError,
5067 "integer argument expected, got float" );
5068 goto exit;
5069 }
5070 fd = _PyLong_AsInt(args[0]);
5071 if (fd == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005072 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005073 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005074 return_value = os_fstat_impl(module, fd);
5075
5076exit:
5077 return return_value;
5078}
5079
5080PyDoc_STRVAR(os_isatty__doc__,
5081"isatty($module, fd, /)\n"
5082"--\n"
5083"\n"
5084"Return True if the fd is connected to a terminal.\n"
5085"\n"
5086"Return True if the file descriptor is an open file descriptor\n"
5087"connected to the slave end of a terminal.");
5088
5089#define OS_ISATTY_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03005090 {"isatty", (PyCFunction)os_isatty, METH_O, os_isatty__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005091
5092static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005093os_isatty_impl(PyObject *module, int fd);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005094
5095static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005096os_isatty(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005097{
5098 PyObject *return_value = NULL;
5099 int fd;
5100 int _return_value;
5101
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02005102 if (PyFloat_Check(arg)) {
5103 PyErr_SetString(PyExc_TypeError,
5104 "integer argument expected, got float" );
5105 goto exit;
5106 }
5107 fd = _PyLong_AsInt(arg);
5108 if (fd == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005109 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005110 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005111 _return_value = os_isatty_impl(module, fd);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005112 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005113 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005114 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005115 return_value = PyBool_FromLong((long)_return_value);
5116
5117exit:
5118 return return_value;
5119}
5120
5121#if defined(HAVE_PIPE)
5122
5123PyDoc_STRVAR(os_pipe__doc__,
5124"pipe($module, /)\n"
5125"--\n"
5126"\n"
5127"Create a pipe.\n"
5128"\n"
5129"Returns a tuple of two file descriptors:\n"
5130" (read_fd, write_fd)");
5131
5132#define OS_PIPE_METHODDEF \
5133 {"pipe", (PyCFunction)os_pipe, METH_NOARGS, os_pipe__doc__},
5134
5135static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005136os_pipe_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005137
5138static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005139os_pipe(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005140{
5141 return os_pipe_impl(module);
5142}
5143
5144#endif /* defined(HAVE_PIPE) */
5145
5146#if defined(HAVE_PIPE2)
5147
5148PyDoc_STRVAR(os_pipe2__doc__,
5149"pipe2($module, flags, /)\n"
5150"--\n"
5151"\n"
5152"Create a pipe with flags set atomically.\n"
5153"\n"
5154"Returns a tuple of two file descriptors:\n"
5155" (read_fd, write_fd)\n"
5156"\n"
5157"flags can be constructed by ORing together one or more of these values:\n"
5158"O_NONBLOCK, O_CLOEXEC.");
5159
5160#define OS_PIPE2_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03005161 {"pipe2", (PyCFunction)os_pipe2, METH_O, os_pipe2__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005162
5163static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005164os_pipe2_impl(PyObject *module, int flags);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005165
5166static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005167os_pipe2(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005168{
5169 PyObject *return_value = NULL;
5170 int flags;
5171
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02005172 if (PyFloat_Check(arg)) {
5173 PyErr_SetString(PyExc_TypeError,
5174 "integer argument expected, got float" );
5175 goto exit;
5176 }
5177 flags = _PyLong_AsInt(arg);
5178 if (flags == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005179 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005180 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005181 return_value = os_pipe2_impl(module, flags);
5182
5183exit:
5184 return return_value;
5185}
5186
5187#endif /* defined(HAVE_PIPE2) */
5188
5189#if defined(HAVE_WRITEV)
5190
5191PyDoc_STRVAR(os_writev__doc__,
5192"writev($module, fd, buffers, /)\n"
5193"--\n"
5194"\n"
5195"Iterate over buffers, and write the contents of each to a file descriptor.\n"
5196"\n"
5197"Returns the total number of bytes written.\n"
5198"buffers must be a sequence of bytes-like objects.");
5199
5200#define OS_WRITEV_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02005201 {"writev", (PyCFunction)(void(*)(void))os_writev, METH_FASTCALL, os_writev__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005202
5203static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005204os_writev_impl(PyObject *module, int fd, PyObject *buffers);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005205
5206static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02005207os_writev(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005208{
5209 PyObject *return_value = NULL;
5210 int fd;
5211 PyObject *buffers;
5212 Py_ssize_t _return_value;
5213
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02005214 if (!_PyArg_CheckPositional("writev", nargs, 2, 2)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01005215 goto exit;
5216 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02005217 if (PyFloat_Check(args[0])) {
5218 PyErr_SetString(PyExc_TypeError,
5219 "integer argument expected, got float" );
5220 goto exit;
5221 }
5222 fd = _PyLong_AsInt(args[0]);
5223 if (fd == -1 && PyErr_Occurred()) {
5224 goto exit;
5225 }
5226 buffers = args[1];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005227 _return_value = os_writev_impl(module, fd, buffers);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005228 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005229 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005230 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005231 return_value = PyLong_FromSsize_t(_return_value);
5232
5233exit:
5234 return return_value;
5235}
5236
5237#endif /* defined(HAVE_WRITEV) */
5238
5239#if defined(HAVE_PWRITE)
5240
5241PyDoc_STRVAR(os_pwrite__doc__,
5242"pwrite($module, fd, buffer, offset, /)\n"
5243"--\n"
5244"\n"
5245"Write bytes to a file descriptor starting at a particular offset.\n"
5246"\n"
5247"Write buffer to fd, starting at offset bytes from the beginning of\n"
5248"the file. Returns the number of bytes writte. Does not change the\n"
5249"current file offset.");
5250
5251#define OS_PWRITE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02005252 {"pwrite", (PyCFunction)(void(*)(void))os_pwrite, METH_FASTCALL, os_pwrite__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005253
5254static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005255os_pwrite_impl(PyObject *module, int fd, Py_buffer *buffer, Py_off_t offset);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005256
5257static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02005258os_pwrite(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005259{
5260 PyObject *return_value = NULL;
5261 int fd;
5262 Py_buffer buffer = {NULL, NULL};
5263 Py_off_t offset;
5264 Py_ssize_t _return_value;
5265
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02005266 if (!_PyArg_CheckPositional("pwrite", nargs, 3, 3)) {
5267 goto exit;
5268 }
5269 if (PyFloat_Check(args[0])) {
5270 PyErr_SetString(PyExc_TypeError,
5271 "integer argument expected, got float" );
5272 goto exit;
5273 }
5274 fd = _PyLong_AsInt(args[0]);
5275 if (fd == -1 && PyErr_Occurred()) {
5276 goto exit;
5277 }
5278 if (PyObject_GetBuffer(args[1], &buffer, PyBUF_SIMPLE) != 0) {
5279 goto exit;
5280 }
5281 if (!PyBuffer_IsContiguous(&buffer, 'C')) {
5282 _PyArg_BadArgument("pwrite", 2, "contiguous buffer", args[1]);
5283 goto exit;
5284 }
5285 if (!Py_off_t_converter(args[2], &offset)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01005286 goto exit;
5287 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005288 _return_value = os_pwrite_impl(module, fd, &buffer, offset);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005289 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005290 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005291 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005292 return_value = PyLong_FromSsize_t(_return_value);
5293
5294exit:
5295 /* Cleanup for buffer */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005296 if (buffer.obj) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005297 PyBuffer_Release(&buffer);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005298 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005299
5300 return return_value;
5301}
5302
5303#endif /* defined(HAVE_PWRITE) */
5304
Pablo Galindo4defba32018-01-27 16:16:37 +00005305#if (defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2))
5306
5307PyDoc_STRVAR(os_pwritev__doc__,
5308"pwritev($module, fd, buffers, offset, flags=0, /)\n"
5309"--\n"
5310"\n"
5311"Writes the contents of bytes-like objects to a file descriptor at a given offset.\n"
5312"\n"
5313"Combines the functionality of writev() and pwrite(). All buffers must be a sequence\n"
5314"of bytes-like objects. Buffers are processed in array order. Entire contents of first\n"
5315"buffer is written before proceeding to second, and so on. The operating system may\n"
5316"set a limit (sysconf() value SC_IOV_MAX) on the number of buffers that can be used.\n"
5317"This function writes the contents of each object to the file descriptor and returns\n"
5318"the total number of bytes written.\n"
5319"\n"
5320"The flags argument contains a bitwise OR of zero or more of the following flags:\n"
5321"\n"
5322"- RWF_DSYNC\n"
5323"- RWF_SYNC\n"
5324"\n"
5325"Using non-zero flags requires Linux 4.7 or newer.");
5326
5327#define OS_PWRITEV_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02005328 {"pwritev", (PyCFunction)(void(*)(void))os_pwritev, METH_FASTCALL, os_pwritev__doc__},
Pablo Galindo4defba32018-01-27 16:16:37 +00005329
5330static Py_ssize_t
5331os_pwritev_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset,
5332 int flags);
5333
5334static PyObject *
5335os_pwritev(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
5336{
5337 PyObject *return_value = NULL;
5338 int fd;
5339 PyObject *buffers;
5340 Py_off_t offset;
5341 int flags = 0;
5342 Py_ssize_t _return_value;
5343
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02005344 if (!_PyArg_CheckPositional("pwritev", nargs, 3, 4)) {
Pablo Galindo4defba32018-01-27 16:16:37 +00005345 goto exit;
5346 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02005347 if (PyFloat_Check(args[0])) {
5348 PyErr_SetString(PyExc_TypeError,
5349 "integer argument expected, got float" );
5350 goto exit;
5351 }
5352 fd = _PyLong_AsInt(args[0]);
5353 if (fd == -1 && PyErr_Occurred()) {
5354 goto exit;
5355 }
5356 buffers = args[1];
5357 if (!Py_off_t_converter(args[2], &offset)) {
5358 goto exit;
5359 }
5360 if (nargs < 4) {
5361 goto skip_optional;
5362 }
5363 if (PyFloat_Check(args[3])) {
5364 PyErr_SetString(PyExc_TypeError,
5365 "integer argument expected, got float" );
5366 goto exit;
5367 }
5368 flags = _PyLong_AsInt(args[3]);
5369 if (flags == -1 && PyErr_Occurred()) {
5370 goto exit;
5371 }
5372skip_optional:
Pablo Galindo4defba32018-01-27 16:16:37 +00005373 _return_value = os_pwritev_impl(module, fd, buffers, offset, flags);
5374 if ((_return_value == -1) && PyErr_Occurred()) {
5375 goto exit;
5376 }
5377 return_value = PyLong_FromSsize_t(_return_value);
5378
5379exit:
5380 return return_value;
5381}
5382
5383#endif /* (defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2)) */
5384
Pablo Galindoaac4d032019-05-31 19:39:47 +01005385#if defined(HAVE_COPY_FILE_RANGE)
5386
5387PyDoc_STRVAR(os_copy_file_range__doc__,
5388"copy_file_range($module, /, src, dst, count, offset_src=None,\n"
5389" offset_dst=None)\n"
5390"--\n"
5391"\n"
5392"Copy count bytes from one file descriptor to another.\n"
5393"\n"
5394" src\n"
5395" Source file descriptor.\n"
5396" dst\n"
5397" Destination file descriptor.\n"
5398" count\n"
5399" Number of bytes to copy.\n"
5400" offset_src\n"
5401" Starting offset in src.\n"
5402" offset_dst\n"
5403" Starting offset in dst.\n"
5404"\n"
5405"If offset_src is None, then src is read from the current position;\n"
5406"respectively for offset_dst.");
5407
5408#define OS_COPY_FILE_RANGE_METHODDEF \
5409 {"copy_file_range", (PyCFunction)(void(*)(void))os_copy_file_range, METH_FASTCALL|METH_KEYWORDS, os_copy_file_range__doc__},
5410
5411static PyObject *
5412os_copy_file_range_impl(PyObject *module, int src, int dst, Py_ssize_t count,
5413 PyObject *offset_src, PyObject *offset_dst);
5414
5415static PyObject *
5416os_copy_file_range(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
5417{
5418 PyObject *return_value = NULL;
5419 static const char * const _keywords[] = {"src", "dst", "count", "offset_src", "offset_dst", NULL};
5420 static _PyArg_Parser _parser = {NULL, _keywords, "copy_file_range", 0};
5421 PyObject *argsbuf[5];
5422 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 3;
5423 int src;
5424 int dst;
5425 Py_ssize_t count;
5426 PyObject *offset_src = Py_None;
5427 PyObject *offset_dst = Py_None;
5428
5429 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 3, 5, 0, argsbuf);
5430 if (!args) {
5431 goto exit;
5432 }
5433 if (PyFloat_Check(args[0])) {
5434 PyErr_SetString(PyExc_TypeError,
5435 "integer argument expected, got float" );
5436 goto exit;
5437 }
5438 src = _PyLong_AsInt(args[0]);
5439 if (src == -1 && PyErr_Occurred()) {
5440 goto exit;
5441 }
5442 if (PyFloat_Check(args[1])) {
5443 PyErr_SetString(PyExc_TypeError,
5444 "integer argument expected, got float" );
5445 goto exit;
5446 }
5447 dst = _PyLong_AsInt(args[1]);
5448 if (dst == -1 && PyErr_Occurred()) {
5449 goto exit;
5450 }
5451 if (PyFloat_Check(args[2])) {
5452 PyErr_SetString(PyExc_TypeError,
5453 "integer argument expected, got float" );
5454 goto exit;
5455 }
5456 {
5457 Py_ssize_t ival = -1;
5458 PyObject *iobj = PyNumber_Index(args[2]);
5459 if (iobj != NULL) {
5460 ival = PyLong_AsSsize_t(iobj);
5461 Py_DECREF(iobj);
5462 }
5463 if (ival == -1 && PyErr_Occurred()) {
5464 goto exit;
5465 }
5466 count = ival;
5467 }
5468 if (!noptargs) {
5469 goto skip_optional_pos;
5470 }
5471 if (args[3]) {
5472 offset_src = args[3];
5473 if (!--noptargs) {
5474 goto skip_optional_pos;
5475 }
5476 }
5477 offset_dst = args[4];
5478skip_optional_pos:
5479 return_value = os_copy_file_range_impl(module, src, dst, count, offset_src, offset_dst);
5480
5481exit:
5482 return return_value;
5483}
5484
5485#endif /* defined(HAVE_COPY_FILE_RANGE) */
5486
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005487#if defined(HAVE_MKFIFO)
5488
5489PyDoc_STRVAR(os_mkfifo__doc__,
5490"mkfifo($module, /, path, mode=438, *, dir_fd=None)\n"
5491"--\n"
5492"\n"
5493"Create a \"fifo\" (a POSIX named pipe).\n"
5494"\n"
5495"If dir_fd is not None, it should be a file descriptor open to a directory,\n"
5496" and path should be relative; path will then be relative to that directory.\n"
5497"dir_fd may not be implemented on your platform.\n"
5498" If it is unavailable, using it will raise a NotImplementedError.");
5499
5500#define OS_MKFIFO_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02005501 {"mkfifo", (PyCFunction)(void(*)(void))os_mkfifo, METH_FASTCALL|METH_KEYWORDS, os_mkfifo__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005502
5503static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005504os_mkfifo_impl(PyObject *module, path_t *path, int mode, int dir_fd);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005505
5506static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02005507os_mkfifo(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005508{
5509 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03005510 static const char * const _keywords[] = {"path", "mode", "dir_fd", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02005511 static _PyArg_Parser _parser = {NULL, _keywords, "mkfifo", 0};
5512 PyObject *argsbuf[3];
5513 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005514 path_t path = PATH_T_INITIALIZE("mkfifo", "path", 0, 0);
5515 int mode = 438;
5516 int dir_fd = DEFAULT_DIR_FD;
5517
Serhiy Storchaka31913912019-03-14 10:32:22 +02005518 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
5519 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005520 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005521 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02005522 if (!path_converter(args[0], &path)) {
5523 goto exit;
5524 }
5525 if (!noptargs) {
5526 goto skip_optional_pos;
5527 }
5528 if (args[1]) {
5529 if (PyFloat_Check(args[1])) {
5530 PyErr_SetString(PyExc_TypeError,
5531 "integer argument expected, got float" );
5532 goto exit;
5533 }
5534 mode = _PyLong_AsInt(args[1]);
5535 if (mode == -1 && PyErr_Occurred()) {
5536 goto exit;
5537 }
5538 if (!--noptargs) {
5539 goto skip_optional_pos;
5540 }
5541 }
5542skip_optional_pos:
5543 if (!noptargs) {
5544 goto skip_optional_kwonly;
5545 }
5546 if (!MKFIFOAT_DIR_FD_CONVERTER(args[2], &dir_fd)) {
5547 goto exit;
5548 }
5549skip_optional_kwonly:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005550 return_value = os_mkfifo_impl(module, &path, mode, dir_fd);
5551
5552exit:
5553 /* Cleanup for path */
5554 path_cleanup(&path);
5555
5556 return return_value;
5557}
5558
5559#endif /* defined(HAVE_MKFIFO) */
5560
5561#if (defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV))
5562
5563PyDoc_STRVAR(os_mknod__doc__,
5564"mknod($module, /, path, mode=384, device=0, *, dir_fd=None)\n"
5565"--\n"
5566"\n"
5567"Create a node in the file system.\n"
5568"\n"
5569"Create a node in the file system (file, device special file or named pipe)\n"
5570"at path. mode specifies both the permissions to use and the\n"
5571"type of node to be created, being combined (bitwise OR) with one of\n"
5572"S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. If S_IFCHR or S_IFBLK is set on mode,\n"
5573"device defines the newly created device special file (probably using\n"
5574"os.makedev()). Otherwise device is ignored.\n"
5575"\n"
5576"If dir_fd is not None, it should be a file descriptor open to a directory,\n"
5577" and path should be relative; path will then be relative to that directory.\n"
5578"dir_fd may not be implemented on your platform.\n"
5579" If it is unavailable, using it will raise a NotImplementedError.");
5580
5581#define OS_MKNOD_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02005582 {"mknod", (PyCFunction)(void(*)(void))os_mknod, METH_FASTCALL|METH_KEYWORDS, os_mknod__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005583
5584static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005585os_mknod_impl(PyObject *module, path_t *path, int mode, dev_t device,
Larry Hastings89964c42015-04-14 18:07:59 -04005586 int dir_fd);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005587
5588static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02005589os_mknod(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005590{
5591 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03005592 static const char * const _keywords[] = {"path", "mode", "device", "dir_fd", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02005593 static _PyArg_Parser _parser = {NULL, _keywords, "mknod", 0};
5594 PyObject *argsbuf[4];
5595 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005596 path_t path = PATH_T_INITIALIZE("mknod", "path", 0, 0);
5597 int mode = 384;
5598 dev_t device = 0;
5599 int dir_fd = DEFAULT_DIR_FD;
5600
Serhiy Storchaka31913912019-03-14 10:32:22 +02005601 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 3, 0, argsbuf);
5602 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005603 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005604 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02005605 if (!path_converter(args[0], &path)) {
5606 goto exit;
5607 }
5608 if (!noptargs) {
5609 goto skip_optional_pos;
5610 }
5611 if (args[1]) {
5612 if (PyFloat_Check(args[1])) {
5613 PyErr_SetString(PyExc_TypeError,
5614 "integer argument expected, got float" );
5615 goto exit;
5616 }
5617 mode = _PyLong_AsInt(args[1]);
5618 if (mode == -1 && PyErr_Occurred()) {
5619 goto exit;
5620 }
5621 if (!--noptargs) {
5622 goto skip_optional_pos;
5623 }
5624 }
5625 if (args[2]) {
5626 if (!_Py_Dev_Converter(args[2], &device)) {
5627 goto exit;
5628 }
5629 if (!--noptargs) {
5630 goto skip_optional_pos;
5631 }
5632 }
5633skip_optional_pos:
5634 if (!noptargs) {
5635 goto skip_optional_kwonly;
5636 }
5637 if (!MKNODAT_DIR_FD_CONVERTER(args[3], &dir_fd)) {
5638 goto exit;
5639 }
5640skip_optional_kwonly:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005641 return_value = os_mknod_impl(module, &path, mode, device, dir_fd);
5642
5643exit:
5644 /* Cleanup for path */
5645 path_cleanup(&path);
5646
5647 return return_value;
5648}
5649
5650#endif /* (defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)) */
5651
5652#if defined(HAVE_DEVICE_MACROS)
5653
5654PyDoc_STRVAR(os_major__doc__,
5655"major($module, device, /)\n"
5656"--\n"
5657"\n"
5658"Extracts a device major number from a raw device number.");
5659
5660#define OS_MAJOR_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03005661 {"major", (PyCFunction)os_major, METH_O, os_major__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005662
5663static unsigned int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005664os_major_impl(PyObject *module, dev_t device);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005665
5666static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005667os_major(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005668{
5669 PyObject *return_value = NULL;
5670 dev_t device;
5671 unsigned int _return_value;
5672
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02005673 if (!_Py_Dev_Converter(arg, &device)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005674 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005675 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005676 _return_value = os_major_impl(module, device);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005677 if ((_return_value == (unsigned int)-1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005678 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005679 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005680 return_value = PyLong_FromUnsignedLong((unsigned long)_return_value);
5681
5682exit:
5683 return return_value;
5684}
5685
5686#endif /* defined(HAVE_DEVICE_MACROS) */
5687
5688#if defined(HAVE_DEVICE_MACROS)
5689
5690PyDoc_STRVAR(os_minor__doc__,
5691"minor($module, device, /)\n"
5692"--\n"
5693"\n"
5694"Extracts a device minor number from a raw device number.");
5695
5696#define OS_MINOR_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03005697 {"minor", (PyCFunction)os_minor, METH_O, os_minor__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005698
5699static unsigned int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005700os_minor_impl(PyObject *module, dev_t device);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005701
5702static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005703os_minor(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005704{
5705 PyObject *return_value = NULL;
5706 dev_t device;
5707 unsigned int _return_value;
5708
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02005709 if (!_Py_Dev_Converter(arg, &device)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005710 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005711 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005712 _return_value = os_minor_impl(module, device);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005713 if ((_return_value == (unsigned int)-1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005714 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005715 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005716 return_value = PyLong_FromUnsignedLong((unsigned long)_return_value);
5717
5718exit:
5719 return return_value;
5720}
5721
5722#endif /* defined(HAVE_DEVICE_MACROS) */
5723
5724#if defined(HAVE_DEVICE_MACROS)
5725
5726PyDoc_STRVAR(os_makedev__doc__,
5727"makedev($module, major, minor, /)\n"
5728"--\n"
5729"\n"
5730"Composes a raw device number from the major and minor device numbers.");
5731
5732#define OS_MAKEDEV_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02005733 {"makedev", (PyCFunction)(void(*)(void))os_makedev, METH_FASTCALL, os_makedev__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005734
5735static dev_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005736os_makedev_impl(PyObject *module, int major, int minor);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005737
5738static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02005739os_makedev(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005740{
5741 PyObject *return_value = NULL;
5742 int major;
5743 int minor;
5744 dev_t _return_value;
5745
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02005746 if (!_PyArg_CheckPositional("makedev", nargs, 2, 2)) {
5747 goto exit;
5748 }
5749 if (PyFloat_Check(args[0])) {
5750 PyErr_SetString(PyExc_TypeError,
5751 "integer argument expected, got float" );
5752 goto exit;
5753 }
5754 major = _PyLong_AsInt(args[0]);
5755 if (major == -1 && PyErr_Occurred()) {
5756 goto exit;
5757 }
5758 if (PyFloat_Check(args[1])) {
5759 PyErr_SetString(PyExc_TypeError,
5760 "integer argument expected, got float" );
5761 goto exit;
5762 }
5763 minor = _PyLong_AsInt(args[1]);
5764 if (minor == -1 && PyErr_Occurred()) {
Victor Stinner259f0e42017-01-17 01:35:17 +01005765 goto exit;
5766 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005767 _return_value = os_makedev_impl(module, major, minor);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005768 if ((_return_value == (dev_t)-1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005769 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005770 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005771 return_value = _PyLong_FromDev(_return_value);
5772
5773exit:
5774 return return_value;
5775}
5776
5777#endif /* defined(HAVE_DEVICE_MACROS) */
5778
Steve Dowerf7377032015-04-12 15:44:54 -04005779#if (defined HAVE_FTRUNCATE || defined MS_WINDOWS)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005780
5781PyDoc_STRVAR(os_ftruncate__doc__,
5782"ftruncate($module, fd, length, /)\n"
5783"--\n"
5784"\n"
5785"Truncate a file, specified by file descriptor, to a specific length.");
5786
5787#define OS_FTRUNCATE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02005788 {"ftruncate", (PyCFunction)(void(*)(void))os_ftruncate, METH_FASTCALL, os_ftruncate__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005789
5790static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005791os_ftruncate_impl(PyObject *module, int fd, Py_off_t length);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005792
5793static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02005794os_ftruncate(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005795{
5796 PyObject *return_value = NULL;
5797 int fd;
5798 Py_off_t length;
5799
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02005800 if (!_PyArg_CheckPositional("ftruncate", nargs, 2, 2)) {
5801 goto exit;
5802 }
5803 if (PyFloat_Check(args[0])) {
5804 PyErr_SetString(PyExc_TypeError,
5805 "integer argument expected, got float" );
5806 goto exit;
5807 }
5808 fd = _PyLong_AsInt(args[0]);
5809 if (fd == -1 && PyErr_Occurred()) {
5810 goto exit;
5811 }
5812 if (!Py_off_t_converter(args[1], &length)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01005813 goto exit;
5814 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005815 return_value = os_ftruncate_impl(module, fd, length);
5816
5817exit:
5818 return return_value;
5819}
5820
Steve Dowerf7377032015-04-12 15:44:54 -04005821#endif /* (defined HAVE_FTRUNCATE || defined MS_WINDOWS) */
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005822
Steve Dowerf7377032015-04-12 15:44:54 -04005823#if (defined HAVE_TRUNCATE || defined MS_WINDOWS)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005824
5825PyDoc_STRVAR(os_truncate__doc__,
5826"truncate($module, /, path, length)\n"
5827"--\n"
5828"\n"
5829"Truncate a file, specified by path, to a specific length.\n"
5830"\n"
5831"On some platforms, path may also be specified as an open file descriptor.\n"
5832" If this functionality is unavailable, using it raises an exception.");
5833
5834#define OS_TRUNCATE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02005835 {"truncate", (PyCFunction)(void(*)(void))os_truncate, METH_FASTCALL|METH_KEYWORDS, os_truncate__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005836
5837static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005838os_truncate_impl(PyObject *module, path_t *path, Py_off_t length);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005839
5840static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02005841os_truncate(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005842{
5843 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03005844 static const char * const _keywords[] = {"path", "length", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02005845 static _PyArg_Parser _parser = {NULL, _keywords, "truncate", 0};
5846 PyObject *argsbuf[2];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005847 path_t path = PATH_T_INITIALIZE("truncate", "path", 0, PATH_HAVE_FTRUNCATE);
5848 Py_off_t length;
5849
Serhiy Storchaka31913912019-03-14 10:32:22 +02005850 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf);
5851 if (!args) {
5852 goto exit;
5853 }
5854 if (!path_converter(args[0], &path)) {
5855 goto exit;
5856 }
5857 if (!Py_off_t_converter(args[1], &length)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005858 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03005859 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005860 return_value = os_truncate_impl(module, &path, length);
5861
5862exit:
5863 /* Cleanup for path */
5864 path_cleanup(&path);
5865
5866 return return_value;
5867}
5868
Steve Dowerf7377032015-04-12 15:44:54 -04005869#endif /* (defined HAVE_TRUNCATE || defined MS_WINDOWS) */
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005870
5871#if (defined(HAVE_POSIX_FALLOCATE) && !defined(POSIX_FADVISE_AIX_BUG))
5872
5873PyDoc_STRVAR(os_posix_fallocate__doc__,
5874"posix_fallocate($module, fd, offset, length, /)\n"
5875"--\n"
5876"\n"
5877"Ensure a file has allocated at least a particular number of bytes on disk.\n"
5878"\n"
5879"Ensure that the file specified by fd encompasses a range of bytes\n"
5880"starting at offset bytes from the beginning and continuing for length bytes.");
5881
5882#define OS_POSIX_FALLOCATE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02005883 {"posix_fallocate", (PyCFunction)(void(*)(void))os_posix_fallocate, METH_FASTCALL, os_posix_fallocate__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005884
5885static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005886os_posix_fallocate_impl(PyObject *module, int fd, Py_off_t offset,
Larry Hastings89964c42015-04-14 18:07:59 -04005887 Py_off_t length);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005888
5889static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02005890os_posix_fallocate(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005891{
5892 PyObject *return_value = NULL;
5893 int fd;
5894 Py_off_t offset;
5895 Py_off_t length;
5896
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02005897 if (!_PyArg_CheckPositional("posix_fallocate", nargs, 3, 3)) {
5898 goto exit;
5899 }
5900 if (PyFloat_Check(args[0])) {
5901 PyErr_SetString(PyExc_TypeError,
5902 "integer argument expected, got float" );
5903 goto exit;
5904 }
5905 fd = _PyLong_AsInt(args[0]);
5906 if (fd == -1 && PyErr_Occurred()) {
5907 goto exit;
5908 }
5909 if (!Py_off_t_converter(args[1], &offset)) {
5910 goto exit;
5911 }
5912 if (!Py_off_t_converter(args[2], &length)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01005913 goto exit;
5914 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005915 return_value = os_posix_fallocate_impl(module, fd, offset, length);
5916
5917exit:
5918 return return_value;
5919}
5920
5921#endif /* (defined(HAVE_POSIX_FALLOCATE) && !defined(POSIX_FADVISE_AIX_BUG)) */
5922
5923#if (defined(HAVE_POSIX_FADVISE) && !defined(POSIX_FADVISE_AIX_BUG))
5924
5925PyDoc_STRVAR(os_posix_fadvise__doc__,
5926"posix_fadvise($module, fd, offset, length, advice, /)\n"
5927"--\n"
5928"\n"
5929"Announce an intention to access data in a specific pattern.\n"
5930"\n"
5931"Announce an intention to access data in a specific pattern, thus allowing\n"
5932"the kernel to make optimizations.\n"
5933"The advice applies to the region of the file specified by fd starting at\n"
5934"offset and continuing for length bytes.\n"
5935"advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,\n"
5936"POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED, or\n"
5937"POSIX_FADV_DONTNEED.");
5938
5939#define OS_POSIX_FADVISE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02005940 {"posix_fadvise", (PyCFunction)(void(*)(void))os_posix_fadvise, METH_FASTCALL, os_posix_fadvise__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005941
5942static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005943os_posix_fadvise_impl(PyObject *module, int fd, Py_off_t offset,
Larry Hastings89964c42015-04-14 18:07:59 -04005944 Py_off_t length, int advice);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005945
5946static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02005947os_posix_fadvise(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005948{
5949 PyObject *return_value = NULL;
5950 int fd;
5951 Py_off_t offset;
5952 Py_off_t length;
5953 int advice;
5954
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02005955 if (!_PyArg_CheckPositional("posix_fadvise", nargs, 4, 4)) {
5956 goto exit;
5957 }
5958 if (PyFloat_Check(args[0])) {
5959 PyErr_SetString(PyExc_TypeError,
5960 "integer argument expected, got float" );
5961 goto exit;
5962 }
5963 fd = _PyLong_AsInt(args[0]);
5964 if (fd == -1 && PyErr_Occurred()) {
5965 goto exit;
5966 }
5967 if (!Py_off_t_converter(args[1], &offset)) {
5968 goto exit;
5969 }
5970 if (!Py_off_t_converter(args[2], &length)) {
5971 goto exit;
5972 }
5973 if (PyFloat_Check(args[3])) {
5974 PyErr_SetString(PyExc_TypeError,
5975 "integer argument expected, got float" );
5976 goto exit;
5977 }
5978 advice = _PyLong_AsInt(args[3]);
5979 if (advice == -1 && PyErr_Occurred()) {
Victor Stinner259f0e42017-01-17 01:35:17 +01005980 goto exit;
5981 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005982 return_value = os_posix_fadvise_impl(module, fd, offset, length, advice);
5983
5984exit:
5985 return return_value;
5986}
5987
5988#endif /* (defined(HAVE_POSIX_FADVISE) && !defined(POSIX_FADVISE_AIX_BUG)) */
5989
5990#if defined(HAVE_PUTENV) && defined(MS_WINDOWS)
5991
5992PyDoc_STRVAR(os_putenv__doc__,
5993"putenv($module, name, value, /)\n"
5994"--\n"
5995"\n"
5996"Change or add an environment variable.");
5997
5998#define OS_PUTENV_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02005999 {"putenv", (PyCFunction)(void(*)(void))os_putenv, METH_FASTCALL, os_putenv__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006000
6001static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006002os_putenv_impl(PyObject *module, PyObject *name, PyObject *value);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006003
6004static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02006005os_putenv(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006006{
6007 PyObject *return_value = NULL;
6008 PyObject *name;
6009 PyObject *value;
6010
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02006011 if (!_PyArg_CheckPositional("putenv", nargs, 2, 2)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01006012 goto exit;
6013 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02006014 if (!PyUnicode_Check(args[0])) {
6015 _PyArg_BadArgument("putenv", 1, "str", args[0]);
6016 goto exit;
6017 }
6018 if (PyUnicode_READY(args[0]) == -1) {
6019 goto exit;
6020 }
6021 name = args[0];
6022 if (!PyUnicode_Check(args[1])) {
6023 _PyArg_BadArgument("putenv", 2, "str", args[1]);
6024 goto exit;
6025 }
6026 if (PyUnicode_READY(args[1]) == -1) {
6027 goto exit;
6028 }
6029 value = args[1];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006030 return_value = os_putenv_impl(module, name, value);
6031
6032exit:
6033 return return_value;
6034}
6035
6036#endif /* defined(HAVE_PUTENV) && defined(MS_WINDOWS) */
6037
6038#if defined(HAVE_PUTENV) && !defined(MS_WINDOWS)
6039
6040PyDoc_STRVAR(os_putenv__doc__,
6041"putenv($module, name, value, /)\n"
6042"--\n"
6043"\n"
6044"Change or add an environment variable.");
6045
6046#define OS_PUTENV_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02006047 {"putenv", (PyCFunction)(void(*)(void))os_putenv, METH_FASTCALL, os_putenv__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006048
6049static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006050os_putenv_impl(PyObject *module, PyObject *name, PyObject *value);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006051
6052static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02006053os_putenv(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006054{
6055 PyObject *return_value = NULL;
6056 PyObject *name = NULL;
6057 PyObject *value = NULL;
6058
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02006059 if (!_PyArg_CheckPositional("putenv", nargs, 2, 2)) {
6060 goto exit;
6061 }
6062 if (!PyUnicode_FSConverter(args[0], &name)) {
6063 goto exit;
6064 }
6065 if (!PyUnicode_FSConverter(args[1], &value)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01006066 goto exit;
6067 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006068 return_value = os_putenv_impl(module, name, value);
6069
6070exit:
6071 /* Cleanup for name */
6072 Py_XDECREF(name);
6073 /* Cleanup for value */
6074 Py_XDECREF(value);
6075
6076 return return_value;
6077}
6078
6079#endif /* defined(HAVE_PUTENV) && !defined(MS_WINDOWS) */
6080
6081#if defined(HAVE_UNSETENV)
6082
6083PyDoc_STRVAR(os_unsetenv__doc__,
6084"unsetenv($module, name, /)\n"
6085"--\n"
6086"\n"
6087"Delete an environment variable.");
6088
6089#define OS_UNSETENV_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03006090 {"unsetenv", (PyCFunction)os_unsetenv, METH_O, os_unsetenv__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006091
6092static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006093os_unsetenv_impl(PyObject *module, PyObject *name);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006094
6095static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006096os_unsetenv(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006097{
6098 PyObject *return_value = NULL;
6099 PyObject *name = NULL;
6100
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02006101 if (!PyUnicode_FSConverter(arg, &name)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006102 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006103 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006104 return_value = os_unsetenv_impl(module, name);
6105
6106exit:
6107 /* Cleanup for name */
6108 Py_XDECREF(name);
6109
6110 return return_value;
6111}
6112
6113#endif /* defined(HAVE_UNSETENV) */
6114
6115PyDoc_STRVAR(os_strerror__doc__,
6116"strerror($module, code, /)\n"
6117"--\n"
6118"\n"
6119"Translate an error code to a message string.");
6120
6121#define OS_STRERROR_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03006122 {"strerror", (PyCFunction)os_strerror, METH_O, os_strerror__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006123
6124static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006125os_strerror_impl(PyObject *module, int code);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006126
6127static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006128os_strerror(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006129{
6130 PyObject *return_value = NULL;
6131 int code;
6132
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02006133 if (PyFloat_Check(arg)) {
6134 PyErr_SetString(PyExc_TypeError,
6135 "integer argument expected, got float" );
6136 goto exit;
6137 }
6138 code = _PyLong_AsInt(arg);
6139 if (code == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006140 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006141 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006142 return_value = os_strerror_impl(module, code);
6143
6144exit:
6145 return return_value;
6146}
6147
6148#if defined(HAVE_SYS_WAIT_H) && defined(WCOREDUMP)
6149
6150PyDoc_STRVAR(os_WCOREDUMP__doc__,
6151"WCOREDUMP($module, status, /)\n"
6152"--\n"
6153"\n"
6154"Return True if the process returning status was dumped to a core file.");
6155
6156#define OS_WCOREDUMP_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03006157 {"WCOREDUMP", (PyCFunction)os_WCOREDUMP, METH_O, os_WCOREDUMP__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006158
6159static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006160os_WCOREDUMP_impl(PyObject *module, int status);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006161
6162static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006163os_WCOREDUMP(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006164{
6165 PyObject *return_value = NULL;
6166 int status;
6167 int _return_value;
6168
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02006169 if (PyFloat_Check(arg)) {
6170 PyErr_SetString(PyExc_TypeError,
6171 "integer argument expected, got float" );
6172 goto exit;
6173 }
6174 status = _PyLong_AsInt(arg);
6175 if (status == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006176 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006177 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006178 _return_value = os_WCOREDUMP_impl(module, status);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006179 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006180 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006181 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006182 return_value = PyBool_FromLong((long)_return_value);
6183
6184exit:
6185 return return_value;
6186}
6187
6188#endif /* defined(HAVE_SYS_WAIT_H) && defined(WCOREDUMP) */
6189
6190#if defined(HAVE_SYS_WAIT_H) && defined(WIFCONTINUED)
6191
6192PyDoc_STRVAR(os_WIFCONTINUED__doc__,
6193"WIFCONTINUED($module, /, status)\n"
6194"--\n"
6195"\n"
6196"Return True if a particular process was continued from a job control stop.\n"
6197"\n"
6198"Return True if the process returning status was continued from a\n"
6199"job control stop.");
6200
6201#define OS_WIFCONTINUED_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02006202 {"WIFCONTINUED", (PyCFunction)(void(*)(void))os_WIFCONTINUED, METH_FASTCALL|METH_KEYWORDS, os_WIFCONTINUED__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006203
6204static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006205os_WIFCONTINUED_impl(PyObject *module, int status);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006206
6207static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02006208os_WIFCONTINUED(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006209{
6210 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03006211 static const char * const _keywords[] = {"status", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02006212 static _PyArg_Parser _parser = {NULL, _keywords, "WIFCONTINUED", 0};
6213 PyObject *argsbuf[1];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006214 int status;
6215 int _return_value;
6216
Serhiy Storchaka31913912019-03-14 10:32:22 +02006217 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
6218 if (!args) {
6219 goto exit;
6220 }
6221 if (PyFloat_Check(args[0])) {
6222 PyErr_SetString(PyExc_TypeError,
6223 "integer argument expected, got float" );
6224 goto exit;
6225 }
6226 status = _PyLong_AsInt(args[0]);
6227 if (status == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006228 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006229 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006230 _return_value = os_WIFCONTINUED_impl(module, status);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006231 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006232 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006233 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006234 return_value = PyBool_FromLong((long)_return_value);
6235
6236exit:
6237 return return_value;
6238}
6239
6240#endif /* defined(HAVE_SYS_WAIT_H) && defined(WIFCONTINUED) */
6241
6242#if defined(HAVE_SYS_WAIT_H) && defined(WIFSTOPPED)
6243
6244PyDoc_STRVAR(os_WIFSTOPPED__doc__,
6245"WIFSTOPPED($module, /, status)\n"
6246"--\n"
6247"\n"
6248"Return True if the process returning status was stopped.");
6249
6250#define OS_WIFSTOPPED_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02006251 {"WIFSTOPPED", (PyCFunction)(void(*)(void))os_WIFSTOPPED, METH_FASTCALL|METH_KEYWORDS, os_WIFSTOPPED__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006252
6253static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006254os_WIFSTOPPED_impl(PyObject *module, int status);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006255
6256static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02006257os_WIFSTOPPED(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006258{
6259 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03006260 static const char * const _keywords[] = {"status", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02006261 static _PyArg_Parser _parser = {NULL, _keywords, "WIFSTOPPED", 0};
6262 PyObject *argsbuf[1];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006263 int status;
6264 int _return_value;
6265
Serhiy Storchaka31913912019-03-14 10:32:22 +02006266 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
6267 if (!args) {
6268 goto exit;
6269 }
6270 if (PyFloat_Check(args[0])) {
6271 PyErr_SetString(PyExc_TypeError,
6272 "integer argument expected, got float" );
6273 goto exit;
6274 }
6275 status = _PyLong_AsInt(args[0]);
6276 if (status == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006277 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006278 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006279 _return_value = os_WIFSTOPPED_impl(module, status);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006280 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006281 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006282 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006283 return_value = PyBool_FromLong((long)_return_value);
6284
6285exit:
6286 return return_value;
6287}
6288
6289#endif /* defined(HAVE_SYS_WAIT_H) && defined(WIFSTOPPED) */
6290
6291#if defined(HAVE_SYS_WAIT_H) && defined(WIFSIGNALED)
6292
6293PyDoc_STRVAR(os_WIFSIGNALED__doc__,
6294"WIFSIGNALED($module, /, status)\n"
6295"--\n"
6296"\n"
6297"Return True if the process returning status was terminated by a signal.");
6298
6299#define OS_WIFSIGNALED_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02006300 {"WIFSIGNALED", (PyCFunction)(void(*)(void))os_WIFSIGNALED, METH_FASTCALL|METH_KEYWORDS, os_WIFSIGNALED__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006301
6302static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006303os_WIFSIGNALED_impl(PyObject *module, int status);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006304
6305static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02006306os_WIFSIGNALED(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006307{
6308 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03006309 static const char * const _keywords[] = {"status", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02006310 static _PyArg_Parser _parser = {NULL, _keywords, "WIFSIGNALED", 0};
6311 PyObject *argsbuf[1];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006312 int status;
6313 int _return_value;
6314
Serhiy Storchaka31913912019-03-14 10:32:22 +02006315 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
6316 if (!args) {
6317 goto exit;
6318 }
6319 if (PyFloat_Check(args[0])) {
6320 PyErr_SetString(PyExc_TypeError,
6321 "integer argument expected, got float" );
6322 goto exit;
6323 }
6324 status = _PyLong_AsInt(args[0]);
6325 if (status == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006326 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006327 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006328 _return_value = os_WIFSIGNALED_impl(module, status);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006329 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006330 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006331 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006332 return_value = PyBool_FromLong((long)_return_value);
6333
6334exit:
6335 return return_value;
6336}
6337
6338#endif /* defined(HAVE_SYS_WAIT_H) && defined(WIFSIGNALED) */
6339
6340#if defined(HAVE_SYS_WAIT_H) && defined(WIFEXITED)
6341
6342PyDoc_STRVAR(os_WIFEXITED__doc__,
6343"WIFEXITED($module, /, status)\n"
6344"--\n"
6345"\n"
6346"Return True if the process returning status exited via the exit() system call.");
6347
6348#define OS_WIFEXITED_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02006349 {"WIFEXITED", (PyCFunction)(void(*)(void))os_WIFEXITED, METH_FASTCALL|METH_KEYWORDS, os_WIFEXITED__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006350
6351static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006352os_WIFEXITED_impl(PyObject *module, int status);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006353
6354static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02006355os_WIFEXITED(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006356{
6357 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03006358 static const char * const _keywords[] = {"status", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02006359 static _PyArg_Parser _parser = {NULL, _keywords, "WIFEXITED", 0};
6360 PyObject *argsbuf[1];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006361 int status;
6362 int _return_value;
6363
Serhiy Storchaka31913912019-03-14 10:32:22 +02006364 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
6365 if (!args) {
6366 goto exit;
6367 }
6368 if (PyFloat_Check(args[0])) {
6369 PyErr_SetString(PyExc_TypeError,
6370 "integer argument expected, got float" );
6371 goto exit;
6372 }
6373 status = _PyLong_AsInt(args[0]);
6374 if (status == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006375 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006376 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006377 _return_value = os_WIFEXITED_impl(module, status);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006378 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006379 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006380 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006381 return_value = PyBool_FromLong((long)_return_value);
6382
6383exit:
6384 return return_value;
6385}
6386
6387#endif /* defined(HAVE_SYS_WAIT_H) && defined(WIFEXITED) */
6388
6389#if defined(HAVE_SYS_WAIT_H) && defined(WEXITSTATUS)
6390
6391PyDoc_STRVAR(os_WEXITSTATUS__doc__,
6392"WEXITSTATUS($module, /, status)\n"
6393"--\n"
6394"\n"
6395"Return the process return code from status.");
6396
6397#define OS_WEXITSTATUS_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02006398 {"WEXITSTATUS", (PyCFunction)(void(*)(void))os_WEXITSTATUS, METH_FASTCALL|METH_KEYWORDS, os_WEXITSTATUS__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006399
6400static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006401os_WEXITSTATUS_impl(PyObject *module, int status);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006402
6403static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02006404os_WEXITSTATUS(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006405{
6406 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03006407 static const char * const _keywords[] = {"status", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02006408 static _PyArg_Parser _parser = {NULL, _keywords, "WEXITSTATUS", 0};
6409 PyObject *argsbuf[1];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006410 int status;
6411 int _return_value;
6412
Serhiy Storchaka31913912019-03-14 10:32:22 +02006413 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
6414 if (!args) {
6415 goto exit;
6416 }
6417 if (PyFloat_Check(args[0])) {
6418 PyErr_SetString(PyExc_TypeError,
6419 "integer argument expected, got float" );
6420 goto exit;
6421 }
6422 status = _PyLong_AsInt(args[0]);
6423 if (status == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006424 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006425 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006426 _return_value = os_WEXITSTATUS_impl(module, status);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006427 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006428 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006429 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006430 return_value = PyLong_FromLong((long)_return_value);
6431
6432exit:
6433 return return_value;
6434}
6435
6436#endif /* defined(HAVE_SYS_WAIT_H) && defined(WEXITSTATUS) */
6437
6438#if defined(HAVE_SYS_WAIT_H) && defined(WTERMSIG)
6439
6440PyDoc_STRVAR(os_WTERMSIG__doc__,
6441"WTERMSIG($module, /, status)\n"
6442"--\n"
6443"\n"
6444"Return the signal that terminated the process that provided the status value.");
6445
6446#define OS_WTERMSIG_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02006447 {"WTERMSIG", (PyCFunction)(void(*)(void))os_WTERMSIG, METH_FASTCALL|METH_KEYWORDS, os_WTERMSIG__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006448
6449static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006450os_WTERMSIG_impl(PyObject *module, int status);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006451
6452static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02006453os_WTERMSIG(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006454{
6455 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03006456 static const char * const _keywords[] = {"status", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02006457 static _PyArg_Parser _parser = {NULL, _keywords, "WTERMSIG", 0};
6458 PyObject *argsbuf[1];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006459 int status;
6460 int _return_value;
6461
Serhiy Storchaka31913912019-03-14 10:32:22 +02006462 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
6463 if (!args) {
6464 goto exit;
6465 }
6466 if (PyFloat_Check(args[0])) {
6467 PyErr_SetString(PyExc_TypeError,
6468 "integer argument expected, got float" );
6469 goto exit;
6470 }
6471 status = _PyLong_AsInt(args[0]);
6472 if (status == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006473 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006474 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006475 _return_value = os_WTERMSIG_impl(module, status);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006476 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006477 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006478 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006479 return_value = PyLong_FromLong((long)_return_value);
6480
6481exit:
6482 return return_value;
6483}
6484
6485#endif /* defined(HAVE_SYS_WAIT_H) && defined(WTERMSIG) */
6486
6487#if defined(HAVE_SYS_WAIT_H) && defined(WSTOPSIG)
6488
6489PyDoc_STRVAR(os_WSTOPSIG__doc__,
6490"WSTOPSIG($module, /, status)\n"
6491"--\n"
6492"\n"
6493"Return the signal that stopped the process that provided the status value.");
6494
6495#define OS_WSTOPSIG_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02006496 {"WSTOPSIG", (PyCFunction)(void(*)(void))os_WSTOPSIG, METH_FASTCALL|METH_KEYWORDS, os_WSTOPSIG__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006497
6498static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006499os_WSTOPSIG_impl(PyObject *module, int status);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006500
6501static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02006502os_WSTOPSIG(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006503{
6504 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03006505 static const char * const _keywords[] = {"status", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02006506 static _PyArg_Parser _parser = {NULL, _keywords, "WSTOPSIG", 0};
6507 PyObject *argsbuf[1];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006508 int status;
6509 int _return_value;
6510
Serhiy Storchaka31913912019-03-14 10:32:22 +02006511 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
6512 if (!args) {
6513 goto exit;
6514 }
6515 if (PyFloat_Check(args[0])) {
6516 PyErr_SetString(PyExc_TypeError,
6517 "integer argument expected, got float" );
6518 goto exit;
6519 }
6520 status = _PyLong_AsInt(args[0]);
6521 if (status == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006522 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006523 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006524 _return_value = os_WSTOPSIG_impl(module, status);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006525 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006526 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006527 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006528 return_value = PyLong_FromLong((long)_return_value);
6529
6530exit:
6531 return return_value;
6532}
6533
6534#endif /* defined(HAVE_SYS_WAIT_H) && defined(WSTOPSIG) */
6535
6536#if (defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H))
6537
6538PyDoc_STRVAR(os_fstatvfs__doc__,
6539"fstatvfs($module, fd, /)\n"
6540"--\n"
6541"\n"
6542"Perform an fstatvfs system call on the given fd.\n"
6543"\n"
6544"Equivalent to statvfs(fd).");
6545
6546#define OS_FSTATVFS_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03006547 {"fstatvfs", (PyCFunction)os_fstatvfs, METH_O, os_fstatvfs__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006548
6549static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006550os_fstatvfs_impl(PyObject *module, int fd);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006551
6552static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006553os_fstatvfs(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006554{
6555 PyObject *return_value = NULL;
6556 int fd;
6557
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02006558 if (PyFloat_Check(arg)) {
6559 PyErr_SetString(PyExc_TypeError,
6560 "integer argument expected, got float" );
6561 goto exit;
6562 }
6563 fd = _PyLong_AsInt(arg);
6564 if (fd == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006565 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006566 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006567 return_value = os_fstatvfs_impl(module, fd);
6568
6569exit:
6570 return return_value;
6571}
6572
6573#endif /* (defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)) */
6574
6575#if (defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H))
6576
6577PyDoc_STRVAR(os_statvfs__doc__,
6578"statvfs($module, /, path)\n"
6579"--\n"
6580"\n"
6581"Perform a statvfs system call on the given path.\n"
6582"\n"
6583"path may always be specified as a string.\n"
6584"On some platforms, path may also be specified as an open file descriptor.\n"
6585" If this functionality is unavailable, using it raises an exception.");
6586
6587#define OS_STATVFS_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02006588 {"statvfs", (PyCFunction)(void(*)(void))os_statvfs, METH_FASTCALL|METH_KEYWORDS, os_statvfs__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006589
6590static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006591os_statvfs_impl(PyObject *module, path_t *path);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006592
6593static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02006594os_statvfs(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006595{
6596 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03006597 static const char * const _keywords[] = {"path", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02006598 static _PyArg_Parser _parser = {NULL, _keywords, "statvfs", 0};
6599 PyObject *argsbuf[1];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006600 path_t path = PATH_T_INITIALIZE("statvfs", "path", 0, PATH_HAVE_FSTATVFS);
6601
Serhiy Storchaka31913912019-03-14 10:32:22 +02006602 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
6603 if (!args) {
6604 goto exit;
6605 }
6606 if (!path_converter(args[0], &path)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006607 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006608 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006609 return_value = os_statvfs_impl(module, &path);
6610
6611exit:
6612 /* Cleanup for path */
6613 path_cleanup(&path);
6614
6615 return return_value;
6616}
6617
6618#endif /* (defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)) */
6619
6620#if defined(MS_WINDOWS)
6621
6622PyDoc_STRVAR(os__getdiskusage__doc__,
6623"_getdiskusage($module, /, path)\n"
6624"--\n"
6625"\n"
6626"Return disk usage statistics about the given path as a (total, free) tuple.");
6627
6628#define OS__GETDISKUSAGE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02006629 {"_getdiskusage", (PyCFunction)(void(*)(void))os__getdiskusage, METH_FASTCALL|METH_KEYWORDS, os__getdiskusage__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006630
6631static PyObject *
Steve Dower23ad6d02018-02-22 10:39:10 -08006632os__getdiskusage_impl(PyObject *module, path_t *path);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006633
6634static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02006635os__getdiskusage(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006636{
6637 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03006638 static const char * const _keywords[] = {"path", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02006639 static _PyArg_Parser _parser = {NULL, _keywords, "_getdiskusage", 0};
6640 PyObject *argsbuf[1];
Steve Dower23ad6d02018-02-22 10:39:10 -08006641 path_t path = PATH_T_INITIALIZE("_getdiskusage", "path", 0, 0);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006642
Serhiy Storchaka31913912019-03-14 10:32:22 +02006643 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
6644 if (!args) {
6645 goto exit;
6646 }
6647 if (!path_converter(args[0], &path)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006648 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006649 }
Steve Dower23ad6d02018-02-22 10:39:10 -08006650 return_value = os__getdiskusage_impl(module, &path);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006651
6652exit:
Steve Dower23ad6d02018-02-22 10:39:10 -08006653 /* Cleanup for path */
6654 path_cleanup(&path);
6655
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006656 return return_value;
6657}
6658
6659#endif /* defined(MS_WINDOWS) */
6660
6661#if defined(HAVE_FPATHCONF)
6662
6663PyDoc_STRVAR(os_fpathconf__doc__,
6664"fpathconf($module, fd, name, /)\n"
6665"--\n"
6666"\n"
6667"Return the configuration limit name for the file descriptor fd.\n"
6668"\n"
6669"If there is no limit, return -1.");
6670
6671#define OS_FPATHCONF_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02006672 {"fpathconf", (PyCFunction)(void(*)(void))os_fpathconf, METH_FASTCALL, os_fpathconf__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006673
6674static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006675os_fpathconf_impl(PyObject *module, int fd, int name);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006676
6677static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02006678os_fpathconf(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006679{
6680 PyObject *return_value = NULL;
6681 int fd;
6682 int name;
6683 long _return_value;
6684
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02006685 if (!_PyArg_CheckPositional("fpathconf", nargs, 2, 2)) {
6686 goto exit;
6687 }
6688 if (PyFloat_Check(args[0])) {
6689 PyErr_SetString(PyExc_TypeError,
6690 "integer argument expected, got float" );
6691 goto exit;
6692 }
6693 fd = _PyLong_AsInt(args[0]);
6694 if (fd == -1 && PyErr_Occurred()) {
6695 goto exit;
6696 }
6697 if (!conv_path_confname(args[1], &name)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01006698 goto exit;
6699 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006700 _return_value = os_fpathconf_impl(module, fd, name);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006701 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006702 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006703 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006704 return_value = PyLong_FromLong(_return_value);
6705
6706exit:
6707 return return_value;
6708}
6709
6710#endif /* defined(HAVE_FPATHCONF) */
6711
6712#if defined(HAVE_PATHCONF)
6713
6714PyDoc_STRVAR(os_pathconf__doc__,
6715"pathconf($module, /, path, name)\n"
6716"--\n"
6717"\n"
6718"Return the configuration limit name for the file or directory path.\n"
6719"\n"
6720"If there is no limit, return -1.\n"
6721"On some platforms, path may also be specified as an open file descriptor.\n"
6722" If this functionality is unavailable, using it raises an exception.");
6723
6724#define OS_PATHCONF_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02006725 {"pathconf", (PyCFunction)(void(*)(void))os_pathconf, METH_FASTCALL|METH_KEYWORDS, os_pathconf__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006726
6727static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006728os_pathconf_impl(PyObject *module, path_t *path, int name);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006729
6730static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02006731os_pathconf(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006732{
6733 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03006734 static const char * const _keywords[] = {"path", "name", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02006735 static _PyArg_Parser _parser = {NULL, _keywords, "pathconf", 0};
6736 PyObject *argsbuf[2];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006737 path_t path = PATH_T_INITIALIZE("pathconf", "path", 0, PATH_HAVE_FPATHCONF);
6738 int name;
6739 long _return_value;
6740
Serhiy Storchaka31913912019-03-14 10:32:22 +02006741 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf);
6742 if (!args) {
6743 goto exit;
6744 }
6745 if (!path_converter(args[0], &path)) {
6746 goto exit;
6747 }
6748 if (!conv_path_confname(args[1], &name)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006749 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006750 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006751 _return_value = os_pathconf_impl(module, &path, name);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006752 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006753 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006754 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006755 return_value = PyLong_FromLong(_return_value);
6756
6757exit:
6758 /* Cleanup for path */
6759 path_cleanup(&path);
6760
6761 return return_value;
6762}
6763
6764#endif /* defined(HAVE_PATHCONF) */
6765
6766#if defined(HAVE_CONFSTR)
6767
6768PyDoc_STRVAR(os_confstr__doc__,
6769"confstr($module, name, /)\n"
6770"--\n"
6771"\n"
6772"Return a string-valued system configuration variable.");
6773
6774#define OS_CONFSTR_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03006775 {"confstr", (PyCFunction)os_confstr, METH_O, os_confstr__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006776
6777static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006778os_confstr_impl(PyObject *module, int name);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006779
6780static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006781os_confstr(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006782{
6783 PyObject *return_value = NULL;
6784 int name;
6785
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02006786 if (!conv_confstr_confname(arg, &name)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006787 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006788 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006789 return_value = os_confstr_impl(module, name);
6790
6791exit:
6792 return return_value;
6793}
6794
6795#endif /* defined(HAVE_CONFSTR) */
6796
6797#if defined(HAVE_SYSCONF)
6798
6799PyDoc_STRVAR(os_sysconf__doc__,
6800"sysconf($module, name, /)\n"
6801"--\n"
6802"\n"
6803"Return an integer-valued system configuration variable.");
6804
6805#define OS_SYSCONF_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03006806 {"sysconf", (PyCFunction)os_sysconf, METH_O, os_sysconf__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006807
6808static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006809os_sysconf_impl(PyObject *module, int name);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006810
6811static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006812os_sysconf(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006813{
6814 PyObject *return_value = NULL;
6815 int name;
6816 long _return_value;
6817
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02006818 if (!conv_sysconf_confname(arg, &name)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006819 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006820 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006821 _return_value = os_sysconf_impl(module, name);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006822 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006823 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006824 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006825 return_value = PyLong_FromLong(_return_value);
6826
6827exit:
6828 return return_value;
6829}
6830
6831#endif /* defined(HAVE_SYSCONF) */
6832
6833PyDoc_STRVAR(os_abort__doc__,
6834"abort($module, /)\n"
6835"--\n"
6836"\n"
6837"Abort the interpreter immediately.\n"
6838"\n"
6839"This function \'dumps core\' or otherwise fails in the hardest way possible\n"
6840"on the hosting operating system. This function never returns.");
6841
6842#define OS_ABORT_METHODDEF \
6843 {"abort", (PyCFunction)os_abort, METH_NOARGS, os_abort__doc__},
6844
6845static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006846os_abort_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006847
6848static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006849os_abort(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006850{
6851 return os_abort_impl(module);
6852}
6853
Steve Dowercc16be82016-09-08 10:35:16 -07006854#if defined(MS_WINDOWS)
6855
6856PyDoc_STRVAR(os_startfile__doc__,
6857"startfile($module, /, filepath, operation=None)\n"
6858"--\n"
6859"\n"
6860"startfile(filepath [, operation])\n"
6861"\n"
6862"Start a file with its associated application.\n"
6863"\n"
6864"When \"operation\" is not specified or \"open\", this acts like\n"
6865"double-clicking the file in Explorer, or giving the file name as an\n"
6866"argument to the DOS \"start\" command: the file is opened with whatever\n"
6867"application (if any) its extension is associated.\n"
6868"When another \"operation\" is given, it specifies what should be done with\n"
6869"the file. A typical operation is \"print\".\n"
6870"\n"
6871"startfile returns as soon as the associated application is launched.\n"
6872"There is no option to wait for the application to close, and no way\n"
6873"to retrieve the application\'s exit status.\n"
6874"\n"
6875"The filepath is relative to the current directory. If you want to use\n"
6876"an absolute path, make sure the first character is not a slash (\"/\");\n"
6877"the underlying Win32 ShellExecute function doesn\'t work if it is.");
6878
6879#define OS_STARTFILE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02006880 {"startfile", (PyCFunction)(void(*)(void))os_startfile, METH_FASTCALL|METH_KEYWORDS, os_startfile__doc__},
Steve Dowercc16be82016-09-08 10:35:16 -07006881
6882static PyObject *
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02006883os_startfile_impl(PyObject *module, path_t *filepath,
6884 const Py_UNICODE *operation);
Steve Dowercc16be82016-09-08 10:35:16 -07006885
6886static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02006887os_startfile(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Steve Dowercc16be82016-09-08 10:35:16 -07006888{
6889 PyObject *return_value = NULL;
Victor Stinner37e4ef72016-09-09 20:00:13 -07006890 static const char * const _keywords[] = {"filepath", "operation", NULL};
6891 static _PyArg_Parser _parser = {"O&|u:startfile", _keywords, 0};
Steve Dowercc16be82016-09-08 10:35:16 -07006892 path_t filepath = PATH_T_INITIALIZE("startfile", "filepath", 0, 0);
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02006893 const Py_UNICODE *operation = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07006894
Victor Stinner3e1fad62017-01-17 01:29:01 +01006895 if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
Steve Dowercc16be82016-09-08 10:35:16 -07006896 path_converter, &filepath, &operation)) {
6897 goto exit;
6898 }
6899 return_value = os_startfile_impl(module, &filepath, operation);
6900
6901exit:
6902 /* Cleanup for filepath */
6903 path_cleanup(&filepath);
6904
6905 return return_value;
6906}
6907
6908#endif /* defined(MS_WINDOWS) */
6909
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006910#if defined(HAVE_GETLOADAVG)
6911
6912PyDoc_STRVAR(os_getloadavg__doc__,
6913"getloadavg($module, /)\n"
6914"--\n"
6915"\n"
6916"Return average recent system load information.\n"
6917"\n"
6918"Return the number of processes in the system run queue averaged over\n"
6919"the last 1, 5, and 15 minutes as a tuple of three floats.\n"
6920"Raises OSError if the load average was unobtainable.");
6921
6922#define OS_GETLOADAVG_METHODDEF \
6923 {"getloadavg", (PyCFunction)os_getloadavg, METH_NOARGS, os_getloadavg__doc__},
6924
6925static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006926os_getloadavg_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006927
6928static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006929os_getloadavg(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006930{
6931 return os_getloadavg_impl(module);
6932}
6933
6934#endif /* defined(HAVE_GETLOADAVG) */
6935
6936PyDoc_STRVAR(os_device_encoding__doc__,
6937"device_encoding($module, /, fd)\n"
6938"--\n"
6939"\n"
6940"Return a string describing the encoding of a terminal\'s file descriptor.\n"
6941"\n"
6942"The file descriptor must be attached to a terminal.\n"
6943"If the device is not a terminal, return None.");
6944
6945#define OS_DEVICE_ENCODING_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02006946 {"device_encoding", (PyCFunction)(void(*)(void))os_device_encoding, METH_FASTCALL|METH_KEYWORDS, os_device_encoding__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006947
6948static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006949os_device_encoding_impl(PyObject *module, int fd);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006950
6951static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02006952os_device_encoding(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006953{
6954 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03006955 static const char * const _keywords[] = {"fd", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02006956 static _PyArg_Parser _parser = {NULL, _keywords, "device_encoding", 0};
6957 PyObject *argsbuf[1];
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006958 int fd;
6959
Serhiy Storchaka31913912019-03-14 10:32:22 +02006960 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
6961 if (!args) {
6962 goto exit;
6963 }
6964 if (PyFloat_Check(args[0])) {
6965 PyErr_SetString(PyExc_TypeError,
6966 "integer argument expected, got float" );
6967 goto exit;
6968 }
6969 fd = _PyLong_AsInt(args[0]);
6970 if (fd == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006971 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03006972 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006973 return_value = os_device_encoding_impl(module, fd);
6974
6975exit:
6976 return return_value;
6977}
6978
6979#if defined(HAVE_SETRESUID)
6980
6981PyDoc_STRVAR(os_setresuid__doc__,
6982"setresuid($module, ruid, euid, suid, /)\n"
6983"--\n"
6984"\n"
6985"Set the current process\'s real, effective, and saved user ids.");
6986
6987#define OS_SETRESUID_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02006988 {"setresuid", (PyCFunction)(void(*)(void))os_setresuid, METH_FASTCALL, os_setresuid__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006989
6990static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006991os_setresuid_impl(PyObject *module, uid_t ruid, uid_t euid, uid_t suid);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006992
6993static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02006994os_setresuid(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006995{
6996 PyObject *return_value = NULL;
6997 uid_t ruid;
6998 uid_t euid;
6999 uid_t suid;
7000
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02007001 if (!_PyArg_CheckPositional("setresuid", nargs, 3, 3)) {
7002 goto exit;
7003 }
7004 if (!_Py_Uid_Converter(args[0], &ruid)) {
7005 goto exit;
7006 }
7007 if (!_Py_Uid_Converter(args[1], &euid)) {
7008 goto exit;
7009 }
7010 if (!_Py_Uid_Converter(args[2], &suid)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01007011 goto exit;
7012 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007013 return_value = os_setresuid_impl(module, ruid, euid, suid);
7014
7015exit:
7016 return return_value;
7017}
7018
7019#endif /* defined(HAVE_SETRESUID) */
7020
7021#if defined(HAVE_SETRESGID)
7022
7023PyDoc_STRVAR(os_setresgid__doc__,
7024"setresgid($module, rgid, egid, sgid, /)\n"
7025"--\n"
7026"\n"
7027"Set the current process\'s real, effective, and saved group ids.");
7028
7029#define OS_SETRESGID_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02007030 {"setresgid", (PyCFunction)(void(*)(void))os_setresgid, METH_FASTCALL, os_setresgid__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007031
7032static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007033os_setresgid_impl(PyObject *module, gid_t rgid, gid_t egid, gid_t sgid);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007034
7035static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02007036os_setresgid(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007037{
7038 PyObject *return_value = NULL;
7039 gid_t rgid;
7040 gid_t egid;
7041 gid_t sgid;
7042
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02007043 if (!_PyArg_CheckPositional("setresgid", nargs, 3, 3)) {
7044 goto exit;
7045 }
7046 if (!_Py_Gid_Converter(args[0], &rgid)) {
7047 goto exit;
7048 }
7049 if (!_Py_Gid_Converter(args[1], &egid)) {
7050 goto exit;
7051 }
7052 if (!_Py_Gid_Converter(args[2], &sgid)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01007053 goto exit;
7054 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007055 return_value = os_setresgid_impl(module, rgid, egid, sgid);
7056
7057exit:
7058 return return_value;
7059}
7060
7061#endif /* defined(HAVE_SETRESGID) */
7062
7063#if defined(HAVE_GETRESUID)
7064
7065PyDoc_STRVAR(os_getresuid__doc__,
7066"getresuid($module, /)\n"
7067"--\n"
7068"\n"
7069"Return a tuple of the current process\'s real, effective, and saved user ids.");
7070
7071#define OS_GETRESUID_METHODDEF \
7072 {"getresuid", (PyCFunction)os_getresuid, METH_NOARGS, os_getresuid__doc__},
7073
7074static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007075os_getresuid_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007076
7077static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007078os_getresuid(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007079{
7080 return os_getresuid_impl(module);
7081}
7082
7083#endif /* defined(HAVE_GETRESUID) */
7084
7085#if defined(HAVE_GETRESGID)
7086
7087PyDoc_STRVAR(os_getresgid__doc__,
7088"getresgid($module, /)\n"
7089"--\n"
7090"\n"
7091"Return a tuple of the current process\'s real, effective, and saved group ids.");
7092
7093#define OS_GETRESGID_METHODDEF \
7094 {"getresgid", (PyCFunction)os_getresgid, METH_NOARGS, os_getresgid__doc__},
7095
7096static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007097os_getresgid_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007098
7099static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007100os_getresgid(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007101{
7102 return os_getresgid_impl(module);
7103}
7104
7105#endif /* defined(HAVE_GETRESGID) */
7106
7107#if defined(USE_XATTRS)
7108
7109PyDoc_STRVAR(os_getxattr__doc__,
7110"getxattr($module, /, path, attribute, *, follow_symlinks=True)\n"
7111"--\n"
7112"\n"
7113"Return the value of extended attribute attribute on path.\n"
7114"\n"
BNMetricsb9427072018-11-02 15:20:19 +00007115"path may be either a string, a path-like object, or an open file descriptor.\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007116"If follow_symlinks is False, and the last element of the path is a symbolic\n"
7117" link, getxattr will examine the symbolic link itself instead of the file\n"
7118" the link points to.");
7119
7120#define OS_GETXATTR_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02007121 {"getxattr", (PyCFunction)(void(*)(void))os_getxattr, METH_FASTCALL|METH_KEYWORDS, os_getxattr__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007122
7123static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007124os_getxattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -04007125 int follow_symlinks);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007126
7127static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02007128os_getxattr(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007129{
7130 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03007131 static const char * const _keywords[] = {"path", "attribute", "follow_symlinks", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02007132 static _PyArg_Parser _parser = {NULL, _keywords, "getxattr", 0};
7133 PyObject *argsbuf[3];
7134 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007135 path_t path = PATH_T_INITIALIZE("getxattr", "path", 0, 1);
7136 path_t attribute = PATH_T_INITIALIZE("getxattr", "attribute", 0, 0);
7137 int follow_symlinks = 1;
7138
Serhiy Storchaka31913912019-03-14 10:32:22 +02007139 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf);
7140 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007141 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03007142 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02007143 if (!path_converter(args[0], &path)) {
7144 goto exit;
7145 }
7146 if (!path_converter(args[1], &attribute)) {
7147 goto exit;
7148 }
7149 if (!noptargs) {
7150 goto skip_optional_kwonly;
7151 }
7152 follow_symlinks = PyObject_IsTrue(args[2]);
7153 if (follow_symlinks < 0) {
7154 goto exit;
7155 }
7156skip_optional_kwonly:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007157 return_value = os_getxattr_impl(module, &path, &attribute, follow_symlinks);
7158
7159exit:
7160 /* Cleanup for path */
7161 path_cleanup(&path);
7162 /* Cleanup for attribute */
7163 path_cleanup(&attribute);
7164
7165 return return_value;
7166}
7167
7168#endif /* defined(USE_XATTRS) */
7169
7170#if defined(USE_XATTRS)
7171
7172PyDoc_STRVAR(os_setxattr__doc__,
7173"setxattr($module, /, path, attribute, value, flags=0, *,\n"
7174" follow_symlinks=True)\n"
7175"--\n"
7176"\n"
7177"Set extended attribute attribute on path to value.\n"
7178"\n"
BNMetricsb9427072018-11-02 15:20:19 +00007179"path may be either a string, a path-like object, or an open file descriptor.\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007180"If follow_symlinks is False, and the last element of the path is a symbolic\n"
7181" link, setxattr will modify the symbolic link itself instead of the file\n"
7182" the link points to.");
7183
7184#define OS_SETXATTR_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02007185 {"setxattr", (PyCFunction)(void(*)(void))os_setxattr, METH_FASTCALL|METH_KEYWORDS, os_setxattr__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007186
7187static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007188os_setxattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -04007189 Py_buffer *value, int flags, int follow_symlinks);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007190
7191static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02007192os_setxattr(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007193{
7194 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03007195 static const char * const _keywords[] = {"path", "attribute", "value", "flags", "follow_symlinks", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02007196 static _PyArg_Parser _parser = {NULL, _keywords, "setxattr", 0};
7197 PyObject *argsbuf[5];
7198 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 3;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007199 path_t path = PATH_T_INITIALIZE("setxattr", "path", 0, 1);
7200 path_t attribute = PATH_T_INITIALIZE("setxattr", "attribute", 0, 0);
7201 Py_buffer value = {NULL, NULL};
7202 int flags = 0;
7203 int follow_symlinks = 1;
7204
Serhiy Storchaka31913912019-03-14 10:32:22 +02007205 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 3, 4, 0, argsbuf);
7206 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007207 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03007208 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02007209 if (!path_converter(args[0], &path)) {
7210 goto exit;
7211 }
7212 if (!path_converter(args[1], &attribute)) {
7213 goto exit;
7214 }
7215 if (PyObject_GetBuffer(args[2], &value, PyBUF_SIMPLE) != 0) {
7216 goto exit;
7217 }
7218 if (!PyBuffer_IsContiguous(&value, 'C')) {
7219 _PyArg_BadArgument("setxattr", 3, "contiguous buffer", args[2]);
7220 goto exit;
7221 }
7222 if (!noptargs) {
7223 goto skip_optional_pos;
7224 }
7225 if (args[3]) {
7226 if (PyFloat_Check(args[3])) {
7227 PyErr_SetString(PyExc_TypeError,
7228 "integer argument expected, got float" );
7229 goto exit;
7230 }
7231 flags = _PyLong_AsInt(args[3]);
7232 if (flags == -1 && PyErr_Occurred()) {
7233 goto exit;
7234 }
7235 if (!--noptargs) {
7236 goto skip_optional_pos;
7237 }
7238 }
7239skip_optional_pos:
7240 if (!noptargs) {
7241 goto skip_optional_kwonly;
7242 }
7243 follow_symlinks = PyObject_IsTrue(args[4]);
7244 if (follow_symlinks < 0) {
7245 goto exit;
7246 }
7247skip_optional_kwonly:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007248 return_value = os_setxattr_impl(module, &path, &attribute, &value, flags, follow_symlinks);
7249
7250exit:
7251 /* Cleanup for path */
7252 path_cleanup(&path);
7253 /* Cleanup for attribute */
7254 path_cleanup(&attribute);
7255 /* Cleanup for value */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03007256 if (value.obj) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007257 PyBuffer_Release(&value);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03007258 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007259
7260 return return_value;
7261}
7262
7263#endif /* defined(USE_XATTRS) */
7264
7265#if defined(USE_XATTRS)
7266
7267PyDoc_STRVAR(os_removexattr__doc__,
7268"removexattr($module, /, path, attribute, *, follow_symlinks=True)\n"
7269"--\n"
7270"\n"
7271"Remove extended attribute attribute on path.\n"
7272"\n"
BNMetricsb9427072018-11-02 15:20:19 +00007273"path may be either a string, a path-like object, or an open file descriptor.\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007274"If follow_symlinks is False, and the last element of the path is a symbolic\n"
7275" link, removexattr will modify the symbolic link itself instead of the file\n"
7276" the link points to.");
7277
7278#define OS_REMOVEXATTR_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02007279 {"removexattr", (PyCFunction)(void(*)(void))os_removexattr, METH_FASTCALL|METH_KEYWORDS, os_removexattr__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007280
7281static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007282os_removexattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -04007283 int follow_symlinks);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007284
7285static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02007286os_removexattr(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007287{
7288 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03007289 static const char * const _keywords[] = {"path", "attribute", "follow_symlinks", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02007290 static _PyArg_Parser _parser = {NULL, _keywords, "removexattr", 0};
7291 PyObject *argsbuf[3];
7292 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007293 path_t path = PATH_T_INITIALIZE("removexattr", "path", 0, 1);
7294 path_t attribute = PATH_T_INITIALIZE("removexattr", "attribute", 0, 0);
7295 int follow_symlinks = 1;
7296
Serhiy Storchaka31913912019-03-14 10:32:22 +02007297 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf);
7298 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007299 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03007300 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02007301 if (!path_converter(args[0], &path)) {
7302 goto exit;
7303 }
7304 if (!path_converter(args[1], &attribute)) {
7305 goto exit;
7306 }
7307 if (!noptargs) {
7308 goto skip_optional_kwonly;
7309 }
7310 follow_symlinks = PyObject_IsTrue(args[2]);
7311 if (follow_symlinks < 0) {
7312 goto exit;
7313 }
7314skip_optional_kwonly:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007315 return_value = os_removexattr_impl(module, &path, &attribute, follow_symlinks);
7316
7317exit:
7318 /* Cleanup for path */
7319 path_cleanup(&path);
7320 /* Cleanup for attribute */
7321 path_cleanup(&attribute);
7322
7323 return return_value;
7324}
7325
7326#endif /* defined(USE_XATTRS) */
7327
7328#if defined(USE_XATTRS)
7329
7330PyDoc_STRVAR(os_listxattr__doc__,
7331"listxattr($module, /, path=None, *, follow_symlinks=True)\n"
7332"--\n"
7333"\n"
7334"Return a list of extended attributes on path.\n"
7335"\n"
BNMetricsb9427072018-11-02 15:20:19 +00007336"path may be either None, a string, a path-like object, or an open file descriptor.\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007337"if path is None, listxattr will examine the current directory.\n"
7338"If follow_symlinks is False, and the last element of the path is a symbolic\n"
7339" link, listxattr will examine the symbolic link itself instead of the file\n"
7340" the link points to.");
7341
7342#define OS_LISTXATTR_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02007343 {"listxattr", (PyCFunction)(void(*)(void))os_listxattr, METH_FASTCALL|METH_KEYWORDS, os_listxattr__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007344
7345static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007346os_listxattr_impl(PyObject *module, path_t *path, int follow_symlinks);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007347
7348static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02007349os_listxattr(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007350{
7351 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03007352 static const char * const _keywords[] = {"path", "follow_symlinks", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02007353 static _PyArg_Parser _parser = {NULL, _keywords, "listxattr", 0};
7354 PyObject *argsbuf[2];
7355 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007356 path_t path = PATH_T_INITIALIZE("listxattr", "path", 1, 1);
7357 int follow_symlinks = 1;
7358
Serhiy Storchaka31913912019-03-14 10:32:22 +02007359 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
7360 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007361 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03007362 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02007363 if (!noptargs) {
7364 goto skip_optional_pos;
7365 }
7366 if (args[0]) {
7367 if (!path_converter(args[0], &path)) {
7368 goto exit;
7369 }
7370 if (!--noptargs) {
7371 goto skip_optional_pos;
7372 }
7373 }
7374skip_optional_pos:
7375 if (!noptargs) {
7376 goto skip_optional_kwonly;
7377 }
7378 follow_symlinks = PyObject_IsTrue(args[1]);
7379 if (follow_symlinks < 0) {
7380 goto exit;
7381 }
7382skip_optional_kwonly:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007383 return_value = os_listxattr_impl(module, &path, follow_symlinks);
7384
7385exit:
7386 /* Cleanup for path */
7387 path_cleanup(&path);
7388
7389 return return_value;
7390}
7391
7392#endif /* defined(USE_XATTRS) */
7393
7394PyDoc_STRVAR(os_urandom__doc__,
7395"urandom($module, size, /)\n"
7396"--\n"
7397"\n"
7398"Return a bytes object containing random bytes suitable for cryptographic use.");
7399
7400#define OS_URANDOM_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03007401 {"urandom", (PyCFunction)os_urandom, METH_O, os_urandom__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007402
7403static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007404os_urandom_impl(PyObject *module, Py_ssize_t size);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007405
7406static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007407os_urandom(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007408{
7409 PyObject *return_value = NULL;
7410 Py_ssize_t size;
7411
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02007412 if (PyFloat_Check(arg)) {
7413 PyErr_SetString(PyExc_TypeError,
7414 "integer argument expected, got float" );
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007415 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03007416 }
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02007417 {
7418 Py_ssize_t ival = -1;
7419 PyObject *iobj = PyNumber_Index(arg);
7420 if (iobj != NULL) {
7421 ival = PyLong_AsSsize_t(iobj);
7422 Py_DECREF(iobj);
7423 }
7424 if (ival == -1 && PyErr_Occurred()) {
7425 goto exit;
7426 }
7427 size = ival;
7428 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007429 return_value = os_urandom_impl(module, size);
7430
7431exit:
7432 return return_value;
7433}
7434
Zackery Spytz43fdbd22019-05-29 13:57:07 -06007435#if defined(HAVE_MEMFD_CREATE)
7436
7437PyDoc_STRVAR(os_memfd_create__doc__,
7438"memfd_create($module, /, name, flags=MFD_CLOEXEC)\n"
7439"--\n"
7440"\n");
7441
7442#define OS_MEMFD_CREATE_METHODDEF \
7443 {"memfd_create", (PyCFunction)(void(*)(void))os_memfd_create, METH_FASTCALL|METH_KEYWORDS, os_memfd_create__doc__},
7444
7445static PyObject *
7446os_memfd_create_impl(PyObject *module, PyObject *name, unsigned int flags);
7447
7448static PyObject *
7449os_memfd_create(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
7450{
7451 PyObject *return_value = NULL;
7452 static const char * const _keywords[] = {"name", "flags", NULL};
7453 static _PyArg_Parser _parser = {NULL, _keywords, "memfd_create", 0};
7454 PyObject *argsbuf[2];
7455 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
7456 PyObject *name = NULL;
7457 unsigned int flags = MFD_CLOEXEC;
7458
7459 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
7460 if (!args) {
7461 goto exit;
7462 }
7463 if (!PyUnicode_FSConverter(args[0], &name)) {
7464 goto exit;
7465 }
7466 if (!noptargs) {
7467 goto skip_optional_pos;
7468 }
7469 if (PyFloat_Check(args[1])) {
7470 PyErr_SetString(PyExc_TypeError,
7471 "integer argument expected, got float" );
7472 goto exit;
7473 }
7474 flags = (unsigned int)PyLong_AsUnsignedLongMask(args[1]);
7475 if (flags == (unsigned int)-1 && PyErr_Occurred()) {
7476 goto exit;
7477 }
7478skip_optional_pos:
7479 return_value = os_memfd_create_impl(module, name, flags);
7480
7481exit:
7482 /* Cleanup for name */
7483 Py_XDECREF(name);
7484
7485 return return_value;
7486}
7487
7488#endif /* defined(HAVE_MEMFD_CREATE) */
7489
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007490PyDoc_STRVAR(os_cpu_count__doc__,
7491"cpu_count($module, /)\n"
7492"--\n"
7493"\n"
Charles-François Natali80d62e62015-08-13 20:37:08 +01007494"Return the number of CPUs in the system; return None if indeterminable.\n"
7495"\n"
7496"This number is not equivalent to the number of CPUs the current process can\n"
7497"use. The number of usable CPUs can be obtained with\n"
7498"``len(os.sched_getaffinity(0))``");
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007499
7500#define OS_CPU_COUNT_METHODDEF \
7501 {"cpu_count", (PyCFunction)os_cpu_count, METH_NOARGS, os_cpu_count__doc__},
7502
7503static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007504os_cpu_count_impl(PyObject *module);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007505
7506static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007507os_cpu_count(PyObject *module, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007508{
7509 return os_cpu_count_impl(module);
7510}
7511
7512PyDoc_STRVAR(os_get_inheritable__doc__,
7513"get_inheritable($module, fd, /)\n"
7514"--\n"
7515"\n"
7516"Get the close-on-exe flag of the specified file descriptor.");
7517
7518#define OS_GET_INHERITABLE_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03007519 {"get_inheritable", (PyCFunction)os_get_inheritable, METH_O, os_get_inheritable__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007520
7521static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007522os_get_inheritable_impl(PyObject *module, int fd);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007523
7524static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007525os_get_inheritable(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007526{
7527 PyObject *return_value = NULL;
7528 int fd;
7529 int _return_value;
7530
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02007531 if (PyFloat_Check(arg)) {
7532 PyErr_SetString(PyExc_TypeError,
7533 "integer argument expected, got float" );
7534 goto exit;
7535 }
7536 fd = _PyLong_AsInt(arg);
7537 if (fd == -1 && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007538 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03007539 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007540 _return_value = os_get_inheritable_impl(module, fd);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03007541 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007542 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03007543 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007544 return_value = PyBool_FromLong((long)_return_value);
7545
7546exit:
7547 return return_value;
7548}
7549
7550PyDoc_STRVAR(os_set_inheritable__doc__,
7551"set_inheritable($module, fd, inheritable, /)\n"
7552"--\n"
7553"\n"
7554"Set the inheritable flag of the specified file descriptor.");
7555
7556#define OS_SET_INHERITABLE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02007557 {"set_inheritable", (PyCFunction)(void(*)(void))os_set_inheritable, METH_FASTCALL, os_set_inheritable__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007558
7559static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007560os_set_inheritable_impl(PyObject *module, int fd, int inheritable);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007561
7562static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02007563os_set_inheritable(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007564{
7565 PyObject *return_value = NULL;
7566 int fd;
7567 int inheritable;
7568
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02007569 if (!_PyArg_CheckPositional("set_inheritable", nargs, 2, 2)) {
7570 goto exit;
7571 }
7572 if (PyFloat_Check(args[0])) {
7573 PyErr_SetString(PyExc_TypeError,
7574 "integer argument expected, got float" );
7575 goto exit;
7576 }
7577 fd = _PyLong_AsInt(args[0]);
7578 if (fd == -1 && PyErr_Occurred()) {
7579 goto exit;
7580 }
7581 if (PyFloat_Check(args[1])) {
7582 PyErr_SetString(PyExc_TypeError,
7583 "integer argument expected, got float" );
7584 goto exit;
7585 }
7586 inheritable = _PyLong_AsInt(args[1]);
7587 if (inheritable == -1 && PyErr_Occurred()) {
Victor Stinner259f0e42017-01-17 01:35:17 +01007588 goto exit;
7589 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007590 return_value = os_set_inheritable_impl(module, fd, inheritable);
7591
7592exit:
7593 return return_value;
7594}
7595
7596#if defined(MS_WINDOWS)
7597
7598PyDoc_STRVAR(os_get_handle_inheritable__doc__,
7599"get_handle_inheritable($module, handle, /)\n"
7600"--\n"
7601"\n"
7602"Get the close-on-exe flag of the specified file descriptor.");
7603
7604#define OS_GET_HANDLE_INHERITABLE_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +03007605 {"get_handle_inheritable", (PyCFunction)os_get_handle_inheritable, METH_O, os_get_handle_inheritable__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007606
7607static int
Victor Stinner581139c2016-09-06 15:54:20 -07007608os_get_handle_inheritable_impl(PyObject *module, intptr_t handle);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007609
7610static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007611os_get_handle_inheritable(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007612{
7613 PyObject *return_value = NULL;
Victor Stinner581139c2016-09-06 15:54:20 -07007614 intptr_t handle;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007615 int _return_value;
7616
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03007617 if (!PyArg_Parse(arg, "" _Py_PARSE_INTPTR ":get_handle_inheritable", &handle)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007618 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03007619 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007620 _return_value = os_get_handle_inheritable_impl(module, handle);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03007621 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007622 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03007623 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007624 return_value = PyBool_FromLong((long)_return_value);
7625
7626exit:
7627 return return_value;
7628}
7629
7630#endif /* defined(MS_WINDOWS) */
7631
7632#if defined(MS_WINDOWS)
7633
7634PyDoc_STRVAR(os_set_handle_inheritable__doc__,
7635"set_handle_inheritable($module, handle, inheritable, /)\n"
7636"--\n"
7637"\n"
7638"Set the inheritable flag of the specified handle.");
7639
7640#define OS_SET_HANDLE_INHERITABLE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02007641 {"set_handle_inheritable", (PyCFunction)(void(*)(void))os_set_handle_inheritable, METH_FASTCALL, os_set_handle_inheritable__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007642
7643static PyObject *
Victor Stinner581139c2016-09-06 15:54:20 -07007644os_set_handle_inheritable_impl(PyObject *module, intptr_t handle,
Larry Hastings89964c42015-04-14 18:07:59 -04007645 int inheritable);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007646
7647static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02007648os_set_handle_inheritable(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007649{
7650 PyObject *return_value = NULL;
Victor Stinner581139c2016-09-06 15:54:20 -07007651 intptr_t handle;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007652 int inheritable;
7653
Sylvain74453812017-06-10 06:51:48 +02007654 if (!_PyArg_ParseStack(args, nargs, "" _Py_PARSE_INTPTR "p:set_handle_inheritable",
7655 &handle, &inheritable)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01007656 goto exit;
7657 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03007658 return_value = os_set_handle_inheritable_impl(module, handle, inheritable);
7659
7660exit:
7661 return return_value;
7662}
7663
7664#endif /* defined(MS_WINDOWS) */
7665
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007666#if !defined(MS_WINDOWS)
7667
7668PyDoc_STRVAR(os_get_blocking__doc__,
7669"get_blocking($module, fd, /)\n"
7670"--\n"
7671"\n"
7672"Get the blocking mode of the file descriptor.\n"
7673"\n"
7674"Return False if the O_NONBLOCK flag is set, True if the flag is cleared.");
7675
7676#define OS_GET_BLOCKING_METHODDEF \
7677 {"get_blocking", (PyCFunction)os_get_blocking, METH_O, os_get_blocking__doc__},
7678
7679static int
7680os_get_blocking_impl(PyObject *module, int fd);
7681
7682static PyObject *
7683os_get_blocking(PyObject *module, PyObject *arg)
7684{
7685 PyObject *return_value = NULL;
7686 int fd;
7687 int _return_value;
7688
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02007689 if (PyFloat_Check(arg)) {
7690 PyErr_SetString(PyExc_TypeError,
7691 "integer argument expected, got float" );
7692 goto exit;
7693 }
7694 fd = _PyLong_AsInt(arg);
7695 if (fd == -1 && PyErr_Occurred()) {
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007696 goto exit;
7697 }
7698 _return_value = os_get_blocking_impl(module, fd);
7699 if ((_return_value == -1) && PyErr_Occurred()) {
7700 goto exit;
7701 }
7702 return_value = PyBool_FromLong((long)_return_value);
7703
7704exit:
7705 return return_value;
7706}
7707
7708#endif /* !defined(MS_WINDOWS) */
7709
7710#if !defined(MS_WINDOWS)
7711
7712PyDoc_STRVAR(os_set_blocking__doc__,
7713"set_blocking($module, fd, blocking, /)\n"
7714"--\n"
7715"\n"
7716"Set the blocking mode of the specified file descriptor.\n"
7717"\n"
7718"Set the O_NONBLOCK flag if blocking is False,\n"
7719"clear the O_NONBLOCK flag otherwise.");
7720
7721#define OS_SET_BLOCKING_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02007722 {"set_blocking", (PyCFunction)(void(*)(void))os_set_blocking, METH_FASTCALL, os_set_blocking__doc__},
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007723
7724static PyObject *
7725os_set_blocking_impl(PyObject *module, int fd, int blocking);
7726
7727static PyObject *
7728os_set_blocking(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
7729{
7730 PyObject *return_value = NULL;
7731 int fd;
7732 int blocking;
7733
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02007734 if (!_PyArg_CheckPositional("set_blocking", nargs, 2, 2)) {
7735 goto exit;
7736 }
7737 if (PyFloat_Check(args[0])) {
7738 PyErr_SetString(PyExc_TypeError,
7739 "integer argument expected, got float" );
7740 goto exit;
7741 }
7742 fd = _PyLong_AsInt(args[0]);
7743 if (fd == -1 && PyErr_Occurred()) {
7744 goto exit;
7745 }
7746 if (PyFloat_Check(args[1])) {
7747 PyErr_SetString(PyExc_TypeError,
7748 "integer argument expected, got float" );
7749 goto exit;
7750 }
7751 blocking = _PyLong_AsInt(args[1]);
7752 if (blocking == -1 && PyErr_Occurred()) {
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007753 goto exit;
7754 }
7755 return_value = os_set_blocking_impl(module, fd, blocking);
7756
7757exit:
7758 return return_value;
7759}
7760
7761#endif /* !defined(MS_WINDOWS) */
7762
Serhiy Storchaka49d02d12016-11-06 13:45:33 +02007763PyDoc_STRVAR(os_DirEntry_is_symlink__doc__,
7764"is_symlink($self, /)\n"
7765"--\n"
7766"\n"
7767"Return True if the entry is a symbolic link; cached per entry.");
7768
7769#define OS_DIRENTRY_IS_SYMLINK_METHODDEF \
7770 {"is_symlink", (PyCFunction)os_DirEntry_is_symlink, METH_NOARGS, os_DirEntry_is_symlink__doc__},
7771
7772static int
7773os_DirEntry_is_symlink_impl(DirEntry *self);
7774
7775static PyObject *
7776os_DirEntry_is_symlink(DirEntry *self, PyObject *Py_UNUSED(ignored))
7777{
7778 PyObject *return_value = NULL;
7779 int _return_value;
7780
7781 _return_value = os_DirEntry_is_symlink_impl(self);
7782 if ((_return_value == -1) && PyErr_Occurred()) {
7783 goto exit;
7784 }
7785 return_value = PyBool_FromLong((long)_return_value);
7786
7787exit:
7788 return return_value;
7789}
7790
7791PyDoc_STRVAR(os_DirEntry_stat__doc__,
7792"stat($self, /, *, follow_symlinks=True)\n"
7793"--\n"
7794"\n"
7795"Return stat_result object for the entry; cached per entry.");
7796
7797#define OS_DIRENTRY_STAT_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02007798 {"stat", (PyCFunction)(void(*)(void))os_DirEntry_stat, METH_FASTCALL|METH_KEYWORDS, os_DirEntry_stat__doc__},
Serhiy Storchaka49d02d12016-11-06 13:45:33 +02007799
7800static PyObject *
7801os_DirEntry_stat_impl(DirEntry *self, int follow_symlinks);
7802
7803static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02007804os_DirEntry_stat(DirEntry *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka49d02d12016-11-06 13:45:33 +02007805{
7806 PyObject *return_value = NULL;
7807 static const char * const _keywords[] = {"follow_symlinks", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02007808 static _PyArg_Parser _parser = {NULL, _keywords, "stat", 0};
7809 PyObject *argsbuf[1];
7810 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
Serhiy Storchaka49d02d12016-11-06 13:45:33 +02007811 int follow_symlinks = 1;
7812
Serhiy Storchaka31913912019-03-14 10:32:22 +02007813 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 0, 0, argsbuf);
7814 if (!args) {
Serhiy Storchaka49d02d12016-11-06 13:45:33 +02007815 goto exit;
7816 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02007817 if (!noptargs) {
7818 goto skip_optional_kwonly;
7819 }
7820 follow_symlinks = PyObject_IsTrue(args[0]);
7821 if (follow_symlinks < 0) {
7822 goto exit;
7823 }
7824skip_optional_kwonly:
Serhiy Storchaka49d02d12016-11-06 13:45:33 +02007825 return_value = os_DirEntry_stat_impl(self, follow_symlinks);
7826
7827exit:
7828 return return_value;
7829}
7830
7831PyDoc_STRVAR(os_DirEntry_is_dir__doc__,
7832"is_dir($self, /, *, follow_symlinks=True)\n"
7833"--\n"
7834"\n"
7835"Return True if the entry is a directory; cached per entry.");
7836
7837#define OS_DIRENTRY_IS_DIR_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02007838 {"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 +02007839
7840static int
7841os_DirEntry_is_dir_impl(DirEntry *self, int follow_symlinks);
7842
7843static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02007844os_DirEntry_is_dir(DirEntry *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka49d02d12016-11-06 13:45:33 +02007845{
7846 PyObject *return_value = NULL;
7847 static const char * const _keywords[] = {"follow_symlinks", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02007848 static _PyArg_Parser _parser = {NULL, _keywords, "is_dir", 0};
7849 PyObject *argsbuf[1];
7850 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
Serhiy Storchaka49d02d12016-11-06 13:45:33 +02007851 int follow_symlinks = 1;
7852 int _return_value;
7853
Serhiy Storchaka31913912019-03-14 10:32:22 +02007854 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 0, 0, argsbuf);
7855 if (!args) {
Serhiy Storchaka49d02d12016-11-06 13:45:33 +02007856 goto exit;
7857 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02007858 if (!noptargs) {
7859 goto skip_optional_kwonly;
7860 }
7861 follow_symlinks = PyObject_IsTrue(args[0]);
7862 if (follow_symlinks < 0) {
7863 goto exit;
7864 }
7865skip_optional_kwonly:
Serhiy Storchaka49d02d12016-11-06 13:45:33 +02007866 _return_value = os_DirEntry_is_dir_impl(self, follow_symlinks);
7867 if ((_return_value == -1) && PyErr_Occurred()) {
7868 goto exit;
7869 }
7870 return_value = PyBool_FromLong((long)_return_value);
7871
7872exit:
7873 return return_value;
7874}
7875
7876PyDoc_STRVAR(os_DirEntry_is_file__doc__,
7877"is_file($self, /, *, follow_symlinks=True)\n"
7878"--\n"
7879"\n"
7880"Return True if the entry is a file; cached per entry.");
7881
7882#define OS_DIRENTRY_IS_FILE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02007883 {"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 +02007884
7885static int
7886os_DirEntry_is_file_impl(DirEntry *self, int follow_symlinks);
7887
7888static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02007889os_DirEntry_is_file(DirEntry *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka49d02d12016-11-06 13:45:33 +02007890{
7891 PyObject *return_value = NULL;
7892 static const char * const _keywords[] = {"follow_symlinks", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02007893 static _PyArg_Parser _parser = {NULL, _keywords, "is_file", 0};
7894 PyObject *argsbuf[1];
7895 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
Serhiy Storchaka49d02d12016-11-06 13:45:33 +02007896 int follow_symlinks = 1;
7897 int _return_value;
7898
Serhiy Storchaka31913912019-03-14 10:32:22 +02007899 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 0, 0, argsbuf);
7900 if (!args) {
Serhiy Storchaka49d02d12016-11-06 13:45:33 +02007901 goto exit;
7902 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02007903 if (!noptargs) {
7904 goto skip_optional_kwonly;
7905 }
7906 follow_symlinks = PyObject_IsTrue(args[0]);
7907 if (follow_symlinks < 0) {
7908 goto exit;
7909 }
7910skip_optional_kwonly:
Serhiy Storchaka49d02d12016-11-06 13:45:33 +02007911 _return_value = os_DirEntry_is_file_impl(self, follow_symlinks);
7912 if ((_return_value == -1) && PyErr_Occurred()) {
7913 goto exit;
7914 }
7915 return_value = PyBool_FromLong((long)_return_value);
7916
7917exit:
7918 return return_value;
7919}
7920
7921PyDoc_STRVAR(os_DirEntry_inode__doc__,
7922"inode($self, /)\n"
7923"--\n"
7924"\n"
7925"Return inode of the entry; cached per entry.");
7926
7927#define OS_DIRENTRY_INODE_METHODDEF \
7928 {"inode", (PyCFunction)os_DirEntry_inode, METH_NOARGS, os_DirEntry_inode__doc__},
7929
7930static PyObject *
7931os_DirEntry_inode_impl(DirEntry *self);
7932
7933static PyObject *
7934os_DirEntry_inode(DirEntry *self, PyObject *Py_UNUSED(ignored))
7935{
7936 return os_DirEntry_inode_impl(self);
7937}
7938
7939PyDoc_STRVAR(os_DirEntry___fspath____doc__,
7940"__fspath__($self, /)\n"
7941"--\n"
7942"\n"
7943"Returns the path for the entry.");
7944
7945#define OS_DIRENTRY___FSPATH___METHODDEF \
7946 {"__fspath__", (PyCFunction)os_DirEntry___fspath__, METH_NOARGS, os_DirEntry___fspath____doc__},
7947
7948static PyObject *
7949os_DirEntry___fspath___impl(DirEntry *self);
7950
7951static PyObject *
7952os_DirEntry___fspath__(DirEntry *self, PyObject *Py_UNUSED(ignored))
7953{
7954 return os_DirEntry___fspath___impl(self);
7955}
7956
7957PyDoc_STRVAR(os_scandir__doc__,
7958"scandir($module, /, path=None)\n"
7959"--\n"
7960"\n"
7961"Return an iterator of DirEntry objects for given path.\n"
7962"\n"
BNMetricsb9427072018-11-02 15:20:19 +00007963"path can be specified as either str, bytes, or a path-like object. If path\n"
Serhiy Storchaka49d02d12016-11-06 13:45:33 +02007964"is bytes, the names of yielded DirEntry objects will also be bytes; in\n"
7965"all other circumstances they will be str.\n"
7966"\n"
7967"If path is None, uses the path=\'.\'.");
7968
7969#define OS_SCANDIR_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02007970 {"scandir", (PyCFunction)(void(*)(void))os_scandir, METH_FASTCALL|METH_KEYWORDS, os_scandir__doc__},
Serhiy Storchaka49d02d12016-11-06 13:45:33 +02007971
7972static PyObject *
7973os_scandir_impl(PyObject *module, path_t *path);
7974
7975static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02007976os_scandir(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka49d02d12016-11-06 13:45:33 +02007977{
7978 PyObject *return_value = NULL;
7979 static const char * const _keywords[] = {"path", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02007980 static _PyArg_Parser _parser = {NULL, _keywords, "scandir", 0};
7981 PyObject *argsbuf[1];
7982 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
Serhiy Storchakaea720fe2017-03-30 09:12:31 +03007983 path_t path = PATH_T_INITIALIZE("scandir", "path", 1, PATH_HAVE_FDOPENDIR);
Serhiy Storchaka49d02d12016-11-06 13:45:33 +02007984
Serhiy Storchaka31913912019-03-14 10:32:22 +02007985 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
7986 if (!args) {
Serhiy Storchaka49d02d12016-11-06 13:45:33 +02007987 goto exit;
7988 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02007989 if (!noptargs) {
7990 goto skip_optional_pos;
7991 }
7992 if (!path_converter(args[0], &path)) {
7993 goto exit;
7994 }
7995skip_optional_pos:
Serhiy Storchaka49d02d12016-11-06 13:45:33 +02007996 return_value = os_scandir_impl(module, &path);
7997
7998exit:
7999 /* Cleanup for path */
8000 path_cleanup(&path);
8001
8002 return return_value;
8003}
8004
Ethan Furman410ef8e2016-06-04 12:06:26 -07008005PyDoc_STRVAR(os_fspath__doc__,
8006"fspath($module, /, path)\n"
8007"--\n"
8008"\n"
8009"Return the file system path representation of the object.\n"
8010"\n"
Brett Cannonb4f43e92016-06-09 14:32:08 -07008011"If the object is str or bytes, then allow it to pass through as-is. If the\n"
8012"object defines __fspath__(), then return the result of that method. All other\n"
8013"types raise a TypeError.");
Ethan Furman410ef8e2016-06-04 12:06:26 -07008014
8015#define OS_FSPATH_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02008016 {"fspath", (PyCFunction)(void(*)(void))os_fspath, METH_FASTCALL|METH_KEYWORDS, os_fspath__doc__},
Ethan Furman410ef8e2016-06-04 12:06:26 -07008017
8018static PyObject *
Serhiy Storchaka2954f832016-07-07 18:20:03 +03008019os_fspath_impl(PyObject *module, PyObject *path);
Ethan Furman410ef8e2016-06-04 12:06:26 -07008020
8021static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02008022os_fspath(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Ethan Furman410ef8e2016-06-04 12:06:26 -07008023{
8024 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03008025 static const char * const _keywords[] = {"path", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02008026 static _PyArg_Parser _parser = {NULL, _keywords, "fspath", 0};
8027 PyObject *argsbuf[1];
Ethan Furman410ef8e2016-06-04 12:06:26 -07008028 PyObject *path;
8029
Serhiy Storchaka31913912019-03-14 10:32:22 +02008030 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
8031 if (!args) {
Ethan Furman410ef8e2016-06-04 12:06:26 -07008032 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03008033 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02008034 path = args[0];
Ethan Furman410ef8e2016-06-04 12:06:26 -07008035 return_value = os_fspath_impl(module, path);
8036
8037exit:
8038 return return_value;
8039}
8040
Victor Stinner9b1f4742016-09-06 16:18:52 -07008041#if defined(HAVE_GETRANDOM_SYSCALL)
8042
8043PyDoc_STRVAR(os_getrandom__doc__,
8044"getrandom($module, /, size, flags=0)\n"
8045"--\n"
8046"\n"
8047"Obtain a series of random bytes.");
8048
8049#define OS_GETRANDOM_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02008050 {"getrandom", (PyCFunction)(void(*)(void))os_getrandom, METH_FASTCALL|METH_KEYWORDS, os_getrandom__doc__},
Victor Stinner9b1f4742016-09-06 16:18:52 -07008051
8052static PyObject *
8053os_getrandom_impl(PyObject *module, Py_ssize_t size, int flags);
8054
8055static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02008056os_getrandom(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Victor Stinner9b1f4742016-09-06 16:18:52 -07008057{
8058 PyObject *return_value = NULL;
8059 static const char * const _keywords[] = {"size", "flags", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +02008060 static _PyArg_Parser _parser = {NULL, _keywords, "getrandom", 0};
8061 PyObject *argsbuf[2];
8062 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
Victor Stinner9b1f4742016-09-06 16:18:52 -07008063 Py_ssize_t size;
8064 int flags = 0;
8065
Serhiy Storchaka31913912019-03-14 10:32:22 +02008066 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
8067 if (!args) {
Victor Stinner9b1f4742016-09-06 16:18:52 -07008068 goto exit;
8069 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02008070 if (PyFloat_Check(args[0])) {
8071 PyErr_SetString(PyExc_TypeError,
8072 "integer argument expected, got float" );
8073 goto exit;
8074 }
8075 {
8076 Py_ssize_t ival = -1;
8077 PyObject *iobj = PyNumber_Index(args[0]);
8078 if (iobj != NULL) {
8079 ival = PyLong_AsSsize_t(iobj);
8080 Py_DECREF(iobj);
8081 }
8082 if (ival == -1 && PyErr_Occurred()) {
8083 goto exit;
8084 }
8085 size = ival;
8086 }
8087 if (!noptargs) {
8088 goto skip_optional_pos;
8089 }
8090 if (PyFloat_Check(args[1])) {
8091 PyErr_SetString(PyExc_TypeError,
8092 "integer argument expected, got float" );
8093 goto exit;
8094 }
8095 flags = _PyLong_AsInt(args[1]);
8096 if (flags == -1 && PyErr_Occurred()) {
8097 goto exit;
8098 }
8099skip_optional_pos:
Victor Stinner9b1f4742016-09-06 16:18:52 -07008100 return_value = os_getrandom_impl(module, size, flags);
8101
8102exit:
8103 return return_value;
8104}
8105
8106#endif /* defined(HAVE_GETRANDOM_SYSCALL) */
8107
Steve Dower2438cdf2019-03-29 16:37:16 -07008108#if defined(MS_WINDOWS)
8109
8110PyDoc_STRVAR(os__add_dll_directory__doc__,
8111"_add_dll_directory($module, /, path)\n"
8112"--\n"
8113"\n"
8114"Add a path to the DLL search path.\n"
8115"\n"
8116"This search path is used when resolving dependencies for imported\n"
8117"extension modules (the module itself is resolved through sys.path),\n"
8118"and also by ctypes.\n"
8119"\n"
8120"Returns an opaque value that may be passed to os.remove_dll_directory\n"
8121"to remove this directory from the search path.");
8122
8123#define OS__ADD_DLL_DIRECTORY_METHODDEF \
8124 {"_add_dll_directory", (PyCFunction)(void(*)(void))os__add_dll_directory, METH_FASTCALL|METH_KEYWORDS, os__add_dll_directory__doc__},
8125
8126static PyObject *
8127os__add_dll_directory_impl(PyObject *module, path_t *path);
8128
8129static PyObject *
8130os__add_dll_directory(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
8131{
8132 PyObject *return_value = NULL;
8133 static const char * const _keywords[] = {"path", NULL};
8134 static _PyArg_Parser _parser = {NULL, _keywords, "_add_dll_directory", 0};
8135 PyObject *argsbuf[1];
8136 path_t path = PATH_T_INITIALIZE("_add_dll_directory", "path", 0, 0);
8137
8138 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
8139 if (!args) {
8140 goto exit;
8141 }
8142 if (!path_converter(args[0], &path)) {
8143 goto exit;
8144 }
8145 return_value = os__add_dll_directory_impl(module, &path);
8146
8147exit:
8148 /* Cleanup for path */
8149 path_cleanup(&path);
8150
8151 return return_value;
8152}
8153
8154#endif /* defined(MS_WINDOWS) */
8155
8156#if defined(MS_WINDOWS)
8157
8158PyDoc_STRVAR(os__remove_dll_directory__doc__,
8159"_remove_dll_directory($module, /, cookie)\n"
8160"--\n"
8161"\n"
8162"Removes a path from the DLL search path.\n"
8163"\n"
8164"The parameter is an opaque value that was returned from\n"
8165"os.add_dll_directory. You can only remove directories that you added\n"
8166"yourself.");
8167
8168#define OS__REMOVE_DLL_DIRECTORY_METHODDEF \
8169 {"_remove_dll_directory", (PyCFunction)(void(*)(void))os__remove_dll_directory, METH_FASTCALL|METH_KEYWORDS, os__remove_dll_directory__doc__},
8170
8171static PyObject *
8172os__remove_dll_directory_impl(PyObject *module, PyObject *cookie);
8173
8174static PyObject *
8175os__remove_dll_directory(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
8176{
8177 PyObject *return_value = NULL;
8178 static const char * const _keywords[] = {"cookie", NULL};
8179 static _PyArg_Parser _parser = {NULL, _keywords, "_remove_dll_directory", 0};
8180 PyObject *argsbuf[1];
8181 PyObject *cookie;
8182
8183 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
8184 if (!args) {
8185 goto exit;
8186 }
8187 cookie = args[0];
8188 return_value = os__remove_dll_directory_impl(module, cookie);
8189
8190exit:
8191 return return_value;
8192}
8193
8194#endif /* defined(MS_WINDOWS) */
8195
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03008196#ifndef OS_TTYNAME_METHODDEF
8197 #define OS_TTYNAME_METHODDEF
8198#endif /* !defined(OS_TTYNAME_METHODDEF) */
8199
8200#ifndef OS_CTERMID_METHODDEF
8201 #define OS_CTERMID_METHODDEF
8202#endif /* !defined(OS_CTERMID_METHODDEF) */
8203
8204#ifndef OS_FCHDIR_METHODDEF
8205 #define OS_FCHDIR_METHODDEF
8206#endif /* !defined(OS_FCHDIR_METHODDEF) */
8207
8208#ifndef OS_FCHMOD_METHODDEF
8209 #define OS_FCHMOD_METHODDEF
8210#endif /* !defined(OS_FCHMOD_METHODDEF) */
8211
8212#ifndef OS_LCHMOD_METHODDEF
8213 #define OS_LCHMOD_METHODDEF
8214#endif /* !defined(OS_LCHMOD_METHODDEF) */
8215
8216#ifndef OS_CHFLAGS_METHODDEF
8217 #define OS_CHFLAGS_METHODDEF
8218#endif /* !defined(OS_CHFLAGS_METHODDEF) */
8219
8220#ifndef OS_LCHFLAGS_METHODDEF
8221 #define OS_LCHFLAGS_METHODDEF
8222#endif /* !defined(OS_LCHFLAGS_METHODDEF) */
8223
8224#ifndef OS_CHROOT_METHODDEF
8225 #define OS_CHROOT_METHODDEF
8226#endif /* !defined(OS_CHROOT_METHODDEF) */
8227
8228#ifndef OS_FSYNC_METHODDEF
8229 #define OS_FSYNC_METHODDEF
8230#endif /* !defined(OS_FSYNC_METHODDEF) */
8231
8232#ifndef OS_SYNC_METHODDEF
8233 #define OS_SYNC_METHODDEF
8234#endif /* !defined(OS_SYNC_METHODDEF) */
8235
8236#ifndef OS_FDATASYNC_METHODDEF
8237 #define OS_FDATASYNC_METHODDEF
8238#endif /* !defined(OS_FDATASYNC_METHODDEF) */
8239
8240#ifndef OS_CHOWN_METHODDEF
8241 #define OS_CHOWN_METHODDEF
8242#endif /* !defined(OS_CHOWN_METHODDEF) */
8243
8244#ifndef OS_FCHOWN_METHODDEF
8245 #define OS_FCHOWN_METHODDEF
8246#endif /* !defined(OS_FCHOWN_METHODDEF) */
8247
8248#ifndef OS_LCHOWN_METHODDEF
8249 #define OS_LCHOWN_METHODDEF
8250#endif /* !defined(OS_LCHOWN_METHODDEF) */
8251
8252#ifndef OS_LINK_METHODDEF
8253 #define OS_LINK_METHODDEF
8254#endif /* !defined(OS_LINK_METHODDEF) */
8255
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03008256#ifndef OS__GETFULLPATHNAME_METHODDEF
8257 #define OS__GETFULLPATHNAME_METHODDEF
8258#endif /* !defined(OS__GETFULLPATHNAME_METHODDEF) */
8259
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03008260#ifndef OS__GETFINALPATHNAME_METHODDEF
8261 #define OS__GETFINALPATHNAME_METHODDEF
8262#endif /* !defined(OS__GETFINALPATHNAME_METHODDEF) */
8263
8264#ifndef OS__GETVOLUMEPATHNAME_METHODDEF
8265 #define OS__GETVOLUMEPATHNAME_METHODDEF
8266#endif /* !defined(OS__GETVOLUMEPATHNAME_METHODDEF) */
8267
8268#ifndef OS_NICE_METHODDEF
8269 #define OS_NICE_METHODDEF
8270#endif /* !defined(OS_NICE_METHODDEF) */
8271
8272#ifndef OS_GETPRIORITY_METHODDEF
8273 #define OS_GETPRIORITY_METHODDEF
8274#endif /* !defined(OS_GETPRIORITY_METHODDEF) */
8275
8276#ifndef OS_SETPRIORITY_METHODDEF
8277 #define OS_SETPRIORITY_METHODDEF
8278#endif /* !defined(OS_SETPRIORITY_METHODDEF) */
8279
8280#ifndef OS_SYSTEM_METHODDEF
8281 #define OS_SYSTEM_METHODDEF
8282#endif /* !defined(OS_SYSTEM_METHODDEF) */
8283
8284#ifndef OS_UNAME_METHODDEF
8285 #define OS_UNAME_METHODDEF
8286#endif /* !defined(OS_UNAME_METHODDEF) */
8287
8288#ifndef OS_EXECV_METHODDEF
8289 #define OS_EXECV_METHODDEF
8290#endif /* !defined(OS_EXECV_METHODDEF) */
8291
8292#ifndef OS_EXECVE_METHODDEF
8293 #define OS_EXECVE_METHODDEF
8294#endif /* !defined(OS_EXECVE_METHODDEF) */
8295
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00008296#ifndef OS_POSIX_SPAWN_METHODDEF
8297 #define OS_POSIX_SPAWN_METHODDEF
8298#endif /* !defined(OS_POSIX_SPAWN_METHODDEF) */
8299
Joannah Nanjekye92b83222019-01-16 16:29:26 +03008300#ifndef OS_POSIX_SPAWNP_METHODDEF
8301 #define OS_POSIX_SPAWNP_METHODDEF
8302#endif /* !defined(OS_POSIX_SPAWNP_METHODDEF) */
8303
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03008304#ifndef OS_SPAWNV_METHODDEF
8305 #define OS_SPAWNV_METHODDEF
8306#endif /* !defined(OS_SPAWNV_METHODDEF) */
8307
8308#ifndef OS_SPAWNVE_METHODDEF
8309 #define OS_SPAWNVE_METHODDEF
8310#endif /* !defined(OS_SPAWNVE_METHODDEF) */
8311
Antoine Pitrou346cbd32017-05-27 17:50:54 +02008312#ifndef OS_REGISTER_AT_FORK_METHODDEF
8313 #define OS_REGISTER_AT_FORK_METHODDEF
8314#endif /* !defined(OS_REGISTER_AT_FORK_METHODDEF) */
8315
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03008316#ifndef OS_FORK1_METHODDEF
8317 #define OS_FORK1_METHODDEF
8318#endif /* !defined(OS_FORK1_METHODDEF) */
8319
8320#ifndef OS_FORK_METHODDEF
8321 #define OS_FORK_METHODDEF
8322#endif /* !defined(OS_FORK_METHODDEF) */
8323
8324#ifndef OS_SCHED_GET_PRIORITY_MAX_METHODDEF
8325 #define OS_SCHED_GET_PRIORITY_MAX_METHODDEF
8326#endif /* !defined(OS_SCHED_GET_PRIORITY_MAX_METHODDEF) */
8327
8328#ifndef OS_SCHED_GET_PRIORITY_MIN_METHODDEF
8329 #define OS_SCHED_GET_PRIORITY_MIN_METHODDEF
8330#endif /* !defined(OS_SCHED_GET_PRIORITY_MIN_METHODDEF) */
8331
8332#ifndef OS_SCHED_GETSCHEDULER_METHODDEF
8333 #define OS_SCHED_GETSCHEDULER_METHODDEF
8334#endif /* !defined(OS_SCHED_GETSCHEDULER_METHODDEF) */
8335
8336#ifndef OS_SCHED_SETSCHEDULER_METHODDEF
8337 #define OS_SCHED_SETSCHEDULER_METHODDEF
8338#endif /* !defined(OS_SCHED_SETSCHEDULER_METHODDEF) */
8339
8340#ifndef OS_SCHED_GETPARAM_METHODDEF
8341 #define OS_SCHED_GETPARAM_METHODDEF
8342#endif /* !defined(OS_SCHED_GETPARAM_METHODDEF) */
8343
8344#ifndef OS_SCHED_SETPARAM_METHODDEF
8345 #define OS_SCHED_SETPARAM_METHODDEF
8346#endif /* !defined(OS_SCHED_SETPARAM_METHODDEF) */
8347
8348#ifndef OS_SCHED_RR_GET_INTERVAL_METHODDEF
8349 #define OS_SCHED_RR_GET_INTERVAL_METHODDEF
8350#endif /* !defined(OS_SCHED_RR_GET_INTERVAL_METHODDEF) */
8351
8352#ifndef OS_SCHED_YIELD_METHODDEF
8353 #define OS_SCHED_YIELD_METHODDEF
8354#endif /* !defined(OS_SCHED_YIELD_METHODDEF) */
8355
8356#ifndef OS_SCHED_SETAFFINITY_METHODDEF
8357 #define OS_SCHED_SETAFFINITY_METHODDEF
8358#endif /* !defined(OS_SCHED_SETAFFINITY_METHODDEF) */
8359
8360#ifndef OS_SCHED_GETAFFINITY_METHODDEF
8361 #define OS_SCHED_GETAFFINITY_METHODDEF
8362#endif /* !defined(OS_SCHED_GETAFFINITY_METHODDEF) */
8363
8364#ifndef OS_OPENPTY_METHODDEF
8365 #define OS_OPENPTY_METHODDEF
8366#endif /* !defined(OS_OPENPTY_METHODDEF) */
8367
8368#ifndef OS_FORKPTY_METHODDEF
8369 #define OS_FORKPTY_METHODDEF
8370#endif /* !defined(OS_FORKPTY_METHODDEF) */
8371
8372#ifndef OS_GETEGID_METHODDEF
8373 #define OS_GETEGID_METHODDEF
8374#endif /* !defined(OS_GETEGID_METHODDEF) */
8375
8376#ifndef OS_GETEUID_METHODDEF
8377 #define OS_GETEUID_METHODDEF
8378#endif /* !defined(OS_GETEUID_METHODDEF) */
8379
8380#ifndef OS_GETGID_METHODDEF
8381 #define OS_GETGID_METHODDEF
8382#endif /* !defined(OS_GETGID_METHODDEF) */
8383
Berker Peksag39404992016-09-15 20:45:16 +03008384#ifndef OS_GETPID_METHODDEF
8385 #define OS_GETPID_METHODDEF
8386#endif /* !defined(OS_GETPID_METHODDEF) */
8387
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03008388#ifndef OS_GETGROUPS_METHODDEF
8389 #define OS_GETGROUPS_METHODDEF
8390#endif /* !defined(OS_GETGROUPS_METHODDEF) */
8391
8392#ifndef OS_GETPGID_METHODDEF
8393 #define OS_GETPGID_METHODDEF
8394#endif /* !defined(OS_GETPGID_METHODDEF) */
8395
8396#ifndef OS_GETPGRP_METHODDEF
8397 #define OS_GETPGRP_METHODDEF
8398#endif /* !defined(OS_GETPGRP_METHODDEF) */
8399
8400#ifndef OS_SETPGRP_METHODDEF
8401 #define OS_SETPGRP_METHODDEF
8402#endif /* !defined(OS_SETPGRP_METHODDEF) */
8403
8404#ifndef OS_GETPPID_METHODDEF
8405 #define OS_GETPPID_METHODDEF
8406#endif /* !defined(OS_GETPPID_METHODDEF) */
8407
8408#ifndef OS_GETLOGIN_METHODDEF
8409 #define OS_GETLOGIN_METHODDEF
8410#endif /* !defined(OS_GETLOGIN_METHODDEF) */
8411
8412#ifndef OS_GETUID_METHODDEF
8413 #define OS_GETUID_METHODDEF
8414#endif /* !defined(OS_GETUID_METHODDEF) */
8415
8416#ifndef OS_KILL_METHODDEF
8417 #define OS_KILL_METHODDEF
8418#endif /* !defined(OS_KILL_METHODDEF) */
8419
8420#ifndef OS_KILLPG_METHODDEF
8421 #define OS_KILLPG_METHODDEF
8422#endif /* !defined(OS_KILLPG_METHODDEF) */
8423
8424#ifndef OS_PLOCK_METHODDEF
8425 #define OS_PLOCK_METHODDEF
8426#endif /* !defined(OS_PLOCK_METHODDEF) */
8427
8428#ifndef OS_SETUID_METHODDEF
8429 #define OS_SETUID_METHODDEF
8430#endif /* !defined(OS_SETUID_METHODDEF) */
8431
8432#ifndef OS_SETEUID_METHODDEF
8433 #define OS_SETEUID_METHODDEF
8434#endif /* !defined(OS_SETEUID_METHODDEF) */
8435
8436#ifndef OS_SETEGID_METHODDEF
8437 #define OS_SETEGID_METHODDEF
8438#endif /* !defined(OS_SETEGID_METHODDEF) */
8439
8440#ifndef OS_SETREUID_METHODDEF
8441 #define OS_SETREUID_METHODDEF
8442#endif /* !defined(OS_SETREUID_METHODDEF) */
8443
8444#ifndef OS_SETREGID_METHODDEF
8445 #define OS_SETREGID_METHODDEF
8446#endif /* !defined(OS_SETREGID_METHODDEF) */
8447
8448#ifndef OS_SETGID_METHODDEF
8449 #define OS_SETGID_METHODDEF
8450#endif /* !defined(OS_SETGID_METHODDEF) */
8451
8452#ifndef OS_SETGROUPS_METHODDEF
8453 #define OS_SETGROUPS_METHODDEF
8454#endif /* !defined(OS_SETGROUPS_METHODDEF) */
8455
8456#ifndef OS_WAIT3_METHODDEF
8457 #define OS_WAIT3_METHODDEF
8458#endif /* !defined(OS_WAIT3_METHODDEF) */
8459
8460#ifndef OS_WAIT4_METHODDEF
8461 #define OS_WAIT4_METHODDEF
8462#endif /* !defined(OS_WAIT4_METHODDEF) */
8463
8464#ifndef OS_WAITID_METHODDEF
8465 #define OS_WAITID_METHODDEF
8466#endif /* !defined(OS_WAITID_METHODDEF) */
8467
8468#ifndef OS_WAITPID_METHODDEF
8469 #define OS_WAITPID_METHODDEF
8470#endif /* !defined(OS_WAITPID_METHODDEF) */
8471
8472#ifndef OS_WAIT_METHODDEF
8473 #define OS_WAIT_METHODDEF
8474#endif /* !defined(OS_WAIT_METHODDEF) */
8475
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03008476#ifndef OS_READLINK_METHODDEF
8477 #define OS_READLINK_METHODDEF
8478#endif /* !defined(OS_READLINK_METHODDEF) */
8479
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03008480#ifndef OS_SYMLINK_METHODDEF
8481 #define OS_SYMLINK_METHODDEF
8482#endif /* !defined(OS_SYMLINK_METHODDEF) */
8483
8484#ifndef OS_TIMES_METHODDEF
8485 #define OS_TIMES_METHODDEF
8486#endif /* !defined(OS_TIMES_METHODDEF) */
8487
8488#ifndef OS_GETSID_METHODDEF
8489 #define OS_GETSID_METHODDEF
8490#endif /* !defined(OS_GETSID_METHODDEF) */
8491
8492#ifndef OS_SETSID_METHODDEF
8493 #define OS_SETSID_METHODDEF
8494#endif /* !defined(OS_SETSID_METHODDEF) */
8495
8496#ifndef OS_SETPGID_METHODDEF
8497 #define OS_SETPGID_METHODDEF
8498#endif /* !defined(OS_SETPGID_METHODDEF) */
8499
8500#ifndef OS_TCGETPGRP_METHODDEF
8501 #define OS_TCGETPGRP_METHODDEF
8502#endif /* !defined(OS_TCGETPGRP_METHODDEF) */
8503
8504#ifndef OS_TCSETPGRP_METHODDEF
8505 #define OS_TCSETPGRP_METHODDEF
8506#endif /* !defined(OS_TCSETPGRP_METHODDEF) */
8507
8508#ifndef OS_LOCKF_METHODDEF
8509 #define OS_LOCKF_METHODDEF
8510#endif /* !defined(OS_LOCKF_METHODDEF) */
8511
8512#ifndef OS_READV_METHODDEF
8513 #define OS_READV_METHODDEF
8514#endif /* !defined(OS_READV_METHODDEF) */
8515
8516#ifndef OS_PREAD_METHODDEF
8517 #define OS_PREAD_METHODDEF
8518#endif /* !defined(OS_PREAD_METHODDEF) */
8519
Pablo Galindo4defba32018-01-27 16:16:37 +00008520#ifndef OS_PREADV_METHODDEF
8521 #define OS_PREADV_METHODDEF
8522#endif /* !defined(OS_PREADV_METHODDEF) */
8523
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02008524#ifndef OS__FCOPYFILE_METHODDEF
8525 #define OS__FCOPYFILE_METHODDEF
8526#endif /* !defined(OS__FCOPYFILE_METHODDEF) */
8527
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03008528#ifndef OS_PIPE_METHODDEF
8529 #define OS_PIPE_METHODDEF
8530#endif /* !defined(OS_PIPE_METHODDEF) */
8531
8532#ifndef OS_PIPE2_METHODDEF
8533 #define OS_PIPE2_METHODDEF
8534#endif /* !defined(OS_PIPE2_METHODDEF) */
8535
8536#ifndef OS_WRITEV_METHODDEF
8537 #define OS_WRITEV_METHODDEF
8538#endif /* !defined(OS_WRITEV_METHODDEF) */
8539
8540#ifndef OS_PWRITE_METHODDEF
8541 #define OS_PWRITE_METHODDEF
8542#endif /* !defined(OS_PWRITE_METHODDEF) */
8543
Pablo Galindo4defba32018-01-27 16:16:37 +00008544#ifndef OS_PWRITEV_METHODDEF
8545 #define OS_PWRITEV_METHODDEF
8546#endif /* !defined(OS_PWRITEV_METHODDEF) */
8547
Pablo Galindoaac4d032019-05-31 19:39:47 +01008548#ifndef OS_COPY_FILE_RANGE_METHODDEF
8549 #define OS_COPY_FILE_RANGE_METHODDEF
8550#endif /* !defined(OS_COPY_FILE_RANGE_METHODDEF) */
8551
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03008552#ifndef OS_MKFIFO_METHODDEF
8553 #define OS_MKFIFO_METHODDEF
8554#endif /* !defined(OS_MKFIFO_METHODDEF) */
8555
8556#ifndef OS_MKNOD_METHODDEF
8557 #define OS_MKNOD_METHODDEF
8558#endif /* !defined(OS_MKNOD_METHODDEF) */
8559
8560#ifndef OS_MAJOR_METHODDEF
8561 #define OS_MAJOR_METHODDEF
8562#endif /* !defined(OS_MAJOR_METHODDEF) */
8563
8564#ifndef OS_MINOR_METHODDEF
8565 #define OS_MINOR_METHODDEF
8566#endif /* !defined(OS_MINOR_METHODDEF) */
8567
8568#ifndef OS_MAKEDEV_METHODDEF
8569 #define OS_MAKEDEV_METHODDEF
8570#endif /* !defined(OS_MAKEDEV_METHODDEF) */
8571
8572#ifndef OS_FTRUNCATE_METHODDEF
8573 #define OS_FTRUNCATE_METHODDEF
8574#endif /* !defined(OS_FTRUNCATE_METHODDEF) */
8575
8576#ifndef OS_TRUNCATE_METHODDEF
8577 #define OS_TRUNCATE_METHODDEF
8578#endif /* !defined(OS_TRUNCATE_METHODDEF) */
8579
8580#ifndef OS_POSIX_FALLOCATE_METHODDEF
8581 #define OS_POSIX_FALLOCATE_METHODDEF
8582#endif /* !defined(OS_POSIX_FALLOCATE_METHODDEF) */
8583
8584#ifndef OS_POSIX_FADVISE_METHODDEF
8585 #define OS_POSIX_FADVISE_METHODDEF
8586#endif /* !defined(OS_POSIX_FADVISE_METHODDEF) */
8587
8588#ifndef OS_PUTENV_METHODDEF
8589 #define OS_PUTENV_METHODDEF
8590#endif /* !defined(OS_PUTENV_METHODDEF) */
8591
8592#ifndef OS_UNSETENV_METHODDEF
8593 #define OS_UNSETENV_METHODDEF
8594#endif /* !defined(OS_UNSETENV_METHODDEF) */
8595
8596#ifndef OS_WCOREDUMP_METHODDEF
8597 #define OS_WCOREDUMP_METHODDEF
8598#endif /* !defined(OS_WCOREDUMP_METHODDEF) */
8599
8600#ifndef OS_WIFCONTINUED_METHODDEF
8601 #define OS_WIFCONTINUED_METHODDEF
8602#endif /* !defined(OS_WIFCONTINUED_METHODDEF) */
8603
8604#ifndef OS_WIFSTOPPED_METHODDEF
8605 #define OS_WIFSTOPPED_METHODDEF
8606#endif /* !defined(OS_WIFSTOPPED_METHODDEF) */
8607
8608#ifndef OS_WIFSIGNALED_METHODDEF
8609 #define OS_WIFSIGNALED_METHODDEF
8610#endif /* !defined(OS_WIFSIGNALED_METHODDEF) */
8611
8612#ifndef OS_WIFEXITED_METHODDEF
8613 #define OS_WIFEXITED_METHODDEF
8614#endif /* !defined(OS_WIFEXITED_METHODDEF) */
8615
8616#ifndef OS_WEXITSTATUS_METHODDEF
8617 #define OS_WEXITSTATUS_METHODDEF
8618#endif /* !defined(OS_WEXITSTATUS_METHODDEF) */
8619
8620#ifndef OS_WTERMSIG_METHODDEF
8621 #define OS_WTERMSIG_METHODDEF
8622#endif /* !defined(OS_WTERMSIG_METHODDEF) */
8623
8624#ifndef OS_WSTOPSIG_METHODDEF
8625 #define OS_WSTOPSIG_METHODDEF
8626#endif /* !defined(OS_WSTOPSIG_METHODDEF) */
8627
8628#ifndef OS_FSTATVFS_METHODDEF
8629 #define OS_FSTATVFS_METHODDEF
8630#endif /* !defined(OS_FSTATVFS_METHODDEF) */
8631
8632#ifndef OS_STATVFS_METHODDEF
8633 #define OS_STATVFS_METHODDEF
8634#endif /* !defined(OS_STATVFS_METHODDEF) */
8635
8636#ifndef OS__GETDISKUSAGE_METHODDEF
8637 #define OS__GETDISKUSAGE_METHODDEF
8638#endif /* !defined(OS__GETDISKUSAGE_METHODDEF) */
8639
8640#ifndef OS_FPATHCONF_METHODDEF
8641 #define OS_FPATHCONF_METHODDEF
8642#endif /* !defined(OS_FPATHCONF_METHODDEF) */
8643
8644#ifndef OS_PATHCONF_METHODDEF
8645 #define OS_PATHCONF_METHODDEF
8646#endif /* !defined(OS_PATHCONF_METHODDEF) */
8647
8648#ifndef OS_CONFSTR_METHODDEF
8649 #define OS_CONFSTR_METHODDEF
8650#endif /* !defined(OS_CONFSTR_METHODDEF) */
8651
8652#ifndef OS_SYSCONF_METHODDEF
8653 #define OS_SYSCONF_METHODDEF
8654#endif /* !defined(OS_SYSCONF_METHODDEF) */
8655
Steve Dowercc16be82016-09-08 10:35:16 -07008656#ifndef OS_STARTFILE_METHODDEF
8657 #define OS_STARTFILE_METHODDEF
8658#endif /* !defined(OS_STARTFILE_METHODDEF) */
8659
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03008660#ifndef OS_GETLOADAVG_METHODDEF
8661 #define OS_GETLOADAVG_METHODDEF
8662#endif /* !defined(OS_GETLOADAVG_METHODDEF) */
8663
8664#ifndef OS_SETRESUID_METHODDEF
8665 #define OS_SETRESUID_METHODDEF
8666#endif /* !defined(OS_SETRESUID_METHODDEF) */
8667
8668#ifndef OS_SETRESGID_METHODDEF
8669 #define OS_SETRESGID_METHODDEF
8670#endif /* !defined(OS_SETRESGID_METHODDEF) */
8671
8672#ifndef OS_GETRESUID_METHODDEF
8673 #define OS_GETRESUID_METHODDEF
8674#endif /* !defined(OS_GETRESUID_METHODDEF) */
8675
8676#ifndef OS_GETRESGID_METHODDEF
8677 #define OS_GETRESGID_METHODDEF
8678#endif /* !defined(OS_GETRESGID_METHODDEF) */
8679
8680#ifndef OS_GETXATTR_METHODDEF
8681 #define OS_GETXATTR_METHODDEF
8682#endif /* !defined(OS_GETXATTR_METHODDEF) */
8683
8684#ifndef OS_SETXATTR_METHODDEF
8685 #define OS_SETXATTR_METHODDEF
8686#endif /* !defined(OS_SETXATTR_METHODDEF) */
8687
8688#ifndef OS_REMOVEXATTR_METHODDEF
8689 #define OS_REMOVEXATTR_METHODDEF
8690#endif /* !defined(OS_REMOVEXATTR_METHODDEF) */
8691
8692#ifndef OS_LISTXATTR_METHODDEF
8693 #define OS_LISTXATTR_METHODDEF
8694#endif /* !defined(OS_LISTXATTR_METHODDEF) */
8695
Zackery Spytz43fdbd22019-05-29 13:57:07 -06008696#ifndef OS_MEMFD_CREATE_METHODDEF
8697 #define OS_MEMFD_CREATE_METHODDEF
8698#endif /* !defined(OS_MEMFD_CREATE_METHODDEF) */
8699
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03008700#ifndef OS_GET_HANDLE_INHERITABLE_METHODDEF
8701 #define OS_GET_HANDLE_INHERITABLE_METHODDEF
8702#endif /* !defined(OS_GET_HANDLE_INHERITABLE_METHODDEF) */
8703
8704#ifndef OS_SET_HANDLE_INHERITABLE_METHODDEF
8705 #define OS_SET_HANDLE_INHERITABLE_METHODDEF
8706#endif /* !defined(OS_SET_HANDLE_INHERITABLE_METHODDEF) */
Victor Stinner9b1f4742016-09-06 16:18:52 -07008707
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03008708#ifndef OS_GET_BLOCKING_METHODDEF
8709 #define OS_GET_BLOCKING_METHODDEF
8710#endif /* !defined(OS_GET_BLOCKING_METHODDEF) */
8711
8712#ifndef OS_SET_BLOCKING_METHODDEF
8713 #define OS_SET_BLOCKING_METHODDEF
8714#endif /* !defined(OS_SET_BLOCKING_METHODDEF) */
8715
Victor Stinner9b1f4742016-09-06 16:18:52 -07008716#ifndef OS_GETRANDOM_METHODDEF
8717 #define OS_GETRANDOM_METHODDEF
8718#endif /* !defined(OS_GETRANDOM_METHODDEF) */
Steve Dower2438cdf2019-03-29 16:37:16 -07008719
8720#ifndef OS__ADD_DLL_DIRECTORY_METHODDEF
8721 #define OS__ADD_DLL_DIRECTORY_METHODDEF
8722#endif /* !defined(OS__ADD_DLL_DIRECTORY_METHODDEF) */
8723
8724#ifndef OS__REMOVE_DLL_DIRECTORY_METHODDEF
8725 #define OS__REMOVE_DLL_DIRECTORY_METHODDEF
8726#endif /* !defined(OS__REMOVE_DLL_DIRECTORY_METHODDEF) */
Steve Dowerdf2d4a62019-08-21 15:27:33 -07008727/*[clinic end generated code: output=b71eff00b91f5e43 input=a9049054013a1b77]*/