Ejemplo n.º 1
0
        public static void SetValue(string attrName, object rawObject, object value)
        {
            var asPyObj = rawObject as PyObject;

            if (asPyObj != null)
            {
                asPyObj.__setattr__(attrName, value);
            }
            else
            {
                try
                {
                    // If it's not a type, then get its type. If it's a type, we have the reflection information already.
                    // This became an issue when we had to resolve stuff being imported from assemblies.
                    var objType = rawObject as Type;
                    if (objType == null)
                    {
                        objType = rawObject.GetType();
                    }

                    // Try it as a field and then as a property.
                    var member = objType.GetMember(attrName);
                    if (member.Length == 0)
                    {
                        // We have a catch for ArgumentException but it also looks like GetMember will just return an empty list if the attribute is not found.
                        throw new EscapedPyException(new AttributeError("'" + objType.Name + "' object has no attribute named '" + attrName + "'"));
                    }
                    if (member[0].MemberType == System.Reflection.MemberTypes.Property)
                    {
                        var property = objType.GetProperty(attrName);
                        property.SetValue(rawObject, PyNetConverter.Convert(value, property.PropertyType));
                    }
                    else if (member[0].MemberType == System.Reflection.MemberTypes.Field)
                    {
                        var field = objType.GetField(attrName);
                        field.SetValue(rawObject, PyNetConverter.Convert(value, field.FieldType));
                    }
                    else if (member[0].MemberType == System.Reflection.MemberTypes.Method)
                    {
                        throw new EscapedPyException(new AttributeError("'" + objType.Name + "' is a .NET object and its methods cannot be reassigned"));
                    }
                    else if (member[0].MemberType == System.Reflection.MemberTypes.Event)
                    {
                        throw new EscapedPyException(new AttributeError("'" + objType.Name + "' is a .NET object and its events cannot be reassigned"));
                    }
                    else
                    {
                        throw new EscapedPyException(new NotImplemented("'" + objType.Name + "' object attribute named '" + attrName + "' is neither a field, method, event, nor property."));
                    }
                }
                catch (ArgumentException e)
                {
                    throw new EscapedPyException(new AttributeError(e.Message));
                }
            }
        }
Ejemplo n.º 2
0
 public static PyString str_builtin(object o)
 {
     if (PyNetConverter.CanConvert(o.GetType(), typeof(PyString)))
     {
         return((PyString)PyNetConverter.Convert(o, typeof(PyString)));
     }
     else
     {
         throw new InvalidCastException("Cannot convert " + o.GetType() + " to a string.");
     }
 }
Ejemplo n.º 3
0
        private static void StoreSubscriptArray(Array asArray, int arrayIndex, object rawValue)
        {
            var value     = rawValue;
            var arrayType = asArray.GetType().GetElementType();

            value = PyNetConverter.Convert(value, arrayType);

            if (arrayIndex < 0)
            {
                // Allow negative indexing, which only works for one wrap around the array.
                arrayIndex = asArray.Length - arrayIndex;
            }

            if (arrayIndex < 0 || arrayIndex >= asArray.Length)
            {
                throw new Exception("IndexError: list index out of range");
            }
            asArray.SetValue(value, arrayIndex);
        }
Ejemplo n.º 4
0
        private static void StoreSubscriptIList(IList container, object index, object value)
        {
            int intIndex    = GetIntIndex(index);
            var listType    = container.GetType();
            var assignValue = value;

            if (listType.IsGenericType)
            {
                if (PyNetConverter.CanConvert(value.GetType(), listType.GenericTypeArguments[0]))
                {
                    assignValue = PyNetConverter.Convert(value, listType.GenericTypeArguments[0]);
                }
                else
                {
                    throw new Exception("Cannot subscript store type " + value.GetType().Name + " to generic list with type " + listType.GenericTypeArguments[0]);
                }
            }
            container[intIndex] = assignValue;
        }
Ejemplo n.º 5
0
        private static void StoreSubscriptIDict(IDictionary container, object index, object rawValue)
        {
            var key      = index;
            var value    = rawValue;
            var dictType = container.GetType();

            if (dictType.IsGenericType)
            {
                var keyType   = dictType.GenericTypeArguments[0];
                var valueType = dictType.GenericTypeArguments[1];
                if (PyNetConverter.CanConvert(key.GetType(), keyType))
                {
                    key = PyNetConverter.Convert(key, keyType);
                }
                else
                {
                    throw new Exception("Cannot subscript store with key " + key.GetType().Name + " to generic dictionary with key type " + keyType);
                }

                if (PyNetConverter.CanConvert(value.GetType(), valueType))
                {
                    value = PyNetConverter.Convert(value, valueType);
                }
                else
                {
                    throw new Exception("Cannot subscript store with value " + value.GetType().Name + " to generic dictionary with value type " + valueType);
                }
            }

            try
            {
                container[key] = value;
            }
            catch (KeyNotFoundException)
            {
                throw new Exception("KeyError: " + index);
            }
        }