PageRenderTime 80ms CodeModel.GetById 48ms RepoModel.GetById 0ms app.codeStats 0ms

/DICK.B1/IronPython/Modules/sys.cs

https://bitbucket.org/williamybs/uidipythontool
C# | 1061 lines | 794 code | 223 blank | 44 comment | 42 complexity | cb53ad1a98513be11b171df11a764c82 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;
  17. using System.Collections.Generic;
  18. using System.IO;
  19. using System.Reflection;
  20. using System.Runtime.CompilerServices;
  21. using System.Runtime.InteropServices;
  22. using System.Security;
  23. using System.Text;
  24. using Microsoft.Scripting;
  25. using Microsoft.Scripting.Runtime;
  26. using IronPython.Runtime;
  27. using IronPython.Runtime.Exceptions;
  28. using IronPython.Runtime.Operations;
  29. using IronPython.Runtime.Types;
  30. #if CLR2
  31. using Microsoft.Scripting.Math;
  32. #else
  33. using System.Numerics;
  34. #endif
  35. [assembly: PythonModule("sys", typeof(IronPython.Modules.SysModule))]
  36. namespace IronPython.Modules {
  37. public static class SysModule {
  38. public const string __doc__ = "Provides access to functions which query or manipulate the Python runtime.";
  39. public const int api_version = 0;
  40. // argv is set by PythonContext and only on the initial load
  41. public static readonly string byteorder = BitConverter.IsLittleEndian ? "little" : "big";
  42. // builtin_module_names is set by PythonContext and updated on reload
  43. public const string copyright = "Copyright (c) Microsoft Corporation. All rights reserved.";
  44. private static string GetPrefix() {
  45. string prefix;
  46. #if SILVERLIGHT
  47. prefix = String.Empty;
  48. #else
  49. try {
  50. prefix = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
  51. } catch (SecurityException) {
  52. prefix = String.Empty;
  53. }
  54. #endif
  55. return prefix;
  56. }
  57. /// <summary>
  58. /// Returns detailed call statistics. Not implemented in IronPython and always returns None.
  59. /// </summary>
  60. public static object callstats() {
  61. return null;
  62. }
  63. /// <summary>
  64. /// Handles output of the expression statement.
  65. /// Prints the value and sets the __builtin__._
  66. /// </summary>
  67. public static void displayhook(CodeContext/*!*/ context, object value) {
  68. if (value != null) {
  69. PythonOps.Print(context, PythonOps.Repr(context, value));
  70. PythonContext.GetContext(context).BuiltinModuleDict["_"] = value;
  71. }
  72. }
  73. public const int dllhandle = 0;
  74. public static void excepthook(CodeContext/*!*/ context, object exctype, object value, object traceback) {
  75. PythonContext pc = PythonContext.GetContext(context);
  76. PythonOps.PrintWithDest(
  77. context,
  78. pc.SystemStandardError,
  79. pc.FormatException(PythonExceptions.ToClr(value))
  80. );
  81. }
  82. public static int getcheckinterval() {
  83. throw PythonOps.NotImplementedError("IronPython does not support sys.getcheckinterval");
  84. }
  85. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "value")]
  86. public static void setcheckinterval(int value) {
  87. throw PythonOps.NotImplementedError("IronPython does not support sys.setcheckinterval");
  88. }
  89. // warnoptions is set by PythonContext and updated on each reload
  90. [Python3Warning("'sys.exc_clear() not supported in 3.x; use except clauses'")]
  91. public static void exc_clear() {
  92. PythonOps.ClearCurrentException();
  93. }
  94. public static PythonTuple exc_info(CodeContext/*!*/ context) {
  95. return PythonOps.GetExceptionInfo(context);
  96. }
  97. // exec_prefix and executable are set by PythonContext and updated on each reload
  98. public static void exit() {
  99. exit(null);
  100. }
  101. public static void exit(object code) {
  102. if (code == null) {
  103. throw new PythonExceptions._SystemExit().InitAndGetClrException();
  104. } else {
  105. PythonTuple pt = code as PythonTuple;
  106. if (pt != null && pt.__len__() == 1) {
  107. code = pt[0];
  108. }
  109. // throw as a python exception here to get the args set.
  110. throw new PythonExceptions._SystemExit().InitAndGetClrException(code);
  111. }
  112. }
  113. public static string getdefaultencoding(CodeContext/*!*/ context) {
  114. return PythonContext.GetContext(context).GetDefaultEncodingName();
  115. }
  116. public static object getfilesystemencoding() {
  117. return null;
  118. }
  119. [PythonHidden]
  120. public static TraceBackFrame/*!*/ _getframeImpl(CodeContext/*!*/ context) {
  121. return _getframeImpl(context, 0);
  122. }
  123. [PythonHidden]
  124. public static TraceBackFrame/*!*/ _getframeImpl(CodeContext/*!*/ context, int depth) {
  125. var stack = PythonOps.GetFunctionStack();
  126. if (depth < stack.Count) {
  127. TraceBackFrame cur = null;
  128. int curTraceFrame = -1;
  129. for (int i = 0; i < stack.Count - depth; i++) {
  130. var elem = stack[i];
  131. if (elem.Frame != null) {
  132. // we previously handed out a frame here, hand out the same one now
  133. cur = elem.Frame;
  134. } else {
  135. // create a new frame and save it for future calls
  136. cur = new TraceBackFrame(
  137. context,
  138. Builtin.globals(elem.Context),
  139. Builtin.locals(elem.Context),
  140. elem.Code,
  141. cur
  142. );
  143. stack[i] = new FunctionStack(elem.Context, elem.Code, cur);
  144. }
  145. curTraceFrame++;
  146. }
  147. return cur;
  148. }
  149. throw PythonOps.ValueError("call stack is not deep enough");
  150. }
  151. public static int getsizeof(object o) {
  152. return ObjectOps.__sizeof__(o);
  153. }
  154. #if !SILVERLIGHT
  155. public static PythonTuple getwindowsversion() {
  156. var osVer = Environment.OSVersion;
  157. return PythonTuple.MakeTuple(
  158. osVer.Version.Major,
  159. osVer.Version.Minor,
  160. osVer.Version.Build,
  161. (int)osVer.Platform,
  162. osVer.ServicePack
  163. );
  164. }
  165. #endif
  166. // hex_version is set by PythonContext
  167. public const int maxint = Int32.MaxValue;
  168. public const int maxsize = Int32.MaxValue;
  169. public const int maxunicode = (int)ushort.MaxValue;
  170. // modules is set by PythonContext and only on the initial load
  171. // path is set by PythonContext and only on the initial load
  172. #if SILVERLIGHT
  173. public const string platform = "silverlight";
  174. #else
  175. public const string platform = "cli";
  176. #endif
  177. public static readonly string prefix = GetPrefix();
  178. // ps1 and ps2 are set by PythonContext and only on the initial load
  179. public static void setdefaultencoding(CodeContext context, object name) {
  180. if (name == null) throw PythonOps.TypeError("name cannot be None");
  181. string strName = name as string;
  182. if (strName == null) throw PythonOps.TypeError("name must be a string");
  183. PythonContext pc = PythonContext.GetContext(context);
  184. Encoding enc;
  185. if (!StringOps.TryGetEncoding(strName, out enc)) {
  186. throw PythonOps.LookupError("'{0}' does not match any available encodings", strName);
  187. }
  188. pc.DefaultEncoding = enc;
  189. }
  190. #if PROFILE_SUPPORT
  191. // not enabled because we don't yet support tracing built-in functions. Doing so is a little
  192. // difficult because it's hard to flip tracing on/off for them w/o a perf overhead in the
  193. // non-profiling case.
  194. public static void setprofile(CodeContext/*!*/ context, TracebackDelegate o) {
  195. PythonContext pyContext = PythonContext.GetContext(context);
  196. pyContext.EnsureDebugContext();
  197. if (o == null) {
  198. pyContext.UnregisterTracebackHandler();
  199. } else {
  200. pyContext.RegisterTracebackHandler();
  201. }
  202. // Register the trace func with the listener
  203. pyContext.TracebackListener.SetProfile(o);
  204. }
  205. #endif
  206. public static void settrace(CodeContext/*!*/ context, object o) {
  207. PythonContext pyContext = PythonContext.GetContext(context);
  208. pyContext.EnsureDebugContext();
  209. if (o == null) {
  210. pyContext.UnregisterTracebackHandler();
  211. PythonTracebackListener.SetTrace(null, null);
  212. } else {
  213. // We're following CPython behavior here.
  214. // If CurrentPythonFrame is not null then we're currently inside a traceback, and
  215. // enabling trace while inside a traceback is only allowed through sys.call_tracing()
  216. var pyThread = PythonOps.GetFunctionStackNoCreate();
  217. if (pyThread == null || !PythonTracebackListener.InTraceBack) {
  218. pyContext.PushTracebackHandler(new PythonTracebackListener((PythonContext)context.LanguageContext));
  219. pyContext.RegisterTracebackHandler();
  220. PythonTracebackListener.SetTrace(o, (TracebackDelegate)Converter.ConvertToDelegate(o, typeof(TracebackDelegate)));
  221. }
  222. }
  223. }
  224. public static void call_tracing(CodeContext/*!*/ context, object func, PythonTuple args) {
  225. PythonContext pyContext = (PythonContext)context.LanguageContext;
  226. pyContext.EnsureDebugContext();
  227. pyContext.UnregisterTracebackHandler();
  228. pyContext.PushTracebackHandler(new PythonTracebackListener((PythonContext)context.LanguageContext));
  229. pyContext.RegisterTracebackHandler();
  230. try {
  231. PythonCalls.Call(func, args.ToArray());
  232. } finally {
  233. pyContext.PopTracebackHandler();
  234. pyContext.UnregisterTracebackHandler();
  235. }
  236. }
  237. public static object gettrace(CodeContext/*!*/ context) {
  238. return PythonTracebackListener.GetTraceObject();
  239. }
  240. public static void setrecursionlimit(CodeContext/*!*/ context, int limit) {
  241. PythonContext.GetContext(context).RecursionLimit = limit;
  242. }
  243. public static int getrecursionlimit(CodeContext/*!*/ context) {
  244. return PythonContext.GetContext(context).RecursionLimit;
  245. }
  246. // stdin, stdout, stderr, __stdin__, __stdout__, and __stderr__ added by PythonContext
  247. // version and version_info are set by PythonContext
  248. public static PythonTuple subversion = PythonTuple.MakeTuple("IronPython", "", "");
  249. public const string winver = "2.6";
  250. #region Special types
  251. [PythonHidden, PythonType("flags"), DontMapIEnumerableToIter]
  252. public sealed class SysFlags : IList<object> {
  253. private const string _className = "sys.flags";
  254. internal SysFlags() { }
  255. private const int INDEX_DEBUG = 0;
  256. private const int INDEX_PY3K_WARNING = 1;
  257. private const int INDEX_DIVISION_WARNING = 2;
  258. private const int INDEX_DIVISION_NEW = 3;
  259. private const int INDEX_INSPECT = 4;
  260. private const int INDEX_INTERACTIVE = 5;
  261. private const int INDEX_OPTIMIZE = 6;
  262. private const int INDEX_DONT_WRITE_BYTECODE = 7;
  263. private const int INDEX_NO_USER_SITE = 8;
  264. private const int INDEX_NO_SITE = 9;
  265. private const int INDEX_IGNORE_ENVIRONMENT = 10;
  266. private const int INDEX_TABCHECK = 11;
  267. private const int INDEX_VERBOSE = 12;
  268. private const int INDEX_UNICODE = 13;
  269. private const int INDEX_BYTES_WARNING = 14;
  270. public const int n_fields = 15;
  271. public const int n_sequence_fields = 15;
  272. public const int n_unnamed_fields = 0;
  273. private static readonly string[] _keys = new string[] {
  274. "debug", "py3k_warning", "division_warning", "division_new", "inspect",
  275. "interactive", "optimize", "dont_write_bytecode", "no_user_site", "no_site",
  276. "ignore_environment", "tabcheck", "verbose", "unicode", "bytes_warning"
  277. };
  278. private object[] _values = new object[n_fields] {
  279. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  280. };
  281. private PythonTuple __tuple = null;
  282. private PythonTuple _tuple {
  283. get {
  284. _Refresh();
  285. return __tuple;
  286. }
  287. }
  288. private string __string = null;
  289. private string _string {
  290. get {
  291. _Refresh();
  292. return __string;
  293. }
  294. }
  295. public override string ToString() {
  296. return _string;
  297. }
  298. public string __repr__() {
  299. return _string;
  300. }
  301. private bool _modified = true;
  302. private void _Refresh() {
  303. if (_modified) {
  304. __tuple = PythonTuple.MakeTuple(_values);
  305. StringBuilder sb = new StringBuilder("sys.flags(");
  306. for (int i = 0; i < n_fields; i++) {
  307. if (_keys[i] == null) {
  308. sb.Append(_values[i]);
  309. } else {
  310. sb.AppendFormat("{0}={1}", _keys[i], _values[i]);
  311. }
  312. if (i < n_fields - 1) {
  313. sb.Append(", ");
  314. } else {
  315. sb.Append(')');
  316. }
  317. }
  318. __string = sb.ToString();
  319. _modified = false;
  320. }
  321. }
  322. private int _GetVal(int index) {
  323. return (int)_values[index];
  324. }
  325. private void _SetVal(int index, int value) {
  326. if ((int)_values[index] != value) {
  327. _modified = true;
  328. _values[index] = value;
  329. }
  330. }
  331. #region ICollection<object> Members
  332. void ICollection<object>.Add(object item) {
  333. throw new InvalidOperationException(_className + " is readonly");
  334. }
  335. void ICollection<object>.Clear() {
  336. throw new InvalidOperationException(_className + " is readonly");
  337. }
  338. [PythonHidden]
  339. public bool Contains(object item) {
  340. return _tuple.Contains(item);
  341. }
  342. [PythonHidden]
  343. public void CopyTo(object[] array, int arrayIndex) {
  344. _tuple.CopyTo(array, arrayIndex);
  345. }
  346. public int Count {
  347. [PythonHidden]
  348. get {
  349. return n_fields;
  350. }
  351. }
  352. bool ICollection<object>.IsReadOnly {
  353. get { return true; }
  354. }
  355. bool ICollection<object>.Remove(object item) {
  356. throw new InvalidOperationException(_className + " is readonly");
  357. }
  358. #endregion
  359. #region IEnumerable Members
  360. [PythonHidden]
  361. public IEnumerator GetEnumerator() {
  362. return _tuple.GetEnumerator();
  363. }
  364. #endregion
  365. #region IEnumerable<object> Members
  366. IEnumerator<object> IEnumerable<object>.GetEnumerator() {
  367. return ((IEnumerable<object>)_tuple).GetEnumerator();
  368. }
  369. #endregion
  370. #region ISequence Members
  371. public int __len__() {
  372. return n_fields;
  373. }
  374. public object this[int i] {
  375. get {
  376. return _tuple[i];
  377. }
  378. }
  379. public object this[BigInteger i] {
  380. get {
  381. return this[(int)i];
  382. }
  383. }
  384. public object __getslice__(int start, int end) {
  385. return _tuple.__getslice__(start, end);
  386. }
  387. public object this[Slice s] {
  388. get {
  389. return _tuple[s];
  390. }
  391. }
  392. public object this[object o] {
  393. get {
  394. return this[Converter.ConvertToIndex(o)];
  395. }
  396. }
  397. #endregion
  398. #region IList<object> Members
  399. [PythonHidden]
  400. public int IndexOf(object item) {
  401. return _tuple.IndexOf(item);
  402. }
  403. void IList<object>.Insert(int index, object item) {
  404. throw new InvalidOperationException(_className + " is readonly");
  405. }
  406. void IList<object>.RemoveAt(int index) {
  407. throw new InvalidOperationException(_className + " is readonly");
  408. }
  409. object IList<object>.this[int index] {
  410. get {
  411. return _tuple[index];
  412. }
  413. set {
  414. throw new InvalidOperationException(_className + " is readonly");
  415. }
  416. }
  417. #endregion
  418. #region binary ops
  419. public static PythonTuple operator +([NotNull]SysFlags f, [NotNull]PythonTuple t) {
  420. return f._tuple + t;
  421. }
  422. public static PythonTuple operator *([NotNull]SysFlags f, int n) {
  423. return f._tuple * n;
  424. }
  425. public static PythonTuple operator *(int n, [NotNull]SysFlags f) {
  426. return f._tuple * n;
  427. }
  428. public static object operator *([NotNull]SysFlags f, [NotNull]Index n) {
  429. return f._tuple * n;
  430. }
  431. public static object operator *([NotNull]Index n, [NotNull]SysFlags f) {
  432. return f._tuple * n;
  433. }
  434. public static object operator *([NotNull]SysFlags f, object n) {
  435. return f._tuple * n;
  436. }
  437. public static object operator *(object n, [NotNull]SysFlags f) {
  438. return f._tuple * n;
  439. }
  440. #endregion
  441. # region comparison and hashing methods
  442. public static bool operator >(SysFlags f, PythonTuple t) {
  443. return f._tuple > t;
  444. }
  445. public static bool operator <(SysFlags f, PythonTuple t) {
  446. return f._tuple < t;
  447. }
  448. public static bool operator >=(SysFlags f, PythonTuple t) {
  449. return f._tuple >= t;
  450. }
  451. public static bool operator <=(SysFlags f, PythonTuple t) {
  452. return f._tuple <= t;
  453. }
  454. public override bool Equals(object obj) {
  455. if (obj is SysFlags) {
  456. return _tuple.Equals(((SysFlags)obj)._tuple);
  457. }
  458. return _tuple.Equals(obj);
  459. }
  460. public override int GetHashCode() {
  461. return _tuple.GetHashCode();
  462. }
  463. # endregion
  464. #region sys.flags API
  465. public int debug {
  466. get { return _GetVal(INDEX_DEBUG); }
  467. internal set { _SetVal(INDEX_DEBUG, value); }
  468. }
  469. public int py3k_warning {
  470. get { return _GetVal(INDEX_PY3K_WARNING); }
  471. internal set { _SetVal(INDEX_PY3K_WARNING, value); }
  472. }
  473. public int division_warning {
  474. get { return _GetVal(INDEX_DIVISION_WARNING); }
  475. internal set { _SetVal(INDEX_DIVISION_WARNING, value); }
  476. }
  477. public int division_new {
  478. get { return _GetVal(INDEX_DIVISION_NEW); }
  479. internal set { _SetVal(INDEX_DIVISION_NEW, value); }
  480. }
  481. public int inspect {
  482. get { return _GetVal(INDEX_INSPECT); }
  483. internal set { _SetVal(INDEX_INSPECT, value); }
  484. }
  485. public int interactive {
  486. get { return _GetVal(INDEX_INTERACTIVE); }
  487. internal set { _SetVal(INDEX_INTERACTIVE, value); }
  488. }
  489. public int optimize {
  490. get { return _GetVal(INDEX_OPTIMIZE); }
  491. internal set { _SetVal(INDEX_OPTIMIZE, value); }
  492. }
  493. public int dont_write_bytecode {
  494. get { return _GetVal(INDEX_DONT_WRITE_BYTECODE); }
  495. internal set { _SetVal(INDEX_DONT_WRITE_BYTECODE, value); }
  496. }
  497. public int no_user_site {
  498. get { return _GetVal(INDEX_NO_USER_SITE); }
  499. internal set { _SetVal(INDEX_NO_USER_SITE, value); }
  500. }
  501. public int no_site {
  502. get { return _GetVal(INDEX_NO_SITE); }
  503. internal set { _SetVal(INDEX_NO_SITE, value); }
  504. }
  505. public int ignore_environment {
  506. get { return _GetVal(INDEX_IGNORE_ENVIRONMENT); }
  507. internal set { _SetVal(INDEX_IGNORE_ENVIRONMENT, value); }
  508. }
  509. public int tabcheck {
  510. get { return _GetVal(INDEX_TABCHECK); }
  511. internal set { _SetVal(INDEX_TABCHECK, value); }
  512. }
  513. public int verbose {
  514. get { return _GetVal(INDEX_VERBOSE); }
  515. internal set { _SetVal(INDEX_VERBOSE, value); }
  516. }
  517. public int unicode {
  518. get { return _GetVal(INDEX_UNICODE); }
  519. internal set { _SetVal(INDEX_UNICODE, value); }
  520. }
  521. public int bytes_warning {
  522. get { return _GetVal(INDEX_BYTES_WARNING); }
  523. internal set { _SetVal(INDEX_BYTES_WARNING, value); }
  524. }
  525. #endregion
  526. }
  527. #endregion
  528. public static floatinfo float_info = new floatinfo(PythonTuple.MakeTuple(Double.MaxValue, 1024, 308, Double.MinValue, -1021, -307, 15, 53, Double.Epsilon, 2, 1), null);
  529. [PythonType, PythonHidden, DontMapIEnumerableToIter]
  530. public class floatinfo : IList, IList<object> {
  531. private readonly object _max, _dig, _mant_dig, _epsilon, _rounds, _max_exp, _max_10_exp, _min, _min_exp, _min_10_exp, _radix;
  532. public const int n_fields = 11;
  533. public const int n_sequence_fields = 11;
  534. public const int n_unnamed_fields = 0;
  535. public floatinfo(IList statResult, [DefaultParameterValue(null)]PythonDictionary dict) {
  536. // dict is allowed by CPython's float_info, but doesn't seem to do anything, so we ignore it here.
  537. if (statResult.Count < 10) {
  538. throw PythonOps.TypeError("float_info() takes an at least 11-sequence ({0}-sequence given)", statResult.Count);
  539. }
  540. _max = statResult[0];
  541. _max_exp = statResult[1];
  542. _max_10_exp = statResult[2];
  543. _min = statResult[3];
  544. _min_exp = statResult[4];
  545. _min_10_exp = statResult[5];
  546. _dig = statResult[6];
  547. _mant_dig = statResult[7];
  548. _epsilon = statResult[8];
  549. _radix = statResult[9];
  550. _rounds = statResult[10];
  551. }
  552. private static object TryShrinkToInt(object value) {
  553. if (!(value is BigInteger)) {
  554. return value;
  555. }
  556. return BigIntegerOps.__int__((BigInteger)value);
  557. }
  558. public object epsilon {
  559. get {
  560. return _epsilon;
  561. }
  562. }
  563. public object mant_dig {
  564. get {
  565. return _mant_dig;
  566. }
  567. }
  568. public object radix {
  569. get {
  570. return _radix;
  571. }
  572. }
  573. public object rounds {
  574. get {
  575. return _rounds;
  576. }
  577. }
  578. public object max_10_exp {
  579. get {
  580. return _max_10_exp;
  581. }
  582. }
  583. public object min_10_exp {
  584. get {
  585. return _min_10_exp;
  586. }
  587. }
  588. public object max_exp {
  589. get {
  590. return _max_exp;
  591. }
  592. }
  593. public object max {
  594. get {
  595. return _max;
  596. }
  597. }
  598. public object min {
  599. get {
  600. return _min;
  601. }
  602. }
  603. public object dig {
  604. get {
  605. return _dig;
  606. }
  607. }
  608. public object min_exp {
  609. get {
  610. return _min_exp;
  611. }
  612. }
  613. public static PythonTuple operator +(floatinfo stat, PythonTuple tuple) {
  614. return stat.MakeTuple() + tuple;
  615. }
  616. public static bool operator >(floatinfo stat, IList o) {
  617. return stat.MakeTuple() > PythonTuple.Make(o);
  618. }
  619. public static bool operator <(floatinfo stat, IList o) {
  620. return stat.MakeTuple() > PythonTuple.Make(o);
  621. }
  622. public static bool operator >=(floatinfo stat, IList o) {
  623. return stat.MakeTuple() >= PythonTuple.Make(o);
  624. }
  625. public static bool operator <=(floatinfo stat, IList o) {
  626. return stat.MakeTuple() >= PythonTuple.Make(o);
  627. }
  628. public static bool operator >(floatinfo stat, object o) {
  629. return true;
  630. }
  631. public static bool operator <(floatinfo stat, object o) {
  632. return false;
  633. }
  634. public static bool operator >=(floatinfo stat, object o) {
  635. return true;
  636. }
  637. public static bool operator <=(floatinfo stat, object o) {
  638. return false;
  639. }
  640. public static PythonTuple operator *(floatinfo stat, int size) {
  641. return stat.MakeTuple() * size;
  642. }
  643. public static PythonTuple operator *(int size, floatinfo stat) {
  644. return stat.MakeTuple() * size;
  645. }
  646. public override string ToString() {
  647. return MakeTuple().ToString();
  648. }
  649. public string/*!*/ __repr__() {
  650. return ToString();
  651. }
  652. public PythonTuple __reduce__() {
  653. PythonDictionary emptyDict = new PythonDictionary(0);
  654. return PythonTuple.MakeTuple(
  655. DynamicHelpers.GetPythonTypeFromType(typeof(floatinfo)),
  656. PythonTuple.MakeTuple(MakeTuple(), emptyDict)
  657. );
  658. }
  659. #region ISequence Members
  660. public object this[int index] {
  661. get {
  662. return MakeTuple()[index];
  663. }
  664. }
  665. public object this[Slice slice] {
  666. get {
  667. return MakeTuple()[slice];
  668. }
  669. }
  670. public object __getslice__(int start, int stop) {
  671. return MakeTuple().__getslice__(start, stop);
  672. }
  673. public int __len__() {
  674. return MakeTuple().__len__();
  675. }
  676. public bool __contains__(object item) {
  677. return ((ICollection<object>)MakeTuple()).Contains(item);
  678. }
  679. #endregion
  680. private PythonTuple MakeTuple() {
  681. return PythonTuple.MakeTuple(
  682. max,
  683. max_exp,
  684. max_10_exp,
  685. min,
  686. min_exp,
  687. min_10_exp,
  688. dig,
  689. _mant_dig,
  690. _epsilon,
  691. _radix,
  692. _rounds
  693. );
  694. }
  695. #region Object overrides
  696. public override bool Equals(object obj) {
  697. if (obj is floatinfo) {
  698. return MakeTuple().Equals(((floatinfo)obj).MakeTuple());
  699. } else {
  700. return MakeTuple().Equals(obj);
  701. }
  702. }
  703. public override int GetHashCode() {
  704. return MakeTuple().GetHashCode();
  705. }
  706. #endregion
  707. #region IList<object> Members
  708. int IList<object>.IndexOf(object item) {
  709. return MakeTuple().IndexOf(item);
  710. }
  711. void IList<object>.Insert(int index, object item) {
  712. throw new InvalidOperationException();
  713. }
  714. void IList<object>.RemoveAt(int index) {
  715. throw new InvalidOperationException();
  716. }
  717. object IList<object>.this[int index] {
  718. get {
  719. return MakeTuple()[index];
  720. }
  721. set {
  722. throw new InvalidOperationException();
  723. }
  724. }
  725. #endregion
  726. #region ICollection<object> Members
  727. void ICollection<object>.Add(object item) {
  728. throw new InvalidOperationException();
  729. }
  730. void ICollection<object>.Clear() {
  731. throw new InvalidOperationException();
  732. }
  733. bool ICollection<object>.Contains(object item) {
  734. return __contains__(item);
  735. }
  736. void ICollection<object>.CopyTo(object[] array, int arrayIndex) {
  737. throw new NotImplementedException();
  738. }
  739. int ICollection<object>.Count {
  740. get { return __len__(); }
  741. }
  742. bool ICollection<object>.IsReadOnly {
  743. get { return true; }
  744. }
  745. bool ICollection<object>.Remove(object item) {
  746. throw new InvalidOperationException();
  747. }
  748. #endregion
  749. #region IEnumerable<object> Members
  750. IEnumerator<object> IEnumerable<object>.GetEnumerator() {
  751. foreach (object o in MakeTuple()) {
  752. yield return o;
  753. }
  754. }
  755. #endregion
  756. #region IEnumerable Members
  757. IEnumerator IEnumerable.GetEnumerator() {
  758. foreach (object o in MakeTuple()) {
  759. yield return o;
  760. }
  761. }
  762. #endregion
  763. #region IList Members
  764. int IList.Add(object value) {
  765. throw new InvalidOperationException();
  766. }
  767. void IList.Clear() {
  768. throw new InvalidOperationException();
  769. }
  770. bool IList.Contains(object value) {
  771. return __contains__(value);
  772. }
  773. int IList.IndexOf(object value) {
  774. return MakeTuple().IndexOf(value);
  775. }
  776. void IList.Insert(int index, object value) {
  777. throw new InvalidOperationException();
  778. }
  779. bool IList.IsFixedSize {
  780. get { return true; }
  781. }
  782. bool IList.IsReadOnly {
  783. get { return true; }
  784. }
  785. void IList.Remove(object value) {
  786. throw new InvalidOperationException();
  787. }
  788. void IList.RemoveAt(int index) {
  789. throw new InvalidOperationException();
  790. }
  791. object IList.this[int index] {
  792. get {
  793. return MakeTuple()[index];
  794. }
  795. set {
  796. throw new InvalidOperationException();
  797. }
  798. }
  799. #endregion
  800. #region ICollection Members
  801. void ICollection.CopyTo(Array array, int index) {
  802. throw new NotImplementedException();
  803. }
  804. int ICollection.Count {
  805. get { return __len__(); }
  806. }
  807. bool ICollection.IsSynchronized {
  808. get { return false; }
  809. }
  810. object ICollection.SyncRoot {
  811. get { return this; }
  812. }
  813. #endregion
  814. }
  815. [SpecialName]
  816. public static void PerformModuleReload(PythonContext/*!*/ context, PythonDictionary/*!*/ dict) {
  817. dict["stdin"] = dict["__stdin__"];
  818. dict["stdout"] = dict["__stdout__"];
  819. dict["stderr"] = dict["__stderr__"];
  820. // !!! These fields do need to be reset on "reload(sys)". However, the initial value is specified by the
  821. // engine elsewhere. For now, we initialize them just once to some default value
  822. dict["warnoptions"] = new List(0);
  823. PublishBuiltinModuleNames(context, dict);
  824. context.SetHostVariables(dict);
  825. dict["meta_path"] = new List(0);
  826. dict["path_hooks"] = new List(0);
  827. dict["path_importer_cache"] = new PythonDictionary();
  828. }
  829. private static void PublishBuiltinModuleNames(PythonContext/*!*/ context, PythonDictionary/*!*/ dict) {
  830. object[] keys = new object[context.BuiltinModules.Keys.Count];
  831. int index = 0;
  832. foreach (object key in context.BuiltinModules.Keys) {
  833. keys[index++] = key;
  834. }
  835. dict["builtin_module_names"] = PythonTuple.MakeTuple(keys);
  836. }
  837. }
  838. }