PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/DICK.B1/IronPython/Runtime/PythonOptions.cs

https://bitbucket.org/williamybs/uidipythontool
C# | 296 lines | 171 code | 36 blank | 89 comment | 18 complexity | c4f96e174bb80aee673778cd75839bec 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;
  16. using System.Collections.Generic;
  17. using System.Dynamic;
  18. using System.Dynamic.Utils;
  19. using Microsoft.Scripting;
  20. using System.Collections.ObjectModel;
  21. namespace IronPython {
  22. [CLSCompliant(true)]
  23. public enum PythonDivisionOptions {
  24. Old,
  25. New,
  26. Warn,
  27. WarnAll
  28. }
  29. [Serializable, CLSCompliant(true)]
  30. public sealed class PythonOptions : LanguageOptions {
  31. private readonly ReadOnlyCollection<string>/*!*/ _arguments;
  32. private readonly ReadOnlyCollection<string>/*!*/ _warningFilters;
  33. private readonly bool _warnPy3k, _python30;
  34. private readonly bool _bytesWarning;
  35. private readonly bool _debug;
  36. private readonly int _recursionLimit;
  37. private readonly Severity _indentationInconsistencySeverity;
  38. private readonly PythonDivisionOptions _division;
  39. private readonly bool _stripDocStrings;
  40. private readonly bool _optimize;
  41. private readonly bool _inspect;
  42. private readonly bool _noUserSite;
  43. private readonly bool _noSite;
  44. private readonly bool _ignoreEnvironment;
  45. private readonly bool _verbose;
  46. private readonly bool _frames, _fullFrames, _tracing;
  47. private readonly Version _version;
  48. private readonly int? _gcStress;
  49. private bool _enableProfiler;
  50. private readonly bool _lightweightScopes;
  51. /// <summary>
  52. /// Gets the collection of command line arguments.
  53. /// </summary>
  54. public ReadOnlyCollection<string>/*!*/ Arguments {
  55. get { return _arguments; }
  56. }
  57. /// <summary>
  58. /// Should we strip out all doc strings (the -O command line option).
  59. /// </summary>
  60. public bool Optimize {
  61. get { return _optimize; }
  62. }
  63. /// <summary>
  64. /// Should we strip out all doc strings (the -OO command line option).
  65. /// </summary>
  66. public bool StripDocStrings {
  67. get { return _stripDocStrings; }
  68. }
  69. /// <summary>
  70. /// List of -W (warning filter) options collected from the command line.
  71. /// </summary>
  72. public ReadOnlyCollection<string>/*!*/ WarningFilters {
  73. get { return _warningFilters; }
  74. }
  75. /// <summary>
  76. /// Enables warnings related to Python 3.0 features.
  77. /// </summary>
  78. public bool WarnPython30 {
  79. get { return _warnPy3k; }
  80. }
  81. /// <summary>
  82. /// Enables 3.0 features that are implemented in IronPython.
  83. /// </summary>
  84. public bool Python30 {
  85. get {
  86. return _python30;
  87. }
  88. }
  89. public bool BytesWarning {
  90. get { return _bytesWarning; }
  91. }
  92. /// <summary>
  93. /// Enables debugging support. When enabled a .NET debugger can be attached
  94. /// to the process to step through Python code.
  95. /// </summary>
  96. public bool Debug {
  97. get { return _debug; }
  98. }
  99. /// <summary>
  100. /// Enables inspect mode. After running the main module the REPL will be started
  101. /// within that modules context.
  102. /// </summary>
  103. public bool Inspect {
  104. get { return _inspect; }
  105. }
  106. /// <summary>
  107. /// Suppresses addition of the user site directory. This is ignored by IronPython
  108. /// except for updating sys.flags.
  109. /// </summary>
  110. public bool NoUserSite {
  111. get { return _noUserSite; }
  112. }
  113. /// <summary>
  114. /// Disables import site on startup.
  115. /// </summary>
  116. public bool NoSite {
  117. get { return _noSite; }
  118. }
  119. /// <summary>
  120. /// Ignore environment variables that configure the IronPython context.
  121. /// </summary>
  122. public bool IgnoreEnvironment {
  123. get { return _ignoreEnvironment; }
  124. }
  125. /// <summary>
  126. /// Enables the verbose option which traces import statements. This is ignored by IronPython
  127. /// except for setting sys.flags.
  128. /// </summary>
  129. public bool Verbose {
  130. get { return _verbose; }
  131. }
  132. /// <summary>
  133. /// Sets the maximum recursion depth. Setting to Int32.MaxValue will disable recursion
  134. /// enforcement.
  135. /// </summary>
  136. public int RecursionLimit {
  137. get { return _recursionLimit; }
  138. }
  139. /// <summary>
  140. /// Makes available sys._getframe. Local variables will not be available in frames unless the
  141. /// function calls locals(), dir(), vars(), etc... For ensuring locals are always available use
  142. /// the FullFrames option.
  143. /// </summary>
  144. public bool Frames {
  145. get {
  146. return _frames;
  147. }
  148. }
  149. /// <summary>
  150. /// Makes available sys._getframe. All locals variables will live on the heap (for a considerable
  151. /// performance cost) enabling introspection of all code.
  152. /// </summary>
  153. public bool FullFrames {
  154. get {
  155. return _fullFrames;
  156. }
  157. }
  158. /// <summary>
  159. /// Tracing is always available. Without this option tracing is only enabled when sys.settrace
  160. /// is called. This means code that was already running before sys.settrace will not be debuggable.
  161. ///
  162. /// With this option pdb.set_trace and pdb.post_mortem will always work properly.
  163. /// </summary>
  164. public bool Tracing {
  165. get {
  166. return _tracing;
  167. }
  168. }
  169. /// <summary>
  170. /// Severity of a warning that indentation is formatted inconsistently.
  171. /// </summary>
  172. public Severity IndentationInconsistencySeverity {
  173. get { return _indentationInconsistencySeverity; }
  174. }
  175. /// <summary>
  176. /// The division options (old, new, warn, warnall)
  177. /// </summary>
  178. public PythonDivisionOptions DivisionOptions {
  179. get { return _division; }
  180. }
  181. /// <summary>
  182. /// Forces all code to be compiled in a mode in which the code can be reliably collected by the CLR.
  183. /// </summary>
  184. public bool LightweightScopes {
  185. get {
  186. return _lightweightScopes;
  187. }
  188. }
  189. /// <summary>
  190. /// Enable profiling code
  191. /// </summary>
  192. public bool EnableProfiler {
  193. get { return _enableProfiler; }
  194. set { _enableProfiler = value; }
  195. }
  196. public int? GCStress {
  197. get { return _gcStress; }
  198. }
  199. public PythonOptions()
  200. : this(null) {
  201. }
  202. /// <summary>
  203. /// Gets the CPython version which IronPython will emulate. Currently limited
  204. /// to either 2.6 or 3.0.
  205. /// </summary>
  206. public Version PythonVersion {
  207. get {
  208. return _version;
  209. }
  210. }
  211. public PythonOptions(IDictionary<string, object> options)
  212. : base(EnsureSearchPaths(options)) {
  213. _arguments = GetStringCollectionOption(options, "Arguments") ?? EmptyStringCollection;
  214. _warningFilters = GetStringCollectionOption(options, "WarningFilters", ';', ',') ?? EmptyStringCollection;
  215. _warnPy3k = GetOption(options, "WarnPy3k", false);
  216. _bytesWarning = GetOption(options, "BytesWarning", false);
  217. _debug = GetOption(options, "Debug", false);
  218. _inspect = GetOption(options, "Inspect", false);
  219. _noUserSite = GetOption(options, "NoUserSite", false);
  220. _noSite = GetOption(options, "NoSite", false);
  221. _ignoreEnvironment = GetOption(options, "IgnoreEnvironment", false);
  222. _verbose = GetOption(options, "Verbose", false);
  223. _optimize = GetOption(options, "Optimize", false);
  224. _stripDocStrings = GetOption(options, "StripDocStrings", false);
  225. _division = GetOption(options, "DivisionOptions", PythonDivisionOptions.Old);
  226. _recursionLimit = GetOption(options, "RecursionLimit", Int32.MaxValue);
  227. _indentationInconsistencySeverity = GetOption(options, "IndentationInconsistencySeverity", Severity.Ignore);
  228. _enableProfiler = GetOption(options, "EnableProfiler", false);
  229. _lightweightScopes = GetOption(options, "LightweightScopes", false);
  230. _fullFrames = GetOption(options, "FullFrames", false);
  231. _frames = _fullFrames || GetOption(options, "Frames", false);
  232. _gcStress = GetOption<int?>(options, "GCStress", null);
  233. _tracing = GetOption(options, "Tracing", false);
  234. object value;
  235. if (options != null && options.TryGetValue("PythonVersion", out value)) {
  236. if (value is Version) {
  237. _version = (Version)value;
  238. } else if (value is string) {
  239. _version = new Version((string)value);
  240. } else {
  241. throw new ArgumentException("Expected string or Version for PythonVersion");
  242. }
  243. if (_version != new Version(2, 6) && _version != new Version(3, 0)) {
  244. throw new ArgumentException("Expected Version to be 2.6 or 3.0");
  245. }
  246. } else {
  247. _version = new Version(2, 6);
  248. }
  249. _python30 = _version == new Version(3, 0);
  250. }
  251. private static IDictionary<string, object> EnsureSearchPaths(IDictionary<string, object> options) {
  252. if (options == null) {
  253. return new Dictionary<string, object>() { { "SearchPaths", new[] { "." } } };
  254. } else if (!options.ContainsKey("SearchPaths")) {
  255. options["SearchPaths"] = new [] { "." };
  256. }
  257. return options;
  258. }
  259. }
  260. }