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