protected void AddMethod(string name, Python.CFunction function) { if (Initialized) throw new InvalidOperationException("Tried to add method after module was initialized"); var def = new PyMethodDef {Name = name, Documentation = "", Flags = MethodFlag.VarArgs, Method = function}; Methods.Add(def); }
protected void AddMethod(string name, Python.CFunction function) { if (Initialized) { throw new InvalidOperationException("Tried to add method after module was initialized"); } var def = new PyMethodDef { Name = name, Documentation = "", Flags = MethodFlag.VarArgs, Method = function }; Methods.Add(def); }
public static IntPtr InitModule(string name, PyMethodDef[] methods) { // unlikely to change since Python27 has entered basic mainteneance only const int apiVersion = 1013; if (string.IsNullOrEmpty(name) || methods == null || methods.Length == 0) throw new InvalidDataException("Invalid parameters for InitModule"); RefKeeper.Add(methods); int stride = Marshal.SizeOf(typeof (PyMethodDef)); var mem = Marshal.AllocHGlobal(stride*(methods.Length + 1)).ToInt32(); for (int i = 0; i < methods.Length; i++) Marshal.StructureToPtr(methods[i], new IntPtr(mem + (stride * i)), false); for (int i = 0; i < stride; i++) Marshal.WriteByte(new IntPtr(mem), i + (methods.Length * stride), 0); // no idea if we can free the memory - just leak it.. return Py_InitModule4(name, new IntPtr(mem), null, IntPtr.Zero, apiVersion); }