2
2
3
3
package py
4
4
5
+ // FIXME If we only have two types of Init and New function pointers
6
+ // we don't really need function pointers, though the ported C code
7
+ // expects them to be pointers
8
+
5
9
import (
6
10
"fmt"
7
11
)
@@ -65,7 +69,7 @@ const (
65
69
)
66
70
67
71
type Type struct {
68
- ObjectType * Type // Type of this object
72
+ ObjectType * Type // Type of this object -- FIXME this is redundant in Base?
69
73
Name string // For printing, in format "<module>.<name>"
70
74
Doc string // Documentation string
71
75
// Methods StringDict // *PyMethodDef
@@ -80,7 +84,7 @@ type Type struct {
80
84
// Subclasses Tuple
81
85
// Weaklist Tuple
82
86
New func (metatype * Type , args Tuple , kwargs StringDict ) * Type
83
- Init func (self Object , args Tuple , kwargs StringDict )
87
+ Init func (self * Type , args Tuple , kwargs StringDict )
84
88
Flags int // Flags to define presence of optional/expanded features
85
89
Qualname string
86
90
@@ -1011,6 +1015,7 @@ func (t *Type) Alloc() *Type {
1011
1015
1012
1016
// Set the type of the new object to this type
1013
1017
obj .ObjectType = t
1018
+ obj .Base = t
1014
1019
1015
1020
return obj
1016
1021
}
@@ -1360,7 +1365,7 @@ func TypeNew(metatype *Type, args Tuple, kwargs StringDict) *Type {
1360
1365
return new_type
1361
1366
}
1362
1367
1363
- func TypeInit (cls Object , args Tuple , kwargs StringDict ) {
1368
+ func TypeInit (cls * Type , args Tuple , kwargs StringDict ) {
1364
1369
if len (kwargs ) != 0 {
1365
1370
// FIXME TypeError
1366
1371
panic (fmt .Sprintf ("TypeError: type.__init__() takes no keyword arguments" ))
@@ -1421,13 +1426,23 @@ func excess_args(args Tuple, kwargs StringDict) bool {
1421
1426
return len (args ) != 0 || len (kwargs ) != 0
1422
1427
}
1423
1428
1424
- func ObjectInit (self Object , args Tuple , kwargs StringDict ) {
1429
+ func ObjectInit (self * Type , args Tuple , kwargs StringDict ) {
1425
1430
t := self .Type ()
1426
1431
// FIXME bodge to compare function pointers
1427
1432
if excess_args (args , kwargs ) && (fmt .Sprintf ("%x" , t .New ) == fmt .Sprintf ("%x" , ObjectNew ) || fmt .Sprintf ("%x" , t .Init ) != fmt .Sprintf ("%x" , ObjectInit )) {
1428
1433
// FIXME type error
1429
1434
panic (fmt .Sprintf ("TypeError: object.__init__() takes no parameters" ))
1430
1435
}
1436
+ // Call the __init__ method if it exists
1437
+ // FIXME this isn't the way cpython does it - it adjusts the function pointers
1438
+ init := self .GetAttrOrNil ("__init__" )
1439
+ fmt .Printf ("init = %v\n " , init )
1440
+ if init != nil {
1441
+ newArgs := make (Tuple , len (args )+ 1 )
1442
+ newArgs [0 ] = self
1443
+ copy (newArgs [1 :], args )
1444
+ Call (init , newArgs , kwargs )
1445
+ }
1431
1446
}
1432
1447
1433
1448
func ObjectNew (t * Type , args Tuple , kwargs StringDict ) * Type {
0 commit comments