* path_cleanup(). However it is safe to do so.)
*/
typedef struct {
- char *function_name;
- char *argument_name;
+ const char *function_name;
+ const char *argument_name;
int nullable;
int allow_fd;
wchar_t *wide;
{
return PyErr_SetFromErrno(PyExc_OSError);
}
-static PyObject *
-posix_error_with_filename(char* name)
-{
- return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
-}
-
-
-static PyObject *
-posix_error_with_allocated_filename(PyObject* name)
-{
- PyObject *name_str, *rc;
- name_str = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AsString(name),
- PyBytes_GET_SIZE(name));
- Py_DECREF(name);
- rc = PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError,
- name_str);
- Py_XDECREF(name_str);
- return rc;
-}
#ifdef MS_WINDOWS
static PyObject *
#endif /* MS_WINDOWS */
-/*
- * Some functions return Win32 errors, others only ever use posix_error
- * (this is for backwards compatibility with exceptions)
- */
-static PyObject *
-path_posix_error(char *function_name, path_t *path)
-{
- if (path->narrow)
- return posix_error_with_filename(path->narrow);
- return posix_error();
-}
-
static PyObject *
-path_error(char *function_name, path_t *path)
+path_error(path_t *path)
{
#ifdef MS_WINDOWS
- if (path->narrow)
- return win32_error(function_name, path->narrow);
- if (path->wide)
- return win32_error_unicode(function_name, path->wide);
- return win32_error(function_name, NULL);
+ return PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError,
+ 0, path->object);
#else
- return path_posix_error(function_name, path);
+ return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object);
#endif
}
}
static PyObject *
-posix_1str(PyObject *args, char *format, int (*func)(const char*))
+posix_1str(const char *func_name, PyObject *args, char *format,
+ int (*func)(const char*))
{
- PyObject *opath1 = NULL;
- char *path1;
+ path_t path;
int res;
+ memset(&path, 0, sizeof(path));
+ path.function_name = func_name;
if (!PyArg_ParseTuple(args, format,
- PyUnicode_FSConverter, &opath1))
+ path_converter, &path))
return NULL;
- path1 = PyBytes_AsString(opath1);
Py_BEGIN_ALLOW_THREADS
- res = (*func)(path1);
+ res = (*func)(path.narrow);
Py_END_ALLOW_THREADS
- if (res < 0)
- return posix_error_with_allocated_filename(opath1);
- Py_DECREF(opath1);
+ if (res < 0) {
+ path_error(&path);
+ path_cleanup(&path);
+ return NULL;
+ }
+ path_cleanup(&path);
Py_INCREF(Py_None);
return Py_None;
}
result = STAT(path->narrow, &st);
Py_END_ALLOW_THREADS
- if (result != 0)
- return path_error("stat", path);
+ if (result != 0) {
+ return path_error(path);
+ }
return _pystat_fromstructstat(&st);
}
PyObject *return_value;
memset(&path, 0, sizeof(path));
+ path.function_name = "stat";
path.allow_fd = 1;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&p:stat", keywords,
path_converter, &path,
PyObject *return_value;
memset(&path, 0, sizeof(path));
+ path.function_name = "lstat";
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:lstat", keywords,
path_converter, &path,
#ifdef HAVE_FSTATAT
static char *keywords[] = {"path", NULL};
memset(&path, 0, sizeof(path));
+ path.function_name = "chdir";
#ifdef HAVE_FCHDIR
path.allow_fd = 1;
#endif
Py_END_ALLOW_THREADS
if (result) {
- return_value = path_error("chdir", &path);
+ return_value = path_error(&path);
goto exit;
}
#endif
memset(&path, 0, sizeof(path));
+ path.function_name = "chmod";
#ifdef HAVE_FCHMOD
path.allow_fd = 1;
#endif
}
else
#endif
- return_value = path_error("chmod", &path);
+ return_value = path_error(&path);
goto exit;
}
#endif
static PyObject *
posix_lchmod(PyObject *self, PyObject *args)
{
- PyObject *opath;
- char *path;
+ path_t path;
int i;
int res;
- if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter,
- &opath, &i))
+ memset(&path, 0, sizeof(path));
+ path.function_name = "lchmod";
+ if (!PyArg_ParseTuple(args, "O&i:lchmod",
+ path_converter, &path, &i))
return NULL;
- path = PyBytes_AsString(opath);
Py_BEGIN_ALLOW_THREADS
- res = lchmod(path, i);
+ res = lchmod(path.narrow, i);
Py_END_ALLOW_THREADS
- if (res < 0)
- return posix_error_with_allocated_filename(opath);
- Py_DECREF(opath);
+ if (res < 0) {
+ path_error(&path);
+ path_cleanup(&path);
+ return NULL;
+ }
+ path_cleanup(&path);
Py_RETURN_NONE;
}
#endif /* HAVE_LCHMOD */
static char *keywords[] = {"path", "flags", "follow_symlinks", NULL};
memset(&path, 0, sizeof(path));
+ path.function_name = "chflags";
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&k|$i:chflags", keywords,
path_converter, &path,
&flags, &follow_symlinks))
Py_END_ALLOW_THREADS
if (result) {
- return_value = path_posix_error("chflags", &path);
+ return_value = path_error(&path);
goto exit;
}
static PyObject *
posix_lchflags(PyObject *self, PyObject *args)
{
- PyObject *opath;
+ path_t path;
char *path;
unsigned long flags;
int res;
+ memset(&path, 0, sizeof(path));
+ path.function_name = "lchflags";
if (!PyArg_ParseTuple(args, "O&k:lchflags",
- PyUnicode_FSConverter, &opath, &flags))
+ path_converter, &path, &flags))
return NULL;
- path = PyBytes_AsString(opath);
Py_BEGIN_ALLOW_THREADS
- res = lchflags(path, flags);
+ res = lchflags(path.narrow, flags);
Py_END_ALLOW_THREADS
- if (res < 0)
- return posix_error_with_allocated_filename(opath);
- Py_DECREF(opath);
- Py_INCREF(Py_None);
- return Py_None;
+ if (res < 0) {
+ path_error(&path);
+ path_cleanup(&path);
+ return NULL;
+ }
+ path_cleanup(&path);
+ Py_RETURN_NONE;
}
#endif /* HAVE_LCHFLAGS */
static PyObject *
posix_chroot(PyObject *self, PyObject *args)
{
- return posix_1str(args, "O&:chroot", chroot);
+ return posix_1str("chroot", args, "O&:chroot", chroot);
}
#endif
"follow_symlinks", NULL};
memset(&path, 0, sizeof(path));
+ path.function_name = "chown";
#ifdef HAVE_FCHOWN
path.allow_fd = 1;
#endif
Py_END_ALLOW_THREADS
if (result) {
- return_value = path_posix_error("chown", &path);
+ return_value = path_error(&path);
goto exit;
}
static PyObject *
posix_lchown(PyObject *self, PyObject *args)
{
- PyObject *opath;
- char *path;
+ path_t path;
long uid, gid;
int res;
+ memset(&path, 0, sizeof(path));
+ path.function_name = "lchown";
if (!PyArg_ParseTuple(args, "O&ll:lchown",
- PyUnicode_FSConverter, &opath,
+ path_converter, &path,
&uid, &gid))
return NULL;
- path = PyBytes_AsString(opath);
Py_BEGIN_ALLOW_THREADS
- res = lchown(path, (uid_t) uid, (gid_t) gid);
+ res = lchown(path.narrow, (uid_t) uid, (gid_t) gid);
Py_END_ALLOW_THREADS
- if (res < 0)
- return posix_error_with_allocated_filename(opath);
- Py_DECREF(opath);
+ if (res < 0) {
+ path_error(&path);
+ path_cleanup(&path);
+ return NULL;
+ }
+ path_cleanup(&path);
Py_INCREF(Py_None);
return Py_None;
}
memset(&src, 0, sizeof(src));
memset(&dst, 0, sizeof(dst));
+ src.function_name = "link";
+ dst.function_name = "link";
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|O&O&p:link", keywords,
path_converter, &src,
path_converter, &dst,
Py_END_ALLOW_THREADS
if (result) {
- return_value = path_error("link", &dst);
+ return_value = path_error(&src);
goto exit;
}
#endif
#endif
memset(&path, 0, sizeof(path));
+ path.function_name = "listdir";
path.nullable = 1;
#ifdef HAVE_FDOPENDIR
path.allow_fd = 1;
}
if (dirp == NULL) {
- list = path_error("listdir", &path);
+ list = path_error(&path);
goto exit;
}
if ((list = PyList_New(0)) == NULL) {
break;
} else {
Py_DECREF(list);
- list = path_error("listdir", &path);
+ list = path_error(&path);
goto exit;
}
}
int result;
memset(&path, 0, sizeof(path));
+ path.function_name = "mkdir";
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|i$O&:mkdir", keywords,
path_converter, &path, &mode,
#ifdef HAVE_MKDIRAT
#endif
Py_END_ALLOW_THREADS
if (result < 0) {
- return_value = path_error("mkdir", &path);
+ return_value = path_error(&path);
goto exit;
}
#endif
memset(&src, 0, sizeof(src));
memset(&dst, 0, sizeof(dst));
+ src.function_name = function_name;
+ dst.function_name = function_name;
strcpy(format, "O&O&|$O&O&:");
strcat(format, function_name);
if (!PyArg_ParseTupleAndKeywords(args, kwargs, format, keywords,
Py_END_ALLOW_THREADS
if (result) {
- return_value = path_error(function_name, &dst);
+ return_value = path_error(&src);
goto exit;
}
#endif
PyObject *return_value = NULL;
memset(&path, 0, sizeof(path));
+ path.function_name = "rmdir";
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:rmdir", keywords,
path_converter, &path,
#ifdef HAVE_UNLINKAT
Py_END_ALLOW_THREADS
if (result) {
- return_value = path_error("rmdir", &path);
+ return_value = path_error(&path);
goto exit;
}
PyObject *return_value = NULL;
memset(&path, 0, sizeof(path));
+ path.function_name = "unlink";
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:unlink", keywords,
path_converter, &path,
#ifdef HAVE_UNLINKAT
Py_END_ALLOW_THREADS
if (result) {
- return_value = path_error("unlink", &path);
+ return_value = path_error(&path);
goto exit;
}
like posix.environ. */
memset(&path, 0, sizeof(path));
+ path.function_name = "execve";
#ifdef HAVE_FEXECVE
path.allow_fd = 1;
#endif
/* If we get here it's definitely an error */
- path_posix_error("execve", &path);
+ path_error(&path);
while (--envc >= 0)
PyMem_DEL(envlist[envc]);
static char *keywords[] = {"path", "dir_fd", NULL};
memset(&path, 0, sizeof(path));
+ path.function_name = "readlink";
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:readlink", keywords,
path_converter, &path,
#ifdef HAVE_READLINKAT
Py_END_ALLOW_THREADS
if (length < 0) {
- return_value = path_posix_error("readlink", &path);
+ return_value = path_error(&path);
goto exit;
}
#endif
memset(&src, 0, sizeof(src));
+ src.function_name = "symlink";
src.argument_name = "src";
memset(&dst, 0, sizeof(dst));
+ dst.function_name = "symlink";
dst.argument_name = "dst";
#ifdef MS_WINDOWS
Py_END_ALLOW_THREADS
if (result) {
- return_value = path_error("symlink", &dst);
+ return_value = path_error(&dst);
goto exit;
}
#endif
static char *keywords[] = {"path", "flags", "mode", "dir_fd", NULL};
memset(&path, 0, sizeof(path));
+ path.function_name = "open";
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&i|i$O&:open", keywords,
path_converter, &path,
&flags, &mode,
return_value = posix_error();
else
#endif
- return_value = path_error("open", &path);
+ return_value = path_error(&path);
goto exit;
}
static char *keywords[] = {"path", "length", NULL};
memset(&path, 0, sizeof(path));
+ path.function_name = "truncate";
#ifdef HAVE_FTRUNCATE
path.allow_fd = 1;
#endif
res = truncate(path.narrow, length);
Py_END_ALLOW_THREADS
if (res < 0)
- result = path_posix_error("truncate", &path);
+ result = path_error(&path);
else {
Py_INCREF(Py_None);
result = Py_None;
struct statvfs st;
memset(&path, 0, sizeof(path));
+ path.function_name = "statvfs";
#ifdef HAVE_FSTATVFS
path.allow_fd = 1;
#endif
Py_END_ALLOW_THREADS
if (result) {
- return_value = path_posix_error("statvfs", &path);
+ return_value = path_error(&path);
goto exit;
}
static char *keywords[] = {"path", "name", NULL};
memset(&path, 0, sizeof(path));
+ path.function_name = "pathconf";
#ifdef HAVE_FPATHCONF
path.allow_fd = 1;
#endif
/* could be a path or name problem */
posix_error();
else
- result = path_posix_error("pathconf", &path);
+ result = path_error(&path);
}
else
result = PyLong_FromLong(limit);
memset(&path, 0, sizeof(path));
memset(&attribute, 0, sizeof(attribute));
+ path.function_name = "getxattr";
+ attribute.function_name = "getxattr";
path.allow_fd = 1;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|$p:getxattr", keywords,
path_converter, &path,
static Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0};
Py_ssize_t buffer_size = buffer_sizes[i];
if (!buffer_size) {
- path_error("getxattr", &path);
+ path_error(&path);
goto exit;
}
buffer = PyBytes_FromStringAndSize(NULL, buffer_size);
buffer = NULL;
if (errno == ERANGE)
continue;
- path_error("getxattr", &path);
+ path_error(&path);
goto exit;
}
"flags", "follow_symlinks", NULL};
memset(&path, 0, sizeof(path));
+ path.function_name = "setxattr";
path.allow_fd = 1;
memset(&attribute, 0, sizeof(attribute));
memset(&value, 0, sizeof(value));
Py_END_ALLOW_THREADS;
if (result) {
- return_value = path_error("setxattr", &path);
+ return_value = path_error(&path);
goto exit;
}
static char *keywords[] = {"path", "attribute", "follow_symlinks", NULL};
memset(&path, 0, sizeof(path));
+ path.function_name = "removexattr";
memset(&attribute, 0, sizeof(attribute));
+ attribute.function_name = "removexattr";
path.allow_fd = 1;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|$p:removexattr",
keywords,
Py_END_ALLOW_THREADS;
if (result) {
- return_value = path_error("removexattr", &path);
+ return_value = path_error(&path);
goto exit;
}
static char *keywords[] = {"path", "follow_symlinks", NULL};
memset(&path, 0, sizeof(path));
+ path.function_name = "listxattr";
path.allow_fd = 1;
path.fd = -1;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O&$p:listxattr", keywords,
Py_ssize_t buffer_size = buffer_sizes[i];
if (!buffer_size) {
/* ERANGE */
- path_error("listxattr", &path);
+ path_error(&path);
break;
}
buffer = PyMem_MALLOC(buffer_size);
if (length < 0) {
if (errno == ERANGE)
continue;
- path_error("listxattr", &path);
+ path_error(&path);
break;
}