PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/DICK.B1/IronPython/Runtime/Types/PythonAssemblyOps.cs

https://bitbucket.org/williamybs/uidipythontool
C# | 88 lines | 60 code | 13 blank | 15 comment | 10 complexity | 7a91b8f8676de8db8bd179f130852775 MD5 | raw file
  1. /* ****************************************************************************
  2. *
  3. * Copyright (c) Microsoft Corporation.
  4. *
  5. * This source code is subject to terms and conditions of the Microsoft Public License. A
  6. * copy of the license can be found in the License.html file at the root of this distribution. If
  7. * you cannot locate the Microsoft Public License, please send an email to
  8. * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. * by the terms of the Microsoft Public License.
  10. *
  11. * You must not remove this notice, or any other, from this software.
  12. *
  13. *
  14. * ***************************************************************************/
  15. using System.Collections.Generic;
  16. using System.Diagnostics;
  17. using System.Reflection;
  18. using System.Runtime.CompilerServices;
  19. using IronPython.Runtime.Types;
  20. using Microsoft.Scripting;
  21. using Microsoft.Scripting.Actions;
  22. using Microsoft.Scripting.Runtime;
  23. namespace IronPython.Runtime.Operations {
  24. public static class PythonAssemblyOps {
  25. [MultiRuntimeAware]
  26. private static readonly object _key = new object();
  27. private static Dictionary<Assembly, TopNamespaceTracker> GetAssemblyMap(PythonContext/*!*/ context) {
  28. return context.GetOrCreateModuleState(_key, () => new Dictionary<Assembly, TopNamespaceTracker>());
  29. }
  30. [SpecialName]
  31. public static object GetBoundMember(CodeContext/*!*/ context, Assembly self, string name) {
  32. TopNamespaceTracker reflectedAssembly = GetReflectedAssembly(context, self);
  33. if (name == "__dict__") {
  34. return new PythonDictionary(new WrapperDictionaryStorage(reflectedAssembly));
  35. }
  36. MemberTracker mem = reflectedAssembly.TryGetPackageAny(name);
  37. if (mem != null) {
  38. if (mem.MemberType == TrackerTypes.Type) {
  39. return DynamicHelpers.GetPythonTypeFromType(((TypeTracker)mem).Type);
  40. }
  41. // namespace or type collision
  42. return mem;
  43. }
  44. return OperationFailed.Value;
  45. }
  46. [SpecialName]
  47. public static List GetMemberNames(CodeContext/*!*/ context, Assembly self) {
  48. Debug.Assert(self != null);
  49. List ret = DynamicHelpers.GetPythonTypeFromType(self.GetType()).GetMemberNames(context);
  50. foreach (object o in GetReflectedAssembly(context, self).Keys) {
  51. if (o is string) {
  52. ret.AddNoLock((string)o);
  53. }
  54. }
  55. return ret;
  56. }
  57. public static object __repr__(Assembly self) {
  58. Assembly asmSelf = self as Assembly;
  59. return "<Assembly " + asmSelf.FullName + ">";
  60. }
  61. private static TopNamespaceTracker GetReflectedAssembly(CodeContext/*!*/ context, Assembly assem) {
  62. Debug.Assert(assem != null);
  63. var assemblyMap = GetAssemblyMap(context.LanguageContext as PythonContext);
  64. lock (assemblyMap) {
  65. TopNamespaceTracker reflectedAssembly;
  66. if (assemblyMap.TryGetValue(assem, out reflectedAssembly))
  67. return reflectedAssembly;
  68. reflectedAssembly = new TopNamespaceTracker(context.LanguageContext.DomainManager);
  69. reflectedAssembly.LoadAssembly(assem);
  70. assemblyMap[assem] = reflectedAssembly;
  71. return reflectedAssembly;
  72. }
  73. }
  74. }
  75. }