@@ -172,15 +172,15 @@ var TypeType *Type = &Type{
172
172
Name : "type" ,
173
173
Doc : "type(object) -> the object's type\n type(name, bases, dict) -> a new type" ,
174
174
}
175
- var BaseObjectType = NewTypeX ("object" , "The most base type" , ObjectNew , ObjectInit )
175
+ var ObjectType = TypeType . NewType ("object" , "The most base type" , ObjectNew , ObjectInit )
176
176
177
177
func init () {
178
178
// Initialises like this to avoid initialisation loops
179
179
TypeType .New = TypeNew
180
180
TypeType .Init = TypeInit
181
181
TypeType .ObjectType = TypeType
182
182
// FIXME put this into NewType
183
- BaseObjectType .Flags |= TPFLAGS_BASETYPE
183
+ ObjectType .Flags |= TPFLAGS_BASETYPE
184
184
}
185
185
186
186
// Type of this object
@@ -212,6 +212,26 @@ func NewTypeX(Name string, Doc string, New NewFunc, Init InitFunc) *Type {
212
212
}
213
213
}
214
214
215
+ // Make a subclass of a type
216
+ //
217
+ // For making Go types
218
+ func (t * Type ) NewType (Name string , Doc string , New NewFunc , Init InitFunc ) * Type {
219
+ // inherit constructors
220
+ if New == nil {
221
+ New = t .New
222
+ }
223
+ if Init == nil {
224
+ Init = t .Init
225
+ }
226
+ return & Type {
227
+ ObjectType : t ,
228
+ Name : Name ,
229
+ Doc : Doc ,
230
+ New : New ,
231
+ Init : Init ,
232
+ }
233
+ }
234
+
215
235
// Determine the most derived metatype.
216
236
func (metatype * Type ) CalculateMetaclass (bases Tuple ) * Type {
217
237
// Determine the proper metatype to deal with this,
@@ -259,7 +279,7 @@ func (a *Type) IsSubtype(b *Type) bool {
259
279
break
260
280
}
261
281
}
262
- return b == BaseObjectType
282
+ return b == ObjectType
263
283
}
264
284
}
265
285
@@ -298,7 +318,7 @@ func (t *Type) Lookup(name string) Object {
298
318
299
319
// FIXME caching
300
320
// if (MCACHE_CACHEABLE_NAME(name) &&
301
- // PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG )) {
321
+ // PyType_HasFeature(type, TPFLAGS_VALID_VERSION_TAG )) {
302
322
// // fast path
303
323
// h = MCACHE_HASH_METHOD(type, name);
304
324
// if (method_cache[h].version == type->tp_version_tag &&
@@ -706,10 +726,10 @@ func (t *Type) mro_internal() {
706
726
707
727
func (t * Type ) inherit_special (base * Type ) {
708
728
// /* Copying basicsize is connected to the GC flags */
709
- // if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC ) &&
710
- // (base->tp_flags & Py_TPFLAGS_HAVE_GC ) &&
729
+ // if (!(type->tp_flags & TPFLAGS_HAVE_GC ) &&
730
+ // (base->tp_flags & TPFLAGS_HAVE_GC ) &&
711
731
// (!type->tp_traverse && !type->tp_clear)) {
712
- // type->tp_flags |= Py_TPFLAGS_HAVE_GC ;
732
+ // type->tp_flags |= TPFLAGS_HAVE_GC ;
713
733
// if (type->tp_traverse == nil)
714
734
// type->tp_traverse = base->tp_traverse;
715
735
// if (type->tp_clear == nil)
@@ -727,7 +747,7 @@ func (t *Type) inherit_special(base *Type) {
727
747
// other built-in type as the default also
728
748
// inherit object.__new__. */
729
749
// if (base != &PyBaseObject_Type ||
730
- // (type->tp_flags & Py_TPFLAGS_HEAPTYPE )) {
750
+ // (type->tp_flags & TPFLAGS_HEAPTYPE )) {
731
751
// if (type->tp_new == nil)
732
752
// type->tp_new = base->tp_new;
733
753
// }
@@ -745,23 +765,27 @@ func (t *Type) inherit_special(base *Type) {
745
765
// COPYVAL(tp_weaklistoffset);
746
766
// COPYVAL(tp_dictoffset);
747
767
748
- // /* Setup fast subclass flags */
749
- // if (PyType_IsSubtype(base, (PyTypeObject*)PyExc_BaseException))
750
- // type->tp_flags |= Py_TPFLAGS_BASE_EXC_SUBCLASS;
751
- // else if (PyType_IsSubtype(base, &PyType_Type))
752
- // type->tp_flags |= Py_TPFLAGS_TYPE_SUBCLASS;
753
- // else if (PyType_IsSubtype(base, &PyLong_Type))
754
- // type->tp_flags |= Py_TPFLAGS_LONG_SUBCLASS;
755
- // else if (PyType_IsSubtype(base, &PyBytes_Type))
756
- // type->tp_flags |= Py_TPFLAGS_BYTES_SUBCLASS;
757
- // else if (PyType_IsSubtype(base, &PyUnicode_Type))
758
- // type->tp_flags |= Py_TPFLAGS_UNICODE_SUBCLASS;
759
- // else if (PyType_IsSubtype(base, &PyTuple_Type))
760
- // type->tp_flags |= Py_TPFLAGS_TUPLE_SUBCLASS;
761
- // else if (PyType_IsSubtype(base, &PyList_Type))
762
- // type->tp_flags |= Py_TPFLAGS_LIST_SUBCLASS;
763
- // else if (PyType_IsSubtype(base, &PyDict_Type))
764
- // type->tp_flags |= Py_TPFLAGS_DICT_SUBCLASS;
768
+ // Setup fast subclass flags
769
+ switch {
770
+ case base .IsSubtype (BaseException ):
771
+ t .Flags |= TPFLAGS_BASE_EXC_SUBCLASS
772
+ case base .IsSubtype (TypeType ):
773
+ t .Flags |= TPFLAGS_TYPE_SUBCLASS
774
+ case base .IsSubtype (IntType ):
775
+ t .Flags |= TPFLAGS_LONG_SUBCLASS
776
+ case base .IsSubtype (BigIntType ):
777
+ t .Flags |= TPFLAGS_LONG_SUBCLASS
778
+ case base .IsSubtype (BytesType ):
779
+ t .Flags |= TPFLAGS_BYTES_SUBCLASS
780
+ case base .IsSubtype (StringType ):
781
+ t .Flags |= TPFLAGS_UNICODE_SUBCLASS
782
+ case base .IsSubtype (TupleType ):
783
+ t .Flags |= TPFLAGS_TUPLE_SUBCLASS
784
+ case base .IsSubtype (ListType ):
785
+ t .Flags |= TPFLAGS_LIST_SUBCLASS
786
+ case base .IsSubtype (DictType ):
787
+ t .Flags |= TPFLAGS_DICT_SUBCLASS
788
+ }
765
789
766
790
}
767
791
@@ -833,13 +857,13 @@ func (t *Type) Ready() {
833
857
834
858
// Initialize tp_base (defaults to BaseObject unless that's us)
835
859
base := t .Base
836
- if base == nil && t != BaseObjectType {
837
- base = BaseObjectType
860
+ if base == nil && t != ObjectType {
861
+ base = ObjectType
838
862
t .Base = base
839
863
}
840
864
841
865
// Now the only way base can still be nil is if type is
842
- // BaseObjectType .
866
+ // ObjectType .
843
867
844
868
// Initialize the base class
845
869
if base != nil && base .Dict == nil {
@@ -850,7 +874,7 @@ func (t *Type) Ready() {
850
874
// compilable separately on Windows can call PyType_Ready() instead of
851
875
// initializing the ob_type field of their type objects.
852
876
// The test for base != nil is really unnecessary, since base is only
853
- // nil when type is BaseObjectType , and we know its ob_type is
877
+ // nil when type is ObjectType , and we know its ob_type is
854
878
// not nil (it's initialized to &PyType_Type). But coverity doesn't
855
879
// know that.
856
880
@@ -976,7 +1000,7 @@ func (t *Type) solid_base() *Type {
976
1000
if t .Base != nil {
977
1001
base = t .Base .solid_base ()
978
1002
} else {
979
- base = BaseObjectType
1003
+ base = ObjectType
980
1004
}
981
1005
if t .extra_ivars (base ) {
982
1006
return t
@@ -1082,7 +1106,7 @@ func TypeNew(metatype *Type, args Tuple, kwargs StringDict) Object {
1082
1106
1083
1107
// Adjust for empty tuple bases
1084
1108
if len (bases ) == 0 {
1085
- bases = Tuple {Object (BaseObjectType )}
1109
+ bases = Tuple {Object (ObjectType )}
1086
1110
}
1087
1111
1088
1112
// Calculate best base, and check that all bases are type objects
@@ -1469,7 +1493,7 @@ func ObjectNew(t *Type, args Tuple, kwargs StringDict) Object {
1469
1493
}
1470
1494
1471
1495
// FIXME abstrac types
1472
- // if (type->tp_flags & Py_TPFLAGS_IS_ABSTRACT ) {
1496
+ // if (type->tp_flags & TPFLAGS_IS_ABSTRACT ) {
1473
1497
// PyObject *abstract_methods = NULL;
1474
1498
// PyObject *builtins;
1475
1499
// PyObject *sorted;
0 commit comments