Implements a Python type that constucts the given Type given a particular ContructorInfo.
Here mostly because I wanted a new __repr__ function for the selected constructor. An earlier implementation hung the __call__ on the ContructorBinding class and returned an Incref()ed self.pyHandle from the __get__ function.
Inheritance: ExtensionType
コード例 #1
0
        /// <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);
        }
コード例 #2
0
        //====================================================================
        // 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);
        }
コード例 #3
0
        //====================================================================
        // 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);
        }
コード例 #4
0
        /// <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));
        }
コード例 #5
0
        /// <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);
        }
コード例 #6
0
ファイル: constructorbinding.cs プロジェクト: JackHang/Wox
        //====================================================================
        // 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;
        }