private static object ImportTopAbsolute(CodeContext /*!*/ context, string /*!*/ name) { object ret; if (TryGetExistingModule(context, name, out ret)) { if (IsReflected(ret)) { // Even though we found something in sys.modules, we need to check if a // clr.AddReference has invalidated it. So try ImportReflected again. ret = ImportReflected(context, name) ?? ret; } NamespaceTracker rp = ret as NamespaceTracker; if (rp != null || ret == PythonContext.GetContext(context).ClrModule) { context.ShowCls = true; } return(ret); } if (TryLoadMetaPathModule(context, name, null, out ret)) { return(ret); } ret = ImportBuiltin(context, name); if (ret != null) { return(ret); } List path; if (PythonContext.GetContext(context).TryGetSystemPath(out path)) { ret = ImportFromPath(context, name, name, path); if (ret != null) { return(ret); } } ret = ImportReflected(context, name); if (ret != null) { return(ret); } return(null); }
private static object ImportModuleFrom(CodeContext /*!*/ context, object from, string[] parts, int current) { PythonModule scope = from as PythonModule; if (scope != null) { object path; List listPath; string stringPath; if (scope.__dict__._storage.TryGetPath(out path)) { if ((listPath = path as List) != null) { return(ImportNestedModule(context, scope, parts, current, listPath)); } else if ((stringPath = path as string) != null) { return(ImportNestedModule(context, scope, parts, current, List.FromArrayNoCopy(stringPath))); } } else { PythonType t = DynamicHelpers.GetPythonType(scope); if (t.TryGetMember(context, scope, "__path__", out path)) { if ((listPath = path as List) != null) { return(ImportNestedModule(context, scope, parts, current, listPath)); } else if ((stringPath = path as string) != null) { return(ImportNestedModule(context, scope, parts, current, List.FromArrayNoCopy(stringPath))); } } } } NamespaceTracker ns = from as NamespaceTracker; if (ns != null) { object val; if (ns.TryGetValue(parts[current], out val)) { return(MemberTrackerToPython(context, val)); } } throw PythonOps.ImportError("No module named {0}", parts[current]); }
public static PythonDictionary Get__dict__(CodeContext context, NamespaceTracker self) { PythonDictionary res = new PythonDictionary(); foreach (var kvp in self) { if (kvp.Value is TypeGroup || kvp.Value is NamespaceTracker) { res[kvp.Key] = kvp.Value; } else { res[kvp.Key] = DynamicHelpers.GetPythonTypeFromType(((TypeTracker)kvp.Value).Type); } } return(res); }
public static object GetCustomMember(CodeContext /*!*/ context, NamespaceTracker /*!*/ self, string name) { if (self.TryGetValue(name, out MemberTracker mt)) { if (mt.MemberType == TrackerTypes.Namespace || mt.MemberType == TrackerTypes.TypeGroup) { return(mt); } PythonTypeSlot pts = PythonTypeOps.GetSlot(new MemberGroup(mt), name, context.LanguageContext.Binder.PrivateBinding); if (pts != null && pts.TryGetValue(context, null, TypeCache.PythonType, out object value)) { return(value); } } return(OperationFailed.Value); }
public static object Get__file__(NamespaceTracker self) { if (self.PackageAssemblies.Count == 1) { return(self.PackageAssemblies[0].FullName); } StringBuilder res = new StringBuilder(); for (int i = 0; i < self.PackageAssemblies.Count; i++) { if (i != 0) { res.Append(", "); } res.Append(self.PackageAssemblies[i].FullName); } return(res.ToString()); }
internal static IList <string> DirHelper(object obj, bool showClr) { NamespaceTracker nt = obj as NamespaceTracker; if (nt != null) { return(nt.GetMemberNames()); } var dir = showClr ? ClrModule.DirClr(obj) : ClrModule.Dir(obj); int len = dir.__len__(); string[] result = new string[len]; for (int i = 0; i < len; i++) { // TODO: validate result[i] = dir[i] as string; } return(result); }
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); } }
internal static ExtensionMethodSet AddExtensions(ExtensionMethodSet extensionMethodSet, object[] extensions) { var res = extensionMethodSet; var newTypes = new HashSet <Type>(); var newAssms = new HashSet <Assembly>(); var newNspaces = new HashSet <NamespaceTracker>(); foreach (object o in extensions) { PythonType type = o as PythonType; if (type != null) { if (res._types != null && res._types.Contains(type)) { continue; } newTypes.Add(type.UnderlyingSystemType); } Assembly asm = o as Assembly; if (asm != null) { if (res._assemblies != null && res._assemblies.Contains(asm)) { continue; } foreach (var method in ReflectionUtils.GetVisibleExtensionMethods(asm)) { if (newTypes.Contains(method.DeclaringType) || (res._types != null && res._types.Contains(method.DeclaringType))) { continue; } newTypes.Add(method.DeclaringType); } } NamespaceTracker ns = o as NamespaceTracker; if (ns != null) { if (res._namespaces != null && res._namespaces.Contains(ns)) { continue; } foreach (var packageAsm in ns.PackageAssemblies) { foreach (var method in ReflectionUtils.GetVisibleExtensionMethods(packageAsm)) { if (newTypes.Contains(method.DeclaringType) || (res._types != null && res._types.Contains(method.DeclaringType))) { continue; } newTypes.Add(method.DeclaringType); } } } } if (newTypes.Count > 0) { if (res._types != null) { newTypes.UnionWith(res._types); } if (res._namespaces != null) { newNspaces.UnionWith(res._namespaces); } if (res._assemblies != null) { newAssms.UnionWith(res._assemblies); } return(new ExtensionMethodSet(newTypes, newNspaces, newAssms)); } return(res); }
public static ExtensionMethodSet AddNamespace(PythonContext context, ExtensionMethodSet /*!*/ existingSet, NamespaceTracker /*!*/ ns) { Assert.NotNull(existingSet, ns); lock (existingSet) { AssemblyLoadInfo asmInfo; Dictionary <Assembly, AssemblyLoadInfo> newDict = null; foreach (var assembly in ns.PackageAssemblies) { if (existingSet != null && existingSet._loadedAssemblies.TryGetValue(assembly, out asmInfo)) { if (asmInfo.IsFullAssemblyLoaded) { // full assembly is already in this set. continue; } if (asmInfo.Namespaces == null || !asmInfo.Namespaces.Contains(ns.Name)) { if (newDict == null) { newDict = NewInfoOrCopy(existingSet); } if (newDict[assembly].Namespaces == null) { newDict[assembly].Namespaces = new HashSet <string>(); } newDict[assembly].Namespaces.Add(ns.Name); } } else { if (newDict == null) { newDict = NewInfoOrCopy(existingSet); } var newAsmInfo = newDict[assembly] = new AssemblyLoadInfo(assembly); newAsmInfo.Namespaces = new HashSet <string>(); newAsmInfo.Namespaces.Add(ns.Name); } } if (newDict != null) { return(context.UniqifyExtensions(new ExtensionMethodSet(newDict))); } return(existingSet); } }
public static string Get__name__(CodeContext context, NamespaceTracker self) { return(Get__name__(self.Name)); }
public static string __repr__(NamespaceTracker self) { return(__str__(self)); }
public NamespaceExpression(NamespaceTracker tracker, SourceSpan span) { _tracker = tracker; ExpressionClass = ExpressionClass.Namespace; Span = span; }
public override ICollection <MemberDoc> GetMembers(object value) { List <MemberDoc> res = new List <MemberDoc>(); PythonModule mod = value as PythonModule; if (mod != null) { foreach (var kvp in mod.__dict__) { AddMember(res, kvp, false); } return(res); } NamespaceTracker ns = value as NamespaceTracker; if (ns != null) { foreach (var v in ns) { AddMember( res, new KeyValuePair <object, object>( v.Key, Importer.MemberTrackerToPython(_context.SharedClsContext, v.Value) ), false ); } } else { OldInstance oi = value as OldInstance; if (oi != null) { foreach (var member in oi.Dictionary) { AddMember(res, member, false); } AddOldClassMembers(res, oi._class); } else { PythonType pt = value as PythonType; if (pt != null) { foreach (PythonType type in pt.ResolutionOrder) { foreach (var member in type.GetMemberDictionary(_context.SharedContext)) { AddMember(res, member, true); } } } else { OldClass oc = value as OldClass; if (oc != null) { AddOldClassMembers(res, oc); } else { pt = DynamicHelpers.GetPythonType(value); foreach (var member in pt.GetMemberDictionary(_context.SharedContext)) { AddMember(res, member, true); } } } IPythonObject ipo = value as IPythonObject; if (ipo != null && ipo.Dict != null) { foreach (var member in ipo.Dict) { AddMember(res, member, false); } } } } return(res.ToArray()); }
private FullNamedExpression LookupNamespaceOrType(NamespaceTracker ns, string name, SourceSpan location, bool ignoreAmbiguousReferences, int genericArity = 0) { MemberTracker tracker; FullNamedExpression fne = null; bool found; var trackerGroup = ns as NamespaceGroupTracker; if (trackerGroup != null) { found = trackerGroup.TryGetValue((SymbolId)name, out tracker); } else { found = ns.TryGetValue((SymbolId)name, out tracker); } if (found) { var namespaceTracker = tracker as NamespaceTracker; if (namespaceTracker != null) { fne = new NamespaceExpression(namespaceTracker); } else { var typeGroup = tracker as TypeGroup; if (typeGroup != null) { fne = new TypeExpression(typeGroup.GetTypeForArity(genericArity).Type, location); } else { fne = new TypeExpression(((TypeTracker)tracker).Type); } } } if (fne != null) { return(fne); } // // Check using entries. // var conflicts = _importedNamespaces .Select(importedNamespace => LookupNamespaceOrType(importedNamespace, name, location, true)) .Where(match => (match != null) && (match is TypeExpression)); foreach (var conflict in conflicts) { if (fne != null) { if (!ignoreAmbiguousReferences) { OnErrorAmbiguousTypeReference(location, name, fne, conflict); } return(null); } fne = conflict; } return(fne); }
public NamespaceExpression(NamespaceTracker tracker) : this(tracker, SourceSpan.None) { }
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); } }
private static object ImportTopAbsolute(CodeContext /*!*/ context, string /*!*/ name) { //Debug.WriteLine("Importing step 4: " + name); object ret; //Debug.WriteLine("TryGetExistingModule"); if (TryGetExistingModule(context, name, out ret)) { //Debug.WriteLine("Getting existing module"); if (IsReflected(ret)) { // Even though we found something in sys.modules, we need to check if a // clr.AddReference has invalidated it. So try ImportReflected again. ret = ImportReflected(context, name) ?? ret; //Debug.WriteLine("Import refelected"); } NamespaceTracker rp = ret as NamespaceTracker; if (rp != null || ret == PythonContext.GetContext(context).ClrModule) { context.ShowCls = true; } //Debug.WriteLine("Got existing module"); return(ret); } //Debug.WriteLine("TryLoadMetaPathModule"); if (TryLoadMetaPathModule(context, name, null, out ret)) { //Debug.WriteLine("LoadMetaPathModule"); return(ret); } //Debug.WriteLine("Importing builtin"); ret = ImportBuiltin(context, name); if (ret != null) { return(ret); /*Debug.WriteLine("Imported builtin");*/ } List path; //Debug.WriteLine("Trygetsyspath"); if (PythonContext.GetContext(context).TryGetSystemPath(out path)) { //Debug.WriteLine("Importing from path"); ret = ImportFromPath(context, name, name, path); if (ret != null) { return(ret); /*Debug.WriteLine("Imported from path");*/ } } //Debug.WriteLine("Importing refelcted"); ret = ImportReflected(context, name); if (ret != null) { return(ret); /*Debug.WriteLine("Imported reflected");*/ } //Debug.WriteLine("Returning null"); return(null); }