private static bool ScopeContains(object key, Scope scope) {
     string strKey = key as string;
     if (strKey != null) {
         object dummy;
         if (scope.TryGetVariable(SymbolTable.StringToId(strKey), out dummy)) {
             return true;
         }
     } else {
         object dummy;
         if (scope.TryGetObjectName(key, out dummy)) {
             return true;
         }
     }
     return false;
 }
Beispiel #2
0
 /// <summary>
 /// Given the parent module name looks up the __path__ property.
 /// </summary>
 private static List GetParentPathAndScope(CodeContext/*!*/ context, string/*!*/ parentModuleName, out Scope parentScope) {
     List path = null;
     object parentModule;
     parentScope = null;
     
     // Try lookup parent module in the sys.modules
     if (PythonContext.GetContext(context).SystemStateModules.TryGetValue(parentModuleName, out parentModule)) {
         // see if it's a module
         parentScope = parentModule as Scope;
         if (parentScope != null) {
             object objPath;
             // get its path as a List if it's there
             if (parentScope.TryGetVariable(Symbols.Path, out objPath)) {
                 path = objPath as List;
             }
         }
     }
     return path;
 }
Beispiel #3
0
        private static bool TryGetNestedModule(CodeContext/*!*/ context, Scope/*!*/ scope, string/*!*/ name, out object nested) {
            Assert.NotNull(context, scope, name);

            if (scope.TryGetVariable(SymbolTable.StringToId(name), out nested)) {
                if (nested is Scope) return true;

                // This allows from System.Math import *
                PythonType dt = nested as PythonType;
                if (dt != null && dt.IsSystemType) {
                    return true;
                }
            }
            return false;
        }
Beispiel #4
0
        public static object ScopeMethodMissing(RubyContext/*!*/ context, Scope/*!*/ globalScope, BlockParam block, object self, SymbolId name, object[]/*!*/ args) {
            Assert.NotNull(context, globalScope);

            string str = SymbolTable.IdToString(name);
            if (str.LastCharacter() == '=') {
                if (args.Length != 1) {
                    throw RubyOps.MakeWrongNumberOfArgumentsError(args.Length, 1);
                }

                // Consider this case:
                // There is {"Foo" -> 1} in the scope.
                // x.foo += 1
                // Without name mangling this would result to {"Foo" -> 1, "foo" -> 2} while the expected result is {"Foo" -> 2}.

                str = str.Substring(0, str.Length - 1);
                name = SymbolTable.StringToId(str);

                if (!globalScope.ContainsVariable(name)) {
                    var unmangled = SymbolTable.StringToId(RubyUtils.TryUnmangleName(str));
                    if (!unmangled.IsEmpty && globalScope.ContainsVariable(unmangled)) {
                        name = unmangled;
                    }
                }

                var value = args[0];
                globalScope.SetVariable(name, value);
                return value;
            } else {
                if (args.Length != 0) {
                    throw RubyOps.MakeWrongNumberOfArgumentsError(args.Length, 0);
                }

                object value;
                if (globalScope.TryGetVariable(name, out value)) {
                    return value;
                }

                string unmangled = RubyUtils.TryUnmangleName(str);
                if (unmangled != null && globalScope.TryGetVariable(SymbolTable.StringToId(unmangled), out value)) {
                    return value;
                }

                if (self != null && str == "scope") {
                    return self;
                }
            }

            // TODO: call super
            throw RubyExceptions.CreateMethodMissing(context, self, SymbolTable.IdToString(name));
        }