diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2020-04-18 16:14:10 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-18 16:14:10 (GMT) |
commit | 2b5603140c09766a7d4e8243a70d7144f684f6f9 (patch) | |
tree | a6705b01de36003264d9699b775da4459e5bc5df /Modules | |
parent | 12446e6a605f066d837d3a595d0a73e4f3b43b65 (diff) | |
download | cpython-2b5603140c09766a7d4e8243a70d7144f684f6f9.zip cpython-2b5603140c09766a7d4e8243a70d7144f684f6f9.tar.gz cpython-2b5603140c09766a7d4e8243a70d7144f684f6f9.tar.bz2 |
bpo-40178: Convert the remaining os functions to Argument Clinic. (GH-19360)
Convert os.getgrouplist(), os.initgroups(), os.sendfile() and
os.get_terminal_size().
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/clinic/posixmodule.c.h | 559 | ||||
-rw-r--r-- | Modules/posixmodule.c | 281 |
2 files changed, 715 insertions, 125 deletions
diff --git a/Modules/clinic/posixmodule.c.h b/Modules/clinic/posixmodule.c.h index 9465be9..9a605e4 100644 --- a/Modules/clinic/posixmodule.c.h +++ b/Modules/clinic/posixmodule.c.h @@ -3247,6 +3247,118 @@ os_getpid(PyObject *module, PyObject *Py_UNUSED(ignored)) #endif /* defined(HAVE_GETPID) */ +#if defined(HAVE_GETGROUPLIST) && defined(__APPLE__) + +PyDoc_STRVAR(os_getgrouplist__doc__, +"getgrouplist($module, user, group, /)\n" +"--\n" +"\n" +"Returns a list of groups to which a user belongs.\n" +"\n" +" user\n" +" username to lookup\n" +" group\n" +" base group id of the user"); + +#define OS_GETGROUPLIST_METHODDEF \ + {"getgrouplist", (PyCFunction)(void(*)(void))os_getgrouplist, METH_FASTCALL, os_getgrouplist__doc__}, + +static PyObject * +os_getgrouplist_impl(PyObject *module, const char *user, int basegid); + +static PyObject * +os_getgrouplist(PyObject *module, PyObject *const *args, Py_ssize_t nargs) +{ + PyObject *return_value = NULL; + const char *user; + int basegid; + + if (!_PyArg_CheckPositional("getgrouplist", nargs, 2, 2)) { + goto exit; + } + if (!PyUnicode_Check(args[0])) { + _PyArg_BadArgument("getgrouplist", "argument 1", "str", args[0]); + goto exit; + } + Py_ssize_t user_length; + user = PyUnicode_AsUTF8AndSize(args[0], &user_length); + if (user == NULL) { + goto exit; + } + if (strlen(user) != (size_t)user_length) { + PyErr_SetString(PyExc_ValueError, "embedded null character"); + goto exit; + } + if (PyFloat_Check(args[1])) { + PyErr_SetString(PyExc_TypeError, + "integer argument expected, got float" ); + goto exit; + } + basegid = _PyLong_AsInt(args[1]); + if (basegid == -1 && PyErr_Occurred()) { + goto exit; + } + return_value = os_getgrouplist_impl(module, user, basegid); + +exit: + return return_value; +} + +#endif /* defined(HAVE_GETGROUPLIST) && defined(__APPLE__) */ + +#if defined(HAVE_GETGROUPLIST) && !defined(__APPLE__) + +PyDoc_STRVAR(os_getgrouplist__doc__, +"getgrouplist($module, user, group, /)\n" +"--\n" +"\n" +"Returns a list of groups to which a user belongs.\n" +"\n" +" user\n" +" username to lookup\n" +" group\n" +" base group id of the user"); + +#define OS_GETGROUPLIST_METHODDEF \ + {"getgrouplist", (PyCFunction)(void(*)(void))os_getgrouplist, METH_FASTCALL, os_getgrouplist__doc__}, + +static PyObject * +os_getgrouplist_impl(PyObject *module, const char *user, gid_t basegid); + +static PyObject * +os_getgrouplist(PyObject *module, PyObject *const *args, Py_ssize_t nargs) +{ + PyObject *return_value = NULL; + const char *user; + gid_t basegid; + + if (!_PyArg_CheckPositional("getgrouplist", nargs, 2, 2)) { + goto exit; + } + if (!PyUnicode_Check(args[0])) { + _PyArg_BadArgument("getgrouplist", "argument 1", "str", args[0]); + goto exit; + } + Py_ssize_t user_length; + user = PyUnicode_AsUTF8AndSize(args[0], &user_length); + if (user == NULL) { + goto exit; + } + if (strlen(user) != (size_t)user_length) { + PyErr_SetString(PyExc_ValueError, "embedded null character"); + goto exit; + } + if (!_Py_Gid_Converter(args[1], &basegid)) { + goto exit; + } + return_value = os_getgrouplist_impl(module, user, basegid); + +exit: + return return_value; +} + +#endif /* defined(HAVE_GETGROUPLIST) && !defined(__APPLE__) */ + #if defined(HAVE_GETGROUPS) PyDoc_STRVAR(os_getgroups__doc__, @@ -3269,6 +3381,102 @@ os_getgroups(PyObject *module, PyObject *Py_UNUSED(ignored)) #endif /* defined(HAVE_GETGROUPS) */ +#if defined(HAVE_INITGROUPS) && defined(__APPLE__) + +PyDoc_STRVAR(os_initgroups__doc__, +"initgroups($module, username, gid, /)\n" +"--\n" +"\n" +"Initialize the group access list.\n" +"\n" +"Call the system initgroups() to initialize the group access list with all of\n" +"the groups of which the specified username is a member, plus the specified\n" +"group id."); + +#define OS_INITGROUPS_METHODDEF \ + {"initgroups", (PyCFunction)(void(*)(void))os_initgroups, METH_FASTCALL, os_initgroups__doc__}, + +static PyObject * +os_initgroups_impl(PyObject *module, PyObject *oname, int gid); + +static PyObject * +os_initgroups(PyObject *module, PyObject *const *args, Py_ssize_t nargs) +{ + PyObject *return_value = NULL; + PyObject *oname = NULL; + int gid; + + if (!_PyArg_CheckPositional("initgroups", nargs, 2, 2)) { + goto exit; + } + if (!PyUnicode_FSConverter(args[0], &oname)) { + goto exit; + } + if (PyFloat_Check(args[1])) { + PyErr_SetString(PyExc_TypeError, + "integer argument expected, got float" ); + goto exit; + } + gid = _PyLong_AsInt(args[1]); + if (gid == -1 && PyErr_Occurred()) { + goto exit; + } + return_value = os_initgroups_impl(module, oname, gid); + +exit: + /* Cleanup for oname */ + Py_XDECREF(oname); + + return return_value; +} + +#endif /* defined(HAVE_INITGROUPS) && defined(__APPLE__) */ + +#if defined(HAVE_INITGROUPS) && !defined(__APPLE__) + +PyDoc_STRVAR(os_initgroups__doc__, +"initgroups($module, username, gid, /)\n" +"--\n" +"\n" +"Initialize the group access list.\n" +"\n" +"Call the system initgroups() to initialize the group access list with all of\n" +"the groups of which the specified username is a member, plus the specified\n" +"group id."); + +#define OS_INITGROUPS_METHODDEF \ + {"initgroups", (PyCFunction)(void(*)(void))os_initgroups, METH_FASTCALL, os_initgroups__doc__}, + +static PyObject * +os_initgroups_impl(PyObject *module, PyObject *oname, gid_t gid); + +static PyObject * +os_initgroups(PyObject *module, PyObject *const *args, Py_ssize_t nargs) +{ + PyObject *return_value = NULL; + PyObject *oname = NULL; + gid_t gid; + + if (!_PyArg_CheckPositional("initgroups", nargs, 2, 2)) { + goto exit; + } + if (!PyUnicode_FSConverter(args[0], &oname)) { + goto exit; + } + if (!_Py_Gid_Converter(args[1], &gid)) { + goto exit; + } + return_value = os_initgroups_impl(module, oname, gid); + +exit: + /* Cleanup for oname */ + Py_XDECREF(oname); + + return return_value; +} + +#endif /* defined(HAVE_INITGROUPS) && !defined(__APPLE__) */ + #if defined(HAVE_GETPGID) PyDoc_STRVAR(os_getpgid__doc__, @@ -5021,6 +5229,283 @@ exit: return return_value; } +#if defined(HAVE_SENDFILE) && defined(__APPLE__) + +PyDoc_STRVAR(os_sendfile__doc__, +"sendfile($module, /, out_fd, in_fd, offset, count, headers=(),\n" +" trailers=(), flags=0)\n" +"--\n" +"\n" +"Copy count bytes from file descriptor in_fd to file descriptor out_fd."); + +#define OS_SENDFILE_METHODDEF \ + {"sendfile", (PyCFunction)(void(*)(void))os_sendfile, METH_FASTCALL|METH_KEYWORDS, os_sendfile__doc__}, + +static PyObject * +os_sendfile_impl(PyObject *module, int out_fd, int in_fd, Py_off_t offset, + Py_off_t sbytes, PyObject *headers, PyObject *trailers, + int flags); + +static PyObject * +os_sendfile(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) +{ + PyObject *return_value = NULL; + static const char * const _keywords[] = {"out_fd", "in_fd", "offset", "count", "headers", "trailers", "flags", NULL}; + static _PyArg_Parser _parser = {NULL, _keywords, "sendfile", 0}; + PyObject *argsbuf[7]; + Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 4; + int out_fd; + int in_fd; + Py_off_t offset; + Py_off_t sbytes; + PyObject *headers = NULL; + PyObject *trailers = NULL; + int flags = 0; + + args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 4, 7, 0, argsbuf); + if (!args) { + goto exit; + } + if (PyFloat_Check(args[0])) { + PyErr_SetString(PyExc_TypeError, + "integer argument expected, got float" ); + goto exit; + } + out_fd = _PyLong_AsInt(args[0]); + if (out_fd == -1 && PyErr_Occurred()) { + goto exit; + } + if (PyFloat_Check(args[1])) { + PyErr_SetString(PyExc_TypeError, + "integer argument expected, got float" ); + goto exit; + } + in_fd = _PyLong_AsInt(args[1]); + if (in_fd == -1 && PyErr_Occurred()) { + goto exit; + } + if (!Py_off_t_converter(args[2], &offset)) { + goto exit; + } + if (!Py_off_t_converter(args[3], &sbytes)) { + goto exit; + } + if (!noptargs) { + goto skip_optional_pos; + } + if (args[4]) { + headers = args[4]; + if (!--noptargs) { + goto skip_optional_pos; + } + } + if (args[5]) { + trailers = args[5]; + if (!--noptargs) { + goto skip_optional_pos; + } + } + if (PyFloat_Check(args[6])) { + PyErr_SetString(PyExc_TypeError, + "integer argument expected, got float" ); + goto exit; + } + flags = _PyLong_AsInt(args[6]); + if (flags == -1 && PyErr_Occurred()) { + goto exit; + } +skip_optional_pos: + return_value = os_sendfile_impl(module, out_fd, in_fd, offset, sbytes, headers, trailers, flags); + +exit: + return return_value; +} + +#endif /* defined(HAVE_SENDFILE) && defined(__APPLE__) */ + +#if defined(HAVE_SENDFILE) && !defined(__APPLE__) && (defined(__FreeBSD__) || defined(__DragonFly__)) + +PyDoc_STRVAR(os_sendfile__doc__, +"sendfile($module, /, out_fd, in_fd, offset, count, headers=(),\n" +" trailers=(), flags=0)\n" +"--\n" +"\n" +"Copy count bytes from file descriptor in_fd to file descriptor out_fd."); + +#define OS_SENDFILE_METHODDEF \ + {"sendfile", (PyCFunction)(void(*)(void))os_sendfile, METH_FASTCALL|METH_KEYWORDS, os_sendfile__doc__}, + +static PyObject * +os_sendfile_impl(PyObject *module, int out_fd, int in_fd, Py_off_t offset, + Py_ssize_t count, PyObject *headers, PyObject *trailers, + int flags); + +static PyObject * +os_sendfile(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) +{ + PyObject *return_value = NULL; + static const char * const _keywords[] = {"out_fd", "in_fd", "offset", "count", "headers", "trailers", "flags", NULL}; + static _PyArg_Parser _parser = {NULL, _keywords, "sendfile", 0}; + PyObject *argsbuf[7]; + Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 4; + int out_fd; + int in_fd; + Py_off_t offset; + Py_ssize_t count; + PyObject *headers = NULL; + PyObject *trailers = NULL; + int flags = 0; + + args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 4, 7, 0, argsbuf); + if (!args) { + goto exit; + } + if (PyFloat_Check(args[0])) { + PyErr_SetString(PyExc_TypeError, + "integer argument expected, got float" ); + goto exit; + } + out_fd = _PyLong_AsInt(args[0]); + if (out_fd == -1 && PyErr_Occurred()) { + goto exit; + } + if (PyFloat_Check(args[1])) { + PyErr_SetString(PyExc_TypeError, + "integer argument expected, got float" ); + goto exit; + } + in_fd = _PyLong_AsInt(args[1]); + if (in_fd == -1 && PyErr_Occurred()) { + goto exit; + } + if (!Py_off_t_converter(args[2], &offset)) { + goto exit; + } + if (PyFloat_Check(args[3])) { + PyErr_SetString(PyExc_TypeError, + "integer argument expected, got float" ); + goto exit; + } + { + Py_ssize_t ival = -1; + PyObject *iobj = PyNumber_Index(args[3]); + if (iobj != NULL) { + ival = PyLong_AsSsize_t(iobj); + Py_DECREF(iobj); + } + if (ival == -1 && PyErr_Occurred()) { + goto exit; + } + count = ival; + } + if (!noptargs) { + goto skip_optional_pos; + } + if (args[4]) { + headers = args[4]; + if (!--noptargs) { + goto skip_optional_pos; + } + } + if (args[5]) { + trailers = args[5]; + if (!--noptargs) { + goto skip_optional_pos; + } + } + if (PyFloat_Check(args[6])) { + PyErr_SetString(PyExc_TypeError, + "integer argument expected, got float" ); + goto exit; + } + flags = _PyLong_AsInt(args[6]); + if (flags == -1 && PyErr_Occurred()) { + goto exit; + } +skip_optional_pos: + return_value = os_sendfile_impl(module, out_fd, in_fd, offset, count, headers, trailers, flags); + +exit: + return return_value; +} + +#endif /* defined(HAVE_SENDFILE) && !defined(__APPLE__) && (defined(__FreeBSD__) || defined(__DragonFly__)) */ + +#if defined(HAVE_SENDFILE) && !defined(__APPLE__) && !(defined(__FreeBSD__) || defined(__DragonFly__)) + +PyDoc_STRVAR(os_sendfile__doc__, +"sendfile($module, /, out_fd, in_fd, offset, count)\n" +"--\n" +"\n" +"Copy count bytes from file descriptor in_fd to file descriptor out_fd."); + +#define OS_SENDFILE_METHODDEF \ + {"sendfile", (PyCFunction)(void(*)(void))os_sendfile, METH_FASTCALL|METH_KEYWORDS, os_sendfile__doc__}, + +static PyObject * +os_sendfile_impl(PyObject *module, int out_fd, int in_fd, PyObject *offobj, + Py_ssize_t count); + +static PyObject * +os_sendfile(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) +{ + PyObject *return_value = NULL; + static const char * const _keywords[] = {"out_fd", "in_fd", "offset", "count", NULL}; + static _PyArg_Parser _parser = {NULL, _keywords, "sendfile", 0}; + PyObject *argsbuf[4]; + int out_fd; + int in_fd; + PyObject *offobj; + Py_ssize_t count; + + args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 4, 4, 0, argsbuf); + if (!args) { + goto exit; + } + if (PyFloat_Check(args[0])) { + PyErr_SetString(PyExc_TypeError, + "integer argument expected, got float" ); + goto exit; + } + out_fd = _PyLong_AsInt(args[0]); + if (out_fd == -1 && PyErr_Occurred()) { + goto exit; + } + if (PyFloat_Check(args[1])) { + PyErr_SetString(PyExc_TypeError, + "integer argument expected, got float" ); + goto exit; + } + in_fd = _PyLong_AsInt(args[1]); + if (in_fd == -1 && PyErr_Occurred()) { + goto exit; + } + offobj = args[2]; + if (PyFloat_Check(args[3])) { + PyErr_SetString(PyExc_TypeError, + "integer argument expected, got float" ); + goto exit; + } + { + Py_ssize_t ival = -1; + PyObject *iobj = PyNumber_Index(args[3]); + if (iobj != NULL) { + ival = PyLong_AsSsize_t(iobj); + Py_DECREF(iobj); + } + if (ival == -1 && PyErr_Occurred()) { + goto exit; + } + count = ival; + } + return_value = os_sendfile_impl(module, out_fd, in_fd, offobj, count); + +exit: + return return_value; +} + +#endif /* defined(HAVE_SENDFILE) && !defined(__APPLE__) && !(defined(__FreeBSD__) || defined(__DragonFly__)) */ + #if defined(__APPLE__) PyDoc_STRVAR(os__fcopyfile__doc__, @@ -7568,6 +8053,62 @@ exit: #endif /* defined(HAVE_MEMFD_CREATE) */ +#if (defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL)) + +PyDoc_STRVAR(os_get_terminal_size__doc__, +"get_terminal_size($module, fd=<unrepresentable>, /)\n" +"--\n" +"\n" +"Return the size of the terminal window as (columns, lines).\n" +"\n" +"The optional argument fd (default standard output) specifies\n" +"which file descriptor should be queried.\n" +"\n" +"If the file descriptor is not connected to a terminal, an OSError\n" +"is thrown.\n" +"\n" +"This function will only be defined if an implementation is\n" +"available for this system.\n" +"\n" +"shutil.get_terminal_size is the high-level function which should\n" +"normally be used, os.get_terminal_size is the low-level implementation."); + +#define OS_GET_TERMINAL_SIZE_METHODDEF \ + {"get_terminal_size", (PyCFunction)(void(*)(void))os_get_terminal_size, METH_FASTCALL, os_get_terminal_size__doc__}, + +static PyObject * +os_get_terminal_size_impl(PyObject *module, int fd); + +static PyObject * +os_get_terminal_size(PyObject *module, PyObject *const *args, Py_ssize_t nargs) +{ + PyObject *return_value = NULL; + int fd = fileno(stdout); + + if (!_PyArg_CheckPositional("get_terminal_size", nargs, 0, 1)) { + goto exit; + } + if (nargs < 1) { + goto skip_optional; + } + if (PyFloat_Check(args[0])) { + PyErr_SetString(PyExc_TypeError, + "integer argument expected, got float" ); + goto exit; + } + fd = _PyLong_AsInt(args[0]); + if (fd == -1 && PyErr_Occurred()) { + goto exit; + } +skip_optional: + return_value = os_get_terminal_size_impl(module, fd); + +exit: + return return_value; +} + +#endif /* (defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL)) */ + PyDoc_STRVAR(os_cpu_count__doc__, "cpu_count($module, /)\n" "--\n" @@ -8522,10 +9063,18 @@ exit: #define OS_GETPID_METHODDEF #endif /* !defined(OS_GETPID_METHODDEF) */ +#ifndef OS_GETGROUPLIST_METHODDEF + #define OS_GETGROUPLIST_METHODDEF +#endif /* !defined(OS_GETGROUPLIST_METHODDEF) */ + #ifndef OS_GETGROUPS_METHODDEF #define OS_GETGROUPS_METHODDEF #endif /* !defined(OS_GETGROUPS_METHODDEF) */ +#ifndef OS_INITGROUPS_METHODDEF + #define OS_INITGROUPS_METHODDEF +#endif /* !defined(OS_INITGROUPS_METHODDEF) */ + #ifndef OS_GETPGID_METHODDEF #define OS_GETPGID_METHODDEF #endif /* !defined(OS_GETPGID_METHODDEF) */ @@ -8662,6 +9211,10 @@ exit: #define OS_PREADV_METHODDEF #endif /* !defined(OS_PREADV_METHODDEF) */ +#ifndef OS_SENDFILE_METHODDEF + #define OS_SENDFILE_METHODDEF +#endif /* !defined(OS_SENDFILE_METHODDEF) */ + #ifndef OS__FCOPYFILE_METHODDEF #define OS__FCOPYFILE_METHODDEF #endif /* !defined(OS__FCOPYFILE_METHODDEF) */ @@ -8838,6 +9391,10 @@ exit: #define OS_MEMFD_CREATE_METHODDEF #endif /* !defined(OS_MEMFD_CREATE_METHODDEF) */ +#ifndef OS_GET_TERMINAL_SIZE_METHODDEF + #define OS_GET_TERMINAL_SIZE_METHODDEF +#endif /* !defined(OS_GET_TERMINAL_SIZE_METHODDEF) */ + #ifndef OS_GET_HANDLE_INHERITABLE_METHODDEF #define OS_GET_HANDLE_INHERITABLE_METHODDEF #endif /* !defined(OS_GET_HANDLE_INHERITABLE_METHODDEF) */ @@ -8869,4 +9426,4 @@ exit: #ifndef OS_WAITSTATUS_TO_EXITCODE_METHODDEF #define OS_WAITSTATUS_TO_EXITCODE_METHODDEF #endif /* !defined(OS_WAITSTATUS_TO_EXITCODE_METHODDEF) */ -/*[clinic end generated code: output=ca63e471c11dc6e7 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=545c08f76d7a6286 input=a9049054013a1b77]*/ diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index 4085b92..2157cbb 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -6947,23 +6947,46 @@ os_getpid_impl(PyObject *module) #ifdef HAVE_GETGROUPLIST -/* AC 3.5: funny apple logic below */ -PyDoc_STRVAR(posix_getgrouplist__doc__, -"getgrouplist(user, group) -> list of groups to which a user belongs\n\n\ -Returns a list of groups to which a user belongs.\n\n\ - user: username to lookup\n\ - group: base group id of the user"); +#ifdef __APPLE__ +/*[clinic input] +os.getgrouplist + + user: str + username to lookup + group as basegid: int + base group id of the user + / + +Returns a list of groups to which a user belongs. +[clinic start generated code]*/ static PyObject * -posix_getgrouplist(PyObject *self, PyObject *args) +os_getgrouplist_impl(PyObject *module, const char *user, int basegid) +/*[clinic end generated code: output=6e734697b8c26de0 input=f8d870374b09a490]*/ +#else +/*[clinic input] +os.getgrouplist + + user: str + username to lookup + group as basegid: gid_t + base group id of the user + / + +Returns a list of groups to which a user belongs. +[clinic start generated code]*/ + +static PyObject * +os_getgrouplist_impl(PyObject *module, const char *user, gid_t basegid) +/*[clinic end generated code: output=0ebd7fb70115575b input=cc61d5c20b08958d]*/ +#endif { - const char *user; int i, ngroups; PyObject *list; #ifdef __APPLE__ - int *groups, basegid; + int *groups; #else - gid_t *groups, basegid; + gid_t *groups; #endif /* @@ -6976,15 +6999,6 @@ posix_getgrouplist(PyObject *self, PyObject *args) */ ngroups = 1 + MAX_GROUPS; -#ifdef __APPLE__ - if (!PyArg_ParseTuple(args, "si:getgrouplist", &user, &basegid)) - return NULL; -#else - if (!PyArg_ParseTuple(args, "sO&:getgrouplist", &user, - _Py_Gid_Converter, &basegid)) - return NULL; -#endif - while (1) { #ifdef __APPLE__ groups = PyMem_New(int, ngroups); @@ -7155,40 +7169,47 @@ os_getgroups_impl(PyObject *module) #endif /* HAVE_GETGROUPS */ #ifdef HAVE_INITGROUPS -PyDoc_STRVAR(posix_initgroups__doc__, -"initgroups(username, gid) -> None\n\n\ -Call the system initgroups() to initialize the group access list with all of\n\ -the groups of which the specified username is a member, plus the specified\n\ -group id."); +#ifdef __APPLE__ +/*[clinic input] +os.initgroups + + username as oname: FSConverter + gid: int + / + +Initialize the group access list. + +Call the system initgroups() to initialize the group access list with all of +the groups of which the specified username is a member, plus the specified +group id. +[clinic start generated code]*/ -/* AC 3.5: funny apple logic */ static PyObject * -posix_initgroups(PyObject *self, PyObject *args) -{ - PyObject *oname; - const char *username; - int res; -#ifdef __APPLE__ - int gid; +os_initgroups_impl(PyObject *module, PyObject *oname, int gid) +/*[clinic end generated code: output=7f074d30a425fd3a input=df3d54331b0af204]*/ #else - gid_t gid; -#endif +/*[clinic input] +os.initgroups -#ifdef __APPLE__ - if (!PyArg_ParseTuple(args, "O&i:initgroups", - PyUnicode_FSConverter, &oname, - &gid)) -#else - if (!PyArg_ParseTuple(args, "O&O&:initgroups", - PyUnicode_FSConverter, &oname, - _Py_Gid_Converter, &gid)) + username as oname: FSConverter + gid: gid_t + / + +Initialize the group access list. + +Call the system initgroups() to initialize the group access list with all of +the groups of which the specified username is a member, plus the specified +group id. +[clinic start generated code]*/ + +static PyObject * +os_initgroups_impl(PyObject *module, PyObject *oname, gid_t gid) +/*[clinic end generated code: output=59341244521a9e3f input=0cb91bdc59a4c564]*/ #endif - return NULL; - username = PyBytes_AS_STRING(oname); +{ + const char *username = PyBytes_AS_STRING(oname); - res = initgroups(username, gid); - Py_DECREF(oname); - if (res == -1) + if (initgroups(username, gid) == -1) return PyErr_SetFromErrno(PyExc_OSError); Py_RETURN_NONE; @@ -9220,46 +9241,77 @@ os_write_impl(PyObject *module, int fd, Py_buffer *data) } #ifdef HAVE_SENDFILE -PyDoc_STRVAR(posix_sendfile__doc__, -"sendfile(out_fd, in_fd, offset, count) -> byteswritten\n\ -sendfile(out_fd, in_fd, offset, count[, headers][, trailers], flags=0)\n\ - -> byteswritten\n\ -Copy count bytes from file descriptor in_fd to file descriptor out_fd."); +#ifdef __APPLE__ +/*[clinic input] +os.sendfile + + out_fd: int + in_fd: int + offset: Py_off_t + count as sbytes: Py_off_t + headers: object(c_default="NULL") = () + trailers: object(c_default="NULL") = () + flags: int = 0 + +Copy count bytes from file descriptor in_fd to file descriptor out_fd. +[clinic start generated code]*/ -/* AC 3.5: don't bother converting, has optional group*/ static PyObject * -posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict) +os_sendfile_impl(PyObject *module, int out_fd, int in_fd, Py_off_t offset, + Py_off_t sbytes, PyObject *headers, PyObject *trailers, + int flags) +/*[clinic end generated code: output=81c4bcd143f5c82b input=b0d72579d4c69afa]*/ +#elif defined(__FreeBSD__) || defined(__DragonFly__) +/*[clinic input] +os.sendfile + + out_fd: int + in_fd: int + offset: Py_off_t + count: Py_ssize_t + headers: object(c_default="NULL") = () + trailers: object(c_default="NULL") = () + flags: int = 0 + +Copy count bytes from file descriptor in_fd to file descriptor out_fd. +[clinic start generated code]*/ + +static PyObject * +os_sendfile_impl(PyObject *module, int out_fd, int in_fd, Py_off_t offset, + Py_ssize_t count, PyObject *headers, PyObject *trailers, + int flags) +/*[clinic end generated code: output=329ea009bdd55afc input=338adb8ff84ae8cd]*/ +#else +/*[clinic input] +os.sendfile + + out_fd: int + in_fd: int + offset as offobj: object + count: Py_ssize_t + +Copy count bytes from file descriptor in_fd to file descriptor out_fd. +[clinic start generated code]*/ + +static PyObject * +os_sendfile_impl(PyObject *module, int out_fd, int in_fd, PyObject *offobj, + Py_ssize_t count) +/*[clinic end generated code: output=ae81216e40f167d8 input=76d64058c74477ba]*/ +#endif { - int in, out; Py_ssize_t ret; int async_err = 0; - off_t offset; #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) #ifndef __APPLE__ - Py_ssize_t len; + off_t sbytes; #endif - PyObject *headers = NULL, *trailers = NULL; Py_buffer *hbuf, *tbuf; - off_t sbytes; struct sf_hdtr sf; - int flags = 0; - static char *keywords[] = {"out_fd", "in_fd", - "offset", "count", - "headers", "trailers", "flags", NULL}; sf.headers = NULL; sf.trailers = NULL; -#ifdef __APPLE__ - if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile", - keywords, &out, &in, Py_off_t_converter, &offset, Py_off_t_converter, &sbytes, -#else - if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile", - keywords, &out, &in, Py_off_t_converter, &offset, &len, -#endif - &headers, &trailers, &flags)) - return NULL; if (headers != NULL) { if (!PySequence_Check(headers)) { PyErr_SetString(PyExc_TypeError, @@ -9321,9 +9373,9 @@ posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict) do { Py_BEGIN_ALLOW_THREADS #ifdef __APPLE__ - ret = sendfile(in, out, offset, &sbytes, &sf, flags); + ret = sendfile(in_fd, out_fd, offset, &sbytes, &sf, flags); #else - ret = sendfile(in, out, offset, len, &sf, &sbytes, flags); + ret = sendfile(in_fd, out_fd, offset, count, &sf, &sbytes, flags); #endif Py_END_ALLOW_THREADS } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); @@ -9358,18 +9410,11 @@ done: #endif #else - Py_ssize_t count; - PyObject *offobj; - static char *keywords[] = {"out_fd", "in_fd", - "offset", "count", NULL}; - if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile", - keywords, &out, &in, &offobj, &count)) - return NULL; #ifdef __linux__ if (offobj == Py_None) { do { Py_BEGIN_ALLOW_THREADS - ret = sendfile(out, in, NULL, count); + ret = sendfile(out_fd, in_fd, NULL, count); Py_END_ALLOW_THREADS } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); if (ret < 0) @@ -9377,12 +9422,13 @@ done: return Py_BuildValue("n", ret); } #endif + off_t offset; if (!Py_off_t_converter(offobj, &offset)) return NULL; do { Py_BEGIN_ALLOW_THREADS - ret = sendfile(out, in, &offset, count); + ret = sendfile(out_fd, in_fd, &offset, count); Py_END_ALLOW_THREADS } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); if (ret < 0) @@ -12360,29 +12406,34 @@ static PyStructSequence_Desc TerminalSize_desc = { }; #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) -/* AC 3.5: fd should accept None */ -PyDoc_STRVAR(termsize__doc__, - "Return the size of the terminal window as (columns, lines).\n" \ - "\n" \ - "The optional argument fd (default standard output) specifies\n" \ - "which file descriptor should be queried.\n" \ - "\n" \ - "If the file descriptor is not connected to a terminal, an OSError\n" \ - "is thrown.\n" \ - "\n" \ - "This function will only be defined if an implementation is\n" \ - "available for this system.\n" \ - "\n" \ - "shutil.get_terminal_size is the high-level function which should\n" \ - "normally be used, os.get_terminal_size is the low-level implementation."); +/*[clinic input] +os.get_terminal_size -static PyObject* -get_terminal_size(PyObject *self, PyObject *args) + fd: int(c_default="fileno(stdout)", py_default="<unrepresentable>") = -1 + / + +Return the size of the terminal window as (columns, lines). + +The optional argument fd (default standard output) specifies +which file descriptor should be queried. + +If the file descriptor is not connected to a terminal, an OSError +is thrown. + +This function will only be defined if an implementation is +available for this system. + +shutil.get_terminal_size is the high-level function which should +normally be used, os.get_terminal_size is the low-level implementation. +[clinic start generated code]*/ + +static PyObject * +os_get_terminal_size_impl(PyObject *module, int fd) +/*[clinic end generated code: output=fbab93acef980508 input=ead5679b82ddb920]*/ { int columns, lines; PyObject *termsize; - int fd = fileno(stdout); /* Under some conditions stdout may not be connected and * fileno(stdout) may point to an invalid file descriptor. For example * GUI apps don't have valid standard streams by default. @@ -12391,9 +12442,6 @@ get_terminal_size(PyObject *self, PyObject *args) * the ioctl below will fail returning EBADF. This is what we want. */ - if (!PyArg_ParseTuple(args, "|i", &fd)) - return NULL; - #ifdef TERMSIZE_USE_IOCTL { struct winsize w; @@ -12433,7 +12481,7 @@ get_terminal_size(PyObject *self, PyObject *args) } #endif /* TERMSIZE_USE_CONIO */ - PyObject *TerminalSizeType = get_posix_state(self)->TerminalSizeType; + PyObject *TerminalSizeType = get_posix_state(module)->TerminalSizeType; termsize = PyStructSequence_New((PyTypeObject *)TerminalSizeType); if (termsize == NULL) return NULL; @@ -13912,9 +13960,7 @@ static PyMethodDef posix_methods[] = { OS_GETEGID_METHODDEF OS_GETEUID_METHODDEF OS_GETGID_METHODDEF -#ifdef HAVE_GETGROUPLIST - {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__}, -#endif + OS_GETGROUPLIST_METHODDEF OS_GETGROUPS_METHODDEF OS_GETPID_METHODDEF OS_GETPGRP_METHODDEF @@ -13924,9 +13970,7 @@ static PyMethodDef posix_methods[] = { OS_KILL_METHODDEF OS_KILLPG_METHODDEF OS_PLOCK_METHODDEF -#ifdef MS_WINDOWS OS_STARTFILE_METHODDEF -#endif OS_SETUID_METHODDEF OS_SETEUID_METHODDEF OS_SETREUID_METHODDEF @@ -13934,9 +13978,7 @@ static PyMethodDef posix_methods[] = { OS_SETEGID_METHODDEF OS_SETREGID_METHODDEF OS_SETGROUPS_METHODDEF -#ifdef HAVE_INITGROUPS - {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__}, -#endif /* HAVE_INITGROUPS */ + OS_INITGROUPS_METHODDEF OS_GETPGID_METHODDEF OS_SETPGRP_METHODDEF OS_WAIT_METHODDEF @@ -13966,10 +14008,7 @@ static PyMethodDef posix_methods[] = { OS_WRITEV_METHODDEF OS_PWRITE_METHODDEF OS_PWRITEV_METHODDEF -#ifdef HAVE_SENDFILE - {"sendfile", (PyCFunction)(void(*)(void))posix_sendfile, METH_VARARGS | METH_KEYWORDS, - posix_sendfile__doc__}, -#endif + OS_SENDFILE_METHODDEF OS_FSTAT_METHODDEF OS_ISATTY_METHODDEF OS_PIPE_METHODDEF @@ -14021,26 +14060,20 @@ static PyMethodDef posix_methods[] = { OS_REMOVEXATTR_METHODDEF OS_LISTXATTR_METHODDEF -#if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) - {"get_terminal_size", get_terminal_size, METH_VARARGS, termsize__doc__}, -#endif + OS_GET_TERMINAL_SIZE_METHODDEF OS_CPU_COUNT_METHODDEF OS_GET_INHERITABLE_METHODDEF OS_SET_INHERITABLE_METHODDEF OS_GET_HANDLE_INHERITABLE_METHODDEF OS_SET_HANDLE_INHERITABLE_METHODDEF -#ifndef MS_WINDOWS OS_GET_BLOCKING_METHODDEF OS_SET_BLOCKING_METHODDEF -#endif OS_SCANDIR_METHODDEF OS_FSPATH_METHODDEF OS_GETRANDOM_METHODDEF OS_MEMFD_CREATE_METHODDEF -#ifdef MS_WINDOWS OS__ADD_DLL_DIRECTORY_METHODDEF OS__REMOVE_DLL_DIRECTORY_METHODDEF -#endif OS_WAITSTATUS_TO_EXITCODE_METHODDEF {NULL, NULL} /* Sentinel */ }; |