Skip to content

Commit b05c0bd

Browse files
ncwcorona10
authored andcommitted
Fix initialisation of function, staticmethod and classmethod __dict__ (#57)
Add test files for function, staticmethod and classmethod Fixes #56
1 parent 0c9eac8 commit b05c0bd

File tree

6 files changed

+174
-6
lines changed

6 files changed

+174
-6
lines changed

py/classmethod.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ func (c *ClassMethod) GetDict() StringDict {
4444

4545
// ClassMethodNew
4646
func ClassMethodNew(metatype *Type, args Tuple, kwargs StringDict) (res Object, err error) {
47-
c := &ClassMethod{}
47+
c := &ClassMethod{
48+
Dict: make(StringDict),
49+
}
4850
err = UnpackTuple(args, kwargs, "classmethod", 1, 1, &c.Callable)
4951
if err != nil {
5052
return nil, err

py/function.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ func NewFunction(code *Code, globals StringDict, qualname string) *Function {
8484
Name: code.Name,
8585
Doc: doc,
8686
Module: module,
87+
Dict: make(StringDict),
8788
}
8889
}
8990

@@ -193,10 +194,6 @@ func init() {
193194
f.Dict = dict
194195
return nil
195196
},
196-
Fdel: func(self Object) error {
197-
self.(*Function).Dict = nil
198-
return nil
199-
},
200197
}
201198
FunctionType.Dict["__name__"] = &Property{
202199
Fget: func(self Object) (Object, error) {

py/staticmethod.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ func (c *StaticMethod) GetDict() StringDict {
4141

4242
// StaticMethodNew
4343
func StaticMethodNew(metatype *Type, args Tuple, kwargs StringDict) (res Object, err error) {
44-
c := &StaticMethod{}
44+
c := &StaticMethod{
45+
Dict: make(StringDict),
46+
}
4547
err = UnpackTuple(args, kwargs, "staticmethod", 1, 1, &c.Callable)
4648
if err != nil {
4749
return nil, err

py/tests/classmethod.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copyright 2019 The go-python Authors. All rights reserved.
2+
# Use of this source code is governed by a BSD-style
3+
# license that can be found in the LICENSE file.
4+
5+
doc="classmethod"
6+
7+
class A:
8+
@classmethod
9+
def fn(cls, p):
10+
assert cls is A
11+
return p+1
12+
13+
a = A()
14+
assert a.fn(1) == 2
15+
16+
a.x = 3
17+
assert a.x == 3
18+
19+
doc="finished"

py/tests/function.py

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
# Copyright 2019 The go-python Authors. All rights reserved.
2+
# Use of this source code is governed by a BSD-style
3+
# license that can be found in the LICENSE file.
4+
5+
doc="function"
6+
7+
def fn(p):
8+
"docstring"
9+
return p+1
10+
11+
assert fn(1) == 2
12+
13+
# FIXME this doesn't work yet
14+
#assert fn.__doc__ == "docstring"
15+
#fn.__doc__ = "hello"
16+
#assert fn.__doc__ == "hello"
17+
18+
assert str(type(fn)) == "<class 'function'>"
19+
20+
fn.x = 3
21+
assert fn.x == 3
22+
23+
def f2(p):
24+
return p+2
25+
26+
doc="check __code__"
27+
fn.__code__ = f2.__code__
28+
assert fn(1) == 3
29+
try:
30+
fn.__code__ = "bad"
31+
except TypeError:
32+
pass
33+
else:
34+
assert False, "TypeError not raised"
35+
36+
doc="check __defaults__"
37+
def f3(p=2):
38+
return p
39+
assert f3.__defaults__ == (2,)
40+
assert f3() == 2
41+
f3.__defaults__ = (10,)
42+
assert f3() == 10
43+
assert f3.__defaults__ == (10,)
44+
try:
45+
f3.__defaults__ = "bad"
46+
except TypeError:
47+
pass
48+
else:
49+
assert False, "TypeError not raised"
50+
del f3.__defaults__
51+
assert f3.__defaults__ == None or f3.__defaults__ == ()
52+
53+
doc="check __kwdefaults__"
54+
def f4(*, b=2):
55+
return b
56+
assert f4.__kwdefaults__ == {"b":2}
57+
assert f4() == 2
58+
f4.__kwdefaults__ = {"b":10}
59+
assert f4() == 10
60+
assert f4.__kwdefaults__ == {"b":10}
61+
try:
62+
f4.__kwdefaults__ = "bad"
63+
except TypeError:
64+
pass
65+
else:
66+
assert False, "TypeError not raised"
67+
del f4.__kwdefaults__
68+
assert f4.__kwdefaults__ == None or f4.__kwdefaults__ == {}
69+
70+
doc="check __annotations__"
71+
def f5(a: "potato") -> "sausage":
72+
pass
73+
assert f5.__annotations__ == {'a': 'potato', 'return': 'sausage'}
74+
f5.__annotations__ = {'a': 'potato', 'return': 'SAUSAGE'}
75+
assert f5.__annotations__ == {'a': 'potato', 'return': 'SAUSAGE'}
76+
try:
77+
f5.__annotations__ = "bad"
78+
except TypeError:
79+
pass
80+
else:
81+
assert False, "TypeError not raised"
82+
del f5.__annotations__
83+
assert f5.__annotations__ == None or f5.__annotations__ == {}
84+
85+
doc="check __dict__"
86+
def f6():
87+
pass
88+
assert f6.__dict__ == {}
89+
f6.__dict__ = {'a': 'potato'}
90+
assert f6.__dict__ == {'a': 'potato'}
91+
try:
92+
f6.__dict__ = "bad"
93+
except TypeError:
94+
pass
95+
else:
96+
assert False, "TypeError not raised"
97+
try:
98+
del f6.__dict__
99+
except (TypeError, AttributeError):
100+
pass
101+
else:
102+
assert False, "Error not raised"
103+
104+
doc="check __name__"
105+
def f7():
106+
pass
107+
assert f7.__name__ == "f7"
108+
f7.__name__ = "new_name"
109+
assert f7.__name__ == "new_name"
110+
try:
111+
f7.__name__ = 1
112+
except TypeError:
113+
pass
114+
else:
115+
assert False, "TypeError not raised"
116+
117+
doc="check __qualname__"
118+
def f8():
119+
pass
120+
assert f8.__qualname__ == "f8"
121+
f8.__qualname__ = "new_qualname"
122+
assert f8.__qualname__ == "new_qualname"
123+
try:
124+
f8.__qualname__ = 1
125+
except TypeError:
126+
pass
127+
else:
128+
assert False, "TypeError not raised"
129+
130+
doc="finished"

py/tests/staticmethod.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Copyright 2019 The go-python Authors. All rights reserved.
2+
# Use of this source code is governed by a BSD-style
3+
# license that can be found in the LICENSE file.
4+
5+
doc="staticmethod"
6+
7+
class A:
8+
@staticmethod
9+
def fn(p):
10+
return p+1
11+
12+
a = A()
13+
assert a.fn(1) == 2
14+
15+
a.x = 3
16+
assert a.x == 3
17+
18+
doc="finished"

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy