2016-11-26 | Paul Boddie | raw files shortlog changelog graph | Changed serialisation to handle classes separately, since <class>.__str__ and <class>.__repr__ are interpreted as type.__str__ and type.__repr__ respectively (being provided by the class of <class>), but these methods would always be unbound even if directly obtained from <class>. | |
lib/__builtins__/core.py (file) lib/__builtins__/identity.py (file) lib/__builtins__/str.py (file) tests/aliases.py (file) |
1.1 --- a/lib/__builtins__/core.py Sat Nov 26 19:30:47 2016 +0100 1.2 +++ b/lib/__builtins__/core.py Sat Nov 26 19:40:09 2016 +0100 1.3 @@ -79,13 +79,9 @@ 1.4 1.5 "The class of all classes." 1.6 1.7 - def __str__(self): 1.8 - 1.9 - "Return a string representation." 1.10 + # __str__ and __repr__ are handled by str and repr for classes. 1.11 1.12 - return "<type>" 1.13 - 1.14 - __repr__ = __str__ 1.15 + pass 1.16 1.17 class BaseException: 1.18
2.1 --- a/lib/__builtins__/identity.py Sat Nov 26 19:30:47 2016 +0100 2.2 +++ b/lib/__builtins__/identity.py Sat Nov 26 19:40:09 2016 +0100 2.3 @@ -86,6 +86,14 @@ 2.4 2.5 "Return a program representation for 'obj'." 2.6 2.7 - return obj.__repr__() 2.8 + # Classes do not provide __repr__ directly. 2.9 + 2.10 + if isclass(obj): 2.11 + return "<type>" 2.12 + 2.13 + # Class attributes of instances provide __repr__. 2.14 + 2.15 + else: 2.16 + return obj.__repr__() 2.17 2.18 # vim: tabstop=4 expandtab shiftwidth=4
3.1 --- a/lib/__builtins__/str.py Sat Nov 26 19:30:47 2016 +0100 3.2 +++ b/lib/__builtins__/str.py Sat Nov 26 19:40:09 2016 +0100 3.3 @@ -19,6 +19,7 @@ 3.4 this program. If not, see <http://www.gnu.org/licenses/>. 3.5 """ 3.6 3.7 +from __builtins__.identity import isclass 3.8 from __builtins__.iterator import listiterator 3.9 from __builtins__.operator import _binary_op, _negate 3.10 import native 3.11 @@ -113,6 +114,14 @@ 3.12 3.13 "Return the string representation of 'obj'." 3.14 3.15 - return obj.__str__() 3.16 + # Classes do not provide __str__ directly. 3.17 + 3.18 + if isclass(obj): 3.19 + return "<type>" 3.20 + 3.21 + # Class attributes of instances provide __str__. 3.22 + 3.23 + else: 3.24 + return obj.__str__() 3.25 3.26 # vim: tabstop=4 expandtab shiftwidth=4
4.1 --- a/tests/aliases.py Sat Nov 26 19:30:47 2016 +0100 4.2 +++ b/tests/aliases.py Sat Nov 26 19:40:09 2016 +0100 4.3 @@ -4,54 +4,107 @@ 4.4 4.5 D = C # alias for C 4.6 4.7 +print C # "<type>" 4.8 +print D # "<type>" 4.9 + 4.10 class E: 4.11 def m(self): 4.12 return 2 4.13 4.14 F = E # alias for E 4.15 4.16 +print E # "<type>" 4.17 +print F # "<type>" 4.18 + 4.19 def f(): 4.20 c = C 4.21 d = D # C 4.22 cm = C.m 4.23 dm = D.m # C.m 4.24 4.25 + print c # "<type>" 4.26 + print d # "<type>" 4.27 + print cm # "<function>" 4.28 + print dm # "<function>" 4.29 + 4.30 c = E 4.31 d = F # E 4.32 cm = E.m 4.33 dm = F.m # E.m 4.34 4.35 + print c # "<type>" 4.36 + print d # "<type>" 4.37 + print cm # "<function>" 4.38 + print dm # "<function>" 4.39 + 4.40 +f() 4.41 + 4.42 Cm = C.m 4.43 Dm = D.m 4.44 Em = E.m 4.45 Fm = F.m 4.46 4.47 +print Cm # "<function>" 4.48 +print Dm # "<function>" 4.49 +print Em # "<function>" 4.50 +print Fm # "<function>" 4.51 + 4.52 def g(): 4.53 Cm = E.m 4.54 Dm = F.m # E.m 4.55 4.56 + print Cm # "<function>" 4.57 + print Dm # "<function>" 4.58 + 4.59 +g() 4.60 + 4.61 def h(): 4.62 global Em, Fm 4.63 Em = C.m 4.64 Fm = D.m # C.m 4.65 4.66 + print Cm # "<function>" 4.67 + print Dm # "<function>" 4.68 + 4.69 +h() 4.70 + 4.71 Ci = C() 4.72 Ei = E() 4.73 4.74 +print Ci # "__main__.C" 4.75 +print Ei # "__main__.E" 4.76 + 4.77 def i(): 4.78 c = Ci 4.79 + print c # "__main__.C" 4.80 c = Ei 4.81 + print c # "__main__.E" 4.82 + 4.83 +i() 4.84 4.85 def j(): 4.86 global Ei 4.87 Ei = C() 4.88 + print Ei # "__main__.C" 4.89 + 4.90 +j() 4.91 4.92 L = [] 4.93 M = [1] 4.94 4.95 +print L # [] 4.96 +print M # [1] 4.97 + 4.98 def k(): 4.99 c = L 4.100 + print c # [] 4.101 + 4.102 +k() 4.103 4.104 def l(): 4.105 global M 4.106 M = [] 4.107 + print M # [] 4.108 + 4.109 +l() 4.110 +print M # []