Ejemplo n.º 1
0
        /// <summary>
        /// Implementation of [] semantics for reflected types. This exists
        /// both to implement the Array[int] syntax for creating arrays and
        /// to support generic name overload resolution using [].
        /// </summary>
        public override IntPtr type_subscript(IntPtr idx)
        {
            // If this type is the Array type, the [<type>] means we need to
            // construct and return an array type of the given element type.
            if (type == typeof(Array))
            {
                if (Runtime.PyTuple_Check(idx))
                {
                    return(Exceptions.RaiseTypeError("type expected"));
                }
                var  c = GetManagedObject(idx) as ClassBase;
                Type t = c != null ? c.type : Converter.GetTypeByAlias(idx);
                if (t == null)
                {
                    return(Exceptions.RaiseTypeError("type expected"));
                }
                Type      a = t.MakeArrayType();
                ClassBase o = ClassManager.GetClass(a);
                Runtime.XIncref(o.pyHandle);
                return(o.pyHandle);
            }

            // If there are generics in our namespace with the same base name
            // as the current type, then [<type>] means the caller wants to
            // bind the generic type matching the given type parameters.
            Type[] types = Runtime.PythonArgsToTypeArray(idx);
            if (types == null)
            {
                return(Exceptions.RaiseTypeError("type(s) expected"));
            }

            Type gtype = AssemblyManager.LookupType($"{type.FullName}`{types.Length}");

            if (gtype != null)
            {
                var g = ClassManager.GetClass(gtype) as GenericType;
                return(g.type_subscript(idx));
                //Runtime.XIncref(g.pyHandle);
                //return g.pyHandle;
            }
            return(Exceptions.RaiseTypeError("unsubscriptable object"));
        }
Ejemplo n.º 2
0
        static Type TryComputeClrArgumentType(Type parameterType, IntPtr argument, bool needsResolution)
        {
            // this logic below handles cases when multiple overloading methods
            // are ambiguous, hence comparison between Python and CLR types
            // is necessary
            Type   clrtype = null;
            IntPtr pyoptype;

            if (needsResolution)
            {
                // HACK: each overload should be weighted in some way instead
                pyoptype = Runtime.PyObject_Type(argument);
                Exceptions.Clear();
                if (pyoptype != IntPtr.Zero)
                {
                    clrtype = Converter.GetTypeByAlias(pyoptype);
                }
                Runtime.XDecref(pyoptype);
            }

            if (clrtype != null)
            {
                var typematch = false;
                if ((parameterType != typeof(object)) && (parameterType != clrtype))
                {
                    IntPtr pytype = Converter.GetPythonTypeByAlias(parameterType);
                    pyoptype = Runtime.PyObject_Type(argument);
                    Exceptions.Clear();
                    if (pyoptype != IntPtr.Zero)
                    {
                        if (pytype != pyoptype)
                        {
                            typematch = false;
                        }
                        else
                        {
                            typematch = true;
                            clrtype   = parameterType;
                        }
                    }
                    if (!typematch)
                    {
                        // this takes care of enum values
                        TypeCode argtypecode   = Type.GetTypeCode(parameterType);
                        TypeCode paramtypecode = Type.GetTypeCode(clrtype);
                        if (argtypecode == paramtypecode)
                        {
                            typematch = true;
                            clrtype   = parameterType;
                        }
                    }
                    Runtime.XDecref(pyoptype);
                    if (!typematch)
                    {
                        return(null);
                    }
                }
                else
                {
                    typematch = true;
                    clrtype   = parameterType;
                }
            }
            else
            {
                clrtype = parameterType;
            }

            return(clrtype);
        }