Advertisement
Guest User

Untitled

a guest
Mar 10th, 2011
564
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.72 KB | None | 0 0
  1. Index: Modules/posixmodule.c
  2. ===================================================================
  3. --- Modules/posixmodule.c   (revision 88757)
  4. +++ Modules/posixmodule.c   (working copy)
  5. @@ -2336,7 +2336,136 @@
  6.  }
  7.  #endif /* MS_WINDOWS */
  8.  
  9. +typedef struct {
  10. +  PyObject_HEAD
  11. +  DIR * dirp;
  12. +  PyObject *oname;
  13. +  int arg_is_unicode;
  14. +} xlistdir_Iter;
  15.  
  16. +static PyObject *
  17. +posix_XListDirIter_iter(PyObject *self)
  18. +{
  19. +    Py_INCREF(self);
  20. +    return self;
  21. +}
  22. +
  23. +static void posix_XListDirIter_dealloc(PyObject *obj)
  24. +{
  25. +    xlistdir_Iter *self = (xlistdir_Iter *)obj;
  26. +    if(self->dirp){
  27. +        Py_BEGIN_ALLOW_THREADS
  28. +        closedir(self->dirp);
  29. +        Py_END_ALLOW_THREADS
  30. +        self->dirp = NULL;
  31. +    }
  32. +    Py_XDECREF(self->oname);
  33. +}
  34. +
  35. +PyObject* posix_XListDirIter_iternext(PyObject *obj)
  36. +{
  37. +    PyObject *v;
  38. +    struct dirent* ep;
  39. +    xlistdir_Iter *self = (xlistdir_Iter *)obj;
  40. +    errno = 0;
  41. +    for(;;) {
  42. +        Py_BEGIN_ALLOW_THREADS
  43. +        ep = readdir(self->dirp);
  44. +        Py_END_ALLOW_THREADS
  45. +        if (ep == NULL) {
  46. +            if (errno == 0) {
  47. +                break;
  48. +            } else {
  49. +                posix_error_with_allocated_filename(self->oname);
  50. +                return NULL;
  51. +            }
  52. +        }
  53. +        if (ep->d_name[0] == '.' &&
  54. +            (NAMLEN(ep) == 1 ||
  55. +             (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
  56. +            continue;
  57. +        if (self->arg_is_unicode)
  58. +            v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
  59. +        else
  60. +            v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
  61. +        if (v == NULL) {
  62. +            break;
  63. +        }
  64. +        return v;
  65. +        }
  66. +    return NULL;
  67. +}
  68. +
  69. +static PyTypeObject posix_XListDirIterType =
  70. +{
  71. +    PyObject_HEAD_INIT(NULL)
  72. +    "posix._XListDirIter",            /*tp_name*/
  73. +    sizeof(xlistdir_Iter),       /*tp_basicsize*/
  74. +    0,                         /*tp_itemsize*/
  75. +    posix_XListDirIter_dealloc,   /*tp_dealloc*/
  76. +    0,                         /*tp_print*/
  77. +    0,                         /*tp_getattr*/
  78. +    0,                         /*tp_setattr*/
  79. +    0,                                /*tp_compare*/
  80. +    0,                         /*tp_repr*/
  81. +    0,                         /*tp_as_number*/
  82. +    0,                         /*tp_as_sequence*/
  83. +    0,                         /*tp_as_mapping*/
  84. +    0,                         /*tp_hash */
  85. +    0,                         /*tp_call*/
  86. +    0,                         /*tp_str*/
  87. +    0,                         /*tp_getattro*/
  88. +    0,                         /*tp_setattro*/
  89. +    0,                         /*tp_as_buffer*/
  90. +    Py_TPFLAGS_DEFAULT,
  91. +    "Internal posix xlistdir iterator object.",           /* tp_doc */
  92. +    0,                         /*tp_traverse*/
  93. +    0,                          /*tp_clear*/
  94. +    0,                         /*tp_richcompare*/
  95. +    0,                         /*tp_weaklistoffset*/
  96. +    posix_XListDirIter_iter,
  97. +    posix_XListDirIter_iternext
  98. +};
  99. +
  100. +static PyObject *
  101. +posix_xlistdir(PyObject *self, PyObject *args)
  102. +{
  103. +    char *name;
  104. +    PyObject *v;
  105. +    xlistdir_Iter *iter;
  106. +
  107. +    iter = PyObject_New(xlistdir_Iter, &posix_XListDirIterType);
  108. +    if (!iter) return NULL;
  109. +    /* I'm not sure if it's strictly necessary. */
  110. +      if (!PyObject_Init((PyObject *)iter, &posix_XListDirIterType)) {
  111. +        Py_DECREF(iter);
  112. +        return NULL;
  113. +      }
  114. +    iter->arg_is_unicode = 1;
  115. +    iter->oname = NULL;
  116. +
  117. +    errno = 0;
  118. +    /* v is never read, so it does not need to be initialized yet. */
  119. +    if (!PyArg_ParseTuple(args, "|U:xlistdir", &v)) {
  120. +        iter->arg_is_unicode = 0;
  121. +        PyErr_Clear();
  122. +    }
  123. +    if (!PyArg_ParseTuple(args, "|O&:xlistdir", PyUnicode_FSConverter, &iter->oname))
  124. +        return NULL;
  125. +    if (iter->oname == NULL) { /* Default arg: "." */
  126. +        iter->oname = PyBytes_FromString(".");
  127. +    }
  128. +    name = PyBytes_AsString(iter->oname);
  129. +    Py_BEGIN_ALLOW_THREADS
  130. +    iter->dirp = opendir(name);
  131. +    Py_END_ALLOW_THREADS
  132. +    if (iter->dirp == NULL) {
  133. +        return posix_error_with_allocated_filename(iter->oname);
  134. +    }
  135. +    return (PyObject *)iter;
  136. +}
  137. +
  138. +
  139.  PyDoc_STRVAR(posix_listdir__doc__,
  140.  "listdir([path]) -> list_of_strings\n\n\
  141. Return a list containing the names of the entries in the directory.\n\
  142. @@ -8684,6 +8813,7 @@
  143.     {"link",            posix_link, METH_VARARGS, posix_link__doc__},
  144. #endif /* HAVE_LINK */
  145.     {"listdir",         posix_listdir, METH_VARARGS, posix_listdir__doc__},
  146. +    {"xlistdir",   posix_xlistdir, METH_VARARGS, NULL},
  147. #ifdef HAVE_FDOPENDIR
  148.     {"fdlistdir",       posix_fdlistdir, METH_VARARGS, posix_fdlistdir__doc__},
  149. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement