Branch data Line data Source code
1 : : /*[clinic input]
2 : : preserve
3 : : [clinic start generated code]*/
4 : :
5 : : PyDoc_STRVAR(fcntl_fcntl__doc__,
6 : : "fcntl($module, fd, cmd, arg=0, /)\n"
7 : : "--\n"
8 : : "\n"
9 : : "Perform the operation `cmd` on file descriptor fd.\n"
10 : : "\n"
11 : : "The values used for `cmd` are operating system dependent, and are available\n"
12 : : "as constants in the fcntl module, using the same names as used in\n"
13 : : "the relevant C header files. The argument arg is optional, and\n"
14 : : "defaults to 0; it may be an int or a string. If arg is given as a string,\n"
15 : : "the return value of fcntl is a string of that length, containing the\n"
16 : : "resulting value put in the arg buffer by the operating system. The length\n"
17 : : "of the arg string is not allowed to exceed 1024 bytes. If the arg given\n"
18 : : "is an integer or if none is specified, the result value is an integer\n"
19 : : "corresponding to the return value of the fcntl call in the C code.");
20 : :
21 : : #define FCNTL_FCNTL_METHODDEF \
22 : : {"fcntl", _PyCFunction_CAST(fcntl_fcntl), METH_FASTCALL, fcntl_fcntl__doc__},
23 : :
24 : : static PyObject *
25 : : fcntl_fcntl_impl(PyObject *module, int fd, int code, PyObject *arg);
26 : :
27 : : static PyObject *
28 : 63 : fcntl_fcntl(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
29 : : {
30 : 63 : PyObject *return_value = NULL;
31 : : int fd;
32 : : int code;
33 : 63 : PyObject *arg = NULL;
34 : :
35 [ + - - + : 63 : if (!_PyArg_CheckPositional("fcntl", nargs, 2, 3)) {
- - ]
36 : 0 : goto exit;
37 : : }
38 [ + + ]: 63 : if (!_PyLong_FileDescriptor_Converter(args[0], &fd)) {
39 : 8 : goto exit;
40 : : }
41 : 55 : code = _PyLong_AsInt(args[1]);
42 [ - + - - ]: 55 : if (code == -1 && PyErr_Occurred()) {
43 : 0 : goto exit;
44 : : }
45 [ + + ]: 55 : if (nargs < 3) {
46 : 24 : goto skip_optional;
47 : : }
48 : 31 : arg = args[2];
49 : 55 : skip_optional:
50 : 55 : return_value = fcntl_fcntl_impl(module, fd, code, arg);
51 : :
52 : 63 : exit:
53 : 63 : return return_value;
54 : : }
55 : :
56 : : PyDoc_STRVAR(fcntl_ioctl__doc__,
57 : : "ioctl($module, fd, request, arg=0, mutate_flag=True, /)\n"
58 : : "--\n"
59 : : "\n"
60 : : "Perform the operation `request` on file descriptor `fd`.\n"
61 : : "\n"
62 : : "The values used for `request` are operating system dependent, and are available\n"
63 : : "as constants in the fcntl or termios library modules, using the same names as\n"
64 : : "used in the relevant C header files.\n"
65 : : "\n"
66 : : "The argument `arg` is optional, and defaults to 0; it may be an int or a\n"
67 : : "buffer containing character data (most likely a string or an array).\n"
68 : : "\n"
69 : : "If the argument is a mutable buffer (such as an array) and if the\n"
70 : : "mutate_flag argument (which is only allowed in this case) is true then the\n"
71 : : "buffer is (in effect) passed to the operating system and changes made by\n"
72 : : "the OS will be reflected in the contents of the buffer after the call has\n"
73 : : "returned. The return value is the integer returned by the ioctl system\n"
74 : : "call.\n"
75 : : "\n"
76 : : "If the argument is a mutable buffer and the mutable_flag argument is false,\n"
77 : : "the behavior is as if a string had been passed.\n"
78 : : "\n"
79 : : "If the argument is an immutable buffer (most likely a string) then a copy\n"
80 : : "of the buffer is passed to the operating system and the return value is a\n"
81 : : "string of the same length containing whatever the operating system put in\n"
82 : : "the buffer. The length of the arg buffer in this case is not allowed to\n"
83 : : "exceed 1024 bytes.\n"
84 : : "\n"
85 : : "If the arg given is an integer or if none is specified, the result value is\n"
86 : : "an integer corresponding to the return value of the ioctl call in the C\n"
87 : : "code.");
88 : :
89 : : #define FCNTL_IOCTL_METHODDEF \
90 : : {"ioctl", _PyCFunction_CAST(fcntl_ioctl), METH_FASTCALL, fcntl_ioctl__doc__},
91 : :
92 : : static PyObject *
93 : : fcntl_ioctl_impl(PyObject *module, int fd, unsigned int code,
94 : : PyObject *ob_arg, int mutate_arg);
95 : :
96 : : static PyObject *
97 : 6 : fcntl_ioctl(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
98 : : {
99 : 6 : PyObject *return_value = NULL;
100 : : int fd;
101 : : unsigned int code;
102 : 6 : PyObject *ob_arg = NULL;
103 : 6 : int mutate_arg = 1;
104 : :
105 [ + - - + : 6 : if (!_PyArg_CheckPositional("ioctl", nargs, 2, 4)) {
- - ]
106 : 0 : goto exit;
107 : : }
108 [ - + ]: 6 : if (!_PyLong_FileDescriptor_Converter(args[0], &fd)) {
109 : 0 : goto exit;
110 : : }
111 : 6 : code = (unsigned int)PyLong_AsUnsignedLongMask(args[1]);
112 [ - + - - ]: 6 : if (code == (unsigned int)-1 && PyErr_Occurred()) {
113 : 0 : goto exit;
114 : : }
115 [ - + ]: 6 : if (nargs < 3) {
116 : 0 : goto skip_optional;
117 : : }
118 : 6 : ob_arg = args[2];
119 [ + - ]: 6 : if (nargs < 4) {
120 : 6 : goto skip_optional;
121 : : }
122 : 0 : mutate_arg = PyObject_IsTrue(args[3]);
123 [ # # ]: 0 : if (mutate_arg < 0) {
124 : 0 : goto exit;
125 : : }
126 : 0 : skip_optional:
127 : 6 : return_value = fcntl_ioctl_impl(module, fd, code, ob_arg, mutate_arg);
128 : :
129 : 6 : exit:
130 : 6 : return return_value;
131 : : }
132 : :
133 : : PyDoc_STRVAR(fcntl_flock__doc__,
134 : : "flock($module, fd, operation, /)\n"
135 : : "--\n"
136 : : "\n"
137 : : "Perform the lock operation `operation` on file descriptor `fd`.\n"
138 : : "\n"
139 : : "See the Unix manual page for flock(2) for details (On some systems, this\n"
140 : : "function is emulated using fcntl()).");
141 : :
142 : : #define FCNTL_FLOCK_METHODDEF \
143 : : {"flock", _PyCFunction_CAST(fcntl_flock), METH_FASTCALL, fcntl_flock__doc__},
144 : :
145 : : static PyObject *
146 : : fcntl_flock_impl(PyObject *module, int fd, int code);
147 : :
148 : : static PyObject *
149 : 22 : fcntl_flock(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
150 : : {
151 : 22 : PyObject *return_value = NULL;
152 : : int fd;
153 : : int code;
154 : :
155 [ + - - + : 22 : if (!_PyArg_CheckPositional("flock", nargs, 2, 2)) {
- - ]
156 : 0 : goto exit;
157 : : }
158 [ + + ]: 22 : if (!_PyLong_FileDescriptor_Converter(args[0], &fd)) {
159 : 3 : goto exit;
160 : : }
161 : 19 : code = _PyLong_AsInt(args[1]);
162 [ - + - - ]: 19 : if (code == -1 && PyErr_Occurred()) {
163 : 0 : goto exit;
164 : : }
165 : 19 : return_value = fcntl_flock_impl(module, fd, code);
166 : :
167 : 22 : exit:
168 : 22 : return return_value;
169 : : }
170 : :
171 : : PyDoc_STRVAR(fcntl_lockf__doc__,
172 : : "lockf($module, fd, cmd, len=0, start=0, whence=0, /)\n"
173 : : "--\n"
174 : : "\n"
175 : : "A wrapper around the fcntl() locking calls.\n"
176 : : "\n"
177 : : "`fd` is the file descriptor of the file to lock or unlock, and operation is one\n"
178 : : "of the following values:\n"
179 : : "\n"
180 : : " LOCK_UN - unlock\n"
181 : : " LOCK_SH - acquire a shared lock\n"
182 : : " LOCK_EX - acquire an exclusive lock\n"
183 : : "\n"
184 : : "When operation is LOCK_SH or LOCK_EX, it can also be bitwise ORed with\n"
185 : : "LOCK_NB to avoid blocking on lock acquisition. If LOCK_NB is used and the\n"
186 : : "lock cannot be acquired, an OSError will be raised and the exception will\n"
187 : : "have an errno attribute set to EACCES or EAGAIN (depending on the operating\n"
188 : : "system -- for portability, check for either value).\n"
189 : : "\n"
190 : : "`len` is the number of bytes to lock, with the default meaning to lock to\n"
191 : : "EOF. `start` is the byte offset, relative to `whence`, to that the lock\n"
192 : : "starts. `whence` is as with fileobj.seek(), specifically:\n"
193 : : "\n"
194 : : " 0 - relative to the start of the file (SEEK_SET)\n"
195 : : " 1 - relative to the current buffer position (SEEK_CUR)\n"
196 : : " 2 - relative to the end of the file (SEEK_END)");
197 : :
198 : : #define FCNTL_LOCKF_METHODDEF \
199 : : {"lockf", _PyCFunction_CAST(fcntl_lockf), METH_FASTCALL, fcntl_lockf__doc__},
200 : :
201 : : static PyObject *
202 : : fcntl_lockf_impl(PyObject *module, int fd, int code, PyObject *lenobj,
203 : : PyObject *startobj, int whence);
204 : :
205 : : static PyObject *
206 : 41 : fcntl_lockf(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
207 : : {
208 : 41 : PyObject *return_value = NULL;
209 : : int fd;
210 : : int code;
211 : 41 : PyObject *lenobj = NULL;
212 : 41 : PyObject *startobj = NULL;
213 : 41 : int whence = 0;
214 : :
215 [ + - - + : 41 : if (!_PyArg_CheckPositional("lockf", nargs, 2, 5)) {
- - ]
216 : 0 : goto exit;
217 : : }
218 [ - + ]: 41 : if (!_PyLong_FileDescriptor_Converter(args[0], &fd)) {
219 : 0 : goto exit;
220 : : }
221 : 41 : code = _PyLong_AsInt(args[1]);
222 [ - + - - ]: 41 : if (code == -1 && PyErr_Occurred()) {
223 : 0 : goto exit;
224 : : }
225 [ + - ]: 41 : if (nargs < 3) {
226 : 41 : goto skip_optional;
227 : : }
228 : 0 : lenobj = args[2];
229 [ # # ]: 0 : if (nargs < 4) {
230 : 0 : goto skip_optional;
231 : : }
232 : 0 : startobj = args[3];
233 [ # # ]: 0 : if (nargs < 5) {
234 : 0 : goto skip_optional;
235 : : }
236 : 0 : whence = _PyLong_AsInt(args[4]);
237 [ # # # # ]: 0 : if (whence == -1 && PyErr_Occurred()) {
238 : 0 : goto exit;
239 : : }
240 : 0 : skip_optional:
241 : 41 : return_value = fcntl_lockf_impl(module, fd, code, lenobj, startobj, whence);
242 : :
243 : 41 : exit:
244 : 41 : return return_value;
245 : : }
246 : : /*[clinic end generated code: output=b8cb14ab35de4c6a input=a9049054013a1b77]*/
|