PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/IronPython_1_0/Src/IronPython/Runtime/ModuleScope.cs

#
C# | 220 lines | 147 code | 44 blank | 29 comment | 12 complexity | e65a0d816765a0a8fe366b16271ecdc6 MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, CPL-1.0, CC-BY-SA-3.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1, Apache-2.0
  1. /* **********************************************************************************
  2. *
  3. * Copyright (c) Microsoft Corporation. All rights reserved.
  4. *
  5. * This source code is subject to terms and conditions of the Shared Source License
  6. * for IronPython. A copy of the license can be found in the License.html file
  7. * at the root of this distribution. If you can not locate the Shared Source License
  8. * for IronPython, please send an email to ironpy@microsoft.com.
  9. * By using this source code in any fashion, you are agreeing to be bound by
  10. * the terms of the Shared Source License for IronPython.
  11. *
  12. * You must not remove this notice, or any other, from this software.
  13. *
  14. * **********************************************************************************/
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Diagnostics;
  18. using System.Reflection;
  19. using IronPython.Runtime.Types;
  20. using IronPython.Runtime.Calls;
  21. using IronPython.Runtime.Operations;
  22. using IronPython.Compiler;
  23. using IronPython.Modules;
  24. namespace IronPython.Runtime {
  25. /// <summary>
  26. /// PythonModule normally runs code using compiled modules. However, CompiledCode snippet code can also
  27. /// be run in the context of a PythonModule. In such a case, ModuleScope holds the context and scope information
  28. /// of the PythonModule. There can be multiple ModuleScopes corresponding to a single PythonModule.
  29. /// </summary>
  30. public class ModuleScope : IModuleEnvironment, ICloneable {
  31. // This is usually the same as __module__.__dict__.
  32. // It differs for code which does:
  33. // eval("someGlobal==someLocal", { "someGlobal":1 }, { "someLocal":1 })
  34. private IAttributesDictionary f_globals;
  35. private object f_locals; // can be any dictionary or IMapping.
  36. private ReflectedType __builtin__;
  37. public PythonModule __module__;
  38. public List<object> staticData;
  39. internal ModuleScope(PythonModule mod)
  40. : this(mod, mod.__dict__, mod.__dict__) {
  41. }
  42. internal ModuleScope(PythonModule mod, IAttributesDictionary globals, object locals) {
  43. __module__ = mod;
  44. f_globals = globals;
  45. f_locals = locals;
  46. __builtin__ = TypeCache.Builtin;
  47. }
  48. internal ModuleScope(PythonModule mod, IAttributesDictionary globals, object locals, ICallerContext context)
  49. : this(mod, globals, locals) {
  50. this.TrueDivision = context.TrueDivision;
  51. }
  52. public override string ToString() {
  53. return Module.ToString();
  54. }
  55. public object GetLocal(SymbolId symbol) {
  56. object ret;
  57. if (TryGetLocal(symbol, out ret)) { return ret; }
  58. return GetGlobal(symbol);
  59. }
  60. private bool TryGetLocal(SymbolId symbol, out object ret) {
  61. if (f_locals != null) {
  62. // couple of exception-free fast paths...
  63. IAttributesDictionary ad = f_locals as IAttributesDictionary;
  64. if (ad != null) {
  65. return ad.TryGetValue(symbol, out ret);
  66. }
  67. string name = symbol.ToString();
  68. // always check for IMapping first, it does the right thing
  69. // w.r.t. overriding __getitem__ & TryGetValue.
  70. IMapping imap = f_locals as IMapping;
  71. if (imap != null) {
  72. return imap.TryGetValue(name, out ret);
  73. }
  74. IDictionary<object, object> dict = f_locals as IDictionary<object, object>;
  75. if (dict != null) {
  76. return dict.TryGetValue(name, out ret);
  77. }
  78. // uh-oh, we may end up throwing...
  79. try {
  80. ret = Ops.GetIndex(f_locals, name);
  81. return true;
  82. } catch (KeyNotFoundException) {
  83. // return false
  84. }
  85. }
  86. ret = null;
  87. return false;
  88. }
  89. public void DelLocal(SymbolId symbol) {
  90. try {
  91. Ops.DelIndex(f_locals, symbol.ToString());
  92. } catch (KeyNotFoundException) {
  93. throw Ops.NameError("name {0} is not defined", symbol);
  94. }
  95. }
  96. public void SetLocal(SymbolId symbol, object value) {
  97. Ops.SetIndexId(f_locals, symbol, value);
  98. }
  99. #region IModuleEnvironment Members
  100. public virtual object GetGlobal(SymbolId symbol) {
  101. object ret;
  102. if (TryGetGlobal(symbol, out ret)) return ret;
  103. // In theory, we need to check if "__builtins__" has been set by the user
  104. // to some custom module. However, we do not do that for perf reasons.
  105. if (Ops.TryGetAttr(__module__.SystemState.modules["__builtin__"],
  106. symbol,
  107. out ret))
  108. return ret;
  109. throw Ops.NameError("name '{0}' not defined", symbol);
  110. }
  111. public virtual bool TryGetGlobal(SymbolId symbol, out object value) {
  112. return f_globals.TryGetValue(symbol, out value);
  113. }
  114. public virtual void SetGlobal(SymbolId symbol, object value) {
  115. Ops.SetIndexId(f_globals, symbol, value);
  116. }
  117. public virtual void DelGlobal(SymbolId symbol) {
  118. if (!f_globals.Remove(symbol)) {
  119. throw Ops.NameError("name {0} not defined", symbol);
  120. }
  121. }
  122. #endregion
  123. #region ICallerContext Members
  124. public PythonModule Module {
  125. get { return __module__; }
  126. }
  127. public SystemState SystemState {
  128. get {
  129. return ((ICallerContext)__module__).SystemState;
  130. }
  131. }
  132. public object Locals {
  133. get { return f_locals; }
  134. }
  135. public IAttributesDictionary Globals {
  136. get { return f_globals; }
  137. }
  138. public object GetStaticData(int index) {
  139. return staticData[index];
  140. }
  141. public CallerContextAttributes ContextFlags {
  142. get {
  143. return ((ICallerContext)this.__module__).ContextFlags;
  144. }
  145. set {
  146. ((ICallerContext)this.__module__).ContextFlags = value;
  147. }
  148. }
  149. public virtual bool TrueDivision {
  150. get { return ((ICallerContext)__module__).TrueDivision; }
  151. set { ((ICallerContext)__module__).TrueDivision = value; }
  152. }
  153. public CompilerContext CreateCompilerContext() {
  154. CompilerContext context = new CompilerContext();
  155. context.TrueDivision = ((ICallerContext)this).TrueDivision;
  156. return context;
  157. }
  158. #endregion
  159. #region ICloneable Members
  160. public object Clone() {
  161. return MemberwiseClone();
  162. }
  163. #endregion
  164. }
  165. class ExecModuleScope : ModuleScope {
  166. private bool trueDivision;
  167. internal ExecModuleScope(PythonModule mod, IAttributesDictionary globals, object locals, ICallerContext context)
  168. : base(mod, globals, locals) {
  169. trueDivision = context.TrueDivision;
  170. }
  171. public override bool TrueDivision {
  172. get { return trueDivision; }
  173. set { trueDivision = value; }
  174. }
  175. }
  176. }