PageRenderTime 79ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/IronPython_Main/Languages/IronPython/IronPython/Runtime/Operations/PythonOps.cs

#
C# | 4560 lines | 3333 code | 813 blank | 414 comment | 946 complexity | 4de2c747f53fd5d597c3ab5c514ee4aa 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
  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 !CLR2
  16. using System.Linq.Expressions;
  17. using System.Numerics;
  18. using Microsoft.Scripting.Ast;
  19. #else
  20. using Microsoft.Scripting.Ast;
  21. using Microsoft.Scripting.Math;
  22. using Complex = Microsoft.Scripting.Math.Complex64;
  23. #endif
  24. using System;
  25. using System.Collections;
  26. using System.Collections.Generic;
  27. using System.Diagnostics;
  28. using System.Dynamic;
  29. using System.IO;
  30. using System.Reflection;
  31. using System.Reflection.Emit;
  32. using System.Runtime.CompilerServices;
  33. using System.Runtime.InteropServices;
  34. using System.Text;
  35. using System.Threading;
  36. using Microsoft.Scripting;
  37. using Microsoft.Scripting.Actions;
  38. using Microsoft.Scripting.Generation;
  39. using Microsoft.Scripting.Hosting.Providers;
  40. using Microsoft.Scripting.Hosting.Shell;
  41. using Microsoft.Scripting.Runtime;
  42. using Microsoft.Scripting.Utils;
  43. using IronPython.Compiler;
  44. using IronPython.Hosting;
  45. using IronPython.Modules;
  46. using IronPython.Runtime.Binding;
  47. using IronPython.Runtime.Exceptions;
  48. using IronPython.Runtime.Types;
  49. #if !SILVERLIGHT
  50. using System.ComponentModel;
  51. #endif
  52. namespace IronPython.Runtime.Operations {
  53. /// <summary>
  54. /// Contains functions that are called directly from
  55. /// generated code to perform low-level runtime functionality.
  56. /// </summary>
  57. public static partial class PythonOps {
  58. #region Shared static data
  59. [ThreadStatic]
  60. private static List<object> InfiniteRepr;
  61. // The "current" exception on this thread that will be returned via sys.exc_info()
  62. [ThreadStatic]
  63. internal static Exception RawException;
  64. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes")]
  65. public static readonly PythonTuple EmptyTuple = PythonTuple.EMPTY;
  66. private static readonly Type[] _DelegateCtorSignature = new Type[] { typeof(object), typeof(IntPtr) };
  67. #endregion
  68. public static BigInteger MakeIntegerFromHex(string s) {
  69. return LiteralParser.ParseBigInteger(s, 16);
  70. }
  71. public static PythonDictionary MakeDict(int size) {
  72. return new PythonDictionary(size);
  73. }
  74. public static PythonDictionary MakeEmptyDict() {
  75. return new PythonDictionary(EmptyDictionaryStorage.Instance);
  76. }
  77. /// <summary>
  78. /// Creates a new dictionary extracting the keys and values from the
  79. /// provided data array. Keys/values are adjacent in the array with
  80. /// the value coming first.
  81. /// </summary>
  82. public static PythonDictionary MakeDictFromItems(params object[] data) {
  83. return new PythonDictionary(new CommonDictionaryStorage(data, false));
  84. }
  85. public static PythonDictionary MakeConstantDict(object items) {
  86. return new PythonDictionary((ConstantDictionaryStorage)items);
  87. }
  88. public static object MakeConstantDictStorage(params object[] data) {
  89. return new ConstantDictionaryStorage(new CommonDictionaryStorage(data, false));
  90. }
  91. public static SetCollection MakeSet(params object[] items) {
  92. return new SetCollection(items);
  93. }
  94. public static SetCollection MakeEmptySet() {
  95. return new SetCollection();
  96. }
  97. /// <summary>
  98. /// Creates a new dictionary extracting the keys and values from the
  99. /// provided data array. Keys/values are adjacent in the array with
  100. /// the value coming first.
  101. /// </summary>
  102. public static PythonDictionary MakeHomogeneousDictFromItems(object[] data) {
  103. return new PythonDictionary(new CommonDictionaryStorage(data, true));
  104. }
  105. public static bool IsCallable(CodeContext/*!*/ context, object o) {
  106. // This tells if an object can be called, but does not make a claim about the parameter list.
  107. // In 1.x, we could check for certain interfaces like ICallable*, but those interfaces were deprecated
  108. // in favor of dynamic sites.
  109. // This is difficult to infer because we'd need to simulate the entire callbinder, which can include
  110. // looking for [SpecialName] call methods and checking for a rule from IDynamicMetaObjectProvider. But even that wouldn't
  111. // be complete since sites require the argument list of the call, and we only have the instance here.
  112. // Thus check a dedicated IsCallable operator. This lets each object describe if it's callable.
  113. // Invoke Operator.IsCallable on the object.
  114. return PythonContext.GetContext(context).IsCallable(o);
  115. }
  116. public static bool UserObjectIsCallable(CodeContext/*!*/ context, object o) {
  117. object callFunc;
  118. return TryGetBoundAttr(context, o, "__call__", out callFunc) && callFunc != null;
  119. }
  120. public static bool IsTrue(object o) {
  121. return Converter.ConvertToBoolean(o);
  122. }
  123. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1002:DoNotExposeGenericLists")]
  124. public static List<object> GetReprInfinite() {
  125. if (InfiniteRepr == null) {
  126. InfiniteRepr = new List<object>();
  127. }
  128. return InfiniteRepr;
  129. }
  130. [LightThrowing]
  131. internal static object LookupEncodingError(CodeContext/*!*/ context, string name) {
  132. Dictionary<string, object> errorHandlers = PythonContext.GetContext(context).ErrorHandlers;
  133. lock (errorHandlers) {
  134. if (errorHandlers.ContainsKey(name))
  135. return errorHandlers[name];
  136. else
  137. return LightExceptions.Throw(PythonOps.LookupError("unknown error handler name '{0}'", name));
  138. }
  139. }
  140. internal static void RegisterEncodingError(CodeContext/*!*/ context, string name, object handler) {
  141. Dictionary<string, object> errorHandlers = PythonContext.GetContext(context).ErrorHandlers;
  142. lock (errorHandlers) {
  143. if (!PythonOps.IsCallable(context, handler))
  144. throw PythonOps.TypeError("handler must be callable");
  145. errorHandlers[name] = handler;
  146. }
  147. }
  148. internal static PythonTuple LookupEncoding(CodeContext/*!*/ context, string encoding) {
  149. PythonContext.GetContext(context).EnsureEncodings();
  150. List<object> searchFunctions = PythonContext.GetContext(context).SearchFunctions;
  151. string normalized = encoding.ToLower().Replace(' ', '-');
  152. if (normalized.IndexOf(Char.MinValue) != -1) {
  153. throw PythonOps.TypeError("lookup string cannot contain null character");
  154. }
  155. lock (searchFunctions) {
  156. for (int i = 0; i < searchFunctions.Count; i++) {
  157. object res = PythonCalls.Call(context, searchFunctions[i], normalized);
  158. if (res != null) return (PythonTuple)res;
  159. }
  160. }
  161. throw PythonOps.LookupError("unknown encoding: {0}", encoding);
  162. }
  163. internal static void RegisterEncoding(CodeContext/*!*/ context, object search_function) {
  164. if (!PythonOps.IsCallable(context, search_function))
  165. throw PythonOps.TypeError("search_function must be callable");
  166. List<object> searchFunctions = PythonContext.GetContext(context).SearchFunctions;
  167. lock (searchFunctions) {
  168. searchFunctions.Add(search_function);
  169. }
  170. }
  171. internal static string GetPythonTypeName(object obj) {
  172. OldInstance oi = obj as OldInstance;
  173. if (oi != null) return oi._class._name.ToString();
  174. else return PythonTypeOps.GetName(obj);
  175. }
  176. public static string Repr(CodeContext/*!*/ context, object o) {
  177. if (o == null) return "None";
  178. string s;
  179. if ((s = o as string) != null) return StringOps.__repr__(s);
  180. if (o is int) return Int32Ops.__repr__((int)o);
  181. if (o is long) return ((long)o).ToString() + "L";
  182. // could be a container object, we need to detect recursion, but only
  183. // for our own built-in types that we're aware of. The user can setup
  184. // infinite recursion in their own class if they want.
  185. ICodeFormattable f = o as ICodeFormattable;
  186. if (f != null) {
  187. return f.__repr__(context);
  188. }
  189. PerfTrack.NoteEvent(PerfTrack.Categories.Temporary, "Repr " + o.GetType().FullName);
  190. return PythonContext.InvokeUnaryOperator(context, UnaryOperators.Repr, o) as string;
  191. }
  192. public static List<object> GetAndCheckInfinite(object o) {
  193. List<object> infinite = GetReprInfinite();
  194. foreach (object o2 in infinite) {
  195. if (o == o2) {
  196. return null;
  197. }
  198. }
  199. return infinite;
  200. }
  201. public static string ToString(object o) {
  202. return ToString(DefaultContext.Default, o);
  203. }
  204. public static string ToString(CodeContext/*!*/ context, object o) {
  205. string x = o as string;
  206. PythonType dt;
  207. OldClass oc;
  208. if (x != null) return x;
  209. if (o == null) return "None";
  210. if (o is double) return DoubleOps.__str__(context, (double)o);
  211. if ((dt = o as PythonType) != null) return dt.__repr__(DefaultContext.Default);
  212. if ((oc = o as OldClass) != null) return oc.ToString();
  213. object value = PythonContext.InvokeUnaryOperator(context, UnaryOperators.String, o);
  214. string ret = value as string;
  215. if (ret == null) {
  216. Extensible<string> es = value as Extensible<string>;
  217. if (es == null) {
  218. throw PythonOps.TypeError("expected str, got {0} from __str__", PythonTypeOps.GetName(value));
  219. }
  220. ret = es.Value;
  221. }
  222. return ret;
  223. }
  224. public static string FormatString(CodeContext/*!*/ context, string str, object data) {
  225. return new StringFormatter(context, str, data).Format();
  226. }
  227. public static string FormatUnicode(CodeContext/*!*/ context, string str, object data) {
  228. return new StringFormatter(context, str, data, true).Format();
  229. }
  230. public static object Plus(object o) {
  231. object ret;
  232. if (o is int) return o;
  233. else if (o is double) return o;
  234. else if (o is BigInteger) return o;
  235. else if (o is Complex) return o;
  236. else if (o is long) return o;
  237. else if (o is float) return o;
  238. else if (o is bool) return ScriptingRuntimeHelpers.Int32ToObject((bool)o ? 1 : 0);
  239. if (PythonTypeOps.TryInvokeUnaryOperator(DefaultContext.Default, o, "__pos__", out ret) &&
  240. ret != NotImplementedType.Value) {
  241. return ret;
  242. }
  243. throw PythonOps.TypeError("bad operand type for unary +");
  244. }
  245. public static object Negate(object o) {
  246. if (o is int) return Int32Ops.Negate((int)o);
  247. else if (o is double) return DoubleOps.Negate((double)o);
  248. else if (o is long) return Int64Ops.Negate((long)o);
  249. else if (o is BigInteger) return BigIntegerOps.Negate((BigInteger)o);
  250. else if (o is Complex) return -(Complex)o;
  251. else if (o is float) return DoubleOps.Negate((float)o);
  252. else if (o is bool) return ScriptingRuntimeHelpers.Int32ToObject((bool)o ? -1 : 0);
  253. object ret;
  254. if (PythonTypeOps.TryInvokeUnaryOperator(DefaultContext.Default, o, "__neg__", out ret) &&
  255. ret != NotImplementedType.Value) {
  256. return ret;
  257. }
  258. throw PythonOps.TypeError("bad operand type for unary -");
  259. }
  260. public static bool IsSubClass(PythonType/*!*/ c, PythonType/*!*/ typeinfo) {
  261. Assert.NotNull(c, typeinfo);
  262. if (c.OldClass != null) {
  263. return typeinfo.__subclasscheck__(c.OldClass);
  264. }
  265. return typeinfo.__subclasscheck__(c);
  266. }
  267. public static bool IsSubClass(CodeContext/*!*/ context, PythonType c, object typeinfo) {
  268. if (c == null) throw PythonOps.TypeError("issubclass: arg 1 must be a class");
  269. if (typeinfo == null) throw PythonOps.TypeError("issubclass: arg 2 must be a class");
  270. PythonTuple pt = typeinfo as PythonTuple;
  271. PythonContext pyContext = PythonContext.GetContext(context);
  272. if (pt != null) {
  273. // Recursively inspect nested tuple(s)
  274. foreach (object o in pt) {
  275. try {
  276. FunctionPushFrame(pyContext);
  277. if (IsSubClass(context, c, o)) {
  278. return true;
  279. }
  280. } finally {
  281. FunctionPopFrame();
  282. }
  283. }
  284. return false;
  285. }
  286. OldClass oc = typeinfo as OldClass;
  287. if (oc != null) {
  288. return c.IsSubclassOf(oc.TypeObject);
  289. }
  290. Type t = typeinfo as Type;
  291. if (t != null) {
  292. typeinfo = DynamicHelpers.GetPythonTypeFromType(t);
  293. }
  294. object bases;
  295. PythonType dt = typeinfo as PythonType;
  296. if (dt == null) {
  297. if (!PythonOps.TryGetBoundAttr(typeinfo, "__bases__", out bases)) {
  298. //!!! deal with classes w/ just __bases__ defined.
  299. throw PythonOps.TypeErrorForBadInstance("issubclass(): {0} is not a class nor a tuple of classes", typeinfo);
  300. }
  301. IEnumerator ie = PythonOps.GetEnumerator(bases);
  302. while (ie.MoveNext()) {
  303. PythonType baseType = ie.Current as PythonType;
  304. if (baseType == null) {
  305. OldClass ocType = ie.Current as OldClass;
  306. if (ocType == null) {
  307. continue;
  308. }
  309. baseType = ocType.TypeObject;
  310. }
  311. if (c.IsSubclassOf(baseType)) return true;
  312. }
  313. return false;
  314. }
  315. return IsSubClass(c, dt);
  316. }
  317. public static bool IsInstance(object o, PythonType typeinfo) {
  318. if (typeinfo.__instancecheck__(o)) {
  319. return true;
  320. }
  321. return IsInstanceDynamic(o, typeinfo, DynamicHelpers.GetPythonType(o));
  322. }
  323. public static bool IsInstance(CodeContext/*!*/ context, object o, PythonTuple typeinfo) {
  324. PythonContext pyContext = PythonContext.GetContext(context);
  325. foreach (object type in typeinfo) {
  326. try {
  327. PythonOps.FunctionPushFrame(pyContext);
  328. if (type is PythonType) {
  329. if (IsInstance(o, (PythonType)type)) {
  330. return true;
  331. }
  332. } else if (type is PythonTuple) {
  333. if (IsInstance(context, o, (PythonTuple)type)) {
  334. return true;
  335. }
  336. } else if (IsInstance(context, o, type)) {
  337. return true;
  338. }
  339. } finally {
  340. PythonOps.FunctionPopFrame();
  341. }
  342. }
  343. return false;
  344. }
  345. public static bool IsInstance(CodeContext/*!*/ context, object o, object typeinfo) {
  346. if (typeinfo == null) throw PythonOps.TypeError("isinstance: arg 2 must be a class, type, or tuple of classes and types");
  347. PythonTuple tt = typeinfo as PythonTuple;
  348. if (tt != null) {
  349. return IsInstance(context, o, tt);
  350. }
  351. if (typeinfo is OldClass) {
  352. // old instances are strange - they all share a common type
  353. // of instance but they can "be subclasses" of other
  354. // OldClass's. To check their types we need the actual
  355. // instance.
  356. OldInstance oi = o as OldInstance;
  357. if (oi != null) return oi._class.IsSubclassOf(typeinfo);
  358. }
  359. PythonType odt = DynamicHelpers.GetPythonType(o);
  360. if (IsSubClass(context, odt, typeinfo)) {
  361. return true;
  362. }
  363. return IsInstanceDynamic(o, typeinfo);
  364. }
  365. private static bool IsInstanceDynamic(object o, object typeinfo) {
  366. return IsInstanceDynamic(o, typeinfo, DynamicHelpers.GetPythonType(o));
  367. }
  368. private static bool IsInstanceDynamic(object o, object typeinfo, PythonType odt) {
  369. if (o is IPythonObject || o is OldInstance) {
  370. object cls;
  371. if (PythonOps.TryGetBoundAttr(o, "__class__", out cls) &&
  372. (!object.ReferenceEquals(odt, cls))) {
  373. return IsSubclassSlow(cls, typeinfo);
  374. }
  375. }
  376. return false;
  377. }
  378. private static bool IsSubclassSlow(object cls, object typeinfo) {
  379. Debug.Assert(typeinfo != null);
  380. if (cls == null) return false;
  381. // Same type
  382. if (cls.Equals(typeinfo)) {
  383. return true;
  384. }
  385. // Get bases
  386. object bases;
  387. if (!PythonOps.TryGetBoundAttr(cls, "__bases__", out bases)) {
  388. return false; // no bases, cannot be subclass
  389. }
  390. PythonTuple tbases = bases as PythonTuple;
  391. if (tbases == null) {
  392. return false; // not a tuple, cannot be subclass
  393. }
  394. foreach (object baseclass in tbases) {
  395. if (IsSubclassSlow(baseclass, typeinfo)) return true;
  396. }
  397. return false;
  398. }
  399. public static object OnesComplement(object o) {
  400. if (o is int) return ~(int)o;
  401. if (o is long) return ~(long)o;
  402. if (o is BigInteger) return ~((BigInteger)o);
  403. if (o is bool) return ScriptingRuntimeHelpers.Int32ToObject((bool)o ? -2 : -1);
  404. object ret;
  405. if (PythonTypeOps.TryInvokeUnaryOperator(DefaultContext.Default, o, "__invert__", out ret) &&
  406. ret != NotImplementedType.Value)
  407. return ret;
  408. throw PythonOps.TypeError("bad operand type for unary ~");
  409. }
  410. public static bool Not(object o) {
  411. return !IsTrue(o);
  412. }
  413. public static object Is(object x, object y) {
  414. return x == y ? ScriptingRuntimeHelpers.True : ScriptingRuntimeHelpers.False;
  415. }
  416. public static bool IsRetBool(object x, object y) {
  417. return x == y;
  418. }
  419. public static object IsNot(object x, object y) {
  420. return x != y ? ScriptingRuntimeHelpers.True : ScriptingRuntimeHelpers.False;
  421. }
  422. public static bool IsNotRetBool(object x, object y) {
  423. return x != y;
  424. }
  425. internal delegate T MultiplySequenceWorker<T>(T self, int count);
  426. /// <summary>
  427. /// Wraps up all the semantics of multiplying sequences so that all of our sequences
  428. /// don't duplicate the same logic. When multiplying sequences we need to deal with
  429. /// only multiplying by valid sequence types (ints, not floats), support coercion
  430. /// to integers if the type supports it, not multiplying by None, and getting the
  431. /// right semantics for multiplying by negative numbers and 1 (w/ and w/o subclasses).
  432. ///
  433. /// This function assumes that it is only called for case where count is not implicitly
  434. /// coercible to int so that check is skipped.
  435. /// </summary>
  436. internal static object MultiplySequence<T>(MultiplySequenceWorker<T> multiplier, T sequence, Index count, bool isForward) {
  437. if (isForward && count != null) {
  438. object ret;
  439. if (PythonTypeOps.TryInvokeBinaryOperator(DefaultContext.Default, count.Value, sequence, "__rmul__", out ret)) {
  440. if (ret != NotImplementedType.Value) return ret;
  441. }
  442. }
  443. int icount = GetSequenceMultiplier(sequence, count.Value);
  444. if (icount < 0) icount = 0;
  445. return multiplier(sequence, icount);
  446. }
  447. internal static int GetSequenceMultiplier(object sequence, object count) {
  448. int icount;
  449. if (!Converter.TryConvertToIndex(count, out icount)) {
  450. PythonTuple pt = null;
  451. if (count is OldInstance || !DynamicHelpers.GetPythonType(count).IsSystemType) {
  452. pt = Builtin.TryCoerce(DefaultContext.Default, count, sequence) as PythonTuple;
  453. }
  454. if (pt == null || !Converter.TryConvertToIndex(pt[0], out icount)) {
  455. throw TypeError("can't multiply sequence by non-int of type '{0}'", PythonTypeOps.GetName(count));
  456. }
  457. }
  458. return icount;
  459. }
  460. public static object Equal(CodeContext/*!*/ context, object x, object y) {
  461. PythonContext pc = PythonContext.GetContext(context);
  462. return pc.EqualSite.Target(pc.EqualSite, x, y);
  463. }
  464. public static bool EqualRetBool(object x, object y) {
  465. //TODO just can't seem to shake these fast paths
  466. if (x is int && y is int) { return ((int)x) == ((int)y); }
  467. if (x is string && y is string) { return ((string)x).Equals((string)y); }
  468. return DynamicHelpers.GetPythonType(x).EqualRetBool(x, y);
  469. }
  470. public static bool EqualRetBool(CodeContext/*!*/ context, object x, object y) {
  471. // TODO: use context
  472. //TODO just can't seem to shake these fast paths
  473. if (x is int && y is int) { return ((int)x) == ((int)y); }
  474. if (x is string && y is string) { return ((string)x).Equals((string)y); }
  475. return DynamicHelpers.GetPythonType(x).EqualRetBool(x, y);
  476. }
  477. public static int Compare(object x, object y) {
  478. return Compare(DefaultContext.Default, x, y);
  479. }
  480. public static int Compare(CodeContext/*!*/ context, object x, object y) {
  481. if (x == y) return 0;
  482. return DynamicHelpers.GetPythonType(x).Compare(x, y);
  483. }
  484. public static object CompareEqual(int res) {
  485. return res == 0 ? ScriptingRuntimeHelpers.True : ScriptingRuntimeHelpers.False;
  486. }
  487. public static object CompareNotEqual(int res) {
  488. return res == 0 ? ScriptingRuntimeHelpers.False : ScriptingRuntimeHelpers.True;
  489. }
  490. public static object CompareGreaterThan(int res) {
  491. return res > 0 ? ScriptingRuntimeHelpers.True : ScriptingRuntimeHelpers.False;
  492. }
  493. public static object CompareGreaterThanOrEqual(int res) {
  494. return res >= 0 ? ScriptingRuntimeHelpers.True : ScriptingRuntimeHelpers.False;
  495. }
  496. public static object CompareLessThan(int res) {
  497. return res < 0 ? ScriptingRuntimeHelpers.True : ScriptingRuntimeHelpers.False;
  498. }
  499. public static object CompareLessThanOrEqual(int res) {
  500. return res <= 0 ? ScriptingRuntimeHelpers.True : ScriptingRuntimeHelpers.False;
  501. }
  502. public static bool CompareTypesEqual(CodeContext/*!*/ context, object x, object y) {
  503. if (x == null && y == null) return true;
  504. if (x == null) return false;
  505. if (y == null) return false;
  506. if (DynamicHelpers.GetPythonType(x) == DynamicHelpers.GetPythonType(y)) {
  507. // avoid going to the ID dispenser if we have the same types...
  508. return x == y;
  509. }
  510. return PythonOps.CompareTypesWorker(context, false, x, y) == 0;
  511. }
  512. public static bool CompareTypesNotEqual(CodeContext/*!*/ context, object x, object y) {
  513. return PythonOps.CompareTypesWorker(context, false, x, y) != 0;
  514. }
  515. public static bool CompareTypesGreaterThan(CodeContext/*!*/ context, object x, object y) {
  516. return PythonOps.CompareTypes(context, x, y) > 0;
  517. }
  518. public static bool CompareTypesLessThan(CodeContext/*!*/ context, object x, object y) {
  519. return PythonOps.CompareTypes(context, x, y) < 0;
  520. }
  521. public static bool CompareTypesGreaterThanOrEqual(CodeContext/*!*/ context, object x, object y) {
  522. return PythonOps.CompareTypes(context, x, y) >= 0;
  523. }
  524. public static bool CompareTypesLessThanOrEqual(CodeContext/*!*/ context, object x, object y) {
  525. return PythonOps.CompareTypes(context, x, y) <= 0;
  526. }
  527. public static int CompareTypesWorker(CodeContext/*!*/ context, bool shouldWarn, object x, object y) {
  528. if (x == null && y == null) return 0;
  529. if (x == null) return -1;
  530. if (y == null) return 1;
  531. string name1, name2;
  532. int diff;
  533. if (DynamicHelpers.GetPythonType(x) != DynamicHelpers.GetPythonType(y)) {
  534. if (shouldWarn && PythonContext.GetContext(context).PythonOptions.WarnPython30) {
  535. PythonOps.Warn(context, PythonExceptions.DeprecationWarning, "comparing unequal types not supported in 3.x");
  536. }
  537. if (x.GetType() == typeof(OldInstance)) {
  538. name1 = ((OldInstance)x)._class.Name;
  539. if (y.GetType() == typeof(OldInstance)) {
  540. name2 = ((OldInstance)y)._class.Name;
  541. } else {
  542. // old instances are always less than new-style classes
  543. return -1;
  544. }
  545. } else if (y.GetType() == typeof(OldInstance)) {
  546. // old instances are always less than new-style classes
  547. return 1;
  548. } else {
  549. name1 = PythonTypeOps.GetName(x);
  550. name2 = PythonTypeOps.GetName(y);
  551. }
  552. diff = String.CompareOrdinal(name1, name2);
  553. if (diff == 0) {
  554. // if the types are different but have the same name compare based upon their types.
  555. diff = (int)(IdDispenser.GetId(DynamicHelpers.GetPythonType(x)) - IdDispenser.GetId(DynamicHelpers.GetPythonType(y)));
  556. }
  557. } else {
  558. diff = (int)(IdDispenser.GetId(x) - IdDispenser.GetId(y));
  559. }
  560. if (diff < 0) return -1;
  561. if (diff == 0) return 0;
  562. return 1;
  563. }
  564. public static int CompareTypes(CodeContext/*!*/ context, object x, object y) {
  565. return CompareTypesWorker(context, true, x, y);
  566. }
  567. public static object GreaterThanHelper(CodeContext/*!*/ context, object self, object other) {
  568. return InternalCompare(context, PythonOperationKind.GreaterThan, self, other);
  569. }
  570. public static object LessThanHelper(CodeContext/*!*/ context, object self, object other) {
  571. return InternalCompare(context, PythonOperationKind.LessThan, self, other);
  572. }
  573. public static object GreaterThanOrEqualHelper(CodeContext/*!*/ context, object self, object other) {
  574. return InternalCompare(context, PythonOperationKind.GreaterThanOrEqual, self, other);
  575. }
  576. public static object LessThanOrEqualHelper(CodeContext/*!*/ context, object self, object other) {
  577. return InternalCompare(context, PythonOperationKind.LessThanOrEqual, self, other);
  578. }
  579. internal static object InternalCompare(CodeContext/*!*/ context, PythonOperationKind op, object self, object other) {
  580. object ret;
  581. if (PythonTypeOps.TryInvokeBinaryOperator(context, self, other, Symbols.OperatorToSymbol(op), out ret))
  582. return ret;
  583. return NotImplementedType.Value;
  584. }
  585. public static int CompareToZero(object value) {
  586. double val;
  587. if (Converter.TryConvertToDouble(value, out val)) {
  588. if (val > 0) return 1;
  589. if (val < 0) return -1;
  590. return 0;
  591. }
  592. throw PythonOps.TypeErrorForBadInstance("an integer is required (got {0})", value);
  593. }
  594. public static int CompareArrays(object[] data0, int size0, object[] data1, int size1) {
  595. int size = Math.Min(size0, size1);
  596. for (int i = 0; i < size; i++) {
  597. int c = PythonOps.Compare(data0[i], data1[i]);
  598. if (c != 0) return c;
  599. }
  600. if (size0 == size1) return 0;
  601. return size0 > size1 ? +1 : -1;
  602. }
  603. public static int CompareArrays(object[] data0, int size0, object[] data1, int size1, IComparer comparer) {
  604. int size = Math.Min(size0, size1);
  605. for (int i = 0; i < size; i++) {
  606. int c = comparer.Compare(data0[i], data1[i]);
  607. if (c != 0) return c;
  608. }
  609. if (size0 == size1) return 0;
  610. return size0 > size1 ? +1 : -1;
  611. }
  612. public static bool ArraysEqual(object[] data0, int size0, object[] data1, int size1) {
  613. if (size0 != size1) {
  614. return false;
  615. }
  616. for (int i = 0; i < size0; i++) {
  617. if (data0[i] != null) {
  618. if (!EqualRetBool(data0[i], data1[i])) {
  619. return false;
  620. }
  621. } else if (data1[i] != null) {
  622. return false;
  623. }
  624. }
  625. return true;
  626. }
  627. public static bool ArraysEqual(object[] data0, int size0, object[] data1, int size1, IEqualityComparer comparer) {
  628. if (size0 != size1) {
  629. return false;
  630. }
  631. for (int i = 0; i < size0; i++) {
  632. if (data0[i] != null) {
  633. if (!comparer.Equals(data0[i], data1[i])) {
  634. return false;
  635. }
  636. } else if (data1[i] != null) {
  637. return false;
  638. }
  639. }
  640. return true;
  641. }
  642. public static object PowerMod(CodeContext/*!*/ context, object x, object y, object z) {
  643. object ret;
  644. if (z == null) {
  645. return PythonContext.GetContext(context).Operation(PythonOperationKind.Power, x, y);
  646. }
  647. if (x is int && y is int && z is int) {
  648. ret = Int32Ops.Power((int)x, (int)y, (int)z);
  649. if (ret != NotImplementedType.Value) return ret;
  650. } else if (x is BigInteger) {
  651. ret = BigIntegerOps.Power((BigInteger)x, y, z);
  652. if (ret != NotImplementedType.Value) return ret;
  653. }
  654. if (x is Complex || y is Complex || z is Complex) {
  655. throw PythonOps.ValueError("complex modulo");
  656. }
  657. if (PythonTypeOps.TryInvokeTernaryOperator(context, x, y, z, "__pow__", out ret)) {
  658. if (ret != NotImplementedType.Value) {
  659. return ret;
  660. } else if (!IsNumericObject(y) || !IsNumericObject(z)) {
  661. // special error message in this case...
  662. throw TypeError("pow() 3rd argument not allowed unless all arguments are integers");
  663. }
  664. }
  665. throw PythonOps.TypeErrorForBinaryOp("power with modulus", x, y);
  666. }
  667. public static long Id(object o) {
  668. return IdDispenser.GetId(o);
  669. }
  670. public static string HexId(object o) {
  671. return string.Format("0x{0:X16}", Id(o));
  672. }
  673. // For hash operators, it's essential that:
  674. // Cmp(x,y)==0 implies hash(x) == hash(y)
  675. //
  676. // Equality is a language semantic determined by the Python's numerical Compare() ops
  677. // in IronPython.Runtime.Operations namespaces.
  678. // For example, the CLR compares float(1.0) and int32(1) as different, but Python
  679. // compares them as equal. So Hash(1.0f) and Hash(1) must be equal.
  680. //
  681. // Python allows an equality relationship between int, double, BigInteger, and complex.
  682. // So each of these hash functions must be aware of their possible equality relationships
  683. // and hash appropriately.
  684. //
  685. // Types which differ in hashing from .NET have __hash__ functions defined in their
  686. // ops classes which do the appropriate hashing.
  687. public static int Hash(CodeContext/*!*/ context, object o) {
  688. return PythonContext.Hash(o);
  689. }
  690. public static object Hex(object o) {
  691. if (o is int) return Int32Ops.__hex__((int)o);
  692. else if (o is BigInteger) return BigIntegerOps.__hex__((BigInteger)o);
  693. object hex;
  694. if (PythonTypeOps.TryInvokeUnaryOperator(DefaultContext.Default,
  695. o,
  696. "__hex__",
  697. out hex)) {
  698. if (!(hex is string) && !(hex is ExtensibleString))
  699. throw PythonOps.TypeError("hex expected string type as return, got '{0}'", PythonTypeOps.GetName(hex));
  700. return hex;
  701. }
  702. throw TypeError("hex() argument cannot be converted to hex");
  703. }
  704. public static object Oct(object o) {
  705. if (o is int) {
  706. return Int32Ops.__oct__((int)o);
  707. } else if (o is BigInteger) {
  708. return BigIntegerOps.__oct__((BigInteger)o);
  709. }
  710. object octal;
  711. if (PythonTypeOps.TryInvokeUnaryOperator(DefaultContext.Default,
  712. o,
  713. "__oct__",
  714. out octal)) {
  715. if (!(octal is string) && !(octal is ExtensibleString))
  716. throw PythonOps.TypeError("hex expected string type as return, got '{0}'", PythonTypeOps.GetName(octal));
  717. return octal;
  718. }
  719. throw TypeError("oct() argument cannot be converted to octal");
  720. }
  721. public static int Length(object o) {
  722. string s = o as string;
  723. if (s != null) {
  724. return s.Length;
  725. }
  726. object[] os = o as object[];
  727. if (os != null) {
  728. return os.Length;
  729. }
  730. object len = PythonContext.InvokeUnaryOperator(DefaultContext.Default, UnaryOperators.Length, o, "len() of unsized object");
  731. int res;
  732. if (len is int) {
  733. res = (int)len;
  734. } else {
  735. res = Converter.ConvertToInt32(len);
  736. }
  737. if (res < 0) {
  738. throw PythonOps.ValueError("__len__ should return >= 0, got {0}", res);
  739. }
  740. return res;
  741. }
  742. public static object CallWithContext(CodeContext/*!*/ context, object func, params object[] args) {
  743. return PythonCalls.Call(context, func, args);
  744. }
  745. /// <summary>
  746. /// Supports calling of functions that require an explicit 'this'
  747. /// Currently, we check if the function object implements the interface
  748. /// that supports calling with 'this'. If not, the 'this' object is dropped
  749. /// and a normal call is made.
  750. /// </summary>
  751. public static object CallWithContextAndThis(CodeContext/*!*/ context, object func, object instance, params object[] args) {
  752. // drop the 'this' and make the call
  753. return CallWithContext(context, func, args);
  754. }
  755. public static object ToPythonType(PythonType dt) {
  756. if (dt != null) {
  757. return ((object)dt.OldClass) ?? ((object)dt);
  758. }
  759. return null;
  760. }
  761. public static object CallWithArgsTupleAndContext(CodeContext/*!*/ context, object func, object[] args, object argsTuple) {
  762. PythonTuple tp = argsTuple as PythonTuple;
  763. if (tp != null) {
  764. object[] nargs = new object[args.Length + tp.__len__()];
  765. for (int i = 0; i < args.Length; i++) nargs[i] = args[i];
  766. for (int i = 0; i < tp.__len__(); i++) nargs[i + args.Length] = tp[i];
  767. return CallWithContext(context, func, nargs);
  768. }
  769. List allArgs = PythonOps.MakeEmptyList(args.Length + 10);
  770. allArgs.AddRange(args);
  771. IEnumerator e = PythonOps.GetEnumerator(argsTuple);
  772. while (e.MoveNext()) allArgs.AddNoLock(e.Current);
  773. return CallWithContext(context, func, allArgs.GetObjectArray());
  774. }
  775. [Obsolete("Use ObjectOpertaions instead")]
  776. public static object CallWithArgsTupleAndKeywordDictAndContext(CodeContext/*!*/ context, object func, object[] args, string[] names, object argsTuple, object kwDict) {
  777. IDictionary kws = kwDict as IDictionary;
  778. if (kws == null && kwDict != null) throw PythonOps.TypeError("argument after ** must be a dictionary");
  779. if ((kws == null || kws.Count == 0) && names.Length == 0) {
  780. List<object> largs = new List<object>(args);
  781. if (argsTuple != null) {
  782. foreach (object arg in PythonOps.GetCollection(argsTuple))
  783. largs.Add(arg);
  784. }
  785. return CallWithContext(context, func, largs.ToArray());
  786. } else {
  787. List<object> largs;
  788. if (argsTuple != null && args.Length == names.Length) {
  789. PythonTuple tuple = argsTuple as PythonTuple;
  790. if (tuple == null) tuple = new PythonTuple(argsTuple);
  791. largs = new List<object>(tuple);
  792. largs.AddRange(args);
  793. } else {
  794. largs = new List<object>(args);
  795. if (argsTuple != null) {
  796. largs.InsertRange(args.Length - names.Length, PythonTuple.Make(argsTuple));
  797. }
  798. }
  799. List<string> lnames = new List<string>(names);
  800. if (kws != null) {
  801. IDictionaryEnumerator ide = kws.GetEnumerator();
  802. while (ide.MoveNext()) {
  803. lnames.Add((string)ide.Key);
  804. largs.Add(ide.Value);
  805. }
  806. }
  807. return PythonCalls.CallWithKeywordArgs(context, func, largs.ToArray(), lnames.ToArray());
  808. }
  809. }
  810. public static object CallWithKeywordArgs(CodeContext/*!*/ context, object func, object[] args, string[] names) {
  811. return PythonCalls.CallWithKeywordArgs(context, func, args, names);
  812. }
  813. public static object CallWithArgsTuple(object func, object[] args, object argsTuple) {
  814. PythonTuple tp = argsTuple as PythonTuple;
  815. if (tp != null) {
  816. object[] nargs = new object[args.Length + tp.__len__()];
  817. for (int i = 0; i < args.Length; i++) nargs[i] = args[i];
  818. for (int i = 0; i < tp.__len__(); i++) nargs[i + args.Length] = tp[i];
  819. return PythonCalls.Call(func, nargs);
  820. }
  821. List allArgs = PythonOps.MakeEmptyList(args.Length + 10);
  822. allArgs.AddRange(args);
  823. IEnumerator e = PythonOps.GetEnumerator(argsTuple);
  824. while (e.MoveNext()) allArgs.AddNoLock(e.Current);
  825. return PythonCalls.Call(func, allArgs.GetObjectArray());
  826. }
  827. public static object GetIndex(CodeContext/*!*/ context, object o, object index) {
  828. PythonContext pc = PythonContext.GetContext(context);
  829. return pc.GetIndexSite.Target(pc.GetIndexSite, o, index);
  830. }
  831. public static bool TryGetBoundAttr(object o, string name, out object ret) {
  832. return TryGetBoundAttr(DefaultContext.Default, o, name, out ret);
  833. }
  834. public static void SetAttr(CodeContext/*!*/ context, object o, string name, object value) {
  835. PythonContext.GetContext(context).SetAttr(context, o, name, value);
  836. }
  837. public static bool TryGetBoundAttr(CodeContext/*!*/ context, object o, string name, out object ret) {
  838. return DynamicHelpers.GetPythonType(o).TryGetBoundAttr(context, o, name, out ret);
  839. }
  840. public static void DeleteAttr(CodeContext/*!*/ context, object o, string name) {
  841. PythonContext.GetContext(context).DeleteAttr(context, o, name);
  842. }
  843. public static bool HasAttr(CodeContext/*!*/ context, object o, string name) {
  844. object dummy;
  845. try {
  846. return TryGetBoundAttr(context, o, name, out dummy);
  847. } catch (SystemExitException) {
  848. throw;
  849. } catch (KeyboardInterruptException) {
  850. // we don't catch ThreadAbortException because it will
  851. // automatically re-throw on it's own.
  852. throw;
  853. } catch {
  854. return false;
  855. }
  856. }
  857. public static object GetBoundAttr(CodeContext/*!*/ context, object o, string name) {
  858. object ret;
  859. if (!DynamicHelpers.GetPythonType(o).TryGetBoundAttr(context, o, name, out ret)) {
  860. if (o is OldClass) {
  861. throw PythonOps.AttributeError("type object '{0}' has no attribute '{1}'",
  862. ((OldClass)o).Name, name);
  863. } else {
  864. throw PythonOps.AttributeError("'{0}' object has no attribute '{1}'", PythonTypeOps.GetName(o), name);
  865. }
  866. }
  867. return ret;
  868. }
  869. public static void ObjectSetAttribute(CodeContext/*!*/ context, object o, string name, object value) {
  870. if (!DynamicHelpers.GetPythonType(o).TrySetNonCustomMember(context, o, name, value))
  871. throw AttributeErrorForMissingOrReadonly(context, DynamicHelpers.GetPythonType(o), name);
  872. }
  873. public static void ObjectDeleteAttribute(CodeContext/*!*/ context, object o, string name) {
  874. if (!DynamicHelpers.GetPythonType(o).TryDeleteNonCustomMember(context, o, name)) {
  875. throw AttributeErrorForMissingOrReadonly(context, DynamicHelpers.GetPythonType(o), name);
  876. }
  877. }
  878. public static object ObjectGetAttribute(CodeContext/*!*/ context, object o, string name) {
  879. OldClass oc = o as OldClass;
  880. if (oc != null) {
  881. return oc.GetMember(context, name);
  882. }
  883. object value;
  884. if (DynamicHelpers.GetPythonType(o).TryGetNonCustomMember(context, o, name, out value)) {
  885. return value;
  886. }
  887. throw PythonOps.AttributeErrorForObjectMissingAttribute(o, name);
  888. }
  889. internal static IList<string> GetStringMemberList(IPythonMembersList pyMemList) {
  890. List<string> res = new List<string>();
  891. foreach (object o in pyMemList.GetMemberNames(DefaultContext.Default)) {
  892. if (o is string) {
  893. res.Add((string)o);
  894. }
  895. }
  896. return res;
  897. }
  898. public static IList<object> GetAttrNames(CodeContext/*!*/ context, object o) {
  899. IPythonMembersList pyMemList = o as IPythonMembersList;
  900. if (pyMemList != null) {
  901. return pyMemList.GetMemberNames(context);
  902. }
  903. IMembersList memList = o as IMembersList;
  904. if (memList != null) {
  905. return new List(memList.GetMemberNames());
  906. }
  907. IPythonObject po = o as IPythonObject;
  908. if (po != null) {
  909. return po.PythonType.GetMemberNames(context, o);
  910. }
  911. List res = DynamicHelpers.GetPythonType(o).GetMemberNames(context, o);
  912. #if !SILVERLIGHT
  913. if (o != null && Microsoft.Scripting.ComInterop.ComBinder.IsComObject(o)) {
  914. foreach (string name in Microsoft.Scripting.ComInterop.ComBinder.GetDynamicMemberNames(o)) {
  915. if (!res.Contains(name)) {
  916. res.AddNoLock(name);
  917. }
  918. }
  919. }
  920. #endif
  921. return res;
  922. }
  923. /// <summary>
  924. /// Called from generated code emitted by NewTypeMaker.
  925. /// </summary>
  926. public static void CheckInitializedAttribute(object o, object self, string name) {
  927. if (o == Uninitialized.Instance) {
  928. throw PythonOps.AttributeError("'{0}' object has no attribute '{1}'",
  929. PythonTypeOps.GetName(self),
  930. name);
  931. }
  932. }
  933. public static object GetUserSlotValue(CodeContext/*!*/ context, PythonTypeUserDescriptorSlot slot, object instance, PythonType type) {
  934. return slot.GetValue(context, instance, type);
  935. }
  936. /// <summary>
  937. /// Handles the descriptor protocol for user-defined objects that may implement __get__
  938. /// </summary>
  939. public static object GetUserDescriptor(object o, object instance, object context) {
  940. if (o is IPythonObject) {
  941. // slow, but only encountred for user defined descriptors.
  942. PerfTrack.NoteEvent(PerfTrack.Categories.DictInvoke, "__get__");
  943. object ret;
  944. if (PythonContext.TryInvokeTernaryOperator(DefaultContext.Default,
  945. TernaryOperators.GetDescriptor,
  946. o,
  947. instance,
  948. context,
  949. out ret)) {
  950. return ret;
  951. }
  952. }
  953. return o;
  954. }
  955. /// <summary>
  956. /// Handles the descriptor protocol for user-defined objects that may implement __set__
  957. /// </summary>
  958. public static bool TrySetUserDescriptor(object o, object instance, object value) {
  959. if (o != null && o.GetType() == typeof(OldInstance)) return false; // only new-style classes have descriptors
  960. // slow, but only encountred for user defined descriptors.
  961. PerfTrack.NoteEvent(PerfTrack.Categories.DictInvoke, "__set__");
  962. object dummy;
  963. return PythonContext.TryInvokeTernaryOperator(DefaultContext.Default,
  964. TernaryOperators.SetDescriptor,
  965. o,
  966. instance,
  967. value,
  968. out dummy);
  969. }
  970. /// <summary>
  971. /// Handles the descriptor protocol for user-defined objects that may implement __delete__
  972. /// </summary>
  973. public static bool TryDeleteUserDescriptor(object o, object instance) {
  974. if (o != null && o.GetType() == typeof(OldInstance)) return false; // only new-style classes can have descriptors
  975. // slow, but only encountred for user defined descriptors.
  976. PerfTrack.NoteEvent(PerfTrack.Categories.DictInvoke, "__delete__");
  977. object dummy;
  978. return PythonTypeOps.TryInvokeBinaryOperator(DefaultContext.Default,
  979. o,
  980. instance,
  981. "__delete__",
  982. out dummy);
  983. }
  984. public static object Invoke(CodeContext/*!*/ context, object target, string name, params object[] args) {
  985. return PythonCalls.Call(context, PythonOps.GetBoundAttr(context, target, name), args);
  986. }
  987. public static Delegate CreateDynamicDelegate(DynamicMethod meth, Type delegateType, object target) {
  988. // Always close delegate around its own instance of the frame
  989. return meth.CreateDelegate(delegateType, target);
  990. }
  991. public static double CheckMath(double v) {
  992. if (double.IsInfinity(v)) {
  993. throw PythonOps.OverflowError("math range error");
  994. } else if (double.IsNaN(v)) {
  995. throw PythonOps.ValueError("math domain error");
  996. } else {
  997. return v;
  998. }
  999. }
  1000. public static double CheckMath(double input, double output) {
  1001. if (double.IsInfinity(input) && double.IsInfinity(output) ||
  1002. double.IsNaN(input) && double.IsNaN(output)) {
  1003. return output;
  1004. } else {
  1005. return CheckMath(output);
  1006. }
  1007. }
  1008. public static double CheckMath(double in0, double in1, double output) {
  1009. if ((double.IsInfinity(in0) || double.IsInfinity(in1)) && double.IsInfinity(output) ||
  1010. (double.IsNaN(in0) || double.IsNaN(in1)) && double.IsNaN(output)) {
  1011. return output;
  1012. } else {
  1013. return CheckMath(output);
  1014. }
  1015. }
  1016. public static object IsMappingType(CodeContext/*!*/ context, object o) {
  1017. if (o is IDictionary || o is PythonDictionary || o is IDictionary<object, object> || o is PythonDictionary) {
  1018. return ScriptingRuntimeHelpers.True;
  1019. }
  1020. object getitem;
  1021. if ((o is IPythonObject || o is OldInstance) && PythonOps.TryGetBoundAttr(context, o, "__getitem__", out getitem)) {
  1022. if (!PythonOps.IsClsVisible(context)) {
  1023. // in standard Python methods aren't mapping types, therefore
  1024. // if the user hasn't broken out of that box yet don't treat
  1025. // them as mapping types.
  1026. if (o is BuiltinFunction) return ScriptingRuntimeHelpers.False;
  1027. }
  1028. return ScriptingRuntimeHelpers.True;
  1029. }
  1030. return ScriptingRuntimeHelpers.False;
  1031. }
  1032. public static int FixSliceIndex(int v, int len) {
  1033. if (v < 0) v = len + v;
  1034. if (v < 0) return 0;
  1035. if (v > len) return len;
  1036. return v;
  1037. }
  1038. public static long FixSliceIndex(long v, long len) {
  1039. if (v < 0) v = len + v;
  1040. if (v < 0) return 0;
  1041. if (v > len) return len;
  1042. return v;
  1043. }
  1044. public static void FixSlice(
  1045. int length, object start, object stop, object step,
  1046. out int ostart, out int ostop, out int ostep
  1047. ) {
  1048. if (step == null) {
  1049. ostep = 1;
  1050. } else {
  1051. ostep = Converter.ConvertToIndex(step);
  1052. if (ostep == 0) {
  1053. throw PythonOps.ValueError("step cannot be zero");
  1054. }
  1055. }
  1056. if (start == null) {
  1057. ostart = ostep > 0 ? 0 : length - 1;
  1058. } else {
  1059. ostart = Converter.ConvertToIndex(start);
  1060. if (ostart < 0) {
  1061. ostart += length;
  1062. if (ostart < 0) {
  1063. ostart = ostep > 0 ? Math.Min(length, 0) : Math.Min(length - 1, -1);
  1064. }
  1065. } else if (ostart >= length) {
  1066. ostart = ostep > 0 ? length : length - 1;
  1067. }
  1068. }
  1069. if (stop == null) {
  1070. ostop = ostep > 0 ? length : -1;
  1071. } else {
  1072. ostop = Converter.ConvertToIndex(stop);
  1073. if (ostop < 0) {
  1074. ostop += length;
  1075. if (ostop < 0) {
  1076. ostop = ostep > 0 ? Math.Min(length, 0) : Math.Min(length - 1, -1);
  1077. }
  1078. } else if (ostop >= length) {
  1079. ostop = ostep > 0 ? length : length - 1;
  1080. }
  1081. }
  1082. }
  1083. public static void FixSlice(
  1084. long length, long? start, long? stop, long? step,
  1085. out long ostart, out long ostop, out long ostep, out long ocount
  1086. ) {
  1087. if (step == null) {
  1088. ostep = 1;
  1089. } else if (step == 0) {
  1090. throw PythonOps.ValueError("step cannot be zero");
  1091. } else {
  1092. ostep = step.Value;
  1093. }
  1094. if (start == null) {
  1095. ostart = ostep > 0 ? 0 : length - 1;
  1096. } else {
  1097. ostart = start.Value;
  1098. if (ostart < 0) {
  1099. ostart += length;
  1100. if (ostart < 0) {
  1101. ostart = ostep > 0 ? Math.Min(length, 0) : Math.Min(length - 1, -1);
  1102. }
  1103. } else if (ostart >= length) {
  1104. ostart = ostep > 0 ? length : length - 1;
  1105. }
  1106. }
  1107. if (stop == null) {
  1108. ostop = ostep > 0 ? length : -1;
  1109. } else {
  1110. ostop = stop.Value;
  1111. if (ostop < 0) {
  1112. ostop += length;
  1113. if (ostop < 0) {
  1114. ostop = ostep > 0 ? Math.Min(length, 0) : Math.Min(length - 1, -1);
  1115. }
  1116. } else if (ostop >= length) {
  1117. ostop = ostep > 0 ? length : length - 1;
  1118. }
  1119. }
  1120. ocount = Math.Max(0, ostep > 0 ? (ostop - ostart + ostep - 1) / ostep
  1121. : (ostop - ostart + ostep + 1) / ostep);
  1122. }
  1123. public static int FixIndex(int v, int len) {
  1124. if (v < 0) {
  1125. v += len;
  1126. if (v < 0) {
  1127. throw PythonOps.IndexError("index out of range: {0}", v - len);
  1128. }
  1129. } else if (v >= len) {
  1130. throw PythonOps.IndexError("index out of range: {0}", v);
  1131. }
  1132. return v;
  1133. }
  1134. public static void InitializeForFinalization(CodeContext/*!*/ context, object newObject) {
  1135. IWeakReferenceable iwr = newObject as IWeakReferenceable;
  1136. Debug.Assert(iwr != null);
  1137. InstanceFinalizer nif = new InstanceFinalizer(context, newObject);
  1138. iwr.SetFinalizer(new WeakRefTracker(nif, nif));
  1139. }
  1140. private static object FindMetaclass(CodeContext/*!*/ context, PythonTuple bases, PythonDictionary dict) {
  1141. // If dict['__metaclass__'] exists, it is used.
  1142. object ret;
  1143. if (dict.TryGetValue("__metaclass__", out ret) && ret != null) return ret;
  1144. // Otherwise, if there is at least one base class, its metaclass is used
  1145. for (int i = 0; i < bases.__len__(); i++) {
  1146. if (!(bases[i] is OldClass)) return DynamicHelpers.GetPythonType(bases[i]);
  1147. }
  1148. // Otherwise, if there's a global variable named __metaclass__, it is used.
  1149. if (context.TryGetGlobalVariable("__metaclass__", out ret) && ret != null) {
  1150. return ret;
  1151. }
  1152. //Otherwise, the classic metaclass (types.ClassType) is used.
  1153. return TypeCache.OldInstance;
  1154. }
  1155. public static object MakeClass(FunctionCode funcCode, Func<CodeContext, CodeContext> body, CodeContext/*!*/ parentContext, string name, object[] bases, string selfNames) {
  1156. Func<CodeContext, CodeContext> func = GetClassCode(parentContext, funcCode, body);
  1157. return MakeClass(parentContext, name, bases, selfNames, func(parentContext).Dict);
  1158. }
  1159. private static Func<CodeContext, CodeContext> GetClassCode(CodeContext/*!*/ context, FunctionCode funcCode, Func<CodeContext, CodeContext> body) {
  1160. if (body == null) {
  1161. if (funcCode.Target == null) {
  1162. funcCode.UpdateDelegate(context.LanguageContext, true);
  1163. }
  1164. return (Func<CodeContext, CodeContext>)funcCode.Target;
  1165. } else {
  1166. if (funcCode.Target == null) {
  1167. funcCode.SetTarget(body);
  1168. funcCode._normalDelegate = body;
  1169. }
  1170. return body;
  1171. }
  1172. }
  1173. internal static object MakeClass(CodeContext/*!*/ context, string name, object[] bases, string selfNames, PythonDictionary vars) {
  1174. foreach (object dt in bases) {
  1175. if (dt is TypeGroup) {
  1176. object[] newBases = new object[bases.Length];
  1177. for (int i = 0; i < bases.Length; i++) {
  1178. TypeGroup tc = bases[i] as TypeGroup;
  1179. if (tc != null) {
  1180. Type nonGenericType;
  1181. if (!tc.TryGetNonGenericType(out nonGenericType)) {
  1182. throw PythonOps.TypeError("cannot derive from open generic types " + Builtin.repr(context, tc).ToString());
  1183. }
  1184. newBases[i] = DynamicHelpers.GetPythonTypeFromType(nonGenericType);
  1185. } else {
  1186. newBases[i] = bases[i];
  1187. }
  1188. }
  1189. bases = newBases;
  1190. break;
  1191. }
  1192. }
  1193. PythonTuple tupleBases = PythonTuple.MakeTuple(bases);
  1194. object metaclass = FindMetaclass(context, tupleBases, vars);
  1195. if (metaclass == TypeCache.OldInstance) {
  1196. return new OldClass(name, tupleBases, vars, selfNames);
  1197. } else if (metaclass == TypeCache.PythonType) {
  1198. return PythonType.__new__(context, TypeCache.PythonType, name, tupleBases, vars, selfNames);
  1199. }
  1200. // eg:
  1201. // def foo(*args): print args
  1202. // __metaclass__ = foo
  1203. // class bar: pass
  1204. // calls our function...
  1205. PythonContext pc = PythonContext.GetContext(context);
  1206. return pc.MetaClassCallSite.Target(
  1207. pc.MetaClassCallSite,
  1208. context,
  1209. metaclass,
  1210. name,
  1211. tupleBases,
  1212. vars
  1213. );
  1214. }
  1215. /// <summary>
  1216. /// Python runtime helper for raising assertions. Used by AssertStatement.
  1217. /// </summary>
  1218. /// <param name="msg">Object representing the assertion message</param>
  1219. public static void RaiseAssertionError(object msg) {
  1220. if (msg == null) {
  1221. throw PythonOps.AssertionError(String.Empty, ArrayUtils.EmptyObjects);
  1222. } else {
  1223. string message = PythonOps.ToString(msg);
  1224. throw PythonOps.AssertionError("{0}", new object[] { message });
  1225. }
  1226. }
  1227. /// <summary>
  1228. /// Python runtime helper to create instance of Python List object.
  1229. /// </summary>
  1230. /// <returns>New instance of List</returns>
  1231. public static List MakeList() {
  1232. return new List();
  1233. }
  1234. /// <summary>
  1235. /// Python runtime helper to create a populated instance of Python List object.
  1236. /// </summary>
  1237. public static List MakeList(params object[] items) {
  1238. return new List(items);
  1239. }
  1240. /// <summary>
  1241. /// Python runtime helper to create a populated instance of Python List object w/o
  1242. /// copying the array contents.
  1243. /// </summary>
  1244. [NoSideEffects]
  1245. public static List MakeListNoCopy(params object[] items) {
  1246. return List.FromArrayNoCopy(items);
  1247. }
  1248. /// <summary>
  1249. /// Python runtime helper to create a populated instance of Python List object.
  1250. ///
  1251. /// List is populated by arbitrary user defined object.
  1252. /// </summary>
  1253. public static List MakeListFromSequence(object sequence) {
  1254. return new List(sequence);
  1255. }
  1256. /// <summary>
  1257. /// Python runtime helper to create an instance of Python List object.
  1258. ///
  1259. /// List has the initial provided capacity.
  1260. /// </summary>
  1261. [NoSideEffects]
  1262. public static List MakeEmptyList(int capacity) {
  1263. return new List(capacity);
  1264. }
  1265. [NoSideEffects]
  1266. public static List MakeEmptyListFromCode() {
  1267. return List.FromArrayNoCopy(ArrayUtils.EmptyObjects);
  1268. }
  1269. /// <summary>
  1270. /// Python runtime helper to create an instance of Tuple
  1271. /// </summary>
  1272. /// <param name="items"></param>
  1273. /// <returns></returns>
  1274. [NoSideEffects]
  1275. public static PythonTuple MakeTuple(params object[] items) {
  1276. return PythonTuple.MakeTuple(items);
  1277. }
  1278. /// <summary>
  1279. /// Python runtime helper to create an instance of Tuple
  1280. /// </summary>
  1281. /// <param name="items"></param>
  1282. [NoSideEffects]
  1283. public static PythonTuple MakeTupleFromSequence(object items) {
  1284. return PythonTuple.Make(items);
  1285. }
  1286. /// <summary>
  1287. /// Python Runtime Helper for enumerator unpacking (tuple assignments, ...)
  1288. /// Creates enumerator from the input parameter e, and then extracts
  1289. /// expected number of values, returning them as array
  1290. ///
  1291. /// If the input is a Python tuple returns the tuples underlying data array. Callers
  1292. /// should not mutate the resulting tuple.
  1293. /// </summary>
  1294. /// <param name="context">The code context of the AST getting enumerator values.</param>
  1295. /// <param name="e">object to enumerate</param>
  1296. /// <param name="expected">expected number of objects to extract from the enumerator</param>
  1297. /// <returns>
  1298. /// array of objects (.Lengh == expected) if exactly expected objects are in the enumerator.
  1299. /// Otherwise throws exception
  1300. /// </returns>
  1301. [LightThrowing]
  1302. public static object GetEnumeratorValues(CodeContext/*!*/ context, object e, int expected) {
  1303. if (e != null && e.GetType() == typeof(PythonTuple)) {
  1304. // fast path for tuples, avoid enumerating & copying the tuple.
  1305. return GetEnumeratorValuesFromTuple((PythonTuple)e, expected);
  1306. }
  1307. IEnumerator ie = PythonOps.GetEnumeratorForUnpack(context, e);
  1308. int count = 0;
  1309. object[] values = new object[expected];
  1310. while (count < expected) {
  1311. if (!ie.MoveNext()) {
  1312. return LightExceptions.Throw(PythonOps.ValueErrorForUnpackMismatch(expected, count));
  1313. }
  1314. values[count] = ie.Current;
  1315. count++;
  1316. }
  1317. if (ie.MoveNext()) {
  1318. return LightExceptions.Throw(PythonOps.ValueErrorForUnpackMismatch(expected, count + 1));
  1319. }
  1320. return values;
  1321. }
  1322. [LightThrowing]
  1323. public static object GetEnumeratorValuesNoComplexSets(CodeContext/*!*/ context, object e, int expected) {
  1324. if (e != null && e.GetType() == typeof(List)) {
  1325. // fast path for lists, avoid enumerating & copying the list.
  1326. return GetEnumeratorValuesFromList((List)e, expected);
  1327. }
  1328. return GetEnumeratorValues(context, e, expected);
  1329. }
  1330. [LightThrowing]
  1331. private static object GetEnumeratorValuesFromTuple(PythonTuple pythonTuple, int expected) {
  1332. if (pythonTuple.Count == expected) {
  1333. return pythonTuple._data;
  1334. }
  1335. return LightExceptions.Throw(PythonOps.ValueErrorForUnpackMismatch(expected, pythonTuple.Count));
  1336. }
  1337. private static object[] GetEnumeratorValuesFromList(List list, int expected) {
  1338. if (list._size == expected) {
  1339. return list._data;
  1340. }
  1341. throw PythonOps.ValueErrorForUnpackMismatch(expected, list._size);
  1342. }
  1343. /// <summary>
  1344. /// Python runtime helper to create instance of Slice object
  1345. /// </summary>
  1346. /// <param name="start">Start of the slice.</param>
  1347. /// <param name="stop">End of the slice.</param>
  1348. /// <param name="step">Step of the slice.</param>
  1349. /// <returns>Slice</returns>
  1350. public static Slice MakeSlice(object start, object stop, object step) {
  1351. return new Slice(start, stop, step);
  1352. }
  1353. #region Standard I/O support
  1354. public static void Write(CodeContext/*!*/ context, object f, string text) {
  1355. PythonContext pc = PythonContext.GetContext(context);
  1356. if (f == null) {
  1357. f = pc.SystemStandardOut;
  1358. }
  1359. if (f == null || f == Uninitialized.Instance) {
  1360. throw PythonOps.RuntimeError("lost sys.stdout");
  1361. }
  1362. PythonFile pf = f as PythonFile;
  1363. if (pf != null) {
  1364. // avoid spinning up a site in the normal case
  1365. pf.write(text);
  1366. return;
  1367. }
  1368. pc.WriteCallSite.Target(
  1369. pc.WriteCallSite,
  1370. context,
  1371. GetBoundAttr(context, f, "write"),
  1372. text
  1373. );
  1374. }
  1375. private static object ReadLine(CodeContext/*!*/ context, object f) {
  1376. if (f == null || f == Uninitialized.Instance) throw PythonOps.RuntimeError("lost sys.std_in");
  1377. return PythonOps.Invoke(context, f, "readline");
  1378. }
  1379. public static void WriteSoftspace(CodeContext/*!*/ context, object f) {
  1380. if (CheckSoftspace(f)) {
  1381. SetSoftspace(f, ScriptingRuntimeHelpers.False);
  1382. Write(context, f, " ");
  1383. }
  1384. }
  1385. public static void SetSoftspace(object f, object value) {
  1386. PythonOps.SetAttr(DefaultContext.Default, f, "softspace", value);
  1387. }
  1388. public static bool CheckSoftspace(object f) {
  1389. PythonFile pf = f as PythonFile;
  1390. if (pf != null) {
  1391. // avoid spinning up a site in the common case
  1392. return pf.softspace;
  1393. }
  1394. object result;
  1395. if (PythonOps.TryGetBoundAttr(f, "softspace", out result)) {
  1396. return PythonOps.IsTrue(result);
  1397. }
  1398. return false;
  1399. }
  1400. // Must stay here for now because libs depend on it.
  1401. public static void Print(CodeContext/*!*/ context, object o) {
  1402. PrintWithDest(context, PythonContext.GetContext(context).SystemStandardOut, o);
  1403. }
  1404. public static void PrintNoNewline(CodeContext/*!*/ context, object o) {
  1405. PrintWithDestNoNewline(context, PythonContext.GetContext(context).SystemStandardOut, o);
  1406. }
  1407. public static void PrintWithDest(CodeContext/*!*/ context, object dest, object o) {
  1408. PrintWithDestNoNewline(context, dest, o);
  1409. Write(context, dest, "\n");
  1410. }
  1411. public static void PrintWithDestNoNewline(CodeContext/*!*/ context, object dest, object o) {
  1412. WriteSoftspace(context, dest);
  1413. Write(context, dest, o == null ? "None" : ToString(o));
  1414. }
  1415. public static object ReadLineFromSrc(CodeContext/*!*/ context, object src) {
  1416. return ReadLine(context, src);
  1417. }
  1418. /// <summary>
  1419. /// Prints newline into default standard output
  1420. /// </summary>
  1421. public static void PrintNewline(CodeContext/*!*/ context) {
  1422. PrintNewlineWithDest(context, PythonContext.GetContext(context).SystemStandardOut);
  1423. }
  1424. /// <summary>
  1425. /// Prints newline into specified destination. Sets softspace property to false.
  1426. /// </summary>
  1427. public static void PrintNewlineWithDest(CodeContext/*!*/ context, object dest) {
  1428. PythonOps.Write(context, dest, "\n");
  1429. PythonOps.SetSoftspace(dest, ScriptingRuntimeHelpers.False);
  1430. }
  1431. /// <summary>
  1432. /// Prints value into default standard output with Python comma semantics.
  1433. /// </summary>
  1434. public static void PrintComma(CodeContext/*!*/ context, object o) {
  1435. PrintCommaWithDest(context, PythonContext.GetContext(context).SystemStandardOut, o);
  1436. }
  1437. /// <summary>
  1438. /// Prints value into specified destination with Python comma semantics.
  1439. /// </summary>
  1440. public static void PrintCommaWithDest(CodeContext/*!*/ context, object dest, object o) {
  1441. PythonOps.WriteSoftspace(context, dest);
  1442. string s = o == null ? "None" : PythonOps.ToString(o);
  1443. PythonOps.Write(context, dest, s);
  1444. PythonOps.SetSoftspace(dest, !s.EndsWith("\n"));
  1445. }
  1446. /// <summary>
  1447. /// Called from generated code when we are supposed to print an expression value
  1448. /// </summary>
  1449. public static void PrintExpressionValue(CodeContext/*!*/ context, object value) {
  1450. PythonContext pc = PythonContext.GetContext(context);
  1451. object dispHook = pc.GetSystemStateValue("displayhook");
  1452. pc.CallWithContext(context, dispHook, value);
  1453. }
  1454. #if !SILVERLIGHT
  1455. public static void PrintException(CodeContext/*!*/ context, Exception/*!*/ exception, IConsole console) {
  1456. PythonContext pc = PythonContext.GetContext(context);
  1457. PythonTuple exInfo = GetExceptionInfoLocal(context, exception);
  1458. pc.SetSystemStateValue("last_type", exInfo[0]);
  1459. pc.SetSystemStateValue("last_value", exInfo[1]);
  1460. pc.SetSystemStateValue("last_traceback", exInfo[2]);
  1461. object exceptHook = pc.GetSystemStateValue("excepthook");
  1462. BuiltinFunction bf = exceptHook as BuiltinFunction;
  1463. if (console != null && bf != null && bf.DeclaringType == typeof(SysModule) && bf.Name == "excepthook") {
  1464. // builtin except hook, display it to the console which may do nice coloring
  1465. console.WriteLine(pc.FormatException(exception), Style.Error);
  1466. } else {
  1467. // user defined except hook or no console
  1468. try {
  1469. PythonCalls.Call(context, exceptHook, exInfo[0], exInfo[1], exInfo[2]);
  1470. } catch (Exception e) {
  1471. PrintWithDest(context, pc.SystemStandardError, "Error in sys.excepthook:");
  1472. PrintWithDest(context, pc.SystemStandardError, pc.FormatException(e));
  1473. PrintNewlineWithDest(context, pc.SystemStandardError);
  1474. PrintWithDest(context, pc.SystemStandardError, "Original exception was:");
  1475. PrintWithDest(context, pc.SystemStandardError, pc.FormatException(exception));
  1476. }
  1477. }
  1478. }
  1479. #endif
  1480. #endregion
  1481. #region Import support
  1482. /// <summary>
  1483. /// Called from generated code for:
  1484. ///
  1485. /// import spam.eggs
  1486. /// </summary>
  1487. [ProfilerTreatsAsExternal, LightThrowing]
  1488. public static object ImportTop(CodeContext/*!*/ context, string fullName, int level) {
  1489. return Importer.ImportLightThrow(context, fullName, null, level);
  1490. }
  1491. /// <summary>
  1492. /// Python helper method called from generated code for:
  1493. ///
  1494. /// import spam.eggs as ham
  1495. /// </summary>
  1496. [ProfilerTreatsAsExternal, LightThrowing]
  1497. public static object ImportBottom(CodeContext/*!*/ context, string fullName, int level) {
  1498. object module = Importer.ImportLightThrow(context, fullName, null, level);
  1499. if (fullName.IndexOf('.') >= 0) {
  1500. // Extract bottom from the imported module chain
  1501. string[] parts = fullName.Split('.');
  1502. for (int i = 1; i < parts.Length; i++) {
  1503. module = PythonOps.GetBoundAttr(context, module, parts[i]);
  1504. }
  1505. }
  1506. return module;
  1507. }
  1508. /// <summary>
  1509. /// Called from generated code for:
  1510. ///
  1511. /// from spam import eggs1, eggs2
  1512. /// </summary>
  1513. [ProfilerTreatsAsExternal, LightThrowing]
  1514. public static object ImportWithNames(CodeContext/*!*/ context, string fullName, string[] names, int level) {
  1515. return Importer.ImportLightThrow(context, fullName, PythonTuple.MakeTuple(names), level);
  1516. }
  1517. /// <summary>
  1518. /// Imports one element from the module in the context of:
  1519. ///
  1520. /// from module import a, b, c, d
  1521. ///
  1522. /// Called repeatedly for all elements being imported (a, b, c, d above)
  1523. /// </summary>
  1524. public static object ImportFrom(CodeContext/*!*/ context, object module, string name) {
  1525. return Importer.ImportFrom(context, module, name);
  1526. }
  1527. /// <summary>
  1528. /// Called from generated code for:
  1529. ///
  1530. /// from spam import *
  1531. /// </summary>
  1532. [ProfilerTreatsAsExternal]
  1533. public static void ImportStar(CodeContext/*!*/ context, string fullName, int level) {
  1534. object newmod = Importer.Import(context, fullName, PythonTuple.MakeTuple("*"), level);
  1535. PythonModule scope = newmod as PythonModule;
  1536. NamespaceTracker nt = newmod as NamespaceTracker;
  1537. PythonType pt = newmod as PythonType;
  1538. if (pt != null &&
  1539. !pt.UnderlyingSystemType.IsEnum &&
  1540. (!pt.UnderlyingSystemType.IsAbstract || !pt.UnderlyingSystemType.IsSealed)) {
  1541. // from type import * only allowed on static classes (and enums)
  1542. throw PythonOps.ImportError("no module named {0}", pt.Name);
  1543. }
  1544. IEnumerator exports;
  1545. object all;
  1546. bool filterPrivates = false;
  1547. // look for __all__, if it's defined then use that to get the attribute names,
  1548. // otherwise get all the names and filter out members starting w/ _'s.
  1549. if (PythonOps.TryGetBoundAttr(context, newmod, "__all__", out all)) {
  1550. exports = PythonOps.GetEnumerator(all);
  1551. } else {
  1552. exports = PythonOps.GetAttrNames(context, newmod).GetEnumerator();
  1553. filterPrivates = true;
  1554. }
  1555. // iterate through the names and populate the scope with the values.
  1556. while (exports.MoveNext()) {
  1557. string name = exports.Current as string;
  1558. if (name == null) {
  1559. throw PythonOps.TypeErrorForNonStringAttribute();
  1560. } else if (filterPrivates && name.Length > 0 && name[0] == '_') {
  1561. continue;
  1562. }
  1563. // we special case several types to avoid one-off code gen of dynamic sites
  1564. if (scope != null) {
  1565. context.SetVariable(name, scope.__dict__[name]);
  1566. } else if (nt != null) {
  1567. object value = NamespaceTrackerOps.GetCustomMember(context, nt, name);
  1568. if (value != OperationFailed.Value) {
  1569. context.SetVariable(name, value);
  1570. }
  1571. } else if (pt != null) {
  1572. PythonTypeSlot pts;
  1573. object value;
  1574. if (pt.TryResolveSlot(context, name, out pts) &&
  1575. pts.TryGetValue(context, null, pt, out value)) {
  1576. context.SetVariable(name, value);
  1577. }
  1578. } else {
  1579. // not a known type, we'll do use a site to do the get...
  1580. context.SetVariable(name, PythonOps.GetBoundAttr(context, newmod, name));
  1581. }
  1582. }
  1583. }
  1584. #endregion
  1585. #region Exec
  1586. /// <summary>
  1587. /// Unqualified exec statement support.
  1588. /// A Python helper which will be called for the statement:
  1589. ///
  1590. /// exec code
  1591. /// </summary>
  1592. [ProfilerTreatsAsExternal]
  1593. public static void UnqualifiedExec(CodeContext/*!*/ context, object code) {
  1594. PythonDictionary locals = null;
  1595. PythonDictionary globals = null;
  1596. // if the user passes us a tuple we'll extract the 3 values out of it
  1597. PythonTuple codeTuple = code as PythonTuple;
  1598. if (codeTuple != null && codeTuple.__len__() > 0 && codeTuple.__len__() <= 3) {
  1599. code = codeTuple[0];
  1600. if (codeTuple.__len__() > 1 && codeTuple[1] != null) {
  1601. globals = codeTuple[1] as PythonDictionary;
  1602. if (globals == null) throw PythonOps.TypeError("globals must be dictionary or none");
  1603. }
  1604. if (codeTuple.__len__() > 2 && codeTuple[2] != null) {
  1605. locals = codeTuple[2] as PythonDictionary;
  1606. if (locals == null) throw PythonOps.TypeError("locals must be dictionary or none");
  1607. } else {
  1608. locals = globals;
  1609. }
  1610. }
  1611. QualifiedExec(context, code, globals, locals);
  1612. }
  1613. /// <summary>
  1614. /// Qualified exec statement support,
  1615. /// Python helper which will be called for the statement:
  1616. ///
  1617. /// exec code in globals [, locals ]
  1618. /// </summary>
  1619. [ProfilerTreatsAsExternal]
  1620. public static void QualifiedExec(CodeContext/*!*/ context, object code, PythonDictionary globals, object locals) {
  1621. PythonFile pf;
  1622. Stream cs;
  1623. var pythonContext = PythonContext.GetContext(context);
  1624. bool noLineFeed = true;
  1625. // TODO: use ContentProvider?
  1626. if ((pf = code as PythonFile) != null) {
  1627. List lines = pf.readlines();
  1628. StringBuilder fullCode = new StringBuilder();
  1629. for (int i = 0; i < lines.__len__(); i++) {
  1630. fullCode.Append(lines[i]);
  1631. }
  1632. code = fullCode.ToString();
  1633. } else if ((cs = code as Stream) != null) {
  1634. using (StreamReader reader = new StreamReader(cs)) { // TODO: encoding?
  1635. code = reader.ReadToEnd();
  1636. }
  1637. noLineFeed = false;
  1638. }
  1639. string strCode = code as string;
  1640. if (strCode != null) {
  1641. SourceUnit source;
  1642. if (noLineFeed) {
  1643. source = pythonContext.CreateSourceUnit(new NoLineFeedSourceContentProvider(strCode), "<string>", SourceCodeKind.Statements);
  1644. } else {
  1645. source = pythonContext.CreateSnippet(strCode, SourceCodeKind.Statements);
  1646. }
  1647. PythonCompilerOptions compilerOptions = Builtin.GetRuntimeGeneratedCodeCompilerOptions(context, true, 0);
  1648. // do interpretation only on strings -- not on files, streams, or code objects
  1649. code = FunctionCode.FromSourceUnit(source, compilerOptions, false);
  1650. }
  1651. FunctionCode fc = code as FunctionCode;
  1652. if (fc == null) {
  1653. throw PythonOps.TypeError("arg 1 must be a string, file, Stream, or code object, not {0}", PythonTypeOps.GetName(code));
  1654. }
  1655. if (locals == null) locals = globals;
  1656. if (globals == null) globals = context.GlobalDict;
  1657. if (locals != null && PythonOps.IsMappingType(context, locals) != ScriptingRuntimeHelpers.True) {
  1658. throw PythonOps.TypeError("exec: arg 3 must be mapping or None");
  1659. }
  1660. CodeContext execContext = Builtin.GetExecEvalScope(context, globals, Builtin.GetAttrLocals(context, locals), true, false);
  1661. if (context.LanguageContext.PythonOptions.Frames) {
  1662. List<FunctionStack> stack = PushFrame(execContext, fc);
  1663. try {
  1664. fc.Call(execContext);
  1665. } finally {
  1666. stack.RemoveAt(stack.Count - 1);
  1667. }
  1668. } else {
  1669. fc.Call(execContext);
  1670. }
  1671. }
  1672. #endregion
  1673. public static ICollection GetCollection(object o) {
  1674. ICollection ret = o as ICollection;
  1675. if (ret != null) return ret;
  1676. List<object> al = new List<object>();
  1677. IEnumerator e = GetEnumerator(o);
  1678. while (e.MoveNext()) al.Add(e.Current);
  1679. return al;
  1680. }
  1681. public static IEnumerator GetEnumerator(object o) {
  1682. return GetEnumerator(DefaultContext.Default, o);
  1683. }
  1684. public static IEnumerator GetEnumerator(CodeContext/*!*/ context, object o) {
  1685. IEnumerator ie;
  1686. if (!TryGetEnumerator(context, o, out ie)) {
  1687. throw PythonOps.TypeError("{0} is not iterable", PythonTypeOps.GetName(o));
  1688. }
  1689. return ie;
  1690. }
  1691. // Lack of type restrictions allows this method to return the direct result of __iter__ without
  1692. // wrapping it. This is the proper behavior for Builtin.iter().
  1693. public static object GetEnumeratorObject(CodeContext/*!*/ context, object o) {
  1694. object iterFunc;
  1695. if (PythonOps.TryGetBoundAttr(context, o, "__iter__", out iterFunc) &&
  1696. !Object.ReferenceEquals(iterFunc, NotImplementedType.Value)) {
  1697. return PythonOps.CallWithContext(context, iterFunc);
  1698. }
  1699. return GetEnumerator(context, o);
  1700. }
  1701. public static IEnumerator GetEnumeratorForUnpack(CodeContext/*!*/ context, object enumerable) {
  1702. IEnumerator enumerator;
  1703. if (!TryGetEnumerator(context, enumerable, out enumerator)) {
  1704. throw TypeErrorForNotIterable(enumerable);
  1705. }
  1706. return enumerator;
  1707. }
  1708. public static Exception TypeErrorForNotIterable(object enumerable) {
  1709. return PythonOps.TypeError("'{0}' object is not iterable", PythonTypeOps.GetName(enumerable));
  1710. }
  1711. public static KeyValuePair<IEnumerator, IDisposable> ThrowTypeErrorForBadIteration(CodeContext/*!*/ context, object enumerable) {
  1712. throw PythonOps.TypeError("iteration over non-sequence of type {0}", PythonTypeOps.GetName(enumerable));
  1713. }
  1714. internal static bool TryGetEnumerator(CodeContext/*!*/ context, object enumerable, out IEnumerator enumerator) {
  1715. enumerator = null;
  1716. IEnumerable enumer;
  1717. if (PythonContext.GetContext(context).TryConvertToIEnumerable(enumerable, out enumer)) {
  1718. enumerator = enumer.GetEnumerator();
  1719. return true;
  1720. }
  1721. return false;
  1722. }
  1723. public static void ForLoopDispose(KeyValuePair<IEnumerator, IDisposable> iteratorInfo) {
  1724. if (iteratorInfo.Value != null) {
  1725. iteratorInfo.Value.Dispose();
  1726. }
  1727. }
  1728. public static KeyValuePair<IEnumerator, IDisposable> StringEnumerator(string str) {
  1729. return new KeyValuePair<IEnumerator, IDisposable>(StringOps.StringEnumerator(str), null);
  1730. }
  1731. public static KeyValuePair<IEnumerator, IDisposable> BytesEnumerator(IList<byte> bytes) {
  1732. return new KeyValuePair<IEnumerator, IDisposable>(IListOfByteOps.BytesEnumerator(bytes), null);
  1733. }
  1734. public static KeyValuePair<IEnumerator, IDisposable> BytesIntEnumerator(IList<byte> bytes) {
  1735. return new KeyValuePair<IEnumerator, IDisposable>(IListOfByteOps.BytesIntEnumerator(bytes), null);
  1736. }
  1737. public static KeyValuePair<IEnumerator, IDisposable> GetEnumeratorFromEnumerable(IEnumerable enumerable) {
  1738. IEnumerator enumerator = enumerable.GetEnumerator();
  1739. return new KeyValuePair<IEnumerator, IDisposable>(enumerator, enumerator as IDisposable);
  1740. }
  1741. public static IEnumerable StringEnumerable(string str) {
  1742. return StringOps.StringEnumerable(str);
  1743. }
  1744. public static IEnumerable BytesEnumerable(IList<byte> bytes) {
  1745. return IListOfByteOps.BytesEnumerable(bytes);
  1746. }
  1747. public static IEnumerable BytesIntEnumerable(IList<byte> bytes) {
  1748. return IListOfByteOps.BytesIntEnumerable(bytes);
  1749. }
  1750. #region Exception handling
  1751. // The semantics here are:
  1752. // 1. Each thread has a "current exception", which is returned as a tuple by sys.exc_info().
  1753. // 2. The current exception is set on encountering an except block, even if the except block doesn't
  1754. // match the exception.
  1755. // 3. Each function on exit (either via exception, return, or yield) will restore the "current exception"
  1756. // to the value it had on function-entry.
  1757. //
  1758. // So common codegen would be:
  1759. //
  1760. // function() {
  1761. // $save = SaveCurrentException();
  1762. // try {
  1763. // def foo():
  1764. // try:
  1765. // except:
  1766. // SetCurrentException($exception)
  1767. // <except body>
  1768. //
  1769. // finally {
  1770. // RestoreCurrentException($save)
  1771. // }
  1772. // Called at the start of the except handlers to set the current exception.
  1773. public static object SetCurrentException(CodeContext/*!*/ context, Exception/*!*/ clrException) {
  1774. Assert.NotNull(clrException);
  1775. // we need to extract before we check because ThreadAbort.ExceptionState is cleared after
  1776. // we reset the abort.
  1777. object res = PythonExceptions.ToPython(clrException);
  1778. #if !SILVERLIGHT
  1779. // Check for thread abort exceptions.
  1780. // This is necessary to be able to catch python's KeyboardInterrupt exceptions.
  1781. // CLR restrictions require that this must be called from within a catch block. This gets
  1782. // called even if we aren't going to handle the exception - we'll just reset the abort
  1783. ThreadAbortException tae = clrException as ThreadAbortException;
  1784. if (tae != null && tae.ExceptionState is KeyboardInterruptException) {
  1785. Thread.ResetAbort();
  1786. }
  1787. #endif
  1788. RawException = clrException;
  1789. return res;
  1790. }
  1791. /// <summary>
  1792. /// Called from generated code at the start of a catch block.
  1793. /// </summary>
  1794. public static void BuildExceptionInfo(CodeContext/*!*/ context, Exception clrException) {
  1795. object pyExcep = PythonExceptions.ToPython(clrException);
  1796. List<DynamicStackFrame> frames = clrException.GetFrameList();
  1797. IPythonObject pyObj = pyExcep as IPythonObject;
  1798. object excType;
  1799. if (pyObj != null) {
  1800. // class is always the Python type for new-style types (this is also the common case)
  1801. excType = pyObj.PythonType;
  1802. } else {
  1803. excType = PythonOps.GetBoundAttr(context, pyExcep, "__class__");
  1804. }
  1805. context.LanguageContext.UpdateExceptionInfo(clrException, excType, pyExcep, frames);
  1806. }
  1807. // Clear the current exception. Most callers should restore the exception.
  1808. // This is mainly for sys.exc_clear()
  1809. public static void ClearCurrentException() {
  1810. RestoreCurrentException(null);
  1811. }
  1812. public static void ExceptionHandled(CodeContext context) {
  1813. var pyCtx = context.LanguageContext;
  1814. pyCtx.ExceptionHandled();
  1815. }
  1816. // Called by code-gen to save it. Codegen just needs to pass this back to RestoreCurrentException.
  1817. public static Exception SaveCurrentException() {
  1818. return RawException;
  1819. }
  1820. // Called at function exit (like popping). Pass value from SaveCurrentException.
  1821. public static void RestoreCurrentException(Exception clrException) {
  1822. RawException = clrException;
  1823. }
  1824. public static object CheckException(CodeContext/*!*/ context, object exception, object test) {
  1825. Debug.Assert(exception != null);
  1826. ObjectException objex;
  1827. if ((objex = exception as ObjectException) != null) {
  1828. if (PythonOps.IsSubClass(context, objex.Type, test)) {
  1829. return objex.Instance;
  1830. }
  1831. return null;
  1832. } else if (test is PythonType) {
  1833. if (PythonOps.IsSubClass(test as PythonType, TypeCache.BaseException)) {
  1834. // catching a Python exception type explicitly.
  1835. if (PythonOps.IsInstance(context, exception, test)) return exception;
  1836. } else if (PythonOps.IsSubClass(test as PythonType, DynamicHelpers.GetPythonTypeFromType(typeof(Exception)))) {
  1837. // catching a CLR exception type explicitly.
  1838. Exception clrEx = PythonExceptions.ToClr(exception);
  1839. if (PythonOps.IsInstance(context, clrEx, test)) return clrEx;
  1840. }
  1841. } else if (test is PythonTuple) {
  1842. // we handle multiple exceptions, we'll check them one at a time.
  1843. PythonTuple tt = test as PythonTuple;
  1844. for (int i = 0; i < tt.__len__(); i++) {
  1845. object res = CheckException(context, exception, tt[i]);
  1846. if (res != null) return res;
  1847. }
  1848. } else if (test is OldClass) {
  1849. if (PythonOps.IsInstance(context, exception, test)) {
  1850. // catching a Python type.
  1851. return exception;
  1852. }
  1853. }
  1854. return null;
  1855. }
  1856. private static TraceBack CreateTraceBack(PythonContext pyContext, Exception e) {
  1857. // user provided trace back
  1858. var tb = e.GetTraceBack();
  1859. if (tb != null) {
  1860. return tb;
  1861. }
  1862. IList<DynamicStackFrame> frames = ((IList<DynamicStackFrame>)e.GetFrameList()) ?? new DynamicStackFrame[0];
  1863. return CreateTraceBack(e, frames, frames.Count);
  1864. }
  1865. internal static TraceBack CreateTraceBack(Exception e, IList<DynamicStackFrame> frames, int frameCount) {
  1866. TraceBack tb = null;
  1867. for (int i = 0; i < frameCount; i++) {
  1868. DynamicStackFrame frame = frames[i];
  1869. string name = frame.GetMethodName();
  1870. if (name.IndexOf('#') > 0) {
  1871. // dynamic method, strip the trailing id...
  1872. name = name.Substring(0, name.IndexOf('#'));
  1873. }
  1874. PythonDynamicStackFrame pyFrame = frame as PythonDynamicStackFrame;
  1875. if (pyFrame != null) {
  1876. CodeContext context = pyFrame.CodeContext;
  1877. FunctionCode code = pyFrame.Code;
  1878. TraceBackFrame tbf = new TraceBackFrame(
  1879. context,
  1880. context.GlobalDict,
  1881. context.Dict,
  1882. code);
  1883. tb = new TraceBack(tb, tbf);
  1884. tb.SetLine(frame.GetFileLineNumber());
  1885. }
  1886. }
  1887. e.SetTraceBack(tb);
  1888. return tb;
  1889. }
  1890. /// <summary>
  1891. /// Get an exception tuple for the "current" exception. This is used for sys.exc_info()
  1892. /// </summary>
  1893. public static PythonTuple GetExceptionInfo(CodeContext/*!*/ context) {
  1894. return GetExceptionInfoLocal(context, RawException);
  1895. }
  1896. /// <summary>
  1897. /// Get an exception tuple for a given exception. This is like the inverse of MakeException.
  1898. /// </summary>
  1899. /// <param name="context">the code context</param>
  1900. /// <param name="ex">the exception to create a tuple for.</param>
  1901. /// <returns>a tuple of (type, value, traceback)</returns>
  1902. /// <remarks>This is called directly by the With statement so that it can get an exception tuple
  1903. /// in its own private except handler without disturbing the thread-wide sys.exc_info(). </remarks>
  1904. public static PythonTuple/*!*/ GetExceptionInfoLocal(CodeContext/*!*/ context, Exception ex) {
  1905. if (ex == null) {
  1906. return PythonTuple.MakeTuple(null, null, null);
  1907. }
  1908. PythonContext pc = context.LanguageContext;
  1909. object pyExcep = PythonExceptions.ToPython(ex);
  1910. TraceBack tb = CreateTraceBack(pc, ex);
  1911. IPythonObject pyObj = pyExcep as IPythonObject;
  1912. object excType;
  1913. if (pyObj != null) {
  1914. // class is always the Python type for new-style types (this is also the common case)
  1915. excType = pyObj.PythonType;
  1916. } else {
  1917. excType = PythonOps.GetBoundAttr(context, pyExcep, "__class__");
  1918. }
  1919. pc.UpdateExceptionInfo(excType, pyExcep, tb);
  1920. return PythonTuple.MakeTuple(excType, pyExcep, tb);
  1921. }
  1922. /// <summary>
  1923. /// helper function for re-raised exceptions.
  1924. /// </summary>
  1925. public static Exception MakeRethrownException(CodeContext/*!*/ context) {
  1926. PythonTuple t = GetExceptionInfo(context);
  1927. Exception e = MakeExceptionWorker(context, t[0], t[1], t[2], true);
  1928. return MakeRethrowExceptionWorker(e);
  1929. }
  1930. public static Exception MakeRethrowExceptionWorker(Exception e) {
  1931. e.RemoveTraceBack();
  1932. ExceptionHelpers.UpdateForRethrow(e);
  1933. return e;
  1934. }
  1935. /// <summary>
  1936. /// helper function for non-re-raise exceptions.
  1937. ///
  1938. /// type is the type of exception to throw or an instance. If it
  1939. /// is an instance then value should be null.
  1940. ///
  1941. /// If type is a type then value can either be an instance of type,
  1942. /// a Tuple, or a single value. This case is handled by EC.CreateThrowable.
  1943. /// </summary>
  1944. public static Exception MakeException(CodeContext/*!*/ context, object type, object value, object traceback) {
  1945. Exception e = MakeExceptionWorker(context, type, value, traceback, false);
  1946. e.RemoveFrameList();
  1947. return e;
  1948. }
  1949. private static Exception MakeExceptionWorker(CodeContext/*!*/ context, object type, object value, object traceback, bool forRethrow) {
  1950. Exception throwable;
  1951. PythonType pt;
  1952. if (type is PythonExceptions.BaseException) {
  1953. throwable = PythonExceptions.ToClr(type);
  1954. } else if (type is Exception) {
  1955. throwable = type as Exception;
  1956. } else if ((pt = type as PythonType) != null && typeof(PythonExceptions.BaseException).IsAssignableFrom(pt.UnderlyingSystemType)) {
  1957. throwable = PythonExceptions.CreateThrowableForRaise(context, pt, value);
  1958. } else if (type is OldClass) {
  1959. if (value == null) {
  1960. throwable = new OldInstanceException((OldInstance)PythonCalls.Call(context, type));
  1961. } else {
  1962. throwable = PythonExceptions.CreateThrowableForRaise(context, (OldClass)type, value);
  1963. }
  1964. } else if (type is OldInstance) {
  1965. throwable = new OldInstanceException((OldInstance)type);
  1966. } else {
  1967. throwable = MakeExceptionTypeError(type);
  1968. }
  1969. if (traceback != null) {
  1970. if (!forRethrow) {
  1971. TraceBack tb = traceback as TraceBack;
  1972. if (tb == null) throw PythonOps.TypeError("traceback argument must be a traceback object");
  1973. throwable.SetTraceBack(tb);
  1974. }
  1975. } else {
  1976. throwable.RemoveTraceBack();
  1977. }
  1978. PerfTrack.NoteEvent(PerfTrack.Categories.Exceptions, throwable);
  1979. return throwable;
  1980. }
  1981. public static Exception CreateThrowable(PythonType type, params object[] args) {
  1982. return PythonExceptions.CreateThrowable(type, args);
  1983. }
  1984. #endregion
  1985. public static string[] GetFunctionSignature(PythonFunction function) {
  1986. return new string[] { function.GetSignatureString() };
  1987. }
  1988. public static PythonDictionary CopyAndVerifyDictionary(PythonFunction function, IDictionary dict) {
  1989. foreach (object o in dict.Keys) {
  1990. if (!(o is string)) {
  1991. throw TypeError("{0}() keywords must be strings", function.__name__);
  1992. }
  1993. }
  1994. return new PythonDictionary(dict);
  1995. }
  1996. public static PythonDictionary/*!*/ CopyAndVerifyUserMapping(PythonFunction/*!*/ function, object dict) {
  1997. return UserMappingToPythonDictionary(function.Context, dict, function.func_name);
  1998. }
  1999. public static PythonDictionary UserMappingToPythonDictionary(CodeContext/*!*/ context, object dict, string funcName) {
  2000. // call dict.keys()
  2001. object keys;
  2002. if (!PythonTypeOps.TryInvokeUnaryOperator(context, dict, "keys", out keys)) {
  2003. throw PythonOps.TypeError("{0}() argument after ** must be a mapping, not {1}",
  2004. funcName,
  2005. PythonTypeOps.GetName(dict));
  2006. }
  2007. PythonDictionary res = new PythonDictionary();
  2008. // enumerate the keys getting their values
  2009. IEnumerator enumerator = GetEnumerator(keys);
  2010. while (enumerator.MoveNext()) {
  2011. object o = enumerator.Current;
  2012. string s = o as string;
  2013. if (s == null) {
  2014. Extensible<string> es = o as Extensible<string>;
  2015. if (es == null) {
  2016. throw PythonOps.TypeError("{0}() keywords must be strings, not {0}",
  2017. funcName,
  2018. PythonTypeOps.GetName(dict));
  2019. }
  2020. s = es.Value;
  2021. }
  2022. res[o] = PythonOps.GetIndex(context, dict, o);
  2023. }
  2024. return res;
  2025. }
  2026. public static PythonDictionary CopyAndVerifyPythonDictionary(PythonFunction function, PythonDictionary dict) {
  2027. if (dict._storage.HasNonStringAttributes()) {
  2028. throw TypeError("{0}() keywords must be strings", function.__name__);
  2029. }
  2030. return new PythonDictionary(dict);
  2031. }
  2032. public static object ExtractDictionaryArgument(PythonFunction function, string name, int argCnt, PythonDictionary dict) {
  2033. object val;
  2034. if (dict.TryGetValue(name, out val)) {
  2035. dict.Remove(name);
  2036. return val;
  2037. }
  2038. throw PythonOps.TypeError("{0}() takes exactly {1} arguments ({2} given)",
  2039. function.__name__,
  2040. function.NormalArgumentCount,
  2041. argCnt);
  2042. }
  2043. public static void AddDictionaryArgument(PythonFunction function, string name, object value, PythonDictionary dict) {
  2044. if (dict.ContainsKey(name)) {
  2045. throw MultipleKeywordArgumentError(function, name);
  2046. }
  2047. dict[name] = value;
  2048. }
  2049. public static void VerifyUnduplicatedByPosition(PythonFunction function, string name, int position, int listlen) {
  2050. if (listlen > 0 && listlen > position) {
  2051. throw MultipleKeywordArgumentError(function, name);
  2052. }
  2053. }
  2054. public static List CopyAndVerifyParamsList(PythonFunction function, object list) {
  2055. return new List(list);
  2056. }
  2057. public static PythonTuple GetOrCopyParamsTuple(PythonFunction function, object input) {
  2058. if (input == null) {
  2059. throw PythonOps.TypeError("{0}() argument after * must be a sequence, not NoneType", function.func_name);
  2060. } else if (input.GetType() == typeof(PythonTuple)) {
  2061. return (PythonTuple)input;
  2062. }
  2063. return PythonTuple.Make(input);
  2064. }
  2065. public static object ExtractParamsArgument(PythonFunction function, int argCnt, List list) {
  2066. if (list.__len__() != 0) {
  2067. return list.pop(0);
  2068. }
  2069. throw function.BadArgumentError(argCnt);
  2070. }
  2071. public static void AddParamsArguments(List list, params object[] args) {
  2072. for (int i = 0; i < args.Length; i++) {
  2073. list.insert(i, args[i]);
  2074. }
  2075. }
  2076. /// <summary>
  2077. /// Extracts an argument from either the dictionary or params
  2078. /// </summary>
  2079. public static object ExtractAnyArgument(PythonFunction function, string name, int argCnt, List list, IDictionary dict) {
  2080. object val;
  2081. if (dict.Contains(name)) {
  2082. if (list.__len__() != 0) {
  2083. throw MultipleKeywordArgumentError(function, name);
  2084. }
  2085. val = dict[name];
  2086. dict.Remove(name);
  2087. return val;
  2088. }
  2089. if (list.__len__() != 0) {
  2090. return list.pop(0);
  2091. }
  2092. if (function.ExpandDictPosition == -1 && dict.Count > 0) {
  2093. // python raises an error for extra splatted kw keys before missing arguments.
  2094. // therefore we check for this in the error case here.
  2095. foreach (string x in dict.Keys) {
  2096. bool found = false;
  2097. foreach (string y in function.ArgNames) {
  2098. if (x == y) {
  2099. found = true;
  2100. break;
  2101. }
  2102. }
  2103. if (!found) {
  2104. throw UnexpectedKeywordArgumentError(function, x);
  2105. }
  2106. }
  2107. }
  2108. throw BinderOps.TypeErrorForIncorrectArgumentCount(
  2109. function.__name__,
  2110. function.NormalArgumentCount,
  2111. function.Defaults.Length,
  2112. argCnt,
  2113. function.ExpandListPosition != -1,
  2114. dict.Count > 0);
  2115. }
  2116. public static ArgumentTypeException SimpleTypeError(string message) {
  2117. return new TypeErrorException(message);
  2118. }
  2119. public static object GetParamsValueOrDefault(PythonFunction function, int index, List extraArgs) {
  2120. if (extraArgs.__len__() > 0) {
  2121. return extraArgs.pop(0);
  2122. }
  2123. return function.Defaults[index];
  2124. }
  2125. public static object GetFunctionParameterValue(PythonFunction function, int index, string name, List extraArgs, PythonDictionary dict) {
  2126. if (extraArgs != null && extraArgs.__len__() > 0) {
  2127. return extraArgs.pop(0);
  2128. }
  2129. object val;
  2130. if (dict != null && dict.TryRemoveValue(name, out val)) {
  2131. return val;
  2132. }
  2133. return function.Defaults[index];
  2134. }
  2135. public static void CheckParamsZero(PythonFunction function, List extraArgs) {
  2136. if (extraArgs.__len__() != 0) {
  2137. throw function.BadArgumentError(extraArgs.__len__() + function.NormalArgumentCount);
  2138. }
  2139. }
  2140. public static void CheckUserParamsZero(PythonFunction function, object sequence) {
  2141. int len = PythonOps.Length(sequence);
  2142. if (len != 0) {
  2143. throw function.BadArgumentError(len + function.NormalArgumentCount);
  2144. }
  2145. }
  2146. public static void CheckDictionaryZero(PythonFunction function, IDictionary dict) {
  2147. if (dict.Count != 0) {
  2148. IDictionaryEnumerator ie = dict.GetEnumerator();
  2149. ie.MoveNext();
  2150. throw UnexpectedKeywordArgumentError(function, (string)ie.Key);
  2151. }
  2152. }
  2153. public static bool CheckDictionaryMembers(PythonDictionary dict, string[] names) {
  2154. if (dict.Count != names.Length) {
  2155. return false;
  2156. }
  2157. foreach (string name in names) {
  2158. if (!dict.ContainsKey(name)) {
  2159. return false;
  2160. }
  2161. }
  2162. return true;
  2163. }
  2164. public static object PythonFunctionGetMember(PythonFunction function, string name) {
  2165. object res;
  2166. if (function._dict != null && function._dict.TryGetValue(name, out res)) {
  2167. return res;
  2168. }
  2169. return OperationFailed.Value;
  2170. }
  2171. public static object PythonFunctionSetMember(PythonFunction function, string name, object value) {
  2172. return function.__dict__[name] = value;
  2173. }
  2174. public static void PythonFunctionDeleteDict() {
  2175. throw PythonOps.TypeError("function's dictionary may not be deleted");
  2176. }
  2177. public static void PythonFunctionDeleteDoc(PythonFunction function) {
  2178. function.__doc__ = null;
  2179. }
  2180. public static void PythonFunctionDeleteDefaults(PythonFunction function) {
  2181. function.__defaults__ = null;
  2182. }
  2183. public static bool PythonFunctionDeleteMember(PythonFunction function, string name) {
  2184. if (function._dict == null) return false;
  2185. return function._dict.Remove(name);
  2186. }
  2187. /// <summary>
  2188. /// Creates a new array the values set to Uninitialized.Instance. The array
  2189. /// is large enough to hold for all of the slots allocated for the type and
  2190. /// its sub types.
  2191. /// </summary>
  2192. public static object[] InitializeUserTypeSlots(PythonType/*!*/ type) {
  2193. if (type.SlotCount == 0) {
  2194. // if we later set the weak reference obj we'll create the array
  2195. return null;
  2196. }
  2197. // weak reference is stored at end of slots
  2198. object[] res = new object[type.SlotCount + 1];
  2199. for (int i = 0; i < res.Length - 1; i++) {
  2200. res[i] = Uninitialized.Instance;
  2201. }
  2202. return res;
  2203. }
  2204. public static bool IsClsVisible(CodeContext/*!*/ context) {
  2205. return context.ModuleContext.ShowCls;
  2206. }
  2207. public static object GetInitMember(CodeContext/*!*/ context, PythonType type, object instance) {
  2208. object value;
  2209. bool res = type.TryGetNonCustomBoundMember(context, instance, "__init__", out value);
  2210. Debug.Assert(res);
  2211. return value;
  2212. }
  2213. public static object GetInitSlotMember(CodeContext/*!*/ context, PythonType type, PythonTypeSlot slot, object instance) {
  2214. object value;
  2215. if (!slot.TryGetValue(context, instance, type, out value)) {
  2216. throw PythonOps.TypeError("bad __init__");
  2217. }
  2218. return value;
  2219. }
  2220. public static object GetMixedMember(CodeContext/*!*/ context, PythonType type, object instance, string name) {
  2221. foreach (PythonType t in type.ResolutionOrder) {
  2222. if (t.IsOldClass) {
  2223. OldClass oc = (OldClass)ToPythonType(t);
  2224. object ret;
  2225. if (oc._dict._storage.TryGetValue(name, out ret)) {
  2226. if (instance != null) return oc.GetOldStyleDescriptor(context, ret, instance, oc);
  2227. return ret;
  2228. }
  2229. } else {
  2230. PythonTypeSlot dts;
  2231. if (t.TryLookupSlot(context, name, out dts)) {
  2232. object ret;
  2233. if (dts.TryGetValue(context, instance, type, out ret)) {
  2234. return ret;
  2235. }
  2236. return dts;
  2237. }
  2238. }
  2239. }
  2240. throw AttributeErrorForMissingAttribute(type, name);
  2241. }
  2242. #region Slicing support
  2243. /// <summary>
  2244. /// Helper to determine if the value is a simple numeric type (int or big int or bool) - used for OldInstance
  2245. /// deprecated form of slicing.
  2246. /// </summary>
  2247. public static bool IsNumericObject(object value) {
  2248. return value is int || value is Extensible<int> || value is BigInteger || value is Extensible<BigInteger> || value is bool;
  2249. }
  2250. /// <summary>
  2251. /// Helper to determine if the type is a simple numeric type (int or big int or bool) - used for OldInstance
  2252. /// deprecated form of slicing.
  2253. /// </summary>
  2254. internal static bool IsNumericType(Type t) {
  2255. return IsNonExtensibleNumericType(t) ||
  2256. t.IsSubclassOf(typeof(Extensible<int>)) ||
  2257. t.IsSubclassOf(typeof(Extensible<BigInteger>));
  2258. }
  2259. /// <summary>
  2260. /// Helper to determine if the type is a simple numeric type (int or big int or bool) but not a subclass
  2261. /// </summary>
  2262. internal static bool IsNonExtensibleNumericType(Type t) {
  2263. return t == typeof(int) ||
  2264. t == typeof(bool) ||
  2265. t == typeof(BigInteger);
  2266. }
  2267. /// <summary>
  2268. /// For slicing. Fixes up a BigInteger and returns an integer w/ the length of the
  2269. /// object added if the value is negative.
  2270. /// </summary>
  2271. public static int NormalizeBigInteger(object self, BigInteger bi, ref Nullable<int> length) {
  2272. int val;
  2273. if (bi < BigInteger.Zero) {
  2274. GetLengthOnce(self, ref length);
  2275. if (bi.AsInt32(out val)) {
  2276. Debug.Assert(length.HasValue);
  2277. return val + length.Value;
  2278. } else {
  2279. return -1;
  2280. }
  2281. } else if (bi.AsInt32(out val)) {
  2282. return val;
  2283. }
  2284. return Int32.MaxValue;
  2285. }
  2286. /// <summary>
  2287. /// For slicing. Gets the length of the object, used to only get the length once.
  2288. /// </summary>
  2289. public static int GetLengthOnce(object self, ref Nullable<int> length) {
  2290. if (length != null) return length.Value;
  2291. length = PythonOps.Length(self);
  2292. return length.Value;
  2293. }
  2294. #endregion
  2295. public static ReflectedEvent.BoundEvent MakeBoundEvent(ReflectedEvent eventObj, object instance, Type type) {
  2296. return new ReflectedEvent.BoundEvent(eventObj, instance, DynamicHelpers.GetPythonTypeFromType(type));
  2297. }
  2298. /// <summary>
  2299. /// Helper method for DynamicSite rules that check the version of their dynamic object
  2300. /// TODO - Remove this method for more direct field accesses
  2301. /// </summary>
  2302. /// <param name="o"></param>
  2303. /// <param name="version"></param>
  2304. /// <returns></returns>
  2305. public static bool CheckTypeVersion(object o, int version) {
  2306. IPythonObject po = o as IPythonObject;
  2307. if (po == null) return false;
  2308. return po.PythonType.Version == version;
  2309. }
  2310. public static bool CheckSpecificTypeVersion(PythonType type, int version) {
  2311. return type.Version == version;
  2312. }
  2313. #region Conversion helpers
  2314. internal static MethodInfo GetConversionHelper(string name, ConversionResultKind resultKind) {
  2315. MethodInfo res;
  2316. switch (resultKind) {
  2317. case ConversionResultKind.ExplicitCast:
  2318. case ConversionResultKind.ImplicitCast:
  2319. res = typeof(PythonOps).GetMethod("Throwing" + name);
  2320. break;
  2321. case ConversionResultKind.ImplicitTry:
  2322. case ConversionResultKind.ExplicitTry:
  2323. res = typeof(PythonOps).GetMethod("NonThrowing" + name);
  2324. break;
  2325. default: throw new InvalidOperationException();
  2326. }
  2327. Debug.Assert(res != null);
  2328. return res;
  2329. }
  2330. public static IEnumerable OldInstanceConvertToIEnumerableNonThrowing(CodeContext/*!*/ context, OldInstance/*!*/ self) {
  2331. object callable;
  2332. if (self.TryGetBoundCustomMember(context, "__iter__", out callable)) {
  2333. return CreatePythonEnumerable(self);
  2334. } else if (self.TryGetBoundCustomMember(context, "__getitem__", out callable)) {
  2335. return CreateItemEnumerable(callable, PythonContext.GetContext(context).GetItemCallSite);
  2336. }
  2337. return null;
  2338. }
  2339. public static IEnumerable/*!*/ OldInstanceConvertToIEnumerableThrowing(CodeContext/*!*/ context, OldInstance/*!*/ self) {
  2340. IEnumerable res = OldInstanceConvertToIEnumerableNonThrowing(context, self);
  2341. if (res == null) {
  2342. throw TypeErrorForTypeMismatch("IEnumerable", self);
  2343. }
  2344. return res;
  2345. }
  2346. public static IEnumerable<T> OldInstanceConvertToIEnumerableOfTNonThrowing<T>(CodeContext/*!*/ context, OldInstance/*!*/ self) {
  2347. object callable;
  2348. if (self.TryGetBoundCustomMember(context, "__iter__", out callable)) {
  2349. return new IEnumerableOfTWrapper<T>(CreatePythonEnumerable(self));
  2350. } else if (self.TryGetBoundCustomMember(context, "__getitem__", out callable)) {
  2351. return new IEnumerableOfTWrapper<T>(CreateItemEnumerable(callable, PythonContext.GetContext(context).GetItemCallSite));
  2352. }
  2353. return null;
  2354. }
  2355. public static IEnumerable<T>/*!*/ OldInstanceConvertToIEnumerableOfTThrowing<T>(CodeContext/*!*/ context, OldInstance/*!*/ self) {
  2356. IEnumerable<T> res = OldInstanceConvertToIEnumerableOfTNonThrowing<T>(context, self);
  2357. if (res == null) {
  2358. throw TypeErrorForTypeMismatch("IEnumerable[T]", self);
  2359. }
  2360. return res;
  2361. }
  2362. public static IEnumerator OldInstanceConvertToIEnumeratorNonThrowing(CodeContext/*!*/ context, OldInstance/*!*/ self) {
  2363. object callable;
  2364. if (self.TryGetBoundCustomMember(context, "__iter__", out callable)) {
  2365. return CreatePythonEnumerator(self);
  2366. } else if (self.TryGetBoundCustomMember(context, "__getitem__", out callable)) {
  2367. return CreateItemEnumerator(callable, PythonContext.GetContext(context).GetItemCallSite);
  2368. }
  2369. return null;
  2370. }
  2371. public static IEnumerator/*!*/ OldInstanceConvertToIEnumeratorThrowing(CodeContext/*!*/ context, OldInstance/*!*/ self) {
  2372. IEnumerator res = OldInstanceConvertToIEnumeratorNonThrowing(context, self);
  2373. if (res == null) {
  2374. throw TypeErrorForTypeMismatch("IEnumerator", self);
  2375. }
  2376. return res;
  2377. }
  2378. public static bool? OldInstanceConvertToBoolNonThrowing(CodeContext/*!*/ context, OldInstance/*!*/ oi) {
  2379. object value;
  2380. if (oi.TryGetBoundCustomMember(context, "__nonzero__", out value)) {
  2381. object res = NonThrowingConvertToNonZero(PythonCalls.Call(context, value));
  2382. if (res is int) {
  2383. return ((int)res) != 0;
  2384. } else if (res is bool) {
  2385. return (bool)res;
  2386. }
  2387. } else if (oi.TryGetBoundCustomMember(context, "__len__", out value)) {
  2388. int res;
  2389. if (Converter.TryConvertToInt32(PythonCalls.Call(context, value), out res)) {
  2390. return res != 0;
  2391. }
  2392. }
  2393. return null;
  2394. }
  2395. public static object OldInstanceConvertToBoolThrowing(CodeContext/*!*/ context, OldInstance/*!*/ oi) {
  2396. object value;
  2397. if (oi.TryGetBoundCustomMember(context, "__nonzero__", out value)) {
  2398. return ThrowingConvertToNonZero(PythonCalls.Call(context, value));
  2399. } else if (oi.TryGetBoundCustomMember(context, "__len__", out value)) {
  2400. return PythonContext.GetContext(context).ConvertToInt32(PythonCalls.Call(context, value)) != 0;
  2401. }
  2402. return null;
  2403. }
  2404. public static object OldInstanceConvertNonThrowing(CodeContext/*!*/ context, OldInstance/*!*/ oi, string conversion) {
  2405. object value;
  2406. if (oi.TryGetBoundCustomMember(context, conversion, out value)) {
  2407. if (conversion == "__int__") {
  2408. return NonThrowingConvertToInt(PythonCalls.Call(context, value));
  2409. } else if (conversion == "__long__") {
  2410. return NonThrowingConvertToLong(PythonCalls.Call(context, value));
  2411. } else if (conversion == "__float__") {
  2412. return NonThrowingConvertToFloat(PythonCalls.Call(context, value));
  2413. } else if (conversion == "__complex__") {
  2414. return NonThrowingConvertToComplex(PythonCalls.Call(context, value));
  2415. } else if (conversion == "__str__") {
  2416. return NonThrowingConvertToString(PythonCalls.Call(context, value));
  2417. } else {
  2418. Debug.Assert(false);
  2419. }
  2420. } else if (conversion == "__complex__") {
  2421. object res = OldInstanceConvertNonThrowing(context, oi, "__float__");
  2422. if (res == null) {
  2423. return null;
  2424. }
  2425. return Converter.ConvertToComplex(res);
  2426. }
  2427. return null;
  2428. }
  2429. public static object OldInstanceConvertThrowing(CodeContext/*!*/ context, OldInstance/*!*/ oi, string conversion) {
  2430. object value;
  2431. if (oi.TryGetBoundCustomMember(context, conversion, out value)) {
  2432. if (conversion == "__int__") {
  2433. return ThrowingConvertToInt(PythonCalls.Call(context, value));
  2434. } else if (conversion == "__long__") {
  2435. return ThrowingConvertToLong(PythonCalls.Call(context, value));
  2436. } else if (conversion == "__float__") {
  2437. return ThrowingConvertToFloat(PythonCalls.Call(context, value));
  2438. } else if (conversion == "__complex__") {
  2439. return ThrowingConvertToComplex(PythonCalls.Call(context, value));
  2440. } else if (conversion == "__str__") {
  2441. return ThrowingConvertToString(PythonCalls.Call(context, value));
  2442. } else {
  2443. Debug.Assert(false);
  2444. }
  2445. } else if (conversion == "__complex__") {
  2446. return OldInstanceConvertThrowing(context, oi, "__float__");
  2447. }
  2448. return null;
  2449. }
  2450. public static object ConvertFloatToComplex(object value) {
  2451. if (value == null) {
  2452. return null;
  2453. }
  2454. double d = (double)value;
  2455. return new Complex(d, 0.0);
  2456. }
  2457. internal static bool CheckingConvertToInt(object value) {
  2458. return value is int || value is BigInteger || value is Extensible<int> || value is Extensible<BigInteger>;
  2459. }
  2460. internal static bool CheckingConvertToLong(object value) {
  2461. return CheckingConvertToInt(value);
  2462. }
  2463. internal static bool CheckingConvertToFloat(object value) {
  2464. return value is double || (value != null && value is Extensible<double>);
  2465. }
  2466. internal static bool CheckingConvertToComplex(object value) {
  2467. return value is Complex || value is Extensible<Complex> || CheckingConvertToInt(value) || CheckingConvertToFloat(value);
  2468. }
  2469. internal static bool CheckingConvertToString(object value) {
  2470. return value is string || value is Extensible<string>;
  2471. }
  2472. public static bool CheckingConvertToNonZero(object value) {
  2473. return value is bool || value is int;
  2474. }
  2475. public static object NonThrowingConvertToInt(object value) {
  2476. if (!CheckingConvertToInt(value)) return null;
  2477. return value;
  2478. }
  2479. public static object NonThrowingConvertToLong(object value) {
  2480. if (!CheckingConvertToInt(value)) return null;
  2481. return value;
  2482. }
  2483. public static object NonThrowingConvertToFloat(object value) {
  2484. if (!CheckingConvertToFloat(value)) return null;
  2485. return value;
  2486. }
  2487. public static object NonThrowingConvertToComplex(object value) {
  2488. if (!CheckingConvertToComplex(value)) return null;
  2489. return value;
  2490. }
  2491. public static object NonThrowingConvertToString(object value) {
  2492. if (!CheckingConvertToString(value)) return null;
  2493. return value;
  2494. }
  2495. public static object NonThrowingConvertToNonZero(object value) {
  2496. if (!CheckingConvertToNonZero(value)) return null;
  2497. return value;
  2498. }
  2499. public static object ThrowingConvertToInt(object value) {
  2500. if (!CheckingConvertToInt(value)) throw TypeError(" __int__ returned non-int (type {0})", PythonTypeOps.GetName(value));
  2501. return value;
  2502. }
  2503. public static object ThrowingConvertToFloat(object value) {
  2504. if (!CheckingConvertToFloat(value)) throw TypeError(" __float__ returned non-float (type {0})", PythonTypeOps.GetName(value));
  2505. return value;
  2506. }
  2507. public static object ThrowingConvertToComplex(object value) {
  2508. if (!CheckingConvertToComplex(value)) throw TypeError(" __complex__ returned non-complex (type {0})", PythonTypeOps.GetName(value));
  2509. return value;
  2510. }
  2511. public static object ThrowingConvertToLong(object value) {
  2512. if (!CheckingConvertToComplex(value)) throw TypeError(" __long__ returned non-long (type {0})", PythonTypeOps.GetName(value));
  2513. return value;
  2514. }
  2515. public static object ThrowingConvertToString(object value) {
  2516. if (!CheckingConvertToString(value)) throw TypeError(" __str__ returned non-str (type {0})", PythonTypeOps.GetName(value));
  2517. return value;
  2518. }
  2519. public static bool ThrowingConvertToNonZero(object value) {
  2520. if (!CheckingConvertToNonZero(value)) throw TypeError("__nonzero__ should return bool or int, returned {0}", PythonTypeOps.GetName(value));
  2521. if (value is bool) {
  2522. return (bool)value;
  2523. }
  2524. return ((int)value) != 0;
  2525. }
  2526. #endregion
  2527. public static bool SlotTryGetBoundValue(CodeContext/*!*/ context, PythonTypeSlot/*!*/ slot, object instance, PythonType owner, out object value) {
  2528. Debug.Assert(slot != null);
  2529. return slot.TryGetValue(context, instance, owner, out value);
  2530. }
  2531. public static bool SlotTryGetValue(CodeContext/*!*/ context, PythonTypeSlot/*!*/ slot, object instance, PythonType owner, out object value) {
  2532. return slot.TryGetValue(context, instance, owner, out value);
  2533. }
  2534. public static object SlotGetValue(CodeContext/*!*/ context, PythonTypeSlot/*!*/ slot, object instance, PythonType owner) {
  2535. object value;
  2536. if (!slot.TryGetValue(context, instance, owner, out value)) {
  2537. throw new InvalidOperationException();
  2538. }
  2539. return value;
  2540. }
  2541. public static bool SlotTrySetValue(CodeContext/*!*/ context, PythonTypeSlot/*!*/ slot, object instance, PythonType owner, object value) {
  2542. return slot.TrySetValue(context, instance, owner, value);
  2543. }
  2544. public static object SlotSetValue(CodeContext/*!*/ context, PythonTypeSlot/*!*/ slot, object instance, PythonType owner, object value) {
  2545. if (!slot.TrySetValue(context, instance, owner, value)) {
  2546. throw new InvalidOperationException();
  2547. }
  2548. return value;
  2549. }
  2550. public static bool SlotTryDeleteValue(CodeContext/*!*/ context, PythonTypeSlot/*!*/ slot, object instance, PythonType owner) {
  2551. return slot.TryDeleteValue(context, instance, owner);
  2552. }
  2553. public static BuiltinFunction/*!*/ MakeBoundBuiltinFunction(BuiltinFunction/*!*/ function, object/*!*/ target) {
  2554. return function.BindToInstance(target);
  2555. }
  2556. public static object GetBuiltinFunctionSelf(BuiltinFunction function) {
  2557. return function.BindingSelf;
  2558. }
  2559. /// <summary>
  2560. /// Called from generated code. Gets a builtin function and the BuiltinFunctionData associated
  2561. /// with the object. Tests to see if the function is bound and has the same data for the generated
  2562. /// rule.
  2563. /// </summary>
  2564. public static bool TestBoundBuiltinFunction(BuiltinFunction/*!*/ function, object data) {
  2565. if (function.IsUnbound) {
  2566. // not bound
  2567. return false;
  2568. }
  2569. return function.TestData(data);
  2570. }
  2571. public static BuiltinFunction/*!*/ GetBuiltinMethodDescriptorTemplate(BuiltinMethodDescriptor/*!*/ descriptor) {
  2572. return descriptor.Template;
  2573. }
  2574. public static int GetTypeVersion(PythonType type) {
  2575. return type.Version;
  2576. }
  2577. public static bool TryResolveTypeSlot(CodeContext/*!*/ context, PythonType type, string name, out PythonTypeSlot slot) {
  2578. return type.TryResolveSlot(context, name, out slot);
  2579. }
  2580. public static T[] ConvertTupleToArray<T>(PythonTuple tuple) {
  2581. T[] res = new T[tuple.__len__()];
  2582. for (int i = 0; i < tuple.__len__(); i++) {
  2583. try {
  2584. res[i] = (T)tuple[i];
  2585. } catch (InvalidCastException) {
  2586. res[i] = Converter.Convert<T>(tuple[i]);
  2587. }
  2588. }
  2589. return res;
  2590. }
  2591. #region Function helpers
  2592. public static PythonGenerator MakeGenerator(PythonFunction function, MutableTuple data, object generatorCode) {
  2593. Func<MutableTuple, object> next = generatorCode as Func<MutableTuple, object>;
  2594. if (next == null) {
  2595. next = ((LazyCode<Func<MutableTuple, object>>)generatorCode).EnsureDelegate();
  2596. }
  2597. return new PythonGenerator(function, next, data);
  2598. }
  2599. public static object MakeGeneratorExpression(object function, object input) {
  2600. PythonFunction func = (PythonFunction)function;
  2601. return ((Func<PythonFunction, object, object>)func.func_code.Target)(func, input);
  2602. }
  2603. public static FunctionCode MakeFunctionCode(CodeContext/*!*/ context, string name, string documentation, string[] argNames, FunctionAttributes flags, int startIndex, int endIndex, string path, Delegate code, string[] freeVars, string[] names, string[] cellVars, string[] varNames, int localCount) {
  2604. Compiler.Ast.SerializedScopeStatement scope = new Compiler.Ast.SerializedScopeStatement(name, argNames, flags, startIndex, endIndex, path, freeVars, names, cellVars, varNames);
  2605. return new FunctionCode(context.LanguageContext, code, scope, documentation, localCount);
  2606. }
  2607. [NoSideEffects]
  2608. public static object MakeFunction(CodeContext/*!*/ context, FunctionCode funcInfo, object modName, object[] defaults) {
  2609. return new PythonFunction(context, funcInfo, modName, defaults, null);
  2610. }
  2611. [NoSideEffects]
  2612. public static object MakeFunctionDebug(CodeContext/*!*/ context, FunctionCode funcInfo, object modName, object[] defaults, Delegate target) {
  2613. funcInfo.SetDebugTarget(PythonContext.GetContext(context), target);
  2614. return new PythonFunction(context, funcInfo, modName, defaults, null);
  2615. }
  2616. public static CodeContext FunctionGetContext(PythonFunction func) {
  2617. return func.Context;
  2618. }
  2619. public static object FunctionGetDefaultValue(PythonFunction func, int index) {
  2620. return func.Defaults[index];
  2621. }
  2622. public static int FunctionGetCompatibility(PythonFunction func) {
  2623. return func.FunctionCompatibility;
  2624. }
  2625. public static int FunctionGetID(PythonFunction func) {
  2626. return func.FunctionID;
  2627. }
  2628. public static Delegate FunctionGetTarget(PythonFunction func) {
  2629. return func.func_code.Target;
  2630. }
  2631. public static Delegate FunctionGetLightThrowTarget(PythonFunction func) {
  2632. return func.func_code.LightThrowTarget;
  2633. }
  2634. public static void FunctionPushFrame(PythonContext context) {
  2635. if (PythonFunction.AddRecursionDepth(1) > context.RecursionLimit) {
  2636. throw PythonOps.RuntimeError("maximum recursion depth exceeded");
  2637. }
  2638. }
  2639. public static void FunctionPushFrameCodeContext(CodeContext context) {
  2640. FunctionPushFrame(PythonContext.GetContext(context));
  2641. }
  2642. public static void FunctionPopFrame() {
  2643. PythonFunction.AddRecursionDepth(-1);
  2644. }
  2645. #endregion
  2646. public static object ReturnConversionResult(object value) {
  2647. PythonTuple pt = value as PythonTuple;
  2648. if (pt != null) {
  2649. return pt[0];
  2650. }
  2651. return NotImplementedType.Value;
  2652. }
  2653. /// <summary>
  2654. /// Convert object to a given type. This code is equivalent to NewTypeMaker.EmitConvertFromObject
  2655. /// except that it happens at runtime instead of compile time.
  2656. /// </summary>
  2657. public static T ConvertFromObject<T>(object obj) {
  2658. Type toType = typeof(T);
  2659. object result;
  2660. MethodInfo fastConvertMethod = PythonBinder.GetFastConvertMethod(toType);
  2661. if (fastConvertMethod != null) {
  2662. result = fastConvertMethod.Invoke(null, new object[] { obj });
  2663. } else if (typeof(Delegate).IsAssignableFrom(toType)) {
  2664. result = Converter.ConvertToDelegate(obj, toType);
  2665. } else {
  2666. result = obj;
  2667. }
  2668. return (T)obj;
  2669. }
  2670. public static DynamicMetaObjectBinder MakeComplexCallAction(int count, bool list, string[] keywords) {
  2671. Argument[] infos = CompilerHelpers.MakeRepeatedArray(Argument.Simple, count + keywords.Length);
  2672. if (list) {
  2673. infos[checked(count - 1)] = new Argument(ArgumentType.List);
  2674. }
  2675. for (int i = 0; i < keywords.Length; i++) {
  2676. infos[count + i] = new Argument(keywords[i]);
  2677. }
  2678. return DefaultContext.DefaultPythonContext.Invoke(
  2679. new CallSignature(infos)
  2680. );
  2681. }
  2682. public static DynamicMetaObjectBinder MakeSimpleCallAction(int count) {
  2683. return DefaultContext.DefaultPythonContext.Invoke(
  2684. new CallSignature(CompilerHelpers.MakeRepeatedArray(Argument.Simple, count))
  2685. );
  2686. }
  2687. public static PythonTuple ValidateCoerceResult(object coerceResult) {
  2688. if (coerceResult == null || coerceResult == NotImplementedType.Value) {
  2689. return null;
  2690. }
  2691. PythonTuple pt = coerceResult as PythonTuple;
  2692. if (pt == null) throw PythonOps.TypeError("coercion should return None, NotImplemented, or 2-tuple, got {0}", PythonTypeOps.GetName(coerceResult));
  2693. return pt;
  2694. }
  2695. public static object GetCoerceResultOne(PythonTuple coerceResult) {
  2696. return coerceResult._data[0];
  2697. }
  2698. public static object GetCoerceResultTwo(PythonTuple coerceResult) {
  2699. return coerceResult._data[1];
  2700. }
  2701. public static object MethodCheckSelf(CodeContext/*!*/ context, Method method, object self) {
  2702. return method.CheckSelf(context, self);
  2703. }
  2704. [LightThrowing]
  2705. public static object GeneratorCheckThrowableAndReturnSendValue(object self) {
  2706. return ((PythonGenerator)self).CheckThrowableAndReturnSendValue();
  2707. }
  2708. public static ItemEnumerable CreateItemEnumerable(object callable, CallSite<Func<CallSite, CodeContext, object, int, object>> site) {
  2709. return new ItemEnumerable(callable, site);
  2710. }
  2711. public static DictionaryKeyEnumerator MakeDictionaryKeyEnumerator(PythonDictionary dict) {
  2712. return new DictionaryKeyEnumerator(dict._storage);
  2713. }
  2714. public static IEnumerable CreatePythonEnumerable(object baseObject) {
  2715. return PythonEnumerable.Create(baseObject);
  2716. }
  2717. public static IEnumerator CreateItemEnumerator(object callable, CallSite<Func<CallSite, CodeContext, object, int, object>> site) {
  2718. return new ItemEnumerator(callable, site);
  2719. }
  2720. public static IEnumerator CreatePythonEnumerator(object baseObject) {
  2721. return PythonEnumerator.Create(baseObject);
  2722. }
  2723. public static bool ContainsFromEnumerable(CodeContext/*!*/ context, object enumerable, object value) {
  2724. IEnumerator ie = enumerable as IEnumerator;
  2725. if (ie == null) {
  2726. IEnumerable ienum = enumerable as IEnumerable;
  2727. if (ienum != null) {
  2728. ie = ienum.GetEnumerator();
  2729. } else {
  2730. ie = Converter.ConvertToIEnumerator(enumerable);
  2731. }
  2732. }
  2733. while (ie.MoveNext()) {
  2734. if (PythonOps.EqualRetBool(context, ie.Current, value)) {
  2735. return true;
  2736. }
  2737. }
  2738. return false;
  2739. }
  2740. public static object PythonTypeGetMember(CodeContext/*!*/ context, PythonType type, object instance, string name) {
  2741. return type.GetMember(context, instance, name);
  2742. }
  2743. [NoSideEffects]
  2744. public static object CheckUninitialized(object value, string name) {
  2745. if (value == Uninitialized.Instance) {
  2746. throw new UnboundLocalException(String.Format("Local variable '{0}' referenced before assignment.", name));
  2747. }
  2748. return value;
  2749. }
  2750. #region OldClass/OldInstance public helpers
  2751. public static PythonDictionary OldClassGetDictionary(OldClass klass) {
  2752. return klass._dict;
  2753. }
  2754. public static string OldClassGetName(OldClass klass) {
  2755. return klass.Name;
  2756. }
  2757. public static bool OldInstanceIsCallable(CodeContext/*!*/ context, OldInstance/*!*/ self) {
  2758. object dummy;
  2759. return self.TryGetBoundCustomMember(context, "__call__", out dummy);
  2760. }
  2761. public static object OldClassCheckCallError(OldClass/*!*/ self, object dictionary, object list) {
  2762. if ((dictionary != null && PythonOps.Length(dictionary) != 0) ||
  2763. (list != null && PythonOps.Length(list) != 0)) {
  2764. return OldClass.MakeCallError();
  2765. }
  2766. return null;
  2767. }
  2768. public static object OldClassSetBases(OldClass oc, object value) {
  2769. oc.SetBases(value);
  2770. return value;
  2771. }
  2772. public static object OldClassSetName(OldClass oc, object value) {
  2773. oc.SetName(value);
  2774. return value;
  2775. }
  2776. public static object OldClassSetDictionary(OldClass oc, object value) {
  2777. oc.SetDictionary(value);
  2778. return value;
  2779. }
  2780. public static object OldClassSetNameHelper(OldClass oc, string name, object value) {
  2781. oc.SetNameHelper(name, value);
  2782. return value;
  2783. }
  2784. public static object OldClassTryLookupInit(OldClass oc, object inst) {
  2785. object ret;
  2786. if (oc.TryLookupInit(inst, out ret)) {
  2787. return ret;
  2788. }
  2789. return OperationFailed.Value;
  2790. }
  2791. public static object OldClassMakeCallError(OldClass oc) {
  2792. return OldClass.MakeCallError();
  2793. }
  2794. public static PythonTuple OldClassGetBaseClasses(OldClass oc) {
  2795. return PythonTuple.MakeTuple(oc.BaseClasses.ToArray());
  2796. }
  2797. public static void OldClassDictionaryIsPublic(OldClass oc) {
  2798. oc.DictionaryIsPublic();
  2799. }
  2800. public static object OldClassTryLookupValue(CodeContext/*!*/ context, OldClass oc, string name) {
  2801. object value;
  2802. if (oc.TryLookupValue(context, name, out value)) {
  2803. return value;
  2804. }
  2805. return OperationFailed.Value;
  2806. }
  2807. public static object OldClassLookupValue(CodeContext/*!*/ context, OldClass oc, string name) {
  2808. return oc.LookupValue(context, name);
  2809. }
  2810. public static object OldInstanceGetOptimizedDictionary(OldInstance instance, int keyVersion) {
  2811. CustomInstanceDictionaryStorage storage = instance.Dictionary._storage as CustomInstanceDictionaryStorage;
  2812. if (storage == null || instance._class.HasSetAttr || storage.KeyVersion != keyVersion) {
  2813. return null;
  2814. }
  2815. return storage;
  2816. }
  2817. public static object OldInstanceDictionaryGetValueHelper(object dict, int index, object oldInstance) {
  2818. return ((CustomInstanceDictionaryStorage)dict).GetValueHelper(index, oldInstance);
  2819. }
  2820. public static bool TryOldInstanceDictionaryGetValueHelper(object dict, int index, object oldInstance, out object res) {
  2821. return ((CustomInstanceDictionaryStorage)dict).TryGetValueHelper(index, oldInstance, out res);
  2822. }
  2823. public static object OldInstanceGetBoundMember(CodeContext/*!*/ context, OldInstance instance, string name) {
  2824. return instance.GetBoundMember(context, name);
  2825. }
  2826. public static object OldInstanceDictionarySetExtraValue(object dict, int index, object value) {
  2827. ((CustomInstanceDictionaryStorage)dict).SetExtraValue(index, value);
  2828. return value;
  2829. }
  2830. public static object OldClassDeleteMember(CodeContext/*!*/ context, OldClass self, string name) {
  2831. self.DeleteCustomMember(context, name);
  2832. return null;
  2833. }
  2834. public static bool OldClassTryLookupOneSlot(PythonType type, OldClass self, string name, out object value) {
  2835. return self.TryLookupOneSlot(type, name, out value);
  2836. }
  2837. public static object OldInstanceTryGetBoundCustomMember(CodeContext/*!*/ context, OldInstance self, string name) {
  2838. object value;
  2839. if (self.TryGetBoundCustomMember(context, name, out value)) {
  2840. return value;
  2841. }
  2842. return OperationFailed.Value;
  2843. }
  2844. public static object OldInstanceSetCustomMember(CodeContext/*!*/ context, OldInstance self, string name, object value) {
  2845. self.SetCustomMember(context, name, value);
  2846. return value;
  2847. }
  2848. public static object OldInstanceDeleteCustomMember(CodeContext/*!*/ context, OldInstance self, string name) {
  2849. self.DeleteCustomMember(context, name);
  2850. return null;
  2851. }
  2852. #endregion
  2853. public static object PythonTypeSetCustomMember(CodeContext/*!*/ context, PythonType self, string name, object value) {
  2854. self.SetCustomMember(context, name, value);
  2855. return value;
  2856. }
  2857. public static object PythonTypeDeleteCustomMember(CodeContext/*!*/ context, PythonType self, string name) {
  2858. self.DeleteCustomMember(context, name);
  2859. return null;
  2860. }
  2861. public static bool IsPythonType(PythonType type) {
  2862. return type.IsPythonType;
  2863. }
  2864. public static object PublishModule(CodeContext/*!*/ context, string name) {
  2865. object original = null;
  2866. context.LanguageContext.SystemStateModules.TryGetValue(name, out original);
  2867. var module = ((PythonScopeExtension)context.GlobalScope.GetExtension(context.LanguageContext.ContextId)).Module;
  2868. context.LanguageContext.SystemStateModules[name] = module;
  2869. return original;
  2870. }
  2871. public static void RemoveModule(CodeContext/*!*/ context, string name, object oldValue) {
  2872. if (oldValue != null) {
  2873. PythonContext.GetContext(context).SystemStateModules[name] = oldValue;
  2874. } else {
  2875. PythonContext.GetContext(context).SystemStateModules.Remove(name);
  2876. }
  2877. }
  2878. public static Ellipsis Ellipsis {
  2879. get {
  2880. return Ellipsis.Value;
  2881. }
  2882. }
  2883. public static NotImplementedType NotImplemented {
  2884. get {
  2885. return NotImplementedType.Value;
  2886. }
  2887. }
  2888. public static void ListAddForComprehension(List l, object o) {
  2889. l.AddNoLock(o);
  2890. }
  2891. public static void SetAddForComprehension(SetCollection s, object o) {
  2892. s._items.AddNoLock(o);
  2893. }
  2894. public static void DictAddForComprehension(PythonDictionary d, object k, object v) {
  2895. d._storage.AddNoLock(ref d._storage, k, v);
  2896. }
  2897. public static void ModuleStarted(CodeContext/*!*/ context, ModuleOptions features) {
  2898. context.ModuleContext.Features |= features;
  2899. }
  2900. public static void Warn(CodeContext/*!*/ context, PythonType category, string message, params object[] args) {
  2901. PythonContext pc = PythonContext.GetContext(context);
  2902. object warnings = pc.GetWarningsModule(), warn = null;
  2903. if (warnings != null) {
  2904. warn = PythonOps.GetBoundAttr(context, warnings, "warn");
  2905. }
  2906. message = FormatWarning(message, args);
  2907. if (warn == null) {
  2908. PythonOps.PrintWithDest(context, pc.SystemStandardError, "warning: " + category.Name + ": " + message);
  2909. } else {
  2910. PythonOps.CallWithContext(context, warn, message, category);
  2911. }
  2912. }
  2913. public static void ShowWarning(CodeContext/*!*/ context, PythonType category, string message, string filename, int lineNo) {
  2914. PythonContext pc = PythonContext.GetContext(context);
  2915. object warnings = pc.GetWarningsModule(), warn = null;
  2916. if (warnings != null) {
  2917. warn = PythonOps.GetBoundAttr(context, warnings, "showwarning");
  2918. }
  2919. if (warn == null) {
  2920. PythonOps.PrintWithDestNoNewline(context, pc.SystemStandardError, String.Format("{0}:{1}: {2}: {3}\n", filename, lineNo, category.Name, message));
  2921. } else {
  2922. PythonOps.CallWithContext(context, warn, message, category, filename ?? "", lineNo);
  2923. }
  2924. }
  2925. private static string FormatWarning(string message, object[] args) {
  2926. for (int i = 0; i < args.Length; i++) {
  2927. args[i] = PythonOps.ToString(args[i]);
  2928. }
  2929. message = String.Format(message, args);
  2930. return message;
  2931. }
  2932. private static bool IsPrimitiveNumber(object o) {
  2933. return IsNumericObject(o) ||
  2934. o is Complex ||
  2935. o is double ||
  2936. o is Extensible<Complex> ||
  2937. o is Extensible<double>;
  2938. }
  2939. public static void WarnDivision(CodeContext/*!*/ context, PythonDivisionOptions options, object self, object other) {
  2940. if (options == PythonDivisionOptions.WarnAll) {
  2941. if (IsPrimitiveNumber(self) && IsPrimitiveNumber(other)) {
  2942. if (self is Complex || other is Complex || self is Extensible<Complex> || other is Extensible<Complex>) {
  2943. Warn(context, PythonExceptions.DeprecationWarning, "classic complex division");
  2944. return;
  2945. } else if (self is double || other is double || self is Extensible<double> || other is Extensible<double>) {
  2946. Warn(context, PythonExceptions.DeprecationWarning, "classic float division");
  2947. return;
  2948. } else {
  2949. WarnDivisionInts(context, self, other);
  2950. }
  2951. }
  2952. } else if (IsNumericObject(self) && IsNumericObject(other)) {
  2953. WarnDivisionInts(context, self, other);
  2954. }
  2955. }
  2956. private static void WarnDivisionInts(CodeContext/*!*/ context, object self, object other) {
  2957. if (self is BigInteger || other is BigInteger || self is Extensible<BigInteger> || other is Extensible<BigInteger>) {
  2958. Warn(context, PythonExceptions.DeprecationWarning, "classic long division");
  2959. } else {
  2960. Warn(context, PythonExceptions.DeprecationWarning, "classic int division");
  2961. }
  2962. }
  2963. public static DynamicMetaObjectBinder MakeComboAction(CodeContext/*!*/ context, DynamicMetaObjectBinder opBinder, DynamicMetaObjectBinder convBinder) {
  2964. return PythonContext.GetContext(context).BinaryOperationRetType((PythonBinaryOperationBinder)opBinder, (PythonConversionBinder)convBinder);
  2965. }
  2966. public static DynamicMetaObjectBinder MakeInvokeAction(CodeContext/*!*/ context, CallSignature signature) {
  2967. return PythonContext.GetContext(context).Invoke(signature);
  2968. }
  2969. public static DynamicMetaObjectBinder MakeGetAction(CodeContext/*!*/ context, string name, bool isNoThrow) {
  2970. return PythonContext.GetContext(context).GetMember(name);
  2971. }
  2972. public static DynamicMetaObjectBinder MakeCompatGetAction(CodeContext/*!*/ context, string name) {
  2973. return PythonContext.GetContext(context).CompatGetMember(name, false);
  2974. }
  2975. public static DynamicMetaObjectBinder MakeCompatInvokeAction(CodeContext/*!*/ context, CallInfo callInfo) {
  2976. return PythonContext.GetContext(context).CompatInvoke(callInfo);
  2977. }
  2978. public static DynamicMetaObjectBinder MakeCompatConvertAction(CodeContext/*!*/ context, Type toType, bool isExplicit) {
  2979. return PythonContext.GetContext(context).Convert(toType, isExplicit ? ConversionResultKind.ExplicitCast : ConversionResultKind.ImplicitCast).CompatBinder;
  2980. }
  2981. public static DynamicMetaObjectBinder MakeSetAction(CodeContext/*!*/ context, string name) {
  2982. return PythonContext.GetContext(context).SetMember(name);
  2983. }
  2984. public static DynamicMetaObjectBinder MakeDeleteAction(CodeContext/*!*/ context, string name) {
  2985. return PythonContext.GetContext(context).DeleteMember(name);
  2986. }
  2987. public static DynamicMetaObjectBinder MakeConversionAction(CodeContext/*!*/ context, Type type, ConversionResultKind kind) {
  2988. return PythonContext.GetContext(context).Convert(type, kind);
  2989. }
  2990. public static DynamicMetaObjectBinder MakeTryConversionAction(CodeContext/*!*/ context, Type type, ConversionResultKind kind) {
  2991. return PythonContext.GetContext(context).Convert(type, kind);
  2992. }
  2993. public static DynamicMetaObjectBinder MakeOperationAction(CodeContext/*!*/ context, int operationName) {
  2994. return PythonContext.GetContext(context).Operation((PythonOperationKind)operationName);
  2995. }
  2996. public static DynamicMetaObjectBinder MakeUnaryOperationAction(CodeContext/*!*/ context, ExpressionType expressionType) {
  2997. return PythonContext.GetContext(context).UnaryOperation(expressionType);
  2998. }
  2999. public static DynamicMetaObjectBinder MakeBinaryOperationAction(CodeContext/*!*/ context, ExpressionType expressionType) {
  3000. return PythonContext.GetContext(context).BinaryOperation(expressionType);
  3001. }
  3002. public static DynamicMetaObjectBinder MakeGetIndexAction(CodeContext/*!*/ context, int argCount) {
  3003. return PythonContext.GetContext(context).GetIndex(argCount);
  3004. }
  3005. public static DynamicMetaObjectBinder MakeSetIndexAction(CodeContext/*!*/ context, int argCount) {
  3006. return PythonContext.GetContext(context).SetIndex(argCount);
  3007. }
  3008. public static DynamicMetaObjectBinder MakeDeleteIndexAction(CodeContext/*!*/ context, int argCount) {
  3009. return PythonContext.GetContext(context).DeleteIndex(argCount);
  3010. }
  3011. public static DynamicMetaObjectBinder MakeGetSliceBinder(CodeContext/*!*/ context) {
  3012. return PythonContext.GetContext(context).GetSlice;
  3013. }
  3014. public static DynamicMetaObjectBinder MakeSetSliceBinder(CodeContext/*!*/ context) {
  3015. return PythonContext.GetContext(context).SetSliceBinder;
  3016. }
  3017. public static DynamicMetaObjectBinder MakeDeleteSliceBinder(CodeContext/*!*/ context) {
  3018. return PythonContext.GetContext(context).DeleteSlice;
  3019. }
  3020. /// <summary>
  3021. /// Provides access to AppDomain.DefineDynamicAssembly which cannot be called from a DynamicMethod
  3022. /// </summary>
  3023. public static AssemblyBuilder DefineDynamicAssembly(AssemblyName name, AssemblyBuilderAccess access) {
  3024. return AppDomain.CurrentDomain.DefineDynamicAssembly(name, access);
  3025. }
  3026. /// <summary>
  3027. /// Generates a new delegate type. The last type in the array is the return type.
  3028. /// </summary>
  3029. public static Type/*!*/ MakeNewCustomDelegate(Type/*!*/[]/*!*/ types) {
  3030. return MakeNewCustomDelegate(types, null);
  3031. }
  3032. /// <summary>
  3033. /// Generates a new delegate type. The last type in the array is the return type.
  3034. /// </summary>
  3035. public static Type/*!*/ MakeNewCustomDelegate(Type/*!*/[]/*!*/ types, CallingConvention? callingConvention) {
  3036. const MethodAttributes CtorAttributes = MethodAttributes.RTSpecialName | MethodAttributes.HideBySig | MethodAttributes.Public;
  3037. const MethodImplAttributes ImplAttributes = MethodImplAttributes.Runtime | MethodImplAttributes.Managed;
  3038. const MethodAttributes InvokeAttributes = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.NewSlot | MethodAttributes.Virtual;
  3039. Type returnType = types[types.Length - 1];
  3040. Type[] parameters = ArrayUtils.RemoveLast(types);
  3041. TypeBuilder builder = Snippets.Shared.DefineDelegateType("Delegate" + types.Length);
  3042. builder.DefineConstructor(CtorAttributes, CallingConventions.Standard, _DelegateCtorSignature).SetImplementationFlags(ImplAttributes);
  3043. builder.DefineMethod("Invoke", InvokeAttributes, returnType, parameters).SetImplementationFlags(ImplAttributes);
  3044. if (callingConvention != null) {
  3045. builder.SetCustomAttribute(new CustomAttributeBuilder(
  3046. typeof(UnmanagedFunctionPointerAttribute).GetConstructor(new[] { typeof(CallingConvention) }),
  3047. new object[] { callingConvention })
  3048. );
  3049. }
  3050. return builder.CreateType();
  3051. }
  3052. #if !SILVERLIGHT
  3053. /// <summary>
  3054. /// Provides the entry point for a compiled module. The stub exe calls into InitializeModule which
  3055. /// does the actual work of adding references and importing the main module. Upon completion it returns
  3056. /// the exit code that the program reported via SystemExit or 0.
  3057. /// </summary>
  3058. public static int InitializeModule(Assembly/*!*/ precompiled, string/*!*/ main, string[] references) {
  3059. ContractUtils.RequiresNotNull(precompiled, "precompiled");
  3060. ContractUtils.RequiresNotNull(main, "main");
  3061. Dictionary<string, object> options = new Dictionary<string, object>();
  3062. options["Arguments"] = ArrayUtils.RemoveFirst(Environment.GetCommandLineArgs()); // remove the EXE
  3063. var pythonEngine = Python.CreateEngine(options);
  3064. var pythonContext = (PythonContext)HostingHelpers.GetLanguageContext(pythonEngine);
  3065. foreach (var scriptCode in SavableScriptCode.LoadFromAssembly(pythonContext.DomainManager, precompiled)) {
  3066. pythonContext.GetCompiledLoader().AddScriptCode(scriptCode);
  3067. }
  3068. if (references != null) {
  3069. foreach (string referenceName in references) {
  3070. pythonContext.DomainManager.LoadAssembly(Assembly.Load(referenceName));
  3071. }
  3072. }
  3073. ModuleContext modCtx = new ModuleContext(new PythonDictionary(), pythonContext);
  3074. // import __main__
  3075. try {
  3076. Importer.Import(modCtx.GlobalContext, main, PythonTuple.EMPTY, 0);
  3077. } catch (SystemExitException ex) {
  3078. object dummy;
  3079. return ex.GetExitCode(out dummy);
  3080. }
  3081. return 0;
  3082. }
  3083. #endif
  3084. public static CodeContext GetPythonTypeContext(PythonType pt) {
  3085. return pt.PythonContext.SharedContext;
  3086. }
  3087. public static Delegate GetDelegate(CodeContext/*!*/ context, object target, Type type) {
  3088. return context.LanguageContext.DelegateCreator.GetDelegate(target, type);
  3089. }
  3090. public static int CompareLists(List self, List other) {
  3091. return self.CompareTo(other);
  3092. }
  3093. public static int CompareTuples(PythonTuple self, PythonTuple other) {
  3094. return self.CompareTo(other);
  3095. }
  3096. public static int CompareFloats(double self, double other) {
  3097. return DoubleOps.Compare(self, other);
  3098. }
  3099. public static Bytes MakeBytes(byte[] bytes) {
  3100. return new Bytes(bytes);
  3101. }
  3102. public static byte[] MakeByteArray(this string s) {
  3103. byte[] ret = new byte[s.Length];
  3104. for (int i = 0; i < s.Length; i++) {
  3105. if (s[i] < 0x100) ret[i] = (byte)s[i];
  3106. else throw PythonOps.UnicodeEncodeError("'ascii' codec can't decode byte {0:X} in position {1}: ordinal not in range", (int)ret[i], i);
  3107. }
  3108. return ret;
  3109. }
  3110. public static string MakeString(this IList<byte> bytes) {
  3111. return MakeString(bytes, bytes.Count);
  3112. }
  3113. internal static string MakeString(this byte[] preamble, IList<byte> bytes) {
  3114. char[] chars = new char[preamble.Length + bytes.Count];
  3115. for (int i = 0; i < preamble.Length; i++) {
  3116. chars[i] = (char)preamble[i];
  3117. }
  3118. for (int i = 0; i < bytes.Count; i++) {
  3119. chars[i + preamble.Length] = (char)bytes[i];
  3120. }
  3121. return new String(chars);
  3122. }
  3123. internal static string MakeString(this IList<byte> bytes, int maxBytes) {
  3124. int bytesToCopy = Math.Min(bytes.Count, maxBytes);
  3125. StringBuilder b = new StringBuilder(bytesToCopy);
  3126. for (int i = 0; i < bytesToCopy; i++) {
  3127. b.Append((char)bytes[i]);
  3128. }
  3129. return b.ToString();
  3130. }
  3131. /// <summary>
  3132. /// Called from generated code, helper to remove a name
  3133. /// </summary>
  3134. public static void RemoveName(CodeContext/*!*/ context, string name) {
  3135. if (!context.TryRemoveVariable(name)) {
  3136. throw PythonOps.NameError(name);
  3137. }
  3138. }
  3139. /// <summary>
  3140. /// Called from generated code, helper to do name lookup
  3141. /// </summary>
  3142. public static object LookupName(CodeContext/*!*/ context, string name) {
  3143. object value;
  3144. if (context.TryLookupName(name, out value)) {
  3145. return value;
  3146. } else if (context.TryLookupBuiltin(name, out value)) {
  3147. return value;
  3148. }
  3149. throw PythonOps.NameError(name);
  3150. }
  3151. /// <summary>
  3152. /// Called from generated code, helper to do name assignment
  3153. /// </summary>
  3154. public static object SetName(CodeContext/*!*/ context, string name, object value) {
  3155. context.SetVariable(name, value);
  3156. return value;
  3157. }
  3158. /// <summary>
  3159. /// Returns an IntPtr in the proper way to CPython - an int or a Python long
  3160. /// </summary>
  3161. public static object/*!*/ ToPython(this IntPtr handle) {
  3162. long value = handle.ToInt64();
  3163. if (value >= Int32.MinValue && value <= Int32.MaxValue) {
  3164. return ScriptingRuntimeHelpers.Int32ToObject((int)value);
  3165. }
  3166. return (BigInteger)value;
  3167. }
  3168. #region Global Access
  3169. public static CodeContext/*!*/ CreateLocalContext(CodeContext/*!*/ outerContext, MutableTuple boxes, string[] args) {
  3170. return new CodeContext(
  3171. new PythonDictionary(
  3172. new RuntimeVariablesDictionaryStorage(boxes, args)
  3173. ),
  3174. outerContext.ModuleContext
  3175. );
  3176. }
  3177. public static CodeContext/*!*/ GetGlobalContext(CodeContext/*!*/ context) {
  3178. return context.ModuleContext.GlobalContext;
  3179. }
  3180. public static ClosureCell/*!*/ MakeClosureCell() {
  3181. return new ClosureCell(Uninitialized.Instance);
  3182. }
  3183. public static ClosureCell/*!*/ MakeClosureCellWithValue(object initialValue) {
  3184. return new ClosureCell(initialValue);
  3185. }
  3186. public static MutableTuple/*!*/ GetClosureTupleFromFunction(PythonFunction/*!*/ function) {
  3187. return GetClosureTupleFromContext(function.Context);
  3188. }
  3189. public static MutableTuple/*!*/ GetClosureTupleFromGenerator(PythonGenerator/*!*/ generator) {
  3190. return GetClosureTupleFromContext(generator.Context);
  3191. }
  3192. public static MutableTuple/*!*/ GetClosureTupleFromContext(CodeContext/*!*/ context) {
  3193. return (context.Dict._storage as RuntimeVariablesDictionaryStorage).Tuple;
  3194. }
  3195. public static CodeContext/*!*/ GetParentContextFromFunction(PythonFunction/*!*/ function) {
  3196. return function.Context;
  3197. }
  3198. public static CodeContext/*!*/ GetParentContextFromGenerator(PythonGenerator/*!*/ generator) {
  3199. return generator.Context;
  3200. }
  3201. public static object GetGlobal(CodeContext/*!*/ context, string name) {
  3202. return GetVariable(context, name, true, false);
  3203. }
  3204. public static object GetLocal(CodeContext/*!*/ context, string name) {
  3205. return GetVariable(context, name, false, false);
  3206. }
  3207. internal static object GetVariable(CodeContext/*!*/ context, string name, bool isGlobal, bool lightThrow) {
  3208. object res;
  3209. if (isGlobal) {
  3210. if (context.TryGetGlobalVariable(name, out res)) {
  3211. return res;
  3212. }
  3213. } else {
  3214. if (context.TryLookupName(name, out res)) {
  3215. return res;
  3216. }
  3217. }
  3218. PythonDictionary builtins = context.GetBuiltinsDict();
  3219. if (builtins != null && builtins.TryGetValue(name, out res)) {
  3220. return res;
  3221. }
  3222. Exception ex;
  3223. if (isGlobal) {
  3224. ex = GlobalNameError(name);
  3225. } else {
  3226. ex = NameError(name);
  3227. }
  3228. if (lightThrow) {
  3229. return LightExceptions.Throw(ex);
  3230. }
  3231. throw ex;
  3232. }
  3233. public static object RawGetGlobal(CodeContext/*!*/ context, string name) {
  3234. object res;
  3235. if (context.TryGetGlobalVariable(name, out res)) {
  3236. return res;
  3237. }
  3238. return Uninitialized.Instance;
  3239. }
  3240. public static object RawGetLocal(CodeContext/*!*/ context, string name) {
  3241. object res;
  3242. if (context.TryLookupName(name, out res)) {
  3243. return res;
  3244. }
  3245. return Uninitialized.Instance;
  3246. }
  3247. public static void SetGlobal(CodeContext/*!*/ context, string name, object value) {
  3248. context.SetGlobalVariable(name, value);
  3249. }
  3250. public static void SetLocal(CodeContext/*!*/ context, string name, object value) {
  3251. context.SetVariable(name, value);
  3252. }
  3253. public static void DeleteGlobal(CodeContext/*!*/ context, string name) {
  3254. if (context.TryRemoveGlobalVariable(name)) {
  3255. return;
  3256. }
  3257. throw NameError(name);
  3258. }
  3259. public static void DeleteLocal(CodeContext/*!*/ context, string name) {
  3260. if (context.TryRemoveVariable(name)) {
  3261. return;
  3262. }
  3263. throw NameError(name);
  3264. }
  3265. public static PythonGlobal/*!*/[] GetGlobalArrayFromContext(CodeContext/*!*/ context) {
  3266. return context.GetGlobalArray();
  3267. }
  3268. #endregion
  3269. #region Exception Factories
  3270. public static Exception MultipleKeywordArgumentError(PythonFunction function, string name) {
  3271. return TypeError("{0}() got multiple values for keyword argument '{1}'", function.__name__, name);
  3272. }
  3273. public static Exception UnexpectedKeywordArgumentError(PythonFunction function, string name) {
  3274. return TypeError("{0}() got an unexpected keyword argument '{1}'", function.__name__, name);
  3275. }
  3276. public static Exception StaticAssignmentFromInstanceError(PropertyTracker tracker, bool isAssignment) {
  3277. if (isAssignment) {
  3278. if (DynamicHelpers.GetPythonTypeFromType(tracker.DeclaringType).IsPythonType) {
  3279. return PythonOps.TypeError(
  3280. "can't set attributes of built-in/extension type '{0}'",
  3281. NameConverter.GetTypeName(tracker.DeclaringType));
  3282. }
  3283. return new MissingMemberException(string.Format(
  3284. Resources.StaticAssignmentFromInstanceError,
  3285. tracker.Name,
  3286. NameConverter.GetTypeName(tracker.DeclaringType)));
  3287. }
  3288. return new MissingMemberException(string.Format(Resources.StaticAccessFromInstanceError,
  3289. tracker.Name,
  3290. NameConverter.GetTypeName(tracker.DeclaringType)));
  3291. }
  3292. public static Exception FunctionBadArgumentError(PythonFunction func, int count) {
  3293. return func.BadArgumentError(count);
  3294. }
  3295. public static Exception BadKeywordArgumentError(PythonFunction func, int count) {
  3296. return func.BadKeywordArgumentError(count);
  3297. }
  3298. public static Exception AttributeErrorForMissingOrReadonly(CodeContext/*!*/ context, PythonType dt, string name) {
  3299. PythonTypeSlot dts;
  3300. if (dt.TryResolveSlot(context, name, out dts)) {
  3301. throw PythonOps.AttributeErrorForReadonlyAttribute(dt.Name, name);
  3302. }
  3303. throw PythonOps.AttributeErrorForMissingAttribute(dt.Name, name);
  3304. }
  3305. public static Exception AttributeErrorForMissingAttribute(object o, string name) {
  3306. PythonType dt = o as PythonType;
  3307. if (dt != null)
  3308. return PythonOps.AttributeErrorForMissingAttribute(dt.Name, name);
  3309. return AttributeErrorForReadonlyAttribute(PythonTypeOps.GetName(o), name);
  3310. }
  3311. public static Exception ValueError(string format, params object[] args) {
  3312. return new ValueErrorException(string.Format(format, args));
  3313. }
  3314. public static Exception KeyError(object key) {
  3315. return PythonExceptions.CreateThrowable(PythonExceptions.KeyError, key);
  3316. }
  3317. public static Exception KeyError(string format, params object[] args) {
  3318. return new KeyNotFoundException(string.Format(format, args));
  3319. }
  3320. public static Exception UnicodeDecodeError(string format, params object[] args) {
  3321. return new System.Text.DecoderFallbackException(string.Format(format, args));
  3322. }
  3323. public static Exception UnicodeEncodeError(string format, params object[] args) {
  3324. return new System.Text.EncoderFallbackException(string.Format(format, args));
  3325. }
  3326. public static Exception IOError(Exception inner) {
  3327. return new System.IO.IOException(inner.Message, inner);
  3328. }
  3329. public static Exception IOError(string format, params object[] args) {
  3330. return new System.IO.IOException(string.Format(format, args));
  3331. }
  3332. public static Exception EofError(string format, params object[] args) {
  3333. return new System.IO.EndOfStreamException(string.Format(format, args));
  3334. }
  3335. public static Exception StandardError(string format, params object[] args) {
  3336. return new SystemException(string.Format(format, args));
  3337. }
  3338. public static Exception ZeroDivisionError(string format, params object[] args) {
  3339. return new DivideByZeroException(string.Format(format, args));
  3340. }
  3341. public static Exception SystemError(string format, params object[] args) {
  3342. return new SystemException(string.Format(format, args));
  3343. }
  3344. public static Exception TypeError(string format, params object[] args) {
  3345. return new TypeErrorException(string.Format(format, args));
  3346. }
  3347. public static Exception IndexError(string format, params object[] args) {
  3348. return new System.IndexOutOfRangeException(string.Format(format, args));
  3349. }
  3350. public static Exception MemoryError(string format, params object[] args) {
  3351. return new OutOfMemoryException(string.Format(format, args));
  3352. }
  3353. public static Exception ArithmeticError(string format, params object[] args) {
  3354. return new ArithmeticException(string.Format(format, args));
  3355. }
  3356. public static Exception NotImplementedError(string format, params object[] args) {
  3357. return new NotImplementedException(string.Format(format, args));
  3358. }
  3359. public static Exception AttributeError(string format, params object[] args) {
  3360. return new MissingMemberException(string.Format(format, args));
  3361. }
  3362. public static Exception OverflowError(string format, params object[] args) {
  3363. return new System.OverflowException(string.Format(format, args));
  3364. }
  3365. public static Exception WindowsError(string format, params object[] args) {
  3366. #if !SILVERLIGHT // System.ComponentModel.Win32Exception
  3367. return new System.ComponentModel.Win32Exception(string.Format(format, args));
  3368. #else
  3369. return new System.SystemException(string.Format(format, args));
  3370. #endif
  3371. }
  3372. public static Exception SystemExit() {
  3373. return new SystemExitException();
  3374. }
  3375. public static void SyntaxWarning(string message, SourceUnit sourceUnit, SourceSpan span, int errorCode) {
  3376. PythonContext pc = (PythonContext)sourceUnit.LanguageContext;
  3377. CodeContext context = pc.SharedContext;
  3378. ShowWarning(context, PythonExceptions.SyntaxWarning, message, sourceUnit.Path, span.Start.Line);
  3379. }
  3380. public static SyntaxErrorException SyntaxError(string message, SourceUnit sourceUnit, SourceSpan span, int errorCode) {
  3381. switch (errorCode & ErrorCodes.ErrorMask) {
  3382. case ErrorCodes.IndentationError:
  3383. return new IndentationException(message, sourceUnit, span, errorCode, Severity.FatalError);
  3384. case ErrorCodes.TabError:
  3385. return new TabException(message, sourceUnit, span, errorCode, Severity.FatalError);
  3386. default:
  3387. var res = new SyntaxErrorException(message, sourceUnit, span, errorCode, Severity.FatalError);
  3388. if ((errorCode & ErrorCodes.NoCaret) != 0) {
  3389. res.Data[PythonContext._syntaxErrorNoCaret] = ScriptingRuntimeHelpers.True;
  3390. }
  3391. return res;
  3392. }
  3393. }
  3394. public static SyntaxErrorException BadSourceError(byte badByte, SourceSpan span, string path) {
  3395. SyntaxErrorException res = new SyntaxErrorException(
  3396. String.Format("Non-ASCII character '\\x{0:x2}' in file {2} on line {1}, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details",
  3397. badByte,
  3398. span.Start.Line,
  3399. path
  3400. ),
  3401. path,
  3402. null,
  3403. null,
  3404. span,
  3405. ErrorCodes.SyntaxError,
  3406. Severity.FatalError
  3407. );
  3408. res.Data[PythonContext._syntaxErrorNoCaret] = ScriptingRuntimeHelpers.True;
  3409. return res;
  3410. }
  3411. public static Exception StopIteration() {
  3412. return StopIteration("");
  3413. }
  3414. public static Exception InvalidType(object o, RuntimeTypeHandle handle) {
  3415. return PythonOps.TypeErrorForTypeMismatch(DynamicHelpers.GetPythonTypeFromType(Type.GetTypeFromHandle(handle)).Name, o);
  3416. }
  3417. public static Exception ZeroDivisionError() {
  3418. return ZeroDivisionError("Attempted to divide by zero.");
  3419. }
  3420. // If you do "(a, b) = (1, 2, 3, 4)"
  3421. public static Exception ValueErrorForUnpackMismatch(int left, int right) {
  3422. System.Diagnostics.Debug.Assert(left != right);
  3423. if (left > right)
  3424. return ValueError("need more than {0} values to unpack", right);
  3425. else
  3426. return ValueError("too many values to unpack");
  3427. }
  3428. public static Exception NameError(string name) {
  3429. return new UnboundNameException(string.Format("name '{0}' is not defined", name));
  3430. }
  3431. public static Exception GlobalNameError(string name) {
  3432. return new UnboundNameException(string.Format("global name '{0}' is not defined", name));
  3433. }
  3434. // If an unbound method is called without a "self" argument, or a "self" argument of a bad type
  3435. public static Exception TypeErrorForUnboundMethodCall(string methodName, Type methodType, object instance) {
  3436. return TypeErrorForUnboundMethodCall(methodName, DynamicHelpers.GetPythonTypeFromType(methodType), instance);
  3437. }
  3438. public static Exception TypeErrorForUnboundMethodCall(string methodName, PythonType methodType, object instance) {
  3439. string message = string.Format("unbound method {0}() must be called with {1} instance as first argument (got {2} instead)",
  3440. methodName, methodType.Name, PythonTypeOps.GetName(instance));
  3441. return TypeError(message);
  3442. }
  3443. // When a generator first starts, before it gets to the first yield point, you can't call generator.Send(x) where x != null.
  3444. // See Pep342 for details.
  3445. public static Exception TypeErrorForIllegalSend() {
  3446. string message = "can't send non-None value to a just-started generator";
  3447. return TypeError(message);
  3448. }
  3449. // If a method is called with an incorrect number of arguments
  3450. // You should use TypeErrorForUnboundMethodCall() for unbound methods called with 0 arguments
  3451. public static Exception TypeErrorForArgumentCountMismatch(string methodName, int expectedArgCount, int actualArgCount) {
  3452. return TypeError("{0}() takes exactly {1} argument{2} ({3} given)",
  3453. methodName, expectedArgCount, expectedArgCount == 1 ? "" : "s", actualArgCount);
  3454. }
  3455. public static Exception TypeErrorForTypeMismatch(string expectedTypeName, object instance) {
  3456. return TypeError("expected {0}, got {1}", expectedTypeName, PythonOps.GetPythonTypeName(instance));
  3457. }
  3458. // If hash is called on an instance of an unhashable type
  3459. public static Exception TypeErrorForUnhashableType(string typeName) {
  3460. return TypeError(typeName + " objects are unhashable");
  3461. }
  3462. public static Exception TypeErrorForUnhashableObject(object obj) {
  3463. return TypeErrorForUnhashableType(PythonTypeOps.GetName(obj));
  3464. }
  3465. internal static Exception TypeErrorForIncompatibleObjectLayout(string prefix, PythonType type, Type newType) {
  3466. return TypeError("{0}: '{1}' object layout differs from '{2}'", prefix, type.Name, newType);
  3467. }
  3468. public static Exception TypeErrorForNonStringAttribute() {
  3469. return TypeError("attribute name must be string");
  3470. }
  3471. internal static Exception TypeErrorForBadInstance(string template, object instance) {
  3472. return TypeError(template, PythonOps.GetPythonTypeName(instance));
  3473. }
  3474. public static Exception TypeErrorForBinaryOp(string opSymbol, object x, object y) {
  3475. throw PythonOps.TypeError("unsupported operand type(s) for {0}: '{1}' and '{2}'",
  3476. opSymbol, GetPythonTypeName(x), GetPythonTypeName(y));
  3477. }
  3478. public static Exception TypeErrorForUnaryOp(string opSymbol, object x) {
  3479. throw PythonOps.TypeError("unsupported operand type for {0}: '{1}'",
  3480. opSymbol, GetPythonTypeName(x));
  3481. }
  3482. public static Exception TypeErrorForNonIterableObject(object o) {
  3483. return PythonOps.TypeError(
  3484. "argument of type '{0}' is not iterable",
  3485. PythonTypeOps.GetName(o)
  3486. );
  3487. }
  3488. public static Exception TypeErrorForDefaultArgument(string message) {
  3489. return PythonOps.TypeError(message);
  3490. }
  3491. public static Exception AttributeErrorForReadonlyAttribute(string typeName, string attributeName) {
  3492. // CPython uses AttributeError for all attributes except "__class__"
  3493. if (attributeName == "__class__")
  3494. return PythonOps.TypeError("can't delete __class__ attribute");
  3495. return PythonOps.AttributeError("attribute '{0}' of '{1}' object is read-only", attributeName, typeName);
  3496. }
  3497. public static Exception AttributeErrorForBuiltinAttributeDeletion(string typeName, string attributeName) {
  3498. return PythonOps.AttributeError("cannot delete attribute '{0}' of builtin type '{1}'", attributeName, typeName);
  3499. }
  3500. public static Exception MissingInvokeMethodException(object o, string name) {
  3501. if (o is OldClass) {
  3502. throw PythonOps.AttributeError("type object '{0}' has no attribute '{1}'",
  3503. ((OldClass)o).Name, name);
  3504. } else {
  3505. throw PythonOps.AttributeError("'{0}' object has no attribute '{1}'", GetPythonTypeName(o), name);
  3506. }
  3507. }
  3508. /// <summary>
  3509. /// Create at TypeError exception for when Raise() can't create the exception requested.
  3510. /// </summary>
  3511. /// <param name="type">original type of exception requested</param>
  3512. /// <returns>a TypeEror exception</returns>
  3513. internal static Exception MakeExceptionTypeError(object type) {
  3514. return PythonOps.TypeError("exceptions must be classes, or instances, not {0}", PythonTypeOps.GetName(type));
  3515. }
  3516. public static Exception AttributeErrorForObjectMissingAttribute(object obj, string attributeName) {
  3517. if (obj is OldInstance) {
  3518. return AttributeErrorForOldInstanceMissingAttribute(((OldInstance)obj)._class.Name, attributeName);
  3519. } else if (obj is OldClass) {
  3520. return AttributeErrorForOldClassMissingAttribute(((OldClass)obj).Name, attributeName);
  3521. } else {
  3522. return AttributeErrorForMissingAttribute(PythonTypeOps.GetName(obj), attributeName);
  3523. }
  3524. }
  3525. public static Exception AttributeErrorForMissingAttribute(string typeName, string attributeName) {
  3526. return PythonOps.AttributeError("'{0}' object has no attribute '{1}'", typeName, attributeName);
  3527. }
  3528. public static Exception AttributeErrorForOldInstanceMissingAttribute(string typeName, string attributeName) {
  3529. return PythonOps.AttributeError("{0} instance has no attribute '{1}'", typeName, attributeName);
  3530. }
  3531. public static Exception AttributeErrorForOldClassMissingAttribute(string typeName, string attributeName) {
  3532. return PythonOps.AttributeError("class {0} has no attribute '{1}'", typeName, attributeName);
  3533. }
  3534. public static Exception UncallableError(object func) {
  3535. return PythonOps.TypeError("{0} is not callable", PythonTypeOps.GetName(func));
  3536. }
  3537. public static Exception TypeErrorForProtectedMember(Type/*!*/ type, string/*!*/ name) {
  3538. return PythonOps.TypeError("cannot access protected member {0} without a python subclass of {1}", name, NameConverter.GetTypeName(type));
  3539. }
  3540. public static Exception TypeErrorForGenericMethod(Type/*!*/ type, string/*!*/ name) {
  3541. return PythonOps.TypeError("{0}.{1} is a generic method and must be indexed with types before calling", NameConverter.GetTypeName(type), name);
  3542. }
  3543. public static Exception TypeErrorForUnIndexableObject(object o) {
  3544. IPythonObject ipo;
  3545. if (o == null) {
  3546. return PythonOps.TypeError("'NoneType' object cannot be interpreted as an index");
  3547. } else if ((ipo = o as IPythonObject) != null) {
  3548. return TypeError("'{0}' object cannot be interpreted as an index", ipo.PythonType.Name);
  3549. }
  3550. return TypeError("object cannot be interpreted as an index");
  3551. }
  3552. [Obsolete("no longer used anywhere")]
  3553. public static Exception/*!*/ TypeErrorForBadDictionaryArgument(PythonFunction/*!*/ f) {
  3554. return PythonOps.TypeError("{0}() argument after ** must be a dictionary", f.__name__);
  3555. }
  3556. public static T TypeErrorForBadEnumConversion<T>(object value) {
  3557. throw TypeError("Cannot convert numeric value {0} to {1}. The value must be zero.", value, NameConverter.GetTypeName(typeof(T)));
  3558. }
  3559. public static Exception/*!*/ UnreadableProperty() {
  3560. return PythonOps.AttributeError("unreadable attribute");
  3561. }
  3562. public static Exception/*!*/ UnsetableProperty() {
  3563. return PythonOps.AttributeError("readonly attribute");
  3564. }
  3565. public static Exception/*!*/ UndeletableProperty() {
  3566. return PythonOps.AttributeError("undeletable attribute");
  3567. }
  3568. public static Exception Warning(string format, params object[] args) {
  3569. return new WarningException(string.Format(format, args));
  3570. }
  3571. #endregion
  3572. [ThreadStatic]
  3573. private static List<FunctionStack> _funcStack;
  3574. public static List<FunctionStack> GetFunctionStack() {
  3575. return _funcStack ?? (_funcStack = new List<FunctionStack>());
  3576. }
  3577. public static List<FunctionStack> GetFunctionStackNoCreate() {
  3578. return _funcStack;
  3579. }
  3580. public static List<FunctionStack> PushFrame(CodeContext/*!*/ context, FunctionCode function) {
  3581. List<FunctionStack> stack = GetFunctionStack();
  3582. stack.Add(new FunctionStack(context, function));
  3583. return stack;
  3584. }
  3585. internal static LightLambdaExpression ToGenerator(this LightLambdaExpression code, bool shouldInterpret, bool debuggable, int compilationThreshold) {
  3586. return Utils.LightLambda(
  3587. typeof(object),
  3588. code.Type,
  3589. new GeneratorRewriter(code.Name, code.Body).Reduce(shouldInterpret, debuggable, compilationThreshold, code.Parameters, x => x),
  3590. code.Name,
  3591. code.Parameters
  3592. );
  3593. }
  3594. public static void UpdateStackTrace(Exception e, CodeContext context, FunctionCode funcCode, int line) {
  3595. if (line != -1) {
  3596. Debug.Assert(line != SourceLocation.None.Line);
  3597. List<DynamicStackFrame> pyFrames = e.GetFrameList();
  3598. if (pyFrames == null) {
  3599. e.SetFrameList(pyFrames = new List<DynamicStackFrame>());
  3600. }
  3601. var frame = new PythonDynamicStackFrame(context, funcCode, line);
  3602. funcCode.LightThrowCompile(context);
  3603. pyFrames.Add(frame);
  3604. }
  3605. }
  3606. /// <summary>
  3607. /// Gets a list of DynamicStackFrames for the given exception. These stack frames
  3608. /// can be programmatically inspected to understand the frames the exception crossed
  3609. /// through including Python frames.
  3610. ///
  3611. /// Dynamic stack frames are not preserved when an exception crosses an app domain
  3612. /// boundary.
  3613. /// </summary>
  3614. public static DynamicStackFrame[] GetDynamicStackFrames(Exception e) {
  3615. return PythonExceptions.GetDynamicStackFrames(e);
  3616. }
  3617. public static byte[] ConvertBufferToByteArray(PythonBuffer buffer) {
  3618. return buffer.ToString().MakeByteArray();
  3619. }
  3620. public static bool ModuleTryGetMember(CodeContext/*!*/ context, PythonModule module, string name, out object res) {
  3621. object value = module.GetAttributeNoThrow(context, name);
  3622. if (value != OperationFailed.Value) {
  3623. res = value;
  3624. return true;
  3625. }
  3626. res = null;
  3627. return false;
  3628. }
  3629. internal static void ScopeSetMember(CodeContext/*!*/ context, Scope scope, string name, object value) {
  3630. ScopeStorage scopeStorage = ((object)scope.Storage) as ScopeStorage;
  3631. if (scopeStorage != null) {
  3632. scopeStorage.SetValue(name, false, value);
  3633. return;
  3634. }
  3635. PythonOps.SetAttr(context, scope, name, value);
  3636. }
  3637. internal static object ScopeGetMember(CodeContext/*!*/ context, Scope scope, string name) {
  3638. ScopeStorage scopeStorage = ((object)scope.Storage) as ScopeStorage;
  3639. if (scopeStorage != null) {
  3640. return scopeStorage.GetValue(name, false);
  3641. }
  3642. return PythonOps.GetBoundAttr(context, scope, name);
  3643. }
  3644. internal static bool ScopeTryGetMember(CodeContext/*!*/ context, Scope scope, string name, out object value) {
  3645. ScopeStorage scopeStorage = ((object)scope.Storage) as ScopeStorage;
  3646. if (scopeStorage != null) {
  3647. return scopeStorage.TryGetValue(name, false, out value);
  3648. }
  3649. return PythonOps.TryGetBoundAttr(context, scope, name, out value);
  3650. }
  3651. internal static bool ScopeContainsMember(CodeContext/*!*/ context, Scope scope, string name) {
  3652. ScopeStorage scopeStorage = ((object)scope.Storage) as ScopeStorage;
  3653. if (scopeStorage != null) {
  3654. return scopeStorage.HasValue(name, false);
  3655. }
  3656. return PythonOps.HasAttr(context, scope, name);
  3657. }
  3658. internal static bool ScopeDeleteMember(CodeContext/*!*/ context, Scope scope, string name) {
  3659. ScopeStorage scopeStorage = ((object)scope.Storage) as ScopeStorage;
  3660. if (scopeStorage != null) {
  3661. return scopeStorage.DeleteValue(name, false);
  3662. }
  3663. bool res = PythonOps.HasAttr(context, scope, name);
  3664. PythonOps.DeleteAttr(context, scope, name);
  3665. return res;
  3666. }
  3667. internal static IList<object> ScopeGetMemberNames(CodeContext/*!*/ context, Scope scope) {
  3668. ScopeStorage scopeStorage = ((object)scope.Storage) as ScopeStorage;
  3669. if (scopeStorage != null) {
  3670. List<object> res = new List<object>();
  3671. foreach (string name in scopeStorage.GetMemberNames()) {
  3672. res.Add(name);
  3673. }
  3674. var objKeys = ((PythonScopeExtension)context.LanguageContext.EnsureScopeExtension(scope)).ObjectKeys;
  3675. if (objKeys != null) {
  3676. foreach (object o in objKeys.Keys) {
  3677. res.Add(o);
  3678. }
  3679. }
  3680. return res;
  3681. }
  3682. return PythonOps.GetAttrNames(context, scope);
  3683. }
  3684. public static bool IsUnicode(object unicodeObj) {
  3685. return unicodeObj == TypeCache.String;
  3686. }
  3687. public static BuiltinFunction GetUnicodeFuntion() {
  3688. return UnicodeHelper.Function;
  3689. }
  3690. public static bool IsExtensionSet(CodeContext codeContext, int id) {
  3691. return codeContext.ModuleContext.ExtensionMethods.Id == id;
  3692. }
  3693. public static object GetExtensionMethodSet(CodeContext context) {
  3694. return context.ModuleContext.ExtensionMethods;
  3695. }
  3696. }
  3697. /// <summary>
  3698. /// Helper clas for calls to unicode(...). We generate code which checks if unicode
  3699. /// is str and if it is we redirect those calls to the unicode function defined on this
  3700. /// class.
  3701. /// </summary>
  3702. public class UnicodeHelper {
  3703. internal static BuiltinFunction Function = BuiltinFunction.MakeFunction("unicode",
  3704. ArrayUtils.ConvertAll(
  3705. typeof(UnicodeHelper).GetMember("unicode"),
  3706. x => (MethodInfo)x
  3707. ),
  3708. typeof(string)
  3709. );
  3710. public static object unicode(CodeContext context) {
  3711. return String.Empty;
  3712. }
  3713. public static object unicode(CodeContext/*!*/ context, object @string) {
  3714. return StringOps.FastNewUnicode(context, @string);
  3715. }
  3716. public static object unicode(CodeContext/*!*/ context, object @string, object encoding) {
  3717. return StringOps.FastNewUnicode(context, @string, encoding);
  3718. }
  3719. public static object unicode(CodeContext/*!*/ context, object @string, [Optional]object encoding, object errors) {
  3720. return StringOps.FastNewUnicode(context, @string, encoding, errors);
  3721. }
  3722. }
  3723. public struct FunctionStack {
  3724. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes")]
  3725. public readonly CodeContext/*!*/ Context;
  3726. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes")]
  3727. public readonly FunctionCode/*!*/ Code;
  3728. public TraceBackFrame Frame;
  3729. internal FunctionStack(CodeContext/*!*/ context, FunctionCode/*!*/ code) {
  3730. Assert.NotNull(context, code);
  3731. Context = context;
  3732. Code = code;
  3733. Frame = null;
  3734. }
  3735. internal FunctionStack(CodeContext/*!*/ context, FunctionCode/*!*/ code, TraceBackFrame frame) {
  3736. Assert.NotNull(context, code);
  3737. Context = context;
  3738. Code = code;
  3739. Frame = frame;
  3740. }
  3741. internal FunctionStack(TraceBackFrame frame) {
  3742. Context = frame.Context;
  3743. Code = frame.f_code;
  3744. Frame = frame;
  3745. }
  3746. }
  3747. }