AddNumberMethodsWithIndex(IntPtr typePtr) { this.AddNumberMethodsWithoutIndex(typePtr); IntPtr nmPtr = CPyMarshal.ReadPtrField(typePtr, typeof(PyTypeObject), "tp_as_number"); CPyMarshal.WritePtrField(nmPtr, typeof(PyNumberMethods), "nb_index", this.GetFuncPtr("PyNumber_Index")); Py_TPFLAGS flags = (Py_TPFLAGS)CPyMarshal.ReadIntField(typePtr, typeof(PyTypeObject), "tp_flags"); flags |= Py_TPFLAGS.HAVE_INDEX; CPyMarshal.WriteIntField(typePtr, typeof(PyTypeObject), "tp_flags", (Int32)flags); }
InheritSubclassFlags(IntPtr typePtr) { Py_TPFLAGS flags = (Py_TPFLAGS)CPyMarshal.ReadIntField(typePtr, typeof(PyTypeObject), "tp_flags"); if (this.PyType_IsSubtype(typePtr, this.PyInt_Type) != 0) { flags |= Py_TPFLAGS.INT_SUBCLASS; } if (this.PyType_IsSubtype(typePtr, this.PyLong_Type) != 0) { flags |= Py_TPFLAGS.LONG_SUBCLASS; } if (this.PyType_IsSubtype(typePtr, this.PyList_Type) != 0) { flags |= Py_TPFLAGS.LIST_SUBCLASS; } if (this.PyType_IsSubtype(typePtr, this.PyTuple_Type) != 0) { flags |= Py_TPFLAGS.TUPLE_SUBCLASS; } if (this.PyType_IsSubtype(typePtr, this.PyString_Type) != 0) { flags |= Py_TPFLAGS.STRING_SUBCLASS; } if (this.PyType_IsSubtype(typePtr, this.PyDict_Type) != 0) { flags |= Py_TPFLAGS.DICT_SUBCLASS; } if (this.PyType_IsSubtype(typePtr, this.PyType_Type) != 0) { flags |= Py_TPFLAGS.TYPE_SUBCLASS; } // TODO: PyExc_BaseException is tedious CPyMarshal.WriteIntField(typePtr, typeof(PyTypeObject), "tp_flags", (Int32)flags); }
PyType_Ready(IntPtr typePtr) { if (typePtr == IntPtr.Zero) { return(-1); } Py_TPFLAGS flags = (Py_TPFLAGS)CPyMarshal.ReadIntField(typePtr, typeof(PyTypeObject), "tp_flags"); if ((Int32)(flags & (Py_TPFLAGS.READY | Py_TPFLAGS.READYING)) != 0) { return(0); } flags |= Py_TPFLAGS.READYING; CPyMarshal.WriteIntField(typePtr, typeof(PyTypeObject), "tp_flags", (Int32)flags); IntPtr typeTypePtr = CPyMarshal.ReadPtrField(typePtr, typeof(PyTypeObject), "ob_type"); if ((typeTypePtr == IntPtr.Zero) && (typePtr != this.PyType_Type)) { CPyMarshal.WritePtrField(typePtr, typeof(PyTypeObject), "ob_type", this.PyType_Type); } IntPtr typeBasePtr = CPyMarshal.ReadPtrField(typePtr, typeof(PyTypeObject), "tp_base"); if ((typeBasePtr == IntPtr.Zero) && (typePtr != this.PyBaseObject_Type)) { typeBasePtr = this.PyBaseObject_Type; CPyMarshal.WritePtrField(typePtr, typeof(PyTypeObject), "tp_base", typeBasePtr); } PyType_Ready(typeBasePtr); this.InheritSubclassFlags(typePtr); this.InheritPtrField(typePtr, "tp_alloc"); this.InheritPtrField(typePtr, "tp_new"); this.InheritPtrField(typePtr, "tp_dealloc"); this.InheritPtrField(typePtr, "tp_free"); this.InheritPtrField(typePtr, "tp_doc"); this.InheritPtrField(typePtr, "tp_call"); this.InheritPtrField(typePtr, "tp_as_number"); this.InheritPtrField(typePtr, "tp_as_sequence"); this.InheritPtrField(typePtr, "tp_as_mapping"); this.InheritPtrField(typePtr, "tp_as_buffer"); this.InheritIntField(typePtr, "tp_basicsize"); this.InheritIntField(typePtr, "tp_itemsize"); if (!this.HasPtr(typePtr)) { this.Retrieve(typePtr); } else { object klass = this.Retrieve(typePtr); if (Builtin.hasattr(this.scratchContext, klass, "__dict__")) { object typeDict = Builtin.getattr(this.scratchContext, klass, "__dict__"); CPyMarshal.WritePtrField(typePtr, typeof(PyTypeObject), "tp_dict", this.Store(typeDict)); } } flags = (Py_TPFLAGS)CPyMarshal.ReadIntField(typePtr, typeof(PyTypeObject), "tp_flags"); flags |= Py_TPFLAGS.READY | Py_TPFLAGS.HAVE_CLASS; flags &= ~Py_TPFLAGS.READYING; CPyMarshal.WriteIntField(typePtr, typeof(PyTypeObject), "tp_flags", (Int32)flags); return(0); }