Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2f3ca6e

Browse files
committed
Completely get rid of __dynamic__ and the corresponding
Py_TPFLAGS_DYNAMICTYPE bit. There is no longer a performance benefit, and I don't really see the use case any more.
1 parent f118cb1 commit 2f3ca6e

3 files changed

Lines changed: 42 additions & 157 deletions

File tree

Include/object.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -432,9 +432,6 @@ given type object has a specified feature.
432432
/* Set if the type allows subclassing */
433433
#define Py_TPFLAGS_BASETYPE (1L<<10)
434434

435-
/* Set if the type's __dict__ may change */
436-
#define Py_TPFLAGS_DYNAMICTYPE (1L<<11)
437-
438435
/* Set if the type is 'ready' -- fully initialized */
439436
#define Py_TPFLAGS_READY (1L<<12)
440437

Lib/test/test_descr.py

Lines changed: 21 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ def testunop(a, res, expr="len(a)", meth="__len__"):
1717
vereq(eval(expr, dict), res)
1818
t = type(a)
1919
m = getattr(t, meth)
20+
while meth not in t.__dict__:
21+
t = t.__bases__[0]
2022
vereq(m, t.__dict__[meth])
2123
vereq(m(a), res)
2224
bm = getattr(a, meth)
@@ -28,6 +30,8 @@ def testbinop(a, b, res, expr="a+b", meth="__add__"):
2830
vereq(eval(expr, dict), res)
2931
t = type(a)
3032
m = getattr(t, meth)
33+
while meth not in t.__dict__:
34+
t = t.__bases__[0]
3135
vereq(m, t.__dict__[meth])
3236
vereq(m(a, b), res)
3337
bm = getattr(a, meth)
@@ -39,6 +43,8 @@ def testternop(a, b, c, res, expr="a[b:c]", meth="__getslice__"):
3943
vereq(eval(expr, dict), res)
4044
t = type(a)
4145
m = getattr(t, meth)
46+
while meth not in t.__dict__:
47+
t = t.__bases__[0]
4248
vereq(m, t.__dict__[meth])
4349
vereq(m(a, b, c), res)
4450
bm = getattr(a, meth)
@@ -51,6 +57,8 @@ def testsetop(a, b, res, stmt="a+=b", meth="__iadd__"):
5157
vereq(dict['a'], res)
5258
t = type(a)
5359
m = getattr(t, meth)
60+
while meth not in t.__dict__:
61+
t = t.__bases__[0]
5462
vereq(m, t.__dict__[meth])
5563
dict['a'] = deepcopy(a)
5664
m(dict['a'], b)
@@ -67,6 +75,8 @@ def testset2op(a, b, c, res, stmt="a[b]=c", meth="__setitem__"):
6775
vereq(dict['a'], res)
6876
t = type(a)
6977
m = getattr(t, meth)
78+
while meth not in t.__dict__:
79+
t = t.__bases__[0]
7080
vereq(m, t.__dict__[meth])
7181
dict['a'] = deepcopy(a)
7282
m(dict['a'], b, c)
@@ -82,6 +92,8 @@ def testset3op(a, b, c, d, res, stmt="a[b:c]=d", meth="__setslice__"):
8292
exec stmt in dict
8393
vereq(dict['a'], res)
8494
t = type(a)
95+
while meth not in t.__dict__:
96+
t = t.__bases__[0]
8597
m = getattr(t, meth)
8698
vereq(m, t.__dict__[meth])
8799
dict['a'] = deepcopy(a)
@@ -104,23 +116,19 @@ class Classic2:
104116

105117
class NewStatic(object):
106118
"Another docstring."
107-
__dynamic__ = 0
108119
vereq(NewStatic.__doc__, "Another docstring.")
109120
vereq(NewStatic.__dict__['__doc__'], "Another docstring.")
110121

111122
class NewStatic2(object):
112-
__dynamic__ = 0
113123
pass
114124
verify(NewStatic2.__doc__ is None)
115125

116126
class NewDynamic(object):
117127
"Another docstring."
118-
__dynamic__ = 1
119128
vereq(NewDynamic.__doc__, "Another docstring.")
120129
vereq(NewDynamic.__dict__['__doc__'], "Another docstring.")
121130

122131
class NewDynamic2(object):
123-
__dynamic__ = 1
124132
pass
125133
verify(NewDynamic2.__doc__ is None)
126134

@@ -628,7 +636,6 @@ class autosuper(type):
628636
# Automatically add __super to the class
629637
# This trick only works for dynamic classes
630638
def __new__(metaclass, name, bases, dict):
631-
assert dict.get("__dynamic__", 1)
632639
cls = super(autosuper, metaclass).__new__(metaclass,
633640
name, bases, dict)
634641
# Name mangling for __super removes leading underscores
@@ -863,54 +870,21 @@ class C3(object):
863870
vereq(x.c, 3)
864871

865872
def dynamics():
866-
if verbose: print "Testing __dynamic__..."
867-
vereq(object.__dynamic__, 0)
868-
vereq(list.__dynamic__, 0)
869-
class S1:
870-
__metaclass__ = type
871-
__dynamic__ = 0
872-
vereq(S1.__dynamic__, 0)
873-
class S(object):
874-
__dynamic__ = 0
875-
vereq(S.__dynamic__, 0)
873+
if verbose: print "Testing class attribute propagation..."
876874
class D(object):
877-
__dynamic__ = 1
878-
vereq(D.__dynamic__, 1)
879-
class E(D, S):
880875
pass
881-
vereq(E.__dynamic__, 1)
882-
class F(S, D):
876+
class E(D):
883877
pass
884-
vereq(F.__dynamic__, 1)
885-
try:
886-
S.foo = 1
887-
except (AttributeError, TypeError):
878+
class F(D):
888879
pass
889-
else:
890-
verify(0, "assignment to a static class attribute should be illegal")
891880
D.foo = 1
892881
vereq(D.foo, 1)
893882
# Test that dynamic attributes are inherited
894883
vereq(E.foo, 1)
895884
vereq(F.foo, 1)
896-
class SS(D):
897-
__dynamic__ = 0
898-
vereq(SS.__dynamic__, 0)
899-
vereq(SS.foo, 1)
900-
try:
901-
SS.foo = 1
902-
except (AttributeError, TypeError):
903-
pass
904-
else:
905-
verify(0, "assignment to SS.foo should be illegal")
906885
# Test dynamic instances
907886
class C(object):
908-
__dynamic__ = 1
909-
# XXX Ideally the following def shouldn't be necessary,
910-
# but it's too much of a performance burden.
911-
# See XXX comment in slot_tp_getattr_hook.
912-
def __getattr__(self, name):
913-
raise AttributeError, name
887+
pass
914888
a = C()
915889
verify(not hasattr(a, "foobar"))
916890
C.foobar = 2
@@ -951,7 +925,7 @@ class D(C):
951925

952926
# Test handling of int*seq and seq*int
953927
class I(int):
954-
__dynamic__ = 1 # XXX why?
928+
pass
955929
vereq("a"*I(2), "aa")
956930
vereq(I(2)*"a", "aa")
957931
vereq(2*I(3), 6)
@@ -960,7 +934,7 @@ class I(int):
960934

961935
# Test handling of long*seq and seq*long
962936
class L(long):
963-
__dynamic__ = 1 # XXX why?
937+
pass
964938
vereq("a"*L(2L), "aa")
965939
vereq(L(2L)*"a", "aa")
966940
vereq(2*L(3), 6)
@@ -969,7 +943,7 @@ class L(long):
969943

970944
# Test comparison of classes with dynamic metaclasses
971945
class dynamicmetaclass(type):
972-
__dynamic__ = 1 # XXX ???
946+
pass
973947
class someclass:
974948
__metaclass__ = dynamicmetaclass
975949
verify(someclass != object)
@@ -1255,7 +1229,6 @@ def __getitem__(self, i):
12551229
verify(10 not in c1)
12561230
# Test the default behavior for dynamic classes
12571231
class D(object):
1258-
__dynamic__ = 1 # XXX why?
12591232
def __getitem__(self, i):
12601233
if 0 <= i < 10: return i
12611234
raise IndexError
@@ -1318,7 +1291,6 @@ def __contains__(self, value):
13181291
verify(10 not in p10)
13191292
# Test overridden behavior for dynamic classes
13201293
class DProxy(object):
1321-
__dynamic__ = 1
13221294
def __init__(self, x):
13231295
self.x = x
13241296
def __nonzero__(self):
@@ -1469,7 +1441,6 @@ def meth(self, a):
14691441
vereq(B().meth(2), "B(2)A(2)")
14701442

14711443
class C(A):
1472-
__dynamic__ = 1
14731444
def meth(self, a):
14741445
return "C(%r)" % a + self.__super.meth(a)
14751446
C._C__super = super(C)
@@ -1565,7 +1536,6 @@ def __repr__(self):
15651536
verify((+a).__class__ is float)
15661537

15671538
class madcomplex(complex):
1568-
__dynamic__ = 0 # XXX Shouldn't be necessary
15691539
def __repr__(self):
15701540
return "%.17gj%+.17g" % (self.imag, self.real)
15711541
a = madcomplex(-3, 4)
@@ -1967,12 +1937,11 @@ def rich_comparisons():
19671937
if verbose:
19681938
print "Testing rich comparisons..."
19691939
class Z(complex):
1970-
__dynamic__ = 0
1940+
pass
19711941
z = Z(1)
19721942
vereq(z, 1+0j)
19731943
vereq(1+0j, z)
19741944
class ZZ(complex):
1975-
__dynamic__ = 0
19761945
def __eq__(self, other):
19771946
try:
19781947
return abs(self - other) <= 1e-6
@@ -2059,8 +2028,7 @@ class F(float): pass
20592028
coerce(0, F(0))
20602029
coerce(0L, F(0))
20612030
coerce(0., F(0))
2062-
class C(complex):
2063-
__dynamic__ = 0
2031+
class C(complex): pass
20642032
coerce(C(0), 0)
20652033
coerce(C(0), 0L)
20662034
coerce(C(0), 0.)

0 commit comments

Comments
 (0)