/IronPython_2_0/Src/IronPython/Runtime/Builtin.cs
C# | 1900 lines | 1517 code | 312 blank | 71 comment | 421 complexity | d5d1d1dda4e2fa8ea4864a9870255b0a MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, CPL-1.0, CC-BY-SA-3.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1, Apache-2.0
Large files files are truncated, but you can click here to view the full file
- /* ****************************************************************************
- *
- * Copyright (c) Microsoft Corporation.
- *
- * This source code is subject to terms and conditions of the Microsoft Public License. A
- * copy of the license can be found in the License.html file at the root of this distribution. If
- * you cannot locate the Microsoft Public License, please send an email to
- * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
- * by the terms of the Microsoft Public License.
- *
- * You must not remove this notice, or any other, from this software.
- *
- *
- * ***************************************************************************/
-
- using System; using Microsoft;
- using System.Collections;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Runtime.InteropServices;
- using Microsoft.Scripting.Actions;
- using System.Text;
- using IronPython.Compiler;
- using IronPython.Runtime;
- using IronPython.Runtime.Binding;
- using IronPython.Runtime.Exceptions;
- using IronPython.Runtime.Operations;
- using IronPython.Runtime.Types;
- using Microsoft.Scripting;
- using Microsoft.Scripting.Math;
- using Microsoft.Scripting.Runtime;
- using Microsoft.Scripting.Utils;
- using SpecialName = System.Runtime.CompilerServices.SpecialNameAttribute;
- using Microsoft.Scripting.Generation;
-
- [assembly: PythonModule("__builtin__", typeof(Builtin))]
- namespace IronPython.Runtime {
- [Documentation("")] // Documentation suppresses XML Doc on startup.
- public static partial class Builtin {
- public static object True {
- get {
- return RuntimeHelpers.True;
- }
- }
-
- public static object False {
- get {
- return RuntimeHelpers.False;
- }
- }
-
- // This will always stay null
- public static readonly object None;
-
- public static IronPython.Runtime.Types.Ellipsis Ellipsis {
- get {
- return IronPython.Runtime.Types.Ellipsis.Value;
- }
- }
-
- public static NotImplementedType NotImplemented {
- get {
- return NotImplementedType.Value;
- }
- }
-
- public static object exit {
- get {
- return "Use Ctrl-Z plus Return to exit";
- }
- }
-
- public static object quit {
- get {
- return "Use Ctrl-Z plus Return to exit";
- }
- }
-
- [Documentation("__import__(name) -> module\n\nImport a module.")]
- public static object __import__(CodeContext/*!*/ context, string name) {
- return __import__(context, name, null, null, null);
- }
-
- [Documentation("__import__(name, globals) -> module\n\nImport a module.")]
- public static object __import__(CodeContext/*!*/ context, string name, object globals) {
- return __import__(context, name, globals, null, null);
-
- }
-
- [Documentation("__import__(name, globals, locals) -> module\n\nImport a module.")]
- public static object __import__(CodeContext/*!*/ context, string name, object globals, object locals) {
- return __import__(context, name, globals, locals, null);
- }
-
- [Documentation("__import__(name, globals, locals, fromlist) -> module\n\nImport a module.")]
- public static object __import__(CodeContext/*!*/ context, string name, object globals, object locals, object fromlist) {
- return __import__(context, name, globals, locals, fromlist, -1);
- }
-
- [Documentation("__import__(name, globals, locals, fromlist) -> module\n\nImport a module.")]
- public static object __import__(CodeContext/*!*/ context, string name, object globals, object locals, object fromlist, int level) {
- //!!! remove suppress in GlobalSuppressions.cs when CodePlex 2704 is fixed.
- ISequence from = fromlist as ISequence;
- PythonContext pc = PythonContext.GetContext(context);
-
- object ret = Importer.ImportModule(context, globals, name, from != null && from.__len__() > 0, level);
- if (ret == null) {
- throw PythonOps.ImportError("No module named {0}", name);
- }
-
- Scope mod = ret as Scope;
- if (mod != null && from != null) {
- string strAttrName;
- for (int i = 0; i < from.__len__(); i++) {
- object attrName = from[i];
-
- if (pc.TryConvertToString(attrName, out strAttrName) &&
- !String.IsNullOrEmpty(strAttrName) &&
- strAttrName != "*") {
- try {
- Importer.ImportFrom(context, mod, strAttrName);
- } catch (ImportException) {
- continue;
- }
- }
- }
- }
-
- return ret;
- }
-
- [Documentation("abs(number) -> number\n\nReturn the absolute value of the argument.")]
- public static object abs(CodeContext/*!*/ context, object o) {
- if (o is int) return Int32Ops.Abs((int)o);
- if (o is long) return Int64Ops.Abs((long)o);
- if (o is double) return DoubleOps.Abs((double)o);
- if (o is bool) return (((bool)o) ? 1 : 0);
- if (o is string) throw PythonOps.TypeError("bad operand type for abs()");
- BigInteger bi = o as BigInteger;
- if (!Object.ReferenceEquals(bi, null)) return BigIntegerOps.__abs__(bi);
- if (o is Complex64) return ComplexOps.Abs((Complex64)o);
-
- object value;
- if (PythonTypeOps.TryInvokeUnaryOperator(context, o, Symbols.AbsoluteValue, out value)) {
- return value;
- }
-
- throw PythonOps.TypeError("bad operand type for abs()");
- }
-
- public static bool all(object x) {
- IEnumerator i = PythonOps.GetEnumerator(x);
- while (i.MoveNext()) {
- if (!PythonOps.IsTrue(i.Current)) return false;
- }
- return true;
- }
-
- public static bool any(object x) {
- IEnumerator i = PythonOps.GetEnumerator(x);
- while (i.MoveNext()) {
- if (PythonOps.IsTrue(i.Current)) return true;
- }
- return false;
- }
-
- [Documentation("apply(object[, args[, kwargs]]) -> value\n\nDeprecated.\nInstead, use:\n function(*args, **keywords).")]
- public static object apply(CodeContext/*!*/ context, object func) {
- return PythonOps.CallWithContext(context, func);
- }
-
- public static object apply(CodeContext/*!*/ context, object func, object args) {
- return PythonOps.CallWithArgsTupleAndContext(context, func, ArrayUtils.EmptyObjects, args);
- }
-
- public static object apply(CodeContext/*!*/ context, object func, object args, object kws) {
- return PythonOps.CallWithArgsTupleAndKeywordDictAndContext(context, func, ArrayUtils.EmptyObjects, ArrayUtils.EmptyStrings, args, kws);
- }
-
- public static PythonType basestring {
- get {
- return DynamicHelpers.GetPythonTypeFromType(typeof(string));
- }
- }
-
- public static PythonType @bool {
- get {
- return DynamicHelpers.GetPythonTypeFromType(typeof(bool));
- }
- }
-
-
- public static PythonType buffer {
- get {
- return DynamicHelpers.GetPythonTypeFromType(typeof(PythonBuffer));
- }
- }
-
- [Documentation("callable(object) -> bool\n\nReturn whether the object is callable (i.e., some kind of function).")]
- public static bool callable(CodeContext/*!*/ context, object o) {
- return PythonOps.IsCallable(context, o);
- }
-
- [Documentation("chr(i) -> character\n\nReturn a string of one character with ordinal i; 0 <= i< 256.")]
- public static string chr(int value) {
- if (value < 0 || value > 0xFF) {
- throw PythonOps.ValueError("{0} is not in required range", value);
- }
- return RuntimeHelpers.CharToString((char)value);
- }
-
- internal static object TryCoerce(CodeContext/*!*/ context, object x, object y) {
- PythonTypeSlot pts;
- PythonType xType = DynamicHelpers.GetPythonType(x);
-
- if (xType.TryResolveSlot(context, Symbols.Coerce, out pts)) {
- object callable;
- if (pts.TryGetBoundValue(context, x, xType, out callable)) {
- return PythonCalls.Call(context, callable, y);
- }
- }
- return NotImplementedType.Value;
- }
-
- [Documentation("coerce(x, y) -> (x1, y1)\n\nReturn a tuple consisting of the two numeric arguments converted to\na common type. If coercion is not possible, raise TypeError.")]
- public static object coerce(CodeContext/*!*/ context, object x, object y) {
- object converted;
-
- if (x == null && y == null) {
- return PythonTuple.MakeTuple(null, null);
- }
-
- converted = TryCoerce(context, x, y);
- if (converted != null && converted != NotImplementedType.Value) {
- return converted;
- }
-
- converted = TryCoerce(context, y, x);
- if (converted != null && converted != NotImplementedType.Value) {
- PythonTuple pt = converted as PythonTuple;
- if (pt != null && pt.Count == 2) {
- return PythonTuple.MakeTuple(pt[1], pt[0]);
- }
- }
-
- throw PythonOps.TypeError("coercion failed");
- }
-
- [Documentation("compile a unit of source code.\n\nThe source can be compiled either as exec, eval, or single.\nexec compiles the code as if it were a file\neval compiles the code as if were an expression\nsingle compiles a single statement\n\n")]
- public static object compile(CodeContext/*!*/ context, string source, string filename, string kind, object flags, object dontInherit) {
- if (source.IndexOf('\0') != -1) {
- throw PythonOps.TypeError("compile() expected string without null bytes");
- }
-
- bool inheritContext = GetCompilerInheritance(dontInherit);
- CompileFlags cflags = GetCompilerFlags(flags);
- PythonCompilerOptions opts = GetDefaultCompilerOptions(context, inheritContext, cflags);
- if ((cflags & CompileFlags.CO_DONT_IMPLY_DEDENT) != 0) {
- opts.DontImplyDedent = true;
- }
- opts.Module |= ModuleOptions.ExecOrEvalCode;
-
- SourceUnit sourceUnit;
- string unitPath = String.IsNullOrEmpty(filename) ? null : filename;
-
- switch (kind) {
- case "exec": sourceUnit = context.LanguageContext.CreateSnippet(source, unitPath, SourceCodeKind.Statements); break;
- case "eval": sourceUnit = context.LanguageContext.CreateSnippet(source, unitPath, SourceCodeKind.Expression); break;
- case "single": sourceUnit = context.LanguageContext.CreateSnippet(source, unitPath, SourceCodeKind.InteractiveCode); break;
- default:
- throw PythonOps.ValueError("compile() arg 3 must be 'exec' or 'eval' or 'single'");
- }
-
- ScriptCode compiledCode = sourceUnit.Compile(opts, ThrowingErrorSink.Default);
- compiledCode.EnsureCompiled();
-
- FunctionCode res = new FunctionCode(compiledCode, cflags);
- res.SetFilename(filename);
- return res;
- }
-
- [Documentation("compile a unit of source code.\n\nThe source can be compiled either as exec, eval, or single.\nexec compiles the code as if it were a file\neval compiles the code as if were an expression\nsingle compiles a single statement\n\n")]
- public static object compile(CodeContext/*!*/ context, string source, string filename, string kind, object flags) {
- return compile(context, source, filename, kind, flags, null);
- }
-
- [Documentation("compile a unit of source code.\n\nThe source can be compiled either as exec, eval, or single.\nexec compiles the code as if it were a file\neval compiles the code as if were an expression\nsingle compiles a single statement\n\n")]
- public static object compile(CodeContext/*!*/ context, string source, string filename, string kind) {
- return compile(context, source, filename, kind, null, null);
- }
-
- public static PythonType classmethod {
- get {
- return DynamicHelpers.GetPythonTypeFromType(typeof(classmethod));
- }
- }
-
- public static int cmp(CodeContext/*!*/ context, object x, object y) {
- return PythonOps.Compare(context, x, y);
- }
-
- // having a cmp overload for double would be nice, but it breaks:
- // x = 1e66666
- // y = x/x
- // cmp(y,y)
- // which returns 0 because id(y) == id(y). If we added a double overload
- // we lose object identity.
-
- public static int cmp(CodeContext/*!*/ context, int x, int y) {
- return Int32Ops.Compare(x, y);
- }
-
- public static int cmp(CodeContext/*!*/ context, [NotNull]BigInteger x, [NotNull]BigInteger y) {
- if ((object)x == (object)y) {
- return 0;
- }
- return BigIntegerOps.Compare(x, y);
- }
-
- public static int cmp(CodeContext/*!*/ context, double x, [NotNull]BigInteger y) {
- return -BigIntegerOps.Compare(y, x);
- }
-
- public static int cmp(CodeContext/*!*/ context, [NotNull]BigInteger x, double y) {
- return BigIntegerOps.Compare(x, y);
- }
-
- public static int cmp(CodeContext/*!*/ context, [NotNull]string x, [NotNull]string y) {
- if ((object)x != (object)y) {
- int res = string.CompareOrdinal(x, y);
- if (res >= 1) {
- return 1;
- } else if (res <= -1) {
- return -1;
- }
- }
-
- return 0;
- }
-
- public static int cmp(CodeContext/*!*/ context, [NotNull]PythonTuple x, [NotNull]PythonTuple y) {
- if ((object)x == (object)y) {
- return 0;
- }
- return x.CompareTo(y);
- }
-
- public static PythonType complex {
- get {
- return DynamicHelpers.GetPythonTypeFromType(typeof(Complex64));
- }
- }
-
- public static void delattr(CodeContext/*!*/ context, object o, string name) {
- PythonOps.DeleteAttr(context, o, SymbolTable.StringToId(name));
- }
-
- public static PythonType dict {
- get {
- return DynamicHelpers.GetPythonTypeFromType(typeof(PythonDictionary));
- }
- }
-
- [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:ValidateArgumentsOfPublicMethods")]
- public static List dir(CodeContext/*!*/ context) {
- List res = PythonOps.MakeListFromSequence(LocalsAsAttributesCollection(context).Keys);
-
- res.sort(context);
- return res;
- }
-
- public static List dir(CodeContext/*!*/ context, object o) {
- IList<object> ret = PythonOps.GetAttrNames(context, o);
- List lret = new List(ret);
- lret.sort(context);
- return lret;
- }
-
- public static object divmod(CodeContext/*!*/ context, object x, object y) {
- Debug.Assert(NotImplementedType.Value != null);
-
- return PythonContext.GetContext(context).DivMod(x, y);
- }
-
- public static PythonType enumerate {
- get {
- return DynamicHelpers.GetPythonTypeFromType(typeof(Enumerate));
- }
- }
-
- public static object eval(CodeContext/*!*/ context, FunctionCode code) {
- Debug.Assert(context != null);
- if (code == null) throw PythonOps.TypeError("eval() argument 1 must be string or code object");
-
- return eval(context, code, null);
- }
-
- public static object eval(CodeContext/*!*/ context, FunctionCode code, IAttributesCollection globals) {
- Debug.Assert(context != null);
- if (code == null) throw PythonOps.TypeError("eval() argument 1 must be string or code object");
-
- return eval(context, code, globals, globals);
- }
-
- public static object eval(CodeContext/*!*/ context, FunctionCode code, IAttributesCollection globals, object locals) {
- Debug.Assert(context != null);
- if (code == null) throw PythonOps.TypeError("eval() argument 1 must be string or code object");
-
- Scope localScope = GetExecEvalScopeOptional(context, globals, locals, false);
- return code.Call(localScope);
- }
-
- internal static IAttributesCollection GetAttrLocals(CodeContext/*!*/ context, object locals) {
- IAttributesCollection attrLocals = null;
- if (locals == null) {
- if (context.Scope.Parent != null) {
- attrLocals = LocalsAsAttributesCollection(context);
- }
- } else {
- attrLocals = locals as IAttributesCollection ?? new PythonDictionary(new ObjectAttributesAdapter(context, locals));
- }
- return attrLocals;
- }
-
- public static object eval(CodeContext/*!*/ context, string expression) {
- Debug.Assert(context != null);
- if (expression == null) throw PythonOps.TypeError("eval() argument 1 must be string or code object");
-
- return eval(context, expression, globals(context), locals(context));
- }
-
- public static object eval(CodeContext/*!*/ context, string expression, IAttributesCollection globals) {
- Debug.Assert(context != null);
- if (expression == null) throw PythonOps.TypeError("eval() argument 1 must be string or code object");
-
- return eval(context, expression, globals, globals);
- }
-
- public static object eval(CodeContext/*!*/ context, string expression, IAttributesCollection globals, object locals) {
- Debug.Assert(context != null);
- if (expression == null) throw PythonOps.TypeError("eval() argument 1 must be string or code object");
-
- if (locals != null && PythonOps.IsMappingType(context, locals) == RuntimeHelpers.False) {
- throw PythonOps.TypeError("locals must be mapping");
- }
-
- var scope = GetExecEvalScopeOptional(context, globals, locals, false);
- var pythonContext = PythonContext.GetContext(context);
-
- // TODO: remove TrimStart
- var sourceUnit = pythonContext.CreateSnippet(expression.TrimStart(' ', '\t'), SourceCodeKind.Expression);
- var compilerOptions = GetDefaultCompilerOptions(context, true, 0);
- var scriptCode = pythonContext.CompileSourceCode(sourceUnit, compilerOptions, ThrowingErrorSink.Default, true); // interpret
-
- return scriptCode.Run(scope);
- }
-
- public static void execfile(CodeContext/*!*/ context, object/*!*/ filename) {
- execfile(context, filename, null, null);
- }
-
- public static void execfile(CodeContext/*!*/ context, object/*!*/ filename, object globals) {
- execfile(context, filename, globals, null);
- }
-
- public static void execfile(CodeContext/*!*/ context, object/*!*/ filename, object globals, object locals) {
- if (filename == null) {
- throw PythonOps.TypeError("execfile() argument 1 must be string, not None");
- }
-
- IAttributesCollection g = globals as IAttributesCollection;
- if (g == null && globals != null) {
- throw PythonOps.TypeError("execfile() arg 2 must be dictionary");
- }
-
- IAttributesCollection l = locals as IAttributesCollection;
- if (l == null && locals != null) {
- throw PythonOps.TypeError("execfile() arg 3 must be dictionary");
- }
-
- if (l == null) {
- l = g;
- }
-
- Scope execScope = GetExecEvalScopeOptional(context, g, l, true);
- string path = Converter.ConvertToString(filename);
- PythonContext pc = PythonContext.GetContext(context);
- if (!pc.DomainManager.Platform.FileExists(path)) {
- throw PythonOps.IOError("execfile: specified file doesn't exist");
- }
-
- SourceUnit sourceUnit = pc.CreateFileUnit(path, pc.DefaultEncoding, SourceCodeKind.Statements);
- ScriptCode code;
-
- try {
- code = sourceUnit.Compile(GetDefaultCompilerOptions(context, true, 0), ThrowingErrorSink.Default);
- } catch (UnauthorizedAccessException x) {
- throw PythonOps.IOError(x);
- }
-
- // Do not attempt evaluation mode for execfile
- code.Run(execScope);
- }
-
- public static PythonType file {
- get {
- return DynamicHelpers.GetPythonTypeFromType(typeof(PythonFile));
- }
- }
-
- public static string filter(CodeContext/*!*/ context, object function, [NotNull]string list) {
- if (function == null) return list;
- if (list == null) throw PythonOps.TypeError("NoneType is not iterable");
-
- StringBuilder sb = new StringBuilder();
- foreach (char c in list) {
- if (PythonOps.IsTrue(PythonCalls.Call(context, function, RuntimeHelpers.CharToString(c)))) sb.Append(c);
- }
-
- return sb.ToString();
- }
-
- public static string filter(CodeContext/*!*/ context, object function, [NotNull]ExtensibleString list) {
- StringBuilder sb = new StringBuilder();
- IEnumerator e = PythonOps.GetEnumerator(list);
- while (e.MoveNext()) {
- object o = e.Current;
- object t = (function != null) ? PythonCalls.Call(context, function, o) : o;
-
- if (PythonOps.IsTrue(t)) {
- sb.Append(Converter.ConvertToString(o));
- }
- }
- return sb.ToString();
- }
-
- /// <summary>
- /// Specialized version because enumerating tuples by Python's definition
- /// doesn't call __getitem__, but filter does!
- /// </summary>
- public static PythonTuple filter(CodeContext/*!*/ context, object function, [NotNull]PythonTuple tuple) {
- List<object> res = new List<object>(tuple.__len__());
-
- for (int i = 0; i < tuple.__len__(); i++) {
- object obj = tuple[i];
- object t = (function != null) ? PythonCalls.Call(context, function, obj) : obj;
-
- if (PythonOps.IsTrue(t)) {
- res.Add(obj);
- }
- }
-
- return PythonTuple.MakeTuple(res.ToArray());
- }
-
- public static List filter(CodeContext/*!*/ context, object function, object list) {
- if (list == null) throw PythonOps.TypeError("NoneType is not iterable");
- List ret = new List();
-
- IEnumerator i = PythonOps.GetEnumerator(list);
- while (i.MoveNext()) {
- if (function == null) {
- if (PythonOps.IsTrue(i.Current)) ret.AddNoLock(i.Current);
- } else {
- if (PythonOps.IsTrue(PythonCalls.Call(context, function, i.Current))) ret.AddNoLock(i.Current);
- }
- }
-
- return ret;
- }
-
- public static PythonType @float {
- get {
- return DynamicHelpers.GetPythonTypeFromType(typeof(double));
- }
- }
-
- public static object getattr(CodeContext/*!*/ context, object o, string name) {
- return PythonOps.GetBoundAttr(context, o, SymbolTable.StringToId(name));
- }
-
- public static object getattr(CodeContext/*!*/ context, object o, string name, object def) {
- object ret;
- if (PythonOps.TryGetBoundAttr(context, o, SymbolTable.StringToId(name), out ret)) return ret;
- else return def;
- }
-
- [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:ValidateArgumentsOfPublicMethods")]
- public static IAttributesCollection globals(CodeContext/*!*/ context) {
- return new PythonDictionary(new GlobalScopeDictionaryStorage(context.Scope));
- }
-
- public static bool hasattr(CodeContext/*!*/ context, object o, string name) {
- return PythonOps.HasAttr(context, o, SymbolTable.StringToId(name));
- }
-
- public static int hash(CodeContext/*!*/ context, object o) {
- return PythonContext.GetContext(context).Hash(o);
- }
-
- public static int hash(CodeContext/*!*/ context, [NotNull]PythonTuple o) {
- return ((IValueEquality)o).GetValueHashCode();
- }
-
- // this is necessary because overload resolution selects the int form.
- public static int hash(CodeContext/*!*/ context, char o) {
- return PythonContext.GetContext(context).Hash(o);
- }
-
- public static int hash(CodeContext/*!*/ context, int o) {
- return o;
- }
-
- public static int hash(CodeContext/*!*/ context, [NotNull]string o) {
- return o.GetHashCode();
- }
-
- // this is necessary because overload resolution will coerce extensible strings to strings.
- public static int hash(CodeContext/*!*/ context, [NotNull]ExtensibleString o) {
- return hash(context, (object)o);
- }
-
- public static int hash(CodeContext/*!*/ context, [NotNull]BigInteger o) {
- return BigIntegerOps.__hash__(o);
- }
-
- public static int hash(CodeContext/*!*/ context, [NotNull]Extensible<BigInteger> o) {
- return hash(context, (object)o);
- }
-
- public static int hash(CodeContext/*!*/ context, double o) {
- return DoubleOps.__hash__(o);
- }
-
- public static void help(CodeContext/*!*/ context, object o) {
- StringBuilder doc = new StringBuilder();
- List<object> doced = new List<object>(); // document things only once
-
- help(context, doced, doc, 0, o);
-
- if (doc.Length == 0) {
- if (!(o is string)) {
- help(context, DynamicHelpers.GetPythonType(o));
- return;
- }
- doc.Append("no documentation found for ");
- doc.Append(PythonOps.Repr(context, o));
- }
-
- string[] strings = doc.ToString().Split('\n');
- for (int i = 0; i < strings.Length; i++) {
- /* should read only a key, not a line, but we don't seem
- * to have a way to do that...
- if ((i % Console.WindowHeight) == 0) {
- Ops.Print(context.SystemState, "-- More --");
- Ops.ReadLineFromSrc(context.SystemState);
- }*/
- PythonOps.Print(context, strings[i]);
- }
- }
-
- private static void help(CodeContext/*!*/ context, List<object>/*!*/ doced, StringBuilder/*!*/ doc, int indent, object obj) {
- PythonType type;
- BuiltinFunction builtinFunction;
- PythonFunction function;
- BuiltinMethodDescriptor methodDesc;
- Method method;
- string strVal;
- Scope scope;
- OldClass oldClass;
-
- if (doced.Contains(obj)) return; // document things only once
- doced.Add(obj);
-
- if ((strVal = obj as string) != null) {
- if (indent != 0) return;
-
- // try and find things that string could refer to,
- // then call help on them.
- foreach (object module in PythonContext.GetContext(context).SystemStateModules.Values) {
- IList<object> attrs = PythonOps.GetAttrNames(context, module);
- List candidates = new List();
- foreach (string s in attrs) {
- if (s == strVal) {
- object modVal;
- if (!PythonOps.TryGetBoundAttr(context, module, SymbolTable.StringToId(strVal), out modVal))
- continue;
-
- candidates.append(modVal);
- }
- }
-
- // favor types, then built-in functions, then python functions,
- // and then only display help for one.
- type = null;
- builtinFunction = null;
- function = null;
- for (int i = 0; i < candidates.__len__(); i++) {
- if ((type = candidates[i] as PythonType) != null) {
- break;
- }
-
- if (builtinFunction == null && (builtinFunction = candidates[i] as BuiltinFunction) != null)
- continue;
-
- if (function == null && (function = candidates[i] as PythonFunction) != null)
- continue;
- }
-
- if (type != null) help(context, doced, doc, indent, type);
- else if (builtinFunction != null) help(context, doced, doc, indent, builtinFunction);
- else if (function != null) help(context, doced, doc, indent, function);
- }
- } else if ((type = obj as PythonType) != null) {
- // find all the functions, and display their
- // documentation
- if (indent == 0) {
- doc.AppendFormat("Help on {0} in module {1}\n\n", type.Name, PythonOps.GetBoundAttr(context, type, Symbols.Module));
- }
-
- PythonTypeSlot dts;
- if (type.TryResolveSlot(context, Symbols.Doc, out dts)) {
- object docText;
- if (dts.TryGetValue(context, null, type, out docText) && docText != null)
- AppendMultiLine(doc, docText.ToString() + Environment.NewLine, indent);
- AppendIndent(doc, indent);
- doc.AppendLine("Data and other attributes defined here:");
- AppendIndent(doc, indent);
- doc.AppendLine();
- }
-
- List names = type.GetMemberNames(context);
- names.sort(context);
-
- foreach (string name in names) {
- if (name == "__class__") continue;
-
- PythonTypeSlot value;
- object val;
- if (type.TryLookupSlot(context, SymbolTable.StringToId(name), out value) &&
- value.TryGetValue(context, null, type, out val)) {
- help(context, doced, doc, indent + 1, val);
- }
- }
- } else if ((methodDesc = obj as BuiltinMethodDescriptor) != null) {
- if (indent == 0) doc.AppendFormat("Help on method-descriptor {0}\n\n", methodDesc.__name__);
- AppendIndent(doc, indent);
- doc.Append(methodDesc.__name__);
- doc.Append("(...)\n");
-
- AppendMultiLine(doc, methodDesc.__doc__, indent + 1);
- } else if ((builtinFunction = obj as BuiltinFunction) != null) {
- if (indent == 0) doc.AppendFormat("Help on built-in function {0}\n\n", builtinFunction.Name);
- AppendIndent(doc, indent);
- doc.Append(builtinFunction.Name);
- doc.Append("(...)\n");
-
- AppendMultiLine(doc, builtinFunction.__doc__, indent + 1);
- } else if ((function = obj as PythonFunction) != null) {
- if (indent == 0) doc.AppendFormat("Help on function {0} in module {1}:\n\n", function.__name__, function.__module__);
-
- AppendIndent(doc, indent);
- doc.Append(function.GetSignatureString());
- string pfDoc = Converter.ConvertToString(function.__doc__);
- if (!String.IsNullOrEmpty(pfDoc)) {
- AppendMultiLine(doc, pfDoc, indent);
- }
- } else if ((method = obj as Method) != null && ((function = method.im_func as PythonFunction) != null)) {
- if (indent == 0) doc.AppendFormat("Help on method {0} in module {1}:\n\n", function.__name__, function.__module__);
-
- AppendIndent(doc, indent);
- doc.Append(function.GetSignatureString());
-
- if (method.im_self == null) {
- doc.AppendFormat(" unbound {0} method\n", PythonOps.ToString(method.im_class));
- } else {
- doc.AppendFormat(" method of {0} instance\n", PythonOps.ToString(method.im_class));
- }
-
- string pfDoc = Converter.ConvertToString(function.__doc__);
- if (!String.IsNullOrEmpty(pfDoc)) {
- AppendMultiLine(doc, pfDoc, indent);
- }
- } else if ((scope = obj as Scope) != null) {
- foreach (SymbolId name in scope.Keys) {
- if (name == Symbols.Class || name == Symbols.Builtins) continue;
-
- object value;
- if (scope.TryGetName(name, out value)) {
- help(context, doced, doc, indent + 1, value);
- }
- }
- } else if ((oldClass = obj as OldClass) != null) {
- if (indent == 0) {
- doc.AppendFormat("Help on {0} in module {1}\n\n", oldClass.__name__, PythonOps.GetBoundAttr(context, oldClass, Symbols.Module));
- }
-
- object docText;
- if (oldClass.TryLookupSlot(Symbols.Doc, out docText) && docText != null) {
- AppendMultiLine(doc, docText.ToString() + Environment.NewLine, indent);
- AppendIndent(doc, indent);
- doc.AppendLine("Data and other attributes defined here:");
- AppendIndent(doc, indent);
- doc.AppendLine();
- }
-
- IList<object> names = ((IMembersList)oldClass).GetMemberNames(context);
- List sortNames = new List(names);
- sortNames.sort(context);
- names = sortNames;
- foreach (string name in names) {
- if (name == "__class__") continue;
-
- object value;
-
- if (oldClass.TryLookupSlot(SymbolTable.StringToId(name), out value))
- help(context, doced, doc, indent + 1, value);
- }
- }
- }
-
- private static void AppendMultiLine(StringBuilder doc, string multiline, int indent) {
- string[] docs = multiline.Split('\n');
- for (int i = 0; i < docs.Length; i++) {
- AppendIndent(doc, indent + 1);
- doc.Append(docs[i]);
- doc.Append('\n');
- }
- }
-
- private static void AppendIndent(StringBuilder doc, int indent) {
- doc.Append(" | ");
- for (int i = 0; i < indent; i++) doc.Append(" ");
- }
-
- //??? type this to string
- public static object hex(object o) {
- return PythonOps.Hex(o);
- }
-
- public static object id(object o) {
- long res = PythonOps.Id(o);
- if (PythonOps.Id(o) <= Int32.MaxValue) {
- return (int)res;
- }
- return (BigInteger)res;
- }
-
- public static object input(CodeContext/*!*/ context) {
- return input(context, null);
- }
-
- public static object input(CodeContext/*!*/ context, object prompt) {
- return eval(context, raw_input(context, prompt));
- }
-
- public static PythonType @int {
- get {
- return DynamicHelpers.GetPythonTypeFromType(typeof(int));
- }
- }
-
- public static string intern(object o) {
- string s = o as string;
- if (s == null) {
- throw PythonOps.TypeError("intern: argument must be string");
- }
- return string.Intern(s);
- }
-
- public static bool isinstance(object o, [NotNull]PythonType typeinfo) {
- return PythonOps.IsInstance(o, typeinfo);
- }
-
- public static bool isinstance(object o, [NotNull]PythonTuple typeinfo) {
- return PythonOps.IsInstance(o, typeinfo);
- }
-
- public static bool isinstance(object o, object typeinfo) {
- return PythonOps.IsInstance(o, typeinfo);
- }
-
- public static bool issubclass([NotNull]OldClass c, object typeinfo) {
- return PythonOps.IsSubClass(c.TypeObject, typeinfo);
- }
-
- public static bool issubclass([NotNull]PythonType c, object typeinfo) {
- return PythonOps.IsSubClass(c, typeinfo);
- }
-
- public static bool issubclass([NotNull]PythonType c, [NotNull]PythonType typeinfo) {
- return PythonOps.IsSubClass(c, typeinfo);
- }
-
- public static bool issubclass(CodeContext/*!*/ context, object o, object typeinfo) {
- PythonTuple pt = typeinfo as PythonTuple;
- if (pt != null) {
- // Recursively inspect nested tuple(s)
- foreach (object subTypeInfo in pt) {
- try {
- PythonOps.FunctionPushFrame();
- if (issubclass(context, o, subTypeInfo)) {
- return true;
- }
- } finally {
- PythonOps.FunctionPopFrame();
- }
- }
- return false;
- }
-
- object bases;
- PythonTuple tupleBases;
-
- if (!PythonOps.TryGetBoundAttr(o, Symbols.Bases, out bases) || (tupleBases = bases as PythonTuple) == null) {
- throw PythonOps.TypeError("issubclass() arg 1 must be a class");
- }
-
- foreach (object baseCls in tupleBases) {
- PythonType pyType;
- OldClass oc;
-
- if (baseCls == typeinfo) {
- return true;
- } else if ((pyType = baseCls as PythonType) != null) {
- if (issubclass(pyType, typeinfo)) {
- return true;
- }
- } else if ((oc = baseCls as OldClass) != null) {
- if (issubclass(oc, typeinfo)) {
- return true;
- }
- } else if (hasattr(context, baseCls, "__bases__")) {
- if (issubclass(context, baseCls, typeinfo)) {
- return true;
- }
- }
- }
-
- return false;
- }
-
- public static IEnumerator iter(object o) {
- return PythonOps.GetEnumerator(o);
- }
-
- public static object iter(CodeContext/*!*/ context, object func, object sentinel) {
- if (!PythonOps.IsCallable(context, func)) {
- throw PythonOps.TypeError("iter(v, w): v must be callable");
- }
- return new SentinelIterator(context, func, sentinel);
- }
-
- public static int len(object o) {
- return PythonOps.Length(o);
- }
-
-
- public static PythonType set {
- get {
- return DynamicHelpers.GetPythonTypeFromType(typeof(SetCollection));
- }
- }
-
- public static PythonType frozenset {
- get {
- return DynamicHelpers.GetPythonTypeFromType(typeof(FrozenSetCollection));
- }
- }
-
- public static PythonType list {
- get {
- return DynamicHelpers.GetPythonTypeFromType(typeof(List));
- }
- }
-
- [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:ValidateArgumentsOfPublicMethods")]
- public static object locals(CodeContext/*!*/ context) {
- ObjectAttributesAdapter adapter = context.Scope.Dict as ObjectAttributesAdapter;
- if (adapter != null) {
- // we've wrapped Locals in an IAttributesCollection, give the user back the
- // original object.
- return adapter.Backing;
- }
-
- return LocalScopeDictionaryStorage.GetObjectFromScope(context.Scope);
- }
-
- internal static IAttributesCollection LocalsAsAttributesCollection(CodeContext/*!*/ context) {
- return LocalScopeDictionaryStorage.GetDictionaryFromScope(context.Scope);
- }
-
- public static PythonType @long {
- get {
- return TypeCache.BigInteger;
- }
- }
-
- private static CallSite<Func<CallSite, CodeContext, T, T1, object>> MakeMapSite<T, T1>(CodeContext/*!*/ context) {
- return CallSite<Func<CallSite, CodeContext, T, T1, object>>.Create(
- new InvokeBinder(
- PythonContext.GetContext(context).DefaultBinderState,
- new CallSignature(1)
- )
- );
- }
-
- public static List map(CodeContext/*!*/ context, SiteLocalStorage<CallSite<Func<CallSite, CodeContext, object, object, object>>> storage, object func, IEnumerable enumerator) {
- if (enumerator == null) {
- throw PythonOps.TypeError("NoneType is not iterable");
- }
-
- List ret = new List();
- CallSite<Func<CallSite, CodeContext, object, object, object>> mapSite = null;
-
- if (func != null) {
- mapSite = MakeMapSite<object, object>(context);
- }
-
- foreach (object o in enumerator) {
- if (func == null) {
- ret.AddNoLock(o);
- } else {
- ret.AddNoLock(mapSite.Target(mapSite, context, func, o));
- }
- }
-
- return ret;
- }
-
- public static List map(CodeContext/*!*/ context, SiteLocalStorage<CallSite<Func<CallSite, CodeContext, object, object, object>>> storage, object func, [NotNull]string enumerator) {
- List ret = new List(enumerator.Length);
- CallSite<Func<CallSite, CodeContext, object, object, object>> mapSite = null;
-
- if (func != null) {
- if (storage.Data == null) {
- storage.Data = MakeMapSite<object, object>(context);
- }
- mapSite = storage.Data;
- }
-
- foreach (char o in enumerator) {
- if (func == null) {
- ret.AddNoLock(RuntimeHelpers.CharToString(o));
- } else {
- ret.AddNoLock(mapSite.Target(mapSite, context, func, RuntimeHelpers.CharToString(o)));
- }
- }
-
- return ret;
- }
-
- public static List map(CodeContext/*!*/ context, SiteLocalStorage<CallSite<Func<CallSite, CodeContext, PythonType, object, object>>> storage, [NotNull]PythonType/*!*/ func, [NotNull]IEnumerable enumerator) {
- CallSite<Func<CallSite, CodeContext, PythonType, object, object>> mapSite;
- if (storage.Data == null) {
- storage.Data = MakeMapSite<PythonType, object>(context);
- }
- mapSite = storage.Data;
-
- List ret = new List();
- foreach (object o in enumerator) {
- ret.AddNoLock(mapSite.Target(mapSite, context, func, o));
- }
- return ret;
- }
-
- public static List map(CodeContext/*!*/ context, SiteLocalStorage<CallSite<Func<CallSite, CodeContext, BuiltinFunction, object, object>>> storage, [NotNull]BuiltinFunction/*!*/ func, [NotNull]IEnumerable enumerator) {
- CallSite<Func<CallSite, CodeContext, BuiltinFunction, object, object>> mapSite;
- if (storage.Data == null) {
- storage.Data = MakeMapSite<BuiltinFunction, object>(context);
- }
- mapSite = storage.Data;
-
- List ret = new List();
- foreach (object o in enumerator) {
- ret.AddNoLock(mapSite.Target(mapSite, context, func, o));
- }
- return ret;
- }
-
- public static List map(CodeContext/*!*/ context, SiteLocalStorage<CallSite<Func<CallSite, CodeContext, BuiltinFunction, object, object>>> storage, [NotNull]BuiltinFunction/*!*/ func, [NotNull]string enumerator) {
- CallSite<Func<CallSite, CodeContext, BuiltinFunction, object, object>> mapSite;
- if (storage.Data == null) {
- storage.Data = MakeMapSite<BuiltinFunction, object>(context);
- }
- mapSite = storage.Data;
-
- List ret = new List(enumerator.Length);
- foreach (char o in enumerator) {
- ret.AddNoLock(mapSite.Target(mapSite, context, func, RuntimeHelpers.CharToString(o)));
- }
- return ret;
- }
-
- public static List map(CodeContext/*!*/ context, SiteLocalStorage<CallSite<Func<CallSite, CodeContext, PythonType, object, object>>> storage, [NotNull]PythonType/*!*/ func, [NotNull]string enumerator) {
- CallSite<Func<CallSite, CodeContext, PythonType, object, object>> mapSite;
- if (storage.Data == null) {
- storage.Data = MakeMapSite<PythonType, object>(context);
- }
- mapSite = storage.Data;
-
- List ret = new List(enumerator.Length);
- foreach (char o in enumerator) {
- ret.AddNoLock(mapSite.Target(mapSite, context, func, RuntimeHelpers.CharToString(o)));
- }
- return ret;
- }
-
- public static List map(CodeContext/*!*/ context, SiteLocalStorage<CallSite<Func<CallSite, CodeContext, PythonFunction, object, object>>> storage, [NotNull]PythonFunction/*!*/ func, [NotNull]IList enumerator) {
- CallSite<Func<CallSite, CodeContext, PythonFunction, object, object>> mapSite;
- if (storage.Data == null) {
- storage.Data = MakeMapSite<PythonFunction, object>(context);
- }
- mapSite = storage.Data;
-
- List ret = new List(enumerator.Count);
- foreach (object o in enumerator) {
- ret.AddNoLock(mapSite.Target(mapSite, context, func, o));
- }
- return ret;
- }
-
- public static List map(CodeContext/*!*/ context, params object[] param) {
- if (param == null || param.Length < 2) {
- throw PythonOps.TypeError("at least 2 arguments required to map");
- }
- List ret = new List();
- object func = param[0];
-
- IEnumerator[] enums = new IEnumerator[param.Length - 1];
- for (int i = 0; i < enums.Length; i++) {
- enums[i] = PythonOps.GetEnumerator(param[i + 1]);
- }
-
- object[] args = new object[enums.Length];
- while (true) {
- bool done = true;
- for (int i = 0; i < enums.Length; i++) {
- if (enums[i].MoveNext()) {
- args[i] = enums[i].Current;
- done = false;
- } else {
- args[i] = null;
- }
- }
- if (done) {
- return ret;
- }
- if (func != null) {
- // splat call w/ args, can't use site here yet...
- ret.AddNoLock(PythonCalls.Call(context, func, args));
- } else if (args.Length == 1) {
- ret.AddNoLock(args[0]);
- } else {
- ret.AddNoLock(PythonTuple.MakeTuple(args));
- args = new object[enums.Length]; // Tuple does not copy the array, allocate new one.
- }
- }
- }
-
- public static object max(CodeContext/*!*/ context, object x) {
- IEnumerator i = PythonOps.GetEnumerator(x);
- if (!i.MoveNext())
- throw PythonOps.ValueError("max() arg is an empty sequence");
- object ret = i.Current;
- PythonContext pc = PythonContext.GetContext(context);
- …
Large files files are truncated, but you can click here to view the full file