/// <summary> /// Implement explicit overload selection using subscript syntax ([]). /// </summary> /// <remarks> /// ConstructorBinding.GetItem(PyObject *o, PyObject *key) /// Return element of o corresponding to the object key or NULL on failure. /// This is the equivalent of the Python expression o[key]. /// </remarks> public static IntPtr mp_subscript(IntPtr op, IntPtr key) { var self = (ConstructorBinding)GetManagedObject(op); if (!self.type.Valid) { return(Exceptions.RaiseTypeError(self.type.DeletedMessage)); } Type tp = self.type.Value; Type[] types = Runtime.PythonArgsToTypeArray(key); if (types == null) { return(Exceptions.RaiseTypeError("type(s) expected")); } //MethodBase[] methBaseArray = self.ctorBinder.GetMethods(); //MethodBase ci = MatchSignature(methBaseArray, types); ConstructorInfo ci = tp.GetConstructor(types); if (ci == null) { return(Exceptions.RaiseTypeError("No match found for constructor signature")); } var boundCtor = new BoundContructor(tp, self.pyTypeHndl, self.ctorBinder, ci); return(boundCtor.pyHandle); }
//==================================================================== // ConstructorBinding dealloc implementation. //==================================================================== public static new void tp_dealloc(IntPtr ob) { BoundContructor self = (BoundContructor)GetManagedObject(ob); Runtime.XDecref(self.repr); Runtime.XDecref(self.pyTypeHndl); ExtensionType.FinalizeObject(self); }
//==================================================================== // BoundContructor __repr__ implementation [borrowed from MethodObject]. //==================================================================== public static IntPtr tp_repr(IntPtr ob) { BoundContructor self = (BoundContructor)GetManagedObject(ob); if (self.repr != IntPtr.Zero) { Runtime.XIncref(self.repr); return(self.repr); } string name = self.type.FullName; string str = self.ctorInfo.ToString(); int idx = str.IndexOf("("); str = String.Format("returns a new {0}{1}", name, str.Substring(idx)); self.repr = Runtime.PyString_FromString(str); Runtime.XIncref(self.repr); return(self.repr); }
/// <summary> /// BoundContructor.__call__(PyObject *callable_object, PyObject *args, PyObject *kw) /// </summary> /// <param name="ob"> PyObject *callable_object </param> /// <param name="args"> PyObject *args </param> /// <param name="kw"> PyObject *kw </param> /// <returns> A reference to a new instance of the class by invoking the selected ctor(). </returns> public static IntPtr tp_call(IntPtr op, IntPtr args, IntPtr kw) { BoundContructor self = (BoundContructor)GetManagedObject(op); // Even though a call with null ctorInfo just produces the old behavior /*if (self.ctorInfo == null) { * string msg = "Usage: Class.Overloads[CLR_or_python_Type, ...]"; * return Exceptions.RaiseTypeError(msg); * }*/ // Bind using ConstructorBinder.Bind and invoke the ctor providing a null instancePtr // which will fire self.ctorInfo using ConstructorInfo.Invoke(). Object obj = self.ctorBinder.InvokeRaw(IntPtr.Zero, args, kw, self.ctorInfo); if (obj == null) { // XXX set an error return(IntPtr.Zero); } // Instantiate the python object that wraps the result of the method call // and return the PyObject* to it. return(CLRObject.GetInstHandle(obj, self.pyTypeHndl)); }
/// <summary> /// Implement explicit overload selection using subscript syntax ([]). /// </summary> /// <remarks> /// ConstructorBinding.GetItem(PyObject *o, PyObject *key) /// Return element of o corresponding to the object key or NULL on failure. /// This is the equivalent of the Python expression o[key]. /// </remarks> public static IntPtr mp_subscript(IntPtr op, IntPtr key) { var self = (ConstructorBinding)GetManagedObject(op); Type[] types = Runtime.PythonArgsToTypeArray(key); if (types == null) { return(Exceptions.RaiseTypeError("type(s) expected")); } //MethodBase[] methBaseArray = self.ctorBinder.GetMethods(); //MethodBase ci = MatchSignature(methBaseArray, types); ConstructorInfo ci = self.type.GetConstructor(types); if (ci == null) { return(Exceptions.RaiseTypeError("No match found for constructor signature")); } var boundCtor = new BoundContructor(self.type, self.pyTypeHndl, self.ctorBinder, ci); /* Since nothing is cached, do we need the increment??? * Runtime.XIncref(boundCtor.pyHandle); // Decref'd by the interpreter??? */ return(boundCtor.pyHandle); }
//==================================================================== // Implement explicit overload selection using subscript syntax ([]). //==================================================================== /// <summary> /// ConstructorBinding.GetItem(PyObject *o, PyObject *key) /// Return element of o corresponding to the object key or NULL on failure. /// This is the equivalent of the Python expression o[key]. /// </summary> /// <param name="tp"></param> /// <param name="idx"></param> /// <returns></returns> public static IntPtr mp_subscript(IntPtr op, IntPtr key) { ConstructorBinding self = (ConstructorBinding)GetManagedObject(op); Type[] types = Runtime.PythonArgsToTypeArray(key); if (types == null) { return Exceptions.RaiseTypeError("type(s) expected"); } //MethodBase[] methBaseArray = self.ctorBinder.GetMethods(); //MethodBase ci = MatchSignature(methBaseArray, types); ConstructorInfo ci = self.type.GetConstructor(types); if (ci == null) { string msg = "No match found for constructor signature"; return Exceptions.RaiseTypeError(msg); } BoundContructor boundCtor = new BoundContructor(self.type, self.pyTypeHndl, self.ctorBinder, ci); /* Since nothing's chached, do we need the increment??? Runtime.Incref(boundCtor.pyHandle); // Decref'd by the interpreter??? */ return boundCtor.pyHandle; }