/DICK.B1/IronPython/Runtime/ObjectAttributesAdapter.cs
C# | 91 lines | 63 code | 13 blank | 15 comment | 0 complexity | fa1b396e136319e7cebb2af0bd49778d MD5 | raw file
- /* ****************************************************************************
- *
- * Copyright (c) Microsoft Corporation.
- *
- * This source code is subject to terms and conditions of the Microsoft Public License. A
- * copy of the license can be found in the License.html file at the root of this distribution. If
- * you cannot locate the Microsoft Public License, please send an email to
- * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
- * by the terms of the Microsoft Public License.
- *
- * You must not remove this notice, or any other, from this software.
- *
- *
- * ***************************************************************************/
-
- using System.Collections.Generic;
- using IronPython.Runtime.Operations;
- using Microsoft.Scripting;
- using Microsoft.Scripting.Runtime;
-
- namespace IronPython.Runtime {
- class ObjectAttributesAdapter : DictionaryStorage {
- private readonly object _backing;
- private readonly CodeContext/*!*/ _context;
-
- public ObjectAttributesAdapter(CodeContext/*!*/ context, object backing) {
- _backing = backing;
- _context = context;
- }
-
- internal object Backing {
- get {
- return _backing;
- }
- }
-
- public override void Add(object key, object value) {
- PythonContext.GetContext(_context).SetIndex(_backing, key, value);
- }
-
- public override bool Contains(object key) {
- object dummy;
- return TryGetValue(key, out dummy);
- }
-
- public override bool Remove(object key) {
- try {
- PythonContext.GetContext(_context).DelIndex(_backing, key);
- return true;
- } catch (KeyNotFoundException) {
- ExceptionHelpers.DynamicStackFrames = null;
- return false;
- }
- }
-
- public override bool TryGetValue(object key, out object value) {
- try {
- value = PythonOps.GetIndex(_context, _backing, key);
- return true;
- } catch (KeyNotFoundException) {
- // return false
- ExceptionHelpers.DynamicStackFrames = null;
- }
- value = null;
- return false;
- }
-
- public override int Count {
- get { return PythonOps.Length(_backing); }
- }
-
- public override void Clear() {
- PythonOps.Invoke(_context, _backing, "clear");
- }
-
- public override List<KeyValuePair<object, object>> GetItems() {
- List<KeyValuePair<object, object>> res = new List<KeyValuePair<object, object>>();
- foreach (object o in Keys) {
- object val;
- TryGetValue(o, out val);
-
- res.Add(new KeyValuePair<object, object>(o, val));
- }
- return res;
- }
-
- private ICollection<object> Keys {
- get { return (ICollection<object>)Converter.Convert(PythonOps.Invoke(_context, _backing, "keys"), typeof(ICollection<object>)); }
- }
- }
- }