Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Index: Modules/posixmodule.c
- ===================================================================
- --- Modules/posixmodule.c (revision 88757)
- +++ Modules/posixmodule.c (working copy)
- @@ -2336,7 +2336,136 @@
- }
- #endif /* MS_WINDOWS */
- +typedef struct {
- + PyObject_HEAD
- + DIR * dirp;
- + PyObject *oname;
- + int arg_is_unicode;
- +} xlistdir_Iter;
- +static PyObject *
- +posix_XListDirIter_iter(PyObject *self)
- +{
- + Py_INCREF(self);
- + return self;
- +}
- +
- +static void posix_XListDirIter_dealloc(PyObject *obj)
- +{
- + xlistdir_Iter *self = (xlistdir_Iter *)obj;
- + if(self->dirp){
- + Py_BEGIN_ALLOW_THREADS
- + closedir(self->dirp);
- + Py_END_ALLOW_THREADS
- + self->dirp = NULL;
- + }
- + Py_XDECREF(self->oname);
- +}
- +
- +PyObject* posix_XListDirIter_iternext(PyObject *obj)
- +{
- + PyObject *v;
- + struct dirent* ep;
- + xlistdir_Iter *self = (xlistdir_Iter *)obj;
- + errno = 0;
- + for(;;) {
- + Py_BEGIN_ALLOW_THREADS
- + ep = readdir(self->dirp);
- + Py_END_ALLOW_THREADS
- + if (ep == NULL) {
- + if (errno == 0) {
- + break;
- + } else {
- + posix_error_with_allocated_filename(self->oname);
- + return NULL;
- + }
- + }
- + if (ep->d_name[0] == '.' &&
- + (NAMLEN(ep) == 1 ||
- + (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
- + continue;
- + if (self->arg_is_unicode)
- + v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
- + else
- + v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
- + if (v == NULL) {
- + break;
- + }
- + return v;
- + }
- + return NULL;
- +}
- +
- +static PyTypeObject posix_XListDirIterType =
- +{
- + PyObject_HEAD_INIT(NULL)
- + "posix._XListDirIter", /*tp_name*/
- + sizeof(xlistdir_Iter), /*tp_basicsize*/
- + 0, /*tp_itemsize*/
- + posix_XListDirIter_dealloc, /*tp_dealloc*/
- + 0, /*tp_print*/
- + 0, /*tp_getattr*/
- + 0, /*tp_setattr*/
- + 0, /*tp_compare*/
- + 0, /*tp_repr*/
- + 0, /*tp_as_number*/
- + 0, /*tp_as_sequence*/
- + 0, /*tp_as_mapping*/
- + 0, /*tp_hash */
- + 0, /*tp_call*/
- + 0, /*tp_str*/
- + 0, /*tp_getattro*/
- + 0, /*tp_setattro*/
- + 0, /*tp_as_buffer*/
- + Py_TPFLAGS_DEFAULT,
- + "Internal posix xlistdir iterator object.", /* tp_doc */
- + 0, /*tp_traverse*/
- + 0, /*tp_clear*/
- + 0, /*tp_richcompare*/
- + 0, /*tp_weaklistoffset*/
- + posix_XListDirIter_iter,
- + posix_XListDirIter_iternext
- +};
- +
- +static PyObject *
- +posix_xlistdir(PyObject *self, PyObject *args)
- +{
- + char *name;
- + PyObject *v;
- + xlistdir_Iter *iter;
- +
- + iter = PyObject_New(xlistdir_Iter, &posix_XListDirIterType);
- + if (!iter) return NULL;
- + /* I'm not sure if it's strictly necessary. */
- + if (!PyObject_Init((PyObject *)iter, &posix_XListDirIterType)) {
- + Py_DECREF(iter);
- + return NULL;
- + }
- + iter->arg_is_unicode = 1;
- + iter->oname = NULL;
- +
- + errno = 0;
- + /* v is never read, so it does not need to be initialized yet. */
- + if (!PyArg_ParseTuple(args, "|U:xlistdir", &v)) {
- + iter->arg_is_unicode = 0;
- + PyErr_Clear();
- + }
- + if (!PyArg_ParseTuple(args, "|O&:xlistdir", PyUnicode_FSConverter, &iter->oname))
- + return NULL;
- + if (iter->oname == NULL) { /* Default arg: "." */
- + iter->oname = PyBytes_FromString(".");
- + }
- + name = PyBytes_AsString(iter->oname);
- + Py_BEGIN_ALLOW_THREADS
- + iter->dirp = opendir(name);
- + Py_END_ALLOW_THREADS
- + if (iter->dirp == NULL) {
- + return posix_error_with_allocated_filename(iter->oname);
- + }
- + return (PyObject *)iter;
- +}
- +
- +
- PyDoc_STRVAR(posix_listdir__doc__,
- "listdir([path]) -> list_of_strings\n\n\
- Return a list containing the names of the entries in the directory.\n\
- @@ -8684,6 +8813,7 @@
- {"link", posix_link, METH_VARARGS, posix_link__doc__},
- #endif /* HAVE_LINK */
- {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
- + {"xlistdir", posix_xlistdir, METH_VARARGS, NULL},
- #ifdef HAVE_FDOPENDIR
- {"fdlistdir", posix_fdlistdir, METH_VARARGS, posix_fdlistdir__doc__},
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement