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