22
33package py
44
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+
59import (
610 "fmt"
711)
@@ -65,7 +69,7 @@ const (
6569)
6670
6771type Type struct {
68- ObjectType * Type // Type of this object
72+ ObjectType * Type // Type of this object -- FIXME this is redundant in Base?
6973 Name string // For printing, in format "<module>.<name>"
7074 Doc string // Documentation string
7175 // Methods StringDict // *PyMethodDef
@@ -80,7 +84,7 @@ type Type struct {
8084 // Subclasses Tuple
8185 // Weaklist Tuple
8286 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 )
8488 Flags int // Flags to define presence of optional/expanded features
8589 Qualname string
8690
@@ -1011,6 +1015,7 @@ func (t *Type) Alloc() *Type {
10111015
10121016 // Set the type of the new object to this type
10131017 obj .ObjectType = t
1018+ obj .Base = t
10141019
10151020 return obj
10161021}
@@ -1360,7 +1365,7 @@ func TypeNew(metatype *Type, args Tuple, kwargs StringDict) *Type {
13601365 return new_type
13611366}
13621367
1363- func TypeInit (cls Object , args Tuple , kwargs StringDict ) {
1368+ func TypeInit (cls * Type , args Tuple , kwargs StringDict ) {
13641369 if len (kwargs ) != 0 {
13651370 // FIXME TypeError
13661371 panic (fmt .Sprintf ("TypeError: type.__init__() takes no keyword arguments" ))
@@ -1421,13 +1426,23 @@ func excess_args(args Tuple, kwargs StringDict) bool {
14211426 return len (args ) != 0 || len (kwargs ) != 0
14221427}
14231428
1424- func ObjectInit (self Object , args Tuple , kwargs StringDict ) {
1429+ func ObjectInit (self * Type , args Tuple , kwargs StringDict ) {
14251430 t := self .Type ()
14261431 // FIXME bodge to compare function pointers
14271432 if excess_args (args , kwargs ) && (fmt .Sprintf ("%x" , t .New ) == fmt .Sprintf ("%x" , ObjectNew ) || fmt .Sprintf ("%x" , t .Init ) != fmt .Sprintf ("%x" , ObjectInit )) {
14281433 // FIXME type error
14291434 panic (fmt .Sprintf ("TypeError: object.__init__() takes no parameters" ))
14301435 }
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+ }
14311446}
14321447
14331448func ObjectNew (t * Type , args Tuple , kwargs StringDict ) * Type {
0 commit comments