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

/DICK.B1/IronPython/Runtime/ObjectAttributesAdapter.cs

https://bitbucket.org/williamybs/uidipythontool
C# | 91 lines | 63 code | 13 blank | 15 comment | 0 complexity | fa1b396e136319e7cebb2af0bd49778d MD5 | raw file
  1. /* ****************************************************************************
  2. *
  3. * Copyright (c) Microsoft Corporation.
  4. *
  5. * This source code is subject to terms and conditions of the Microsoft Public License. A
  6. * copy of the license can be found in the License.html file at the root of this distribution. If
  7. * you cannot locate the Microsoft Public License, please send an email to
  8. * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. * by the terms of the Microsoft Public License.
  10. *
  11. * You must not remove this notice, or any other, from this software.
  12. *
  13. *
  14. * ***************************************************************************/
  15. using System.Collections.Generic;
  16. using IronPython.Runtime.Operations;
  17. using Microsoft.Scripting;
  18. using Microsoft.Scripting.Runtime;
  19. namespace IronPython.Runtime {
  20. class ObjectAttributesAdapter : DictionaryStorage {
  21. private readonly object _backing;
  22. private readonly CodeContext/*!*/ _context;
  23. public ObjectAttributesAdapter(CodeContext/*!*/ context, object backing) {
  24. _backing = backing;
  25. _context = context;
  26. }
  27. internal object Backing {
  28. get {
  29. return _backing;
  30. }
  31. }
  32. public override void Add(object key, object value) {
  33. PythonContext.GetContext(_context).SetIndex(_backing, key, value);
  34. }
  35. public override bool Contains(object key) {
  36. object dummy;
  37. return TryGetValue(key, out dummy);
  38. }
  39. public override bool Remove(object key) {
  40. try {
  41. PythonContext.GetContext(_context).DelIndex(_backing, key);
  42. return true;
  43. } catch (KeyNotFoundException) {
  44. ExceptionHelpers.DynamicStackFrames = null;
  45. return false;
  46. }
  47. }
  48. public override bool TryGetValue(object key, out object value) {
  49. try {
  50. value = PythonOps.GetIndex(_context, _backing, key);
  51. return true;
  52. } catch (KeyNotFoundException) {
  53. // return false
  54. ExceptionHelpers.DynamicStackFrames = null;
  55. }
  56. value = null;
  57. return false;
  58. }
  59. public override int Count {
  60. get { return PythonOps.Length(_backing); }
  61. }
  62. public override void Clear() {
  63. PythonOps.Invoke(_context, _backing, "clear");
  64. }
  65. public override List<KeyValuePair<object, object>> GetItems() {
  66. List<KeyValuePair<object, object>> res = new List<KeyValuePair<object, object>>();
  67. foreach (object o in Keys) {
  68. object val;
  69. TryGetValue(o, out val);
  70. res.Add(new KeyValuePair<object, object>(o, val));
  71. }
  72. return res;
  73. }
  74. private ICollection<object> Keys {
  75. get { return (ICollection<object>)Converter.Convert(PythonOps.Invoke(_context, _backing, "keys"), typeof(ICollection<object>)); }
  76. }
  77. }
  78. }