internal static PythonTypeSlot /*!*/ GetFinalSlotForFunction(BuiltinFunction /*!*/ func)
        {
            if ((func.FunctionType & FunctionType.Method) != 0)
            {
                BuiltinMethodDescriptor desc;
                lock (_methodCache) {
                    if (!_methodCache.TryGetValue(func, out desc))
                    {
                        _methodCache[func] = desc = new BuiltinMethodDescriptor(func);
                    }

                    return(desc);
                }
            }

            if (func.Targets[0].IsDefined(typeof(ClassMethodAttribute), true))
            {
                lock (_classMethodCache) {
                    ClassMethodDescriptor desc;
                    if (!_classMethodCache.TryGetValue(func, out desc))
                    {
                        _classMethodCache[func] = desc = new ClassMethodDescriptor(func);
                    }

                    return(desc);
                }
            }

            return(func);
        }
Beispiel #2
0
        public BuiltinMethodInfo(BuiltinMethodDescriptor method, ProjectState projectState)
            : base(ClrModule.GetPythonType(typeof(BuiltinMethodDescriptor)), projectState)
        {
            // TODO: get return information, parameters, members
            _method = method;

            var function = PythonOps.GetBuiltinMethodDescriptorTemplate(method);

            _returnTypes = Utils.GetReturnTypes(function, projectState);
            _doc         = null;
        }
Beispiel #3
0
        private static BuiltinFunction TryConvertToBuiltinFunction(object o)
        {
            BuiltinMethodDescriptor md = o as BuiltinMethodDescriptor;

            if (md != null)
            {
                return(md.Template);
            }

            return(o as BuiltinFunction);
        }
Beispiel #4
0
        /// <summary>
        /// Determines if a type member can be imported.  This is used to treat static types like modules.
        /// </summary>
        private static bool IsStaticTypeMemberInAll(CodeContext /*!*/ context, PythonType /*!*/ pt, string name, out object res)
        {
            PythonTypeSlot pts;

            res = null;
            if (pt.TryResolveSlot(context, name, out pts))
            {
                if (name == "__doc__" || name == "__class__")
                {
                    // these exist but we don't want to clobber __doc__ on import * or bring in __class__
                    return(false);
                }
                else if (pts is ReflectedGetterSetter)
                {
                    // property or indexer, these fetch the value at runtime, the user needs to explicitly
                    // import them using from type import property
                    return(false);
                }

                ReflectedField rf = pts as ReflectedField;
                if (rf != null && !rf._info.IsInitOnly && !rf._info.IsLiteral)
                {
                    // only bring in read-only fields, if the value can change the user needs to explicitly
                    // import by name
                    return(false);
                }

                BuiltinMethodDescriptor method = pts as BuiltinMethodDescriptor;
                if (method != null && (!method.DeclaringType.IsSealed() || !method.DeclaringType.IsAbstract()))
                {
                    // inherited object member on a static class (GetHashCode, Equals, etc...)
                    return(false);
                }

                BuiltinFunction bf = pts as BuiltinFunction;
                if (bf != null && (!bf.DeclaringType.IsSealed() || !bf.DeclaringType.IsAbstract()))
                {
                    // __new__/ReferenceEquals inherited from object
                    return(false);
                }

                if (pts.TryGetValue(context, null, pt, out res))
                {
                    return(true);
                }
            }

            res = null;
            return(false);
        }
Beispiel #5
0
 public FastMethodGet(Type type, BuiltinMethodDescriptor method)
 {
     _type   = type;
     _method = method;
 }
        internal IMember MakeObject(object obj)
        {
            if (obj == null)
            {
                return(null);
            }
            lock (this) {
                IMember res;
                if (!_members.TryGetValue(obj, out res))
                {
                    PythonModule mod = obj as PythonModule;
                    if (mod != null)
                    {
                        // FIXME: name
                        object name;
                        if (!mod.Get__dict__().TryGetValue("__name__", out name) || !(name is string))
                        {
                            name = "";
                        }
                        _members[obj] = res = new IronPythonModule(this, mod, (string)name);
                    }

                    PythonType type = obj as PythonType;
                    if (type != null)
                    {
                        _members[obj] = res = GetTypeFromType(type.__clrtype__());
                    }

                    BuiltinFunction func = obj as BuiltinFunction;
                    if (func != null)
                    {
                        _members[obj] = res = new IronPythonBuiltinFunction(this, func);
                    }

                    BuiltinMethodDescriptor methodDesc = obj as BuiltinMethodDescriptor;
                    if (methodDesc != null)
                    {
                        _members[obj] = res = new IronPythonBuiltinMethodDescriptor(this, methodDesc);
                    }

                    ReflectedField field = obj as ReflectedField;
                    if (field != null)
                    {
                        return(new IronPythonField(this, field));
                    }

                    ReflectedProperty prop = obj as ReflectedProperty;
                    if (prop != null)
                    {
                        _members[obj] = res = new IronPythonProperty(this, prop);
                    }

                    ReflectedExtensionProperty extProp = obj as ReflectedExtensionProperty;
                    if (extProp != null)
                    {
                        _members[obj] = res = new IronPythonExtensionProperty(this, extProp);
                    }

                    NamespaceTracker ns = obj as NamespaceTracker;
                    if (ns != null)
                    {
                        _members[obj] = res = new IronPythonNamespace(this, ns);
                    }

                    Method method = obj as Method;
                    if (method != null)
                    {
                        _members[obj] = res = new IronPythonGenericMember(this, method, PythonMemberType.Method);
                    }

                    var classMethod = obj as ClassMethodDescriptor;
                    if (classMethod != null)
                    {
                        _members[obj] = res = new IronPythonGenericMember(this, classMethod, PythonMemberType.Method);
                    }

                    var typeSlot = obj as PythonTypeTypeSlot;
                    if (typeSlot != null)
                    {
                        _members[obj] = res = new IronPythonGenericMember(this, typeSlot, PythonMemberType.Property);
                    }

                    ReflectedEvent eventObj = obj as ReflectedEvent;
                    if (eventObj != null)
                    {
                        return(new IronPythonEvent(this, eventObj));
                    }

                    if (res == null)
                    {
                        var genericTypeSlot = obj as PythonTypeSlot;
                        if (genericTypeSlot != null)
                        {
                            _members[obj] = res = new IronPythonGenericMember(this, genericTypeSlot, PythonMemberType.Property);
                        }
                    }

                    TypeGroup tg = obj as TypeGroup;
                    if (tg != null)
                    {
                        _members[obj] = res = new PythonObject <TypeGroup>(this, tg);
                    }

                    var attrType = (obj != null) ? obj.GetType() : typeof(DynamicNull);
                    if (attrType == typeof(bool) || attrType == typeof(int) || attrType == typeof(Complex) ||
                        attrType == typeof(string) || attrType == typeof(long) || attrType == typeof(double) ||
                        attrType.IsEnum || obj == null)
                    {
                        _members[obj] = res = new IronPythonConstant(this, obj);
                    }

                    if (res == null)
                    {
                        Debug.Assert(!(obj is bool));
                        _members[obj] = res = new PythonObject <object>(this, obj);
                    }
                }

                return(res);
            }
        }
Beispiel #7
0
 public MetaBuiltinMethodDescriptor(Expression /*!*/ expression, BindingRestrictions /*!*/ restrictions, BuiltinMethodDescriptor /*!*/ value)
     : base(expression, BindingRestrictions.Empty, value)
 {
     Assert.NotNull(value);
 }
Beispiel #8
0
 internal DictionaryGetMethod(BuiltinMethodDescriptor method, ProjectState projectState, DictionaryInfo myDict)
     : base(method, projectState)
 {
     _myDict = myDict;
 }