Esempio n. 1
0
        /// <summary>
        /// Gateway into importing ... called from Ops.  This is called after
        /// importing the module and is used to return individual items from
        /// the module.  The outer modules dictionary is then updated with the
        /// result.
        /// </summary>
        public static object ImportFrom(CodeContext /*!*/ context, object from, string name)
        {
            if (from is PythonModule scope)
            {
                object ret;
                if (scope.GetType() == typeof(PythonModule))
                {
                    if (scope.__dict__.TryGetValue(name, out ret))
                    {
                        return(ret);
                    }
                }
                else
                {
                    // subclass of module, it could have overridden __getattr__ or __getattribute__
                    if (PythonOps.TryGetBoundAttr(context, scope, name, out ret))
                    {
                        return(ret);
                    }
                }

                if (scope.__dict__._storage.TryGetPath(out object path))
                {
                    if (path is PythonList listPath)
                    {
                        return(ImportNestedModule(context, scope, new[] { name }, 0, listPath));
                    }
                    if (path is string stringPath)
                    {
                        return(ImportNestedModule(context, scope, new[] { name }, 0, PythonList.FromArrayNoCopy(stringPath)));
                    }
                }
            }
            else if (from is PythonType pt)
            {
                if (pt.TryResolveSlot(context, name, out PythonTypeSlot pts) &&
                    pts.TryGetValue(context, null, pt, out object res))
                {
                    return(res);
                }
            }
            else if (from is NamespaceTracker nt)
            {
                object res = NamespaceTrackerOps.GetCustomMember(context, nt, name);
                if (res != OperationFailed.Value)
                {
                    return(res);
                }
            }
            else
            {
                // This is too lax, for example it allows from module.class import member
                if (PythonOps.TryGetBoundAttr(context, from, name, out object ret))
                {
                    return(ret);
                }
            }

            throw PythonOps.ImportError("Cannot import name {0}", name);
        }
Esempio n. 2
0
            public object GetDict(CallSite site, object self, CodeContext context)
            {
                if (self != null && self.GetType() == typeof(NamespaceTracker))
                {
                    return(NamespaceTrackerOps.Get__dict__(context, (NamespaceTracker)self));
                }

                return(Update(site, self, context));
            }
Esempio n. 3
0
            public object NoThrowTarget(CallSite site, object self, CodeContext context)
            {
                if (self != null && self.GetType() == typeof(NamespaceTracker))
                {
                    return(NamespaceTrackerOps.GetCustomMember(context, (NamespaceTracker)self, _name));
                }

                return(Update(site, self, context));
            }
Esempio n. 4
0
            public object Target(CallSite site, object self, CodeContext context)
            {
                if (self != null && self.GetType() == typeof(NamespaceTracker))
                {
                    object res = NamespaceTrackerOps.GetCustomMember(context, (NamespaceTracker)self, _name);
                    if (res != OperationFailed.Value)
                    {
                        return(res);
                    }

                    throw PythonOps.AttributeErrorForMissingAttribute(self, _name);
                }

                return(Update(site, self, context));
            }
Esempio n. 5
0
        internal object[] GetReflectedNamespaces(IList <string> names, bool bottom)
        {
            if (names == null || names.Count == 0)
            {
                return(null);
            }
            var topName = names[0];

            if (String.IsNullOrEmpty(topName))
            {
                // user typed "import."
                return(null);
            }

            var builtinRefs = new List <object>();

            foreach (var asm in _references)
            {
                object attr = NamespaceTrackerOps.GetCustomMember(_codeContext, asm.Value, topName);
                if (attr != null && attr != OperationFailed.Value)
                {
                    if (bottom)
                    {
                        for (int i = 1; i < names.Count; i++)
                        {
                            if (names[i] != null)
                            {
                                object nextAttr;
                                if (!TryGetMember(attr, names[i], out nextAttr) || nextAttr == null)
                                {
                                    attr = null;
                                    break;
                                }
                                attr = nextAttr;
                            }
                        }
                    }
                    if (attr != null)
                    {
                        builtinRefs.Add(attr);
                    }
                }
            }

            return(builtinRefs.ToArray());
        }
Esempio n. 6
0
        private object GetOne(string index, bool showClr)
        {
            if (IsVisible(index, showClr))
            {
                PythonType pyType = (_obj as PythonType);
                if (pyType != null)
                {
                    foreach (var baseType in pyType.mro())
                    {
                        PythonType curType = (baseType as PythonType);
                        if (curType != null)
                        {
                            IDictionary <object, object> dict = new DictProxy(curType);
                            object bresult;
                            if (dict.TryGetValue(index, out bresult))
                            {
                                return(bresult);
                            }
                        }
                    }
                }

                var tracker = _obj as NamespaceTracker;
                if (tracker != null)
                {
                    object value = NamespaceTrackerOps.GetCustomMember(_interpreter.CodeContext, tracker, index);
                    if (value != OperationFailed.Value)
                    {
                        return(value);
                    }
                    else
                    {
                        return(this);
                    }
                }
                object result;
                if (_interpreter.TryGetMember(_obj, index, showClr, out result))
                {
                    return(result);
                }
            }

            return(this); // sentinel indicating failure
        }
Esempio n. 7
0
        private bool TryGetMember(CodeContext codeContext, object obj, string name, out object value)
        {
            NamespaceTracker nt = obj as NamespaceTracker;

            if (nt != null)
            {
                value = NamespaceTrackerOps.GetCustomMember(codeContext, nt, name);
                return(value != OperationFailed.Value);
            }

            object result = Builtin.getattr(codeContext, obj, name, this);

            if (result == this)
            {
                value = null;
                return(false);
            }
            else
            {
                value = result;
                return(true);
            }
        }
Esempio n. 8
0
        /// <summary>
        /// Gateway into importing ... called from Ops.  This is called after
        /// importing the module and is used to return individual items from
        /// the module.  The outer modules dictionary is then updated with the
        /// result.
        /// </summary>
        public static object ImportFrom(CodeContext /*!*/ context, object from, string name)
        {
            PythonModule     scope = from as PythonModule;
            PythonType       pt;
            NamespaceTracker nt;

            if (scope != null)
            {
                object ret;
                if (scope.GetType() == typeof(PythonModule))
                {
                    if (scope.__dict__.TryGetValue(name, out ret))
                    {
                        return(ret);
                    }
                }
                else
                {
                    // subclass of module, it could have overridden __getattr__ or __getattribute__
                    if (PythonOps.TryGetBoundAttr(context, scope, name, out ret))
                    {
                        return(ret);
                    }
                }

                object path;
                List   listPath;
                if (scope.__dict__._storage.TryGetPath(out path) && (listPath = path as List) != null)
                {
                    return(ImportNestedModule(context, scope, name, listPath));
                }
            }
            else if ((pt = from as PythonType) != null)
            {
                PythonTypeSlot pts;
                object         res;
                if (pt.TryResolveSlot(context, name, out pts) &&
                    pts.TryGetValue(context, null, pt, out res))
                {
                    return(res);
                }
            }
            else if ((nt = from as NamespaceTracker) != null)
            {
                object res = NamespaceTrackerOps.GetCustomMember(context, nt, name);
                if (res != OperationFailed.Value)
                {
                    return(res);
                }
            }
            else
            {
                // This is too lax, for example it allows from module.class import member
                object ret;
                if (PythonOps.TryGetBoundAttr(context, from, name, out ret))
                {
                    return(ret);
                }
            }

            throw PythonOps.ImportError("Cannot import name {0}", name);
        }