PageRenderTime 42ms CodeModel.GetById 8ms RepoModel.GetById 1ms app.codeStats 0ms

/Languages/IronPython/IronPython/Modules/sys.cs

http://github.com/IronLanguages/main
C# | 838 lines | 630 code | 160 blank | 48 comment | 40 complexity | 139fdbf6747d96d96c0cb32126cbd685 MD5 | raw file
Possible License(s): CPL-1.0, BSD-3-Clause, ISC, GPL-2.0, MPL-2.0-no-copyleft-exception
  1. /* ****************************************************************************
  2. *
  3. * Copyright (c) Microsoft Corporation.
  4. *
  5. * This source code is subject to terms and conditions of the Apache License, Version 2.0. 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 Apache License, Version 2.0, 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 Apache License, Version 2.0.
  10. *
  11. * You must not remove this notice, or any other, from this software.
  12. *
  13. *
  14. * ***************************************************************************/
  15. #if FEATURE_NUMERICS
  16. using System.Numerics;
  17. #else
  18. using Microsoft.Scripting.Math;
  19. #endif
  20. using System;
  21. using System.Collections;
  22. using System.Collections.Generic;
  23. using System.IO;
  24. using System.Reflection;
  25. using System.Runtime.CompilerServices;
  26. using System.Security;
  27. using System.Text;
  28. using Microsoft.Scripting.Runtime;
  29. using IronPython.Runtime;
  30. using IronPython.Runtime.Exceptions;
  31. using IronPython.Runtime.Operations;
  32. using IronPython.Runtime.Types;
  33. using Microsoft.Scripting.Utils;
  34. #if NETCOREAPP1_0
  35. using Environment = System.FakeEnvironment;
  36. #endif
  37. [assembly: PythonModule("sys", typeof(IronPython.Modules.SysModule))]
  38. namespace IronPython.Modules {
  39. public static class SysModule {
  40. public const string __doc__ = "Provides access to functions which query or manipulate the Python runtime.";
  41. public const int api_version = 0;
  42. // argv is set by PythonContext and only on the initial load
  43. public static readonly string byteorder = BitConverter.IsLittleEndian ? "little" : "big";
  44. // builtin_module_names is set by PythonContext and updated on reload
  45. public const string copyright = "Copyright (c) IronPython Team";
  46. private static string GetPrefix() {
  47. string prefix;
  48. #if FEATURE_ASSEMBLY_LOCATION
  49. try {
  50. prefix = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
  51. } catch (SecurityException) {
  52. prefix = String.Empty;
  53. } catch (ArgumentException) {
  54. prefix = String.Empty;
  55. } catch (MethodAccessException) {
  56. prefix = String.Empty;
  57. }
  58. #else
  59. prefix = String.Empty;
  60. #endif
  61. return prefix;
  62. }
  63. /// <summary>
  64. /// Returns detailed call statistics. Not implemented in IronPython and always returns None.
  65. /// </summary>
  66. public static object callstats() {
  67. return null;
  68. }
  69. /// <summary>
  70. /// Handles output of the expression statement.
  71. /// Prints the value and sets the __builtin__._
  72. /// </summary>
  73. [PythonHidden]
  74. [Documentation(@"displayhook(object) -> None
  75. Print an object to sys.stdout and also save it in __builtin__._")]
  76. public static void displayhookImpl(CodeContext/*!*/ context, object value) {
  77. if (value != null) {
  78. PythonOps.Print(context, PythonOps.Repr(context, value));
  79. PythonContext.GetContext(context).BuiltinModuleDict["_"] = value;
  80. }
  81. }
  82. public static BuiltinFunction displayhook = BuiltinFunction.MakeFunction(
  83. "displayhook",
  84. ArrayUtils.ConvertAll(typeof(SysModule).GetMember("displayhookImpl"), (x) => (MethodBase)x),
  85. typeof(SysModule)
  86. );
  87. public static readonly BuiltinFunction __displayhook__ = displayhook;
  88. public const int dllhandle = 0;
  89. [PythonHidden]
  90. [Documentation(@"excepthook(exctype, value, traceback) -> None
  91. Handle an exception by displaying it with a traceback on sys.stderr._")]
  92. public static void excepthookImpl(CodeContext/*!*/ context, object exctype, object value, object traceback) {
  93. PythonContext pc = PythonContext.GetContext(context);
  94. PythonOps.PrintWithDest(
  95. context,
  96. pc.SystemStandardError,
  97. pc.FormatException(PythonExceptions.ToClr(value))
  98. );
  99. }
  100. public static readonly BuiltinFunction excepthook = BuiltinFunction.MakeFunction(
  101. "excepthook",
  102. ArrayUtils.ConvertAll(typeof(SysModule).GetMember("excepthookImpl"), (x) => (MethodBase)x),
  103. typeof(SysModule)
  104. );
  105. public static readonly BuiltinFunction __excepthook__ = excepthook;
  106. public static int getcheckinterval() {
  107. throw PythonOps.NotImplementedError("IronPython does not support sys.getcheckinterval");
  108. }
  109. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "value")]
  110. public static void setcheckinterval(int value) {
  111. throw PythonOps.NotImplementedError("IronPython does not support sys.setcheckinterval");
  112. }
  113. public static int getrefcount(CodeContext/*!*/ context, object o) {
  114. // getrefcount() is used at various places in the CPython test suite, usually to
  115. // check that instances are not cloned. Under .NET, we cannot provide that functionality,
  116. // but we can at least return a dummy result so that the tests can continue.
  117. PythonOps.Warn(context, PythonExceptions.RuntimeWarning, "IronPython does not support sys.getrefcount. A dummy result is returned.");
  118. return 1000000;
  119. }
  120. // warnoptions is set by PythonContext and updated on each reload
  121. [Python3Warning("sys.exc_clear() not supported in 3.x; use except clauses")]
  122. public static void exc_clear() {
  123. PythonOps.ClearCurrentException();
  124. }
  125. public static PythonTuple exc_info(CodeContext/*!*/ context) {
  126. return PythonOps.GetExceptionInfo(context);
  127. }
  128. // exec_prefix and executable are set by PythonContext and updated on each reload
  129. public static void exit() {
  130. exit(null);
  131. }
  132. public static void exit(object code) {
  133. if (code == null) {
  134. throw new PythonExceptions._SystemExit().InitAndGetClrException();
  135. } else {
  136. PythonTuple pt = code as PythonTuple;
  137. if (pt != null && pt.__len__() == 1) {
  138. code = pt[0];
  139. }
  140. // throw as a python exception here to get the args set.
  141. throw new PythonExceptions._SystemExit().InitAndGetClrException(code);
  142. }
  143. }
  144. public static string getdefaultencoding(CodeContext/*!*/ context) {
  145. return PythonContext.GetContext(context).GetDefaultEncodingName();
  146. }
  147. public static object getfilesystemencoding() {
  148. if(Environment.OSVersion.Platform == PlatformID.Unix)
  149. return "utf-8";
  150. return "mbcs";
  151. }
  152. [PythonHidden]
  153. public static TraceBackFrame/*!*/ _getframeImpl(CodeContext/*!*/ context) {
  154. return _getframeImpl(context, 0);
  155. }
  156. [PythonHidden]
  157. public static TraceBackFrame/*!*/ _getframeImpl(CodeContext/*!*/ context, int depth) {
  158. return _getframeImpl(context, depth, PythonOps.GetFunctionStack());
  159. }
  160. internal static TraceBackFrame/*!*/ _getframeImpl(CodeContext/*!*/ context, int depth, List<FunctionStack> stack) {
  161. if (depth < stack.Count) {
  162. TraceBackFrame cur = null;
  163. for (int i = 0; i < stack.Count - depth; i++) {
  164. var elem = stack[i];
  165. if (elem.Frame != null) {
  166. // we previously handed out a frame here, hand out the same one now
  167. cur = elem.Frame;
  168. } else {
  169. // create a new frame and save it for future calls
  170. cur = new TraceBackFrame(
  171. context,
  172. Builtin.globals(elem.Context),
  173. Builtin.locals(elem.Context),
  174. elem.Code,
  175. cur
  176. );
  177. stack[i] = new FunctionStack(elem.Context, elem.Code, cur);
  178. }
  179. }
  180. return cur;
  181. }
  182. throw PythonOps.ValueError("call stack is not deep enough");
  183. }
  184. public static int getsizeof(object o) {
  185. return ObjectOps.__sizeof__(o);
  186. }
  187. public static PythonTuple getwindowsversion() {
  188. var osVer = Environment.OSVersion;
  189. return new windows_version(
  190. osVer.Version.Major,
  191. osVer.Version.Minor,
  192. osVer.Version.Build,
  193. (int)osVer.Platform
  194. #if FEATURE_OS_SERVICEPACK
  195. , osVer.ServicePack
  196. #else
  197. , ""
  198. #endif
  199. );
  200. }
  201. [PythonType("sys.getwindowsversion"), PythonHidden]
  202. public class windows_version : PythonTuple {
  203. internal windows_version(int major, int minor, int build, int platform, string service_pack)
  204. : base(new object[] { major, minor, build, platform, service_pack }) {
  205. this.major = major;
  206. this.minor = minor;
  207. this.build = build;
  208. this.platform = platform;
  209. this.service_pack = service_pack;
  210. }
  211. public readonly int major;
  212. public readonly int minor;
  213. public readonly int build;
  214. public readonly int platform;
  215. public readonly string service_pack;
  216. public const int n_fields = 5;
  217. public const int n_sequence_fields = 5;
  218. public const int n_unnamed_fields = 0;
  219. public override string __repr__(CodeContext context) {
  220. return string.Format("sys.getwindowsversion(major={0}, minor={1}, build={2}, platform={3}, service_pack='{4}')",
  221. this.major, this.minor, this.build, this.platform, this.service_pack);
  222. }
  223. }
  224. // hex_version is set by PythonContext
  225. public const int maxint = Int32.MaxValue;
  226. public const int maxsize = Int32.MaxValue;
  227. public const int maxunicode = (int)ushort.MaxValue;
  228. // modules is set by PythonContext and only on the initial load
  229. // path is set by PythonContext and only on the initial load
  230. #if !SILVERLIGHT
  231. public const string platform = "cli";
  232. #else
  233. public const string platform = "silverlight";
  234. #endif
  235. public static readonly string prefix = GetPrefix();
  236. // ps1 and ps2 are set by PythonContext and only on the initial load
  237. public static void setdefaultencoding(CodeContext context, object name) {
  238. if (name == null) throw PythonOps.TypeError("name cannot be None");
  239. string strName = name as string;
  240. if (strName == null) throw PythonOps.TypeError("name must be a string");
  241. PythonContext pc = PythonContext.GetContext(context);
  242. Encoding enc;
  243. if (!StringOps.TryGetEncoding(strName, out enc)) {
  244. throw PythonOps.LookupError("'{0}' does not match any available encodings", strName);
  245. }
  246. pc.DefaultEncoding = enc;
  247. }
  248. #if PROFILE_SUPPORT
  249. // not enabled because we don't yet support tracing built-in functions. Doing so is a little
  250. // difficult because it's hard to flip tracing on/off for them w/o a perf overhead in the
  251. // non-profiling case.
  252. public static void setprofile(CodeContext/*!*/ context, TracebackDelegate o) {
  253. PythonContext pyContext = PythonContext.GetContext(context);
  254. pyContext.EnsureDebugContext();
  255. if (o == null) {
  256. pyContext.UnregisterTracebackHandler();
  257. } else {
  258. pyContext.RegisterTracebackHandler();
  259. }
  260. // Register the trace func with the listener
  261. pyContext.TracebackListener.SetProfile(o);
  262. }
  263. #endif
  264. public static void settrace(CodeContext/*!*/ context, object o) {
  265. context.LanguageContext.SetTrace(o);
  266. }
  267. public static object call_tracing(CodeContext/*!*/ context, object func, PythonTuple args) {
  268. return context.LanguageContext.CallTracing(func, args);
  269. }
  270. public static object gettrace(CodeContext/*!*/ context) {
  271. return context.LanguageContext.GetTrace();
  272. }
  273. public static void setrecursionlimit(CodeContext/*!*/ context, int limit) {
  274. PythonContext.GetContext(context).RecursionLimit = limit;
  275. }
  276. public static int getrecursionlimit(CodeContext/*!*/ context) {
  277. return PythonContext.GetContext(context).RecursionLimit;
  278. }
  279. // stdin, stdout, stderr, __stdin__, __stdout__, and __stderr__ added by PythonContext
  280. // version and version_info are set by PythonContext
  281. public static PythonTuple subversion = PythonTuple.MakeTuple("IronPython", "", "");
  282. public const string winver = CurrentVersion.Series;
  283. #region Special types
  284. [PythonHidden, PythonType("flags"), DontMapIEnumerableToIter]
  285. public sealed class SysFlags : IList<object> {
  286. private const string _className = "sys.flags";
  287. internal SysFlags() { }
  288. private const int INDEX_DEBUG = 0;
  289. private const int INDEX_PY3K_WARNING = 1;
  290. private const int INDEX_DIVISION_WARNING = 2;
  291. private const int INDEX_DIVISION_NEW = 3;
  292. private const int INDEX_INSPECT = 4;
  293. private const int INDEX_INTERACTIVE = 5;
  294. private const int INDEX_OPTIMIZE = 6;
  295. private const int INDEX_DONT_WRITE_BYTECODE = 7;
  296. private const int INDEX_NO_USER_SITE = 8;
  297. private const int INDEX_NO_SITE = 9;
  298. private const int INDEX_IGNORE_ENVIRONMENT = 10;
  299. private const int INDEX_TABCHECK = 11;
  300. private const int INDEX_VERBOSE = 12;
  301. private const int INDEX_UNICODE = 13;
  302. private const int INDEX_BYTES_WARNING = 14;
  303. public const int n_fields = 15;
  304. public const int n_sequence_fields = 15;
  305. public const int n_unnamed_fields = 0;
  306. private static readonly string[] _keys = new string[] {
  307. "debug", "py3k_warning", "division_warning", "division_new", "inspect",
  308. "interactive", "optimize", "dont_write_bytecode", "no_user_site", "no_site",
  309. "ignore_environment", "tabcheck", "verbose", "unicode", "bytes_warning"
  310. };
  311. private object[] _values = new object[n_fields] {
  312. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  313. };
  314. private PythonTuple __tuple = null;
  315. private PythonTuple _tuple {
  316. get {
  317. _Refresh();
  318. return __tuple;
  319. }
  320. }
  321. private string __string = null;
  322. private string _string {
  323. get {
  324. _Refresh();
  325. return __string;
  326. }
  327. }
  328. public override string ToString() {
  329. return _string;
  330. }
  331. public string __repr__() {
  332. return _string;
  333. }
  334. private bool _modified = true;
  335. private void _Refresh() {
  336. if (_modified) {
  337. __tuple = PythonTuple.MakeTuple(_values);
  338. StringBuilder sb = new StringBuilder("sys.flags(");
  339. for (int i = 0; i < n_fields; i++) {
  340. if (_keys[i] == null) {
  341. sb.Append(_values[i]);
  342. } else {
  343. sb.AppendFormat("{0}={1}", _keys[i], _values[i]);
  344. }
  345. if (i < n_fields - 1) {
  346. sb.Append(", ");
  347. } else {
  348. sb.Append(')');
  349. }
  350. }
  351. __string = sb.ToString();
  352. _modified = false;
  353. }
  354. }
  355. private int _GetVal(int index) {
  356. return (int)_values[index];
  357. }
  358. private void _SetVal(int index, int value) {
  359. if ((int)_values[index] != value) {
  360. _modified = true;
  361. _values[index] = value;
  362. }
  363. }
  364. #region ICollection<object> Members
  365. void ICollection<object>.Add(object item) {
  366. throw new InvalidOperationException(_className + " is readonly");
  367. }
  368. void ICollection<object>.Clear() {
  369. throw new InvalidOperationException(_className + " is readonly");
  370. }
  371. [PythonHidden]
  372. public bool Contains(object item) {
  373. return _tuple.Contains(item);
  374. }
  375. [PythonHidden]
  376. public void CopyTo(object[] array, int arrayIndex) {
  377. _tuple.CopyTo(array, arrayIndex);
  378. }
  379. public int Count {
  380. [PythonHidden]
  381. get {
  382. return n_fields;
  383. }
  384. }
  385. bool ICollection<object>.IsReadOnly {
  386. get { return true; }
  387. }
  388. bool ICollection<object>.Remove(object item) {
  389. throw new InvalidOperationException(_className + " is readonly");
  390. }
  391. #endregion
  392. #region IEnumerable Members
  393. [PythonHidden]
  394. public IEnumerator GetEnumerator() {
  395. return _tuple.GetEnumerator();
  396. }
  397. #endregion
  398. #region IEnumerable<object> Members
  399. IEnumerator<object> IEnumerable<object>.GetEnumerator() {
  400. return ((IEnumerable<object>)_tuple).GetEnumerator();
  401. }
  402. #endregion
  403. #region ISequence Members
  404. public int __len__() {
  405. return n_fields;
  406. }
  407. public object this[int i] {
  408. get {
  409. return _tuple[i];
  410. }
  411. }
  412. public object this[BigInteger i] {
  413. get {
  414. return this[(int)i];
  415. }
  416. }
  417. public object __getslice__(int start, int end) {
  418. return _tuple.__getslice__(start, end);
  419. }
  420. public object this[Slice s] {
  421. get {
  422. return _tuple[s];
  423. }
  424. }
  425. public object this[object o] {
  426. get {
  427. return this[Converter.ConvertToIndex(o)];
  428. }
  429. }
  430. #endregion
  431. #region IList<object> Members
  432. [PythonHidden]
  433. public int IndexOf(object item) {
  434. return _tuple.IndexOf(item);
  435. }
  436. void IList<object>.Insert(int index, object item) {
  437. throw new InvalidOperationException(_className + " is readonly");
  438. }
  439. void IList<object>.RemoveAt(int index) {
  440. throw new InvalidOperationException(_className + " is readonly");
  441. }
  442. object IList<object>.this[int index] {
  443. get {
  444. return _tuple[index];
  445. }
  446. set {
  447. throw new InvalidOperationException(_className + " is readonly");
  448. }
  449. }
  450. #endregion
  451. #region binary ops
  452. public static PythonTuple operator +([NotNull]SysFlags f, [NotNull]PythonTuple t) {
  453. return f._tuple + t;
  454. }
  455. public static PythonTuple operator *([NotNull]SysFlags f, int n) {
  456. return f._tuple * n;
  457. }
  458. public static PythonTuple operator *(int n, [NotNull]SysFlags f) {
  459. return f._tuple * n;
  460. }
  461. public static object operator *([NotNull]SysFlags f, [NotNull]Index n) {
  462. return f._tuple * n;
  463. }
  464. public static object operator *([NotNull]Index n, [NotNull]SysFlags f) {
  465. return f._tuple * n;
  466. }
  467. public static object operator *([NotNull]SysFlags f, object n) {
  468. return f._tuple * n;
  469. }
  470. public static object operator *(object n, [NotNull]SysFlags f) {
  471. return f._tuple * n;
  472. }
  473. #endregion
  474. # region comparison and hashing methods
  475. public static bool operator >(SysFlags f, PythonTuple t) {
  476. return f._tuple > t;
  477. }
  478. public static bool operator <(SysFlags f, PythonTuple t) {
  479. return f._tuple < t;
  480. }
  481. public static bool operator >=(SysFlags f, PythonTuple t) {
  482. return f._tuple >= t;
  483. }
  484. public static bool operator <=(SysFlags f, PythonTuple t) {
  485. return f._tuple <= t;
  486. }
  487. public override bool Equals(object obj) {
  488. if (obj is SysFlags) {
  489. return _tuple.Equals(((SysFlags)obj)._tuple);
  490. }
  491. return _tuple.Equals(obj);
  492. }
  493. public override int GetHashCode() {
  494. return _tuple.GetHashCode();
  495. }
  496. # endregion
  497. #region sys.flags API
  498. public int debug {
  499. get { return _GetVal(INDEX_DEBUG); }
  500. internal set { _SetVal(INDEX_DEBUG, value); }
  501. }
  502. public int py3k_warning {
  503. get { return _GetVal(INDEX_PY3K_WARNING); }
  504. internal set { _SetVal(INDEX_PY3K_WARNING, value); }
  505. }
  506. public int division_warning {
  507. get { return _GetVal(INDEX_DIVISION_WARNING); }
  508. internal set { _SetVal(INDEX_DIVISION_WARNING, value); }
  509. }
  510. public int division_new {
  511. get { return _GetVal(INDEX_DIVISION_NEW); }
  512. internal set { _SetVal(INDEX_DIVISION_NEW, value); }
  513. }
  514. public int inspect {
  515. get { return _GetVal(INDEX_INSPECT); }
  516. internal set { _SetVal(INDEX_INSPECT, value); }
  517. }
  518. public int interactive {
  519. get { return _GetVal(INDEX_INTERACTIVE); }
  520. internal set { _SetVal(INDEX_INTERACTIVE, value); }
  521. }
  522. public int optimize {
  523. get { return _GetVal(INDEX_OPTIMIZE); }
  524. internal set { _SetVal(INDEX_OPTIMIZE, value); }
  525. }
  526. public int dont_write_bytecode {
  527. get { return _GetVal(INDEX_DONT_WRITE_BYTECODE); }
  528. internal set { _SetVal(INDEX_DONT_WRITE_BYTECODE, value); }
  529. }
  530. public int no_user_site {
  531. get { return _GetVal(INDEX_NO_USER_SITE); }
  532. internal set { _SetVal(INDEX_NO_USER_SITE, value); }
  533. }
  534. public int no_site {
  535. get { return _GetVal(INDEX_NO_SITE); }
  536. internal set { _SetVal(INDEX_NO_SITE, value); }
  537. }
  538. public int ignore_environment {
  539. get { return _GetVal(INDEX_IGNORE_ENVIRONMENT); }
  540. internal set { _SetVal(INDEX_IGNORE_ENVIRONMENT, value); }
  541. }
  542. public int tabcheck {
  543. get { return _GetVal(INDEX_TABCHECK); }
  544. internal set { _SetVal(INDEX_TABCHECK, value); }
  545. }
  546. public int verbose {
  547. get { return _GetVal(INDEX_VERBOSE); }
  548. internal set { _SetVal(INDEX_VERBOSE, value); }
  549. }
  550. public int unicode {
  551. get { return _GetVal(INDEX_UNICODE); }
  552. internal set { _SetVal(INDEX_UNICODE, value); }
  553. }
  554. public int bytes_warning {
  555. get { return _GetVal(INDEX_BYTES_WARNING); }
  556. internal set { _SetVal(INDEX_BYTES_WARNING, value); }
  557. }
  558. #endregion
  559. }
  560. #endregion
  561. // These values are based on the .NET 2 BigInteger in Microsoft.Scripting.Math
  562. public static longinfo long_info = new longinfo(32, 4);
  563. [PythonType("sys.long_info"), PythonHidden]
  564. public class longinfo : PythonTuple {
  565. internal longinfo(int bits_per_digit, int sizeof_digit)
  566. : base(new object[] {bits_per_digit, sizeof_digit}) {
  567. this.bits_per_digit = bits_per_digit;
  568. this.sizeof_digit = sizeof_digit;
  569. }
  570. public readonly int bits_per_digit;
  571. public readonly int sizeof_digit;
  572. public const int n_fields = 2;
  573. public const int n_sequence_fields = 2;
  574. public const int n_unnamed_fields = 0;
  575. public override string __repr__(CodeContext context) {
  576. return string.Format("sys.long_info(bits_per_digit={0}, sizeof_digit={1})",
  577. this.bits_per_digit, this.sizeof_digit);
  578. }
  579. }
  580. public static floatinfo float_info = new floatinfo(
  581. Double.MaxValue, // DBL_MAX
  582. 1024, // DBL_MAX_EXP
  583. 308, // DBL_MAX_10_EXP
  584. // DBL_MIN
  585. BitConverter.Int64BitsToDouble(BitConverter.IsLittleEndian ? 0x0010000000000000 : 0x0000000000001000),
  586. -1021, // DBL_MIN_EXP
  587. -307, // DBL_MIN_10_EXP
  588. 15, // DBL_DIG
  589. 53, // DBL_MANT_DIG
  590. // DBL_EPSILON
  591. BitConverter.Int64BitsToDouble(BitConverter.IsLittleEndian ? 0x3cb0000000000000 : 0x000000000000b03c),
  592. 2, // FLT_RADIX
  593. 1); // FLT_ROUNDS
  594. [PythonType("sys.float_info"), PythonHidden]
  595. public class floatinfo : PythonTuple {
  596. internal floatinfo(double max, int max_exp, int max_10_exp,
  597. double min, int min_exp, int min_10_exp,
  598. int dig, int mant_dig, double epsilon, int radix, int rounds)
  599. : base(new object[] { max, max_exp, max_10_exp,
  600. min, min_exp, min_10_exp,
  601. dig, mant_dig, epsilon, radix, rounds}) {
  602. this.max = max;
  603. this.max_exp = max_exp;
  604. this.max_10_exp = max_10_exp;
  605. this.min = min;
  606. this.min_exp = min_exp;
  607. this.min_10_exp = min_10_exp;
  608. this.dig = dig;
  609. this.mant_dig = mant_dig;
  610. this.epsilon = epsilon;
  611. this.radix = radix;
  612. this.rounds = rounds;
  613. }
  614. public readonly double max;
  615. public readonly int max_exp;
  616. public readonly int max_10_exp;
  617. public readonly double min;
  618. public readonly int min_exp;
  619. public readonly int min_10_exp;
  620. public readonly int dig;
  621. public readonly int mant_dig;
  622. public readonly double epsilon;
  623. public readonly int radix;
  624. public readonly int rounds;
  625. public const int n_fields = 11;
  626. public const int n_sequence_fields = 11;
  627. public const int n_unnamed_fields = 0;
  628. public override string __repr__(CodeContext context) {
  629. return string.Format("sys.float_info(max={0}, max_exp={1}, max_10_exp={2}, " +
  630. "min={3}, min_exp={4}, min_10_exp={5}, " +
  631. "dig={6}, mant_dig={7}, epsilon={8}, radix={9}, rounds={10})",
  632. max, max_exp, max_10_exp,
  633. min, min_exp, min_10_exp,
  634. dig, mant_dig, epsilon, radix, rounds);
  635. }
  636. }
  637. [SpecialName]
  638. public static void PerformModuleReload(PythonContext/*!*/ context, PythonDictionary/*!*/ dict) {
  639. dict["stdin"] = dict["__stdin__"];
  640. dict["stdout"] = dict["__stdout__"];
  641. dict["stderr"] = dict["__stderr__"];
  642. // !!! These fields do need to be reset on "reload(sys)". However, the initial value is specified by the
  643. // engine elsewhere. For now, we initialize them just once to some default value
  644. dict["warnoptions"] = new List(0);
  645. PublishBuiltinModuleNames(context, dict);
  646. context.SetHostVariables(dict);
  647. dict["meta_path"] = new List(0);
  648. dict["path_hooks"] = new List(0);
  649. // add zipimport to the path hooks for importing from zip files.
  650. try {
  651. PythonModule zipimport = Importer.ImportModule(
  652. context.SharedClsContext, context.SharedClsContext.GlobalDict,
  653. "zipimport", false, -1) as PythonModule;
  654. if (zipimport != null) {
  655. object zipimporter = PythonOps.GetBoundAttr(
  656. context.SharedClsContext, zipimport, "zipimporter");
  657. List path_hooks = dict["path_hooks"] as List;
  658. if (path_hooks != null && zipimporter != null) {
  659. path_hooks.Add(zipimporter);
  660. }
  661. }
  662. } catch {
  663. // this is not a fatal error, so we don't do anything.
  664. }
  665. dict["path_importer_cache"] = new PythonDictionary();
  666. }
  667. internal static void PublishBuiltinModuleNames(PythonContext/*!*/ context, PythonDictionary/*!*/ dict) {
  668. object[] keys = new object[context.BuiltinModules.Keys.Count];
  669. int index = 0;
  670. foreach (object key in context.BuiltinModules.Keys) {
  671. keys[index++] = key;
  672. }
  673. dict["builtin_module_names"] = PythonTuple.MakeTuple(keys);
  674. }
  675. }
  676. }