Skip to content

Commit 3aadc0b

Browse files
committed
Attributes for Function object
1 parent 150601a commit 3aadc0b

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed

py/function.go

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,119 @@ func (f *Function) M__get__(instance, owner Object) Object {
144144
return f
145145
}
146146

147+
// Properties
148+
func init() {
149+
FunctionType.Dict["__code__"] = &Property{
150+
Fget: func(self Object) Object {
151+
return self.(*Function).Code
152+
},
153+
Fset: func(self, value Object) {
154+
f := self.(*Function)
155+
// Not legal to set f.func_code to anything other than a code object.
156+
code, ok := value.(*Code)
157+
if !ok {
158+
panic(ExceptionNewf(TypeError, "__code__ must be set to a code object"))
159+
}
160+
nfree := len(code.Freevars)
161+
nclosure := len(f.Closure)
162+
if nfree != nclosure {
163+
panic(ExceptionNewf(ValueError, "%s() requires a code object with %d free vars, not %d", f.Name, nclosure, nfree))
164+
}
165+
f.Code = code
166+
},
167+
}
168+
FunctionType.Dict["__defaults__"] = &Property{
169+
Fget: func(self Object) Object {
170+
return self.(*Function).Defaults
171+
},
172+
Fset: func(self, value Object) {
173+
f := self.(*Function)
174+
defaults, ok := value.(Tuple)
175+
if !ok {
176+
panic(ExceptionNewf(TypeError, "__defaults__ must be set to a tuple object"))
177+
}
178+
f.Defaults = defaults
179+
},
180+
Fdel: func(self Object) {
181+
self.(*Function).Defaults = nil
182+
},
183+
}
184+
FunctionType.Dict["__kwdefaults__"] = &Property{
185+
Fget: func(self Object) Object {
186+
return self.(*Function).KwDefaults
187+
},
188+
Fset: func(self, value Object) {
189+
f := self.(*Function)
190+
kwdefaults, ok := value.(StringDict)
191+
if !ok {
192+
panic(ExceptionNewf(TypeError, "__kwdefaults__ must be set to a dict object"))
193+
}
194+
f.KwDefaults = kwdefaults
195+
},
196+
Fdel: func(self Object) {
197+
self.(*Function).KwDefaults = nil
198+
},
199+
}
200+
FunctionType.Dict["__annotations__"] = &Property{
201+
Fget: func(self Object) Object {
202+
return self.(*Function).Annotations
203+
},
204+
Fset: func(self, value Object) {
205+
f := self.(*Function)
206+
annotations, ok := value.(StringDict)
207+
if !ok {
208+
panic(ExceptionNewf(TypeError, "__annotations__ must be set to a dict object"))
209+
}
210+
f.Annotations = annotations
211+
},
212+
Fdel: func(self Object) {
213+
self.(*Function).Annotations = nil
214+
},
215+
}
216+
FunctionType.Dict["__dict__"] = &Property{
217+
Fget: func(self Object) Object {
218+
return self.(*Function).Dict
219+
},
220+
Fset: func(self, value Object) {
221+
f := self.(*Function)
222+
dict, ok := value.(StringDict)
223+
if !ok {
224+
panic(ExceptionNewf(TypeError, "__dict__ must be set to a dict object"))
225+
}
226+
f.Dict = dict
227+
},
228+
Fdel: func(self Object) {
229+
self.(*Function).Dict = nil
230+
},
231+
}
232+
FunctionType.Dict["__name__"] = &Property{
233+
Fget: func(self Object) Object {
234+
return String(self.(*Function).Name)
235+
},
236+
Fset: func(self, value Object) {
237+
f := self.(*Function)
238+
name, ok := value.(String)
239+
if !ok {
240+
panic(ExceptionNewf(TypeError, "__name__ must be set to a string object"))
241+
}
242+
f.Name = string(name)
243+
},
244+
}
245+
FunctionType.Dict["__qualname__"] = &Property{
246+
Fget: func(self Object) Object {
247+
return String(self.(*Function).Qualname)
248+
},
249+
Fset: func(self, value Object) {
250+
f := self.(*Function)
251+
qualname, ok := value.(String)
252+
if !ok {
253+
panic(ExceptionNewf(TypeError, "__qualname__ must be set to a string object"))
254+
}
255+
f.Qualname = string(qualname)
256+
},
257+
}
258+
}
259+
147260
// Make sure it satisfies the interface
148261
var _ Object = (*Function)(nil)
149262
var _ I__call__ = (*Function)(nil)

0 commit comments

Comments
 (0)