/Languages/IronPython/IronPython/Runtime/FutureBuiltins.cs

https://github.com/thomo13/ironruby · C# · 115 lines · 88 code · 13 blank · 14 comment · 20 complexity · 4964a460abb45e93abd456e0d42eea2f MD5 · raw file

  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. using System;
  16. using System.Collections;
  17. using System.Collections.Generic;
  18. using System.Diagnostics;
  19. using System.Runtime.CompilerServices;
  20. using System.Runtime.InteropServices;
  21. using System.Text;
  22. using Microsoft.Scripting;
  23. using Microsoft.Scripting.Actions;
  24. using Microsoft.Scripting.Generation;
  25. using Microsoft.Scripting.Runtime;
  26. using Microsoft.Scripting.Utils;
  27. using IronPython.Compiler;
  28. using IronPython.Runtime;
  29. using IronPython.Runtime.Binding;
  30. using IronPython.Runtime.Exceptions;
  31. using IronPython.Runtime.Operations;
  32. using IronPython.Runtime.Types;
  33. #if CLR2
  34. using Microsoft.Scripting.Math;
  35. #else
  36. using System.Numerics;
  37. #endif
  38. [assembly: PythonModule("future_builtins", typeof(FutureBuiltins))]
  39. namespace IronPython.Runtime {
  40. public static partial class FutureBuiltins {
  41. public const string __doc__ = "Provides access to built-ins which will be defined differently in Python 3.0.";
  42. [SpecialName]
  43. public static void PerformModuleReload(PythonContext context, PythonDictionary dict) {
  44. PythonModule scope = Importer.ImportModule(context.SharedContext, context.SharedContext.GlobalDict, "itertools", false, -1) as PythonModule;
  45. if (scope != null) {
  46. dict["map"] = scope.__dict__["imap"];
  47. dict["filter"] = scope.__dict__["ifilter"];
  48. dict["zip"] = scope.__dict__["izip"];
  49. }
  50. }
  51. public static string ascii(CodeContext/*!*/ context, object @object) {
  52. return PythonOps.Repr(context, @object);
  53. }
  54. public static string hex(CodeContext/*!*/ context, object number) {
  55. if (number is int) {
  56. return Int32Ops.__hex__((int)number);
  57. } else if (number is BigInteger) {
  58. BigInteger x = (BigInteger)number;
  59. if (x < 0) {
  60. return "-0x" + (-x).ToString(16).ToLower();
  61. } else {
  62. return "0x" + x.ToString(16).ToLower();
  63. }
  64. }
  65. object value;
  66. if (PythonTypeOps.TryInvokeUnaryOperator(context,
  67. number,
  68. "__index__",
  69. out value)) {
  70. if (!(value is int) && !(value is BigInteger))
  71. throw PythonOps.TypeError("index returned non-(int, long), got '{0}'", PythonTypeOps.GetName(value));
  72. return hex(context, value);
  73. }
  74. throw PythonOps.TypeError("hex() argument cannot be interpreted as an index");
  75. }
  76. public static string oct(CodeContext context, object number) {
  77. if (number is int) {
  78. number = (BigInteger)(int)number;
  79. }
  80. if (number is BigInteger) {
  81. BigInteger x = (BigInteger)number;
  82. if (x == 0) {
  83. return "0o0";
  84. } else if (x > 0) {
  85. return "0o" + x.ToString(8);
  86. } else {
  87. return "-0o" + (-x).ToString(8);
  88. }
  89. }
  90. object value;
  91. if (PythonTypeOps.TryInvokeUnaryOperator(context,
  92. number,
  93. "__index__",
  94. out value)) {
  95. if (!(value is int) && !(value is BigInteger))
  96. throw PythonOps.TypeError("index returned non-(int, long), got '{0}'", PythonTypeOps.GetName(value));
  97. return oct(context, value);
  98. }
  99. throw PythonOps.TypeError("oct() argument cannot be interpreted as an index");
  100. }
  101. }
  102. }