[Python-bugs-list] [ python-Bugs-446049 ] file type is callable but crashes

noreply@sourceforge.net noreply@sourceforge.net
Tue, 07 Aug 2001 09:55:48 -0700


Bugs item #446049, was opened at 2001-07-30 11:51
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=446049&group_id=5470

Category: type/class unification
Group: Python 2.2
>Status: Closed
>Resolution: Fixed
Priority: 5
Submitted By: Gregory H. Ball (greg_ball)
Assigned to: Guido van Rossum (gvanrossum)
Summary: file type is callable but crashes

Initial Comment:
Calling the type of a file object results
in a crash.  More details to follow.


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

>Comment By: Guido van Rossum (gvanrossum)
Date: 2001-08-07 09:55

Message:
Logged In: YES 
user_id=6380

All fixed in CVS.

typeobject.c:2.22 fixes the object-instantiability (it was
harder than I thought).

fileobject.c:2.116 fixes the 'closed' attribute.

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

Comment By: Guido van Rossum (gvanrossum)
Date: 2001-08-04 10:50

Message:
Logged In: YES 
user_id=6380

OK, I think I'll remove the object-instantiability then.

I'll also look at why file.closed is always 2 -- probably
because I misdefined the attribute descriptor and there's no
test suite for this.

And yes, this is how dir() is going to work (I think -- it
makes sense since this is how it works for classes too).

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

Comment By: Gregory H. Ball (greg_ball)
Date: 2001-08-04 09:01

Message:
Logged In: YES 
user_id=11365

I played around a bit with the featureless objects, and they
are cute but hardly necessary, surely?  How would they be
documented?
(I see that they can be dictionary keys, which vaguely
suggests some kind
of application as tokens...?)

I like that you can inherit from object in python though. 
Do you have to be able to instantiate object to be able to
instantiate python-derived subclasses of object in the
current design?  If not I could certainly lose the
featureless objects.  It does seem like a reasonable goal to
avoid making objects which violate their invariants.

BTW, the file object is now (CVS) broken in respect of its 
closed attribute, which seems to be always 2, file open or
closed.
(This is on linux2.2)

Also,  is the fact that dir() turns up nothing on file
objects (which have four attributes) and on instances of
classes inheriting from object (and using __slots__ ) a bug
or just the way dir() is going to work now?

Cheers.


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

Comment By: Guido van Rossum (gvanrossum)
Date: 2001-08-02 08:02

Message:
Logged In: YES 
user_id=6380

The direct cause is that the tp_new slot inherited from
'object' creates a file object that is initialized to all
zeros, and the file object's tp_repr slot doesn't anticipate
that.

I can fix the file object in a number of ways (make it work
for uninitialized objects, or add a tp_new slot that
initializes it properly). But I expect that file is not the
only type that is vulnerable to this kind of attack,
especially 3rd party types that adopt
PyObject_GenericGetAttr() but don't define their own tp_new
are vulnerable.

The alternative would be to remove the default tp_new
implementation from 'object', and add one to dynamically
created types. That would mean you couldn't call object() to
get a featureless object, which is currently supported and
even tested in test_descr.py.

Any preferences?

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

Comment By: Gregory H. Ball (greg_ball)
Date: 2001-07-30 11:57

Message:
Logged In: YES 
user_id=11365

Python 2.2a1 (#2, Jul 26 2001, 11:50:01) 
[GCC 2.95.3 19991030 (prerelease)] on linux2
Type "copyright", "credits" or "license" for more
information.
>>> f = open('README')
>>> type(f)()
Segmentation fault (core dumped)

Seems to me that it would be nice if FileType
worked like the 'open' builtin.
You could have a 'file' builtin and alias it to open.
Wild speculation: I guess open could be eventually
deprecated.  At which point you could safely do 
from os import *  ;-)


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

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