Ejemplo n.º 1
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.º 2
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.º 3
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);
            }
        }