summaryrefslogtreecommitdiff
path: root/Modules/posixmodule.c
diff options
context:
space:
mode:
Diffstat (limited to 'Modules/posixmodule.c')
-rw-r--r--Modules/posixmodule.c214
1 files changed, 104 insertions, 110 deletions
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
index 8f8ba255ec..ffe4815d66 100644
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -1871,8 +1871,7 @@ stat_float_times(PyObject* self, PyObject *args)
/* Return old value */
return PyBool_FromLong(_stat_float_times);
_stat_float_times = newval;
- Py_INCREF(Py_None);
- return Py_None;
+ Py_RETURN_NONE;
}
static PyObject *billion = NULL;
@@ -6194,8 +6193,7 @@ posix_initgroups(PyObject *self, PyObject *args)
if (res == -1)
return PyErr_SetFromErrno(PyExc_OSError);
- Py_INCREF(Py_None);
- return Py_None;
+ Py_RETURN_NONE;
}
#endif /* HAVE_INITGROUPS */
@@ -6258,8 +6256,7 @@ os_setpgrp_impl(PyObject *module)
if (setpgrp() < 0)
#endif /* SETPGRP_HAVE_ARG */
return posix_error();
- Py_INCREF(Py_None);
- return Py_None;
+ Py_RETURN_NONE;
}
#endif /* HAVE_SETPGRP */
@@ -6585,8 +6582,7 @@ os_setreuid_impl(PyObject *module, uid_t ruid, uid_t euid)
if (setreuid(ruid, euid) < 0) {
return posix_error();
} else {
- Py_INCREF(Py_None);
- return Py_None;
+ Py_RETURN_NONE;
}
}
#endif /* HAVE_SETREUID */
@@ -6681,8 +6677,7 @@ os_setgroups(PyObject *module, PyObject *groups)
if (setgroups(len, grouplist) < 0)
return posix_error();
- Py_INCREF(Py_None);
- return Py_None;
+ Py_RETURN_NONE;
}
#endif /* HAVE_SETGROUPS */
@@ -10341,8 +10336,13 @@ os_abort_impl(PyObject *module)
{
abort();
/*NOTREACHED*/
+#ifndef __clang__
+ /* Issue #28152: abort() is declared with __attribute__((__noreturn__)).
+ GCC emits a warning without "return NULL;" (compiler bug?), but Clang
+ is smarter and emits a warning on the return. */
Py_FatalError("abort() called from Python code didn't abort!");
return NULL;
+#endif
}
#ifdef MS_WINDOWS
@@ -11143,10 +11143,10 @@ posix_set_blocking(PyObject *self, PyObject *args)
#endif /* !MS_WINDOWS */
-PyDoc_STRVAR(posix_scandir__doc__,
-"scandir(path='.') -> iterator of DirEntry objects for given path");
-
-static char *follow_symlinks_keywords[] = {"follow_symlinks", NULL};
+/*[clinic input]
+class os.DirEntry "DirEntry *" "&DirEntryType"
+[clinic start generated code]*/
+/*[clinic end generated code: output=da39a3ee5e6b4b0d input=3138f09f7c683f1d]*/
typedef struct {
PyObject_HEAD
@@ -11180,9 +11180,15 @@ DirEntry_dealloc(DirEntry *entry)
static int
DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits);
-/* Set exception and return -1 on error, 0 for False, 1 for True */
+/*[clinic input]
+os.DirEntry.is_symlink -> bool
+
+Return True if the entry is a symbolic link; cached per entry.
+[clinic start generated code]*/
+
static int
-DirEntry_is_symlink(DirEntry *self)
+os_DirEntry_is_symlink_impl(DirEntry *self)
+/*[clinic end generated code: output=42244667d7bcfc25 input=1605a1b4b96976c3]*/
{
#ifdef MS_WINDOWS
return (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK;
@@ -11199,17 +11205,6 @@ DirEntry_is_symlink(DirEntry *self)
}
static PyObject *
-DirEntry_py_is_symlink(DirEntry *self)
-{
- int result;
-
- result = DirEntry_is_symlink(self);
- if (result == -1)
- return NULL;
- return PyBool_FromLong(result);
-}
-
-static PyObject *
DirEntry_fetch_stat(DirEntry *self, int follow_symlinks)
{
int result;
@@ -11251,14 +11246,23 @@ DirEntry_get_lstat(DirEntry *self)
return self->lstat;
}
+/*[clinic input]
+os.DirEntry.stat
+ *
+ follow_symlinks: bool = True
+
+Return stat_result object for the entry; cached per entry.
+[clinic start generated code]*/
+
static PyObject *
-DirEntry_get_stat(DirEntry *self, int follow_symlinks)
+os_DirEntry_stat_impl(DirEntry *self, int follow_symlinks)
+/*[clinic end generated code: output=008593b3a6d01305 input=280d14c1d6f1d00d]*/
{
if (!follow_symlinks)
return DirEntry_get_lstat(self);
if (!self->stat) {
- int result = DirEntry_is_symlink(self);
+ int result = os_DirEntry_is_symlink_impl(self);
if (result == -1)
return NULL;
else if (result)
@@ -11271,18 +11275,6 @@ DirEntry_get_stat(DirEntry *self, int follow_symlinks)
return self->stat;
}
-static PyObject *
-DirEntry_stat(DirEntry *self, PyObject *args, PyObject *kwargs)
-{
- int follow_symlinks = 1;
-
- if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|$p:DirEntry.stat",
- follow_symlinks_keywords, &follow_symlinks))
- return NULL;
-
- return DirEntry_get_stat(self, follow_symlinks);
-}
-
/* Set exception and return -1 on error, 0 for False, 1 for True */
static int
DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits)
@@ -11311,7 +11303,7 @@ DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits
#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
if (need_stat) {
#endif
- stat = DirEntry_get_stat(self, follow_symlinks);
+ stat = os_DirEntry_stat_impl(self, follow_symlinks);
if (!stat) {
if (PyErr_ExceptionMatches(PyExc_FileNotFoundError)) {
/* If file doesn't exist (anymore), then return False
@@ -11362,43 +11354,45 @@ error:
return -1;
}
-static PyObject *
-DirEntry_py_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits)
-{
- int result;
+/*[clinic input]
+os.DirEntry.is_dir -> bool
+ *
+ follow_symlinks: bool = True
- result = DirEntry_test_mode(self, follow_symlinks, mode_bits);
- if (result == -1)
- return NULL;
- return PyBool_FromLong(result);
-}
+Return True if the entry is a directory; cached per entry.
+[clinic start generated code]*/
-static PyObject *
-DirEntry_is_dir(DirEntry *self, PyObject *args, PyObject *kwargs)
+static int
+os_DirEntry_is_dir_impl(DirEntry *self, int follow_symlinks)
+/*[clinic end generated code: output=ad2e8d54365da287 input=0135232766f53f58]*/
{
- int follow_symlinks = 1;
+ return DirEntry_test_mode(self, follow_symlinks, S_IFDIR);
+}
- if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|$p:DirEntry.is_dir",
- follow_symlinks_keywords, &follow_symlinks))
- return NULL;
+/*[clinic input]
+os.DirEntry.is_file -> bool
+ *
+ follow_symlinks: bool = True
- return DirEntry_py_test_mode(self, follow_symlinks, S_IFDIR);
-}
+Return True if the entry is a file; cached per entry.
+[clinic start generated code]*/
-static PyObject *
-DirEntry_is_file(DirEntry *self, PyObject *args, PyObject *kwargs)
+static int
+os_DirEntry_is_file_impl(DirEntry *self, int follow_symlinks)
+/*[clinic end generated code: output=8462ade481d8a476 input=0dc90be168b041ee]*/
{
- int follow_symlinks = 1;
+ return DirEntry_test_mode(self, follow_symlinks, S_IFREG);
+}
- if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|$p:DirEntry.is_file",
- follow_symlinks_keywords, &follow_symlinks))
- return NULL;
+/*[clinic input]
+os.DirEntry.inode
- return DirEntry_py_test_mode(self, follow_symlinks, S_IFREG);
-}
+Return inode of the entry; cached per entry.
+[clinic start generated code]*/
static PyObject *
-DirEntry_inode(DirEntry *self)
+os_DirEntry_inode_impl(DirEntry *self)
+/*[clinic end generated code: output=156bb3a72162440e input=3ee7b872ae8649f0]*/
{
#ifdef MS_WINDOWS
if (!self->got_file_index) {
@@ -11435,8 +11429,15 @@ DirEntry_repr(DirEntry *self)
return PyUnicode_FromFormat("<DirEntry %R>", self->name);
}
+/*[clinic input]
+os.DirEntry.__fspath__
+
+Returns the path for the entry.
+[clinic start generated code]*/
+
static PyObject *
-DirEntry_fspath(DirEntry *self)
+os_DirEntry___fspath___impl(DirEntry *self)
+/*[clinic end generated code: output=6dd7f7ef752e6f4f input=3c49d0cf38df4fac]*/
{
Py_INCREF(self->path);
return self->path;
@@ -11450,25 +11451,15 @@ static PyMemberDef DirEntry_members[] = {
{NULL}
};
+#include "clinic/posixmodule.c.h"
+
static PyMethodDef DirEntry_methods[] = {
- {"is_dir", (PyCFunction)DirEntry_is_dir, METH_VARARGS | METH_KEYWORDS,
- "return True if the entry is a directory; cached per entry"
- },
- {"is_file", (PyCFunction)DirEntry_is_file, METH_VARARGS | METH_KEYWORDS,
- "return True if the entry is a file; cached per entry"
- },
- {"is_symlink", (PyCFunction)DirEntry_py_is_symlink, METH_NOARGS,
- "return True if the entry is a symbolic link; cached per entry"
- },
- {"stat", (PyCFunction)DirEntry_stat, METH_VARARGS | METH_KEYWORDS,
- "return stat_result object for the entry; cached per entry"
- },
- {"inode", (PyCFunction)DirEntry_inode, METH_NOARGS,
- "return inode of the entry; cached per entry",
- },
- {"__fspath__", (PyCFunction)DirEntry_fspath, METH_NOARGS,
- "returns the path for the entry",
- },
+ OS_DIRENTRY_IS_DIR_METHODDEF
+ OS_DIRENTRY_IS_FILE_METHODDEF
+ OS_DIRENTRY_IS_SYMLINK_METHODDEF
+ OS_DIRENTRY_STAT_METHODDEF
+ OS_DIRENTRY_INODE_METHODDEF
+ OS_DIRENTRY___FSPATH___METHODDEF
{NULL}
};
@@ -11940,23 +11931,34 @@ static PyTypeObject ScandirIteratorType = {
(destructor)ScandirIterator_finalize, /* tp_finalize */
};
+/*[clinic input]
+os.scandir
+
+ path : path_t(nullable=True) = None
+
+Return an iterator of DirEntry objects for given path.
+
+path can be specified as either str, bytes or path-like object. If path
+is bytes, the names of yielded DirEntry objects will also be bytes; in
+all other circumstances they will be str.
+
+If path is None, uses the path='.'.
+[clinic start generated code]*/
+
static PyObject *
-posix_scandir(PyObject *self, PyObject *args, PyObject *kwargs)
+os_scandir_impl(PyObject *module, path_t *path)
+/*[clinic end generated code: output=6eb2668b675ca89e input=e62b08b3cd41f604]*/
{
ScandirIterator *iterator;
- static char *keywords[] = {"path", NULL};
#ifdef MS_WINDOWS
wchar_t *path_strW;
#else
- const char *path;
+ const char *path_str;
#endif
iterator = PyObject_New(ScandirIterator, &ScandirIteratorType);
if (!iterator)
return NULL;
- memset(&iterator->path, 0, sizeof(path_t));
- iterator->path.function_name = "scandir";
- iterator->path.nullable = 1;
#ifdef MS_WINDOWS
iterator->handle = INVALID_HANDLE_VALUE;
@@ -11964,9 +11966,10 @@ posix_scandir(PyObject *self, PyObject *args, PyObject *kwargs)
iterator->dirp = NULL;
#endif
- if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O&:scandir", keywords,
- path_converter, &iterator->path))
- goto error;
+ memcpy(&iterator->path, path, sizeof(path_t));
+ /* Move the ownership to iterator->path */
+ path->object = NULL;
+ path->cleanup = NULL;
#ifdef MS_WINDOWS
iterator->first_time = 1;
@@ -11987,13 +11990,13 @@ posix_scandir(PyObject *self, PyObject *args, PyObject *kwargs)
}
#else /* POSIX */
if (iterator->path.narrow)
- path = iterator->path.narrow;
+ path_str = iterator->path.narrow;
else
- path = ".";
+ path_str = ".";
errno = 0;
Py_BEGIN_ALLOW_THREADS
- iterator->dirp = opendir(path);
+ iterator->dirp = opendir(path_str);
Py_END_ALLOW_THREADS
if (!iterator->dirp) {
@@ -12038,7 +12041,7 @@ PyOS_FSPath(PyObject *path)
Py_TYPE(path)->tp_name);
}
- path_repr = PyObject_CallFunctionObjArgs(func, NULL);
+ path_repr = _PyObject_CallNoArg(func);
Py_DECREF(func);
if (NULL == path_repr) {
return NULL;
@@ -12136,13 +12139,6 @@ error:
}
#endif /* HAVE_GETRANDOM_SYSCALL */
-#include "clinic/posixmodule.c.h"
-
-/*[clinic input]
-dump buffer
-[clinic start generated code]*/
-/*[clinic end generated code: output=da39a3ee5e6b4b0d input=524ce2e021e4eba6]*/
-
static PyMethodDef posix_methods[] = {
@@ -12332,9 +12328,7 @@ static PyMethodDef posix_methods[] = {
{"get_blocking", posix_get_blocking, METH_VARARGS, get_blocking__doc__},
{"set_blocking", posix_set_blocking, METH_VARARGS, set_blocking__doc__},
#endif
- {"scandir", (PyCFunction)posix_scandir,
- METH_VARARGS | METH_KEYWORDS,
- posix_scandir__doc__},
+ OS_SCANDIR_METHODDEF
OS_FSPATH_METHODDEF
OS_GETRANDOM_METHODDEF
{NULL, NULL} /* Sentinel */