[Patches] [ python-Patches-462296 ] Add attributes to os.stat results

noreply@sourceforge.net noreply@sourceforge.net
Fri, 21 Sep 2001 11:01:08 -0700


Patches item #462296, was opened at 2001-09-17 10:57
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=305470&aid=462296&group_id=5470

Category: Library (Lib)
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Nick Mathewson (nickm)
Assigned to: Nobody/Anonymous (nobody)
Summary: Add attributes to os.stat results

Initial Comment:
See bug #111481, and PEP 0042.  Both suggest that the
return values for os.{stat,lstat,statvfs,fstatvfs}
ought to be struct-like objects rather than simple tuples. 

With this patch, the os module will modify the
aformentioned functions so that their results still
obey the previous tuple protocol, but now have
read-only attributes as well.  In other words,
"os.stat('filename')[0]" is now synonymous with
"os.stat('filename').st_mode.

The patch also modifies test_os.py to test the new
behavior.

In order to prevent old code from breaking, these new
return types extend tuple.  They also use the new
attribute descriptor interface. (Thanks for
PEP-025[23], Guido!)

Backward compatibility:  Code will only break if it
assumes that type(os.stat(...)) == TupleType, or if it
assumes that os.stat(...) has no attributes beyond
those defined in tuple.

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

>Comment By: Nick Mathewson (nickm)
Date: 2001-09-21 11:01

Message:
Logged In: YES 
user_id=499

Well, here's a posixmodule-only, all-C version.  If this
seems like a good approach, I'll add some better docstrings,
move it into whichever module you like, and make
riscosmodule.c and macmodule.c use it too.


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

Comment By: Guido van Rossum (gvanrossum)
Date: 2001-09-19 21:35

Message:
Logged In: YES 
user_id=6380

Or you could put it in modsupport.c, which is already a
grab-bag of handy stuff.

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

Comment By: Martin v. Löwis (loewis)
Date: 2001-09-19 11:36

Message:
Logged In: YES 
user_id=21627

There aren't actually so many copies of the module, since 
posixmodule implements "posix","nt", and "os2". I found 
alternative implementations in riscosmodule and macmodule.

Still, putting the support type into a shared C file is 
appropriate. I can think of two candidate places: 
tupleobject.c and fileobject.c. It may be actually 
worthwhile attempting to share the stat() implementations 
as well, but that could be an add-on.


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

Comment By: Nick Mathewson (nickm)
Date: 2001-09-19 11:10

Message:
Logged In: YES 
user_id=499

I'm becoming more and more convinced that doing it in C is
the right thing, but I have issue with doing it in the posix
module.  The stat function is provided on (nearly?) all
platforms, and doing it in C will require minor changes to
all of these modules.  We can probably live with this, but I
don't think we should duplicate code between all of the os
modules.  

Is there some other appropriate place to put it in C?

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

Comment By: Martin v. Löwis (loewis)
Date: 2001-09-18 23:52

Message:
Logged In: YES 
user_id=21627

Using posix.stat is common, see

http://groups.yahoo.com/group/python-list/message/4349
http://www.washington.edu/computing/training/125/mkdoc.html
http://groups.google.com/groups?th=7d7d118fed161e0&seekm=5qdjch%24dci%40nntp6.u.washington.edu

for examples. None of these would break with your change,
though, since they don't rely on the lenght of the tuple.

If you are going to implement the type in C, I'd put it in
the posix module. If you are going to implement it in Python
(and only use it from the Posix module), making it
general-purpose may be desirable. However, a number of
things would need to be considered, so a PEP might be
appropriate. If that is done, I'd propose an interface like

tuple_with_attrs((value-tuple), (tuple-of-field-names),
exposed-length-of-tuple))

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

Comment By: Nick Mathewson (nickm)
Date: 2001-09-18 14:11

Message:
Logged In: YES 
user_id=499

Ah!  Now I see.  I hadn't realized that anybody used the
posix module directly.  (People really do this?)

I'll try to write up a patch in C tonight or tomorrow
morning.  A couple of questions on which I could use advice: 
(1) Where is the proper place to put this kind of
tuple-with-fields hybrid? Modules? Objects?  In a new file
or an existing one?
(2) Should I try to make it general enough for non-stat use?

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

Comment By: Martin v. Löwis (loewis)
Date: 2001-09-18 00:54

Message:
Logged In: YES 
user_id=21627

The problem with your second and third patch is that it
includes an incompatibility for users of posix.stat (and
friends), since it changes the siye of the tuple. If you
want to continue to return a tuple (as the top-level data
structure), you'll break compatibility for applications
using the C module directly. An example of code that would
be broken is

mode, ino, dev, nlink, uid, gid, size, a, c, m =
posix.stat(filename)

To pass the additional fields, you already need your class
_StatResult available in C.
You may find a way to define it in Python and use it in C,
but that has proven to be very fragile in the past.

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

Comment By: Guido van Rossum (gvanrossum)
Date: 2001-09-17 18:54

Message:
Logged In: YES 
user_id=6380

Haven't had time to review the patch yet, but the idea of
providing a structure with fields that doubles as a tuple is
a good one. It's been tried before and can be done in pure
Python as well.

Regarding the field names: I think the field names should
keep their st_ prefix -- IMO this makes the code more
recognizable and hence readable.

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

Comment By: Nick Mathewson (nickm)
Date: 2001-09-17 17:32

Message:
Logged In: YES 
user_id=499

Here's the revised (*example only*) patch that takes the
more portable approach I mention below.

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

Comment By: Nick Mathewson (nickm)
Date: 2001-09-17 16:10

Message:
Logged In: YES 
user_id=499

On further consideration, the approach taken in the second
(*example only*) patch is indeed too fragile.  The C code
should not lengthen the tuple arbitrarily and depend on the
Python code to decode it; instead, it should return a
dictionary of extra fields.  I think that this approach uses
a minimum of C, is easily maintainable, and very extensible.

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

Comment By: Nick Mathewson (nickm)
Date: 2001-09-17 15:53

Message:
Logged In: YES 
user_id=499

Martin: I'm not entirely sure what you mean here; while my
patch for extra fields requires a minor chunk of C (to
access the struct fields), the rest still works in pure
python.  I'm attaching this second version for reference.

I'm not sure it makes much sense to do this with pure C; it
would certainly take a lot more code, with little benefit I
can descern.  But you're more experienced than I; what am I
missing?

I agree that the field naming is suboptimal; I was taking my
lead from the stat and statvfs modules.  If people prefer,
we can name the fields whatever we like.

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

Comment By: Martin v. Löwis (loewis)
Date: 2001-09-17 15:24

Message:
Logged In: YES 
user_id=21627

I second the request for supporting additional fields 
where available. At the same time, it appears 
unimplementable using pure Python.

Consequently, I'd like to see this patch redone in C. The 
implementation strategy could probably remain the same, 
i.e. inherit from tuple for best compatibility; add the 
remaining fields as slots. It may be reasonable to 
implement attribute access using a custom getattr 
function, though. 

I have also my doubts about the naming of the fields. The 
st_ prefix originates from the time where struct fields 
were living in the global namespace (i.e. across different 
structures), so prefixing them for uniqueness was 
essential. I'm not sure whether we should inherit this 
into Python...


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

Comment By: Nick Mathewson (nickm)
Date: 2001-09-17 13:58

Message:
Logged In: YES 
user_id=499

BTW, if this gets in, I have another patch that adds support
for st_blksize, st_blocks, and st_rdev on platforms that
support them.  It don't expose these new fields in the
tuple, as that would break all the old code that tries to
unpack all the fields of the tuple.  Instead, these fields
are only accessible as attributes. 

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

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=305470&aid=462296&group_id=5470