[ python-Bugs-1467952 ] os.listdir on Linux returns empty list on some errors

SourceForge.net noreply at sourceforge.net
Tue Apr 11 08:51:43 CEST 2006


Bugs item #1467952, was opened at 2006-04-10 20:12
Message generated for change (Comment added) made by gbrandl
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1467952&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Python Library
Group: Platform-specific
>Status: Closed
>Resolution: Fixed
Priority: 5
Submitted By: Gary Stiehr (gstiehr)
Assigned to: Nobody/Anonymous (nobody)
Summary: os.listdir on Linux returns empty list on some errors

Initial Comment:
os.listdir() (defined in the posix_listdir() function
on line 1449 of Modules/posixmodule.c in the
Python-2.4.3 source distribution) does not check for an
error condition when it calls the readdir() system call
on line 1665 of posixmodule.c.  

According to the readdir(3) man page included the
Scientific Linux 4.2 (based off of Red Hat Enterprise
Linux 4 sources):

The  readdir()  function  returns  a pointer to a
dirent structure, or NULL if an error occurs or
end-of-file is reached.


It seems that the posix_listdir() function assumes that
NULL can only mean end-of-file.  Therefore, in the
situation where readdir() returns NULL due to some
error, such as an I/O Error, posix_listdir() ends the
while loop started at line 1665 and goes to the
closedir() call at line 1702.  This results in an
os.listdir() call returning an empty list (as if the
directory had no contents) instead of raising an exception.

This error was noticed because we performed an ls in a
particular directory and it returned with an I/O error.
 I was then writing a python script to monitor for this
condition when I found that the os.listdir() in this
directory returned an empty list instead of an I/O error.

This behavior was noticed using Python 2.3.4; I looked
at the latest (as of 2006-04-07) stable release source
(Python 2.4.3) to investigate and to provide the
details in this bug report.  I also took a quick look
at the most recent 2.5.x code and although the
posix_listdir function has changed, it still appears as
if it would return an empty list if the readdir() call
returns NULL.

----------------------------------------------------------------------

>Comment By: Georg Brandl (gbrandl)
Date: 2006-04-11 06:51

Message:
Logged In: YES 
user_id=849994

Thank you for the report, fixed in rev. 45259, 45260.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1467952&group_id=5470


More information about the Python-bugs-list mailing list