Kaydet (Commit) d3077402 authored tarafından Guido van Rossum's avatar Guido van Rossum

- Expand test for dynamic objects.

- Remove various 'global' directives and move some global definitions
  inside the test functions that use them -- we have nested scopes so
  the old hacks using globals are no longer needed.
üst 8e24818c
...@@ -365,7 +365,6 @@ def pylists(): ...@@ -365,7 +365,6 @@ def pylists():
def metaclass(): def metaclass():
if verbose: print "Testing __metaclass__..." if verbose: print "Testing __metaclass__..."
global C
class C: class C:
__metaclass__ = type __metaclass__ = type
def __init__(self): def __init__(self):
...@@ -383,13 +382,11 @@ def metaclass(): ...@@ -383,13 +382,11 @@ def metaclass():
def myself(cls): return cls def myself(cls): return cls
verify(D.myself() == D) verify(D.myself() == D)
import sys
MT = type(sys)
def pymods(): def pymods():
if verbose: print "Testing Python subclass of module..." if verbose: print "Testing Python subclass of module..."
global log
log = [] log = []
import sys
MT = type(sys)
class MM(MT): class MM(MT):
def __init__(self): def __init__(self):
MT.__init__(self) MT.__init__(self)
...@@ -415,7 +412,6 @@ def pymods(): ...@@ -415,7 +412,6 @@ def pymods():
def multi(): def multi():
if verbose: print "Testing multiple inheritance..." if verbose: print "Testing multiple inheritance..."
global C
class C(object): class C(object):
def __init__(self): def __init__(self):
self.__state = 0 self.__state = 0
...@@ -584,22 +580,39 @@ def dynamics(): ...@@ -584,22 +580,39 @@ def dynamics():
# Test dynamic instances # Test dynamic instances
class C(object): class C(object):
__dynamic__ = 1 __dynamic__ = 1
foobar = 1
def __repr__(self):
return "<C object>"
a = C() a = C()
verify(not hasattr(a, "spam")) verify(not hasattr(a, "foobar"))
verify(a.foobar == 1)
C.foobar = 2 C.foobar = 2
verify(a.foobar == 2) verify(a.foobar == 2)
C.method = lambda self: 42 C.method = lambda self: 42
verify(a.method() == 42) verify(a.method() == 42)
verify(repr(a) == "<C object>")
C.__repr__ = lambda self: "C()" C.__repr__ = lambda self: "C()"
verify(repr(a) == "C()") verify(repr(a) == "C()")
# The following test should succeed, but doesn't yet C.__int__ = lambda self: 100
## C.__int__ = lambda self: 100 verify(int(a) == 100)
## verify(int(a) == 100) verify(a.foobar == 2)
verify(not hasattr(a, "spam"))
def mygetattr(self, name):
if name == "spam":
return "spam"
else:
return object.__getattr__(self, name)
C.__getattr__ = mygetattr
verify(a.spam == "spam")
a.new = 12
verify(a.new == 12)
def mysetattr(self, name, value):
if name == "spam":
raise AttributeError
return object.__setattr__(self, name, value)
C.__setattr__ = mysetattr
try:
a.spam = "not spam"
except AttributeError:
pass
else:
verify(0, "expected AttributeError")
verify(a.spam == "spam")
def errors(): def errors():
if verbose: print "Testing errors..." if verbose: print "Testing errors..."
...@@ -750,12 +763,6 @@ def newslot(): ...@@ -750,12 +763,6 @@ def newslot():
verify(b.foo == 3) verify(b.foo == 3)
verify(b.__class__ is D) verify(b.__class__ is D)
class PerverseMetaType(type):
def mro(cls):
L = type.mro(cls)
L.reverse()
return L
def altmro(): def altmro():
if verbose: print "Testing mro() and overriding it..." if verbose: print "Testing mro() and overriding it..."
class A(object): class A(object):
...@@ -768,6 +775,11 @@ def altmro(): ...@@ -768,6 +775,11 @@ def altmro():
pass pass
verify(D.mro() == [D, B, C, A, object] == list(D.__mro__)) verify(D.mro() == [D, B, C, A, object] == list(D.__mro__))
verify(D().f() == "C") verify(D().f() == "C")
class PerverseMetaType(type):
def mro(cls):
L = type.mro(cls)
L.reverse()
return L
class X(A,B,C,D): class X(A,B,C,D):
__metaclass__ = PerverseMetaType __metaclass__ = PerverseMetaType
verify(X.__mro__ == (object, A, C, B, D, X)) verify(X.__mro__ == (object, A, C, B, D, X))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment