示例#1
0
 public IEnumerable <PyType> GetBaseTypes(Type type, IList <PyType> existingBases)
 {
     if (type == typeof(InheritanceTestBaseClassWrapper))
     {
         return(new[] { PyType.Get(type.BaseType), ExtraBase });
     }
     return(existingBases);
 }
示例#2
0
        private static bool IsList(PyType objectType)
        {
            //TODO accept any python object that implements the sequence and list protocols
            //must implement sequence protocol to fully implement list protocol
            //if (!SequenceDecoder.IsSequence(objectType)) return false;

            //returns wheter the type is a list.
            return(PythonReferenceComparer.Instance.Equals(objectType, Runtime.PyListType));
        }
示例#3
0
 public static IntPtr DoThrowSimple()
 {
     using (Py.GIL())
     {
         dynamic builtins      = Py.Import("builtins");
         var     typeErrorType = new PyType(builtins.TypeError);
         var     pyerr         = new PythonException(typeErrorType, value: null, traceback: null, "Type error, the first", innerException: null);
         throw new ArgumentException("Bogus bad parameter", pyerr);
     }
 }
示例#4
0
        public PyDynamic(PyType type, Scene.Entity3D ent)
        {
            Sent = ent;
            PhysicsManager.AddObj(this);
            Mat = PhysicsManager.py.CreateMaterial(0.7f, 0.7f, 0.1f);
            switch (type)
            {
            case PyType.Box:
                CreateBox(ent);
                break;

                break;
            }
        }
示例#5
0
        internal static bool IsSequence(PyType objectType)
        {
            //must implement iterable protocol to fully implement sequence protocol
            if (!IterableDecoder.IsIterable(objectType))
            {
                return(false);
            }

            //returns wheter it implements the sequence protocol
            //according to python doc this needs to exclude dict subclasses
            //but I don't know how to look for that given the objectType
            //rather than the instance.
            return(objectType.HasAttr("__getitem__"));
        }
示例#6
0
        /// <summary>
        ///     Returns the size of the given type (tuple, otherwise list)
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public int Size(PyType type)
        {
            if (!IsValid)
            {
                return(-1);
            }

            try
            {
                var getSize = (type == PyType.TupleType || type == PyType.DerivedTupleType) ? (Func <IntPtr, int>)Py.PyTuple_Size : Py.PyList_Size;
                return(getSize(this));
            }
            finally
            {
                HandlePythonError();
            }
        }
示例#7
0
        /// <summary>
        ///     Returns a list item from the current Python object
        /// </summary>
        /// <param name="index"></param>
        /// <param name="type">Force the PyType to List or Tuple</param>
        /// <returns></returns>
        public PyObject Item(int index, PyType type)
        {
            if (!IsValid)
            {
                return(PySharp.PyZero);
            }

            var getItem = (type == PyType.TupleType || type == PyType.DerivedTupleType) ? (Func <IntPtr, int, IntPtr>)Py.PyTuple_GetItem : Py.PyList_GetItem;

            PyObject result;

            if (!_itemCache.TryGetValue(index, out result))
            {
                result            = new PyObject(_pySharp, getItem(this, index), false);
                _itemCache[index] = result;
            }
            return(result);
        }
示例#8
0
        /// <summary>
        /// Gets a concrete instance of <see cref="IPyObjectDecoder"/>
        /// (potentially selecting one from a collection),
        /// that can decode from <paramref name="objectType"/> to <paramref name="targetType"/>,
        /// or <c>null</c> if a matching decoder can not be found.
        /// </summary>
        public static IPyObjectDecoder GetDecoder(
            this IPyObjectDecoder decoder,
            PyType objectType, Type targetType)
        {
            if (decoder is null)
            {
                throw new ArgumentNullException(nameof(decoder));
            }

            if (decoder is IEnumerable <IPyObjectDecoder> composite)
            {
                return(composite
                       .Select(nestedDecoder => nestedDecoder.GetDecoder(objectType, targetType))
                       .FirstOrDefault(d => d != null));
            }

            return(decoder.CanDecode(objectType, targetType) ? decoder : null);
        }
示例#9
0
        /// <summary>
        ///     Clear this PyObject (the PyObject must be a List or Dictionary)
        /// </summary>
        /// <param name="pyType">Force this Python Type</param>
        /// <returns></returns>
        /// <remarks>
        ///     For a list attribute, the list is cleared
        ///     For a Dictionary attribute, the dictionary is cleared
        /// </remarks>
        public bool Clear(PyType pyType)
        {
            try
            {
                switch (pyType)
                {
                case PyType.ListType:
                case PyType.DerivedListType:
                    return(Py.PyList_SetSlice(this, 0, Size() - 1, PySharp.PyZero) == 0);

                case PyType.DictType:
                case PyType.DictProxyType:
                case PyType.DerivedDictType:
                case PyType.DerivedDictProxyType:
                    return(ToDictionary().All(item => Py.PyDict_DelItem(this, item.Key) == 0));
                }

                return(false);
            }
            finally
            {
                HandlePythonError();
            }
        }
示例#10
0
 public bool CanDecode(PyType objectType, Type targetType)
 {
     return(targetType.IsEnum &&
            objectType.IsSubclass(Runtime.PyLongType));
 }
示例#11
0
 public bool CanDecode(PyType objectType, Type targetType)
 {
     return(targetType == typeof(DateTime));
 }
示例#12
0
 public bool CanDecode(PyType objectType, Type targetType)
 => PythonReferenceComparer.Instance.Equals(objectType, TheOnlySupportedSourceType) &&
 targetType == typeof(TTarget);
示例#13
0
 public bool CanDecode(PyType objectType, Type targetType)
 => PythonReferenceComparer.Instance.Equals(PyErr, objectType);
示例#14
0
 public bool CanDecode(PyType objectType, Type targetType)
 => this.CanEncode(targetType) &&
 PythonReferenceComparer.Instance.Equals(objectType, PythonEngine.Eval("ValueError"));
示例#15
0
 public bool CanDecode(PyType objectType, Type targetType)
 {
     return(targetType.IsAssignableFrom(typeof(EverythingElseToSelfDecoder)));
 }
示例#16
0
 public bool CanDecode(PyType objectType, Type targetType)
 => objectType.Handle == Runtime.PyTupleType && this.CanEncode(targetType);
示例#17
0
 public bool CanDecode(PyType objectType, Type targetType)
 {
     return(IsIterable(objectType) && IsIterable(targetType));
 }
示例#18
0
 internal static bool IsIterable(PyType objectType)
 {
     return(objectType.HasAttr("__iter__"));
 }
示例#19
0
 public bool CanDecode(PyType objectType, Type targetType)
 => objectType.Handle == TheOnlySupportedSourceType.Handle &&
 targetType == typeof(TTarget);
示例#20
0
 public bool CanDecode(PyType objectType, Type targetType)
 {
     return(IsSequence(objectType) && IsSequence(targetType));
 }
示例#21
0
 /// <inheritdoc />
 public bool CanDecode(PyType objectType, Type targetType)
 => this.decoders.Any(decoder => decoder.CanDecode(objectType, targetType));
示例#22
0
 public bool CanDecode(PyType objectType, Type targetType)
 {
     var type = objectType.ToString();
     return _decoders.ContainsKey(type);
 }
示例#23
0
 public bool CanDecode(PyType objectType, Type targetType)
 {
     return(IsList(objectType) && IsList(targetType));
 }
示例#24
0
 public bool CanDecode(PyType objectType, Type targetType)
 {
     return(targetType.IsEnum &&
            objectType.IsSubclass(new BorrowedReference(Runtime.PyLongType)));
 }
示例#25
0
        /// <summary>
        /// Create an instance of <see cref="ColumnInfo"/>
        /// </summary>
        /// <param name="utils">Python utilities</param>
        /// <param name="name">Column name</param>
        /// <param name="pyType">Column type</param>
        public ColumnInfo(PythonUtils utils, string name, PyType pyType)
        {
            _utils = utils ?? throw new ArgumentNullException(nameof(utils));
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentException("Name can't be null or empty", nameof(name));
            }

            Name       = name;
            PythonType = pyType;
            switch (PythonType)
            {
            case PyType.Undefined:
                ManagedType = typeof(string);
                break;

            case PyType.BoolType:
                ManagedType = typeof(bool);
                break;

            case PyType.Int8Type:
                ManagedType = typeof(sbyte);
                break;

            case PyType.Uint8Type:
                ManagedType = typeof(byte);
                break;

            case PyType.Int16Type:
                ManagedType = typeof(short);
                break;

            case PyType.Uint16Type:
                ManagedType = typeof(ushort);
                break;

            case PyType.Uint32Type:
                ManagedType = typeof(uint);
                break;

            case PyType.Int64Type:
                ManagedType = typeof(long);
                break;

            case PyType.Uint64Type:
                ManagedType = typeof(ulong);
                break;

            case PyType.Int32Type:
                ManagedType = typeof(int);
                break;

            case PyType.Float16Type:
                ManagedType = typeof(float);
                break;

            case PyType.Float32Type:
                ManagedType = typeof(float);
                break;

            case PyType.Float64Type:
                ManagedType = typeof(double);
                break;

            case PyType.Complex128Type:
                ManagedType = typeof(string);
                break;

            case PyType.Complex64Type:
                ManagedType = typeof(string);
                break;

            case PyType.ObjectType:
                ManagedType = typeof(string);
                break;

            case PyType.Datetime64Type:
                ManagedType = typeof(DateTime);
                break;

            case PyType.Timedelta64Type:
                ManagedType = typeof(TimeSpan);
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
示例#26
0
 public bool CanDecode(PyType objectType, Type targetType)
 => PythonReferenceComparer.Instance.Equals(objectType, Runtime.PyTupleType) &&
 this.CanEncode(targetType);
示例#27
0
 /// <summary>
 /// Create slots holder for holding the delegate of slots and be able  to reset them.
 /// </summary>
 /// <param name="type">Steals a reference to target type</param>
 public SlotsHolder(PyType type)
 {
     this.Type = type;
 }