@@ -65,24 +65,24 @@ const (
65
65
)
66
66
67
67
type Type struct {
68
- ObjectType * Type // Type of this object
69
- Name string // For printing, in format "<module>.<name>"
70
- Doc string // Documentation string
71
- Methods StringDict // *PyMethodDef
72
- Members StringDict // *PyMemberDef
68
+ ObjectType * Type // Type of this object
69
+ Name string // For printing, in format "<module>.<name>"
70
+ Doc string // Documentation string
71
+ // Methods StringDict // *PyMethodDef
72
+ // Members StringDict // *PyMemberDef
73
73
// Getset *PyGetSetDef
74
- Base * Type
75
- Dict StringDict
76
- Dictoffset int
77
- Bases Tuple
78
- Mro Tuple // method resolution order
74
+ Base * Type
75
+ Dict StringDict
76
+ // Dictoffset int
77
+ Bases Tuple
78
+ Mro Tuple // method resolution order
79
79
// Cache Object
80
- Subclasses Tuple
81
- Weaklist Tuple
82
- New func (metatype * Type , args Tuple , kwargs StringDict ) * Type
83
- Init func (self Object , args Tuple , kwargs StringDict )
84
- Flags int // Flags to define presence of optional/expanded features
85
- Qualname string
80
+ // Subclasses Tuple
81
+ // Weaklist Tuple
82
+ New func (metatype * Type , args Tuple , kwargs StringDict ) * Type
83
+ Init func (self Object , args Tuple , kwargs StringDict )
84
+ Flags int // Flags to define presence of optional/expanded features
85
+ Qualname string
86
86
87
87
/*
88
88
Py_ssize_t tp_basicsize, tp_itemsize; // For allocation
@@ -322,6 +322,68 @@ func (t *Type) Lookup(name string) Object {
322
322
return res
323
323
}
324
324
325
+ // Get an attribute from the type
326
+ //
327
+ // FIXME this isn't totally correct!
328
+ // as we are ignoring getattribute etc
329
+ // See _PyObject_GenericGetAttrWithDict in object.c
330
+ func (t * Type ) GetMethod (name string ) Object {
331
+ // Look in instance dictionary first
332
+ if res , ok := t .Dict [name ]; ok {
333
+ return res
334
+ }
335
+ // Then look in type Dict
336
+ if res , ok := t .Type ().Dict [name ]; ok {
337
+ return res
338
+ }
339
+ return t .Lookup (name )
340
+ }
341
+
342
+ // Calls method on name
343
+ //
344
+ // If method not found returns (nil, false)
345
+ //
346
+ // If method found returns (object, true)
347
+ //
348
+ // May raise exceptions if calling the method failed
349
+ func (t * Type ) CallMethod (name string , args Tuple , kwargs StringDict ) (Object , bool ) {
350
+ fn := t .GetMethod (name )
351
+ if fn == nil {
352
+ return nil , false
353
+ }
354
+ return Call (fn , args , kwargs ), true
355
+ }
356
+
357
+ // Calls a type method on obj
358
+ //
359
+ // If obj isnt a *Type or the method isn't found on it returns (nil, false)
360
+ //
361
+ // Otherwise returns (object, true)
362
+ //
363
+ // May raise exceptions if calling the method fails
364
+ func TypeCall (self Object , name string , args Tuple , kwargs StringDict ) (Object , bool ) {
365
+ t , ok := self .(* Type )
366
+ if ! ok {
367
+ return nil , false
368
+ }
369
+ return t .CallMethod (name , args , kwargs )
370
+ }
371
+
372
+ // Calls TypeCall with 0 arguments
373
+ func TypeCall0 (self Object , name string ) (Object , bool ) {
374
+ return TypeCall (self , name , nil , nil )
375
+ }
376
+
377
+ // Calls TypeCall with 1 argument
378
+ func TypeCall1 (self Object , name string , arg Object ) (Object , bool ) {
379
+ return TypeCall (self , name , Tuple {arg }, nil )
380
+ }
381
+
382
+ // Calls TypeCall with 2 arguments
383
+ func TypeCall2 (self Object , name string , arg1 , arg2 Object ) (Object , bool ) {
384
+ return TypeCall (self , name , Tuple {arg1 , arg2 }, nil )
385
+ }
386
+
325
387
// Internal routines to do a method lookup in the type
326
388
// without looking in the instance dictionary
327
389
// (so we can't use PyObject_GetAttr) but still binding
@@ -950,6 +1012,7 @@ func (t *Type) Alloc() *Type {
950
1012
951
1013
// Create a new type
952
1014
func TypeNew (metatype * Type , args Tuple , kwargs StringDict ) * Type {
1015
+ fmt .Printf ("TypeNew(type=%q, args=%v, kwargs=%v\n " , metatype .Name , args , kwargs )
953
1016
var nameObj , basesObj , orig_dictObj Object
954
1017
var new_type , base , winner * Type
955
1018
// PyHeapTypeObject et;
@@ -1159,10 +1222,11 @@ func TypeNew(metatype *Type, args Tuple, kwargs StringDict) *Type {
1159
1222
1160
1223
// Initialize tp_dict from passed-in dict
1161
1224
new_type .Dict = dict
1225
+ fmt .Printf ("New type dict is %v\n " , dict )
1162
1226
1163
1227
// Set __module__ in the dict
1164
1228
if _ , ok := dict ["__module__" ]; ! ok {
1165
- fmt .Printf ("FIXME neet to get the current vm globals somehow\n " )
1229
+ fmt .Printf ("*** FIXME need to get the current vm globals somehow\n " )
1166
1230
// tmp = PyEval_GetGlobals()
1167
1231
// if tmp != nil {
1168
1232
// tmp, ok := tmp["__name__"]
0 commit comments