[Patches] [ python-Patches-635933 ] make some type attrs writable

noreply@sourceforge.net noreply@sourceforge.net
Thu, 14 Nov 2002 09:28:20 -0800

Patches item #635933, was opened at 2002-11-09 09:59
You can respond by visiting: 

Category: Core (C code)
Group: Python 2.3
Status: Open
Resolution: None
Priority: 5
Submitted By: Michael Hudson (mwh)
>Assigned to: Michael Hudson (mwh)
Summary: make some type attrs writable

Initial Comment:
As per discussion on python-dev, this patch makes the
following attributes of type objects writable from Python:

 - __name__
 - __bases__
 - __mro__

It also relaxes the restriction on not returning
__module__ when that's been set to a non-string. This
(tiny) part is a 2.2.3 candidate IMHO.

It lets the following work:

class C(object):

class D(C):

class E(object):
    def meth(self):
        print 1

d = D()
D.__bases__ = (C, E)

but that's the extent of my testing so far.  Needs a
test and docs -- if the current behaviour is documented

Currently, if an assignment to __bases__ would change
__base__, it complains (was easiest).

Assigned to Guido so he sees it, but anyone else is
encouraged to review it!


>Comment By: Guido van Rossum (gvanrossum)
Date: 2002-11-14 12:28

Logged In: YES 

Michael, can you checking some of this in as separate pieces?

The __module__ relaxation should go in first, and marked as
backport candidate.

The __name__ fix is close, but I think it *should* be
allowed to put dots in the name (this is actually a feature
for old classes); instead of '.' I want a check that there
are no \0 bytes in the string (see set_name() in classobject.c).

I think the restrictions on __bases__ are sufficiently
thought out; with old-style classes, you can do much more
class switching:

>>> class C: pass
>>> class D: pass
>>> D.__bases__ = (C,)

I'd like this to work for new-style classes too. It means
that __base__ has to change though.

There's a bug in set_mro(): it checks PyInstance_Check()
where it clearly means PyClass_Check(). Other than that I
think it's good to go. (Though this is the ultimate weird
feature! What's the use case again?)

Hoping for unit tests,


Comment By: Michael Hudson (mwh)
Date: 2002-11-09 10:01

Logged In: YES 

Hmm, I misunderstood __base__.  It's the base class that
*leads* to the solid base, not the solid base.  So an
assignment to __bases__ may justifyable change it.  Oops.

Will try again later...


You can respond by visiting: